From a6dce73e3399b5c50a0adf8be8fb77ac69fb5c07 Mon Sep 17 00:00:00 2001 From: FUJITA Tomonori Date: Thu, 26 Sep 2013 05:08:19 +0900 Subject: [PATCH] packet lib: add get_protocol API get_protocols returns the list of protocols. This is useful for a packet including the same protocol multiple times (e.g. tunneling such GRE). However, it's a rare use case. Instead of 'get_protocols(hoge)[0]', let's do 'get_protocol(hoge)' simply. Signed-off-by: FUJITA Tomonori --- ryu/lib/packet/packet.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ryu/lib/packet/packet.py b/ryu/lib/packet/packet.py index f21edbe2..0661e9b2 100644 --- a/ryu/lib/packet/packet.py +++ b/ryu/lib/packet/packet.py @@ -97,6 +97,15 @@ class Packet(object): assert issubclass(protocol, packet_base.PacketBase) return [p for p in self.protocols if isinstance(p, protocol)] + def get_protocol(self, protocol): + """Returns the firstly found protocol that matches to the + specified protocol. + """ + result = self.get_protocols(protocol) + if len(result) > 0: + return result[0] + return None + def next(self): try: p = self.protocols[self.protocol_idx]