37 |
|
#include <stdio.h> |
38 |
|
#include <sys/types.h> |
39 |
|
#include <regex.h> |
40 |
+ |
#include <sys/ioctl.h> |
41 |
+ |
#include <sys/socket.h> |
42 |
+ |
#include <net/if.h> |
43 |
+ |
#include <ctype.h> |
44 |
|
#include "tools.h" |
45 |
+ |
/* Stuff which could be defined by defining KERNEL, but |
46 |
+ |
* that would be a bad idea, so we'll just declare it here |
47 |
+ |
*/ |
48 |
+ |
typedef __uint8_t u8; |
49 |
+ |
typedef __uint16_t u16; |
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=NO_DUPLEX; |
343 |
> |
net_stats->dup=UNKNOWN_DUPLEX; |
344 |
|
net_stats++; |
345 |
|
} |
346 |
|
} |
388 |
|
int s; |
389 |
|
int x; |
390 |
|
#endif |
391 |
< |
|
391 |
> |
#ifdef LINUX |
392 |
> |
FILE *f; |
393 |
> |
/* Horrible big enough, but it should be easily big enough */ |
394 |
> |
char line[8096]; |
395 |
> |
void *eth_tool_cmd_buf; |
396 |
> |
int buf_size; |
397 |
> |
int sock; |
398 |
> |
#endif |
399 |
|
ifaces = 0; |
400 |
|
#ifdef ALLBSD |
401 |
|
if(getifaddrs(&net) != 0){ |
451 |
|
case(IFM_1000_SX): |
452 |
|
case(IFM_1000_LX): |
453 |
|
case(IFM_1000_CX): |
454 |
+ |
#if defined(FREEBSD) && !defined(FREEBSD5) |
455 |
|
case(IFM_1000_TX): |
456 |
+ |
case(IFM_1000_FX): |
457 |
+ |
#else |
458 |
+ |
case(IFM_1000_T): |
459 |
+ |
#endif |
460 |
|
network_iface_stat_ptr->speed = 1000; |
461 |
|
break; |
462 |
|
/* We don't know what it is */ |
470 |
|
}else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){ |
471 |
|
network_iface_stat_ptr->dup = HALF_DUPLEX; |
472 |
|
}else{ |
473 |
< |
network_iface_stat_ptr->dup = NO_DUPLEX; |
473 |
> |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
474 |
|
} |
475 |
|
ifaces++; |
476 |
|
} |
477 |
|
freeifaddrs(net); |
478 |
+ |
close(s); |
479 |
|
#endif |
480 |
|
|
481 |
|
#ifdef SOLARIS |
517 |
|
kstat_close(kc); |
518 |
|
|
519 |
|
#endif |
520 |
+ |
#ifdef LINUX |
521 |
+ |
|
522 |
+ |
f = fopen("/proc/net/dev", "r"); |
523 |
+ |
if(f == NULL){ |
524 |
+ |
return NULL; |
525 |
+ |
} |
526 |
+ |
|
527 |
+ |
/* Setup stuff so we can do the ioctl to get the info */ |
528 |
+ |
if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){ |
529 |
+ |
return NULL; |
530 |
+ |
} |
531 |
+ |
|
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 |
+ |
|
536 |
+ |
/* Ignore first 2 lines.. Just headings */ |
537 |
+ |
if((fgets(line, sizeof(line), f)) == NULL) return NULL; |
538 |
+ |
if((fgets(line, sizeof(line), f)) == NULL) return NULL; |
539 |
+ |
|
540 |
+ |
while((fgets(line, sizeof(line), f)) != NULL){ |
541 |
+ |
char *name, *ptr; |
542 |
+ |
struct ifreq ifr; |
543 |
+ |
struct ethtool_cmd *ethcmd; |
544 |
+ |
int err; |
545 |
+ |
|
546 |
+ |
/* Get the interface name */ |
547 |
+ |
ptr = strchr(line, ':'); |
548 |
+ |
if (ptr == NULL) continue; |
549 |
+ |
*ptr='\0'; |
550 |
+ |
name = line; |
551 |
+ |
while(isspace(*(name))){ |
552 |
+ |
name++; |
553 |
+ |
} |
554 |
+ |
|
555 |
+ |
memset(&ifr, 0, sizeof(ifr)); |
556 |
+ |
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)); |
559 |
+ |
|
560 |
+ |
ethcmd = (struct ethtool_cmd *) ifr.ifr_data; |
561 |
+ |
ethcmd->cmd = ETHTOOL_GSET; |
562 |
+ |
|
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 |
+ |
|
571 |
+ |
/* We have a good interface to add */ |
572 |
+ |
network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats); |
573 |
+ |
if(network_iface_stats==NULL){ |
574 |
+ |
return NULL; |
575 |
+ |
} |
576 |
+ |
network_iface_stat_ptr = network_iface_stats + ifaces; |
577 |
+ |
network_iface_stat_ptr->interface_name = strdup(name); |
578 |
+ |
network_iface_stat_ptr->speed = ethcmd->speed; |
579 |
+ |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
580 |
+ |
if(ethcmd->duplex == 0x00){ |
581 |
+ |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
582 |
+ |
} |
583 |
+ |
if(ethcmd->duplex == 0x01){ |
584 |
+ |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
585 |
+ |
} |
586 |
+ |
ifaces++; |
587 |
+ |
} |
588 |
+ |
close(sock); |
589 |
+ |
free(eth_tool_cmd_buf); |
590 |
+ |
#endif |
591 |
|
*entries = ifaces; |
592 |
|
return network_iface_stats; |
593 |
|
} |