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 |
|
#endif |
54 |
|
#ifdef ALLBSD |
55 |
|
#include <sys/types.h> |
56 |
|
#include <sys/socket.h> |
57 |
|
#include <ifaddrs.h> |
58 |
|
#include <net/if.h> |
59 |
+ |
#include <net/if_media.h> |
60 |
+ |
#include <sys/ioctl.h> |
61 |
|
#endif |
62 |
|
|
63 |
|
static network_stat_t *network_stats=NULL; |
374 |
|
static network_iface_stat_t *network_iface_stats; |
375 |
|
network_iface_stat_t *network_iface_stat_ptr; |
376 |
|
static int sizeof_network_iface_stats=0; |
377 |
< |
int ifaces; |
377 |
> |
static int ifaces; |
378 |
|
|
379 |
|
#ifdef SOLARIS |
380 |
|
kstat_ctl_t *kc; |
381 |
|
kstat_t *ksp; |
382 |
|
kstat_named_t *knp; |
383 |
|
#endif |
384 |
+ |
#ifdef ALLBSD |
385 |
+ |
struct ifaddrs *net, *net_ptr; |
386 |
+ |
struct ifmediareq ifmed; |
387 |
+ |
int s; |
388 |
+ |
int x; |
389 |
+ |
#endif |
390 |
+ |
#ifdef LINUX |
391 |
+ |
FILE *f; |
392 |
+ |
/* Horrible big enough, but it should be easily big enough */ |
393 |
+ |
char line[8096]; |
394 |
+ |
void *eth_tool_cmd_buf; |
395 |
+ |
int buf_size; |
396 |
+ |
int sock; |
397 |
+ |
#endif |
398 |
+ |
ifaces = 0; |
399 |
+ |
#ifdef ALLBSD |
400 |
+ |
if(getifaddrs(&net) != 0){ |
401 |
+ |
return NULL; |
402 |
+ |
} |
403 |
|
|
404 |
+ |
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == NULL) 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; |
408 |
+ |
network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats); |
409 |
+ |
if(network_iface_stats==NULL){ |
410 |
+ |
return NULL; |
411 |
+ |
} |
412 |
+ |
network_iface_stat_ptr = network_iface_stats + ifaces; |
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){ |
417 |
+ |
continue; |
418 |
+ |
} |
419 |
+ |
|
420 |
+ |
/* We may need to change this if we start doing wireless devices too */ |
421 |
+ |
if( (ifmed.ifm_active | IFM_ETHER) != ifmed.ifm_active ){ |
422 |
+ |
/* Not a ETHER device */ |
423 |
+ |
continue; |
424 |
+ |
} |
425 |
+ |
|
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 |
+ |
|
430 |
+ |
/* Only intrested in the first 4 bits) - Assuming only ETHER devices */ |
431 |
+ |
x = ifmed.ifm_active & 0x0f; |
432 |
+ |
switch(x){ |
433 |
+ |
/* 10 Mbit connections. Speedy :) */ |
434 |
+ |
case(IFM_10_T): |
435 |
+ |
case(IFM_10_2): |
436 |
+ |
case(IFM_10_5): |
437 |
+ |
case(IFM_10_STP): |
438 |
+ |
case(IFM_10_FL): |
439 |
+ |
network_iface_stat_ptr->speed = 10; |
440 |
+ |
break; |
441 |
+ |
/* 100 Mbit conneections */ |
442 |
+ |
case(IFM_100_TX): |
443 |
+ |
case(IFM_100_FX): |
444 |
+ |
case(IFM_100_T4): |
445 |
+ |
case(IFM_100_VG): |
446 |
+ |
case(IFM_100_T2): |
447 |
+ |
network_iface_stat_ptr->speed = 100; |
448 |
+ |
break; |
449 |
+ |
/* 1000 Mbit connections */ |
450 |
+ |
case(IFM_1000_SX): |
451 |
+ |
case(IFM_1000_LX): |
452 |
+ |
case(IFM_1000_CX): |
453 |
+ |
case(IFM_1000_TX): |
454 |
+ |
network_iface_stat_ptr->speed = 1000; |
455 |
+ |
break; |
456 |
+ |
/* We don't know what it is */ |
457 |
+ |
default: |
458 |
+ |
network_iface_stat_ptr->speed = 0; |
459 |
+ |
break; |
460 |
+ |
} |
461 |
+ |
|
462 |
+ |
if( (ifmed.ifm_active | IFM_FDX) == ifmed.ifm_active ){ |
463 |
+ |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
464 |
+ |
}else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){ |
465 |
+ |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
466 |
+ |
}else{ |
467 |
+ |
network_iface_stat_ptr->dup = NO_DUPLEX; |
468 |
+ |
} |
469 |
+ |
ifaces++; |
470 |
+ |
} |
471 |
+ |
freeifaddrs(net); |
472 |
+ |
#endif |
473 |
+ |
|
474 |
|
#ifdef SOLARIS |
475 |
|
if ((kc = kstat_open()) == NULL) { |
476 |
|
return NULL; |
477 |
|
} |
478 |
|
|
376 |
– |
ifaces=0; |
377 |
– |
|
479 |
|
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
480 |
|
if (!strcmp(ksp->ks_class, "net")) { |
481 |
|
kstat_read(kc, ksp, NULL); |
487 |
|
if(network_iface_stats==NULL){ |
488 |
|
return NULL; |
489 |
|
} |
490 |
< |
network_iface_stat_ptr = network_iface_stats + interfaces; |
490 |
> |
network_iface_stat_ptr = network_iface_stats + ifaces; |
491 |
|
network_iface_stat_ptr->speed = knp->value.ui64 / (1000*1000); |
492 |
|
|
493 |
|
if((knp=kstat_data_lookup(ksp, "link_duplex"))==NULL){ |
501 |
|
network_iface_stat_ptr->dup = HALF_DUPLEX; |
502 |
|
} |
503 |
|
|
504 |
+ |
if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name); |
505 |
|
network_iface_stat_ptr->interface_name = strdup(ksp->ks_name); |
506 |
< |
interfaces++; |
506 |
> |
if(network_iface_stat_ptr->interface_name == NULL) return NULL; |
507 |
> |
ifaces++; |
508 |
|
} |
509 |
|
} |
510 |
|
kstat_close(kc); |
511 |
|
|
512 |
|
#endif |
513 |
< |
*entries = interfaces; |
513 |
> |
#ifdef LINUX |
514 |
> |
|
515 |
> |
f = fopen("/proc/net/dev", "r"); |
516 |
> |
if(f == NULL){ |
517 |
> |
return NULL; |
518 |
> |
} |
519 |
> |
|
520 |
> |
/* Setup stuff so we can do the ioctl to get the info */ |
521 |
> |
if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){ |
522 |
> |
return NULL; |
523 |
> |
} |
524 |
> |
|
525 |
> |
buf_size = sizeof(struct ethtool_cmd); |
526 |
> |
eth_tool_cmd_buf = malloc(buf_size); |
527 |
> |
if(eth_tool_cmd_buf == NULL) return NULL; |
528 |
> |
|
529 |
> |
/* Ignore first 2 lines.. Just headings */ |
530 |
> |
fgets(line, sizeof(line), f); |
531 |
> |
fgets(line, sizeof(line), f); |
532 |
> |
|
533 |
> |
while((fgets(line, sizeof(line), f)) != NULL){ |
534 |
> |
char *name, *ptr; |
535 |
> |
struct ifreq ifr; |
536 |
> |
struct ethtool_cmd *ethcmd; |
537 |
> |
int err; |
538 |
> |
|
539 |
> |
/* Get the interface name */ |
540 |
> |
ptr = strchr(line, ':'); |
541 |
> |
if (ptr == NULL) continue; |
542 |
> |
*ptr='\0'; |
543 |
> |
name = line; |
544 |
> |
while(isspace(*(name))){ |
545 |
> |
name++; |
546 |
> |
} |
547 |
> |
|
548 |
> |
memset(&ifr, 0, sizeof(ifr)); |
549 |
> |
memset(eth_tool_cmd_buf, 0, buf_size); |
550 |
> |
ifr.ifr_data = (caddr_t) eth_tool_cmd_buf; |
551 |
> |
strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); |
552 |
> |
|
553 |
> |
ethcmd = (struct ethtool_cmd *)(&ifr)->ifr_data; |
554 |
> |
ethcmd->cmd = ETHTOOL_GSET; |
555 |
> |
|
556 |
> |
err = ioctl(sock, SIOCETHTOOL, &ifr); |
557 |
> |
if(err < 0){ |
558 |
> |
/* This could fail if the interface doesn't support the command. Carry |
559 |
> |
* on to the next :) |
560 |
> |
*/ |
561 |
> |
continue; |
562 |
> |
} |
563 |
> |
|
564 |
> |
/* We have a good interface to add */ |
565 |
> |
network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats); |
566 |
> |
if(network_iface_stats==NULL){ |
567 |
> |
return NULL; |
568 |
> |
} |
569 |
> |
network_iface_stat_ptr = network_iface_stats + ifaces; |
570 |
> |
network_iface_stat_ptr->interface_name = strdup(name); |
571 |
> |
network_iface_stat_ptr->speed = ethcmd->speed; |
572 |
> |
network_iface_stat_ptr->dup = NO_DUPLEX; |
573 |
> |
if(ethcmd->duplex == 0x00){ |
574 |
> |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
575 |
> |
} |
576 |
> |
if(ethcmd->duplex == 0x01){ |
577 |
> |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
578 |
> |
} |
579 |
> |
ifaces++; |
580 |
> |
} |
581 |
> |
|
582 |
> |
free(eth_tool_cmd_buf); |
583 |
> |
#endif |
584 |
> |
*entries = ifaces; |
585 |
|
return network_iface_stats; |
586 |
|
} |
587 |
|
|