summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Sommerseth <davids@redhat.com>2009-08-24 13:20:31 +0200
committerDavid Sommerseth <davids@redhat.com>2009-08-24 13:20:31 +0200
commit8d9878c54d311f7532cb93412863d41431bd8fb1 (patch)
tree130d0c5aa7cb58eba48574f3c12961f3b626d727
parent64d39e125d1ff823d248c0cce04d492d4a2688de (diff)
downloadpython-ethtool-8d9878c54d311f7532cb93412863d41431bd8fb1.tar.gz
python-ethtool-8d9878c54d311f7532cb93412863d41431bd8fb1.tar.xz
python-ethtool-8d9878c54d311f7532cb93412863d41431bd8fb1.zip
Added new function: get_ipaddresses() - retrieves IPv4 and IPv6 addresses for all devices
-rw-r--r--python-ethtool/ethtool.c40
-rw-r--r--setup.py2
2 files changed, 40 insertions, 2 deletions
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 <sys/ioctl.h>
#include <sys/types.h>
+#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;
@@ -776,6 +806,11 @@ static struct PyMethodDef PyEthModuleMethods[] = {
.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,
.ml_flags = METH_VARARGS,
@@ -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();
+}
diff --git a/setup.py b/setup.py
index 285e1e3..662b92c 100644
--- a/setup.py
+++ b/setup.py
@@ -3,7 +3,7 @@
from distutils.core import setup, Extension
ethtool = Extension('ethtool',
- sources = ['python-ethtool/ethtool.c'])
+ sources = ['python-ethtool/ethtool.c', 'python-ethtool/etherinfo.c'])
# don't reformat this line, Makefile parses it
setup(name='ethtool',