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.26 by pajs, Sun Jan 25 20:13:57 2004 UTC

# Line 44 | Line 44
44   #include <sys/socket.h>
45   #include <ifaddrs.h>
46   #include <net/if.h>
47 + #include <net/if_media.h>
48 + #include <sys/ioctl.h>
49   #endif
50  
51   static network_stat_t *network_stats=NULL;
# Line 318 | Line 320 | network_stat_t *get_network_stats_diff(int *entries) {
320          *entries = diff_count;
321          return diff;
322   }
323 + /* NETWORK INTERFACE STATS */
324 +
325 + void network_iface_stat_init(int start, int end, network_iface_stat_t *net_stats){
326 +
327 +        for(net_stats+=start; start<end; start++){
328 +                net_stats->interface_name=NULL;
329 +                net_stats->speed=0;
330 +                net_stats->dup=NO_DUPLEX;
331 +                net_stats++;
332 +        }
333 + }
334 +
335 + network_iface_stat_t *network_iface_stat_malloc(int needed_entries, int *cur_entries, network_iface_stat_t *net_stats){
336 +
337 +        if(net_stats==NULL){
338 +
339 +                if((net_stats=malloc(needed_entries * sizeof(network_iface_stat_t)))==NULL){
340 +                        return NULL;
341 +                }
342 +                network_iface_stat_init(0, needed_entries, net_stats);
343 +                *cur_entries=needed_entries;
344 +
345 +                return net_stats;
346 +        }
347 +
348 +
349 +        if(*cur_entries<needed_entries){
350 +                net_stats=realloc(net_stats, (sizeof(network_iface_stat_t)*needed_entries));
351 +                if(net_stats==NULL){
352 +                        return NULL;
353 +                }
354 +                network_iface_stat_init(*cur_entries, needed_entries, net_stats);
355 +                *cur_entries=needed_entries;
356 +        }
357 +
358 +        return net_stats;
359 + }
360 +
361 + network_iface_stat_t *get_network_iface_stats(int *entries){
362 +        static network_iface_stat_t *network_iface_stats;
363 +        network_iface_stat_t *network_iface_stat_ptr;
364 +        static int sizeof_network_iface_stats=0;        
365 +        static int ifaces;
366 +
367 + #ifdef SOLARIS
368 +        kstat_ctl_t *kc;
369 +        kstat_t *ksp;
370 +        kstat_named_t *knp;
371 + #endif
372 + #ifdef ALLBSD
373 +        struct ifaddrs *net, *net_ptr;
374 +        struct ifmediareq ifmed;
375 +        int s;
376 +        int x;
377 + #endif
378 +
379 +        ifaces = 0;
380 + #ifdef ALLBSD
381 +        if(getifaddrs(&net) != 0){
382 +                return NULL;
383 +        }
384 +
385 +        if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == NULL) return NULL;
386 +
387 +        for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){
388 +                if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
389 +                network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
390 +                if(network_iface_stats==NULL){
391 +                        return NULL;
392 +                }
393 +                network_iface_stat_ptr = network_iface_stats + ifaces;
394 +
395 +                memset(&ifmed, 0, sizeof(struct ifmediareq));
396 +                strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name));
397 +                if(ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){
398 +                        continue;
399 +                }
400 +
401 +                /* We may need to change this if we start doing wireless devices too */
402 +                if( (ifmed.ifm_active | IFM_ETHER) != ifmed.ifm_active ){
403 +                        /* Not a ETHER device */
404 +                        continue;
405 +                }
406 +
407 +                if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
408 +                network_iface_stat_ptr->interface_name = strdup(net_ptr->ifa_name);
409 +                if(network_iface_stat_ptr->interface_name == NULL) return NULL;
410 +
411 +                /* Only intrested in the first 4 bits)  - Assuming only ETHER devices */
412 +                x = ifmed.ifm_active & 0x0f;    
413 +                switch(x){
414 +                        /* 10 Mbit connections. Speedy :) */
415 +                        case(IFM_10_T):
416 +                        case(IFM_10_2):
417 +                        case(IFM_10_5):
418 +                        case(IFM_10_STP):
419 +                        case(IFM_10_FL):
420 +                                network_iface_stat_ptr->speed = 10;
421 +                                break;
422 +                        /* 100 Mbit conneections */
423 +                        case(IFM_100_TX):
424 +                        case(IFM_100_FX):
425 +                        case(IFM_100_T4):
426 +                        case(IFM_100_VG):
427 +                        case(IFM_100_T2):
428 +                                network_iface_stat_ptr->speed = 100;
429 +                                break;
430 +                        /* 1000 Mbit connections */
431 +                        case(IFM_1000_SX):
432 +                        case(IFM_1000_LX):
433 +                        case(IFM_1000_CX):
434 +                        case(IFM_1000_TX):
435 +                                network_iface_stat_ptr->speed = 1000;
436 +                                break;
437 +                        /* We don't know what it is */
438 +                        default:
439 +                                network_iface_stat_ptr->speed = 0;
440 +                                break;
441 +                }
442 +
443 +                if( (ifmed.ifm_active | IFM_FDX) == ifmed.ifm_active ){
444 +                        network_iface_stat_ptr->dup = FULL_DUPLEX;
445 +                }else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){
446 +                        network_iface_stat_ptr->dup = HALF_DUPLEX;
447 +                }else{
448 +                        network_iface_stat_ptr->dup = NO_DUPLEX;
449 +                }
450 +                ifaces++;
451 +        }      
452 +        freeifaddrs(net);
453 + #endif
454 +
455 + #ifdef SOLARIS
456 +        if ((kc = kstat_open()) == NULL) {
457 +                return NULL;
458 +        }
459 +
460 +        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
461 +                if (!strcmp(ksp->ks_class, "net")) {
462 +                        kstat_read(kc, ksp, NULL);
463 +                        if((knp=kstat_data_lookup(ksp, "ifspeed"))==NULL){
464 +                                /* Not a network interface, so skip to the next entry */
465 +                                continue;
466 +                        }
467 +                        network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
468 +                        if(network_iface_stats==NULL){
469 +                                return NULL;
470 +                        }
471 +                        network_iface_stat_ptr = network_iface_stats + ifaces;
472 +                        network_iface_stat_ptr->speed = knp->value.ui64 / (1000*1000);
473 +
474 +                        if((knp=kstat_data_lookup(ksp, "link_duplex"))==NULL){
475 +                                /* Not a network interface, so skip to the next entry */
476 +                                continue;
477 +                        }
478 +
479 +                        if(knp->value.ui64 == 0){
480 +                                network_iface_stat_ptr->dup = FULL_DUPLEX;
481 +                        }else{
482 +                                network_iface_stat_ptr->dup = HALF_DUPLEX;
483 +                        }
484 +
485 +                        if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
486 +                        network_iface_stat_ptr->interface_name = strdup(ksp->ks_name);
487 +                        if(network_iface_stat_ptr->interface_name == NULL) return NULL;
488 +                        ifaces++;
489 +                }
490 +        }
491 +        kstat_close(kc);
492 +        
493 + #endif  
494 +        *entries = ifaces;
495 +        return network_iface_stats;
496 + }
497 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines