1 import qualified Data.Char as DC
2 import qualified Data.List as DL
3 import qualified Numeric as N
4 import System.Exit (exitFailure)
5 import Text.Regex.Posix
7 splitWith :: (a -> Bool) -> [a] -> [[a]]
11 _:ws -> splitWith p ws
12 where (ys,zs) = break p xs
15 octets :: String -> [Int]
16 octets cidr = map read (take 4 (splitWith (`elem` "./") cidr))
18 maskbits :: String -> Int
19 maskbits cidr = read ((splitWith (`elem` "/") cidr) !! 1)
22 pad_left_to :: Int -> a -> [a] -> [a]
23 pad_left_to len pad_elem xs =
24 if (length xs) >= len then
27 (replicate padcount pad_elem) ++ xs
29 padcount = len - (length xs)
31 pad_right_to :: Int -> a -> [a] -> [a]
32 pad_right_to len pad_elem xs =
33 if (length xs) >= len then
36 xs ++ (replicate padcount pad_elem)
38 padcount = len - (length xs)
41 base_two :: Int -> String
42 base_two n = N.showIntAtBase 2 DC.intToDigit n ""
44 ip_base_two :: [Int] -> String
45 ip_base_two octet_list =
46 DL.concatMap ((pad_left_to 8 '0') .base_two) octet_list
48 min_base_two_address :: String -> String
49 min_base_two_address cidr =
50 pad_right_to 32 '0' netpart
52 netpart = take (maskbits cidr) (ip_base_two (octets cidr))
54 max_base_two_address :: String -> String
55 max_base_two_address cidr =
56 pad_right_to 32 '1' netpart
58 netpart = take (maskbits cidr) (ip_base_two (octets cidr))
60 min_base_two_octets :: String -> [String]
61 min_base_two_octets cidr =
62 [octet1, octet2, octet3, octet4]
64 addr = min_base_two_address cidr
65 octet1 = fst (DL.splitAt 8 addr)
66 octet2 = fst (DL.splitAt 8 (snd (DL.splitAt 8 addr)))
67 octet3 = fst (DL.splitAt 8 (snd (DL.splitAt 16 addr)))
68 octet4 = snd (DL.splitAt 24 addr)
70 max_base_two_octets :: String -> [String]
71 max_base_two_octets cidr =
72 [octet1, octet2, octet3, octet4]
74 addr = max_base_two_address cidr
75 octet1 = fst (DL.splitAt 8 addr)
76 octet2 = fst (DL.splitAt 8 (snd (DL.splitAt 8 addr)))
77 octet3 = fst (DL.splitAt 8 (snd (DL.splitAt 16 addr)))
78 octet4 = snd (DL.splitAt 24 addr)
81 min_octets :: String -> [Int]
83 map base_two_to_base_ten (min_base_two_octets cidr)
85 max_octets :: String -> [Int]
87 map base_two_to_base_ten (max_base_two_octets cidr)
89 is_binary_digit :: Char -> Bool
91 if c `elem` ['0','1'] then
96 base_two_to_base_ten :: String -> Int
97 base_two_to_base_ten s =
98 if (length parsed) == 0 then
103 parsed = N.readInt 2 is_binary_digit DC.digitToInt s
106 cidr_to_regex :: String -> String
108 range1 ++ "\\." ++ range2 ++ "\\." ++ range3 ++ "\\." ++ range4
110 range1 = numeric_range min1 max1
111 range2 = numeric_range min2 max2
112 range3 = numeric_range min3 max3
113 range4 = numeric_range min4 max4
114 min1 = (min_octets cidr) !! 0
115 min2 = (min_octets cidr) !! 1
116 min3 = (min_octets cidr) !! 2
117 min4 = (min_octets cidr) !! 3
118 max1 = (max_octets cidr) !! 0
119 max2 = (max_octets cidr) !! 1
120 max3 = (max_octets cidr) !! 2
121 max4 = (max_octets cidr) !! 3
124 is_valid_cidr :: String -> Bool
125 is_valid_cidr cidr = cidr =~ "([0-9]{1,3}\\.){3}[0-9]{1,3}/[0-9]{1,2}"
127 alternate :: [String] -> String
128 alternate terms = "(" ++ (concat (DL.intersperse "|" terms)) ++ ")"
130 numeric_range :: Int -> Int -> String
132 alternate (map show [lower..upper])
134 lower = minimum [x,y]
135 upper = maximum [x,y]
141 if (is_valid_cidr line)
143 putStrLn (cidr_to_regex line)
145 putStrLn "Error: not valid CIDR notation."