]> gitweb.michael.orlitzky.com - hath.git/blob - src/Cidr.hs
08eebcba23fd75a533e687cd742fb6db4a7e6af8
[hath.git] / src / Cidr.hs
1 module Cidr
2 ( Cidr(..),
3 cidr_from_string,
4 combine_all
5 ) where
6
7 import Data.List (nubBy)
8
9 import IPv4Address
10 import ListUtils
11 import Maskable
12 import Maskbits
13 import Octet
14
15
16 data Cidr = None | Cidr { ipv4address :: IPv4Address,
17 maskbits :: Maskbits }
18 deriving (Eq)
19
20
21 instance Show Cidr where
22 show Cidr.None = "None"
23 show cidr = (show (ipv4address cidr)) ++ "/" ++ (show (maskbits cidr))
24
25
26 -- Two CIDR ranges are equivalent if they have the same network bits
27 -- and the masks are the same.
28 equivalent :: Cidr -> Cidr -> Bool
29 equivalent Cidr.None Cidr.None = True
30 equivalent Cidr.None _ = False
31 equivalent _ Cidr.None = False
32 equivalent (Cidr addr1 mbits1) (Cidr addr2 mbits2) =
33 (mbits1 == mbits2) && ((apply_mask addr1 mbits1) == (apply_mask addr2 mbits2))
34
35 -- Returns the mask portion of a CIDR address. That is, everything
36 -- after the trailing slash.
37 maskbits_from_cidr_string :: String -> Maskbits
38 maskbits_from_cidr_string s =
39 maskbits_from_string ((splitWith (`elem` "/") s) !! 1)
40
41
42 -- Takes an IP address String in CIDR notation, and returns a list of
43 -- its octets (as Ints).
44 octets_from_cidr_string :: String -> [Octet]
45 octets_from_cidr_string s =
46 map octet_from_string (take 4 (splitWith (`elem` "./") s))
47
48
49 cidr_from_string :: String -> Cidr
50 cidr_from_string s
51 | addr == IPv4Address.None = Cidr.None
52 | mbits == Maskbits.None = Cidr.None
53 | otherwise = Cidr addr mbits
54 where
55 addr = ipv4address_from_octets (oct1) (oct2) (oct3) (oct4)
56 oct1 = (octs !! 0)
57 oct2 = (octs !! 1)
58 oct3 = (octs !! 2)
59 oct4 = (octs !! 3)
60 octs = octets_from_cidr_string s
61 mbits = maskbits_from_cidr_string s
62
63
64
65 -- Return true if the first argument (a CIDR range) contains the
66 -- second (another CIDR range). There are a lot of ways we can be fed
67 -- junk here. For lack of a better alternative, just return False when
68 -- we are given nonsense.
69 contains :: Cidr -> Cidr -> Bool
70 contains Cidr.None _ = False
71 contains _ Cidr.None = False
72 contains (Cidr _ Maskbits.None) _ = False
73 contains (Cidr IPv4Address.None _) _ = False
74 contains _ (Cidr _ Maskbits.None) = False
75 contains _ (Cidr IPv4Address.None _) = False
76
77 -- If the number of bits in the network part of the first address is
78 -- larger than the number of bits in the second, there is no way that
79 -- the first range can contain the second. For, if the number of
80 -- network bits is larger, then the number of host bits must be
81 -- smaller, and if cidr1 has fewer hosts than cidr2, cidr1 most
82 -- certainly does not contain cidr2.
83 --
84 -- On the other hand, if the first argument (cidr1) has fewer (or the
85 -- same number of) network bits as the second, it can contain the
86 -- second. In this case, we need to check that every host in cidr2 is
87 -- contained in cidr1. If a host in cidr2 is contained in cidr1, then
88 -- at least mbits1 of an address in cidr2 will match cidr1. For
89 -- example,
90 --
91 -- cidr1 = 192.168.1.0/23, cidr2 = 192.168.1.100/24
92 --
93 -- Here, cidr2 contains all of 192.168.1.0 through
94 -- 192.168.1.255. However, cidr1 contains BOTH 192.168.0.0 through
95 -- 192.168.0.255 and 192.168.1.0 through 192.168.1.255. In essence,
96 -- what we want to check is that cidr2 "begins with" something that
97 -- cidr1 CAN begin with. Since cidr1 can begin with 192.168.1, and
98 -- cidr2 DOES, cidr1 contains cidr2..
99 --
100 -- The way that we check this is to apply cidr1's mask to cidr2's
101 -- address and see if the result is the same as cidr1's mask applied
102 -- to cidr1's address.
103 --
104 contains (Cidr addr1 mbits1) (Cidr addr2 mbits2)
105 | mbits1 > mbits2 = False
106 | otherwise = addr1masked == addr2masked
107 where
108 addr1masked = apply_mask addr1 mbits1
109 addr2masked = apply_mask addr2 mbits1
110
111
112 contains_proper :: Cidr -> Cidr -> Bool
113 contains_proper cidr1 cidr2 =
114 (cidr1 `contains` cidr2) && (not (cidr1 == cidr2))
115
116
117 -- A CIDR range is redundant (with respect to the given list) if
118 -- another CIDR range in that list properly contains it.
119 redundant :: [Cidr] -> Cidr -> Bool
120 redundant cidrlist cidr = any ((flip contains_proper) cidr) cidrlist
121
122
123 -- First, we look at all possible pairs of cidrs, and combine the
124 -- adjacent ones in to a new list. Then, we concatenate that list with
125 -- the original one, and filter out all of the redundancies. If two
126 -- adjacent Cidrs are combined into a larger one, they will be removed
127 -- in the second step since the larger Cidr must contain the smaller
128 -- two.
129 combine_all :: [Cidr] -> [Cidr]
130 combine_all cidrs =
131 combine_contained unique_cidrs
132 where
133 unique_cidrs = nubBy equivalent valid_cidr_combinations
134 valid_cidr_combinations = filter (/= Cidr.None) cidr_combinations
135 cidr_combinations =
136 cidrs ++ [ (combine_adjacent x y) | x <- cidrs, y <- cidrs ]
137
138
139 -- Take a list of CIDR ranges and filter out all of the ones that are
140 -- contained entirelt within some other range in the list.
141 combine_contained :: [Cidr] -> [Cidr]
142 combine_contained cidrs =
143 filter (not . (redundant cidrs)) cidrs
144
145
146 -- If the two Cidrs are not adjacent, return Cidr.None. Otherwise,
147 -- decrement the maskbits of cidr1 and return that; it will contain
148 -- both cidr1 and cidr2.
149 combine_adjacent :: Cidr -> Cidr -> Cidr
150 combine_adjacent cidr1 cidr2
151 | not (adjacent cidr1 cidr2) = Cidr.None
152 | (maskbits cidr1 == Zero) = Cidr.None
153 | otherwise = cidr1 { maskbits = decrement (maskbits cidr1) }
154
155
156
157 -- Determine whether or not two CIDR ranges are adjacent. If two
158 -- ranges lie consecutively within the IP space, they can be
159 -- combined. For example, 10.1.0.0/24 and 10.0.1.0/24 are adjacent,
160 -- and can be combined in to 10.1.0.0/23.
161 adjacent :: Cidr -> Cidr -> Bool
162 adjacent Cidr.None _ = False
163 adjacent _ Cidr.None = False
164 adjacent cidr1 cidr2
165 | mbits1 /= mbits2 = False
166 | mbits1 == Maskbits.Zero = False -- They're equal.
167 | otherwise = (mbits1 == (most_sig_bit_different addr1 addr2))
168 where
169 addr1 = ipv4address cidr1
170 addr2 = ipv4address cidr2
171 mbits1 = maskbits cidr1
172 mbits2 = maskbits cidr2