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 18import struct 19 20import dns.edns 21import dns.exception 22import dns.rdata 23 24 25class OPT(dns.rdata.Rdata): 26 27 """OPT record""" 28 29 __slots__ = ['options'] 30 31 def __init__(self, rdclass, rdtype, options): 32 """Initialize an OPT rdata. 33 34 *rdclass*, an ``int`` is the rdataclass of the Rdata, 35 which is also the payload size. 36 37 *rdtype*, an ``int`` is the rdatatype of the Rdata. 38 39 *options*, a tuple of ``bytes`` 40 """ 41 42 super().__init__(rdclass, rdtype) 43 object.__setattr__(self, 'options', dns.rdata._constify(options)) 44 45 def _to_wire(self, file, compress=None, origin=None, canonicalize=False): 46 for opt in self.options: 47 owire = opt.to_wire() 48 file.write(struct.pack("!HH", opt.otype, len(owire))) 49 file.write(owire) 50 51 def to_text(self, origin=None, relativize=True, **kw): 52 return ' '.join(opt.to_text() for opt in self.options) 53 54 @classmethod 55 def from_wire_parser(cls, rdclass, rdtype, parser, origin=None): 56 options = [] 57 while parser.remaining() > 0: 58 (otype, olen) = parser.get_struct('!HH') 59 with parser.restrict_to(olen): 60 opt = dns.edns.option_from_wire_parser(otype, parser) 61 options.append(opt) 62 return cls(rdclass, rdtype, options) 63 64 @property 65 def payload(self): 66 "payload size" 67 return self.rdclass 68