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.24 by ats, Wed Jan 21 23:46:54 2004 UTC vs.
Revision 1.39 by ats, Sat Feb 14 00:29: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 318 | Line 333 | network_stat_t *get_network_stats_diff(int *entries) {
333          *entries = diff_count;
334          return diff;
335   }
336 + /* NETWORK INTERFACE STATS */
337 +
338 + void network_iface_stat_init(int start, int end, network_iface_stat_t *net_stats){
339 +
340 +        for(net_stats+=start; start<end; start++){
341 +                net_stats->interface_name=NULL;
342 +                net_stats->speed=0;
343 +                net_stats->dup=UNKNOWN_DUPLEX;
344 +                net_stats++;
345 +        }
346 + }
347 +
348 + network_iface_stat_t *network_iface_stat_malloc(int needed_entries, int *cur_entries, network_iface_stat_t *net_stats){
349 +
350 +        if(net_stats==NULL){
351 +
352 +                if((net_stats=malloc(needed_entries * sizeof(network_iface_stat_t)))==NULL){
353 +                        return NULL;
354 +                }
355 +                network_iface_stat_init(0, needed_entries, net_stats);
356 +                *cur_entries=needed_entries;
357 +
358 +                return net_stats;
359 +        }
360 +
361 +
362 +        if(*cur_entries<needed_entries){
363 +                net_stats=realloc(net_stats, (sizeof(network_iface_stat_t)*needed_entries));
364 +                if(net_stats==NULL){
365 +                        return NULL;
366 +                }
367 +                network_iface_stat_init(*cur_entries, needed_entries, net_stats);
368 +                *cur_entries=needed_entries;
369 +        }
370 +
371 +        return net_stats;
372 + }
373 +
374 + network_iface_stat_t *get_network_iface_stats(int *entries){
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 +        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(&ifmed, 0, sizeof(struct ifmediareq));
415 +                strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name));
416 +                if(ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){
417 +                        continue;
418 +                }
419 +
420 +                /* We may need to change this if we start doing wireless devices too */
421 +                if( (ifmed.ifm_active | IFM_ETHER) != ifmed.ifm_active ){
422 +                        /* Not a ETHER device */
423 +                        continue;
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 +                /* Only intrested in the first 4 bits)  - Assuming only ETHER devices */
431 +                x = ifmed.ifm_active & 0x0f;    
432 +                switch(x){
433 +                        /* 10 Mbit connections. Speedy :) */
434 +                        case(IFM_10_T):
435 +                        case(IFM_10_2):
436 +                        case(IFM_10_5):
437 +                        case(IFM_10_STP):
438 +                        case(IFM_10_FL):
439 +                                network_iface_stat_ptr->speed = 10;
440 +                                break;
441 +                        /* 100 Mbit conneections */
442 +                        case(IFM_100_TX):
443 +                        case(IFM_100_FX):
444 +                        case(IFM_100_T4):
445 +                        case(IFM_100_VG):
446 +                        case(IFM_100_T2):
447 +                                network_iface_stat_ptr->speed = 100;
448 +                                break;
449 +                        /* 1000 Mbit connections */
450 +                        case(IFM_1000_SX):
451 +                        case(IFM_1000_LX):
452 +                        case(IFM_1000_CX):
453 + #if defined(FREEBSD) && !defined(FREEBSD5)
454 +                        case(IFM_1000_TX):
455 +                        case(IFM_1000_FX):
456 + #else
457 +                        case(IFM_1000_T):
458 + #endif
459 +                                network_iface_stat_ptr->speed = 1000;
460 +                                break;
461 +                        /* We don't know what it is */
462 +                        default:
463 +                                network_iface_stat_ptr->speed = 0;
464 +                                break;
465 +                }
466 +
467 +                if( (ifmed.ifm_active | IFM_FDX) == ifmed.ifm_active ){
468 +                        network_iface_stat_ptr->dup = FULL_DUPLEX;
469 +                }else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){
470 +                        network_iface_stat_ptr->dup = HALF_DUPLEX;
471 +                }else{
472 +                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
473 +                }
474 +
475 +                memset(&ifr, 0, sizeof(ifr));
476 +                strncpy(ifr.ifr_name, net_ptr->ifa_name, sizeof(ifr.ifr_name));
477 +
478 +                if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0){
479 +                        continue;
480 +                }      
481 +                if((ifr.ifr_flags & IFF_UP) != 0){
482 +                        network_iface_stat_ptr->up = 1;
483 +                }else{
484 +                        network_iface_stat_ptr->up = 0;
485 +                }
486 +
487 +                ifaces++;
488 +        }      
489 +        freeifaddrs(net);
490 +        close(sock);
491 + #endif
492 +
493 + #ifdef SOLARIS
494 +        if ((kc = kstat_open()) == NULL) {
495 +                return NULL;
496 +        }
497 +
498 +        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
499 +                if (!strcmp(ksp->ks_class, "net")) {
500 +                        kstat_read(kc, ksp, NULL);
501 +                        if((knp=kstat_data_lookup(ksp, "ifspeed"))==NULL){
502 +                                /* Not a network interface, so skip to the next entry */
503 +                                continue;
504 +                        }
505 +                        network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
506 +                        if(network_iface_stats==NULL){
507 +                                return NULL;
508 +                        }
509 +                        network_iface_stat_ptr = network_iface_stats + ifaces;
510 +                        network_iface_stat_ptr->speed = knp->value.ui64 / (1000*1000);
511 +
512 +                        if((knp=kstat_data_lookup(ksp, "link_up"))==NULL){
513 +                                /* Not a network interface, so skip to the next entry */
514 +                                continue;
515 +                        }
516 +                        /* Solaris has 1 for up, and 0 for not. As we do too */
517 +                        network_iface_stat_ptr->up = value.ui32;
518 +
519 +                        if((knp=kstat_data_lookup(ksp, "link_duplex"))==NULL){
520 +                                /* Not a network interface, so skip to the next entry */
521 +                                continue;
522 +                        }
523 +
524 +                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
525 +                        if(knp->value.ui32 == 2){
526 +                                network_iface_stat_ptr->dup = FULL_DUPLEX;
527 +                        }
528 +                        if(knp->value.ui32 == 1){
529 +                                network_iface_stat_ptr->dup = HALF_DUPLEX;
530 +                        }
531 +
532 +                        if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
533 +                        network_iface_stat_ptr->interface_name = strdup(ksp->ks_name);
534 +                        if(network_iface_stat_ptr->interface_name == NULL) return NULL;
535 +                        ifaces++;
536 +                }
537 +        }
538 +        kstat_close(kc);
539 +        
540 + #endif  
541 + #ifdef LINUX
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 +        /* Ignore first 2 lines.. Just headings */
553 +        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
554 +        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
555 +
556 +        while((fgets(line, sizeof(line), f)) != NULL){
557 +                char *name, *ptr;
558 +                struct ifreq ifr;
559 +                struct ethtool_cmd ethcmd;
560 +                int err;
561 +
562 +                /* Get the interface name */
563 +                ptr = strchr(line, ':');
564 +                if (ptr == NULL) continue;
565 +                *ptr='\0';
566 +                name = line;
567 +                while(isspace(*(name))){
568 +                        name++;
569 +                }
570 +
571 +                memset(&ifr, 0, sizeof ifr);
572 +                strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
573 +
574 +                if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
575 +                        continue;
576 +                }
577 +
578 +                /* We have a good interface to add */
579 +                network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
580 +                if(network_iface_stats==NULL){
581 +                        return NULL;
582 +                }
583 +                network_iface_stat_ptr = network_iface_stats + ifaces;
584 +                network_iface_stat_ptr->interface_name = strdup(name);
585 +                if ((ifr.ifr_flags & IFF_UP) != 0) {
586 +                        network_iface_stat_ptr->up = 1;
587 +                } else {
588 +                        network_iface_stat_ptr->up = 0;
589 +                }
590 +
591 +                memset(&ethcmd, 0, sizeof ethcmd);
592 +                ethcmd.cmd = ETHTOOL_GSET;
593 +                ifr.ifr_data = (caddr_t) &ethcmd;
594 +
595 +                err = ioctl(sock, SIOCETHTOOL, &ifr);
596 +                if (err == 0) {
597 +                        network_iface_stat_ptr->speed = ethcmd.speed;
598 +
599 +                        switch (ethcmd.duplex) {
600 +                        case 0x00:
601 +                                network_iface_stat_ptr->dup = FULL_DUPLEX;
602 +                                break;
603 +                        case 0x01:
604 +                                network_iface_stat_ptr->dup = HALF_DUPLEX;
605 +                                break;
606 +                        default:
607 +                                network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
608 +                        }
609 +                } else {
610 +                        /* Not all interfaces support the ethtool ioctl. */
611 +                        network_iface_stat_ptr->speed = 0;
612 +                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
613 +                }
614 +
615 +                ifaces++;
616 +        }
617 +        close(sock);
618 +        fclose(f);
619 + #endif
620 +        *entries = ifaces;
621 +        return network_iface_stats;
622 + }
623 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines