Debian Codegraph @

dns/rdtypes/ANY/NSEC3PARAM.py

raw
1 # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
2
3 # Copyright (C) 2004-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 binascii
19 import struct
20
21 import dns.exception
22 import dns.immutable
23 import dns.rdata
24
25
26 @dns.immutable.immutable
27 class NSEC3PARAM(dns.rdata.Rdata):
28 """NSEC3PARAM record"""
29
30 __slots__ = ["algorithm", "flags", "iterations", "salt"]
31
32 def __init__(self, rdclass, rdtype, algorithm, flags, iterations, salt):
33 super().__init__(rdclass, rdtype)
34 self.algorithm = self._as_uint8(algorithm)
35 self.flags = self._as_uint8(flags)
36 self.iterations = self._as_uint16(iterations)
37 self.salt = self._as_bytes(salt, True, 255)
38
39 def to_text(self, origin=None, relativize=True, **kw):
40 if self.salt == b"":
41 salt = "-"
42 else:
43 salt = binascii.hexlify(self.salt).decode()
44 return f"{self.algorithm} {self.flags} {self.iterations} {salt}"
45
46 @classmethod
47 def from_text(
48 cls, rdclass, rdtype, tok, origin=None, relativize=True, relativize_to=None
49 ):
50 algorithm = tok.get_uint8()
51 flags = tok.get_uint8()
52 iterations = tok.get_uint16()
53 salt = tok.get_string()
54 if salt == "-":
55 salt = ""
56 else:
57 salt = binascii.unhexlify(salt.encode())
58 return cls(rdclass, rdtype, algorithm, flags, iterations, salt)
59
60 def _to_wire(self, file, compress=None, origin=None, canonicalize=False):
61 l = len(self.salt)
62 file.write(struct.pack("!BBHB", self.algorithm, self.flags, self.iterations, l))
63 file.write(self.salt)
64
65 @classmethod
66 def from_wire_parser(cls, rdclass, rdtype, parser, origin=None):
67 (algorithm, flags, iterations) = parser.get_struct("!BBH")
68 salt = parser.get_counted_bytes()
69 return cls(rdclass, rdtype, algorithm, flags, iterations, salt)