diff options
author | Bob Halley <halley@nominum.com> | 2009-06-19 11:57:39 +0100 |
---|---|---|
committer | Bob Halley <halley@nominum.com> | 2009-06-19 11:57:39 +0100 |
commit | f6a2d3d27d0e093458a4f1eeff63f2493cd60a97 (patch) | |
tree | 0866a7ace589fafa0dcf71e7c22981ce55d1a2f7 /dns/rdata.py | |
parent | 066101a9d9e27d01c23850fff4720906322aa9ab (diff) | |
download | dnspython-f6a2d3d27d0e093458a4f1eeff63f2493cd60a97.tar.gz |
Add to_digestable() methods to rdata classes
Diffstat (limited to 'dns/rdata.py')
-rw-r--r-- | dns/rdata.py | 61 |
1 files changed, 35 insertions, 26 deletions
diff --git a/dns/rdata.py b/dns/rdata.py index e51faa3..f371fa7 100644 --- a/dns/rdata.py +++ b/dns/rdata.py @@ -25,6 +25,8 @@ default is 'dns.rdtypes'. Changing this value will break the library. chunk of hexstring that _hexify() produces before whitespace occurs. @type _hex_chunk: int""" +import cStringIO + import dns.exception import dns.rdataclass import dns.rdatatype @@ -41,7 +43,7 @@ def _hexify(data, chunksize=None): @param chunksize: the chunk size. Default is L{dns.rdata._hex_chunksize} @rtype: string """ - + if chunksize is None: chunksize = _hex_chunksize hex = data.encode('hex_codec') @@ -95,7 +97,7 @@ def _escapify(qstring): @returns: the escaped string @rtype: string """ - + text = '' for c in qstring: if c in __escaped: @@ -114,7 +116,7 @@ def _truncate_bitmap(what): @type what: string @rtype: string """ - + for i in xrange(len(what) - 1, -1, -1): if what[i] != '\x00': break @@ -125,7 +127,7 @@ class Rdata(object): """ __slots__ = ['rdclass', 'rdtype'] - + def __init__(self, rdclass, rdtype): """Initialize an rdata. @param rdclass: The rdata class @@ -134,8 +136,8 @@ class Rdata(object): @type rdtype: int """ - self.rdclass = rdclass - self.rdtype = rdtype + self.rdclass = rdclass + self.rdtype = rdtype def covers(self): """DNS SIG/RRSIG rdatas apply to a specific type; this type is @@ -145,8 +147,8 @@ class Rdata(object): of a particular type, e.g. RRSIG(NS). @rtype: int """ - - return dns.rdatatype.NONE + + return dns.rdatatype.NONE def extended_rdatatype(self): """Return a 32-bit type value, the least significant 16 bits of @@ -154,22 +156,29 @@ class Rdata(object): the "covered" type, if any. @rtype: int """ - + return self.covers() << 16 | self.rdtype def to_text(self, origin=None, relativize=True, **kw): """Convert an rdata to text format. @rtype: string """ - raise NotImplementedError + raise NotImplementedError def to_wire(self, file, compress = None, origin = None): """Convert an rdata to wire format. @rtype: string """ - + raise NotImplementedError + def to_digestable(self, origin = None): + """Convert rdata to a format suitable for digesting in hashes. This + is also the DNSSEC canonical form.""" + f = cStringIO.StringIO() + self.to_wire(f, None, origin) + return f.getvalue() + def validate(self): """Check that the current contents of the rdata's fields are valid. If you change an rdata by assigning to its fields, @@ -177,28 +186,28 @@ class Rdata(object): changes. """ dns.rdata.from_text(self.rdclass, self.rdtype, self.to_text()) - + def __repr__(self): - covers = self.covers() + covers = self.covers() if covers == dns.rdatatype.NONE: ctext = '' else: ctext = '(' + dns.rdatatype.to_text(covers) + ')' return '<DNS ' + dns.rdataclass.to_text(self.rdclass) + ' ' + \ dns.rdatatype.to_text(self.rdtype) + ctext + ' rdata: ' + \ - str(self) + '>' + str(self) + '>' def __str__(self): - return self.to_text() + return self.to_text() def _cmp(self, other): """Compare an rdata with another rdata of the same rdtype and rdclass. Return < 0 if self < other in the DNSSEC ordering, 0 if self == other, and > 0 if self > other. """ - + raise NotImplementedError - + def __eq__(self, other): if not isinstance(other, Rdata): return False @@ -265,7 +274,7 @@ class Rdata(object): def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None): """Build an rdata object from wire format - + @param rdclass: The rdata class @type rdclass: int @param rdtype: The rdata type @@ -289,7 +298,7 @@ class Rdata(object): """Convert any domain names in the rdata to the specified relativization. """ - + pass @@ -301,11 +310,11 @@ class GenericRdata(Rdata): """ __slots__ = ['data'] - + def __init__(self, rdclass, rdtype, data): super(GenericRdata, self).__init__(rdclass, rdtype) self.data = data - + def to_text(self, origin=None, relativize=True, **kw): return r'\# %d ' % len(self.data) + _hexify(self.data) @@ -331,14 +340,14 @@ class GenericRdata(Rdata): def to_wire(self, file, compress = None, origin = None): file.write(self.data) - + def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None): return cls(rdclass, rdtype, wire[current : current + rdlen]) from_wire = classmethod(from_wire) def _cmp(self, other): - return cmp(self.data, other.data) + return cmp(self.data, other.data) _rdata_modules = {} _module_prefix = 'dns.rdtypes' @@ -351,7 +360,7 @@ def get_rdata_class(rdclass, rdtype): for comp in components[1:]: mod = getattr(mod, comp) return mod - + mod = _rdata_modules.get((rdclass, rdtype)) rdclass_text = dns.rdataclass.to_text(rdclass) rdtype_text = dns.rdatatype.to_text(rdtype) @@ -398,7 +407,7 @@ def from_text(rdclass, rdtype, tok, origin = None, relativize = True): @param relativize: Should names be relativized? @type relativize: bool @rtype: dns.rdata.Rdata instance""" - + if isinstance(tok, str): tok = dns.tokenizer.Tokenizer(tok) cls = get_rdata_class(rdclass, rdtype) @@ -429,7 +438,7 @@ def from_wire(rdclass, rdtype, wire, current, rdlen, origin = None): Once a class is chosen, its from_wire() class method is called with the parameters to this function. - + @param rdclass: The rdata class @type rdclass: int @param rdtype: The rdata type |