From 8d9878c54d311f7532cb93412863d41431bd8fb1 Mon Sep 17 00:00:00 2001 From: David Sommerseth Date: Mon, 24 Aug 2009 13:20:31 +0200 Subject: Added new function: get_ipaddresses() - retrieves IPv4 and IPv6 addresses for all devices --- python-ethtool/ethtool.c | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) (limited to 'python-ethtool') diff --git a/python-ethtool/ethtool.c b/python-ethtool/ethtool.c index a8c0366..8d3d37c 100644 --- a/python-ethtool/ethtool.c +++ b/python-ethtool/ethtool.c @@ -26,6 +26,8 @@ #include #include +#include "etherinfo.h" + #ifndef IFF_DYNAMIC #define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses*/ #endif @@ -42,6 +44,8 @@ typedef __uint8_t u8; #define _PATH_PROCNET_DEV "/proc/net/dev" +struct etherinfo *ethernet_devices = NULL; + static PyObject *get_active_devices(PyObject *self __unused, PyObject *args __unused) { PyObject *list; @@ -217,6 +221,32 @@ static PyObject *get_ipaddress(PyObject *self __unused, PyObject *args) return PyString_FromString(ipaddr); } +static PyObject *get_ipaddresses(PyObject *self __unused, PyObject *args) { + PyObject *devlist = NULL; + struct etherinfo *ethptr = NULL; + + devlist = PyList_New(0); + for( ethptr = ethernet_devices; ethptr->next != NULL; ethptr = ethptr->next) { + if( ethptr->ipv4_address ) { + PyObject *dev = PyList_New(0); + PyList_Append(dev, PyString_FromString(ethptr->device)); + PyList_Append(dev, PyInt_FromLong(AF_INET)); + PyList_Append(dev, PyString_FromString(ethptr->ipv4_address)); + PyList_Append(devlist, dev); + } + if( ethptr->ipv6_address ) { + PyObject *dev = PyList_New(0); + PyList_Append(dev, PyString_FromString(ethptr->device)); + PyList_Append(dev, PyInt_FromLong(AF_INET6)); + PyList_Append(dev, PyString_FromString(ethptr->ipv6_address)); + PyList_Append(devlist, dev); + } + } + + return devlist; +} + + static PyObject *get_flags (PyObject *self __unused, PyObject *args) { struct ifreq ifr; @@ -775,6 +805,11 @@ static struct PyMethodDef PyEthModuleMethods[] = { .ml_meth = (PyCFunction)get_ipaddress, .ml_flags = METH_VARARGS, }, + { + .ml_name = "get_ipaddresses", + .ml_meth = (PyCFunction)get_ipaddresses, + .ml_flags = METH_VARARGS, + }, { .ml_name = "get_netmask", .ml_meth = (PyCFunction)get_netmask, @@ -868,6 +903,9 @@ PyMODINIT_FUNC initethtool(void) PyModule_AddIntConstant(m, "IFF_PORTSEL", IFF_PORTSEL); /* Can set media type. */ PyModule_AddIntConstant(m, "IFF_AUTOMEDIA", IFF_AUTOMEDIA); /* Auto media select active. */ PyModule_AddIntConstant(m, "IFF_DYNAMIC", IFF_DYNAMIC); /* Dialup device with changing addresses. */ -} + PyModule_AddIntConstant(m, "AF_INET", AF_INET); /* IPv4 interface */ + PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IPv6 interface */ + ethernet_devices = get_etherinfo(); +} -- cgit