Import checksumming from scapy

This commit is contained in:
Nico Schottelius 2019-06-23 13:56:49 +02:00
parent d5bc273111
commit e7769abe31
1 changed files with 23 additions and 0 deletions

View File

@ -0,0 +1,23 @@
#!/usr/bin/env python3
# Code copied from /usr/lib/python3/dist-packages/scapy/utils.py
if struct.pack("H",1) == b"\x00\x01": # big endian
def checksum(pkt):
if len(pkt) % 2 == 1:
pkt += b"\0"
s = sum(array.array("H", pkt))
s = (s >> 16) + (s & 0xffff)
s += s >> 16
s = ~s
return s & 0xffff
else:
def checksum(pkt):
if len(pkt) % 2 == 1:
pkt += b"\0"
s = sum(array.array("H", pkt))
s = (s >> 16) + (s & 0xffff)
s += s >> 16
s = ~s
return (((s>>8)&0xff)|s<<8) & 0xffff