我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用socket.SO_RCVBUF。
def connect(self, host, port): self.__socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.__socket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 2 ** 13) self.__socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 2 ** 13) self.__socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) self.__socket.setblocking(0) self.__readBuffer = '' self.__writeBuffer = '' self.__lastReadTime = time.time() try: self.__socket.connect((host, port)) except socket.error as e: if e.errno != socket.errno.EINPROGRESS: return False self.__fileno = self.__socket.fileno() self.__disconnected = False return True
def __onNewConnection(self, localDescr, event): if event & POLL_EVENT_TYPE.READ: try: sock, addr = self.__socket.accept() sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, self.__conf.sendBufferSize) sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, self.__conf.recvBufferSize) sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) sock.setblocking(0) conn = Connection(socket=sock, timeout=self.__conf.connectionTimeout) descr = conn.fileno() self.__unknownConnections[descr] = conn self.__poller.subscribe(descr, self.__processUnknownConnections, POLL_EVENT_TYPE.READ | POLL_EVENT_TYPE.ERROR) except socket.error as e: if e.errno != socket.errno.EAGAIN: self.__isInitialized = False LOG_WARNING('Error in main socket:' + str(e)) if event & POLL_EVENT_TYPE.ERROR: self.__isInitialized = False LOG_WARNING('Error in main socket')
def create_sock(self): sock = socket.socket(socket.AF_INET) sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 32*1024) sock.settimeout(5) try: if not self.http_proxy: if self.use_https: if os.path.isfile(self.cert): sock = ssl.wrap_socket(sock, ca_certs=self.cert, do_handshake_on_connect=True) else: sock = ssl.wrap_socket(sock, do_handshake_on_connect=True) sock.connect(self.address) else: sock.connect(self.http_proxy) except Exception as e: #logging.warn("create connect to %s:%d fail:%r", self.address[0], self.address[1], e) sock.close() return None return sock
def modify_buff_size(): sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM ) # Get the size of the socket's send buffer bufsize = sock.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF) print ("Buffer size [Before]:%d" %bufsize) sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1) sock.setsockopt( socket.SOL_SOCKET, socket.SO_SNDBUF, SEND_BUF_SIZE) sock.setsockopt( socket.SOL_SOCKET, socket.SO_RCVBUF, RECV_BUF_SIZE) bufsize = sock.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF) print ("Buffer size [After]:%d" %bufsize)
def __init__(self): self.wakeup_sock, self.write_sock = socket.socketpair() self.wakeup_sock.setblocking(False) self.write_sock.setblocking(False) # This somewhat reduces the amount of memory wasted queueing up data # for wakeups. With these settings, maximum number of 1-byte sends # before getting BlockingIOError: # Linux 4.8: 6 # MacOS (darwin 15.5): 1 # Windows 10: 525347 # Windows you're weird. (And on Windows setting SNDBUF to 0 makes send # blocking, even on non-blocking sockets, so don't do that.) self.wakeup_sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 1) self.write_sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 1) # On Windows this is a TCP socket so this might matter. On other # platforms this fails b/c AF_UNIX sockets aren't actually TCP. try: self.write_sock.setsockopt( socket.IPPROTO_TCP, socket.TCP_NODELAY, 1 ) except OSError: pass
def run(self): """Run the server continuously.""" s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 6553600) s.settimeout(120) try: s.bind(('', self.port)) except Exception as e: debug_print_error("Binding Error: " + str(e)) self.outQ.put((None, e)) sys.exit(-1) s.listen(1) # only have 1 connection debug_print("Server awaiting connection on port %d" % self.port) conn, _ = s.accept() debug_print("Server Accepted connection") self._handle_connection(conn) s.shutdown(socket.SHUT_RDWR) s.close() debug_print("Shutdown server")
def _testCongestion(self): # test the behavior in case of congestion self.data = b'fill' self.cli.setblocking(False) try: # try to lower the receiver's socket buffer size self.cli.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 16384) except OSError: pass with self.assertRaises(OSError) as cm: try: # fill the receiver's socket buffer while True: self.cli.sendto(self.data, 0, (HOST, self.port)) finally: # signal the receiver we're done self.evt.set() # sendto() should have failed with ENOBUFS self.assertEqual(cm.exception.errno, errno.ENOBUFS) # and we should have received a congestion notification through poll r, w, x = select.select([self.serv], [], [], 3.0) self.assertIn(self.serv, r)
def open(self): """ Open this Endpoint for sending and receiving packets. """ for _ in xrange(10000): try: self._logger.debug("Listening at %d", self._port) self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 870400) self._socket.bind((self._ip, self._port)) self._socket.setblocking(0) self._port = self._socket.getsockname()[1] except socket.error: self._logger.debug("Listening failed at %d", self._port) self._port += 1 continue break self._running = True self._thread = threading.Thread(name="UDPEndpoint", target=self._loop) self._thread.daemon = True self._thread.start() return True
def __init__(self, sock=None, sockMap=None): asyncore.dispatcher.__init__(self) if sock is None: if self.sockFamily is None: raise error.CarrierError( 'Address family %s not supported' % self.__class__.__name__ ) if self.sockType is None: raise error.CarrierError( 'Socket type %s not supported' % self.__class__.__name__ ) try: sock = socket.socket(self.sockFamily, self.sockType) except socket.error: raise error.CarrierError('socket() failed: %s' % sys.exc_info()[1]) try: for b in socket.SO_RCVBUF, socket.SO_SNDBUF: bsize = sock.getsockopt(socket.SOL_SOCKET, b) if bsize < self.bufferSize: sock.setsockopt(socket.SOL_SOCKET, b, self.bufferSize) debug.logger & debug.flagIO and debug.logger('%s: socket %d buffer size increased from %d to %d for buffer %d' % (self.__class__.__name__, sock.fileno(), bsize, self.bufferSize, b)) except Exception: debug.logger & debug.flagIO and debug.logger('%s: socket buffer size option mangling failure for buffer %d: %s' % (self.__class__.__name__, b, sys.exc_info()[1])) # The socket map is managed by the AsyncoreDispatcher on # which this transport is registered. Here we just prepare # socket and postpone transport registration at dispatcher # till AsyncoreDispatcher invokes registerSocket() sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.setblocking(0) self.set_socket(sock)
def __onNewConnection(self, descr, event): if event & POLL_EVENT_TYPE.READ: try: sock, addr = self.__socket.accept() sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, self.__sendBufferSize) sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, self.__recvBufferSize) sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) sock.setblocking(0) conn = TcpConnection(poller=self.__poller, socket=sock, timeout=self.__connectionTimeout, sendBufferSize=self.__sendBufferSize, recvBufferSize=self.__recvBufferSize) self.__onNewConnectionCallback(conn) except socket.error as e: if e.errno not in (socket.errno.EAGAIN, socket.errno.EWOULDBLOCK): self.unbind() return if event & POLL_EVENT_TYPE.ERROR: self.unbind() return
def connect(self, host, port): self.__state = CONNECTION_STATE.DISCONNECTED self.__fileno = None self.__socket = socket.socket(_getAddrType(host), socket.SOCK_STREAM) self.__socket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, self.__sendBufferSize) self.__socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, self.__recvBufferSize) self.__socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) self.__socket.setblocking(0) self.__readBuffer = bytes() self.__writeBuffer = bytes() self.__lastReadTime = time.time() try: self.__socket.connect((host, port)) except socket.error as e: if e.errno not in (socket.errno.EINPROGRESS, socket.errno.EWOULDBLOCK): return False self.__fileno = self.__socket.fileno() self.__state = CONNECTION_STATE.CONNECTING self.__poller.subscribe(self.__fileno, self.__processConnection, POLL_EVENT_TYPE.READ | POLL_EVENT_TYPE.WRITE | POLL_EVENT_TYPE.ERROR) return True
def __init__(self, interface_name, on_ip_incoming, on_ip_outgoing): self.interface_name = interface_name self.on_ip_incoming = on_ip_incoming self.on_ip_outgoing = on_ip_outgoing # The raw in (listen) socket is a L2 raw socket that listens # for all packets going through a specific interface. # SOL_SOCKET: SO_LINGER, SO_RCVBUF, SO_SNDBUF, TCP_NODELAY # SO_LINGER ???????????????????TIME_WAIT????? # ????????????????????tcp?? # SO_RCVBUF?SO_SNDBUF ???????????????? # SO_SNDBUF ?? MSS??????? 2MSS # TCP_NODELAY ?? nagle ?? self.sock = socket.socket( socket.AF_PACKET, socket.SOCK_RAW, socket.htons(ETH_P_IP)) self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 2 ** 30) self.sock.bind((self.interface_name, ETH_P_IP))
def bind_and_activate(self): server_listen_ip = self.orig_server_address[0] try: #???? IPv6 sock = socket.socket(socket.AF_INET6) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # server_address ??????? v6 ? v4 ???'::' ????????? if server_listen_ip == '': sock.setsockopt(IPPROTO_IPV6, socket.IPV6_V6ONLY, 0) sock.bind(self.orig_server_address) except: sock.close() sock = socket.socket(socket.AF_INET) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind(self.orig_server_address) #???????????? if sys.platform != 'darwin' and server_listen_ip in ('127.0.0.1', '::1'): sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 0) #?? nagle's algorithm ?? sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, True) self.socket = sock self.server_address = sock.getsockname() sock.listen(self.request_queue_size) self.is_not_online = False
def __init__(self): self._txMutex = threading.Lock() OSCAddressSpace.__init__(self) self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, self.sndbuf_size) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, self.rcvbuf_size) self.socket.settimeout(1.0) self._running = False
def handle_write(self): if self.needs_config: self.needs_config = False self.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) self.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 128 * 1024) self.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 128 * 1024) if len(self.buffer) > 0: sent = self.send(self.buffer) logging.debug('[{0:d}] TCP => {1:d} byte(s)'.format(self.client_id, sent)) self.buffer = self.buffer[sent:] if self.needs_close and len(self.buffer) == 0: self.needs_close = False self.handle_close()
def __init__(self, connected_socket, client_id): global options asyncore.dispatcher.__init__(self, connected_socket) self.client_id = client_id self.state = self.STATE_WAITING_FOR_HANDSHAKE self.ip = None self.addresses = None self.hostname = None self.port = None self.requested_address = None self.buffer = '' self.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) self.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 128 * 1024) self.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 128 * 1024) self.needs_close = False
def mkcmdsock(ip_address=None, port=0): "Create a command socket." ip_address = gethostip() if not ip_address else ip_address cmdsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0) cmdsock.bind((ip_address, port)) cmdsock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) cmdsock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, SOCKET_BUFSIZE) return cmdsock
def mkdatasock(ip_address=None, multicast_address=MULTICAST_ADDRESS, port=PORT_DATA): "Create a data socket." ip_address = gethostip() if not ip_address else ip_address datasock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0) datasock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) datasock.bind((ip_address, port)) # join a multicast group mreq = struct.pack("=4sl", socket.inet_aton(multicast_address), socket.INADDR_ANY) datasock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq) datasock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, SOCKET_BUFSIZE) return datasock
def set_buf_sizes(self, recv_size, send_size): self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, recv_size) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, send_size) return self.get_buf_sizes()
def get_buf_sizes(self): recv = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF) send = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF) return recv,send
def __init__(self, type = ETH_P_ALL, filter=None, promisc=None, iface=None, nofilter=0): self.type = type self.ins = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(type)) self.ins.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 0) _flush_fd(self.ins) if iface: self.ins.bind((iface, type)) if not nofilter: if conf.except_filter: if filter: filter = "(%s) and not (%s)" % (filter, conf.except_filter) else: filter = "not (%s)" % conf.except_filter if filter is not None: attach_filter(self.ins, filter) self.ins.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 2**30) self.outs = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(type)) self.outs.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 2**30) if promisc is None: promisc = conf.promisc self.promisc = promisc if self.promisc: if iface is None: self.iff = get_if_list() else: if iface.__class__ is list: self.iff = iface else: self.iff = [iface] for i in self.iff: set_promisc(self.ins, i)
def __init__(self, iface = None, type = ETH_P_ALL, filter=None, nofilter=0): if iface is None: iface = conf.iface self.ins = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(type)) self.ins.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 0) _flush_fd(self.ins) if not nofilter: if conf.except_filter: if filter: filter = "(%s) and not (%s)" % (filter, conf.except_filter) else: filter = "not (%s)" % conf.except_filter if filter is not None: attach_filter(self.ins, filter) self.ins.bind((iface, type)) self.ins.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 2**30) self.outs = self.ins self.outs.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 2**30) sa_ll = self.outs.getsockname() if sa_ll[3] in conf.l2types: self.LL = conf.l2types[sa_ll[3]] elif sa_ll[1] in conf.l3types: self.LL = conf.l3types[sa_ll[1]] else: self.LL = conf.default_l2 warning("Unable to guess type (interface=%s protocol=%#x family=%i). Using %s" % (sa_ll[0],sa_ll[1],sa_ll[3],self.LL.name))
def __init__(self, iface = None, type = ETH_P_ALL, promisc=None, filter=None, nofilter=0): self.type = type self.outs = None self.ins = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(type)) self.ins.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 0) _flush_fd(self.ins) if iface is not None: self.ins.bind((iface, type)) if not nofilter: if conf.except_filter: if filter: filter = "(%s) and not (%s)" % (filter, conf.except_filter) else: filter = "not (%s)" % conf.except_filter if filter is not None: attach_filter(self.ins, filter) if promisc is None: promisc = conf.sniff_promisc self.promisc = promisc if iface is None: self.iff = get_if_list() else: if iface.__class__ is list: self.iff = iface else: self.iff = [iface] if self.promisc: for i in self.iff: set_promisc(self.ins, i) self.ins.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 2**30)
def _create_remote_socket(self, ip, port): if self._remote_udp: addrs_v6 = socket.getaddrinfo("::", 0, 0, socket.SOCK_DGRAM, socket.SOL_UDP) addrs = socket.getaddrinfo("0.0.0.0", 0, 0, socket.SOCK_DGRAM, socket.SOL_UDP) else: addrs = socket.getaddrinfo(ip, port, 0, socket.SOCK_STREAM, socket.SOL_TCP) if len(addrs) == 0: raise Exception("getaddrinfo failed for %s:%d" % (ip, port)) af, socktype, proto, canonname, sa = addrs[0] if self._forbidden_iplist: if common.to_str(sa[0]) in self._forbidden_iplist: raise Exception('IP %s is in forbidden list, reject' % common.to_str(sa[0])) remote_sock = socket.socket(af, socktype, proto) self._remote_sock = remote_sock self._fd_to_handlers[remote_sock.fileno()] = self if self._remote_udp: af, socktype, proto, canonname, sa = addrs_v6[0] remote_sock_v6 = socket.socket(af, socktype, proto) self._remote_sock_v6 = remote_sock_v6 self._fd_to_handlers[remote_sock_v6.fileno()] = self remote_sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 1024 * 32) remote_sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 1024 * 32) remote_sock_v6.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 1024 * 32) remote_sock_v6.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 1024 * 32) remote_sock.setblocking(False) if self._remote_udp: remote_sock_v6.setblocking(False) else: remote_sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1) return remote_sock
def receiver(service='mihome'): from plugins import gateway assert service in MULTICAST, 'No such service' store = get_store() address, port = MULTICAST.get(service) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind(("0.0.0.0", port)) mreq = struct.pack("=4sl", socket.inet_aton(address), socket.INADDR_ANY) sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 32) sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 1) sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, SOCKET_BUFSIZE) sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq) current = {} while True: data, _ = sock.recvfrom(SOCKET_BUFSIZE) # buffer size is 1024 bytes print(datetime.now().isoformat(), data) if service == 'mihome': message = json.loads(data.decode()) data = json.loads(message['data']) if message.get('model') in ('sensor_ht', 'weather.v1') and not sensor_ht.process(conn, cursor, current, message, data): continue elif message.get('model') == 'magnet': magnet.process(store, message, data) elif message.get('model') == 'gateway': gateway.process(store, message, data) current = {} elif service == 'yeelight': yeelight.process(data.decode())
def __bind(self): self.__socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.__socket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, self.__conf.sendBufferSize) self.__socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, self.__conf.recvBufferSize) self.__socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) self.__socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.__socket.setblocking(0) host, port = self.__selfNodeAddr.split(':') self.__socket.bind((host, int(port))) self.__socket.listen(5) self.__poller.subscribe(self.__socket.fileno(), self.__onNewConnection, POLL_EVENT_TYPE.READ | POLL_EVENT_TYPE.ERROR)
def set_sock_buff_size(sock): try: sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1) sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, BUFFER_SIZE) sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, BUFFER_SIZE) except Exception, e: logger = logging.getLogger("logger") logger.error(traceback.format_exc())
def do_connect(self, host, port): self.xlog.info("session_id:%s create_conn %d %s:%d", self.session.session_id, self.conn_id, host, port) connect_timeout = 30 sock = None # start_time = time.time() ip = "" try: if ':' in host: # IPV6 ip = host elif utils.check_ip_valid(host): # IPV4 ip = host else: # xlog.debug("getting ip of %s", host) ip = socket.gethostbyname(host) # xlog.debug("resolve %s to %s", host, ip) sock = socket.socket(socket.AF_INET if ':' not in ip else socket.AF_INET6) # set reuseaddr option to avoid 10048 socket error sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # resize socket recv buffer 8K->32K to improve browser releated application performance sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 32 * 1024) # disable negal algorithm to send http request quickly. sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, True) # set a short timeout to trigger timeout retry more quickly. sock.settimeout(connect_timeout) sock.connect((ip, port)) # record TCP connection time # conn_time = time.time() - start_time # xlog.debug("tcp conn %s %s time:%d", host, ip, conn_time * 1000) return sock, True except Exception as e: # conn_time = int((time.time() - start_time) * 1000) # xlog.debug("tcp conn host:%s %s:%d fail t:%d %r", host, ip, port, conn_time, e) if sock: sock.close() return e, False
def __init__(self, type = ETH_P_ALL, filter=None, promisc=None, iface=None, nofilter=0): self.type = type self.ins = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(type)) self.ins.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 0) if iface: self.ins.bind((iface, type)) if not nofilter: if conf.except_filter: if filter: filter = "(%s) and not (%s)" % (filter, conf.except_filter) else: filter = "not (%s)" % conf.except_filter if filter is not None: attach_filter(self.ins, filter) _flush_fd(self.ins) self.ins.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 2**30) self.outs = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(type)) self.outs.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 2**30) if promisc is None: promisc = conf.promisc self.promisc = promisc if self.promisc: if iface is None: self.iff = get_if_list() else: if iface.__class__ is list: self.iff = iface else: self.iff = [iface] for i in self.iff: set_promisc(self.ins, i)
def __init__(self, iface = None, type = ETH_P_ALL, filter=None, nofilter=0): if iface is None: iface = conf.iface self.ins = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(type)) self.ins.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 0) if not nofilter: if conf.except_filter: if filter: filter = "(%s) and not (%s)" % (filter, conf.except_filter) else: filter = "not (%s)" % conf.except_filter if filter is not None: attach_filter(self.ins, filter) self.ins.bind((iface, type)) _flush_fd(self.ins) self.ins.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 2**30) self.outs = self.ins self.outs.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 2**30) sa_ll = self.outs.getsockname() if sa_ll[3] in conf.l2types: self.LL = conf.l2types[sa_ll[3]] elif sa_ll[1] in conf.l3types: self.LL = conf.l3types[sa_ll[1]] else: self.LL = conf.default_l2 warning("Unable to guess type (interface=%s protocol=%#x family=%i). Using %s" % (sa_ll[0],sa_ll[1],sa_ll[3],self.LL.name))
def __init__(self, iface = None, type = ETH_P_ALL, promisc=None, filter=None, nofilter=0): self.type = type self.outs = None self.ins = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(type)) self.ins.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 0) if iface is not None: self.ins.bind((iface, type)) if not nofilter: if conf.except_filter: if filter: filter = "(%s) and not (%s)" % (filter, conf.except_filter) else: filter = "not (%s)" % conf.except_filter if filter is not None: attach_filter(self.ins, filter) if promisc is None: promisc = conf.sniff_promisc self.promisc = promisc if iface is None: self.iff = get_if_list() else: if iface.__class__ is list: self.iff = iface else: self.iff = [iface] if self.promisc: for i in self.iff: set_promisc(self.ins, i) _flush_fd(self.ins) self.ins.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 2**30)
def __init__(self, sock, control): asyncore.dispatcher.__init__(self, sock) if self.socket: self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 4096) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 4096) self.set_reuse_addr() self.control = control self.port = control.port self.out_buffer = [] if not self._repr: self._repr = '<unbound>'