]> gitweb.michael.orlitzky.com - hath.git/blob - src/Cidr.hs
Added Arbitrary instance definitions for the main data types.
[hath.git] / src / Cidr.hs
1 module Cidr
2 ( Cidr(..),
3 cidr_from_string,
4 cidr_tests,
5 combine_all
6 ) where
7
8 import Data.List (nubBy)
9 import Test.HUnit
10 import Test.QuickCheck
11
12 import IPv4Address
13 import ListUtils
14 import Maskable
15 import Maskbits
16 import Octet
17
18
19 data Cidr = None | Cidr { ipv4address :: IPv4Address,
20 maskbits :: Maskbits }
21 deriving (Eq)
22
23
24 instance Show Cidr where
25 show Cidr.None = "None"
26 show cidr = (show (ipv4address cidr)) ++ "/" ++ (show (maskbits cidr))
27
28
29 instance Arbitrary Cidr where
30 arbitrary = do
31 ipv4 <- arbitrary :: Gen IPv4Address
32 mask <- arbitrary :: Gen Maskbits
33 return (Cidr ipv4 mask)
34
35 coarbitrary _ = variant 0
36
37
38 -- Two CIDR ranges are equivalent if they have the same network bits
39 -- and the masks are the same.
40 equivalent :: Cidr -> Cidr -> Bool
41 equivalent Cidr.None Cidr.None = True
42 equivalent Cidr.None _ = False
43 equivalent _ Cidr.None = False
44 equivalent (Cidr addr1 mbits1) (Cidr addr2 mbits2) =
45 (mbits1 == mbits2) && ((apply_mask addr1 mbits1) == (apply_mask addr2 mbits2))
46
47 -- Returns the mask portion of a CIDR address. That is, everything
48 -- after the trailing slash.
49 maskbits_from_cidr_string :: String -> Maskbits
50 maskbits_from_cidr_string s =
51 maskbits_from_string ((splitWith (`elem` "/") s) !! 1)
52
53
54 -- Takes an IP address String in CIDR notation, and returns a list of
55 -- its octets (as Ints).
56 octets_from_cidr_string :: String -> [Octet]
57 octets_from_cidr_string s =
58 map octet_from_string (take 4 (splitWith (`elem` "./") s))
59
60
61 cidr_from_string :: String -> Cidr
62 cidr_from_string s
63 | addr == IPv4Address.None = Cidr.None
64 | mbits == Maskbits.None = Cidr.None
65 | otherwise = Cidr addr mbits
66 where
67 addr = ipv4address_from_octets (oct1) (oct2) (oct3) (oct4)
68 oct1 = (octs !! 0)
69 oct2 = (octs !! 1)
70 oct3 = (octs !! 2)
71 oct4 = (octs !! 3)
72 octs = octets_from_cidr_string s
73 mbits = maskbits_from_cidr_string s
74
75
76
77 -- Return true if the first argument (a CIDR range) contains the
78 -- second (another CIDR range). There are a lot of ways we can be fed
79 -- junk here. For lack of a better alternative, just return False when
80 -- we are given nonsense.
81 contains :: Cidr -> Cidr -> Bool
82 contains Cidr.None _ = False
83 contains _ Cidr.None = False
84 contains (Cidr _ Maskbits.None) _ = False
85 contains (Cidr IPv4Address.None _) _ = False
86 contains _ (Cidr _ Maskbits.None) = False
87 contains _ (Cidr IPv4Address.None _) = False
88
89 -- If the number of bits in the network part of the first address is
90 -- larger than the number of bits in the second, there is no way that
91 -- the first range can contain the second. For, if the number of
92 -- network bits is larger, then the number of host bits must be
93 -- smaller, and if cidr1 has fewer hosts than cidr2, cidr1 most
94 -- certainly does not contain cidr2.
95 --
96 -- On the other hand, if the first argument (cidr1) has fewer (or the
97 -- same number of) network bits as the second, it can contain the
98 -- second. In this case, we need to check that every host in cidr2 is
99 -- contained in cidr1. If a host in cidr2 is contained in cidr1, then
100 -- at least mbits1 of an address in cidr2 will match cidr1. For
101 -- example,
102 --
103 -- cidr1 = 192.168.1.0/23, cidr2 = 192.168.1.100/24
104 --
105 -- Here, cidr2 contains all of 192.168.1.0 through
106 -- 192.168.1.255. However, cidr1 contains BOTH 192.168.0.0 through
107 -- 192.168.0.255 and 192.168.1.0 through 192.168.1.255. In essence,
108 -- what we want to check is that cidr2 "begins with" something that
109 -- cidr1 CAN begin with. Since cidr1 can begin with 192.168.1, and
110 -- cidr2 DOES, cidr1 contains cidr2..
111 --
112 -- The way that we check this is to apply cidr1's mask to cidr2's
113 -- address and see if the result is the same as cidr1's mask applied
114 -- to cidr1's address.
115 --
116 contains (Cidr addr1 mbits1) (Cidr addr2 mbits2)
117 | mbits1 > mbits2 = False
118 | otherwise = addr1masked == addr2masked
119 where
120 addr1masked = apply_mask addr1 mbits1
121 addr2masked = apply_mask addr2 mbits1
122
123
124 contains_proper :: Cidr -> Cidr -> Bool
125 contains_proper cidr1 cidr2 =
126 (cidr1 `contains` cidr2) && (not (cidr1 == cidr2))
127
128
129 -- A CIDR range is redundant (with respect to the given list) if
130 -- another CIDR range in that list properly contains it.
131 redundant :: [Cidr] -> Cidr -> Bool
132 redundant cidrlist cidr = any ((flip contains_proper) cidr) cidrlist
133
134
135 -- First, we look at all possible pairs of cidrs, and combine the
136 -- adjacent ones in to a new list. Then, we concatenate that list with
137 -- the original one, and filter out all of the redundancies. If two
138 -- adjacent Cidrs are combined into a larger one, they will be removed
139 -- in the second step since the larger Cidr must contain the smaller
140 -- two.
141 combine_all :: [Cidr] -> [Cidr]
142 combine_all cidrs =
143 combine_contained unique_cidrs
144 where
145 unique_cidrs = nubBy equivalent valid_cidr_combinations
146 valid_cidr_combinations = filter (/= Cidr.None) cidr_combinations
147 cidr_combinations =
148 cidrs ++ [ (combine_adjacent x y) | x <- cidrs, y <- cidrs ]
149
150
151 -- Take a list of CIDR ranges and filter out all of the ones that are
152 -- contained entirelt within some other range in the list.
153 combine_contained :: [Cidr] -> [Cidr]
154 combine_contained cidrs =
155 filter (not . (redundant cidrs)) cidrs
156
157
158 -- If the two Cidrs are not adjacent, return Cidr.None. Otherwise,
159 -- decrement the maskbits of cidr1 and return that; it will contain
160 -- both cidr1 and cidr2.
161 combine_adjacent :: Cidr -> Cidr -> Cidr
162 combine_adjacent cidr1 cidr2
163 | not (adjacent cidr1 cidr2) = Cidr.None
164 | (maskbits cidr1 == Zero) = Cidr.None
165 | otherwise = cidr1 { maskbits = decrement (maskbits cidr1) }
166
167
168
169 -- Determine whether or not two CIDR ranges are adjacent. If two
170 -- ranges lie consecutively within the IP space, they can be
171 -- combined. For example, 10.1.0.0/24 and 10.0.1.0/24 are adjacent,
172 -- and can be combined in to 10.1.0.0/23.
173 adjacent :: Cidr -> Cidr -> Bool
174 adjacent Cidr.None _ = False
175 adjacent _ Cidr.None = False
176 adjacent cidr1 cidr2
177 | mbits1 /= mbits2 = False
178 | mbits1 == Maskbits.Zero = False -- They're equal.
179 | otherwise = (mbits1 == (most_sig_bit_different addr1 addr2))
180 where
181 addr1 = ipv4address cidr1
182 addr2 = ipv4address cidr2
183 mbits1 = maskbits cidr1
184 mbits2 = maskbits cidr2
185
186
187
188
189
190 -- HUnit Tests
191
192 test_equality1 :: Test
193 test_equality1 =
194 TestCase $ assertEqual "10.1.1.0/23 equals itself" True (cidr1 == cidr1)
195 where
196 cidr1 = cidr_from_string "10.1.1.0/23"
197
198
199 test_contains1 :: Test
200 test_contains1 =
201 TestCase $ assertEqual "10.1.1.0/23 contains 10.1.1.0/24" True (cidr1 `contains` cidr2)
202 where
203 cidr1 = cidr_from_string "10.1.1.0/23"
204 cidr2 = cidr_from_string "10.1.1.0/24"
205
206
207 test_contains2 :: Test
208 test_contains2 =
209 TestCase $ assertEqual "10.1.1.0/23 contains itself" True (cidr1 `contains` cidr1)
210 where
211 cidr1 = cidr_from_string "10.1.1.0/23"
212
213
214 test_contains_proper1 :: Test
215 test_contains_proper1 =
216 TestCase $ assertEqual "10.1.1.0/23 contains 10.1.1.0/24 properly" True (cidr1 `contains_proper` cidr2)
217 where
218 cidr1 = cidr_from_string "10.1.1.0/23"
219 cidr2 = cidr_from_string "10.1.1.0/24"
220
221
222 test_contains_proper2 :: Test
223 test_contains_proper2 =
224 TestCase $ assertEqual "10.1.1.0/23 does not contain itself properly" False (cidr1 `contains_proper` cidr1)
225 where
226 cidr1 = cidr_from_string "10.1.1.0/23"
227
228
229 test_adjacent1 :: Test
230 test_adjacent1 =
231 TestCase $ assertEqual "10.1.0.0/24 is adjacent to 10.1.1.0/24" True (cidr1 `adjacent` cidr2)
232 where
233 cidr1 = cidr_from_string "10.1.0.0/24"
234 cidr2 = cidr_from_string "10.1.1.0/24"
235
236
237 test_adjacent2 :: Test
238 test_adjacent2 =
239 TestCase $ assertEqual "10.1.0.0/23 is not adjacent to 10.1.0.0/24" False (cidr1 `adjacent` cidr2)
240 where
241 cidr1 = cidr_from_string "10.1.0.0/23"
242 cidr2 = cidr_from_string "10.1.0.0/24"
243
244
245 test_adjacent3 :: Test
246 test_adjacent3 =
247 TestCase $ assertEqual "10.1.0.0/24 is not adjacent to 10.2.5.0/24" False (cidr1 `adjacent` cidr2)
248 where
249 cidr1 = cidr_from_string "10.1.0.0/24"
250 cidr2 = cidr_from_string "10.2.5.0/24"
251
252
253 test_adjacent4 :: Test
254 test_adjacent4 =
255 TestCase $ assertEqual "10.1.1.0/24 is not adjacent to 10.1.2.0/24" False (cidr1 `adjacent` cidr2)
256 where
257 cidr1 = cidr_from_string "10.1.1.0/24"
258 cidr2 = cidr_from_string "10.1.2.0/24"
259
260
261 test_combine_contained1 :: Test
262 test_combine_contained1 =
263 TestCase $ assertEqual "10.0.0.0/8, 10.1.0.0/16, and 10.1.1.0/24 combine to 10.0.0.0/8" expected_cidrs (combine_all test_cidrs)
264 where
265 cidr1 = cidr_from_string "10.0.0.0/8"
266 cidr2 = cidr_from_string "10.1.0.0/16"
267 cidr3 = cidr_from_string "10.1.1.0/24"
268 expected_cidrs = [cidr1]
269 test_cidrs = [cidr1, cidr2, cidr3]
270
271
272 test_combine_contained2 :: Test
273 test_combine_contained2 =
274 TestCase $ assertEqual "192.168.1.0/24 combines with itself" [cidr1] (combine_all [cidr1, cidr1])
275 where
276 cidr1 = cidr_from_string "192.168.1.0/24"
277
278
279 cidr_tests :: [Test]
280 cidr_tests = [ test_equality1,
281 test_contains1,
282 test_contains2,
283 test_contains_proper1,
284 test_contains_proper2,
285 test_adjacent1,
286 test_adjacent2,
287 test_adjacent3,
288 test_adjacent4,
289 test_combine_contained1,
290 test_combine_contained2
291 ]