ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/network_stats.c
Revision: 1.78
Committed: Mon Oct 9 14:09:38 2006 UTC (17 years, 7 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.77: +6 -1 lines
Log Message:
Make sure we always close kstat.

Spotted by: "Javier Donaire" <jyuyu@fraguel.org> (in load_stats.c)

File Contents

# User Rev Content
1 tdb 1.22 /*
2 tdb 1.64 * i-scream libstatgrab
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.78 * $Id: network_stats.c,v 1.77 2006/01/22 18:10:39 tdb 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 ats 1.59 #include "vector.h"
32 ats 1.60 #include "tools.h"
33 tdb 1.11 #include <time.h>
34 pajs 1.1 #ifdef SOLARIS
35     #include <kstat.h>
36     #include <sys/sysinfo.h>
37 ats 1.44 #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 tdb 1.61 #include <unistd.h>
44 pajs 1.6 #endif
45     #ifdef LINUX
46     #include <stdio.h>
47     #include <sys/types.h>
48 pajs 1.27 #include <sys/ioctl.h>
49     #include <sys/socket.h>
50     #include <net/if.h>
51     #include <ctype.h>
52 tdb 1.73 #include <linux/version.h>
53 ats 1.75 #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 pajs 1.27 typedef __uint8_t u8;
57     typedef __uint16_t u16;
58     typedef __uint32_t u32;
59 ats 1.42 typedef __uint64_t u64;
60 pajs 1.27 #include <linux/ethtool.h>
61     #include <linux/sockios.h>
62 ats 1.32 #include <unistd.h>
63 pajs 1.1 #endif
64 ats 1.18 #ifdef ALLBSD
65 pajs 1.14 #include <sys/types.h>
66     #include <sys/socket.h>
67     #include <ifaddrs.h>
68     #include <net/if.h>
69 pajs 1.26 #include <net/if_media.h>
70     #include <sys/ioctl.h>
71 tdb 1.58 #include <unistd.h>
72 pajs 1.14 #endif
73 tdb 1.76 #ifdef WIN32
74     #include <windows.h>
75     #include <Iphlpapi.h>
76     #include "win32.h"
77     #endif
78 pajs 1.1
79 ats 1.62 static void network_stat_init(sg_network_io_stats *s) {
80 ats 1.59 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 pajs 1.3 }
89    
90 ats 1.62 static void network_stat_destroy(sg_network_io_stats *s) {
91 ats 1.59 free(s->interface_name);
92 pajs 1.1 }
93    
94 ats 1.62 VECTOR_DECLARE_STATIC(network_stats, sg_network_io_stats, 5,
95 tdb 1.65 network_stat_init, network_stat_destroy);
96 pajs 1.3
97 tdb 1.76 #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    
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     if_table = tmp;
118     }
119    
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 ats 1.62 sg_network_io_stats *sg_get_network_io_stats(int *entries){
130 ats 1.59 int interfaces;
131 ats 1.62 sg_network_io_stats *network_stat_ptr;
132 pajs 1.6
133     #ifdef SOLARIS
134 tdb 1.65 kstat_ctl_t *kc;
135     kstat_t *ksp;
136 pajs 1.1 kstat_named_t *knp;
137 pajs 1.6 #endif
138 pajs 1.3
139 pajs 1.6 #ifdef LINUX
140     FILE *f;
141 pajs 1.12 /* Horrible big enough, but it should be easily big enough */
142 pajs 1.6 char line[8096];
143     regex_t regex;
144 ats 1.49 regmatch_t line_match[9];
145 pajs 1.14 #endif
146 ats 1.18 #ifdef ALLBSD
147 pajs 1.14 struct ifaddrs *net, *net_ptr;
148     struct if_data *net_data;
149     #endif
150 tdb 1.76 #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 pajs 1.14
160 ats 1.18 #ifdef ALLBSD
161 pajs 1.14 if(getifaddrs(&net) != 0){
162 ats 1.69 sg_set_error_with_errno(SG_ERROR_GETIFADDRS, NULL);
163 pajs 1.14 return NULL;
164     }
165    
166     interfaces=0;
167    
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 ats 1.59
171     if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) {
172 pajs 1.14 return NULL;
173     }
174     network_stat_ptr=network_stats+interfaces;
175    
176 ats 1.62 if (sg_update_string(&network_stat_ptr->interface_name,
177 tdb 1.65 net_ptr->ifa_name) < 0) {
178 ats 1.60 return NULL;
179     }
180 pajs 1.14 net_data=(struct if_data *)net_ptr->ifa_data;
181     network_stat_ptr->rx=net_data->ifi_ibytes;
182 tdb 1.46 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 pajs 1.14 network_stat_ptr->systime=time(NULL);
189     interfaces++;
190     }
191     freeifaddrs(net);
192 pajs 1.6 #endif
193 pajs 1.1
194 pajs 1.6 #ifdef SOLARIS
195 tdb 1.65 if ((kc = kstat_open()) == NULL) {
196 tdb 1.66 sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
197 tdb 1.65 return NULL;
198     }
199 pajs 1.1
200 pajs 1.6 interfaces=0;
201 pajs 1.3
202 tdb 1.66 for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
203 tdb 1.76 if (strcmp(ksp->ks_class, "net") == 0) {
204 tdb 1.65 kstat_read(kc, ksp, NULL);
205 pajs 1.1
206 pajs 1.7 #ifdef SOL7
207 tdb 1.53 #define LRX "rbytes"
208     #define LTX "obytes"
209     #define LIPACKETS "ipackets"
210     #define LOPACKETS "opackets"
211 pajs 1.7 #define VALTYPE value.ui32
212     #else
213 tdb 1.53 #define LRX "rbytes64"
214     #define LTX "obytes64"
215     #define LIPACKETS "ipackets64"
216     #define LOPACKETS "opackets64"
217 pajs 1.7 #define VALTYPE value.ui64
218     #endif
219    
220 tdb 1.53 /* Read rx */
221     if((knp=kstat_data_lookup(ksp, LRX))==NULL){
222 ats 1.44 /* 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 ats 1.55 /* FIXME: Show packet counts when byte counts
227     * not available. */
228 pajs 1.1 continue;
229     }
230 pajs 1.3
231 tdb 1.53 /* Create new network_stats */
232 ats 1.59 if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) {
233 tdb 1.78 kstat_close(kc);
234 pajs 1.1 return NULL;
235     }
236     network_stat_ptr=network_stats+interfaces;
237 tdb 1.53
238     /* Finish reading rx */
239 pajs 1.7 network_stat_ptr->rx=knp->VALTYPE;
240 pajs 1.1
241 tdb 1.53 /* Read tx */
242     if((knp=kstat_data_lookup(ksp, LTX))==NULL){
243 pajs 1.1 continue;
244     }
245 pajs 1.7 network_stat_ptr->tx=knp->VALTYPE;
246 tdb 1.53
247     /* Read ipackets */
248     if((knp=kstat_data_lookup(ksp, LIPACKETS))==NULL){
249     continue;
250     }
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 ats 1.62 if (sg_update_string(&network_stat_ptr->interface_name,
279 tdb 1.65 ksp->ks_name) < 0) {
280 tdb 1.78 kstat_close(kc);
281 ats 1.60 return NULL;
282 pajs 1.1 }
283 pajs 1.3
284 tdb 1.53 /* Store systime */
285 pajs 1.3 network_stat_ptr->systime=time(NULL);
286 tdb 1.53
287 pajs 1.1 interfaces++;
288     }
289     }
290 pajs 1.2
291     kstat_close(kc);
292 pajs 1.6 #endif
293     #ifdef LINUX
294     f=fopen("/proc/net/dev", "r");
295     if(f==NULL){
296 ats 1.69 sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/net/dev");
297 pajs 1.6 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 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){
305 tdb 1.66 sg_set_error(SG_ERROR_PARSE, NULL);
306 pajs 1.6 return NULL;
307     }
308    
309     interfaces=0;
310 pajs 1.1
311 pajs 1.6 while((fgets(line, sizeof(line), f)) != NULL){
312 ats 1.51 if((regexec(&regex, line, 9, line_match, 0))!=0){
313 pajs 1.6 continue;
314     }
315 ats 1.59
316     if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) {
317     return NULL;
318 pajs 1.6 }
319 tdb 1.65 network_stat_ptr=network_stats+interfaces;
320 pajs 1.6
321     if(network_stat_ptr->interface_name!=NULL){
322     free(network_stat_ptr->interface_name);
323     }
324    
325 ats 1.62 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 pajs 1.6 network_stat_ptr->systime=time(NULL);
334    
335     interfaces++;
336     }
337 pajs 1.12 fclose(f);
338 pajs 1.13 regfree(&regex);
339 pajs 1.6
340     #endif
341 ats 1.20
342     #ifdef CYGWIN
343 tdb 1.66 sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin");
344 ats 1.70 return NULL;
345     #endif
346     #ifdef HPUX
347     sg_set_error(SG_ERROR_UNSUPPORTED, "HP-UX");
348 ats 1.20 return NULL;
349     #endif
350    
351 tdb 1.76 #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 pajs 1.1 *entries=interfaces;
414    
415     return network_stats;
416 pajs 1.3 }
417    
418 ats 1.62 static long long transfer_diff(long long new, long long old){
419 tdb 1.76 #if defined(SOL7) || defined(LINUX) || defined(FREEBSD) || defined(DFBSD) || defined(OPENBSD) || defined(WIN32)
420 ats 1.56 /* 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 pajs 1.8 #else
428 ats 1.56 /* 64-bit quantities, so plain subtraction works. */
429     return new - old;
430 pajs 1.8 #endif
431     }
432    
433 ats 1.62 sg_network_io_stats *sg_get_network_io_stats_diff(int *entries) {
434     VECTOR_DECLARE_STATIC(diff, sg_network_io_stats, 1,
435 tdb 1.65 network_stat_init, network_stat_destroy);
436 ats 1.62 sg_network_io_stats *src = NULL, *dest;
437 ats 1.59 int i, j, diff_count, new_count;
438 ats 1.24
439     if (network_stats == NULL) {
440     /* No previous stats, so we can't calculate a difference. */
441 ats 1.62 return sg_get_network_io_stats(entries);
442 pajs 1.3 }
443    
444 ats 1.24 /* Resize the results array to match the previous stats. */
445 ats 1.59 diff_count = VECTOR_SIZE(network_stats);
446     if (VECTOR_RESIZE(diff, diff_count) < 0) {
447 pajs 1.3 return NULL;
448     }
449    
450 ats 1.24 /* 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 pajs 1.3
455 ats 1.62 if (sg_update_string(&dest->interface_name,
456 tdb 1.65 src->interface_name) < 0) {
457 ats 1.60 return NULL;
458 pajs 1.3 }
459 ats 1.24 dest->rx = src->rx;
460     dest->tx = src->tx;
461 tdb 1.47 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 ats 1.24 dest->systime = src->systime;
467     }
468 pajs 1.3
469 ats 1.24 /* Get a new set of stats. */
470 ats 1.62 if (sg_get_network_io_stats(&new_count) == NULL) {
471 ats 1.21 return NULL;
472     }
473 pajs 1.3
474 ats 1.24 /* 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 pajs 1.3 }
491    
492 ats 1.24 /* ... 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 tdb 1.47 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 ats 1.24 dest->systime = src->systime - dest->systime;
502 pajs 1.3 }
503    
504 ats 1.24 *entries = diff_count;
505     return diff;
506     }
507 ats 1.59
508 ats 1.68 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 pajs 1.25 /* NETWORK INTERFACE STATS */
516    
517 ats 1.62 static void network_iface_stat_init(sg_network_iface_stats *s) {
518 ats 1.59 s->interface_name = NULL;
519     s->speed = 0;
520 tdb 1.74 s->duplex = SG_IFACE_DUPLEX_UNKNOWN;
521 pajs 1.25 }
522    
523 ats 1.62 static void network_iface_stat_destroy(sg_network_iface_stats *s) {
524 ats 1.59 free(s->interface_name);
525 pajs 1.25 }
526    
527 ats 1.62 sg_network_iface_stats *sg_get_network_iface_stats(int *entries){
528     VECTOR_DECLARE_STATIC(network_iface_stats, sg_network_iface_stats, 5,
529 tdb 1.65 network_iface_stat_init, network_iface_stat_destroy);
530 ats 1.62 sg_network_iface_stats *network_iface_stat_ptr;
531 ats 1.43 int ifaces = 0;
532 pajs 1.25
533     #ifdef SOLARIS
534 tdb 1.65 kstat_ctl_t *kc;
535     kstat_t *ksp;
536 pajs 1.25 kstat_named_t *knp;
537 ats 1.44 int sock;
538 pajs 1.25 #endif
539 pajs 1.26 #ifdef ALLBSD
540 tdb 1.65 struct ifaddrs *net, *net_ptr;
541 pajs 1.26 struct ifmediareq ifmed;
542 pajs 1.36 struct ifreq ifr;
543 ats 1.37 int sock;
544 pajs 1.26 int x;
545     #endif
546 pajs 1.27 #ifdef LINUX
547 tdb 1.65 FILE *f;
548     /* Horrible big enough, but it should be easily big enough */
549     char line[8096];
550 pajs 1.27 int sock;
551     #endif
552 tdb 1.76 #ifdef WIN32
553     PMIB_IFTABLE if_table;
554     MIB_IFROW if_row;
555     int i,j,no;
556     char buf[5];
557     #endif
558 ats 1.43
559 pajs 1.26 #ifdef ALLBSD
560 tdb 1.65 if(getifaddrs(&net) != 0){
561 ats 1.69 sg_set_error_with_errno(SG_ERROR_GETIFADDRS, NULL);
562 tdb 1.65 return NULL;
563     }
564 pajs 1.26
565 ats 1.37 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return NULL;
566 pajs 1.26
567     for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){
568 tdb 1.65 if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
569 ats 1.59
570     if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) {
571 tdb 1.65 return NULL;
572     }
573     network_iface_stat_ptr = network_iface_stats + ifaces;
574 pajs 1.26
575 ats 1.40 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 ats 1.62 if (sg_update_string(&network_iface_stat_ptr->interface_name,
588 tdb 1.65 net_ptr->ifa_name) < 0) {
589 ats 1.60 return NULL;
590     }
591 ats 1.40
592     network_iface_stat_ptr->speed = 0;
593 tdb 1.74 network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
594 ats 1.40 ifaces++;
595    
596 pajs 1.26 memset(&ifmed, 0, sizeof(struct ifmediareq));
597 ats 1.62 sg_strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name));
598 ats 1.37 if(ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){
599 ats 1.40 /* Not all interfaces support the media ioctls. */
600 pajs 1.26 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 tdb 1.54 #if defined(IFM_1000_TX) && !defined(OPENBSD)
633     case(IFM_1000_TX): /* FreeBSD 4 and others (but NOT OpenBSD)? */
634 tdb 1.45 #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 tdb 1.28 #endif
641 pajs 1.26 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 tdb 1.74 network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_FULL;
651 pajs 1.26 }else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){
652 tdb 1.74 network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_HALF;
653 pajs 1.26 }else{
654 tdb 1.74 network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
655 pajs 1.26 }
656 pajs 1.36
657 pajs 1.26 }
658     freeifaddrs(net);
659 ats 1.37 close(sock);
660 pajs 1.26 #endif
661 pajs 1.25
662     #ifdef SOLARIS
663 ats 1.44 if ((kc = kstat_open()) == NULL) {
664 tdb 1.66 sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
665 ats 1.44 return NULL;
666     }
667    
668     if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) {
669 ats 1.69 sg_set_error_with_errno(SG_ERROR_SOCKET, NULL);
670 tdb 1.78 kstat_close(kc);
671 ats 1.44 return NULL;
672     }
673 pajs 1.25
674 ats 1.44 for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
675 tdb 1.76 if (strcmp(ksp->ks_class, "net") == 0) {
676 ats 1.44 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 pajs 1.25 continue;
684     }
685 ats 1.44
686 ats 1.59 if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) {
687 tdb 1.78 kstat_close(kc);
688 pajs 1.25 return NULL;
689     }
690 pajs 1.26 network_iface_stat_ptr = network_iface_stats + ifaces;
691 ats 1.44 ifaces++;
692    
693 ats 1.62 if (sg_update_string(&network_iface_stat_ptr->interface_name,
694 tdb 1.65 ksp->ks_name) < 0) {
695 tdb 1.78 kstat_close(kc);
696 ats 1.60 return NULL;
697     }
698 pajs 1.25
699 ats 1.44 if ((ifr.ifr_flags & IFF_UP) != 0) {
700     network_iface_stat_ptr->up = 1;
701     } else {
702 tdb 1.77 network_iface_stat_ptr->up = 0;
703 ats 1.44 }
704 pajs 1.35
705 ats 1.44 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 pajs 1.25 }
710    
711 tdb 1.74 network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
712 ats 1.44 if ((knp = kstat_data_lookup(ksp, "link_duplex")) != NULL) {
713     switch (knp->value.ui32) {
714     case 1:
715 tdb 1.74 network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_HALF;
716 ats 1.44 break;
717     case 2:
718 tdb 1.74 network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_FULL;
719 ats 1.44 break;
720     }
721 pajs 1.33 }
722 pajs 1.25 }
723     }
724 ats 1.44
725     close(sock);
726 pajs 1.25 kstat_close(kc);
727 tdb 1.76 #endif
728 pajs 1.27 #ifdef LINUX
729     f = fopen("/proc/net/dev", "r");
730 tdb 1.65 if(f == NULL){
731 ats 1.69 sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/net/dev");
732 tdb 1.65 return NULL;
733     }
734 pajs 1.27
735     /* Setup stuff so we can do the ioctl to get the info */
736     if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
737 ats 1.69 sg_set_error_with_errno(SG_ERROR_SOCKET, NULL);
738 pajs 1.27 return NULL;
739     }
740    
741     /* Ignore first 2 lines.. Just headings */
742 tdb 1.66 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 pajs 1.27
751 tdb 1.65 while((fgets(line, sizeof(line), f)) != NULL){
752     char *name, *ptr;
753     struct ifreq ifr;
754     struct ethtool_cmd ethcmd;
755     int err;
756 pajs 1.27
757     /* Get the interface name */
758 tdb 1.65 ptr = strchr(line, ':');
759     if (ptr == NULL) continue;
760     *ptr='\0';
761     name = line;
762     while(isspace(*(name))){
763     name++;
764     }
765 pajs 1.27
766 tdb 1.65 memset(&ifr, 0, sizeof ifr);
767     strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
768 pajs 1.27
769 ats 1.38 if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
770     continue;
771     }
772 pajs 1.27
773     /* We have a good interface to add */
774 ats 1.59 if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) {
775 pajs 1.27 return NULL;
776     }
777     network_iface_stat_ptr = network_iface_stats + ifaces;
778 ats 1.60
779 ats 1.62 if (sg_update_string(&network_iface_stat_ptr->interface_name,
780 tdb 1.65 name) < 0) {
781 ats 1.60 return NULL;
782     }
783 ats 1.38 if ((ifr.ifr_flags & IFF_UP) != 0) {
784 pajs 1.35 network_iface_stat_ptr->up = 1;
785 ats 1.38 } else {
786 pajs 1.35 network_iface_stat_ptr->up = 0;
787     }
788    
789 tdb 1.65 memset(&ethcmd, 0, sizeof ethcmd);
790     ethcmd.cmd = ETHTOOL_GSET;
791     ifr.ifr_data = (caddr_t) &ethcmd;
792 ats 1.38
793 tdb 1.65 err = ioctl(sock, SIOCETHTOOL, &ifr);
794     if (err == 0) {
795 ats 1.38 network_iface_stat_ptr->speed = ethcmd.speed;
796    
797     switch (ethcmd.duplex) {
798 tdb 1.72 case DUPLEX_FULL:
799 tdb 1.74 network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_FULL;
800 ats 1.38 break;
801 tdb 1.72 case DUPLEX_HALF:
802 tdb 1.74 network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_HALF;
803 ats 1.38 break;
804     default:
805 tdb 1.74 network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
806 ats 1.38 }
807     } else {
808     /* Not all interfaces support the ethtool ioctl. */
809 ats 1.39 network_iface_stat_ptr->speed = 0;
810 tdb 1.74 network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
811 pajs 1.27 }
812 ats 1.38
813 pajs 1.27 ifaces++;
814     }
815 pajs 1.30 close(sock);
816 ats 1.38 fclose(f);
817 pajs 1.27 #endif
818 ats 1.71 #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 tdb 1.76 #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 tdb 1.74 #endif
882    
883     #ifdef SG_ENABLE_DEPRECATED
884     network_iface_stat_ptr->dup = network_iface_stat_ptr->duplex;
885 ats 1.71 #endif
886    
887 pajs 1.26 *entries = ifaces;
888 pajs 1.25 return network_iface_stats;
889 ats 1.68 }
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 pajs 1.25 }
897