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

# Source Code for Module dns.rdtypes.IN.IPSECKEY

      1  # Copyright (C) 2006, 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 cStringIO
     17  import struct
     18
     19  import dns.exception
     20  import dns.inet
     21  import dns.name
     22
     23 -class IPSECKEY(dns.rdata.Rdata):
     24      """IPSECKEY record
     25
     26      @ivar precedence: the precedence for this key data
     27      @type precedence: int
     28      @ivar gateway_type: the gateway type
     29      @type gateway_type: int
     30      @ivar algorithm: the algorithm to use
     31      @type algorithm: int
     32      @ivar gateway: the public key
     33      @type gateway: None, IPv4 address, IPV6 address, or domain name
     34      @ivar key: the public key
     35      @type key: string
     36      @see: RFC 4025"""
     37
     38      __slots__ = ['precedence', 'gateway_type', 'algorithm', 'gateway', 'key']
     39
     40 -    def __init__(self, rdclass, rdtype, precedence, gateway_type, algorithm,
     41                   gateway, key):
     42          super(IPSECKEY, self).__init__(rdclass, rdtype)
     43          if gateway_type == 0:
     44              if gateway != '.' and not gateway is None:
     45                  raise SyntaxError('invalid gateway for gateway type 0')
     46              gateway = None
     47          elif gateway_type == 1:
     48              # check that it's OK
     49              junk = dns.inet.inet_pton(dns.inet.AF_INET, gateway)
     50          elif gateway_type == 2:
     51              # check that it's OK
     52              junk = dns.inet.inet_pton(dns.inet.AF_INET6, gateway)
     53          elif gateway_type == 3:
     54              pass
     55          else:
     56              raise SyntaxError('invalid IPSECKEY gateway type: %d' % gateway_type)
     57          self.precedence = precedence
     58          self.gateway_type = gateway_type
     59          self.algorithm = algorithm
     60          self.gateway = gateway
     61          self.key = key
     62
     63 -    def to_text(self, origin=None, relativize=True, **kw):
     64          if self.gateway_type == 0:
     65              gateway = '.'
     66          elif self.gateway_type == 1:
     67              gateway = self.gateway
     68          elif self.gateway_type == 2:
     69              gateway = self.gateway
     70          elif self.gateway_type == 3:
     71              gateway = str(self.gateway.choose_relativity(origin, relativize))
     72          else:
     73              raise ValueError('invalid gateway type')
     74          return '%d %d %d %s %s' % (self.precedence, self.gateway_type,
     75                                     self.algorithm, gateway,
     76                                     dns.rdata._base64ify(self.key))
     77
     78 -    def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
     79          precedence = tok.get_uint8()
     80          gateway_type = tok.get_uint8()
     81          algorithm = tok.get_uint8()
     82          if gateway_type == 3:
     83              gateway = tok.get_name().choose_relativity(origin, relativize)
     84          else:
     85              gateway = tok.get_string()
     86          chunks = []
     87          while 1:
     88              t = tok.get().unescape()
     89              if t.is_eol_or_eof():
     90                  break
     91              if not t.is_identifier():
     92                  raise dns.exception.SyntaxError
     93              chunks.append(t.value)
     94          b64 = ''.join(chunks)
     95          key = b64.decode('base64_codec')
     96          return cls(rdclass, rdtype, precedence, gateway_type, algorithm,
     97                     gateway, key)
     98
     99      from_text = classmethod(from_text)
    100
    101 -    def to_wire(self, file, compress = None, origin = None):
    102          header = struct.pack("!BBB", self.precedence, self.gateway_type,
    103                               self.algorithm)
    104          file.write(header)
    105          if self.gateway_type == 0:
    106              pass
    107          elif self.gateway_type == 1:
    108              file.write(dns.inet.inet_pton(dns.inet.AF_INET, self.gateway))
    109          elif self.gateway_type == 2:
    110              file.write(dns.inet.inet_pton(dns.inet.AF_INET6, self.gateway))
    111          elif self.gateway_type == 3:
    112              self.gateway.to_wire(file, None, origin)
    113          else:
    114              raise ValueError('invalid gateway type')
    115          file.write(self.key)
    116
    117 -    def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
    118          if rdlen < 3:
    119              raise dns.exception.FormError
    120          header = struct.unpack('!BBB', wire[current : current + 3])
    121          gateway_type = header[1]
    122          current += 3
    123          rdlen -= 3
    124          if gateway_type == 0:
    125              gateway = None
    126          elif gateway_type == 1:
    127              gateway = dns.inet.inet_ntop(dns.inet.AF_INET,
    128                                           wire[current : current + 4])
    129              current += 4
    130              rdlen -= 4
    131          elif gateway_type == 2:
    132              gateway = dns.inet.inet_ntop(dns.inet.AF_INET6,
    133                                           wire[current : current + 16])
    134              current += 16
    135              rdlen -= 16
    136          elif gateway_type == 3:
    137              (gateway, cused) = dns.name.from_wire(wire[: current + rdlen],
    138                                                    current)
    139              current += cused
    140              rdlen -= cused
    141          else:
    142              raise dns.exception.FormError('invalid IPSECKEY gateway type')
    143          key = wire[current : current + rdlen].unwrap()
    144          return cls(rdclass, rdtype, header[0], gateway_type, header[2],
    145                     gateway, key)
    146
    147      from_wire = classmethod(from_wire)
    148
    149 -    def _cmp(self, other):
    150          f = cStringIO.StringIO()
    151          self.to_wire(f)
    152          wire1 = f.getvalue()
    153          f.seek(0)
    154          f.truncate()
    155          other.to_wire(f)
    156          wire2 = f.getvalue()
    157          f.close()
    158
    159          return cmp(wire1, wire2)
    160