Package dns :: Package rdtypes :: Module dsbase |
[hide private]
[frames] | no frames]

# Source Code for Module dns.rdtypes.dsbase

     1  # Copyright (C) 2010, 2011 Nominum, Inc.
     2  #
     3  # Permission to use, copy, modify, and distribute this software and its
     4  # documentation for any purpose with or without fee is hereby granted,
     5  # provided that the above copyright notice and this permission notice
     6  # appear in all copies.
     7  #
     8  # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
     9  # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
    10  # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
    11  # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    12  # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
    13  # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
    14  # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    15
    16  import struct
    17  import binascii
    18
    19  import dns.rdata
    20  import dns.rdatatype
    21
    22
    23 -class DSBase(dns.rdata.Rdata):
    24
    25      """Base class for rdata that is like a DS record
    26
    27      @ivar key_tag: the key tag
    28      @type key_tag: int
    29      @ivar algorithm: the algorithm
    30      @type algorithm: int
    31      @ivar digest_type: the digest type
    32      @type digest_type: int
    33      @ivar digest: the digest
    34      @type digest: int
    35      @see: draft-ietf-dnsext-delegation-signer-14.txt"""
    36
    37      __slots__ = ['key_tag', 'algorithm', 'digest_type', 'digest']
    38
    39 -    def __init__(self, rdclass, rdtype, key_tag, algorithm, digest_type,
    40                   digest):
    41          super(DSBase, self).__init__(rdclass, rdtype)
    42          self.key_tag = key_tag
    43          self.algorithm = algorithm
    44          self.digest_type = digest_type
    45          self.digest = digest
    46
    47 -    def to_text(self, origin=None, relativize=True, **kw):
    48          return '%d %d %d %s' % (self.key_tag, self.algorithm,
    49                                  self.digest_type,
    50                                  dns.rdata._hexify(self.digest,
    51                                                    chunksize=128))
    52
    53      @classmethod
    54 -    def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True):
    55          key_tag = tok.get_uint16()
    56          algorithm = tok.get_uint8()
    57          digest_type = tok.get_uint8()
    58          chunks = []
    59          while 1:
    60              t = tok.get().unescape()
    61              if t.is_eol_or_eof():
    62                  break
    63              if not t.is_identifier():
    64                  raise dns.exception.SyntaxError
    65              chunks.append(t.value.encode())
    66          digest = b''.join(chunks)
    67          digest = binascii.unhexlify(digest)
    68          return cls(rdclass, rdtype, key_tag, algorithm, digest_type,
    69                     digest)
    70
    71 -    def to_wire(self, file, compress=None, origin=None):
    72          header = struct.pack("!HBB", self.key_tag, self.algorithm,
    73                               self.digest_type)
    74          file.write(header)
    75          file.write(self.digest)
    76
    77      @classmethod
    78 -    def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None):
    79          header = struct.unpack("!HBB", wire[current: current + 4])
    80          current += 4
    81          rdlen -= 4
    82          digest = wire[current: current + rdlen].unwrap()
    83          return cls(rdclass, rdtype, header[0], header[1], header[2], digest)
    84