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

# Source Code for Module dns.rdtypes.ANY.SSHFP

     1  # Copyright (C) 2005-2007, 2009-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 SSHFP(dns.rdata.Rdata):
    24
    25      """SSHFP record
    26
    27      @ivar algorithm: the algorithm
    28      @type algorithm: int
    29      @ivar fp_type: the digest type
    30      @type fp_type: int
    31      @ivar fingerprint: the fingerprint
    32      @type fingerprint: string
    33      @see: draft-ietf-secsh-dns-05.txt"""
    34
    35      __slots__ = ['algorithm', 'fp_type', 'fingerprint']
    36
    37 -    def __init__(self, rdclass, rdtype, algorithm, fp_type,
    38                   fingerprint):
    39          super(SSHFP, self).__init__(rdclass, rdtype)
    40          self.algorithm = algorithm
    41          self.fp_type = fp_type
    42          self.fingerprint = fingerprint
    43
    44 -    def to_text(self, origin=None, relativize=True, **kw):
    45          return '%d %d %s' % (self.algorithm,
    46                               self.fp_type,
    47                               dns.rdata._hexify(self.fingerprint,
    48                                                 chunksize=128))
    49
    50      @classmethod
    51 -    def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True):
    52          algorithm = tok.get_uint8()
    53          fp_type = tok.get_uint8()
    54          chunks = []
    55          while 1:
    56              t = tok.get().unescape()
    57              if t.is_eol_or_eof():
    58                  break
    59              if not t.is_identifier():
    60                  raise dns.exception.SyntaxError
    61              chunks.append(t.value.encode())
    62          fingerprint = b''.join(chunks)
    63          fingerprint = binascii.unhexlify(fingerprint)
    64          return cls(rdclass, rdtype, algorithm, fp_type, fingerprint)
    65
    66 -    def to_wire(self, file, compress=None, origin=None):
    67          header = struct.pack("!BB", self.algorithm, self.fp_type)
    68          file.write(header)
    69          file.write(self.fingerprint)
    70
    71      @classmethod
    72 -    def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None):
    73          header = struct.unpack("!BB", wire[current: current + 2])
    74          current += 2
    75          rdlen -= 2
    76          fingerprint = wire[current: current + rdlen].unwrap()
    77          return cls(rdclass, rdtype, header[0], header[1], fingerprint)
    78