| 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 |
@dns.immutable.immutable |
| 28 |
class SRV(dns.rdata.Rdata): |
| 29 |
"""SRV record""" |
| 30 |
|
| 31 |
# see: RFC 2782 |
| 32 |
|
| 33 |
__slots__ = ["priority", "weight", "port", "target"] |
| 34 |
|
| 35 |
def __init__(self, rdclass, rdtype, priority, weight, port, target): |
| 36 |
super().__init__(rdclass, rdtype) |
| 37 |
self.priority = self._as_uint16(priority) |
| 38 |
self.weight = self._as_uint16(weight) |
| 39 |
self.port = self._as_uint16(port) |
| 40 |
self.target = self._as_name(target) |
| 41 |
|
| 42 |
def to_text(self, origin=None, relativize=True, **kw): |
| 43 |
target = self.target.choose_relativity(origin, relativize) |
| 44 |
return f"{self.priority} {self.weight} {self.port} {target}" |
| 45 |
|
| 46 |
@classmethod |
| 47 |
def from_text( |
| 48 |
cls, rdclass, rdtype, tok, origin=None, relativize=True, relativize_to=None |
| 49 |
): |
| 50 |
priority = tok.get_uint16() |
| 51 |
weight = tok.get_uint16() |
| 52 |
port = tok.get_uint16() |
| 53 |
target = tok.get_name(origin, relativize, relativize_to) |
| 54 |
return cls(rdclass, rdtype, priority, weight, port, target) |
| 55 |
|
| 56 |
def _to_wire(self, file, compress=None, origin=None, canonicalize=False): |
| 57 |
three_ints = struct.pack("!HHH", self.priority, self.weight, self.port) |
| 58 |
file.write(three_ints) |
| 59 |
self.target.to_wire(file, compress, origin, canonicalize) |
| 60 |
|
| 61 |
@classmethod |
| 62 |
def from_wire_parser(cls, rdclass, rdtype, parser, origin=None): |
| 63 |
(priority, weight, port) = parser.get_struct("!HHH") |
| 64 |
target = parser.get_name(origin) |
| 65 |
return cls(rdclass, rdtype, priority, weight, port, target) |
| 66 |
|
| 67 |
def _processing_priority(self): |
| 68 |
return self.priority |
| 69 |
|
| 70 |
def _processing_weight(self): |
| 71 |
return self.weight |
| 72 |
|
| 73 |
@classmethod |
| 74 |
def _processing_order(cls, iterable): |
| 75 |
return dns.rdtypes.util.weighted_processing_order(iterable) |