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.41 by pajs, Sat Feb 14 11:33:33 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 sock;
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 +        int sock;
397 + #endif
398 +        ifaces = 0;
399 + #ifdef ALLBSD
400 +        if(getifaddrs(&net) != 0){
401 +                return NULL;
402 +        }
403  
404 +        if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return NULL;
405 +
406 +        for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){
407 +                if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
408 +                network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
409 +                if(network_iface_stats==NULL){
410 +                        return NULL;
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 +
441 +                /* We may need to change this if we start doing wireless devices too */
442 +                if( (ifmed.ifm_active | IFM_ETHER) != ifmed.ifm_active ){
443 +                        /* Not a ETHER device */
444 +                        continue;
445 +                }
446 +
447 +                /* Only intrested in the first 4 bits)  - Assuming only ETHER devices */
448 +                x = ifmed.ifm_active & 0x0f;    
449 +                switch(x){
450 +                        /* 10 Mbit connections. Speedy :) */
451 +                        case(IFM_10_T):
452 +                        case(IFM_10_2):
453 +                        case(IFM_10_5):
454 +                        case(IFM_10_STP):
455 +                        case(IFM_10_FL):
456 +                                network_iface_stat_ptr->speed = 10;
457 +                                break;
458 +                        /* 100 Mbit conneections */
459 +                        case(IFM_100_TX):
460 +                        case(IFM_100_FX):
461 +                        case(IFM_100_T4):
462 +                        case(IFM_100_VG):
463 +                        case(IFM_100_T2):
464 +                                network_iface_stat_ptr->speed = 100;
465 +                                break;
466 +                        /* 1000 Mbit connections */
467 +                        case(IFM_1000_SX):
468 +                        case(IFM_1000_LX):
469 +                        case(IFM_1000_CX):
470 + #if defined(FREEBSD) && !defined(FREEBSD5)
471 +                        case(IFM_1000_TX):
472 +                        case(IFM_1000_FX):
473 + #else
474 +                        case(IFM_1000_T):
475 + #endif
476 +                                network_iface_stat_ptr->speed = 1000;
477 +                                break;
478 +                        /* We don't know what it is */
479 +                        default:
480 +                                network_iface_stat_ptr->speed = 0;
481 +                                break;
482 +                }
483 +
484 +                if( (ifmed.ifm_active | IFM_FDX) == ifmed.ifm_active ){
485 +                        network_iface_stat_ptr->dup = FULL_DUPLEX;
486 +                }else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){
487 +                        network_iface_stat_ptr->dup = HALF_DUPLEX;
488 +                }else{
489 +                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
490 +                }
491 +
492 +        }      
493 +        freeifaddrs(net);
494 +        close(sock);
495 + #endif
496 +
497   #ifdef SOLARIS
498          if ((kc = kstat_open()) == NULL) {
499                  return NULL;
500          }
501  
376        ifaces=0;
377
502          for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
503                  if (!strcmp(ksp->ks_class, "net")) {
504                          kstat_read(kc, ksp, NULL);
# Line 386 | Line 510 | network_iface_stat_t *get_network_iface_stats(int *ent
510                          if(network_iface_stats==NULL){
511                                  return NULL;
512                          }
513 <                        network_iface_stat_ptr = network_iface_stats + interfaces;
513 >                        network_iface_stat_ptr = network_iface_stats + ifaces;
514                          network_iface_stat_ptr->speed = knp->value.ui64 / (1000*1000);
515  
516 +                        if((knp=kstat_data_lookup(ksp, "link_up"))==NULL){
517 +                                /* Not a network interface, so skip to the next entry */
518 +                                continue;
519 +                        }
520 +                        /* Solaris has 1 for up, and 0 for not. As we do too */
521 +                        network_iface_stat_ptr->up = knp->value.ui32;
522 +
523                          if((knp=kstat_data_lookup(ksp, "link_duplex"))==NULL){
524                                  /* Not a network interface, so skip to the next entry */
525                                  continue;
526                          }
527  
528 <                        if(knp->value.ui64 == 0){
528 >                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
529 >                        if(knp->value.ui32 == 2){
530                                  network_iface_stat_ptr->dup = FULL_DUPLEX;
531 <                        }else{
531 >                        }
532 >                        if(knp->value.ui32 == 1){
533                                  network_iface_stat_ptr->dup = HALF_DUPLEX;
534                          }
535  
536 +                        if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
537                          network_iface_stat_ptr->interface_name = strdup(ksp->ks_name);
538 <                        interfaces++;
538 >                        if(network_iface_stat_ptr->interface_name == NULL) return NULL;
539 >                        ifaces++;
540                  }
541          }
542          kstat_close(kc);
543          
544   #endif  
545 <        *entries = interfaces;
545 > #ifdef LINUX
546 >        f = fopen("/proc/net/dev", "r");
547 >        if(f == NULL){
548 >                return NULL;
549 >        }
550 >
551 >        /* Setup stuff so we can do the ioctl to get the info */
552 >        if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
553 >                return NULL;
554 >        }
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 >                strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
577 >
578 >                if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
579 >                        continue;
580 >                }
581 >
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){
585 >                        return NULL;
586 >                }
587 >                network_iface_stat_ptr = network_iface_stats + ifaces;
588 >                network_iface_stat_ptr->interface_name = strdup(name);
589 >                if ((ifr.ifr_flags & IFF_UP) != 0) {
590 >                        network_iface_stat_ptr->up = 1;
591 >                } else {
592 >                        network_iface_stat_ptr->up = 0;
593 >                }
594 >
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 >
619 >                ifaces++;
620 >        }
621 >        close(sock);
622 >        fclose(f);
623 > #endif
624 >        *entries = ifaces;
625          return network_iface_stats;
626   }
627  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines