iproute-1.2.11: IP Routing Table

Safe Haskell | None

Data.IP

Contents

- Documentation
  - IP data
  - IP range data
  - Address class

Description

Data structures to express IPv4, IPv6 and IP range.

Synopsis

- data IP
  - = IPv4 {
    - ipv4 :: IPv4
    }
  - | IPv6 {
    - ipv6 :: IPv6
    }
- data IPv4
- toIPv4 :: [Int] -> IPv4
- fromIPv4 :: IPv4 -> [Int]
- fromHostAddress :: HostAddress -> IPv4
- toHostAddress :: IPv4 -> HostAddress
- data IPv6
- toIPv6 :: [Int] -> IPv6
- fromIPv6 :: IPv6 -> [Int]
- fromHostAddress6 :: HostAddress6 -> IPv6
- toHostAddress6 :: IPv6 -> HostAddress6
- data IPRange
  - = IPv4Range {
    - ipv4range :: AddrRange IPv4
    }
  - | IPv6Range {
    - ipv6range :: AddrRange IPv6
    }
- data AddrRange a
- class Eq a => Addr a where
  - masked :: a -> a -> a
  - intToMask :: Int -> a
- makeAddrRange :: Addr a => a -> Int -> AddrRange a
- (>:>) :: Addr a => AddrRange a -> AddrRange a -> Bool
- isMatchedTo :: Addr a => a -> AddrRange a -> Bool

# Documentation

## IP data

data IP Source

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

Constructors

IPv4 |

Fields

ipv4 :: IPv4

IPv6 |

Fields

ipv6 :: IPv6

Instances

Eq IP |
Read IP |
Show IP |
IsString IP |

data IPv4 Source

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.

    >>> read "192.0.2.1" :: IPv4
    192.0.2.1

Instances

Eq IPv4 |
Ord IPv4 |
Read IPv4 |
Show IPv4 |
IsString IPv4 |
Addr IPv4 |
Routable IPv4 |
Read (AddrRange IPv4) |
IsString (AddrRange IPv4) |

toIPv4 :: [Int] -> IPv4 Source

The `toIPv4` function takes a list of `Int` and returns `IPv4`.

    >>> toIPv4 [192,0,2,1]
    192.0.2.1

fromIPv4 :: IPv4 -> [Int]Source

The `fromIPv4` function converts `IPv4` to a list of `Int`.

    >>> fromIPv4 (toIPv4 [192,0,2,1])
    [192,0,2,1]

fromHostAddress :: HostAddress -> IPv4 Source

The `fromHostAddress` function converts `HostAddress` to `IPv4`.

toHostAddress :: IPv4 -> HostAddress Source

The `toHostAddress` function converts `IPv4` to `HostAddress`.

data IPv6 Source

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

Instances

Eq IPv6 |
Ord IPv6 |
Read IPv6 |
Show IPv6 |
IsString IPv6 |
Addr IPv6 |
Routable IPv6 |
Read (AddrRange IPv6) |
IsString (AddrRange IPv6) |

toIPv6 :: [Int] -> IPv6 Source

The `toIPv6` function takes a list of `Int` and returns `IPv6`.

    >>> toIPv6 [0x2001,0xDB8,0,0,0,0,0,1]
    2001:db8:00:00:00:00:00:01

fromIPv6 :: IPv6 -> [Int]Source

The `toIPv6` function converts `IPv6` to a list of `Int`.

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

fromHostAddress6 :: HostAddress6 -> IPv6 Source

The `fromHostAddress6` function converts `HostAddress6` to `IPv6`.

toHostAddress6 :: IPv6 -> HostAddress6 Source

The `toHostAddress6` function converts `IPv6` to `HostAddress6`.

## IP range data

data IPRange Source

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

Constructors

IPv4Range |

Fields

ipv4range :: AddrRange IPv4

IPv6Range |

Fields

ipv6range :: AddrRange IPv6

Instances

Eq IPRange |
Read IPRange |
Show IPRange |
IsString IPRange |

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` `"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 IPv4
    192.0.2.0/24
    >>> read "2001:db8:00:00:00:00:00:01/48" :: AddrRange IPv6
    2001:db8:00:00:00:00:00:00/48

Instances

Eq a => Eq (AddrRange a) |
Ord a => Ord (AddrRange a) |
Read (AddrRange IPv6) |
Read (AddrRange IPv4) |
Show a => Show (AddrRange a) |
IsString (AddrRange IPv6) |
IsString (AddrRange IPv4) |

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

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

intToMask :: Int -> aSource

The `intToMask` function takes `Int` and returns a contiguous mask.

Instances

Addr IPv6 |
Addr IPv4 |

makeAddrRange :: Addr a => a -> Int -> AddrRange aSource

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]) 8
    127.0.0.0/8
    >>> makeAddrRange (toIPv6 [0x2001,0xDB8,0,0,0,0,0,1]) 8
    2000:00:00:00:00:00:00:00/8

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

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" 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` address and an `AddrRange`, and returns `True` 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