range package:ip
Smart constructor for
IPv4Range. Ensures the mask is
appropriately sized and sets masked bits in the
IPv4 to zero.
Smart constructor for
IPv6Range. Ensures the mask is
appropriately sized and sets masked bits in the
IPv6 to zero.
>>> let addr = IPv6.ipv6 0xDEAD 0xBEEF 0x3240 0xA426 0xBA68 0x1CD0 0x4263 0x109B
>>> IPv6.printRange $ IPv6.range addr 25
dead:be80::/25
The length should be between 0 and 32. These bounds are inclusive.
This expectation is not in any way enforced by this library because it
does not cause errors. A mask length greater than 32 will be treated
as if it were 32.
Encode an
IPv4Range to a
Builder.
>>> IPv4.builderRange (IPv4.IPv4Range (IPv4.ipv4 172 16 0 0) 12)
"172.16.0.0/12"
Decode an
IPv4Range from
Text.
>>> IPv4.decodeRange "172.16.0.0/12"
Just (IPv4Range {ipv4RangeBase = ipv4 172 16 0 0, ipv4RangeLength = 12})
>>> IPv4.decodeRange "192.168.25.254/16"
Just (IPv4Range {ipv4RangeBase = ipv4 192 168 0 0, ipv4RangeLength = 16})
Encode an
IPv4Range as
Text.
>>> IPv4.encodeRange (IPv4.IPv4Range (IPv4.ipv4 172 16 0 0) 12)
"172.16.0.0/12"
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
Print an
IPv4Range. Helper function that exists mostly for
testing purposes.
An
IPv6Range. It is made up of the first
IPv6 in the
range and its length.
Decode an
IPv6Range from
Text.
>>> addr = IPv6.ipv6 0xDEAD 0xBEEF 0x3240 0xA426 0xBA68 0x1CD0 0x4263 0x109B
>>> fmap IPv6.encodeRange $ IPv6.decodeRange (Text.pack "dead:beef:3240:a426:ba68:1cd0:4263:109b/28")
Just "dead:bee0::/28"
Encode an
IPv6Range as
Text.
>>> addr = IPv6.ipv6 0xDEAD 0xBEEF 0x3240 0xA426 0xBA68 0x1CD0 0x4263 0x109B
>>> T.putStrLn $ IPv6.encodeRange $ IPv6.IPv6Range addr 28
dead:beef:3240:a426:ba68:1cd0:4263:109b/28
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
Print an
IPv6Range using the textual encoding.