]> www.infradead.org Git - users/dhowells/kafs-utils.git/commitdiff
Use gethostbyname_ex() and gethostbyaddr() rather than the DNS resolver
authorDavid Howells <dhowells@redhat.com>
Sat, 12 Apr 2014 13:33:46 +0000 (14:33 +0100)
committerDavid Howells <dhowells@redhat.com>
Sat, 12 Apr 2014 16:49:55 +0000 (17:49 +0100)
Use gethostbyname_ex() and gethostbyaddr() for A and PTR record retrieval
rather than using the DNS resolver directly so that /etc/hosts and suchlike
are also used.

Signed-off-by: David Howells <dhowells@redhat.com>
suite/lib/addrcache.py

index 926b38baa02cf0898aacb5025e96cf808dc89672..95b7bb4cc53f306b2135fddbb7a0b8e4067c8523 100644 (file)
@@ -22,9 +22,8 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 """
 
 from afs import exception
-from socket import inet_pton, inet_ntoa, AF_INET, AF_INET6;
-import dns.reversename
-import dns.resolver
+from socket import gethostbyname_ex, gethostbyaddr, gaierror, herror, inet_pton, inet_ntoa, AF_INET, AF_INET6
+import sys
 
 cache_n2a = dict()
 cache_a2n = dict()
@@ -40,6 +39,17 @@ def add(name, addr):
         cache_n2a[name].append(addr)
     cache_a2n[addr] = name
 
+def add_ghb(name, addr, result):
+    hostname, aliaslist, ipaddrlist = result
+    if addr and addr not in ipaddrlist:
+        print("Inconsistent lookup of '", addr, "'", file=sys.stderr)
+    for i in ipaddrlist:
+        if name:
+            add(name, i)
+        add(hostname, i)
+        for j in aliaslist:
+            add(j, i)
+
 ###############################################################################
 #
 # Convert a name to an array of addresses in string form (eg. "1.2.3.4").
@@ -65,10 +75,10 @@ def name2addrs(name):
         return cache_n2a[name]
 
     try:
-        for A in dns.resolver.query(name, 'A'):
-            add(name, A.address)
-    except dns.resolver.NoAnswer:
-        pass
+        result = gethostbyname_ex(name)
+    except gaierror as e:
+        raise NetAddressError("Couldn't resolve '" + name + "'")
+    add_ghb(name, None, result)
 
     return cache_n2a[name]
 
@@ -143,12 +153,10 @@ def addrs2name(addrs):
 
     for addr in addrs:
         try:
-            revname = dns.reversename.from_address(addr)
-            for PTR in dns.resolver.query(revname, 'PTR'):
-                name = str(PTR).lower().rstrip(".")
-                add(name, addr)
-                return name
-        except (dns.resolver.NoAnswer, dns.resolver.NXDOMAIN):
+            result = gethostbyaddr(addr)
+            add_ghb(None, addr, result)
+            return cache_a2n[addr]
+        except herror:
             pass
 
     return None