| 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 |
"""MX-like base classes.""" |
| 19 |
|
| 20 |
import struct |
| 21 |
|
| 22 |
import dns.exception |
| 23 |
import dns.immutable |
| 24 |
import dns.name |
| 25 |
import dns.rdata |
| 26 |
import dns.rdtypes.util |
| 27 |
|
| 28 |
|
| 29 |
@dns.immutable.immutable |
| 30 |
class MXBase(dns.rdata.Rdata): |
| 31 |
"""Base class for rdata that is like an MX record.""" |
| 32 |
|
| 33 |
__slots__ = ["preference", "exchange"] |
| 34 |
|
| 35 |
def __init__(self, rdclass, rdtype, preference, exchange): |
| 36 |
super().__init__(rdclass, rdtype) |
| 37 |
self.preference = self._as_uint16(preference) |
| 38 |
self.exchange = self._as_name(exchange) |
| 39 |
|
| 40 |
def to_text(self, origin=None, relativize=True, **kw): |
| 41 |
exchange = self.exchange.choose_relativity(origin, relativize) |
| 42 |
return f"{self.preference} {exchange}" |
| 43 |
|
| 44 |
@classmethod |
| 45 |
def from_text( |
| 46 |
cls, rdclass, rdtype, tok, origin=None, relativize=True, relativize_to=None |
| 47 |
): |
| 48 |
preference = tok.get_uint16() |
| 49 |
exchange = tok.get_name(origin, relativize, relativize_to) |
| 50 |
return cls(rdclass, rdtype, preference, exchange) |
| 51 |
|
| 52 |
def _to_wire(self, file, compress=None, origin=None, canonicalize=False): |
| 53 |
pref = struct.pack("!H", self.preference) |
| 54 |
file.write(pref) |
| 55 |
self.exchange.to_wire(file, compress, origin, canonicalize) |
| 56 |
|
| 57 |
@classmethod |
| 58 |
def from_wire_parser(cls, rdclass, rdtype, parser, origin=None): |
| 59 |
preference = parser.get_uint16() |
| 60 |
exchange = parser.get_name(origin) |
| 61 |
return cls(rdclass, rdtype, preference, exchange) |
| 62 |
|
| 63 |
def _processing_priority(self): |
| 64 |
return self.preference |
| 65 |
|
| 66 |
@classmethod |
| 67 |
def _processing_order(cls, iterable): |
| 68 |
return dns.rdtypes.util.priority_processing_order(iterable) |
| 69 |
|
| 70 |
|
| 71 |
@dns.immutable.immutable |
| 72 |
class UncompressedMX(MXBase): |
| 73 |
"""Base class for rdata that is like an MX record, but whose name |
| 74 |
is not compressed when converted to DNS wire format, and whose |
| 75 |
digestable form is not downcased.""" |
| 76 |
|
| 77 |
def _to_wire(self, file, compress=None, origin=None, canonicalize=False): |
| 78 |
super()._to_wire(file, None, origin, False) |
| 79 |
|
| 80 |
|
| 81 |
@dns.immutable.immutable |
| 82 |
class UncompressedDowncasingMX(MXBase): |
| 83 |
"""Base class for rdata that is like an MX record, but whose name |
| 84 |
is not compressed when convert to DNS wire format.""" |
| 85 |
|
| 86 |
def _to_wire(self, file, compress=None, origin=None, canonicalize=False): |
| 87 |
super()._to_wire(file, None, origin, canonicalize) |