Index: socketmodule.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Modules/socketmodule.c,v retrieving revision 1.249 diff -u -u -r1.249 socketmodule.c --- socketmodule.c 11 Dec 2002 13:10:57 -0000 1.249 +++ socketmodule.c 24 Dec 2002 21:04:06 -0000 @@ -2757,6 +2757,84 @@ return PyString_FromString(inet_ntoa(packed_addr)); } +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 for use\n\ +with low-level network functions."); + +/* Python interface to inet_pton(af, src, dst) */ +static PyObject * +socket_inet_pton(PyObject *self, PyObject *args) +{ + int af; + char* ip; + int retval; + char packed[16]; + + if (!PyArg_ParseTuple(args, "is:inet_pton", &af, &ip)) { + return NULL; + } + + retval = inet_pton(af, ip, &packed[0]); + 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[0], + sizeof(struct in_addr)); +#ifdef ENABLE_IPV6 + } else if (af == AF_INET6) { + return PyString_FromStringAndSize(&packed[0], + 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\ +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; /* ignored */ + const char* retval; +#ifdef ENABLE_IPV6 + char ip[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)]; +#else + char ip[INET_ADDRSTRLEN]; +#endif + if (!PyArg_ParseTuple(args, "is#:inet_ntop", &af, &packed, &len)) { + return NULL; + } + + retval = inet_ntop(af, packed, &ip[0], sizeof(ip)); + if (!retval) { + PyErr_SetFromErrno(socket_error); + return NULL; + } else if (af == AF_INET) { + return PyString_FromString(retval); +#ifdef ENABLE_IPV6 + } else if (af == AF_INET6) { + return PyString_FromString(retval); +#endif + } else { + PyErr_SetString(socket_error, "unknown address family"); + return NULL; + } +} + /* Python interface to getaddrinfo(host, port). */ /*ARGSUSED*/ @@ -2998,6 +3076,10 @@ METH_VARARGS, inet_aton_doc}, {"inet_ntoa", socket_inet_ntoa, METH_VARARGS, inet_ntoa_doc}, + {"inet_pton", socket_inet_pton, + METH_VARARGS, inet_pton_doc}, + {"inet_ntop", socket_inet_ntop, + METH_VARARGS, inet_ntop_doc}, {"getaddrinfo", socket_getaddrinfo, METH_VARARGS, getaddrinfo_doc}, {"getnameinfo", socket_getnameinfo,