5106 lines · 126.3 KB · C
RawDownload
 	return PyInt_FromLong((long) res);} PyDoc_STRVAR(connect_ex_doc,"connect_ex(address) -> errno\n\\n\This is like connect(address), but returns an error code (the errno value)\n\instead of raising an exception when an error occurs.");  /* s.fileno() method */ static PyObject *sock_fileno(PySocketSockObject *s){#if SIZEOF_SOCKET_T <= SIZEOF_LONG	return PyInt_FromLong((long) s->sock_fd);#else	return PyLong_FromLongLong((PY_LONG_LONG)s->sock_fd);#endif} PyDoc_STRVAR(fileno_doc,"fileno() -> integer\n\\n\Return the integer file descriptor of the socket.");  #ifndef NO_DUP/* s.dup() method */ static PyObject *sock_dup(PySocketSockObject *s){	SOCKET_T newfd;	PyObject *sock; 	newfd = dup(s->sock_fd);	if (newfd < 0)		return s->errorhandler();	sock = (PyObject *) new_sockobject(newfd,					   s->sock_family,					   s->sock_type,					   s->sock_proto);	if (sock == NULL)		SOCKETCLOSE(newfd);	return sock;} PyDoc_STRVAR(dup_doc,"dup() -> socket object\n\\n\Return a new socket object connected to the same system resource."); #endif  /* s.getsockname() method */ static PyObject *sock_getsockname(PySocketSockObject *s){	sock_addr_t addrbuf;	int res;	socklen_t addrlen; 	if (!getsockaddrlen(s, &addrlen))		return NULL;	memset(&addrbuf, 0, addrlen);	Py_BEGIN_ALLOW_THREADS	res = getsockname(s->sock_fd, SAS2SA(&addrbuf), &addrlen);	Py_END_ALLOW_THREADS	if (res < 0)		return s->errorhandler();	return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,			    s->sock_proto);} PyDoc_STRVAR(getsockname_doc,"getsockname() -> address info\n\\n\Return the address of the local endpoint.  For IP sockets, the address\n\info is a pair (hostaddr, port).");  #ifdef HAVE_GETPEERNAME		/* Cray APP doesn't have this :-( *//* s.getpeername() method */ static PyObject *sock_getpeername(PySocketSockObject *s){	sock_addr_t addrbuf;	int res;	socklen_t addrlen; 	if (!getsockaddrlen(s, &addrlen))		return NULL;	memset(&addrbuf, 0, addrlen);	Py_BEGIN_ALLOW_THREADS	res = getpeername(s->sock_fd, SAS2SA(&addrbuf), &addrlen);	Py_END_ALLOW_THREADS	if (res < 0)		return s->errorhandler();	return makesockaddr(s->sock_fd, SAS2SA(&addrbuf), addrlen,			    s->sock_proto);} PyDoc_STRVAR(getpeername_doc,"getpeername() -> address info\n\\n\Return the address of the remote endpoint.  For IP sockets, the address\n\info is a pair (hostaddr, port)."); #endif /* HAVE_GETPEERNAME */  /* s.listen(n) method */ static PyObject *sock_listen(PySocketSockObject *s, PyObject *arg){	int backlog;	int res; 	backlog = PyInt_AsLong(arg);	if (backlog == -1 && PyErr_Occurred())		return NULL;	Py_BEGIN_ALLOW_THREADS	if (backlog < 1)		backlog = 1;	res = listen(s->sock_fd, backlog);	Py_END_ALLOW_THREADS	if (res < 0)		return s->errorhandler();	Py_INCREF(Py_None);	return Py_None;} PyDoc_STRVAR(listen_doc,"listen(backlog)\n\\n\Enable a server to accept connections.  The backlog argument must be at\n\least 1; it specifies the number of unaccepted connection that the system\n\will allow before refusing new connections.");  #ifndef NO_DUP/* s.makefile(mode) method.   Create a new open file object referring to a dupped version of   the socket's file descriptor.  (The dup() call is necessary so   that the open file and socket objects may be closed independent   of each other.)   The mode argument specifies 'r' or 'w' passed to fdopen(). */ static PyObject *sock_makefile(PySocketSockObject *s, PyObject *args){	extern int fclose(FILE *);	char *mode = "r";	int bufsize = -1;#ifdef MS_WIN32	Py_intptr_t fd;#else	int fd;#endif	FILE *fp;	PyObject *f;#ifdef __VMS	char *mode_r = "r";	char *mode_w = "w";#endif 	if (!PyArg_ParseTuple(args, "|si:makefile", &mode, &bufsize))		return NULL;#ifdef __VMS	if (strcmp(mode,"rb") == 0) {	    mode = mode_r;	}	else {		if (strcmp(mode,"wb") == 0) {			mode = mode_w;		}	}#endif#ifdef MS_WIN32	if (((fd = _open_osfhandle(s->sock_fd, _O_BINARY)) < 0) ||	    ((fd = dup(fd)) < 0) || ((fp = fdopen(fd, mode)) == NULL))#else	if ((fd = dup(s->sock_fd)) < 0 || (fp = fdopen(fd, mode)) == NULL)#endif	{		if (fd >= 0)			SOCKETCLOSE(fd);		return s->errorhandler();	}	f = PyFile_FromFile(fp, "<socket>", mode, fclose);	if (f != NULL)		PyFile_SetBufSize(f, bufsize);	return f;} PyDoc_STRVAR(makefile_doc,"makefile([mode[, buffersize]]) -> file object\n\\n\Return a regular file object corresponding to the socket.\n\The mode and buffersize arguments are as for the built-in open() function."); #endif /* NO_DUP */ /* * This is the guts of the recv() and recv_into() methods, which reads into a * char buffer.  If you have any inc/def ref to do to the objects that contain * the buffer, do it in the caller.  This function returns the number of bytes * succesfully read.  If there was an error, it returns -1.  Note that it is * also possible that we return a number of bytes smaller than the request * bytes. */static ssize_tsock_recv_guts(PySocketSockObject *s, char* cbuf, int len, int flags){        ssize_t outlen = -1;        int timeout;#ifdef __VMS	int remaining;	char *read_buf;#endif 	if (!IS_SELECTABLE(s)) {		select_error();		return -1;	} #ifndef __VMS	Py_BEGIN_ALLOW_THREADS	timeout = internal_select(s, 0);	if (!timeout)		outlen = recv(s->sock_fd, cbuf, len, flags);	Py_END_ALLOW_THREADS 	if (timeout == 1) {		PyErr_SetString(socket_timeout, "timed out");		return -1;	}	if (outlen < 0) {		/* Note: the call to errorhandler() ALWAYS indirectly returned		   NULL, so ignore its return value */		s->errorhandler();		return -1;	}#else	read_buf = cbuf;	remaining = len;	while (remaining != 0) {		unsigned int segment;		int nread = -1; 		segment = remaining /SEGMENT_SIZE;		if (segment != 0) {			segment = SEGMENT_SIZE;		}		else {			segment = remaining;		} 		Py_BEGIN_ALLOW_THREADS		timeout = internal_select(s, 0);		if (!timeout)			nread = recv(s->sock_fd, read_buf, segment, flags);		Py_END_ALLOW_THREADS 		if (timeout == 1) {			PyErr_SetString(socket_timeout, "timed out");			return -1;		}		if (nread < 0) {			s->errorhandler();			return -1;		}		if (nread != remaining) {			read_buf += nread;			break;		} 		remaining -= segment;		read_buf += segment;	}	outlen = read_buf - cbuf;#endif /* !__VMS */ 	return outlen;}  /* s.recv(nbytes [,flags]) method */ static PyObject *sock_recv(PySocketSockObject *s, PyObject *args){	int recvlen, flags = 0;        ssize_t outlen;	PyObject *buf; 	if (!PyArg_ParseTuple(args, "i|i:recv", &recvlen, &flags))		return NULL; 	if (recvlen < 0) {		PyErr_SetString(PyExc_ValueError,				"negative buffersize in recv");		return NULL;	} 	/* Allocate a new string. */	buf = PyString_FromStringAndSize((char *) 0, recvlen);	if (buf == NULL)		return NULL; 	/* Call the guts */	outlen = sock_recv_guts(s, PyString_AS_STRING(buf), recvlen, flags);	if (outlen < 0) {		/* An error occurred, release the string and return an		   error. */		Py_DECREF(buf);		return NULL;	}	if (outlen != recvlen) {		/* We did not read as many bytes as we anticipated, resize the		   string if possible and be succesful. */		if (_PyString_Resize(&buf, outlen) < 0)			/* Oopsy, not so succesful after all. */			return NULL;	} 	return buf;} PyDoc_STRVAR(recv_doc,"recv(buffersize[, flags]) -> data\n\\n\Receive up to buffersize bytes from the socket.  For the optional flags\n\argument, see the Unix manual.  When no data is available, block until\n\at least one byte is available or until the remote end is closed.  When\n\the remote end is closed and all data is read, return the empty string.");  /* s.recv_into(buffer, [nbytes [,flags]]) method */ static PyObject*sock_recv_into(PySocketSockObject *s, PyObject *args, PyObject *kwds){	static char *kwlist[] = {"buffer", "nbytes", "flags", 0}; 	int recvlen = 0, flags = 0;        ssize_t readlen;	char *buf;	int buflen; 	/* Get the buffer's memory */	if (!PyArg_ParseTupleAndKeywords(args, kwds, "w#|ii:recv_into", kwlist,					 &buf, &buflen, &recvlen, &flags))		return NULL;	assert(buf != 0 && buflen > 0); 	if (recvlen < 0) {		PyErr_SetString(PyExc_ValueError,				"negative buffersize in recv_into");		return NULL;	}	if (recvlen == 0) {            /* If nbytes was not specified, use the buffer's length */            recvlen = buflen;	} 	/* Check if the buffer is large enough */	if (buflen < recvlen) {		PyErr_SetString(PyExc_ValueError,				"buffer too small for requested bytes");		return NULL;	} 	/* Call the guts */	readlen = sock_recv_guts(s, buf, recvlen, flags);	if (readlen < 0) {		/* Return an error. */		return NULL;	} 	/* Return the number of bytes read.  Note that we do not do anything	   special here in the case that readlen < recvlen. */	return PyInt_FromSsize_t(readlen);} PyDoc_STRVAR(recv_into_doc,"recv_into(buffer, [nbytes[, flags]]) -> nbytes_read\n\\n\A version of recv() that stores its data into a buffer rather than creating \n\a new string.  Receive up to buffersize bytes from the socket.  If buffersize \n\is not specified (or 0), receive up to the size available in the given buffer.\n\\n\See recv() for documentation about the flags.");  /* * This is the guts of the recv() and recv_into() methods, which reads into a * char buffer.  If you have any inc/def ref to do to the objects that contain * the buffer, do it in the caller.  This function returns the number of bytes * succesfully read.  If there was an error, it returns -1.  Note that it is * also possible that we return a number of bytes smaller than the request * bytes. * * 'addr' is a return value for the address object.  Note that you must decref * it yourself. */static ssize_tsock_recvfrom_guts(PySocketSockObject *s, char* cbuf, int len, int flags,		   PyObject** addr){	sock_addr_t addrbuf;	int timeout;	ssize_t n = -1;	socklen_t addrlen; 	*addr = NULL; 	if (!getsockaddrlen(s, &addrlen))		return -1; 	if (!IS_SELECTABLE(s)) {		select_error();		return -1;	} 	Py_BEGIN_ALLOW_THREADS	memset(&addrbuf, 0, addrlen);	timeout = internal_select(s, 0);	if (!timeout) {#ifndef MS_WINDOWS#if defined(PYOS_OS2) && !defined(PYCC_GCC)		n = recvfrom(s->sock_fd, cbuf, len, flags,			     SAS2SA(&addrbuf), &addrlen);#else		n = recvfrom(s->sock_fd, cbuf, len, flags,			     (void *) &addrbuf, &addrlen);#endif#else		n = recvfrom(s->sock_fd, cbuf, len, flags,			     SAS2SA(&addrbuf), &addrlen);#endif	}	Py_END_ALLOW_THREADS 	if (timeout == 1) {		PyErr_SetString(socket_timeout, "timed out");		return -1;	}	if (n < 0) {		s->errorhandler();                return -1;	} 	if (!(*addr = makesockaddr(s->sock_fd, SAS2SA(&addrbuf),				   addrlen, s->sock_proto)))		return -1; 	return n;} /* s.recvfrom(nbytes [,flags]) method */ static PyObject *sock_recvfrom(PySocketSockObject *s, PyObject *args){	PyObject *buf = NULL;	PyObject *addr = NULL;	PyObject *ret = NULL;	int recvlen, flags = 0;        ssize_t outlen; 	if (!PyArg_ParseTuple(args, "i|i:recvfrom", &recvlen, &flags))		return NULL; 	if (recvlen < 0) {		PyErr_SetString(PyExc_ValueError,				"negative buffersize in recvfrom");		return NULL;	} 	buf = PyString_FromStringAndSize((char *) 0, recvlen);	if (buf == NULL)		return NULL; 	outlen = sock_recvfrom_guts(s, PyString_AS_STRING(buf),				    recvlen, flags, &addr);	if (outlen < 0) {		goto finally;	} 	if (outlen != recvlen) {		/* We did not read as many bytes as we anticipated, resize the		   string if possible and be succesful. */

Lines 2001–2500 of 5106. Previous lines