]> gitweb.michael.orlitzky.com - hath.git/blob - src/Main.hs
deb902297c45d3fac0990854f7a471c80f09a9b2
[hath.git] / src / Main.hs
1 module Main
2 where
3
4 import Control.Concurrent.ParallelIO.Global ( stopGlobalPool )
5 import Control.Monad (when)
6 import qualified Data.ByteString.Char8 as BS (intercalate, pack, unpack)
7 import Data.List ((\\), intercalate)
8 import Data.Maybe (catMaybes, isNothing)
9 import Data.String.Utils (splitWs)
10 import System.Exit (ExitCode(..), exitWith)
11 import System.IO (stderr, hPutStrLn)
12 import Text.Read (readMaybe)
13
14 import Cidr (
15 Cidr(..),
16 combine_all,
17 enumerate,
18 max_octet1,
19 max_octet2,
20 max_octet3,
21 max_octet4,
22 min_octet1,
23 min_octet2,
24 min_octet3,
25 min_octet4 )
26 import CommandLine (Args(..), get_args)
27 import DNS (Domain, PTRResult, lookup_ptrs)
28 import ExitCodes ( exit_invalid_cidr )
29 import Octet ()
30
31
32 -- | A regular expression that matches a non-address character.
33 non_addr_char :: String
34 non_addr_char = "[^\\.0-9]"
35
36
37 -- | Add non_addr_chars on either side of the given String. This
38 -- prevents (for example) the regex '127.0.0.1' from matching
39 -- '127.0.0.100'.
40 add_barriers :: String -> String
41 add_barriers x = non_addr_char ++ x ++ non_addr_char
42
43
44 -- | The magic happens here. We take a CIDR String as an argument, and
45 -- return the equivalent regular expression. We do this as follows:
46 --
47 -- 1. Compute the minimum possible value of each octet.
48 -- 2. Compute the maximum possible value of each octet.
49 -- 3. Generate a regex matching every value between those min and
50 -- max values.
51 -- 4. Join the regexes from step 3 with regexes matching periods.
52 -- 5. Stick an address boundary on either side of the result if
53 -- use_barriers is True.
54 --
55 cidr_to_regex :: Bool -> Cidr.Cidr -> String
56 cidr_to_regex use_barriers cidr =
57 let f = if use_barriers then add_barriers else id in
58 f (intercalate "\\." [range1, range2, range3, range4])
59 where
60 range1 = numeric_range min1 max1
61 range2 = numeric_range min2 max2
62 range3 = numeric_range min3 max3
63 range4 = numeric_range min4 max4
64 min1 = fromEnum (min_octet1 cidr)
65 min2 = fromEnum (min_octet2 cidr)
66 min3 = fromEnum (min_octet3 cidr)
67 min4 = fromEnum (min_octet4 cidr)
68 max1 = fromEnum (max_octet1 cidr)
69 max2 = fromEnum (max_octet2 cidr)
70 max3 = fromEnum (max_octet3 cidr)
71 max4 = fromEnum (max_octet4 cidr)
72
73
74
75 -- | Take a list of Strings, and return a regular expression matching
76 -- any of them.
77 alternate :: [String] -> String
78 alternate terms = "(" ++ (intercalate "|" terms) ++ ")"
79
80
81 -- | Take two Ints as parameters, and return a regex matching any
82 -- integer between them (inclusive).
83 --
84 -- IMPORTANT: we match from max to min so that if e.g. the last
85 -- octet is '255', we want '255' to match before '2' in the regex
86 -- (255|254|...|3|2|1) which does not happen if we use
87 -- (1|2|3|...|254|255).
88 --
89 numeric_range :: Int -> Int -> String
90 numeric_range x y =
91 alternate (map show $ reverse [lower..upper])
92 where
93 lower = minimum [x,y]
94 upper = maximum [x,y]
95
96
97 main :: IO ()
98 main = do
99 args <- get_args
100
101 -- This reads stdin.
102 input <- getContents
103
104 let cidr_strings = splitWs input
105 let cidrs = map readMaybe cidr_strings
106
107 when (any isNothing cidrs) $ do
108 hPutStrLn stderr "ERROR: not valid CIDR notation:"
109
110 -- Output the bad lines, safely.
111 let pairs = zip cidr_strings cidrs
112 let print_pair (x, Nothing) = hPutStrLn stderr (" * " ++ x)
113 print_pair (_, _) = return ()
114
115 mapM_ print_pair pairs
116 exitWith (ExitFailure exit_invalid_cidr)
117
118 -- Filter out only the valid ones.
119 let valid_cidrs = catMaybes cidrs
120
121 case args of
122 Regexed{} -> do
123 let cidrs' = combine_all valid_cidrs
124 let regexes = map (cidr_to_regex (barriers args)) cidrs'
125 putStrLn $ alternate regexes
126 Reduced{} ->
127 mapM_ print (combine_all valid_cidrs)
128 Duped{} ->
129 mapM_ print dupes
130 where
131 dupes = valid_cidrs \\ (combine_all valid_cidrs)
132 Diffed{} -> do
133 mapM_ putStrLn deletions
134 mapM_ putStrLn additions
135 where
136 dupes = valid_cidrs \\ (combine_all valid_cidrs)
137 deletions = map (\s -> '-' : (show s)) dupes
138 newcidrs = (combine_all valid_cidrs) \\ valid_cidrs
139 additions = map (\s -> '+' : (show s)) newcidrs
140 Listed{} -> do
141 let combined_cidrs = combine_all valid_cidrs
142 let addrs = concatMap enumerate combined_cidrs
143 mapM_ print addrs
144 Reversed{} -> do
145 let combined_cidrs = combine_all valid_cidrs
146 let addrs = concatMap enumerate combined_cidrs
147 let addr_bytestrings = map (BS.pack . show) addrs
148 ptrs <- lookup_ptrs addr_bytestrings
149 let pairs = zip addr_bytestrings ptrs
150 mapM_ (putStrLn . show_pair) pairs
151
152 stopGlobalPool
153
154 where
155 show_pair :: (Domain, PTRResult) -> String
156 show_pair (s, eds) =
157 (BS.unpack s) ++ ": " ++ results
158 where
159 space = BS.pack " "
160 results =
161 case eds of
162 Left err -> "ERROR (" ++ (show err) ++ ")"
163 Right ds -> BS.unpack $ BS.intercalate space ds