module IPv4Address
( ipv4address_from_octets,
+ ipv4address_tests,
IPv4Address(None),
- Maskbits,
- max_octet1,
- max_octet2,
- max_octet3,
- max_octet4,
- min_octet1,
- min_octet2,
- min_octet3,
- min_octet4
-)
- where
+ max_address,
+ min_address,
+ most_sig_bit_different,
+ octet1,
+ octet2,
+ octet3,
+ octet4
+) where
-import Bit
-import Octet
+import Test.HUnit
+import Test.QuickCheck
-type Maskbits = Int
+import Maskable
+import Maskbits
+import Octet
data IPv4Address = None | IPv4Address { octet1 :: Octet,
octet2 :: Octet,
octet3 :: Octet,
octet4 :: Octet }
- deriving (Eq, Show)
+ deriving (Eq)
+
+
+instance Show IPv4Address where
+ show IPv4Address.None = "None"
+ show addr = concat [(show oct1) ++ ".",
+ (show oct2) ++ ".",
+ (show oct3) ++ ".",
+ (show oct4)]
+ where
+ oct1 = (octet1 addr)
+ oct2 = (octet2 addr)
+ oct3 = (octet3 addr)
+ oct4 = (octet4 addr)
+
+
+instance Arbitrary IPv4Address where
+ arbitrary = do
+ oct1 <- arbitrary :: Gen Octet
+ oct2 <- arbitrary :: Gen Octet
+ oct3 <- arbitrary :: Gen Octet
+ oct4 <- arbitrary :: Gen Octet
+ return (IPv4Address oct1 oct2 oct3 oct4)
+
+ coarbitrary _ = variant 0
+
+
+
+instance Maskable IPv4Address where
+ apply_mask _ Maskbits.None = IPv4Address.None
+ apply_mask addr mask
+ | mask == ThirtyTwo = ipv4address_from_octets oct1 oct2 oct3 oct4
+ | mask == ThirtyOne = ipv4address_from_octets oct1 oct2 oct3 (apply_mask oct4 Seven)
+ | mask == Thirty = ipv4address_from_octets oct1 oct2 oct3 (apply_mask oct4 Six)
+ | mask == TwentyNine = ipv4address_from_octets oct1 oct2 oct3 (apply_mask oct4 Five)
+ | mask == TwentyEight = ipv4address_from_octets oct1 oct2 oct3 (apply_mask oct4 Four)
+ | mask == TwentySeven = ipv4address_from_octets oct1 oct2 oct3 (apply_mask oct4 Three)
+ | mask == TwentySix = ipv4address_from_octets oct1 oct2 oct3 (apply_mask oct4 Two)
+ | mask == TwentyFive = ipv4address_from_octets oct1 oct2 oct3 (apply_mask oct4 One)
+ | mask == TwentyFour = ipv4address_from_octets oct1 oct2 oct3 (min_octet)
+ | mask == TwentyThree = ipv4address_from_octets oct1 oct2 (apply_mask oct3 Seven) (min_octet)
+ | mask == TwentyTwo = ipv4address_from_octets oct1 oct2 (apply_mask oct3 Six) (min_octet)
+ | mask == TwentyOne = ipv4address_from_octets oct1 oct2 (apply_mask oct3 Five) (min_octet)
+ | mask == Twenty = ipv4address_from_octets oct1 oct2 (apply_mask oct3 Four) (min_octet)
+ | mask == Nineteen = ipv4address_from_octets oct1 oct2 (apply_mask oct3 Three) (min_octet)
+ | mask == Eighteen = ipv4address_from_octets oct1 oct2 (apply_mask oct3 Two) (min_octet)
+ | mask == Seventeen = ipv4address_from_octets oct1 oct2 (apply_mask oct3 One) (min_octet)
+ | mask == Sixteen = ipv4address_from_octets oct1 oct2 (min_octet) (min_octet)
+ | mask == Fifteen = ipv4address_from_octets oct1 (apply_mask oct2 Seven) (min_octet) (min_octet)
+ | mask == Fourteen = ipv4address_from_octets oct1 (apply_mask oct2 Six) (min_octet) (min_octet)
+ | mask == Thirteen = ipv4address_from_octets oct1 (apply_mask oct2 Five) (min_octet) (min_octet)
+ | mask == Twelve = ipv4address_from_octets oct1 (apply_mask oct2 Four) (min_octet) (min_octet)
+ | mask == Eleven = ipv4address_from_octets oct1 (apply_mask oct2 Three) (min_octet) (min_octet)
+ | mask == Ten = ipv4address_from_octets oct1 (apply_mask oct2 Two) (min_octet) (min_octet)
+ | mask == Nine = ipv4address_from_octets oct1 (apply_mask oct2 One) (min_octet) (min_octet)
+ | mask == Eight = ipv4address_from_octets oct1 (min_octet) (min_octet) (min_octet)
+ | mask == Seven = ipv4address_from_octets (apply_mask oct1 Seven) (min_octet) (min_octet) (min_octet)
+ | mask == Six = ipv4address_from_octets (apply_mask oct1 Six) (min_octet) (min_octet) (min_octet)
+ | mask == Five = ipv4address_from_octets (apply_mask oct1 Five) (min_octet) (min_octet) (min_octet)
+ | mask == Four = ipv4address_from_octets (apply_mask oct1 Four) (min_octet) (min_octet) (min_octet)
+ | mask == Three = ipv4address_from_octets (apply_mask oct1 Three) (min_octet) (min_octet) (min_octet)
+ | mask == Two = ipv4address_from_octets (apply_mask oct1 Two) (min_octet) (min_octet) (min_octet)
+ | mask == One = ipv4address_from_octets (apply_mask oct1 One) (min_octet) (min_octet) (min_octet)
+ | mask == Zero = ipv4address_from_octets (min_octet) (min_octet) (min_octet) (min_octet)
+ | otherwise = IPv4Address.None
+ where
+ oct1 = (octet1 addr)
+ oct2 = (octet2 addr)
+ oct3 = (octet3 addr)
+ oct4 = (octet4 addr)
-- We don't export our constructor so this function is the only
| otherwise = IPv4Address oct1 oct2 oct3 oct4
-min_address :: IPv4Address -> Maskbits -> IPv4Address
-min_address addr mask
- | mask == 32 = IPv4Address oct1 oct2 oct3 oct4
- | mask == 31 = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 a28 a29 a30 a31 Zero)
- | mask == 30 = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 a28 a29 a30 Zero Zero)
- | mask == 29 = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 a28 a29 Zero Zero Zero)
- | mask == 28 = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 a28 Zero Zero Zero Zero)
- | mask == 27 = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 Zero Zero Zero Zero Zero)
- | mask == 26 = IPv4Address oct1 oct2 oct3 (Octet a25 a26 Zero Zero Zero Zero Zero Zero)
- | mask == 25 = IPv4Address oct1 oct2 oct3 (Octet a25 Zero Zero Zero Zero Zero Zero Zero)
- | mask == 24 = IPv4Address oct1 oct2 oct3 (min_octet)
- | mask == 23 = IPv4Address oct1 oct2 (Octet a17 a18 a19 a20 a21 a22 a23 Zero) (min_octet)
- | mask == 22 = IPv4Address oct1 oct2 (Octet a17 a18 a19 a20 a21 a22 Zero Zero) (min_octet)
- | mask == 21 = IPv4Address oct1 oct2 (Octet a17 a18 a19 a20 a21 Zero Zero Zero) (min_octet)
- | mask == 20 = IPv4Address oct1 oct2 (Octet a17 a18 a19 a20 Zero Zero Zero Zero) (min_octet)
- | mask == 19 = IPv4Address oct1 oct2 (Octet a17 a18 a19 Zero Zero Zero Zero Zero) (min_octet)
- | mask == 18 = IPv4Address oct1 oct2 (Octet a17 a18 Zero Zero Zero Zero Zero Zero) (min_octet)
- | mask == 17 = IPv4Address oct1 oct2 (Octet a17 Zero Zero Zero Zero Zero Zero Zero) (min_octet)
- | mask == 16 = IPv4Address oct1 oct2 (min_octet) (min_octet)
- | mask == 15 = IPv4Address oct1 (Octet a9 a10 a11 a12 a13 a14 a15 Zero) (min_octet) (min_octet)
- | mask == 14 = IPv4Address oct1 (Octet a9 a10 a11 a12 a13 a14 Zero Zero) (min_octet) (min_octet)
- | mask == 13 = IPv4Address oct1 (Octet a9 a10 a11 a12 a13 Zero Zero Zero) (min_octet) (min_octet)
- | mask == 12 = IPv4Address oct1 (Octet a9 a10 a11 a12 Zero Zero Zero Zero) (min_octet) (min_octet)
- | mask == 11 = IPv4Address oct1 (Octet a9 a10 a11 Zero Zero Zero Zero Zero) (min_octet) (min_octet)
- | mask == 10 = IPv4Address oct1 (Octet a9 a10 Zero Zero Zero Zero Zero Zero) (min_octet) (min_octet)
- | mask == 9 = IPv4Address oct1 (Octet a9 Zero Zero Zero Zero Zero Zero Zero) (min_octet) (min_octet)
- | mask == 8 = IPv4Address oct1 (min_octet) (min_octet) (min_octet)
- | mask == 7 = IPv4Address (Octet a1 a2 a3 a4 a5 a6 a7 Zero) (min_octet) (min_octet) (min_octet)
- | mask == 6 = IPv4Address (Octet a1 a2 a3 a4 a5 a6 Zero Zero) (min_octet) (min_octet) (min_octet)
- | mask == 5 = IPv4Address (Octet a1 a2 a3 a4 a5 Zero Zero Zero) (min_octet) (min_octet) (min_octet)
- | mask == 4 = IPv4Address (Octet a1 a2 a3 a4 Zero Zero Zero Zero) (min_octet) (min_octet) (min_octet)
- | mask == 3 = IPv4Address (Octet a1 a2 a3 Zero Zero Zero Zero Zero) (min_octet) (min_octet) (min_octet)
- | mask == 2 = IPv4Address (Octet a1 a2 Zero Zero Zero Zero Zero Zero) (min_octet) (min_octet) (min_octet)
- | mask == 1 = IPv4Address (Octet a1 Zero Zero Zero Zero Zero Zero Zero) (min_octet) (min_octet) (min_octet)
- | mask == 0 = IPv4Address (min_octet) (min_octet) (min_octet) (min_octet)
- | otherwise = addr
- where
- a1 = (b1 oct1)
- a2 = (b2 oct1)
- a3 = (b3 oct1)
- a4 = (b4 oct1)
- a5 = (b5 oct1)
- a6 = (b6 oct1)
- a7 = (b7 oct1)
- a9 = (b1 oct2)
- a10 = (b2 oct2)
- a11 = (b3 oct2)
- a12 = (b4 oct2)
- a13 = (b5 oct2)
- a14 = (b6 oct2)
- a15 = (b7 oct2)
- a17 = (b1 oct3)
- a18 = (b2 oct3)
- a19 = (b3 oct3)
- a20 = (b4 oct3)
- a21 = (b5 oct3)
- a22 = (b6 oct3)
- a23 = (b7 oct3)
- a25 = (b1 oct4)
- a26 = (b2 oct4)
- a27 = (b3 oct4)
- a28 = (b4 oct4)
- a29 = (b5 oct4)
- a30 = (b6 oct4)
- a31 = (b7 oct4)
- oct1 = (octet1 addr)
- oct2 = (octet2 addr)
- oct3 = (octet3 addr)
- oct4 = (octet4 addr)
-
-
-
-max_address :: IPv4Address -> Maskbits -> IPv4Address
-max_address addr mask
- | mask == 32 = IPv4Address oct1 oct2 oct3 oct4
- | mask == 31 = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 a28 a29 a30 a31 One)
- | mask == 30 = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 a28 a29 a30 One One)
- | mask == 29 = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 a28 a29 One One One)
- | mask == 28 = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 a28 One One One One)
- | mask == 27 = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 One One One One One)
- | mask == 26 = IPv4Address oct1 oct2 oct3 (Octet a25 a26 One One One One One One)
- | mask == 25 = IPv4Address oct1 oct2 oct3 (Octet a25 One One One One One One One)
- | mask == 24 = IPv4Address oct1 oct2 oct3 (max_octet)
- | mask == 23 = IPv4Address oct1 oct2 (Octet a17 a18 a19 a20 a21 a22 a23 One) (max_octet)
- | mask == 22 = IPv4Address oct1 oct2 (Octet a17 a18 a19 a20 a21 a22 One One) (max_octet)
- | mask == 21 = IPv4Address oct1 oct2 (Octet a17 a18 a19 a20 a21 One One One) (max_octet)
- | mask == 20 = IPv4Address oct1 oct2 (Octet a17 a18 a19 a20 One One One One) (max_octet)
- | mask == 19 = IPv4Address oct1 oct2 (Octet a17 a18 a19 One One One One One) (max_octet)
- | mask == 18 = IPv4Address oct1 oct2 (Octet a17 a18 One One One One One One) (max_octet)
- | mask == 17 = IPv4Address oct1 oct2 (Octet a17 One One One One One One One) (max_octet)
- | mask == 16 = IPv4Address oct1 oct2 (max_octet) (max_octet)
- | mask == 15 = IPv4Address oct1 (Octet a9 a10 a11 a12 a13 a14 a15 One) (max_octet) (max_octet)
- | mask == 14 = IPv4Address oct1 (Octet a9 a10 a11 a12 a13 a14 One One) (max_octet) (max_octet)
- | mask == 13 = IPv4Address oct1 (Octet a9 a10 a11 a12 a13 One One One) (max_octet) (max_octet)
- | mask == 12 = IPv4Address oct1 (Octet a9 a10 a11 a12 One One One One) (max_octet) (max_octet)
- | mask == 11 = IPv4Address oct1 (Octet a9 a10 a11 One One One One One) (max_octet) (max_octet)
- | mask == 10 = IPv4Address oct1 (Octet a9 a10 One One One One One One) (max_octet) (max_octet)
- | mask == 9 = IPv4Address oct1 (Octet a9 One One One One One One One) (max_octet) (max_octet)
- | mask == 8 = IPv4Address oct1 (max_octet) (max_octet) (max_octet)
- | mask == 7 = IPv4Address (Octet a1 a2 a3 a4 a5 a6 a7 One) (max_octet) (max_octet) (max_octet)
- | mask == 6 = IPv4Address (Octet a1 a2 a3 a4 a5 a6 One One) (max_octet) (max_octet) (max_octet)
- | mask == 5 = IPv4Address (Octet a1 a2 a3 a4 a5 One One One) (max_octet) (max_octet) (max_octet)
- | mask == 4 = IPv4Address (Octet a1 a2 a3 a4 One One One One) (max_octet) (max_octet) (max_octet)
- | mask == 3 = IPv4Address (Octet a1 a2 a3 One One One One One) (max_octet) (max_octet) (max_octet)
- | mask == 2 = IPv4Address (Octet a1 a2 One One One One One One) (max_octet) (max_octet) (max_octet)
- | mask == 1 = IPv4Address (Octet a1 One One One One One One One) (max_octet) (max_octet) (max_octet)
- | mask == 0 = IPv4Address (max_octet) (max_octet) (max_octet) (max_octet)
- | otherwise = addr
+-- The minimum possible IPv4 address, 0.0.0.0.
+min_address :: IPv4Address
+min_address = IPv4Address min_octet min_octet min_octet min_octet
+
+
+-- The maximum possible IPv4 address, 255.255.255.255.
+max_address :: IPv4Address
+max_address = IPv4Address max_octet max_octet max_octet max_octet
+
+
+-- Given two addresses, find the number of the most significant bit
+-- where they differ. If the addresses are the same, return
+-- Maskbits.Zero.
+most_sig_bit_different :: IPv4Address -> IPv4Address -> Maskbits
+most_sig_bit_different addr1 addr2
+ | addr1 == addr2 = Maskbits.Zero
+ | m1 /= n1 = Maskbits.One
+ | m2 /= n2 = Two
+ | m3 /= n3 = Three
+ | m4 /= n4 = Four
+ | m5 /= n5 = Five
+ | m6 /= n6 = Six
+ | m7 /= n7 = Seven
+ | m8 /= n8 = Eight
+ | m9 /= n9 = Nine
+ | m10 /= n10 = Ten
+ | m11 /= n11 = Eleven
+ | m12 /= n12 = Twelve
+ | m13 /= n13 = Thirteen
+ | m14 /= n14 = Fourteen
+ | m15 /= n15 = Fifteen
+ | m16 /= n16 = Sixteen
+ | m17 /= n17 = Seventeen
+ | m18 /= n18 = Eighteen
+ | m19 /= n19 = Nineteen
+ | m20 /= n20 = Twenty
+ | m21 /= n21 = TwentyOne
+ | m22 /= n22 = TwentyTwo
+ | m23 /= n23 = TwentyThree
+ | m24 /= n24 = TwentyFour
+ | m25 /= n25 = TwentyFive
+ | m26 /= n26 = TwentySix
+ | m27 /= n27 = TwentySeven
+ | m28 /= n28 = TwentyEight
+ | m29 /= n29 = TwentyNine
+ | m30 /= n30 = Thirty
+ | m31 /= n31 = ThirtyOne
+ | m32 /= n32 = ThirtyTwo
+ | otherwise = Maskbits.Zero
+ where
+ m1 = (b1 oct1a)
+ m2 = (b2 oct1a)
+ m3 = (b3 oct1a)
+ m4 = (b4 oct1a)
+ m5 = (b5 oct1a)
+ m6 = (b6 oct1a)
+ m7 = (b7 oct1a)
+ m8 = (b8 oct1a)
+ m9 = (b1 oct2a)
+ m10 = (b2 oct2a)
+ m11 = (b3 oct2a)
+ m12 = (b4 oct2a)
+ m13 = (b5 oct2a)
+ m14 = (b6 oct2a)
+ m15 = (b7 oct2a)
+ m16 = (b8 oct2a)
+ m17 = (b1 oct3a)
+ m18 = (b2 oct3a)
+ m19 = (b3 oct3a)
+ m20 = (b4 oct3a)
+ m21 = (b5 oct3a)
+ m22 = (b6 oct3a)
+ m23 = (b7 oct3a)
+ m24 = (b8 oct3a)
+ m25 = (b1 oct4a)
+ m26 = (b2 oct4a)
+ m27 = (b3 oct4a)
+ m28 = (b4 oct4a)
+ m29 = (b5 oct4a)
+ m30 = (b6 oct4a)
+ m31 = (b7 oct4a)
+ m32 = (b8 oct4a)
+ oct1a = (octet1 addr1)
+ oct2a = (octet2 addr1)
+ oct3a = (octet3 addr1)
+ oct4a = (octet4 addr1)
+ n1 = (b1 oct1b)
+ n2 = (b2 oct1b)
+ n3 = (b3 oct1b)
+ n4 = (b4 oct1b)
+ n5 = (b5 oct1b)
+ n6 = (b6 oct1b)
+ n7 = (b7 oct1b)
+ n8 = (b8 oct1b)
+ n9 = (b1 oct2b)
+ n10 = (b2 oct2b)
+ n11 = (b3 oct2b)
+ n12 = (b4 oct2b)
+ n13 = (b5 oct2b)
+ n14 = (b6 oct2b)
+ n15 = (b7 oct2b)
+ n16 = (b8 oct2b)
+ n17 = (b1 oct3b)
+ n18 = (b2 oct3b)
+ n19 = (b3 oct3b)
+ n20 = (b4 oct3b)
+ n21 = (b5 oct3b)
+ n22 = (b6 oct3b)
+ n23 = (b7 oct3b)
+ n24 = (b8 oct3b)
+ n25 = (b1 oct4b)
+ n26 = (b2 oct4b)
+ n27 = (b3 oct4b)
+ n28 = (b4 oct4b)
+ n29 = (b5 oct4b)
+ n30 = (b6 oct4b)
+ n31 = (b7 oct4b)
+ n32 = (b8 oct4b)
+ oct1b = (octet1 addr2)
+ oct2b = (octet2 addr2)
+ oct3b = (octet3 addr2)
+ oct4b = (octet4 addr2)
+
+
+
+-- HUnit Tests
+mk_testaddr :: Int -> Int -> Int -> Int -> IPv4Address
+mk_testaddr a b c d =
+ IPv4Address oct1 oct2 oct3 oct4
where
- a1 = (b1 oct1)
- a2 = (b2 oct1)
- a3 = (b3 oct1)
- a4 = (b4 oct1)
- a5 = (b5 oct1)
- a6 = (b6 oct1)
- a7 = (b7 oct1)
- a9 = (b1 oct2)
- a10 = (b2 oct2)
- a11 = (b3 oct2)
- a12 = (b4 oct2)
- a13 = (b5 oct2)
- a14 = (b6 oct2)
- a15 = (b7 oct2)
- a17 = (b1 oct3)
- a18 = (b2 oct3)
- a19 = (b3 oct3)
- a20 = (b4 oct3)
- a21 = (b5 oct3)
- a22 = (b6 oct3)
- a23 = (b7 oct3)
- a25 = (b1 oct4)
- a26 = (b2 oct4)
- a27 = (b3 oct4)
- a28 = (b4 oct4)
- a29 = (b5 oct4)
- a30 = (b6 oct4)
- a31 = (b7 oct4)
- oct1 = (octet1 addr)
- oct2 = (octet2 addr)
- oct3 = (octet3 addr)
- oct4 = (octet4 addr)
-
-
-
-min_octet1 :: IPv4Address -> Maskbits -> Octet
-min_octet1 addr mask = octet1 (min_address addr mask)
-
-min_octet2 :: IPv4Address -> Maskbits -> Octet
-min_octet2 addr mask = octet2 (min_address addr mask)
-
-min_octet3 :: IPv4Address -> Maskbits -> Octet
-min_octet3 addr mask = octet3 (min_address addr mask)
-
-min_octet4 :: IPv4Address -> Maskbits -> Octet
-min_octet4 addr mask = octet4 (min_address addr mask)
-
-max_octet1 :: IPv4Address -> Maskbits -> Octet
-max_octet1 addr mask = octet1 (max_address addr mask)
-
-max_octet2 :: IPv4Address -> Maskbits -> Octet
-max_octet2 addr mask = octet2 (max_address addr mask)
-
-max_octet3 :: IPv4Address -> Maskbits -> Octet
-max_octet3 addr mask = octet3 (max_address addr mask)
-
-max_octet4 :: IPv4Address -> Maskbits -> Octet
-max_octet4 addr mask = octet4 (max_address addr mask)
+ oct1 = octet_from_int a
+ oct2 = octet_from_int b
+ oct3 = octet_from_int c
+ oct4 = octet_from_int d
+
+
+test_most_sig_bit_different1 :: Test
+test_most_sig_bit_different1 =
+ 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))
+
+
+test_most_sig_bit_different2 :: Test
+test_most_sig_bit_different2 =
+ 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))
+
+
+ipv4address_tests :: [Test]
+ipv4address_tests = [ test_most_sig_bit_different1,
+ test_most_sig_bit_different2 ]