Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/dns/rdtypes/ANY/OPT.py: 43%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
3# Copyright (C) 2001-2017 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.
18import struct
20import dns.edns
21import dns.exception
22import dns.immutable
23import dns.rdata
25# We don't implement from_text, and that's ok.
26# pylint: disable=abstract-method
29@dns.immutable.immutable
30class OPT(dns.rdata.Rdata):
31 """OPT record"""
33 __slots__ = ["options"]
35 def __init__(self, rdclass, rdtype, options):
36 """Initialize an OPT rdata.
38 :param rdclass: The rdataclass of the Rdata, which is also the
39 payload size.
40 :type rdclass: int
41 :param rdtype: The rdatatype of the Rdata.
42 :type rdtype: int
43 :param options: The EDNS options.
44 :type options: tuple of :py:class:`dns.edns.Option`
45 """
47 super().__init__(rdclass, rdtype)
49 def as_option(option):
50 if not isinstance(option, dns.edns.Option):
51 raise ValueError("option is not a dns.edns.option")
52 return option
54 self.options = self._as_tuple(options, as_option)
56 def _to_wire(self, file, compress=None, origin=None, canonicalize=False):
57 for opt in self.options:
58 owire = opt.to_wire()
59 file.write(struct.pack("!HH", opt.otype, len(owire)))
60 file.write(owire)
62 def to_styled_text(self, style: dns.rdata.RdataStyle) -> str:
63 return " ".join(opt.to_text() for opt in self.options)
65 @classmethod
66 def from_wire_parser(cls, rdclass, rdtype, parser, origin=None):
67 options = []
68 while parser.remaining() > 0:
69 otype, olen = parser.get_struct("!HH")
70 with parser.restrict_to(olen):
71 opt = dns.edns.option_from_wire_parser(otype, parser)
72 options.append(opt)
73 return cls(rdclass, rdtype, options)
75 @property
76 def payload(self):
77 "payload size"
78 return self.rdclass