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),
23 import System.Exit (ExitCode(..), exitWith)
24 import System.IO (hPutStrLn, stderr)
27 import CommandLine (Args(..), get_args)
33 import ExitCodes (exit_bad_server)
34 import Timeout (Timeout(..))
37 -- | Given a list of delegates, report results for this
40 -- If there's an empty list in the second component, there were no
41 -- query results, so we report that the domain was not delegated. If
42 -- there were some results and there are leftovers (after removing
43 -- the delegates), we report those as well.
45 report :: [Domain] -- ^ The list of @delgts@
49 -- If the lookup resulted in a DNS error, we just ignore the whole
51 report _ (_, Left _) = return ()
53 -- If the lookup succeeded but there were no results, report that the
54 -- domain is not delegated.
55 report _ (d, Right []) =
56 putStrLn $ "Domain " ++ (show d) ++ " not delegated."
58 -- Otherwise, subtract our delegates from the list of results and
59 -- report the leftovers.
60 report delgts (d, Right hosts) = do
61 let leftovers = hosts \\ delgts
62 unless (null leftovers) $
63 putStrLn $ "Domain " ++
65 " delegates somewhere else: " ++
66 (join " " (map show leftovers))
74 input <- BS.getContents
76 -- Split the input on any whitespace characters.
77 let raw_domains = BS.words input
79 -- Convert these to ByteStrings.
80 let raw_delegates = map BS.pack (delegates cfg)
82 let normalize_function =
83 if (no_append_root cfg)
87 -- Normalize the given names and delegates
88 let nrml_domains = map normalize_function raw_domains
89 let nrml_delegates = map normalize_function raw_delegates
91 rc' <- case (server cfg) of
92 Nothing -> return defaultResolvConf
94 s' <- resolve_address s
98 hPutStrLn stderr ("Bad DNS server or lookup error: " ++ errmsg)
99 exitWith (ExitFailure exit_bad_server)
101 hPutStrLn stderr ("Hostname " ++ s ++ " has no 'A' records.")
102 exitWith (ExitFailure exit_bad_server)
104 return $ defaultResolvConf { resolvInfo =
105 RCHostName (show srv) }
107 -- Set the timeout from the command line. The resolvTimeout field is
108 -- in microseconds, so we multiply by one million.
109 let rc = rc' { resolvTimeout = 1000 * 1000 * seconds (timeout cfg) }
110 rs <- makeResolvSeed rc
112 let lookup_function = case cfg of
116 let lookup' d = withResolver rs $ \resolver ->
117 lookup_function resolver d
119 -- Construct a list of [IO whatever]. The withResolver calls
120 -- are the ones that should be run in parallel.
121 let actions = map lookup' nrml_domains
123 -- Run the lookup actions in parallel.
124 results <- parallelInterleaved actions
126 -- Output the results.
127 _ <- mapM (report nrml_delegates) results