Skip to content

Simplify byteswapping primitives #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions cbits/byteorder.c

This file was deleted.

9 changes: 3 additions & 6 deletions io-streams-haproxy.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ copyright: (c) 2014 Google, Inc. and CONTRIBUTORS
category: Network, IO-Streams
build-type: Simple
extra-source-files:
CONTRIBUTORS,
cbits/byteorder.c
CONTRIBUTORS

cabal-version: >=1.10
Bug-Reports: https://github.com/snapframework/io-streams-haproxy/issues
Expand All @@ -28,9 +27,8 @@ library
ghc-prof-options: -prof -auto-all
exposed-modules: System.IO.Streams.Network.HAProxy
other-modules: System.IO.Streams.Network.Internal.Address
c-sources: cbits/byteorder.c

build-depends: base >= 4.4 && < 4.8,
build-depends: base >= 4.5 && < 4.8,
attoparsec >= 0.7 && < 0.13,
bytestring >= 0.9 && < 0.11,
io-streams >= 1.1 && < 1.3,
Expand All @@ -47,7 +45,6 @@ test-suite testsuite
hs-source-dirs: src test
Main-is: TestSuite.hs
Default-language: Haskell2010
c-sources: cbits/byteorder.c

ghc-options: -Wall -fhpc -fwarn-tabs -funbox-strict-fields -threaded
-fno-warn-unused-do-bind
Expand All @@ -56,7 +53,7 @@ test-suite testsuite
Other-modules: System.IO.Streams.Network.HAProxy,
System.IO.Streams.Network.HAProxy.Tests

build-depends: base >= 4.4 && < 4.8,
build-depends: base >= 4.5 && < 4.8,
attoparsec >= 0.7 && < 0.13,
bytestring >= 0.9 && < 0.11,
io-streams >= 1.1 && < 1.3,
Expand Down
28 changes: 22 additions & 6 deletions src/System/IO/Streams/Network/HAProxy.hs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE CApiFFI #-}
{-# LANGUAGE CPP #-}

{-|

Expand Down Expand Up @@ -35,6 +36,8 @@ module System.IO.Streams.Network.HAProxy
, getSocketType
) where

#include "MachDeps.h"

------------------------------------------------------------------------------
import Control.Applicative ((<$>), (<|>))
import Control.Monad (void, when)
Expand All @@ -46,7 +49,9 @@ import Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as S
import qualified Data.ByteString.Unsafe as S
import Data.Word (Word16, Word32, Word8)
import Foreign.C.Types (CUInt (..), CUShort (..))
#if MIN_VERSION_base(4,7,0)
import Data.Word (byteSwap16, byteSwap32)
#endif
import Foreign.Ptr (castPtr)
import Foreign.Storable (peek)
import qualified Network.Socket as N
Expand Down Expand Up @@ -323,14 +328,25 @@ parseNewHaProxy localProxyInfo = do

toUnixPath = S.unpack . fst . S.break (=='\x00')

foreign import ccall unsafe "iostreams_ntohs" c_ntohs :: CUShort -> CUShort
foreign import ccall unsafe "iostreams_ntohl" c_ntohl :: CUInt -> CUInt

#if WORDS_BIGENDIAN
ntohs :: Word16 -> Word16
ntohs = id

ntohl :: Word32 -> Word32
ntohl = id
#elif MIN_VERSION_base(4,7,0)
ntohs :: Word16 -> Word16
ntohs = fromIntegral . c_ntohs . fromIntegral
ntohs = byteSwap16

ntohl :: Word32 -> Word32
ntohl = fromIntegral . c_ntohl . fromIntegral
ntohl = byteSwap32
#else
-- uint16_t ntohs(uint16_t netshort);
foreign import capi unsafe "arpa/inet.h ntohs" ntohs :: Word16 -> Word16
-- uint32_t ntohl(uint32_t netlong);
foreign import capi unsafe "arpa/inet.h ntohl" ntohl :: Word32 -> Word32
#endif

snarf32 :: Parser Word32
snarf32 = do
Expand Down