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.40 by ats, Sat Feb 14 00:47:24 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 413 | Line 411 | network_iface_stat_t *get_network_iface_stats(int *ent
411                  }
412                  network_iface_stat_ptr = network_iface_stats + ifaces;
413  
414 +                memset(&ifr, 0, sizeof(ifr));
415 +                strncpy(ifr.ifr_name, net_ptr->ifa_name, sizeof(ifr.ifr_name));
416 +
417 +                if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0){
418 +                        continue;
419 +                }      
420 +                if((ifr.ifr_flags & IFF_UP) != 0){
421 +                        network_iface_stat_ptr->up = 1;
422 +                }else{
423 +                        network_iface_stat_ptr->up = 0;
424 +                }
425 +
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 +
430 +                network_iface_stat_ptr->speed = 0;
431 +                network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
432 +                ifaces++;
433 +
434                  memset(&ifmed, 0, sizeof(struct ifmediareq));
435                  strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name));
436                  if(ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){
437 +                        /* Not all interfaces support the media ioctls. */
438                          continue;
439                  }
440  
# Line 425 | Line 444 | network_iface_stat_t *get_network_iface_stats(int *ent
444                          continue;
445                  }
446  
428                if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
429                network_iface_stat_ptr->interface_name = strdup(net_ptr->ifa_name);
430                if(network_iface_stat_ptr->interface_name == NULL) return NULL;
431
447                  /* Only intrested in the first 4 bits)  - Assuming only ETHER devices */
448                  x = ifmed.ifm_active & 0x0f;    
449                  switch(x){
# Line 474 | Line 489 | network_iface_stat_t *get_network_iface_stats(int *ent
489                          network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
490                  }
491  
477                memset(&ifr, 0, sizeof(ifr));
478                strncpy(ifr.ifr_name, net_ptr->ifa_name, sizeof(ifr.ifr_name));
479
480                if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0){
481                        continue;
482                }      
483                if((ifr.ifr_flags & IFF_UP) != 0){
484                        network_iface_stat_ptr->up = 1;
485                }else{
486                        network_iface_stat_ptr->up = 0;
487                }
488
489                ifaces++;
492          }      
493          freeifaddrs(net);
494          close(sock);
# Line 541 | Line 543 | network_iface_stat_t *get_network_iface_stats(int *ent
543          
544   #endif  
545   #ifdef LINUX
544
546          f = fopen("/proc/net/dev", "r");
547          if(f == NULL){
548                  return NULL;
# Line 552 | Line 553 | network_iface_stat_t *get_network_iface_stats(int *ent
553                  return NULL;
554          }
555  
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
556          /* Ignore first 2 lines.. Just headings */
557          if((fgets(line, sizeof(line), f)) == NULL) return NULL;
558          if((fgets(line, sizeof(line), f)) == NULL) return NULL;
# Line 563 | Line 560 | network_iface_stat_t *get_network_iface_stats(int *ent
560          while((fgets(line, sizeof(line), f)) != NULL){
561                  char *name, *ptr;
562                  struct ifreq ifr;
563 <                struct ethtool_cmd *ethcmd;
563 >                struct ethtool_cmd ethcmd;
564                  int err;
565  
566                  /* Get the interface name */
# Line 575 | Line 572 | network_iface_stat_t *get_network_iface_stats(int *ent
572                          name++;
573                  }
574  
575 <                memset(&ifr, 0, sizeof(ifr));
576 <                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));
575 >                memset(&ifr, 0, sizeof ifr);
576 >                strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
577  
578 <                ethcmd = (struct ethtool_cmd *) ifr.ifr_data;
579 <                ethcmd->cmd = ETHTOOL_GSET;
578 >                if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
579 >                        continue;
580 >                }
581  
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
582                  /* We have a good interface to add */
583                  network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
584                  if(network_iface_stats==NULL){
# Line 598 | Line 586 | network_iface_stat_t *get_network_iface_stats(int *ent
586                  }
587                  network_iface_stat_ptr = network_iface_stats + ifaces;
588                  network_iface_stat_ptr->interface_name = strdup(name);
589 <                network_iface_stat_ptr->speed = ethcmd->speed;
602 <                if((ifr.ifr_flags & IFF_UP) != 0){
589 >                if ((ifr.ifr_flags & IFF_UP) != 0) {
590                          network_iface_stat_ptr->up = 1;
591 <                }else{
591 >                } else {
592                          network_iface_stat_ptr->up = 0;
593                  }
594  
595 <                network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
596 <                if(ethcmd->duplex == 0x00){
597 <                        network_iface_stat_ptr->dup = FULL_DUPLEX;
595 >                memset(&ethcmd, 0, sizeof ethcmd);
596 >                ethcmd.cmd = ETHTOOL_GSET;
597 >                ifr.ifr_data = (caddr_t) &ethcmd;
598 >
599 >                err = ioctl(sock, SIOCETHTOOL, &ifr);
600 >                if (err == 0) {
601 >                        network_iface_stat_ptr->speed = ethcmd.speed;
602 >
603 >                        switch (ethcmd.duplex) {
604 >                        case 0x00:
605 >                                network_iface_stat_ptr->dup = FULL_DUPLEX;
606 >                                break;
607 >                        case 0x01:
608 >                                network_iface_stat_ptr->dup = HALF_DUPLEX;
609 >                                break;
610 >                        default:
611 >                                network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
612 >                        }
613 >                } else {
614 >                        /* Not all interfaces support the ethtool ioctl. */
615 >                        network_iface_stat_ptr->speed = 0;
616 >                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
617                  }
618 <                if(ethcmd->duplex == 0x01){
613 <                        network_iface_stat_ptr->dup = HALF_DUPLEX;
614 <                }
618 >
619                  ifaces++;
620          }
621          close(sock);
622 <        free(eth_tool_cmd_buf);
622 >        fclose(f);
623   #endif
624          *entries = ifaces;
625          return network_iface_stats;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines