5106 lines · 126.3 KB · C
RawDownload
 #ifdef HAVE_NETPACKET_PACKET_H	case AF_PACKET:	{		*len_ret = sizeof (struct sockaddr_ll);		return 1;	}#endif 	/* More cases here... */ 	default:		PyErr_SetString(socket_error, "getsockaddrlen: bad family");		return 0; 	}}  /* s.accept() method */ static PyObject *sock_accept(PySocketSockObject *s){	sock_addr_t addrbuf;	SOCKET_T newfd;	socklen_t addrlen;	PyObject *sock = NULL;	PyObject *addr = NULL;	PyObject *res = NULL;	int timeout; 	if (!getsockaddrlen(s, &addrlen))		return NULL;	memset(&addrbuf, 0, addrlen); #ifdef MS_WINDOWS	newfd = INVALID_SOCKET;#else	newfd = -1;#endif 	if (!IS_SELECTABLE(s))		return select_error(); 	Py_BEGIN_ALLOW_THREADS	timeout = internal_select(s, 0);	if (!timeout)		newfd = accept(s->sock_fd, SAS2SA(&addrbuf), &addrlen);	Py_END_ALLOW_THREADS 	if (timeout == 1) {		PyErr_SetString(socket_timeout, "timed out");		return NULL;	} #ifdef MS_WINDOWS	if (newfd == INVALID_SOCKET)#else	if (newfd < 0)#endif		return s->errorhandler(); 	/* Create the new object with unspecified family,	   to avoid calls to bind() etc. on it. */	sock = (PyObject *) new_sockobject(newfd,					   s->sock_family,					   s->sock_type,					   s->sock_proto); 	if (sock == NULL) {		SOCKETCLOSE(newfd);		goto finally;	}	addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),			    addrlen, s->sock_proto);	if (addr == NULL)		goto finally; 	res = PyTuple_Pack(2, sock, addr); finally:	Py_XDECREF(sock);	Py_XDECREF(addr);	return res;} PyDoc_STRVAR(accept_doc,"accept() -> (socket object, address info)\n\\n\Wait for an incoming connection.  Return a new socket representing the\n\connection, and the address of the client.  For IP sockets, the address\n\info is a pair (hostaddr, port)."); /* s.setblocking(flag) method.  Argument:   False -- non-blocking mode; same as settimeout(0)   True -- blocking mode; same as settimeout(None)*/ static PyObject *sock_setblocking(PySocketSockObject *s, PyObject *arg){	int block; 	block = PyInt_AsLong(arg);	if (block == -1 && PyErr_Occurred())		return NULL; 	s->sock_timeout = block ? -1.0 : 0.0;	internal_setblocking(s, block); 	Py_INCREF(Py_None);	return Py_None;} PyDoc_STRVAR(setblocking_doc,"setblocking(flag)\n\\n\Set the socket to blocking (flag is true) or non-blocking (false).\n\setblocking(True) is equivalent to settimeout(None);\n\setblocking(False) is equivalent to settimeout(0.0)."); /* s.settimeout(timeout) method.  Argument:   None -- no timeout, blocking mode; same as setblocking(True)   0.0  -- non-blocking mode; same as setblocking(False)   > 0  -- timeout mode; operations time out after timeout seconds   < 0  -- illegal; raises an exception*/static PyObject *sock_settimeout(PySocketSockObject *s, PyObject *arg){	double timeout; 	if (arg == Py_None)		timeout = -1.0;	else {		timeout = PyFloat_AsDouble(arg);		if (timeout < 0.0) {			if (!PyErr_Occurred())				PyErr_SetString(PyExc_ValueError,						"Timeout value out of range");			return NULL;		}	} 	s->sock_timeout = timeout;	internal_setblocking(s, timeout < 0.0); 	Py_INCREF(Py_None);	return Py_None;} PyDoc_STRVAR(settimeout_doc,"settimeout(timeout)\n\\n\Set a timeout on socket operations.  'timeout' can be a float,\n\giving in seconds, or None.  Setting a timeout of None disables\n\the timeout feature and is equivalent to setblocking(1).\n\Setting a timeout of zero is the same as setblocking(0)."); /* s.gettimeout() method.   Returns the timeout associated with a socket. */static PyObject *sock_gettimeout(PySocketSockObject *s){	if (s->sock_timeout < 0.0) {		Py_INCREF(Py_None);		return Py_None;	}	else		return PyFloat_FromDouble(s->sock_timeout);} PyDoc_STRVAR(gettimeout_doc,"gettimeout() -> timeout\n\\n\Returns the timeout in floating seconds associated with socket \n\operations. A timeout of None indicates that timeouts on socket \n\operations are disabled."); #ifdef RISCOS/* s.sleeptaskw(1 | 0) method */ static PyObject *sock_sleeptaskw(PySocketSockObject *s,PyObject *arg){	int block;	block = PyInt_AsLong(arg);	if (block == -1 && PyErr_Occurred())		return NULL;	Py_BEGIN_ALLOW_THREADS	socketioctl(s->sock_fd, 0x80046679, (u_long*)&block);	Py_END_ALLOW_THREADS 	Py_INCREF(Py_None);	return Py_None;}PyDoc_STRVAR(sleeptaskw_doc,"sleeptaskw(flag)\n\\n\Allow sleeps in taskwindows.");#endif  /* s.setsockopt() method.   With an integer third argument, sets an integer option.   With a string third argument, sets an option from a buffer;   use optional built-in module 'struct' to encode the string. */ static PyObject *sock_setsockopt(PySocketSockObject *s, PyObject *args){	int level;	int optname;	int res;	char *buf;	int buflen;	int flag; 	if (PyArg_ParseTuple(args, "iii:setsockopt",			     &level, &optname, &flag)) {		buf = (char *) &flag;		buflen = sizeof flag;	}	else {		PyErr_Clear();		if (!PyArg_ParseTuple(args, "iis#:setsockopt",				      &level, &optname, &buf, &buflen))			return NULL;	}	res = setsockopt(s->sock_fd, level, optname, (void *)buf, buflen);	if (res < 0)		return s->errorhandler();	Py_INCREF(Py_None);	return Py_None;} PyDoc_STRVAR(setsockopt_doc,"setsockopt(level, option, value)\n\\n\Set a socket option.  See the Unix manual for level and option.\n\The value argument can either be an integer or a string.");  /* s.getsockopt() method.   With two arguments, retrieves an integer option.   With a third integer argument, retrieves a string buffer of that size;   use optional built-in module 'struct' to decode the string. */ static PyObject *sock_getsockopt(PySocketSockObject *s, PyObject *args){	int level;	int optname;	int res;	PyObject *buf;	socklen_t buflen = 0; #ifdef __BEOS__	/* We have incomplete socket support. */	PyErr_SetString(socket_error, "getsockopt not supported");	return NULL;#else 	if (!PyArg_ParseTuple(args, "ii|i:getsockopt",			      &level, &optname, &buflen))		return NULL; 	if (buflen == 0) {		int flag = 0;		socklen_t flagsize = sizeof flag;		res = getsockopt(s->sock_fd, level, optname,				 (void *)&flag, &flagsize);		if (res < 0)			return s->errorhandler();		return PyInt_FromLong(flag);	}#ifdef __VMS	/* socklen_t is unsigned so no negative test is needed,	   test buflen == 0 is previously done */	if (buflen > 1024) {#else	if (buflen <= 0 || buflen > 1024) {#endif		PyErr_SetString(socket_error,				"getsockopt buflen out of range");		return NULL;	}	buf = PyString_FromStringAndSize((char *)NULL, buflen);	if (buf == NULL)		return NULL;	res = getsockopt(s->sock_fd, level, optname,			 (void *)PyString_AS_STRING(buf), &buflen);	if (res < 0) {		Py_DECREF(buf);		return s->errorhandler();	}	_PyString_Resize(&buf, buflen);	return buf;#endif /* __BEOS__ */} PyDoc_STRVAR(getsockopt_doc,"getsockopt(level, option[, buffersize]) -> value\n\\n\Get a socket option.  See the Unix manual for level and option.\n\If a nonzero buffersize argument is given, the return value is a\n\string of that length; otherwise it is an integer.");  /* s.bind(sockaddr) method */ static PyObject *sock_bind(PySocketSockObject *s, PyObject *addro){	sock_addr_t addrbuf;	int addrlen;	int res; 	if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))		return NULL;	Py_BEGIN_ALLOW_THREADS	res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen);	Py_END_ALLOW_THREADS	if (res < 0)		return s->errorhandler();	Py_INCREF(Py_None);	return Py_None;} PyDoc_STRVAR(bind_doc,"bind(address)\n\\n\Bind the socket to a local address.  For IP sockets, the address is a\n\pair (host, port); the host must refer to the local host. For raw packet\n\sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])");  /* s.close() method.   Set the file descriptor to -1 so operations tried subsequently   will surely fail. */ static PyObject *sock_close(PySocketSockObject *s){	SOCKET_T fd; 	if ((fd = s->sock_fd) != -1) {		s->sock_fd = -1;		Py_BEGIN_ALLOW_THREADS		(void) SOCKETCLOSE(fd);		Py_END_ALLOW_THREADS	}	Py_INCREF(Py_None);	return Py_None;} PyDoc_STRVAR(close_doc,"close()\n\\n\Close the socket.  It cannot be used after this call."); static intinternal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen,		 int *timeoutp){	int res, timeout; 	timeout = 0;	res = connect(s->sock_fd, addr, addrlen); #ifdef MS_WINDOWS 	if (s->sock_timeout > 0.0) {		if (res < 0 && WSAGetLastError() == WSAEWOULDBLOCK &&		    IS_SELECTABLE(s)) {			/* This is a mess.  Best solution: trust select */			fd_set fds;			fd_set fds_exc;			struct timeval tv;			tv.tv_sec = (int)s->sock_timeout;			tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);			FD_ZERO(&fds);			FD_SET(s->sock_fd, &fds);			FD_ZERO(&fds_exc);			FD_SET(s->sock_fd, &fds_exc);			res = select(s->sock_fd+1, NULL, &fds, &fds_exc, &tv);			if (res == 0) {				res = WSAEWOULDBLOCK;				timeout = 1;			} else if (res > 0) {				if (FD_ISSET(s->sock_fd, &fds))					/* The socket is in the writeable set - this					   means connected */					res = 0;				else {					/* As per MS docs, we need to call getsockopt()					   to get the underlying error */					int res_size = sizeof res;					/* It must be in the exception set */					assert(FD_ISSET(s->sock_fd, &fds_exc));					if (0 == getsockopt(s->sock_fd, SOL_SOCKET, SO_ERROR,					                    (char *)&res, &res_size))						/* getsockopt also clears WSAGetLastError,						   so reset it back. */						WSASetLastError(res);					else						res = WSAGetLastError();				}			}			/* else if (res < 0) an error occurred */		}	} 	if (res < 0)		res = WSAGetLastError(); #else 	if (s->sock_timeout > 0.0) {		if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) {			timeout = internal_select(s, 1);			if (timeout == 0) {				res = connect(s->sock_fd, addr, addrlen);				if (res < 0 && errno == EISCONN)					res = 0;			}			else if (timeout == -1)				res = errno;		/* had error */			else				res = EWOULDBLOCK;	/* timed out */		}	} 	if (res < 0)		res = errno; #endif	*timeoutp = timeout; 	return res;} /* s.connect(sockaddr) method */ static PyObject *sock_connect(PySocketSockObject *s, PyObject *addro){	sock_addr_t addrbuf;	int addrlen;	int res;	int timeout; 	if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))		return NULL; 	Py_BEGIN_ALLOW_THREADS	res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);	Py_END_ALLOW_THREADS 	if (timeout == 1) {		PyErr_SetString(socket_timeout, "timed out");		return NULL;	}	if (res != 0)		return s->errorhandler();	Py_INCREF(Py_None);	return Py_None;} PyDoc_STRVAR(connect_doc,"connect(address)\n\\n\Connect the socket to a remote address.  For IP sockets, the address\n\is a pair (host, port).");  /* s.connect_ex(sockaddr) method */ static PyObject *sock_connect_ex(PySocketSockObject *s, PyObject *addro){	sock_addr_t addrbuf;	int addrlen;	int res;	int timeout; 	if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))		return NULL; 	Py_BEGIN_ALLOW_THREADS	res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout);	Py_END_ALLOW_THREADS 	/* Signals are not errors (though they may raise exceptions).  Adapted	   from PyErr_SetFromErrnoWithFilenameObject(). */#ifdef EINTR	if (res == EINTR && PyErr_CheckSignals())		return NULL;#endif

Lines 1501–2000 of 5106. Previous lines