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),
21 import System.Exit (ExitCode(..), exitWith)
22 import System.IO (hPutStrLn, stderr)
25 import CommandLine (Args(..), get_args)
33 import ExitCodes (exit_bad_server)
36 -- | Report results for this LookupResult. If there's a Nothing in the
37 -- second component, there were no query results, so we report that
38 -- the domain was not delegated. If there were some results and
39 -- there are leftovers (after "cleaning"), we report those as well.
40 report :: LookupResult -> IO ()
42 putStrLn $ "Domain " ++ (show d) ++ " not delegated."
43 report (d, Just leftovers) =
44 unless (null leftovers) $
45 putStrLn $ "Domain " ++
47 " delegates somewhere else: " ++
48 (join " " (map show leftovers))
51 -- | "Clean" a lookup result by subtracting out the list of delegates.
52 -- There shouldn't be anything left over. If there were no lookup
53 -- results, we leave the Nothing in place so that 'report' can
54 -- pattern match on it.
55 clean :: [Domain] -- ^ List of delegates, @ds@
58 clean _ p@(_, Nothing) = p
59 clean delgts (d, Just targets) =
60 (d, Just $ targets \\ delgts)
69 input <- BS.getContents
71 -- Split the input on any whitespace characters.
72 let raw_domains = BS.words input
74 -- Convert these to ByteStrings.
75 let raw_delegates = map BS.pack (delegates cfg)
77 let normalize_function =
78 if (no_append_root cfg)
82 -- Normalize the given names and delegates
83 let nrml_domains = map normalize_function raw_domains
84 let nrml_delegates = map normalize_function raw_delegates
86 rc <- case (server cfg) of
87 Nothing -> return defaultResolvConf
89 s' <- resolve_address s
92 hPutStrLn stderr ("Bad DNS server or lookup error: " ++ s)
93 exitWith (ExitFailure exit_bad_server)
95 return $ defaultResolvConf { resolvInfo =
96 RCHostName (show s'') }
98 rs <- makeResolvSeed rc
100 let lookup_function = case cfg of
104 _ <- withResolver rs $ \resolver -> do
105 -- Bad stuff happens if we try to run these lookups in parallel
106 -- instead of the reports.
107 records <- mapM (lookup_function resolver) nrml_domains
108 let cleaned_records = map (clean nrml_delegates) records
109 parallel (map report cleaned_records)