简体   繁体   English

Scapy:添加新协议/层时出现TypeError

[英]Scapy: TypeError when adding new protocol/ layer

I am trying to build a new layer/protocol in Scapy. 我正在尝试在Scapy中构建新的层/协议。 I see this error when I am trying to send/show2 it. 我尝试发送/显示2时看到此错误。

I have put breakpoints to see what is happening but somehow internally the str is getting converted to a Tuple. 我已经设置了断点以查看发生了什么,但是在某种程度上内部将str转换为Tuple。 I am not sure what I am missing. 我不确定我缺少什么。 Can someone help me nail down the issue? 有人可以帮我确定这个问题吗?

>>> p1=PCEPOPEN()
>>> p1.show2()
> /usr/local/lib64/python2.6/site-packages/scapy/fields.py(71)addfield()
-> return s+struct.pack(self.fmt, self.i2m(pkt,val))
(Pdb) val
1
(Pdb) p s
''
(Pdb) c
> /usr/local/lib64/python2.6/site-packages/scapy/fields.py(71)addfield()
-> return s+struct.pack(self.fmt, self.i2m(pkt,val))
(Pdb) p s
'\x01\x10'
(Pdb) val
4
(Pdb) p s
'\x01\x10'
(Pdb) c
> /usr/local/lib64/python2.6/site-packages/scapy/fields.py(71)addfield()
-> return s+struct.pack(self.fmt, self.i2m(pkt,val))
(Pdb) val
10
(Pdb) p s
('\x01\x10\x00\x04', 3, 1L)
(Pdb) c
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib64/python2.6/site-packages/scapy/packet.py", line 831, in show2
    self.__class__(str(self)).show()
  File "/usr/local/lib64/python2.6/site-packages/scapy/packet.py", line 262, in __str__
    return self.build()
  File "/usr/local/lib64/python2.6/site-packages/scapy/packet.py", line 321, in build
    p = self.do_build()
  File "/usr/local/lib64/python2.6/site-packages/scapy/packet.py", line 309, in do_build
    pkt = self.self_build()
  File "/usr/local/lib64/python2.6/site-packages/scapy/packet.py", line 300, in self_build
    p = f.addfield(self, p, val)
  File "/usr/local/lib64/python2.6/site-packages/scapy/fields.py", line 71, in addfield
    return s+struct.pack(self.fmt, self.i2m(pkt,val))
TypeError: can only concatenate tuple (not "str") to tuple
>>> p1.show()
###[ OPEN Object for Open message ]###
  oclass= OPEN
  oType= 1
  resflags=
  pflag=
  iflag=
  obLength= 4
  ver= 1
  kalive= 10
  dead= 40
  sid= 1
>>>      


class PCEPOPEN(Packet):
  """OPEN message to establish a PCEP session"""

  name="OPEN Object for Open message"
  fields_desc = [ByteEnumField("oclass",1,_object_class),
                   BitField("oType",1,4),
                   BitField("resflags", 0, 2),
                   FlagsField("pflag", 0x0, 1, "P"),
                   FlagsField("iflag", 0x0, 1, "I"),
                   ShortField("obLength", 4),
                   BitField("ver",1,3),
                   ByteField("kalive",10),
                   ByteField("dead",40),
                   ByteField("sid",1)]

  def post_build(self, pkt, pay):
    if self.obLength is 4:
      olen = len(pkt) + len(pay)
      pkt = pkt[:2]+struct.pack("!h", olen)
    return pkt+pay

Looks like I accidentally deleted a field which caused the problem. 好像我不小心删除了导致问题的字段。 I had used BitField and added 3 bits but I was had deleted the line which contained BitField with 5 more bits. 我使用了BitField并添加了3位,但是我删除了包含BitField并增加5位的行。 Since the packets are Byte aligned scapy was complaining. 由于数据包是字节对齐的,因此,scapy抱怨。 It was good to learn that :) 很高兴得知:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM