X-Git-Url: http://gitweb.michael.orlitzky.com/?p=haeredes.git;a=blobdiff_plain;f=src%2FDNS.hs;h=34a8bd18a6f17a06abc4bcaecf55169b77f38263;hp=5222cbec3f3a54c1d8b334bb52b615e56b1cc2e2;hb=689bda2499ec821772b456cb7982527d6740649f;hpb=fa4220513eb9d2036c3db0cc9c518c6346050318 diff --git a/src/DNS.hs b/src/DNS.hs index 5222cbe..34a8bd1 100644 --- a/src/DNS.hs +++ b/src/DNS.hs @@ -1,56 +1,190 @@ module DNS ( LookupResult, + dns_properties, + dns_tests, lookupMX', lookupNS', - normalize - ) + normalize, + normalize_case, + resolve_address ) where +import Control.Monad (liftM) import qualified Data.ByteString.Char8 as BS ( append, last, map, + null, pack ) import Data.Char (toLower) +import Data.IP (IPv4) import Network.DNS ( Domain, + DNSError, Resolver, + defaultResolvConf, + lookupA, lookupMX, - lookupNS ) + lookupNS, + lookupNSAuth, + makeResolvSeed, + withResolver ) +import Test.Framework (Test, testGroup) +import Test.Framework.Providers.HUnit (testCase) +import Test.Framework.Providers.QuickCheck2 (testProperty) +import Test.HUnit (assertEqual) +import Text.Read (readMaybe) -type LookupResult = (Domain, Maybe [Domain]) +type LookupResult = (Domain, Either DNSError [Domain]) +-- | Takes a String representing either a hostname or an IP +-- address. If a hostname was supplied, it is resolved to either an +-- [IPv4] or an error. If an IP address is supplied, it is returned +-- as a singleton [IPv4]. +-- +-- Examples: +-- +-- >>> resolve_address "example.com" +-- Right [93.184.216.119] +-- >>> resolve_address "93.184.216.119" +-- Right [93.184.216.119] +-- +resolve_address :: String -> IO (Either DNSError [IPv4]) +resolve_address s = + case read_result of + Just addr -> return $ Right [addr] + Nothing -> do + default_rs <- makeResolvSeed defaultResolvConf + withResolver default_rs $ \resolver -> + lookupA resolver (BS.pack s) + where + read_result :: Maybe IPv4 + read_result = readMaybe s + + +-- | This calls lookupMX, and returns the result as the second +-- component of a tuple whose first component is the domain name +-- itself. +-- +-- Examples: +-- +-- The example domain, example.com, has no MX record. +-- +-- >>> rs <- makeResolvSeed defaultResolvConf +-- >>> let domain = BS.pack "example.com." +-- >>> withResolver rs $ \resolver -> lookupMX' resolver domain +-- ("example.com.",Right []) +-- lookupMX' :: Resolver -> Domain -> IO LookupResult lookupMX' resolver domain = - lookupMX resolver domain >>= return . pair_em . drop_priority + liftM (pair_em . drop_priority) $ lookupMX resolver domain where - drop_priority :: Maybe [(Domain, Int)] -> Maybe [Domain] + drop_priority :: Either DNSError [(Domain, Int)] + -> Either DNSError [Domain] drop_priority = fmap (map fst) pair_em :: a -> (Domain, a) pair_em = (,) domain --- This function keeps the domain matches with its NS records. + +-- | This calls lookupNS, and returns the result as the second +-- component of a tuple whose first component is the domain name +-- itself. +-- +-- Examples: +-- +-- The example domain, example.com, does have NS records, but the +-- order in which they are returned is variable, so we have to sort +-- them to get a reliable result. +-- +-- >>> import Data.List (sort) +-- >>> import Control.Applicative ((<$>)) +-- >>> +-- >>> let sort_snd (x,y) = (x, sort <$> y) +-- >>> rs <- makeResolvSeed defaultResolvConf +-- >>> let domain = BS.pack "example.com." +-- >>> withResolver rs $ \resolver -> sort_snd <$> lookupNS' resolver domain +-- ("example.com.",Right ["a.iana-servers.net.","b.iana-servers.net."]) +-- lookupNS' :: Resolver -> Domain -> IO LookupResult -lookupNS' resolver domain = - lookupNS resolver domain >>= return . pair_em +lookupNS' resolver domain = do + answer_result <- lookupNS resolver domain + auth_result <- lookupNSAuth resolver domain + liftM pair_em $ return $ combine answer_result auth_result where pair_em :: a -> (Domain, a) pair_em = (,) domain --- | Normalize the given name by lowercasing and appending a trailing --- dot (the root) if necessary. + combine :: (Either DNSError [Domain]) + -> (Either DNSError [Domain]) + -> (Either DNSError [Domain]) + combine e1 e2 = do + l1 <- e1 + l2 <- e2 + return (l1 ++ l2) + +-- | Perform both normalize_case and normalize_root. normalize :: Domain -> Domain normalize = normalize_case . normalize_root - +-- | Normalize the given name by appending a trailing dot (the DNS +-- root) if necessary. normalize_root :: Domain -> Domain normalize_root d + | BS.null d = BS.pack "." | BS.last d == '.' = d | otherwise = d `BS.append` trailing_dot where trailing_dot = BS.pack "." +-- | Normalize the given name by lowercasing it. normalize_case :: Domain -> Domain normalize_case = BS.map toLower + + +test_normalize_case :: Test +test_normalize_case = + testCase desc $ + assertEqual desc expected actual + where + desc = "normalize_case lowercases DNS names" + expected = BS.pack "example.com" + actual = normalize_case $ BS.pack "ExAmPlE.COM" + +prop_normalize_case_idempotent :: String -> Bool +prop_normalize_case_idempotent s = + (normalize_case . normalize_case) bs == normalize_case bs + where + bs = BS.pack s + +test_normalize_root_adds_dot :: Test +test_normalize_root_adds_dot = + testCase desc $ + assertEqual desc expected actual + where + desc = "normalize_root adds a trailing dot" + expected = BS.pack "example.com." + actual = normalize_root $ BS.pack "example.com" + +prop_normalize_root_idempotent :: String -> Bool +prop_normalize_root_idempotent s = + (normalize_root . normalize_root) bs == normalize_root bs + where + bs = BS.pack s + +dns_tests :: Test +dns_tests = + testGroup "DNS Tests" [ + test_normalize_case, + test_normalize_root_adds_dot ] + +dns_properties :: Test +dns_properties = + testGroup "DNS Properties" [ + testProperty + "normalize_case is idempotent" + prop_normalize_case_idempotent, + testProperty + "normalize_root is idempotent" + prop_normalize_root_idempotent ]