Add ICMP sub encoder/decoder class
 for Destination Unreachable Message and Time Exceeded Message.


Signed-off-by: WATANABE Fumitaka <[email protected]>
---
 ryu/lib/packet/icmp.py |  106 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 106 insertions(+)

diff --git a/ryu/lib/packet/icmp.py b/ryu/lib/packet/icmp.py
index e42fa40..640aa55 100644
--- a/ryu/lib/packet/icmp.py
+++ b/ryu/lib/packet/icmp.py
@@ -24,6 +24,12 @@ ICMP_DEST_UNREACH = 3
 ICMP_SRC_QUENCH = 4
 ICMP_REDIRECT = 5
 ICMP_ECHO_REQUEST = 8
+ICMP_TIME_EXCEEDED = 11
+
+ICMP_ECHO_REPLY_CODE = 0
+ICMP_HOST_UNREACH_CODE = 1
+ICMP_PORT_UNREACH_CODE = 3
+ICMP_TTL_EXPIRED_CODE = 0


 class icmp(packet_base.PacketBase):
@@ -146,3 +152,103 @@ class echo(object):
             hdr += self.data

         return hdr
+
+
[email protected]_icmp_type(ICMP_DEST_UNREACH)
+class dest_unreach(object):
+    """ICMP sub encoder/decoder class for Destination Unreachable Message.
+
+    This is used with ryu.lib.packet.icmp.icmp for
+    ICMP Destination Unreachable Message.
+
+    An instance has the following attributes at least.
+    Most of them are same to the on-wire counterparts but in host byte order.
+    __init__ takes the correspondig args in this order.
+
+    ============== ====================
+    Attribute      Description
+    ============== ====================
+    data_len       data length
+    data           Internet Header + leading octets of original datagram
+    ============== ====================
+    """
+
+    _PACK_STR = '!BBH'
+    _MIN_LEN = struct.calcsize(_PACK_STR)
+
+    def __init__(self, data_len=0, data=None):
+        self.unused = 0
+        self.data_len = data_len
+        self.mtu = 0
+        self.data = data
+
+    @classmethod
+    def parser(cls, buf, offset):
+        (unused, data_len, mtu) = struct.unpack_from(cls._PACK_STR,
+                                                     buf, offset)
+        msg = cls(data_len)
+        offset += cls._MIN_LEN
+
+        if len(buf) > offset:
+            msg.data = buf[offset:]
+
+        return msg
+
+    def serialize(self):
+        hdr = bytearray(struct.pack(dest_unreach._PACK_STR,
+                        self.unused, self.data_len, self.mtu))
+
+        if self.data is not None:
+            hdr += self.data
+
+        return hdr
+
+
[email protected]_icmp_type(ICMP_TIME_EXCEEDED)
+class time_exceeded(object):
+    """ICMP sub encoder/decoder class for Time Exceeded Message.
+
+    This is used with ryu.lib.packet.icmp.icmp for
+    ICMP Time Exceeded Message.
+
+    An instance has the following attributes at least.
+    Most of them are same to the on-wire counterparts but in host byte order.
+    __init__ takes the correspondig args in this order.
+
+    ============== ====================
+    Attribute      Description
+    ============== ====================
+    data_len       data length
+    data           Internet Header + leading octets of original datagram
+    ============== ====================
+    """
+
+    _PACK_STR = '!BBH'
+    _MIN_LEN = struct.calcsize(_PACK_STR)
+
+    def __init__(self, data_len=0, data=None):
+        self.unused = 0
+        self.data_len = data_len
+        self.mtu = 0
+        self.data = data
+
+    @classmethod
+    def parser(cls, buf, offset):
+        (unused, data_len, mtu) = struct.unpack_from(cls._PACK_STR,
+                                                     buf, offset)
+        msg = cls(data_len)
+        offset += cls._MIN_LEN
+
+        if len(buf) > offset:
+            msg.data = buf[offset:]
+
+        return msg
+
+    def serialize(self):
+        hdr = bytearray(struct.pack(time_exceeded._PACK_STR,
+                        self.unused, self.data_len, self.mtu))
+
+        if self.data is not None:
+            hdr += self.data
+
+        return hdr
-- 1.7.10.4


------------------------------------------------------------------------------
Try New Relic Now & We'll Send You this Cool Shirt
New Relic is the only SaaS-based application performance monitoring service 
that delivers powerful full stack analytics. Optimize and monitor your
browser, app, & servers with just a few lines of code. Try New Relic
and get this awesome Nerd Life shirt! http://p.sf.net/sfu/newrelic_d2d_may
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to