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 18import struct 19import base64 20 21import dns.exception 22import dns.rdtypes.util 23 24 25class Gateway(dns.rdtypes.util.Gateway): 26 name = 'IPSECKEY gateway' 27 28class IPSECKEY(dns.rdata.Rdata): 29 30 """IPSECKEY record""" 31 32 # see: RFC 4025 33 34 __slots__ = ['precedence', 'gateway_type', 'algorithm', 'gateway', 'key'] 35 36 def __init__(self, rdclass, rdtype, precedence, gateway_type, algorithm, 37 gateway, key): 38 super().__init__(rdclass, rdtype) 39 Gateway(gateway_type, gateway).check() 40 object.__setattr__(self, 'precedence', precedence) 41 object.__setattr__(self, 'gateway_type', gateway_type) 42 object.__setattr__(self, 'algorithm', algorithm) 43 object.__setattr__(self, 'gateway', gateway) 44 object.__setattr__(self, 'key', key) 45 46 def to_text(self, origin=None, relativize=True, **kw): 47 gateway = Gateway(self.gateway_type, self.gateway).to_text(origin, 48 relativize) 49 return '%d %d %d %s %s' % (self.precedence, self.gateway_type, 50 self.algorithm, gateway, 51 dns.rdata._base64ify(self.key)) 52 53 @classmethod 54 def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True, 55 relativize_to=None): 56 precedence = tok.get_uint8() 57 gateway_type = tok.get_uint8() 58 algorithm = tok.get_uint8() 59 gateway = Gateway(gateway_type).from_text(tok, origin, relativize, 60 relativize_to) 61 b64 = tok.concatenate_remaining_identifiers().encode() 62 key = base64.b64decode(b64) 63 return cls(rdclass, rdtype, precedence, gateway_type, algorithm, 64 gateway, key) 65 66 def _to_wire(self, file, compress=None, origin=None, canonicalize=False): 67 header = struct.pack("!BBB", self.precedence, self.gateway_type, 68 self.algorithm) 69 file.write(header) 70 Gateway(self.gateway_type, self.gateway).to_wire(file, compress, 71 origin, canonicalize) 72 file.write(self.key) 73 74 @classmethod 75 def from_wire_parser(cls, rdclass, rdtype, parser, origin=None): 76 header = parser.get_struct('!BBB') 77 gateway_type = header[1] 78 gateway = Gateway(gateway_type).from_wire_parser(parser, origin) 79 key = parser.get_remaining() 80 return cls(rdclass, rdtype, header[0], gateway_type, header[2], 81 gateway, key) 82