Package dns :: Package rdtypes :: Package IN :: Module SRV
[hide private]
[frames] | no frames]

Source Code for Module dns.rdtypes.IN.SRV

 1  # Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. 
 2  # 
 3  # Permission to use, copy, modify, and distribute this software and its 
 4  # documentation for any purpose with or without fee is hereby granted, 
 5  # provided that the above copyright notice and this permission notice 
 6  # appear in all copies. 
 7  # 
 8  # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES 
 9  # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 
10  # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR 
11  # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 
12  # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
13  # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 
14  # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 
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
81 - def choose_relativity(self, origin = None, relativize = True):
82 self.target = self.target.choose_relativity(origin, relativize)
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