- 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)
-
-
--- 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
-
-
-min_address :: IPv4Address -> Maskbits -> IPv4Address
-min_address IPv4Address.None _ = IPv4Address.None
-min_address _ Maskbits.None = IPv4Address.None
-min_address addr mask
- | mask == ThirtyTwo = IPv4Address oct1 oct2 oct3 oct4
- | mask == ThirtyOne = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 a28 a29 a30 a31 B.Zero)
- | mask == Thirty = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 a28 a29 a30 B.Zero B.Zero)
- | mask == TwentyNine = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 a28 a29 B.Zero B.Zero B.Zero)
- | mask == TwentyEight = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 a28 B.Zero B.Zero B.Zero B.Zero)
- | mask == TwentySeven = IPv4Address oct1 oct2 oct3 (Octet a25 a26 a27 B.Zero B.Zero B.Zero B.Zero B.Zero)
- | mask == TwentySix = IPv4Address oct1 oct2 oct3 (Octet a25 a26 B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero)
- | mask == TwentyFive = IPv4Address oct1 oct2 oct3 (Octet a25 B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero)
- | mask == TwentyFour = IPv4Address oct1 oct2 oct3 (min_octet)
- | mask == TwentyThree = IPv4Address oct1 oct2 (Octet a17 a18 a19 a20 a21 a22 a23 B.Zero) (min_octet)
- | mask == TwentyTwo = IPv4Address oct1 oct2 (Octet a17 a18 a19 a20 a21 a22 B.Zero B.Zero) (min_octet)
- | mask == TwentyOne = IPv4Address oct1 oct2 (Octet a17 a18 a19 a20 a21 B.Zero B.Zero B.Zero) (min_octet)
- | mask == Twenty = IPv4Address oct1 oct2 (Octet a17 a18 a19 a20 B.Zero B.Zero B.Zero B.Zero) (min_octet)
- | mask == Nineteen = IPv4Address oct1 oct2 (Octet a17 a18 a19 B.Zero B.Zero B.Zero B.Zero B.Zero) (min_octet)
- | mask == Eighteen = IPv4Address oct1 oct2 (Octet a17 a18 B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero) (min_octet)
- | mask == Seventeen = IPv4Address oct1 oct2 (Octet a17 B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero) (min_octet)
- | mask == Sixteen = IPv4Address oct1 oct2 (min_octet) (min_octet)
- | mask == Fifteen = IPv4Address oct1 (Octet a9 a10 a11 a12 a13 a14 a15 B.Zero) (min_octet) (min_octet)
- | mask == Fourteen = IPv4Address oct1 (Octet a9 a10 a11 a12 a13 a14 B.Zero B.Zero) (min_octet) (min_octet)
- | mask == Thirteen = IPv4Address oct1 (Octet a9 a10 a11 a12 a13 B.Zero B.Zero B.Zero) (min_octet) (min_octet)
- | mask == Twelve = IPv4Address oct1 (Octet a9 a10 a11 a12 B.Zero B.Zero B.Zero B.Zero) (min_octet) (min_octet)
- | mask == Eleven = IPv4Address oct1 (Octet a9 a10 a11 B.Zero B.Zero B.Zero B.Zero B.Zero) (min_octet) (min_octet)
- | mask == Ten = IPv4Address oct1 (Octet a9 a10 B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero) (min_octet) (min_octet)
- | mask == Nine = IPv4Address oct1 (Octet a9 B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero) (min_octet) (min_octet)
- | mask == Eight = IPv4Address oct1 (min_octet) (min_octet) (min_octet)
- | mask == Seven = IPv4Address (Octet a1 a2 a3 a4 a5 a6 a7 B.Zero) (min_octet) (min_octet) (min_octet)
- | mask == Six = IPv4Address (Octet a1 a2 a3 a4 a5 a6 B.Zero B.Zero) (min_octet) (min_octet) (min_octet)
- | mask == Five = IPv4Address (Octet a1 a2 a3 a4 a5 B.Zero B.Zero B.Zero) (min_octet) (min_octet) (min_octet)
- | mask == Four = IPv4Address (Octet a1 a2 a3 a4 B.Zero B.Zero B.Zero B.Zero) (min_octet) (min_octet) (min_octet)
- | mask == Three = IPv4Address (Octet a1 a2 a3 B.Zero B.Zero B.Zero B.Zero B.Zero) (min_octet) (min_octet) (min_octet)
- | mask == Two = IPv4Address (Octet a1 a2 B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero) (min_octet) (min_octet) (min_octet)
- | 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)
- | mask == Zero = IPv4Address (min_octet) (min_octet) (min_octet) (min_octet)
- | otherwise = 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)
+
+
+
+instance Maskable IPv4Address where
+
+ 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