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

Source Code for Module dns.rdtypes.IN.IPSECKEY

  1  # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license 
  2   
  3  # Copyright (C) 2006, 2007, 2009-2011 Nominum, Inc. 
  4  # 
  5  # Permission to use, copy, modify, and distribute this software and its 
  6  # documentation for any purpose with or without fee is hereby granted, 
  7  # provided that the above copyright notice and this permission notice 
  8  # appear in all copies. 
  9  # 
 10  # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES 
 11  # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 
 12  # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR 
 13  # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 
 14  # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
 15  # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 
 16  # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 
 17   
 18  import struct 
 19  import base64 
 20   
 21  import dns.exception 
 22  import dns.inet 
 23  import dns.name 
24 25 26 -class IPSECKEY(dns.rdata.Rdata):
27 28 """IPSECKEY record 29 30 @ivar precedence: the precedence for this key data 31 @type precedence: int 32 @ivar gateway_type: the gateway type 33 @type gateway_type: int 34 @ivar algorithm: the algorithm to use 35 @type algorithm: int 36 @ivar gateway: the public key 37 @type gateway: None, IPv4 address, IPV6 address, or domain name 38 @ivar key: the public key 39 @type key: string 40 @see: RFC 4025""" 41 42 __slots__ = ['precedence', 'gateway_type', 'algorithm', 'gateway', 'key'] 43
44 - def __init__(self, rdclass, rdtype, precedence, gateway_type, algorithm, 45 gateway, key):
46 super(IPSECKEY, self).__init__(rdclass, rdtype) 47 if gateway_type == 0: 48 if gateway != '.' and gateway is not None: 49 raise SyntaxError('invalid gateway for gateway type 0') 50 gateway = None 51 elif gateway_type == 1: 52 # check that it's OK 53 dns.inet.inet_pton(dns.inet.AF_INET, gateway) 54 elif gateway_type == 2: 55 # check that it's OK 56 dns.inet.inet_pton(dns.inet.AF_INET6, gateway) 57 elif gateway_type == 3: 58 pass 59 else: 60 raise SyntaxError( 61 'invalid IPSECKEY gateway type: %d' % gateway_type) 62 self.precedence = precedence 63 self.gateway_type = gateway_type 64 self.algorithm = algorithm 65 self.gateway = gateway 66 self.key = key
67
68 - def to_text(self, origin=None, relativize=True, **kw):
69 if self.gateway_type == 0: 70 gateway = '.' 71 elif self.gateway_type == 1: 72 gateway = self.gateway 73 elif self.gateway_type == 2: 74 gateway = self.gateway 75 elif self.gateway_type == 3: 76 gateway = str(self.gateway.choose_relativity(origin, relativize)) 77 else: 78 raise ValueError('invalid gateway type') 79 return '%d %d %d %s %s' % (self.precedence, self.gateway_type, 80 self.algorithm, gateway, 81 dns.rdata._base64ify(self.key))
82 83 @classmethod
84 - def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True):
85 precedence = tok.get_uint8() 86 gateway_type = tok.get_uint8() 87 algorithm = tok.get_uint8() 88 if gateway_type == 3: 89 gateway = tok.get_name().choose_relativity(origin, relativize) 90 else: 91 gateway = tok.get_string() 92 chunks = [] 93 while 1: 94 t = tok.get().unescape() 95 if t.is_eol_or_eof(): 96 break 97 if not t.is_identifier(): 98 raise dns.exception.SyntaxError 99 chunks.append(t.value.encode()) 100 b64 = b''.join(chunks) 101 key = base64.b64decode(b64) 102 return cls(rdclass, rdtype, precedence, gateway_type, algorithm, 103 gateway, key)
104
105 - def to_wire(self, file, compress=None, origin=None):
106 header = struct.pack("!BBB", self.precedence, self.gateway_type, 107 self.algorithm) 108 file.write(header) 109 if self.gateway_type == 0: 110 pass 111 elif self.gateway_type == 1: 112 file.write(dns.inet.inet_pton(dns.inet.AF_INET, self.gateway)) 113 elif self.gateway_type == 2: 114 file.write(dns.inet.inet_pton(dns.inet.AF_INET6, self.gateway)) 115 elif self.gateway_type == 3: 116 self.gateway.to_wire(file, None, origin) 117 else: 118 raise ValueError('invalid gateway type') 119 file.write(self.key)
120 121 @classmethod
122 - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None):
123 if rdlen < 3: 124 raise dns.exception.FormError 125 header = struct.unpack('!BBB', wire[current: current + 3]) 126 gateway_type = header[1] 127 current += 3 128 rdlen -= 3 129 if gateway_type == 0: 130 gateway = None 131 elif gateway_type == 1: 132 gateway = dns.inet.inet_ntop(dns.inet.AF_INET, 133 wire[current: current + 4]) 134 current += 4 135 rdlen -= 4 136 elif gateway_type == 2: 137 gateway = dns.inet.inet_ntop(dns.inet.AF_INET6, 138 wire[current: current + 16]) 139 current += 16 140 rdlen -= 16 141 elif gateway_type == 3: 142 (gateway, cused) = dns.name.from_wire(wire[: current + rdlen], 143 current) 144 current += cused 145 rdlen -= cused 146 else: 147 raise dns.exception.FormError('invalid IPSECKEY gateway type') 148 key = wire[current: current + rdlen].unwrap() 149 return cls(rdclass, rdtype, header[0], gateway_type, header[2], 150 gateway, key)
151