I need to convert IPv6 from string like "fe80::dd99:5d56:cf09:b1ef" to Binary or to (Word64,Word64). Closest I came to is Network.IP.Addr package, but I'm a newbie and this signature is impenetrable for me:
net6Parser :: (CharParsing μ, Monad μ, IsNetAddr n, NetHost n ~ IP6) => μ n
Can someone please provide an example how to use it, or recommend any other package?
The network-ip
package appears to use the textual
type class and its related package. Looking at that class's documentation, I see mention of an obvious helper fromString
that sounds good. A quick test suggests this is what you're looking for:
> import Data.Textual
> import Network.IP.Addr
> fromString "fe80::dd99:5d56:cf09:b1ef" :: Maybe IP6
Just (ip6FromWords 0xfe80 0x0 0x0 0x0 0xdd99 0x5d56 0xcf09 0xb1ef)
So that works fine. Now how do we get those 8 Word16
values into a couple Word64
s? The most efficient way is to use fromIntegral
, bit shifts, and logical OR. I'm feeling lazy so lets just serialize it into a bytestring then deserialize it as two Word64s. No promise that I have the endian right but...
> import Data.Binary
> let x = it -- the prior result, the IPv6
> fmap (decode . encode) it :: Maybe (Word64,Word64)
Just (18338657682652659712,15967896581240893935)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With