parser package:ip

Parse an IPv4 address using a Parser.
>>> AT.parseOnly IPv4.parser "192.168.2.47"
Right (ipv4 192 168 2 47)
>>> AT.parseOnly IPv4.parser "192.168.2.470"
Left "Failed reading: All octets in an IPv4 address must be between 0 and 255"
Parse an IPv6 using Parser.
>>> Atto.parseOnly IPv6.parser (Text.pack "dead:beef:3240:a426:ba68:1cd0:4263:109b")
Right (ipv6 0xdead 0xbeef 0x3240 0xa426 0xba68 0x1cd0 0x4263 0x109b)
Parse a Mac address using a Parser.
>>> AT.parseOnly Mac.parser (Text.pack "a4:7f:24:7a:b4:23")
Right (mac 0xa47f247ab423)
>>> AT.parseOnly Mac.parser (Text.pack "a47-f24-7ab-423")
Left "':': Failed reading: satisfy"
Parse UTF-8-encoded Bytes as an IP address.
Parse an IPv4Range using a Parser.
>>> AT.parseOnly IPv4.parserRange "192.168.25.254/16"
Right (IPv4Range {ipv4RangeBase = ipv4 192 168 0 0, ipv4RangeLength = 16})
Parse UTF-8-encoded Bytes into an IPv4Range. This requires the mask to be present.
>>> maybe (putStrLn "nope") IPv4.printRange $ Parser.parseBytesMaybe (IPv4.parserRangeUtf8Bytes ()) (Ascii.fromString "192.168.0.0/16")
192.168.0.0/16

>>> maybe (putStrLn "nope") IPv4.printRange $ Parser.parseBytesMaybe (IPv4.parserRangeUtf8Bytes ()) (Ascii.fromString "10.10.10.1")
nope
See parserRangeUtf8BytesLenient for a variant that treats a missing mask as a /32 mask.
Variant of parserRangeUtf8Bytes that allows the mask to be omitted. An omitted mask is treated as a /32 mask.
>>> maybe (putStrLn "nope") IPv4.printRange $ Parser.parseBytesMaybe (IPv4.parserRangeUtf8BytesLenient ()) (Ascii.fromString "192.168.0.0/16")
192.168.0.0/16

>>> maybe (putStrLn "nope") IPv4.printRange $ Parser.parseBytesMaybe (IPv4.parserRangeUtf8BytesLenient ()) (Ascii.fromString "10.10.10.1")
10.10.10.1/32
Parse an IPv4 using a Parser.
>>> AB.parseOnly IPv4.parserUtf8 "192.168.2.47"
Right (ipv4 192 168 2 47)
>>> AB.parseOnly IPv4.parserUtf8 "192.168.2.470"
Left "Failed reading: All octets in an ipv4 address must be between 0 and 255"
Parse UTF-8-encoded Bytes as an IPv4 address.
>>> Parser.parseBytes (IPv4.parserUtf8Bytes ()) (Ascii.fromString "10.0.1.254")
Success (Slice {offset = 10, length = 0, value = ipv4 10 0 1 254})
Variant of parserUtf8Bytes with unboxed result type.
Parse an IPv6Range using a Parser.
Parse UTF-8-encoded Bytes into an IPv4Range. This requires the mask to be present.
>>> maybe (putStrLn "nope") IPv6.printRange $ Parser.parseBytesMaybe (IPv6.parserRangeUtf8Bytes ()) (Ascii.fromString "1b02:f001:5:200b::/80")
1b02:f001:5:200b::/80

>>> maybe (putStrLn "nope") IPv6.printRange $ Parser.parseBytesMaybe (IPv6.parserRangeUtf8Bytes ()) (Ascii.fromString "abcd::")
nope
See parserRangeUtf8BytesLenient for a variant that treats a missing mask as a /32 mask.
Variant of parserRangeUtf8Bytes that allows the mask to be omitted. An omitted mask is treated as a /128 mask.
>>> maybe (putStrLn "nope") IPv6.printRange $ Parser.parseBytesMaybe (IPv6.parserRangeUtf8BytesLenient ()) (Ascii.fromString "1b02:f001:5:200b::/80")
1b02:f001:5:200b::/80

>>> maybe (putStrLn "nope") IPv6.printRange $ Parser.parseBytesMaybe (IPv6.parserRangeUtf8BytesLenient ()) (Ascii.fromString "abcd::")
abcd::/128
Parse UTF-8-encoded Bytes as an IPv6 address. This accepts both uppercase and lowercase characters in the hexadecimal components.
>>> let str = "dead:beef:3240:a426:ba68:1cd0:4263:109b -> alive"

>>> Parser.parseBytes (parserUtf8Bytes ()) (Ascii.fromString str)
Success (Slice {offset = 39, length = 9, value = ipv6 0xdead 0xbeef 0x3240 0xa426 0xba68 0x1cd0 0x4263 0x109b})
This does not currently support parsing embedded IPv4 address (e.g. ff00:8000:abc::224.1.2.3).
Lenient parser for a Mac address using any character as the separator and accepting any digit grouping (i.e. FA:43:B2:C0:0F:99 or A065.647B.87FA).
Leniently parse UTF-8-encoded Bytes as a Mac address. This is case insensitive and allows either : or - as the separator. It also allows leading zeroes to be missing.
>>> Parser.parseBytes (Mac.parserUtf8Bytes ()) (Ascii.fromString "de:ad:BE:EF:1:23")
Success (Slice {offset = 16, length = 0, value = mac 0xdeadbeef0123})
Parser a Mac address using the given MacCodec.
>>> p1 = Mac.parserWith Mac.defCodec

>>> AT.parseOnly p1 (Text.pack "a4:7f:24:7a:b4:23")
Right (mac 0xa47f247ab423)
>>> p2 = Mac.parserWith (Mac.MacCodec Mac.MacGroupingNoSeparator False)

>>> AT.parseOnly p2 (Text.pack "a47f247ab423")
Right (mac 0xa47f247ab423)
Parser for a Mac address using the provided settings.