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.35 by pajs, Fri Feb 13 18:45:43 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 +        int s;
389 +        int x;
390 + #endif
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){
402 +                return NULL;
403 +        }
404 +
405 +        if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == NULL) 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(&ifmed, 0, sizeof(struct ifmediareq));
416 +                strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name));
417 +                if(ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){
418 +                        continue;
419 +                }
420 +
421 +                /* We may need to change this if we start doing wireless devices too */
422 +                if( (ifmed.ifm_active | IFM_ETHER) != ifmed.ifm_active ){
423 +                        /* Not a ETHER device */
424 +                        continue;
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 +                /* Only intrested in the first 4 bits)  - Assuming only ETHER devices */
432 +                x = ifmed.ifm_active & 0x0f;    
433 +                switch(x){
434 +                        /* 10 Mbit connections. Speedy :) */
435 +                        case(IFM_10_T):
436 +                        case(IFM_10_2):
437 +                        case(IFM_10_5):
438 +                        case(IFM_10_STP):
439 +                        case(IFM_10_FL):
440 +                                network_iface_stat_ptr->speed = 10;
441 +                                break;
442 +                        /* 100 Mbit conneections */
443 +                        case(IFM_100_TX):
444 +                        case(IFM_100_FX):
445 +                        case(IFM_100_T4):
446 +                        case(IFM_100_VG):
447 +                        case(IFM_100_T2):
448 +                                network_iface_stat_ptr->speed = 100;
449 +                                break;
450 +                        /* 1000 Mbit connections */
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 */
463 +                        default:
464 +                                network_iface_stat_ptr->speed = 0;
465 +                                break;
466 +                }
467 +
468 +                if((ifmr.ifm_status & IFM_ACTIVE)){
469 +                        network_iface_stat_ptr->up = 1;
470 +                }else{
471 +                        network_iface_stat_ptr->up = 0;
472 +                }
473 +
474 +                if( (ifmed.ifm_active | IFM_FDX) == ifmed.ifm_active ){
475 +                        network_iface_stat_ptr->dup = FULL_DUPLEX;
476 +                }else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){
477 +                        network_iface_stat_ptr->dup = HALF_DUPLEX;
478 +                }else{
479 +                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
480 +                }
481 +                ifaces++;
482 +        }      
483 +        freeifaddrs(net);
484 +        close(s);
485 + #endif
486 +
487 + #ifdef SOLARIS
488 +        if ((kc = kstat_open()) == NULL) {
489 +                return NULL;
490 +        }
491 +
492 +        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
493 +                if (!strcmp(ksp->ks_class, "net")) {
494 +                        kstat_read(kc, ksp, NULL);
495 +                        if((knp=kstat_data_lookup(ksp, "ifspeed"))==NULL){
496 +                                /* Not a network interface, so skip to the next entry */
497 +                                continue;
498 +                        }
499 +                        network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
500 +                        if(network_iface_stats==NULL){
501 +                                return NULL;
502 +                        }
503 +                        network_iface_stat_ptr = network_iface_stats + ifaces;
504 +                        network_iface_stat_ptr->speed = knp->value.ui64 / (1000*1000);
505 +
506 +                        if((knp=kstat_data_lookup(ksp, "link_up"))==NULL){
507 +                                /* Not a network interface, so skip to the next entry */
508 +                                continue;
509 +                        }
510 +                        /* Solaris has 1 for up, and 0 for not. As we do too */
511 +                        network_iface_stat_ptr->up = value.ui32;
512 +
513 +                        if((knp=kstat_data_lookup(ksp, "link_duplex"))==NULL){
514 +                                /* Not a network interface, so skip to the next entry */
515 +                                continue;
516 +                        }
517 +
518 +                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
519 +                        if(knp->value.ui32 == 2){
520 +                                network_iface_stat_ptr->dup = FULL_DUPLEX;
521 +                        }
522 +                        if(knp->value.ui32 == 1){
523 +                                network_iface_stat_ptr->dup = HALF_DUPLEX;
524 +                        }
525 +
526 +                        if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
527 +                        network_iface_stat_ptr->interface_name = strdup(ksp->ks_name);
528 +                        if(network_iface_stat_ptr->interface_name == NULL) return NULL;
529 +                        ifaces++;
530 +                }
531 +        }
532 +        kstat_close(kc);
533 +        
534 + #endif  
535 + #ifdef LINUX
536 +
537 +        f = fopen("/proc/net/dev", "r");
538 +        if(f == NULL){
539 +                return NULL;
540 +        }
541 +
542 +        /* Setup stuff so we can do the ioctl to get the info */
543 +        if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
544 +                return NULL;
545 +        }
546 +
547 +        buf_size = sizeof(struct ethtool_cmd);
548 +        eth_tool_cmd_buf = malloc(buf_size);
549 +        if(eth_tool_cmd_buf == NULL) return NULL;
550 +
551 +        /* Ignore first 2 lines.. Just headings */
552 +        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
553 +        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
554 +
555 +        while((fgets(line, sizeof(line), f)) != NULL){
556 +                char *name, *ptr;
557 +                struct ifreq ifr;
558 +                struct ethtool_cmd *ethcmd;
559 +                int err;
560 +
561 +                /* Get the interface name */
562 +                ptr = strchr(line, ':');
563 +                if (ptr == NULL) continue;
564 +                *ptr='\0';
565 +                name = line;
566 +                while(isspace(*(name))){
567 +                        name++;
568 +                }
569 +
570 +                memset(&ifr, 0, sizeof(ifr));
571 +                memset(eth_tool_cmd_buf, 0, buf_size);
572 +                ifr.ifr_data = (caddr_t) eth_tool_cmd_buf;
573 +                strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
574 +
575 +                ethcmd = (struct ethtool_cmd *) ifr.ifr_data;
576 +                ethcmd->cmd = ETHTOOL_GSET;
577 +
578 +                err = ioctl(sock, SIOCETHTOOL, &ifr);
579 +                if(err < 0){
580 +                        /* This could fail if the interface doesn't support the command. Carry
581 +                         * on to the next :)
582 +                         */
583 +                        continue;
584 +                }
585 +
586 +                /* We have a good interface to add */
587 +                network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
588 +                if(network_iface_stats==NULL){
589 +                        return NULL;
590 +                }
591 +                network_iface_stat_ptr = network_iface_stats + ifaces;
592 +                network_iface_stat_ptr->interface_name = strdup(name);
593 +                network_iface_stat_ptr->speed = ethcmd->speed;
594 +                if((ifr.ifr_flags & IFF_UP) != 0){
595 +                        network_iface_stat_ptr->up = 1;
596 +                }else{
597 +                        network_iface_stat_ptr->up = 0;
598 +                }
599 +
600 +                network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
601 +                if(ethcmd->duplex == 0x00){
602 +                        network_iface_stat_ptr->dup = FULL_DUPLEX;
603 +                }
604 +                if(ethcmd->duplex == 0x01){
605 +                        network_iface_stat_ptr->dup = HALF_DUPLEX;
606 +                }
607 +                ifaces++;
608 +        }
609 +        close(sock);
610 +        free(eth_tool_cmd_buf);
611 + #endif
612 +        *entries = ifaces;
613 +        return network_iface_stats;
614 + }
615 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines