def startListening(self): """ Start listening """ if self.islistening: self.debug('already listening') return # Start the tcp connection self.debug('starting to listen') # log event tpl = self.encapsule(ip_event=AdapterIP.sent(), udp_event=templates.starting()) self.logSentEvent(shortEvt="starting", tplEvt=tpl) self.__mutex__.acquire() try: # set the socket version if self.cfg['sock-family'] == AdapterIP.IPv4: sockType = TestAdapterLib.INIT_DGRAM_SOCKET elif self.cfg['sock-family'] == AdapterIP.IPv6: sockType = TestAdapterLib.INIT6_DGRAM_SOCKET else: raise Exception('socket family unknown: %s' % str(self.cfg['socket-family'])) # Create the socket self.socket = TestAdapterLib.getSocket(sockType=sockType) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.debug('bind socket on %s:%s' % (self.cfg['bind-ip'], self.cfg['bind-port'])) self.socket.bind((self.cfg['bind-ip'], self.cfg['bind-port'])) # listening successful self.__setSource() # dispatch self.lastActivity = time.time() self.islistening = True self.onStartListening() # start thread self.setRunning() except socket.error as e: self.onStartListeningFailed(e) except Exception as e: self.error("start listen error: %s" % str(e)) self.stopListening() self.__mutex__.release()
def startListening(self, eth, srcMac=None): """ Start listening @param eth: device @type eth: string @param srcMac: source mac address @type srcMac: none/string """ if self.sniffing: self.debug('already sniffing') return self.cfg['src-eth'] = eth # log event ether_tpl = templates.ethernet(more=templates.starting( interface=self.cfg['src-eth'])) frame_tpl = self.encapsule(layer_ether=ether_tpl) self.logSentEvent(shortEvt="starting", tplEvt=frame_tpl) if self.cfg['agent-support']: if srcMac is not None: if not isinstance(srcMac, str) and not isinstance( srcMac, unicode): raise Exception( 'Adapter>startListening: bad source mac type: %s' % type(srcMac)) if not len(srcMac): raise Exception( 'Adapter>startListening: source mac address can not be empty' ) self.src_mac = srcMac remote_cfg = { 'cmd': 'connect', 'sock-type': 'raw', 'src-eth': self.cfg['src-eth'] } #self.parent.sendInitToAgent(adapterId=self.getAdapterId(), agentName=self.cfg['agent-name'], agentData=remote_cfg) self.sendNotifyToAgent(data=remote_cfg) self.setRunning() else: try: # Create the socket: ethernet protocol, raw Linux packet socket self.socket = TestAdapterLib.getSocket( sockType=TestAdapterLib.RAW_PACKET_SOCKET) self.socket.bind((eth, socket.SOCK_RAW)) # save source mac addr if srcMac is not None: if not isinstance(srcMac, str) and not isinstance( srcMac, unicode): raise Exception( 'Adapter>startListening: bad source mac type: %s' % type(srcMac)) if not len(srcMac): raise Exception( 'Adapter>startListening: source mac address can not be empty' ) self.src_mac = srcMac else: self.src_mac = ':'.join([ "%02X" % (ord(ch), ) for ch in self.socket.getsockname()[-1] ]) self.debug("source mac: %s" % self.src_mac) self.sniffing = True # start thread self.onStartSniffing() self.setRunning() except socket.error, e: self.onStartSniffingFailed(e) except Exception as e: self.error("listen error: %s" % str(e)) self.stopListening()
def startListening(self): """ Start listening """ if self.islistening: self.debug('already listening') return # Optional: resolve destination hostname if self.cfg['dst-host'] != '': self.cfg['dst-ip'] = self.dns.resolveHost( host=self.cfg['dst-host']) if not len(self.cfg['dst-ip']): return # Start the tcp connection self.debug('starting to listen') # log event tpl = self.encapsule(ip_event=AdapterIP.sent(), udp_event=templates.starting()) self.logSentEvent(shortEvt="starting", tplEvt=tpl) if self.cfg['agent-support']: remote_cfg = { 'cmd': 'connect', 'sock-type': 'udp', 'bind-ip': self.cfg['bind-ip'], 'bind-port': self.cfg['bind-port'], 'sock-family': self.cfg['sock-family'], 'dst-ip': self.cfg['dst-ip'], 'dst-port': self.cfg['dst-port'], } self.sendNotifyToAgent(data=remote_cfg) # start thread self.lastActivity = time.time() self.setRunning() else: self.__mutex__.acquire() try: # set the socket version if self.cfg['sock-family'] == AdapterIP.IPv4: sockType = TestAdapterLib.INIT_DGRAM_SOCKET elif self.cfg['sock-family'] == AdapterIP.IPv6: sockType = TestAdapterLib.INIT6_DGRAM_SOCKET else: raise Exception('socket family unknown: %s' % str(self.cfg['socket-family'])) # Create the socket self.socket = TestAdapterLib.getSocket(sockType=sockType) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.debug('bind socket on %s:%s' % (self.cfg['bind-ip'], self.cfg['bind-port'])) self.socket.bind((self.cfg['bind-ip'], self.cfg['bind-port'])) # listening successful self.__setSource() # dispatch self.lastActivity = time.time() self.islistening = True self.onStartListening() # start thread self.setRunning() except socket.error as e: self.onStartListeningFailed(e) except Exception as e: self.error("start listen error: %s" % str(e)) self.stopListening() self.__mutex__.release()