IPv6 address parsing

I need to convert IPv6 from a string like "fe80 :: dd99: 5d56: cf09: b1ef" to binary or to (Word64, Word64). The closest I came to the Network.IP.Addr package, but I'm new, and this signature is impenetrable to me:

net6Parser :: (CharParsing μ, Monad μ, IsNetAddr n, NetHost n ~ IP6) => μ n

Can someone provide a usage example or recommend some other package?

+4
source share
2 answers

The package network-ipuses the type class textualand its associated package. Looking at the documentation for this class, I see a mention of an obvious helper fromStringthat sounds good. A quick test shows that this is what you are 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)

, . , 8 Word16 Word64 s? fromIntegral, . , bytestring, Word64. , , ...

> import Data.Binary
> let x = it -- the prior result, the IPv6
> fmap (decode . encode) it :: Maybe (Word64,Word64)
Just (18338657682652659712,15967896581240893935)
+3

, getAddrInfo, , :

fromSockAddr :: SockAddr -> (Word64, Word64)
fromSockAddr a = case a of
               -- SockAddrInet pn ha (IPV4 not needed)
               SockAddrInet6 pn fi ha sid -> decode $ encode ha :: (Word64,Word64)
0

All Articles