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.1 by pajs, Tue Feb 18 19:28:30 2003 UTC vs.
Revision 1.78 by tdb, Mon Oct 9 14:09:38 2006 UTC

# Line 1 | Line 1
1 < /*
2 < * i-scream central monitoring system
3 < * http://www.i-scream.org.uk
4 < * Copyright (C) 2000-2002 i-scream
1 > /*
2 > * i-scream libstatgrab
3 > * http://www.i-scream.org
4 > * Copyright (C) 2000-2004 i-scream
5   *
6 < * This program is free software; you can redistribute it and/or
7 < * modify it under the terms of the GNU General Public License
8 < * as published by the Free Software Foundation; either version 2
9 < * of the License, or (at your option) any later version.
6 > * 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   *
11 < * This program is distributed in the hope that it will be useful,
11 > * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 < * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 < * GNU General Public License for more details.
13 > * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 > * Lesser General Public License for more details.
15   *
16 < * You should have received a copy of the GNU General Public License
17 < * along with this program; if not, write to the Free Software
18 < * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16 > * 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 > *
21 > * $Id$
22   */
23  
24   #ifdef HAVE_CONFIG_H
25   #include "config.h"
26   #endif
27  
25 #include <stdio.h>
26 #include "statgrab.h"
28   #include <stdlib.h>
29 + #include <string.h>
30 + #include "statgrab.h"
31 + #include "vector.h"
32 + #include "tools.h"
33 + #include <time.h>
34   #ifdef SOLARIS
35   #include <kstat.h>
36   #include <sys/sysinfo.h>
37 < #include <string.h>
37 > #include <sys/types.h>
38 > #include <sys/socket.h>
39 > #include <sys/ioctl.h>
40 > #include <net/if.h>
41 > #include <netinet/in.h>
42 > #include <sys/sockio.h>
43 > #include <unistd.h>
44   #endif
45 + #ifdef LINUX
46 + #include <stdio.h>
47 + #include <sys/types.h>
48 + #include <sys/ioctl.h>
49 + #include <sys/socket.h>
50 + #include <net/if.h>
51 + #include <ctype.h>
52 + #include <linux/version.h>
53 + #include <asm/types.h>
54 + /* These aren't defined by asm/types.h unless the kernel is being
55 +   compiled, but some versions of ethtool.h need them. */
56 + typedef __uint8_t u8;
57 + typedef __uint16_t u16;
58 + typedef __uint32_t u32;
59 + typedef __uint64_t u64;
60 + #include <linux/ethtool.h>
61 + #include <linux/sockios.h>
62 + #include <unistd.h>
63 + #endif
64 + #ifdef ALLBSD
65 + #include <sys/types.h>
66 + #include <sys/socket.h>
67 + #include <ifaddrs.h>
68 + #include <net/if.h>
69 + #include <net/if_media.h>
70 + #include <sys/ioctl.h>
71 + #include <unistd.h>
72 + #endif
73 + #ifdef WIN32
74 + #include <windows.h>
75 + #include <Iphlpapi.h>
76 + #include "win32.h"
77 + #endif
78  
79 < #define START_VAL 1
79 > static void network_stat_init(sg_network_io_stats *s) {
80 >        s->interface_name = NULL;
81 >        s->tx = 0;
82 >        s->rx = 0;
83 >        s->ipackets = 0;
84 >        s->opackets = 0;
85 >        s->ierrors = 0;
86 >        s->oerrors = 0;
87 >        s->collisions = 0;
88 > }
89  
90 < network_stat_t *network_stat_init(int num_iface, int *wmark, network_stat_t *net_stats){
91 <        int x;
92 <        network_stat_t *net_stats_ptr;
90 > static void network_stat_destroy(sg_network_io_stats *s) {
91 >        free(s->interface_name);
92 > }
93  
94 <        if(*wmark==-1){
95 <                printf("new malloc\n");
96 <                if((net_stats=malloc(START_VAL * sizeof(network_stat_t)))==NULL){
97 <                        return NULL;
98 <                }
99 <                *wmark=START_VAL;
100 <                net_stats_ptr=net_stats;
101 <                for(x=0;x<(*wmark);x++){
102 <                        net_stats_ptr->interface_name=NULL;
103 <                        net_stats_ptr++;
104 <                }
105 <                return net_stats;
94 > VECTOR_DECLARE_STATIC(network_stats, sg_network_io_stats, 5,
95 >                      network_stat_init, network_stat_destroy);
96 >
97 > #ifdef WIN32
98 > static PMIB_IFTABLE win32_get_devices()
99 > {
100 >        PMIB_IFTABLE if_table;
101 >        PMIB_IFTABLE tmp;
102 >        unsigned long dwSize = 0;
103 >
104 >        // Allocate memory for pointers
105 >        if_table = sg_malloc(sizeof(MIB_IFTABLE));
106 >        if(if_table == NULL) {
107 >                return NULL;
108          }
109 <        if(num_iface>(*wmark-1)){
110 <                if((net_stats=realloc(net_stats, (*wmark)*2 * sizeof(network_stat_t)))==NULL){
109 >
110 >        // Get necessary size for the buffer
111 >        if(GetIfTable(if_table, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) {
112 >                tmp = sg_realloc(if_table, dwSize);
113 >                if(tmp == NULL) {
114 >                        free(if_table);
115                          return NULL;
116                  }
117 <
58 <                *wmark=(*wmark)*2;
59 <                net_stats_ptr=net_stats+num_iface;
60 <                for(x=num_iface;x<(*wmark);x++){
61 <                        net_stats_ptr->interface_name=NULL;
62 <                        net_stats_ptr++;
63 <                }
64 <                return net_stats;
117 >                if_table = tmp;
118          }
119  
120 <        return net_stats;
120 >        // Get the data
121 >        if(GetIfTable(if_table, &dwSize, 0) != NO_ERROR) {
122 >                free(if_table);
123 >                return NULL;
124 >        }
125 >        return if_table;
126   }
127 + #endif /* WIN32 */
128  
129 < network_stat_t *get_network_stats(int *entries){
130 <        kstat_ctl_t *kc;
131 <        kstat_t *ksp;
129 > sg_network_io_stats *sg_get_network_io_stats(int *entries){
130 >        int interfaces;
131 >        sg_network_io_stats *network_stat_ptr;
132 >
133 > #ifdef SOLARIS
134 >        kstat_ctl_t *kc;
135 >        kstat_t *ksp;
136          kstat_named_t *knp;
137 + #endif
138 +
139 + #ifdef LINUX
140 +        FILE *f;
141 +        /* Horrible big enough, but it should be easily big enough */
142 +        char line[8096];
143 +        regex_t regex;
144 +        regmatch_t line_match[9];
145 + #endif
146 + #ifdef ALLBSD
147 +        struct ifaddrs *net, *net_ptr;
148 +        struct if_data *net_data;
149 + #endif
150 + #ifdef WIN32
151 +        PMIB_IFTABLE if_table;
152 +        MIB_IFROW if_row;
153 +        int i, no, j;
154 +
155 +        /* used for duplicate interface names. 5 for space, hash, up to two
156 +         * numbers and terminating slash */
157 +        char buf[5];
158 + #endif
159 +
160 + #ifdef ALLBSD
161 +        if(getifaddrs(&net) != 0){
162 +                sg_set_error_with_errno(SG_ERROR_GETIFADDRS, NULL);
163 +                return NULL;
164 +        }
165 +
166 +        interfaces=0;
167          
168 <        int interfaces=0;      
169 <        network_stat_t *network_stat_ptr;
77 <        static network_stat_t *network_stats=NULL;
78 <        static int watermark=-1;
168 >        for(net_ptr=net;net_ptr!=NULL;net_ptr=net_ptr->ifa_next){
169 >                if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
170  
171 <        if ((kc = kstat_open()) == NULL) {
172 <                return NULL;
173 <        }
171 >                if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) {
172 >                        return NULL;
173 >                }
174 >                network_stat_ptr=network_stats+interfaces;
175 >                
176 >                if (sg_update_string(&network_stat_ptr->interface_name,
177 >                                     net_ptr->ifa_name) < 0) {
178 >                        return NULL;
179 >                }
180 >                net_data=(struct if_data *)net_ptr->ifa_data;
181 >                network_stat_ptr->rx=net_data->ifi_ibytes;
182 >                network_stat_ptr->tx=net_data->ifi_obytes;
183 >                network_stat_ptr->ipackets=net_data->ifi_ipackets;
184 >                network_stat_ptr->opackets=net_data->ifi_opackets;
185 >                network_stat_ptr->ierrors=net_data->ifi_ierrors;
186 >                network_stat_ptr->oerrors=net_data->ifi_oerrors;
187 >                network_stat_ptr->collisions=net_data->ifi_collisions;
188 >                network_stat_ptr->systime=time(NULL);
189 >                interfaces++;
190 >        }
191 >        freeifaddrs(net);      
192 > #endif
193  
194 <        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
195 <                if (!strcmp(ksp->ks_class, "net")) {
196 <                        kstat_read(kc, ksp, NULL);
194 > #ifdef SOLARIS
195 >        if ((kc = kstat_open()) == NULL) {
196 >                sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
197 >                return NULL;
198 >        }
199  
200 <                        if((knp=kstat_data_lookup(ksp, "rbytes64"))==NULL){
201 <                                /* Not a network interface, so skip to the next entry */
200 >        interfaces=0;
201 >
202 >        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
203 >                if (strcmp(ksp->ks_class, "net") == 0) {
204 >                        kstat_read(kc, ksp, NULL);
205 >
206 > #ifdef SOL7
207 > #define LRX "rbytes"
208 > #define LTX "obytes"
209 > #define LIPACKETS "ipackets"
210 > #define LOPACKETS "opackets"
211 > #define VALTYPE value.ui32
212 > #else
213 > #define LRX "rbytes64"
214 > #define LTX "obytes64"
215 > #define LIPACKETS "ipackets64"
216 > #define LOPACKETS "opackets64"
217 > #define VALTYPE value.ui64
218 > #endif
219 >
220 >                        /* Read rx */
221 >                        if((knp=kstat_data_lookup(ksp, LRX))==NULL){
222 >                                /* This is a network interface, but it doesn't
223 >                                 * have the rbytes/obytes values; for instance,
224 >                                 * the loopback devices have this behaviour
225 >                                 * (although they do track packets in/out). */
226 >                                /* FIXME: Show packet counts when byte counts
227 >                                 * not available. */
228                                  continue;
229                          }
230 <                        network_stats=network_stat_init(interfaces, &watermark, network_stats);
231 <                        if(network_stats==NULL){
230 >
231 >                        /* Create new network_stats */
232 >                        if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) {
233 >                                kstat_close(kc);
234                                  return NULL;
235                          }
236                          network_stat_ptr=network_stats+interfaces;
97                        network_stat_ptr->rx=knp->value.ui64;
237  
238 <                        if((knp=kstat_data_lookup(ksp, "obytes64"))==NULL){
239 <                                /* Not a network interface, so skip to the next entry */
238 >                        /* Finish reading rx */
239 >                        network_stat_ptr->rx=knp->VALTYPE;
240 >
241 >                        /* Read tx */
242 >                        if((knp=kstat_data_lookup(ksp, LTX))==NULL){
243                                  continue;
244                          }
245 <                        network_stat_ptr->tx=knp->value.ui64;
246 <                        if(network_stat_ptr->interface_name!=NULL){
247 <                                free(network_stat_ptr->interface_name);
245 >                        network_stat_ptr->tx=knp->VALTYPE;
246 >
247 >                        /* Read ipackets */
248 >                        if((knp=kstat_data_lookup(ksp, LIPACKETS))==NULL){
249 >                                continue;
250                          }
251 <                        network_stat_ptr->interface_name=strdup(ksp->ks_name);
251 >                        network_stat_ptr->ipackets=knp->VALTYPE;
252 >
253 >                        /* Read opackets */
254 >                        if((knp=kstat_data_lookup(ksp, LOPACKETS))==NULL){
255 >                                continue;
256 >                        }
257 >                        network_stat_ptr->opackets=knp->VALTYPE;
258 >
259 >                        /* Read ierrors */
260 >                        if((knp=kstat_data_lookup(ksp, "ierrors"))==NULL){
261 >                                continue;
262 >                        }
263 >                        network_stat_ptr->ierrors=knp->value.ui32;
264 >
265 >                        /* Read oerrors */
266 >                        if((knp=kstat_data_lookup(ksp, "oerrors"))==NULL){
267 >                                continue;
268 >                        }
269 >                        network_stat_ptr->oerrors=knp->value.ui32;
270 >
271 >                        /* Read collisions */
272 >                        if((knp=kstat_data_lookup(ksp, "collisions"))==NULL){
273 >                                continue;
274 >                        }
275 >                        network_stat_ptr->collisions=knp->value.ui32;
276 >
277 >                        /* Read interface name */
278 >                        if (sg_update_string(&network_stat_ptr->interface_name,
279 >                                             ksp->ks_name) < 0) {
280 >                                kstat_close(kc);
281 >                                return NULL;
282 >                        }
283 >
284 >                        /* Store systime */
285 >                        network_stat_ptr->systime=time(NULL);
286 >
287                          interfaces++;
288                  }
289          }
290 <                        
290 >                
291 >        kstat_close(kc);        
292 > #endif
293 > #ifdef LINUX
294 >        f=fopen("/proc/net/dev", "r");
295 >        if(f==NULL){
296 >                sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/net/dev");
297 >                return NULL;
298 >        }
299 >        /* read the 2 lines.. Its the title, so we dont care :) */
300 >        fgets(line, sizeof(line), f);
301 >        fgets(line, sizeof(line), f);
302  
303 +
304 +        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){
305 +                sg_set_error(SG_ERROR_PARSE, NULL);
306 +                return NULL;
307 +        }
308 +
309 +        interfaces=0;
310 +
311 +        while((fgets(line, sizeof(line), f)) != NULL){
312 +                if((regexec(&regex, line, 9, line_match, 0))!=0){
313 +                        continue;
314 +                }
315 +
316 +                if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) {
317 +                        return NULL;
318 +                }
319 +                network_stat_ptr=network_stats+interfaces;
320 +
321 +                if(network_stat_ptr->interface_name!=NULL){
322 +                        free(network_stat_ptr->interface_name);
323 +                }
324 +
325 +                network_stat_ptr->interface_name=sg_get_string_match(line, &line_match[1]);
326 +                network_stat_ptr->rx=sg_get_ll_match(line, &line_match[2]);
327 +                network_stat_ptr->tx=sg_get_ll_match(line, &line_match[5]);
328 +                network_stat_ptr->ipackets=sg_get_ll_match(line, &line_match[3]);
329 +                network_stat_ptr->opackets=sg_get_ll_match(line, &line_match[6]);
330 +                network_stat_ptr->ierrors=sg_get_ll_match(line, &line_match[4]);
331 +                network_stat_ptr->oerrors=sg_get_ll_match(line, &line_match[7]);
332 +                network_stat_ptr->collisions=sg_get_ll_match(line, &line_match[8]);
333 +                network_stat_ptr->systime=time(NULL);
334 +
335 +                interfaces++;
336 +        }
337 +        fclose(f);
338 +        regfree(&regex);
339 +
340 + #endif
341 +
342 + #ifdef CYGWIN
343 +        sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin");
344 +        return NULL;
345 + #endif
346 + #ifdef HPUX
347 +        sg_set_error(SG_ERROR_UNSUPPORTED, "HP-UX");
348 +        return NULL;
349 + #endif
350 +
351 + #ifdef WIN32
352 +        interfaces = 0;
353 +
354 +        if((if_table = win32_get_devices()) == NULL) {
355 +                sg_set_error(SG_ERROR_DEVICES, "network");
356 +                return NULL;
357 +        }
358 +
359 +        if(VECTOR_RESIZE(network_stats, if_table->dwNumEntries) < 0) {
360 +                free(if_table);
361 +                return NULL;
362 +        }
363 +
364 +        for (i=0; i<if_table->dwNumEntries; i++) {
365 +                network_stat_ptr=network_stats+i;
366 +                if_row = if_table->table[i];
367 +
368 +                if(sg_update_string(&network_stat_ptr->interface_name,
369 +                                        if_row.bDescr) < 0) {
370 +                        free(if_table);
371 +                        return NULL;
372 +                }
373 +                network_stat_ptr->tx = if_row.dwOutOctets;
374 +                network_stat_ptr->rx = if_row.dwInOctets;
375 +                network_stat_ptr->ipackets = if_row.dwInUcastPkts + if_row.dwInNUcastPkts;
376 +                network_stat_ptr->opackets = if_row.dwOutUcastPkts + if_row.dwOutNUcastPkts;
377 +                network_stat_ptr->ierrors = if_row.dwInErrors;
378 +                network_stat_ptr->oerrors = if_row.dwOutErrors;
379 +                network_stat_ptr->collisions = 0; /* can't do that */
380 +                network_stat_ptr->systime = time(NULL);
381 +
382 +                interfaces++;
383 +        }
384 +        free(if_table);
385 +
386 +        /* Please say there's a nicer way to do this...  If windows has two (or
387 +         * more) identical network cards, GetIfTable returns them with the same
388 +         * name, not like in Device Manager where the other has a #2 etc after
389 +         * it. So, add the #number here. Should we be doing this? Or should the
390 +         * end programs be dealing with duplicate names? Currently breaks
391 +         * watch.pl in rrdgraphing. But Unix does not have the issue of
392 +         * duplicate net device names.
393 +         */
394 +        for (i=0; i<interfaces; i++) {
395 +                no = 2;
396 +                for(j=i+1; j<interfaces; j++) {
397 +                        network_stat_ptr=network_stats+j;
398 +                        if(strcmp(network_stats[i].interface_name,
399 +                                        network_stat_ptr->interface_name) == 0) {
400 +                                if(snprintf(buf, sizeof(buf), " #%d", no) < 0) {
401 +                                        break;
402 +                                }
403 +                                if(sg_concat_string(&network_stat_ptr->interface_name, buf) != 0) {
404 +                                        return NULL;
405 +                                }
406 +
407 +                                no++;
408 +                        }
409 +                }
410 +        }
411 + #endif
412 +
413          *entries=interfaces;
414  
415          return network_stats;  
416 + }
417  
418 + static long long transfer_diff(long long new, long long old){
419 + #if defined(SOL7) || defined(LINUX) || defined(FREEBSD) || defined(DFBSD) || defined(OPENBSD) || defined(WIN32)
420 +        /* 32-bit quantities, so we must explicitly deal with wraparound. */
421 + #define MAXVAL 0x100000000LL
422 +        if (new >= old) {
423 +                return new - old;
424 +        } else {
425 +                return MAXVAL + new - old;
426 +        }
427 + #else
428 +        /* 64-bit quantities, so plain subtraction works. */
429 +        return new - old;
430 + #endif
431 + }
432 +
433 + sg_network_io_stats *sg_get_network_io_stats_diff(int *entries) {
434 +        VECTOR_DECLARE_STATIC(diff, sg_network_io_stats, 1,
435 +                              network_stat_init, network_stat_destroy);
436 +        sg_network_io_stats *src = NULL, *dest;
437 +        int i, j, diff_count, new_count;
438 +
439 +        if (network_stats == NULL) {
440 +                /* No previous stats, so we can't calculate a difference. */
441 +                return sg_get_network_io_stats(entries);
442 +        }
443 +
444 +        /* Resize the results array to match the previous stats. */
445 +        diff_count = VECTOR_SIZE(network_stats);
446 +        if (VECTOR_RESIZE(diff, diff_count) < 0) {
447 +                return NULL;
448 +        }
449 +
450 +        /* Copy the previous stats into the result. */
451 +        for (i = 0; i < diff_count; i++) {
452 +                src = &network_stats[i];
453 +                dest = &diff[i];
454 +
455 +                if (sg_update_string(&dest->interface_name,
456 +                                     src->interface_name) < 0) {
457 +                        return NULL;
458 +                }
459 +                dest->rx = src->rx;
460 +                dest->tx = src->tx;
461 +                dest->ipackets = src->ipackets;
462 +                dest->opackets = src->opackets;
463 +                dest->ierrors = src->ierrors;
464 +                dest->oerrors = src->oerrors;
465 +                dest->collisions = src->collisions;
466 +                dest->systime = src->systime;
467 +        }
468 +
469 +        /* Get a new set of stats. */
470 +        if (sg_get_network_io_stats(&new_count) == NULL) {
471 +                return NULL;
472 +        }
473 +
474 +        /* For each previous stat... */
475 +        for (i = 0; i < diff_count; i++) {
476 +                dest = &diff[i];
477 +
478 +                /* ... find the corresponding new stat ... */
479 +                for (j = 0; j < new_count; j++) {
480 +                        /* Try the new stat in the same position first,
481 +                           since that's most likely to be it. */
482 +                        src = &network_stats[(i + j) % new_count];
483 +                        if (strcmp(src->interface_name, dest->interface_name) == 0) {
484 +                                break;
485 +                        }
486 +                }
487 +                if (j == new_count) {
488 +                        /* No match found. */
489 +                        continue;
490 +                }
491 +
492 +                /* ... and subtract the previous stat from it to get the
493 +                   difference. */
494 +                dest->rx = transfer_diff(src->rx, dest->rx);
495 +                dest->tx = transfer_diff(src->tx, dest->tx);
496 +                dest->ipackets = transfer_diff(src->ipackets, dest->ipackets);
497 +                dest->opackets = transfer_diff(src->opackets, dest->opackets);
498 +                dest->ierrors = transfer_diff(src->ierrors, dest->ierrors);
499 +                dest->oerrors = transfer_diff(src->oerrors, dest->oerrors);
500 +                dest->collisions = transfer_diff(src->collisions, dest->collisions);
501 +                dest->systime = src->systime - dest->systime;
502 +        }
503 +
504 +        *entries = diff_count;
505 +        return diff;
506 + }
507 +
508 + int sg_network_io_compare_name(const void *va, const void *vb) {
509 +        const sg_network_io_stats *a = (const sg_network_io_stats *)va;
510 +        const sg_network_io_stats *b = (const sg_network_io_stats *)vb;
511 +
512 +        return strcmp(a->interface_name, b->interface_name);
513 + }
514 +
515 + /* NETWORK INTERFACE STATS */
516 +
517 + static void network_iface_stat_init(sg_network_iface_stats *s) {
518 +        s->interface_name = NULL;
519 +        s->speed = 0;
520 +        s->duplex = SG_IFACE_DUPLEX_UNKNOWN;
521 + }
522 +
523 + static void network_iface_stat_destroy(sg_network_iface_stats *s) {
524 +        free(s->interface_name);
525 + }
526 +
527 + sg_network_iface_stats *sg_get_network_iface_stats(int *entries){
528 +        VECTOR_DECLARE_STATIC(network_iface_stats, sg_network_iface_stats, 5,
529 +                              network_iface_stat_init, network_iface_stat_destroy);
530 +        sg_network_iface_stats *network_iface_stat_ptr;
531 +        int ifaces = 0;
532 +
533 + #ifdef SOLARIS
534 +        kstat_ctl_t *kc;
535 +        kstat_t *ksp;
536 +        kstat_named_t *knp;
537 +        int sock;
538 + #endif
539 + #ifdef ALLBSD
540 +        struct ifaddrs *net, *net_ptr;
541 +        struct ifmediareq ifmed;
542 +        struct ifreq ifr;
543 +        int sock;
544 +        int x;
545 + #endif
546 + #ifdef LINUX
547 +        FILE *f;
548 +        /* Horrible big enough, but it should be easily big enough */
549 +        char line[8096];
550 +        int sock;
551 + #endif
552 + #ifdef WIN32
553 +        PMIB_IFTABLE if_table;
554 +        MIB_IFROW if_row;
555 +        int i,j,no;
556 +        char buf[5];
557 + #endif
558 +
559 + #ifdef ALLBSD
560 +        if(getifaddrs(&net) != 0){
561 +                sg_set_error_with_errno(SG_ERROR_GETIFADDRS, NULL);
562 +                return NULL;
563 +        }
564 +
565 +        if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return NULL;
566 +
567 +        for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){
568 +                if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
569 +
570 +                if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) {
571 +                        return NULL;
572 +                }
573 +                network_iface_stat_ptr = network_iface_stats + ifaces;
574 +
575 +                memset(&ifr, 0, sizeof(ifr));
576 +                strncpy(ifr.ifr_name, net_ptr->ifa_name, sizeof(ifr.ifr_name));
577 +
578 +                if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0){
579 +                        continue;
580 +                }      
581 +                if((ifr.ifr_flags & IFF_UP) != 0){
582 +                        network_iface_stat_ptr->up = 1;
583 +                }else{
584 +                        network_iface_stat_ptr->up = 0;
585 +                }
586 +
587 +                if (sg_update_string(&network_iface_stat_ptr->interface_name,
588 +                                     net_ptr->ifa_name) < 0) {
589 +                        return NULL;
590 +                }
591 +
592 +                network_iface_stat_ptr->speed = 0;
593 +                network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
594 +                ifaces++;
595 +
596 +                memset(&ifmed, 0, sizeof(struct ifmediareq));
597 +                sg_strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name));
598 +                if(ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){
599 +                        /* Not all interfaces support the media ioctls. */
600 +                        continue;
601 +                }
602 +
603 +                /* We may need to change this if we start doing wireless devices too */
604 +                if( (ifmed.ifm_active | IFM_ETHER) != ifmed.ifm_active ){
605 +                        /* Not a ETHER device */
606 +                        continue;
607 +                }
608 +
609 +                /* Only intrested in the first 4 bits)  - Assuming only ETHER devices */
610 +                x = ifmed.ifm_active & 0x0f;    
611 +                switch(x){
612 +                        /* 10 Mbit connections. Speedy :) */
613 +                        case(IFM_10_T):
614 +                        case(IFM_10_2):
615 +                        case(IFM_10_5):
616 +                        case(IFM_10_STP):
617 +                        case(IFM_10_FL):
618 +                                network_iface_stat_ptr->speed = 10;
619 +                                break;
620 +                        /* 100 Mbit conneections */
621 +                        case(IFM_100_TX):
622 +                        case(IFM_100_FX):
623 +                        case(IFM_100_T4):
624 +                        case(IFM_100_VG):
625 +                        case(IFM_100_T2):
626 +                                network_iface_stat_ptr->speed = 100;
627 +                                break;
628 +                        /* 1000 Mbit connections */
629 +                        case(IFM_1000_SX):
630 +                        case(IFM_1000_LX):
631 +                        case(IFM_1000_CX):
632 + #if defined(IFM_1000_TX) && !defined(OPENBSD)
633 +                        case(IFM_1000_TX): /* FreeBSD 4 and others (but NOT OpenBSD)? */
634 + #endif
635 + #ifdef IFM_1000_FX
636 +                        case(IFM_1000_FX): /* FreeBSD 4 */
637 + #endif
638 + #ifdef IFM_1000_T
639 +                        case(IFM_1000_T): /* FreeBSD 5 */
640 + #endif
641 +                                network_iface_stat_ptr->speed = 1000;
642 +                                break;
643 +                        /* We don't know what it is */
644 +                        default:
645 +                                network_iface_stat_ptr->speed = 0;
646 +                                break;
647 +                }
648 +
649 +                if( (ifmed.ifm_active | IFM_FDX) == ifmed.ifm_active ){
650 +                        network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_FULL;
651 +                }else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){
652 +                        network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_HALF;
653 +                }else{
654 +                        network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
655 +                }
656 +
657 +        }      
658 +        freeifaddrs(net);
659 +        close(sock);
660 + #endif
661 +
662 + #ifdef SOLARIS
663 +        if ((kc = kstat_open()) == NULL) {
664 +                sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
665 +                return NULL;
666 +        }
667 +
668 +        if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) {
669 +                sg_set_error_with_errno(SG_ERROR_SOCKET, NULL);
670 +                kstat_close(kc);
671 +                return NULL;
672 +        }
673 +
674 +        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
675 +                if (strcmp(ksp->ks_class, "net") == 0) {
676 +                        struct ifreq ifr;
677 +
678 +                        kstat_read(kc, ksp, NULL);
679 +
680 +                        strncpy(ifr.ifr_name, ksp->ks_name, sizeof ifr.ifr_name);
681 +                        if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
682 +                                /* Not a network interface. */
683 +                                continue;
684 +                        }
685 +
686 +                        if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) {
687 +                                kstat_close(kc);
688 +                                return NULL;
689 +                        }
690 +                        network_iface_stat_ptr = network_iface_stats + ifaces;
691 +                        ifaces++;
692 +
693 +                        if (sg_update_string(&network_iface_stat_ptr->interface_name,
694 +                                             ksp->ks_name) < 0) {
695 +                                kstat_close(kc);
696 +                                return NULL;
697 +                        }
698 +
699 +                        if ((ifr.ifr_flags & IFF_UP) != 0) {
700 +                                network_iface_stat_ptr->up = 1;
701 +                        } else {
702 +                                network_iface_stat_ptr->up = 0;
703 +                        }
704 +
705 +                        if ((knp = kstat_data_lookup(ksp, "ifspeed")) != NULL) {
706 +                                network_iface_stat_ptr->speed = knp->value.ui64 / (1000 * 1000);
707 +                        } else {
708 +                                network_iface_stat_ptr->speed = 0;
709 +                        }
710 +
711 +                        network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
712 +                        if ((knp = kstat_data_lookup(ksp, "link_duplex")) != NULL) {
713 +                                switch (knp->value.ui32) {
714 +                                case 1:
715 +                                        network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_HALF;
716 +                                        break;
717 +                                case 2:
718 +                                        network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_FULL;
719 +                                        break;
720 +                                }
721 +                        }
722 +                }
723 +        }
724 +
725 +        close(sock);
726 +        kstat_close(kc);
727 + #endif
728 + #ifdef LINUX
729 +        f = fopen("/proc/net/dev", "r");
730 +        if(f == NULL){
731 +                sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/net/dev");
732 +                return NULL;
733 +        }
734 +
735 +        /* Setup stuff so we can do the ioctl to get the info */
736 +        if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
737 +                sg_set_error_with_errno(SG_ERROR_SOCKET, NULL);
738 +                return NULL;
739 +        }
740 +
741 +        /* Ignore first 2 lines.. Just headings */
742 +        if((fgets(line, sizeof(line), f)) == NULL) {
743 +                sg_set_error(SG_ERROR_PARSE, NULL);
744 +                return NULL;
745 +        }
746 +        if((fgets(line, sizeof(line), f)) == NULL) {
747 +                sg_set_error(SG_ERROR_PARSE, NULL);
748 +                return NULL;
749 +        }
750 +
751 +        while((fgets(line, sizeof(line), f)) != NULL){
752 +                char *name, *ptr;
753 +                struct ifreq ifr;
754 +                struct ethtool_cmd ethcmd;
755 +                int err;
756 +
757 +                /* Get the interface name */
758 +                ptr = strchr(line, ':');
759 +                if (ptr == NULL) continue;
760 +                *ptr='\0';
761 +                name = line;
762 +                while(isspace(*(name))){
763 +                        name++;
764 +                }
765 +
766 +                memset(&ifr, 0, sizeof ifr);
767 +                strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
768 +
769 +                if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
770 +                        continue;
771 +                }
772 +
773 +                /* We have a good interface to add */
774 +                if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) {
775 +                        return NULL;
776 +                }
777 +                network_iface_stat_ptr = network_iface_stats + ifaces;
778 +                
779 +                if (sg_update_string(&network_iface_stat_ptr->interface_name,
780 +                                     name) < 0) {
781 +                        return NULL;
782 +                }
783 +                if ((ifr.ifr_flags & IFF_UP) != 0) {
784 +                        network_iface_stat_ptr->up = 1;
785 +                } else {
786 +                        network_iface_stat_ptr->up = 0;
787 +                }
788 +
789 +                memset(&ethcmd, 0, sizeof ethcmd);
790 +                ethcmd.cmd = ETHTOOL_GSET;
791 +                ifr.ifr_data = (caddr_t) &ethcmd;
792 +
793 +                err = ioctl(sock, SIOCETHTOOL, &ifr);
794 +                if (err == 0) {
795 +                        network_iface_stat_ptr->speed = ethcmd.speed;
796 +
797 +                        switch (ethcmd.duplex) {
798 +                        case DUPLEX_FULL:
799 +                                network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_FULL;
800 +                                break;
801 +                        case DUPLEX_HALF:
802 +                                network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_HALF;
803 +                                break;
804 +                        default:
805 +                                network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
806 +                        }
807 +                } else {
808 +                        /* Not all interfaces support the ethtool ioctl. */
809 +                        network_iface_stat_ptr->speed = 0;
810 +                        network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
811 +                }
812 +
813 +                ifaces++;
814 +        }
815 +        close(sock);
816 +        fclose(f);
817 + #endif
818 + #ifdef CYGWIN
819 +        sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin");
820 +        return NULL;
821 + #endif
822 + #ifdef HPUX
823 +        sg_set_error(SG_ERROR_UNSUPPORTED, "HP-UX");
824 +        return NULL;
825 + #endif
826 + #ifdef WIN32
827 +        ifaces = 0;
828 +
829 +        if((if_table = win32_get_devices()) == NULL) {
830 +                sg_set_error(SG_ERROR_DEVICES, "network interfaces");
831 +                return NULL;
832 +        }
833 +
834 +        if(VECTOR_RESIZE(network_iface_stats, if_table->dwNumEntries) < 0) {
835 +                free(if_table);
836 +                return NULL;
837 +        }
838 +
839 +        for(i=0; i<if_table->dwNumEntries; i++) {
840 +                network_iface_stat_ptr=network_iface_stats+i;
841 +                if_row = if_table->table[i];
842 +
843 +                if(sg_update_string(&network_iface_stat_ptr->interface_name,
844 +                                        if_row.bDescr) < 0) {
845 +                        free(if_table);
846 +                        return NULL;
847 +                }
848 +                network_iface_stat_ptr->speed = if_row.dwSpeed /1000000;
849 +
850 +                if((if_row.dwOperStatus == MIB_IF_OPER_STATUS_CONNECTED ||
851 +                                if_row.dwOperStatus ==
852 +                                        MIB_IF_OPER_STATUS_OPERATIONAL) &&
853 +                                if_row.dwAdminStatus == 1) {
854 +                        network_iface_stat_ptr->up = 1;
855 +                } else {
856 +                        network_iface_stat_ptr->up = 0;
857 +                }
858 +
859 +                ifaces++;
860 +        }
861 +        free(if_table);
862 +
863 +        /* again with the renumbering */
864 +        for (i=0; i<ifaces; i++) {
865 +                no = 2;
866 +                for(j=i+1; j<ifaces; j++) {
867 +                        network_iface_stat_ptr=network_iface_stats+j;
868 +                        if(strcmp(network_iface_stats[i].interface_name,
869 +                                        network_iface_stat_ptr->interface_name) == 0) {
870 +                                if(snprintf(buf, sizeof(buf), " #%d", no) < 0) {
871 +                                        break;
872 +                                }
873 +                                if(sg_concat_string(&network_iface_stat_ptr->interface_name, buf) != 0) {
874 +                                        return NULL;
875 +                                }
876 +
877 +                                no++;
878 +                        }
879 +                }
880 +        }
881 + #endif
882 +
883 + #ifdef SG_ENABLE_DEPRECATED
884 +        network_iface_stat_ptr->dup = network_iface_stat_ptr->duplex;
885 + #endif
886 +
887 +        *entries = ifaces;
888 +        return network_iface_stats;
889 + }
890 +
891 + int sg_network_iface_compare_name(const void *va, const void *vb) {
892 +        const sg_network_iface_stats *a = (const sg_network_iface_stats *)va;
893 +        const sg_network_iface_stats *b = (const sg_network_iface_stats *)vb;
894 +
895 +        return strcmp(a->interface_name, b->interface_name);
896   }
897  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines