]> gitweb.michael.orlitzky.com - hath.git/blob - src/IPv4Address.hs
29afa5f3dd2c34264a98b39c250d93b5160db6af
[hath.git] / src / IPv4Address.hs
1 module IPv4Address
2 ( ipv4address_from_octets,
3 ipv4address_tests,
4 IPv4Address(None),
5 max_octet1,
6 max_octet2,
7 max_octet3,
8 max_octet4,
9 min_octet1,
10 min_octet2,
11 min_octet3,
12 min_octet4,
13 most_sig_bit_different,
14 octet1,
15 octet2,
16 octet3,
17 octet4
18 ) where
19
20 import Test.HUnit
21
22 import qualified Bit as B
23 import Maskbits
24 import Octet
25
26 data IPv4Address = None | IPv4Address { octet1 :: Octet,
27 octet2 :: Octet,
28 octet3 :: Octet,
29 octet4 :: Octet }
30 deriving (Eq)
31
32
33 instance Show IPv4Address where
34 show IPv4Address.None = "None"
35 show addr = concat [(show oct1) ++ ".",
36 (show oct2) ++ ".",
37 (show oct3) ++ ".",
38 (show oct4)]
39 where
40 oct1 = (octet1 addr)
41 oct2 = (octet2 addr)
42 oct3 = (octet3 addr)
43 oct4 = (octet4 addr)
44
45
46 -- We don't export our constructor so this function is the only
47 -- way to construct an address from octets. As a result, we can
48 -- return IPv4Address.None in response to being passed one of more
49 -- Octet.None octets.
50 ipv4address_from_octets :: Octet -> Octet -> Octet -> Octet -> IPv4Address
51 ipv4address_from_octets oct1 oct2 oct3 oct4
52 | or [oct1 == Octet.None,
53 oct2 == Octet.None,
54 oct3 == Octet.None,
55 oct4 == Octet.None] = IPv4Address.None
56 | otherwise = IPv4Address oct1 oct2 oct3 oct4
57
58
59 min_address :: IPv4Address -> Maskbits -> IPv4Address
60 min_address IPv4Address.None _ = IPv4Address.None
61 min_address _ Maskbits.None = IPv4Address.None
62 min_address addr mask
63 | mask == ThirtyTwo = IPv4Address oct1 oct2 oct3 oct4
64 | mask == ThirtyOne = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 a28 a29 a30 a31 B.Zero)
65 | mask == Thirty = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 a28 a29 a30 B.Zero B.Zero)
66 | mask == TwentyNine = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 a28 a29 B.Zero B.Zero B.Zero)
67 | mask == TwentyEight = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 a28 B.Zero B.Zero B.Zero B.Zero)
68 | mask == TwentySeven = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 B.Zero B.Zero B.Zero B.Zero B.Zero)
69 | mask == TwentySix = IPv4Address oct1 oct2 oct3 (Octet a25 a26 B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero)
70 | mask == TwentyFive = IPv4Address oct1 oct2 oct3 (Octet a25 B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero)
71 | mask == TwentyFour = IPv4Address oct1 oct2 oct3 (min_octet)
72 | mask == TwentyThree = IPv4Address oct1 oct2 (Octet a17 a18 a19 a20 a21 a22 a23 B.Zero) (min_octet)
73 | mask == TwentyTwo = IPv4Address oct1 oct2 (Octet a17 a18 a19 a20 a21 a22 B.Zero B.Zero) (min_octet)
74 | mask == TwentyOne = IPv4Address oct1 oct2 (Octet a17 a18 a19 a20 a21 B.Zero B.Zero B.Zero) (min_octet)
75 | mask == Twenty = IPv4Address oct1 oct2 (Octet a17 a18 a19 a20 B.Zero B.Zero B.Zero B.Zero) (min_octet)
76 | mask == Nineteen = IPv4Address oct1 oct2 (Octet a17 a18 a19 B.Zero B.Zero B.Zero B.Zero B.Zero) (min_octet)
77 | mask == Eighteen = IPv4Address oct1 oct2 (Octet a17 a18 B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero) (min_octet)
78 | mask == Seventeen = IPv4Address oct1 oct2 (Octet a17 B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero) (min_octet)
79 | mask == Sixteen = IPv4Address oct1 oct2 (min_octet) (min_octet)
80 | mask == Fifteen = IPv4Address oct1 (Octet a9 a10 a11 a12 a13 a14 a15 B.Zero) (min_octet) (min_octet)
81 | mask == Fourteen = IPv4Address oct1 (Octet a9 a10 a11 a12 a13 a14 B.Zero B.Zero) (min_octet) (min_octet)
82 | mask == Thirteen = IPv4Address oct1 (Octet a9 a10 a11 a12 a13 B.Zero B.Zero B.Zero) (min_octet) (min_octet)
83 | mask == Twelve = IPv4Address oct1 (Octet a9 a10 a11 a12 B.Zero B.Zero B.Zero B.Zero) (min_octet) (min_octet)
84 | mask == Eleven = IPv4Address oct1 (Octet a9 a10 a11 B.Zero B.Zero B.Zero B.Zero B.Zero) (min_octet) (min_octet)
85 | mask == Ten = IPv4Address oct1 (Octet a9 a10 B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero) (min_octet) (min_octet)
86 | mask == Nine = IPv4Address oct1 (Octet a9 B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero) (min_octet) (min_octet)
87 | mask == Eight = IPv4Address oct1 (min_octet) (min_octet) (min_octet)
88 | mask == Seven = IPv4Address (Octet a1 a2 a3 a4 a5 a6 a7 B.Zero) (min_octet) (min_octet) (min_octet)
89 | mask == Six = IPv4Address (Octet a1 a2 a3 a4 a5 a6 B.Zero B.Zero) (min_octet) (min_octet) (min_octet)
90 | mask == Five = IPv4Address (Octet a1 a2 a3 a4 a5 B.Zero B.Zero B.Zero) (min_octet) (min_octet) (min_octet)
91 | mask == Four = IPv4Address (Octet a1 a2 a3 a4 B.Zero B.Zero B.Zero B.Zero) (min_octet) (min_octet) (min_octet)
92 | mask == Three = IPv4Address (Octet a1 a2 a3 B.Zero B.Zero B.Zero B.Zero B.Zero) (min_octet) (min_octet) (min_octet)
93 | mask == Two = IPv4Address (Octet a1 a2 B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero) (min_octet) (min_octet) (min_octet)
94 | mask == One = IPv4Address (Octet a1 B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero) (min_octet) (min_octet) (min_octet)
95 | mask == Zero = IPv4Address (min_octet) (min_octet) (min_octet) (min_octet)
96 | otherwise = addr
97 where
98 a1 = (b1 oct1)
99 a2 = (b2 oct1)
100 a3 = (b3 oct1)
101 a4 = (b4 oct1)
102 a5 = (b5 oct1)
103 a6 = (b6 oct1)
104 a7 = (b7 oct1)
105 a9 = (b1 oct2)
106 a10 = (b2 oct2)
107 a11 = (b3 oct2)
108 a12 = (b4 oct2)
109 a13 = (b5 oct2)
110 a14 = (b6 oct2)
111 a15 = (b7 oct2)
112 a17 = (b1 oct3)
113 a18 = (b2 oct3)
114 a19 = (b3 oct3)
115 a20 = (b4 oct3)
116 a21 = (b5 oct3)
117 a22 = (b6 oct3)
118 a23 = (b7 oct3)
119 a25 = (b1 oct4)
120 a26 = (b2 oct4)
121 a27 = (b3 oct4)
122 a28 = (b4 oct4)
123 a29 = (b5 oct4)
124 a30 = (b6 oct4)
125 a31 = (b7 oct4)
126 oct1 = (octet1 addr)
127 oct2 = (octet2 addr)
128 oct3 = (octet3 addr)
129 oct4 = (octet4 addr)
130
131
132
133 max_address :: IPv4Address -> Maskbits -> IPv4Address
134 max_address IPv4Address.None _ = IPv4Address.None
135 max_address _ Maskbits.None = IPv4Address.None
136 max_address addr mask
137 | mask == ThirtyTwo = IPv4Address oct1 oct2 oct3 oct4
138 | mask == ThirtyOne = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 a28 a29 a30 a31 B.One)
139 | mask == Thirty = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 a28 a29 a30 B.One B.One)
140 | mask == TwentyNine = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 a28 a29 B.One B.One B.One)
141 | mask == TwentyEight = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 a28 B.One B.One B.One B.One)
142 | mask == TwentySeven = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 B.One B.One B.One B.One B.One)
143 | mask == TwentySix = IPv4Address oct1 oct2 oct3 (Octet a25 a26 B.One B.One B.One B.One B.One B.One)
144 | mask == TwentyFive = IPv4Address oct1 oct2 oct3 (Octet a25 B.One B.One B.One B.One B.One B.One B.One)
145 | mask == TwentyFour = IPv4Address oct1 oct2 oct3 (max_octet)
146 | mask == TwentyThree = IPv4Address oct1 oct2 (Octet a17 a18 a19 a20 a21 a22 a23 B.One) (max_octet)
147 | mask == TwentyTwo = IPv4Address oct1 oct2 (Octet a17 a18 a19 a20 a21 a22 B.One B.One) (max_octet)
148 | mask == TwentyOne = IPv4Address oct1 oct2 (Octet a17 a18 a19 a20 a21 B.One B.One B.One) (max_octet)
149 | mask == Twenty = IPv4Address oct1 oct2 (Octet a17 a18 a19 a20 B.One B.One B.One B.One) (max_octet)
150 | mask == Nineteen = IPv4Address oct1 oct2 (Octet a17 a18 a19 B.One B.One B.One B.One B.One) (max_octet)
151 | mask == Eighteen = IPv4Address oct1 oct2 (Octet a17 a18 B.One B.One B.One B.One B.One B.One) (max_octet)
152 | mask == Seventeen = IPv4Address oct1 oct2 (Octet a17 B.One B.One B.One B.One B.One B.One B.One) (max_octet)
153 | mask == Sixteen = IPv4Address oct1 oct2 (max_octet) (max_octet)
154 | mask == Fifteen = IPv4Address oct1 (Octet a9 a10 a11 a12 a13 a14 a15 B.One) (max_octet) (max_octet)
155 | mask == Fourteen = IPv4Address oct1 (Octet a9 a10 a11 a12 a13 a14 B.One B.One) (max_octet) (max_octet)
156 | mask == Thirteen = IPv4Address oct1 (Octet a9 a10 a11 a12 a13 B.One B.One B.One) (max_octet) (max_octet)
157 | mask == Twelve = IPv4Address oct1 (Octet a9 a10 a11 a12 B.One B.One B.One B.One) (max_octet) (max_octet)
158 | mask == Eleven = IPv4Address oct1 (Octet a9 a10 a11 B.One B.One B.One B.One B.One) (max_octet) (max_octet)
159 | mask == Ten = IPv4Address oct1 (Octet a9 a10 B.One B.One B.One B.One B.One B.One) (max_octet) (max_octet)
160 | mask == Nine = IPv4Address oct1 (Octet a9 B.One B.One B.One B.One B.One B.One B.One) (max_octet) (max_octet)
161 | mask == Eight = IPv4Address oct1 (max_octet) (max_octet) (max_octet)
162 | mask == Seven = IPv4Address (Octet a1 a2 a3 a4 a5 a6 a7 B.One) (max_octet) (max_octet) (max_octet)
163 | mask == Six = IPv4Address (Octet a1 a2 a3 a4 a5 a6 B.One B.One) (max_octet) (max_octet) (max_octet)
164 | mask == Five = IPv4Address (Octet a1 a2 a3 a4 a5 B.One B.One B.One) (max_octet) (max_octet) (max_octet)
165 | mask == Four = IPv4Address (Octet a1 a2 a3 a4 B.One B.One B.One B.One) (max_octet) (max_octet) (max_octet)
166 | mask == Three = IPv4Address (Octet a1 a2 a3 B.One B.One B.One B.One B.One) (max_octet) (max_octet) (max_octet)
167 | mask == Two = IPv4Address (Octet a1 a2 B.One B.One B.One B.One B.One B.One) (max_octet) (max_octet) (max_octet)
168 | mask == One = IPv4Address (Octet a1 B.One B.One B.One B.One B.One B.One B.One) (max_octet) (max_octet) (max_octet)
169 | mask == Zero = IPv4Address (max_octet) (max_octet) (max_octet) (max_octet)
170 | otherwise = addr
171 where
172 a1 = (b1 oct1)
173 a2 = (b2 oct1)
174 a3 = (b3 oct1)
175 a4 = (b4 oct1)
176 a5 = (b5 oct1)
177 a6 = (b6 oct1)
178 a7 = (b7 oct1)
179 a9 = (b1 oct2)
180 a10 = (b2 oct2)
181 a11 = (b3 oct2)
182 a12 = (b4 oct2)
183 a13 = (b5 oct2)
184 a14 = (b6 oct2)
185 a15 = (b7 oct2)
186 a17 = (b1 oct3)
187 a18 = (b2 oct3)
188 a19 = (b3 oct3)
189 a20 = (b4 oct3)
190 a21 = (b5 oct3)
191 a22 = (b6 oct3)
192 a23 = (b7 oct3)
193 a25 = (b1 oct4)
194 a26 = (b2 oct4)
195 a27 = (b3 oct4)
196 a28 = (b4 oct4)
197 a29 = (b5 oct4)
198 a30 = (b6 oct4)
199 a31 = (b7 oct4)
200 oct1 = (octet1 addr)
201 oct2 = (octet2 addr)
202 oct3 = (octet3 addr)
203 oct4 = (octet4 addr)
204
205
206
207 min_octet1 :: IPv4Address -> Maskbits -> Octet
208 min_octet1 addr mask = octet1 (min_address addr mask)
209
210 min_octet2 :: IPv4Address -> Maskbits -> Octet
211 min_octet2 addr mask = octet2 (min_address addr mask)
212
213 min_octet3 :: IPv4Address -> Maskbits -> Octet
214 min_octet3 addr mask = octet3 (min_address addr mask)
215
216 min_octet4 :: IPv4Address -> Maskbits -> Octet
217 min_octet4 addr mask = octet4 (min_address addr mask)
218
219 max_octet1 :: IPv4Address -> Maskbits -> Octet
220 max_octet1 addr mask = octet1 (max_address addr mask)
221
222 max_octet2 :: IPv4Address -> Maskbits -> Octet
223 max_octet2 addr mask = octet2 (max_address addr mask)
224
225 max_octet3 :: IPv4Address -> Maskbits -> Octet
226 max_octet3 addr mask = octet3 (max_address addr mask)
227
228 max_octet4 :: IPv4Address -> Maskbits -> Octet
229 max_octet4 addr mask = octet4 (max_address addr mask)
230
231
232 -- Given two addresses, find the number of the most significant bit
233 -- where they differ. If the addresses are the same, return
234 -- Maskbits.Zero.
235 most_sig_bit_different :: IPv4Address -> IPv4Address -> Maskbits
236 most_sig_bit_different addr1 addr2
237 | addr1 == addr2 = Maskbits.Zero
238 | m1 /= n1 = Maskbits.One
239 | m2 /= n2 = Two
240 | m3 /= n3 = Three
241 | m4 /= n4 = Four
242 | m5 /= n5 = Five
243 | m6 /= n6 = Six
244 | m7 /= n7 = Seven
245 | m8 /= n8 = Eight
246 | m9 /= n9 = Nine
247 | m10 /= n10 = Ten
248 | m11 /= n11 = Eleven
249 | m12 /= n12 = Twelve
250 | m13 /= n13 = Thirteen
251 | m14 /= n14 = Fourteen
252 | m15 /= n15 = Fifteen
253 | m16 /= n16 = Sixteen
254 | m17 /= n17 = Seventeen
255 | m18 /= n18 = Eighteen
256 | m19 /= n19 = Nineteen
257 | m20 /= n20 = Twenty
258 | m21 /= n21 = TwentyOne
259 | m22 /= n22 = TwentyTwo
260 | m23 /= n23 = TwentyThree
261 | m24 /= n24 = TwentyFour
262 | m25 /= n25 = TwentyFive
263 | m26 /= n26 = TwentySix
264 | m27 /= n27 = TwentySeven
265 | m28 /= n28 = TwentyEight
266 | m29 /= n29 = TwentyNine
267 | m30 /= n30 = Thirty
268 | m31 /= n31 = ThirtyOne
269 | m32 /= n32 = ThirtyTwo
270 | otherwise = Maskbits.Zero
271 where
272 m1 = (b1 oct1a)
273 m2 = (b2 oct1a)
274 m3 = (b3 oct1a)
275 m4 = (b4 oct1a)
276 m5 = (b5 oct1a)
277 m6 = (b6 oct1a)
278 m7 = (b7 oct1a)
279 m8 = (b8 oct1a)
280 m9 = (b1 oct2a)
281 m10 = (b2 oct2a)
282 m11 = (b3 oct2a)
283 m12 = (b4 oct2a)
284 m13 = (b5 oct2a)
285 m14 = (b6 oct2a)
286 m15 = (b7 oct2a)
287 m16 = (b8 oct2a)
288 m17 = (b1 oct3a)
289 m18 = (b2 oct3a)
290 m19 = (b3 oct3a)
291 m20 = (b4 oct3a)
292 m21 = (b5 oct3a)
293 m22 = (b6 oct3a)
294 m23 = (b7 oct3a)
295 m24 = (b8 oct3a)
296 m25 = (b1 oct4a)
297 m26 = (b2 oct4a)
298 m27 = (b3 oct4a)
299 m28 = (b4 oct4a)
300 m29 = (b5 oct4a)
301 m30 = (b6 oct4a)
302 m31 = (b7 oct4a)
303 m32 = (b8 oct4a)
304 oct1a = (octet1 addr1)
305 oct2a = (octet2 addr1)
306 oct3a = (octet3 addr1)
307 oct4a = (octet4 addr1)
308 n1 = (b1 oct1b)
309 n2 = (b2 oct1b)
310 n3 = (b3 oct1b)
311 n4 = (b4 oct1b)
312 n5 = (b5 oct1b)
313 n6 = (b6 oct1b)
314 n7 = (b7 oct1b)
315 n8 = (b8 oct1b)
316 n9 = (b1 oct2b)
317 n10 = (b2 oct2b)
318 n11 = (b3 oct2b)
319 n12 = (b4 oct2b)
320 n13 = (b5 oct2b)
321 n14 = (b6 oct2b)
322 n15 = (b7 oct2b)
323 n16 = (b8 oct2b)
324 n17 = (b1 oct3b)
325 n18 = (b2 oct3b)
326 n19 = (b3 oct3b)
327 n20 = (b4 oct3b)
328 n21 = (b5 oct3b)
329 n22 = (b6 oct3b)
330 n23 = (b7 oct3b)
331 n24 = (b8 oct3b)
332 n25 = (b1 oct4b)
333 n26 = (b2 oct4b)
334 n27 = (b3 oct4b)
335 n28 = (b4 oct4b)
336 n29 = (b5 oct4b)
337 n30 = (b6 oct4b)
338 n31 = (b7 oct4b)
339 n32 = (b8 oct4b)
340 oct1b = (octet1 addr2)
341 oct2b = (octet2 addr2)
342 oct3b = (octet3 addr2)
343 oct4b = (octet4 addr2)
344
345
346
347 -- HUnit Tests
348 mk_testaddr :: Int -> Int -> Int -> Int -> IPv4Address
349 mk_testaddr a b c d =
350 IPv4Address oct1 oct2 oct3 oct4
351 where
352 oct1 = octet_from_int a
353 oct2 = octet_from_int b
354 oct3 = octet_from_int c
355 oct4 = octet_from_int d
356
357
358 test_most_sig_bit_different1 =
359 TestCase $ assertEqual "10.1.1.0 and 10.1.0.0 differ in bit 24" TwentyFour (most_sig_bit_different (mk_testaddr 10 1 1 0) (mk_testaddr 10 1 0 0))
360
361
362 test_most_sig_bit_different2 =
363 TestCase $ assertEqual "10.1.2.0 and 10.1.1.0 differ in bit 23" TwentyThree (most_sig_bit_different (mk_testaddr 10 1 2 0) (mk_testaddr 10 1 1 0))
364
365
366
367 ipv4address_tests = [ test_most_sig_bit_different1,
368 test_most_sig_bit_different2 ]