2 ( ipv4address_from_octets,
13 most_sig_bit_different,
22 import qualified Bit as B
26 data IPv4Address = None | IPv4Address { octet1 :: Octet,
33 instance Show IPv4Address where
34 show IPv4Address.None = "None"
35 show addr = concat [(show oct1) ++ ".",
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
50 ipv4address_from_octets :: Octet -> Octet -> Octet -> Octet -> IPv4Address
51 ipv4address_from_octets oct1 oct2 oct3 oct4
52 | or [oct1 == Octet.None,
55 oct4 == Octet.None] = IPv4Address.None
56 | otherwise = IPv4Address oct1 oct2 oct3 oct4
59 min_address :: IPv4Address -> Maskbits -> IPv4Address
60 min_address IPv4Address.None _ = IPv4Address.None
61 min_address _ Maskbits.None = IPv4Address.None
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)
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)
207 min_octet1 :: IPv4Address -> Maskbits -> Octet
208 min_octet1 addr mask = octet1 (min_address addr mask)
210 min_octet2 :: IPv4Address -> Maskbits -> Octet
211 min_octet2 addr mask = octet2 (min_address addr mask)
213 min_octet3 :: IPv4Address -> Maskbits -> Octet
214 min_octet3 addr mask = octet3 (min_address addr mask)
216 min_octet4 :: IPv4Address -> Maskbits -> Octet
217 min_octet4 addr mask = octet4 (min_address addr mask)
219 max_octet1 :: IPv4Address -> Maskbits -> Octet
220 max_octet1 addr mask = octet1 (max_address addr mask)
222 max_octet2 :: IPv4Address -> Maskbits -> Octet
223 max_octet2 addr mask = octet2 (max_address addr mask)
225 max_octet3 :: IPv4Address -> Maskbits -> Octet
226 max_octet3 addr mask = octet3 (max_address addr mask)
228 max_octet4 :: IPv4Address -> Maskbits -> Octet
229 max_octet4 addr mask = octet4 (max_address addr mask)
232 -- Given two addresses, find the number of the most significant bit
233 -- where they differ. If the addresses are the same, return
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
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
304 oct1a = (octet1 addr1)
305 oct2a = (octet2 addr1)
306 oct3a = (octet3 addr1)
307 oct4a = (octet4 addr1)
340 oct1b = (octet1 addr2)
341 oct2b = (octet2 addr2)
342 oct3b = (octet3 addr2)
343 oct4b = (octet4 addr2)
348 mk_testaddr :: Int -> Int -> Int -> Int -> IPv4Address
349 mk_testaddr a b c d =
350 IPv4Address oct1 oct2 oct3 oct4
352 oct1 = octet_from_int a
353 oct2 = octet_from_int b
354 oct3 = octet_from_int c
355 oct4 = octet_from_int d
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))
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))
367 ipv4address_tests = [ test_most_sig_bit_different1,
368 test_most_sig_bit_different2 ]