ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/network_stats.c
Revision: 1.58
Committed: Sun Apr 4 21:54:48 2004 UTC (20 years, 1 month ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.57: +2 -1 lines
Log Message:
Fix a bunch of warnings.

File Contents

# User Rev Content
1 tdb 1.22 /*
2 pajs 1.1 * i-scream central monitoring system
3 tdb 1.15 * http://www.i-scream.org
4 tdb 1.22 * Copyright (C) 2000-2004 i-scream
5 pajs 1.1 *
6 tdb 1.22 * This library is free software; you can redistribute it and/or
7     * modify it under the terms of the GNU Lesser General Public
8     * License as published by the Free Software Foundation; either
9     * version 2.1 of the License, or (at your option) any later version.
10 pajs 1.1 *
11 tdb 1.22 * This library is distributed in the hope that it will be useful,
12 pajs 1.1 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 tdb 1.22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14     * Lesser General Public License for more details.
15 pajs 1.1 *
16 tdb 1.22 * You should have received a copy of the GNU Lesser General Public
17     * License along with this library; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19     * 02111-1307 USA
20 tdb 1.23 *
21 tdb 1.58 * $Id: network_stats.c,v 1.57 2004/04/04 21:38:50 ats Exp $
22 pajs 1.1 */
23    
24     #ifdef HAVE_CONFIG_H
25     #include "config.h"
26     #endif
27    
28 tdb 1.4 #include <stdlib.h>
29 pajs 1.6 #include <string.h>
30 pajs 1.1 #include "statgrab.h"
31 tdb 1.11 #include <time.h>
32 pajs 1.1 #ifdef SOLARIS
33     #include <kstat.h>
34     #include <sys/sysinfo.h>
35 ats 1.44 #include <sys/types.h>
36     #include <sys/socket.h>
37     #include <sys/ioctl.h>
38     #include <net/if.h>
39     #include <netinet/in.h>
40     #include <sys/sockio.h>
41 pajs 1.6 #endif
42     #ifdef LINUX
43     #include <stdio.h>
44     #include <sys/types.h>
45     #include <regex.h>
46 pajs 1.27 #include <sys/ioctl.h>
47     #include <sys/socket.h>
48     #include <net/if.h>
49     #include <ctype.h>
50 pajs 1.6 #include "tools.h"
51 pajs 1.27 /* Stuff which could be defined by defining KERNEL, but
52     * that would be a bad idea, so we'll just declare it here
53     */
54     typedef __uint8_t u8;
55     typedef __uint16_t u16;
56     typedef __uint32_t u32;
57 ats 1.42 typedef __uint64_t u64;
58 pajs 1.27 #include <linux/ethtool.h>
59     #include <linux/sockios.h>
60 ats 1.32 #include <unistd.h>
61 pajs 1.1 #endif
62 ats 1.18 #ifdef ALLBSD
63 pajs 1.14 #include <sys/types.h>
64     #include <sys/socket.h>
65     #include <ifaddrs.h>
66     #include <net/if.h>
67 pajs 1.26 #include <net/if_media.h>
68     #include <sys/ioctl.h>
69 tdb 1.58 #include <unistd.h>
70 pajs 1.14 #endif
71 pajs 1.1
72 pajs 1.3 static network_stat_t *network_stats=NULL;
73     static int interfaces=0;
74 pajs 1.1
75 pajs 1.3 void network_stat_init(int start, int end, network_stat_t *net_stats){
76    
77     for(net_stats+=start; start<end; start++){
78     net_stats->interface_name=NULL;
79     net_stats->tx=0;
80     net_stats->rx=0;
81 tdb 1.47 net_stats->ipackets=0;
82     net_stats->opackets=0;
83     net_stats->ierrors=0;
84     net_stats->oerrors=0;
85     net_stats->collisions=0;
86 pajs 1.3 net_stats++;
87     }
88     }
89    
90     network_stat_t *network_stat_malloc(int needed_entries, int *cur_entries, network_stat_t *net_stats){
91    
92     if(net_stats==NULL){
93    
94     if((net_stats=malloc(needed_entries * sizeof(network_stat_t)))==NULL){
95 pajs 1.1 return NULL;
96     }
97 pajs 1.3 network_stat_init(0, needed_entries, net_stats);
98     *cur_entries=needed_entries;
99    
100 pajs 1.1 return net_stats;
101     }
102 pajs 1.3
103    
104     if(*cur_entries<needed_entries){
105     net_stats=realloc(net_stats, (sizeof(network_stat_t)*needed_entries));
106     if(net_stats==NULL){
107 pajs 1.1 return NULL;
108     }
109 pajs 1.3 network_stat_init(*cur_entries, needed_entries, net_stats);
110     *cur_entries=needed_entries;
111 pajs 1.1 }
112    
113     return net_stats;
114     }
115    
116 pajs 1.3
117 pajs 1.1 network_stat_t *get_network_stats(int *entries){
118 pajs 1.6
119     static int sizeof_network_stats=0;
120     network_stat_t *network_stat_ptr;
121    
122     #ifdef SOLARIS
123 pajs 1.1 kstat_ctl_t *kc;
124     kstat_t *ksp;
125     kstat_named_t *knp;
126 pajs 1.6 #endif
127 pajs 1.3
128 pajs 1.6 #ifdef LINUX
129     FILE *f;
130 pajs 1.12 /* Horrible big enough, but it should be easily big enough */
131 pajs 1.6 char line[8096];
132     regex_t regex;
133 ats 1.49 regmatch_t line_match[9];
134 pajs 1.14 #endif
135 ats 1.18 #ifdef ALLBSD
136 pajs 1.14 struct ifaddrs *net, *net_ptr;
137     struct if_data *net_data;
138     #endif
139    
140 ats 1.18 #ifdef ALLBSD
141 pajs 1.14 if(getifaddrs(&net) != 0){
142     return NULL;
143     }
144    
145     interfaces=0;
146    
147     for(net_ptr=net;net_ptr!=NULL;net_ptr=net_ptr->ifa_next){
148     if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
149     network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats);
150     if(network_stats==NULL){
151     return NULL;
152     }
153     network_stat_ptr=network_stats+interfaces;
154    
155     if(network_stat_ptr->interface_name!=NULL) free(network_stat_ptr->interface_name);
156     network_stat_ptr->interface_name=strdup(net_ptr->ifa_name);
157     if(network_stat_ptr->interface_name==NULL) return NULL;
158     net_data=(struct if_data *)net_ptr->ifa_data;
159     network_stat_ptr->rx=net_data->ifi_ibytes;
160 tdb 1.46 network_stat_ptr->tx=net_data->ifi_obytes;
161     network_stat_ptr->ipackets=net_data->ifi_ipackets;
162     network_stat_ptr->opackets=net_data->ifi_opackets;
163     network_stat_ptr->ierrors=net_data->ifi_ierrors;
164     network_stat_ptr->oerrors=net_data->ifi_oerrors;
165     network_stat_ptr->collisions=net_data->ifi_collisions;
166 pajs 1.14 network_stat_ptr->systime=time(NULL);
167     interfaces++;
168     }
169     freeifaddrs(net);
170 pajs 1.6 #endif
171 pajs 1.1
172 pajs 1.6 #ifdef SOLARIS
173 pajs 1.1 if ((kc = kstat_open()) == NULL) {
174     return NULL;
175     }
176    
177 pajs 1.6 interfaces=0;
178 pajs 1.3
179 pajs 1.1 for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
180     if (!strcmp(ksp->ks_class, "net")) {
181     kstat_read(kc, ksp, NULL);
182    
183 pajs 1.7 #ifdef SOL7
184 tdb 1.53 #define LRX "rbytes"
185     #define LTX "obytes"
186     #define LIPACKETS "ipackets"
187     #define LOPACKETS "opackets"
188 pajs 1.7 #define VALTYPE value.ui32
189     #else
190 tdb 1.53 #define LRX "rbytes64"
191     #define LTX "obytes64"
192     #define LIPACKETS "ipackets64"
193     #define LOPACKETS "opackets64"
194 pajs 1.7 #define VALTYPE value.ui64
195     #endif
196    
197 tdb 1.53 /* Read rx */
198     if((knp=kstat_data_lookup(ksp, LRX))==NULL){
199 ats 1.44 /* This is a network interface, but it doesn't
200     * have the rbytes/obytes values; for instance,
201     * the loopback devices have this behaviour
202     * (although they do track packets in/out). */
203 ats 1.55 /* FIXME: Show packet counts when byte counts
204     * not available. */
205 pajs 1.1 continue;
206     }
207 pajs 1.3
208 tdb 1.53 /* Create new network_stats */
209 pajs 1.3 network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats);
210 pajs 1.1 if(network_stats==NULL){
211     return NULL;
212     }
213     network_stat_ptr=network_stats+interfaces;
214 tdb 1.53
215     /* Finish reading rx */
216 pajs 1.7 network_stat_ptr->rx=knp->VALTYPE;
217 pajs 1.1
218 tdb 1.53 /* Read tx */
219     if((knp=kstat_data_lookup(ksp, LTX))==NULL){
220 pajs 1.1 continue;
221     }
222 pajs 1.7 network_stat_ptr->tx=knp->VALTYPE;
223 tdb 1.53
224     /* Read ipackets */
225     if((knp=kstat_data_lookup(ksp, LIPACKETS))==NULL){
226     continue;
227     }
228     network_stat_ptr->ipackets=knp->VALTYPE;
229    
230     /* Read opackets */
231     if((knp=kstat_data_lookup(ksp, LOPACKETS))==NULL){
232     continue;
233     }
234     network_stat_ptr->opackets=knp->VALTYPE;
235    
236     /* Read ierrors */
237     if((knp=kstat_data_lookup(ksp, "ierrors"))==NULL){
238     continue;
239     }
240     network_stat_ptr->ierrors=knp->value.ui32;
241    
242     /* Read oerrors */
243     if((knp=kstat_data_lookup(ksp, "oerrors"))==NULL){
244     continue;
245     }
246     network_stat_ptr->oerrors=knp->value.ui32;
247    
248     /* Read collisions */
249     if((knp=kstat_data_lookup(ksp, "collisions"))==NULL){
250     continue;
251     }
252     network_stat_ptr->collisions=knp->value.ui32;
253    
254     /* Read interface name */
255 pajs 1.1 if(network_stat_ptr->interface_name!=NULL){
256     free(network_stat_ptr->interface_name);
257     }
258     network_stat_ptr->interface_name=strdup(ksp->ks_name);
259 pajs 1.3
260 tdb 1.53 /* Store systime */
261 pajs 1.3 network_stat_ptr->systime=time(NULL);
262 tdb 1.53
263 pajs 1.1 interfaces++;
264     }
265     }
266 pajs 1.2
267     kstat_close(kc);
268 pajs 1.6 #endif
269     #ifdef LINUX
270     f=fopen("/proc/net/dev", "r");
271     if(f==NULL){
272     return NULL;
273     }
274     /* read the 2 lines.. Its the title, so we dont care :) */
275     fgets(line, sizeof(line), f);
276     fgets(line, sizeof(line), f);
277    
278    
279 ats 1.50 if((regcomp(&regex, "^ *([^:]+): *([0-9]+) +([0-9]+) +([0-9]+) +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +([0-9]+) +([0-9]+) +([0-9]+) +[0-9]+ +[0-9]+ +([0-9]+)", REG_EXTENDED))!=0){
280 pajs 1.6 return NULL;
281     }
282    
283     interfaces=0;
284 pajs 1.1
285 pajs 1.6 while((fgets(line, sizeof(line), f)) != NULL){
286 ats 1.51 if((regexec(&regex, line, 9, line_match, 0))!=0){
287 pajs 1.6 continue;
288     }
289     network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats);
290     if(network_stats==NULL){
291     return NULL;
292     }
293     network_stat_ptr=network_stats+interfaces;
294    
295     if(network_stat_ptr->interface_name!=NULL){
296     free(network_stat_ptr->interface_name);
297     }
298    
299     network_stat_ptr->interface_name=get_string_match(line, &line_match[1]);
300     network_stat_ptr->rx=get_ll_match(line, &line_match[2]);
301 tdb 1.48 network_stat_ptr->tx=get_ll_match(line, &line_match[5]);
302     network_stat_ptr->ipackets=get_ll_match(line, &line_match[3]);
303     network_stat_ptr->opackets=get_ll_match(line, &line_match[6]);
304     network_stat_ptr->ierrors=get_ll_match(line, &line_match[4]);
305     network_stat_ptr->oerrors=get_ll_match(line, &line_match[7]);
306     network_stat_ptr->collisions=get_ll_match(line, &line_match[8]);
307 pajs 1.6 network_stat_ptr->systime=time(NULL);
308    
309     interfaces++;
310     }
311 pajs 1.12 fclose(f);
312 pajs 1.13 regfree(&regex);
313 pajs 1.6
314     #endif
315 ats 1.20
316     #ifdef CYGWIN
317     return NULL;
318     #endif
319    
320 pajs 1.1 *entries=interfaces;
321    
322     return network_stats;
323 pajs 1.3 }
324    
325 pajs 1.8 long long transfer_diff(long long new, long long old){
326 ats 1.56 #if defined(SOL7) || defined(LINUX) || defined(FREEBSD) || defined(DFBSD) || defined(OPENBSD)
327     /* 32-bit quantities, so we must explicitly deal with wraparound. */
328     #define MAXVAL 0x100000000LL
329     if (new >= old) {
330     return new - old;
331     } else {
332     return MAXVAL + new - old;
333     }
334 pajs 1.8 #else
335 ats 1.56 /* 64-bit quantities, so plain subtraction works. */
336     return new - old;
337 pajs 1.8 #endif
338     }
339    
340 ats 1.24 network_stat_t *get_network_stats_diff(int *entries) {
341     static network_stat_t *diff = NULL;
342     static int diff_count = 0;
343 ats 1.57 network_stat_t *src = NULL, *dest;
344 ats 1.24 int i, j, new_count;
345    
346     if (network_stats == NULL) {
347     /* No previous stats, so we can't calculate a difference. */
348     return get_network_stats(entries);
349 pajs 1.3 }
350    
351 ats 1.24 /* Resize the results array to match the previous stats. */
352     diff = network_stat_malloc(interfaces, &diff_count, diff);
353     if (diff == NULL) {
354 pajs 1.3 return NULL;
355     }
356    
357 ats 1.24 /* Copy the previous stats into the result. */
358     for (i = 0; i < diff_count; i++) {
359     src = &network_stats[i];
360     dest = &diff[i];
361 pajs 1.3
362 ats 1.24 if (dest->interface_name != NULL) {
363     free(dest->interface_name);
364 pajs 1.3 }
365 ats 1.24 dest->interface_name = strdup(src->interface_name);
366     dest->rx = src->rx;
367     dest->tx = src->tx;
368 tdb 1.47 dest->ipackets = src->ipackets;
369     dest->opackets = src->opackets;
370     dest->ierrors = src->ierrors;
371     dest->oerrors = src->oerrors;
372     dest->collisions = src->collisions;
373 ats 1.24 dest->systime = src->systime;
374     }
375 pajs 1.3
376 ats 1.24 /* Get a new set of stats. */
377     if (get_network_stats(&new_count) == NULL) {
378 ats 1.21 return NULL;
379     }
380 pajs 1.3
381 ats 1.24 /* For each previous stat... */
382     for (i = 0; i < diff_count; i++) {
383     dest = &diff[i];
384    
385     /* ... find the corresponding new stat ... */
386     for (j = 0; j < new_count; j++) {
387     /* Try the new stat in the same position first,
388     since that's most likely to be it. */
389     src = &network_stats[(i + j) % new_count];
390     if (strcmp(src->interface_name, dest->interface_name) == 0) {
391     break;
392     }
393     }
394     if (j == new_count) {
395     /* No match found. */
396     continue;
397 pajs 1.3 }
398    
399 ats 1.24 /* ... and subtract the previous stat from it to get the
400     difference. */
401     dest->rx = transfer_diff(src->rx, dest->rx);
402     dest->tx = transfer_diff(src->tx, dest->tx);
403 tdb 1.47 dest->ipackets = transfer_diff(src->ipackets, dest->ipackets);
404     dest->opackets = transfer_diff(src->opackets, dest->opackets);
405     dest->ierrors = transfer_diff(src->ierrors, dest->ierrors);
406     dest->oerrors = transfer_diff(src->oerrors, dest->oerrors);
407     dest->collisions = transfer_diff(src->collisions, dest->collisions);
408 ats 1.24 dest->systime = src->systime - dest->systime;
409 pajs 1.3 }
410    
411 ats 1.24 *entries = diff_count;
412     return diff;
413     }
414 pajs 1.25 /* NETWORK INTERFACE STATS */
415    
416     void network_iface_stat_init(int start, int end, network_iface_stat_t *net_stats){
417    
418     for(net_stats+=start; start<end; start++){
419     net_stats->interface_name=NULL;
420     net_stats->speed=0;
421 ats 1.31 net_stats->dup=UNKNOWN_DUPLEX;
422 pajs 1.25 net_stats++;
423     }
424     }
425    
426     network_iface_stat_t *network_iface_stat_malloc(int needed_entries, int *cur_entries, network_iface_stat_t *net_stats){
427    
428     if(net_stats==NULL){
429    
430     if((net_stats=malloc(needed_entries * sizeof(network_iface_stat_t)))==NULL){
431     return NULL;
432     }
433     network_iface_stat_init(0, needed_entries, net_stats);
434     *cur_entries=needed_entries;
435    
436     return net_stats;
437     }
438    
439    
440     if(*cur_entries<needed_entries){
441     net_stats=realloc(net_stats, (sizeof(network_iface_stat_t)*needed_entries));
442     if(net_stats==NULL){
443     return NULL;
444     }
445     network_iface_stat_init(*cur_entries, needed_entries, net_stats);
446     *cur_entries=needed_entries;
447     }
448    
449     return net_stats;
450     }
451    
452     network_iface_stat_t *get_network_iface_stats(int *entries){
453     static network_iface_stat_t *network_iface_stats;
454     network_iface_stat_t *network_iface_stat_ptr;
455     static int sizeof_network_iface_stats=0;
456 ats 1.43 int ifaces = 0;
457 pajs 1.25
458     #ifdef SOLARIS
459     kstat_ctl_t *kc;
460     kstat_t *ksp;
461     kstat_named_t *knp;
462 ats 1.44 int sock;
463 pajs 1.25 #endif
464 pajs 1.26 #ifdef ALLBSD
465     struct ifaddrs *net, *net_ptr;
466     struct ifmediareq ifmed;
467 pajs 1.36 struct ifreq ifr;
468 ats 1.37 int sock;
469 pajs 1.26 int x;
470     #endif
471 pajs 1.27 #ifdef LINUX
472     FILE *f;
473     /* Horrible big enough, but it should be easily big enough */
474     char line[8096];
475     int sock;
476     #endif
477 ats 1.43
478 pajs 1.26 #ifdef ALLBSD
479     if(getifaddrs(&net) != 0){
480     return NULL;
481     }
482    
483 ats 1.37 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return NULL;
484 pajs 1.26
485     for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){
486     if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
487     network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
488     if(network_iface_stats==NULL){
489     return NULL;
490     }
491     network_iface_stat_ptr = network_iface_stats + ifaces;
492    
493 ats 1.40 memset(&ifr, 0, sizeof(ifr));
494     strncpy(ifr.ifr_name, net_ptr->ifa_name, sizeof(ifr.ifr_name));
495    
496     if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0){
497     continue;
498     }
499     if((ifr.ifr_flags & IFF_UP) != 0){
500     network_iface_stat_ptr->up = 1;
501     }else{
502     network_iface_stat_ptr->up = 0;
503     }
504    
505     if (network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
506     network_iface_stat_ptr->interface_name = strdup(net_ptr->ifa_name);
507     if (network_iface_stat_ptr->interface_name == NULL) return NULL;
508    
509     network_iface_stat_ptr->speed = 0;
510     network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
511     ifaces++;
512    
513 pajs 1.26 memset(&ifmed, 0, sizeof(struct ifmediareq));
514     strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name));
515 ats 1.37 if(ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){
516 ats 1.40 /* Not all interfaces support the media ioctls. */
517 pajs 1.26 continue;
518     }
519    
520     /* We may need to change this if we start doing wireless devices too */
521     if( (ifmed.ifm_active | IFM_ETHER) != ifmed.ifm_active ){
522     /* Not a ETHER device */
523     continue;
524     }
525    
526     /* Only intrested in the first 4 bits) - Assuming only ETHER devices */
527     x = ifmed.ifm_active & 0x0f;
528     switch(x){
529     /* 10 Mbit connections. Speedy :) */
530     case(IFM_10_T):
531     case(IFM_10_2):
532     case(IFM_10_5):
533     case(IFM_10_STP):
534     case(IFM_10_FL):
535     network_iface_stat_ptr->speed = 10;
536     break;
537     /* 100 Mbit conneections */
538     case(IFM_100_TX):
539     case(IFM_100_FX):
540     case(IFM_100_T4):
541     case(IFM_100_VG):
542     case(IFM_100_T2):
543     network_iface_stat_ptr->speed = 100;
544     break;
545     /* 1000 Mbit connections */
546     case(IFM_1000_SX):
547     case(IFM_1000_LX):
548     case(IFM_1000_CX):
549 tdb 1.54 #if defined(IFM_1000_TX) && !defined(OPENBSD)
550     case(IFM_1000_TX): /* FreeBSD 4 and others (but NOT OpenBSD)? */
551 tdb 1.45 #endif
552     #ifdef IFM_1000_FX
553     case(IFM_1000_FX): /* FreeBSD 4 */
554     #endif
555     #ifdef IFM_1000_T
556     case(IFM_1000_T): /* FreeBSD 5 */
557 tdb 1.28 #endif
558 pajs 1.26 network_iface_stat_ptr->speed = 1000;
559     break;
560     /* We don't know what it is */
561     default:
562     network_iface_stat_ptr->speed = 0;
563     break;
564     }
565    
566     if( (ifmed.ifm_active | IFM_FDX) == ifmed.ifm_active ){
567     network_iface_stat_ptr->dup = FULL_DUPLEX;
568     }else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){
569     network_iface_stat_ptr->dup = HALF_DUPLEX;
570     }else{
571 ats 1.31 network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
572 pajs 1.26 }
573 pajs 1.36
574 pajs 1.26 }
575     freeifaddrs(net);
576 ats 1.37 close(sock);
577 pajs 1.26 #endif
578 pajs 1.25
579     #ifdef SOLARIS
580 ats 1.44 if ((kc = kstat_open()) == NULL) {
581     return NULL;
582     }
583    
584     if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) {
585     return NULL;
586     }
587 pajs 1.25
588 ats 1.44 for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
589     if (!strcmp(ksp->ks_class, "net")) {
590     struct ifreq ifr;
591    
592     kstat_read(kc, ksp, NULL);
593    
594     strncpy(ifr.ifr_name, ksp->ks_name, sizeof ifr.ifr_name);
595     if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
596     /* Not a network interface. */
597 pajs 1.25 continue;
598     }
599 ats 1.44
600     network_iface_stats = network_iface_stat_malloc(ifaces + 1, &sizeof_network_iface_stats, network_iface_stats);
601     if (network_iface_stats == NULL) {
602 pajs 1.25 return NULL;
603     }
604 pajs 1.26 network_iface_stat_ptr = network_iface_stats + ifaces;
605 ats 1.44 ifaces++;
606    
607     if (network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
608     network_iface_stat_ptr->interface_name = strdup(ksp->ks_name);
609     if (network_iface_stat_ptr->interface_name == NULL) return NULL;
610 pajs 1.25
611 ats 1.44 if ((ifr.ifr_flags & IFF_UP) != 0) {
612     network_iface_stat_ptr->up = 1;
613     } else {
614     network_iface_stat_ptr->up = 1;
615     }
616 pajs 1.35
617 ats 1.44 if ((knp = kstat_data_lookup(ksp, "ifspeed")) != NULL) {
618     network_iface_stat_ptr->speed = knp->value.ui64 / (1000 * 1000);
619     } else {
620     network_iface_stat_ptr->speed = 0;
621 pajs 1.25 }
622    
623 pajs 1.33 network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
624 ats 1.44 if ((knp = kstat_data_lookup(ksp, "link_duplex")) != NULL) {
625     switch (knp->value.ui32) {
626     case 1:
627     network_iface_stat_ptr->dup = HALF_DUPLEX;
628     break;
629     case 2:
630     network_iface_stat_ptr->dup = FULL_DUPLEX;
631     break;
632     }
633 pajs 1.33 }
634 pajs 1.25 }
635     }
636 ats 1.44
637     close(sock);
638 pajs 1.25 kstat_close(kc);
639     #endif
640 pajs 1.27 #ifdef LINUX
641     f = fopen("/proc/net/dev", "r");
642     if(f == NULL){
643     return NULL;
644     }
645    
646     /* Setup stuff so we can do the ioctl to get the info */
647     if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
648     return NULL;
649     }
650    
651     /* Ignore first 2 lines.. Just headings */
652 pajs 1.29 if((fgets(line, sizeof(line), f)) == NULL) return NULL;
653     if((fgets(line, sizeof(line), f)) == NULL) return NULL;
654 pajs 1.27
655     while((fgets(line, sizeof(line), f)) != NULL){
656     char *name, *ptr;
657     struct ifreq ifr;
658 ats 1.38 struct ethtool_cmd ethcmd;
659 pajs 1.27 int err;
660    
661     /* Get the interface name */
662     ptr = strchr(line, ':');
663     if (ptr == NULL) continue;
664     *ptr='\0';
665     name = line;
666     while(isspace(*(name))){
667     name++;
668     }
669    
670 ats 1.38 memset(&ifr, 0, sizeof ifr);
671     strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
672 pajs 1.27
673 ats 1.38 if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
674     continue;
675     }
676 pajs 1.27
677     /* We have a good interface to add */
678     network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
679     if(network_iface_stats==NULL){
680     return NULL;
681     }
682     network_iface_stat_ptr = network_iface_stats + ifaces;
683     network_iface_stat_ptr->interface_name = strdup(name);
684 ats 1.38 if ((ifr.ifr_flags & IFF_UP) != 0) {
685 pajs 1.35 network_iface_stat_ptr->up = 1;
686 ats 1.38 } else {
687 pajs 1.35 network_iface_stat_ptr->up = 0;
688     }
689    
690 ats 1.38 memset(&ethcmd, 0, sizeof ethcmd);
691     ethcmd.cmd = ETHTOOL_GSET;
692     ifr.ifr_data = (caddr_t) &ethcmd;
693    
694     err = ioctl(sock, SIOCETHTOOL, &ifr);
695     if (err == 0) {
696     network_iface_stat_ptr->speed = ethcmd.speed;
697    
698     switch (ethcmd.duplex) {
699     case 0x00:
700     network_iface_stat_ptr->dup = FULL_DUPLEX;
701     break;
702     case 0x01:
703     network_iface_stat_ptr->dup = HALF_DUPLEX;
704     break;
705     default:
706     network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
707     }
708     } else {
709     /* Not all interfaces support the ethtool ioctl. */
710 ats 1.39 network_iface_stat_ptr->speed = 0;
711 ats 1.38 network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
712 pajs 1.27 }
713 ats 1.38
714 pajs 1.27 ifaces++;
715     }
716 pajs 1.30 close(sock);
717 ats 1.38 fclose(f);
718 pajs 1.27 #endif
719 pajs 1.26 *entries = ifaces;
720 pajs 1.25 return network_iface_stats;
721     }
722