]> gitweb.michael.orlitzky.com - hath.git/blobdiff - src/DNS.hs
Bump version to 0.2.1.
[hath.git] / src / DNS.hs
index e1c4e51e5fa029e1bd367d6fb0e381143333057f..a535de59adeb17a75b3c52333504bc65303e4bee 100644 (file)
@@ -1,35 +1,24 @@
 -- | Helpers to perform DNS queries.
 module DNS (
   Domain,
-  lookup_ptrs
-  )
+  PTRResult,
+  lookup_ptrs )
 where
 
-import qualified  Data.ByteString.Char8 as BS (
-  append,
-  intercalate,
-  pack,
-  split )
+import Control.Concurrent.ParallelIO.Global ( parallel )
 import Network.DNS (
   Domain,
+  DNSError,
   ResolvConf(..),
   defaultResolvConf,
-  lookupPTR,
+  lookupRDNS,
   makeResolvSeed,
-  withResolver
-  )
+  withResolver )
 
 
--- | Convert the given IP address (as a ByteString) to the format
---   required for a PTR lookup. For example, "192.168.0.0" should be
---   converted to "0.0.168.192.in-addr.arpa".
-ip_to_in_addr_arpa :: Domain -> Domain
-ip_to_in_addr_arpa ip =
-  rev_ip `BS.append` suffix
-  where
-    dot = BS.pack "."
-    suffix = BS.pack ".in-addr.arpa"
-    rev_ip = BS.intercalate dot (reverse (BS.split '.' ip))
+-- The return type of lookupRDNS.
+type PTRResult = Either DNSError [Domain]
+
 
 -- | Take the default ResolvConf and increase the timeout to 15
 --   seconds.
@@ -40,10 +29,10 @@ our_resolv_conf =
 
 -- | Takes a list of IP addresses (as ByteStrings) and performs
 --   reverse (PTR) lookups on each of them.
-lookup_ptrs :: [Domain] -> IO [Maybe [Domain]]
+lookup_ptrs :: [Domain] -> IO [PTRResult]
 lookup_ptrs ips = do
   rs <- makeResolvSeed our_resolv_conf
-  withResolver rs $ \resolver ->
-    mapM (lookupPTR resolver) in_addrs
-  where
-    in_addrs = map ip_to_in_addr_arpa ips
+  let lookup' addr = withResolver rs $ \resolver ->
+                       lookupRDNS resolver addr
+
+  parallel $ map lookup' ips