4 import Control.Concurrent.ParallelIO.Global (
7 import Control.Monad (unless)
8 import qualified Data.ByteString.Char8 as BS (
12 import Data.List ((\\))
13 import Data.String.Utils (join)
16 FileOrNumericHost(RCHostName),
17 ResolvConf(resolvInfo, resolvTimeout),
21 import System.Exit (ExitCode(..), exitWith)
22 import System.IO (hPutStrLn, stderr)
25 import CommandLine (Args(..), get_args)
33 import ExitCodes (exit_bad_server)
34 import Timeout (Timeout(..))
37 -- | Report results for this LookupResult. If there's a Nothing in the
38 -- second component, there were no query results, so we report that
39 -- the domain was not delegated. If there were some results and
40 -- there are leftovers (after "cleaning"), we report those as well.
41 report :: LookupResult -> IO ()
43 putStrLn $ "Domain " ++ (show d) ++ " not delegated."
44 report (d, Just leftovers) =
45 unless (null leftovers) $
46 putStrLn $ "Domain " ++
48 " delegates somewhere else: " ++
49 (join " " (map show leftovers))
52 -- | "Clean" a lookup result by subtracting out the list of delegates.
53 -- There shouldn't be anything left over. If there were no lookup
54 -- results, we leave the Nothing in place so that 'report' can
55 -- pattern match on it.
56 clean :: [Domain] -- ^ List of delegates, @ds@
59 clean _ p@(_, Nothing) = p
60 clean delgts (d, Just targets) =
61 (d, Just $ targets \\ delgts)
70 input <- BS.getContents
72 -- Split the input on any whitespace characters.
73 let raw_domains = BS.words input
75 -- Convert these to ByteStrings.
76 let raw_delegates = map BS.pack (delegates cfg)
78 let normalize_function =
79 if (no_append_root cfg)
83 -- Normalize the given names and delegates
84 let nrml_domains = map normalize_function raw_domains
85 let nrml_delegates = map normalize_function raw_delegates
87 rc' <- case (server cfg) of
88 Nothing -> return defaultResolvConf
90 s' <- resolve_address s
93 hPutStrLn stderr ("Bad DNS server or lookup error: " ++ s)
94 exitWith (ExitFailure exit_bad_server)
96 return $ defaultResolvConf { resolvInfo =
97 RCHostName (show s'') }
99 -- Set the timeout from the command line. The resolvTimeout field is
100 -- in microseconds, so we multiply by one million.
101 let rc = rc' { resolvTimeout = 1000 * 1000 * (seconds $ timeout cfg) }
102 rs <- makeResolvSeed rc
104 let lookup_function = case cfg of
108 _ <- withResolver rs $ \resolver -> do
109 -- Bad stuff happens if we try to run these lookups in parallel
110 -- instead of the reports.
111 records <- mapM (lookup_function resolver) nrml_domains
112 let cleaned_records = map (clean nrml_delegates) records
113 parallel (map report cleaned_records)