diff options
author | David Sommerseth <davids@redhat.com> | 2013-12-16 21:02:31 +0100 |
---|---|---|
committer | David Sommerseth <davids@redhat.com> | 2013-12-16 21:02:31 +0100 |
commit | e9784985e3f7b72cc5f3210d60a88014625b2660 (patch) | |
tree | a8a812dd0e3781709883c48d534d9c1731e3801a | |
parent | 44838a858671264cfb2085a3b1dbe6e01ca48feb (diff) | |
download | python-ethtool-e9784985e3f7b72cc5f3210d60a88014625b2660.tar.gz python-ethtool-e9784985e3f7b72cc5f3210d60a88014625b2660.tar.xz python-ethtool-e9784985e3f7b72cc5f3210d60a88014625b2660.zip |
Splitting up get_etherinfo() calls
Make the calls to retrieve IPv4 and IPv6 addresses individual. This
is the the beginning of the rewrite of the whole etherinfo main class.
Signed-off-by: David Sommerseth <davids@redhat.com>
-rw-r--r-- | python-ethtool/etherinfo.c | 61 | ||||
-rw-r--r-- | python-ethtool/etherinfo.h | 2 | ||||
-rw-r--r-- | python-ethtool/etherinfo_obj.c | 13 |
3 files changed, 43 insertions, 33 deletions
diff --git a/python-ethtool/etherinfo.c b/python-ethtool/etherinfo.c index 37e86e3..74b30d4 100644 --- a/python-ethtool/etherinfo.c +++ b/python-ethtool/etherinfo.c @@ -97,16 +97,14 @@ static void callback_nl_link(struct nl_object *obj, void *arg) */ static void callback_nl_address(struct nl_object *obj, void *arg) { - struct etherinfo *ethi = (struct etherinfo *) arg; + PyObject *py_addrlist = (PyObject *) arg; struct rtnl_addr *rtaddr = (struct rtnl_addr *) obj; PyObject *addr_obj = NULL; int af_family = -1; - if( ethi == NULL ) { + if( py_addrlist == NULL ) { return; } - assert(ethi->ipv4_addresses); - assert(ethi->ipv6_addresses); /* Ensure that we're processing only known address types. * Currently only IPv4 and IPv6 is handled @@ -121,9 +119,8 @@ static void callback_nl_address(struct nl_object *obj, void *arg) if (!addr_obj) { return; } - /* Append the IP address object to the proper address list */ - PyList_Append((af_family == AF_INET6 ? ethi->ipv6_addresses : ethi->ipv4_addresses), - addr_obj); + /* Append the IP address object to the address list */ + PyList_Append(py_addrlist, addr_obj); Py_DECREF(addr_obj); } @@ -152,6 +149,8 @@ int get_etherinfo(struct etherinfo_obj_data *data, nlQuery query) struct rtnl_addr *addr; struct rtnl_link *link; struct etherinfo *ethinf = NULL; + PyObject *addrlist = NULL; + int ret = 0; if( !data || !data->ethinfo ) { @@ -207,7 +206,8 @@ int get_etherinfo(struct etherinfo_obj_data *data, nlQuery query) ret = 1; break; - case NLQRY_ADDR: + case NLQRY_ADDR4: + case NLQRY_ADDR6: /* Extract IP address information */ if( rtnl_addr_alloc_cache(*data->nlc, &addr_cache) < 0) { nl_cache_free(addr_cache); @@ -216,26 +216,35 @@ int get_etherinfo(struct etherinfo_obj_data *data, nlQuery query) addr = rtnl_addr_alloc(); rtnl_addr_set_ifindex(addr, ethinf->index); - /* 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; - } + if( query == NLQRY_ADDR4 ) { + rtnl_addr_set_family(addr, AF_INET); - /* 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; - } + /* 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); - /* Retrieve all address information */ - nl_cache_foreach_filter(addr_cache, OBJ_CAST(addr), callback_nl_address, ethinf); + /* 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; + } + /* 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; diff --git a/python-ethtool/etherinfo.h b/python-ethtool/etherinfo.h index 4265e10..1997807 100644 --- a/python-ethtool/etherinfo.h +++ b/python-ethtool/etherinfo.h @@ -17,7 +17,7 @@ #ifndef _ETHERINFO_H #define _ETHERINFO_H -typedef enum {NLQRY_LINK, NLQRY_ADDR} nlQuery; /**< Supported query types in the etherinfo code */ +typedef enum {NLQRY_LINK, NLQRY_ADDR4, NLQRY_ADDR6} nlQuery; /**< Supported query types in the etherinfo code */ int get_etherinfo(struct etherinfo_obj_data *data, nlQuery query); void free_etherinfo(struct etherinfo *ptr); diff --git a/python-ethtool/etherinfo_obj.c b/python-ethtool/etherinfo_obj.c index 56d88e7..55a42db 100644 --- a/python-ethtool/etherinfo_obj.c +++ b/python-ethtool/etherinfo_obj.c @@ -155,7 +155,7 @@ PyObject *_ethtool_etherinfo_getter(etherinfo_py *self, PyObject *attr_o) Py_INCREF(self->data->ethinfo->hwaddress); return self->data->ethinfo->hwaddress; } else if( strcmp(attr, "ipv4_address") == 0 ) { - get_etherinfo(self->data, NLQRY_ADDR); + get_etherinfo(self->data, NLQRY_ADDR4); /* For compatiblity with old approach, return last IPv4 address: */ py_addr = get_last_ipv4_address(self); if (py_addr) { @@ -166,14 +166,14 @@ PyObject *_ethtool_etherinfo_getter(etherinfo_py *self, PyObject *attr_o) } Py_RETURN_NONE; } else if( strcmp(attr, "ipv4_netmask") == 0 ) { - get_etherinfo(self->data, NLQRY_ADDR); + get_etherinfo(self->data, 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->data, NLQRY_ADDR); + get_etherinfo(self->data, NLQRY_ADDR4); py_addr = get_last_ipv4_address(self); if (py_addr) { if (py_addr->ipv4_broadcast) { @@ -221,7 +221,8 @@ PyObject *_ethtool_etherinfo_str(etherinfo_py *self) } get_etherinfo(self->data, NLQRY_LINK); - get_etherinfo(self->data, NLQRY_ADDR); + get_etherinfo(self->data, NLQRY_ADDR4); + get_etherinfo(self->data, NLQRY_ADDR6); ret = PyString_FromFormat("Device %s:\n", self->data->ethinfo->device); if( self->data->ethinfo->hwaddress ) { @@ -280,7 +281,7 @@ static PyObject *_ethtool_etherinfo_get_ipv4_addresses(etherinfo_py *self, PyObj return NULL; } - get_etherinfo(self->data, NLQRY_ADDR); + get_etherinfo(self->data, NLQRY_ADDR4); /* Transfer ownership of reference: */ ret = self->data->ethinfo->ipv4_addresses; @@ -306,7 +307,7 @@ static PyObject *_ethtool_etherinfo_get_ipv6_addresses(etherinfo_py *self, PyObj return NULL; } - get_etherinfo(self->data, NLQRY_ADDR); + get_etherinfo(self->data, NLQRY_ADDR6); /* Transfer ownership of reference: */ ret = self->data->ethinfo->ipv6_addresses; |