Skip to content

Commit d3f5fd7

Browse files
author
David Sommerseth
committed
Merge PyNetlinkIPv4Address and PyNetlinkIPv6Address classes
Simplify the overall implementation by reusing code more efficiently. The differences between the IPv4 and IPv6 implementation in libnl is minimal and can more easily be differentiated those few places its needed instead. Signed-off-by: David Sommerseth <davids@redhat.com>
1 parent f8b0e05 commit d3f5fd7

File tree

4 files changed

+123
-167
lines changed

4 files changed

+123
-167
lines changed

python-ethtool/etherinfo_obj.c

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ int _ethtool_etherinfo_init(etherinfo_py *self, PyObject *args, PyObject *kwds)
100100
101101
The return value is a *borrowed reference* (or NULL)
102102
*/
103-
static PyNetlinkIPv4Address*
104-
get_last_address(etherinfo_py *self)
103+
static PyNetlinkIPaddress * get_last_ipv4_address(etherinfo_py *self)
105104
{
106105
Py_ssize_t size;
107106
PyObject *list;
@@ -119,8 +118,8 @@ get_last_address(etherinfo_py *self)
119118
size = PyList_Size(list);
120119
if (size > 0) {
121120
PyObject *item = PyList_GetItem(list, size - 1);
122-
if (Py_TYPE(item) == &ethtool_netlink_ipv4_address_Type) {
123-
return (PyNetlinkIPv4Address*)item;
121+
if (Py_TYPE(item) == &ethtool_netlink_ip_address_Type) {
122+
return (PyNetlinkIPaddress*)item;
124123
}
125124
}
126125

@@ -138,7 +137,7 @@ get_last_address(etherinfo_py *self)
138137
PyObject *_ethtool_etherinfo_getter(etherinfo_py *self, PyObject *attr_o)
139138
{
140139
char *attr = PyString_AsString(attr_o);
141-
PyNetlinkIPv4Address *py_addr;
140+
PyNetlinkIPaddress *py_addr;
142141

143142
if( !self || !self->data ) {
144143
PyErr_SetString(PyExc_AttributeError, "No data available");
@@ -153,24 +152,24 @@ PyObject *_ethtool_etherinfo_getter(etherinfo_py *self, PyObject *attr_o)
153152
} else if( strcmp(attr, "ipv4_address") == 0 ) {
154153
get_etherinfo(self->data, NLQRY_ADDR);
155154
/* For compatiblity with old approach, return last IPv4 address: */
156-
py_addr = get_last_address(self);
155+
py_addr = get_last_ipv4_address(self);
157156
if (py_addr) {
158-
if (py_addr->ipv4_address) {
159-
Py_INCREF(py_addr->ipv4_address);
160-
return py_addr->ipv4_address;
157+
if (py_addr->local) {
158+
Py_INCREF(py_addr->local);
159+
return py_addr->local;
161160
}
162161
}
163162
Py_RETURN_NONE;
164163
} else if( strcmp(attr, "ipv4_netmask") == 0 ) {
165164
get_etherinfo(self->data, NLQRY_ADDR);
166-
py_addr = get_last_address(self);
165+
py_addr = get_last_ipv4_address(self);
167166
if (py_addr) {
168-
return PyInt_FromLong(py_addr->ipv4_netmask);
167+
return PyInt_FromLong(py_addr->prefixlen);
169168
}
170169
return PyInt_FromLong(0);
171170
} else if( strcmp(attr, "ipv4_broadcast") == 0 ) {
172171
get_etherinfo(self->data, NLQRY_ADDR);
173-
py_addr = get_last_address(self);
172+
py_addr = get_last_ipv4_address(self);
174173
if (py_addr) {
175174
if (py_addr->ipv4_broadcast) {
176175
Py_INCREF(py_addr->ipv4_broadcast);
@@ -229,10 +228,10 @@ PyObject *_ethtool_etherinfo_str(etherinfo_py *self)
229228
if( self->data->ethinfo->ipv4_addresses ) {
230229
Py_ssize_t i;
231230
for (i = 0; i < PyList_Size(self->data->ethinfo->ipv4_addresses); i++) {
232-
PyNetlinkIPv4Address *py_addr = (PyNetlinkIPv4Address *)PyList_GetItem(self->data->ethinfo->ipv4_addresses, i);
231+
PyNetlinkIPaddress *py_addr = (PyNetlinkIPaddress *)PyList_GetItem(self->data->ethinfo->ipv4_addresses, i);
233232
PyObject *tmp = PyString_FromFormat("\tIPv4 address: ");
234-
PyString_Concat(&tmp, py_addr->ipv4_address);
235-
PyString_ConcatAndDel(&tmp, PyString_FromFormat("/%d", py_addr->ipv4_netmask));
233+
PyString_Concat(&tmp, py_addr->local);
234+
PyString_ConcatAndDel(&tmp, PyString_FromFormat("/%d", py_addr->prefixlen));
236235
if (py_addr->ipv4_broadcast ) {
237236
PyString_ConcatAndDel(&tmp,
238237
PyString_FromString(" Broadcast: "));
@@ -246,12 +245,12 @@ PyObject *_ethtool_etherinfo_str(etherinfo_py *self)
246245
if( self->data->ethinfo->ipv6_addresses ) {
247246
Py_ssize_t i;
248247
for (i = 0; i < PyList_Size(self->data->ethinfo->ipv6_addresses); i++) {
249-
PyNetlinkIPv6Address *py_addr = (PyNetlinkIPv6Address *)PyList_GetItem(self->data->ethinfo->ipv6_addresses, i);
248+
PyNetlinkIPaddress *py_addr = (PyNetlinkIPaddress *)PyList_GetItem(self->data->ethinfo->ipv6_addresses, i);
250249
PyObject *tmp = PyString_FromFormat("\tIPv6 address: [");
251-
PyString_Concat(&tmp, py_addr->ipv6_scope);
250+
PyString_Concat(&tmp, py_addr->scope);
252251
PyString_ConcatAndDel(&tmp, PyString_FromString("] "));
253-
PyString_Concat(&tmp, py_addr->ipv6_address);
254-
PyString_ConcatAndDel(&tmp, PyString_FromFormat("/%d", py_addr->ipv6_netmask));
252+
PyString_Concat(&tmp, py_addr->local);
253+
PyString_ConcatAndDel(&tmp, PyString_FromFormat("/%d", py_addr->prefixlen));
255254
PyString_ConcatAndDel(&tmp, PyString_FromString("\n"));
256255
PyString_ConcatAndDel(&ret, tmp);
257256
}
@@ -318,10 +317,9 @@ static PyObject *_ethtool_etherinfo_get_ipv6_addresses(etherinfo_py *self, PyObj
318317
*/
319318
static PyMethodDef _ethtool_etherinfo_methods[] = {
320319
{"get_ipv4_addresses", (PyCFunction)_ethtool_etherinfo_get_ipv4_addresses, METH_NOARGS,
321-
"Retrieve configured IPv4 addresses. Returns a list of NetlinkIP4Address objects"},
320+
"Retrieve configured IPv4 addresses. Returns a list of NetlinkIPaddress objects"},
322321
{"get_ipv6_addresses", (PyCFunction)_ethtool_etherinfo_get_ipv6_addresses, METH_NOARGS,
323-
"Retrieve configured IPv6 addresses. Returns a list of NetlinkIP6Address objects"},
324-
322+
"Retrieve configured IPv6 addresses. Returns a list of NetlinkIPaddress objects"},
325323
{NULL} /**< No methods defined */
326324
};
327325

python-ethtool/etherinfo_struct.h

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,16 @@ struct etherinfo {
3939
};
4040

4141
/* Python object containing data baked from a (struct rtnl_addr) */
42-
typedef struct PyNetlinkIPv4Address {
42+
typedef struct PyNetlinkIPaddress {
4343
PyObject_HEAD
44-
PyObject *ipv4_address; /**< string: Configured IPv4 address */
45-
int ipv4_netmask; /**< int: Configured IPv4 netmask */
44+
int family; /**< int: must be AF_INET or AF_INET6 */
45+
PyObject *local; /**< string: Configured local IP address */
46+
PyObject *peer; /**< string: Configured peer IP address */
4647
PyObject *ipv4_broadcast; /**< string: Configured IPv4 broadcast address */
47-
} PyNetlinkIPv4Address;
48-
extern PyTypeObject ethtool_netlink_ipv4_address_Type;
49-
50-
/* Python object containing data baked from a (struct rtnl_addr) */
51-
typedef struct PyNetlinkIPv6Address {
52-
PyObject_HEAD
53-
PyObject *ipv6_address; /**< string: Configured IPv6 address */
54-
int ipv6_netmask; /**< int: Configured IPv6 prefix */
55-
PyObject *ipv6_scope; /**< string: IPv6 address scope */
56-
} PyNetlinkIPv6Address;
57-
extern PyTypeObject ethtool_netlink_ipv6_address_Type;
48+
int prefixlen; /**< int: Configured network prefix (netmask) */
49+
PyObject *scope; /**< string: IP address scope */
50+
} PyNetlinkIPaddress;
51+
extern PyTypeObject ethtool_netlink_ip_address_Type;
5852

5953
/**
6054
* Contains the internal data structure of the

python-ethtool/ethtool.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -969,9 +969,7 @@ PyMODINIT_FUNC initethtool(void)
969969
PyModule_AddObject(m, "etherinfo", (PyObject *)&ethtool_etherinfoType);
970970

971971
// Prepare the ethtool IPv6 and IPv4 address types
972-
if (PyType_Ready(&ethtool_netlink_ipv6_address_Type) < 0)
973-
return;
974-
if (PyType_Ready(&ethtool_netlink_ipv4_address_Type))
972+
if (PyType_Ready(&ethtool_netlink_ip_address_Type))
975973
return;
976974

977975
// Setup constants

0 commit comments

Comments
 (0)