dnspython  1.16.0
About: dnspython is a DNS toolkit (for Python 2.x) that supports almost all record types.
  Fossies Dox: dnspython-1.16.0.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

mxbase.py
Go to the documentation of this file.
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 from io import BytesIO
21 import struct
22 
23 import dns.exception
24 import dns.rdata
25 import dns.name
26 
27 
29 
30  """Base class for rdata that is like an MX record.
31 
32  @ivar preference: the preference value
33  @type preference: int
34  @ivar exchange: the exchange name
35  @type exchange: dns.name.Name object"""
36 
37  __slots__ = ['preference', 'exchange']
38 
39  def __init__(self, rdclass, rdtype, preference, exchange):
40  super(MXBase, self).__init__(rdclass, rdtype)
41  self.preference = preference
42  self.exchange = exchange
43 
44  def to_text(self, origin=None, relativize=True, **kw):
45  exchange = self.exchange.choose_relativity(origin, relativize)
46  return '%d %s' % (self.preference, exchange)
47 
48  @classmethod
49  def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True):
50  preference = tok.get_uint16()
51  exchange = tok.get_name()
52  exchange = exchange.choose_relativity(origin, relativize)
53  tok.get_eol()
54  return cls(rdclass, rdtype, preference, exchange)
55 
56  def to_wire(self, file, compress=None, origin=None):
57  pref = struct.pack("!H", self.preference)
58  file.write(pref)
59  self.exchange.to_wire(file, compress, origin)
60 
61  def to_digestable(self, origin=None):
62  return struct.pack("!H", self.preference) + \
63  self.exchange.to_digestable(origin)
64 
65  @classmethod
66  def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None):
67  (preference, ) = struct.unpack('!H', wire[current: current + 2])
68  current += 2
69  rdlen -= 2
70  (exchange, cused) = dns.name.from_wire(wire[: current + rdlen],
71  current)
72  if cused != rdlen:
74  if origin is not None:
75  exchange = exchange.relativize(origin)
76  return cls(rdclass, rdtype, preference, exchange)
77 
78  def choose_relativity(self, origin=None, relativize=True):
79  self.exchange = self.exchange.choose_relativity(origin, relativize)
80 
81 
83 
84  """Base class for rdata that is like an MX record, but whose name
85  is not compressed when converted to DNS wire format, and whose
86  digestable form is not downcased."""
87 
88  def to_wire(self, file, compress=None, origin=None):
89  super(UncompressedMX, self).to_wire(file, None, origin)
90 
91  def to_digestable(self, origin=None):
92  f = BytesIO()
93  self.to_wire(f, None, origin)
94  return f.getvalue()
95 
96 
98 
99  """Base class for rdata that is like an MX record, but whose name
100  is not compressed when convert to DNS wire format."""
101 
102  def to_wire(self, file, compress=None, origin=None):
103  super(UncompressedDowncasingMX, self).to_wire(file, None, origin)
dns.exception.FormError
Definition: exception.py:109
dns.rdtypes.mxbase.MXBase.to_text
def to_text(self, origin=None, relativize=True, **kw)
Definition: mxbase.py:44
dns.rdtypes.mxbase.MXBase.from_text
def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True)
Definition: mxbase.py:49
dns.rdtypes.mxbase.UncompressedDowncasingMX
Definition: mxbase.py:97
dns.rdtypes.mxbase.MXBase
Definition: mxbase.py:28
dns.rdtypes.mxbase.MXBase.preference
preference
Definition: mxbase.py:41
dns.rdtypes.mxbase.MXBase.__init__
def __init__(self, rdclass, rdtype, preference, exchange)
Definition: mxbase.py:39
dns.rdtypes.mxbase.MXBase.choose_relativity
def choose_relativity(self, origin=None, relativize=True)
Definition: mxbase.py:78
dns.rdtypes.mxbase.MXBase.to_wire
def to_wire(self, file, compress=None, origin=None)
Definition: mxbase.py:56
dns.name.from_wire
def from_wire(message, current)
Definition: name.py:945
dns.rdtypes.mxbase.UncompressedMX
Definition: mxbase.py:82
dns.name
Definition: name.py:1
dns.rdtypes.mxbase.MXBase.from_wire
def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None)
Definition: mxbase.py:66
dns.rdata
Definition: rdata.py:1
dns.rdtypes.mxbase.UncompressedMX.to_digestable
def to_digestable(self, origin=None)
Definition: mxbase.py:91
dns.rdtypes.mxbase.UncompressedDowncasingMX.to_wire
def to_wire(self, file, compress=None, origin=None)
Definition: mxbase.py:102
dns.rdtypes.mxbase.MXBase.exchange
exchange
Definition: mxbase.py:42
dns.rdata.Rdata.to_wire
def to_wire(self, file, compress=None, origin=None)
Definition: rdata.py:142
dns.rdtypes.mxbase.MXBase.to_digestable
def to_digestable(self, origin=None)
Definition: mxbase.py:61
dns.rdtypes.mxbase.UncompressedMX.to_wire
def to_wire(self, file, compress=None, origin=None)
Definition: mxbase.py:88
dns.exception
Definition: exception.py:1
dns.rdata.Rdata
Definition: rdata.py:95