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.43 by ats, Sat Feb 14 15:48:42 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 + typedef __uint64_t u64;
52 + #include <linux/ethtool.h>
53 + #include <linux/sockios.h>
54 + #include <unistd.h>
55   #endif
56   #ifdef ALLBSD
57   #include <sys/types.h>
58   #include <sys/socket.h>
59   #include <ifaddrs.h>
60   #include <net/if.h>
61 + #include <net/if_media.h>
62 + #include <sys/ioctl.h>
63   #endif
64  
65   static network_stat_t *network_stats=NULL;
# Line 318 | Line 334 | network_stat_t *get_network_stats_diff(int *entries) {
334          *entries = diff_count;
335          return diff;
336   }
337 + /* NETWORK INTERFACE STATS */
338 +
339 + void network_iface_stat_init(int start, int end, network_iface_stat_t *net_stats){
340 +
341 +        for(net_stats+=start; start<end; start++){
342 +                net_stats->interface_name=NULL;
343 +                net_stats->speed=0;
344 +                net_stats->dup=UNKNOWN_DUPLEX;
345 +                net_stats++;
346 +        }
347 + }
348 +
349 + network_iface_stat_t *network_iface_stat_malloc(int needed_entries, int *cur_entries, network_iface_stat_t *net_stats){
350 +
351 +        if(net_stats==NULL){
352 +
353 +                if((net_stats=malloc(needed_entries * sizeof(network_iface_stat_t)))==NULL){
354 +                        return NULL;
355 +                }
356 +                network_iface_stat_init(0, needed_entries, net_stats);
357 +                *cur_entries=needed_entries;
358 +
359 +                return net_stats;
360 +        }
361 +
362 +
363 +        if(*cur_entries<needed_entries){
364 +                net_stats=realloc(net_stats, (sizeof(network_iface_stat_t)*needed_entries));
365 +                if(net_stats==NULL){
366 +                        return NULL;
367 +                }
368 +                network_iface_stat_init(*cur_entries, needed_entries, net_stats);
369 +                *cur_entries=needed_entries;
370 +        }
371 +
372 +        return net_stats;
373 + }
374 +
375 + network_iface_stat_t *get_network_iface_stats(int *entries){
376 +        static network_iface_stat_t *network_iface_stats;
377 +        network_iface_stat_t *network_iface_stat_ptr;
378 +        static int sizeof_network_iface_stats=0;        
379 +        int ifaces = 0;
380 +
381 + #ifdef SOLARIS
382 +        kstat_ctl_t *kc;
383 +        kstat_t *ksp;
384 +        kstat_named_t *knp;
385 + #endif
386 + #ifdef ALLBSD
387 +        struct ifaddrs *net, *net_ptr;
388 +        struct ifmediareq ifmed;
389 +        struct ifreq ifr;
390 +        int sock;
391 +        int x;
392 + #endif
393 + #ifdef LINUX
394 +        FILE *f;
395 +        /* Horrible big enough, but it should be easily big enough */
396 +        char line[8096];
397 +        int sock;
398 + #endif
399 +
400 + #ifdef ALLBSD
401 +        if(getifaddrs(&net) != 0){
402 +                return NULL;
403 +        }
404 +
405 +        if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return NULL;
406 +
407 +        for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){
408 +                if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
409 +                network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
410 +                if(network_iface_stats==NULL){
411 +                        return NULL;
412 +                }
413 +                network_iface_stat_ptr = network_iface_stats + ifaces;
414 +
415 +                memset(&ifr, 0, sizeof(ifr));
416 +                strncpy(ifr.ifr_name, net_ptr->ifa_name, sizeof(ifr.ifr_name));
417 +
418 +                if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0){
419 +                        continue;
420 +                }      
421 +                if((ifr.ifr_flags & IFF_UP) != 0){
422 +                        network_iface_stat_ptr->up = 1;
423 +                }else{
424 +                        network_iface_stat_ptr->up = 0;
425 +                }
426 +
427 +                if (network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
428 +                network_iface_stat_ptr->interface_name = strdup(net_ptr->ifa_name);
429 +                if (network_iface_stat_ptr->interface_name == NULL) return NULL;
430 +
431 +                network_iface_stat_ptr->speed = 0;
432 +                network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
433 +                ifaces++;
434 +
435 +                memset(&ifmed, 0, sizeof(struct ifmediareq));
436 +                strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name));
437 +                if(ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){
438 +                        /* Not all interfaces support the media ioctls. */
439 +                        continue;
440 +                }
441 +
442 +                /* We may need to change this if we start doing wireless devices too */
443 +                if( (ifmed.ifm_active | IFM_ETHER) != ifmed.ifm_active ){
444 +                        /* Not a ETHER device */
445 +                        continue;
446 +                }
447 +
448 +                /* Only intrested in the first 4 bits)  - Assuming only ETHER devices */
449 +                x = ifmed.ifm_active & 0x0f;    
450 +                switch(x){
451 +                        /* 10 Mbit connections. Speedy :) */
452 +                        case(IFM_10_T):
453 +                        case(IFM_10_2):
454 +                        case(IFM_10_5):
455 +                        case(IFM_10_STP):
456 +                        case(IFM_10_FL):
457 +                                network_iface_stat_ptr->speed = 10;
458 +                                break;
459 +                        /* 100 Mbit conneections */
460 +                        case(IFM_100_TX):
461 +                        case(IFM_100_FX):
462 +                        case(IFM_100_T4):
463 +                        case(IFM_100_VG):
464 +                        case(IFM_100_T2):
465 +                                network_iface_stat_ptr->speed = 100;
466 +                                break;
467 +                        /* 1000 Mbit connections */
468 +                        case(IFM_1000_SX):
469 +                        case(IFM_1000_LX):
470 +                        case(IFM_1000_CX):
471 + #if defined(FREEBSD) && !defined(FREEBSD5)
472 +                        case(IFM_1000_TX):
473 +                        case(IFM_1000_FX):
474 + #else
475 +                        case(IFM_1000_T):
476 + #endif
477 +                                network_iface_stat_ptr->speed = 1000;
478 +                                break;
479 +                        /* We don't know what it is */
480 +                        default:
481 +                                network_iface_stat_ptr->speed = 0;
482 +                                break;
483 +                }
484 +
485 +                if( (ifmed.ifm_active | IFM_FDX) == ifmed.ifm_active ){
486 +                        network_iface_stat_ptr->dup = FULL_DUPLEX;
487 +                }else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){
488 +                        network_iface_stat_ptr->dup = HALF_DUPLEX;
489 +                }else{
490 +                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
491 +                }
492 +
493 +        }      
494 +        freeifaddrs(net);
495 +        close(sock);
496 + #endif
497 +
498 + #ifdef SOLARIS
499 +        if ((kc = kstat_open()) == NULL) {
500 +                return NULL;
501 +        }
502 +
503 +        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
504 +                if (!strcmp(ksp->ks_class, "net")) {
505 +                        kstat_read(kc, ksp, NULL);
506 +                        if((knp=kstat_data_lookup(ksp, "ifspeed"))==NULL){
507 +                                /* Not a network interface, so skip to the next entry */
508 +                                continue;
509 +                        }
510 +                        network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
511 +                        if(network_iface_stats==NULL){
512 +                                return NULL;
513 +                        }
514 +                        network_iface_stat_ptr = network_iface_stats + ifaces;
515 +                        network_iface_stat_ptr->speed = knp->value.ui64 / (1000*1000);
516 +
517 +                        if((knp=kstat_data_lookup(ksp, "link_up"))==NULL){
518 +                                /* Not a network interface, so skip to the next entry */
519 +                                continue;
520 +                        }
521 +                        /* Solaris has 1 for up, and 0 for not. As we do too */
522 +                        network_iface_stat_ptr->up = knp->value.ui32;
523 +
524 +                        if((knp=kstat_data_lookup(ksp, "link_duplex"))==NULL){
525 +                                /* Not a network interface, so skip to the next entry */
526 +                                continue;
527 +                        }
528 +
529 +                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
530 +                        if(knp->value.ui32 == 2){
531 +                                network_iface_stat_ptr->dup = FULL_DUPLEX;
532 +                        }
533 +                        if(knp->value.ui32 == 1){
534 +                                network_iface_stat_ptr->dup = HALF_DUPLEX;
535 +                        }
536 +
537 +                        if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
538 +                        network_iface_stat_ptr->interface_name = strdup(ksp->ks_name);
539 +                        if(network_iface_stat_ptr->interface_name == NULL) return NULL;
540 +                        ifaces++;
541 +                }
542 +        }
543 +        kstat_close(kc);
544 +        
545 + #endif  
546 + #ifdef LINUX
547 +        f = fopen("/proc/net/dev", "r");
548 +        if(f == NULL){
549 +                return NULL;
550 +        }
551 +
552 +        /* Setup stuff so we can do the ioctl to get the info */
553 +        if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
554 +                return NULL;
555 +        }
556 +
557 +        /* Ignore first 2 lines.. Just headings */
558 +        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
559 +        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
560 +
561 +        while((fgets(line, sizeof(line), f)) != NULL){
562 +                char *name, *ptr;
563 +                struct ifreq ifr;
564 +                struct ethtool_cmd ethcmd;
565 +                int err;
566 +
567 +                /* Get the interface name */
568 +                ptr = strchr(line, ':');
569 +                if (ptr == NULL) continue;
570 +                *ptr='\0';
571 +                name = line;
572 +                while(isspace(*(name))){
573 +                        name++;
574 +                }
575 +
576 +                memset(&ifr, 0, sizeof ifr);
577 +                strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
578 +
579 +                if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
580 +                        continue;
581 +                }
582 +
583 +                /* We have a good interface to add */
584 +                network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
585 +                if(network_iface_stats==NULL){
586 +                        return NULL;
587 +                }
588 +                network_iface_stat_ptr = network_iface_stats + ifaces;
589 +                network_iface_stat_ptr->interface_name = strdup(name);
590 +                if ((ifr.ifr_flags & IFF_UP) != 0) {
591 +                        network_iface_stat_ptr->up = 1;
592 +                } else {
593 +                        network_iface_stat_ptr->up = 0;
594 +                }
595 +
596 +                memset(&ethcmd, 0, sizeof ethcmd);
597 +                ethcmd.cmd = ETHTOOL_GSET;
598 +                ifr.ifr_data = (caddr_t) &ethcmd;
599 +
600 +                err = ioctl(sock, SIOCETHTOOL, &ifr);
601 +                if (err == 0) {
602 +                        network_iface_stat_ptr->speed = ethcmd.speed;
603 +
604 +                        switch (ethcmd.duplex) {
605 +                        case 0x00:
606 +                                network_iface_stat_ptr->dup = FULL_DUPLEX;
607 +                                break;
608 +                        case 0x01:
609 +                                network_iface_stat_ptr->dup = HALF_DUPLEX;
610 +                                break;
611 +                        default:
612 +                                network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
613 +                        }
614 +                } else {
615 +                        /* Not all interfaces support the ethtool ioctl. */
616 +                        network_iface_stat_ptr->speed = 0;
617 +                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
618 +                }
619 +
620 +                ifaces++;
621 +        }
622 +        close(sock);
623 +        fclose(f);
624 + #endif
625 +        *entries = ifaces;
626 +        return network_iface_stats;
627 + }
628 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines