--- Return true if the first argument (a CIDR range) contains the
--- second (another CIDR range). There are a lot of ways we can be fed
--- junk here. For lack of a better alternative, just return False when
--- we are given nonsense.
-contains :: Cidr -> Cidr -> Bool
-contains Cidr.None _ = False
-contains _ Cidr.None = False
-contains (Cidr _ Maskbits.None) _ = False
-contains (Cidr IPv4Address.None _) _ = False
-contains _ (Cidr _ Maskbits.None) = False
-contains _ (Cidr IPv4Address.None _) = False
-
--- If the number of bits in the network part of the first address is
--- larger than the number of bits in the second, there is no way that
--- the first range can contain the second. For, if the number of
--- network bits is larger, then the number of host bits must be
--- smaller, and if cidr1 has fewer hosts than cidr2, cidr1 most
--- certainly does not contain cidr2.
+-- | Takes an IP address String in CIDR notation, and returns a list
+-- of its octets (as Ints).
+octets_from_cidr_string :: String -> [Octet]
+octets_from_cidr_string s =
+ case parts of
+ (p1:p2:p3:p4:_) -> mapMaybe readMaybe [p1,p2,p3,p4]
+ _ -> []
+ where
+ parts = splitOneOf "./" s
+
+instance Read Cidr where
+ -- | Parse everything or nothing.
+ readsPrec _ s =
+ case (octets_from_cidr_string s) of
+ [oct1, oct2, oct3, oct4] ->
+ case (maskbits_from_cidr_string s) of
+ Just mbits ->
+ [(Cidr (IPv4Address oct1 oct2 oct3 oct4) mbits, "")]
+ _ -> []
+ _ -> []
+
+
+-- | Given a CIDR, return the minimum valid IPv4 address contained
+-- within it.
+min_host :: Cidr -> IPv4Address
+min_host (Cidr addr mask) = apply_mask addr mask B.Zero
+
+-- | Given a CIDR, return the maximum valid IPv4 address contained
+-- within it.
+max_host :: Cidr -> IPv4Address
+max_host (Cidr addr mask) = apply_mask addr mask B.One
+
+-- | Given a CIDR, return the first octet of the minimum valid IPv4
+-- address contained within it.
+min_octet1 :: Cidr -> Octet
+min_octet1 cidr = octet1 (min_host cidr)
+
+-- | Given a CIDR, return the second octet of the minimum valid IPv4
+-- address contained within it.
+min_octet2 :: Cidr -> Octet
+min_octet2 cidr = octet2 (min_host cidr)
+
+-- | Given a CIDR, return the third octet of the minimum valid IPv4
+-- address contained within it.
+min_octet3 :: Cidr -> Octet
+min_octet3 cidr = octet3 (min_host cidr)
+
+-- | Given a CIDR, return the fourth octet of the minimum valid IPv4
+-- address contained within it.
+min_octet4 :: Cidr -> Octet
+min_octet4 cidr = octet4 (min_host cidr)
+
+-- | Given a CIDR, return the first octet of the maximum valid IPv4
+-- address contained within it.
+max_octet1 :: Cidr -> Octet
+max_octet1 cidr = octet1 (max_host cidr)
+
+-- | Given a CIDR, return the second octet of the maximum valid IPv4
+-- address contained within it.
+max_octet2 :: Cidr -> Octet
+max_octet2 cidr = octet2 (max_host cidr)
+
+-- | Given a CIDR, return the third octet of the maximum valid IPv4
+-- address contained within it.
+max_octet3 :: Cidr -> Octet
+max_octet3 cidr = octet3 (max_host cidr)
+
+-- | Given a CIDR, return the fourth octet of the maximum valid IPv4
+-- address contained within it.
+max_octet4 :: Cidr -> Octet
+max_octet4 cidr = octet4 (max_host cidr)
+
+
+
+-- | Return true if the first argument (a CIDR range) contains the
+-- second (another CIDR range). There are a lot of ways we can be
+-- fed junk here. For lack of a better alternative, just return
+-- False when we are given nonsense.
+--
+-- If the number of bits in the network part of the first address is
+-- larger than the number of bits in the second, there is no way
+-- that the first range can contain the second. For, if the number
+-- of network bits is larger, then the number of host bits must be
+-- smaller, and if cidr1 has fewer hosts than cidr2, cidr1 most
+-- certainly does not contain cidr2.