Ticket #4912: dns_ex.diff

File dns_ex.diff, 4.2 KB (added by bra, 6 years ago)

extended dns patch

  • internet/interfaces.py

     
    5656
    5757class IResolverSimple(Interface):
    5858
    59     def getHostByName(name, timeout = (1, 3, 11, 45)):
     59    def getHostByName(name, timeout = (1, 3, 11, 45), extended = False):
    6060        """
    6161        Resolve the domain name C{name} into an IP address.
    6262
    6363        @type name: C{str}
    6464        @type timeout: C{tuple}
     65        @type extended: C{bool}
    6566        @rtype: L{twisted.internet.defer.Deferred}
    6667        @return: The callback of the Deferred that is returned will be
    6768        passed a string that represents the IP address of the specified
     
    6970        multiple types of address records are associated with the name,
    7071        A6 records will be returned in preference to AAAA records, which
    7172        will be returned in preference to A records.  If there are multiple
    72         records of the type to be returned, one will be selected at random.
     73        records of the type to be returned, one will be selected at random,
     74        except when extended = True is specified, where the gethostbyname_ex
     75        Python function's return value is returned, which is a tuple of
     76        (hostname, aliaslist, ipaddrlist) where hostname is the primary host
     77        name responding to the given ip_address, aliaslist is a (possibly
     78        empty) list of alternative host names for the same address, and
     79        ipaddrlist is a list of IPv4 addresses for the same interface on the
     80        same host (often but not always a single address).
    7381
    7482        @raise twisted.internet.defer.TimeoutError: Raised (asynchronously)
    7583        if the name cannot be resolved within the specified timeout period.
  • internet/base.py

     
    250250                userDeferred.callback(result)
    251251
    252252
    253     def getHostByName(self, name, timeout = (1, 3, 11, 45)):
     253    def getHostByName(self, name, timeout = (1, 3, 11, 45), extended = False):
    254254        """
    255255        See L{twisted.internet.interfaces.IResolverSimple.getHostByName}.
    256256
     
    258258        as a timeout for the lookup.  Any intermediate timeout or retry logic
    259259        is left up to the platform via L{socket.gethostbyname}.
    260260        """
     261        if extended:
     262            _gethostbyname = socket.gethostbyname_ex
     263        else:
     264            _gethostbyname = socket.gethostbyname
    261265        if timeout:
    262266            timeoutDelay = sum(timeout)
    263267        else:
     
    265269        userDeferred = defer.Deferred()
    266270        lookupDeferred = threads.deferToThreadPool(
    267271            self.reactor, self.reactor.getThreadPool(),
    268             socket.gethostbyname, name)
     272            _gethostbyname, name)
    269273        cancelCall = self.reactor.callLater(
    270274            timeoutDelay, self._cleanup, name, lookupDeferred)
    271275        self._runningQueries[lookupDeferred] = (userDeferred, cancelCall)
     
    277281class BlockingResolver:
    278282    implements(IResolverSimple)
    279283
    280     def getHostByName(self, name, timeout = (1, 3, 11, 45)):
     284    def getHostByName(self, name, timeout = (1, 3, 11, 45), extended = False):
    281285        try:
    282             address = socket.gethostbyname(name)
     286            if extended:
     287                address = socket.gethostbyname_ex(name)
     288            else:
     289                address = socket.gethostbyname(name)
    283290        except socket.error:
    284291            msg = "address %r not found" % (name,)
    285292            err = error.DNSLookupError(msg)
     
    548555            reflect.qual(self.__class__) + " did not implement getWriters")
    549556
    550557
    551     def resolve(self, name, timeout = (1, 3, 11, 45)):
     558    def resolve(self, name, timeout = (1, 3, 11, 45), extended = False):
    552559        """Return a Deferred that will resolve a hostname.
    553560        """
    554561        if not name:
     
    556563            return defer.succeed('0.0.0.0')
    557564        if abstract.isIPAddress(name):
    558565            return defer.succeed(name)
    559         return self.resolver.getHostByName(name, timeout)
     566        return self.resolver.getHostByName(name, timeout, extended)
    560567
    561568    # Installation.
    562569