我想知道如何根据另一个类的字段动态定义类的字段类型??? 我的意思是如何定义一个类和另一个类的字段,例如:
class TemplateRecord(Packet):
name = "Template Record"
fields_desc = [ ShortField("Type", 0),
ShortField("Length", 0) ]
def extract_padding(self, s):
return "", s
class DataRecord(Packet):
name = "Data Record"
fields_desc = [ ==> Here i would like something like this :
"if Type == 0 from Class TemplateRecord:
IPField("ipsrc", "0.0.0.0"),
if Type == 1 from Class TemplateRecord
IPField("ipdst", "0.0.0.0"),
if Type == 2 from Class TemplateRecord
ByteField("DstMask", 0),
....etc..."
]
def extract_padding(self, s):
return "", s
答案 0 :(得分:1)
我相信Scapy最接近您想要的功能是Conditional Field。格式为:
ConditionalField(XShortField("chksum",None),lambda pkt:pkt.chksumpresent==1)
尝试将其与Packet.underlayer结合使用。
假设图层设置为TemplateRecord()/ DataRecord()
ConditionalField(IPField("ipsrc", "0.0.0.0"), lambda pkt:pkt.underlayer.Type==0),
ConditionalField(IPField("ipdst", "0.0.0.0"), lambda pkt:pkt.underlayer.Type==1),
ConditionalField(ByteField("DstMask", 0), lambda pkt:pkt.underlayer.Type==2)
答案 1 :(得分:0)
您还可以使用bind_layers函数添加条件图层。 步骤进行:
你的案例的语法是:
bind_layers(TemplateRecord, DataRecordForType1, {'Type':1})