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 <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
FUJITA Tomonori 2013-09-26 05:08:19 +09:00
parent 239f4c7122
commit a6dce73e33

View File

@ -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]