kbandla / dpkt

fast, simple packet creation / parsing, with definitions for the basic TCP/IP protocols
Other
1.08k stars 270 forks source link

a bug in IPv6 Packet's length calculation #563

Closed galosss closed 3 years ago

galosss commented 3 years ago

Hey there,

there's a bug in IPv6 packets length calculation, using len(packet). IPv6 packet uses the default __len__ implementation of dpkt.Packet, but doesn't take the extensions header into account. It's leading to a bug in length calculation when extensions headers are used, and len(bytes(packet)) > len(packet).

thanks.

galosss commented 3 years ago

the fix could be

def __len__(self):
    return super().__len__() + len(self.headers_str()[1])
obormot commented 3 years ago

Hi @galosss please see if the referenced PR https://github.com/kbandla/dpkt/pull/566 fixes the issue

galosss commented 3 years ago

yeah it looks good. tnx!

why do you prefer clone some extension headers parse code rather than use len(self.headers_str()[1]) as suggested above?

thanks, Gal

obormot commented 3 years ago

@galosss Thanks! In general we want to avoid serialization (or packing into str/bytes), when we're only trying to calculate the size. My solution does repeat some code but avoids serialization.