From c2c0bcbeea3f3cb6fc72ab35e4ed3d3384fece83 Mon Sep 17 00:00:00 2001 From: TW Date: Tue, 9 Apr 2019 21:16:48 +0200 Subject: [PATCH] trying to fix #399 (#421) rev_lookup: try up to 5 times, more selective error handler, fixes #399 --- src/nsupdate/main/dnstools.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/nsupdate/main/dnstools.py b/src/nsupdate/main/dnstools.py index f12e4d3..431add9 100644 --- a/src/nsupdate/main/dnstools.py +++ b/src/nsupdate/main/dnstools.py @@ -17,6 +17,7 @@ UNAVAILABLE_RETRY = 120.0 import binascii +import errno import time from datetime import timedelta from collections import namedtuple @@ -276,13 +277,24 @@ def rev_lookup(ipaddr): :param ipaddr: ip address (str) :return: hostname (or empty string if lookup failed) """ - name = '' if ipaddr: - try: - name = socket.gethostbyaddr(ipaddr)[0] - except socket.error: - pass - return name + retries = 4 + delay = 0.02 + while retries >= 0: + retries -= 1 + try: + return socket.gethostbyaddr(ipaddr)[0] + except socket.error as err: + if err.errno in (errno.EPERM, ): + # EPERM == 1 == unknown host + break + if err.errno not in (errno.ENOENT, errno.EAGAIN): + # ENOENT == 2 == UDP Packet lost? + # EAGAIN == "try again" + raise + time.sleep(delay) + delay *= 2 + return '' def get_ns_info(fqdn):