Safe Haskell | None |
---|---|
Language | Haskell2010 |
Synopsis
- data IPv4Range = IPv4Range {
- ipv4RangeBase :: !IPv4
- ipv4RangeLength :: !Word8
- newtype IPv4 = IPv4 {}
- decodeRange :: Text -> Maybe IPv4Range
- parserRange :: Parser IPv4Range
- parser :: Parser IPv4
- dotDecimalParser :: Parser IPv4
- contains :: IPv4Range -> IPv4 -> Bool
- mask :: Word8 -> Word32
- member :: IPv4 -> IPv4Range -> Bool
- fromOctets :: Word8 -> Word8 -> Word8 -> Word8 -> IPv4
- fromTupleOctets :: (Word8, Word8, Word8, Word8) -> IPv4
- fromOctets' :: Word -> Word -> Word -> Word -> IPv4
- ipOctetSizeErrorMsg :: String
- normalize :: IPv4Range -> IPv4Range
- rightToMaybe :: Either a b -> Maybe b
Documentation
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.
IPv4Range | |
|
Instances
Eq IPv4Range Source # | |
Ord IPv4Range Source # | |
Defined in Net.IPv4 | |
Read IPv4Range Source # | |
Show IPv4Range Source # | |
Generic IPv4Range Source # | |
type Rep IPv4Range Source # | |
Defined in Net.IPv4 type Rep IPv4Range = D1 ('MetaData "IPv4Range" "Net.IPv4" "graphql-engine-1.0.0-inplace" 'False) (C1 ('MetaCons "IPv4Range" 'PrefixI 'True) (S1 ('MetaSel ('Just "ipv4RangeBase") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 IPv4) :*: S1 ('MetaSel ('Just "ipv4RangeLength") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Word8))) |
A 32-bit Internet Protocol version 4 address. To use this with the
network
library, it is necessary to use Network.Socket.htonl
to
convert the underlying Word32
from host byte order to network byte
order.
Instances
Bounded IPv4 Source # | |
Enum IPv4 Source # | |
Eq IPv4 Source # | |
Ord IPv4 Source # | |
Read IPv4 Source # | |
Show IPv4 Source # | |
Ix IPv4 Source # | |
Generic IPv4 Source # | |
Bits IPv4 Source # | |
Defined in Net.IPv4 (.&.) :: IPv4 -> IPv4 -> IPv4 # (.|.) :: IPv4 -> IPv4 -> IPv4 # complement :: IPv4 -> IPv4 # shift :: IPv4 -> Int -> IPv4 # rotate :: IPv4 -> Int -> IPv4 # setBit :: IPv4 -> Int -> IPv4 # clearBit :: IPv4 -> Int -> IPv4 # complementBit :: IPv4 -> Int -> IPv4 # testBit :: IPv4 -> Int -> Bool # bitSizeMaybe :: IPv4 -> Maybe Int # shiftL :: IPv4 -> Int -> IPv4 # unsafeShiftL :: IPv4 -> Int -> IPv4 # shiftR :: IPv4 -> Int -> IPv4 # unsafeShiftR :: IPv4 -> Int -> IPv4 # rotateL :: IPv4 -> Int -> IPv4 # | |
FiniteBits IPv4 Source # | |
Defined in Net.IPv4 | |
Hashable IPv4 Source # | |
type Rep IPv4 Source # | |
decodeRange :: Text -> Maybe IPv4Range Source #
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})
parserRange :: Parser IPv4Range Source #
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})
parser :: Parser IPv4 Source #
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"
dotDecimalParser :: Parser IPv4 Source #
This does not do an endOfInput check because it is reused in the range parser implementation.
contains :: IPv4Range -> IPv4 -> Bool Source #
Checks to see if an IPv4
address belongs in the IPv4Range
.
>>>
let ip = IPv4.fromOctets 10 10 1 92
>>>
IPv4.contains (IPv4.IPv4Range (IPv4.fromOctets 10 0 0 0) 8) ip
True>>>
IPv4.contains (IPv4.IPv4Range (IPv4.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 = IPv4.IPv4Range (IPv4.fromOctets 10 10 10 6) 31
>>>
mapM_ (P.print . IPv4.contains r) (take 5 $ iterate succ $ IPv4.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 Source #
This is provided to mirror the interface provided by Data.Set
. It
behaves just like contains
but with flipped arguments.
IPv4.member ip r == IPv4.contains r ip
fromOctets :: Word8 -> Word8 -> Word8 -> Word8 -> IPv4 Source #
An alias for the ipv4
smart constructor.
fromTupleOctets :: (Word8, Word8, Word8, Word8) -> IPv4 Source #
An uncurried variant of fromOctets
.
fromOctets' :: Word -> Word -> Word -> Word -> IPv4 Source #
This is sort of a misnomer. It takes Word to make dotDecimalParser perform better. This is mostly for internal use. The arguments must all fit in a Word8.
normalize :: IPv4Range -> IPv4Range Source #
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:
>>>
IPv4.printRange $ IPv4.normalize $ IPv4.IPv4Range (IPv4.fromOctets 192 168 1 19) 24
192.168.1.0/24>>>
IPv4.printRange $ IPv4.normalize $ IPv4.IPv4Range (IPv4.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:
IPv4.normalize r == (IPv4.normalize . IPv4.normalize) r
rightToMaybe :: Either a b -> Maybe b Source #