ip-1.3.0: Library for IP and MAC addresses

Safe HaskellNone
LanguageHaskell2010

Net.IPv4.Range

Contents

Synopsis

Range functions

range :: IPv4 -> Word8 -> IPv4Range Source #

Smart constructor for IPv4Range. Ensures the mask is appropriately sized and sets masked bits in the IPv4 to zero.

fromBounds :: IPv4 -> IPv4 -> IPv4Range Source #

Given an inclusive lower and upper ip address, create the smallest IPv4Range that contains the two. This is helpful in situations where input given as a range like 192.168.16.0-192.168.19.255 needs to be handled. This makes the range broader if it cannot be represented in CIDR notation.

>>> print $ fromBounds (fromOctets 192 168 16 0) (fromOctets 192 168 19 255)
192.168.16.0/22
>>> print $ fromBounds (fromOctets 10 0 5 7) (fromOctets 10 0 5 14)
10.0.5.0/28

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:

>>> 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 IPv4Ranges 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 Source #

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 Source #

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 Source #

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)

Conversion to IPv4

toList :: IPv4Range -> [IPv4] Source #

Convert an IPv4Range into a list of the IPv4 addresses that are in it. >>> let r = IPv4Range (fromOctets 192 168 1 8) 30 >>> mapM_ (T.putStrLn . I.encode) (toList r) 192.168.1.8 192.168.1.9 192.168.1.10 192.168.1.11

Private Ranges

private24 :: IPv4Range Source #

The RFC1918 24-bit block. Subnet mask: 10.0.0.0/8

private20 :: IPv4Range Source #

The RFC1918 20-bit block. Subnet mask: 172.16.0.0/12

private16 :: IPv4Range Source #

The RFC1918 16-bit block. Subnet mask: 192.168.0.0/16

Textual Conversion

Text

print :: IPv4Range -> IO () Source #

This exists mostly for testing purposes.

Types

data IPv4Range Source #

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 

Instances

Eq IPv4Range Source # 
Ord IPv4Range Source # 
Read IPv4Range Source # 
Show IPv4Range Source # 
Generic IPv4Range Source # 

Associated Types

type Rep IPv4Range :: * -> * #

Hashable IPv4Range Source # 
ToJSON IPv4Range Source # 
FromJSON IPv4Range Source # 
Bits IPv4Range Source #

Notes:

  • bit operations use network order (big endian),
  • do not operate on host bits,
  • return a normalized range dropping host bits,
  • and "promote operands" by extending the length to the larger of two ranges.
FiniteBits IPv4Range Source #

Note: the size is determined by the range length

Unbox IPv4Range Source # 
Vector Vector IPv4Range Source # 
MVector MVector IPv4Range Source # 
type Rep IPv4Range Source # 
type Rep IPv4Range = D1 * (MetaData "IPv4Range" "Net.IPv4.Range" "ip-1.3.0-8akPoClmXX6CvMDevW5iZ7" False) (C1 * (MetaCons "IPv4Range" PrefixI True) ((:*:) * (S1 * (MetaSel (Just Symbol "ipv4RangeBase") SourceUnpack SourceStrict DecidedStrict) (Rec0 * IPv4)) (S1 * (MetaSel (Just Symbol "ipv4RangeLength") SourceUnpack SourceStrict DecidedStrict) (Rec0 * Word8))))
data Vector IPv4Range Source # 
data MVector s IPv4Range Source #