1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import struct
17
18 import dns.exception
19 import dns.rdata
20 import dns.name
21 import dns.util
22
23 -class SRV(dns.rdata.Rdata):
24 """SRV record
25
26 @ivar priority: the priority
27 @type priority: int
28 @ivar weight: the weight
29 @type weight: int
30 @ivar port: the port of the service
31 @type port: int
32 @ivar target: the target host
33 @type target: dns.name.Name object
34 @see: RFC 2782"""
35
36 __slots__ = ['priority', 'weight', 'port', 'target']
37
38 - def __init__(self, rdclass, rdtype, priority, weight, port, target):
39 super(SRV, self).__init__(rdclass, rdtype)
40 self.priority = priority
41 self.weight = weight
42 self.port = port
43 self.target = target
44
45 - def to_text(self, origin=None, relativize=True, **kw):
46 target = self.target.choose_relativity(origin, relativize)
47 return '%d %d %d %s' % (self.priority, self.weight, self.port,
48 target)
49
50 - def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
51 priority = tok.get_uint16()
52 weight = tok.get_uint16()
53 port = tok.get_uint16()
54 target = tok.get_name(None)
55 target = target.choose_relativity(origin, relativize)
56 tok.get_eol()
57 return cls(rdclass, rdtype, priority, weight, port, target)
58
59 from_text = classmethod(from_text)
60
61 - def to_wire(self, file, compress = None, origin = None):
62 three_ints = struct.pack("!HHH", self.priority, self.weight, self.port)
63 file.write(three_ints)
64 self.target.to_wire(file, compress, origin)
65
66 - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
67 (priority, weight, port) = struct.unpack('!HHH',
68 wire[current : current + 6])
69 current += 6
70 rdlen -= 6
71 (target, cused) = dns.name.from_wire(wire[: current + rdlen],
72 current)
73 if cused != rdlen:
74 raise dns.exception.FormError
75 if not origin is None:
76 target = target.relativize(origin)
77 return cls(rdclass, rdtype, priority, weight, port, target)
78
79 from_wire = classmethod(from_wire)
80
83
84 - def _cmp(self, other):
85 sp = struct.pack("!HHH", self.priority, self.weight, self.port)
86 op = struct.pack("!HHH", other.priority, other.weight, other.port)
87 v = dns.util.cmp(sp, op)
88 if v == 0:
89 v = dns.util.cmp(self.target, other.target)
90 return v
91