def _lookup(self, name, cls, type, timeout=None): """ Determine a response to a particular DNS query. @param name: The name which is being queried and for which to lookup a response. @type name: L{bytes} @param cls: The class which is being queried. Only I{IN} is implemented here and this value is presently disregarded. @type cls: L{int} @param type: The type of records being queried. See the types defined in L{twisted.names.dns}. @type type: L{int} @param timeout: All processing is done locally and a result is available immediately, so the timeout value is ignored. @return: A L{Deferred} that fires with a L{tuple} of three sets of response records (to comprise the I{answer}, I{authority}, and I{additional} sections of a DNS response) or with a L{Failure} if there is a problem processing the query. """ cnames = [] results = [] authority = [] additional = [] default_ttl = max(self.soa[1].minimum, self.soa[1].expire) domain_records = self.records.get(name.lower()) if domain_records: for record in domain_records: if record.ttl is not None: ttl = record.ttl else: ttl = default_ttl if (record.TYPE == dns.NS and name.lower() != self.soa[0].lower()): # NS record belong to a child zone: this is a referral. As # NS records are authoritative in the child zone, ours here # are not. RFC 2181, section 6.1. authority.append( dns.RRHeader( name, record.TYPE, dns.IN, ttl, record, auth=False ) ) elif record.TYPE == type or type == dns.ALL_RECORDS: results.append( dns.RRHeader( name, record.TYPE, dns.IN, ttl, record, auth=True ) ) if record.TYPE == dns.CNAME: cnames.append( dns.RRHeader( name, record.TYPE, dns.IN, ttl, record, auth=True ) ) if not results: results = cnames # Sort of https://tools.ietf.org/html/rfc1034#section-4.3.2 . # See https://twistedmatrix.com/trac/ticket/6732 additionalInformation = self._additionalRecords( results, authority, default_ttl) if cnames: results.extend(additionalInformation) else: additional.extend(additionalInformation) if not results and not authority: # Empty response. Include SOA record to allow clients to cache # this response. RFC 1034, sections 3.7 and 4.3.4, and RFC 2181 # section 7.1. authority.append( dns.RRHeader( self.soa[0], dns.SOA, dns.IN, ttl, self.soa[1], auth=True ) ) return defer.succeed((results, authority, additional)) else: if dns._isSubdomainOf(name, self.soa[0]): # We may be the authority and we didn't find it. # XXX: The QNAME may also be in a delegated child zone. See # #6581 and #6580 return defer.fail( failure.Failure(dns.AuthoritativeDomainError(name)) ) else: # The QNAME is not a descendant of this zone. Fail with # DomainError so that the next chained authority or # resolver will be queried. return defer.fail(failure.Failure(error.DomainError(name)))
def _lookup(self, name, cls, type, timeout = None): cnames = [] results = [] authority = [] additional = [] default_ttl = max(self.soa[1].minimum, self.soa[1].expire) domain_records = self.records.get(name.lower()) if domain_records: for record in domain_records: if record.ttl is not None: ttl = record.ttl else: ttl = default_ttl if record.TYPE == dns.NS and name.lower() != self.soa[0].lower(): # NS record belong to a child zone: this is a referral. As # NS records are authoritative in the child zone, ours here # are not. RFC 2181, section 6.1. authority.append( dns.RRHeader(name, record.TYPE, dns.IN, ttl, record, auth=False) ) elif record.TYPE == type or type == dns.ALL_RECORDS: results.append( dns.RRHeader(name, record.TYPE, dns.IN, ttl, record, auth=True) ) if record.TYPE == dns.CNAME: cnames.append( dns.RRHeader(name, record.TYPE, dns.IN, ttl, record, auth=True) ) if not results: results = cnames for record in results + authority: section = {dns.NS: additional, dns.CNAME: results, dns.MX: additional}.get(record.type) if section is not None: n = str(record.payload.name) for rec in self.records.get(n.lower(), ()): if rec.TYPE == dns.A: section.append( dns.RRHeader(n, dns.A, dns.IN, rec.ttl or default_ttl, rec, auth=True) ) if not results and not authority: # Empty response. Include SOA record to allow clients to cache # this response. RFC 1034, sections 3.7 and 4.3.4, and RFC 2181 # section 7.1. authority.append( dns.RRHeader(self.soa[0], dns.SOA, dns.IN, ttl, self.soa[1], auth=True) ) return defer.succeed((results, authority, additional)) else: if dns._isSubdomainOf(name, self.soa[0]): # We may be the authority and we didn't find it. # XXX: The QNAME may also be a in a delegated child zone. See # #6581 and #6580 return defer.fail(failure.Failure(dns.AuthoritativeDomainError(name))) else: # The QNAME is not a descendant of this zone. Fail with # DomainError so that the next chained authority or # resolver will be queried. return defer.fail(failure.Failure(error.DomainError(name)))