Skip to content

Commit e957168

Browse files
author
David Sommerseth
committed
Even more clean up. Improved more error messages
1 parent 8a75248 commit e957168

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

python-ethtool/etherinfo.c

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ int update_etherinfo(struct etherinfo *ipadrchain, int index, int af_type,
7474
}
7575
}
7676
if( ptr == NULL ) {
77-
return 0;
77+
PyErr_SetString(PyExc_RuntimeError,
78+
"Could not locate interface record");
79+
return 0;
7880
}
7981

8082
switch( af_type ) {
@@ -221,7 +223,7 @@ int read_netlink_results(int fd, struct sockaddr_nl *local,
221223
}
222224

223225
if (status == 0) {
224-
fprintf(stderr, "** ERROR ** EOF on netlink\n");
226+
PyErr_SetString(PyExc_RuntimeError, "EOF on netlink");
225227
return 0;
226228
}
227229

@@ -240,7 +242,8 @@ int read_netlink_results(int fd, struct sockaddr_nl *local,
240242
struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
241243

242244
if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
243-
fprintf(stderr, "** ERROR ** Error message truncated\n");
245+
PyErr_SetString(PyExc_RuntimeError,
246+
"Error message truncated\n");
244247
} else {
245248
errno = -err->error;
246249
PyErr_SetString(PyExc_OSError, strerror(errno));
@@ -249,19 +252,19 @@ int read_netlink_results(int fd, struct sockaddr_nl *local,
249252
}
250253
// Process/decode data
251254
if( !callback(h, ethinf, &process_ethinfo_idxptr) ) {
252-
fprintf(stderr, "** ERROR ** callback failed\n");
255+
// Error already set in callback
253256
return 0;
254257
}
255258
skip_data:
256259
h = NLMSG_NEXT(h, status);
257260
}
258261

259262
if (msg.msg_flags & MSG_TRUNC) {
260-
fprintf(stderr, "** INFO ** Message truncated\n");
263+
PyErr_SetString(PyExc_RuntimeError, "Message truncated\n");
261264
continue;
262265
}
263266
if (status) {
264-
fprintf(stderr, "** WARNING ** Remnant of size %d\n", status);
267+
PyErr_SetString(PyExc_RuntimeError, "Not all data available was processed");
265268
return 0;
266269
}
267270
}
@@ -322,6 +325,8 @@ int etherinfo_proc_getlink(struct nlmsghdr *msg, struct etherinfo *ethinfchain,
322325
// Append new record if we hit the end of the chain
323326
(*idxptr)->next = new_etherinfo_record();
324327
if( *idxptr == NULL ) {
328+
PyErr_SetString(PyExc_RuntimeError,
329+
"Could not allocate memory to another interface");
325330
return 0;
326331
}
327332
}
@@ -333,10 +338,20 @@ int etherinfo_proc_getlink(struct nlmsghdr *msg, struct etherinfo *ethinfchain,
333338
switch( rta->rta_type ) {
334339
case IFLA_IFNAME:
335340
(*idxptr)->device = strdup((char *)RTA_DATA(rta));
341+
if( !(*idxptr)->device ) {
342+
PyErr_SetString(PyExc_RuntimeError,
343+
"Could not allocate memory for interface name");
344+
return 0;
345+
}
336346
break;
337347

338348
case IFLA_ADDRESS:
339349
(*idxptr)->hwaddress = (char *)malloc(258);
350+
if( !(*idxptr)->hwaddress ) {
351+
PyErr_SetString(PyExc_RuntimeError,
352+
"Could not allocate memory for hardware address");
353+
return 0;
354+
}
340355
memset((*idxptr)->hwaddress, 0, 258);
341356
ll_addr_n2a(RTA_DATA(rta), RTA_PAYLOAD(rta), ifinfo->ifi_type,
342357
(*idxptr)->hwaddress, 256);
@@ -369,13 +384,23 @@ int etherinfo_proc_getaddr(struct nlmsghdr *msg, struct etherinfo *ethinfchain,
369384
switch( rta->rta_type ) {
370385
case IFA_ADDRESS: // IP address + netmask
371386
ifa_addr = (char *) malloc(130);
387+
if( !ifa_addr ) {
388+
PyErr_SetString(PyExc_RuntimeError,
389+
"Could not allocate memory for IP address");
390+
return 0;
391+
}
372392
memset(ifa_addr, 0, 130);
373393
inet_ntop(ifaddr->ifa_family, RTA_DATA(rta), ifa_addr, 128);
374394
ifa_netmask = ifaddr->ifa_prefixlen;
375395
break;
376396

377397
case IFA_BROADCAST:
378398
ifa_brd = (char *) malloc(130);
399+
if( !ifa_brd ) {
400+
PyErr_SetString(PyExc_RuntimeError,
401+
"Could not allocate memory for broadcase address");
402+
return 0;
403+
}
379404
memset(ifa_brd, 0, 130);
380405
inet_ntop(ifaddr->ifa_family, RTA_DATA(rta), ifa_brd, 128);
381406
break;
@@ -429,7 +454,7 @@ void dump_etherinfo(FILE *fp, struct etherinfo *ethinfo)
429454

430455
struct etherinfo *get_etherinfo()
431456
{
432-
int fd, result;
457+
int fd;
433458
struct sockaddr_nl local;
434459
struct etherinfo *ethinf = NULL;
435460

@@ -452,37 +477,29 @@ struct etherinfo *get_etherinfo()
452477

453478
// Get some hardware info - ifname, type and hwaddress. Populates ethinf
454479
if( !send_netlink_query(fd, GET_LINK) ) {
455-
result = 1;
456480
goto error;
457481
}
458482
if( !read_netlink_results(fd, &local, etherinfo_proc_getlink, ethinf) ) {
459-
result = 1;
460483
goto error;
461484
}
462485

463486

464487
// IPv4 information - updates the interfaces found in ethinfo
465488
if( !send_netlink_query(fd, GET_IPV4) ) {
466-
result = 1;
467489
goto error;
468490
}
469491
if( !read_netlink_results(fd, &local, etherinfo_proc_getaddr, ethinf) ) {
470-
result = 1;
471492
goto error;
472493
}
473494

474495

475496
// IPv6 information - updates the interfaces found in ethinfo
476497
if( !send_netlink_query(fd, GET_IPV6) ) {
477-
result = 1;
478498
goto error;
479499
}
480500
if( !read_netlink_results(fd, &local, etherinfo_proc_getaddr, ethinf) ) {
481-
result = 1;
482501
goto error;
483502
}
484-
485-
result = 0;
486503
goto exit;
487504

488505
error:

0 commit comments

Comments
 (0)