avoid using htonl

It got removed from network-3.0.0.0 and nothing in the haskell ecosystem
currently provides it (which seems it ought to be fixed).

Tested new code on both little-endian and big-endian with:

ghci> hostAddressToTuple $ fromJust $ embeddedIpv4 (0,0,0,0,0,0xffff,0x7f00,1)
(127,0,0,1)
This commit is contained in:
Joey Hess 2019-02-19 12:17:20 -04:00
parent fe9bfa8157
commit 4603713b4e
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38

View file

@ -11,6 +11,7 @@ import Utility.Exception
import Network.Socket
import Data.Word
import Data.Memory.Endian
import Control.Applicative
import Prelude
@ -67,8 +68,18 @@ embeddedIpv4 v = case v of
(0x64,0xff9b,0,0,0,0,a,b) -> Just (toipv4 a b)
_ -> Nothing
where
toipv4 a b = htonl $ fromIntegral a * (2^halfipv4bits) + fromIntegral b
halfipv4bits = 16 :: Word32
toipv4 a b =
let n = fromIntegral a * (2^halfipv4bits) + fromIntegral b
-- HostAddress is in network byte order, but n is using host
-- byte order so needs to be swapped.
-- Could just use htonl n, but it's been dropped from the
-- network library, so work around by manually swapping.
in case getSystemEndianness of
LittleEndian ->
let (b1, b2, b3, b4) = hostAddressToTuple n
in tupleToHostAddress (b4, b3, b2, b1)
BigEndian -> n
{- Given a string containing an IP address, make a function that will
- match that address in a SockAddr. Nothing when the address cannot be