+ partlist = splitOneOf "/" s
+
+
+-- | 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 =
+ mapMaybe readMaybe (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.
+--
+-- On the other hand, if the first argument (cidr1) has fewer (or
+-- the same number of) network bits as the second, it can contain
+-- the second. In this case, we need to check that every host in
+-- cidr2 is contained in cidr1. If a host in cidr2 is contained in
+-- cidr1, then at least mbits1 of an address in cidr2 will match
+-- cidr1. For example,
+--
+-- cidr1 = 192.168.1.0\/23, cidr2 = 192.168.1.100\/24
+--
+-- Here, cidr2 contains all of 192.168.1.0 through
+-- 192.168.1.255. However, cidr1 contains BOTH 192.168.0.0 through
+-- 192.168.0.255 and 192.168.1.0 through 192.168.1.255. In essence,
+-- what we want to check is that cidr2 "begins with" something that
+-- cidr1 CAN begin with. Since cidr1 can begin with 192.168.1, and
+-- cidr2 DOES, cidr1 contains cidr2..
+--
+-- The way that we check this is to apply cidr1's mask to cidr2's
+-- address and see if the result is the same as cidr1's mask applied
+-- to cidr1's address.
+--
+contains :: Cidr -> Cidr -> Bool
+contains (Cidr addr1 mbits1) (Cidr addr2 mbits2)
+ | mbits1 > mbits2 = False
+ | otherwise = addr1masked == addr2masked
+ where
+ addr1masked = apply_mask addr1 mbits1 B.Zero
+ addr2masked = apply_mask addr2 mbits1 B.Zero
+
+
+-- | Contains but is not equal to.
+contains_proper :: Cidr -> Cidr -> Bool
+contains_proper cidr1 cidr2 =
+ (cidr1 `contains` cidr2) && (not (cidr2 `contains` cidr1))
+
+
+-- | A CIDR range is redundant (with respect to the given list) if
+-- another CIDR range in that list properly contains it.
+redundant :: [Cidr] -> Cidr -> Bool
+redundant cidrlist cidr = any ((flip contains_proper) cidr) cidrlist
+
+
+-- | First, we look at all possible pairs of cidrs, and combine the
+-- adjacent ones in to a new list. Then, we concatenate that list
+-- with the original one, and filter out all of the redundancies. If
+-- two adjacent Cidrs are combined into a larger one, they will be
+-- removed in the second step since the larger Cidr must contain the
+-- smaller two.
+--
+-- Once this is done, we see whether or not the result is different
+-- than the argument that was passed in. If nothing changed, we're
+-- done and return the list that was passed to us. However, if
+-- something changed, we recurse and try to combine the list again.
+combine_all :: [Cidr] -> [Cidr]
+combine_all cidrs
+ | cidrs == (combine_contained unique_cidrs) = cidrs
+ | otherwise = combine_all (combine_contained unique_cidrs)