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 | 109 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/ryu/lib/packet/icmp.py b/ryu/lib/packet/icmp.py index e42fa40..550a076 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): @@ -123,6 +129,7 @@ class echo(object): _MIN_LEN = struct.calcsize(_PACK_STR) def __init__(self, id_, seq, data=None): + super(echo, self).__init__() self.id = id_ self.seq = seq self.data = data @@ -146,3 +153,105 @@ 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): + super(dest_unreach, self).__init__() + 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): + super(time_exceeded, self).__init__() + 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 ------------------------------------------------------------------------------ Introducing AppDynamics Lite, a free troubleshooting tool for Java/.NET Get 100% visibility into your production application - at no cost. Code-level diagnostics for performance bottlenecks with <2% overhead Download for free and get started troubleshooting in minutes. http://p.sf.net/sfu/appdyn_d2d_ap1 _______________________________________________ Ryu-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/ryu-devel
