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

时间:2014-10-17 22:26:03

标签: python scapy

我正在尝试在Scapy中构建一个新的图层/协议。当我尝试发送/ show2时,我看到了这个错误。

我已经设置断点来查看正在发生的事情,但不知何故内部str正在转换为元组。我不确定我错过了什么。有人可以帮我解决这个问题吗?

>>> 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

1 个答案:

答案 0 :(得分:1)

看起来我不小心删除了导致问题的字段。我曾经使用BitField并添加了3位,但是我删除了包含BitField的行,还有5位。由于数据包是Byte对齐scapy抱怨。很高兴得知:)