]> gitweb.michael.orlitzky.com - hath.git/blob - src/Cidr.hs
a2aed97d6a9b39a84a2753c5d746a01eee898052
[hath.git] / src / Cidr.hs
1 module Cidr
2 ( Cidr,
3 from_string,
4 min_first_octet,
5 max_first_octet,
6 min_second_octet,
7 max_second_octet,
8 min_third_octet,
9 max_third_octet,
10 min_fourth_octet,
11 max_fourth_octet
12 ) where
13
14 import Data.Char (digitToInt, intToDigit)
15 import Numeric (readInt, showIntAtBase)
16 import ListUtils
17
18 type Maskbits = Int
19 type Octet = Int
20 type OctetList = (Octet, Octet, Octet, Octet)
21 type BaseTwoOctetList = (String, String, String, String)
22
23 data Cidr = Cidr { octet1 :: Octet,
24 octet2 :: Octet,
25 octet3 :: Octet,
26 octet4 :: Octet,
27 maskbits :: Maskbits }
28 deriving (Show)
29
30 first :: (a,b,c,d) -> a
31 first (w,_,_,_) = w
32
33 second :: (a,b,c,d) -> b
34 second (_,x,_,_) = x
35
36 third :: (a,b,c,d) -> c
37 third (_,_,y,_) = y
38
39 fourth :: (a,b,c,d) -> d
40 fourth (_,_,_,z) = z
41
42 min_first_octet :: Cidr -> Octet
43 min_first_octet cidr = first (min_octets cidr)
44
45 min_second_octet :: Cidr -> Octet
46 min_second_octet cidr = second (min_octets cidr)
47
48 min_third_octet :: Cidr -> Octet
49 min_third_octet cidr = third (min_octets cidr)
50
51 min_fourth_octet :: Cidr -> Octet
52 min_fourth_octet cidr = fourth (min_octets cidr)
53
54 max_first_octet :: Cidr -> Octet
55 max_first_octet cidr = first (max_octets cidr)
56
57 max_second_octet :: Cidr -> Octet
58 max_second_octet cidr = second (max_octets cidr)
59
60 max_third_octet :: Cidr -> Octet
61 max_third_octet cidr = third (max_octets cidr)
62
63 max_fourth_octet :: Cidr -> Octet
64 max_fourth_octet cidr = fourth (max_octets cidr)
65
66
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)
71
72
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))
77
78
79 from_string :: String -> Cidr
80 from_string s = Cidr (octs !! 0) (octs !! 1) (octs !! 2) (octs !! 3) mbits
81 where
82 octs = octets_from_string s
83 mbits = maskbits_from_string s
84
85
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
88 -- should do it.
89 is_binary_digit :: Char -> Bool
90 is_binary_digit c =
91 if c `elem` ['0','1'] then
92 True
93 else
94 False
95
96
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 ""
100
101
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 =
107 s1 ++ s2 ++ s3 ++ s4
108 where
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)
113
114
115 base_two_octetlist_to_octetlist :: BaseTwoOctetList -> OctetList
116 base_two_octetlist_to_octetlist b2ol =
117 (oct1, oct2, oct3, oct4)
118 where
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)
123
124
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
129 0
130 else
131 fst (parsed !! 0)
132 where
133 parsed = readInt 2 is_binary_digit digitToInt s
134
135
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
141 where
142 netpart = take (maskbits cidr) (octets_base_two cidr)
143
144
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
150 where
151 netpart = take (maskbits cidr) (octets_base_two cidr)
152
153
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)
158 where
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)
164
165
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)
170 where
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)
176
177
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)
181
182
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)