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> |
61 |
|
#endif |
62 |
|
#ifdef ALLBSD |
63 |
|
#include <sys/types.h> |
151 |
|
if(network_stat_ptr->interface_name==NULL) return NULL; |
152 |
|
net_data=(struct if_data *)net_ptr->ifa_data; |
153 |
|
network_stat_ptr->rx=net_data->ifi_ibytes; |
154 |
< |
network_stat_ptr->tx=net_data->ifi_obytes; |
154 |
> |
network_stat_ptr->tx=net_data->ifi_obytes; |
155 |
> |
network_stat_ptr->ipackets=net_data->ifi_ipackets; |
156 |
> |
network_stat_ptr->opackets=net_data->ifi_opackets; |
157 |
> |
network_stat_ptr->ierrors=net_data->ifi_ierrors; |
158 |
> |
network_stat_ptr->oerrors=net_data->ifi_oerrors; |
159 |
> |
network_stat_ptr->collisions=net_data->ifi_collisions; |
160 |
|
network_stat_ptr->systime=time(NULL); |
161 |
|
interfaces++; |
162 |
|
} |
185 |
|
#endif |
186 |
|
|
187 |
|
if((knp=kstat_data_lookup(ksp, RLOOKUP))==NULL){ |
188 |
< |
/* Not a network interface, so skip to the next entry */ |
188 |
> |
/* This is a network interface, but it doesn't |
189 |
> |
* have the rbytes/obytes values; for instance, |
190 |
> |
* the loopback devices have this behaviour |
191 |
> |
* (although they do track packets in/out). */ |
192 |
|
continue; |
193 |
|
} |
194 |
|
|
200 |
|
network_stat_ptr->rx=knp->VALTYPE; |
201 |
|
|
202 |
|
if((knp=kstat_data_lookup(ksp, WLOOKUP))==NULL){ |
187 |
– |
/* Not a network interface, so skip to the next entry */ |
203 |
|
continue; |
204 |
|
} |
205 |
|
network_stat_ptr->tx=knp->VALTYPE; |
267 |
|
} |
268 |
|
|
269 |
|
long long transfer_diff(long long new, long long old){ |
270 |
< |
#if defined(SOL7) || defined(LINUX) || defined(FREEBSD) |
270 |
> |
#if defined(SOL7) || defined(LINUX) || defined(FREEBSD) || defined(DFBSD) |
271 |
|
#define MAXVAL 4294967296LL |
272 |
|
#else |
273 |
|
#define MAXVAL 18446744073709551616LL |
354 |
|
for(net_stats+=start; start<end; start++){ |
355 |
|
net_stats->interface_name=NULL; |
356 |
|
net_stats->speed=0; |
357 |
< |
net_stats->dup=UNKNOWN_DUPEX; |
357 |
> |
net_stats->dup=UNKNOWN_DUPLEX; |
358 |
|
net_stats++; |
359 |
|
} |
360 |
|
} |
389 |
|
static network_iface_stat_t *network_iface_stats; |
390 |
|
network_iface_stat_t *network_iface_stat_ptr; |
391 |
|
static int sizeof_network_iface_stats=0; |
392 |
< |
static int ifaces; |
392 |
> |
int ifaces = 0; |
393 |
|
|
394 |
|
#ifdef SOLARIS |
395 |
|
kstat_ctl_t *kc; |
396 |
|
kstat_t *ksp; |
397 |
|
kstat_named_t *knp; |
398 |
+ |
int sock; |
399 |
|
#endif |
400 |
|
#ifdef ALLBSD |
401 |
|
struct ifaddrs *net, *net_ptr; |
402 |
|
struct ifmediareq ifmed; |
403 |
< |
int s; |
403 |
> |
struct ifreq ifr; |
404 |
> |
int sock; |
405 |
|
int x; |
406 |
|
#endif |
407 |
|
#ifdef LINUX |
408 |
|
FILE *f; |
409 |
|
/* Horrible big enough, but it should be easily big enough */ |
410 |
|
char line[8096]; |
394 |
– |
void *eth_tool_cmd_buf; |
395 |
– |
int buf_size; |
411 |
|
int sock; |
412 |
|
#endif |
413 |
< |
ifaces = 0; |
413 |
> |
|
414 |
|
#ifdef ALLBSD |
415 |
|
if(getifaddrs(&net) != 0){ |
416 |
|
return NULL; |
417 |
|
} |
418 |
|
|
419 |
< |
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == NULL) return NULL; |
419 |
> |
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return NULL; |
420 |
|
|
421 |
|
for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){ |
422 |
|
if(net_ptr->ifa_addr->sa_family != AF_LINK) continue; |
426 |
|
} |
427 |
|
network_iface_stat_ptr = network_iface_stats + ifaces; |
428 |
|
|
429 |
+ |
memset(&ifr, 0, sizeof(ifr)); |
430 |
+ |
strncpy(ifr.ifr_name, net_ptr->ifa_name, sizeof(ifr.ifr_name)); |
431 |
+ |
|
432 |
+ |
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0){ |
433 |
+ |
continue; |
434 |
+ |
} |
435 |
+ |
if((ifr.ifr_flags & IFF_UP) != 0){ |
436 |
+ |
network_iface_stat_ptr->up = 1; |
437 |
+ |
}else{ |
438 |
+ |
network_iface_stat_ptr->up = 0; |
439 |
+ |
} |
440 |
+ |
|
441 |
+ |
if (network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name); |
442 |
+ |
network_iface_stat_ptr->interface_name = strdup(net_ptr->ifa_name); |
443 |
+ |
if (network_iface_stat_ptr->interface_name == NULL) return NULL; |
444 |
+ |
|
445 |
+ |
network_iface_stat_ptr->speed = 0; |
446 |
+ |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
447 |
+ |
ifaces++; |
448 |
+ |
|
449 |
|
memset(&ifmed, 0, sizeof(struct ifmediareq)); |
450 |
|
strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name)); |
451 |
< |
if(ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){ |
451 |
> |
if(ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){ |
452 |
> |
/* Not all interfaces support the media ioctls. */ |
453 |
|
continue; |
454 |
|
} |
455 |
|
|
459 |
|
continue; |
460 |
|
} |
461 |
|
|
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 |
– |
|
462 |
|
/* Only intrested in the first 4 bits) - Assuming only ETHER devices */ |
463 |
|
x = ifmed.ifm_active & 0x0f; |
464 |
|
switch(x){ |
482 |
|
case(IFM_1000_SX): |
483 |
|
case(IFM_1000_LX): |
484 |
|
case(IFM_1000_CX): |
485 |
< |
#ifdef FREEBSD5 |
486 |
< |
case(IFM_1000_T): |
455 |
< |
#else |
456 |
< |
case(IFM_1000_TX): |
457 |
< |
case(IFM_1000_FX): |
485 |
> |
#ifdef IFM_1000_TX |
486 |
> |
case(IFM_1000_TX): /* FreeBSD 4 and others? */ |
487 |
|
#endif |
488 |
+ |
#ifdef IFM_1000_FX |
489 |
+ |
case(IFM_1000_FX): /* FreeBSD 4 */ |
490 |
+ |
#endif |
491 |
+ |
#ifdef IFM_1000_T |
492 |
+ |
case(IFM_1000_T): /* FreeBSD 5 */ |
493 |
+ |
#endif |
494 |
|
network_iface_stat_ptr->speed = 1000; |
495 |
|
break; |
496 |
|
/* We don't know what it is */ |
504 |
|
}else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){ |
505 |
|
network_iface_stat_ptr->dup = HALF_DUPLEX; |
506 |
|
}else{ |
507 |
< |
network_iface_stat_ptr->dup = UNKNOWN_DUPEX; |
507 |
> |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
508 |
|
} |
509 |
< |
ifaces++; |
509 |
> |
|
510 |
|
} |
511 |
|
freeifaddrs(net); |
512 |
+ |
close(sock); |
513 |
|
#endif |
514 |
|
|
515 |
|
#ifdef SOLARIS |
516 |
< |
if ((kc = kstat_open()) == NULL) { |
517 |
< |
return NULL; |
518 |
< |
} |
516 |
> |
if ((kc = kstat_open()) == NULL) { |
517 |
> |
return NULL; |
518 |
> |
} |
519 |
|
|
520 |
< |
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
521 |
< |
if (!strcmp(ksp->ks_class, "net")) { |
522 |
< |
kstat_read(kc, ksp, NULL); |
523 |
< |
if((knp=kstat_data_lookup(ksp, "ifspeed"))==NULL){ |
524 |
< |
/* Not a network interface, so skip to the next entry */ |
520 |
> |
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) { |
521 |
> |
return NULL; |
522 |
> |
} |
523 |
> |
|
524 |
> |
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
525 |
> |
if (!strcmp(ksp->ks_class, "net")) { |
526 |
> |
struct ifreq ifr; |
527 |
> |
|
528 |
> |
kstat_read(kc, ksp, NULL); |
529 |
> |
|
530 |
> |
strncpy(ifr.ifr_name, ksp->ks_name, sizeof ifr.ifr_name); |
531 |
> |
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) { |
532 |
> |
/* Not a network interface. */ |
533 |
|
continue; |
534 |
|
} |
535 |
< |
network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats); |
536 |
< |
if(network_iface_stats==NULL){ |
535 |
> |
|
536 |
> |
network_iface_stats = network_iface_stat_malloc(ifaces + 1, &sizeof_network_iface_stats, network_iface_stats); |
537 |
> |
if (network_iface_stats == NULL) { |
538 |
|
return NULL; |
539 |
|
} |
540 |
|
network_iface_stat_ptr = network_iface_stats + ifaces; |
541 |
< |
network_iface_stat_ptr->speed = knp->value.ui64 / (1000*1000); |
541 |
> |
ifaces++; |
542 |
|
|
543 |
< |
if((knp=kstat_data_lookup(ksp, "link_duplex"))==NULL){ |
544 |
< |
/* Not a network interface, so skip to the next entry */ |
545 |
< |
continue; |
543 |
> |
if (network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name); |
544 |
> |
network_iface_stat_ptr->interface_name = strdup(ksp->ks_name); |
545 |
> |
if (network_iface_stat_ptr->interface_name == NULL) return NULL; |
546 |
> |
|
547 |
> |
if ((ifr.ifr_flags & IFF_UP) != 0) { |
548 |
> |
network_iface_stat_ptr->up = 1; |
549 |
> |
} else { |
550 |
> |
network_iface_stat_ptr->up = 1; |
551 |
|
} |
552 |
|
|
553 |
< |
if(knp->value.ui64 == 0){ |
554 |
< |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
555 |
< |
}else{ |
556 |
< |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
553 |
> |
if ((knp = kstat_data_lookup(ksp, "ifspeed")) != NULL) { |
554 |
> |
network_iface_stat_ptr->speed = knp->value.ui64 / (1000 * 1000); |
555 |
> |
} else { |
556 |
> |
network_iface_stat_ptr->speed = 0; |
557 |
|
} |
558 |
|
|
559 |
< |
if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name); |
560 |
< |
network_iface_stat_ptr->interface_name = strdup(ksp->ks_name); |
561 |
< |
if(network_iface_stat_ptr->interface_name == NULL) return NULL; |
562 |
< |
ifaces++; |
559 |
> |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
560 |
> |
if ((knp = kstat_data_lookup(ksp, "link_duplex")) != NULL) { |
561 |
> |
switch (knp->value.ui32) { |
562 |
> |
case 1: |
563 |
> |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
564 |
> |
break; |
565 |
> |
case 2: |
566 |
> |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
567 |
> |
break; |
568 |
> |
} |
569 |
> |
} |
570 |
|
} |
571 |
|
} |
572 |
+ |
|
573 |
+ |
close(sock); |
574 |
|
kstat_close(kc); |
516 |
– |
|
575 |
|
#endif |
576 |
|
#ifdef LINUX |
519 |
– |
|
577 |
|
f = fopen("/proc/net/dev", "r"); |
578 |
|
if(f == NULL){ |
579 |
|
return NULL; |
584 |
|
return NULL; |
585 |
|
} |
586 |
|
|
530 |
– |
buf_size = sizeof(struct ethtool_cmd); |
531 |
– |
eth_tool_cmd_buf = malloc(buf_size); |
532 |
– |
if(eth_tool_cmd_buf == NULL) return NULL; |
533 |
– |
|
587 |
|
/* Ignore first 2 lines.. Just headings */ |
588 |
|
if((fgets(line, sizeof(line), f)) == NULL) return NULL; |
589 |
|
if((fgets(line, sizeof(line), f)) == NULL) return NULL; |
591 |
|
while((fgets(line, sizeof(line), f)) != NULL){ |
592 |
|
char *name, *ptr; |
593 |
|
struct ifreq ifr; |
594 |
< |
struct ethtool_cmd *ethcmd; |
594 |
> |
struct ethtool_cmd ethcmd; |
595 |
|
int err; |
596 |
|
|
597 |
|
/* Get the interface name */ |
603 |
|
name++; |
604 |
|
} |
605 |
|
|
606 |
< |
memset(&ifr, 0, sizeof(ifr)); |
607 |
< |
memset(eth_tool_cmd_buf, 0, buf_size); |
555 |
< |
ifr.ifr_data = (caddr_t) eth_tool_cmd_buf; |
556 |
< |
strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); |
606 |
> |
memset(&ifr, 0, sizeof ifr); |
607 |
> |
strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); |
608 |
|
|
609 |
< |
ethcmd = (struct ethtool_cmd *) ifr.ifr_data; |
610 |
< |
ethcmd->cmd = ETHTOOL_GSET; |
609 |
> |
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) { |
610 |
> |
continue; |
611 |
> |
} |
612 |
|
|
561 |
– |
err = ioctl(sock, SIOCETHTOOL, &ifr); |
562 |
– |
if(err < 0){ |
563 |
– |
/* This could fail if the interface doesn't support the command. Carry |
564 |
– |
* on to the next :) |
565 |
– |
*/ |
566 |
– |
continue; |
567 |
– |
} |
568 |
– |
|
613 |
|
/* We have a good interface to add */ |
614 |
|
network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats); |
615 |
|
if(network_iface_stats==NULL){ |
617 |
|
} |
618 |
|
network_iface_stat_ptr = network_iface_stats + ifaces; |
619 |
|
network_iface_stat_ptr->interface_name = strdup(name); |
620 |
< |
network_iface_stat_ptr->speed = ethcmd->speed; |
621 |
< |
network_iface_stat_ptr->dup = UNKNOWN_DUPEX; |
622 |
< |
if(ethcmd->duplex == 0x00){ |
623 |
< |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
620 |
> |
if ((ifr.ifr_flags & IFF_UP) != 0) { |
621 |
> |
network_iface_stat_ptr->up = 1; |
622 |
> |
} else { |
623 |
> |
network_iface_stat_ptr->up = 0; |
624 |
|
} |
625 |
< |
if(ethcmd->duplex == 0x01){ |
626 |
< |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
625 |
> |
|
626 |
> |
memset(ðcmd, 0, sizeof ethcmd); |
627 |
> |
ethcmd.cmd = ETHTOOL_GSET; |
628 |
> |
ifr.ifr_data = (caddr_t) ðcmd; |
629 |
> |
|
630 |
> |
err = ioctl(sock, SIOCETHTOOL, &ifr); |
631 |
> |
if (err == 0) { |
632 |
> |
network_iface_stat_ptr->speed = ethcmd.speed; |
633 |
> |
|
634 |
> |
switch (ethcmd.duplex) { |
635 |
> |
case 0x00: |
636 |
> |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
637 |
> |
break; |
638 |
> |
case 0x01: |
639 |
> |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
640 |
> |
break; |
641 |
> |
default: |
642 |
> |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
643 |
> |
} |
644 |
> |
} else { |
645 |
> |
/* Not all interfaces support the ethtool ioctl. */ |
646 |
> |
network_iface_stat_ptr->speed = 0; |
647 |
> |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
648 |
|
} |
649 |
+ |
|
650 |
|
ifaces++; |
651 |
|
} |
652 |
< |
|
653 |
< |
free(eth_tool_cmd_buf); |
652 |
> |
close(sock); |
653 |
> |
fclose(f); |
654 |
|
#endif |
655 |
|
*entries = ifaces; |
656 |
|
return network_iface_stats; |