Beispiel #1
0
	def __init__(self, destaddr, output=None):
		Dispatcher.__init__(self)
		
		self.eventloop = EventLoop()
		
		self.starttime = None
		self.output = output

		self.ready = True
		self.max_ttl = 16
		self.current_ttl = 16
		self.min_ttl = 0
		self.ident = -1
		
		try:		
			sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_UDP)
			sock.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)						
			self.set_socket(sock)
		except socket.error, err:
			if _sockerror(err.args[0]) == 'EPERM':
				logging.root.error('You need sudo privilages to use raw sockets')			
			else:
				log_str = 'Socket Error: %s' % _sockerror(err.args[0])
				logging.root.error(log_str)
			sys.exit(-1)
Beispiel #2
0
	def send(self, data):
		try:
			result = self.sock.send(data)
			return result
		except socket.error, err:
			log_str = 'Could not send data: %s' % _sockerror(err.args[0])
			logging.root.info(log_str)
			
			if err.args[0] == EWOULDBLOCOK:
				return 0
			elif err.args[0] in _disconnected:
				self.handle_close()
				return 0
			else:
				log_str = 'Error: %s' % _sockerror(err.args[0])
				logging.root.error(log_str)
				raise
Beispiel #3
0
	def recv(self, buffer_size):
		try:
			databuffer = self.socket.recv(buffer_size)
			if not databuffer:
				self.handle_close()
				return ''
			else:
				return databuffer
		except socket.error, err:
			log_str = 'Could not recv data: %s' % _sockerror(err.args[0])
			logging.root.info(log_str)

			if err.args[0]  in _disconnected:
				self.handle_close()
				return ''
			else:
				log_str = 'Error: %s' % _sockerror(err.args[0])
				logging.root.error(log_str)
				raise
Beispiel #4
0
	def close(self):
		self.accepting = False
		try:
			if self.sock.fileno() in self.eventloop.socket_table:
				del self.eventloop.socket_table[self.sock.fileno()]			
			self.sock.close()
		except socket.error, err:
			if err.args[0] not in (ENOTCONN, EBADF):
				log_str = 'Error: %s' % _sockerror(err.args[0])
				logging.root.exception(log_str)
Beispiel #5
0
    def __init__(self, destaddr):
        Dispatcher.__init__(self)
        self.eventloop = EventLoop()

        self.destaddr = destaddr
        self.srcaddr = ("0.0.0.0", 1)

        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
            self.set_socket(sock)
            self.bind(self.srcaddr)
        except socket.error, err:
            log_str = "Socket Error: %s" % _sockerror(err.args[0])
            logging.root.exception(log_str)
            sys.exit(-1)