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:
parent
fe9bfa8157
commit
4603713b4e
1 changed files with 12 additions and 1 deletions
|
@ -11,6 +11,7 @@ import Utility.Exception
|
||||||
|
|
||||||
import Network.Socket
|
import Network.Socket
|
||||||
import Data.Word
|
import Data.Word
|
||||||
|
import Data.Memory.Endian
|
||||||
import Control.Applicative
|
import Control.Applicative
|
||||||
import Prelude
|
import Prelude
|
||||||
|
|
||||||
|
@ -67,8 +68,18 @@ embeddedIpv4 v = case v of
|
||||||
(0x64,0xff9b,0,0,0,0,a,b) -> Just (toipv4 a b)
|
(0x64,0xff9b,0,0,0,0,a,b) -> Just (toipv4 a b)
|
||||||
_ -> Nothing
|
_ -> Nothing
|
||||||
where
|
where
|
||||||
toipv4 a b = htonl $ fromIntegral a * (2^halfipv4bits) + fromIntegral b
|
|
||||||
halfipv4bits = 16 :: Word32
|
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
|
{- Given a string containing an IP address, make a function that will
|
||||||
- match that address in a SockAddr. Nothing when the address cannot be
|
- match that address in a SockAddr. Nothing when the address cannot be
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue