我正在尝试使用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)开始。
我读到过PacketLenField和FieldLenField,但它们似乎都描述了一个字段的长度,而不是一组字段(据我所知)。
我也读到过LenField (它在代码中写为类型),但它计算的是下一层的长度,而不是当前层的长度(所以在这种情况下总是给0)。
你知道我该怎么解决这个问题吗?
谢谢!
发布于 2019-01-11 01:39:19
这通常是使用post_build回调来完成的,因此:(您需要将其添加到您的包中)
(摘自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。
使用PacketListField时使用PacketLenField,使用FieldListField时使用FieldLenField
https://stackoverflow.com/questions/54125711
复制相似问题