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