X-Git-Url: http://gitweb.michael.orlitzky.com/?p=haeredes.git;a=blobdiff_plain;f=src%2FMain.hs;h=d425734c275404e24310edb650ac33c9b4fe0b67;hp=142ace670cf62d4a83796c431185d5af2eeb891f;hb=70dffd6160208adf2c1c5746924c100cb0ffcb2e;hpb=825753e2731b8491d93d9f2d66b457fe31b1c763 diff --git a/src/Main.hs b/src/Main.hs index 142ace6..d425734 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -1,9 +1,15 @@ 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, @@ -12,12 +18,10 @@ import Network.DNS ( 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) = @@ -30,24 +34,30 @@ report (d, Just leftovers) = " 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 @@ -57,12 +67,17 @@ main = do 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