用于MPEG2-TS的Scapy层,带有conditionalField用于自适应

时间:2015-04-29 15:47:16

标签: python scapy

我尝试为scapy创建一个MPEG2-TS图层。

我需要"添加"并根据适配字段解析其他字段。

这是我的代码:

_adapt_type = {
0x0 : 'Reserved',
0x1 : 'Payload only',
0x2 : 'Adaptation only',
0x3 : 'Adaptation and payload',
}

class MPEG2_TS(Packet):
    name = "MPEG2-TS"
    fields_desc=[

            ByteEnumField('sync_byte', 0x47, _sync_type),
            BitField('trans_err',0x0, 1),
            # Transport Error Indicator (TEI) if set to 1 decoder ignore packet

            BitField('start_ind',0x0, 1),
            BitField('trans_pri',0x0, 1),
            XBitField('pid', 0xA, 13),
            BitEnumField('trans_scramb', 0x0, 2, _scramble_type),
            BitEnumField('adapt_ctrl', 0x0, 2, _adapt_type),
            BitField('cont_count', 0x0, 4),
            ]

如果adapt_ctrl设置为:

,我需要解析其他数据
0x1 : 'Payload only',
0x2 : 'Adaptation only',
0x3 : 'Adaptation and payload'

如何根据此字段的结果创建一个能够解析它的条件? 如果"有效负载"如果设置了比特,则标题后面没有适配字段,因此我们不需要解析它。 如果"适应和有效载荷"设置好后,我需要使用以下结构解析自适应字段:

_adapt_fields = [

            # Number of bytes in the adaptation field immediately following this byte
            ByteField('ada_len', 0x0),

            # Set to 1 if current TS packet is in a discontinuity state with respect to either the continuity counter or the program clock reference
            BitField('dis', 0x0, 1),

            # Set to 1 if the PES packet in this TS packet starts a video/audio sequence
            BitField('ran_acc', 0x0, 1),

            # 1 = higher priority
            BitField('ES_str_pri', 0x0, 1),

            # Set to 1 if adaptation field contains a PCR field
            BitField('PCR_fla', 0x0, 1),                

            # Set to 1 if adaptation field contains an OPCR field
            BitField('OPCR_fla', 0x0, 1),

            # Set to 1 if adaptation field contains a splice countdown field
            BitField('spl_fla', 0x0, 1),

            # Set to 1 if adaptation field contains private data bytes
            BitField('trs_pri_dat_fla', 0x0, 1),

            # Set to 1 if adaptation field contains an extension
            BitField('ada_ext_fla', 0x0, 1),                

            # variable Depends on flags
            # BitField('ada_ext_fla', 0x0, 1),              
            # PCR   33+6+9      Program clock reference, stored in 6 octets in big-endian as 33 bits base, 6 bits padding, 9 bits extension.
            # OPCR  33+6+9      Original Program clock reference. Helps when one TS is copied into another
            # Splice countdown  8       Indicates how many TS packets from this one a splicing point occurs (may be negative)
            # Stuffing bytes    variable        
            ]

我尝试了一些ConditionalField,但直到现在还没有运气。 谢谢你的帮助。

0 个答案:

没有答案