44 |
|
#include <sys/socket.h> |
45 |
|
#include <ifaddrs.h> |
46 |
|
#include <net/if.h> |
47 |
+ |
#include <net/if_media.h> |
48 |
+ |
#include <sys/ioctl.h> |
49 |
|
#endif |
50 |
|
|
51 |
|
static network_stat_t *network_stats=NULL; |
362 |
|
static network_iface_stat_t *network_iface_stats; |
363 |
|
network_iface_stat_t *network_iface_stat_ptr; |
364 |
|
static int sizeof_network_iface_stats=0; |
365 |
< |
int ifaces; |
365 |
> |
static int ifaces; |
366 |
|
|
367 |
|
#ifdef SOLARIS |
368 |
|
kstat_ctl_t *kc; |
369 |
|
kstat_t *ksp; |
370 |
|
kstat_named_t *knp; |
371 |
|
#endif |
372 |
+ |
#ifdef ALLBSD |
373 |
+ |
struct ifaddrs *net, *net_ptr; |
374 |
+ |
struct ifmediareq ifmed; |
375 |
+ |
int s; |
376 |
+ |
int x; |
377 |
+ |
#endif |
378 |
|
|
379 |
+ |
ifaces = 0; |
380 |
+ |
#ifdef ALLBSD |
381 |
+ |
if(getifaddrs(&net) != 0){ |
382 |
+ |
return NULL; |
383 |
+ |
} |
384 |
+ |
|
385 |
+ |
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == NULL) return NULL; |
386 |
+ |
|
387 |
+ |
for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){ |
388 |
+ |
if(net_ptr->ifa_addr->sa_family != AF_LINK) continue; |
389 |
+ |
network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats); |
390 |
+ |
if(network_iface_stats==NULL){ |
391 |
+ |
return NULL; |
392 |
+ |
} |
393 |
+ |
network_iface_stat_ptr = network_iface_stats + ifaces; |
394 |
+ |
|
395 |
+ |
memset(&ifmed, 0, sizeof(struct ifmediareq)); |
396 |
+ |
strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name)); |
397 |
+ |
if(ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){ |
398 |
+ |
continue; |
399 |
+ |
} |
400 |
+ |
|
401 |
+ |
/* We may need to change this if we start doing wireless devices too */ |
402 |
+ |
if( (ifmed.ifm_active | IFM_ETHER) != ifmed.ifm_active ){ |
403 |
+ |
/* Not a ETHER device */ |
404 |
+ |
continue; |
405 |
+ |
} |
406 |
+ |
|
407 |
+ |
if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name); |
408 |
+ |
network_iface_stat_ptr->interface_name = strdup(net_ptr->ifa_name); |
409 |
+ |
if(network_iface_stat_ptr->interface_name == NULL) return NULL; |
410 |
+ |
|
411 |
+ |
/* Only intrested in the first 4 bits) - Assuming only ETHER devices */ |
412 |
+ |
x = ifmed.ifm_active & 0x0f; |
413 |
+ |
switch(x){ |
414 |
+ |
/* 10 Mbit connections. Speedy :) */ |
415 |
+ |
case(IFM_10_T): |
416 |
+ |
case(IFM_10_2): |
417 |
+ |
case(IFM_10_5): |
418 |
+ |
case(IFM_10_STP): |
419 |
+ |
case(IFM_10_FL): |
420 |
+ |
network_iface_stat_ptr->speed = 10; |
421 |
+ |
break; |
422 |
+ |
/* 100 Mbit conneections */ |
423 |
+ |
case(IFM_100_TX): |
424 |
+ |
case(IFM_100_FX): |
425 |
+ |
case(IFM_100_T4): |
426 |
+ |
case(IFM_100_VG): |
427 |
+ |
case(IFM_100_T2): |
428 |
+ |
network_iface_stat_ptr->speed = 100; |
429 |
+ |
break; |
430 |
+ |
/* 1000 Mbit connections */ |
431 |
+ |
case(IFM_1000_SX): |
432 |
+ |
case(IFM_1000_LX): |
433 |
+ |
case(IFM_1000_CX): |
434 |
+ |
case(IFM_1000_TX): |
435 |
+ |
network_iface_stat_ptr->speed = 1000; |
436 |
+ |
break; |
437 |
+ |
/* We don't know what it is */ |
438 |
+ |
default: |
439 |
+ |
network_iface_stat_ptr->speed = 0; |
440 |
+ |
break; |
441 |
+ |
} |
442 |
+ |
|
443 |
+ |
if( (ifmed.ifm_active | IFM_FDX) == ifmed.ifm_active ){ |
444 |
+ |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
445 |
+ |
}else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){ |
446 |
+ |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
447 |
+ |
}else{ |
448 |
+ |
network_iface_stat_ptr->dup = NO_DUPLEX; |
449 |
+ |
} |
450 |
+ |
ifaces++; |
451 |
+ |
} |
452 |
+ |
freeifaddrs(net); |
453 |
+ |
#endif |
454 |
+ |
|
455 |
|
#ifdef SOLARIS |
456 |
|
if ((kc = kstat_open()) == NULL) { |
457 |
|
return NULL; |
458 |
|
} |
459 |
|
|
376 |
– |
ifaces=0; |
377 |
– |
|
460 |
|
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
461 |
|
if (!strcmp(ksp->ks_class, "net")) { |
462 |
|
kstat_read(kc, ksp, NULL); |
468 |
|
if(network_iface_stats==NULL){ |
469 |
|
return NULL; |
470 |
|
} |
471 |
< |
network_iface_stat_ptr = network_iface_stats + interfaces; |
471 |
> |
network_iface_stat_ptr = network_iface_stats + ifaces; |
472 |
|
network_iface_stat_ptr->speed = knp->value.ui64 / (1000*1000); |
473 |
|
|
474 |
|
if((knp=kstat_data_lookup(ksp, "link_duplex"))==NULL){ |
482 |
|
network_iface_stat_ptr->dup = HALF_DUPLEX; |
483 |
|
} |
484 |
|
|
485 |
+ |
if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name); |
486 |
|
network_iface_stat_ptr->interface_name = strdup(ksp->ks_name); |
487 |
< |
interfaces++; |
487 |
> |
if(network_iface_stat_ptr->interface_name == NULL) return NULL; |
488 |
> |
ifaces++; |
489 |
|
} |
490 |
|
} |
491 |
|
kstat_close(kc); |
492 |
|
|
493 |
|
#endif |
494 |
< |
*entries = interfaces; |
494 |
> |
*entries = ifaces; |
495 |
|
return network_iface_stats; |
496 |
|
} |
497 |
|
|