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.26 by pajs, Sun Jan 25 20:13:57 2004 UTC vs.
Revision 1.37 by ats, Sat Feb 14 00:08:51 2004 UTC

# Line 37 | Line 37
37   #include <stdio.h>
38   #include <sys/types.h>
39   #include <regex.h>
40 + #include <sys/ioctl.h>
41 + #include <sys/socket.h>
42 + #include <net/if.h>
43 + #include <ctype.h>
44   #include "tools.h"
45 + /* Stuff which could be defined by defining KERNEL, but
46 + * that would be a bad idea, so we'll just declare it here
47 + */
48 + typedef __uint8_t u8;
49 + typedef __uint16_t u16;
50 + typedef __uint32_t u32;
51 + #include <linux/ethtool.h>
52 + #include <linux/sockios.h>
53 + #include <unistd.h>
54   #endif
55   #ifdef ALLBSD
56   #include <sys/types.h>
# Line 327 | Line 340 | void network_iface_stat_init(int start, int end, netwo
340          for(net_stats+=start; start<end; start++){
341                  net_stats->interface_name=NULL;
342                  net_stats->speed=0;
343 <                net_stats->dup=NO_DUPLEX;
343 >                net_stats->dup=UNKNOWN_DUPLEX;
344                  net_stats++;
345          }
346   }
# Line 372 | Line 385 | network_iface_stat_t *get_network_iface_stats(int *ent
385   #ifdef ALLBSD
386          struct ifaddrs *net, *net_ptr;
387          struct ifmediareq ifmed;
388 <        int s;
388 >        struct ifreq ifr;
389 >        int sock;
390          int x;
391   #endif
392 <
392 > #ifdef LINUX
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;
398 >        int sock;
399 > #endif
400          ifaces = 0;
401   #ifdef ALLBSD
402          if(getifaddrs(&net) != 0){
403                  return NULL;
404          }
405  
406 <        if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == NULL) return NULL;
406 >        if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return NULL;
407  
408          for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){
409                  if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
# Line 394 | Line 415 | network_iface_stat_t *get_network_iface_stats(int *ent
415  
416                  memset(&ifmed, 0, sizeof(struct ifmediareq));
417                  strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name));
418 <                if(ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){
418 >                if(ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){
419                          continue;
420                  }
421  
# Line 431 | Line 452 | network_iface_stat_t *get_network_iface_stats(int *ent
452                          case(IFM_1000_SX):
453                          case(IFM_1000_LX):
454                          case(IFM_1000_CX):
455 + #if defined(FREEBSD) && !defined(FREEBSD5)
456                          case(IFM_1000_TX):
457 +                        case(IFM_1000_FX):
458 + #else
459 +                        case(IFM_1000_T):
460 + #endif
461                                  network_iface_stat_ptr->speed = 1000;
462                                  break;
463                          /* We don't know what it is */
# Line 445 | Line 471 | network_iface_stat_t *get_network_iface_stats(int *ent
471                  }else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){
472                          network_iface_stat_ptr->dup = HALF_DUPLEX;
473                  }else{
474 <                        network_iface_stat_ptr->dup = NO_DUPLEX;
474 >                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
475                  }
476 +
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++;
490          }      
491          freeifaddrs(net);
492 +        close(sock);
493   #endif
494  
495   #ifdef SOLARIS
# Line 471 | Line 511 | network_iface_stat_t *get_network_iface_stats(int *ent
511                          network_iface_stat_ptr = network_iface_stats + ifaces;
512                          network_iface_stat_ptr->speed = knp->value.ui64 / (1000*1000);
513  
514 +                        if((knp=kstat_data_lookup(ksp, "link_up"))==NULL){
515 +                                /* Not a network interface, so skip to the next entry */
516 +                                continue;
517 +                        }
518 +                        /* Solaris has 1 for up, and 0 for not. As we do too */
519 +                        network_iface_stat_ptr->up = value.ui32;
520 +
521                          if((knp=kstat_data_lookup(ksp, "link_duplex"))==NULL){
522                                  /* Not a network interface, so skip to the next entry */
523                                  continue;
524                          }
525  
526 <                        if(knp->value.ui64 == 0){
526 >                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
527 >                        if(knp->value.ui32 == 2){
528                                  network_iface_stat_ptr->dup = FULL_DUPLEX;
529 <                        }else{
529 >                        }
530 >                        if(knp->value.ui32 == 1){
531                                  network_iface_stat_ptr->dup = HALF_DUPLEX;
532                          }
533  
# Line 491 | Line 540 | network_iface_stat_t *get_network_iface_stats(int *ent
540          kstat_close(kc);
541          
542   #endif  
543 + #ifdef LINUX
544 +
545 +        f = fopen("/proc/net/dev", "r");
546 +        if(f == NULL){
547 +                return NULL;
548 +        }
549 +
550 +        /* Setup stuff so we can do the ioctl to get the info */
551 +        if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
552 +                return NULL;
553 +        }
554 +
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 +
559 +        /* Ignore first 2 lines.. Just headings */
560 +        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
561 +        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
562 +
563 +        while((fgets(line, sizeof(line), f)) != NULL){
564 +                char *name, *ptr;
565 +                struct ifreq ifr;
566 +                struct ethtool_cmd *ethcmd;
567 +                int err;
568 +
569 +                /* Get the interface name */
570 +                ptr = strchr(line, ':');
571 +                if (ptr == NULL) continue;
572 +                *ptr='\0';
573 +                name = line;
574 +                while(isspace(*(name))){
575 +                        name++;
576 +                }
577 +
578 +                memset(&ifr, 0, sizeof(ifr));
579 +                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));
582 +
583 +                ethcmd = (struct ethtool_cmd *) ifr.ifr_data;
584 +                ethcmd->cmd = ETHTOOL_GSET;
585 +
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 +
594 +                /* We have a good interface to add */
595 +                network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
596 +                if(network_iface_stats==NULL){
597 +                        return NULL;
598 +                }
599 +                network_iface_stat_ptr = network_iface_stats + ifaces;
600 +                network_iface_stat_ptr->interface_name = strdup(name);
601 +                network_iface_stat_ptr->speed = ethcmd->speed;
602 +                if((ifr.ifr_flags & IFF_UP) != 0){
603 +                        network_iface_stat_ptr->up = 1;
604 +                }else{
605 +                        network_iface_stat_ptr->up = 0;
606 +                }
607 +
608 +                network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
609 +                if(ethcmd->duplex == 0x00){
610 +                        network_iface_stat_ptr->dup = FULL_DUPLEX;
611 +                }
612 +                if(ethcmd->duplex == 0x01){
613 +                        network_iface_stat_ptr->dup = HALF_DUPLEX;
614 +                }
615 +                ifaces++;
616 +        }
617 +        close(sock);
618 +        free(eth_tool_cmd_buf);
619 + #endif
620          *entries = ifaces;
621          return network_iface_stats;
622   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines