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.64 by tdb, Tue Apr 6 14:52:58 2004 UTC vs.
Revision 1.74 by tdb, Wed Jul 13 09:31:54 2005 UTC

# Line 45 | Line 45
45   #ifdef LINUX
46   #include <stdio.h>
47   #include <sys/types.h>
48 #include <regex.h>
48   #include <sys/ioctl.h>
49   #include <sys/socket.h>
50   #include <net/if.h>
# Line 53 | Line 52
52   /* Stuff which could be defined by defining KERNEL, but
53   * that would be a bad idea, so we'll just declare it here
54   */
55 + #include <linux/version.h>
56 + #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
57 + typedef __uint8_t __u8;
58 + typedef __uint16_t __u16;
59 + typedef __uint32_t __u32;
60 + typedef __uint64_t __u64;
61 + #else
62   typedef __uint8_t u8;
63   typedef __uint16_t u16;
64   typedef __uint32_t u32;
65   typedef __uint64_t u64;
66 + #endif
67   #include <linux/ethtool.h>
68   #include <linux/sockios.h>
69   #include <unistd.h>
# Line 87 | Line 94 | static void network_stat_destroy(sg_network_io_stats *
94   }
95  
96   VECTOR_DECLARE_STATIC(network_stats, sg_network_io_stats, 5,
97 <                      network_stat_init, network_stat_destroy);
97 >                      network_stat_init, network_stat_destroy);
98  
99   sg_network_io_stats *sg_get_network_io_stats(int *entries){
100          int interfaces;
101          sg_network_io_stats *network_stat_ptr;
102  
103   #ifdef SOLARIS
104 <        kstat_ctl_t *kc;
105 <        kstat_t *ksp;
104 >        kstat_ctl_t *kc;
105 >        kstat_t *ksp;
106          kstat_named_t *knp;
107   #endif
108  
# Line 113 | Line 120 | sg_network_io_stats *sg_get_network_io_stats(int *entr
120  
121   #ifdef ALLBSD
122          if(getifaddrs(&net) != 0){
123 +                sg_set_error_with_errno(SG_ERROR_GETIFADDRS, NULL);
124                  return NULL;
125          }
126  
# Line 127 | Line 135 | sg_network_io_stats *sg_get_network_io_stats(int *entr
135                  network_stat_ptr=network_stats+interfaces;
136                  
137                  if (sg_update_string(&network_stat_ptr->interface_name,
138 <                                     net_ptr->ifa_name) < 0) {
138 >                                     net_ptr->ifa_name) < 0) {
139                          return NULL;
140                  }
141                  net_data=(struct if_data *)net_ptr->ifa_data;
# Line 145 | Line 153 | sg_network_io_stats *sg_get_network_io_stats(int *entr
153   #endif
154  
155   #ifdef SOLARIS
156 <        if ((kc = kstat_open()) == NULL) {
157 <                return NULL;
158 <        }
156 >        if ((kc = kstat_open()) == NULL) {
157 >                sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
158 >                return NULL;
159 >        }
160  
161          interfaces=0;
162  
163 <        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
164 <                if (!strcmp(ksp->ks_class, "net")) {
165 <                        kstat_read(kc, ksp, NULL);
163 >        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
164 >                if (!strcmp(ksp->ks_class, "net")) {
165 >                        kstat_read(kc, ksp, NULL);
166  
167   #ifdef SOL7
168   #define LRX "rbytes"
# Line 227 | Line 236 | sg_network_io_stats *sg_get_network_io_stats(int *entr
236  
237                          /* Read interface name */
238                          if (sg_update_string(&network_stat_ptr->interface_name,
239 <                                             ksp->ks_name) < 0) {
239 >                                             ksp->ks_name) < 0) {
240                                  return NULL;
241                          }
242  
# Line 243 | Line 252 | sg_network_io_stats *sg_get_network_io_stats(int *entr
252   #ifdef LINUX
253          f=fopen("/proc/net/dev", "r");
254          if(f==NULL){
255 +                sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/net/dev");
256                  return NULL;
257          }
258          /* read the 2 lines.. Its the title, so we dont care :) */
# Line 251 | Line 261 | sg_network_io_stats *sg_get_network_io_stats(int *entr
261  
262  
263          if((regcomp(&regex, "^ *([^:]+): *([0-9]+) +([0-9]+) +([0-9]+) +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +([0-9]+) +([0-9]+) +([0-9]+) +[0-9]+ +[0-9]+ +([0-9]+)", REG_EXTENDED))!=0){
264 +                sg_set_error(SG_ERROR_PARSE, NULL);
265                  return NULL;
266          }
267  
# Line 264 | Line 275 | sg_network_io_stats *sg_get_network_io_stats(int *entr
275                  if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) {
276                          return NULL;
277                  }
278 <                network_stat_ptr=network_stats+interfaces;
278 >                network_stat_ptr=network_stats+interfaces;
279  
280                  if(network_stat_ptr->interface_name!=NULL){
281                          free(network_stat_ptr->interface_name);
# Line 288 | Line 299 | sg_network_io_stats *sg_get_network_io_stats(int *entr
299   #endif
300  
301   #ifdef CYGWIN
302 +        sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin");
303          return NULL;
304   #endif
305 + #ifdef HPUX
306 +        sg_set_error(SG_ERROR_UNSUPPORTED, "HP-UX");
307 +        return NULL;
308 + #endif
309  
310          *entries=interfaces;
311  
# Line 313 | Line 329 | static long long transfer_diff(long long new, long lon
329  
330   sg_network_io_stats *sg_get_network_io_stats_diff(int *entries) {
331          VECTOR_DECLARE_STATIC(diff, sg_network_io_stats, 1,
332 <                              network_stat_init, network_stat_destroy);
332 >                              network_stat_init, network_stat_destroy);
333          sg_network_io_stats *src = NULL, *dest;
334          int i, j, diff_count, new_count;
335  
# Line 334 | Line 350 | sg_network_io_stats *sg_get_network_io_stats_diff(int
350                  dest = &diff[i];
351  
352                  if (sg_update_string(&dest->interface_name,
353 <                                     src->interface_name) < 0) {
353 >                                     src->interface_name) < 0) {
354                          return NULL;
355                  }
356                  dest->rx = src->rx;
# Line 386 | Line 402 | sg_network_io_stats *sg_get_network_io_stats_diff(int
402          return diff;
403   }
404  
405 + int sg_network_io_compare_name(const void *va, const void *vb) {
406 +        const sg_network_io_stats *a = (const sg_network_io_stats *)va;
407 +        const sg_network_io_stats *b = (const sg_network_io_stats *)vb;
408 +
409 +        return strcmp(a->interface_name, b->interface_name);
410 + }
411 +
412   /* NETWORK INTERFACE STATS */
413  
414   static void network_iface_stat_init(sg_network_iface_stats *s) {
415          s->interface_name = NULL;
416          s->speed = 0;
417 <        s->dup = SG_IFACE_DUPLEX_UNKNOWN;
417 >        s->duplex = SG_IFACE_DUPLEX_UNKNOWN;
418   }
419  
420   static void network_iface_stat_destroy(sg_network_iface_stats *s) {
# Line 400 | Line 423 | static void network_iface_stat_destroy(sg_network_ifac
423  
424   sg_network_iface_stats *sg_get_network_iface_stats(int *entries){
425          VECTOR_DECLARE_STATIC(network_iface_stats, sg_network_iface_stats, 5,
426 <                              network_iface_stat_init, network_iface_stat_destroy);
426 >                              network_iface_stat_init, network_iface_stat_destroy);
427          sg_network_iface_stats *network_iface_stat_ptr;
428          int ifaces = 0;
429  
430   #ifdef SOLARIS
431 <        kstat_ctl_t *kc;
432 <        kstat_t *ksp;
431 >        kstat_ctl_t *kc;
432 >        kstat_t *ksp;
433          kstat_named_t *knp;
434          int sock;
435   #endif
436   #ifdef ALLBSD
437 <        struct ifaddrs *net, *net_ptr;
437 >        struct ifaddrs *net, *net_ptr;
438          struct ifmediareq ifmed;
439          struct ifreq ifr;
440          int sock;
441          int x;
442   #endif
443   #ifdef LINUX
444 <        FILE *f;
445 <        /* Horrible big enough, but it should be easily big enough */
446 <        char line[8096];
444 >        FILE *f;
445 >        /* Horrible big enough, but it should be easily big enough */
446 >        char line[8096];
447          int sock;
448   #endif
449  
450   #ifdef ALLBSD
451 <        if(getifaddrs(&net) != 0){
452 <                return NULL;
453 <        }
451 >        if(getifaddrs(&net) != 0){
452 >                sg_set_error_with_errno(SG_ERROR_GETIFADDRS, NULL);
453 >                return NULL;
454 >        }
455  
456          if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return NULL;
457  
458          for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){
459 <                if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
459 >                if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
460  
461                  if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) {
462 <                        return NULL;
463 <                }
464 <                network_iface_stat_ptr = network_iface_stats + ifaces;
462 >                        return NULL;
463 >                }
464 >                network_iface_stat_ptr = network_iface_stats + ifaces;
465  
466                  memset(&ifr, 0, sizeof(ifr));
467                  strncpy(ifr.ifr_name, net_ptr->ifa_name, sizeof(ifr.ifr_name));
# Line 452 | Line 476 | sg_network_iface_stats *sg_get_network_iface_stats(int
476                  }
477  
478                  if (sg_update_string(&network_iface_stat_ptr->interface_name,
479 <                                     net_ptr->ifa_name) < 0) {
479 >                                     net_ptr->ifa_name) < 0) {
480                          return NULL;
481                  }
482  
483                  network_iface_stat_ptr->speed = 0;
484 <                network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_UNKNOWN;
484 >                network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
485                  ifaces++;
486  
487                  memset(&ifmed, 0, sizeof(struct ifmediareq));
# Line 514 | Line 538 | sg_network_iface_stats *sg_get_network_iface_stats(int
538                  }
539  
540                  if( (ifmed.ifm_active | IFM_FDX) == ifmed.ifm_active ){
541 <                        network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_FULL;
541 >                        network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_FULL;
542                  }else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){
543 <                        network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_HALF;
543 >                        network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_HALF;
544                  }else{
545 <                        network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_UNKNOWN;
545 >                        network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
546                  }
547  
548          }      
# Line 528 | Line 552 | sg_network_iface_stats *sg_get_network_iface_stats(int
552  
553   #ifdef SOLARIS
554          if ((kc = kstat_open()) == NULL) {
555 +                sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
556                  return NULL;
557          }
558  
559          if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) {
560 +                sg_set_error_with_errno(SG_ERROR_SOCKET, NULL);
561                  return NULL;
562          }
563  
# Line 554 | Line 580 | sg_network_iface_stats *sg_get_network_iface_stats(int
580                          ifaces++;
581  
582                          if (sg_update_string(&network_iface_stat_ptr->interface_name,
583 <                                             ksp->ks_name) < 0) {
583 >                                             ksp->ks_name) < 0) {
584                                  return NULL;
585                          }
586  
# Line 570 | Line 596 | sg_network_iface_stats *sg_get_network_iface_stats(int
596                                  network_iface_stat_ptr->speed = 0;
597                          }
598  
599 <                        network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_UNKNOWN;
599 >                        network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
600                          if ((knp = kstat_data_lookup(ksp, "link_duplex")) != NULL) {
601                                  switch (knp->value.ui32) {
602                                  case 1:
603 <                                        network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_HALF;
603 >                                        network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_HALF;
604                                          break;
605                                  case 2:
606 <                                        network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_FULL;
606 >                                        network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_FULL;
607                                          break;
608                                  }
609                          }
# Line 589 | Line 615 | sg_network_iface_stats *sg_get_network_iface_stats(int
615   #endif  
616   #ifdef LINUX
617          f = fopen("/proc/net/dev", "r");
618 <        if(f == NULL){
619 <                return NULL;
620 <        }
618 >        if(f == NULL){
619 >                sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/net/dev");
620 >                return NULL;
621 >        }
622  
623          /* Setup stuff so we can do the ioctl to get the info */
624          if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
625 +                sg_set_error_with_errno(SG_ERROR_SOCKET, NULL);
626                  return NULL;
627          }
628  
629          /* Ignore first 2 lines.. Just headings */
630 <        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
631 <        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
630 >        if((fgets(line, sizeof(line), f)) == NULL) {
631 >                sg_set_error(SG_ERROR_PARSE, NULL);
632 >                return NULL;
633 >        }
634 >        if((fgets(line, sizeof(line), f)) == NULL) {
635 >                sg_set_error(SG_ERROR_PARSE, NULL);
636 >                return NULL;
637 >        }
638  
639 <        while((fgets(line, sizeof(line), f)) != NULL){
640 <                char *name, *ptr;
641 <                struct ifreq ifr;
642 <                struct ethtool_cmd ethcmd;
643 <                int err;
639 >        while((fgets(line, sizeof(line), f)) != NULL){
640 >                char *name, *ptr;
641 >                struct ifreq ifr;
642 >                struct ethtool_cmd ethcmd;
643 >                int err;
644  
645                  /* Get the interface name */
646 <                ptr = strchr(line, ':');
647 <                if (ptr == NULL) continue;
648 <                *ptr='\0';
649 <                name = line;
650 <                while(isspace(*(name))){
651 <                        name++;
652 <                }
646 >                ptr = strchr(line, ':');
647 >                if (ptr == NULL) continue;
648 >                *ptr='\0';
649 >                name = line;
650 >                while(isspace(*(name))){
651 >                        name++;
652 >                }
653  
654 <                memset(&ifr, 0, sizeof ifr);
655 <                strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
654 >                memset(&ifr, 0, sizeof ifr);
655 >                strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
656  
657                  if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
658                          continue;
# Line 631 | Line 665 | sg_network_iface_stats *sg_get_network_iface_stats(int
665                  network_iface_stat_ptr = network_iface_stats + ifaces;
666                  
667                  if (sg_update_string(&network_iface_stat_ptr->interface_name,
668 <                                     name) < 0) {
668 >                                     name) < 0) {
669                          return NULL;
670                  }
671                  if ((ifr.ifr_flags & IFF_UP) != 0) {
# Line 640 | Line 674 | sg_network_iface_stats *sg_get_network_iface_stats(int
674                          network_iface_stat_ptr->up = 0;
675                  }
676  
677 <                memset(&ethcmd, 0, sizeof ethcmd);
678 <                ethcmd.cmd = ETHTOOL_GSET;
679 <                ifr.ifr_data = (caddr_t) &ethcmd;
677 >                memset(&ethcmd, 0, sizeof ethcmd);
678 >                ethcmd.cmd = ETHTOOL_GSET;
679 >                ifr.ifr_data = (caddr_t) &ethcmd;
680  
681 <                err = ioctl(sock, SIOCETHTOOL, &ifr);
682 <                if (err == 0) {
681 >                err = ioctl(sock, SIOCETHTOOL, &ifr);
682 >                if (err == 0) {
683                          network_iface_stat_ptr->speed = ethcmd.speed;
684  
685                          switch (ethcmd.duplex) {
686 <                        case 0x00:
687 <                                network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_FULL;
686 >                        case DUPLEX_FULL:
687 >                                network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_FULL;
688                                  break;
689 <                        case 0x01:
690 <                                network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_HALF;
689 >                        case DUPLEX_HALF:
690 >                                network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_HALF;
691                                  break;
692                          default:
693 <                                network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_UNKNOWN;
693 >                                network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
694                          }
695                  } else {
696                          /* Not all interfaces support the ethtool ioctl. */
697                          network_iface_stat_ptr->speed = 0;
698 <                        network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_UNKNOWN;
698 >                        network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
699                  }
700  
701                  ifaces++;
# Line 669 | Line 703 | sg_network_iface_stats *sg_get_network_iface_stats(int
703          close(sock);
704          fclose(f);
705   #endif
706 + #ifdef CYGWIN
707 +        sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin");
708 +        return NULL;
709 + #endif
710 + #ifdef HPUX
711 +        sg_set_error(SG_ERROR_UNSUPPORTED, "HP-UX");
712 +        return NULL;
713 + #endif
714 +
715 + #ifdef SG_ENABLE_DEPRECATED
716 +        network_iface_stat_ptr->dup = network_iface_stat_ptr->duplex;
717 + #endif
718 +
719          *entries = ifaces;
720          return network_iface_stats;
721 + }
722 +
723 + int sg_network_iface_compare_name(const void *va, const void *vb) {
724 +        const sg_network_iface_stats *a = (const sg_network_iface_stats *)va;
725 +        const sg_network_iface_stats *b = (const sg_network_iface_stats *)vb;
726 +
727 +        return strcmp(a->interface_name, b->interface_name);
728   }
729  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines