summaryrefslogtreecommitdiffstats
path: root/python-ethtool
diff options
context:
space:
mode:
authorDavid Sommerseth <davids@redhat.com>2013-12-20 01:08:46 +0100
committerDavid Sommerseth <davids@redhat.com>2013-12-20 01:11:28 +0100
commitd10055619793bf8ff74814f4126ddb8ba5ee913f (patch)
treeb36082d75bc761782747af536f7c7d998d690832 /python-ethtool
parent52b17fc36d39c32d6e766c18ee8fd4f472f62d39 (diff)
Get rid of the internal IP address lists
With the new structure, these pointers are of no use. Kick them out. The result of this is that get_etherinfo_address() now returns a Python object which contains a list of IP address objects. Signed-off-by: David Sommerseth <davids@redhat.com>
Diffstat (limited to 'python-ethtool')
-rw-r--r--python-ethtool/etherinfo.c57
-rw-r--r--python-ethtool/etherinfo.h2
-rw-r--r--python-ethtool/etherinfo_obj.c65
-rw-r--r--python-ethtool/etherinfo_struct.h2
4 files changed, 41 insertions, 85 deletions
diff --git a/python-ethtool/etherinfo.c b/python-ethtool/etherinfo.c
index 8e9d0b5..61fe037 100644
--- a/python-ethtool/etherinfo.c
+++ b/python-ethtool/etherinfo.c
@@ -55,8 +55,6 @@ void free_etherinfo(struct etherinfo *ptr)
free(ptr->device);
Py_XDECREF(ptr->hwaddress);
- Py_XDECREF(ptr->ipv4_addresses);
- Py_XDECREF(ptr->ipv6_addresses);
free(ptr);
}
@@ -219,18 +217,17 @@ int get_etherinfo_link(etherinfo_py *self)
* @param nlc Pointer to the libnl handle, which is used for the query against NETLINK
* @param query What to query for. Must be NLQRY_ADDR4 or NLQRY_ADDR6.
*
- * @return Returns 1 on success, otherwise 0.
+ * @return Returns a Python list containing PyNetlinkIPaddress objects on success, otherwise NULL
*/
-int get_etherinfo_address(etherinfo_py *self, nlQuery query)
+PyObject * get_etherinfo_address(etherinfo_py *self, nlQuery query)
{
struct nl_cache *addr_cache;
struct rtnl_addr *addr;
struct etherinfo *ethinf = NULL;
PyObject *addrlist = NULL;
- int ret = 0;
if( !self || !self->ethinfo ) {
- return 0;
+ return NULL;
}
ethinf = self->ethinfo;
@@ -239,11 +236,11 @@ int get_etherinfo_address(etherinfo_py *self, nlQuery query)
PyErr_Format(PyExc_RuntimeError,
"Could not open a NETLINK connection for %s",
ethinf->device);
- return 0;
+ return NULL;
}
if( _set_device_index(ethinf) != 1) {
- return 0;
+ return NULL;
}
/* Query the for requested info via NETLINK */
@@ -251,7 +248,7 @@ int get_etherinfo_address(etherinfo_py *self, nlQuery query)
/* Extract IP address information */
if( rtnl_addr_alloc_cache(get_nlc(), &addr_cache) < 0) {
nl_cache_free(addr_cache);
- return 0;
+ return NULL;
}
addr = rtnl_addr_alloc();
/* FIXME: Error handling? */
@@ -260,46 +257,24 @@ int get_etherinfo_address(etherinfo_py *self, nlQuery query)
switch( query ) {
case NLQRY_ADDR4:
rtnl_addr_set_family(addr, AF_INET);
-
- /* Make sure we don't have any old IPv4 addresses saved */
- Py_XDECREF(ethinf->ipv4_addresses);
- ethinf->ipv4_addresses = PyList_New(0);
- if (!ethinf->ipv4_addresses) {
- rtnl_addr_put(addr);
- nl_cache_free(addr_cache);
- return 0;
- }
- assert(ethinf->ipv4_addresses);
- addrlist = ethinf->ipv4_addresses;
- ret = 1;
break;
case NLQRY_ADDR6:
rtnl_addr_set_family(addr, AF_INET6);
-
- /* Likewise for IPv6 addresses: */
- Py_XDECREF(ethinf->ipv6_addresses);
- ethinf->ipv6_addresses = PyList_New(0);
- if (!ethinf->ipv6_addresses) {
- rtnl_addr_put(addr);
- nl_cache_free(addr_cache);
- return 0;
- }
- assert(ethinf->ipv6_addresses);
- addrlist = ethinf->ipv6_addresses;
- ret = 1;
break;
default:
- ret = 0;
+ return NULL;
}
- if( ret == 1 ) {
- /* Retrieve all address information - common code for NLQRY_ADDR4 and NLQRY_ADDR6*/
- nl_cache_foreach_filter(addr_cache, OBJ_CAST(addr), callback_nl_address, addrlist);
- rtnl_addr_put(addr);
- nl_cache_free(addr_cache);
- }
+ /* Retrieve all address information */
+ addrlist = PyList_New(0); /* The list where to put the address object */
+ assert(addrlist);
+
+ /* Loop through all configured addresses */
+ nl_cache_foreach_filter(addr_cache, OBJ_CAST(addr), callback_nl_address, addrlist);
+ rtnl_addr_put(addr);
+ nl_cache_free(addr_cache);
- return ret;
+ return addrlist;
}
diff --git a/python-ethtool/etherinfo.h b/python-ethtool/etherinfo.h
index 3d4eb96..2fc602e 100644
--- a/python-ethtool/etherinfo.h
+++ b/python-ethtool/etherinfo.h
@@ -20,7 +20,7 @@
typedef enum {NLQRY_ADDR4, NLQRY_ADDR6} nlQuery; /**< Supported query types in the etherinfo code */
int get_etherinfo_link(etherinfo_py *data);
-int get_etherinfo_address(etherinfo_py *data, nlQuery query);
+PyObject * get_etherinfo_address(etherinfo_py *self, nlQuery query);
void free_etherinfo(struct etherinfo *ptr);
int open_netlink(etherinfo_py *);
diff --git a/python-ethtool/etherinfo_obj.c b/python-ethtool/etherinfo_obj.c
index 4ac42a0..8ab207d 100644
--- a/python-ethtool/etherinfo_obj.c
+++ b/python-ethtool/etherinfo_obj.c
@@ -96,26 +96,23 @@ int _ethtool_etherinfo_init(etherinfo_py *self, PyObject *args, PyObject *kwds)
The return value is a *borrowed reference* (or NULL)
*/
-static PyNetlinkIPaddress * get_last_ipv4_address(etherinfo_py *self)
+static PyNetlinkIPaddress * get_last_ipv4_address(PyObject *addrlist)
{
Py_ssize_t size;
- PyObject *list;
- assert(self);
- list = self->ethinfo->ipv4_addresses;
- if (!list) {
+ if (!addrlist) {
return NULL;
}
- if (!PyList_Check(list)) {
+ if (!PyList_Check(addrlist)) {
return NULL;
}
- size = PyList_Size(list);
+ size = PyList_Size(addrlist);
if (size > 0) {
- PyObject *item = PyList_GetItem(list, size - 1);
+ PyNetlinkIPaddress *item = (PyNetlinkIPaddress *)PyList_GetItem(addrlist, size - 1);
if (Py_TYPE(item) == &ethtool_netlink_ip_address_Type) {
- return (PyNetlinkIPaddress*)item;
+ return item;
}
}
@@ -134,6 +131,7 @@ PyObject *_ethtool_etherinfo_getter(etherinfo_py *self, PyObject *attr_o)
{
char *attr = PyString_AsString(attr_o);
PyNetlinkIPaddress *py_addr;
+ PyObject *addrlist = NULL;
if( !self || !self->ethinfo ) {
PyErr_SetString(PyExc_AttributeError, "No data available");
@@ -151,9 +149,9 @@ PyObject *_ethtool_etherinfo_getter(etherinfo_py *self, PyObject *attr_o)
Py_INCREF(self->ethinfo->hwaddress);
return self->ethinfo->hwaddress;
} else if( strcmp(attr, "ipv4_address") == 0 ) {
- get_etherinfo_address(self, NLQRY_ADDR4);
+ addrlist = get_etherinfo_address(self, NLQRY_ADDR4);
/* For compatiblity with old approach, return last IPv4 address: */
- py_addr = get_last_ipv4_address(self);
+ py_addr = get_last_ipv4_address(addrlist);
if (py_addr) {
if (py_addr->local) {
Py_INCREF(py_addr->local);
@@ -162,15 +160,15 @@ PyObject *_ethtool_etherinfo_getter(etherinfo_py *self, PyObject *attr_o)
}
Py_RETURN_NONE;
} else if( strcmp(attr, "ipv4_netmask") == 0 ) {
- get_etherinfo_address(self, NLQRY_ADDR4);
- py_addr = get_last_ipv4_address(self);
+ addrlist = get_etherinfo_address(self, NLQRY_ADDR4);
+ py_addr = get_last_ipv4_address(addrlist);
if (py_addr) {
return PyInt_FromLong(py_addr->prefixlen);
}
return PyInt_FromLong(0);
} else if( strcmp(attr, "ipv4_broadcast") == 0 ) {
- get_etherinfo_address(self, NLQRY_ADDR4);
- py_addr = get_last_ipv4_address(self);
+ addrlist = get_etherinfo_address(self, NLQRY_ADDR4);
+ py_addr = get_last_ipv4_address(addrlist);
if (py_addr) {
if (py_addr->ipv4_broadcast) {
Py_INCREF(py_addr->ipv4_broadcast);
@@ -210,6 +208,7 @@ int _ethtool_etherinfo_setter(etherinfo_py *self, PyObject *attr_o, PyObject *va
PyObject *_ethtool_etherinfo_str(etherinfo_py *self)
{
PyObject *ret = NULL;
+ PyObject *ipv4addrs = NULL, *ipv6addrs = NULL;
if( !self || !self->ethinfo ) {
PyErr_SetString(PyExc_AttributeError, "No data available");
@@ -217,8 +216,6 @@ PyObject *_ethtool_etherinfo_str(etherinfo_py *self)
}
get_etherinfo_link(self);
- get_etherinfo_address(self, NLQRY_ADDR4);
- get_etherinfo_address(self, NLQRY_ADDR6);
ret = PyString_FromFormat("Device %s:\n", self->ethinfo->device);
if( self->ethinfo->hwaddress ) {
@@ -227,10 +224,11 @@ PyObject *_ethtool_etherinfo_str(etherinfo_py *self)
PyString_ConcatAndDel(&ret, PyString_FromString("\n"));
}
- if( self->ethinfo->ipv4_addresses ) {
+ ipv4addrs = get_etherinfo_address(self, NLQRY_ADDR4);
+ if( ipv4addrs ) {
Py_ssize_t i;
- for (i = 0; i < PyList_Size(self->ethinfo->ipv4_addresses); i++) {
- PyNetlinkIPaddress *py_addr = (PyNetlinkIPaddress *)PyList_GetItem(self->ethinfo->ipv4_addresses, i);
+ for (i = 0; i < PyList_Size(ipv4addrs); i++) {
+ PyNetlinkIPaddress *py_addr = (PyNetlinkIPaddress *)PyList_GetItem(ipv4addrs, i);
PyObject *tmp = PyString_FromFormat("\tIPv4 address: ");
PyString_Concat(&tmp, py_addr->local);
PyString_ConcatAndDel(&tmp, PyString_FromFormat("/%d", py_addr->prefixlen));
@@ -244,10 +242,11 @@ PyObject *_ethtool_etherinfo_str(etherinfo_py *self)
}
}
- if( self->ethinfo->ipv6_addresses ) {
+ ipv6addrs = get_etherinfo_address(self, NLQRY_ADDR6);
+ if( ipv6addrs ) {
Py_ssize_t i;
- for (i = 0; i < PyList_Size(self->ethinfo->ipv6_addresses); i++) {
- PyNetlinkIPaddress *py_addr = (PyNetlinkIPaddress *)PyList_GetItem(self->ethinfo->ipv6_addresses, i);
+ for (i = 0; i < PyList_Size(ipv6addrs); i++) {
+ PyNetlinkIPaddress *py_addr = (PyNetlinkIPaddress *)PyList_GetItem(ipv6addrs, i);
PyObject *tmp = PyString_FromFormat("\tIPv6 address: [");
PyString_Concat(&tmp, py_addr->scope);
PyString_ConcatAndDel(&tmp, PyString_FromString("] "));
@@ -270,20 +269,12 @@ PyObject *_ethtool_etherinfo_str(etherinfo_py *self)
* @return Returns a Python tuple list of NetlinkIP4Address objects
*/
static PyObject *_ethtool_etherinfo_get_ipv4_addresses(etherinfo_py *self, PyObject *notused) {
- PyObject *ret;
-
if( !self || !self->ethinfo ) {
PyErr_SetString(PyExc_AttributeError, "No data available");
return NULL;
}
- get_etherinfo_address(self, NLQRY_ADDR4);
-
- /* Transfer ownership of reference: */
- ret = self->ethinfo->ipv4_addresses;
- self->ethinfo->ipv4_addresses = NULL;
-
- return ret;
+ return get_etherinfo_address(self, NLQRY_ADDR4);
}
@@ -296,20 +287,12 @@ static PyObject *_ethtool_etherinfo_get_ipv4_addresses(etherinfo_py *self, PyObj
* @return Returns a Python tuple list of NetlinkIP6Address objects
*/
static PyObject *_ethtool_etherinfo_get_ipv6_addresses(etherinfo_py *self, PyObject *notused) {
- PyObject *ret;
-
if( !self || !self->ethinfo ) {
PyErr_SetString(PyExc_AttributeError, "No data available");
return NULL;
}
- get_etherinfo_address(self, NLQRY_ADDR6);
-
- /* Transfer ownership of reference: */
- ret = self->ethinfo->ipv6_addresses;
- self->ethinfo->ipv6_addresses = NULL;
-
- return ret;
+ return get_etherinfo_address(self, NLQRY_ADDR6);
}
diff --git a/python-ethtool/etherinfo_struct.h b/python-ethtool/etherinfo_struct.h
index cc7b32f..bc681bc 100644
--- a/python-ethtool/etherinfo_struct.h
+++ b/python-ethtool/etherinfo_struct.h
@@ -34,8 +34,6 @@ struct etherinfo {
char *device; /**< Device name */
int index; /**< NETLINK index reference */
PyObject *hwaddress; /**< string: HW address / MAC address of device */
- PyObject *ipv4_addresses; /**< list of PyNetlinkIPv4Address instances */
- PyObject *ipv6_addresses; /**< list of PyNetlinkIPv6Addresses instances */
};
/* Python object containing data baked from a (struct rtnl_addr) */