5106 lines · 126.3 KB · C
RawDownload
		if (_PyString_Resize(&buf, outlen) < 0)			/* Oopsy, not so succesful after all. */			goto finally;	} 	ret = PyTuple_Pack(2, buf, addr); finally:	Py_XDECREF(buf);	Py_XDECREF(addr);	return ret;} PyDoc_STRVAR(recvfrom_doc,"recvfrom(buffersize[, flags]) -> (data, address info)\n\\n\Like recv(buffersize, flags) but also return the sender's address info.");  /* s.recvfrom_into(buffer[, nbytes [,flags]]) method */ static PyObject *sock_recvfrom_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; 	PyObject *addr = NULL; 	if (!PyArg_ParseTupleAndKeywords(args, kwds, "w#|ii:recvfrom_into",					 kwlist, &buf, &buflen,					 &recvlen, &flags))		return NULL;	assert(buf != 0 && buflen > 0); 	if (recvlen < 0) {		PyErr_SetString(PyExc_ValueError,				"negative buffersize in recvfrom_into");		return NULL;	}	if (recvlen == 0) {            /* If nbytes was not specified, use the buffer's length */            recvlen = buflen;	} 	if (buflen < recvlen) {		PyErr_SetString(PyExc_ValueError,				"buffer too small for requested bytes");		return NULL;	} 	readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr);	if (readlen < 0) {		/* Return an error */		Py_XDECREF(addr);		return NULL;	} 	/* Return the number of bytes read and the address.  Note that we do	   not do anything special here in the case that readlen < recvlen. */ 	return Py_BuildValue("lN", readlen, addr);} PyDoc_STRVAR(recvfrom_into_doc,"recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)\n\\n\Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info.");  /* s.send(data [,flags]) method */ static PyObject *sock_send(PySocketSockObject *s, PyObject *args){	char *buf;	int len, n = -1, flags = 0, timeout; 	if (!PyArg_ParseTuple(args, "s#|i:send", &buf, &len, &flags))		return NULL; 	if (!IS_SELECTABLE(s))		return select_error(); 	Py_BEGIN_ALLOW_THREADS	timeout = internal_select(s, 1);	if (!timeout)#ifdef __VMS		n = sendsegmented(s->sock_fd, buf, len, flags);#else		n = send(s->sock_fd, buf, len, flags);#endif	Py_END_ALLOW_THREADS 	if (timeout == 1) {		PyErr_SetString(socket_timeout, "timed out");		return NULL;	}	if (n < 0)		return s->errorhandler();	return PyInt_FromLong((long)n);} PyDoc_STRVAR(send_doc,"send(data[, flags]) -> count\n\\n\Send a data string to the socket.  For the optional flags\n\argument, see the Unix manual.  Return the number of bytes\n\sent; this may be less than len(data) if the network is busy.");  /* s.sendall(data [,flags]) method */ static PyObject *sock_sendall(PySocketSockObject *s, PyObject *args){	char *buf;	int len, n = -1, flags = 0, timeout; 	if (!PyArg_ParseTuple(args, "s#|i:sendall", &buf, &len, &flags))		return NULL; 	if (!IS_SELECTABLE(s))		return select_error(); 	Py_BEGIN_ALLOW_THREADS	do {		timeout = internal_select(s, 1);		n = -1;		if (timeout)			break;#ifdef __VMS		n = sendsegmented(s->sock_fd, buf, len, flags);#else		n = send(s->sock_fd, buf, len, flags);#endif		if (n < 0)			break;		buf += n;		len -= n;	} while (len > 0);	Py_END_ALLOW_THREADS 	if (timeout == 1) {		PyErr_SetString(socket_timeout, "timed out");		return NULL;	}	if (n < 0)		return s->errorhandler(); 	Py_INCREF(Py_None);	return Py_None;} PyDoc_STRVAR(sendall_doc,"sendall(data[, flags])\n\\n\Send a data string to the socket.  For the optional flags\n\argument, see the Unix manual.  This calls send() repeatedly\n\until all data is sent.  If an error occurs, it's impossible\n\to tell how much data has been sent.");  /* s.sendto(data, [flags,] sockaddr) method */ static PyObject *sock_sendto(PySocketSockObject *s, PyObject *args){	PyObject *addro;	char *buf;	sock_addr_t addrbuf;	int addrlen, len, n = -1, flags, timeout; 	flags = 0;	if (!PyArg_ParseTuple(args, "s#O:sendto", &buf, &len, &addro)) {		PyErr_Clear();		if (!PyArg_ParseTuple(args, "s#iO:sendto",				      &buf, &len, &flags, &addro))			return NULL;	} 	if (!IS_SELECTABLE(s))		return select_error(); 	if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen))		return NULL; 	Py_BEGIN_ALLOW_THREADS	timeout = internal_select(s, 1);	if (!timeout)		n = sendto(s->sock_fd, buf, len, flags, SAS2SA(&addrbuf), addrlen);	Py_END_ALLOW_THREADS 	if (timeout == 1) {		PyErr_SetString(socket_timeout, "timed out");		return NULL;	}	if (n < 0)		return s->errorhandler();	return PyInt_FromLong((long)n);} PyDoc_STRVAR(sendto_doc,"sendto(data[, flags], address) -> count\n\\n\Like send(data, flags) but allows specifying the destination address.\n\For IP sockets, the address is a pair (hostaddr, port).");  /* s.shutdown(how) method */ static PyObject *sock_shutdown(PySocketSockObject *s, PyObject *arg){	int how;	int res; 	how = PyInt_AsLong(arg);	if (how == -1 && PyErr_Occurred())		return NULL;	Py_BEGIN_ALLOW_THREADS	res = shutdown(s->sock_fd, how);	Py_END_ALLOW_THREADS	if (res < 0)		return s->errorhandler();	Py_INCREF(Py_None);	return Py_None;} PyDoc_STRVAR(shutdown_doc,"shutdown(flag)\n\\n\Shut down the reading side of the socket (flag == SHUT_RD), the writing side\n\of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).");  /* List of methods for socket objects */ static PyMethodDef sock_methods[] = {	{"accept",	  (PyCFunction)sock_accept, METH_NOARGS,			  accept_doc},	{"bind",	  (PyCFunction)sock_bind, METH_O,			  bind_doc},	{"close",	  (PyCFunction)sock_close, METH_NOARGS,			  close_doc},	{"connect",	  (PyCFunction)sock_connect, METH_O,			  connect_doc},	{"connect_ex",	  (PyCFunction)sock_connect_ex, METH_O,			  connect_ex_doc},#ifndef NO_DUP	{"dup",		  (PyCFunction)sock_dup, METH_NOARGS,			  dup_doc},#endif	{"fileno",	  (PyCFunction)sock_fileno, METH_NOARGS,			  fileno_doc},#ifdef HAVE_GETPEERNAME	{"getpeername",	  (PyCFunction)sock_getpeername,			  METH_NOARGS, getpeername_doc},#endif	{"getsockname",	  (PyCFunction)sock_getsockname,			  METH_NOARGS, getsockname_doc},	{"getsockopt",	  (PyCFunction)sock_getsockopt, METH_VARARGS,			  getsockopt_doc},	{"listen",	  (PyCFunction)sock_listen, METH_O,			  listen_doc},#ifndef NO_DUP	{"makefile",	  (PyCFunction)sock_makefile, METH_VARARGS,			  makefile_doc},#endif	{"recv",	  (PyCFunction)sock_recv, METH_VARARGS,			  recv_doc},	{"recv_into",	  (PyCFunction)sock_recv_into, METH_VARARGS | METH_KEYWORDS,			  recv_into_doc},	{"recvfrom",	  (PyCFunction)sock_recvfrom, METH_VARARGS,			  recvfrom_doc},	{"recvfrom_into",  (PyCFunction)sock_recvfrom_into, METH_VARARGS | METH_KEYWORDS,			  recvfrom_into_doc},	{"send",	  (PyCFunction)sock_send, METH_VARARGS,			  send_doc},	{"sendall",	  (PyCFunction)sock_sendall, METH_VARARGS,			  sendall_doc},	{"sendto",	  (PyCFunction)sock_sendto, METH_VARARGS,			  sendto_doc},	{"setblocking",	  (PyCFunction)sock_setblocking, METH_O,			  setblocking_doc},	{"settimeout",    (PyCFunction)sock_settimeout, METH_O,			  settimeout_doc},	{"gettimeout",    (PyCFunction)sock_gettimeout, METH_NOARGS,			  gettimeout_doc},	{"setsockopt",	  (PyCFunction)sock_setsockopt, METH_VARARGS,			  setsockopt_doc},	{"shutdown",	  (PyCFunction)sock_shutdown, METH_O,			  shutdown_doc},#ifdef RISCOS	{"sleeptaskw",	  (PyCFunction)sock_sleeptaskw, METH_O,	 		  sleeptaskw_doc},#endif	{NULL,			NULL}		/* sentinel */}; /* SockObject members */static PyMemberDef sock_memberlist[] = {       {"family", T_INT, offsetof(PySocketSockObject, sock_family), READONLY, "the socket family"},       {"type", T_INT, offsetof(PySocketSockObject, sock_type), READONLY, "the socket type"},       {"proto", T_INT, offsetof(PySocketSockObject, sock_proto), READONLY, "the socket protocol"},       {"timeout", T_DOUBLE, offsetof(PySocketSockObject, sock_timeout), READONLY, "the socket timeout"},       {0},}; /* Deallocate a socket object in response to the last Py_DECREF().   First close the file description. */ static voidsock_dealloc(PySocketSockObject *s){	if (s->sock_fd != -1)		(void) SOCKETCLOSE(s->sock_fd);	s->ob_type->tp_free((PyObject *)s);}  static PyObject *sock_repr(PySocketSockObject *s){	char buf[512];#if SIZEOF_SOCKET_T > SIZEOF_LONG	if (s->sock_fd > LONG_MAX) {		/* this can occur on Win64, and actually there is a special		   ugly printf formatter for decimal pointer length integer		   printing, only bother if necessary*/		PyErr_SetString(PyExc_OverflowError,				"no printf formatter to display "				"the socket descriptor in decimal");		return NULL;	}#endif	PyOS_snprintf(		buf, sizeof(buf),		"<socket object, fd=%ld, family=%d, type=%d, protocol=%d>",		(long)s->sock_fd, s->sock_family,		s->sock_type,		s->sock_proto);	return PyString_FromString(buf);}  /* Create a new, uninitialized socket object. */ static PyObject *sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds){	PyObject *new; 	new = type->tp_alloc(type, 0);	if (new != NULL) {		((PySocketSockObject *)new)->sock_fd = -1;		((PySocketSockObject *)new)->sock_timeout = -1.0;		((PySocketSockObject *)new)->errorhandler = &set_error;	}	return new;}  /* Initialize a new socket object. */ /*ARGSUSED*/static intsock_initobj(PyObject *self, PyObject *args, PyObject *kwds){	PySocketSockObject *s = (PySocketSockObject *)self;	SOCKET_T fd;	int family = AF_INET, type = SOCK_STREAM, proto = 0;	static char *keywords[] = {"family", "type", "proto", 0}; 	if (!PyArg_ParseTupleAndKeywords(args, kwds,					 "|iii:socket", keywords,					 &family, &type, &proto))		return -1; 	Py_BEGIN_ALLOW_THREADS	fd = socket(family, type, proto);	Py_END_ALLOW_THREADS #ifdef MS_WINDOWS	if (fd == INVALID_SOCKET)#else	if (fd < 0)#endif	{		set_error();		return -1;	}	init_sockobject(s, fd, family, type, proto); 	return 0; }  /* Type object for socket objects. */ static PyTypeObject sock_type = {	PyObject_HEAD_INIT(0)	/* Must fill in type value later */	0,					/* ob_size */	"_socket.socket",			/* tp_name */	sizeof(PySocketSockObject),		/* tp_basicsize */	0,					/* tp_itemsize */	(destructor)sock_dealloc,		/* tp_dealloc */	0,					/* tp_print */	0,					/* tp_getattr */	0,					/* tp_setattr */	0,					/* tp_compare */	(reprfunc)sock_repr,			/* tp_repr */	0,					/* tp_as_number */	0,					/* tp_as_sequence */	0,					/* tp_as_mapping */	0,					/* tp_hash */	0,					/* tp_call */	0,					/* tp_str */	PyObject_GenericGetAttr,		/* tp_getattro */	0,					/* tp_setattro */	0,					/* tp_as_buffer */	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */	sock_doc,				/* tp_doc */	0,					/* tp_traverse */	0,					/* tp_clear */	0,					/* tp_richcompare */	0,					/* tp_weaklistoffset */	0,					/* tp_iter */	0,					/* tp_iternext */	sock_methods,				/* tp_methods */	sock_memberlist,			/* tp_members */	0,					/* tp_getset */	0,					/* tp_base */	0,					/* tp_dict */	0,					/* tp_descr_get */	0,					/* tp_descr_set */	0,					/* tp_dictoffset */	sock_initobj,				/* tp_init */	PyType_GenericAlloc,			/* tp_alloc */	sock_new,				/* tp_new */	PyObject_Del,				/* tp_free */};  /* Python interface to gethostname(). */ /*ARGSUSED*/static PyObject *socket_gethostname(PyObject *self, PyObject *unused){	char buf[1024];	int res;	Py_BEGIN_ALLOW_THREADS	res = gethostname(buf, (int) sizeof buf - 1);	Py_END_ALLOW_THREADS	if (res < 0)		return set_error();	buf[sizeof buf - 1] = '\0';	return PyString_FromString(buf);} PyDoc_STRVAR(gethostname_doc,"gethostname() -> string\n\\n\Return the current host name.");  /* Python interface to gethostbyname(name). */ /*ARGSUSED*/static PyObject *socket_gethostbyname(PyObject *self, PyObject *args){	char *name;	sock_addr_t addrbuf; 	if (!PyArg_ParseTuple(args, "s:gethostbyname", &name))		return NULL;	if (setipaddr(name, SAS2SA(&addrbuf),  sizeof(addrbuf), AF_INET) < 0)		return NULL;	return makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in));} PyDoc_STRVAR(gethostbyname_doc,"gethostbyname(host) -> address\n\\n\Return the IP address (a string of the form '255.255.255.255') for a host.");  /* Convenience function common to gethostbyname_ex and gethostbyaddr */ static PyObject *gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af){	char **pch;	PyObject *rtn_tuple = (PyObject *)NULL;

Lines 2501–3000 of 5106. Previous lines