5106 lines · 126.3 KB · C
RawDownload
 PyDoc_STRVAR(getdefaulttimeout_doc,"getdefaulttimeout() -> timeout\n\\n\Returns the default timeout in floating seconds for new socket objects.\n\A value of None indicates that new socket objects have no timeout.\n\When the socket module is first imported, the default is None."); static PyObject *socket_setdefaulttimeout(PyObject *self, 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;		}	} 	defaulttimeout = timeout; 	Py_INCREF(Py_None);	return Py_None;} PyDoc_STRVAR(setdefaulttimeout_doc,"setdefaulttimeout(timeout)\n\\n\Set the default timeout in floating seconds for new socket objects.\n\A value of None indicates that new socket objects have no timeout.\n\When the socket module is first imported, the default is None.");  /* List of functions exported by this module. */ static PyMethodDef socket_methods[] = {	{"gethostbyname",	socket_gethostbyname,	 METH_VARARGS, gethostbyname_doc},	{"gethostbyname_ex",	socket_gethostbyname_ex,	 METH_VARARGS, ghbn_ex_doc},	{"gethostbyaddr",	socket_gethostbyaddr,	 METH_VARARGS, gethostbyaddr_doc},	{"gethostname",		socket_gethostname,	 METH_NOARGS,  gethostname_doc},	{"getservbyname",	socket_getservbyname,	 METH_VARARGS, getservbyname_doc},	{"getservbyport",	socket_getservbyport,	 METH_VARARGS, getservbyport_doc},	{"getprotobyname",	socket_getprotobyname,	 METH_VARARGS, getprotobyname_doc},#ifndef NO_DUP	{"fromfd",		socket_fromfd,	 METH_VARARGS, fromfd_doc},#endif#ifdef HAVE_SOCKETPAIR	{"socketpair",		socket_socketpair,	 METH_VARARGS, socketpair_doc},#endif	{"ntohs",		socket_ntohs,	 METH_VARARGS, ntohs_doc},	{"ntohl",		socket_ntohl,	 METH_O, ntohl_doc},	{"htons",		socket_htons,	 METH_VARARGS, htons_doc},	{"htonl",		socket_htonl,	 METH_O, htonl_doc},	{"inet_aton",		socket_inet_aton,	 METH_VARARGS, inet_aton_doc},	{"inet_ntoa",		socket_inet_ntoa,	 METH_VARARGS, inet_ntoa_doc},#ifdef HAVE_INET_PTON	{"inet_pton",		socket_inet_pton,	 METH_VARARGS, inet_pton_doc},	{"inet_ntop",		socket_inet_ntop,	 METH_VARARGS, inet_ntop_doc},#endif	{"getaddrinfo",		socket_getaddrinfo,	 METH_VARARGS, getaddrinfo_doc},	{"getnameinfo",		socket_getnameinfo,	 METH_VARARGS, getnameinfo_doc},	{"getdefaulttimeout",	(PyCFunction)socket_getdefaulttimeout,	 METH_NOARGS, getdefaulttimeout_doc},	{"setdefaulttimeout",	socket_setdefaulttimeout,	 METH_O, setdefaulttimeout_doc},	{NULL,			NULL}		 /* Sentinel */};  #ifdef RISCOS#define OS_INIT_DEFINED static intos_init(void){	_kernel_swi_regs r; 	r.r[0] = 0;	_kernel_swi(0x43380, &r, &r);	taskwindow = r.r[0]; 	return 1;} #endif /* RISCOS */  #ifdef MS_WINDOWS#define OS_INIT_DEFINED /* Additional initialization and cleanup for Windows */ static voidos_cleanup(void){	WSACleanup();} static intos_init(void){	WSADATA WSAData;	int ret;	char buf[100];	ret = WSAStartup(0x0101, &WSAData);	switch (ret) {	case 0:	/* No error */		Py_AtExit(os_cleanup);		return 1; /* Success */	case WSASYSNOTREADY:		PyErr_SetString(PyExc_ImportError,				"WSAStartup failed: network not ready");		break;	case WSAVERNOTSUPPORTED:	case WSAEINVAL:		PyErr_SetString(			PyExc_ImportError,			"WSAStartup failed: requested version not supported");		break;	default:		PyOS_snprintf(buf, sizeof(buf),			      "WSAStartup failed: error code %d", ret);		PyErr_SetString(PyExc_ImportError, buf);		break;	}	return 0; /* Failure */} #endif /* MS_WINDOWS */  #ifdef PYOS_OS2#define OS_INIT_DEFINED /* Additional initialization for OS/2 */ static intos_init(void){#ifndef PYCC_GCC	char reason[64];	int rc = sock_init(); 	if (rc == 0) {		return 1; /* Success */	} 	PyOS_snprintf(reason, sizeof(reason),		      "OS/2 TCP/IP Error# %d", sock_errno());	PyErr_SetString(PyExc_ImportError, reason); 	return 0;  /* Failure */#else	/* No need to initialise sockets with GCC/EMX */	return 1; /* Success */#endif} #endif /* PYOS_OS2 */  #ifndef OS_INIT_DEFINEDstatic intos_init(void){	return 1; /* Success */}#endif  /* C API table - always add new things to the end for binary   compatibility. */staticPySocketModule_APIObject PySocketModuleAPI ={	&sock_type,        NULL};  /* Initialize the _socket module.    This module is actually called "_socket", and there's a wrapper   "socket.py" which implements some additional functionality.  On some   platforms (e.g. Windows and OS/2), socket.py also implements a   wrapper for the socket type that provides missing functionality such   as makefile(), dup() and fromfd().  The import of "_socket" may fail   with an ImportError exception if os-specific initialization fails.   On Windows, this does WINSOCK initialization.  When WINSOCK is   initialized succesfully, a call to WSACleanup() is scheduled to be   made at exit time.*/ PyDoc_STRVAR(socket_doc,"Implementation module for socket operations.\n\\n\See the socket module for documentation."); PyMODINIT_FUNCinit_socket(void){	PyObject *m, *has_ipv6; 	if (!os_init())		return; 	sock_type.ob_type = &PyType_Type;	m = Py_InitModule3(PySocket_MODULE_NAME,			   socket_methods,			   socket_doc);	if (m == NULL)		return; 	socket_error = PyErr_NewException("socket.error", NULL, NULL);	if (socket_error == NULL)		return;        PySocketModuleAPI.error = socket_error;	Py_INCREF(socket_error);	PyModule_AddObject(m, "error", socket_error);	socket_herror = PyErr_NewException("socket.herror",					   socket_error, NULL);	if (socket_herror == NULL)		return;	Py_INCREF(socket_herror);	PyModule_AddObject(m, "herror", socket_herror);	socket_gaierror = PyErr_NewException("socket.gaierror", socket_error,	    NULL);	if (socket_gaierror == NULL)		return;	Py_INCREF(socket_gaierror);	PyModule_AddObject(m, "gaierror", socket_gaierror);	socket_timeout = PyErr_NewException("socket.timeout",					    socket_error, NULL);	if (socket_timeout == NULL)		return;	Py_INCREF(socket_timeout);	PyModule_AddObject(m, "timeout", socket_timeout);	Py_INCREF((PyObject *)&sock_type);	if (PyModule_AddObject(m, "SocketType",			       (PyObject *)&sock_type) != 0)		return;	Py_INCREF((PyObject *)&sock_type);	if (PyModule_AddObject(m, "socket",			       (PyObject *)&sock_type) != 0)		return; #ifdef ENABLE_IPV6	has_ipv6 = Py_True;#else	has_ipv6 = Py_False;#endif	Py_INCREF(has_ipv6);	PyModule_AddObject(m, "has_ipv6", has_ipv6); 	/* Export C API */	if (PyModule_AddObject(m, PySocket_CAPI_NAME,	       PyCObject_FromVoidPtr((void *)&PySocketModuleAPI, NULL)				 ) != 0)		return; 	/* Address families (we only support AF_INET and AF_UNIX) */#ifdef AF_UNSPEC	PyModule_AddIntConstant(m, "AF_UNSPEC", AF_UNSPEC);#endif	PyModule_AddIntConstant(m, "AF_INET", AF_INET);#ifdef AF_INET6	PyModule_AddIntConstant(m, "AF_INET6", AF_INET6);#endif /* AF_INET6 */#if defined(AF_UNIX)	PyModule_AddIntConstant(m, "AF_UNIX", AF_UNIX);#endif /* AF_UNIX */#ifdef AF_AX25	/* Amateur Radio AX.25 */	PyModule_AddIntConstant(m, "AF_AX25", AF_AX25);#endif#ifdef AF_IPX	PyModule_AddIntConstant(m, "AF_IPX", AF_IPX); /* Novell IPX */#endif#ifdef AF_APPLETALK	/* Appletalk DDP */	PyModule_AddIntConstant(m, "AF_APPLETALK", AF_APPLETALK);#endif#ifdef AF_NETROM	/* Amateur radio NetROM */	PyModule_AddIntConstant(m, "AF_NETROM", AF_NETROM);#endif#ifdef AF_BRIDGE	/* Multiprotocol bridge */	PyModule_AddIntConstant(m, "AF_BRIDGE", AF_BRIDGE);#endif#ifdef AF_ATMPVC	/* ATM PVCs */	PyModule_AddIntConstant(m, "AF_ATMPVC", AF_ATMPVC);#endif#ifdef AF_AAL5	/* Reserved for Werner's ATM */	PyModule_AddIntConstant(m, "AF_AAL5", AF_AAL5);#endif#ifdef AF_X25	/* Reserved for X.25 project */	PyModule_AddIntConstant(m, "AF_X25", AF_X25);#endif#ifdef AF_INET6	PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IP version 6 */#endif#ifdef AF_ROSE	/* Amateur Radio X.25 PLP */	PyModule_AddIntConstant(m, "AF_ROSE", AF_ROSE);#endif#ifdef AF_DECnet	/* Reserved for DECnet project */	PyModule_AddIntConstant(m, "AF_DECnet", AF_DECnet);#endif#ifdef AF_NETBEUI	/* Reserved for 802.2LLC project */	PyModule_AddIntConstant(m, "AF_NETBEUI", AF_NETBEUI);#endif#ifdef AF_SECURITY	/* Security callback pseudo AF */	PyModule_AddIntConstant(m, "AF_SECURITY", AF_SECURITY);#endif#ifdef AF_KEY	/* PF_KEY key management API */	PyModule_AddIntConstant(m, "AF_KEY", AF_KEY);#endif#ifdef AF_NETLINK	/*  */	PyModule_AddIntConstant(m, "AF_NETLINK", AF_NETLINK);	PyModule_AddIntConstant(m, "NETLINK_ROUTE", NETLINK_ROUTE);#ifdef NETLINK_SKIP	PyModule_AddIntConstant(m, "NETLINK_SKIP", NETLINK_SKIP);#endif#ifdef NETLINK_W1	PyModule_AddIntConstant(m, "NETLINK_W1", NETLINK_W1);#endif	PyModule_AddIntConstant(m, "NETLINK_USERSOCK", NETLINK_USERSOCK);	PyModule_AddIntConstant(m, "NETLINK_FIREWALL", NETLINK_FIREWALL);#ifdef NETLINK_TCPDIAG	PyModule_AddIntConstant(m, "NETLINK_TCPDIAG", NETLINK_TCPDIAG);#endif#ifdef NETLINK_NFLOG	PyModule_AddIntConstant(m, "NETLINK_NFLOG", NETLINK_NFLOG);#endif#ifdef NETLINK_XFRM	PyModule_AddIntConstant(m, "NETLINK_XFRM", NETLINK_XFRM);#endif#ifdef NETLINK_ARPD	PyModule_AddIntConstant(m, "NETLINK_ARPD", NETLINK_ARPD);#endif#ifdef NETLINK_ROUTE6	PyModule_AddIntConstant(m, "NETLINK_ROUTE6", NETLINK_ROUTE6);#endif	PyModule_AddIntConstant(m, "NETLINK_IP6_FW", NETLINK_IP6_FW);#ifdef NETLINK_DNRTMSG	PyModule_AddIntConstant(m, "NETLINK_DNRTMSG", NETLINK_DNRTMSG);#endif #ifdef NETLINK_TAPBASE	PyModule_AddIntConstant(m, "NETLINK_TAPBASE", NETLINK_TAPBASE);#endif#endif /* AF_NETLINK */#ifdef AF_ROUTE	/* Alias to emulate 4.4BSD */	PyModule_AddIntConstant(m, "AF_ROUTE", AF_ROUTE);#endif#ifdef AF_ASH	/* Ash */	PyModule_AddIntConstant(m, "AF_ASH", AF_ASH);#endif#ifdef AF_ECONET	/* Acorn Econet */	PyModule_AddIntConstant(m, "AF_ECONET", AF_ECONET);#endif#ifdef AF_ATMSVC	/* ATM SVCs */	PyModule_AddIntConstant(m, "AF_ATMSVC", AF_ATMSVC);#endif#ifdef AF_SNA	/* Linux SNA Project (nutters!) */	PyModule_AddIntConstant(m, "AF_SNA", AF_SNA);#endif#ifdef AF_IRDA	/* IRDA sockets */	PyModule_AddIntConstant(m, "AF_IRDA", AF_IRDA);#endif#ifdef AF_PPPOX	/* PPPoX sockets */	PyModule_AddIntConstant(m, "AF_PPPOX", AF_PPPOX);#endif#ifdef AF_WANPIPE	/* Wanpipe API Sockets */	PyModule_AddIntConstant(m, "AF_WANPIPE", AF_WANPIPE);#endif#ifdef AF_LLC	/* Linux LLC */	PyModule_AddIntConstant(m, "AF_LLC", AF_LLC);#endif #ifdef USE_BLUETOOTH	PyModule_AddIntConstant(m, "AF_BLUETOOTH", AF_BLUETOOTH);	PyModule_AddIntConstant(m, "BTPROTO_L2CAP", BTPROTO_L2CAP);#if !defined(__FreeBSD__)	PyModule_AddIntConstant(m, "BTPROTO_SCO", BTPROTO_SCO);#endif	PyModule_AddIntConstant(m, "BTPROTO_RFCOMM", BTPROTO_RFCOMM);	PyModule_AddStringConstant(m, "BDADDR_ANY", "00:00:00:00:00:00");	PyModule_AddStringConstant(m, "BDADDR_LOCAL", "00:00:00:FF:FF:FF");#endif #ifdef HAVE_NETPACKET_PACKET_H	PyModule_AddIntConstant(m, "AF_PACKET", AF_PACKET);	PyModule_AddIntConstant(m, "PF_PACKET", PF_PACKET);	PyModule_AddIntConstant(m, "PACKET_HOST", PACKET_HOST);	PyModule_AddIntConstant(m, "PACKET_BROADCAST", PACKET_BROADCAST);	PyModule_AddIntConstant(m, "PACKET_MULTICAST", PACKET_MULTICAST);	PyModule_AddIntConstant(m, "PACKET_OTHERHOST", PACKET_OTHERHOST);	PyModule_AddIntConstant(m, "PACKET_OUTGOING", PACKET_OUTGOING);	PyModule_AddIntConstant(m, "PACKET_LOOPBACK", PACKET_LOOPBACK);	PyModule_AddIntConstant(m, "PACKET_FASTROUTE", PACKET_FASTROUTE);#endif 	/* Socket types */	PyModule_AddIntConstant(m, "SOCK_STREAM", SOCK_STREAM);	PyModule_AddIntConstant(m, "SOCK_DGRAM", SOCK_DGRAM);#ifndef __BEOS__/* We have incomplete socket support. */	PyModule_AddIntConstant(m, "SOCK_RAW", SOCK_RAW);	PyModule_AddIntConstant(m, "SOCK_SEQPACKET", SOCK_SEQPACKET);#if defined(SOCK_RDM)	PyModule_AddIntConstant(m, "SOCK_RDM", SOCK_RDM);#endif#endif #ifdef	SO_DEBUG	PyModule_AddIntConstant(m, "SO_DEBUG", SO_DEBUG);#endif#ifdef	SO_ACCEPTCONN	PyModule_AddIntConstant(m, "SO_ACCEPTCONN", SO_ACCEPTCONN);#endif#ifdef	SO_REUSEADDR	PyModule_AddIntConstant(m, "SO_REUSEADDR", SO_REUSEADDR);#endif#ifdef SO_EXCLUSIVEADDRUSE	PyModule_AddIntConstant(m, "SO_EXCLUSIVEADDRUSE", SO_EXCLUSIVEADDRUSE);#endif #ifdef	SO_KEEPALIVE	PyModule_AddIntConstant(m, "SO_KEEPALIVE", SO_KEEPALIVE);#endif#ifdef	SO_DONTROUTE	PyModule_AddIntConstant(m, "SO_DONTROUTE", SO_DONTROUTE);#endif#ifdef	SO_BROADCAST	PyModule_AddIntConstant(m, "SO_BROADCAST", SO_BROADCAST);#endif#ifdef	SO_USELOOPBACK	PyModule_AddIntConstant(m, "SO_USELOOPBACK", SO_USELOOPBACK);#endif#ifdef	SO_LINGER	PyModule_AddIntConstant(m, "SO_LINGER", SO_LINGER);#endif#ifdef	SO_OOBINLINE	PyModule_AddIntConstant(m, "SO_OOBINLINE", SO_OOBINLINE);#endif#ifdef	SO_REUSEPORT	PyModule_AddIntConstant(m, "SO_REUSEPORT", SO_REUSEPORT);#endif#ifdef	SO_SNDBUF	PyModule_AddIntConstant(m, "SO_SNDBUF", SO_SNDBUF);#endif#ifdef	SO_RCVBUF	PyModule_AddIntConstant(m, "SO_RCVBUF", SO_RCVBUF);#endif#ifdef	SO_SNDLOWAT	PyModule_AddIntConstant(m, "SO_SNDLOWAT", SO_SNDLOWAT);#endif

Lines 4001–4500 of 5106. Previous lines