Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 重复块中的PacketListField长度_Python_Scapy - Fatal编程技术网

Python 重复块中的PacketListField长度

Python 重复块中的PacketListField长度,python,scapy,Python,Scapy,作为我涉及Scapy库的第一个主要项目,我正在尝试实现HEP3/EEP3协议解析器()。我正在努力理解如何分解这些数据块(一个HEP3数据包中可能有许多数据块)。我从docs和google搜索中得到,我应该使用PacketListField,但是我不知道如何获得块的长度,让scapy解析所有的块。以下是我目前掌握的代码: #!/usr/bin/env python3 # -*- coding: utf-8 -*- import scapy.all as sa HEP_CHUNK_HEADE

作为我涉及Scapy库的第一个主要项目,我正在尝试实现HEP3/EEP3协议解析器()。我正在努力理解如何分解这些数据块(一个HEP3数据包中可能有许多数据块)。我从docs和google搜索中得到,我应该使用PacketListField,但是我不知道如何获得块的长度,让scapy解析所有的块。以下是我目前掌握的代码:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import scapy.all as sa


HEP_CHUNK_HEADER = 6


class HEP3Chunk(sa.Packet):
    name = "HEP3Chunk"
    fields_desc = [
        sa.ShortField("chunk_vendor_id", 0),
        sa.ShortField("chunk_type_id", 0),
        sa.ShortField("chunk_length", 0),
        sa.StrLenField("chunk_val", None, length_from=lambda pkt: pkt.chunk1_length - HEP_CHUNK_HEADER),
    ]
    def extract_padding(self, s):
        return "", s

class HEP3(sa.Packet):
    name = "HEP3"
    fields_desc = [
        sa.StrFixedLenField("hep_proto_id", "HEP3", 4),
        sa.ShortField("total_length", 0),
        #sa.FieldLenField("flf", None, length_of="hep_chunk"),
        sa.PacketListField("chunks", None, HEP3Chunk, count_from=lambda pkt: None, length_from=lambda pkt: None),
    ]
到目前为止,结果(硬编码7字节长度)如下:

<HEP3  hep_proto_id='HEP3' total_length=642 chunks=[<Raw  load='\x00\x00\x00\x01\x00\x07\x02' |>] |<Raw  load='\x00\x00\x00\x02\x00\x07\x11\x00\x00\x00\x07\x00\x08\x13\xc4\x00\x00\x00\x08\x00\x08\x13\xc4\x00\x00\x00\t\x00\n[\xd9\x97<\x00\x00\x00\n\x00\n\x00\x04\xc2\xc0\x00\x00\x00\x0b\x00\x07\x01\x00\x00\x00\x0c\x00\n\x00\x00\x04\xd2\x00\x00\x00\x03\x00\n\n\x02\x03\x0e\x00\x00\x00\x04\x00\n\n\x02\x00\xe8\x00\x00\x00\x0e\x00\tfoo\x00\x00\x00\x11\x00\x100-MhCMehI0\x00\x00\x00\x0f\x02\x0cREGISTER sip:10.2.0.232 SIP/2.0\r\nVia: SIP/2.0/UDP 10.2.3.14:5060;branch=z9hG4bK.ALC1d3n1M;rport\r\nFrom: <sip:3000@10.2.0.232>;tag=b92HFYQZe\r\nTo: sip:3000@10.2.0.232\r\nCSeq: 20 REGISTER\r\nCall-ID: 0-MhCMehI0\r\nMax-Forwards: 70\r\nSupported: replaces, outbound\r\nAccept: application/sdp\r\nAccept: text/plain\r\nAccept: application/vnd.gsma.rcs-ft-http+xml\r\nContact: <sip:3000@10.2.3.14;transport=udp>;+sip.instance="<urn:uuid:fddeaf99-e306-4b6f-b8dc-c779ac8988ac>"\r\nExpires: 3600\r\nUser-Agent: Linphone/3.12.0 (belle-sip/1.6.3)\r\n\r\n' |>>
>
你能给我一个如何处理块长度的提示,让所有的块都被读取和解析吗


谢谢。

这里有一些提示

  • 如果未使用中的
    length\u和
    中的
    count\u功能,请不要指定它们
  • 你在HEP3Chunk中所做的很好,你甚至得到了大家都忘记的
    extract\u padding
  • 对于HEP3,您可以设置
    length\u from=lambda pkt:pkt.total\u length-6
    (我不计算注释字段)