ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/network_stats.c
(Generate patch)

Comparing projects/libstatgrab/src/libstatgrab/network_stats.c (file contents):
Revision 1.24 by ats, Wed Jan 21 23:46:54 2004 UTC vs.
Revision 1.46 by tdb, Sat Mar 6 19:04:29 2004 UTC

# Line 32 | Line 32
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;
# Line 129 | Line 151 | network_stat_t *get_network_stats(int *entries){
151                  if(network_stat_ptr->interface_name==NULL) return NULL;
152                  net_data=(struct if_data *)net_ptr->ifa_data;
153                  network_stat_ptr->rx=net_data->ifi_ibytes;
154 <                network_stat_ptr->tx=net_data->ifi_obytes;                      
154 >                network_stat_ptr->tx=net_data->ifi_obytes;
155 >                network_stat_ptr->ipackets=net_data->ifi_ipackets;
156 >                network_stat_ptr->opackets=net_data->ifi_opackets;
157 >                network_stat_ptr->ierrors=net_data->ifi_ierrors;
158 >                network_stat_ptr->oerrors=net_data->ifi_oerrors;
159 >                network_stat_ptr->collisions=net_data->ifi_collisions;
160                  network_stat_ptr->systime=time(NULL);
161                  interfaces++;
162          }
# Line 158 | Line 185 | network_stat_t *get_network_stats(int *entries){
185   #endif
186  
187                          if((knp=kstat_data_lookup(ksp, RLOOKUP))==NULL){
188 <                                /* Not a network interface, so skip to the next entry */
188 >                                /* This is a network interface, but it doesn't
189 >                                 * have the rbytes/obytes values; for instance,
190 >                                 * the loopback devices have this behaviour
191 >                                 * (although they do track packets in/out). */
192                                  continue;
193                          }
194  
# Line 170 | Line 200 | network_stat_t *get_network_stats(int *entries){
200                          network_stat_ptr->rx=knp->VALTYPE;
201  
202                          if((knp=kstat_data_lookup(ksp, WLOOKUP))==NULL){
173                                /* Not a network interface, so skip to the next entry */
203                                  continue;
204                          }
205                          network_stat_ptr->tx=knp->VALTYPE;
# Line 238 | Line 267 | network_stat_t *get_network_stats(int *entries){
267   }
268  
269   long long transfer_diff(long long new, long long old){
270 < #if defined(SOL7) || defined(LINUX) || defined(FREEBSD)
270 > #if defined(SOL7) || defined(LINUX) || defined(FREEBSD) || defined(DFBSD)
271   #define MAXVAL 4294967296LL
272   #else
273   #define MAXVAL 18446744073709551616LL
# Line 318 | Line 347 | network_stat_t *get_network_stats_diff(int *entries) {
347          *entries = diff_count;
348          return diff;
349   }
350 + /* NETWORK INTERFACE STATS */
351 +
352 + void network_iface_stat_init(int start, int end, network_iface_stat_t *net_stats){
353 +
354 +        for(net_stats+=start; start<end; start++){
355 +                net_stats->interface_name=NULL;
356 +                net_stats->speed=0;
357 +                net_stats->dup=UNKNOWN_DUPLEX;
358 +                net_stats++;
359 +        }
360 + }
361 +
362 + network_iface_stat_t *network_iface_stat_malloc(int needed_entries, int *cur_entries, network_iface_stat_t *net_stats){
363 +
364 +        if(net_stats==NULL){
365 +
366 +                if((net_stats=malloc(needed_entries * sizeof(network_iface_stat_t)))==NULL){
367 +                        return NULL;
368 +                }
369 +                network_iface_stat_init(0, needed_entries, net_stats);
370 +                *cur_entries=needed_entries;
371 +
372 +                return net_stats;
373 +        }
374 +
375 +
376 +        if(*cur_entries<needed_entries){
377 +                net_stats=realloc(net_stats, (sizeof(network_iface_stat_t)*needed_entries));
378 +                if(net_stats==NULL){
379 +                        return NULL;
380 +                }
381 +                network_iface_stat_init(*cur_entries, needed_entries, net_stats);
382 +                *cur_entries=needed_entries;
383 +        }
384 +
385 +        return net_stats;
386 + }
387 +
388 + network_iface_stat_t *get_network_iface_stats(int *entries){
389 +        static network_iface_stat_t *network_iface_stats;
390 +        network_iface_stat_t *network_iface_stat_ptr;
391 +        static int sizeof_network_iface_stats=0;        
392 +        int ifaces = 0;
393 +
394 + #ifdef SOLARIS
395 +        kstat_ctl_t *kc;
396 +        kstat_t *ksp;
397 +        kstat_named_t *knp;
398 +        int sock;
399 + #endif
400 + #ifdef ALLBSD
401 +        struct ifaddrs *net, *net_ptr;
402 +        struct ifmediareq ifmed;
403 +        struct ifreq ifr;
404 +        int sock;
405 +        int x;
406 + #endif
407 + #ifdef LINUX
408 +        FILE *f;
409 +        /* Horrible big enough, but it should be easily big enough */
410 +        char line[8096];
411 +        int sock;
412 + #endif
413 +
414 + #ifdef ALLBSD
415 +        if(getifaddrs(&net) != 0){
416 +                return NULL;
417 +        }
418 +
419 +        if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return NULL;
420 +
421 +        for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){
422 +                if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
423 +                network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
424 +                if(network_iface_stats==NULL){
425 +                        return NULL;
426 +                }
427 +                network_iface_stat_ptr = network_iface_stats + ifaces;
428 +
429 +                memset(&ifr, 0, sizeof(ifr));
430 +                strncpy(ifr.ifr_name, net_ptr->ifa_name, sizeof(ifr.ifr_name));
431 +
432 +                if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0){
433 +                        continue;
434 +                }      
435 +                if((ifr.ifr_flags & IFF_UP) != 0){
436 +                        network_iface_stat_ptr->up = 1;
437 +                }else{
438 +                        network_iface_stat_ptr->up = 0;
439 +                }
440 +
441 +                if (network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
442 +                network_iface_stat_ptr->interface_name = strdup(net_ptr->ifa_name);
443 +                if (network_iface_stat_ptr->interface_name == NULL) return NULL;
444 +
445 +                network_iface_stat_ptr->speed = 0;
446 +                network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
447 +                ifaces++;
448 +
449 +                memset(&ifmed, 0, sizeof(struct ifmediareq));
450 +                strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name));
451 +                if(ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){
452 +                        /* Not all interfaces support the media ioctls. */
453 +                        continue;
454 +                }
455 +
456 +                /* We may need to change this if we start doing wireless devices too */
457 +                if( (ifmed.ifm_active | IFM_ETHER) != ifmed.ifm_active ){
458 +                        /* Not a ETHER device */
459 +                        continue;
460 +                }
461 +
462 +                /* Only intrested in the first 4 bits)  - Assuming only ETHER devices */
463 +                x = ifmed.ifm_active & 0x0f;    
464 +                switch(x){
465 +                        /* 10 Mbit connections. Speedy :) */
466 +                        case(IFM_10_T):
467 +                        case(IFM_10_2):
468 +                        case(IFM_10_5):
469 +                        case(IFM_10_STP):
470 +                        case(IFM_10_FL):
471 +                                network_iface_stat_ptr->speed = 10;
472 +                                break;
473 +                        /* 100 Mbit conneections */
474 +                        case(IFM_100_TX):
475 +                        case(IFM_100_FX):
476 +                        case(IFM_100_T4):
477 +                        case(IFM_100_VG):
478 +                        case(IFM_100_T2):
479 +                                network_iface_stat_ptr->speed = 100;
480 +                                break;
481 +                        /* 1000 Mbit connections */
482 +                        case(IFM_1000_SX):
483 +                        case(IFM_1000_LX):
484 +                        case(IFM_1000_CX):
485 + #ifdef IFM_1000_TX
486 +                        case(IFM_1000_TX): /* FreeBSD 4 and others? */
487 + #endif
488 + #ifdef IFM_1000_FX
489 +                        case(IFM_1000_FX): /* FreeBSD 4 */
490 + #endif
491 + #ifdef IFM_1000_T
492 +                        case(IFM_1000_T): /* FreeBSD 5 */
493 + #endif
494 +                                network_iface_stat_ptr->speed = 1000;
495 +                                break;
496 +                        /* We don't know what it is */
497 +                        default:
498 +                                network_iface_stat_ptr->speed = 0;
499 +                                break;
500 +                }
501 +
502 +                if( (ifmed.ifm_active | IFM_FDX) == ifmed.ifm_active ){
503 +                        network_iface_stat_ptr->dup = FULL_DUPLEX;
504 +                }else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){
505 +                        network_iface_stat_ptr->dup = HALF_DUPLEX;
506 +                }else{
507 +                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
508 +                }
509 +
510 +        }      
511 +        freeifaddrs(net);
512 +        close(sock);
513 + #endif
514 +
515 + #ifdef SOLARIS
516 +        if ((kc = kstat_open()) == NULL) {
517 +                return NULL;
518 +        }
519 +
520 +        if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) {
521 +                return NULL;
522 +        }
523 +
524 +        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
525 +                if (!strcmp(ksp->ks_class, "net")) {
526 +                        struct ifreq ifr;
527 +
528 +                        kstat_read(kc, ksp, NULL);
529 +
530 +                        strncpy(ifr.ifr_name, ksp->ks_name, sizeof ifr.ifr_name);
531 +                        if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
532 +                                /* Not a network interface. */
533 +                                continue;
534 +                        }
535 +
536 +                        network_iface_stats = network_iface_stat_malloc(ifaces + 1, &sizeof_network_iface_stats, network_iface_stats);
537 +                        if (network_iface_stats == NULL) {
538 +                                return NULL;
539 +                        }
540 +                        network_iface_stat_ptr = network_iface_stats + ifaces;
541 +                        ifaces++;
542 +
543 +                        if (network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
544 +                        network_iface_stat_ptr->interface_name = strdup(ksp->ks_name);
545 +                        if (network_iface_stat_ptr->interface_name == NULL) return NULL;
546 +
547 +                        if ((ifr.ifr_flags & IFF_UP) != 0) {
548 +                                network_iface_stat_ptr->up = 1;
549 +                        } else {
550 +                                network_iface_stat_ptr->up = 1;
551 +                        }
552 +
553 +                        if ((knp = kstat_data_lookup(ksp, "ifspeed")) != NULL) {
554 +                                network_iface_stat_ptr->speed = knp->value.ui64 / (1000 * 1000);
555 +                        } else {
556 +                                network_iface_stat_ptr->speed = 0;
557 +                        }
558 +
559 +                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
560 +                        if ((knp = kstat_data_lookup(ksp, "link_duplex")) != NULL) {
561 +                                switch (knp->value.ui32) {
562 +                                case 1:
563 +                                        network_iface_stat_ptr->dup = HALF_DUPLEX;
564 +                                        break;
565 +                                case 2:
566 +                                        network_iface_stat_ptr->dup = FULL_DUPLEX;
567 +                                        break;
568 +                                }
569 +                        }
570 +                }
571 +        }
572 +
573 +        close(sock);
574 +        kstat_close(kc);
575 + #endif  
576 + #ifdef LINUX
577 +        f = fopen("/proc/net/dev", "r");
578 +        if(f == NULL){
579 +                return NULL;
580 +        }
581 +
582 +        /* Setup stuff so we can do the ioctl to get the info */
583 +        if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
584 +                return NULL;
585 +        }
586 +
587 +        /* Ignore first 2 lines.. Just headings */
588 +        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
589 +        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
590 +
591 +        while((fgets(line, sizeof(line), f)) != NULL){
592 +                char *name, *ptr;
593 +                struct ifreq ifr;
594 +                struct ethtool_cmd ethcmd;
595 +                int err;
596 +
597 +                /* Get the interface name */
598 +                ptr = strchr(line, ':');
599 +                if (ptr == NULL) continue;
600 +                *ptr='\0';
601 +                name = line;
602 +                while(isspace(*(name))){
603 +                        name++;
604 +                }
605 +
606 +                memset(&ifr, 0, sizeof ifr);
607 +                strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
608 +
609 +                if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
610 +                        continue;
611 +                }
612 +
613 +                /* We have a good interface to add */
614 +                network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
615 +                if(network_iface_stats==NULL){
616 +                        return NULL;
617 +                }
618 +                network_iface_stat_ptr = network_iface_stats + ifaces;
619 +                network_iface_stat_ptr->interface_name = strdup(name);
620 +                if ((ifr.ifr_flags & IFF_UP) != 0) {
621 +                        network_iface_stat_ptr->up = 1;
622 +                } else {
623 +                        network_iface_stat_ptr->up = 0;
624 +                }
625 +
626 +                memset(&ethcmd, 0, sizeof ethcmd);
627 +                ethcmd.cmd = ETHTOOL_GSET;
628 +                ifr.ifr_data = (caddr_t) &ethcmd;
629 +
630 +                err = ioctl(sock, SIOCETHTOOL, &ifr);
631 +                if (err == 0) {
632 +                        network_iface_stat_ptr->speed = ethcmd.speed;
633 +
634 +                        switch (ethcmd.duplex) {
635 +                        case 0x00:
636 +                                network_iface_stat_ptr->dup = FULL_DUPLEX;
637 +                                break;
638 +                        case 0x01:
639 +                                network_iface_stat_ptr->dup = HALF_DUPLEX;
640 +                                break;
641 +                        default:
642 +                                network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
643 +                        }
644 +                } else {
645 +                        /* Not all interfaces support the ethtool ioctl. */
646 +                        network_iface_stat_ptr->speed = 0;
647 +                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
648 +                }
649 +
650 +                ifaces++;
651 +        }
652 +        close(sock);
653 +        fclose(f);
654 + #endif
655 +        *entries = ifaces;
656 +        return network_iface_stats;
657 + }
658 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines