HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduleiproute-1.7.15Haskell2010

Data.IP

Data structures to express IPv4, IPv6 and IP range.

  • 5 types
  • 1 class
  • 25 values
  • Packageiproute-1.7.15
  • Exports31
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceIP.hs

IP data

1 declaration
datadata IP
#

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.

Example2 expressions
(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

Constructors

Instances9Enum, Eq, Data, Ord, Read, Show, …
  • Enum IPDefined in iproute-1.7.15 · Data.IP.Addr
  • Eq IPDefined in iproute-1.7.15 · Data.IP.Addr

    Equality over IP addresses. Correctly compare IPv4 and IPv4-embedded-in-IPv6 addresses.

    Example8 expressions
    (read "2001:db8:00:00:00:00:00:01" :: IP) == (read "2001:db8:00:00:00:00:00:01" :: IP)True(read "2001:db8:00:00:00:00:00:01" :: IP) == (read "2001:db8:00:00:00:00:00:05" :: IP)False(read "127.0.0.1" :: IP) == (read "127.0.0.1" :: IP)True(read "127.0.0.1" :: IP) == (read "10.0.0.1" :: IP)False(read "::ffff:127.0.0.1" :: IP) == (read "127.0.0.1" :: IP)True(read "::ffff:127.0.0.1" :: IP) == (read "127.0.0.9" :: IP)False(read "::ffff:127.0.0.1" :: IP) >= (read "127.0.0.1" :: IP)True(read "::ffff:127.0.0.1" :: IP) <= (read "127.0.0.1" :: IP)True
  • Data IPDefined in iproute-1.7.15 · Data.IP.Addr
  • Ord IPDefined in iproute-1.7.15 · Data.IP.Addr
  • Read IPDefined in iproute-1.7.15 · Data.IP.Addr
  • Show IPDefined in iproute-1.7.15 · Data.IP.Addr
  • IsString IPDefined in iproute-1.7.15 · Data.IP.Addr
  • Generic IPDefined in iproute-1.7.15 · Data.IP.Addr
  • type Rep IP = D1 ('MetaData "IP" "Data.IP.Addr" "iproute-1.7.15-3X50rHbdFZS8qgLxLNnKxb" 'False) (C1 ('MetaCons "IPv4" 'PrefixI 'True) (S1 ('MetaSel ('Just "ipv4") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedUnpack) (Rec0 IPv4)) :+: C1 ('MetaCons "IPv6" 'PrefixI 'True) (S1 ('MetaSel ('Just "ipv6") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 IPv6)))Defined in iproute-1.7.15 · Data.IP.Addr

IPv4

newtypenewtype IPv4
#

The abstract data type to express an IPv4 address. To create this, use toIPv4. Or use read "192.0.2.1" :: IPv4, for example. Also, "192.0.2.1" can be used as literal with OverloadedStrings.

Example1 expression
read "192.0.2.1" :: IPv4192.0.2.1
Instances14Bounded, Enum, Eq, Data, Ord, Show, …
valuetoIPv4 :: [Int] -> IPv4
#

The toIPv4 function returns the IPv4 address corresponding to the given list of Int octets. The function is strict in the four elements of the list. An error is returned if the list has a differnet length. The input elements are silently truncated to their 8 least-significant bits before they are combined to form the IPv4 address.

Example1 expression
toIPv4 [192,0,2,1]192.0.2.1
valuetoIPv4w :: Word32 -> IPv4
#

The toIPv4w function constructs the IPv4 address corresponding to the given Word32 value. Unlike the fromHostAddress function, it is strict in the input value, which here is in host byte order.

Example1 expression
toIPv4w 0xc0000201192.0.2.1
valuefromIPv4 :: IPv4 -> [Int]
#

The fromIPv4 function returns the list of four Int octets corresponding to the given IPv4 address.

Example1 expression
fromIPv4 (toIPv4 [192,0,2,1])[192,0,2,1]
valuefromIPv4w :: IPv4 -> Word32
#

The fromIPv4w function returns a single Word32 value corresponding to the given the IPv4 address. Unlike the toHostAddress function, the returned value is strictly evaluated, and is not converted to network byte order.

Example1 expression
fromIPv4w (toIPv4 [0xc0,0,2,1]) == 0xc0000201True

IPv6

newtypenewtype IPv6
#

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.

Example7 expressions
read "2001:db8:00:00:00:00:00:01" :: IPv62001:db8::1read "2001:db8:11e:c00::101" :: IPv62001:db8:11e:c00::101read "2001:db8:11e:c00:aa:bb:192.0.2.1" :: IPv62001:db8:11e:c00:aa:bb:c000:201read "2001:db8::192.0.2.1" :: IPv62001:db8::c000:201read "0::ffff:192.0.2.1" :: IPv6::ffff:192.0.2.1read "0::0:c000:201" :: IPv6::192.0.2.1read "::0.0.0.1" :: IPv6::1
Instances14Bounded, Enum, Eq, Data, Ord, Show, …
valuetoIPv6 :: [Int] -> IPv6
#

The toIPv6 function returns the IPv6 address corresponding to the given list of eight 16-bit Ints. The function is strict in the eight elements of the list. An error is returned if the list has a differnet length. The input elements are in host byte order and are silently truncated to their 16 least-signicant bits before they are combined to form the IPv6 address.

Example1 expression
toIPv6 [0x2001,0xDB8,0,0,0,0,0,1]2001:db8::1
valuetoIPv6b :: [Int] -> IPv6
#

The toIPv6b function returns the IPv6 address corresponding to the given list of sixteen Int octets. The function is strict in the sixteen elements of the list. An error is returned if the list has a differnet length. The input elements are silently truncated to their 8 least-signicant bits before they are combined to form the IPv6 address.

Example1 expression
toIPv6b [0x20,0x01,0xD,0xB8,0,0,0,0,0,0,0,0,0,0,0,1]2001:db8::1
valuetoIPv6w :: (Word32, Word32, Word32, Word32) -> IPv6
#

The toIPv6w function constructs the IPv6 address corresponding to the given four-tuple of host byte order Word32 values. This function differs from the fromHostAddress6 function only in the fact that it is strict in the elements of the tuple.

Example1 expression
toIPv6w (0x20010DB8,0x0,0x0,0x1)2001:db8::1
valuefromIPv6 :: IPv6 -> [Int]
#

The fromIPv6 function returns a list eight Ints in host byte order corresponding to the eight 16-bit fragments of the given IPv6 address.

Example1 expression
fromIPv6 (toIPv6 [0x2001,0xDB8,0,0,0,0,0,1])[8193,3512,0,0,0,0,0,1]
valuefromIPv6b :: IPv6 -> [Int]
#

The fromIPv6b function returns the 16 Int octets corresponding to the 16 bytes of the given IPv6 address.

Example1 expression
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]
valuefromIPv6w :: IPv6 -> (Word32, Word32, Word32, Word32)
#

The fromIPv6w function returns a four-tuple of Word32 values in host byte order corresponding to the given IPv6 address. This is identical to the toHostAddress6 function, except that the elements of four-tuple are first strictly evaluated.

Example1 expression
fromIPv6w (toIPv6 [0x2001,0xDB8,0,0,0,0,0,1]) == (0x20010DB8, 0, 0, 1)True

Converters

IP range data

5 declarations
datadata IPRange
#

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.

Example2 expressions
(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
Instances8Eq, Data, Ord, Read, Show, IsString, …
datadata AddrRange a
#

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.

Example2 expressions
read "192.0.2.1/24" :: AddrRange IPv4192.0.2.0/24read "2001:db8:00:00:00:00:00:01/48" :: AddrRange IPv62001:db8::/48
Instances10Eq, Data, Ord, Read, Show, IsString, …

Address class

6 declarations
classclass Eq a => Addr a where
#
Example1 expression
toIPv4 [127,0,2,1] `masked` intToMask 7126.0.0.0

Methods

  • masked :: a -> a -> a

    The masked function takes an Addr and a contiguous mask and returned a masked Addr.

  • intToMask :: Int -> a

    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.

    Example1 expression
    intToMask 16 :: IPv4255.255.0.0
    Example1 expression
    intToMask (-16) :: IPv40.0.255.255
    Example1 expression
    intToMask 16 :: IPv6ffff::
    Example1 expression
    intToMask (-16) :: IPv6::ffff
Instances2Addr
  • Addr IPv4Defined in iproute-1.7.15 · Data.IP.Op
  • Addr IPv6Defined in iproute-1.7.15 · Data.IP.Op
valuemakeAddrRange :: 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.

Example2 expressions
makeAddrRange (toIPv4 [127,0,2,1]) 8127.0.0.0/8makeAddrRange (toIPv6 [0x2001,0xDB8,0,0,0,0,0,1]) 82000::/8
value(>:>) :: 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.

Example4 expressions
makeAddrRange ("127.0.2.1" :: IPv4) 8 >:> makeAddrRange "127.0.2.1" 24TruemakeAddrRange ("127.0.2.1" :: IPv4) 24 >:> makeAddrRange "127.0.2.1" 8FalsemakeAddrRange ("2001:DB8::1" :: IPv6) 16 >:> makeAddrRange "2001:DB8::1" 32TruemakeAddrRange ("2001:DB8::1" :: IPv6) 32 >:> makeAddrRange "2001:DB8::1" 16False
valueisMatchedTo :: Addr a => a -> AddrRange a -> Bool
#

The isMatchedTo function take an Addr address and an AddrRange, and returns True if the range contains the address.

Example4 expressions
("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
valueaddrRangePair :: Addr a => AddrRange a -> (a, Int)
#

The unmakeAddrRange functions take a AddrRange and returns the network address and a mask length.

Example2 expressions
addrRangePair ("127.0.0.0/8" :: AddrRange IPv4)(127.0.0.0,8)addrRangePair ("2000::/8" :: AddrRange IPv6)(2000::,8)