Safe Haskell | None |
---|---|
Language | Haskell2010 |
Synopsis
- decodeRange :: Text -> Maybe IPv6Range
- parserRange :: Parser IPv6Range
- data IPv6Range = IPv6Range {
- ipv6RangeBase :: !IPv6
- ipv6RangeLength :: !Word8
- newtype IPv6 = IPv6 {
- getIPv6 :: Word128
- parser :: Parser IPv6
- normalize :: IPv6Range -> IPv6Range
- mask128 :: IPv6
- mask :: Word8 -> IPv6
- fromWord16s :: Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> IPv6
- fromWord16sWord128 :: Word128 -> Word128 -> Word128 -> Word128 -> Word128 -> Word128 -> Word128 -> Word128 -> Word128
- fromTupleWord32s :: (Word32, Word32, Word32, Word32) -> IPv6
- member :: IPv6 -> IPv6Range -> Bool
- rightToMaybe :: Either a b -> Maybe b
- contains :: IPv6Range -> IPv6 -> Bool
- fromWord32s :: Word32 -> Word32 -> Word32 -> Word32 -> IPv6
- fromWord32sWord128 :: Word128 -> Word128 -> Word128 -> Word128 -> Word128
Documentation
decodeRange :: Text -> Maybe IPv6Range Source #
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"
parserRange :: Parser IPv6Range Source #
Parse an IPv6Range
using a Parser
.
IPv6Range | |
|
parser :: Parser IPv6 Source #
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)
normalize :: IPv6Range -> IPv6Range Source #
Normalize an IPv6Range
. The first result of this is that the
IPv6
inside the IPv6Range
is changed so that the insignificant
bits are zeroed out. For example:
>>>
addr1 = IPv6.ipv6 0x0192 0x0168 0x0001 0x0019 0x0000 0x0000 0x0000 0x0000
>>>
addr2 = IPv6.ipv6 0x0192 0x0168 0x0001 0x0163 0x0000 0x0000 0x0000 0x0000
>>>
IPv6.printRange $ IPv6.normalize $ IPv6.IPv6Range addr1 24
192:100::/24>>>
IPv6.printRange $ IPv6.normalize $ IPv6.IPv6Range addr2 28
192:160::/28
The second effect of this is that the mask length is lowered to be 128
or smaller. Working with IPv6Range
s that have not been normalized does
not cause any issues for this library, although other applications may
reject such ranges (especially those with a mask length above 128).
Note that 'normalize is idempotent, that is:
IPv6.normalize r == (IPv6.normalize . IPv6.normalize) r
fromWord16s :: Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> Word16 -> IPv6 Source #
An alias for the ipv6
smart constructor.
fromWord16sWord128 :: Word128 -> Word128 -> Word128 -> Word128 -> Word128 -> Word128 -> Word128 -> Word128 -> Word128 Source #
fromTupleWord32s :: (Word32, Word32, Word32, Word32) -> IPv6 Source #
Uncurried variant of fromWord32s
.
member :: IPv6 -> IPv6Range -> Bool Source #
This is provided to mirror the interface provided by Data.Set
. It
behaves just like contains
but with flipped arguments.
IPv6.member ip r == IPv6.contains r ip
rightToMaybe :: Either a b -> Maybe b Source #
contains :: IPv6Range -> IPv6 -> Bool Source #
Checks to see if an IPv6
address belongs in the IPv6Range
.
>>>
let ip = IPv6.ipv6 0x2001 0x0db8 0x0db8 0x1094 0x2051 0x0000 0x0000 0x0001
>>>
let iprange mask = IPv6.IPv6Range (IPv6.ipv6 0x2001 0x0db8 0x0000 0x0000 0x0000 0x0000 0x0000 0x0001) mask
>>>
IPv6.contains (iprange 8) ip
True>>>
IPv6.contains (iprange 48) ip
False
Typically, element-testing functions are written to take the element as the first argument and the set as the second argument. This is intentionally written the other way for better performance when iterating over a collection. For example, you might test elements in a list for membership like this:
>>>
let r = IPv6.IPv6Range (IPv6.ipv6 0x2001 0x0db8 0x0000 0x0000 0x0000 0x0000 0x0000 0x0001) 64
>>>
fmap (IPv6.contains r) (take 5 $ iterate succ $ IPv6.ipv6 0x2001 0x0db8 0x0000 0x0000 0xffff 0xffff 0xffff 0xfffe)
[True,True,False,False,False]
The implementation of contains
ensures that (with GHC), the bitmask
creation and range normalization only occur once in the above example.
They are reused as the list is iterated.
fromWord32s :: Word32 -> Word32 -> Word32 -> Word32 -> IPv6 Source #
Build an IPv6
from four 32-bit words. The leftmost argument
is the high word and the rightword is the low word.
fromWord32sWord128 :: Word128 -> Word128 -> Word128 -> Word128 -> Word128 Source #