module CommandLine (
Args(..),
- Delegates(..),
get_args
)
where
"IP address of server to query " ++
"(will use resolv.conf if not specified)"
-newtype Delegates =
- Delegates { get_delegates :: [String] }
- deriving (Data, Show, Typeable)
-
-instance Default Delegates where
- def = Delegates []
-
data Args =
- NS { server :: Maybe String, delegates :: Delegates } |
- MX { server :: Maybe String, delegates :: Delegates }
+ NS { server :: Maybe String, delegates :: [String] } |
+ MX { server :: Maybe String, delegates :: [String] }
deriving (Data, Show, Typeable)
arg_spec :: Args
--- /dev/null
+module DNS (
+ normalize
+ )
+where
+
+import qualified Data.ByteString.Char8 as BS (
+ append,
+ last,
+ map,
+ pack )
+import Data.Char (toLower)
+import Network.DNS.Types (Domain)
+
+-- | Normalize the given name by lowercasing and appending a trailing
+-- dot (the root) if necessary.
+normalize :: Domain -> Domain
+normalize = normalize_case . normalize_root
+
+
+normalize_root :: Domain -> Domain
+normalize_root d
+ | BS.last d == '.' = d
+ | otherwise = d `BS.append` trailing_dot
+ where
+ trailing_dot = BS.pack "."
+
+
+normalize_case :: Domain -> Domain
+normalize_case = BS.map toLower
module Main
where
-import qualified Data.ByteString.Char8 as BS (pack)
+import Control.Concurrent.ParallelIO.Global (
+ parallel,
+ stopGlobalPool )
+import qualified Data.ByteString.Char8 as BS (
+ getContents,
+ pack,
+ words )
import Data.List ((\\))
-import Data.String.Utils (join, splitWs)
+import Data.String.Utils (join)
import Network.DNS (
Domain,
defaultResolvConf,
lookupNS,
makeResolvSeed,
- withResolver
- )
+ withResolver )
-import System.IO (hGetContents, stdin)
-
-import CommandLine (Args(..), Delegates(..), get_args)
+import CommandLine (Args(..), get_args)
+import DNS (normalize)
report :: (Domain, Maybe [Domain]) -> IO ()
report (d, Nothing) =
" delegates somewhere else: " ++
(join " " (map show leftovers))
-clean :: Delegates -> (Domain, Maybe [Domain]) -> (Domain, Maybe [Domain])
+clean :: [Domain] -- ^ List of delegates, @ds@
+ -> (Domain, Maybe [Domain]) -- ^ Pairs of (domain name, lookup result)
+ -> (Domain, Maybe [Domain])
clean _ p@(_, Nothing) = p
-clean (Delegates ds') (d, Just targets) =
+clean ds (d, Just targets) =
(d, Just $ targets \\ ds)
- where
- ds = map BS.pack ds'
+
main :: IO ()
main = do
cfg <- get_args
- print cfg
- input <- hGetContents stdin
+
+ -- This reads stdin.
+ input <- BS.getContents
-- Split the input on any whitespace characters.
- let domains' = splitWs input
+ let raw_domains = BS.words input
+
+ -- Convert these to ByteStrings.
+ let raw_delegates = map BS.pack (delegates cfg)
- -- Convert those Strings to ByteStrings
- let domains = map BS.pack domains'
+ -- Normalize the given names and delegates
+ let nrml_domains = map normalize raw_domains
+ let nrml_delegates = map normalize raw_delegates
let rc = case (server cfg) of
Nothing -> defaultResolvConf
withResolver rs $ \resolver -> do
-- This function keeps the domain matches with its NS records.
let lookupNS' = \d -> (lookupNS resolver d) >>= (return . ((,) d))
- domains_ns <- mapM lookupNS' domains
- let cdns = map (clean (delegates cfg)) domains_ns
-
- case cfg of
- (NS _ _) ->
- -- We're only checking NS records, so report what we found.
- mapM_ report cdns
- (MX _ _) ->
- print "Hello, world."
+
+ -- Bad stuff happens if we try to run these lookups in parallel
+ -- instead of the reports.
+ domains_ns <- mapM lookupNS' nrml_domains
+ let cdns = map (clean nrml_delegates) domains_ns
+
+ _ <- case cfg of
+ (NS _ _) ->
+ -- We're only checking NS records, so report what we found.
+ parallel (map report cdns)
+ (MX _ _) ->
+ return [()]
+
+ stopGlobalPool