def get_query_answer(self, name, rdtype): ''' return - rdataset from query answer ''' origin = dns.name.Name(self.zonename.split('.')) qname = name.derelativize(origin) #print('name={}, type={} to NS={}'.format(qname.to_text(), dns.rdatatype.to_text(rdtype), self.resolver.nameservers) ) if rdtype == dns.rdatatype.NS: pass #ans = self.resolver.query(qname, rdtype, raise_on_no_answer=False) try: ans = self.resolver.query(qname, rdtype) except Exception as err: if rdtype == dns.rdatatype.NS: ans = self.resolver.query(qname, rdtype, raise_on_no_answer=False) return ans.response.authority[0] elif rdtype == dns.rdatatype.A: ans = self.resolver.query(qname, rdtype, raise_on_no_answer=False) for ad in ans.response.additional: if ad.name == qname: #print('{} vs {}'.format(qname, ad.name)) return ad raise Exception raise Exception return ans.rrset
def _rrsig_labels(name, origin): """ Get label count of the given dns name as required for RRSIG labels field. See RFC-4034, section 3.1.3. for details. """ labels = [x for x in name.derelativize(origin).labels if len(x) and x != '*'] return len(labels)
def rdata_to_text(self, origin, name, rdataset, rdata): 'a text formatter like "www.myexample.com 1800 IN A 10.20.30.60"' name_txt = name.derelativize(origin).to_text() ttl = rdataset.ttl cls_txt = dns.rdataclass.to_text(rdataset.rdclass) rtype_txt = dns.rdatatype.to_text(rdataset.rdtype) record_txt = rdata.to_text() return '{} {} {} {} {}'.format(name_txt, ttl, cls_txt, rtype_txt, record_txt)
def echo_zone(self): ''' for debug use ''' print('Zone: {}'.format(self.zone.origin)) print('===============') for name, node in self.zone.nodes.items(): for rdataset in node.rdatasets: for d in rdataset: print('Name={}, d={}'.format( name.derelativize(self.zone.origin), d))
def _validate_name(self, name: dns.name.Name) -> dns.name.Name: if name.is_absolute(): if self.origin is None: # This should probably never happen as other code (e.g. # _rr_line) will notice the lack of an origin before us, but # we check just in case! raise KeyError("no zone origin is defined") if not name.is_subdomain(self.origin): raise KeyError("name is not a subdomain of the zone origin") if self.zone.relativize: name = name.relativize(self.origin) elif not self.zone.relativize: # We have a relative name in a non-relative zone, so derelativize. if self.origin is None: raise KeyError("no zone origin is defined") name = name.derelativize(self.origin) return name
def _validate_name(self, name): if isinstance(name, str): name = dns.name.from_text(name, None) elif not isinstance(name, dns.name.Name): raise KeyError("name parameter must be convertible to a DNS name") if name.is_absolute(): if not name.is_subdomain(self.origin): raise KeyError( "name parameter must be a subdomain of the zone origin") if self.relativize: name = name.relativize(self.origin) elif not self.relativize: # We have a relative name in a non-relative zone, so derelativize. if self.origin is None: raise KeyError('no zone origin is defined') name = name.derelativize(self.origin) return name
def _validate_name(self, name: Union[dns.name.Name, str]) -> dns.name.Name: if isinstance(name, str): name = dns.name.from_text(name, None) elif not isinstance(name, dns.name.Name): raise KeyError("name parameter must be convertible to a DNS name") if name.is_absolute(): if self.origin is None: # This should probably never happen as other code (e.g. # _rr_line) will notice the lack of an origin before us, but # we check just in case! raise KeyError("no zone origin is defined") if not name.is_subdomain(self.origin): raise KeyError( "name parameter must be a subdomain of the zone origin") if self.relativize: name = name.relativize(self.origin) elif not self.relativize: # We have a relative name in a non-relative zone, so derelativize. if self.origin is None: raise KeyError("no zone origin is defined") name = name.derelativize(self.origin) return name