packet lib: set _MIN_LEN class variable

All protocols need to check if the data length is long enough so let's
set the minimum length in the same way.

Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
FUJITA Tomonori 2012-08-28 08:13:50 +09:00
parent 4d00bc8025
commit 77df140ecf
6 changed files with 12 additions and 9 deletions

View File

@ -19,6 +19,7 @@ from . import packet_base
class arp(packet_base.PacketBase):
_PACK_STR = '!HHBBH6sI6sI'
_MIN_LEN = struct.calcsize(_PACK_STR)
def __init__(self, hwtype, proto, hlen, plen, opcode,
src_mac, src_ip, dst_mac, dst_ip):
@ -32,7 +33,7 @@ class arp(packet_base.PacketBase):
self.src_ip = src_ip
self.dst_mac = dst_mac
self.dst_ip = dst_ip
self.length = struct.calcsize(arp._PACK_STR)
self.length = arp._MIN_LEN
@classmethod
def parser(cls, buf):

View File

@ -21,13 +21,14 @@ from ryu.ofproto import ether
class ethernet(packet_base.PacketBase):
_PACK_STR = '!6s6sH'
_MIN_LEN = struct.calcsize(_PACK_STR)
def __init__(self, dst, src, ethertype):
super(ethernet, self).__init__()
self.dst = dst
self.src = src
self.ethertype = ethertype
self.length = struct.calcsize(ethernet._PACK_STR)
self.length = ethernet._MIN_LEN
@classmethod
def parser(cls, buf):

View File

@ -23,6 +23,7 @@ from ryu.ofproto import inet
class ipv4(packet_base.PacketBase):
_PACK_STR = '!BBHHHBBHII'
_MIN_LEN = struct.calcsize(_PACK_STR)
def __init__(self, version, header_length, tos, total_length,
identification, flags, offset, ttl, proto, csum,
@ -53,8 +54,8 @@ class ipv4(packet_base.PacketBase):
msg = cls(version, header_length, tos, total_length, identification,
flags, offset, ttl, proto, csum, src, dst)
if msg.length > struct.calcsize(ipv4._PACK_STR):
self.extra = buf[struct.calcsize(ipv4._PACK_STR):msg.length]
if msg.length > ipv4._MIN_LEN:
self.extra = buf[ipv4._MIN_LEN:msg.length]
return msg, ipv4.get_packet_type(proto)

View File

@ -22,6 +22,7 @@ import ipv4
class tcp(packet_base.PacketBase):
_PACK_STR = '!HHIIBBHHH'
_MIN_LEN = struct.calcsize(_PACK_STR)
def __init__(self, src_port, dst_port, seq, ack, offset,
bits, window_size, csum, urgent):

View File

@ -22,6 +22,7 @@ import ipv4
class udp(packet_base.PacketBase):
_PACK_STR = '!HHHH'
_MIN_LEN = struct.calcsize(_PACK_STR)
def __init__(self, src_port, dst_port, length, csum=0):
super(udp, self).__init__()
@ -35,14 +36,11 @@ class udp(packet_base.PacketBase):
(src_port, dst_port, length, csum) = struct.unpack_from(cls._PACK_STR,
buf)
msg = cls(src_port, dst_port, length, csum)
if length > struct.calcsize(cls._PACK_STR):
msg.data = buf[struct.calcsize(cls._PACK_STR):length]
return msg, None
def serialize(self, payload, prev):
if self.length == 0:
self.length = struct.calcsize(udp._PACK_STR) + len(payload)
self.length = udp._MIN_LEN + len(payload)
h = struct.pack(udp._PACK_STR, self.src_port, self.dst_port,
self.length, self.csum)
if self.csum == 0:

View File

@ -23,6 +23,7 @@ from ryu.ofproto.ofproto_parser import msg_pack_into
class vlan(packet_base.PacketBase):
_PACK_STR = "!HH"
_MIN_LEN = struct.calcsize(_PACK_STR)
def __init__(self, pcp, cfi, vid, ethertype):
super(vlan, self).__init__()
@ -30,7 +31,7 @@ class vlan(packet_base.PacketBase):
self.cfi = cfi
self.vid = vid
self.ethertype = ethertype
self.length = struct.calcsize(vlan._PACK_STR)
self.length = vlan._MIN_LEN
@classmethod
def parser(cls, buf):