48 |
|
typedef __uint8_t u8; |
49 |
|
typedef __uint16_t u16; |
50 |
|
typedef __uint32_t u32; |
51 |
+ |
typedef __uint64_t u64; |
52 |
|
#include <linux/ethtool.h> |
53 |
|
#include <linux/sockios.h> |
54 |
|
#include <unistd.h> |
394 |
|
FILE *f; |
395 |
|
/* Horrible big enough, but it should be easily big enough */ |
396 |
|
char line[8096]; |
396 |
– |
void *eth_tool_cmd_buf; |
397 |
– |
int buf_size; |
397 |
|
int sock; |
398 |
|
#endif |
399 |
|
ifaces = 0; |
412 |
|
} |
413 |
|
network_iface_stat_ptr = network_iface_stats + ifaces; |
414 |
|
|
415 |
+ |
memset(&ifr, 0, sizeof(ifr)); |
416 |
+ |
strncpy(ifr.ifr_name, net_ptr->ifa_name, sizeof(ifr.ifr_name)); |
417 |
+ |
|
418 |
+ |
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0){ |
419 |
+ |
continue; |
420 |
+ |
} |
421 |
+ |
if((ifr.ifr_flags & IFF_UP) != 0){ |
422 |
+ |
network_iface_stat_ptr->up = 1; |
423 |
+ |
}else{ |
424 |
+ |
network_iface_stat_ptr->up = 0; |
425 |
+ |
} |
426 |
+ |
|
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 |
+ |
|
431 |
+ |
network_iface_stat_ptr->speed = 0; |
432 |
+ |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
433 |
+ |
ifaces++; |
434 |
+ |
|
435 |
|
memset(&ifmed, 0, sizeof(struct ifmediareq)); |
436 |
|
strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name)); |
437 |
|
if(ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){ |
438 |
+ |
/* Not all interfaces support the media ioctls. */ |
439 |
|
continue; |
440 |
|
} |
441 |
|
|
445 |
|
continue; |
446 |
|
} |
447 |
|
|
428 |
– |
if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name); |
429 |
– |
network_iface_stat_ptr->interface_name = strdup(net_ptr->ifa_name); |
430 |
– |
if(network_iface_stat_ptr->interface_name == NULL) return NULL; |
431 |
– |
|
448 |
|
/* Only intrested in the first 4 bits) - Assuming only ETHER devices */ |
449 |
|
x = ifmed.ifm_active & 0x0f; |
450 |
|
switch(x){ |
490 |
|
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
491 |
|
} |
492 |
|
|
477 |
– |
memset(&ifr, 0, sizeof(ifr)); |
478 |
– |
strncpy(ifr.ifr_name, net_ptr->ifa_name, sizeof(ifr.ifr_name)); |
479 |
– |
|
480 |
– |
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0){ |
481 |
– |
continue; |
482 |
– |
} |
483 |
– |
if((ifr.ifr_flags & IFF_UP) != 0){ |
484 |
– |
network_iface_stat_ptr->up = 1; |
485 |
– |
}else{ |
486 |
– |
network_iface_stat_ptr->up = 0; |
487 |
– |
} |
488 |
– |
|
489 |
– |
ifaces++; |
493 |
|
} |
494 |
|
freeifaddrs(net); |
495 |
|
close(sock); |
519 |
|
continue; |
520 |
|
} |
521 |
|
/* Solaris has 1 for up, and 0 for not. As we do too */ |
522 |
< |
network_iface_stat_ptr->up = value.ui32; |
522 |
> |
network_iface_stat_ptr->up = knp->value.ui32; |
523 |
|
|
524 |
|
if((knp=kstat_data_lookup(ksp, "link_duplex"))==NULL){ |
525 |
|
/* Not a network interface, so skip to the next entry */ |
544 |
|
|
545 |
|
#endif |
546 |
|
#ifdef LINUX |
544 |
– |
|
547 |
|
f = fopen("/proc/net/dev", "r"); |
548 |
|
if(f == NULL){ |
549 |
|
return NULL; |
554 |
|
return NULL; |
555 |
|
} |
556 |
|
|
555 |
– |
buf_size = sizeof(struct ethtool_cmd); |
556 |
– |
eth_tool_cmd_buf = malloc(buf_size); |
557 |
– |
if(eth_tool_cmd_buf == NULL) return NULL; |
558 |
– |
|
557 |
|
/* Ignore first 2 lines.. Just headings */ |
558 |
|
if((fgets(line, sizeof(line), f)) == NULL) return NULL; |
559 |
|
if((fgets(line, sizeof(line), f)) == NULL) return NULL; |
561 |
|
while((fgets(line, sizeof(line), f)) != NULL){ |
562 |
|
char *name, *ptr; |
563 |
|
struct ifreq ifr; |
564 |
< |
struct ethtool_cmd *ethcmd; |
564 |
> |
struct ethtool_cmd ethcmd; |
565 |
|
int err; |
566 |
|
|
567 |
|
/* Get the interface name */ |
573 |
|
name++; |
574 |
|
} |
575 |
|
|
576 |
< |
memset(&ifr, 0, sizeof(ifr)); |
577 |
< |
memset(eth_tool_cmd_buf, 0, buf_size); |
580 |
< |
ifr.ifr_data = (caddr_t) eth_tool_cmd_buf; |
581 |
< |
strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); |
576 |
> |
memset(&ifr, 0, sizeof ifr); |
577 |
> |
strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); |
578 |
|
|
579 |
< |
ethcmd = (struct ethtool_cmd *) ifr.ifr_data; |
580 |
< |
ethcmd->cmd = ETHTOOL_GSET; |
579 |
> |
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) { |
580 |
> |
continue; |
581 |
> |
} |
582 |
|
|
586 |
– |
err = ioctl(sock, SIOCETHTOOL, &ifr); |
587 |
– |
if(err < 0){ |
588 |
– |
/* This could fail if the interface doesn't support the command. Carry |
589 |
– |
* on to the next :) |
590 |
– |
*/ |
591 |
– |
continue; |
592 |
– |
} |
593 |
– |
|
583 |
|
/* We have a good interface to add */ |
584 |
|
network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats); |
585 |
|
if(network_iface_stats==NULL){ |
587 |
|
} |
588 |
|
network_iface_stat_ptr = network_iface_stats + ifaces; |
589 |
|
network_iface_stat_ptr->interface_name = strdup(name); |
590 |
< |
network_iface_stat_ptr->speed = ethcmd->speed; |
602 |
< |
if((ifr.ifr_flags & IFF_UP) != 0){ |
590 |
> |
if ((ifr.ifr_flags & IFF_UP) != 0) { |
591 |
|
network_iface_stat_ptr->up = 1; |
592 |
< |
}else{ |
592 |
> |
} else { |
593 |
|
network_iface_stat_ptr->up = 0; |
594 |
|
} |
595 |
|
|
596 |
< |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
597 |
< |
if(ethcmd->duplex == 0x00){ |
598 |
< |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
596 |
> |
memset(ðcmd, 0, sizeof ethcmd); |
597 |
> |
ethcmd.cmd = ETHTOOL_GSET; |
598 |
> |
ifr.ifr_data = (caddr_t) ðcmd; |
599 |
> |
|
600 |
> |
err = ioctl(sock, SIOCETHTOOL, &ifr); |
601 |
> |
if (err == 0) { |
602 |
> |
network_iface_stat_ptr->speed = ethcmd.speed; |
603 |
> |
|
604 |
> |
switch (ethcmd.duplex) { |
605 |
> |
case 0x00: |
606 |
> |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
607 |
> |
break; |
608 |
> |
case 0x01: |
609 |
> |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
610 |
> |
break; |
611 |
> |
default: |
612 |
> |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
613 |
> |
} |
614 |
> |
} else { |
615 |
> |
/* Not all interfaces support the ethtool ioctl. */ |
616 |
> |
network_iface_stat_ptr->speed = 0; |
617 |
> |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
618 |
|
} |
619 |
< |
if(ethcmd->duplex == 0x01){ |
613 |
< |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
614 |
< |
} |
619 |
> |
|
620 |
|
ifaces++; |
621 |
|
} |
622 |
|
close(sock); |
623 |
< |
free(eth_tool_cmd_buf); |
623 |
> |
fclose(f); |
624 |
|
#endif |
625 |
|
*entries = ifaces; |
626 |
|
return network_iface_stats; |