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.35 by pajs, Fri Feb 13 18:45:43 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 375 | Line 388 | network_iface_stat_t *get_network_iface_stats(int *ent
388          int s;
389          int x;
390   #endif
391 <
391 > #ifdef LINUX
392 >        FILE *f;
393 >        /* Horrible big enough, but it should be easily big enough */
394 >        char line[8096];
395 >        void *eth_tool_cmd_buf;
396 >        int buf_size;
397 >        int sock;
398 > #endif
399          ifaces = 0;
400   #ifdef ALLBSD
401          if(getifaddrs(&net) != 0){
# Line 431 | Line 451 | network_iface_stat_t *get_network_iface_stats(int *ent
451                          case(IFM_1000_SX):
452                          case(IFM_1000_LX):
453                          case(IFM_1000_CX):
454 + #if defined(FREEBSD) && !defined(FREEBSD5)
455                          case(IFM_1000_TX):
456 +                        case(IFM_1000_FX):
457 + #else
458 +                        case(IFM_1000_T):
459 + #endif
460                                  network_iface_stat_ptr->speed = 1000;
461                                  break;
462                          /* We don't know what it is */
# Line 440 | Line 465 | network_iface_stat_t *get_network_iface_stats(int *ent
465                                  break;
466                  }
467  
468 +                if((ifmr.ifm_status & IFM_ACTIVE)){
469 +                        network_iface_stat_ptr->up = 1;
470 +                }else{
471 +                        network_iface_stat_ptr->up = 0;
472 +                }
473 +
474                  if( (ifmed.ifm_active | IFM_FDX) == ifmed.ifm_active ){
475                          network_iface_stat_ptr->dup = FULL_DUPLEX;
476                  }else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){
477                          network_iface_stat_ptr->dup = HALF_DUPLEX;
478                  }else{
479 <                        network_iface_stat_ptr->dup = NO_DUPLEX;
479 >                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
480                  }
481                  ifaces++;
482          }      
483          freeifaddrs(net);
484 +        close(s);
485   #endif
486  
487   #ifdef SOLARIS
# Line 471 | Line 503 | network_iface_stat_t *get_network_iface_stats(int *ent
503                          network_iface_stat_ptr = network_iface_stats + ifaces;
504                          network_iface_stat_ptr->speed = knp->value.ui64 / (1000*1000);
505  
506 +                        if((knp=kstat_data_lookup(ksp, "link_up"))==NULL){
507 +                                /* Not a network interface, so skip to the next entry */
508 +                                continue;
509 +                        }
510 +                        /* Solaris has 1 for up, and 0 for not. As we do too */
511 +                        network_iface_stat_ptr->up = value.ui32;
512 +
513                          if((knp=kstat_data_lookup(ksp, "link_duplex"))==NULL){
514                                  /* Not a network interface, so skip to the next entry */
515                                  continue;
516                          }
517  
518 <                        if(knp->value.ui64 == 0){
518 >                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
519 >                        if(knp->value.ui32 == 2){
520                                  network_iface_stat_ptr->dup = FULL_DUPLEX;
521 <                        }else{
521 >                        }
522 >                        if(knp->value.ui32 == 1){
523                                  network_iface_stat_ptr->dup = HALF_DUPLEX;
524                          }
525  
# Line 491 | Line 532 | network_iface_stat_t *get_network_iface_stats(int *ent
532          kstat_close(kc);
533          
534   #endif  
535 + #ifdef LINUX
536 +
537 +        f = fopen("/proc/net/dev", "r");
538 +        if(f == NULL){
539 +                return NULL;
540 +        }
541 +
542 +        /* Setup stuff so we can do the ioctl to get the info */
543 +        if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
544 +                return NULL;
545 +        }
546 +
547 +        buf_size = sizeof(struct ethtool_cmd);
548 +        eth_tool_cmd_buf = malloc(buf_size);
549 +        if(eth_tool_cmd_buf == NULL) return NULL;
550 +
551 +        /* Ignore first 2 lines.. Just headings */
552 +        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
553 +        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
554 +
555 +        while((fgets(line, sizeof(line), f)) != NULL){
556 +                char *name, *ptr;
557 +                struct ifreq ifr;
558 +                struct ethtool_cmd *ethcmd;
559 +                int err;
560 +
561 +                /* Get the interface name */
562 +                ptr = strchr(line, ':');
563 +                if (ptr == NULL) continue;
564 +                *ptr='\0';
565 +                name = line;
566 +                while(isspace(*(name))){
567 +                        name++;
568 +                }
569 +
570 +                memset(&ifr, 0, sizeof(ifr));
571 +                memset(eth_tool_cmd_buf, 0, buf_size);
572 +                ifr.ifr_data = (caddr_t) eth_tool_cmd_buf;
573 +                strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
574 +
575 +                ethcmd = (struct ethtool_cmd *) ifr.ifr_data;
576 +                ethcmd->cmd = ETHTOOL_GSET;
577 +
578 +                err = ioctl(sock, SIOCETHTOOL, &ifr);
579 +                if(err < 0){
580 +                        /* This could fail if the interface doesn't support the command. Carry
581 +                         * on to the next :)
582 +                         */
583 +                        continue;
584 +                }
585 +
586 +                /* We have a good interface to add */
587 +                network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
588 +                if(network_iface_stats==NULL){
589 +                        return NULL;
590 +                }
591 +                network_iface_stat_ptr = network_iface_stats + ifaces;
592 +                network_iface_stat_ptr->interface_name = strdup(name);
593 +                network_iface_stat_ptr->speed = ethcmd->speed;
594 +                if((ifr.ifr_flags & IFF_UP) != 0){
595 +                        network_iface_stat_ptr->up = 1;
596 +                }else{
597 +                        network_iface_stat_ptr->up = 0;
598 +                }
599 +
600 +                network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
601 +                if(ethcmd->duplex == 0x00){
602 +                        network_iface_stat_ptr->dup = FULL_DUPLEX;
603 +                }
604 +                if(ethcmd->duplex == 0x01){
605 +                        network_iface_stat_ptr->dup = HALF_DUPLEX;
606 +                }
607 +                ifaces++;
608 +        }
609 +        close(sock);
610 +        free(eth_tool_cmd_buf);
611 + #endif
612          *entries = ifaces;
613          return network_iface_stats;
614   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines