| 1 |
# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license |
| 2 |
|
| 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. |
| 17 |
|
| 18 |
import struct |
| 19 |
|
| 20 |
import dns.edns |
| 21 |
import dns.exception |
| 22 |
import dns.immutable |
| 23 |
import dns.rdata |
| 24 |
|
| 25 |
# We don't implement from_text, and that's ok. |
| 26 |
# pylint: disable=abstract-method |
| 27 |
|
| 28 |
|
| 29 |
@dns.immutable.immutable |
| 30 |
class OPT(dns.rdata.Rdata): |
| 31 |
"""OPT record""" |
| 32 |
|
| 33 |
__slots__ = ["options"] |
| 34 |
|
| 35 |
def __init__(self, rdclass, rdtype, options): |
| 36 |
"""Initialize an OPT rdata. |
| 37 |
|
| 38 |
*rdclass*, an ``int`` is the rdataclass of the Rdata, |
| 39 |
which is also the payload size. |
| 40 |
|
| 41 |
*rdtype*, an ``int`` is the rdatatype of the Rdata. |
| 42 |
|
| 43 |
*options*, a tuple of ``bytes`` |
| 44 |
""" |
| 45 |
|
| 46 |
super().__init__(rdclass, rdtype) |
| 47 |
|
| 48 |
def as_option(option): |
| 49 |
if not isinstance(option, dns.edns.Option): |
| 50 |
raise ValueError("option is not a dns.edns.option") |
| 51 |
return option |
| 52 |
|
| 53 |
self.options = self._as_tuple(options, as_option) |
| 54 |
|
| 55 |
def _to_wire(self, file, compress=None, origin=None, canonicalize=False): |
| 56 |
for opt in self.options: |
| 57 |
owire = opt.to_wire() |
| 58 |
file.write(struct.pack("!HH", opt.otype, len(owire))) |
| 59 |
file.write(owire) |
| 60 |
|
| 61 |
def to_text(self, origin=None, relativize=True, **kw): |
| 62 |
return " ".join(opt.to_text() for opt in self.options) |
| 63 |
|
| 64 |
@classmethod |
| 65 |
def from_wire_parser(cls, rdclass, rdtype, parser, origin=None): |
| 66 |
options = [] |
| 67 |
while parser.remaining() > 0: |
| 68 |
(otype, olen) = parser.get_struct("!HH") |
| 69 |
with parser.restrict_to(olen): |
| 70 |
opt = dns.edns.option_from_wire_parser(otype, parser) |
| 71 |
options.append(opt) |
| 72 |
return cls(rdclass, rdtype, options) |
| 73 |
|
| 74 |
@property |
| 75 |
def payload(self): |
| 76 |
"payload size" |
| 77 |
return self.rdclass |