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.33 by pajs, Fri Feb 13 15:29:16 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 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 173 | 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 185 | 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){
188                                /* Not a network interface, so skip to the next entry */
198                                  continue;
199                          }
200                          network_stat_ptr->tx=knp->VALTYPE;
# Line 253 | 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 375 | Line 384 | network_iface_stat_t *get_network_iface_stats(int *ent
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 <        static int ifaces;
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 <        int s;
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];
395        void *eth_tool_cmd_buf;
396        int buf_size;
406          int sock;
407   #endif
408 <        ifaces = 0;
408 >
409   #ifdef ALLBSD
410          if(getifaddrs(&net) != 0){
411                  return NULL;
412          }
413  
414 <        if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == NULL) return NULL;
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;
# Line 412 | Line 421 | network_iface_stat_t *get_network_iface_stats(int *ent
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(s, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){
446 >                if(ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){
447 >                        /* Not all interfaces support the media ioctls. */
448                          continue;
449                  }
450  
# Line 424 | Line 454 | network_iface_stat_t *get_network_iface_stats(int *ent
454                          continue;
455                  }
456  
427                if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
428                network_iface_stat_ptr->interface_name = strdup(net_ptr->ifa_name);
429                if(network_iface_stat_ptr->interface_name == NULL) return NULL;
430
457                  /* Only intrested in the first 4 bits)  - Assuming only ETHER devices */
458                  x = ifmed.ifm_active & 0x0f;    
459                  switch(x){
# Line 451 | 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):
456 <                        case(IFM_1000_FX):
457 < #else
458 <                        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 472 | Line 501 | network_iface_stat_t *get_network_iface_stats(int *ent
501                  }else{
502                          network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
503                  }
504 <                ifaces++;
504 >
505          }      
506          freeifaddrs(net);
507 <        close(s);
507 >        close(sock);
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_duplex"))==NULL){
539 <                                /* Not a network interface, so skip to the next entry */
540 <                                continue;
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 ((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.ui64 == 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                          }
509                        if(knp->value.ui64 == 1){
510                                network_iface_stat_ptr->dup = HALF_DUPLEX;
511                        }
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);
520        
570   #endif  
571   #ifdef LINUX
523
572          f = fopen("/proc/net/dev", "r");
573          if(f == NULL){
574                  return NULL;
# Line 531 | Line 579 | network_iface_stat_t *get_network_iface_stats(int *ent
579                  return NULL;
580          }
581  
534        buf_size = sizeof(struct ethtool_cmd);
535        eth_tool_cmd_buf = malloc(buf_size);
536        if(eth_tool_cmd_buf == NULL) return NULL;
537
582          /* Ignore first 2 lines.. Just headings */
583          if((fgets(line, sizeof(line), f)) == NULL) return NULL;
584          if((fgets(line, sizeof(line), f)) == NULL) return NULL;
# Line 542 | Line 586 | network_iface_stat_t *get_network_iface_stats(int *ent
586          while((fgets(line, sizeof(line), f)) != NULL){
587                  char *name, *ptr;
588                  struct ifreq ifr;
589 <                struct ethtool_cmd *ethcmd;
589 >                struct ethtool_cmd ethcmd;
590                  int err;
591  
592                  /* Get the interface name */
# Line 554 | Line 598 | network_iface_stat_t *get_network_iface_stats(int *ent
598                          name++;
599                  }
600  
601 <                memset(&ifr, 0, sizeof(ifr));
602 <                memset(eth_tool_cmd_buf, 0, buf_size);
559 <                ifr.ifr_data = (caddr_t) eth_tool_cmd_buf;
560 <                strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
601 >                memset(&ifr, 0, sizeof ifr);
602 >                strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
603  
604 <                ethcmd = (struct ethtool_cmd *) ifr.ifr_data;
605 <                ethcmd->cmd = ETHTOOL_GSET;
604 >                if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
605 >                        continue;
606 >                }
607  
565                err = ioctl(sock, SIOCETHTOOL, &ifr);
566                if(err < 0){
567                        /* This could fail if the interface doesn't support the command. Carry
568                         * on to the next :)
569                         */
570                        continue;
571                }
572
608                  /* We have a good interface to add */
609                  network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
610                  if(network_iface_stats==NULL){
# Line 577 | Line 612 | network_iface_stat_t *get_network_iface_stats(int *ent
612                  }
613                  network_iface_stat_ptr = network_iface_stats + ifaces;
614                  network_iface_stat_ptr->interface_name = strdup(name);
615 <                network_iface_stat_ptr->speed = ethcmd->speed;
616 <                network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
617 <                if(ethcmd->duplex == 0x00){
618 <                        network_iface_stat_ptr->dup = FULL_DUPLEX;
615 >                if ((ifr.ifr_flags & IFF_UP) != 0) {
616 >                        network_iface_stat_ptr->up = 1;
617 >                } else {
618 >                        network_iface_stat_ptr->up = 0;
619                  }
620 <                if(ethcmd->duplex == 0x01){
621 <                        network_iface_stat_ptr->dup = HALF_DUPLEX;
620 >
621 >                memset(&ethcmd, 0, sizeof ethcmd);
622 >                ethcmd.cmd = ETHTOOL_GSET;
623 >                ifr.ifr_data = (caddr_t) &ethcmd;
624 >
625 >                err = ioctl(sock, SIOCETHTOOL, &ifr);
626 >                if (err == 0) {
627 >                        network_iface_stat_ptr->speed = ethcmd.speed;
628 >
629 >                        switch (ethcmd.duplex) {
630 >                        case 0x00:
631 >                                network_iface_stat_ptr->dup = FULL_DUPLEX;
632 >                                break;
633 >                        case 0x01:
634 >                                network_iface_stat_ptr->dup = HALF_DUPLEX;
635 >                                break;
636 >                        default:
637 >                                network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
638 >                        }
639 >                } else {
640 >                        /* Not all interfaces support the ethtool ioctl. */
641 >                        network_iface_stat_ptr->speed = 0;
642 >                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
643                  }
644 +
645                  ifaces++;
646          }
647          close(sock);
648 <        free(eth_tool_cmd_buf);
648 >        fclose(f);
649   #endif
650          *entries = ifaces;
651          return network_iface_stats;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines