Skip to content

Commit 8d9878c

Browse files
author
David Sommerseth
committed
Added new function: get_ipaddresses() - retrieves IPv4 and IPv6 addresses for all devices
1 parent 64d39e1 commit 8d9878c

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

python-ethtool/ethtool.c

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include <sys/ioctl.h>
2727
#include <sys/types.h>
2828

29+
#include "etherinfo.h"
30+
2931
#ifndef IFF_DYNAMIC
3032
#define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses*/
3133
#endif
@@ -42,6 +44,8 @@ typedef __uint8_t u8;
4244

4345
#define _PATH_PROCNET_DEV "/proc/net/dev"
4446

47+
struct etherinfo *ethernet_devices = NULL;
48+
4549
static PyObject *get_active_devices(PyObject *self __unused, PyObject *args __unused)
4650
{
4751
PyObject *list;
@@ -217,6 +221,32 @@ static PyObject *get_ipaddress(PyObject *self __unused, PyObject *args)
217221
return PyString_FromString(ipaddr);
218222
}
219223

224+
static PyObject *get_ipaddresses(PyObject *self __unused, PyObject *args) {
225+
PyObject *devlist = NULL;
226+
struct etherinfo *ethptr = NULL;
227+
228+
devlist = PyList_New(0);
229+
for( ethptr = ethernet_devices; ethptr->next != NULL; ethptr = ethptr->next) {
230+
if( ethptr->ipv4_address ) {
231+
PyObject *dev = PyList_New(0);
232+
PyList_Append(dev, PyString_FromString(ethptr->device));
233+
PyList_Append(dev, PyInt_FromLong(AF_INET));
234+
PyList_Append(dev, PyString_FromString(ethptr->ipv4_address));
235+
PyList_Append(devlist, dev);
236+
}
237+
if( ethptr->ipv6_address ) {
238+
PyObject *dev = PyList_New(0);
239+
PyList_Append(dev, PyString_FromString(ethptr->device));
240+
PyList_Append(dev, PyInt_FromLong(AF_INET6));
241+
PyList_Append(dev, PyString_FromString(ethptr->ipv6_address));
242+
PyList_Append(devlist, dev);
243+
}
244+
}
245+
246+
return devlist;
247+
}
248+
249+
220250
static PyObject *get_flags (PyObject *self __unused, PyObject *args)
221251
{
222252
struct ifreq ifr;
@@ -775,6 +805,11 @@ static struct PyMethodDef PyEthModuleMethods[] = {
775805
.ml_meth = (PyCFunction)get_ipaddress,
776806
.ml_flags = METH_VARARGS,
777807
},
808+
{
809+
.ml_name = "get_ipaddresses",
810+
.ml_meth = (PyCFunction)get_ipaddresses,
811+
.ml_flags = METH_VARARGS,
812+
},
778813
{
779814
.ml_name = "get_netmask",
780815
.ml_meth = (PyCFunction)get_netmask,
@@ -868,6 +903,9 @@ PyMODINIT_FUNC initethtool(void)
868903
PyModule_AddIntConstant(m, "IFF_PORTSEL", IFF_PORTSEL); /* Can set media type. */
869904
PyModule_AddIntConstant(m, "IFF_AUTOMEDIA", IFF_AUTOMEDIA); /* Auto media select active. */
870905
PyModule_AddIntConstant(m, "IFF_DYNAMIC", IFF_DYNAMIC); /* Dialup device with changing addresses. */
871-
}
906+
PyModule_AddIntConstant(m, "AF_INET", AF_INET); /* IPv4 interface */
907+
PyModule_AddIntConstant(m, "AF_INET6", AF_INET6); /* IPv6 interface */
872908

909+
ethernet_devices = get_etherinfo();
910+
}
873911

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from distutils.core import setup, Extension
44

55
ethtool = Extension('ethtool',
6-
sources = ['python-ethtool/ethtool.c'])
6+
sources = ['python-ethtool/ethtool.c', 'python-ethtool/etherinfo.c'])
77

88
# don't reformat this line, Makefile parses it
99
setup(name='ethtool',

0 commit comments

Comments
 (0)