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.43 by ats, Sat Feb 14 15:48:42 2004 UTC vs.
Revision 1.44 by ats, Sat Feb 14 16:58:19 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>
# Line 174 | Line 180 | network_stat_t *get_network_stats(int *entries){
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  
# Line 186 | Line 195 | network_stat_t *get_network_stats(int *entries){
195                          network_stat_ptr->rx=knp->VALTYPE;
196  
197                          if((knp=kstat_data_lookup(ksp, WLOOKUP))==NULL){
189                                /* Not a network interface, so skip to the next entry */
198                                  continue;
199                          }
200                          network_stat_ptr->tx=knp->VALTYPE;
# Line 382 | Line 390 | network_iface_stat_t *get_network_iface_stats(int *ent
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;
# Line 496 | Line 505 | network_iface_stat_t *get_network_iface_stats(int *ent
505   #endif
506  
507   #ifdef SOLARIS
508 <        if ((kc = kstat_open()) == NULL) {
509 <                return NULL;
510 <        }
508 >        if ((kc = kstat_open()) == NULL) {
509 >                return NULL;
510 >        }
511  
512 <        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
513 <                if (!strcmp(ksp->ks_class, "net")) {
514 <                        kstat_read(kc, ksp, NULL);
515 <                        if((knp=kstat_data_lookup(ksp, "ifspeed"))==NULL){
516 <                                /* Not a network interface, so skip to the next entry */
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 <                        network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
528 <                        if(network_iface_stats==NULL){
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 <                        network_iface_stat_ptr->speed = knp->value.ui64 / (1000*1000);
533 >                        ifaces++;
534  
535 <                        if((knp=kstat_data_lookup(ksp, "link_up"))==NULL){
536 <                                /* Not a network interface, so skip to the next entry */
537 <                                continue;
520 <                        }
521 <                        /* Solaris has 1 for up, and 0 for not. As we do too */
522 <                        network_iface_stat_ptr->up = knp->value.ui32;
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((knp=kstat_data_lookup(ksp, "link_duplex"))==NULL){
540 <                                /* Not a network interface, so skip to the next entry */
541 <                                continue;
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 <                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
546 <                        if(knp->value.ui32 == 2){
547 <                                network_iface_stat_ptr->dup = FULL_DUPLEX;
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                          }
533                        if(knp->value.ui32 == 1){
534                                network_iface_stat_ptr->dup = HALF_DUPLEX;
535                        }
550  
551 <                        if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
552 <                        network_iface_stat_ptr->interface_name = strdup(ksp->ks_name);
553 <                        if(network_iface_stat_ptr->interface_name == NULL) return NULL;
554 <                        ifaces++;
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);
544        
567   #endif  
568   #ifdef LINUX
569          f = fopen("/proc/net/dev", "r");

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines