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.25 by pajs, Fri Jan 23 23:23:33 2004 UTC vs.
Revision 1.36 by pajs, Fri Feb 13 18:54:29 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>
57   #include <sys/socket.h>
58   #include <ifaddrs.h>
59   #include <net/if.h>
60 + #include <net/if_media.h>
61 + #include <sys/ioctl.h>
62   #endif
63  
64   static network_stat_t *network_stats=NULL;
# Line 325 | 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 360 | Line 375 | network_iface_stat_t *get_network_iface_stats(int *ent
375          static network_iface_stat_t *network_iface_stats;
376          network_iface_stat_t *network_iface_stat_ptr;
377          static int sizeof_network_iface_stats=0;        
378 <        int ifaces;
378 >        static int ifaces;
379  
380   #ifdef SOLARIS
381          kstat_ctl_t *kc;
382          kstat_t *ksp;
383          kstat_named_t *knp;
384   #endif
385 + #ifdef ALLBSD
386 +        struct ifaddrs *net, *net_ptr;
387 +        struct ifmediareq ifmed;
388 +        struct ifreq ifr;
389 +        int s;
390 +        int x;
391 + #endif
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;
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;
410 +                network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
411 +                if(network_iface_stats==NULL){
412 +                        return NULL;
413 +                }
414 +                network_iface_stat_ptr = network_iface_stats + ifaces;
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){
419 +                        continue;
420 +                }
421 +
422 +                /* We may need to change this if we start doing wireless devices too */
423 +                if( (ifmed.ifm_active | IFM_ETHER) != ifmed.ifm_active ){
424 +                        /* Not a ETHER device */
425 +                        continue;
426 +                }
427 +
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 +
432 +                /* Only intrested in the first 4 bits)  - Assuming only ETHER devices */
433 +                x = ifmed.ifm_active & 0x0f;    
434 +                switch(x){
435 +                        /* 10 Mbit connections. Speedy :) */
436 +                        case(IFM_10_T):
437 +                        case(IFM_10_2):
438 +                        case(IFM_10_5):
439 +                        case(IFM_10_STP):
440 +                        case(IFM_10_FL):
441 +                                network_iface_stat_ptr->speed = 10;
442 +                                break;
443 +                        /* 100 Mbit conneections */
444 +                        case(IFM_100_TX):
445 +                        case(IFM_100_FX):
446 +                        case(IFM_100_T4):
447 +                        case(IFM_100_VG):
448 +                        case(IFM_100_T2):
449 +                                network_iface_stat_ptr->speed = 100;
450 +                                break;
451 +                        /* 1000 Mbit connections */
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 */
464 +                        default:
465 +                                network_iface_stat_ptr->speed = 0;
466 +                                break;
467 +                }
468 +
469 +                if( (ifmed.ifm_active | IFM_FDX) == ifmed.ifm_active ){
470 +                        network_iface_stat_ptr->dup = FULL_DUPLEX;
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 = UNKNOWN_DUPLEX;
475 +                }
476 +
477 +                if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0){
478 +                        continue;
479 +                }      
480 +                if((ifr.ifr_flags & IFF_UP) != 0){
481 +                        network_iface_stat_ptr->up = 1;
482 +                }else{
483 +                        network_iface_stat_ptr->up = 0;
484 +                }
485 +
486 +                ifaces++;
487 +        }      
488 +        freeifaddrs(net);
489 +        close(s);
490 + #endif
491 +
492   #ifdef SOLARIS
493          if ((kc = kstat_open()) == NULL) {
494                  return NULL;
495          }
496  
376        ifaces=0;
377
497          for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
498                  if (!strcmp(ksp->ks_class, "net")) {
499                          kstat_read(kc, ksp, NULL);
# Line 386 | Line 505 | network_iface_stat_t *get_network_iface_stats(int *ent
505                          if(network_iface_stats==NULL){
506                                  return NULL;
507                          }
508 <                        network_iface_stat_ptr = network_iface_stats + interfaces;
508 >                        network_iface_stat_ptr = network_iface_stats + ifaces;
509                          network_iface_stat_ptr->speed = knp->value.ui64 / (1000*1000);
510  
511 +                        if((knp=kstat_data_lookup(ksp, "link_up"))==NULL){
512 +                                /* Not a network interface, so skip to the next entry */
513 +                                continue;
514 +                        }
515 +                        /* Solaris has 1 for up, and 0 for not. As we do too */
516 +                        network_iface_stat_ptr->up = value.ui32;
517 +
518                          if((knp=kstat_data_lookup(ksp, "link_duplex"))==NULL){
519                                  /* Not a network interface, so skip to the next entry */
520                                  continue;
521                          }
522  
523 <                        if(knp->value.ui64 == 0){
523 >                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
524 >                        if(knp->value.ui32 == 2){
525                                  network_iface_stat_ptr->dup = FULL_DUPLEX;
526 <                        }else{
526 >                        }
527 >                        if(knp->value.ui32 == 1){
528                                  network_iface_stat_ptr->dup = HALF_DUPLEX;
529                          }
530  
531 +                        if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
532                          network_iface_stat_ptr->interface_name = strdup(ksp->ks_name);
533 <                        interfaces++;
533 >                        if(network_iface_stat_ptr->interface_name == NULL) return NULL;
534 >                        ifaces++;
535                  }
536          }
537          kstat_close(kc);
538          
539   #endif  
540 <        *entries = interfaces;
540 > #ifdef LINUX
541 >
542 >        f = fopen("/proc/net/dev", "r");
543 >        if(f == NULL){
544 >                return NULL;
545 >        }
546 >
547 >        /* Setup stuff so we can do the ioctl to get the info */
548 >        if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
549 >                return NULL;
550 >        }
551 >
552 >        buf_size = sizeof(struct ethtool_cmd);
553 >        eth_tool_cmd_buf = malloc(buf_size);
554 >        if(eth_tool_cmd_buf == NULL) return NULL;
555 >
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;
559 >
560 >        while((fgets(line, sizeof(line), f)) != NULL){
561 >                char *name, *ptr;
562 >                struct ifreq ifr;
563 >                struct ethtool_cmd *ethcmd;
564 >                int err;
565 >
566 >                /* Get the interface name */
567 >                ptr = strchr(line, ':');
568 >                if (ptr == NULL) continue;
569 >                *ptr='\0';
570 >                name = line;
571 >                while(isspace(*(name))){
572 >                        name++;
573 >                }
574 >
575 >                memset(&ifr, 0, sizeof(ifr));
576 >                memset(eth_tool_cmd_buf, 0, buf_size);
577 >                ifr.ifr_data = (caddr_t) eth_tool_cmd_buf;
578 >                strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
579 >
580 >                ethcmd = (struct ethtool_cmd *) ifr.ifr_data;
581 >                ethcmd->cmd = ETHTOOL_GSET;
582 >
583 >                err = ioctl(sock, SIOCETHTOOL, &ifr);
584 >                if(err < 0){
585 >                        /* This could fail if the interface doesn't support the command. Carry
586 >                         * on to the next :)
587 >                         */
588 >                        continue;
589 >                }
590 >
591 >                /* We have a good interface to add */
592 >                network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
593 >                if(network_iface_stats==NULL){
594 >                        return NULL;
595 >                }
596 >                network_iface_stat_ptr = network_iface_stats + ifaces;
597 >                network_iface_stat_ptr->interface_name = strdup(name);
598 >                network_iface_stat_ptr->speed = ethcmd->speed;
599 >                if((ifr.ifr_flags & IFF_UP) != 0){
600 >                        network_iface_stat_ptr->up = 1;
601 >                }else{
602 >                        network_iface_stat_ptr->up = 0;
603 >                }
604 >
605 >                network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
606 >                if(ethcmd->duplex == 0x00){
607 >                        network_iface_stat_ptr->dup = FULL_DUPLEX;
608 >                }
609 >                if(ethcmd->duplex == 0x01){
610 >                        network_iface_stat_ptr->dup = HALF_DUPLEX;
611 >                }
612 >                ifaces++;
613 >        }
614 >        close(sock);
615 >        free(eth_tool_cmd_buf);
616 > #endif
617 >        *entries = ifaces;
618          return network_iface_stats;
619   }
620  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines