1# $FreeBSD$ 2 3import threading 4import scapy.all as sp 5 6class Sniffer(threading.Thread): 7 def __init__(self, args, check_function): 8 threading.Thread.__init__(self) 9 10 self._args = args 11 self._recvif = args.recvif[0] 12 self._check_function = check_function 13 self.foundCorrectPacket = False 14 self._endme = False 15 16 self.start() 17 18 def _checkPacket(self, packet): 19 ret = self._check_function(self._args, packet) 20 if ret: 21 self.foundCorrectPacket = True 22 return ret 23 24 def setEnd(self): 25 self._endme = True 26 27 def stopFilter(self, pkt): 28 if pkt is not None: 29 self._checkPacket(pkt) 30 if self.foundCorrectPacket or self._endme: 31 return True 32 else: 33 return False 34 35 def run(self): 36 while True: 37 self.packets = sp.sniff(iface=self._recvif, store=False, 38 stop_filter=self.stopFilter, timeout=90) 39 if self.stopFilter(None): 40 break 41