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.39 by ats, Sat Feb 14 00:29:29 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>
# Line 48 | Line 54
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>
# Line 144 | 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 173 | 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 185 | 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){
188                                /* Not a network interface, so skip to the next entry */
203                                  continue;
204                          }
205                          network_stat_ptr->tx=knp->VALTYPE;
# Line 253 | 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 375 | Line 389 | network_iface_stat_t *get_network_iface_stats(int *ent
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 <        static int ifaces;
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;
# Line 395 | Line 410 | network_iface_stat_t *get_network_iface_stats(int *ent
410          char line[8096];
411          int sock;
412   #endif
413 <        ifaces = 0;
413 >
414   #ifdef ALLBSD
415          if(getifaddrs(&net) != 0){
416                  return NULL;
# Line 411 | Line 426 | network_iface_stat_t *get_network_iface_stats(int *ent
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  
# Line 423 | Line 459 | network_iface_stat_t *get_network_iface_stats(int *ent
459                          continue;
460                  }
461  
426                if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
427                network_iface_stat_ptr->interface_name = strdup(net_ptr->ifa_name);
428                if(network_iface_stat_ptr->interface_name == NULL) return NULL;
429
462                  /* Only intrested in the first 4 bits)  - Assuming only ETHER devices */
463                  x = ifmed.ifm_active & 0x0f;    
464                  switch(x){
# Line 450 | Line 482 | network_iface_stat_t *get_network_iface_stats(int *ent
482                          case(IFM_1000_SX):
483                          case(IFM_1000_LX):
484                          case(IFM_1000_CX):
485 < #if defined(FREEBSD) && !defined(FREEBSD5)
486 <                        case(IFM_1000_TX):
455 <                        case(IFM_1000_FX):
456 < #else
457 <                        case(IFM_1000_T):
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 */
# Line 472 | Line 507 | network_iface_stat_t *get_network_iface_stats(int *ent
507                          network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
508                  }
509  
475                memset(&ifr, 0, sizeof(ifr));
476                strncpy(ifr.ifr_name, net_ptr->ifa_name, sizeof(ifr.ifr_name));
477
478                if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0){
479                        continue;
480                }      
481                if((ifr.ifr_flags & IFF_UP) != 0){
482                        network_iface_stat_ptr->up = 1;
483                }else{
484                        network_iface_stat_ptr->up = 0;
485                }
486
487                ifaces++;
510          }      
511          freeifaddrs(net);
512          close(sock);
513   #endif
514  
515   #ifdef SOLARIS
516 <        if ((kc = kstat_open()) == NULL) {
517 <                return NULL;
518 <        }
516 >        if ((kc = kstat_open()) == NULL) {
517 >                return NULL;
518 >        }
519  
520 <        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
521 <                if (!strcmp(ksp->ks_class, "net")) {
522 <                        kstat_read(kc, ksp, NULL);
523 <                        if((knp=kstat_data_lookup(ksp, "ifspeed"))==NULL){
524 <                                /* Not a network interface, so skip to the next entry */
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 <                        network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
536 <                        if(network_iface_stats==NULL){
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 <                        network_iface_stat_ptr->speed = knp->value.ui64 / (1000*1000);
541 >                        ifaces++;
542  
543 <                        if((knp=kstat_data_lookup(ksp, "link_up"))==NULL){
544 <                                /* Not a network interface, so skip to the next entry */
545 <                                continue;
515 <                        }
516 <                        /* Solaris has 1 for up, and 0 for not. As we do too */
517 <                        network_iface_stat_ptr->up = value.ui32;
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((knp=kstat_data_lookup(ksp, "link_duplex"))==NULL){
548 <                                /* Not a network interface, so skip to the next entry */
549 <                                continue;
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 <                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
554 <                        if(knp->value.ui32 == 2){
555 <                                network_iface_stat_ptr->dup = FULL_DUPLEX;
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                          }
528                        if(knp->value.ui32 == 1){
529                                network_iface_stat_ptr->dup = HALF_DUPLEX;
530                        }
558  
559 <                        if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
560 <                        network_iface_stat_ptr->interface_name = strdup(ksp->ks_name);
561 <                        if(network_iface_stat_ptr->interface_name == NULL) return NULL;
562 <                        ifaces++;
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);
539        
575   #endif  
576   #ifdef LINUX
577          f = fopen("/proc/net/dev", "r");

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines