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 -- Before processing, all names are normalized using the supplied
46 -- function @normalize_function@. Ideally this should be the same
47 -- function applied to the user-input names.
49 report :: [Domain] -- ^ The list of @delgts@
50 -> (Domain -> Domain) -- ^ Domain name normalization function,
51 -- @normalize_function@.
55 -- If the lookup resulted in a DNS error, we just ignore the whole
57 report _ _ (_, Left _) = return ()
59 -- If the lookup succeeded but there were no results, report that the
60 -- domain is not delegated.
61 report _ _ (d, Right []) =
62 putStrLn $ "Domain " ++ (show d) ++ " not delegated."
64 -- Otherwise, subtract our delegates from the list of results and
65 -- report the leftovers.
66 report delgts normalize_function (d, Right raw_hosts) = do
67 let nrml_hosts = map normalize_function raw_hosts
68 let leftovers = nrml_hosts \\ delgts
69 unless (null leftovers) $
70 putStrLn $ "Domain " ++
72 " delegates somewhere else: " ++
73 (join " " (map show leftovers))
81 input <- BS.getContents
83 -- Split the input on any whitespace characters.
84 let raw_domains = BS.words input
86 -- Convert these to ByteStrings.
87 let raw_delegates = map BS.pack (delegates cfg)
89 let normalize_function =
90 if (no_append_root cfg)
94 -- Normalize the given names and delegates
95 let nrml_domains = map normalize_function raw_domains
96 let nrml_delegates = map normalize_function raw_delegates
98 rc' <- case (server cfg) of
99 Nothing -> return defaultResolvConf
101 s' <- resolve_address s
104 let errmsg = show err
105 hPutStrLn stderr ("Bad DNS server or lookup error: " ++ errmsg)
106 exitWith (ExitFailure exit_bad_server)
108 hPutStrLn stderr ("Hostname " ++ s ++ " has no 'A' records.")
109 exitWith (ExitFailure exit_bad_server)
111 return $ defaultResolvConf { resolvInfo =
112 RCHostName (show srv) }
114 -- Set the timeout from the command line. The resolvTimeout field is
115 -- in microseconds, so we multiply by one million.
116 let a_million = 1000 * 1000 :: Int
117 let rc = rc' { resolvTimeout = a_million * seconds (timeout cfg) }
118 rs <- makeResolvSeed rc
120 let lookup_function = case cfg of
124 let lookup' d = withResolver rs $ \resolver ->
125 lookup_function resolver d
127 -- Construct a list of [IO whatever]. The withResolver calls
128 -- are the ones that should be run in parallel.
129 let actions = map lookup' nrml_domains
131 -- Run the lookup actions in parallel.
132 results <- parallelInterleaved actions
134 -- Output the results.
135 _ <- mapM (report nrml_delegates normalize_function) results