]> gitweb.michael.orlitzky.com - hath.git/blob - src/Cidr.hs
Make combine_all operate recursively.
[hath.git] / src / Cidr.hs
1 module Cidr
2 ( Cidr(..),
3 cidr_from_string,
4 cidr_tests,
5 combine_all,
6 contains,
7 contains_proper,
8 max_octet1,
9 max_octet2,
10 max_octet3,
11 max_octet4,
12 min_octet1,
13 min_octet2,
14 min_octet3,
15 min_octet4,
16 prop_all_cidrs_contain_themselves,
17 prop_contains_proper_intransitive
18 ) where
19
20 import Data.List (nubBy)
21 import Test.HUnit
22 import Test.QuickCheck
23
24 import qualified Bit as B
25 import IPv4Address
26 import ListUtils
27 import Maskable
28 import Maskbits
29 import Octet
30
31
32 data Cidr = None | Cidr { ipv4address :: IPv4Address,
33 maskbits :: Maskbits }
34
35
36
37 instance Show Cidr where
38 show Cidr.None = "None"
39 show cidr = (show (ipv4address cidr)) ++ "/" ++ (show (maskbits cidr))
40
41
42 instance Arbitrary Cidr where
43 arbitrary = do
44 ipv4 <- arbitrary :: Gen IPv4Address
45 mask <- arbitrary :: Gen Maskbits
46 return (Cidr ipv4 mask)
47
48 coarbitrary _ = variant 0
49
50 instance Eq Cidr where
51 cidr1 == cidr2 = (cidr1 `equivalent` cidr2)
52
53
54 -- Two CIDR ranges are equivalent if they have the same network bits
55 -- and the masks are the same.
56 equivalent :: Cidr -> Cidr -> Bool
57 equivalent Cidr.None Cidr.None = True
58 equivalent Cidr.None _ = False
59 equivalent _ Cidr.None = False
60 equivalent (Cidr addr1 mbits1) (Cidr addr2 mbits2) =
61 (mbits1 == mbits2) && ((apply_mask addr1 mbits1 B.Zero) == (apply_mask addr2 mbits2 B.Zero))
62
63 -- Returns the mask portion of a CIDR address. That is, everything
64 -- after the trailing slash.
65 maskbits_from_cidr_string :: String -> Maskbits
66 maskbits_from_cidr_string s
67 | length partlist == 2 = maskbits_from_string (partlist !! 1)
68 | otherwise = Maskbits.None
69 where
70 partlist = (splitWith (`elem` "/") s)
71
72
73 -- Takes an IP address String in CIDR notation, and returns a list of
74 -- its octets (as Ints).
75 octets_from_cidr_string :: String -> [Octet]
76 octets_from_cidr_string s =
77 map octet_from_string (take 4 (splitWith (`elem` "./") s))
78
79
80 cidr_from_string :: String -> Cidr
81 cidr_from_string s
82 | addr == IPv4Address.None = Cidr.None
83 | mbits == Maskbits.None = Cidr.None
84 | otherwise = Cidr addr mbits
85 where
86 addr = ipv4address_from_octets (oct1) (oct2) (oct3) (oct4)
87 oct1 = (octs !! 0)
88 oct2 = (octs !! 1)
89 oct3 = (octs !! 2)
90 oct4 = (octs !! 3)
91 octs = octets_from_cidr_string s
92 mbits = maskbits_from_cidr_string s
93
94
95
96 min_host :: Cidr -> IPv4Address
97 min_host Cidr.None = IPv4Address.None
98 min_host (Cidr IPv4Address.None _) = IPv4Address.None
99 min_host (Cidr _ Maskbits.None) = IPv4Address.None
100 min_host (Cidr addr mask) = apply_mask addr mask B.Zero
101
102
103 max_host :: Cidr -> IPv4Address
104 max_host Cidr.None = IPv4Address.None
105 max_host (Cidr IPv4Address.None _) = IPv4Address.None
106 max_host (Cidr _ Maskbits.None) = IPv4Address.None
107 max_host (Cidr addr mask) = apply_mask addr mask B.One
108
109
110 min_octet1 :: Cidr -> Octet
111 min_octet1 cidr = octet1 (min_host cidr)
112
113 min_octet2 :: Cidr -> Octet
114 min_octet2 cidr = octet2 (min_host cidr)
115
116 min_octet3 :: Cidr -> Octet
117 min_octet3 cidr = octet3 (min_host cidr)
118
119 min_octet4 :: Cidr -> Octet
120 min_octet4 cidr = octet4 (min_host cidr)
121
122 max_octet1 :: Cidr -> Octet
123 max_octet1 cidr = octet1 (max_host cidr)
124
125 max_octet2 :: Cidr -> Octet
126 max_octet2 cidr = octet2 (max_host cidr)
127
128 max_octet3 :: Cidr -> Octet
129 max_octet3 cidr = octet3 (max_host cidr)
130
131 max_octet4 :: Cidr -> Octet
132 max_octet4 cidr = octet4 (max_host cidr)
133
134
135
136 -- Return true if the first argument (a CIDR range) contains the
137 -- second (another CIDR range). There are a lot of ways we can be fed
138 -- junk here. For lack of a better alternative, just return False when
139 -- we are given nonsense.
140 contains :: Cidr -> Cidr -> Bool
141 contains Cidr.None _ = False
142 contains _ Cidr.None = False
143 contains (Cidr _ Maskbits.None) _ = False
144 contains (Cidr IPv4Address.None _) _ = False
145 contains _ (Cidr _ Maskbits.None) = False
146 contains _ (Cidr IPv4Address.None _) = False
147
148 -- If the number of bits in the network part of the first address is
149 -- larger than the number of bits in the second, there is no way that
150 -- the first range can contain the second. For, if the number of
151 -- network bits is larger, then the number of host bits must be
152 -- smaller, and if cidr1 has fewer hosts than cidr2, cidr1 most
153 -- certainly does not contain cidr2.
154 --
155 -- On the other hand, if the first argument (cidr1) has fewer (or the
156 -- same number of) network bits as the second, it can contain the
157 -- second. In this case, we need to check that every host in cidr2 is
158 -- contained in cidr1. If a host in cidr2 is contained in cidr1, then
159 -- at least mbits1 of an address in cidr2 will match cidr1. For
160 -- example,
161 --
162 -- cidr1 = 192.168.1.0/23, cidr2 = 192.168.1.100/24
163 --
164 -- Here, cidr2 contains all of 192.168.1.0 through
165 -- 192.168.1.255. However, cidr1 contains BOTH 192.168.0.0 through
166 -- 192.168.0.255 and 192.168.1.0 through 192.168.1.255. In essence,
167 -- what we want to check is that cidr2 "begins with" something that
168 -- cidr1 CAN begin with. Since cidr1 can begin with 192.168.1, and
169 -- cidr2 DOES, cidr1 contains cidr2..
170 --
171 -- The way that we check this is to apply cidr1's mask to cidr2's
172 -- address and see if the result is the same as cidr1's mask applied
173 -- to cidr1's address.
174 --
175 contains (Cidr addr1 mbits1) (Cidr addr2 mbits2)
176 | mbits1 > mbits2 = False
177 | otherwise = addr1masked == addr2masked
178 where
179 addr1masked = apply_mask addr1 mbits1 B.Zero
180 addr2masked = apply_mask addr2 mbits1 B.Zero
181
182
183 contains_proper :: Cidr -> Cidr -> Bool
184 contains_proper cidr1 cidr2 =
185 (cidr1 `contains` cidr2) && (not (cidr2 `contains` cidr1))
186
187
188 -- A CIDR range is redundant (with respect to the given list) if
189 -- another CIDR range in that list properly contains it.
190 redundant :: [Cidr] -> Cidr -> Bool
191 redundant cidrlist cidr = any ((flip contains_proper) cidr) cidrlist
192
193
194 -- First, we look at all possible pairs of cidrs, and combine the
195 -- adjacent ones in to a new list. Then, we concatenate that list with
196 -- the original one, and filter out all of the redundancies. If two
197 -- adjacent Cidrs are combined into a larger one, they will be removed
198 -- in the second step since the larger Cidr must contain the smaller
199 -- two.
200 --
201 -- Once this is done, we see whether or not the result is different
202 -- than the argument that was passed in. If nothing changed, we're
203 -- done and return the list that was passed to us. However, if
204 -- something changed, we recurse and try to combine the list again.
205 combine_all :: [Cidr] -> [Cidr]
206 combine_all cidrs
207 | cidrs == (combine_contained unique_cidrs) = cidrs
208 | otherwise = combine_all (combine_contained unique_cidrs)
209 where
210 unique_cidrs = nubBy equivalent valid_cidr_combinations
211 valid_cidr_combinations = filter (/= Cidr.None) cidr_combinations
212 cidr_combinations =
213 cidrs ++ [ (combine_adjacent x y) | x <- cidrs, y <- cidrs ]
214
215
216 -- Take a list of CIDR ranges and filter out all of the ones that are
217 -- contained entirelt within some other range in the list.
218 combine_contained :: [Cidr] -> [Cidr]
219 combine_contained cidrs =
220 filter (not . (redundant cidrs)) cidrs
221
222
223 -- If the two Cidrs are not adjacent, return Cidr.None. Otherwise,
224 -- decrement the maskbits of cidr1 and return that; it will contain
225 -- both cidr1 and cidr2.
226 combine_adjacent :: Cidr -> Cidr -> Cidr
227 combine_adjacent cidr1 cidr2
228 | not (adjacent cidr1 cidr2) = Cidr.None
229 | (maskbits cidr1 == Zero) = Cidr.None
230 | otherwise = cidr1 { maskbits = decrement (maskbits cidr1) }
231
232
233
234 -- Determine whether or not two CIDR ranges are adjacent. If two
235 -- ranges lie consecutively within the IP space, they can be
236 -- combined. For example, 10.1.0.0/24 and 10.0.1.0/24 are adjacent,
237 -- and can be combined in to 10.1.0.0/23.
238 adjacent :: Cidr -> Cidr -> Bool
239 adjacent Cidr.None _ = False
240 adjacent _ Cidr.None = False
241 adjacent cidr1 cidr2
242 | mbits1 /= mbits2 = False
243 | mbits1 == Maskbits.Zero = False -- They're equal.
244 | otherwise = (mbits1 == (most_sig_bit_different addr1 addr2))
245 where
246 addr1 = ipv4address cidr1
247 addr2 = ipv4address cidr2
248 mbits1 = maskbits cidr1
249 mbits2 = maskbits cidr2
250
251
252
253
254
255 -- HUnit Tests
256
257 test_min_host1 :: Test
258 test_min_host1 =
259 TestCase $ assertEqual "The minimum host in 10.0.0.0/24 is 10.0.0.0" expected actual
260 where
261 actual = show $ min_host (cidr_from_string "10.0.0.0/24")
262 expected = "10.0.0.0"
263
264
265 test_max_host1 :: Test
266 test_max_host1 =
267 TestCase $ assertEqual "The maximum host in 10.0.0.0/24 is 10.0.0.255" expected actual
268 where
269 actual = show $ max_host (cidr_from_string "10.0.0.0/24")
270 expected = "10.0.0.255"
271
272
273 test_equality1 :: Test
274 test_equality1 =
275 TestCase $ assertEqual "10.1.1.0/23 equals itself" True (cidr1 == cidr1)
276 where
277 cidr1 = cidr_from_string "10.1.1.0/23"
278
279
280 test_contains1 :: Test
281 test_contains1 =
282 TestCase $ assertEqual "10.1.1.0/23 contains 10.1.1.0/24" True (cidr1 `contains` cidr2)
283 where
284 cidr1 = cidr_from_string "10.1.1.0/23"
285 cidr2 = cidr_from_string "10.1.1.0/24"
286
287
288 test_contains2 :: Test
289 test_contains2 =
290 TestCase $ assertEqual "10.1.1.0/23 contains itself" True (cidr1 `contains` cidr1)
291 where
292 cidr1 = cidr_from_string "10.1.1.0/23"
293
294
295 test_contains_proper1 :: Test
296 test_contains_proper1 =
297 TestCase $ assertEqual "10.1.1.0/23 contains 10.1.1.0/24 properly" True (cidr1 `contains_proper` cidr2)
298 where
299 cidr1 = cidr_from_string "10.1.1.0/23"
300 cidr2 = cidr_from_string "10.1.1.0/24"
301
302
303 test_contains_proper2 :: Test
304 test_contains_proper2 =
305 TestCase $ assertEqual "10.1.1.0/23 does not contain itself properly" False (cidr1 `contains_proper` cidr1)
306 where
307 cidr1 = cidr_from_string "10.1.1.0/23"
308
309
310 test_adjacent1 :: Test
311 test_adjacent1 =
312 TestCase $ assertEqual "10.1.0.0/24 is adjacent to 10.1.1.0/24" True (cidr1 `adjacent` cidr2)
313 where
314 cidr1 = cidr_from_string "10.1.0.0/24"
315 cidr2 = cidr_from_string "10.1.1.0/24"
316
317
318 test_adjacent2 :: Test
319 test_adjacent2 =
320 TestCase $ assertEqual "10.1.0.0/23 is not adjacent to 10.1.0.0/24" False (cidr1 `adjacent` cidr2)
321 where
322 cidr1 = cidr_from_string "10.1.0.0/23"
323 cidr2 = cidr_from_string "10.1.0.0/24"
324
325
326 test_adjacent3 :: Test
327 test_adjacent3 =
328 TestCase $ assertEqual "10.1.0.0/24 is not adjacent to 10.2.5.0/24" False (cidr1 `adjacent` cidr2)
329 where
330 cidr1 = cidr_from_string "10.1.0.0/24"
331 cidr2 = cidr_from_string "10.2.5.0/24"
332
333
334 test_adjacent4 :: Test
335 test_adjacent4 =
336 TestCase $ assertEqual "10.1.1.0/24 is not adjacent to 10.1.2.0/24" False (cidr1 `adjacent` cidr2)
337 where
338 cidr1 = cidr_from_string "10.1.1.0/24"
339 cidr2 = cidr_from_string "10.1.2.0/24"
340
341
342 test_combine_contained1 :: Test
343 test_combine_contained1 =
344 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_contained test_cidrs)
345 where
346 cidr1 = cidr_from_string "10.0.0.0/8"
347 cidr2 = cidr_from_string "10.1.0.0/16"
348 cidr3 = cidr_from_string "10.1.1.0/24"
349 expected_cidrs = [cidr1]
350 test_cidrs = [cidr1, cidr2, cidr3]
351
352
353 test_combine_contained2 :: Test
354 test_combine_contained2 =
355 TestCase $ assertEqual "192.168.3.0/23 does not contain 192.168.1.0/24" [cidr1, cidr2] (combine_contained [cidr1, cidr2])
356 where
357 cidr1 = cidr_from_string "192.168.3.0/23"
358 cidr2 = cidr_from_string "192.168.1.0/24"
359
360
361 test_combine_all1 :: Test
362 test_combine_all1 =
363 TestCase $ assertEqual "10.0.0.0/24 is adjacent to 10.0.1.0/24 and 10.0.3.0/23 contains 10.0.2.0/24" expected_cidrs (combine_all test_cidrs)
364 where
365 cidr1 = cidr_from_string "10.0.0.0/24"
366 cidr2 = cidr_from_string "10.0.1.0/24"
367 cidr3 = cidr_from_string "10.0.2.0/24"
368 cidr4 = cidr_from_string "10.0.3.0/23"
369 cidr5 = cidr_from_string "10.0.0.0/23"
370 expected_cidrs = [cidr_from_string "10.0.0.0/22"]
371 test_cidrs = [cidr1, cidr2, cidr3, cidr4, cidr5]
372
373
374 test_combine_all2 :: Test
375 test_combine_all2 =
376 TestCase $ assertEqual "127.0.0.1/32 combines with itself recursively" expected_cidrs (combine_all test_cidrs)
377 where
378 cidr1 = cidr_from_string "127.0.0.1/32"
379 expected_cidrs = [cidr1]
380 test_cidrs = [cidr1, cidr1, cidr1, cidr1, cidr1]
381
382
383 test_combine_all3 :: Test
384 test_combine_all3 =
385 TestCase $ assertEqual "10.0.0.16, 10.0.0.17, 10.0.0.18, and 10.0.0.19 get combined into 10.0.0.16/30" expected_cidrs (combine_all test_cidrs)
386 where
387 cidr1 = cidr_from_string "10.0.0.16/32"
388 cidr2 = cidr_from_string "10.0.0.17/32"
389 cidr3 = cidr_from_string "10.0.0.18/32"
390 cidr4 = cidr_from_string "10.0.0.19/32"
391 expected_cidrs = [cidr_from_string "10.0.0.16/30"]
392 test_cidrs = [cidr1, cidr2, cidr3, cidr4]
393
394
395 cidr_tests :: [Test]
396 cidr_tests = [ test_min_host1,
397 test_max_host1,
398 test_equality1,
399 test_contains1,
400 test_contains2,
401 test_contains_proper1,
402 test_contains_proper2,
403 test_adjacent1,
404 test_adjacent2,
405 test_adjacent3,
406 test_adjacent4,
407 test_combine_contained1,
408 test_combine_contained2,
409 test_combine_all1,
410 test_combine_all2,
411 test_combine_all3
412 ]
413
414
415 -- QuickCheck Tests
416 prop_all_cidrs_contain_themselves :: Cidr -> Bool
417 prop_all_cidrs_contain_themselves cidr1 = cidr1 `contains` cidr1
418
419
420 -- If cidr1 properly contains cidr2, then by definition cidr2
421 -- does not properly contain cidr1.
422 prop_contains_proper_intransitive :: Cidr -> Cidr -> Property
423 prop_contains_proper_intransitive cidr1 cidr2 =
424 (cidr1 `contains_proper` cidr2) ==>
425 (not (cidr2 `contains_proper` cidr1))