From cbb5c7ddfee2f5061f247cbed0b4a5d790c14db0 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sun, 9 May 2010 13:03:54 -0400 Subject: [PATCH] Added the adjacent function to Cidr. --- src/Cidr.hs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Cidr.hs b/src/Cidr.hs index df35138..1b7d1dd 100644 --- a/src/Cidr.hs +++ b/src/Cidr.hs @@ -114,3 +114,21 @@ redundant cidrlist cidr = any ((flip contains_proper) cidr) cidrlist combine_all :: [Cidr] -> [Cidr] combine_all cidrs = filter (not . (redundant cidrs)) cidrs + + +-- Determine whether or not two CIDR ranges are adjacent. If two +-- ranges lie consecutively within the IP space, they can be +-- combined. For example, 10.1.0.0/24 and 10.0.1.0/24 are adjacent, +-- and can be combined in to 10.1.0.0/23. +adjacent :: Cidr -> Cidr -> Bool +adjacent Cidr.None _ = False +adjacent _ Cidr.None = False +adjacent cidr1 cidr2 + | mbits1 /= mbits2 = False + | mbits1 == Maskbits.Zero = False -- They're equal. + | otherwise = (mbits1 == (most_sig_bit_different addr1 addr2)) + where + addr1 = ipv4address cidr1 + addr2 = ipv4address cidr2 + mbits1 = maskbits cidr1 + mbits2 = maskbits cidr2 -- 2.44.2