summaryrefslogtreecommitdiffstats
path: root/python-ethtool/etherinfo_obj.c
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2013-01-15 22:10:09 -0500
committerDavid Malcolm <dmalcolm@redhat.com>2013-01-30 13:41:37 -0500
commit7ff5e2746b3955e50c92ffe61bc7390e6aecbfda (patch)
tree21373cb9d72b18f0c1579e964fb1de436eb5d939 /python-ethtool/etherinfo_obj.c
parentd33ad02be5a459a26451a1ae3c6506040894bee4 (diff)
downloadpython-ethtool-7ff5e2746b3955e50c92ffe61bc7390e6aecbfda.tar.gz
python-ethtool-7ff5e2746b3955e50c92ffe61bc7390e6aecbfda.tar.xz
python-ethtool-7ff5e2746b3955e50c92ffe61bc7390e6aecbfda.zip
Support devices with multiple IPv4 addresses
Add a get_ipv4_addresses() method to ethtool.etherinfo to support devices with multiple IPv4 addresses (rhbz#759150) Previously, get_etherinfo() made queries to NETLINK with NLQRY_ADDR, and callback_nl_address handled responses of family AF_INET (IPv4) by writing to fields within a struct etherinfo. If multiple AF_INET responses come back, each overwrote the last, and the last one won. This patch generalizes things by moving the relevant fields: char *ipv4_address; /**< Configured IPv4 address */ int ipv4_netmask; /**< Configured IPv4 netmask */ char *ipv4_broadcast; from (struct etherinfo) into a new Python class, currently named PyNetlinkIPv4Address. This object has a sane repr(): >>> ethtool.get_interfaces_info('eth1')[0].get_ipv4_addresses() [ethtool.NetlinkIPv4Address(address='192.168.1.10', netmask=24, broadcast='192.168.1.255')] and attributes: >>> print [iface.address for iface in ethtool.get_interfaces_info('eth1')[0].get_ipv4_addresses()] ['192.168.1.10'] >>> print [iface.netmask for iface in ethtool.get_interfaces_info('eth1')[0].get_ipv4_addresses()] [24] >>> print [iface.broadcast for iface in ethtool.get_interfaces_info('eth1')[0].get_ipv4_addresses()] ['192.168.1.255'] The (struct etherinfo) then gains a new field: PyObject *ipv4_addresses; /**< list of PyNetlinkIPv4Address instances */ which is created before starting the query, and populated by the callback as responses come in. All direct usage of the old fields (which assumed a single IPv4 address) are changed to use the last entry in the list (if any), to mimic the old behavior. dump_etherinfo() and _ethtool_etherinfo_str() are changed to loop over all of the IPv4 addresses when outputting, rather than just outputting one. Caveats: * the exact terminology is probably incorrect: I'm not a networking specialist * the relationship between each of devices, get_interfaces_info() results, and addresses seems both unclear and messy to me: how changable is the API? >>> ethtool.get_interfaces_info('eth1')[0].get_ipv4_addresses() [ethtool.NetlinkIPv4Address(address='192.168.1.10', netmask=24, broadcast='192.168.1.255')] It seems that an etherinfo object relates to a device: perhaps it should be named as such? But it may be too late to make this change. Notes: The _ethtool_etherinfo_members array within python-ethtool/etherinfo_obj.c was broken: it defined 4 attributes of type PyObject*, to be extracted from etherinfo_py->data, which is of a completed different type. If these PyMemberDef fields were ever used, Python would segfault. Thankfully _ethtool_etherinfo_getter() has handlers for these attributes, and gets called first. This is a modified version of the patch applied downstream in RHEL 6.4 within python-ethtool-0.6-3.el6: python-ethtool-0.6-add-get_ipv4_addresses-method.patch ported to take account of 508ffffbb3c48eeeb11eeab2bf971180fe4e1940
Diffstat (limited to 'python-ethtool/etherinfo_obj.c')
-rw-r--r--python-ethtool/etherinfo_obj.c131
1 files changed, 95 insertions, 36 deletions
diff --git a/python-ethtool/etherinfo_obj.c b/python-ethtool/etherinfo_obj.c
index b158a47..dad426b 100644
--- a/python-ethtool/etherinfo_obj.c
+++ b/python-ethtool/etherinfo_obj.c
@@ -91,6 +91,41 @@ int _ethtool_etherinfo_init(etherinfo_py *self, PyObject *args, PyObject *kwds)
return 0;
}
+/*
+ The old approach of having a single IPv4 address per device meant each result
+ that came in from netlink overwrote the old result.
+
+ Mimic it by returning the last entry in the list (if any).
+
+ The return value is a *borrowed reference* (or NULL)
+*/
+static PyNetlinkIPv4Address*
+get_last_address(etherinfo_py *self)
+{
+ Py_ssize_t size;
+ PyObject *list;
+
+ assert(self);
+ list = self->data->ethinfo->ipv4_addresses;
+ if (!list) {
+ return NULL;
+ }
+
+ if (!PyList_Check(list)) {
+ return NULL;
+ }
+
+ size = PyList_Size(list);
+ if (size > 0) {
+ PyObject *item = PyList_GetItem(list, size - 1);
+ if (Py_TYPE(item) == &ethtool_netlink_ipv4_address_Type) {
+ return (PyNetlinkIPv4Address*)item;
+ }
+ }
+
+ return NULL;
+}
+
/**
* ethtool.etherinfo function for retrieving data from a Python object.
*
@@ -102,6 +137,7 @@ int _ethtool_etherinfo_init(etherinfo_py *self, PyObject *args, PyObject *kwds)
PyObject *_ethtool_etherinfo_getter(etherinfo_py *self, PyObject *attr_o)
{
char *attr = PyString_AsString(attr_o);
+ PyNetlinkIPv4Address *py_addr;
if( !self || !self->data ) {
PyErr_SetString(PyExc_AttributeError, "No data available");
@@ -115,13 +151,32 @@ PyObject *_ethtool_etherinfo_getter(etherinfo_py *self, PyObject *attr_o)
return RETURN_STRING(self->data->ethinfo->hwaddress);
} else if( strcmp(attr, "ipv4_address") == 0 ) {
get_etherinfo(self->data, NLQRY_ADDR);
- return RETURN_STRING(self->data->ethinfo->ipv4_address);
+ /* For compatiblity with old approach, return last IPv4 address: */
+ py_addr = get_last_address(self);
+ if (py_addr) {
+ if (py_addr->ipv4_address) {
+ Py_INCREF(py_addr->ipv4_address);
+ return py_addr->ipv4_address;
+ }
+ }
+ Py_RETURN_NONE;
} else if( strcmp(attr, "ipv4_netmask") == 0 ) {
get_etherinfo(self->data, NLQRY_ADDR);
- return PyInt_FromLong(self->data->ethinfo->ipv4_netmask);
+ py_addr = get_last_address(self);
+ if (py_addr) {
+ return PyInt_FromLong(py_addr->ipv4_netmask);
+ }
+ return PyInt_FromLong(0);
} else if( strcmp(attr, "ipv4_broadcast") == 0 ) {
get_etherinfo(self->data, NLQRY_ADDR);
- return RETURN_STRING(self->data->ethinfo->ipv4_broadcast);
+ py_addr = get_last_address(self);
+ if (py_addr) {
+ if (py_addr->ipv4_broadcast) {
+ Py_INCREF(py_addr->ipv4_broadcast);
+ return py_addr->ipv4_broadcast;
+ }
+ }
+ Py_RETURN_NONE;
} else {
return PyObject_GenericGetAttr((PyObject *)self, attr_o);
}
@@ -170,19 +225,21 @@ PyObject *_ethtool_etherinfo_str(etherinfo_py *self)
Py_DECREF(tmp);
}
- if( self->data->ethinfo->ipv4_address ) {
- PyObject *tmp = PyString_FromFormat("\tIPv4 address: %s/%i",
- self->data->ethinfo->ipv4_address,
- self->data->ethinfo->ipv4_netmask);
- if( self->data->ethinfo->ipv4_broadcast ) {
- PyObject *tmp2 = PyString_FromFormat(" Broadcast: %s",
- self->data->ethinfo->ipv4_broadcast);
- PyString_Concat(&tmp, tmp2);
- Py_DECREF(tmp2);
- }
- PyString_Concat(&tmp, PyString_FromString("\n"));
- PyString_Concat(&ret, tmp);
- Py_DECREF(tmp);
+ if( self->data->ethinfo->ipv4_addresses ) {
+ Py_ssize_t i;
+ for (i = 0; i < PyList_Size(self->data->ethinfo->ipv4_addresses); i++) {
+ PyNetlinkIPv4Address *py_addr = (PyNetlinkIPv4Address *)PyList_GetItem(self->data->ethinfo->ipv4_addresses, i);
+ PyObject *tmp = PyString_FromFormat("\tIPv4 address: ");
+ PyString_Concat(&tmp, py_addr->ipv4_address);
+ PyString_ConcatAndDel(&tmp, PyString_FromFormat("/%d", py_addr->ipv4_netmask));
+ if (py_addr->ipv4_broadcast ) {
+ PyString_ConcatAndDel(&tmp,
+ PyString_FromString(" Broadcast: "));
+ PyString_Concat(&tmp, py_addr->ipv4_broadcast);
+ }
+ PyString_ConcatAndDel(&tmp, PyString_FromString("\n"));
+ PyString_ConcatAndDel(&ret, tmp);
+ }
}
if( self->data->ethinfo->ipv6_addresses ) {
@@ -203,6 +260,24 @@ PyObject *_ethtool_etherinfo_str(etherinfo_py *self)
return ret;
}
+static PyObject *
+_ethtool_etherinfo_get_ipv4_addresses(etherinfo_py *self, PyObject *notused) {
+ PyObject *ret;
+
+ if( !self || !self->data ) {
+ PyErr_SetString(PyExc_AttributeError, "No data available");
+ return NULL;
+ }
+
+ get_etherinfo(self->data, NLQRY_ADDR);
+
+ /* Transfer ownership of reference: */
+ ret = self->data->ethinfo->ipv4_addresses;
+ self->data->ethinfo->ipv4_addresses = NULL;
+
+ return ret;
+}
+
/**
* Returns a tuple list of ethertool.etherinfo_ipv6addr objects, containing configured
@@ -276,30 +351,14 @@ PyObject * _ethtool_etherinfo_get_ipv6_addresses(etherinfo_py *self, PyObject *n
*
*/
static PyMethodDef _ethtool_etherinfo_methods[] = {
- {"get_ipv6_addresses", _ethtool_etherinfo_get_ipv6_addresses, METH_NOARGS,
+ {"get_ipv4_addresses", (PyCFunction)_ethtool_etherinfo_get_ipv4_addresses, METH_NOARGS,
+ "Retrieve configured IPv4 addresses. Returns a list of NetlinkIP4Address objects"},
+ {"get_ipv6_addresses", (PyCFunction)_ethtool_etherinfo_get_ipv6_addresses, METH_NOARGS,
"Retrieve configured IPv6 addresses. Returns a tuple list of etherinfo_ipv6addr objects"},
{NULL} /**< No methods defined */
};
/**
- * Defines all accessible object members
- *
- */
-static PyMemberDef _ethtool_etherinfo_members[] = {
- {"device", T_OBJECT_EX, offsetof(etherinfo_py, data), 0,
- "Device name of the interface"},
- {"mac_address", T_OBJECT_EX, offsetof(etherinfo_py, data), 0,
- "MAC address / hardware address of the interface"},
- {"ipv4_address", T_OBJECT_EX, offsetof(etherinfo_py, data), 0,
- "IPv4 address"},
- {"ipv4_netmask", T_INT, offsetof(etherinfo_py, data), 0,
- "IPv4 netmask in bits"},
- {"ipv4_broadcast", T_OBJECT_EX, offsetof(etherinfo_py, data), 0,
- "IPv4 broadcast address"},
- {NULL} /* End of member list */
-};
-
-/**
* Definition of the functions a Python class/object requires.
*
*/
@@ -333,7 +392,7 @@ PyTypeObject ethtool_etherinfoType = {
0, /* tp_iter */
0, /* tp_iternext */
_ethtool_etherinfo_methods, /* tp_methods */
- _ethtool_etherinfo_members, /* tp_members */
+ 0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */