5106 lines · 126.3 KB · C
RawDownload
	PyObject *name_list = (PyObject *)NULL;	PyObject *addr_list = (PyObject *)NULL;	PyObject *tmp; 	if (h == NULL) {		/* Let's get real error message to return */#ifndef RISCOS		set_herror(h_errno);#else		PyErr_SetString(socket_error, "host not found");#endif		return NULL;	} 	if (h->h_addrtype != af) {#ifdef HAVE_STRERROR		/* Let's get real error message to return */		PyErr_SetString(socket_error,				(char *)strerror(EAFNOSUPPORT));#else		PyErr_SetString(			socket_error,			"Address family not supported by protocol family");#endif		return NULL;	} 	switch (af) { 	case AF_INET:		if (alen < sizeof(struct sockaddr_in))			return NULL;		break; #ifdef ENABLE_IPV6	case AF_INET6:		if (alen < sizeof(struct sockaddr_in6))			return NULL;		break;#endif 	} 	if ((name_list = PyList_New(0)) == NULL)		goto err; 	if ((addr_list = PyList_New(0)) == NULL)		goto err; 	/* SF #1511317: h_aliases can be NULL */	if (h->h_aliases) {		for (pch = h->h_aliases; *pch != NULL; pch++) {			int status;			tmp = PyString_FromString(*pch);			if (tmp == NULL)				goto err; 			status = PyList_Append(name_list, tmp);			Py_DECREF(tmp); 			if (status)				goto err;		}	} 	for (pch = h->h_addr_list; *pch != NULL; pch++) {		int status; 		switch (af) { 		case AF_INET:		    {			struct sockaddr_in sin;			memset(&sin, 0, sizeof(sin));			sin.sin_family = af;#ifdef HAVE_SOCKADDR_SA_LEN			sin.sin_len = sizeof(sin);#endif			memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));			tmp = makeipaddr((struct sockaddr *)&sin, sizeof(sin)); 			if (pch == h->h_addr_list && alen >= sizeof(sin))				memcpy((char *) addr, &sin, sizeof(sin));			break;		    } #ifdef ENABLE_IPV6		case AF_INET6:		    {			struct sockaddr_in6 sin6;			memset(&sin6, 0, sizeof(sin6));			sin6.sin6_family = af;#ifdef HAVE_SOCKADDR_SA_LEN			sin6.sin6_len = sizeof(sin6);#endif			memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));			tmp = makeipaddr((struct sockaddr *)&sin6,				sizeof(sin6)); 			if (pch == h->h_addr_list && alen >= sizeof(sin6))				memcpy((char *) addr, &sin6, sizeof(sin6));			break;		    }#endif 		default:	/* can't happen */			PyErr_SetString(socket_error,					"unsupported address family");			return NULL;		} 		if (tmp == NULL)			goto err; 		status = PyList_Append(addr_list, tmp);		Py_DECREF(tmp); 		if (status)			goto err;	} 	rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);  err:	Py_XDECREF(name_list);	Py_XDECREF(addr_list);	return rtn_tuple;}  /* Python interface to gethostbyname_ex(name). */ /*ARGSUSED*/static PyObject *socket_gethostbyname_ex(PyObject *self, PyObject *args){	char *name;	struct hostent *h;#ifdef ENABLE_IPV6        struct sockaddr_storage addr;#else        struct sockaddr_in addr;#endif	struct sockaddr *sa;	PyObject *ret;#ifdef HAVE_GETHOSTBYNAME_R	struct hostent hp_allocated;#ifdef HAVE_GETHOSTBYNAME_R_3_ARG	struct hostent_data data;#else	char buf[16384];	int buf_len = (sizeof buf) - 1;	int errnop;#endif#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)	int result;#endif#endif /* HAVE_GETHOSTBYNAME_R */ 	if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name))		return NULL;	if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0)		return NULL;	Py_BEGIN_ALLOW_THREADS#ifdef HAVE_GETHOSTBYNAME_R#if   defined(HAVE_GETHOSTBYNAME_R_6_ARG)	result = gethostbyname_r(name, &hp_allocated, buf, buf_len,				 &h, &errnop);#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)	h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);#else /* HAVE_GETHOSTBYNAME_R_3_ARG */	memset((void *) &data, '\0', sizeof(data));	result = gethostbyname_r(name, &hp_allocated, &data);	h = (result != 0) ? NULL : &hp_allocated;#endif#else /* not HAVE_GETHOSTBYNAME_R */#ifdef USE_GETHOSTBYNAME_LOCK	PyThread_acquire_lock(netdb_lock, 1);#endif	h = gethostbyname(name);#endif /* HAVE_GETHOSTBYNAME_R */	Py_END_ALLOW_THREADS	/* Some C libraries would require addr.__ss_family instead of	   addr.ss_family.	   Therefore, we cast the sockaddr_storage into sockaddr to	   access sa_family. */	sa = (struct sockaddr*)&addr;	ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr),			     sa->sa_family);#ifdef USE_GETHOSTBYNAME_LOCK	PyThread_release_lock(netdb_lock);#endif	return ret;} PyDoc_STRVAR(ghbn_ex_doc,"gethostbyname_ex(host) -> (name, aliaslist, addresslist)\n\\n\Return the true host name, a list of aliases, and a list of IP addresses,\n\for a host.  The host argument is a string giving a host name or IP number.");  /* Python interface to gethostbyaddr(IP). */ /*ARGSUSED*/static PyObject *socket_gethostbyaddr(PyObject *self, PyObject *args){#ifdef ENABLE_IPV6	struct sockaddr_storage addr;#else	struct sockaddr_in addr;#endif	struct sockaddr *sa = (struct sockaddr *)&addr;	char *ip_num;	struct hostent *h;	PyObject *ret;#ifdef HAVE_GETHOSTBYNAME_R	struct hostent hp_allocated;#ifdef HAVE_GETHOSTBYNAME_R_3_ARG	struct hostent_data data;#else	char buf[16384];	int buf_len = (sizeof buf) - 1;	int errnop;#endif#if defined(HAVE_GETHOSTBYNAME_R_3_ARG) || defined(HAVE_GETHOSTBYNAME_R_6_ARG)	int result;#endif#endif /* HAVE_GETHOSTBYNAME_R */	char *ap;	int al;	int af; 	if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num))		return NULL;	af = AF_UNSPEC;	if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)		return NULL;	af = sa->sa_family;	ap = NULL;	al = 0;	switch (af) {	case AF_INET:		ap = (char *)&((struct sockaddr_in *)sa)->sin_addr;		al = sizeof(((struct sockaddr_in *)sa)->sin_addr);		break;#ifdef ENABLE_IPV6	case AF_INET6:		ap = (char *)&((struct sockaddr_in6 *)sa)->sin6_addr;		al = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);		break;#endif	default:		PyErr_SetString(socket_error, "unsupported address family");		return NULL;	}	Py_BEGIN_ALLOW_THREADS#ifdef HAVE_GETHOSTBYNAME_R#if   defined(HAVE_GETHOSTBYNAME_R_6_ARG)	result = gethostbyaddr_r(ap, al, af,		&hp_allocated, buf, buf_len,		&h, &errnop);#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)	h = gethostbyaddr_r(ap, al, af,			    &hp_allocated, buf, buf_len, &errnop);#else /* HAVE_GETHOSTBYNAME_R_3_ARG */	memset((void *) &data, '\0', sizeof(data));	result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);	h = (result != 0) ? NULL : &hp_allocated;#endif#else /* not HAVE_GETHOSTBYNAME_R */#ifdef USE_GETHOSTBYNAME_LOCK	PyThread_acquire_lock(netdb_lock, 1);#endif	h = gethostbyaddr(ap, al, af);#endif /* HAVE_GETHOSTBYNAME_R */	Py_END_ALLOW_THREADS	ret = gethost_common(h, (struct sockaddr *)&addr, sizeof(addr), af);#ifdef USE_GETHOSTBYNAME_LOCK	PyThread_release_lock(netdb_lock);#endif	return ret;} PyDoc_STRVAR(gethostbyaddr_doc,"gethostbyaddr(host) -> (name, aliaslist, addresslist)\n\\n\Return the true host name, a list of aliases, and a list of IP addresses,\n\for a host.  The host argument is a string giving a host name or IP number.");  /* Python interface to getservbyname(name).   This only returns the port number, since the other info is already   known or not useful (like the list of aliases). */ /*ARGSUSED*/static PyObject *socket_getservbyname(PyObject *self, PyObject *args){	char *name, *proto=NULL;	struct servent *sp;	if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto))		return NULL;	Py_BEGIN_ALLOW_THREADS	sp = getservbyname(name, proto);	Py_END_ALLOW_THREADS	if (sp == NULL) {		PyErr_SetString(socket_error, "service/proto not found");		return NULL;	}	return PyInt_FromLong((long) ntohs(sp->s_port));} PyDoc_STRVAR(getservbyname_doc,"getservbyname(servicename[, protocolname]) -> integer\n\\n\Return a port number from a service name and protocol name.\n\The optional protocol name, if given, should be 'tcp' or 'udp',\n\otherwise any protocol will match.");  /* Python interface to getservbyport(port).   This only returns the service name, since the other info is already   known or not useful (like the list of aliases). */ /*ARGSUSED*/static PyObject *socket_getservbyport(PyObject *self, PyObject *args){	unsigned short port;	char *proto=NULL;	struct servent *sp;	if (!PyArg_ParseTuple(args, "H|s:getservbyport", &port, &proto))		return NULL;	Py_BEGIN_ALLOW_THREADS	sp = getservbyport(htons(port), proto);	Py_END_ALLOW_THREADS	if (sp == NULL) {		PyErr_SetString(socket_error, "port/proto not found");		return NULL;	}	return PyString_FromString(sp->s_name);} PyDoc_STRVAR(getservbyport_doc,"getservbyport(port[, protocolname]) -> string\n\\n\Return the service name from a port number and protocol name.\n\The optional protocol name, if given, should be 'tcp' or 'udp',\n\otherwise any protocol will match."); /* Python interface to getprotobyname(name).   This only returns the protocol number, since the other info is   already known or not useful (like the list of aliases). */ /*ARGSUSED*/static PyObject *socket_getprotobyname(PyObject *self, PyObject *args){	char *name;	struct protoent *sp;#ifdef __BEOS__/* Not available in BeOS yet. - [cjh] */	PyErr_SetString(socket_error, "getprotobyname not supported");	return NULL;#else	if (!PyArg_ParseTuple(args, "s:getprotobyname", &name))		return NULL;	Py_BEGIN_ALLOW_THREADS	sp = getprotobyname(name);	Py_END_ALLOW_THREADS	if (sp == NULL) {		PyErr_SetString(socket_error, "protocol not found");		return NULL;	}	return PyInt_FromLong((long) sp->p_proto);#endif} PyDoc_STRVAR(getprotobyname_doc,"getprotobyname(name) -> integer\n\\n\Return the protocol number for the named protocol.  (Rarely used.)");  #ifdef HAVE_SOCKETPAIR/* Create a pair of sockets using the socketpair() function.   Arguments as for socket() except the default family is AF_UNIX if   defined on the platform; otherwise, the default is AF_INET. */ /*ARGSUSED*/static PyObject *socket_socketpair(PyObject *self, PyObject *args){	PySocketSockObject *s0 = NULL, *s1 = NULL;	SOCKET_T sv[2];	int family, type = SOCK_STREAM, proto = 0;	PyObject *res = NULL; #if defined(AF_UNIX)	family = AF_UNIX;#else	family = AF_INET;#endif	if (!PyArg_ParseTuple(args, "|iii:socketpair",			      &family, &type, &proto))		return NULL;	/* Create a pair of socket fds */	if (socketpair(family, type, proto, sv) < 0)		return set_error();	s0 = new_sockobject(sv[0], family, type, proto);	if (s0 == NULL)		goto finally;	s1 = new_sockobject(sv[1], family, type, proto);	if (s1 == NULL)		goto finally;	res = PyTuple_Pack(2, s0, s1); finally:	if (res == NULL) {		if (s0 == NULL)			SOCKETCLOSE(sv[0]);		if (s1 == NULL)			SOCKETCLOSE(sv[1]);	}	Py_XDECREF(s0);	Py_XDECREF(s1);	return res;} PyDoc_STRVAR(socketpair_doc,"socketpair([family[, type[, proto]]]) -> (socket object, socket object)\n\\n\Create a pair of socket objects from the sockets returned by the platform\n\socketpair() function.\n\The arguments are the same as for socket() except the default family is\n\AF_UNIX if defined on the platform; otherwise, the default is AF_INET."); #endif /* HAVE_SOCKETPAIR */  #ifndef NO_DUP/* Create a socket object from a numeric file description.   Useful e.g. if stdin is a socket.   Additional arguments as for socket(). */ /*ARGSUSED*/static PyObject *socket_fromfd(PyObject *self, PyObject *args){	PySocketSockObject *s;	SOCKET_T fd;	int family, type, proto = 0;	if (!PyArg_ParseTuple(args, "iii|i:fromfd",			      &fd, &family, &type, &proto))		return NULL;	/* Dup the fd so it and the socket can be closed independently */	fd = dup(fd);	if (fd < 0)		return set_error();	s = new_sockobject(fd, family, type, proto);	return (PyObject *) s;} PyDoc_STRVAR(fromfd_doc,"fromfd(fd, family, type[, proto]) -> socket object\n\\n\Create a socket object from a duplicate of the given\n\file descriptor.\n\The remaining arguments are the same as for socket()."); #endif /* NO_DUP */  static PyObject *socket_ntohs(PyObject *self, PyObject *args){	int x1, x2; 	if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {		return NULL;	}	x2 = (int)ntohs((short)x1);	return PyInt_FromLong(x2);} PyDoc_STRVAR(ntohs_doc,"ntohs(integer) -> integer\n\\n\Convert a 16-bit integer from network to host byte order.");  static PyObject *socket_ntohl(PyObject *self, PyObject *arg){	unsigned long x; 	if (PyInt_Check(arg)) {		x = PyInt_AS_LONG(arg);

Lines 3001–3500 of 5106. Previous lines