The patch teaches packet library to truncate padding octets.
The protocol class that knows its payload length should set its payload
length as payload_length_ > 0 attributes.
Then, the packet.parser truncates its payload.
payload_length_ is used to avoid name clash with ipv6.payload_length.

Cc: Shaun Crampton <[email protected]>
Signed-off-by: Isaku Yamahata <[email protected]>
---
 ryu/lib/packet/ipv4.py        |    2 ++
 ryu/lib/packet/ipv6.py        |    2 ++
 ryu/lib/packet/lldp.py        |    1 +
 ryu/lib/packet/packet.py      |    4 ++++
 ryu/lib/packet/packet_base.py |    1 +
 ryu/lib/packet/udp.py         |    2 ++
 6 files changed, 12 insertions(+)

diff --git a/ryu/lib/packet/ipv4.py b/ryu/lib/packet/ipv4.py
index 09ed734..f6054f4 100644
--- a/ryu/lib/packet/ipv4.py
+++ b/ryu/lib/packet/ipv4.py
@@ -83,6 +83,8 @@ class ipv4(packet_base.PacketBase):
         self.length = header_length * 4
         self.option = option
 
+        self.payoad_length_ = total_length
+
     @classmethod
     def parser(cls, buf):
         (version, tos, total_length, identification, flags, ttl, proto, csum,
diff --git a/ryu/lib/packet/ipv6.py b/ryu/lib/packet/ipv6.py
index a1cb203..988bcaf 100644
--- a/ryu/lib/packet/ipv6.py
+++ b/ryu/lib/packet/ipv6.py
@@ -65,6 +65,8 @@ class ipv6(packet_base.PacketBase):
         self.dst = dst
         self.length = 40
 
+        self.payload_length_ = payload_length
+
     @classmethod
     def parser(cls, buf):
         (v_tc_flow, plen, nxt, hlim, src, dst) = struct.unpack_from(
diff --git a/ryu/lib/packet/lldp.py b/ryu/lib/packet/lldp.py
index f343359..21373b7 100644
--- a/ryu/lib/packet/lldp.py
+++ b/ryu/lib/packet/lldp.py
@@ -116,6 +116,7 @@ class lldp(packet_base.PacketBase):
             length += LLDP_TLV_SIZE + tlv.len
 
         self.length = length
+        self.payload_length_ = length
 
     # at least it must have chassis id, port id, ttl and end
     def _tlvs_len_valid(self):
diff --git a/ryu/lib/packet/packet.py b/ryu/lib/packet/packet.py
index 84e5bf8..7289865 100644
--- a/ryu/lib/packet/packet.py
+++ b/ryu/lib/packet/packet.py
@@ -47,6 +47,10 @@ class Packet(object):
             if proto:
                 self.parsed_bytes += proto.length
                 self.protocols.append(proto)
+                if proto.payload_length_ > 0:
+                    truncation_length = (self.parsed_bytes +
+                                         proto.payload_length_)
+                    self.data = self.data[:truncation_length]
 
         if len(self.data) > self.parsed_bytes:
             self.protocols.append(self.data[self.parsed_bytes:])
diff --git a/ryu/lib/packet/packet_base.py b/ryu/lib/packet/packet_base.py
index b0aeca0..7472d33 100644
--- a/ryu/lib/packet/packet_base.py
+++ b/ryu/lib/packet/packet_base.py
@@ -37,6 +37,7 @@ class PacketBase(object):
     def __init__(self):
         super(PacketBase, self).__init__()
         self.length = 0
+        self.payload_length_ = 0        # '_' to avoid name clash
         self.protocol_name = self.__class__.__name__
 
     @classmethod
diff --git a/ryu/lib/packet/udp.py b/ryu/lib/packet/udp.py
index c1bb10c..df2eb4f 100644
--- a/ryu/lib/packet/udp.py
+++ b/ryu/lib/packet/udp.py
@@ -49,6 +49,8 @@ class udp(packet_base.PacketBase):
         self.csum = csum
         self.length = udp._MIN_LEN
 
+        self.payload_length_ = total_length
+
     @classmethod
     def parser(cls, buf):
         (src_port, dst_port, total_length, csum) = struct.unpack_from(
-- 
1.7.10.4


------------------------------------------------------------------------------
Get 100% visibility into Java/.NET code with AppDynamics Lite
It's a free troubleshooting tool designed for production
Get down to code-level detail for bottlenecks, with <2% overhead.
Download for free and get started troubleshooting in minutes.
http://p.sf.net/sfu/appdyn_d2d_ap2
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to