Ticket #2276: dns.py.patch

File dns.py.patch, 4.3 KB (added by agatonsax, 9 years ago)

Patch to names/common.oy

  • dns.py

     
    6969 RP, AFSDB) = range(1, 19)
    7070AAAA = 28
    7171SRV = 33
     72NAPTR = 35
    7273A6 = 38
    7374DNAME = 39
    7475
     
    9697
    9798    AAAA: 'AAAA',
    9899    SRV: 'SRV',
    99 
     100        NAPTR: 'NAPTR',
    100101    A6: 'A6',
    101102    DNAME: 'DNAME'
    102103}
     
    212213        encoded in the data is it necessary.
    213214        """
    214215
     216class Charstr:
     217    implements(IEncodable)
     218
     219    def __init__(self, string=''):
     220        assert isinstance(string, types.StringTypes), "%r is not a string" % (name,)
     221        self.string = string
     222
     223    def encode(self, strio, compDict=None):
     224        """
     225        Encode this Character string into the appropriate byte format.
     226
     227        @type strio: file
     228        @param strio: The byte representation of this Charstr will be written to
     229        this file.
     230
     231        @type compDict: dict
     232        @param compDict: dictionary of Charstrs that have already been encoded
     233        and whose addresses may be backreferenced by this Charstr (for the purpose
     234        of reducing the message size).
     235        """
     236        string = self.string
     237        if compDict is not None:
     238            if compDict.has_key(string):
     239                strio.write(
     240                    struct.pack("!H", 0xc000 | compDict[string]))
     241                return
     242            else:
     243                compDict[string] = strio.tell() + Message.headerSize
     244
     245        ind = len(string)
     246        strio.write(chr(ind))
     247        strio.write(string)
     248
     249    def decode(self, strio, length = None):
     250        """
     251        Decode a byte string into this Name.
     252
     253        @type strio: file
     254        @param strio: Bytes will be read from this file until the full Name
     255        is decoded.
     256
     257        @raise EOFError: Raised when there are not enough bytes available
     258        from C{strio}.
     259        """
     260        self.string = ''
     261        off = 0
     262        l = ord(readPrecisely(strio, 1))
     263        if l != 0:
     264            self.string = readPrecisely(strio, l)
     265
     266    def __eq__(self, other):
     267        if isinstance(other, Charstr):
     268            return self.string == other.string
     269        return 0
     270
     271    def __str__(self):
     272        return self.string
    215273
    216274class Name:
    217275    implements(IEncodable)
     
    738796        self.target.decode(strio)
    739797
    740798
    741     def __hash__(self):
    742         return hash((self.priority, self.weight, self.port, self.target))
     799def __hash__(self):
     800    return hash((self.priority, self.weight, self.port, self.target))
     801
    743802
     803    class Record_NAPTR(tputil.FancyEqMixin, tputil.FancyStrMixin):                # EXPERIMENTAL
     804        implements(IEncodable, IRecord)
     805        TYPE = NAPTR
     806
     807        compareAttributes = ('order', 'preference', 'flags', 'service', 'regexp', "replacement")
     808        showAttributes = ('order', 'preference', ('flags', 'flags', '%s'),('service', 'service', '%s'),('regexp', 'regexp', '%s'),('replacement', 'replacement', '%s'),'ttl')
     809
     810        def __init__(self, order=0, preference=0, flags='', service='', regexp='', replacement='', ttl=None):
     811            self.order = int(order)
     812            self.preference = int(preference)
     813            self.flags = Charstr(flags)
     814            self.service = Charstr(service)
     815            self.regexp = Charstr(regexp)
     816            self.replacement = Name(replacement)
     817            self.ttl = str2time(ttl)
     818
     819        def encode(self, strio, compDict = None):
     820            strio.write(struct.pack('!HH', self.order, self.preference))
     821            # This can't be compressed
     822            self.flags.encode(strio, None)
     823            self.service.encode(strio, None)
     824            self.regexp.encode(strio, None)
     825            self.replacement.encode(strio, None)
     826
     827        def decode(self, strio, length = None):
     828            r = struct.unpack('!HH', readPrecisely(strio, struct.calcsize('!HH')))
     829            self.order, self.preference = r
     830            self.flags = Charstr()
     831            self.service = Charstr()
     832            self.regexp = Charstr()
     833            self.replacement = Name()
     834            self.flags.decode(strio)
     835            self.service.decode(strio)
     836            self.regexp.decode(strio)
     837            self.replacement.decode(strio)
     838
     839        def __hash__(self):
     840            return hash((
     841                self.order, self.preference, self.flags,
     842                self.service, self.regexp, self.replacement))
    744843
    745844
    746845class Record_AFSDB(tputil.FancyStrMixin, tputil.FancyEqMixin):