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.33 by pajs, Fri Feb 13 15:29:16 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 445 | Line 470 | network_iface_stat_t *get_network_iface_stats(int *ent
470                  }else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){
471                          network_iface_stat_ptr->dup = HALF_DUPLEX;
472                  }else{
473 <                        network_iface_stat_ptr->dup = NO_DUPLEX;
473 >                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
474                  }
475                  ifaces++;
476          }      
477          freeifaddrs(net);
478 +        close(s);
479   #endif
480  
481   #ifdef SOLARIS
# Line 476 | Line 502 | network_iface_stat_t *get_network_iface_stats(int *ent
502                                  continue;
503                          }
504  
505 <                        if(knp->value.ui64 == 0){
505 >                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
506 >                        if(knp->value.ui64 == 2){
507                                  network_iface_stat_ptr->dup = FULL_DUPLEX;
508 <                        }else{
508 >                        }
509 >                        if(knp->value.ui64 == 1){
510                                  network_iface_stat_ptr->dup = HALF_DUPLEX;
511                          }
512  
# Line 491 | Line 519 | network_iface_stat_t *get_network_iface_stats(int *ent
519          kstat_close(kc);
520          
521   #endif  
522 + #ifdef LINUX
523 +
524 +        f = fopen("/proc/net/dev", "r");
525 +        if(f == NULL){
526 +                return NULL;
527 +        }
528 +
529 +        /* Setup stuff so we can do the ioctl to get the info */
530 +        if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
531 +                return NULL;
532 +        }
533 +
534 +        buf_size = sizeof(struct ethtool_cmd);
535 +        eth_tool_cmd_buf = malloc(buf_size);
536 +        if(eth_tool_cmd_buf == NULL) return NULL;
537 +
538 +        /* Ignore first 2 lines.. Just headings */
539 +        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
540 +        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
541 +
542 +        while((fgets(line, sizeof(line), f)) != NULL){
543 +                char *name, *ptr;
544 +                struct ifreq ifr;
545 +                struct ethtool_cmd *ethcmd;
546 +                int err;
547 +
548 +                /* Get the interface name */
549 +                ptr = strchr(line, ':');
550 +                if (ptr == NULL) continue;
551 +                *ptr='\0';
552 +                name = line;
553 +                while(isspace(*(name))){
554 +                        name++;
555 +                }
556 +
557 +                memset(&ifr, 0, sizeof(ifr));
558 +                memset(eth_tool_cmd_buf, 0, buf_size);
559 +                ifr.ifr_data = (caddr_t) eth_tool_cmd_buf;
560 +                strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
561 +
562 +                ethcmd = (struct ethtool_cmd *) ifr.ifr_data;
563 +                ethcmd->cmd = ETHTOOL_GSET;
564 +
565 +                err = ioctl(sock, SIOCETHTOOL, &ifr);
566 +                if(err < 0){
567 +                        /* This could fail if the interface doesn't support the command. Carry
568 +                         * on to the next :)
569 +                         */
570 +                        continue;
571 +                }
572 +
573 +                /* We have a good interface to add */
574 +                network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
575 +                if(network_iface_stats==NULL){
576 +                        return NULL;
577 +                }
578 +                network_iface_stat_ptr = network_iface_stats + ifaces;
579 +                network_iface_stat_ptr->interface_name = strdup(name);
580 +                network_iface_stat_ptr->speed = ethcmd->speed;
581 +                network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
582 +                if(ethcmd->duplex == 0x00){
583 +                        network_iface_stat_ptr->dup = FULL_DUPLEX;
584 +                }
585 +                if(ethcmd->duplex == 0x01){
586 +                        network_iface_stat_ptr->dup = HALF_DUPLEX;
587 +                }
588 +                ifaces++;
589 +        }
590 +        close(sock);
591 +        free(eth_tool_cmd_buf);
592 + #endif
593          *entries = ifaces;
594          return network_iface_stats;
595   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines