]> gitweb.michael.orlitzky.com - hath.git/blob - src/IPv4Address.hs
85d86616023b19f3cc936ac2f69f1b65020df880
[hath.git] / src / IPv4Address.hs
1 module IPv4Address(
2 IPv4Address(..),
3 ipv4address_properties,
4 ipv4address_tests,
5 most_sig_bit_different )
6 where
7
8 import Data.Word (Word32)
9
10 import Test.Tasty ( TestTree, testGroup )
11 import Test.Tasty.HUnit ( (@?=), testCase )
12 import Test.Tasty.QuickCheck (
13 Arbitrary( arbitrary ),
14 Gen,
15 Property,
16 (==>),
17 testProperty )
18
19 import Maskable ( Maskable( apply_mask) )
20 import Maskbits (
21 Maskbits(
22 Zero, One, Two, Three, Four, Five, Six, Seven, Eight,
23 Nine, Ten, Eleven, Twelve, Thirteen, Fourteen, Fifteen, Sixteen,
24 Seventeen, Eighteen, Nineteen, Twenty, TwentyOne, TwentyTwo, TwentyThree,
25 TwentyFour, TwentyFive, TwentySix, TwentySeven, TwentyEight, TwentyNine,
26 Thirty, ThirtyOne, ThirtyTwo ) )
27 import Octet ( Octet( b1, b2, b3, b4, b5, b6, b7, b8) )
28
29 data IPv4Address =
30 IPv4Address { octet1 :: Octet,
31 octet2 :: Octet,
32 octet3 :: Octet,
33 octet4 :: Octet }
34 deriving (Eq, Ord)
35
36
37 instance Show IPv4Address where
38 show addr = concat [(show oct1) ++ ".",
39 (show oct2) ++ ".",
40 (show oct3) ++ ".",
41 (show oct4)]
42 where
43 oct1 = (octet1 addr)
44 oct2 = (octet2 addr)
45 oct3 = (octet3 addr)
46 oct4 = (octet4 addr)
47
48
49 instance Arbitrary IPv4Address where
50 arbitrary = do
51 oct1 <- arbitrary :: Gen Octet
52 oct2 <- arbitrary :: Gen Octet
53 oct3 <- arbitrary :: Gen Octet
54 oct4 <- arbitrary :: Gen Octet
55 return (IPv4Address oct1 oct2 oct3 oct4)
56
57
58
59 instance Maskable IPv4Address where
60
61 apply_mask addr mask bit =
62 apply_mask' mask
63 where
64 oct1 = octet1 addr
65 oct2 = octet2 addr
66 oct3 = octet3 addr
67 oct4 = octet4 addr
68
69 -- A copy of 'addr' with the fourth octet zeroed (or oned).
70 new_addr1 = addr { octet4 = (apply_mask oct4 Zero bit) }
71
72 -- Likewise for new_addr1's third octet.
73 new_addr2 = new_addr1 { octet3 = (apply_mask oct3 Zero bit) }
74
75 -- And new_addr2's second octet.
76 new_addr3 = new_addr2 { octet2 = (apply_mask oct2 Zero bit) }
77
78 -- This helper function allows us to pattern-match cleanly.
79 apply_mask' :: Maskbits -> IPv4Address
80
81 apply_mask' ThirtyTwo = addr
82
83 apply_mask' ThirtyOne = addr { octet4 = (apply_mask oct4 Seven bit) }
84
85 apply_mask' Thirty =
86 addr { octet4 = (apply_mask oct4 Six bit) }
87
88 apply_mask' TwentyNine =
89 addr { octet4 = (apply_mask oct4 Five bit) }
90
91 apply_mask' TwentyEight =
92 addr { octet4 = (apply_mask oct4 Four bit) }
93
94 apply_mask' TwentySeven =
95 addr { octet4 = (apply_mask oct4 Three bit) }
96
97 apply_mask' TwentySix =
98 addr { octet4 = (apply_mask oct4 Two bit) }
99
100 apply_mask' TwentyFive =
101 addr { octet4 = (apply_mask oct4 One bit) }
102
103 apply_mask' TwentyFour = new_addr1
104
105 apply_mask' TwentyThree =
106 new_addr1 { octet3 = (apply_mask oct3 Seven bit) }
107
108 apply_mask' TwentyTwo =
109 new_addr1 { octet3 = (apply_mask oct3 Six bit) }
110
111 apply_mask' TwentyOne =
112 new_addr1 { octet3 = (apply_mask oct3 Five bit) }
113
114 apply_mask' Twenty =
115 new_addr1 { octet3 = (apply_mask oct3 Four bit) }
116
117 apply_mask' Nineteen =
118 new_addr1 { octet3 = (apply_mask oct3 Three bit) }
119
120 apply_mask' Eighteen =
121 new_addr1 { octet3 = (apply_mask oct3 Two bit) }
122
123 apply_mask' Seventeen =
124 new_addr1 { octet3 = (apply_mask oct3 One bit) }
125
126 apply_mask' Sixteen =
127 new_addr2
128
129 apply_mask' Fifteen =
130 new_addr2 { octet2 = (apply_mask oct2 Seven bit) }
131
132 apply_mask' Fourteen =
133 new_addr2 { octet2 = (apply_mask oct2 Six bit) }
134
135 apply_mask' Thirteen =
136 new_addr2 { octet2 = (apply_mask oct2 Five bit) }
137
138 apply_mask' Twelve =
139 new_addr2 { octet2 = (apply_mask oct2 Four bit) }
140
141 apply_mask' Eleven =
142 new_addr2 { octet2 = (apply_mask oct2 Three bit) }
143
144 apply_mask' Ten =
145 new_addr2 { octet2 = (apply_mask oct2 Two bit) }
146
147 apply_mask' Nine =
148 new_addr2 { octet2 = (apply_mask oct2 One bit) }
149
150 apply_mask' Eight =
151 new_addr3 { octet2 = (apply_mask oct2 Zero bit) }
152
153 apply_mask' Seven =
154 new_addr3 { octet1 = (apply_mask oct1 Seven bit) }
155
156 apply_mask' Six =
157 new_addr3 { octet1 = (apply_mask oct1 Six bit) }
158
159 apply_mask' Five =
160 new_addr3 { octet1 = (apply_mask oct1 Five bit) }
161
162 apply_mask' Four =
163 new_addr3 { octet1 = (apply_mask oct1 Four bit) }
164
165 apply_mask' Three =
166 new_addr3 { octet1 = (apply_mask oct1 Three bit) }
167
168 apply_mask' Two =
169 new_addr3 { octet1 = (apply_mask oct1 Two bit) }
170
171 apply_mask' One =
172 new_addr3 { octet1 = (apply_mask oct1 One bit) }
173
174 apply_mask' Zero =
175 new_addr3 { octet1 = (apply_mask oct1 Zero bit) }
176
177
178 instance Bounded IPv4Address where
179 -- | The minimum possible IPv4 address, 0.0.0.0.
180 minBound = IPv4Address minBound minBound minBound minBound
181
182 -- | The maximum possible IPv4 address, 255.255.255.255.
183 maxBound = IPv4Address maxBound maxBound maxBound maxBound
184
185
186
187
188 instance Enum IPv4Address where
189 -- | Convert an 'Int' @x@ to an 'IPv4Address'. Each octet of @x@ is
190 -- right-shifted by the appropriate number of bits, and the fractional
191 -- part is dropped.
192 toEnum y =
193 IPv4Address oct1 oct2 oct3 oct4
194 where
195 -- Convert the input Int to a Word32 before we proceed. On x86,
196 -- the Int that we get could be negative (half of all IP
197 -- addresses correspond to negative numbers), and then the magic
198 -- below doesn't work. The Word32 type is unsigned, so we do the
199 -- math on that and then convert everything back to Int later on
200 -- once we have four much-smaller non-negative numbers.
201 x = fromIntegral y :: Word32
202
203 -- Chop off the higher octets. x1 = x `mod` 2^32, would be
204 -- redundant.
205 x2 = x `mod` 2^(24 :: Integer)
206 x3 = x `mod` 2^(16 :: Integer)
207 x4 = (fromIntegral $ x `mod` 2^(8 :: Integer)) :: Int
208 -- Perform right-shifts. x4 doesn't need a shift.
209 shifted_x1 = (fromIntegral $ x `quot` 2^(24 :: Integer)) :: Int
210 shifted_x2 = (fromIntegral $ x2 `quot` 2^(16 :: Integer)) :: Int
211 shifted_x3 = fromIntegral $ x3 `quot` 2^(8 :: Integer) :: Int
212 oct1 = toEnum shifted_x1 :: Octet
213 oct2 = toEnum shifted_x2 :: Octet
214 oct3 = toEnum shifted_x3 :: Octet
215 oct4 = toEnum x4 :: Octet
216
217 -- | Convert @addr@ to an 'Int' by converting each octet to an 'Int'
218 -- and shifting the result to the left by 0,8.16, or 24 bits.
219 fromEnum addr =
220 (shifted_oct1) + (shifted_oct2) + (shifted_oct3) + oct4
221 where
222 oct1 = fromEnum (octet1 addr)
223 oct2 = fromEnum (octet2 addr)
224 oct3 = fromEnum (octet3 addr)
225 oct4 = fromEnum (octet4 addr)
226 shifted_oct1 = oct1 * 2^(24 :: Integer)
227 shifted_oct2 = oct2 * 2^(16 :: Integer)
228 shifted_oct3 = oct3 * 2^(8 :: Integer)
229
230 -- | Given two addresses, find the number of the most significant bit
231 -- where they differ. If the addresses are the same, return
232 -- Maskbits.Zero.
233 most_sig_bit_different :: IPv4Address -> IPv4Address -> Maskbits
234 most_sig_bit_different addr1 addr2
235 | addr1 == addr2 = Maskbits.Zero
236 | m1 /= n1 = Maskbits.One
237 | m2 /= n2 = Two
238 | m3 /= n3 = Three
239 | m4 /= n4 = Four
240 | m5 /= n5 = Five
241 | m6 /= n6 = Six
242 | m7 /= n7 = Seven
243 | m8 /= n8 = Eight
244 | m9 /= n9 = Nine
245 | m10 /= n10 = Ten
246 | m11 /= n11 = Eleven
247 | m12 /= n12 = Twelve
248 | m13 /= n13 = Thirteen
249 | m14 /= n14 = Fourteen
250 | m15 /= n15 = Fifteen
251 | m16 /= n16 = Sixteen
252 | m17 /= n17 = Seventeen
253 | m18 /= n18 = Eighteen
254 | m19 /= n19 = Nineteen
255 | m20 /= n20 = Twenty
256 | m21 /= n21 = TwentyOne
257 | m22 /= n22 = TwentyTwo
258 | m23 /= n23 = TwentyThree
259 | m24 /= n24 = TwentyFour
260 | m25 /= n25 = TwentyFive
261 | m26 /= n26 = TwentySix
262 | m27 /= n27 = TwentySeven
263 | m28 /= n28 = TwentyEight
264 | m29 /= n29 = TwentyNine
265 | m30 /= n30 = Thirty
266 | m31 /= n31 = ThirtyOne
267 | m32 /= n32 = ThirtyTwo
268 | otherwise = Maskbits.Zero
269 where
270 m1 = (b1 oct1a)
271 m2 = (b2 oct1a)
272 m3 = (b3 oct1a)
273 m4 = (b4 oct1a)
274 m5 = (b5 oct1a)
275 m6 = (b6 oct1a)
276 m7 = (b7 oct1a)
277 m8 = (b8 oct1a)
278 m9 = (b1 oct2a)
279 m10 = (b2 oct2a)
280 m11 = (b3 oct2a)
281 m12 = (b4 oct2a)
282 m13 = (b5 oct2a)
283 m14 = (b6 oct2a)
284 m15 = (b7 oct2a)
285 m16 = (b8 oct2a)
286 m17 = (b1 oct3a)
287 m18 = (b2 oct3a)
288 m19 = (b3 oct3a)
289 m20 = (b4 oct3a)
290 m21 = (b5 oct3a)
291 m22 = (b6 oct3a)
292 m23 = (b7 oct3a)
293 m24 = (b8 oct3a)
294 m25 = (b1 oct4a)
295 m26 = (b2 oct4a)
296 m27 = (b3 oct4a)
297 m28 = (b4 oct4a)
298 m29 = (b5 oct4a)
299 m30 = (b6 oct4a)
300 m31 = (b7 oct4a)
301 m32 = (b8 oct4a)
302 oct1a = (octet1 addr1)
303 oct2a = (octet2 addr1)
304 oct3a = (octet3 addr1)
305 oct4a = (octet4 addr1)
306 n1 = (b1 oct1b)
307 n2 = (b2 oct1b)
308 n3 = (b3 oct1b)
309 n4 = (b4 oct1b)
310 n5 = (b5 oct1b)
311 n6 = (b6 oct1b)
312 n7 = (b7 oct1b)
313 n8 = (b8 oct1b)
314 n9 = (b1 oct2b)
315 n10 = (b2 oct2b)
316 n11 = (b3 oct2b)
317 n12 = (b4 oct2b)
318 n13 = (b5 oct2b)
319 n14 = (b6 oct2b)
320 n15 = (b7 oct2b)
321 n16 = (b8 oct2b)
322 n17 = (b1 oct3b)
323 n18 = (b2 oct3b)
324 n19 = (b3 oct3b)
325 n20 = (b4 oct3b)
326 n21 = (b5 oct3b)
327 n22 = (b6 oct3b)
328 n23 = (b7 oct3b)
329 n24 = (b8 oct3b)
330 n25 = (b1 oct4b)
331 n26 = (b2 oct4b)
332 n27 = (b3 oct4b)
333 n28 = (b4 oct4b)
334 n29 = (b5 oct4b)
335 n30 = (b6 oct4b)
336 n31 = (b7 oct4b)
337 n32 = (b8 oct4b)
338 oct1b = (octet1 addr2)
339 oct2b = (octet2 addr2)
340 oct3b = (octet3 addr2)
341 oct4b = (octet4 addr2)
342
343
344 -- Test lists.
345 ipv4address_tests :: TestTree
346 ipv4address_tests =
347 testGroup "IPv4 Address Tests" [
348 test_enum,
349 test_maxBound,
350 test_minBound,
351 test_most_sig_bit_different1,
352 test_most_sig_bit_different2,
353 test_ord_instance1,
354 test_ord_instance2,
355 test_ord_instance3,
356 test_ord_instance4,
357 test_to_enum ]
358
359 ipv4address_properties :: TestTree
360 ipv4address_properties =
361 testGroup
362 "IPv4 Address Properties "
363 [ prop_from_enum_to_enum_inverses ]
364
365 -- QuickCheck properties
366 prop_from_enum_to_enum_inverses :: TestTree
367 prop_from_enum_to_enum_inverses =
368 testProperty "fromEnum and toEnum are inverses" prop
369 where
370 prop :: Int -> Property
371 prop x =
372 (0 <= x) && (x <= 2^(32 :: Integer) - 1) ==>
373 fromEnum (toEnum x :: IPv4Address) == x
374
375 -- HUnit Tests
376 mk_testaddr :: Int -> Int -> Int -> Int -> IPv4Address
377 mk_testaddr a b c d =
378 IPv4Address oct1 oct2 oct3 oct4
379 where
380 oct1 = toEnum a :: Octet
381 oct2 = toEnum b :: Octet
382 oct3 = toEnum c :: Octet
383 oct4 = toEnum d :: Octet
384
385
386 test_minBound :: TestTree
387 test_minBound =
388 testCase desc $ actual @?= expected
389 where
390 desc = "minBound should be 0.0.0.0"
391 expected = mk_testaddr 0 0 0 0
392 actual = minBound :: IPv4Address
393
394
395 test_maxBound :: TestTree
396 test_maxBound =
397 testCase desc $ actual @?= expected
398 where
399 desc = "maxBound should be 255.255.255.255"
400 expected = mk_testaddr 255 255 255 255
401 actual = maxBound :: IPv4Address
402
403
404 test_enum :: TestTree
405 test_enum =
406 testCase desc $ actual @?= expected
407 where
408 desc = "enumerating a /24 gives the correct addresses"
409 expected = ["192.168.0." ++ (show x) | x <- [0..255::Int] ]
410 lb = mk_testaddr 192 168 0 0
411 ub = mk_testaddr 192 168 0 255
412 actual = map show [lb..ub]
413
414
415 test_most_sig_bit_different1 :: TestTree
416 test_most_sig_bit_different1 =
417 testCase desc $ actual @?= expected
418 where
419 desc = "10.1.1.0 and 10.1.0.0 differ in bit 24"
420 addr1 = mk_testaddr 10 1 1 0
421 addr2 = (mk_testaddr 10 1 0 0)
422 expected = TwentyFour
423 actual = most_sig_bit_different addr1 addr2
424
425
426
427 test_most_sig_bit_different2 :: TestTree
428 test_most_sig_bit_different2 =
429 testCase desc $ actual @?= expected
430 where
431 desc = "10.1.2.0 and 10.1.1.0 differ in bit 23"
432 addr1 = mk_testaddr 10 1 2 0
433 addr2 = mk_testaddr 10 1 1 0
434 expected = TwentyThree
435 actual = most_sig_bit_different addr1 addr2
436
437
438 test_to_enum :: TestTree
439 test_to_enum =
440 testCase desc $ actual @?= expected
441 where
442 desc = "192.168.0.0 in base-10 is 3232235520"
443 expected = mk_testaddr 192 168 0 0
444 -- We declare the big number as Word32 because otherwise, on x86,
445 -- we get a warning that it's too big to fit in a 32-bit integer.
446 -- Ultimately we convert it to a (negative) Int on those systems
447 -- anyway, but the gymnastics declare our intent to the compiler.
448 actual = toEnum (fromIntegral (3232235520 :: Word32)) :: IPv4Address
449
450
451 test_ord_instance1 :: TestTree
452 test_ord_instance1 =
453 testCase desc $ actual @?= expected
454 where
455 desc = "127.0.0.0 is less than 127.0.0.1"
456 addr1 = mk_testaddr 127 0 0 0
457 addr2 = mk_testaddr 127 0 0 1
458 expected = True
459 actual = addr1 <= addr2
460
461
462 test_ord_instance2 :: TestTree
463 test_ord_instance2 =
464 testCase desc $ actual @?= expected
465 where
466 desc = "127.0.0.0 is less than 127.0.1.0"
467 addr1 = mk_testaddr 127 0 0 0
468 addr2 = mk_testaddr 127 0 1 0
469 expected = True
470 actual = addr1 <= addr2
471
472 test_ord_instance3 :: TestTree
473 test_ord_instance3 =
474 testCase desc $ actual @?= expected
475 where
476 desc = "127.0.0.0 is less than 127.1.0.0"
477 addr1 = mk_testaddr 127 0 0 0
478 addr2 = mk_testaddr 127 1 0 0
479 expected = True
480 actual = addr1 <= addr2
481
482 test_ord_instance4 :: TestTree
483 test_ord_instance4 =
484 testCase desc $ actual @?= expected
485 where
486 desc = "127.0.0.0 is less than 128.0.0.0"
487 addr1 = mk_testaddr 127 0 0 0
488 addr2 = mk_testaddr 128 0 0 0
489 expected = True
490 actual = addr1 <= addr2