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.37 by ats, Sat Feb 14 00:08:51 2004 UTC vs.
Revision 1.38 by ats, Sat Feb 14 00:27:58 2004 UTC

# Line 393 | Line 393 | network_iface_stat_t *get_network_iface_stats(int *ent
393          FILE *f;
394          /* Horrible big enough, but it should be easily big enough */
395          char line[8096];
396        void *eth_tool_cmd_buf;
397        int buf_size;
396          int sock;
397   #endif
398          ifaces = 0;
# Line 541 | Line 539 | network_iface_stat_t *get_network_iface_stats(int *ent
539          
540   #endif  
541   #ifdef LINUX
544
542          f = fopen("/proc/net/dev", "r");
543          if(f == NULL){
544                  return NULL;
# Line 552 | Line 549 | network_iface_stat_t *get_network_iface_stats(int *ent
549                  return NULL;
550          }
551  
555        buf_size = sizeof(struct ethtool_cmd);
556        eth_tool_cmd_buf = malloc(buf_size);
557        if(eth_tool_cmd_buf == NULL) return NULL;
558
552          /* Ignore first 2 lines.. Just headings */
553          if((fgets(line, sizeof(line), f)) == NULL) return NULL;
554          if((fgets(line, sizeof(line), f)) == NULL) return NULL;
# Line 563 | Line 556 | network_iface_stat_t *get_network_iface_stats(int *ent
556          while((fgets(line, sizeof(line), f)) != NULL){
557                  char *name, *ptr;
558                  struct ifreq ifr;
559 <                struct ethtool_cmd *ethcmd;
559 >                struct ethtool_cmd ethcmd;
560                  int err;
561  
562                  /* Get the interface name */
# Line 575 | Line 568 | network_iface_stat_t *get_network_iface_stats(int *ent
568                          name++;
569                  }
570  
571 <                memset(&ifr, 0, sizeof(ifr));
572 <                memset(eth_tool_cmd_buf, 0, buf_size);
580 <                ifr.ifr_data = (caddr_t) eth_tool_cmd_buf;
581 <                strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
571 >                memset(&ifr, 0, sizeof ifr);
572 >                strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
573  
574 <                ethcmd = (struct ethtool_cmd *) ifr.ifr_data;
575 <                ethcmd->cmd = ETHTOOL_GSET;
574 >                if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
575 >                        continue;
576 >                }
577  
586                err = ioctl(sock, SIOCETHTOOL, &ifr);
587                if(err < 0){
588                        /* This could fail if the interface doesn't support the command. Carry
589                         * on to the next :)
590                         */
591                        continue;
592                }
593
578                  /* We have a good interface to add */
579                  network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
580                  if(network_iface_stats==NULL){
# Line 598 | Line 582 | network_iface_stat_t *get_network_iface_stats(int *ent
582                  }
583                  network_iface_stat_ptr = network_iface_stats + ifaces;
584                  network_iface_stat_ptr->interface_name = strdup(name);
585 <                network_iface_stat_ptr->speed = ethcmd->speed;
602 <                if((ifr.ifr_flags & IFF_UP) != 0){
585 >                if ((ifr.ifr_flags & IFF_UP) != 0) {
586                          network_iface_stat_ptr->up = 1;
587 <                }else{
587 >                } else {
588                          network_iface_stat_ptr->up = 0;
589                  }
590  
591 <                network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
592 <                if(ethcmd->duplex == 0x00){
593 <                        network_iface_stat_ptr->dup = FULL_DUPLEX;
591 >                memset(&ethcmd, 0, sizeof ethcmd);
592 >                ethcmd.cmd = ETHTOOL_GSET;
593 >                ifr.ifr_data = (caddr_t) &ethcmd;
594 >
595 >                err = ioctl(sock, SIOCETHTOOL, &ifr);
596 >                if (err == 0) {
597 >                        network_iface_stat_ptr->speed = ethcmd.speed;
598 >
599 >                        switch (ethcmd.duplex) {
600 >                        case 0x00:
601 >                                network_iface_stat_ptr->dup = FULL_DUPLEX;
602 >                                break;
603 >                        case 0x01:
604 >                                network_iface_stat_ptr->dup = HALF_DUPLEX;
605 >                                break;
606 >                        default:
607 >                                network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
608 >                        }
609 >                } else {
610 >                        /* Not all interfaces support the ethtool ioctl. */
611 >                        network_iface_stat_ptr->speed = -1;
612 >                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
613                  }
614 <                if(ethcmd->duplex == 0x01){
613 <                        network_iface_stat_ptr->dup = HALF_DUPLEX;
614 <                }
614 >
615                  ifaces++;
616          }
617          close(sock);
618 <        free(eth_tool_cmd_buf);
618 >        fclose(f);
619   #endif
620          *entries = ifaces;
621          return network_iface_stats;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines