| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Data.IP
Description
Data structures to express IPv4, IPv6 and IP range.
- data IP
- data IPv4
- toIPv4 :: [Int] -> IPv4
- fromIPv4 :: IPv4 -> [Int]
- fromHostAddress :: HostAddress -> IPv4
- toHostAddress :: IPv4 -> HostAddress
- data IPv6
- toIPv6 :: [Int] -> IPv6
- toIPv6b :: [Int] -> IPv6
- fromIPv6 :: IPv6 -> [Int]
- fromIPv6b :: IPv6 -> [Int]
- fromHostAddress6 :: HostAddress6 -> IPv6
- toHostAddress6 :: IPv6 -> HostAddress6
- data IPRange
- data AddrRange a
- class Eq a => Addr a where
- makeAddrRange :: Addr a => a -> Int -> AddrRange a
- (>:>) :: Addr a => AddrRange a -> AddrRange a -> Bool
- isMatchedTo :: Addr a => a -> AddrRange a -> Bool
- addrRangePair :: Addr a => AddrRange a -> (a, Int)
- ipv4RangeToIPv6 :: AddrRange IPv4 -> AddrRange IPv6
- ipv4ToIPv6 :: IPv4 -> IPv6
Documentation
IP data
A unified IP data for IPv4 and IPv6.
To create this, use the data constructors. Or use read "192.0.2.1" :: IP, for example. Also, "192.0.2.1" can be used as literal with OverloadedStrings.
>>>(read "192.0.2.1" :: IP) == IPv4 (read "192.0.2.1" :: IPv4)True>>>(read "2001:db8:00:00:00:00:00:01" :: IP) == IPv6 (read "2001:db8:00:00:00:00:00:01" :: IPv6)True
Instances
| Enum IP # | |
| Eq IP # | Equality over IP addresses. Correctly compare IPv4 and IPv4-embedded-in-IPv6 addresses.
|
| Data IP # | |
| Ord IP # | |
| Read IP # | |
| Show IP # | |
| IsString IP # | |
| Generic IP # | |
| type Rep IP # | |
fromHostAddress :: HostAddress -> IPv4 #
The fromHostAddress function converts HostAddress to IPv4.
toHostAddress :: IPv4 -> HostAddress #
The toHostAddress function converts IPv4 to HostAddress.
The abstract data type to express an IPv6 address.
To create this, use toIPv6. Or use read "2001:DB8::1" :: IPv6, for example. Also, "2001:DB8::1" can be used as literal with OverloadedStrings.
>>>read "2001:db8:00:00:00:00:00:01" :: IPv62001:db8::1>>>read "2001:db8:11e:c00::101" :: IPv62001:db8:11e:c00::101>>>read "2001:db8:11e:c00:aa:bb:192.0.2.1" :: IPv62001:db8:11e:c00:aa:bb:c000:201>>>read "2001:db8::192.0.2.1" :: IPv62001:db8::c000:201>>>read "0::ffff:192.0.2.1" :: IPv6::ffff:192.0.2.1>>>read "0::0:c000:201" :: IPv6::192.0.2.1>>>read "::0.0.0.1" :: IPv6::1
fromHostAddress6 :: HostAddress6 -> IPv6 #
The fromHostAddress6 function converts HostAddress6 to IPv6.
toHostAddress6 :: IPv6 -> HostAddress6 #
The toHostAddress6 function converts IPv6 to HostAddress6.
IP range data
A unified data for AddrRange IPv4 and AddrRange IPv6.
To create this, use read "192.0.2.0/24" :: IPRange.
Also, "192.0.2.0/24" can be used as literal with OverloadedStrings.
>>>(read "192.0.2.1/24" :: IPRange) == IPv4Range (read "192.0.2.0/24" :: AddrRange IPv4)True>>>(read "2001:db8:00:00:00:00:00:01/48" :: IPRange) == IPv6Range (read "2001:db8:00:00:00:00:00:01/48" :: AddrRange IPv6)True
The Addr range consists of an address, a contiguous mask, and mask length. The contiguous mask and the mask length are essentially same information but contained for pre calculation.
To create this, use makeAddrRange or read "192.0.2.0/24" :: AddrRange IPv4.
Also, "192.0.2.0/24" can be used as literal with OverloadedStrings.
>>>read "192.0.2.1/24" :: AddrRange IPv4192.0.2.0/24>>>read "2001:db8:00:00:00:00:00:01/48" :: AddrRange IPv62001:db8::/48
Address class
>>>toIPv4 [127,0,2,1] `masked` intToMask 7126.0.0.0
Methods
The intToMask function takes an Int representing the number of bits to
be set in the returned contiguous mask. When this integer is positive the
bits will be starting from the MSB and from the LSB otherwise.
>>>intToMask 16 :: IPv4255.255.0.0
>>>intToMask (-16) :: IPv40.0.255.255
>>>intToMask 16 :: IPv6ffff::
>>>intToMask (-16) :: IPv6::ffff
makeAddrRange :: Addr a => a -> Int -> AddrRange a #
The makeAddrRange functions takes an Addr address and a mask
length. It creates a bit mask from the mask length and masks
the Addr address, then returns AddrRange made of them.
>>>makeAddrRange (toIPv4 [127,0,2,1]) 8127.0.0.0/8>>>makeAddrRange (toIPv6 [0x2001,0xDB8,0,0,0,0,0,1]) 82000::/8
(>:>) :: Addr a => AddrRange a -> AddrRange a -> Bool #
The >:> operator takes two AddrRange. It returns True if
the first AddrRange contains the second AddrRange. Otherwise,
it returns False.
>>>makeAddrRange ("127.0.2.1" :: IPv4) 8 >:> makeAddrRange "127.0.2.1" 24True>>>makeAddrRange ("127.0.2.1" :: IPv4) 24 >:> makeAddrRange "127.0.2.1" 8False>>>makeAddrRange ("2001:DB8::1" :: IPv6) 16 >:> makeAddrRange "2001:DB8::1" 32True>>>makeAddrRange ("2001:DB8::1" :: IPv6) 32 >:> makeAddrRange "2001:DB8::1" 16False
isMatchedTo :: Addr a => a -> AddrRange a -> Bool #
The toMatchedTo function take an Addr address and an AddrRange,
and returns True if the range contains the address.
>>>("127.0.2.0" :: IPv4) `isMatchedTo` makeAddrRange "127.0.2.1" 24True>>>("127.0.2.0" :: IPv4) `isMatchedTo` makeAddrRange "127.0.2.1" 32False>>>("2001:DB8::1" :: IPv6) `isMatchedTo` makeAddrRange "2001:DB8::1" 32True>>>("2001:DB8::" :: IPv6) `isMatchedTo` makeAddrRange "2001:DB8::1" 128False
addrRangePair :: Addr a => AddrRange a -> (a, Int) #
The unmakeAddrRange functions take a AddrRange and
returns the network address and a mask length.
>>>addrRangePair ("127.0.0.0/8" :: AddrRange IPv4)(127.0.0.0,8)>>>addrRangePair ("2000::/8" :: AddrRange IPv6)(2000::,8)
ipv4RangeToIPv6 :: AddrRange IPv4 -> AddrRange IPv6 #
Convert IPv4 range to IPV4-embedded-in-IPV6 range
ipv4ToIPv6 :: IPv4 -> IPv6 #
Convert IPv4 address to IPv4-embedded-in-IPv6