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.29 by pajs, Fri Feb 13 14:39:34 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   #endif
54   #ifdef ALLBSD
55   #include <sys/types.h>
56   #include <sys/socket.h>
57   #include <ifaddrs.h>
58   #include <net/if.h>
59 + #include <net/if_media.h>
60 + #include <sys/ioctl.h>
61   #endif
62  
63   static network_stat_t *network_stats=NULL;
# Line 318 | Line 332 | network_stat_t *get_network_stats_diff(int *entries) {
332          *entries = diff_count;
333          return diff;
334   }
335 + /* NETWORK INTERFACE STATS */
336 +
337 + void network_iface_stat_init(int start, int end, network_iface_stat_t *net_stats){
338 +
339 +        for(net_stats+=start; start<end; start++){
340 +                net_stats->interface_name=NULL;
341 +                net_stats->speed=0;
342 +                net_stats->dup=UNKNOWN_DUPEX;
343 +                net_stats++;
344 +        }
345 + }
346 +
347 + network_iface_stat_t *network_iface_stat_malloc(int needed_entries, int *cur_entries, network_iface_stat_t *net_stats){
348 +
349 +        if(net_stats==NULL){
350 +
351 +                if((net_stats=malloc(needed_entries * sizeof(network_iface_stat_t)))==NULL){
352 +                        return NULL;
353 +                }
354 +                network_iface_stat_init(0, needed_entries, net_stats);
355 +                *cur_entries=needed_entries;
356 +
357 +                return net_stats;
358 +        }
359 +
360 +
361 +        if(*cur_entries<needed_entries){
362 +                net_stats=realloc(net_stats, (sizeof(network_iface_stat_t)*needed_entries));
363 +                if(net_stats==NULL){
364 +                        return NULL;
365 +                }
366 +                network_iface_stat_init(*cur_entries, needed_entries, net_stats);
367 +                *cur_entries=needed_entries;
368 +        }
369 +
370 +        return net_stats;
371 + }
372 +
373 + network_iface_stat_t *get_network_iface_stats(int *entries){
374 +        static network_iface_stat_t *network_iface_stats;
375 +        network_iface_stat_t *network_iface_stat_ptr;
376 +        static int sizeof_network_iface_stats=0;        
377 +        static int ifaces;
378 +
379 + #ifdef SOLARIS
380 +        kstat_ctl_t *kc;
381 +        kstat_t *ksp;
382 +        kstat_named_t *knp;
383 + #endif
384 + #ifdef ALLBSD
385 +        struct ifaddrs *net, *net_ptr;
386 +        struct ifmediareq ifmed;
387 +        int s;
388 +        int x;
389 + #endif
390 + #ifdef LINUX
391 +        FILE *f;
392 +        /* Horrible big enough, but it should be easily big enough */
393 +        char line[8096];
394 +        void *eth_tool_cmd_buf;
395 +        int buf_size;
396 +        int sock;
397 + #endif
398 +        ifaces = 0;
399 + #ifdef ALLBSD
400 +        if(getifaddrs(&net) != 0){
401 +                return NULL;
402 +        }
403 +
404 +        if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == NULL) 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(s, 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 + #ifdef FREEBSD5
454 +                        case(IFM_1000_T):
455 + #else
456 +                        case(IFM_1000_TX):
457 +                        case(IFM_1000_FX):
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_DUPEX;
473 +                }
474 +                ifaces++;
475 +        }      
476 +        freeifaddrs(net);
477 + #endif
478 +
479 + #ifdef SOLARIS
480 +        if ((kc = kstat_open()) == NULL) {
481 +                return NULL;
482 +        }
483 +
484 +        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
485 +                if (!strcmp(ksp->ks_class, "net")) {
486 +                        kstat_read(kc, ksp, NULL);
487 +                        if((knp=kstat_data_lookup(ksp, "ifspeed"))==NULL){
488 +                                /* Not a network interface, so skip to the next entry */
489 +                                continue;
490 +                        }
491 +                        network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
492 +                        if(network_iface_stats==NULL){
493 +                                return NULL;
494 +                        }
495 +                        network_iface_stat_ptr = network_iface_stats + ifaces;
496 +                        network_iface_stat_ptr->speed = knp->value.ui64 / (1000*1000);
497 +
498 +                        if((knp=kstat_data_lookup(ksp, "link_duplex"))==NULL){
499 +                                /* Not a network interface, so skip to the next entry */
500 +                                continue;
501 +                        }
502 +
503 +                        if(knp->value.ui64 == 0){
504 +                                network_iface_stat_ptr->dup = FULL_DUPLEX;
505 +                        }else{
506 +                                network_iface_stat_ptr->dup = HALF_DUPLEX;
507 +                        }
508 +
509 +                        if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
510 +                        network_iface_stat_ptr->interface_name = strdup(ksp->ks_name);
511 +                        if(network_iface_stat_ptr->interface_name == NULL) return NULL;
512 +                        ifaces++;
513 +                }
514 +        }
515 +        kstat_close(kc);
516 +        
517 + #endif  
518 + #ifdef LINUX
519 +
520 +        f = fopen("/proc/net/dev", "r");
521 +        if(f == NULL){
522 +                return NULL;
523 +        }
524 +
525 +        /* Setup stuff so we can do the ioctl to get the info */
526 +        if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
527 +                return NULL;
528 +        }
529 +
530 +        buf_size = sizeof(struct ethtool_cmd);
531 +        eth_tool_cmd_buf = malloc(buf_size);
532 +        if(eth_tool_cmd_buf == NULL) return NULL;
533 +
534 +        /* Ignore first 2 lines.. Just headings */
535 +        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
536 +        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
537 +
538 +        while((fgets(line, sizeof(line), f)) != NULL){
539 +                char *name, *ptr;
540 +                struct ifreq ifr;
541 +                struct ethtool_cmd *ethcmd;
542 +                int err;
543 +
544 +                /* Get the interface name */
545 +                ptr = strchr(line, ':');
546 +                if (ptr == NULL) continue;
547 +                *ptr='\0';
548 +                name = line;
549 +                while(isspace(*(name))){
550 +                        name++;
551 +                }
552 +
553 +                memset(&ifr, 0, sizeof(ifr));
554 +                memset(eth_tool_cmd_buf, 0, buf_size);
555 +                ifr.ifr_data = (caddr_t) eth_tool_cmd_buf;
556 +                strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
557 +
558 +                ethcmd = (struct ethtool_cmd *) ifr.ifr_data;
559 +                ethcmd->cmd = ETHTOOL_GSET;
560 +
561 +                err = ioctl(sock, SIOCETHTOOL, &ifr);
562 +                if(err < 0){
563 +                        /* This could fail if the interface doesn't support the command. Carry
564 +                         * on to the next :)
565 +                         */
566 +                        continue;
567 +                }
568 +
569 +                /* We have a good interface to add */
570 +                network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
571 +                if(network_iface_stats==NULL){
572 +                        return NULL;
573 +                }
574 +                network_iface_stat_ptr = network_iface_stats + ifaces;
575 +                network_iface_stat_ptr->interface_name = strdup(name);
576 +                network_iface_stat_ptr->speed = ethcmd->speed;
577 +                network_iface_stat_ptr->dup = UNKNOWN_DUPEX;
578 +                if(ethcmd->duplex == 0x00){
579 +                        network_iface_stat_ptr->dup = FULL_DUPLEX;
580 +                }
581 +                if(ethcmd->duplex == 0x01){
582 +                        network_iface_stat_ptr->dup = HALF_DUPLEX;
583 +                }
584 +                ifaces++;
585 +        }
586 +
587 +        free(eth_tool_cmd_buf);
588 + #endif
589 +        *entries = ifaces;
590 +        return network_iface_stats;
591 + }
592 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines