mike01 / pypacker

:package: The fastest and simplest packet manipulation lib for Python
GNU General Public License v2.0
251 stars 46 forks source link

ICMP Echo - len error #34

Closed samgd closed 7 years ago

samgd commented 7 years ago

Using this package for the first time earlier today: for the code below, the ICMP Echo len returns a value great than the ICMP len and throws an exception when trying to print the Echo. Is this unexpected or am I misunderstanding the API?

from pypacker import psocket
from pypacker.layer3 import ip

psock = psocket.SocketHndl(iface_name='lo', timeout=10)

p = psock.recvp()[0]
print(len(p.ip.icmp), ":", p.ip.icmp)
print(len(p.ip.icmp.echo), ":", p.ip.icmp.echo)

psock.close()

Testing the above using:

$ ping -4 -c 1 -s 0 127.0.0.1

Causes:

$ sudo python test2.py
8 : ICMP(type=8, code=0, sum=BAFB, handler=echo)
12 : Traceback (most recent call last):
  File "/home/sam/.virtualenvs/foo/lib/python3.6/site-packages/pypacker/pypacker.py", line 581, in _unpack
    header_unpacked = self._header_format.unpack(self._header_cached)
struct.error: unpack requires a bytes object of length 12

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test2.py", line 8, in <module>
    print(len(p.ip.icmp.echo), ":", p.ip.icmp.echo)
  File "/home/sam/.virtualenvs/foo/lib/python3.6/site-packages/pypacker/pypacker.py", line 546, in __str__
    return self._summarize()
  File "/home/sam/.virtualenvs/foo/lib/python3.6/site-packages/pypacker/pypacker.py", line 516, in _summarize
    self._unpack()
  File "/home/sam/.virtualenvs/foo/lib/python3.6/site-packages/pypacker/pypacker.py", line 586, in _unpack
    self._header_field_names, self._header_cached, len(self._header_cached)))
Exception: could not unpack in: Echo, format: b'>HHd', names: ['_id', '_seq', '_ts'], value to unpack: b'=\x03\x00\x01' (4 bytes), not enough bytes?
$ python --version
Python 3.6.1
$ pip show pypacker | grep Version                       
Version: 4.0
mike01 commented 7 years ago

Should be fixed now..

samgd commented 7 years ago

Working here - thanks!