15 import Data.Char (digitToInt, intToDigit)
16 import Numeric (readInt, showIntAtBase)
17 import Text.Regex.Posix
23 type OctetList = (Octet, Octet, Octet, Octet)
24 type BaseTwoOctetList = (String, String, String, String)
26 data Cidr = Cidr { octet1 :: Octet,
30 maskbits :: Maskbits }
33 -- Will return True if the passed String is in CIDR notation, False
35 is_valid_cidr :: String -> Bool
36 is_valid_cidr cidr = cidr =~ "([0-9]{1,3}\\.){3}[0-9]{1,3}/[0-9]{1,2}"
38 first :: (a,b,c,d) -> a
41 second :: (a,b,c,d) -> b
44 third :: (a,b,c,d) -> c
47 fourth :: (a,b,c,d) -> d
50 min_first_octet :: Cidr -> Octet
51 min_first_octet cidr = first (min_octets cidr)
53 min_second_octet :: Cidr -> Octet
54 min_second_octet cidr = second (min_octets cidr)
56 min_third_octet :: Cidr -> Octet
57 min_third_octet cidr = third (min_octets cidr)
59 min_fourth_octet :: Cidr -> Octet
60 min_fourth_octet cidr = fourth (min_octets cidr)
62 max_first_octet :: Cidr -> Octet
63 max_first_octet cidr = first (max_octets cidr)
65 max_second_octet :: Cidr -> Octet
66 max_second_octet cidr = second (max_octets cidr)
68 max_third_octet :: Cidr -> Octet
69 max_third_octet cidr = third (max_octets cidr)
71 max_fourth_octet :: Cidr -> Octet
72 max_fourth_octet cidr = fourth (max_octets cidr)
75 -- Returns the mask portion of a CIDR address. That is, everything
76 -- after the trailing slash.
77 maskbits_from_string :: String -> Maskbits
78 maskbits_from_string s = read ((splitWith (`elem` "/") s) !! 1)
81 -- Takes an IP address String in CIDR notation, and returns a list of
82 -- its octets (converted to Int).
83 octets_from_string :: String -> [Octet]
84 octets_from_string s = map read (take 4 (splitWith (`elem` "./") s))
87 from_string :: String -> Cidr
88 from_string s = Cidr (octs !! 0) (octs !! 1) (octs !! 2) (octs !! 3) mbits
90 octs = octets_from_string s
91 mbits = maskbits_from_string s
94 -- The base_two_to_base_ten function requires a way to determine
95 -- whether or not the character it's currently parsing is valid. This
97 is_binary_digit :: Char -> Bool
99 if c `elem` ['0','1'] then
105 -- Takes an Int, and returns its base-two representation as a String.
106 base_two :: Int -> String
107 base_two n = showIntAtBase 2 intToDigit n ""
110 -- Takes a set of octets, and converts them to base-two
111 -- individually. The results are then zero-padded on the left to 8
112 -- characters, and concatenated together.
113 octets_base_two :: Cidr -> String
114 octets_base_two cidr =
117 s1 = ((pad_left_to 8 '0') . base_two) (octet1 cidr)
118 s2 = ((pad_left_to 8 '0') . base_two) (octet2 cidr)
119 s3 = ((pad_left_to 8 '0') . base_two) (octet3 cidr)
120 s4 = ((pad_left_to 8 '0') . base_two) (octet4 cidr)
123 base_two_octetlist_to_octetlist :: BaseTwoOctetList -> OctetList
124 base_two_octetlist_to_octetlist b2ol =
125 (oct1, oct2, oct3, oct4)
127 oct1 = base_two_to_base_ten (first b2ol)
128 oct2 = base_two_to_base_ten (second b2ol)
129 oct3 = base_two_to_base_ten (third b2ol)
130 oct4 = base_two_to_base_ten (fourth b2ol)
133 -- Convert a base-two String to an Int.
134 base_two_to_base_ten :: String -> Int
135 base_two_to_base_ten s =
136 if (length parsed) == 0 then
141 parsed = readInt 2 is_binary_digit digitToInt s
144 -- Returns the minimum address (as a base-two string) satisfying the
145 -- given CIDR string.
146 min_base_two_address :: Cidr -> String
147 min_base_two_address cidr =
148 pad_right_to 32 '0' netpart
150 netpart = take (maskbits cidr) (octets_base_two cidr)
153 -- Returns the maximum address (as a base-two string) satisfying the
154 -- given CIDR string.
155 max_base_two_address :: Cidr -> String
156 max_base_two_address cidr =
157 pad_right_to 32 '1' netpart
159 netpart = take (maskbits cidr) (octets_base_two cidr)
162 -- The octet components of min_base_two_address, as a base-two String.
163 min_base_two_octets :: Cidr -> BaseTwoOctetList
164 min_base_two_octets cidr =
165 (oct1, oct2, oct3, oct4)
167 addr = min_base_two_address cidr
168 oct1 = fst (splitAt 8 addr)
169 oct2 = fst (splitAt 8 (snd (splitAt 8 addr)))
170 oct3 = fst (splitAt 8 (snd (splitAt 16 addr)))
171 oct4 = snd (splitAt 24 addr)
174 -- The octet components of max_base_two_address, as a base-two String.
175 max_base_two_octets :: Cidr -> BaseTwoOctetList
176 max_base_two_octets cidr =
177 (oct1, oct2, oct3, oct4)
179 addr = max_base_two_address cidr
180 oct1 = fst (splitAt 8 addr)
181 oct2 = fst (splitAt 8 (snd (splitAt 8 addr)))
182 oct3 = fst (splitAt 8 (snd (splitAt 16 addr)))
183 oct4 = snd (splitAt 24 addr)
186 -- The octet components of min_base_two_address, as Ints.
187 min_octets :: Cidr -> OctetList
188 min_octets cidr = base_two_octetlist_to_octetlist (min_base_two_octets cidr)
191 -- The octet components of max_base_two_address, as Ints.
192 max_octets :: Cidr -> OctetList
193 max_octets cidr = base_two_octetlist_to_octetlist (max_base_two_octets cidr)