- 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
--- 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 = addr
- | mask == ThirtyOne = addr { octet4 = apply_mask oct4 Seven }
- | mask == Thirty = addr { octet4 = apply_mask oct4 Six }
- | mask == TwentyNine = addr { octet4 = apply_mask oct4 Five }
- | mask == TwentyEight = addr { octet4 = apply_mask oct4 Four }
- | mask == TwentySeven = addr { octet4 = apply_mask oct4 Three }
- | mask == TwentySix = addr { octet4 = apply_mask oct4 Two }
- | mask == TwentyFive = addr { octet4 = apply_mask oct4 One }
- | mask == TwentyFour = addr { octet4 = min_octet }
- | mask == TwentyThree = addr { octet3 = apply_mask oct3 Seven,
- octet4 = min_octet }
- | mask == TwentyTwo = addr { octet3 = apply_mask oct3 Six,
- octet4 = min_octet }
- | mask == TwentyOne = addr { octet3 = apply_mask oct3 Five,
- octet4 = min_octet }
- | mask == Twenty = addr { octet3 = apply_mask oct3 Four,
- octet4 = min_octet }
- | mask == Nineteen = addr { octet3 = apply_mask oct3 Three,
- octet4 = min_octet }
- | mask == Eighteen = addr { octet3 = apply_mask oct3 Two,
- octet4 = min_octet }
- | mask == Seventeen = addr { octet3 = apply_mask oct3 One,
- octet4 = min_octet }
- | mask == Sixteen = addr { octet3 = min_octet,
- octet4 = min_octet }
- | mask == Fifteen = addr { octet2 = apply_mask oct2 Seven,
- octet3 = min_octet,
- octet4 = min_octet }
- | mask == Fourteen = addr { octet2 = apply_mask oct2 Six,
- octet3 = min_octet,
- octet4 = min_octet }
- | mask == Thirteen = addr { octet2 = apply_mask oct2 Five,
- octet3 = min_octet,
- octet4 = min_octet }
- | mask == Twelve = addr { octet2 = apply_mask oct2 Four,
- octet3 = min_octet,
- octet4 = min_octet }
- | mask == Eleven = addr { octet2 = apply_mask oct2 Three,
- octet3 = min_octet,
- octet4 = min_octet }
- | mask == Ten = addr { octet2 = apply_mask oct2 Two,
- octet3 = min_octet,
- octet4 = min_octet }
- | mask == Nine = addr { octet2 = apply_mask oct2 One,
- octet3 = min_octet,
- octet4 = min_octet }
- | mask == Eight = addr { octet2 = min_octet,
- octet3 = min_octet,
- octet4 = min_octet }
- | mask == Seven = addr { octet1 = apply_mask oct1 Seven,
- octet2 = min_octet,
- octet3 = min_octet,
- octet4 = min_octet }
- | mask == Six = addr { octet1 = apply_mask oct1 Six,
- octet2 = min_octet,
- octet3 = min_octet,
- octet4 = min_octet }
- | mask == Five = addr { octet1 = apply_mask oct1 Five,
- octet2 = min_octet,
- octet3 = min_octet,
- octet4 = min_octet }
- | mask == Four = addr { octet1 = apply_mask oct1 Four,
- octet2 = min_octet,
- octet3 = min_octet,
- octet4 = min_octet }
- | mask == Three = addr { octet1 = apply_mask oct1 Three,
- octet2 = min_octet,
- octet3 = min_octet,
- octet4 = min_octet }
- | mask == Two = addr { octet1 = apply_mask oct1 Two,
- octet2 = min_octet,
- octet3 = min_octet,
- octet4 = min_octet }
- | mask == One = addr { octet1 = apply_mask oct1 One,
- octet2 = min_octet,
- octet3 = min_octet,
- octet4 = min_octet }
- | mask == Zero = addr { octet1 = min_octet,
- octet2 = min_octet,
- octet3 = min_octet,
- octet4 = min_octet }
- | otherwise = addr
+
+ 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 signed_x =
+ IPv4Address oct1 oct2 oct3 oct4