diff options
author | David Malcolm <dmalcolm@redhat.com> | 2013-01-15 22:10:09 -0500 |
---|---|---|
committer | David Malcolm <dmalcolm@redhat.com> | 2013-01-30 13:41:37 -0500 |
commit | 7ff5e2746b3955e50c92ffe61bc7390e6aecbfda (patch) | |
tree | 21373cb9d72b18f0c1579e964fb1de436eb5d939 /python-ethtool/netlink-address.c | |
parent | d33ad02be5a459a26451a1ae3c6506040894bee4 (diff) | |
download | python-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/netlink-address.c')
-rw-r--r-- | python-ethtool/netlink-address.c | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/python-ethtool/netlink-address.c b/python-ethtool/netlink-address.c new file mode 100644 index 0000000..21976be --- /dev/null +++ b/python-ethtool/netlink-address.c @@ -0,0 +1,162 @@ +/* + * Copyright (C) 2011, 2012 Red Hat Inc. + * + * David Malcolm <dmalcolm@redhat.com> + * + * This application is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; version 2. + * + * This application is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + */ + +/* Python object corresponding to a (struct rtnl_addr) */ +#include <Python.h> +#include "structmember.h" + +#include <netlink/route/rtnl.h> +#include "etherinfo_struct.h" +#include "etherinfo.h" + +/* IPv4 Addresses: */ +static PyObject * +PyNetlinkIPv4Address_from_rtnl_addr(struct nl_object *nl_obj, struct rtnl_addr *addr) +{ + PyNetlinkIPv4Address *py_obj; + char buf[INET_ADDRSTRLEN+1]; + struct nl_addr *brdcst; + + py_obj = PyObject_New(PyNetlinkIPv4Address, + ðtool_netlink_ipv4_address_Type); + if (!py_obj) { + return NULL; + } + + /* Set ipv4_address: */ + memset(&buf, 0, sizeof(buf)); + if (!inet_ntop(AF_INET, nl_addr_get_binary_addr((struct nl_addr *)addr), + buf, sizeof(buf))) { + PyErr_SetFromErrno(PyExc_RuntimeError); + goto error; + } + py_obj->ipv4_address = PyString_FromString(buf); + if (!py_obj->ipv4_address) { + goto error; + } + + /* Set ipv4_netmask: */ + py_obj->ipv4_netmask = rtnl_addr_get_prefixlen((struct rtnl_addr*)nl_obj); + + /* Set ipv4_broadcast: */ + py_obj->ipv4_broadcast = NULL; + brdcst = rtnl_addr_get_broadcast((struct rtnl_addr*)nl_obj); + if( brdcst ) { + memset(&buf, 0, sizeof(buf)); + if (!inet_ntop(AF_INET, nl_addr_get_binary_addr(brdcst), + buf, sizeof(buf))) { + PyErr_SetFromErrno(PyExc_RuntimeError); + goto error; + } + py_obj->ipv4_broadcast = PyString_FromString(buf); + if (!py_obj->ipv4_broadcast) { + goto error; + } + } + + return (PyObject*)py_obj; + + error: + Py_DECREF(py_obj); + return NULL; +} + +static void +netlink_ipv4_address_dealloc(PyNetlinkIPv4Address *obj) +{ + Py_DECREF(obj->ipv4_address); + Py_XDECREF(obj->ipv4_broadcast); + + /* We can call PyObject_Del directly rather than calling through + tp_free since the type is not subtypable (Py_TPFLAGS_BASETYPE is + not set): */ + PyObject_Del(obj); +} + +static PyObject* +netlink_ipv4_address_repr(PyNetlinkIPv4Address *obj) +{ + PyObject *result = PyString_FromString("ethtool.NetlinkIPv4Address(address='"); + PyString_Concat(&result, obj->ipv4_address); + PyString_ConcatAndDel(&result, + PyString_FromFormat("', netmask=%d", + obj->ipv4_netmask)); + if (obj->ipv4_broadcast) { + PyString_ConcatAndDel(&result, PyString_FromString(", broadcast='")); + PyString_Concat(&result, obj->ipv4_broadcast); + PyString_ConcatAndDel(&result, PyString_FromString("'")); + } + PyString_ConcatAndDel(&result, PyString_FromString(")")); + return result; +} + +static PyMemberDef _ethtool_netlink_ipv4_address_members[] = { + {"address", + T_OBJECT_EX, + offsetof(PyNetlinkIPv4Address, ipv4_address), + 0, + NULL}, + {"netmask", + T_INT, + offsetof(PyNetlinkIPv4Address, ipv4_netmask), + 0, + NULL}, + {"broadcast", + T_OBJECT, /* can be NULL */ + offsetof(PyNetlinkIPv4Address, ipv4_broadcast), + 0, + NULL}, + {NULL} /* End of member list */ +}; + +PyTypeObject ethtool_netlink_ipv4_address_Type = { + PyVarObject_HEAD_INIT(0, 0) + .tp_name = "ethtool.NetlinkIPv4Address", + .tp_basicsize = sizeof(PyNetlinkIPv4Address), + .tp_dealloc = (destructor)netlink_ipv4_address_dealloc, + .tp_repr = (reprfunc)netlink_ipv4_address_repr, + .tp_members = _ethtool_netlink_ipv4_address_members, +}; + +/* Factory function, in case we want to generalize this to add IPv6 support */ +PyObject * +make_python_address_from_rtnl_addr(struct nl_object *obj, + struct rtnl_addr *addr) +{ + int family; + assert(addr); + + family = nl_addr_get_family((struct nl_addr *)addr); + + switch( family ) { + + case AF_INET: + return PyNetlinkIPv4Address_from_rtnl_addr(obj, addr); + + /* + For now, we just support IPv4 addresses. + */ + + default: + return PyErr_SetFromErrno(PyExc_RuntimeError); + } +} + +/* +Local variables: +c-basic-offset: 8 +indent-tabs-mode: y +End: +*/ |