簡體   English   中英

Scapy-圖層的長度字段

[英]Scapy - layer's length field

我正在嘗試使用Scapy構建PTPv2協議。 該協議中的消息類型很少,因此我使用ConditionalField來描述不同的字段選項:

class PTPv2(Packet):
    name = "Precision Time Protocol V2"
    fields_desc = [
        # Header
        BitField('transportSpecific', 1, 4),
        BitEnumField('messageType', 0, 4, Message_Types),
        ByteField('versionPTP', 2),
        LenField('messageLength', None),
        ByteField('subdomainNumber', 0),
        ByteField('empty1', 0),
        XShortField('flags', 0),
        LongField('correction', 0),
        IntField('empty2', 0),
        XLongField('ClockIdentity', 0),
        XShortField('SourcePortId', 0),
        XShortField('sequenceId', 0),
        ByteField('control', 0),
        SignedByteField('logMessagePeriod', 0),

    # SYNC message, messageType=0
        ConditionalField(XBitField('TimestampSec', 0, 48),lambda pkt: pkt.messageType==0),
        ConditionalField(IntField('TimestampNanoSec', 0), lambda pkt: pkt.messageType == 0),

    # Follow up message, messageType=8
        ConditionalField(XBitField('preciseOriginTimestampSec', 0, 48), lambda pkt: pkt.messageType == 8),
        ConditionalField(IntField('preciseOriginTimestampNanoSec', 0), lambda pkt: pkt.messageType == 8)

     # Path delay resp follow up message, messageType=0xA
        ConditionalField(XBitField('responseOriginTimestampSec', 0, 48), lambda pkt: pkt.messageType == 0xA),
        ConditionalField(IntField('responseOriginTimestampNanoSec', 0), lambda pkt: pkt.messageType == 0xA),
        ConditionalField(XLongField('requestingSourcePortIdentity', 0), lambda pkt: pkt.messageType == 0xA),
        ConditionalField(XShortField('requestingSourcePortId', 0), lambda pkt: pkt.messageType == 0xA)

現在,我希望messageLength字段描述圖層的長度,並根據現有字段自動進行計算。 它應該描述PTPv2層中所有字段的長度,從第一個字段開始( transportSpecific )。

我讀了PacketLenFieldFieldLenField ,但似乎它們都描述了一個字段的長度,而不是一組字段的長度(據我所知)。

我還閱讀了有關LenField (它在代碼中寫為類型),但是它計算的是下一層的長度,而不是當前層的長度(因此,在這種情況下始終為0)。

知道我該如何解決嗎?

謝謝!

通常,這是使用post_build回調在scapy中完成的,因此:(您需要將其添加到數據包中)

(摘自inet6.py)

def post_build(self, p, pay):
    # p += pay  # if you also want the payload to be taken into account
    if self.messageLength is None:
        tmp_len = len(p) # edit as you want
        p = p[:2] + struct.pack("!H", tmp_len) + p[4:]  # Adds length as short on bytes 3-4
    return p + pay # edit if previous is changed

然后,您可以相應地使用ByteField / ShortField ...而不是LenField。

PacketLenField是當您使用PacketListFieldFieldLenField是用於FieldListField

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM