分析dnspython TLSA 代码

自己刚开始用了好几天的dnspython ,因为没有用过python ,所以感觉比较吃力,想记录下来自己对于dnspython的 TLSA的浅显的理解。理解存在错误在所难免


import struct

import dns.rdata
import dns.rdatatype

class TLSA(dns.rdata.Rdata):
    """TLSA record

    @ivar usage: The certificate usage
    @type usage: int
    @ivar selector: The selector field
    @type selector: int
    @ivar mtype: The 'matching type' field
    @type mtype: int
    @ivar cert: The 'Certificate Association Data' field
    @type cert: string
    @see: RFC 6698"""

    __slots__ = ['usage', 'selector', 'mtype', 'cert']   #TLSA对象的四个属性

    def __init__(self, rdclass, rdtype, usage, selector,
                 mtype, cert):
        super(TLSA, self).__init__(rdclass, rdtype)
        self.usage = usage
        self.selector = selector
        self.mtype = mtype
        self.cert = cert

    def to_text(self, origin=None, relativize=True, **kw): #打印TLSA对象
        return '%d %d %d %s' % (self.usage,
                                self.selector,
                                self.mtype,
                                dns.rdata._hexify(self.cert,
                                               chunksize=128))

    def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):#从传入的参数值构建TLSA对象
        usage = tok.get_uint8() #usage 及另外两个属性都是1字节 的无符号整数
        selector = tok.get_uint8()
        mtype = tok.get_uint8()
        cert_chunks = []
        while 1:
            t = tok.get().unescape()
            if t.is_eol_or_eof():
                break
            if not t.is_identifier():
                raise dns.exception.SyntaxError
            cert_chunks.append(t.value)
        cert = ''.join(cert_chunks)
        cert = cert.decode('hex_codec')
        return cls(rdclass, rdtype, usage, selector, mtype, cert)

    from_text = classmethod(from_text)

    def to_wire(self, file, compress = None, origin = None):#to_wire函数是将对象转换成2进制的串以便于tcp传输
        header = struct.pack("!BBB", self.usage, self.selector, self.mtype)#使用struct pack命令,!表示网络字节序(大端),B对应的是1字节的无符号整数,BBB亦即说明除cert外的三个属性都会以相应的形式被转成2进制
        file.write(header)
        file.write(self.cert)#写入三个属性和cert值后,tlsa文件就转化成了2进制的表示,是不是类似java的序列化

    def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):#将2进制形式转化成TLSA对象,与to_wire相对应
        header = struct.unpack("!BBB", wire[current : current + 3])#3 指的是字节数
        current += 3
        rdlen -= 3
        cert = wire[current : current + rdlen].unwrap()
        return cls(rdclass, rdtype, header[0], header[1], header[2], cert)

    from_wire = classmethod(from_wire)

    def _cmp(self, other): #比较TLSA对象
        hs = struct.pack("!BBB", self.usage, self.selector, self.mtype)
        ho = struct.pack("!BBB", other.usage, other.selector, other.mtype)
        v = cmp(hs, ho)
        if v == 0:
            v = cmp(self.cert, other.cert)
        return v


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值