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> |
180 |
|
#endif |
181 |
|
|
182 |
|
if((knp=kstat_data_lookup(ksp, RLOOKUP))==NULL){ |
183 |
< |
/* Not a network interface, so skip to the next entry */ |
183 |
> |
/* This is a network interface, but it doesn't |
184 |
> |
* have the rbytes/obytes values; for instance, |
185 |
> |
* the loopback devices have this behaviour |
186 |
> |
* (although they do track packets in/out). */ |
187 |
|
continue; |
188 |
|
} |
189 |
|
|
195 |
|
network_stat_ptr->rx=knp->VALTYPE; |
196 |
|
|
197 |
|
if((knp=kstat_data_lookup(ksp, WLOOKUP))==NULL){ |
188 |
– |
/* Not a network interface, so skip to the next entry */ |
198 |
|
continue; |
199 |
|
} |
200 |
|
network_stat_ptr->tx=knp->VALTYPE; |
262 |
|
} |
263 |
|
|
264 |
|
long long transfer_diff(long long new, long long old){ |
265 |
< |
#if defined(SOL7) || defined(LINUX) || defined(FREEBSD) |
265 |
> |
#if defined(SOL7) || defined(LINUX) || defined(FREEBSD) || defined(DFBSD) |
266 |
|
#define MAXVAL 4294967296LL |
267 |
|
#else |
268 |
|
#define MAXVAL 18446744073709551616LL |
384 |
|
static network_iface_stat_t *network_iface_stats; |
385 |
|
network_iface_stat_t *network_iface_stat_ptr; |
386 |
|
static int sizeof_network_iface_stats=0; |
387 |
< |
static int ifaces; |
387 |
> |
int ifaces = 0; |
388 |
|
|
389 |
|
#ifdef SOLARIS |
390 |
|
kstat_ctl_t *kc; |
391 |
|
kstat_t *ksp; |
392 |
|
kstat_named_t *knp; |
393 |
+ |
int sock; |
394 |
|
#endif |
395 |
|
#ifdef ALLBSD |
396 |
|
struct ifaddrs *net, *net_ptr; |
405 |
|
char line[8096]; |
406 |
|
int sock; |
407 |
|
#endif |
408 |
< |
ifaces = 0; |
408 |
> |
|
409 |
|
#ifdef ALLBSD |
410 |
|
if(getifaddrs(&net) != 0){ |
411 |
|
return NULL; |
421 |
|
} |
422 |
|
network_iface_stat_ptr = network_iface_stats + ifaces; |
423 |
|
|
424 |
+ |
memset(&ifr, 0, sizeof(ifr)); |
425 |
+ |
strncpy(ifr.ifr_name, net_ptr->ifa_name, sizeof(ifr.ifr_name)); |
426 |
+ |
|
427 |
+ |
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0){ |
428 |
+ |
continue; |
429 |
+ |
} |
430 |
+ |
if((ifr.ifr_flags & IFF_UP) != 0){ |
431 |
+ |
network_iface_stat_ptr->up = 1; |
432 |
+ |
}else{ |
433 |
+ |
network_iface_stat_ptr->up = 0; |
434 |
+ |
} |
435 |
+ |
|
436 |
+ |
if (network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name); |
437 |
+ |
network_iface_stat_ptr->interface_name = strdup(net_ptr->ifa_name); |
438 |
+ |
if (network_iface_stat_ptr->interface_name == NULL) return NULL; |
439 |
+ |
|
440 |
+ |
network_iface_stat_ptr->speed = 0; |
441 |
+ |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
442 |
+ |
ifaces++; |
443 |
+ |
|
444 |
|
memset(&ifmed, 0, sizeof(struct ifmediareq)); |
445 |
|
strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name)); |
446 |
|
if(ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){ |
447 |
+ |
/* Not all interfaces support the media ioctls. */ |
448 |
|
continue; |
449 |
|
} |
450 |
|
|
454 |
|
continue; |
455 |
|
} |
456 |
|
|
426 |
– |
if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name); |
427 |
– |
network_iface_stat_ptr->interface_name = strdup(net_ptr->ifa_name); |
428 |
– |
if(network_iface_stat_ptr->interface_name == NULL) return NULL; |
429 |
– |
|
457 |
|
/* Only intrested in the first 4 bits) - Assuming only ETHER devices */ |
458 |
|
x = ifmed.ifm_active & 0x0f; |
459 |
|
switch(x){ |
477 |
|
case(IFM_1000_SX): |
478 |
|
case(IFM_1000_LX): |
479 |
|
case(IFM_1000_CX): |
480 |
< |
#if defined(FREEBSD) && !defined(FREEBSD5) |
481 |
< |
case(IFM_1000_TX): |
455 |
< |
case(IFM_1000_FX): |
456 |
< |
#else |
457 |
< |
case(IFM_1000_T): |
480 |
> |
#ifdef IFM_1000_TX |
481 |
> |
case(IFM_1000_TX): /* FreeBSD 4 and others? */ |
482 |
|
#endif |
483 |
+ |
#ifdef IFM_1000_FX |
484 |
+ |
case(IFM_1000_FX): /* FreeBSD 4 */ |
485 |
+ |
#endif |
486 |
+ |
#ifdef IFM_1000_T |
487 |
+ |
case(IFM_1000_T): /* FreeBSD 5 */ |
488 |
+ |
#endif |
489 |
|
network_iface_stat_ptr->speed = 1000; |
490 |
|
break; |
491 |
|
/* We don't know what it is */ |
502 |
|
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
503 |
|
} |
504 |
|
|
475 |
– |
memset(&ifr, 0, sizeof(ifr)); |
476 |
– |
strncpy(ifr.ifr_name, net_ptr->ifa_name, sizeof(ifr.ifr_name)); |
477 |
– |
|
478 |
– |
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0){ |
479 |
– |
continue; |
480 |
– |
} |
481 |
– |
if((ifr.ifr_flags & IFF_UP) != 0){ |
482 |
– |
network_iface_stat_ptr->up = 1; |
483 |
– |
}else{ |
484 |
– |
network_iface_stat_ptr->up = 0; |
485 |
– |
} |
486 |
– |
|
487 |
– |
ifaces++; |
505 |
|
} |
506 |
|
freeifaddrs(net); |
507 |
|
close(sock); |
508 |
|
#endif |
509 |
|
|
510 |
|
#ifdef SOLARIS |
511 |
< |
if ((kc = kstat_open()) == NULL) { |
512 |
< |
return NULL; |
513 |
< |
} |
511 |
> |
if ((kc = kstat_open()) == NULL) { |
512 |
> |
return NULL; |
513 |
> |
} |
514 |
|
|
515 |
< |
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
516 |
< |
if (!strcmp(ksp->ks_class, "net")) { |
517 |
< |
kstat_read(kc, ksp, NULL); |
518 |
< |
if((knp=kstat_data_lookup(ksp, "ifspeed"))==NULL){ |
519 |
< |
/* Not a network interface, so skip to the next entry */ |
515 |
> |
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) { |
516 |
> |
return NULL; |
517 |
> |
} |
518 |
> |
|
519 |
> |
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
520 |
> |
if (!strcmp(ksp->ks_class, "net")) { |
521 |
> |
struct ifreq ifr; |
522 |
> |
|
523 |
> |
kstat_read(kc, ksp, NULL); |
524 |
> |
|
525 |
> |
strncpy(ifr.ifr_name, ksp->ks_name, sizeof ifr.ifr_name); |
526 |
> |
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) { |
527 |
> |
/* Not a network interface. */ |
528 |
|
continue; |
529 |
|
} |
530 |
< |
network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats); |
531 |
< |
if(network_iface_stats==NULL){ |
530 |
> |
|
531 |
> |
network_iface_stats = network_iface_stat_malloc(ifaces + 1, &sizeof_network_iface_stats, network_iface_stats); |
532 |
> |
if (network_iface_stats == NULL) { |
533 |
|
return NULL; |
534 |
|
} |
535 |
|
network_iface_stat_ptr = network_iface_stats + ifaces; |
536 |
< |
network_iface_stat_ptr->speed = knp->value.ui64 / (1000*1000); |
536 |
> |
ifaces++; |
537 |
|
|
538 |
< |
if((knp=kstat_data_lookup(ksp, "link_up"))==NULL){ |
539 |
< |
/* Not a network interface, so skip to the next entry */ |
540 |
< |
continue; |
515 |
< |
} |
516 |
< |
/* Solaris has 1 for up, and 0 for not. As we do too */ |
517 |
< |
network_iface_stat_ptr->up = value.ui32; |
538 |
> |
if (network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name); |
539 |
> |
network_iface_stat_ptr->interface_name = strdup(ksp->ks_name); |
540 |
> |
if (network_iface_stat_ptr->interface_name == NULL) return NULL; |
541 |
|
|
542 |
< |
if((knp=kstat_data_lookup(ksp, "link_duplex"))==NULL){ |
543 |
< |
/* Not a network interface, so skip to the next entry */ |
544 |
< |
continue; |
542 |
> |
if ((ifr.ifr_flags & IFF_UP) != 0) { |
543 |
> |
network_iface_stat_ptr->up = 1; |
544 |
> |
} else { |
545 |
> |
network_iface_stat_ptr->up = 1; |
546 |
|
} |
547 |
|
|
548 |
< |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
549 |
< |
if(knp->value.ui32 == 2){ |
550 |
< |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
548 |
> |
if ((knp = kstat_data_lookup(ksp, "ifspeed")) != NULL) { |
549 |
> |
network_iface_stat_ptr->speed = knp->value.ui64 / (1000 * 1000); |
550 |
> |
} else { |
551 |
> |
network_iface_stat_ptr->speed = 0; |
552 |
|
} |
528 |
– |
if(knp->value.ui32 == 1){ |
529 |
– |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
530 |
– |
} |
553 |
|
|
554 |
< |
if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name); |
555 |
< |
network_iface_stat_ptr->interface_name = strdup(ksp->ks_name); |
556 |
< |
if(network_iface_stat_ptr->interface_name == NULL) return NULL; |
557 |
< |
ifaces++; |
554 |
> |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
555 |
> |
if ((knp = kstat_data_lookup(ksp, "link_duplex")) != NULL) { |
556 |
> |
switch (knp->value.ui32) { |
557 |
> |
case 1: |
558 |
> |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
559 |
> |
break; |
560 |
> |
case 2: |
561 |
> |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
562 |
> |
break; |
563 |
> |
} |
564 |
> |
} |
565 |
|
} |
566 |
|
} |
567 |
+ |
|
568 |
+ |
close(sock); |
569 |
|
kstat_close(kc); |
539 |
– |
|
570 |
|
#endif |
571 |
|
#ifdef LINUX |
572 |
|
f = fopen("/proc/net/dev", "r"); |
638 |
|
} |
639 |
|
} else { |
640 |
|
/* Not all interfaces support the ethtool ioctl. */ |
641 |
< |
network_iface_stat_ptr->speed = -1; |
641 |
> |
network_iface_stat_ptr->speed = 0; |
642 |
|
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
643 |
|
} |
644 |
|
|