if (x == (unsigned long) -1 && PyErr_Occurred()) return NULL; } else if (PyLong_Check(arg)) { x = PyLong_AsUnsignedLong(arg); if (x == (unsigned long) -1 && PyErr_Occurred()) return NULL;#if SIZEOF_LONG > 4 { unsigned long y; /* only want the trailing 32 bits */ y = x & 0xFFFFFFFFUL; if (y ^ x) return PyErr_Format(PyExc_OverflowError, "long int larger than 32 bits"); x = y; }#endif } else return PyErr_Format(PyExc_TypeError, "expected int/long, %s found", arg->ob_type->tp_name); if (x == (unsigned long) -1 && PyErr_Occurred()) return NULL; return PyInt_FromLong(ntohl(x));} PyDoc_STRVAR(ntohl_doc,"ntohl(integer) -> integer\n\\n\Convert a 32-bit integer from network to host byte order."); static PyObject *socket_htons(PyObject *self, PyObject *args){ int x1, x2; if (!PyArg_ParseTuple(args, "i:htons", &x1)) { return NULL; } x2 = (int)htons((short)x1); return PyInt_FromLong(x2);} PyDoc_STRVAR(htons_doc,"htons(integer) -> integer\n\\n\Convert a 16-bit integer from host to network byte order."); static PyObject *socket_htonl(PyObject *self, PyObject *arg){ unsigned long x; if (PyInt_Check(arg)) { x = PyInt_AS_LONG(arg); if (x == (unsigned long) -1 && PyErr_Occurred()) return NULL; } else if (PyLong_Check(arg)) { x = PyLong_AsUnsignedLong(arg); if (x == (unsigned long) -1 && PyErr_Occurred()) return NULL;#if SIZEOF_LONG > 4 { unsigned long y; /* only want the trailing 32 bits */ y = x & 0xFFFFFFFFUL; if (y ^ x) return PyErr_Format(PyExc_OverflowError, "long int larger than 32 bits"); x = y; }#endif } else return PyErr_Format(PyExc_TypeError, "expected int/long, %s found", arg->ob_type->tp_name); return PyInt_FromLong(htonl(x));} PyDoc_STRVAR(htonl_doc,"htonl(integer) -> integer\n\\n\Convert a 32-bit integer from host to network byte order."); /* socket.inet_aton() and socket.inet_ntoa() functions. */ PyDoc_STRVAR(inet_aton_doc,"inet_aton(string) -> packed 32-bit IP representation\n\\n\Convert an IP address in string format (123.45.67.89) to the 32-bit packed\n\binary format used in low-level network functions."); static PyObject*socket_inet_aton(PyObject *self, PyObject *args){#ifndef INADDR_NONE#define INADDR_NONE (-1)#endif#ifdef HAVE_INET_ATON struct in_addr buf;#endif #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) /* Have to use inet_addr() instead */ unsigned long packed_addr;#endif char *ip_addr; if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) return NULL; #ifdef HAVE_INET_ATON #ifdef USE_INET_ATON_WEAKLINK if (inet_aton != NULL) {#endif if (inet_aton(ip_addr, &buf)) return PyString_FromStringAndSize((char *)(&buf), sizeof(buf)); PyErr_SetString(socket_error, "illegal IP address string passed to inet_aton"); return NULL; #ifdef USE_INET_ATON_WEAKLINK } else {#endif #endif #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) /* special-case this address as inet_addr might return INADDR_NONE * for this */ if (strcmp(ip_addr, "255.255.255.255") == 0) { packed_addr = 0xFFFFFFFF; } else { packed_addr = inet_addr(ip_addr); if (packed_addr == INADDR_NONE) { /* invalid address */ PyErr_SetString(socket_error, "illegal IP address string passed to inet_aton"); return NULL; } } return PyString_FromStringAndSize((char *) &packed_addr, sizeof(packed_addr)); #ifdef USE_INET_ATON_WEAKLINK }#endif #endif} PyDoc_STRVAR(inet_ntoa_doc,"inet_ntoa(packed_ip) -> ip_address_string\n\\n\Convert an IP address from 32-bit packed binary format to string format"); static PyObject*socket_inet_ntoa(PyObject *self, PyObject *args){ char *packed_str; int addr_len; struct in_addr packed_addr; if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) { return NULL; } if (addr_len != sizeof(packed_addr)) { PyErr_SetString(socket_error, "packed IP wrong length for inet_ntoa"); return NULL; } memcpy(&packed_addr, packed_str, addr_len); return PyString_FromString(inet_ntoa(packed_addr));} #ifdef HAVE_INET_PTON PyDoc_STRVAR(inet_pton_doc,"inet_pton(af, ip) -> packed IP address string\n\\n\Convert an IP address from string format to a packed string suitable\n\for use with low-level network functions."); static PyObject *socket_inet_pton(PyObject *self, PyObject *args){ int af; char* ip; int retval;#ifdef ENABLE_IPV6 char packed[MAX(sizeof(struct in_addr), sizeof(struct in6_addr))];#else char packed[sizeof(struct in_addr)];#endif if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) { return NULL; } #if !defined(ENABLE_IPV6) && defined(AF_INET6) if(af == AF_INET6) { PyErr_SetString(socket_error, "can't use AF_INET6, IPv6 is disabled"); return NULL; }#endif retval = inet_pton(af, ip, packed); if (retval < 0) { PyErr_SetFromErrno(socket_error); return NULL; } else if (retval == 0) { PyErr_SetString(socket_error, "illegal IP address string passed to inet_pton"); return NULL; } else if (af == AF_INET) { return PyString_FromStringAndSize(packed, sizeof(struct in_addr));#ifdef ENABLE_IPV6 } else if (af == AF_INET6) { return PyString_FromStringAndSize(packed, sizeof(struct in6_addr));#endif } else { PyErr_SetString(socket_error, "unknown address family"); return NULL; }} PyDoc_STRVAR(inet_ntop_doc,"inet_ntop(af, packed_ip) -> string formatted IP address\n\\n\Convert a packed IP address of the given family to string format."); static PyObject *socket_inet_ntop(PyObject *self, PyObject *args){ int af; char* packed; int len; const char* retval;#ifdef ENABLE_IPV6 char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1];#else char ip[INET_ADDRSTRLEN + 1];#endif /* Guarantee NUL-termination for PyString_FromString() below */ memset((void *) &ip[0], '\0', sizeof(ip)); if (!PyArg_ParseTuple(args, "is#:inet_ntop", &af, &packed, &len)) { return NULL; } if (af == AF_INET) { if (len != sizeof(struct in_addr)) { PyErr_SetString(PyExc_ValueError, "invalid length of packed IP address string"); return NULL; }#ifdef ENABLE_IPV6 } else if (af == AF_INET6) { if (len != sizeof(struct in6_addr)) { PyErr_SetString(PyExc_ValueError, "invalid length of packed IP address string"); return NULL; }#endif } else { PyErr_Format(PyExc_ValueError, "unknown address family %d", af); return NULL; } retval = inet_ntop(af, packed, ip, sizeof(ip)); if (!retval) { PyErr_SetFromErrno(socket_error); return NULL; } else { return PyString_FromString(retval); } /* NOTREACHED */ PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop"); return NULL;} #endif /* HAVE_INET_PTON */ /* Python interface to getaddrinfo(host, port). */ /*ARGSUSED*/static PyObject *socket_getaddrinfo(PyObject *self, PyObject *args){ struct addrinfo hints, *res; struct addrinfo *res0 = NULL; PyObject *hobj = NULL; PyObject *pobj = (PyObject *)NULL; char pbuf[30]; char *hptr, *pptr; int family, socktype, protocol, flags; int error; PyObject *all = (PyObject *)NULL; PyObject *single = (PyObject *)NULL; PyObject *idna = NULL; family = socktype = protocol = flags = 0; family = AF_UNSPEC; if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo", &hobj, &pobj, &family, &socktype, &protocol, &flags)) { return NULL; } if (hobj == Py_None) { hptr = NULL; } else if (PyUnicode_Check(hobj)) { idna = PyObject_CallMethod(hobj, "encode", "s", "idna"); if (!idna) return NULL; hptr = PyString_AsString(idna); } else if (PyString_Check(hobj)) { hptr = PyString_AsString(hobj); } else { PyErr_SetString(PyExc_TypeError, "getaddrinfo() argument 1 must be string or None"); return NULL; } if (PyInt_Check(pobj)) { PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", PyInt_AsLong(pobj)); pptr = pbuf; } else if (PyString_Check(pobj)) { pptr = PyString_AsString(pobj); } else if (pobj == Py_None) { pptr = (char *)NULL; } else { PyErr_SetString(socket_error, "Int or String expected"); goto err; } memset(&hints, 0, sizeof(hints)); hints.ai_family = family; hints.ai_socktype = socktype; hints.ai_protocol = protocol; hints.ai_flags = flags; Py_BEGIN_ALLOW_THREADS ACQUIRE_GETADDRINFO_LOCK error = getaddrinfo(hptr, pptr, &hints, &res0); Py_END_ALLOW_THREADS RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ if (error) { set_gaierror(error); goto err; } if ((all = PyList_New(0)) == NULL) goto err; for (res = res0; res; res = res->ai_next) { PyObject *addr = makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol); if (addr == NULL) goto err; single = Py_BuildValue("iiisO", res->ai_family, res->ai_socktype, res->ai_protocol, res->ai_canonname ? res->ai_canonname : "", addr); Py_DECREF(addr); if (single == NULL) goto err; if (PyList_Append(all, single)) goto err; Py_XDECREF(single); } Py_XDECREF(idna); if (res0) freeaddrinfo(res0); return all; err: Py_XDECREF(single); Py_XDECREF(all); Py_XDECREF(idna); if (res0) freeaddrinfo(res0); return (PyObject *)NULL;} PyDoc_STRVAR(getaddrinfo_doc,"getaddrinfo(host, port [, family, socktype, proto, flags])\n\ -> list of (family, socktype, proto, canonname, sockaddr)\n\\n\Resolve host and port into addrinfo struct."); /* Python interface to getnameinfo(sa, flags). */ /*ARGSUSED*/static PyObject *socket_getnameinfo(PyObject *self, PyObject *args){ PyObject *sa = (PyObject *)NULL; int flags; char *hostp; int port, flowinfo, scope_id; char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; struct addrinfo hints, *res = NULL; int error; PyObject *ret = (PyObject *)NULL; flags = flowinfo = scope_id = 0; if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags)) return NULL; if (!PyArg_ParseTuple(sa, "si|ii", &hostp, &port, &flowinfo, &scope_id)) return NULL; PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port); memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */ Py_BEGIN_ALLOW_THREADS ACQUIRE_GETADDRINFO_LOCK error = getaddrinfo(hostp, pbuf, &hints, &res); Py_END_ALLOW_THREADS RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ if (error) { set_gaierror(error); goto fail; } if (res->ai_next) { PyErr_SetString(socket_error, "sockaddr resolved to multiple addresses"); goto fail; } switch (res->ai_family) { case AF_INET: { char *t1; int t2; if (PyArg_ParseTuple(sa, "si", &t1, &t2) == 0) { PyErr_SetString(socket_error, "IPv4 sockaddr must be 2 tuple"); goto fail; } break; }#ifdef ENABLE_IPV6 case AF_INET6: { struct sockaddr_in6 *sin6; sin6 = (struct sockaddr_in6 *)res->ai_addr; sin6->sin6_flowinfo = flowinfo; sin6->sin6_scope_id = scope_id; break; }#endif } error = getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags); if (error) { set_gaierror(error); goto fail; } ret = Py_BuildValue("ss", hbuf, pbuf); fail: if (res) freeaddrinfo(res); return ret;} PyDoc_STRVAR(getnameinfo_doc,"getnameinfo(sockaddr, flags) --> (host, port)\n\\n\Get host and port for a sockaddr."); /* Python API to getting and setting the default timeout value. */ static PyObject *socket_getdefaulttimeout(PyObject *self){ if (defaulttimeout < 0.0) { Py_INCREF(Py_None); return Py_None; } else return PyFloat_FromDouble(defaulttimeout);}Lines 3501–4000 of 5106. Previous lines Load more
if (x == (unsigned long) -1 && PyErr_Occurred()) return NULL; } else if (PyLong_Check(arg)) { x = PyLong_AsUnsignedLong(arg); if (x == (unsigned long) -1 && PyErr_Occurred()) return NULL; #if SIZEOF_LONG > 4 { unsigned long y; /* only want the trailing 32 bits */ y = x & 0xFFFFFFFFUL; if (y ^ x) return PyErr_Format(PyExc_OverflowError, "long int larger than 32 bits"); x = y; } #endif } else return PyErr_Format(PyExc_TypeError, "expected int/long, %s found", arg->ob_type->tp_name); if (x == (unsigned long) -1 && PyErr_Occurred()) return NULL; return PyInt_FromLong(ntohl(x)); } PyDoc_STRVAR(ntohl_doc, "ntohl(integer) -> integer\n\ \n\ Convert a 32-bit integer from network to host byte order."); static PyObject * socket_htons(PyObject *self, PyObject *args) { int x1, x2; if (!PyArg_ParseTuple(args, "i:htons", &x1)) { return NULL; } x2 = (int)htons((short)x1); return PyInt_FromLong(x2); } PyDoc_STRVAR(htons_doc, "htons(integer) -> integer\n\ \n\ Convert a 16-bit integer from host to network byte order."); static PyObject * socket_htonl(PyObject *self, PyObject *arg) { unsigned long x; if (PyInt_Check(arg)) { x = PyInt_AS_LONG(arg); if (x == (unsigned long) -1 && PyErr_Occurred()) return NULL; } else if (PyLong_Check(arg)) { x = PyLong_AsUnsignedLong(arg); if (x == (unsigned long) -1 && PyErr_Occurred()) return NULL; #if SIZEOF_LONG > 4 { unsigned long y; /* only want the trailing 32 bits */ y = x & 0xFFFFFFFFUL; if (y ^ x) return PyErr_Format(PyExc_OverflowError, "long int larger than 32 bits"); x = y; } #endif } else return PyErr_Format(PyExc_TypeError, "expected int/long, %s found", arg->ob_type->tp_name); return PyInt_FromLong(htonl(x)); } PyDoc_STRVAR(htonl_doc, "htonl(integer) -> integer\n\ \n\ Convert a 32-bit integer from host to network byte order."); /* socket.inet_aton() and socket.inet_ntoa() functions. */ PyDoc_STRVAR(inet_aton_doc, "inet_aton(string) -> packed 32-bit IP representation\n\ \n\ Convert an IP address in string format (123.45.67.89) to the 32-bit packed\n\ binary format used in low-level network functions."); static PyObject* socket_inet_aton(PyObject *self, PyObject *args) { #ifndef INADDR_NONE #define INADDR_NONE (-1) #endif #ifdef HAVE_INET_ATON struct in_addr buf; #endif #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) /* Have to use inet_addr() instead */ unsigned long packed_addr; #endif char *ip_addr; if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) return NULL; #ifdef HAVE_INET_ATON #ifdef USE_INET_ATON_WEAKLINK if (inet_aton != NULL) { #endif if (inet_aton(ip_addr, &buf)) return PyString_FromStringAndSize((char *)(&buf), sizeof(buf)); PyErr_SetString(socket_error, "illegal IP address string passed to inet_aton"); return NULL; #ifdef USE_INET_ATON_WEAKLINK } else { #endif #endif #if !defined(HAVE_INET_ATON) || defined(USE_INET_ATON_WEAKLINK) /* special-case this address as inet_addr might return INADDR_NONE * for this */ if (strcmp(ip_addr, "255.255.255.255") == 0) { packed_addr = 0xFFFFFFFF; } else { packed_addr = inet_addr(ip_addr); if (packed_addr == INADDR_NONE) { /* invalid address */ PyErr_SetString(socket_error, "illegal IP address string passed to inet_aton"); return NULL; } } return PyString_FromStringAndSize((char *) &packed_addr, sizeof(packed_addr)); #ifdef USE_INET_ATON_WEAKLINK } #endif #endif } PyDoc_STRVAR(inet_ntoa_doc, "inet_ntoa(packed_ip) -> ip_address_string\n\ \n\ Convert an IP address from 32-bit packed binary format to string format"); static PyObject* socket_inet_ntoa(PyObject *self, PyObject *args) { char *packed_str; int addr_len; struct in_addr packed_addr; if (!PyArg_ParseTuple(args, "s#:inet_ntoa", &packed_str, &addr_len)) { return NULL; } if (addr_len != sizeof(packed_addr)) { PyErr_SetString(socket_error, "packed IP wrong length for inet_ntoa"); return NULL; } memcpy(&packed_addr, packed_str, addr_len); return PyString_FromString(inet_ntoa(packed_addr)); } #ifdef HAVE_INET_PTON PyDoc_STRVAR(inet_pton_doc, "inet_pton(af, ip) -> packed IP address string\n\ \n\ Convert an IP address from string format to a packed string suitable\n\ for use with low-level network functions."); static PyObject * socket_inet_pton(PyObject *self, PyObject *args) { int af; char* ip; int retval; #ifdef ENABLE_IPV6 char packed[MAX(sizeof(struct in_addr), sizeof(struct in6_addr))]; #else char packed[sizeof(struct in_addr)]; #endif if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) { return NULL; } #if !defined(ENABLE_IPV6) && defined(AF_INET6) if(af == AF_INET6) { PyErr_SetString(socket_error, "can't use AF_INET6, IPv6 is disabled"); return NULL; } #endif retval = inet_pton(af, ip, packed); if (retval < 0) { PyErr_SetFromErrno(socket_error); return NULL; } else if (retval == 0) { PyErr_SetString(socket_error, "illegal IP address string passed to inet_pton"); return NULL; } else if (af == AF_INET) { return PyString_FromStringAndSize(packed, sizeof(struct in_addr)); #ifdef ENABLE_IPV6 } else if (af == AF_INET6) { return PyString_FromStringAndSize(packed, sizeof(struct in6_addr)); #endif } else { PyErr_SetString(socket_error, "unknown address family"); return NULL; } } PyDoc_STRVAR(inet_ntop_doc, "inet_ntop(af, packed_ip) -> string formatted IP address\n\ \n\ Convert a packed IP address of the given family to string format."); static PyObject * socket_inet_ntop(PyObject *self, PyObject *args) { int af; char* packed; int len; const char* retval; #ifdef ENABLE_IPV6 char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) + 1]; #else char ip[INET_ADDRSTRLEN + 1]; #endif /* Guarantee NUL-termination for PyString_FromString() below */ memset((void *) &ip[0], '\0', sizeof(ip)); if (!PyArg_ParseTuple(args, "is#:inet_ntop", &af, &packed, &len)) { return NULL; } if (af == AF_INET) { if (len != sizeof(struct in_addr)) { PyErr_SetString(PyExc_ValueError, "invalid length of packed IP address string"); return NULL; } #ifdef ENABLE_IPV6 } else if (af == AF_INET6) { if (len != sizeof(struct in6_addr)) { PyErr_SetString(PyExc_ValueError, "invalid length of packed IP address string"); return NULL; } #endif } else { PyErr_Format(PyExc_ValueError, "unknown address family %d", af); return NULL; } retval = inet_ntop(af, packed, ip, sizeof(ip)); if (!retval) { PyErr_SetFromErrno(socket_error); return NULL; } else { return PyString_FromString(retval); } /* NOTREACHED */ PyErr_SetString(PyExc_RuntimeError, "invalid handling of inet_ntop"); return NULL; } #endif /* HAVE_INET_PTON */ /* Python interface to getaddrinfo(host, port). */ /*ARGSUSED*/ static PyObject * socket_getaddrinfo(PyObject *self, PyObject *args) { struct addrinfo hints, *res; struct addrinfo *res0 = NULL; PyObject *hobj = NULL; PyObject *pobj = (PyObject *)NULL; char pbuf[30]; char *hptr, *pptr; int family, socktype, protocol, flags; int error; PyObject *all = (PyObject *)NULL; PyObject *single = (PyObject *)NULL; PyObject *idna = NULL; family = socktype = protocol = flags = 0; family = AF_UNSPEC; if (!PyArg_ParseTuple(args, "OO|iiii:getaddrinfo", &hobj, &pobj, &family, &socktype, &protocol, &flags)) { return NULL; } if (hobj == Py_None) { hptr = NULL; } else if (PyUnicode_Check(hobj)) { idna = PyObject_CallMethod(hobj, "encode", "s", "idna"); if (!idna) return NULL; hptr = PyString_AsString(idna); } else if (PyString_Check(hobj)) { hptr = PyString_AsString(hobj); } else { PyErr_SetString(PyExc_TypeError, "getaddrinfo() argument 1 must be string or None"); return NULL; } if (PyInt_Check(pobj)) { PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", PyInt_AsLong(pobj)); pptr = pbuf; } else if (PyString_Check(pobj)) { pptr = PyString_AsString(pobj); } else if (pobj == Py_None) { pptr = (char *)NULL; } else { PyErr_SetString(socket_error, "Int or String expected"); goto err; } memset(&hints, 0, sizeof(hints)); hints.ai_family = family; hints.ai_socktype = socktype; hints.ai_protocol = protocol; hints.ai_flags = flags; Py_BEGIN_ALLOW_THREADS ACQUIRE_GETADDRINFO_LOCK error = getaddrinfo(hptr, pptr, &hints, &res0); Py_END_ALLOW_THREADS RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ if (error) { set_gaierror(error); goto err; } if ((all = PyList_New(0)) == NULL) goto err; for (res = res0; res; res = res->ai_next) { PyObject *addr = makesockaddr(-1, res->ai_addr, res->ai_addrlen, protocol); if (addr == NULL) goto err; single = Py_BuildValue("iiisO", res->ai_family, res->ai_socktype, res->ai_protocol, res->ai_canonname ? res->ai_canonname : "", addr); Py_DECREF(addr); if (single == NULL) goto err; if (PyList_Append(all, single)) goto err; Py_XDECREF(single); } Py_XDECREF(idna); if (res0) freeaddrinfo(res0); return all; err: Py_XDECREF(single); Py_XDECREF(all); Py_XDECREF(idna); if (res0) freeaddrinfo(res0); return (PyObject *)NULL; } PyDoc_STRVAR(getaddrinfo_doc, "getaddrinfo(host, port [, family, socktype, proto, flags])\n\ -> list of (family, socktype, proto, canonname, sockaddr)\n\ \n\ Resolve host and port into addrinfo struct."); /* Python interface to getnameinfo(sa, flags). */ /*ARGSUSED*/ static PyObject * socket_getnameinfo(PyObject *self, PyObject *args) { PyObject *sa = (PyObject *)NULL; int flags; char *hostp; int port, flowinfo, scope_id; char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; struct addrinfo hints, *res = NULL; int error; PyObject *ret = (PyObject *)NULL; flags = flowinfo = scope_id = 0; if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags)) return NULL; if (!PyArg_ParseTuple(sa, "si|ii", &hostp, &port, &flowinfo, &scope_id)) return NULL; PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port); memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; /* make numeric port happy */ Py_BEGIN_ALLOW_THREADS ACQUIRE_GETADDRINFO_LOCK error = getaddrinfo(hostp, pbuf, &hints, &res); Py_END_ALLOW_THREADS RELEASE_GETADDRINFO_LOCK /* see comment in setipaddr() */ if (error) { set_gaierror(error); goto fail; } if (res->ai_next) { PyErr_SetString(socket_error, "sockaddr resolved to multiple addresses"); goto fail; } switch (res->ai_family) { case AF_INET: { char *t1; int t2; if (PyArg_ParseTuple(sa, "si", &t1, &t2) == 0) { PyErr_SetString(socket_error, "IPv4 sockaddr must be 2 tuple"); goto fail; } break; } #ifdef ENABLE_IPV6 case AF_INET6: { struct sockaddr_in6 *sin6; sin6 = (struct sockaddr_in6 *)res->ai_addr; sin6->sin6_flowinfo = flowinfo; sin6->sin6_scope_id = scope_id; break; } #endif } error = getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), flags); if (error) { set_gaierror(error); goto fail; } ret = Py_BuildValue("ss", hbuf, pbuf); fail: if (res) freeaddrinfo(res); return ret; } PyDoc_STRVAR(getnameinfo_doc, "getnameinfo(sockaddr, flags) --> (host, port)\n\ \n\ Get host and port for a sockaddr."); /* Python API to getting and setting the default timeout value. */ static PyObject * socket_getdefaulttimeout(PyObject *self) { if (defaulttimeout < 0.0) { Py_INCREF(Py_None); return Py_None; } else return PyFloat_FromDouble(defaulttimeout); }