Skip to content

Commit b80e659

Browse files
author
David Sommerseth
committed
Get rid of not needed struct wrapping
The struct nl_handle was wrapped inside struct _nlconnection. This is really not needed if open_netlink() and close_netlink() functions uses "pointer's pointer" (struct nl_handle **) instead. Removes also the need to declare a static struct _nlconnection, as the global nlconnection variable can now be a pointer as well. Signed-off-by: David Sommerseth <davids@redhat.com>
1 parent 19be403 commit b80e659

File tree

4 files changed

+25
-32
lines changed

4 files changed

+25
-32
lines changed

python-ethtool/etherinfo.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ void dump_etherinfo(FILE *fp, struct etherinfo *ptr)
274274
*
275275
* @return Returns 1 on success, otherwise 0.
276276
*/
277-
int get_etherinfo(struct etherinfo *ethinf, struct _nlconnection *nlc, nlQuery query)
277+
int get_etherinfo(struct etherinfo *ethinf, struct nl_handle *nlc, nlQuery query)
278278
{
279279
struct nl_cache *link_cache;
280280
struct nl_cache *addr_cache;
@@ -291,7 +291,7 @@ int get_etherinfo(struct etherinfo *ethinf, struct _nlconnection *nlc, nlQuery q
291291
* interface index if we have that
292292
*/
293293
if( ethinf->index < 0 ) {
294-
link_cache = rtnl_link_alloc_cache(nlc->nlrt_handle);
294+
link_cache = rtnl_link_alloc_cache(nlc);
295295
ethinf->index = rtnl_link_name2i(link_cache, ethinf->device);
296296
if( ethinf->index < 0 ) {
297297
return 0;
@@ -303,7 +303,7 @@ int get_etherinfo(struct etherinfo *ethinf, struct _nlconnection *nlc, nlQuery q
303303
switch( query ) {
304304
case NLQRY_LINK:
305305
/* Extract MAC/hardware address of the interface */
306-
link_cache = rtnl_link_alloc_cache(nlc->nlrt_handle);
306+
link_cache = rtnl_link_alloc_cache(nlc);
307307
link = rtnl_link_alloc();
308308
rtnl_link_set_ifindex(link, ethinf->index);
309309
nl_cache_foreach_filter(link_cache, (struct nl_object *)link, callback_nl_link, ethinf);
@@ -318,7 +318,7 @@ int get_etherinfo(struct etherinfo *ethinf, struct _nlconnection *nlc, nlQuery q
318318
ethinf->ipv6_addresses = NULL;
319319

320320
/* Extract IP address information */
321-
addr_cache = rtnl_addr_alloc_cache(nlc->nlrt_handle);
321+
addr_cache = rtnl_addr_alloc_cache(nlc);
322322
addr = rtnl_addr_alloc();
323323
rtnl_addr_set_ifindex(addr, ethinf->index);
324324
nl_cache_foreach_filter(addr_cache, (struct nl_object *)addr, callback_nl_address, ethinf);

python-ethtool/etherinfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
typedef enum {NLQRY_LINK, NLQRY_ADDR} nlQuery; /**< Supported query types in the etherinfo code */
2828

29-
int get_etherinfo(struct etherinfo *ethinf, struct _nlconnection *nlc, nlQuery query);
29+
int get_etherinfo(struct etherinfo *ethinf, struct nl_handle *nlc, nlQuery query);
3030
void free_etherinfo(struct etherinfo *ptr);
3131
void dump_etherinfo(FILE *, struct etherinfo *);
3232

python-ethtool/etherinfo_struct.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,14 @@ struct ipv6address {
5151
struct ipv6address *next; /**< Pointer to next configured IPv6 address */
5252
};
5353

54-
/**
55-
* NETLINK connection handle and related information to be shared
56-
* among all the instantiated etherinfo objects.
57-
*/
58-
struct _nlconnection {
59-
struct nl_handle *nlrt_handle;
60-
};
6154

6255
/**
6356
* Contains the internal data structure of the
6457
* ethtool.etherinfo object.
6558
*
6659
*/
6760
struct etherinfo_obj_data {
68-
struct _nlconnection *nlc; /**< Contains NETLINK connection info */
61+
struct nl_handle *nlc; /**< Contains NETLINK connection info */
6962
struct etherinfo *ethinfo; /**< Contains info about our current interface */
7063
};
7164

python-ethtool/ethtool.c

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include "etherinfo_obj.h"
3232
#include "etherinfo.h"
3333

34-
static struct _nlconnection nlconnection;
34+
static struct nl_handle *nlconnection = NULL;
3535
extern PyTypeObject ethtool_etherinfoType;
3636
extern PyTypeObject ethtool_etherinfoIPv6Type;
3737

@@ -307,7 +307,7 @@ static PyObject *get_interfaces_info(PyObject *self __unused, PyObject *args) {
307307
*/
308308
objdata->ethinfo->device = strdup(fetch_devs[i]);
309309
objdata->ethinfo->index = -1;
310-
objdata->nlc = &nlconnection; /* Global variable */
310+
objdata->nlc = nlconnection; /* Global variable */
311311

312312
/* Instantiate a new etherinfo object with the device information */
313313
ethinf_py = PyCObject_FromVoidPtr(objdata, NULL);
@@ -971,48 +971,48 @@ static struct PyMethodDef PyEthModuleMethods[] = {
971971

972972

973973
/**
974-
* Connects to the NETLINK interface and stores the connection handles in the given struct. This
975-
* should be called as part of the main ethtool module init.
974+
* Connects to the NETLINK interface. This should only be
975+
* called once as part of the main ethtool module init.
976976
*
977-
* @param nlc Structure which keeps the NETLINK connection handle
977+
* @param nlc Structure which keeps the NETLINK connection handle (struct nl_handle)
978978
*
979979
* @return Returns 1 on success, otherwise 0.
980980
*/
981-
int open_netlink(struct _nlconnection *nlc)
981+
int open_netlink(struct nl_handle **nlc)
982982
{
983-
if( !nlc ) {
983+
if( *nlc ) {
984984
return 0;
985985
}
986986

987-
nlc->nlrt_handle = nl_handle_alloc();
988-
nl_connect(nlc->nlrt_handle, NETLINK_ROUTE);
989-
return (nlc->nlrt_handle != NULL);
987+
*nlc = nl_handle_alloc();
988+
nl_connect(*nlc, NETLINK_ROUTE);
989+
return (*nlc != NULL);
990990
}
991991

992992

993993
/**
994994
* Closes the NETLINK connection. This should be called automatically whenever
995995
* the ethtool module is unloaded from Python.
996996
*
997-
* @param ptr Points at a struct _nlconnection object with an open NETLINK connection
997+
* @param ptr Pointer to the pointer of struct nl_handle, which contains the NETLINK connection
998998
*/
999-
void close_netlink(void *ptr)
999+
void close_netlink(void **ptr)
10001000
{
1001-
struct _nlconnection *nlc;
1001+
struct nl_handle *nlc;
10021002

1003-
if( !ptr ) {
1003+
if( !ptr && !*ptr ) {
10041004
return;
10051005
}
10061006

1007-
nlc = (struct _nlconnection *) ptr;
1008-
if( !nlc->nlrt_handle ) {
1007+
nlc = (struct nl_handle *) *ptr;
1008+
if( !nlc ) {
10091009
return;
10101010
}
10111011

10121012
/* Close NETLINK connection */
1013-
nl_close(nlc->nlrt_handle);
1014-
nl_handle_destroy(nlc->nlrt_handle);
1015-
nlc->nlrt_handle = NULL;
1013+
nl_close(nlc);
1014+
nl_handle_destroy(nlc);
1015+
*ptr = NULL; /* reset the pointers pointer address */
10161016
}
10171017

10181018

0 commit comments

Comments
 (0)