Data.IP (original) (raw)

data IP Source #

A unified IP data for [IP](Data-IP.html#t:IP "Data.IP") and [IP](Data-IP.html#t:IP "Data.IP"). To create this, use the data constructors. Or use [read](/package/base-4.12.0.0/docs/Text-Read.html#v:read "Text.Read") "192.0.2.1" :: [IP](Data-IP.html#t:IP "Data.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

IPv4

data IPv4 Source #

The abstract data type to express an IPv4 address. To create this, use [toIPv4](Data-IP.html#v:toIPv4 "Data.IP"). Or use [read](/package/base-4.12.0.0/docs/Text-Read.html#v:read "Text.Read") "192.0.2.1" :: [IP](Data-IP.html#t:IP "Data.IP"), for example. Also, "192.0.2.1" can be used as literal with OverloadedStrings.

>>> **read "192.0.2.1" :: IPv4** ****192.0.2.1

IPv6

data IPv6 Source #

The abstract data type to express an IPv6 address. To create this, use [toIPv6](Data-IP.html#v:toIPv6 "Data.IP"). Or use [read](/package/base-4.12.0.0/docs/Text-Read.html#v:read "Text.Read") "2001:DB8::1" :: [IP](Data-IP.html#t:IP "Data.IP"), for example. Also, "2001:DB8::1" can be used as literal with OverloadedStrings.

>>> **read "2001:db8:00:00:00:00:00:01" :: IPv6** ****2001:db8::1 >>> **read "2001:db8:11e:c00::101" :: IPv6** ****2001:db8:11e:c00::101 >>> **read "2001:db8:11e:c00:aa:bb:192.0.2.1" :: IPv6** ****2001:db8:11e:c00:aa:bb:c000:201 >>> **read "2001:db8::192.0.2.1" :: IPv6** ****2001: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

toIPv6b :: [Int] -> IPv6 Source #

The [toIPv6b](Data-IP.html#v:toIPv6b "Data.IP") function takes a list of [Int](/package/base-4.12.0.0/docs/Data-Int.html#t:Int "Data.Int") where each member repserents a single byte and returns [IP](Data-IP.html#t:IP "Data.IP").

>>> **toIPv6b [0x20,0x01,0xD,0xB8,0,0,0,0,0,0,0,0,0,0,0,1]** ****2001:db8::1

fromIPv6 :: IPv6 -> [Int] Source #

The [toIPv6](Data-IP.html#v:toIPv6 "Data.IP") function converts [IP](Data-IP.html#t:IP "Data.IP") to a list of [Int](/package/base-4.12.0.0/docs/Data-Int.html#t:Int "Data.Int").

>>> fromIPv6 (toIPv6 [0x2001,0xDB8,0,0,0,0,0,1])** **[8193,3512,0,0,0,0,0,1]

fromIPv6b :: IPv6 -> [Int] Source #

The [fromIPv6b](Data-IP.html#v:fromIPv6b "Data.IP") function converts [IP](Data-IP.html#t:IP "Data.IP") to a list of [Int](/package/base-4.12.0.0/docs/Data-Int.html#t:Int "Data.Int") where each member represents a single byte.

>>> fromIPv6b (toIPv6b [0x20,0x01,0xD,0xB8,0,0,0,0,0,0,0,0,0,0,0,1])** **[32,1,13,184,0,0,0,0,0,0,0,0,0,0,0,1]

ConvertersIP range data

data IPRange Source #

A unified data for [AddrRange](Data-IP.html#t:AddrRange "Data.IP") [IP](Data-IP.html#t:IP "Data.IP") and [AddrRange](Data-IP.html#t:AddrRange "Data.IP") [IP](Data-IP.html#t:IP "Data.IP"). To create this, use [read](/package/base-4.12.0.0/docs/Text-Read.html#v:read "Text.Read") "192.0.2.0/24" :: [IPRange](Data-IP.html#t:IPRange "Data.IP"). 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

data AddrRange a Source #

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](/package/base-4.12.0.0/docs/Text-Read.html#v:read "Text.Read") "192.0.2.0/24" :: [AddrRange](Data-IP.html#t:AddrRange "Data.IP") [IP](Data-IP.html#t:IP "Data.IP"). Also, "192.0.2.0/24" can be used as literal with OverloadedStrings.

>>> **read "192.0.2.1/24" :: AddrRange IPv4** ****192.0.2.0/24 >>> **read "2001:db8:00:00:00:00:00:01/48" :: AddrRange IPv6** ****2001:db8::/48

Address class

class Eq a => Addr a where Source #

>>> `` toIPv4 [127,0,2,1] masked intToMask 7 **``**126.0.0.0

Methods

masked :: a -> a -> a Source #

The [masked](Data-IP.html#v:masked "Data.IP") function takes an [Addr](Data-IP.html#t:Addr "Data.IP") and a contiguous mask and returned a masked [Addr](Data-IP.html#t:Addr "Data.IP").

intToMask :: Int -> a Source #

The [intToMask](Data-IP.html#v:intToMask "Data.IP") function takes an [Int](/package/base-4.12.0.0/docs/Data-Int.html#t:Int "Data.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 :: IPv4** ****255.255.0.0

>>> **intToMask (-16) :: IPv4** ****0.0.255.255

>>> **intToMask 16 :: IPv6** ****ffff::

>>> intToMask (-16) :: IPv6** **::ffff

makeAddrRange :: Addr a => a -> Int -> AddrRange a Source #

The [makeAddrRange](Data-IP.html#v:makeAddrRange "Data.IP") functions takes an [Addr](Data-IP.html#t:Addr "Data.IP") address and a mask length. It creates a bit mask from the mask length and masks the [Addr](Data-IP.html#t:Addr "Data.IP") address, then returns [AddrRange](Data-IP.html#t:AddrRange "Data.IP") made of them.

>>> **makeAddrRange (toIPv4 [127,0,2,1]) 8** ****127.0.0.0/8 >>> **makeAddrRange (toIPv6 [0x2001,0xDB8,0,0,0,0,0,1]) 8** ****2000::/8

(>:>) :: Addr a => AddrRange a -> AddrRange a -> Bool Source #

The >:> operator takes two [AddrRange](Data-IP.html#t:AddrRange "Data.IP"). It returns [True](/package/base-4.12.0.0/docs/Data-Bool.html#v:True "Data.Bool") if the first [AddrRange](Data-IP.html#t:AddrRange "Data.IP") contains the second [AddrRange](Data-IP.html#t:AddrRange "Data.IP"). Otherwise, it returns [False](/package/base-4.12.0.0/docs/Data-Bool.html#v:False "Data.Bool").

>>> **makeAddrRange ("127.0.2.1" :: IPv4) 8 >:> makeAddrRange "127.0.2.1" 24** ****True >>> **makeAddrRange ("127.0.2.1" :: IPv4) 24 >:> makeAddrRange "127.0.2.1" 8** ****False >>> **makeAddrRange ("2001:DB8::1" :: IPv6) 16 >:> makeAddrRange "2001:DB8::1" 32** ****True >>> **makeAddrRange ("2001:DB8::1" :: IPv6) 32 >:> makeAddrRange "2001:DB8::1" 16** ****False

isMatchedTo :: Addr a => a -> AddrRange a -> Bool Source #

The toMatchedTo function take an [Addr](Data-IP.html#t:Addr "Data.IP") address and an [AddrRange](Data-IP.html#t:AddrRange "Data.IP"), and returns [True](/package/base-4.12.0.0/docs/Data-Bool.html#v:True "Data.Bool") if the range contains the address.

>>> `` ("127.0.2.0" :: IPv4) isMatchedTo makeAddrRange "127.0.2.1" 24 ****True `>>> ` ** ("127.0.2.0" :: IPv4) isMatchedTo makeAddrRange "127.0.2.1" 32** ****False `>>> ` ** ("2001:DB8::1" :: IPv6) isMatchedTo makeAddrRange "2001:DB8::1" 32** ****True `>>> ` ** ("2001:DB8::" :: IPv6) isMatchedTo makeAddrRange "2001:DB8::1" 128** **``**False

addrRangePair :: Addr a => AddrRange a -> (a, Int) Source #

The unmakeAddrRange functions take a [AddrRange](Data-IP.html#t:AddrRange "Data.IP") 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)