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