packet lib: calculate the udp length

If the length is not given, calculate it.

Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
FUJITA Tomonori 2012-08-27 11:37:26 +09:00
parent 79ae375136
commit 4e887d03c8

View File

@ -20,13 +20,12 @@ from . import packet_base
class udp(packet_base.PacketBase):
_PACK_STR = '!HHHH'
def __init__(self, src_port, dst_port, length, csum=0, data=None):
def __init__(self, src_port, dst_port, length, csum=0):
super(udp, self).__init__()
self.src_port = src_port
self.dst_port = dst_port
self.length = length
self.csum = csum
self.data = data
@classmethod
def parser(cls, buf):
@ -39,5 +38,7 @@ class udp(packet_base.PacketBase):
return msg, None
def serialize(self, payload, prev):
if self.length == 0:
self.length = struct.calcsize(udp._PACK_STR) + len(payload)
return struct.pack(udp._PACK_STR, self.src_port, self.dst_port,
self.length, self.csum)