Debian Codegraph @

doc/reveng/pinspect/packethandler.py

raw
1 #!/usr/bin/env python3
2
3 """
4 THis module implements some basic packet handler, that can filter and process packets captured by
5 wireshark.
6 """
7
8 from abc import ABCMeta, abstractmethod
9 from pyshark import FileCapture
10
11
12 class PacketHandler(metaclass=ABCMeta):
13 """ Abstract base class for all packet handlers. """
14
15 def __init__(self):
16 self._handler = []
17
18 def attach(self, handler):
19 self._handler.append(handler)
20 return self
21
22 @staticmethod
23 def isFromHost(p):
24 return "host" == p.usb.src
25
26 @staticmethod
27 def isToHost(p):
28 return "host" == p.usb.dest
29
30 def handle(self, packet):
31 for handler in self._handler:
32 handler.handle(packet)
33
34 def process(self, filename:str):
35 for packet in FileCapture(filename, include_raw=True, use_json=True):
36 self.handle(packet)
37