50 |
|
typedef __uint32_t u32; |
51 |
|
#include <linux/ethtool.h> |
52 |
|
#include <linux/sockios.h> |
53 |
+ |
#include <unistd.h> |
54 |
|
#endif |
55 |
|
#ifdef ALLBSD |
56 |
|
#include <sys/types.h> |
340 |
|
for(net_stats+=start; start<end; start++){ |
341 |
|
net_stats->interface_name=NULL; |
342 |
|
net_stats->speed=0; |
343 |
< |
net_stats->dup=UNKNOWN_DUPEX; |
343 |
> |
net_stats->dup=UNKNOWN_DUPLEX; |
344 |
|
net_stats++; |
345 |
|
} |
346 |
|
} |
385 |
|
#ifdef ALLBSD |
386 |
|
struct ifaddrs *net, *net_ptr; |
387 |
|
struct ifmediareq ifmed; |
388 |
< |
int s; |
388 |
> |
struct ifreq ifr; |
389 |
> |
int sock; |
390 |
|
int x; |
391 |
|
#endif |
392 |
|
#ifdef LINUX |
393 |
|
FILE *f; |
394 |
|
/* Horrible big enough, but it should be easily big enough */ |
395 |
|
char line[8096]; |
394 |
– |
void *eth_tool_cmd_buf; |
395 |
– |
int buf_size; |
396 |
|
int sock; |
397 |
|
#endif |
398 |
|
ifaces = 0; |
401 |
|
return NULL; |
402 |
|
} |
403 |
|
|
404 |
< |
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == NULL) return NULL; |
404 |
> |
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return NULL; |
405 |
|
|
406 |
|
for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){ |
407 |
|
if(net_ptr->ifa_addr->sa_family != AF_LINK) continue; |
413 |
|
|
414 |
|
memset(&ifmed, 0, sizeof(struct ifmediareq)); |
415 |
|
strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name)); |
416 |
< |
if(ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){ |
416 |
> |
if(ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){ |
417 |
|
continue; |
418 |
|
} |
419 |
|
|
450 |
|
case(IFM_1000_SX): |
451 |
|
case(IFM_1000_LX): |
452 |
|
case(IFM_1000_CX): |
453 |
< |
#ifdef FREEBSD5 |
454 |
< |
case(IFM_1000_T): |
455 |
< |
#else |
453 |
> |
#if defined(FREEBSD) && !defined(FREEBSD5) |
454 |
|
case(IFM_1000_TX): |
455 |
|
case(IFM_1000_FX): |
456 |
+ |
#else |
457 |
+ |
case(IFM_1000_T): |
458 |
|
#endif |
459 |
|
network_iface_stat_ptr->speed = 1000; |
460 |
|
break; |
469 |
|
}else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){ |
470 |
|
network_iface_stat_ptr->dup = HALF_DUPLEX; |
471 |
|
}else{ |
472 |
< |
network_iface_stat_ptr->dup = UNKNOWN_DUPEX; |
472 |
> |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
473 |
|
} |
474 |
+ |
|
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++; |
488 |
|
} |
489 |
|
freeifaddrs(net); |
490 |
+ |
close(sock); |
491 |
|
#endif |
492 |
|
|
493 |
|
#ifdef SOLARIS |
509 |
|
network_iface_stat_ptr = network_iface_stats + ifaces; |
510 |
|
network_iface_stat_ptr->speed = knp->value.ui64 / (1000*1000); |
511 |
|
|
512 |
+ |
if((knp=kstat_data_lookup(ksp, "link_up"))==NULL){ |
513 |
+ |
/* Not a network interface, so skip to the next entry */ |
514 |
+ |
continue; |
515 |
+ |
} |
516 |
+ |
/* Solaris has 1 for up, and 0 for not. As we do too */ |
517 |
+ |
network_iface_stat_ptr->up = value.ui32; |
518 |
+ |
|
519 |
|
if((knp=kstat_data_lookup(ksp, "link_duplex"))==NULL){ |
520 |
|
/* Not a network interface, so skip to the next entry */ |
521 |
|
continue; |
522 |
|
} |
523 |
|
|
524 |
< |
if(knp->value.ui64 == 0){ |
524 |
> |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
525 |
> |
if(knp->value.ui32 == 2){ |
526 |
|
network_iface_stat_ptr->dup = FULL_DUPLEX; |
527 |
< |
}else{ |
527 |
> |
} |
528 |
> |
if(knp->value.ui32 == 1){ |
529 |
|
network_iface_stat_ptr->dup = HALF_DUPLEX; |
530 |
|
} |
531 |
|
|
539 |
|
|
540 |
|
#endif |
541 |
|
#ifdef LINUX |
519 |
– |
|
542 |
|
f = fopen("/proc/net/dev", "r"); |
543 |
|
if(f == NULL){ |
544 |
|
return NULL; |
549 |
|
return NULL; |
550 |
|
} |
551 |
|
|
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 |
– |
|
552 |
|
/* Ignore first 2 lines.. Just headings */ |
553 |
|
if((fgets(line, sizeof(line), f)) == NULL) return NULL; |
554 |
|
if((fgets(line, sizeof(line), f)) == NULL) return NULL; |
556 |
|
while((fgets(line, sizeof(line), f)) != NULL){ |
557 |
|
char *name, *ptr; |
558 |
|
struct ifreq ifr; |
559 |
< |
struct ethtool_cmd *ethcmd; |
559 |
> |
struct ethtool_cmd ethcmd; |
560 |
|
int err; |
561 |
|
|
562 |
|
/* Get the interface name */ |
568 |
|
name++; |
569 |
|
} |
570 |
|
|
571 |
< |
memset(&ifr, 0, sizeof(ifr)); |
572 |
< |
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)); |
571 |
> |
memset(&ifr, 0, sizeof ifr); |
572 |
> |
strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); |
573 |
|
|
574 |
< |
ethcmd = (struct ethtool_cmd *) ifr.ifr_data; |
575 |
< |
ethcmd->cmd = ETHTOOL_GSET; |
574 |
> |
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) { |
575 |
> |
continue; |
576 |
> |
} |
577 |
|
|
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 |
– |
|
578 |
|
/* We have a good interface to add */ |
579 |
|
network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats); |
580 |
|
if(network_iface_stats==NULL){ |
582 |
|
} |
583 |
|
network_iface_stat_ptr = network_iface_stats + ifaces; |
584 |
|
network_iface_stat_ptr->interface_name = strdup(name); |
585 |
< |
network_iface_stat_ptr->speed = ethcmd->speed; |
586 |
< |
network_iface_stat_ptr->dup = UNKNOWN_DUPEX; |
587 |
< |
if(ethcmd->duplex == 0x00){ |
588 |
< |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
585 |
> |
if ((ifr.ifr_flags & IFF_UP) != 0) { |
586 |
> |
network_iface_stat_ptr->up = 1; |
587 |
> |
} else { |
588 |
> |
network_iface_stat_ptr->up = 0; |
589 |
|
} |
590 |
< |
if(ethcmd->duplex == 0x01){ |
591 |
< |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
590 |
> |
|
591 |
> |
memset(ðcmd, 0, sizeof ethcmd); |
592 |
> |
ethcmd.cmd = ETHTOOL_GSET; |
593 |
> |
ifr.ifr_data = (caddr_t) ðcmd; |
594 |
> |
|
595 |
> |
err = ioctl(sock, SIOCETHTOOL, &ifr); |
596 |
> |
if (err == 0) { |
597 |
> |
network_iface_stat_ptr->speed = ethcmd.speed; |
598 |
> |
|
599 |
> |
switch (ethcmd.duplex) { |
600 |
> |
case 0x00: |
601 |
> |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
602 |
> |
break; |
603 |
> |
case 0x01: |
604 |
> |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
605 |
> |
break; |
606 |
> |
default: |
607 |
> |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
608 |
> |
} |
609 |
> |
} else { |
610 |
> |
/* Not all interfaces support the ethtool ioctl. */ |
611 |
> |
network_iface_stat_ptr->speed = -1; |
612 |
> |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
613 |
|
} |
614 |
+ |
|
615 |
|
ifaces++; |
616 |
|
} |
617 |
< |
|
618 |
< |
free(eth_tool_cmd_buf); |
617 |
> |
close(sock); |
618 |
> |
fclose(f); |
619 |
|
#endif |
620 |
|
*entries = ifaces; |
621 |
|
return network_iface_stats; |