Skip to content

Commit e978498

Browse files
author
David Sommerseth
committed
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>
1 parent 44838a8 commit e978498

File tree

3 files changed

+43
-33
lines changed

3 files changed

+43
-33
lines changed

python-ethtool/etherinfo.c

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,14 @@ static void callback_nl_link(struct nl_object *obj, void *arg)
9797
*/
9898
static void callback_nl_address(struct nl_object *obj, void *arg)
9999
{
100-
struct etherinfo *ethi = (struct etherinfo *) arg;
100+
PyObject *py_addrlist = (PyObject *) arg;
101101
struct rtnl_addr *rtaddr = (struct rtnl_addr *) obj;
102102
PyObject *addr_obj = NULL;
103103
int af_family = -1;
104104

105-
if( ethi == NULL ) {
105+
if( py_addrlist == NULL ) {
106106
return;
107107
}
108-
assert(ethi->ipv4_addresses);
109-
assert(ethi->ipv6_addresses);
110108

111109
/* Ensure that we're processing only known address types.
112110
* Currently only IPv4 and IPv6 is handled
@@ -121,9 +119,8 @@ static void callback_nl_address(struct nl_object *obj, void *arg)
121119
if (!addr_obj) {
122120
return;
123121
}
124-
/* Append the IP address object to the proper address list */
125-
PyList_Append((af_family == AF_INET6 ? ethi->ipv6_addresses : ethi->ipv4_addresses),
126-
addr_obj);
122+
/* Append the IP address object to the address list */
123+
PyList_Append(py_addrlist, addr_obj);
127124
Py_DECREF(addr_obj);
128125
}
129126

@@ -152,6 +149,8 @@ int get_etherinfo(struct etherinfo_obj_data *data, nlQuery query)
152149
struct rtnl_addr *addr;
153150
struct rtnl_link *link;
154151
struct etherinfo *ethinf = NULL;
152+
PyObject *addrlist = NULL;
153+
155154
int ret = 0;
156155

157156
if( !data || !data->ethinfo ) {
@@ -207,7 +206,8 @@ int get_etherinfo(struct etherinfo_obj_data *data, nlQuery query)
207206
ret = 1;
208207
break;
209208

210-
case NLQRY_ADDR:
209+
case NLQRY_ADDR4:
210+
case NLQRY_ADDR6:
211211
/* Extract IP address information */
212212
if( rtnl_addr_alloc_cache(*data->nlc, &addr_cache) < 0) {
213213
nl_cache_free(addr_cache);
@@ -216,26 +216,35 @@ int get_etherinfo(struct etherinfo_obj_data *data, nlQuery query)
216216
addr = rtnl_addr_alloc();
217217
rtnl_addr_set_ifindex(addr, ethinf->index);
218218

219-
/* Make sure we don't have any old IPv4 addresses saved */
220-
Py_XDECREF(ethinf->ipv4_addresses);
221-
ethinf->ipv4_addresses = PyList_New(0);
222-
if (!ethinf->ipv4_addresses) {
223-
rtnl_addr_put(addr);
224-
nl_cache_free(addr_cache);
225-
return 0;
226-
}
219+
if( query == NLQRY_ADDR4 ) {
220+
rtnl_addr_set_family(addr, AF_INET);
227221

228-
/* Likewise for IPv6 addresses: */
229-
Py_XDECREF(ethinf->ipv6_addresses);
230-
ethinf->ipv6_addresses = PyList_New(0);
231-
if (!ethinf->ipv6_addresses) {
232-
rtnl_addr_put(addr);
233-
nl_cache_free(addr_cache);
234-
return 0;
235-
}
222+
/* Make sure we don't have any old IPv4 addresses saved */
223+
Py_XDECREF(ethinf->ipv4_addresses);
224+
ethinf->ipv4_addresses = PyList_New(0);
225+
if (!ethinf->ipv4_addresses) {
226+
rtnl_addr_put(addr);
227+
nl_cache_free(addr_cache);
228+
return 0;
229+
}
230+
assert(ethinf->ipv4_addresses);
231+
addrlist = ethinf->ipv4_addresses;
232+
} else if( query == NLQRY_ADDR6 ) {
233+
rtnl_addr_set_family(addr, AF_INET6);
236234

237-
/* Retrieve all address information */
238-
nl_cache_foreach_filter(addr_cache, OBJ_CAST(addr), callback_nl_address, ethinf);
235+
/* Likewise for IPv6 addresses: */
236+
Py_XDECREF(ethinf->ipv6_addresses);
237+
ethinf->ipv6_addresses = PyList_New(0);
238+
if (!ethinf->ipv6_addresses) {
239+
rtnl_addr_put(addr);
240+
nl_cache_free(addr_cache);
241+
return 0;
242+
}
243+
assert(ethinf->ipv6_addresses);
244+
addrlist = ethinf->ipv6_addresses;
245+
}
246+
/* Retrieve all address information - common code for NLQRY_ADDR4 and NLQRY_ADDR6*/
247+
nl_cache_foreach_filter(addr_cache, OBJ_CAST(addr), callback_nl_address, addrlist);
239248
rtnl_addr_put(addr);
240249
nl_cache_free(addr_cache);
241250
ret = 1;

python-ethtool/etherinfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#ifndef _ETHERINFO_H
1818
#define _ETHERINFO_H
1919

20-
typedef enum {NLQRY_LINK, NLQRY_ADDR} nlQuery; /**< Supported query types in the etherinfo code */
20+
typedef enum {NLQRY_LINK, NLQRY_ADDR4, NLQRY_ADDR6} nlQuery; /**< Supported query types in the etherinfo code */
2121

2222
int get_etherinfo(struct etherinfo_obj_data *data, nlQuery query);
2323
void free_etherinfo(struct etherinfo *ptr);

python-ethtool/etherinfo_obj.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ PyObject *_ethtool_etherinfo_getter(etherinfo_py *self, PyObject *attr_o)
155155
Py_INCREF(self->data->ethinfo->hwaddress);
156156
return self->data->ethinfo->hwaddress;
157157
} else if( strcmp(attr, "ipv4_address") == 0 ) {
158-
get_etherinfo(self->data, NLQRY_ADDR);
158+
get_etherinfo(self->data, NLQRY_ADDR4);
159159
/* For compatiblity with old approach, return last IPv4 address: */
160160
py_addr = get_last_ipv4_address(self);
161161
if (py_addr) {
@@ -166,14 +166,14 @@ PyObject *_ethtool_etherinfo_getter(etherinfo_py *self, PyObject *attr_o)
166166
}
167167
Py_RETURN_NONE;
168168
} else if( strcmp(attr, "ipv4_netmask") == 0 ) {
169-
get_etherinfo(self->data, NLQRY_ADDR);
169+
get_etherinfo(self->data, NLQRY_ADDR4);
170170
py_addr = get_last_ipv4_address(self);
171171
if (py_addr) {
172172
return PyInt_FromLong(py_addr->prefixlen);
173173
}
174174
return PyInt_FromLong(0);
175175
} else if( strcmp(attr, "ipv4_broadcast") == 0 ) {
176-
get_etherinfo(self->data, NLQRY_ADDR);
176+
get_etherinfo(self->data, NLQRY_ADDR4);
177177
py_addr = get_last_ipv4_address(self);
178178
if (py_addr) {
179179
if (py_addr->ipv4_broadcast) {
@@ -221,7 +221,8 @@ PyObject *_ethtool_etherinfo_str(etherinfo_py *self)
221221
}
222222

223223
get_etherinfo(self->data, NLQRY_LINK);
224-
get_etherinfo(self->data, NLQRY_ADDR);
224+
get_etherinfo(self->data, NLQRY_ADDR4);
225+
get_etherinfo(self->data, NLQRY_ADDR6);
225226

226227
ret = PyString_FromFormat("Device %s:\n", self->data->ethinfo->device);
227228
if( self->data->ethinfo->hwaddress ) {
@@ -280,7 +281,7 @@ static PyObject *_ethtool_etherinfo_get_ipv4_addresses(etherinfo_py *self, PyObj
280281
return NULL;
281282
}
282283

283-
get_etherinfo(self->data, NLQRY_ADDR);
284+
get_etherinfo(self->data, NLQRY_ADDR4);
284285

285286
/* Transfer ownership of reference: */
286287
ret = self->data->ethinfo->ipv4_addresses;
@@ -306,7 +307,7 @@ static PyObject *_ethtool_etherinfo_get_ipv6_addresses(etherinfo_py *self, PyObj
306307
return NULL;
307308
}
308309

309-
get_etherinfo(self->data, NLQRY_ADDR);
310+
get_etherinfo(self->data, NLQRY_ADDR6);
310311

311312
/* Transfer ownership of reference: */
312313
ret = self->data->ethinfo->ipv6_addresses;

0 commit comments

Comments
 (0)