32 |
|
#ifdef SOLARIS |
33 |
|
#include <kstat.h> |
34 |
|
#include <sys/sysinfo.h> |
35 |
+ |
#include <sys/types.h> |
36 |
+ |
#include <sys/socket.h> |
37 |
+ |
#include <sys/ioctl.h> |
38 |
+ |
#include <net/if.h> |
39 |
+ |
#include <netinet/in.h> |
40 |
+ |
#include <sys/sockio.h> |
41 |
|
#endif |
42 |
|
#ifdef LINUX |
43 |
|
#include <stdio.h> |
54 |
|
typedef __uint8_t u8; |
55 |
|
typedef __uint16_t u16; |
56 |
|
typedef __uint32_t u32; |
57 |
+ |
typedef __uint64_t u64; |
58 |
|
#include <linux/ethtool.h> |
59 |
|
#include <linux/sockios.h> |
60 |
|
#include <unistd.h> |
77 |
|
net_stats->interface_name=NULL; |
78 |
|
net_stats->tx=0; |
79 |
|
net_stats->rx=0; |
80 |
+ |
net_stats->ipackets=0; |
81 |
+ |
net_stats->opackets=0; |
82 |
+ |
net_stats->ierrors=0; |
83 |
+ |
net_stats->oerrors=0; |
84 |
+ |
net_stats->collisions=0; |
85 |
|
net_stats++; |
86 |
|
} |
87 |
|
} |
156 |
|
if(network_stat_ptr->interface_name==NULL) return NULL; |
157 |
|
net_data=(struct if_data *)net_ptr->ifa_data; |
158 |
|
network_stat_ptr->rx=net_data->ifi_ibytes; |
159 |
< |
network_stat_ptr->tx=net_data->ifi_obytes; |
159 |
> |
network_stat_ptr->tx=net_data->ifi_obytes; |
160 |
> |
network_stat_ptr->ipackets=net_data->ifi_ipackets; |
161 |
> |
network_stat_ptr->opackets=net_data->ifi_opackets; |
162 |
> |
network_stat_ptr->ierrors=net_data->ifi_ierrors; |
163 |
> |
network_stat_ptr->oerrors=net_data->ifi_oerrors; |
164 |
> |
network_stat_ptr->collisions=net_data->ifi_collisions; |
165 |
|
network_stat_ptr->systime=time(NULL); |
166 |
|
interfaces++; |
167 |
|
} |
190 |
|
#endif |
191 |
|
|
192 |
|
if((knp=kstat_data_lookup(ksp, RLOOKUP))==NULL){ |
193 |
< |
/* Not a network interface, so skip to the next entry */ |
193 |
> |
/* This is a network interface, but it doesn't |
194 |
> |
* have the rbytes/obytes values; for instance, |
195 |
> |
* the loopback devices have this behaviour |
196 |
> |
* (although they do track packets in/out). */ |
197 |
|
continue; |
198 |
|
} |
199 |
|
|
205 |
|
network_stat_ptr->rx=knp->VALTYPE; |
206 |
|
|
207 |
|
if((knp=kstat_data_lookup(ksp, WLOOKUP))==NULL){ |
188 |
– |
/* Not a network interface, so skip to the next entry */ |
208 |
|
continue; |
209 |
|
} |
210 |
|
network_stat_ptr->tx=knp->VALTYPE; |
230 |
|
fgets(line, sizeof(line), f); |
231 |
|
|
232 |
|
|
233 |
< |
if((regcomp(®ex, "^[[:space:]]*([^:]+):[[:space:]]*([[:digit:]]+)[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+[[:space:]]+([[:digit:]]+)", REG_EXTENDED))!=0){ |
233 |
> |
if((regcomp(®ex, "^[[:space:]]*([^:]+):[[:space:]]*([[:digit:]]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:digit:]]+)[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+[[:space:]]+([[:digit:]]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:digit:]]+)[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+[[:space:]]+([[:digit:]]+)", REG_EXTENDED))!=0){ |
234 |
|
return NULL; |
235 |
|
} |
236 |
|
|
252 |
|
|
253 |
|
network_stat_ptr->interface_name=get_string_match(line, &line_match[1]); |
254 |
|
network_stat_ptr->rx=get_ll_match(line, &line_match[2]); |
255 |
< |
network_stat_ptr->tx=get_ll_match(line, &line_match[3]); |
255 |
> |
network_stat_ptr->tx=get_ll_match(line, &line_match[5]); |
256 |
> |
network_stat_ptr->ipackets=get_ll_match(line, &line_match[3]); |
257 |
> |
network_stat_ptr->opackets=get_ll_match(line, &line_match[6]); |
258 |
> |
network_stat_ptr->ierrors=get_ll_match(line, &line_match[4]); |
259 |
> |
network_stat_ptr->oerrors=get_ll_match(line, &line_match[7]); |
260 |
> |
network_stat_ptr->collisions=get_ll_match(line, &line_match[8]); |
261 |
|
network_stat_ptr->systime=time(NULL); |
262 |
|
|
263 |
|
interfaces++; |
277 |
|
} |
278 |
|
|
279 |
|
long long transfer_diff(long long new, long long old){ |
280 |
< |
#if defined(SOL7) || defined(LINUX) || defined(FREEBSD) |
280 |
> |
#if defined(SOL7) || defined(LINUX) || defined(FREEBSD) || defined(DFBSD) |
281 |
|
#define MAXVAL 4294967296LL |
282 |
|
#else |
283 |
|
#define MAXVAL 18446744073709551616LL |
321 |
|
dest->interface_name = strdup(src->interface_name); |
322 |
|
dest->rx = src->rx; |
323 |
|
dest->tx = src->tx; |
324 |
+ |
dest->ipackets = src->ipackets; |
325 |
+ |
dest->opackets = src->opackets; |
326 |
+ |
dest->ierrors = src->ierrors; |
327 |
+ |
dest->oerrors = src->oerrors; |
328 |
+ |
dest->collisions = src->collisions; |
329 |
|
dest->systime = src->systime; |
330 |
|
} |
331 |
|
|
356 |
|
difference. */ |
357 |
|
dest->rx = transfer_diff(src->rx, dest->rx); |
358 |
|
dest->tx = transfer_diff(src->tx, dest->tx); |
359 |
+ |
dest->ipackets = transfer_diff(src->ipackets, dest->ipackets); |
360 |
+ |
dest->opackets = transfer_diff(src->opackets, dest->opackets); |
361 |
+ |
dest->ierrors = transfer_diff(src->ierrors, dest->ierrors); |
362 |
+ |
dest->oerrors = transfer_diff(src->oerrors, dest->oerrors); |
363 |
+ |
dest->collisions = transfer_diff(src->collisions, dest->collisions); |
364 |
|
dest->systime = src->systime - dest->systime; |
365 |
|
} |
366 |
|
|
409 |
|
static network_iface_stat_t *network_iface_stats; |
410 |
|
network_iface_stat_t *network_iface_stat_ptr; |
411 |
|
static int sizeof_network_iface_stats=0; |
412 |
< |
static int ifaces; |
412 |
> |
int ifaces = 0; |
413 |
|
|
414 |
|
#ifdef SOLARIS |
415 |
|
kstat_ctl_t *kc; |
416 |
|
kstat_t *ksp; |
417 |
|
kstat_named_t *knp; |
418 |
+ |
int sock; |
419 |
|
#endif |
420 |
|
#ifdef ALLBSD |
421 |
|
struct ifaddrs *net, *net_ptr; |
422 |
|
struct ifmediareq ifmed; |
423 |
< |
int s; |
423 |
> |
struct ifreq ifr; |
424 |
> |
int sock; |
425 |
|
int x; |
426 |
|
#endif |
427 |
|
#ifdef LINUX |
428 |
|
FILE *f; |
429 |
|
/* Horrible big enough, but it should be easily big enough */ |
430 |
|
char line[8096]; |
395 |
– |
void *eth_tool_cmd_buf; |
396 |
– |
int buf_size; |
431 |
|
int sock; |
432 |
|
#endif |
433 |
< |
ifaces = 0; |
433 |
> |
|
434 |
|
#ifdef ALLBSD |
435 |
|
if(getifaddrs(&net) != 0){ |
436 |
|
return NULL; |
437 |
|
} |
438 |
|
|
439 |
< |
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == NULL) return NULL; |
439 |
> |
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return NULL; |
440 |
|
|
441 |
|
for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){ |
442 |
|
if(net_ptr->ifa_addr->sa_family != AF_LINK) continue; |
446 |
|
} |
447 |
|
network_iface_stat_ptr = network_iface_stats + ifaces; |
448 |
|
|
449 |
+ |
memset(&ifr, 0, sizeof(ifr)); |
450 |
+ |
strncpy(ifr.ifr_name, net_ptr->ifa_name, sizeof(ifr.ifr_name)); |
451 |
+ |
|
452 |
+ |
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0){ |
453 |
+ |
continue; |
454 |
+ |
} |
455 |
+ |
if((ifr.ifr_flags & IFF_UP) != 0){ |
456 |
+ |
network_iface_stat_ptr->up = 1; |
457 |
+ |
}else{ |
458 |
+ |
network_iface_stat_ptr->up = 0; |
459 |
+ |
} |
460 |
+ |
|
461 |
+ |
if (network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name); |
462 |
+ |
network_iface_stat_ptr->interface_name = strdup(net_ptr->ifa_name); |
463 |
+ |
if (network_iface_stat_ptr->interface_name == NULL) return NULL; |
464 |
+ |
|
465 |
+ |
network_iface_stat_ptr->speed = 0; |
466 |
+ |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
467 |
+ |
ifaces++; |
468 |
+ |
|
469 |
|
memset(&ifmed, 0, sizeof(struct ifmediareq)); |
470 |
|
strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name)); |
471 |
< |
if(ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){ |
471 |
> |
if(ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){ |
472 |
> |
/* Not all interfaces support the media ioctls. */ |
473 |
|
continue; |
474 |
|
} |
475 |
|
|
479 |
|
continue; |
480 |
|
} |
481 |
|
|
427 |
– |
if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name); |
428 |
– |
network_iface_stat_ptr->interface_name = strdup(net_ptr->ifa_name); |
429 |
– |
if(network_iface_stat_ptr->interface_name == NULL) return NULL; |
430 |
– |
|
482 |
|
/* Only intrested in the first 4 bits) - Assuming only ETHER devices */ |
483 |
|
x = ifmed.ifm_active & 0x0f; |
484 |
|
switch(x){ |
502 |
|
case(IFM_1000_SX): |
503 |
|
case(IFM_1000_LX): |
504 |
|
case(IFM_1000_CX): |
505 |
< |
#if defined(FREEBSD) && !defined(FREEBSD5) |
506 |
< |
case(IFM_1000_TX): |
456 |
< |
case(IFM_1000_FX): |
457 |
< |
#else |
458 |
< |
case(IFM_1000_T): |
505 |
> |
#ifdef IFM_1000_TX |
506 |
> |
case(IFM_1000_TX): /* FreeBSD 4 and others? */ |
507 |
|
#endif |
508 |
+ |
#ifdef IFM_1000_FX |
509 |
+ |
case(IFM_1000_FX): /* FreeBSD 4 */ |
510 |
+ |
#endif |
511 |
+ |
#ifdef IFM_1000_T |
512 |
+ |
case(IFM_1000_T): /* FreeBSD 5 */ |
513 |
+ |
#endif |
514 |
|
network_iface_stat_ptr->speed = 1000; |
515 |
|
break; |
516 |
|
/* We don't know what it is */ |
526 |
|
}else{ |
527 |
|
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
528 |
|
} |
529 |
< |
ifaces++; |
529 |
> |
|
530 |
|
} |
531 |
|
freeifaddrs(net); |
532 |
< |
close(s); |
532 |
> |
close(sock); |
533 |
|
#endif |
534 |
|
|
535 |
|
#ifdef SOLARIS |
536 |
< |
if ((kc = kstat_open()) == NULL) { |
537 |
< |
return NULL; |
538 |
< |
} |
536 |
> |
if ((kc = kstat_open()) == NULL) { |
537 |
> |
return NULL; |
538 |
> |
} |
539 |
|
|
540 |
< |
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
541 |
< |
if (!strcmp(ksp->ks_class, "net")) { |
542 |
< |
kstat_read(kc, ksp, NULL); |
543 |
< |
if((knp=kstat_data_lookup(ksp, "ifspeed"))==NULL){ |
544 |
< |
/* Not a network interface, so skip to the next entry */ |
540 |
> |
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) { |
541 |
> |
return NULL; |
542 |
> |
} |
543 |
> |
|
544 |
> |
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
545 |
> |
if (!strcmp(ksp->ks_class, "net")) { |
546 |
> |
struct ifreq ifr; |
547 |
> |
|
548 |
> |
kstat_read(kc, ksp, NULL); |
549 |
> |
|
550 |
> |
strncpy(ifr.ifr_name, ksp->ks_name, sizeof ifr.ifr_name); |
551 |
> |
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) { |
552 |
> |
/* Not a network interface. */ |
553 |
|
continue; |
554 |
|
} |
555 |
< |
network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats); |
556 |
< |
if(network_iface_stats==NULL){ |
555 |
> |
|
556 |
> |
network_iface_stats = network_iface_stat_malloc(ifaces + 1, &sizeof_network_iface_stats, network_iface_stats); |
557 |
> |
if (network_iface_stats == NULL) { |
558 |
|
return NULL; |
559 |
|
} |
560 |
|
network_iface_stat_ptr = network_iface_stats + ifaces; |
561 |
< |
network_iface_stat_ptr->speed = knp->value.ui64 / (1000*1000); |
561 |
> |
ifaces++; |
562 |
|
|
563 |
< |
if((knp=kstat_data_lookup(ksp, "link_duplex"))==NULL){ |
564 |
< |
/* Not a network interface, so skip to the next entry */ |
565 |
< |
continue; |
563 |
> |
if (network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name); |
564 |
> |
network_iface_stat_ptr->interface_name = strdup(ksp->ks_name); |
565 |
> |
if (network_iface_stat_ptr->interface_name == NULL) return NULL; |
566 |
> |
|
567 |
> |
if ((ifr.ifr_flags & IFF_UP) != 0) { |
568 |
> |
network_iface_stat_ptr->up = 1; |
569 |
> |
} else { |
570 |
> |
network_iface_stat_ptr->up = 1; |
571 |
|
} |
572 |
|
|
573 |
< |
if(knp->value.ui64 == 0){ |
574 |
< |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
575 |
< |
}else{ |
576 |
< |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
573 |
> |
if ((knp = kstat_data_lookup(ksp, "ifspeed")) != NULL) { |
574 |
> |
network_iface_stat_ptr->speed = knp->value.ui64 / (1000 * 1000); |
575 |
> |
} else { |
576 |
> |
network_iface_stat_ptr->speed = 0; |
577 |
|
} |
578 |
|
|
579 |
< |
if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name); |
580 |
< |
network_iface_stat_ptr->interface_name = strdup(ksp->ks_name); |
581 |
< |
if(network_iface_stat_ptr->interface_name == NULL) return NULL; |
582 |
< |
ifaces++; |
579 |
> |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
580 |
> |
if ((knp = kstat_data_lookup(ksp, "link_duplex")) != NULL) { |
581 |
> |
switch (knp->value.ui32) { |
582 |
> |
case 1: |
583 |
> |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
584 |
> |
break; |
585 |
> |
case 2: |
586 |
> |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
587 |
> |
break; |
588 |
> |
} |
589 |
> |
} |
590 |
|
} |
591 |
|
} |
592 |
+ |
|
593 |
+ |
close(sock); |
594 |
|
kstat_close(kc); |
518 |
– |
|
595 |
|
#endif |
596 |
|
#ifdef LINUX |
521 |
– |
|
597 |
|
f = fopen("/proc/net/dev", "r"); |
598 |
|
if(f == NULL){ |
599 |
|
return NULL; |
604 |
|
return NULL; |
605 |
|
} |
606 |
|
|
532 |
– |
buf_size = sizeof(struct ethtool_cmd); |
533 |
– |
eth_tool_cmd_buf = malloc(buf_size); |
534 |
– |
if(eth_tool_cmd_buf == NULL) return NULL; |
535 |
– |
|
607 |
|
/* Ignore first 2 lines.. Just headings */ |
608 |
|
if((fgets(line, sizeof(line), f)) == NULL) return NULL; |
609 |
|
if((fgets(line, sizeof(line), f)) == NULL) return NULL; |
611 |
|
while((fgets(line, sizeof(line), f)) != NULL){ |
612 |
|
char *name, *ptr; |
613 |
|
struct ifreq ifr; |
614 |
< |
struct ethtool_cmd *ethcmd; |
614 |
> |
struct ethtool_cmd ethcmd; |
615 |
|
int err; |
616 |
|
|
617 |
|
/* Get the interface name */ |
623 |
|
name++; |
624 |
|
} |
625 |
|
|
626 |
< |
memset(&ifr, 0, sizeof(ifr)); |
627 |
< |
memset(eth_tool_cmd_buf, 0, buf_size); |
557 |
< |
ifr.ifr_data = (caddr_t) eth_tool_cmd_buf; |
558 |
< |
strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); |
626 |
> |
memset(&ifr, 0, sizeof ifr); |
627 |
> |
strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); |
628 |
|
|
629 |
< |
ethcmd = (struct ethtool_cmd *) ifr.ifr_data; |
630 |
< |
ethcmd->cmd = ETHTOOL_GSET; |
629 |
> |
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) { |
630 |
> |
continue; |
631 |
> |
} |
632 |
|
|
563 |
– |
err = ioctl(sock, SIOCETHTOOL, &ifr); |
564 |
– |
if(err < 0){ |
565 |
– |
/* This could fail if the interface doesn't support the command. Carry |
566 |
– |
* on to the next :) |
567 |
– |
*/ |
568 |
– |
continue; |
569 |
– |
} |
570 |
– |
|
633 |
|
/* We have a good interface to add */ |
634 |
|
network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats); |
635 |
|
if(network_iface_stats==NULL){ |
637 |
|
} |
638 |
|
network_iface_stat_ptr = network_iface_stats + ifaces; |
639 |
|
network_iface_stat_ptr->interface_name = strdup(name); |
640 |
< |
network_iface_stat_ptr->speed = ethcmd->speed; |
641 |
< |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
642 |
< |
if(ethcmd->duplex == 0x00){ |
643 |
< |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
640 |
> |
if ((ifr.ifr_flags & IFF_UP) != 0) { |
641 |
> |
network_iface_stat_ptr->up = 1; |
642 |
> |
} else { |
643 |
> |
network_iface_stat_ptr->up = 0; |
644 |
|
} |
645 |
< |
if(ethcmd->duplex == 0x01){ |
646 |
< |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
645 |
> |
|
646 |
> |
memset(ðcmd, 0, sizeof ethcmd); |
647 |
> |
ethcmd.cmd = ETHTOOL_GSET; |
648 |
> |
ifr.ifr_data = (caddr_t) ðcmd; |
649 |
> |
|
650 |
> |
err = ioctl(sock, SIOCETHTOOL, &ifr); |
651 |
> |
if (err == 0) { |
652 |
> |
network_iface_stat_ptr->speed = ethcmd.speed; |
653 |
> |
|
654 |
> |
switch (ethcmd.duplex) { |
655 |
> |
case 0x00: |
656 |
> |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
657 |
> |
break; |
658 |
> |
case 0x01: |
659 |
> |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
660 |
> |
break; |
661 |
> |
default: |
662 |
> |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
663 |
> |
} |
664 |
> |
} else { |
665 |
> |
/* Not all interfaces support the ethtool ioctl. */ |
666 |
> |
network_iface_stat_ptr->speed = 0; |
667 |
> |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
668 |
|
} |
669 |
+ |
|
670 |
|
ifaces++; |
671 |
|
} |
672 |
|
close(sock); |
673 |
< |
free(eth_tool_cmd_buf); |
673 |
> |
fclose(f); |
674 |
|
#endif |
675 |
|
*entries = ifaces; |
676 |
|
return network_iface_stats; |