summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Sommerseth <davids@redhat.com>2013-12-20 00:18:56 +0100
committerDavid Sommerseth <davids@redhat.com>2013-12-20 00:20:16 +0100
commit52b17fc36d39c32d6e766c18ee8fd4f472f62d39 (patch)
treefc51b65fa6927cba0e34c6966f4e87394645146c
parentd8b7393db11aec4e0e332dd1dfb6dc6f34888206 (diff)
downloadpython-ethtool-52b17fc36d39c32d6e766c18ee8fd4f472f62d39.tar.gz
python-ethtool-52b17fc36d39c32d6e766c18ee8fd4f472f62d39.tar.xz
python-ethtool-52b17fc36d39c32d6e766c18ee8fd4f472f62d39.zip
Clean-up get_etherinfo() and move it to get_etherinfo_address()
This follows the previous commit. Just cleaning up and making things a bit more clearer. Signed-off-by: David Sommerseth <davids@redhat.com>
-rw-r--r--python-ethtool/etherinfo.c88
-rw-r--r--python-ethtool/etherinfo.h4
-rw-r--r--python-ethtool/etherinfo_obj.c14
3 files changed, 56 insertions, 50 deletions
diff --git a/python-ethtool/etherinfo.c b/python-ethtool/etherinfo.c
index 2e6a425..8e9d0b5 100644
--- a/python-ethtool/etherinfo.c
+++ b/python-ethtool/etherinfo.c
@@ -221,7 +221,7 @@ int get_etherinfo_link(etherinfo_py *self)
*
* @return Returns 1 on success, otherwise 0.
*/
-int get_etherinfo(etherinfo_py *self, nlQuery query)
+int get_etherinfo_address(etherinfo_py *self, nlQuery query)
{
struct nl_cache *addr_cache;
struct rtnl_addr *addr;
@@ -246,54 +246,60 @@ int get_etherinfo(etherinfo_py *self, nlQuery query)
return 0;
}
- /* Query the for requested info vai NETLINK */
+ /* Query the for requested info via NETLINK */
+
+ /* Extract IP address information */
+ if( rtnl_addr_alloc_cache(get_nlc(), &addr_cache) < 0) {
+ nl_cache_free(addr_cache);
+ return 0;
+ }
+ addr = rtnl_addr_alloc();
+ /* FIXME: Error handling? */
+ rtnl_addr_set_ifindex(addr, ethinf->index);
+
switch( query ) {
case NLQRY_ADDR4:
- case NLQRY_ADDR6:
- /* Extract IP address information */
- if( rtnl_addr_alloc_cache(get_nlc(), &addr_cache) < 0) {
- nl_cache_free(addr_cache);
+ 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;
}
- addr = rtnl_addr_alloc();
- rtnl_addr_set_ifindex(addr, ethinf->index);
-
- if( query == 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;
- } else if( query == 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;
+ 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;
}
- /* 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);
- ret = 1;
- break;
+ assert(ethinf->ipv6_addresses);
+ addrlist = ethinf->ipv6_addresses;
+ ret = 1;
+ break;
default:
ret = 0;
}
+
+ 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);
+ }
+
return ret;
}
diff --git a/python-ethtool/etherinfo.h b/python-ethtool/etherinfo.h
index f817602..3d4eb96 100644
--- a/python-ethtool/etherinfo.h
+++ b/python-ethtool/etherinfo.h
@@ -17,10 +17,10 @@
#ifndef _ETHERINFO_H
#define _ETHERINFO_H
-typedef enum {NLQRY_LINK, NLQRY_ADDR4, NLQRY_ADDR6} nlQuery; /**< Supported query types in the etherinfo code */
+typedef enum {NLQRY_ADDR4, NLQRY_ADDR6} nlQuery; /**< Supported query types in the etherinfo code */
int get_etherinfo_link(etherinfo_py *data);
-int get_etherinfo(etherinfo_py *data, nlQuery query);
+int get_etherinfo_address(etherinfo_py *data, 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 8e3da62..4ac42a0 100644
--- a/python-ethtool/etherinfo_obj.c
+++ b/python-ethtool/etherinfo_obj.c
@@ -151,7 +151,7 @@ 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(self, NLQRY_ADDR4);
+ get_etherinfo_address(self, NLQRY_ADDR4);
/* For compatiblity with old approach, return last IPv4 address: */
py_addr = get_last_ipv4_address(self);
if (py_addr) {
@@ -162,14 +162,14 @@ PyObject *_ethtool_etherinfo_getter(etherinfo_py *self, PyObject *attr_o)
}
Py_RETURN_NONE;
} else if( strcmp(attr, "ipv4_netmask") == 0 ) {
- get_etherinfo(self, NLQRY_ADDR4);
+ get_etherinfo_address(self, NLQRY_ADDR4);
py_addr = get_last_ipv4_address(self);
if (py_addr) {
return PyInt_FromLong(py_addr->prefixlen);
}
return PyInt_FromLong(0);
} else if( strcmp(attr, "ipv4_broadcast") == 0 ) {
- get_etherinfo(self, NLQRY_ADDR4);
+ get_etherinfo_address(self, NLQRY_ADDR4);
py_addr = get_last_ipv4_address(self);
if (py_addr) {
if (py_addr->ipv4_broadcast) {
@@ -217,8 +217,8 @@ PyObject *_ethtool_etherinfo_str(etherinfo_py *self)
}
get_etherinfo_link(self);
- get_etherinfo(self, NLQRY_ADDR4);
- get_etherinfo(self, NLQRY_ADDR6);
+ 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 ) {
@@ -277,7 +277,7 @@ static PyObject *_ethtool_etherinfo_get_ipv4_addresses(etherinfo_py *self, PyObj
return NULL;
}
- get_etherinfo(self, NLQRY_ADDR4);
+ get_etherinfo_address(self, NLQRY_ADDR4);
/* Transfer ownership of reference: */
ret = self->ethinfo->ipv4_addresses;
@@ -303,7 +303,7 @@ static PyObject *_ethtool_etherinfo_get_ipv6_addresses(etherinfo_py *self, PyObj
return NULL;
}
- get_etherinfo(self, NLQRY_ADDR6);
+ get_etherinfo_address(self, NLQRY_ADDR6);
/* Transfer ownership of reference: */
ret = self->ethinfo->ipv6_addresses;