Safe Haskell | None |
---|---|
Language | Haskell2010 |
Net.IPv4.Range
- range :: IPv4 -> Word8 -> IPv4Range
- normalize :: IPv4Range -> IPv4Range
- contains :: IPv4Range -> IPv4 -> Bool
- member :: IPv4 -> IPv4Range -> Bool
- lowerInclusive :: IPv4Range -> IPv4
- upperInclusive :: IPv4Range -> IPv4
- toList :: IPv4Range -> [IPv4]
- toGenerator :: MonadPlus m => IPv4Range -> m IPv4
- private24 :: IPv4Range
- private20 :: IPv4Range
- private16 :: IPv4Range
- encode :: IPv4Range -> Text
- decode :: Text -> Maybe IPv4Range
- builder :: IPv4Range -> Builder
- parser :: Parser IPv4Range
- print :: IPv4Range -> IO ()
- data IPv4Range = IPv4Range {
- ipv4RangeBase :: !IPv4
- ipv4RangeLength :: !Word8
Range functions
normalize :: IPv4Range -> IPv4Range #
Normalize an IPv4Range
. The first result of this is that the
IPv4
inside the IPv4Range
is changed so that the insignificant
bits are zeroed out. For example:
>>>
print $ normalize $ IPv4Range (fromOctets 192 168 1 19) 24
192.168.1.0/24>>>
print $ normalize $ IPv4Range (fromOctets 192 168 1 163) 28
192.168.1.160/28
The second effect of this is that the mask length is lowered to
be 32 or smaller. Working with IPv4Range
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 32).
Note that normalize
is idempotent, that is:
normalize r == (normalize . normalize) r
contains :: IPv4Range -> IPv4 -> Bool #
Checks to see if an IPv4
address belongs in the IPv4Range
.
>>>
let ip = fromOctets 10 10 1 92
>>>
contains (IPv4Range (fromOctets 10 0 0 0) 8) ip
True>>>
contains (IPv4Range (fromOctets 10 11 0 0) 16) 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 = IPv4Range (fromOctets 10 10 10 6) 31
>>>
mapM_ (P.print . contains r) (take 5 $ iterate succ $ fromOctets 10 10 10 5)
False True True 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.
member :: IPv4 -> IPv4Range -> Bool #
This is provided to mirror the interface provided by Data.Set
. It
behaves just like contains
but with flipped arguments.
member ip r == contains r ip
lowerInclusive :: IPv4Range -> IPv4 #
The inclusive lower bound of an IPv4Range
. This is conventionally
understood to be the broadcast address of a subnet. For example:
>>>
T.putStrLn $ I.encode $ lowerInclusive $ IPv4Range (ipv4 10 10 1 160) 25
10.10.1.128
Note that the lower bound of a normalized IPv4Range
is simply the
ip address of the range:
lowerInclusive r == ipv4RangeBase (normalize r)
upperInclusive :: IPv4Range -> IPv4 #
Conversion to IPv4
toGenerator :: MonadPlus m => IPv4Range -> m IPv4 #
Private Ranges
Textual Conversion
Text
Types
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.
Constructors
IPv4Range | |
Fields
|
Instances
Eq IPv4Range # | |
Ord IPv4Range # | |
Read IPv4Range # | |
Show IPv4Range # | |
Generic IPv4Range # | |
Hashable IPv4Range # | |
ToJSON IPv4Range # | |
FromJSON IPv4Range # | |
Bits IPv4Range # | Notes:
|
FiniteBits IPv4Range # | Note: the size is determined by the range length |
Unbox IPv4Range # | |
Vector Vector IPv4Range # | |
MVector MVector IPv4Range # | |
type Rep IPv4Range # | |
data Vector IPv4Range # | |
data MVector s IPv4Range # | |