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