- map octet_from_string (take 4 (splitWith (`elem` "./") s))
-
-
-cidr_from_string :: String -> Cidr
-cidr_from_string s
- | addr == IPv4Address.None = Cidr.None
- | mbits == Maskbits.None = Cidr.None
- | otherwise = Cidr addr mbits
- where
- addr = ipv4address_from_octets (oct1) (oct2) (oct3) (oct4)
- oct1 = (octs !! 0)
- oct2 = (octs !! 1)
- oct3 = (octs !! 2)
- oct4 = (octs !! 3)
- octs = octets_from_cidr_string s
- mbits = maskbits_from_cidr_string s
-
-
-
--- 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.
+ catMaybes $ map octet_from_string (take 4 (splitOneOf "./" s))
+
+
+-- | Return Nothing if we can't parse both maskbits and octets from
+-- the string.
+cidr_from_string :: String -> Maybe Cidr
+cidr_from_string s =
+ case (octets_from_cidr_string s) of
+ [oct1, oct2, oct3, oct4] ->
+ case (maskbits_from_cidr_string s) of
+ Just mbits ->
+ Just $ Cidr (IPv4Address oct1 oct2 oct3 oct4) mbits
+ _ -> Nothing
+ _ -> Nothing
+
+
+
+-- | 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.