Index: Doc/lib/libsocket.tex =================================================================== --- Doc/lib/libsocket.tex (revision 56147) +++ Doc/lib/libsocket.tex (working copy) @@ -448,6 +448,22 @@ \versionadded{2.3} \end{funcdesc} +\begin{funcdesc}{if_nameindex}{} +Returns a list of tuples in the form +\code{(\var{interface_index}, \var{interface_name)}}. +\versionadded{2.6} +\end{funcdesc} + +\begin{funcdesc}{if_nametoindex}{if_name} +Returns the interface index corresponding to \var{if_name}. +\versionadded{2.6} +\end{funcdesc} + +\begin{funcdesc}{if_indextoname}{if_index} +Returns the interface name corresponding to \var{if_index}. +\versionadded{2.6} +\end{funcdesc} + \begin{datadesc}{SocketType} This is a Python type object that represents the socket object type. It is the same as \code{type(socket(...))}. Index: Modules/socketmodule.c =================================================================== --- Modules/socketmodule.c (revision 56147) +++ Modules/socketmodule.c (working copy) @@ -42,6 +42,9 @@ - socket.inet_ntoa(packed IP) -> IP address string - socket.getdefaulttimeout() -> None | float - socket.setdefaulttimeout(None | float) +- socket.if_nameindex() -> list of tuples (if_index, if_name) +- socket.if_nametoindex(name) -> corresponding interface index +- socket.if_indextoname(index) -> corresponding interface name - an Internet socket address is a pair (hostname, port) where hostname can be anything recognized by gethostbyname() (including the dd.dd.dd.dd notation) and port is in host byte order @@ -115,6 +118,9 @@ setsockopt(level, optname, value) -- set socket options\n\ settimeout(None | float) -- set or clear the timeout\n\ shutdown(how) -- shut down traffic in one or both directions\n\ +if_nameindex() -- return all network interface names and indexes\n\ +if_nametoindex(name) -- returns the corresponding interface index\n\ +if_indextoname(index) -- returns the corresponding interface name\n\ \n\ [*] not available on all platforms!"); @@ -4082,6 +4088,115 @@ When the socket module is first imported, the default is None."); +#ifdef ENABLE_IPV6 +/* Python API for getting interface indices and names for use with IPV6 */ + +static PyObject * +socket_if_nameindex(PyObject *self, PyObject *arg) +{ + + + struct if_nameindex *ni = if_nameindex(); + + if (ni == NULL) { + PyErr_SetString(socket_error, "not enough memory"); + return NULL; + } + + int i = 0; + + PyObject* list = PyList_New(0); + + while (ni[i].if_index != 0) { + PyObject *index = PyInt_FromLong(ni[i].if_index); + PyObject *name = PyString_FromString(ni[i].if_name); + + PyObject *ni_tuple = PyTuple_Pack(2, index,name); + + Py_DECREF(index); + Py_DECREF(name); + + PyList_Append(list, ni_tuple); + + Py_DECREF(ni_tuple); + + i++; + } + + if_freenameindex(ni); + + return list; + + +} + +PyDoc_STRVAR(if_nameindex_doc, +"if_nameindex()\n\ +\n\ +The if_nameindex function returns a list of tuples in the form\n\ +(interface_index, interface_name)."); + + +PyObject* +socket_if_nametoindex(PyObject *self, PyObject *arg) +{ + + char* ifname = PyString_AsString(arg); + + if (ifname == NULL) + return NULL; + + int index = if_nametoindex(ifname); + + if (index == 0) { + PyErr_SetString(socket_error, "no interface with this name"); + return NULL; + } + + return PyInt_FromLong(index); + +} + +PyDoc_STRVAR(if_nametoindex_doc, +"if_nametoindex(if_name)\n\ +\n\ +The if_nametoindex() function returns the interface index corresponding to\n\ +the interface name if_name."); + + +PyObject* +socket_if_indextoname(PyObject *self, PyObject *arg) +{ + + int index = PyInt_AsLong(arg); + + if (index == -1) { + if (PyErr_Occurred()) + return NULL; + } + + char name[IF_NAMESIZE + 1]; + + char *ret = if_indextoname(index, &name[0]); + + if (ret == NULL) { + PyErr_SetString(socket_error, "no interface with this index"); + return NULL; + } + + return PyString_FromString(name); + +} + +PyDoc_STRVAR(if_indextoname_doc, +"if_indextoname(if_index)\n\ +\n\ +The if_indextoname() function returns the interface bane corresponding to\n\ +the interface index if_index."); + +#endif + + /* List of functions exported by this module. */ static PyMethodDef socket_methods[] = { @@ -4133,6 +4248,14 @@ METH_NOARGS, getdefaulttimeout_doc}, {"setdefaulttimeout", socket_setdefaulttimeout, METH_O, setdefaulttimeout_doc}, +#ifdef ENABLE_IPV6 + {"if_nameindex", socket_if_nameindex, + METH_NOARGS, if_nameindex_doc}, + {"if_nametoindex", socket_if_nametoindex, + METH_O, if_nametoindex_doc}, + {"if_indextoname", socket_if_indextoname, + METH_O, if_indextoname_doc}, +#endif {NULL, NULL} /* Sentinel */ };