32 |
|
#ifdef SOLARIS |
33 |
|
#include <kstat.h> |
34 |
|
#include <sys/sysinfo.h> |
35 |
+ |
#include <sys/types.h> |
36 |
+ |
#include <sys/socket.h> |
37 |
+ |
#include <sys/ioctl.h> |
38 |
+ |
#include <net/if.h> |
39 |
+ |
#include <netinet/in.h> |
40 |
+ |
#include <sys/sockio.h> |
41 |
|
#endif |
42 |
|
#ifdef LINUX |
43 |
|
#include <stdio.h> |
44 |
|
#include <sys/types.h> |
45 |
|
#include <regex.h> |
46 |
+ |
#include <sys/ioctl.h> |
47 |
+ |
#include <sys/socket.h> |
48 |
+ |
#include <net/if.h> |
49 |
+ |
#include <ctype.h> |
50 |
|
#include "tools.h" |
51 |
+ |
/* Stuff which could be defined by defining KERNEL, but |
52 |
+ |
* that would be a bad idea, so we'll just declare it here |
53 |
+ |
*/ |
54 |
+ |
typedef __uint8_t u8; |
55 |
+ |
typedef __uint16_t u16; |
56 |
+ |
typedef __uint32_t u32; |
57 |
+ |
typedef __uint64_t u64; |
58 |
+ |
#include <linux/ethtool.h> |
59 |
+ |
#include <linux/sockios.h> |
60 |
+ |
#include <unistd.h> |
61 |
|
#endif |
62 |
|
#ifdef ALLBSD |
63 |
|
#include <sys/types.h> |
64 |
|
#include <sys/socket.h> |
65 |
|
#include <ifaddrs.h> |
66 |
|
#include <net/if.h> |
67 |
+ |
#include <net/if_media.h> |
68 |
+ |
#include <sys/ioctl.h> |
69 |
|
#endif |
70 |
|
|
71 |
|
static network_stat_t *network_stats=NULL; |
180 |
|
#endif |
181 |
|
|
182 |
|
if((knp=kstat_data_lookup(ksp, RLOOKUP))==NULL){ |
183 |
< |
/* Not a network interface, so skip to the next entry */ |
183 |
> |
/* This is a network interface, but it doesn't |
184 |
> |
* have the rbytes/obytes values; for instance, |
185 |
> |
* the loopback devices have this behaviour |
186 |
> |
* (although they do track packets in/out). */ |
187 |
|
continue; |
188 |
|
} |
189 |
|
|
195 |
|
network_stat_ptr->rx=knp->VALTYPE; |
196 |
|
|
197 |
|
if((knp=kstat_data_lookup(ksp, WLOOKUP))==NULL){ |
173 |
– |
/* Not a network interface, so skip to the next entry */ |
198 |
|
continue; |
199 |
|
} |
200 |
|
network_stat_ptr->tx=knp->VALTYPE; |
342 |
|
*entries = diff_count; |
343 |
|
return diff; |
344 |
|
} |
345 |
+ |
/* NETWORK INTERFACE STATS */ |
346 |
+ |
|
347 |
+ |
void network_iface_stat_init(int start, int end, network_iface_stat_t *net_stats){ |
348 |
+ |
|
349 |
+ |
for(net_stats+=start; start<end; start++){ |
350 |
+ |
net_stats->interface_name=NULL; |
351 |
+ |
net_stats->speed=0; |
352 |
+ |
net_stats->dup=UNKNOWN_DUPLEX; |
353 |
+ |
net_stats++; |
354 |
+ |
} |
355 |
+ |
} |
356 |
+ |
|
357 |
+ |
network_iface_stat_t *network_iface_stat_malloc(int needed_entries, int *cur_entries, network_iface_stat_t *net_stats){ |
358 |
+ |
|
359 |
+ |
if(net_stats==NULL){ |
360 |
+ |
|
361 |
+ |
if((net_stats=malloc(needed_entries * sizeof(network_iface_stat_t)))==NULL){ |
362 |
+ |
return NULL; |
363 |
+ |
} |
364 |
+ |
network_iface_stat_init(0, needed_entries, net_stats); |
365 |
+ |
*cur_entries=needed_entries; |
366 |
+ |
|
367 |
+ |
return net_stats; |
368 |
+ |
} |
369 |
+ |
|
370 |
+ |
|
371 |
+ |
if(*cur_entries<needed_entries){ |
372 |
+ |
net_stats=realloc(net_stats, (sizeof(network_iface_stat_t)*needed_entries)); |
373 |
+ |
if(net_stats==NULL){ |
374 |
+ |
return NULL; |
375 |
+ |
} |
376 |
+ |
network_iface_stat_init(*cur_entries, needed_entries, net_stats); |
377 |
+ |
*cur_entries=needed_entries; |
378 |
+ |
} |
379 |
+ |
|
380 |
+ |
return net_stats; |
381 |
+ |
} |
382 |
+ |
|
383 |
+ |
network_iface_stat_t *get_network_iface_stats(int *entries){ |
384 |
+ |
static network_iface_stat_t *network_iface_stats; |
385 |
+ |
network_iface_stat_t *network_iface_stat_ptr; |
386 |
+ |
static int sizeof_network_iface_stats=0; |
387 |
+ |
int ifaces = 0; |
388 |
+ |
|
389 |
+ |
#ifdef SOLARIS |
390 |
+ |
kstat_ctl_t *kc; |
391 |
+ |
kstat_t *ksp; |
392 |
+ |
kstat_named_t *knp; |
393 |
+ |
int sock; |
394 |
+ |
#endif |
395 |
+ |
#ifdef ALLBSD |
396 |
+ |
struct ifaddrs *net, *net_ptr; |
397 |
+ |
struct ifmediareq ifmed; |
398 |
+ |
struct ifreq ifr; |
399 |
+ |
int sock; |
400 |
+ |
int x; |
401 |
+ |
#endif |
402 |
+ |
#ifdef LINUX |
403 |
+ |
FILE *f; |
404 |
+ |
/* Horrible big enough, but it should be easily big enough */ |
405 |
+ |
char line[8096]; |
406 |
+ |
int sock; |
407 |
+ |
#endif |
408 |
+ |
|
409 |
+ |
#ifdef ALLBSD |
410 |
+ |
if(getifaddrs(&net) != 0){ |
411 |
+ |
return NULL; |
412 |
+ |
} |
413 |
+ |
|
414 |
+ |
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return NULL; |
415 |
+ |
|
416 |
+ |
for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){ |
417 |
+ |
if(net_ptr->ifa_addr->sa_family != AF_LINK) continue; |
418 |
+ |
network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats); |
419 |
+ |
if(network_iface_stats==NULL){ |
420 |
+ |
return NULL; |
421 |
+ |
} |
422 |
+ |
network_iface_stat_ptr = network_iface_stats + ifaces; |
423 |
+ |
|
424 |
+ |
memset(&ifr, 0, sizeof(ifr)); |
425 |
+ |
strncpy(ifr.ifr_name, net_ptr->ifa_name, sizeof(ifr.ifr_name)); |
426 |
+ |
|
427 |
+ |
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0){ |
428 |
+ |
continue; |
429 |
+ |
} |
430 |
+ |
if((ifr.ifr_flags & IFF_UP) != 0){ |
431 |
+ |
network_iface_stat_ptr->up = 1; |
432 |
+ |
}else{ |
433 |
+ |
network_iface_stat_ptr->up = 0; |
434 |
+ |
} |
435 |
+ |
|
436 |
+ |
if (network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name); |
437 |
+ |
network_iface_stat_ptr->interface_name = strdup(net_ptr->ifa_name); |
438 |
+ |
if (network_iface_stat_ptr->interface_name == NULL) return NULL; |
439 |
+ |
|
440 |
+ |
network_iface_stat_ptr->speed = 0; |
441 |
+ |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
442 |
+ |
ifaces++; |
443 |
+ |
|
444 |
+ |
memset(&ifmed, 0, sizeof(struct ifmediareq)); |
445 |
+ |
strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name)); |
446 |
+ |
if(ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){ |
447 |
+ |
/* Not all interfaces support the media ioctls. */ |
448 |
+ |
continue; |
449 |
+ |
} |
450 |
+ |
|
451 |
+ |
/* We may need to change this if we start doing wireless devices too */ |
452 |
+ |
if( (ifmed.ifm_active | IFM_ETHER) != ifmed.ifm_active ){ |
453 |
+ |
/* Not a ETHER device */ |
454 |
+ |
continue; |
455 |
+ |
} |
456 |
+ |
|
457 |
+ |
/* Only intrested in the first 4 bits) - Assuming only ETHER devices */ |
458 |
+ |
x = ifmed.ifm_active & 0x0f; |
459 |
+ |
switch(x){ |
460 |
+ |
/* 10 Mbit connections. Speedy :) */ |
461 |
+ |
case(IFM_10_T): |
462 |
+ |
case(IFM_10_2): |
463 |
+ |
case(IFM_10_5): |
464 |
+ |
case(IFM_10_STP): |
465 |
+ |
case(IFM_10_FL): |
466 |
+ |
network_iface_stat_ptr->speed = 10; |
467 |
+ |
break; |
468 |
+ |
/* 100 Mbit conneections */ |
469 |
+ |
case(IFM_100_TX): |
470 |
+ |
case(IFM_100_FX): |
471 |
+ |
case(IFM_100_T4): |
472 |
+ |
case(IFM_100_VG): |
473 |
+ |
case(IFM_100_T2): |
474 |
+ |
network_iface_stat_ptr->speed = 100; |
475 |
+ |
break; |
476 |
+ |
/* 1000 Mbit connections */ |
477 |
+ |
case(IFM_1000_SX): |
478 |
+ |
case(IFM_1000_LX): |
479 |
+ |
case(IFM_1000_CX): |
480 |
+ |
#if defined(FREEBSD) && !defined(FREEBSD5) |
481 |
+ |
case(IFM_1000_TX): |
482 |
+ |
case(IFM_1000_FX): |
483 |
+ |
#else |
484 |
+ |
case(IFM_1000_T): |
485 |
+ |
#endif |
486 |
+ |
network_iface_stat_ptr->speed = 1000; |
487 |
+ |
break; |
488 |
+ |
/* We don't know what it is */ |
489 |
+ |
default: |
490 |
+ |
network_iface_stat_ptr->speed = 0; |
491 |
+ |
break; |
492 |
+ |
} |
493 |
+ |
|
494 |
+ |
if( (ifmed.ifm_active | IFM_FDX) == ifmed.ifm_active ){ |
495 |
+ |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
496 |
+ |
}else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){ |
497 |
+ |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
498 |
+ |
}else{ |
499 |
+ |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
500 |
+ |
} |
501 |
+ |
|
502 |
+ |
} |
503 |
+ |
freeifaddrs(net); |
504 |
+ |
close(sock); |
505 |
+ |
#endif |
506 |
+ |
|
507 |
+ |
#ifdef SOLARIS |
508 |
+ |
if ((kc = kstat_open()) == NULL) { |
509 |
+ |
return NULL; |
510 |
+ |
} |
511 |
+ |
|
512 |
+ |
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) { |
513 |
+ |
return NULL; |
514 |
+ |
} |
515 |
+ |
|
516 |
+ |
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
517 |
+ |
if (!strcmp(ksp->ks_class, "net")) { |
518 |
+ |
struct ifreq ifr; |
519 |
+ |
|
520 |
+ |
kstat_read(kc, ksp, NULL); |
521 |
+ |
|
522 |
+ |
strncpy(ifr.ifr_name, ksp->ks_name, sizeof ifr.ifr_name); |
523 |
+ |
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) { |
524 |
+ |
/* Not a network interface. */ |
525 |
+ |
continue; |
526 |
+ |
} |
527 |
+ |
|
528 |
+ |
network_iface_stats = network_iface_stat_malloc(ifaces + 1, &sizeof_network_iface_stats, network_iface_stats); |
529 |
+ |
if (network_iface_stats == NULL) { |
530 |
+ |
return NULL; |
531 |
+ |
} |
532 |
+ |
network_iface_stat_ptr = network_iface_stats + ifaces; |
533 |
+ |
ifaces++; |
534 |
+ |
|
535 |
+ |
if (network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name); |
536 |
+ |
network_iface_stat_ptr->interface_name = strdup(ksp->ks_name); |
537 |
+ |
if (network_iface_stat_ptr->interface_name == NULL) return NULL; |
538 |
+ |
|
539 |
+ |
if ((ifr.ifr_flags & IFF_UP) != 0) { |
540 |
+ |
network_iface_stat_ptr->up = 1; |
541 |
+ |
} else { |
542 |
+ |
network_iface_stat_ptr->up = 1; |
543 |
+ |
} |
544 |
+ |
|
545 |
+ |
if ((knp = kstat_data_lookup(ksp, "ifspeed")) != NULL) { |
546 |
+ |
network_iface_stat_ptr->speed = knp->value.ui64 / (1000 * 1000); |
547 |
+ |
} else { |
548 |
+ |
network_iface_stat_ptr->speed = 0; |
549 |
+ |
} |
550 |
+ |
|
551 |
+ |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
552 |
+ |
if ((knp = kstat_data_lookup(ksp, "link_duplex")) != NULL) { |
553 |
+ |
switch (knp->value.ui32) { |
554 |
+ |
case 1: |
555 |
+ |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
556 |
+ |
break; |
557 |
+ |
case 2: |
558 |
+ |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
559 |
+ |
break; |
560 |
+ |
} |
561 |
+ |
} |
562 |
+ |
} |
563 |
+ |
} |
564 |
+ |
|
565 |
+ |
close(sock); |
566 |
+ |
kstat_close(kc); |
567 |
+ |
#endif |
568 |
+ |
#ifdef LINUX |
569 |
+ |
f = fopen("/proc/net/dev", "r"); |
570 |
+ |
if(f == NULL){ |
571 |
+ |
return NULL; |
572 |
+ |
} |
573 |
+ |
|
574 |
+ |
/* Setup stuff so we can do the ioctl to get the info */ |
575 |
+ |
if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){ |
576 |
+ |
return NULL; |
577 |
+ |
} |
578 |
+ |
|
579 |
+ |
/* Ignore first 2 lines.. Just headings */ |
580 |
+ |
if((fgets(line, sizeof(line), f)) == NULL) return NULL; |
581 |
+ |
if((fgets(line, sizeof(line), f)) == NULL) return NULL; |
582 |
+ |
|
583 |
+ |
while((fgets(line, sizeof(line), f)) != NULL){ |
584 |
+ |
char *name, *ptr; |
585 |
+ |
struct ifreq ifr; |
586 |
+ |
struct ethtool_cmd ethcmd; |
587 |
+ |
int err; |
588 |
+ |
|
589 |
+ |
/* Get the interface name */ |
590 |
+ |
ptr = strchr(line, ':'); |
591 |
+ |
if (ptr == NULL) continue; |
592 |
+ |
*ptr='\0'; |
593 |
+ |
name = line; |
594 |
+ |
while(isspace(*(name))){ |
595 |
+ |
name++; |
596 |
+ |
} |
597 |
+ |
|
598 |
+ |
memset(&ifr, 0, sizeof ifr); |
599 |
+ |
strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); |
600 |
+ |
|
601 |
+ |
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) { |
602 |
+ |
continue; |
603 |
+ |
} |
604 |
+ |
|
605 |
+ |
/* We have a good interface to add */ |
606 |
+ |
network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats); |
607 |
+ |
if(network_iface_stats==NULL){ |
608 |
+ |
return NULL; |
609 |
+ |
} |
610 |
+ |
network_iface_stat_ptr = network_iface_stats + ifaces; |
611 |
+ |
network_iface_stat_ptr->interface_name = strdup(name); |
612 |
+ |
if ((ifr.ifr_flags & IFF_UP) != 0) { |
613 |
+ |
network_iface_stat_ptr->up = 1; |
614 |
+ |
} else { |
615 |
+ |
network_iface_stat_ptr->up = 0; |
616 |
+ |
} |
617 |
+ |
|
618 |
+ |
memset(ðcmd, 0, sizeof ethcmd); |
619 |
+ |
ethcmd.cmd = ETHTOOL_GSET; |
620 |
+ |
ifr.ifr_data = (caddr_t) ðcmd; |
621 |
+ |
|
622 |
+ |
err = ioctl(sock, SIOCETHTOOL, &ifr); |
623 |
+ |
if (err == 0) { |
624 |
+ |
network_iface_stat_ptr->speed = ethcmd.speed; |
625 |
+ |
|
626 |
+ |
switch (ethcmd.duplex) { |
627 |
+ |
case 0x00: |
628 |
+ |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
629 |
+ |
break; |
630 |
+ |
case 0x01: |
631 |
+ |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
632 |
+ |
break; |
633 |
+ |
default: |
634 |
+ |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
635 |
+ |
} |
636 |
+ |
} else { |
637 |
+ |
/* Not all interfaces support the ethtool ioctl. */ |
638 |
+ |
network_iface_stat_ptr->speed = 0; |
639 |
+ |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
640 |
+ |
} |
641 |
+ |
|
642 |
+ |
ifaces++; |
643 |
+ |
} |
644 |
+ |
close(sock); |
645 |
+ |
fclose(f); |
646 |
+ |
#endif |
647 |
+ |
*entries = ifaces; |
648 |
+ |
return network_iface_stats; |
649 |
+ |
} |
650 |
+ |
|