X-Git-Url: http://gitweb.michael.orlitzky.com/?p=hath.git;a=blobdiff_plain;f=src%2FIPv4Address.hs;h=f4bfc9749cab1adb63ffcb7b0907d0ce3c9ebae2;hp=15e69665e7ab8e22c4bd027b401860d579cf3727;hb=45ac9baca360d0f0dabe4576fb44c1634a50dbf7;hpb=18a81546e7c4ad10574918efb08b2e104e911d73 diff --git a/src/IPv4Address.hs b/src/IPv4Address.hs index 15e6966..f4bfc97 100644 --- a/src/IPv4Address.hs +++ b/src/IPv4Address.hs @@ -1,20 +1,19 @@ module IPv4Address( + IPv4Address(..), ipv4address_properties, ipv4address_tests, - IPv4Address(..), - most_sig_bit_different, -) where + most_sig_bit_different ) +where -import Data.Maybe (fromJust) import Test.HUnit (assertEqual) import Test.Framework (Test, testGroup) import Test.Framework.Providers.HUnit (testCase) import Test.Framework.Providers.QuickCheck2 (testProperty) import Test.QuickCheck (Arbitrary(..), Gen, Property, (==>)) -import Maskable -import Maskbits -import Octet +import Maskable (Maskable(..)) +import Maskbits (Maskbits(..)) +import Octet (Octet(..)) data IPv4Address = IPv4Address { octet1 :: Octet, @@ -174,55 +173,40 @@ instance Bounded IPv4Address where --- | 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. -ipv4address_to_int :: IPv4Address -> Int -ipv4address_to_int 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) - - - --- | 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. -ipv4address_from_int :: Int -> Maybe IPv4Address -ipv4address_from_int x - | (x < 0) || (x > 2^(32 :: Integer) - 1) = Nothing - | otherwise = do - -- If the algebra is right, none of these octet_from_int calls - -- below can fail since 0 <= x <= 2^32 - 1. - oct1 <- octet_from_int shifted_x1 - oct2 <- octet_from_int shifted_x2 - oct3 <- octet_from_int shifted_x3 - oct4 <- octet_from_int x4 - return $ 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) - instance Enum IPv4Address where - -- We're supposed to throw a runtime error if you call (succ - -- maxBound), so the fromJust here doesn't introduce any additional - -- badness. - toEnum = fromJust . ipv4address_from_int - fromEnum = ipv4address_to_int + -- | 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 + oct2 = toEnum shifted_x2 + oct3 = toEnum shifted_x3 + oct4 = toEnum x4 + + -- | 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 @@ -368,10 +352,10 @@ mk_testaddr :: Int -> Int -> Int -> Int -> IPv4Address mk_testaddr a b c d = IPv4Address oct1 oct2 oct3 oct4 where - oct1 = fromJust $ octet_from_int a - oct2 = fromJust $ octet_from_int b - oct3 = fromJust $ octet_from_int c - oct4 = fromJust $ octet_from_int d + oct1 = toEnum a + oct2 = toEnum b + oct3 = toEnum c + oct4 = toEnum d test_minBound :: Test test_minBound =