14 import Data.Char (digitToInt, intToDigit)
15 import Numeric (readInt, showIntAtBase)
20 type OctetList = (Octet, Octet, Octet, Octet)
21 type BaseTwoOctetList = (String, String, String, String)
23 data Cidr = Cidr { octet1 :: Octet,
27 maskbits :: Maskbits }
30 first :: (a,b,c,d) -> a
33 second :: (a,b,c,d) -> b
36 third :: (a,b,c,d) -> c
39 fourth :: (a,b,c,d) -> d
42 min_first_octet :: Cidr -> Octet
43 min_first_octet cidr = first (min_octets cidr)
45 min_second_octet :: Cidr -> Octet
46 min_second_octet cidr = second (min_octets cidr)
48 min_third_octet :: Cidr -> Octet
49 min_third_octet cidr = third (min_octets cidr)
51 min_fourth_octet :: Cidr -> Octet
52 min_fourth_octet cidr = fourth (min_octets cidr)
54 max_first_octet :: Cidr -> Octet
55 max_first_octet cidr = first (max_octets cidr)
57 max_second_octet :: Cidr -> Octet
58 max_second_octet cidr = second (max_octets cidr)
60 max_third_octet :: Cidr -> Octet
61 max_third_octet cidr = third (max_octets cidr)
63 max_fourth_octet :: Cidr -> Octet
64 max_fourth_octet cidr = fourth (max_octets cidr)
67 -- Returns the mask portion of a CIDR address. That is, everything
68 -- after the trailing slash.
69 maskbits_from_string :: String -> Maskbits
70 maskbits_from_string s = read ((splitWith (`elem` "/") s) !! 1)
73 -- Takes an IP address String in CIDR notation, and returns a list of
74 -- its octets (converted to Int).
75 octets_from_string :: String -> [Octet]
76 octets_from_string s = map read (take 4 (splitWith (`elem` "./") s))
79 from_string :: String -> Cidr
80 from_string s = Cidr (octs !! 0) (octs !! 1) (octs !! 2) (octs !! 3) mbits
82 octs = octets_from_string s
83 mbits = maskbits_from_string s
86 -- The base_two_to_base_ten function requires a way to determine
87 -- whether or not the character it's currently parsing is valid. This
89 is_binary_digit :: Char -> Bool
91 if c `elem` ['0','1'] then
97 -- Takes an Int, and returns its base-two representation as a String.
98 base_two :: Int -> String
99 base_two n = showIntAtBase 2 intToDigit n ""
102 -- Takes a set of octets, and converts them to base-two
103 -- individually. The results are then zero-padded on the left to 8
104 -- characters, and concatenated together.
105 octets_base_two :: Cidr -> String
106 octets_base_two cidr =
109 s1 = ((pad_left_to 8 '0') . base_two) (octet1 cidr)
110 s2 = ((pad_left_to 8 '0') . base_two) (octet2 cidr)
111 s3 = ((pad_left_to 8 '0') . base_two) (octet3 cidr)
112 s4 = ((pad_left_to 8 '0') . base_two) (octet4 cidr)
115 base_two_octetlist_to_octetlist :: BaseTwoOctetList -> OctetList
116 base_two_octetlist_to_octetlist b2ol =
117 (oct1, oct2, oct3, oct4)
119 oct1 = base_two_to_base_ten (first b2ol)
120 oct2 = base_two_to_base_ten (second b2ol)
121 oct3 = base_two_to_base_ten (third b2ol)
122 oct4 = base_two_to_base_ten (fourth b2ol)
125 -- Convert a base-two String to an Int.
126 base_two_to_base_ten :: String -> Int
127 base_two_to_base_ten s =
128 if (length parsed) == 0 then
133 parsed = readInt 2 is_binary_digit digitToInt s
136 -- Returns the minimum address (as a base-two string) satisfying the
137 -- given CIDR string.
138 min_base_two_address :: Cidr -> String
139 min_base_two_address cidr =
140 pad_right_to 32 '0' netpart
142 netpart = take (maskbits cidr) (octets_base_two cidr)
145 -- Returns the maximum address (as a base-two string) satisfying the
146 -- given CIDR string.
147 max_base_two_address :: Cidr -> String
148 max_base_two_address cidr =
149 pad_right_to 32 '1' netpart
151 netpart = take (maskbits cidr) (octets_base_two cidr)
154 -- The octet components of min_base_two_address, as a base-two String.
155 min_base_two_octets :: Cidr -> BaseTwoOctetList
156 min_base_two_octets cidr =
157 (oct1, oct2, oct3, oct4)
159 addr = min_base_two_address cidr
160 oct1 = fst (splitAt 8 addr)
161 oct2 = fst (splitAt 8 (snd (splitAt 8 addr)))
162 oct3 = fst (splitAt 8 (snd (splitAt 16 addr)))
163 oct4 = snd (splitAt 24 addr)
166 -- The octet components of max_base_two_address, as a base-two String.
167 max_base_two_octets :: Cidr -> BaseTwoOctetList
168 max_base_two_octets cidr =
169 (oct1, oct2, oct3, oct4)
171 addr = max_base_two_address cidr
172 oct1 = fst (splitAt 8 addr)
173 oct2 = fst (splitAt 8 (snd (splitAt 8 addr)))
174 oct3 = fst (splitAt 8 (snd (splitAt 16 addr)))
175 oct4 = snd (splitAt 24 addr)
178 -- The octet components of min_base_two_address, as Ints.
179 min_octets :: Cidr -> OctetList
180 min_octets cidr = base_two_octetlist_to_octetlist (min_base_two_octets cidr)
183 -- The octet components of max_base_two_address, as Ints.
184 max_octets :: Cidr -> OctetList
185 max_octets cidr = base_two_octetlist_to_octetlist (max_base_two_octets cidr)