Safe Haskell | None |
---|---|
Language | Haskell2010 |
IP routing table is a tree of IPRange
to search one of them on the longest
match base. It is a kind of TRIE with one
way branching removed. Both IPv4 and IPv6
are supported.
For more information, see: http://www.mew.org/~kazu/proj/iproute/
- class Addr a => Routable a where
- data IPRTable k a
- empty :: Routable k => IPRTable k a
- insert :: Routable k => AddrRange k -> a -> IPRTable k a -> IPRTable k a
- delete :: Routable k => AddrRange k -> IPRTable k a -> IPRTable k a
- lookup :: Routable k => AddrRange k -> IPRTable k a -> Maybe a
- lookupKeyValue :: Routable k => AddrRange k -> IPRTable k a -> Maybe (AddrRange k, a)
- findMatch :: MonadPlus m => Routable k => AddrRange k -> IPRTable k a -> m (AddrRange k, a)
- fromList :: Routable k => [(AddrRange k, a)] -> IPRTable k a
- toList :: Routable k => IPRTable k a -> [(AddrRange k, a)]
Documentation
Routable class
class Addr a => Routable a where Source #
A class to contain IPv4 and IPv6.
Type for IP routing table
The Tree structure for IP routing table based on TRIE with
one way branching removed. This is an abstract data type,
so you cannot touch its inside. Please use insert
or lookup
, instead.
Functor (IPRTable k) Source # | |
Foldable (IPRTable k) Source # | |
Traversable (IPRTable k) Source # | |
Generic1 * (IPRTable k) Source # | |
(Eq a, Eq k) => Eq (IPRTable k a) Source # | |
(Show a, Show k) => Show (IPRTable k a) Source # | |
Generic (IPRTable k a) Source # | |
type Rep1 * (IPRTable k) Source # | |
type Rep (IPRTable k a) Source # | |
Functions to manipulate an IP routing table
empty :: Routable k => IPRTable k a Source #
The empty
function returns an empty IP routing table.
>>>
(empty :: IPRTable IPv4 ()) == fromList []
True
lookup :: Routable k => AddrRange k -> IPRTable k a -> Maybe a Source #
The lookup
function looks up IPRTable
with a key of AddrRange
.
If a routing information in IPRTable
matches the key, its value
is returned.
>>>
let v4 = ["133.4.0.0/16","133.5.0.0/16","133.5.16.0/24","133.5.23.0/24"] :: [AddrRange IPv4]
>>>
let rt = fromList $ zip v4 v4
>>>
lookup "127.0.0.1" rt
Nothing>>>
lookup "133.3.0.1" rt
Nothing>>>
lookup "133.4.0.0" rt
Just 133.4.0.0/16>>>
lookup "133.4.0.1" rt
Just 133.4.0.0/16>>>
lookup "133.5.16.0" rt
Just 133.5.16.0/24>>>
lookup "133.5.16.1" rt
Just 133.5.16.0/24
lookupKeyValue :: Routable k => AddrRange k -> IPRTable k a -> Maybe (AddrRange k, a) Source #
The lookupKeyValue
function looks up IPRTable
with a key of AddrRange
.
If a routing information in IPRTable
matches the key, both key and value
are returned.
>>>
:set -XOverloadedStrings
>>>
let rt = fromList ([("192.168.0.0/24", 1), ("10.10.0.0/16", 2)] :: [(AddrRange IPv4, Int)])
>>>
lookupKeyValue "127.0.0.1" rt
Nothing>>>
lookupKeyValue "192.168.0.1" rt
Just (192.168.0.0/24,1)>>>
lookupKeyValue "10.10.0.1" rt
Just (10.10.0.0/16,2)
findMatch :: MonadPlus m => Routable k => AddrRange k -> IPRTable k a -> m (AddrRange k, a) Source #
The findMatch
function looks up IPRTable
with a key of AddrRange
.
If the key matches routing informations in IPRTable
, they are
returned.
>>>
let v4 = ["133.4.0.0/16","133.5.0.0/16","133.5.16.0/24","133.5.23.0/24"] :: [AddrRange IPv4]
>>>
let rt = fromList $ zip v4 $ repeat ()
>>>
findMatch "133.4.0.0/15" rt :: [(AddrRange IPv4,())]
[(133.4.0.0/16,()),(133.5.0.0/16,()),(133.5.16.0/24,()),(133.5.23.0/24,())]