Skip to content

Commit 089a30a

Browse files
authored
Merge pull request #19
Porting and fixing tests Fixes #11
2 parents f4a537e + d1e38e5 commit 089a30a

File tree

9 files changed

+381
-229
lines changed

9 files changed

+381
-229
lines changed

MANIFEST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ python-ethtool/etherinfo_obj.c
88
python-ethtool/etherinfo_struct.h
99
python-ethtool/etherinfo.h
1010
python-ethtool/etherinfo_obj.h
11+
python-ethtool/include/py3c/compat.h
1112
python-ethtool/netlink.c
1213
python-ethtool/netlink-address.c
1314
man/pethtool.8.asciidoc

python-ethtool/etherinfo.c

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
#include <Python.h>
19+
#include "include/py3c/compat.h"
1920
#include <bytesobject.h>
2021
#include <bits/sockaddr.h>
2122
#include <stdio.h>
@@ -66,7 +67,7 @@ static void callback_nl_link(struct nl_object *obj, void *arg)
6667
if( ethi->hwaddress ) {
6768
Py_XDECREF(ethi->hwaddress);
6869
}
69-
ethi->hwaddress = PyBytes_FromFormat("%s", hwaddr);
70+
ethi->hwaddress = PyStr_FromFormat("%s", hwaddr);
7071
}
7172

7273

@@ -126,18 +127,16 @@ static int _set_device_index(PyEtherInfo *self)
126127
*/
127128
if( self->index < 0 ) {
128129
if( (errno = rtnl_link_alloc_cache(get_nlc(), AF_UNSPEC, &link_cache)) < 0) {
129-
PyErr_SetString(PyExc_OSError, nl_geterror(errno));
130-
return 0;
131-
}
132-
133-
link = rtnl_link_get_by_name(link_cache, PyBytes_AsString(self->device));
134-
if( !link ) {
130+
PyErr_SetString(PyExc_OSError, nl_geterror(errno));
131+
return 0;
132+
}
133+
link = rtnl_link_get_by_name(link_cache, PyStr_AsString(self->device));
134+
if( !link ) {
135135
errno = ENODEV;
136136
PyErr_SetFromErrno(PyExc_IOError);
137137
nl_cache_free(link_cache);
138-
return 0;
139-
}
140-
138+
return 0;
139+
}
141140
self->index = rtnl_link_get_ifindex(link);
142141
if( self->index <= 0 ) {
143142
errno = ENODEV;
@@ -146,6 +145,7 @@ static int _set_device_index(PyEtherInfo *self)
146145
nl_cache_free(link_cache);
147146
return 0;
148147
}
148+
149149
rtnl_link_put(link);
150150
nl_cache_free(link_cache);
151151
}
@@ -180,7 +180,7 @@ int get_etherinfo_link(PyEtherInfo *self)
180180
if( !open_netlink(self) ) {
181181
PyErr_Format(PyExc_RuntimeError,
182182
"Could not open a NETLINK connection for %s",
183-
PyBytes_AsString(self->device));
183+
PyStr_AsString(self->device));
184184
return 0;
185185
}
186186

@@ -234,29 +234,30 @@ PyObject * get_etherinfo_address(PyEtherInfo *self, nlQuery query)
234234
if( !open_netlink(self) ) {
235235
PyErr_Format(PyExc_RuntimeError,
236236
"Could not open a NETLINK connection for %s",
237-
PyBytes_AsString(self->device));
237+
PyStr_AsString(self->device));
238238
return NULL;
239239
}
240240

241-
if( _set_device_index(self) != 1) {
242-
return NULL;
243-
}
241+
if(!_set_device_index(self)) {
242+
return NULL;
243+
}
244244

245245
/* Query the for requested info via NETLINK */
246-
247-
/* Extract IP address information */
248-
if( (err = rtnl_addr_alloc_cache(get_nlc(), &addr_cache)) < 0) {
249-
PyErr_SetString(PyExc_OSError, nl_geterror(err));
250-
nl_cache_free(addr_cache);
251-
return NULL;
252-
}
253-
addr = rtnl_addr_alloc();
254-
if( !addr ) {
255-
errno = ENOMEM;
256-
PyErr_SetFromErrno(PyExc_OSError);
257-
return NULL;
258-
}
259-
rtnl_addr_set_ifindex(addr, self->index);
246+
/* Extract IP address information */
247+
if( (err = rtnl_addr_alloc_cache(get_nlc(), &addr_cache)) < 0) {
248+
PyErr_SetString(PyExc_OSError, nl_geterror(err));
249+
nl_cache_free(addr_cache);
250+
return NULL;
251+
}
252+
253+
addr = rtnl_addr_alloc();
254+
255+
if( !addr ) {
256+
errno = ENOMEM;
257+
PyErr_SetFromErrno(PyExc_OSError);
258+
return NULL;
259+
}
260+
rtnl_addr_set_ifindex(addr, self->index);
260261

261262
switch( query ) {
262263
case NLQRY_ADDR4:

python-ethtool/etherinfo_obj.c

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525

2626
#include <Python.h>
27+
#include "include/py3c/compat.h"
2728
#include <bytesobject.h>
2829
#include "structmember.h"
2930

@@ -87,7 +88,7 @@ static PyNetlinkIPaddress * get_last_ipv4_address(PyObject *addrlist)
8788
PyObject *_ethtool_etherinfo_str(PyEtherInfo *self)
8889
{
8990
PyObject *ret = NULL;
90-
PyObject *ipv4addrs = NULL, *ipv6addrs = NULL;
91+
PyObject *ipv4addrs = NULL, *ipv6addrs = NULL;
9192

9293
if( !self ) {
9394
PyErr_SetString(PyExc_AttributeError, "No data available");
@@ -96,56 +97,46 @@ PyObject *_ethtool_etherinfo_str(PyEtherInfo *self)
9697

9798
get_etherinfo_link(self);
9899

99-
ret = PyBytes_FromFormat("Device ");
100-
PyBytes_Concat(&ret, self->device);
101-
PyBytes_ConcatAndDel(&ret, PyBytes_FromString(":\n"));
100+
ret = PyStr_FromFormat("Device %s:\n", PyStr_AsString(self->device));
102101

103102
if( self->hwaddress ) {
104-
PyBytes_ConcatAndDel(&ret, PyBytes_FromString("\tMAC address: "));
105-
PyBytes_Concat(&ret, self->hwaddress);
106-
PyBytes_ConcatAndDel(&ret, PyBytes_FromString("\n"));
103+
ret = PyStr_Concat(ret,
104+
PyStr_FromFormat("\tMAC address: %s\n",
105+
PyStr_AsString(self->hwaddress)));
107106
}
108107

109108
ipv4addrs = get_etherinfo_address(self, NLQRY_ADDR4);
110109
if( ipv4addrs ) {
111-
Py_ssize_t i;
112-
for (i = 0; i < PyList_Size(ipv4addrs); i++) {
113-
PyNetlinkIPaddress *py_addr = (PyNetlinkIPaddress *)PyList_GetItem(ipv4addrs, i);
114-
PyObject *tmp = PyBytes_FromFormat("\tIPv4 address: ");
115-
PyBytes_Concat(&tmp, py_addr->local);
116-
PyBytes_ConcatAndDel(&tmp, PyBytes_FromFormat("/%d", py_addr->prefixlen));
117-
if (py_addr->ipv4_broadcast ) {
118-
PyBytes_ConcatAndDel(&tmp,
119-
PyBytes_FromString(" Broadcast: "));
120-
PyBytes_Concat(&tmp, py_addr->ipv4_broadcast);
121-
}
122-
PyBytes_ConcatAndDel(&tmp, PyBytes_FromString("\n"));
123-
PyBytes_ConcatAndDel(&ret, tmp);
124-
}
110+
Py_ssize_t i;
111+
for (i = 0; i < PyList_Size(ipv4addrs); i++) {
112+
PyNetlinkIPaddress *py_addr = (PyNetlinkIPaddress *)PyList_GetItem(ipv4addrs, i);
113+
PyObject *tmp = PyStr_FromFormat("\tIPv4 address: ");
114+
tmp = PyStr_Concat(tmp, py_addr->local);
115+
tmp = PyStr_Concat(tmp, PyStr_FromFormat("/%d", py_addr->prefixlen));
116+
if (py_addr->ipv4_broadcast ) {
117+
tmp = PyStr_Concat(tmp,
118+
PyStr_FromFormat("\tBroadcast: %s\n",
119+
PyStr_AsString(py_addr->ipv4_broadcast)));
120+
} else {
121+
tmp = PyStr_Concat(tmp, PyStr_FromFormat("\n"));
122+
}
123+
124+
ret = PyStr_Concat(ret, tmp);
125+
}
125126
}
126127

127128
ipv6addrs = get_etherinfo_address(self, NLQRY_ADDR6);
128129
if( ipv6addrs ) {
129-
Py_ssize_t i;
130-
for (i = 0; i < PyList_Size(ipv6addrs); i++) {
131-
PyNetlinkIPaddress *py_addr = (PyNetlinkIPaddress *)PyList_GetItem(ipv6addrs, i);
132-
PyObject *tmp = PyBytes_FromFormat("\tIPv6 address: [");
133-
PyBytes_Concat(&tmp, py_addr->scope);
134-
PyBytes_ConcatAndDel(&tmp, PyBytes_FromString("] "));
135-
PyBytes_Concat(&tmp, py_addr->local);
136-
PyBytes_ConcatAndDel(&tmp, PyBytes_FromFormat("/%d", py_addr->prefixlen));
137-
PyBytes_ConcatAndDel(&tmp, PyBytes_FromString("\n"));
138-
PyBytes_ConcatAndDel(&ret, tmp);
139-
}
140-
}
141-
142-
#if PY_MAJOR_VERSION >= 3
143-
{
144-
PyObject *bytestr = ret;
145-
ret = PyUnicode_FromString(PyBytes_AsString(bytestr));
146-
Py_DECREF(bytestr);
130+
Py_ssize_t i;
131+
for (i = 0; i < PyList_Size(ipv6addrs); i++) {
132+
PyNetlinkIPaddress *py_addr = (PyNetlinkIPaddress *)PyList_GetItem(ipv6addrs, i);
133+
PyObject *tmp = PyStr_FromFormat("\tIPv6 address: [%s] %s/%d\n",
134+
PyStr_AsString(py_addr->scope),
135+
PyStr_AsString(py_addr->local),
136+
py_addr->prefixlen);
137+
ret = PyStr_Concat(ret, tmp);
138+
}
147139
}
148-
#endif
149140

150141
return ret;
151142
}
@@ -235,7 +226,12 @@ static PyObject *get_ipv4_addr(PyObject *obj, void *info)
235226
return py_addr->local;
236227
}
237228
}
238-
Py_RETURN_NONE;
229+
230+
if (PyErr_Occurred()) {
231+
return NULL;
232+
} else {
233+
Py_RETURN_NONE;
234+
}
239235
}
240236

241237
static PyObject *get_ipv4_mask(PyObject *obj, void *info)
@@ -247,9 +243,14 @@ static PyObject *get_ipv4_mask(PyObject *obj, void *info)
247243
addrlist = get_etherinfo_address(self, NLQRY_ADDR4);
248244
py_addr = get_last_ipv4_address(addrlist);
249245
if (py_addr) {
250-
return PyLong_FromLong(py_addr->prefixlen);
246+
return PyInt_FromLong(py_addr->prefixlen);
247+
}
248+
249+
if (PyErr_Occurred()) {
250+
return NULL;
251+
} else {
252+
return PyInt_FromLong(0);
251253
}
252-
return PyLong_FromLong(0);
253254
}
254255

255256
static PyObject *get_ipv4_bcast(PyObject *obj, void *info)
@@ -266,7 +267,12 @@ static PyObject *get_ipv4_bcast(PyObject *obj, void *info)
266267
return py_addr->ipv4_broadcast;
267268
}
268269
}
269-
Py_RETURN_NONE;
270+
271+
if (PyErr_Occurred()) {
272+
return NULL;
273+
} else {
274+
return PyStr_FromString("0.0.0.0");
275+
}
270276
}
271277

272278

0 commit comments

Comments
 (0)