resolve.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # -*- test-case-name: twisted.names.test.test_resolve -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Lookup a name using multiple resolvers.
  6. Future Plans: This needs someway to specify which resolver answered
  7. the query, or someway to specify (authority|ttl|cache behavior|more?)
  8. """
  9. from __future__ import division, absolute_import
  10. from zope.interface import implementer
  11. from twisted.internet import defer, interfaces
  12. from twisted.names import dns, common, error
  13. class FailureHandler:
  14. def __init__(self, resolver, query, timeout):
  15. self.resolver = resolver
  16. self.query = query
  17. self.timeout = timeout
  18. def __call__(self, failure):
  19. # AuthoritativeDomainErrors should halt resolution attempts
  20. failure.trap(dns.DomainError, defer.TimeoutError, NotImplementedError)
  21. return self.resolver(self.query, self.timeout)
  22. @implementer(interfaces.IResolver)
  23. class ResolverChain(common.ResolverBase):
  24. """
  25. Lookup an address using multiple L{IResolver}s
  26. """
  27. def __init__(self, resolvers):
  28. """
  29. @type resolvers: L{list}
  30. @param resolvers: A L{list} of L{IResolver} providers.
  31. """
  32. common.ResolverBase.__init__(self)
  33. self.resolvers = resolvers
  34. def _lookup(self, name, cls, type, timeout):
  35. """
  36. Build a L{dns.Query} for the given parameters and dispatch it
  37. to each L{IResolver} in C{self.resolvers} until an answer or
  38. L{error.AuthoritativeDomainError} is returned.
  39. @type name: C{str}
  40. @param name: DNS name to resolve.
  41. @type type: C{int}
  42. @param type: DNS record type.
  43. @type cls: C{int}
  44. @param cls: DNS record class.
  45. @type timeout: Sequence of C{int}
  46. @param timeout: Number of seconds after which to reissue the query.
  47. When the last timeout expires, the query is considered failed.
  48. @rtype: L{Deferred}
  49. @return: A L{Deferred} which fires with a three-tuple of lists of
  50. L{twisted.names.dns.RRHeader} instances. The first element of the
  51. tuple gives answers. The second element of the tuple gives
  52. authorities. The third element of the tuple gives additional
  53. information. The L{Deferred} may instead fail with one of the
  54. exceptions defined in L{twisted.names.error} or with
  55. C{NotImplementedError}.
  56. """
  57. if not self.resolvers:
  58. return defer.fail(error.DomainError())
  59. q = dns.Query(name, type, cls)
  60. d = self.resolvers[0].query(q, timeout)
  61. for r in self.resolvers[1:]:
  62. d = d.addErrback(
  63. FailureHandler(r.query, q, timeout)
  64. )
  65. return d
  66. def lookupAllRecords(self, name, timeout=None):
  67. # XXX: Why is this necessary? dns.ALL_RECORDS queries should
  68. # be handled just the same as any other type by _lookup
  69. # above. If I remove this method all names tests still
  70. # pass. See #6604 -rwall
  71. if not self.resolvers:
  72. return defer.fail(error.DomainError())
  73. d = self.resolvers[0].lookupAllRecords(name, timeout)
  74. for r in self.resolvers[1:]:
  75. d = d.addErrback(
  76. FailureHandler(r.lookupAllRecords, name, timeout)
  77. )
  78. return d