45 |
|
#ifdef LINUX |
46 |
|
#include <stdio.h> |
47 |
|
#include <sys/types.h> |
48 |
– |
#include <regex.h> |
48 |
|
#include <sys/ioctl.h> |
49 |
|
#include <sys/socket.h> |
50 |
|
#include <net/if.h> |
112 |
|
|
113 |
|
#ifdef ALLBSD |
114 |
|
if(getifaddrs(&net) != 0){ |
115 |
+ |
sg_set_error(SG_ERROR_GETIFADDRS, NULL); |
116 |
|
return NULL; |
117 |
|
} |
118 |
|
|
146 |
|
|
147 |
|
#ifdef SOLARIS |
148 |
|
if ((kc = kstat_open()) == NULL) { |
149 |
+ |
sg_set_error(SG_ERROR_KSTAT_OPEN, NULL); |
150 |
|
return NULL; |
151 |
|
} |
152 |
|
|
153 |
|
interfaces=0; |
154 |
|
|
155 |
< |
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
155 |
> |
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
156 |
|
if (!strcmp(ksp->ks_class, "net")) { |
157 |
|
kstat_read(kc, ksp, NULL); |
158 |
|
|
244 |
|
#ifdef LINUX |
245 |
|
f=fopen("/proc/net/dev", "r"); |
246 |
|
if(f==NULL){ |
247 |
+ |
sg_set_error(SG_ERROR_OPEN, "/proc/net/dev"); |
248 |
|
return NULL; |
249 |
|
} |
250 |
|
/* read the 2 lines.. Its the title, so we dont care :) */ |
253 |
|
|
254 |
|
|
255 |
|
if((regcomp(®ex, "^ *([^:]+): *([0-9]+) +([0-9]+) +([0-9]+) +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +([0-9]+) +([0-9]+) +([0-9]+) +[0-9]+ +[0-9]+ +([0-9]+)", REG_EXTENDED))!=0){ |
256 |
+ |
sg_set_error(SG_ERROR_PARSE, NULL); |
257 |
|
return NULL; |
258 |
|
} |
259 |
|
|
291 |
|
#endif |
292 |
|
|
293 |
|
#ifdef CYGWIN |
294 |
+ |
sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin"); |
295 |
|
return NULL; |
296 |
|
#endif |
297 |
|
|
390 |
|
return diff; |
391 |
|
} |
392 |
|
|
393 |
+ |
int sg_network_io_compare_name(const void *va, const void *vb) { |
394 |
+ |
const sg_network_io_stats *a = (const sg_network_io_stats *)va; |
395 |
+ |
const sg_network_io_stats *b = (const sg_network_io_stats *)vb; |
396 |
+ |
|
397 |
+ |
return strcmp(a->interface_name, b->interface_name); |
398 |
+ |
} |
399 |
+ |
|
400 |
|
/* NETWORK INTERFACE STATS */ |
401 |
|
|
402 |
|
static void network_iface_stat_init(sg_network_iface_stats *s) { |
437 |
|
|
438 |
|
#ifdef ALLBSD |
439 |
|
if(getifaddrs(&net) != 0){ |
440 |
+ |
sg_set_error(SG_ERROR_GETIFADDRS, NULL); |
441 |
|
return NULL; |
442 |
|
} |
443 |
|
|
540 |
|
|
541 |
|
#ifdef SOLARIS |
542 |
|
if ((kc = kstat_open()) == NULL) { |
543 |
+ |
sg_set_error(SG_ERROR_KSTAT_OPEN, NULL); |
544 |
|
return NULL; |
545 |
|
} |
546 |
|
|
547 |
|
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) { |
548 |
+ |
sg_set_error(SG_ERROR_SOCKET, NULL); |
549 |
|
return NULL; |
550 |
|
} |
551 |
|
|
604 |
|
#ifdef LINUX |
605 |
|
f = fopen("/proc/net/dev", "r"); |
606 |
|
if(f == NULL){ |
607 |
+ |
sg_set_error(SG_ERROR_OPEN, "/proc/net/dev"); |
608 |
|
return NULL; |
609 |
|
} |
610 |
|
|
611 |
|
/* Setup stuff so we can do the ioctl to get the info */ |
612 |
|
if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){ |
613 |
+ |
sg_set_error(SG_ERROR_SOCKET, NULL); |
614 |
|
return NULL; |
615 |
|
} |
616 |
|
|
617 |
|
/* Ignore first 2 lines.. Just headings */ |
618 |
< |
if((fgets(line, sizeof(line), f)) == NULL) return NULL; |
619 |
< |
if((fgets(line, sizeof(line), f)) == NULL) return NULL; |
618 |
> |
if((fgets(line, sizeof(line), f)) == NULL) { |
619 |
> |
sg_set_error(SG_ERROR_PARSE, NULL); |
620 |
> |
return NULL; |
621 |
> |
} |
622 |
> |
if((fgets(line, sizeof(line), f)) == NULL) { |
623 |
> |
sg_set_error(SG_ERROR_PARSE, NULL); |
624 |
> |
return NULL; |
625 |
> |
} |
626 |
|
|
627 |
|
while((fgets(line, sizeof(line), f)) != NULL){ |
628 |
|
char *name, *ptr; |
693 |
|
#endif |
694 |
|
*entries = ifaces; |
695 |
|
return network_iface_stats; |
696 |
+ |
} |
697 |
+ |
|
698 |
+ |
int sg_network_iface_compare_name(const void *va, const void *vb) { |
699 |
+ |
const sg_network_iface_stats *a = (const sg_network_iface_stats *)va; |
700 |
+ |
const sg_network_iface_stats *b = (const sg_network_iface_stats *)vb; |
701 |
+ |
|
702 |
+ |
return strcmp(a->interface_name, b->interface_name); |
703 |
|
} |
704 |
|
|