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.45 by tdb, Mon Feb 16 14:55:32 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 254 | Line 262 | network_stat_t *get_network_stats(int *entries){
262   }
263  
264   long long transfer_diff(long long new, long long old){
265 < #if defined(SOL7) || defined(LINUX) || defined(FREEBSD)
265 > #if defined(SOL7) || defined(LINUX) || defined(FREEBSD) || defined(DFBSD)
266   #define MAXVAL 4294967296LL
267   #else
268   #define MAXVAL 18446744073709551616LL
# 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 468 | Line 477 | network_iface_stat_t *get_network_iface_stats(int *ent
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):
473 <                        case(IFM_1000_FX):
474 < #else
475 <                        case(IFM_1000_T):
480 > #ifdef IFM_1000_TX
481 >                        case(IFM_1000_TX): /* FreeBSD 4 and others? */
482   #endif
483 + #ifdef IFM_1000_FX
484 +                        case(IFM_1000_FX): /* FreeBSD 4 */
485 + #endif
486 + #ifdef IFM_1000_T
487 +                        case(IFM_1000_T): /* FreeBSD 5 */
488 + #endif
489                                  network_iface_stat_ptr->speed = 1000;
490                                  break;
491                          /* We don't know what it is */
# Line 496 | Line 508 | network_iface_stat_t *get_network_iface_stats(int *ent
508   #endif
509  
510   #ifdef SOLARIS
511 <        if ((kc = kstat_open()) == NULL) {
512 <                return NULL;
513 <        }
511 >        if ((kc = kstat_open()) == NULL) {
512 >                return NULL;
513 >        }
514  
515 <        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
516 <                if (!strcmp(ksp->ks_class, "net")) {
517 <                        kstat_read(kc, ksp, NULL);
518 <                        if((knp=kstat_data_lookup(ksp, "ifspeed"))==NULL){
519 <                                /* Not a network interface, so skip to the next entry */
515 >        if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) {
516 >                return NULL;
517 >        }
518 >
519 >        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
520 >                if (!strcmp(ksp->ks_class, "net")) {
521 >                        struct ifreq ifr;
522 >
523 >                        kstat_read(kc, ksp, NULL);
524 >
525 >                        strncpy(ifr.ifr_name, ksp->ks_name, sizeof ifr.ifr_name);
526 >                        if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
527 >                                /* Not a network interface. */
528                                  continue;
529                          }
530 <                        network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
531 <                        if(network_iface_stats==NULL){
530 >
531 >                        network_iface_stats = network_iface_stat_malloc(ifaces + 1, &sizeof_network_iface_stats, network_iface_stats);
532 >                        if (network_iface_stats == NULL) {
533                                  return NULL;
534                          }
535                          network_iface_stat_ptr = network_iface_stats + ifaces;
536 <                        network_iface_stat_ptr->speed = knp->value.ui64 / (1000*1000);
536 >                        ifaces++;
537  
538 <                        if((knp=kstat_data_lookup(ksp, "link_up"))==NULL){
539 <                                /* Not a network interface, so skip to the next entry */
540 <                                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;
538 >                        if (network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
539 >                        network_iface_stat_ptr->interface_name = strdup(ksp->ks_name);
540 >                        if (network_iface_stat_ptr->interface_name == NULL) return NULL;
541  
542 <                        if((knp=kstat_data_lookup(ksp, "link_duplex"))==NULL){
543 <                                /* Not a network interface, so skip to the next entry */
544 <                                continue;
542 >                        if ((ifr.ifr_flags & IFF_UP) != 0) {
543 >                                network_iface_stat_ptr->up = 1;
544 >                        } else {
545 >                                network_iface_stat_ptr->up = 1;
546                          }
547  
548 <                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
549 <                        if(knp->value.ui32 == 2){
550 <                                network_iface_stat_ptr->dup = FULL_DUPLEX;
548 >                        if ((knp = kstat_data_lookup(ksp, "ifspeed")) != NULL) {
549 >                                network_iface_stat_ptr->speed = knp->value.ui64 / (1000 * 1000);
550 >                        } else {
551 >                                network_iface_stat_ptr->speed = 0;
552                          }
533                        if(knp->value.ui32 == 1){
534                                network_iface_stat_ptr->dup = HALF_DUPLEX;
535                        }
553  
554 <                        if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
555 <                        network_iface_stat_ptr->interface_name = strdup(ksp->ks_name);
556 <                        if(network_iface_stat_ptr->interface_name == NULL) return NULL;
557 <                        ifaces++;
554 >                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
555 >                        if ((knp = kstat_data_lookup(ksp, "link_duplex")) != NULL) {
556 >                                switch (knp->value.ui32) {
557 >                                case 1:
558 >                                        network_iface_stat_ptr->dup = HALF_DUPLEX;
559 >                                        break;
560 >                                case 2:
561 >                                        network_iface_stat_ptr->dup = FULL_DUPLEX;
562 >                                        break;
563 >                                }
564 >                        }
565                  }
566          }
567 +
568 +        close(sock);
569          kstat_close(kc);
544        
570   #endif  
571   #ifdef LINUX
572          f = fopen("/proc/net/dev", "r");

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines