Skip to content

Commit 52b17fc

Browse files
author
David Sommerseth
committed
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>
1 parent d8b7393 commit 52b17fc

File tree

3 files changed

+56
-50
lines changed

3 files changed

+56
-50
lines changed

python-ethtool/etherinfo.c

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ int get_etherinfo_link(etherinfo_py *self)
221221
*
222222
* @return Returns 1 on success, otherwise 0.
223223
*/
224-
int get_etherinfo(etherinfo_py *self, nlQuery query)
224+
int get_etherinfo_address(etherinfo_py *self, nlQuery query)
225225
{
226226
struct nl_cache *addr_cache;
227227
struct rtnl_addr *addr;
@@ -246,54 +246,60 @@ int get_etherinfo(etherinfo_py *self, nlQuery query)
246246
return 0;
247247
}
248248

249-
/* Query the for requested info vai NETLINK */
249+
/* Query the for requested info via NETLINK */
250+
251+
/* Extract IP address information */
252+
if( rtnl_addr_alloc_cache(get_nlc(), &addr_cache) < 0) {
253+
nl_cache_free(addr_cache);
254+
return 0;
255+
}
256+
addr = rtnl_addr_alloc();
257+
/* FIXME: Error handling? */
258+
rtnl_addr_set_ifindex(addr, ethinf->index);
259+
250260
switch( query ) {
251261
case NLQRY_ADDR4:
252-
case NLQRY_ADDR6:
253-
/* Extract IP address information */
254-
if( rtnl_addr_alloc_cache(get_nlc(), &addr_cache) < 0) {
255-
nl_cache_free(addr_cache);
262+
rtnl_addr_set_family(addr, AF_INET);
263+
264+
/* Make sure we don't have any old IPv4 addresses saved */
265+
Py_XDECREF(ethinf->ipv4_addresses);
266+
ethinf->ipv4_addresses = PyList_New(0);
267+
if (!ethinf->ipv4_addresses) {
268+
rtnl_addr_put(addr);
269+
nl_cache_free(addr_cache);
256270
return 0;
257271
}
258-
addr = rtnl_addr_alloc();
259-
rtnl_addr_set_ifindex(addr, ethinf->index);
260-
261-
if( query == NLQRY_ADDR4 ) {
262-
rtnl_addr_set_family(addr, AF_INET);
263-
264-
/* Make sure we don't have any old IPv4 addresses saved */
265-
Py_XDECREF(ethinf->ipv4_addresses);
266-
ethinf->ipv4_addresses = PyList_New(0);
267-
if (!ethinf->ipv4_addresses) {
268-
rtnl_addr_put(addr);
269-
nl_cache_free(addr_cache);
270-
return 0;
271-
}
272-
assert(ethinf->ipv4_addresses);
273-
addrlist = ethinf->ipv4_addresses;
274-
} else if( query == NLQRY_ADDR6 ) {
275-
rtnl_addr_set_family(addr, AF_INET6);
276-
277-
/* Likewise for IPv6 addresses: */
278-
Py_XDECREF(ethinf->ipv6_addresses);
279-
ethinf->ipv6_addresses = PyList_New(0);
280-
if (!ethinf->ipv6_addresses) {
281-
rtnl_addr_put(addr);
282-
nl_cache_free(addr_cache);
283-
return 0;
284-
}
285-
assert(ethinf->ipv6_addresses);
286-
addrlist = ethinf->ipv6_addresses;
272+
assert(ethinf->ipv4_addresses);
273+
addrlist = ethinf->ipv4_addresses;
274+
ret = 1;
275+
break;
276+
277+
case NLQRY_ADDR6:
278+
rtnl_addr_set_family(addr, AF_INET6);
279+
280+
/* Likewise for IPv6 addresses: */
281+
Py_XDECREF(ethinf->ipv6_addresses);
282+
ethinf->ipv6_addresses = PyList_New(0);
283+
if (!ethinf->ipv6_addresses) {
284+
rtnl_addr_put(addr);
285+
nl_cache_free(addr_cache);
286+
return 0;
287287
}
288-
/* Retrieve all address information - common code for NLQRY_ADDR4 and NLQRY_ADDR6*/
289-
nl_cache_foreach_filter(addr_cache, OBJ_CAST(addr), callback_nl_address, addrlist);
290-
rtnl_addr_put(addr);
291-
nl_cache_free(addr_cache);
292-
ret = 1;
293-
break;
288+
assert(ethinf->ipv6_addresses);
289+
addrlist = ethinf->ipv6_addresses;
290+
ret = 1;
291+
break;
294292

295293
default:
296294
ret = 0;
297295
}
296+
297+
if( ret == 1 ) {
298+
/* Retrieve all address information - common code for NLQRY_ADDR4 and NLQRY_ADDR6*/
299+
nl_cache_foreach_filter(addr_cache, OBJ_CAST(addr), callback_nl_address, addrlist);
300+
rtnl_addr_put(addr);
301+
nl_cache_free(addr_cache);
302+
}
303+
298304
return ret;
299305
}

python-ethtool/etherinfo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
#ifndef _ETHERINFO_H
1818
#define _ETHERINFO_H
1919

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

2222
int get_etherinfo_link(etherinfo_py *data);
23-
int get_etherinfo(etherinfo_py *data, nlQuery query);
23+
int get_etherinfo_address(etherinfo_py *data, nlQuery query);
2424
void free_etherinfo(struct etherinfo *ptr);
2525

2626
int open_netlink(etherinfo_py *);

python-ethtool/etherinfo_obj.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ PyObject *_ethtool_etherinfo_getter(etherinfo_py *self, PyObject *attr_o)
151151
Py_INCREF(self->ethinfo->hwaddress);
152152
return self->ethinfo->hwaddress;
153153
} else if( strcmp(attr, "ipv4_address") == 0 ) {
154-
get_etherinfo(self, NLQRY_ADDR4);
154+
get_etherinfo_address(self, NLQRY_ADDR4);
155155
/* For compatiblity with old approach, return last IPv4 address: */
156156
py_addr = get_last_ipv4_address(self);
157157
if (py_addr) {
@@ -162,14 +162,14 @@ PyObject *_ethtool_etherinfo_getter(etherinfo_py *self, PyObject *attr_o)
162162
}
163163
Py_RETURN_NONE;
164164
} else if( strcmp(attr, "ipv4_netmask") == 0 ) {
165-
get_etherinfo(self, NLQRY_ADDR4);
165+
get_etherinfo_address(self, NLQRY_ADDR4);
166166
py_addr = get_last_ipv4_address(self);
167167
if (py_addr) {
168168
return PyInt_FromLong(py_addr->prefixlen);
169169
}
170170
return PyInt_FromLong(0);
171171
} else if( strcmp(attr, "ipv4_broadcast") == 0 ) {
172-
get_etherinfo(self, NLQRY_ADDR4);
172+
get_etherinfo_address(self, NLQRY_ADDR4);
173173
py_addr = get_last_ipv4_address(self);
174174
if (py_addr) {
175175
if (py_addr->ipv4_broadcast) {
@@ -217,8 +217,8 @@ PyObject *_ethtool_etherinfo_str(etherinfo_py *self)
217217
}
218218

219219
get_etherinfo_link(self);
220-
get_etherinfo(self, NLQRY_ADDR4);
221-
get_etherinfo(self, NLQRY_ADDR6);
220+
get_etherinfo_address(self, NLQRY_ADDR4);
221+
get_etherinfo_address(self, NLQRY_ADDR6);
222222

223223
ret = PyString_FromFormat("Device %s:\n", self->ethinfo->device);
224224
if( self->ethinfo->hwaddress ) {
@@ -277,7 +277,7 @@ static PyObject *_ethtool_etherinfo_get_ipv4_addresses(etherinfo_py *self, PyObj
277277
return NULL;
278278
}
279279

280-
get_etherinfo(self, NLQRY_ADDR4);
280+
get_etherinfo_address(self, NLQRY_ADDR4);
281281

282282
/* Transfer ownership of reference: */
283283
ret = self->ethinfo->ipv4_addresses;
@@ -303,7 +303,7 @@ static PyObject *_ethtool_etherinfo_get_ipv6_addresses(etherinfo_py *self, PyObj
303303
return NULL;
304304
}
305305

306-
get_etherinfo(self, NLQRY_ADDR6);
306+
get_etherinfo_address(self, NLQRY_ADDR6);
307307

308308
/* Transfer ownership of reference: */
309309
ret = self->ethinfo->ipv6_addresses;

0 commit comments

Comments
 (0)