diff --git a/cbits/byteorder.c b/cbits/byteorder.c deleted file mode 100644 index ca7058a..0000000 --- a/cbits/byteorder.c +++ /dev/null @@ -1,21 +0,0 @@ -/* Sigh. According to the docs for , htonl() and friends may be - implemented as macros only, so I have to re-export functions here to do the - conversions. */ - -#include - -uint32_t iostreams_htonl(uint32_t hostlong) { - return htonl(hostlong); -} - -uint16_t iostreams_htons(uint16_t hostshort) { - return htons(hostshort); -} - -uint32_t iostreams_ntohl(uint32_t netlong) { - return ntohl(netlong); -} - -uint16_t iostreams_ntohs(uint16_t netshort) { - return ntohs(netshort); -} diff --git a/io-streams-haproxy.cabal b/io-streams-haproxy.cabal index 85c99e9..e0e2b55 100644 --- a/io-streams-haproxy.cabal +++ b/io-streams-haproxy.cabal @@ -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 @@ -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, @@ -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 @@ -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, diff --git a/src/System/IO/Streams/Network/HAProxy.hs b/src/System/IO/Streams/Network/HAProxy.hs index 545f9ac..c0b1443 100644 --- a/src/System/IO/Streams/Network/HAProxy.hs +++ b/src/System/IO/Streams/Network/HAProxy.hs @@ -1,9 +1,10 @@ {-# LANGUAGE BangPatterns #-} {-# LANGUAGE DeriveDataTypeable #-} -{-# LANGUAGE ForeignFunctionInterface #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE Trustworthy #-} {-# LANGUAGE TupleSections #-} +{-# LANGUAGE CApiFFI #-} +{-# LANGUAGE CPP #-} {-| @@ -35,6 +36,8 @@ module System.IO.Streams.Network.HAProxy , getSocketType ) where +#include "MachDeps.h" + ------------------------------------------------------------------------------ import Control.Applicative ((<$>), (<|>)) import Control.Monad (void, when) @@ -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 @@ -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