16 prop_all_cidrs_contain_themselves,
17 prop_contains_proper_intransitive
20 import Data.List (nubBy)
22 import Test.QuickCheck
24 import qualified Bit as B
32 data Cidr = None | Cidr { ipv4address :: IPv4Address,
33 maskbits :: Maskbits }
37 instance Show Cidr where
38 show Cidr.None = "None"
39 show cidr = (show (ipv4address cidr)) ++ "/" ++ (show (maskbits cidr))
42 instance Arbitrary Cidr where
44 ipv4 <- arbitrary :: Gen IPv4Address
45 mask <- arbitrary :: Gen Maskbits
46 return (Cidr ipv4 mask)
48 coarbitrary _ = variant 0
50 instance Eq Cidr where
51 cidr1 == cidr2 = (cidr1 `equivalent` cidr2)
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))
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
70 partlist = (splitWith (`elem` "/") s)
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))
80 cidr_from_string :: String -> Cidr
82 | addr == IPv4Address.None = Cidr.None
83 | mbits == Maskbits.None = Cidr.None
84 | otherwise = Cidr addr mbits
86 addr = ipv4address_from_octets (oct1) (oct2) (oct3) (oct4)
91 octs = octets_from_cidr_string s
92 mbits = maskbits_from_cidr_string s
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
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
110 min_octet1 :: Cidr -> Octet
111 min_octet1 cidr = octet1 (min_host cidr)
113 min_octet2 :: Cidr -> Octet
114 min_octet2 cidr = octet2 (min_host cidr)
116 min_octet3 :: Cidr -> Octet
117 min_octet3 cidr = octet3 (min_host cidr)
119 min_octet4 :: Cidr -> Octet
120 min_octet4 cidr = octet4 (min_host cidr)
122 max_octet1 :: Cidr -> Octet
123 max_octet1 cidr = octet1 (max_host cidr)
125 max_octet2 :: Cidr -> Octet
126 max_octet2 cidr = octet2 (max_host cidr)
128 max_octet3 :: Cidr -> Octet
129 max_octet3 cidr = octet3 (max_host cidr)
131 max_octet4 :: Cidr -> Octet
132 max_octet4 cidr = octet4 (max_host cidr)
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
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.
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
162 -- cidr1 = 192.168.1.0/23, cidr2 = 192.168.1.100/24
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..
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.
175 contains (Cidr addr1 mbits1) (Cidr addr2 mbits2)
176 | mbits1 > mbits2 = False
177 | otherwise = addr1masked == addr2masked
179 addr1masked = apply_mask addr1 mbits1 B.Zero
180 addr2masked = apply_mask addr2 mbits1 B.Zero
183 contains_proper :: Cidr -> Cidr -> Bool
184 contains_proper cidr1 cidr2 =
185 (cidr1 `contains` cidr2) && (not (cidr2 `contains` cidr1))
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
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
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]
207 | cidrs == (combine_contained unique_cidrs) = cidrs
208 | otherwise = combine_all (combine_contained unique_cidrs)
210 unique_cidrs = nubBy equivalent valid_cidr_combinations
211 valid_cidr_combinations = filter (/= Cidr.None) cidr_combinations
213 cidrs ++ [ (combine_adjacent x y) | x <- cidrs, y <- cidrs ]
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
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) }
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
242 | mbits1 /= mbits2 = False
243 | mbits1 == Maskbits.Zero = False -- They're equal.
244 | otherwise = (mbits1 == (most_sig_bit_different addr1 addr2))
246 addr1 = ipv4address cidr1
247 addr2 = ipv4address cidr2
248 mbits1 = maskbits cidr1
249 mbits2 = maskbits cidr2
257 test_min_host1 :: Test
259 TestCase $ assertEqual "The minimum host in 10.0.0.0/24 is 10.0.0.0" expected actual
261 actual = show $ min_host (cidr_from_string "10.0.0.0/24")
262 expected = "10.0.0.0"
265 test_max_host1 :: Test
267 TestCase $ assertEqual "The maximum host in 10.0.0.0/24 is 10.0.0.255" expected actual
269 actual = show $ max_host (cidr_from_string "10.0.0.0/24")
270 expected = "10.0.0.255"
273 test_equality1 :: Test
275 TestCase $ assertEqual "10.1.1.0/23 equals itself" True (cidr1 == cidr1)
277 cidr1 = cidr_from_string "10.1.1.0/23"
280 test_contains1 :: Test
282 TestCase $ assertEqual "10.1.1.0/23 contains 10.1.1.0/24" True (cidr1 `contains` cidr2)
284 cidr1 = cidr_from_string "10.1.1.0/23"
285 cidr2 = cidr_from_string "10.1.1.0/24"
288 test_contains2 :: Test
290 TestCase $ assertEqual "10.1.1.0/23 contains itself" True (cidr1 `contains` cidr1)
292 cidr1 = cidr_from_string "10.1.1.0/23"
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)
299 cidr1 = cidr_from_string "10.1.1.0/23"
300 cidr2 = cidr_from_string "10.1.1.0/24"
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)
307 cidr1 = cidr_from_string "10.1.1.0/23"
310 test_adjacent1 :: Test
312 TestCase $ assertEqual "10.1.0.0/24 is adjacent to 10.1.1.0/24" True (cidr1 `adjacent` cidr2)
314 cidr1 = cidr_from_string "10.1.0.0/24"
315 cidr2 = cidr_from_string "10.1.1.0/24"
318 test_adjacent2 :: Test
320 TestCase $ assertEqual "10.1.0.0/23 is not adjacent to 10.1.0.0/24" False (cidr1 `adjacent` cidr2)
322 cidr1 = cidr_from_string "10.1.0.0/23"
323 cidr2 = cidr_from_string "10.1.0.0/24"
326 test_adjacent3 :: Test
328 TestCase $ assertEqual "10.1.0.0/24 is not adjacent to 10.2.5.0/24" False (cidr1 `adjacent` cidr2)
330 cidr1 = cidr_from_string "10.1.0.0/24"
331 cidr2 = cidr_from_string "10.2.5.0/24"
334 test_adjacent4 :: Test
336 TestCase $ assertEqual "10.1.1.0/24 is not adjacent to 10.1.2.0/24" False (cidr1 `adjacent` cidr2)
338 cidr1 = cidr_from_string "10.1.1.0/24"
339 cidr2 = cidr_from_string "10.1.2.0/24"
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)
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]
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])
357 cidr1 = cidr_from_string "192.168.3.0/23"
358 cidr2 = cidr_from_string "192.168.1.0/24"
361 test_combine_all1 :: Test
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)
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]
374 test_combine_all2 :: Test
376 TestCase $ assertEqual "127.0.0.1/32 combines with itself recursively" expected_cidrs (combine_all test_cidrs)
378 cidr1 = cidr_from_string "127.0.0.1/32"
379 expected_cidrs = [cidr1]
380 test_cidrs = [cidr1, cidr1, cidr1, cidr1, cidr1]
383 test_combine_all3 :: Test
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)
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]
396 cidr_tests = [ test_min_host1,
401 test_contains_proper1,
402 test_contains_proper2,
407 test_combine_contained1,
408 test_combine_contained2,
416 prop_all_cidrs_contain_themselves :: Cidr -> Bool
417 prop_all_cidrs_contain_themselves cidr1 = cidr1 `contains` cidr1
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))