-module IPv4Address
-( ipv4address_from_octets,
+module IPv4Address(
+ IPv4Address(..),
+ ipv4address_properties,
ipv4address_tests,
- IPv4Address(None),
- max_address,
- min_address,
- most_sig_bit_different,
- octet1,
- octet2,
- octet3,
- octet4
-) where
-
-import Test.HUnit
-import Test.QuickCheck
-
-import Maskable
-import Maskbits
-import Octet
-
-data IPv4Address = None | IPv4Address { octet1 :: Octet,
- octet2 :: Octet,
- octet3 :: Octet,
- octet4 :: Octet }
- deriving (Eq)
+ most_sig_bit_different )
+where
+
+
+import Test.Tasty ( TestTree, testGroup )
+import Test.Tasty.HUnit ( (@?=), testCase )
+import Test.Tasty.QuickCheck (
+ Arbitrary( arbitrary ),
+ Gen,
+ Property,
+ (==>),
+ testProperty )
+
+import Maskable ( Maskable( apply_mask) )
+import Maskbits (
+ Maskbits(
+ Zero, One, Two, Three, Four, Five, Six, Seven, Eight,
+ Nine, Ten, Eleven, Twelve, Thirteen, Fourteen, Fifteen, Sixteen,
+ Seventeen, Eighteen, Nineteen, Twenty, TwentyOne, TwentyTwo, TwentyThree,
+ TwentyFour, TwentyFive, TwentySix, TwentySeven, TwentyEight, TwentyNine,
+ Thirty, ThirtyOne, ThirtyTwo ) )
+import Octet ( Octet( b1, b2, b3, b4, b5, b6, b7, b8) )
+
+data IPv4Address =
+ IPv4Address { octet1 :: Octet,
+ octet2 :: Octet,
+ octet3 :: Octet,
+ octet4 :: Octet }
+ deriving (Eq, Ord)
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)
+ 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
+ arbitrary = do
+ oct1 <- arbitrary :: Gen Octet
+ oct2 <- arbitrary :: Gen Octet
+ oct3 <- arbitrary :: Gen Octet
+ oct4 <- arbitrary :: Gen Octet
+ return (IPv4Address oct1 oct2 oct3 oct4)
instance Maskable IPv4Address where
- apply_mask _ Maskbits.None _ = IPv4Address.None
- apply_mask addr mask bit
- | mask == ThirtyTwo = addr
- | mask == ThirtyOne = addr { octet4 = (apply_mask oct4 Seven bit) }
- | mask == Thirty = addr { octet4 = (apply_mask oct4 Six bit) }
- | mask == TwentyNine = addr { octet4 = (apply_mask oct4 Five bit) }
- | mask == TwentyEight = addr { octet4 = (apply_mask oct4 Four bit) }
- | mask == TwentySeven = addr { octet4 = (apply_mask oct4 Three bit) }
- | mask == TwentySix = addr { octet4 = (apply_mask oct4 Two bit) }
- | mask == TwentyFive = addr { octet4 = (apply_mask oct4 One bit) }
- | mask == TwentyFour = addr { octet4 = (apply_mask oct4 Zero bit) }
- | mask == TwentyThree = addr { octet3 = (apply_mask oct3 Seven bit),
- octet4 = (apply_mask oct4 Zero bit) }
- | mask == TwentyTwo = addr { octet3 = (apply_mask oct3 Six bit),
- octet4 = (apply_mask oct4 Zero bit) }
- | mask == TwentyOne = addr { octet3 = (apply_mask oct3 Five bit),
- octet4 = (apply_mask oct4 Zero bit) }
- | mask == Twenty = addr { octet3 = (apply_mask oct3 Four bit),
- octet4 = (apply_mask oct4 Zero bit) }
- | mask == Nineteen = addr { octet3 = (apply_mask oct3 Three bit),
- octet4 = (apply_mask oct4 Zero bit) }
- | mask == Eighteen = addr { octet3 = (apply_mask oct3 Two bit),
- octet4 = (apply_mask oct4 Zero bit) }
- | mask == Seventeen = addr { octet3 = (apply_mask oct3 One bit),
- octet4 = (apply_mask oct4 Zero bit) }
- | mask == Sixteen = addr { octet3 = (apply_mask oct3 Zero bit),
- octet4 = (apply_mask oct4 Zero bit) }
- | mask == Fifteen = addr { octet2 = (apply_mask oct2 Seven bit),
- octet3 = (apply_mask oct3 Zero bit),
- octet4 = (apply_mask oct4 Zero bit) }
- | mask == Fourteen = addr { octet2 = (apply_mask oct2 Six bit),
- octet3 = (apply_mask oct3 Zero bit),
- octet4 = (apply_mask oct4 Zero bit)}
- | mask == Thirteen = addr { octet2 = (apply_mask oct2 Five bit),
- octet3 = (apply_mask oct3 Zero bit),
- octet4 = (apply_mask oct4 Zero bit)}
- | mask == Twelve = addr { octet2 = (apply_mask oct2 Four bit),
- octet3 = (apply_mask oct3 Zero bit),
- octet4 = (apply_mask oct4 Zero bit)}
- | mask == Eleven = addr { octet2 = (apply_mask oct2 Three bit),
- octet3 = (apply_mask oct3 Zero bit),
- octet4 = (apply_mask oct4 Zero bit)}
- | mask == Ten = addr { octet2 = (apply_mask oct2 Two bit),
- octet3 = (apply_mask oct3 Zero bit),
- octet4 = (apply_mask oct4 Zero bit)}
- | mask == Nine = addr { octet2 = (apply_mask oct2 One bit),
- octet3 = (apply_mask oct3 Zero bit),
- octet4 = (apply_mask oct4 Zero bit)}
- | mask == Eight = addr { octet2 = (apply_mask oct2 Zero bit),
- octet3 = (apply_mask oct3 Zero bit),
- octet4 = (apply_mask oct4 Zero bit)}
- | mask == Seven = addr { octet1 = (apply_mask oct1 Seven bit),
- octet2 = (apply_mask oct2 Zero bit),
- octet3 = (apply_mask oct3 Zero bit),
- octet4 = (apply_mask oct4 Zero bit)}
- | mask == Six = addr { octet1 = (apply_mask oct1 Six bit),
- octet2 = (apply_mask oct2 Zero bit),
- octet3 = (apply_mask oct3 Zero bit),
- octet4 = (apply_mask oct4 Zero bit)}
- | mask == Five = addr { octet1 = (apply_mask oct1 Five bit),
- octet2 = (apply_mask oct2 Zero bit),
- octet3 = (apply_mask oct3 Zero bit),
- octet4 = (apply_mask oct4 Zero bit)}
- | mask == Four = addr { octet1 = (apply_mask oct1 Four bit),
- octet2 = (apply_mask oct2 Zero bit),
- octet3 = (apply_mask oct3 Zero bit),
- octet4 = (apply_mask oct4 Zero bit)}
- | mask == Three = addr { octet1 = (apply_mask oct1 Three bit),
- octet2 = (apply_mask oct2 Zero bit),
- octet3 = (apply_mask oct3 Zero bit),
- octet4 = (apply_mask oct4 Zero bit)}
- | mask == Two = addr { octet1 = (apply_mask oct1 Two bit),
- octet2 = (apply_mask oct2 Zero bit),
- octet3 = (apply_mask oct3 Zero bit),
- octet4 = (apply_mask oct4 Zero bit)}
- | mask == One = addr { octet1 = (apply_mask oct1 One bit),
- octet2 = (apply_mask oct2 Zero bit),
- octet3 = (apply_mask oct3 Zero bit),
- octet4 = (apply_mask oct4 Zero bit)}
- | mask == Zero = addr { octet1 = (apply_mask oct1 Zero bit),
- octet2 = (apply_mask oct2 Zero bit),
- octet3 = (apply_mask oct3 Zero bit),
- octet4 = (apply_mask oct4 Zero bit)}
- | 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
--- way to construct an address from octets. As a result, we can
--- return IPv4Address.None in response to being passed one of more
--- Octet.None octets.
-ipv4address_from_octets :: Octet -> Octet -> Octet -> Octet -> IPv4Address
-ipv4address_from_octets oct1 oct2 oct3 oct4
- | or [oct1 == Octet.None,
- oct2 == Octet.None,
- oct3 == Octet.None,
- oct4 == Octet.None] = IPv4Address.None
- | otherwise = IPv4Address oct1 oct2 oct3 oct4
-
-
--- 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.
+
+ apply_mask addr mask bit =
+ apply_mask' mask
+ where
+ oct1 = octet1 addr
+ oct2 = octet2 addr
+ oct3 = octet3 addr
+ oct4 = octet4 addr
+
+ -- A copy of 'addr' with the fourth octet zeroed (or oned).
+ new_addr1 = addr { octet4 = (apply_mask oct4 Zero bit) }
+
+ -- Likewise for new_addr1's third octet.
+ new_addr2 = new_addr1 { octet3 = (apply_mask oct3 Zero bit) }
+
+ -- And new_addr2's second octet.
+ new_addr3 = new_addr2 { octet2 = (apply_mask oct2 Zero bit) }
+
+ -- This helper function allows us to pattern-match cleanly.
+ apply_mask' :: Maskbits -> IPv4Address
+
+ apply_mask' ThirtyTwo = addr
+
+ apply_mask' ThirtyOne = addr { octet4 = (apply_mask oct4 Seven bit) }
+
+ apply_mask' Thirty =
+ addr { octet4 = (apply_mask oct4 Six bit) }
+
+ apply_mask' TwentyNine =
+ addr { octet4 = (apply_mask oct4 Five bit) }
+
+ apply_mask' TwentyEight =
+ addr { octet4 = (apply_mask oct4 Four bit) }
+
+ apply_mask' TwentySeven =
+ addr { octet4 = (apply_mask oct4 Three bit) }
+
+ apply_mask' TwentySix =
+ addr { octet4 = (apply_mask oct4 Two bit) }
+
+ apply_mask' TwentyFive =
+ addr { octet4 = (apply_mask oct4 One bit) }
+
+ apply_mask' TwentyFour = new_addr1
+
+ apply_mask' TwentyThree =
+ new_addr1 { octet3 = (apply_mask oct3 Seven bit) }
+
+ apply_mask' TwentyTwo =
+ new_addr1 { octet3 = (apply_mask oct3 Six bit) }
+
+ apply_mask' TwentyOne =
+ new_addr1 { octet3 = (apply_mask oct3 Five bit) }
+
+ apply_mask' Twenty =
+ new_addr1 { octet3 = (apply_mask oct3 Four bit) }
+
+ apply_mask' Nineteen =
+ new_addr1 { octet3 = (apply_mask oct3 Three bit) }
+
+ apply_mask' Eighteen =
+ new_addr1 { octet3 = (apply_mask oct3 Two bit) }
+
+ apply_mask' Seventeen =
+ new_addr1 { octet3 = (apply_mask oct3 One bit) }
+
+ apply_mask' Sixteen =
+ new_addr2
+
+ apply_mask' Fifteen =
+ new_addr2 { octet2 = (apply_mask oct2 Seven bit) }
+
+ apply_mask' Fourteen =
+ new_addr2 { octet2 = (apply_mask oct2 Six bit) }
+
+ apply_mask' Thirteen =
+ new_addr2 { octet2 = (apply_mask oct2 Five bit) }
+
+ apply_mask' Twelve =
+ new_addr2 { octet2 = (apply_mask oct2 Four bit) }
+
+ apply_mask' Eleven =
+ new_addr2 { octet2 = (apply_mask oct2 Three bit) }
+
+ apply_mask' Ten =
+ new_addr2 { octet2 = (apply_mask oct2 Two bit) }
+
+ apply_mask' Nine =
+ new_addr2 { octet2 = (apply_mask oct2 One bit) }
+
+ apply_mask' Eight =
+ new_addr3 { octet2 = (apply_mask oct2 Zero bit) }
+
+ apply_mask' Seven =
+ new_addr3 { octet1 = (apply_mask oct1 Seven bit) }
+
+ apply_mask' Six =
+ new_addr3 { octet1 = (apply_mask oct1 Six bit) }
+
+ apply_mask' Five =
+ new_addr3 { octet1 = (apply_mask oct1 Five bit) }
+
+ apply_mask' Four =
+ new_addr3 { octet1 = (apply_mask oct1 Four bit) }
+
+ apply_mask' Three =
+ new_addr3 { octet1 = (apply_mask oct1 Three bit) }
+
+ apply_mask' Two =
+ new_addr3 { octet1 = (apply_mask oct1 Two bit) }
+
+ apply_mask' One =
+ new_addr3 { octet1 = (apply_mask oct1 One bit) }
+
+ apply_mask' Zero =
+ new_addr3 { octet1 = (apply_mask oct1 Zero bit) }
+
+
+instance Bounded IPv4Address where
+ -- | The minimum possible IPv4 address, 0.0.0.0.
+ minBound = IPv4Address minBound minBound minBound minBound
+
+ -- | The maximum possible IPv4 address, 255.255.255.255.
+ maxBound = IPv4Address maxBound maxBound maxBound maxBound
+
+
+
+
+instance Enum IPv4Address where
+ -- | Convert an 'Int' @x@ to an 'IPv4Address'. Each octet of @x@ is
+ -- right-shifted by the appropriate number of bits, and the fractional
+ -- part is dropped.
+ toEnum x =
+ IPv4Address oct1 oct2 oct3 oct4
+ where
+ -- Chop off the higher octets. x1 = x `mod` 2^32, would be
+ -- redundant.
+ x2 = x `mod` 2^(24 :: Integer)
+ x3 = x `mod` 2^(16 :: Integer)
+ x4 = x `mod` 2^(8 :: Integer)
+ -- Perform right-shifts. x4 doesn't need a shift.
+ shifted_x1 = x `quot` 2^(24 :: Integer)
+ shifted_x2 = x2 `quot` 2^(16 :: Integer)
+ shifted_x3 = x3 `quot` 2^(8 :: Integer)
+ oct1 = toEnum shifted_x1 :: Octet
+ oct2 = toEnum shifted_x2 :: Octet
+ oct3 = toEnum shifted_x3 :: Octet
+ oct4 = toEnum x4 :: Octet
+
+ -- | Convert @addr@ to an 'Int' by converting each octet to an 'Int'
+ -- and shifting the result to the left by 0,8.16, or 24 bits.
+ fromEnum addr =
+ (shifted_oct1) + (shifted_oct2) + (shifted_oct3) + oct4
+ where
+ oct1 = fromEnum (octet1 addr)
+ oct2 = fromEnum (octet2 addr)
+ oct3 = fromEnum (octet3 addr)
+ oct4 = fromEnum (octet4 addr)
+ shifted_oct1 = oct1 * 2^(24 :: Integer)
+ shifted_oct2 = oct2 * 2^(16 :: Integer)
+ shifted_oct3 = oct3 * 2^(8 :: Integer)
+
+-- | 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
oct4b = (octet4 addr2)
+-- Test lists.
+ipv4address_tests :: TestTree
+ipv4address_tests =
+ testGroup "IPv4 Address Tests" [
+ test_enum,
+ test_maxBound,
+ test_minBound,
+ test_most_sig_bit_different1,
+ test_most_sig_bit_different2,
+ test_to_enum ]
+
+ipv4address_properties :: TestTree
+ipv4address_properties =
+ testGroup
+ "IPv4 Address Properties "
+ [ prop_from_enum_to_enum_inverses ]
+
+-- QuickCheck properties
+prop_from_enum_to_enum_inverses :: TestTree
+prop_from_enum_to_enum_inverses =
+ testProperty "fromEnum and toEnum are inverses" prop
+ where
+ prop :: Int -> Property
+ prop x =
+ (0 <= x) && (x <= 2^(32 :: Integer) - 1) ==>
+ fromEnum (toEnum x :: IPv4Address) == x
-- HUnit Tests
mk_testaddr :: Int -> Int -> Int -> Int -> IPv4Address
mk_testaddr a b c d =
- IPv4Address oct1 oct2 oct3 oct4
- where
- oct1 = octet_from_int a
- oct2 = octet_from_int b
- oct3 = octet_from_int c
- oct4 = octet_from_int d
+ IPv4Address oct1 oct2 oct3 oct4
+ where
+ oct1 = toEnum a :: Octet
+ oct2 = toEnum b :: Octet
+ oct3 = toEnum c :: Octet
+ oct4 = toEnum d :: Octet
+
+
+test_minBound :: TestTree
+test_minBound =
+ testCase desc $ actual @?= expected
+ where
+ desc = "minBound should be 0.0.0.0"
+ expected = mk_testaddr 0 0 0 0
+ actual = minBound :: IPv4Address
+
+
+test_maxBound :: TestTree
+test_maxBound =
+ testCase desc $ actual @?= expected
+ where
+ desc = "maxBound should be 255.255.255.255"
+ expected = mk_testaddr 255 255 255 255
+ actual = maxBound :: IPv4Address
+
+
+test_enum :: TestTree
+test_enum =
+ testCase desc $ actual @?= expected
+ where
+ desc = "enumerating a /24 gives the correct addresses"
+ expected = ["192.168.0." ++ (show x) | x <- [0..255::Int] ]
+ lb = mk_testaddr 192 168 0 0
+ ub = mk_testaddr 192 168 0 255
+ actual = map show [lb..ub]
-test_most_sig_bit_different1 :: Test
+test_most_sig_bit_different1 :: TestTree
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))
+ testCase desc $ actual @?= expected
+ where
+ desc = "10.1.1.0 and 10.1.0.0 differ in bit 24"
+ addr1 = mk_testaddr 10 1 1 0
+ addr2 = (mk_testaddr 10 1 0 0)
+ expected = TwentyFour
+ actual = most_sig_bit_different addr1 addr2
+
-test_most_sig_bit_different2 :: Test
+test_most_sig_bit_different2 :: TestTree
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))
+ testCase desc $ actual @?= expected
+ where
+ desc = "10.1.2.0 and 10.1.1.0 differ in bit 23"
+ addr1 = mk_testaddr 10 1 2 0
+ addr2 = mk_testaddr 10 1 1 0
+ expected = TwentyThree
+ actual = most_sig_bit_different addr1 addr2
-ipv4address_tests :: [Test]
-ipv4address_tests = [ test_most_sig_bit_different1,
- test_most_sig_bit_different2 ]
+test_to_enum :: TestTree
+test_to_enum =
+ testCase desc $ actual @?= expected
+ where
+ desc = "192.168.0.0 in base-10 is 3232235520"
+ expected = mk_testaddr 192 168 0 0
+ actual = toEnum 3232235520 :: IPv4Address