dnspython  1.16.0
About: dnspython is a DNS toolkit (for Python 2.x) that supports almost all record types.
  Fossies Dox: dnspython-1.16.0.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

IPSECKEY.py
Go to the documentation of this file.
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 
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,
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():
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:
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)
dns.rdtypes.IN.IPSECKEY.IPSECKEY.gateway_type
gateway_type
Definition: IPSECKEY.py:62
dns.exception.FormError
Definition: exception.py:109
dns.exception.SyntaxError
Definition: exception.py:113
dns.rdtypes.IN.IPSECKEY.IPSECKEY.to_wire
def to_wire(self, file, compress=None, origin=None)
Definition: IPSECKEY.py:105
dns.rdtypes.IN.IPSECKEY.IPSECKEY.key
key
Definition: IPSECKEY.py:65
dns.rdtypes.IN.IPSECKEY.IPSECKEY.gateway
gateway
Definition: IPSECKEY.py:64
dns.rdtypes.IN.IPSECKEY.IPSECKEY.precedence
precedence
Definition: IPSECKEY.py:61
dns.rdtypes.IN.IPSECKEY.IPSECKEY.algorithm
algorithm
Definition: IPSECKEY.py:63
dns.inet
Definition: inet.py:1
dns.rdata.Rdata.choose_relativity
def choose_relativity(self, origin=None, relativize=True)
Definition: rdata.py:256
dns.name.from_wire
def from_wire(message, current)
Definition: name.py:945
dns.rdtypes.IN.IPSECKEY.IPSECKEY.from_text
def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True)
Definition: IPSECKEY.py:84
dns.rdtypes.IN.IPSECKEY.IPSECKEY
Definition: IPSECKEY.py:26
dns.name
Definition: name.py:1
dns.rdtypes.IN.IPSECKEY.IPSECKEY.to_text
def to_text(self, origin=None, relativize=True, **kw)
Definition: IPSECKEY.py:68
dns.rdata._base64ify
def _base64ify(data, chunksize=_base64_chunksize)
Definition: rdata.py:53
dns.rdtypes.IN.IPSECKEY.IPSECKEY.__init__
def __init__(self, rdclass, rdtype, precedence, gateway_type, algorithm, gateway, key)
Definition: IPSECKEY.py:44
dns.inet.inet_pton
def inet_pton(family, text)
Definition: inet.py:41
dns.rdtypes.IN.IPSECKEY.IPSECKEY.from_wire
def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None)
Definition: IPSECKEY.py:122
dns.inet.inet_ntop
def inet_ntop(family, address)
Definition: inet.py:62
dns.exception
Definition: exception.py:1
dns.rdata.Rdata
Definition: rdata.py:95