| 1 |
# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license |
| 2 |
|
| 3 |
# Copyright (C) 2003-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 struct |
| 19 |
|
| 20 |
import dns.exception |
| 21 |
import dns.immutable |
| 22 |
import dns.name |
| 23 |
import dns.rdata |
| 24 |
import dns.rdtypes.util |
| 25 |
|
| 26 |
|
| 27 |
def _write_string(file, s): |
| 28 |
l = len(s) |
| 29 |
assert l < 256 |
| 30 |
file.write(struct.pack("!B", l)) |
| 31 |
file.write(s) |
| 32 |
|
| 33 |
|
| 34 |
@dns.immutable.immutable |
| 35 |
class NAPTR(dns.rdata.Rdata): |
| 36 |
"""NAPTR record""" |
| 37 |
|
| 38 |
# see: RFC 3403 |
| 39 |
|
| 40 |
__slots__ = ["order", "preference", "flags", "service", "regexp", "replacement"] |
| 41 |
|
| 42 |
def __init__( |
| 43 |
self, rdclass, rdtype, order, preference, flags, service, regexp, replacement |
| 44 |
): |
| 45 |
super().__init__(rdclass, rdtype) |
| 46 |
self.flags = self._as_bytes(flags, True, 255) |
| 47 |
self.service = self._as_bytes(service, True, 255) |
| 48 |
self.regexp = self._as_bytes(regexp, True, 255) |
| 49 |
self.order = self._as_uint16(order) |
| 50 |
self.preference = self._as_uint16(preference) |
| 51 |
self.replacement = self._as_name(replacement) |
| 52 |
|
| 53 |
def to_text(self, origin=None, relativize=True, **kw): |
| 54 |
replacement = self.replacement.choose_relativity(origin, relativize) |
| 55 |
return ( |
| 56 |
f"{self.order} {self.preference} " |
| 57 |
f'"{dns.rdata._escapify(self.flags)}" ' |
| 58 |
f'"{dns.rdata._escapify(self.service)}" ' |
| 59 |
f'"{dns.rdata._escapify(self.regexp)}" ' |
| 60 |
f"{replacement}" |
| 61 |
) |
| 62 |
|
| 63 |
@classmethod |
| 64 |
def from_text( |
| 65 |
cls, rdclass, rdtype, tok, origin=None, relativize=True, relativize_to=None |
| 66 |
): |
| 67 |
order = tok.get_uint16() |
| 68 |
preference = tok.get_uint16() |
| 69 |
flags = tok.get_string() |
| 70 |
service = tok.get_string() |
| 71 |
regexp = tok.get_string() |
| 72 |
replacement = tok.get_name(origin, relativize, relativize_to) |
| 73 |
return cls( |
| 74 |
rdclass, rdtype, order, preference, flags, service, regexp, replacement |
| 75 |
) |
| 76 |
|
| 77 |
def _to_wire(self, file, compress=None, origin=None, canonicalize=False): |
| 78 |
two_ints = struct.pack("!HH", self.order, self.preference) |
| 79 |
file.write(two_ints) |
| 80 |
_write_string(file, self.flags) |
| 81 |
_write_string(file, self.service) |
| 82 |
_write_string(file, self.regexp) |
| 83 |
self.replacement.to_wire(file, compress, origin, canonicalize) |
| 84 |
|
| 85 |
@classmethod |
| 86 |
def from_wire_parser(cls, rdclass, rdtype, parser, origin=None): |
| 87 |
(order, preference) = parser.get_struct("!HH") |
| 88 |
strings = [] |
| 89 |
for _ in range(3): |
| 90 |
s = parser.get_counted_bytes() |
| 91 |
strings.append(s) |
| 92 |
replacement = parser.get_name(origin) |
| 93 |
return cls( |
| 94 |
rdclass, |
| 95 |
rdtype, |
| 96 |
order, |
| 97 |
preference, |
| 98 |
strings[0], |
| 99 |
strings[1], |
| 100 |
strings[2], |
| 101 |
replacement, |
| 102 |
) |
| 103 |
|
| 104 |
def _processing_priority(self): |
| 105 |
return (self.order, self.preference) |
| 106 |
|
| 107 |
@classmethod |
| 108 |
def _processing_order(cls, iterable): |
| 109 |
return dns.rdtypes.util.priority_processing_order(iterable) |