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