lib/packet/packet_utils: improve checksum padding

IP checksum needs padding.
Move padding logic into checksum from caller.

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
Isaku Yamahata 2013-03-29 12:50:57 +09:00 committed by FUJITA Tomonori
parent 01798a7439
commit d0c5c14ad8
5 changed files with 3 additions and 8 deletions

View File

@ -72,8 +72,6 @@ class icmp(packet_base.PacketBase):
hdr += self.data
if self.csum == 0:
if len(hdr) % 2:
hdr += '\0'
self.csum = socket.htons(packet_utils.checksum(hdr))
struct.pack_into('!H', hdr, 2, self.csum)

View File

@ -107,8 +107,6 @@ class icmpv6(packet_base.PacketBase):
ph = struct.pack('!16s16sBBH', prev.src, prev.dst, 0, prev.nxt,
length)
f = ph + hdr + payload
if len(f) % 2:
f += '\x00'
self.csum = socket.htons(packet_utils.checksum(f))
struct.pack_into('!H', hdr, 2, self.csum)

View File

@ -20,6 +20,9 @@ def carry_around_add(a, b):
def checksum(data):
if len(data) % 2:
data += '\x00'
s = 0
for i in range(0, len(data), 2):
w = data[i] + (data[i + 1] << 8)

View File

@ -72,8 +72,6 @@ class tcp(packet_base.PacketBase):
ph = struct.pack('!16s16sBBH', prev.src, prev.dst, 0, 6,
length)
f = ph + h + payload
if len(f) % 2:
f += '\x00'
self.csum = socket.htons(packet_utils.checksum(f))
struct.pack_into('!H', h, 16, self.csum)
return h

View File

@ -48,8 +48,6 @@ class udp(packet_base.PacketBase):
ph = struct.pack('!IIBBH', prev.src, prev.dst, 0, 17,
self.total_length)
f = ph + h + payload
if len(f) % 2:
f += '\x00'
self.csum = socket.htons(packet_utils.checksum(f))
h = struct.pack(udp._PACK_STR, self.src_port, self.dst_port,
self.total_length, self.csum)