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> |
86 |
|
} |
87 |
|
|
88 |
|
VECTOR_DECLARE_STATIC(network_stats, sg_network_io_stats, 5, |
89 |
< |
network_stat_init, network_stat_destroy); |
89 |
> |
network_stat_init, network_stat_destroy); |
90 |
|
|
91 |
|
sg_network_io_stats *sg_get_network_io_stats(int *entries){ |
92 |
|
int interfaces; |
93 |
|
sg_network_io_stats *network_stat_ptr; |
94 |
|
|
95 |
|
#ifdef SOLARIS |
96 |
< |
kstat_ctl_t *kc; |
97 |
< |
kstat_t *ksp; |
96 |
> |
kstat_ctl_t *kc; |
97 |
> |
kstat_t *ksp; |
98 |
|
kstat_named_t *knp; |
99 |
|
#endif |
100 |
|
|
112 |
|
|
113 |
|
#ifdef ALLBSD |
114 |
|
if(getifaddrs(&net) != 0){ |
115 |
+ |
sg_set_error(SG_ERROR_GETIFADDRS, NULL); |
116 |
|
return NULL; |
117 |
|
} |
118 |
|
|
127 |
|
network_stat_ptr=network_stats+interfaces; |
128 |
|
|
129 |
|
if (sg_update_string(&network_stat_ptr->interface_name, |
130 |
< |
net_ptr->ifa_name) < 0) { |
130 |
> |
net_ptr->ifa_name) < 0) { |
131 |
|
return NULL; |
132 |
|
} |
133 |
|
net_data=(struct if_data *)net_ptr->ifa_data; |
145 |
|
#endif |
146 |
|
|
147 |
|
#ifdef SOLARIS |
148 |
< |
if ((kc = kstat_open()) == NULL) { |
149 |
< |
return NULL; |
150 |
< |
} |
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) { |
156 |
< |
if (!strcmp(ksp->ks_class, "net")) { |
157 |
< |
kstat_read(kc, ksp, NULL); |
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 |
|
|
159 |
|
#ifdef SOL7 |
160 |
|
#define LRX "rbytes" |
228 |
|
|
229 |
|
/* Read interface name */ |
230 |
|
if (sg_update_string(&network_stat_ptr->interface_name, |
231 |
< |
ksp->ks_name) < 0) { |
231 |
> |
ksp->ks_name) < 0) { |
232 |
|
return NULL; |
233 |
|
} |
234 |
|
|
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 |
|
|
267 |
|
if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) { |
268 |
|
return NULL; |
269 |
|
} |
270 |
< |
network_stat_ptr=network_stats+interfaces; |
270 |
> |
network_stat_ptr=network_stats+interfaces; |
271 |
|
|
272 |
|
if(network_stat_ptr->interface_name!=NULL){ |
273 |
|
free(network_stat_ptr->interface_name); |
291 |
|
#endif |
292 |
|
|
293 |
|
#ifdef CYGWIN |
294 |
+ |
sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin"); |
295 |
|
return NULL; |
296 |
|
#endif |
297 |
|
|
317 |
|
|
318 |
|
sg_network_io_stats *sg_get_network_io_stats_diff(int *entries) { |
319 |
|
VECTOR_DECLARE_STATIC(diff, sg_network_io_stats, 1, |
320 |
< |
network_stat_init, network_stat_destroy); |
320 |
> |
network_stat_init, network_stat_destroy); |
321 |
|
sg_network_io_stats *src = NULL, *dest; |
322 |
|
int i, j, diff_count, new_count; |
323 |
|
|
338 |
|
dest = &diff[i]; |
339 |
|
|
340 |
|
if (sg_update_string(&dest->interface_name, |
341 |
< |
src->interface_name) < 0) { |
341 |
> |
src->interface_name) < 0) { |
342 |
|
return NULL; |
343 |
|
} |
344 |
|
dest->rx = src->rx; |
404 |
|
|
405 |
|
sg_network_iface_stats *sg_get_network_iface_stats(int *entries){ |
406 |
|
VECTOR_DECLARE_STATIC(network_iface_stats, sg_network_iface_stats, 5, |
407 |
< |
network_iface_stat_init, network_iface_stat_destroy); |
407 |
> |
network_iface_stat_init, network_iface_stat_destroy); |
408 |
|
sg_network_iface_stats *network_iface_stat_ptr; |
409 |
|
int ifaces = 0; |
410 |
|
|
411 |
|
#ifdef SOLARIS |
412 |
< |
kstat_ctl_t *kc; |
413 |
< |
kstat_t *ksp; |
412 |
> |
kstat_ctl_t *kc; |
413 |
> |
kstat_t *ksp; |
414 |
|
kstat_named_t *knp; |
415 |
|
int sock; |
416 |
|
#endif |
417 |
|
#ifdef ALLBSD |
418 |
< |
struct ifaddrs *net, *net_ptr; |
418 |
> |
struct ifaddrs *net, *net_ptr; |
419 |
|
struct ifmediareq ifmed; |
420 |
|
struct ifreq ifr; |
421 |
|
int sock; |
422 |
|
int x; |
423 |
|
#endif |
424 |
|
#ifdef LINUX |
425 |
< |
FILE *f; |
426 |
< |
/* Horrible big enough, but it should be easily big enough */ |
427 |
< |
char line[8096]; |
425 |
> |
FILE *f; |
426 |
> |
/* Horrible big enough, but it should be easily big enough */ |
427 |
> |
char line[8096]; |
428 |
|
int sock; |
429 |
|
#endif |
430 |
|
|
431 |
|
#ifdef ALLBSD |
432 |
< |
if(getifaddrs(&net) != 0){ |
433 |
< |
return NULL; |
434 |
< |
} |
432 |
> |
if(getifaddrs(&net) != 0){ |
433 |
> |
sg_set_error(SG_ERROR_GETIFADDRS, NULL); |
434 |
> |
return NULL; |
435 |
> |
} |
436 |
|
|
437 |
|
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return NULL; |
438 |
|
|
439 |
|
for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){ |
440 |
< |
if(net_ptr->ifa_addr->sa_family != AF_LINK) continue; |
440 |
> |
if(net_ptr->ifa_addr->sa_family != AF_LINK) continue; |
441 |
|
|
442 |
|
if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) { |
443 |
< |
return NULL; |
444 |
< |
} |
445 |
< |
network_iface_stat_ptr = network_iface_stats + ifaces; |
443 |
> |
return NULL; |
444 |
> |
} |
445 |
> |
network_iface_stat_ptr = network_iface_stats + ifaces; |
446 |
|
|
447 |
|
memset(&ifr, 0, sizeof(ifr)); |
448 |
|
strncpy(ifr.ifr_name, net_ptr->ifa_name, sizeof(ifr.ifr_name)); |
457 |
|
} |
458 |
|
|
459 |
|
if (sg_update_string(&network_iface_stat_ptr->interface_name, |
460 |
< |
net_ptr->ifa_name) < 0) { |
460 |
> |
net_ptr->ifa_name) < 0) { |
461 |
|
return NULL; |
462 |
|
} |
463 |
|
|
533 |
|
|
534 |
|
#ifdef SOLARIS |
535 |
|
if ((kc = kstat_open()) == NULL) { |
536 |
+ |
sg_set_error(SG_ERROR_KSTAT_OPEN, NULL); |
537 |
|
return NULL; |
538 |
|
} |
539 |
|
|
540 |
|
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) { |
541 |
+ |
sg_set_error(SG_ERROR_SOCKET, NULL); |
542 |
|
return NULL; |
543 |
|
} |
544 |
|
|
561 |
|
ifaces++; |
562 |
|
|
563 |
|
if (sg_update_string(&network_iface_stat_ptr->interface_name, |
564 |
< |
ksp->ks_name) < 0) { |
564 |
> |
ksp->ks_name) < 0) { |
565 |
|
return NULL; |
566 |
|
} |
567 |
|
|
596 |
|
#endif |
597 |
|
#ifdef LINUX |
598 |
|
f = fopen("/proc/net/dev", "r"); |
599 |
< |
if(f == NULL){ |
600 |
< |
return NULL; |
601 |
< |
} |
599 |
> |
if(f == NULL){ |
600 |
> |
sg_set_error(SG_ERROR_OPEN, "/proc/net/dev"); |
601 |
> |
return NULL; |
602 |
> |
} |
603 |
|
|
604 |
|
/* Setup stuff so we can do the ioctl to get the info */ |
605 |
|
if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){ |
606 |
+ |
sg_set_error(SG_ERROR_SOCKET, NULL); |
607 |
|
return NULL; |
608 |
|
} |
609 |
|
|
610 |
|
/* Ignore first 2 lines.. Just headings */ |
611 |
< |
if((fgets(line, sizeof(line), f)) == NULL) return NULL; |
612 |
< |
if((fgets(line, sizeof(line), f)) == NULL) return NULL; |
611 |
> |
if((fgets(line, sizeof(line), f)) == NULL) { |
612 |
> |
sg_set_error(SG_ERROR_PARSE, NULL); |
613 |
> |
return NULL; |
614 |
> |
} |
615 |
> |
if((fgets(line, sizeof(line), f)) == NULL) { |
616 |
> |
sg_set_error(SG_ERROR_PARSE, NULL); |
617 |
> |
return NULL; |
618 |
> |
} |
619 |
|
|
620 |
< |
while((fgets(line, sizeof(line), f)) != NULL){ |
621 |
< |
char *name, *ptr; |
622 |
< |
struct ifreq ifr; |
623 |
< |
struct ethtool_cmd ethcmd; |
624 |
< |
int err; |
620 |
> |
while((fgets(line, sizeof(line), f)) != NULL){ |
621 |
> |
char *name, *ptr; |
622 |
> |
struct ifreq ifr; |
623 |
> |
struct ethtool_cmd ethcmd; |
624 |
> |
int err; |
625 |
|
|
626 |
|
/* Get the interface name */ |
627 |
< |
ptr = strchr(line, ':'); |
628 |
< |
if (ptr == NULL) continue; |
629 |
< |
*ptr='\0'; |
630 |
< |
name = line; |
631 |
< |
while(isspace(*(name))){ |
632 |
< |
name++; |
633 |
< |
} |
627 |
> |
ptr = strchr(line, ':'); |
628 |
> |
if (ptr == NULL) continue; |
629 |
> |
*ptr='\0'; |
630 |
> |
name = line; |
631 |
> |
while(isspace(*(name))){ |
632 |
> |
name++; |
633 |
> |
} |
634 |
|
|
635 |
< |
memset(&ifr, 0, sizeof ifr); |
636 |
< |
strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); |
635 |
> |
memset(&ifr, 0, sizeof ifr); |
636 |
> |
strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); |
637 |
|
|
638 |
|
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) { |
639 |
|
continue; |
646 |
|
network_iface_stat_ptr = network_iface_stats + ifaces; |
647 |
|
|
648 |
|
if (sg_update_string(&network_iface_stat_ptr->interface_name, |
649 |
< |
name) < 0) { |
649 |
> |
name) < 0) { |
650 |
|
return NULL; |
651 |
|
} |
652 |
|
if ((ifr.ifr_flags & IFF_UP) != 0) { |
655 |
|
network_iface_stat_ptr->up = 0; |
656 |
|
} |
657 |
|
|
658 |
< |
memset(ðcmd, 0, sizeof ethcmd); |
659 |
< |
ethcmd.cmd = ETHTOOL_GSET; |
660 |
< |
ifr.ifr_data = (caddr_t) ðcmd; |
658 |
> |
memset(ðcmd, 0, sizeof ethcmd); |
659 |
> |
ethcmd.cmd = ETHTOOL_GSET; |
660 |
> |
ifr.ifr_data = (caddr_t) ðcmd; |
661 |
|
|
662 |
< |
err = ioctl(sock, SIOCETHTOOL, &ifr); |
663 |
< |
if (err == 0) { |
662 |
> |
err = ioctl(sock, SIOCETHTOOL, &ifr); |
663 |
> |
if (err == 0) { |
664 |
|
network_iface_stat_ptr->speed = ethcmd.speed; |
665 |
|
|
666 |
|
switch (ethcmd.duplex) { |