ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/network_stats.c
Revision: 1.75
Committed: Sat Jul 30 13:23:46 2005 UTC (18 years, 9 months ago) by ats
Content type: text/plain
Branch: MAIN
CVS Tags: LIBSTATGRAB_0_12
Changes since 1.74: +4 -11 lines
Log Message:
Use <asm/types.h> to define __u32 etc., and always define u32 etc. because
the Debian 2.6.0-test9 headers still use them in ethtool.h.

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 ats 1.75 * $Id: network_stats.c,v 1.74 2005/07/13 09:31:54 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 pajs 1.1
74 ats 1.62 static void network_stat_init(sg_network_io_stats *s) {
75 ats 1.59 s->interface_name = NULL;
76     s->tx = 0;
77     s->rx = 0;
78     s->ipackets = 0;
79     s->opackets = 0;
80     s->ierrors = 0;
81     s->oerrors = 0;
82     s->collisions = 0;
83 pajs 1.3 }
84    
85 ats 1.62 static void network_stat_destroy(sg_network_io_stats *s) {
86 ats 1.59 free(s->interface_name);
87 pajs 1.1 }
88    
89 ats 1.62 VECTOR_DECLARE_STATIC(network_stats, sg_network_io_stats, 5,
90 tdb 1.65 network_stat_init, network_stat_destroy);
91 pajs 1.3
92 ats 1.62 sg_network_io_stats *sg_get_network_io_stats(int *entries){
93 ats 1.59 int interfaces;
94 ats 1.62 sg_network_io_stats *network_stat_ptr;
95 pajs 1.6
96     #ifdef SOLARIS
97 tdb 1.65 kstat_ctl_t *kc;
98     kstat_t *ksp;
99 pajs 1.1 kstat_named_t *knp;
100 pajs 1.6 #endif
101 pajs 1.3
102 pajs 1.6 #ifdef LINUX
103     FILE *f;
104 pajs 1.12 /* Horrible big enough, but it should be easily big enough */
105 pajs 1.6 char line[8096];
106     regex_t regex;
107 ats 1.49 regmatch_t line_match[9];
108 pajs 1.14 #endif
109 ats 1.18 #ifdef ALLBSD
110 pajs 1.14 struct ifaddrs *net, *net_ptr;
111     struct if_data *net_data;
112     #endif
113    
114 ats 1.18 #ifdef ALLBSD
115 pajs 1.14 if(getifaddrs(&net) != 0){
116 ats 1.69 sg_set_error_with_errno(SG_ERROR_GETIFADDRS, NULL);
117 pajs 1.14 return NULL;
118     }
119    
120     interfaces=0;
121    
122     for(net_ptr=net;net_ptr!=NULL;net_ptr=net_ptr->ifa_next){
123     if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
124 ats 1.59
125     if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) {
126 pajs 1.14 return NULL;
127     }
128     network_stat_ptr=network_stats+interfaces;
129    
130 ats 1.62 if (sg_update_string(&network_stat_ptr->interface_name,
131 tdb 1.65 net_ptr->ifa_name) < 0) {
132 ats 1.60 return NULL;
133     }
134 pajs 1.14 net_data=(struct if_data *)net_ptr->ifa_data;
135     network_stat_ptr->rx=net_data->ifi_ibytes;
136 tdb 1.46 network_stat_ptr->tx=net_data->ifi_obytes;
137     network_stat_ptr->ipackets=net_data->ifi_ipackets;
138     network_stat_ptr->opackets=net_data->ifi_opackets;
139     network_stat_ptr->ierrors=net_data->ifi_ierrors;
140     network_stat_ptr->oerrors=net_data->ifi_oerrors;
141     network_stat_ptr->collisions=net_data->ifi_collisions;
142 pajs 1.14 network_stat_ptr->systime=time(NULL);
143     interfaces++;
144     }
145     freeifaddrs(net);
146 pajs 1.6 #endif
147 pajs 1.1
148 pajs 1.6 #ifdef SOLARIS
149 tdb 1.65 if ((kc = kstat_open()) == NULL) {
150 tdb 1.66 sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
151 tdb 1.65 return NULL;
152     }
153 pajs 1.1
154 pajs 1.6 interfaces=0;
155 pajs 1.3
156 tdb 1.66 for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
157 tdb 1.65 if (!strcmp(ksp->ks_class, "net")) {
158     kstat_read(kc, ksp, NULL);
159 pajs 1.1
160 pajs 1.7 #ifdef SOL7
161 tdb 1.53 #define LRX "rbytes"
162     #define LTX "obytes"
163     #define LIPACKETS "ipackets"
164     #define LOPACKETS "opackets"
165 pajs 1.7 #define VALTYPE value.ui32
166     #else
167 tdb 1.53 #define LRX "rbytes64"
168     #define LTX "obytes64"
169     #define LIPACKETS "ipackets64"
170     #define LOPACKETS "opackets64"
171 pajs 1.7 #define VALTYPE value.ui64
172     #endif
173    
174 tdb 1.53 /* Read rx */
175     if((knp=kstat_data_lookup(ksp, LRX))==NULL){
176 ats 1.44 /* This is a network interface, but it doesn't
177     * have the rbytes/obytes values; for instance,
178     * the loopback devices have this behaviour
179     * (although they do track packets in/out). */
180 ats 1.55 /* FIXME: Show packet counts when byte counts
181     * not available. */
182 pajs 1.1 continue;
183     }
184 pajs 1.3
185 tdb 1.53 /* Create new network_stats */
186 ats 1.59 if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) {
187 pajs 1.1 return NULL;
188     }
189     network_stat_ptr=network_stats+interfaces;
190 tdb 1.53
191     /* Finish reading rx */
192 pajs 1.7 network_stat_ptr->rx=knp->VALTYPE;
193 pajs 1.1
194 tdb 1.53 /* Read tx */
195     if((knp=kstat_data_lookup(ksp, LTX))==NULL){
196 pajs 1.1 continue;
197     }
198 pajs 1.7 network_stat_ptr->tx=knp->VALTYPE;
199 tdb 1.53
200     /* Read ipackets */
201     if((knp=kstat_data_lookup(ksp, LIPACKETS))==NULL){
202     continue;
203     }
204     network_stat_ptr->ipackets=knp->VALTYPE;
205    
206     /* Read opackets */
207     if((knp=kstat_data_lookup(ksp, LOPACKETS))==NULL){
208     continue;
209     }
210     network_stat_ptr->opackets=knp->VALTYPE;
211    
212     /* Read ierrors */
213     if((knp=kstat_data_lookup(ksp, "ierrors"))==NULL){
214     continue;
215     }
216     network_stat_ptr->ierrors=knp->value.ui32;
217    
218     /* Read oerrors */
219     if((knp=kstat_data_lookup(ksp, "oerrors"))==NULL){
220     continue;
221     }
222     network_stat_ptr->oerrors=knp->value.ui32;
223    
224     /* Read collisions */
225     if((knp=kstat_data_lookup(ksp, "collisions"))==NULL){
226     continue;
227     }
228     network_stat_ptr->collisions=knp->value.ui32;
229    
230     /* Read interface name */
231 ats 1.62 if (sg_update_string(&network_stat_ptr->interface_name,
232 tdb 1.65 ksp->ks_name) < 0) {
233 ats 1.60 return NULL;
234 pajs 1.1 }
235 pajs 1.3
236 tdb 1.53 /* Store systime */
237 pajs 1.3 network_stat_ptr->systime=time(NULL);
238 tdb 1.53
239 pajs 1.1 interfaces++;
240     }
241     }
242 pajs 1.2
243     kstat_close(kc);
244 pajs 1.6 #endif
245     #ifdef LINUX
246     f=fopen("/proc/net/dev", "r");
247     if(f==NULL){
248 ats 1.69 sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/net/dev");
249 pajs 1.6 return NULL;
250     }
251     /* read the 2 lines.. Its the title, so we dont care :) */
252     fgets(line, sizeof(line), f);
253     fgets(line, sizeof(line), f);
254    
255    
256 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){
257 tdb 1.66 sg_set_error(SG_ERROR_PARSE, NULL);
258 pajs 1.6 return NULL;
259     }
260    
261     interfaces=0;
262 pajs 1.1
263 pajs 1.6 while((fgets(line, sizeof(line), f)) != NULL){
264 ats 1.51 if((regexec(&regex, line, 9, line_match, 0))!=0){
265 pajs 1.6 continue;
266     }
267 ats 1.59
268     if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) {
269     return NULL;
270 pajs 1.6 }
271 tdb 1.65 network_stat_ptr=network_stats+interfaces;
272 pajs 1.6
273     if(network_stat_ptr->interface_name!=NULL){
274     free(network_stat_ptr->interface_name);
275     }
276    
277 ats 1.62 network_stat_ptr->interface_name=sg_get_string_match(line, &line_match[1]);
278     network_stat_ptr->rx=sg_get_ll_match(line, &line_match[2]);
279     network_stat_ptr->tx=sg_get_ll_match(line, &line_match[5]);
280     network_stat_ptr->ipackets=sg_get_ll_match(line, &line_match[3]);
281     network_stat_ptr->opackets=sg_get_ll_match(line, &line_match[6]);
282     network_stat_ptr->ierrors=sg_get_ll_match(line, &line_match[4]);
283     network_stat_ptr->oerrors=sg_get_ll_match(line, &line_match[7]);
284     network_stat_ptr->collisions=sg_get_ll_match(line, &line_match[8]);
285 pajs 1.6 network_stat_ptr->systime=time(NULL);
286    
287     interfaces++;
288     }
289 pajs 1.12 fclose(f);
290 pajs 1.13 regfree(&regex);
291 pajs 1.6
292     #endif
293 ats 1.20
294     #ifdef CYGWIN
295 tdb 1.66 sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin");
296 ats 1.70 return NULL;
297     #endif
298     #ifdef HPUX
299     sg_set_error(SG_ERROR_UNSUPPORTED, "HP-UX");
300 ats 1.20 return NULL;
301     #endif
302    
303 pajs 1.1 *entries=interfaces;
304    
305     return network_stats;
306 pajs 1.3 }
307    
308 ats 1.62 static long long transfer_diff(long long new, long long old){
309 ats 1.56 #if defined(SOL7) || defined(LINUX) || defined(FREEBSD) || defined(DFBSD) || defined(OPENBSD)
310     /* 32-bit quantities, so we must explicitly deal with wraparound. */
311     #define MAXVAL 0x100000000LL
312     if (new >= old) {
313     return new - old;
314     } else {
315     return MAXVAL + new - old;
316     }
317 pajs 1.8 #else
318 ats 1.56 /* 64-bit quantities, so plain subtraction works. */
319     return new - old;
320 pajs 1.8 #endif
321     }
322    
323 ats 1.62 sg_network_io_stats *sg_get_network_io_stats_diff(int *entries) {
324     VECTOR_DECLARE_STATIC(diff, sg_network_io_stats, 1,
325 tdb 1.65 network_stat_init, network_stat_destroy);
326 ats 1.62 sg_network_io_stats *src = NULL, *dest;
327 ats 1.59 int i, j, diff_count, new_count;
328 ats 1.24
329     if (network_stats == NULL) {
330     /* No previous stats, so we can't calculate a difference. */
331 ats 1.62 return sg_get_network_io_stats(entries);
332 pajs 1.3 }
333    
334 ats 1.24 /* Resize the results array to match the previous stats. */
335 ats 1.59 diff_count = VECTOR_SIZE(network_stats);
336     if (VECTOR_RESIZE(diff, diff_count) < 0) {
337 pajs 1.3 return NULL;
338     }
339    
340 ats 1.24 /* Copy the previous stats into the result. */
341     for (i = 0; i < diff_count; i++) {
342     src = &network_stats[i];
343     dest = &diff[i];
344 pajs 1.3
345 ats 1.62 if (sg_update_string(&dest->interface_name,
346 tdb 1.65 src->interface_name) < 0) {
347 ats 1.60 return NULL;
348 pajs 1.3 }
349 ats 1.24 dest->rx = src->rx;
350     dest->tx = src->tx;
351 tdb 1.47 dest->ipackets = src->ipackets;
352     dest->opackets = src->opackets;
353     dest->ierrors = src->ierrors;
354     dest->oerrors = src->oerrors;
355     dest->collisions = src->collisions;
356 ats 1.24 dest->systime = src->systime;
357     }
358 pajs 1.3
359 ats 1.24 /* Get a new set of stats. */
360 ats 1.62 if (sg_get_network_io_stats(&new_count) == NULL) {
361 ats 1.21 return NULL;
362     }
363 pajs 1.3
364 ats 1.24 /* For each previous stat... */
365     for (i = 0; i < diff_count; i++) {
366     dest = &diff[i];
367    
368     /* ... find the corresponding new stat ... */
369     for (j = 0; j < new_count; j++) {
370     /* Try the new stat in the same position first,
371     since that's most likely to be it. */
372     src = &network_stats[(i + j) % new_count];
373     if (strcmp(src->interface_name, dest->interface_name) == 0) {
374     break;
375     }
376     }
377     if (j == new_count) {
378     /* No match found. */
379     continue;
380 pajs 1.3 }
381    
382 ats 1.24 /* ... and subtract the previous stat from it to get the
383     difference. */
384     dest->rx = transfer_diff(src->rx, dest->rx);
385     dest->tx = transfer_diff(src->tx, dest->tx);
386 tdb 1.47 dest->ipackets = transfer_diff(src->ipackets, dest->ipackets);
387     dest->opackets = transfer_diff(src->opackets, dest->opackets);
388     dest->ierrors = transfer_diff(src->ierrors, dest->ierrors);
389     dest->oerrors = transfer_diff(src->oerrors, dest->oerrors);
390     dest->collisions = transfer_diff(src->collisions, dest->collisions);
391 ats 1.24 dest->systime = src->systime - dest->systime;
392 pajs 1.3 }
393    
394 ats 1.24 *entries = diff_count;
395     return diff;
396     }
397 ats 1.59
398 ats 1.68 int sg_network_io_compare_name(const void *va, const void *vb) {
399     const sg_network_io_stats *a = (const sg_network_io_stats *)va;
400     const sg_network_io_stats *b = (const sg_network_io_stats *)vb;
401    
402     return strcmp(a->interface_name, b->interface_name);
403     }
404    
405 pajs 1.25 /* NETWORK INTERFACE STATS */
406    
407 ats 1.62 static void network_iface_stat_init(sg_network_iface_stats *s) {
408 ats 1.59 s->interface_name = NULL;
409     s->speed = 0;
410 tdb 1.74 s->duplex = SG_IFACE_DUPLEX_UNKNOWN;
411 pajs 1.25 }
412    
413 ats 1.62 static void network_iface_stat_destroy(sg_network_iface_stats *s) {
414 ats 1.59 free(s->interface_name);
415 pajs 1.25 }
416    
417 ats 1.62 sg_network_iface_stats *sg_get_network_iface_stats(int *entries){
418     VECTOR_DECLARE_STATIC(network_iface_stats, sg_network_iface_stats, 5,
419 tdb 1.65 network_iface_stat_init, network_iface_stat_destroy);
420 ats 1.62 sg_network_iface_stats *network_iface_stat_ptr;
421 ats 1.43 int ifaces = 0;
422 pajs 1.25
423     #ifdef SOLARIS
424 tdb 1.65 kstat_ctl_t *kc;
425     kstat_t *ksp;
426 pajs 1.25 kstat_named_t *knp;
427 ats 1.44 int sock;
428 pajs 1.25 #endif
429 pajs 1.26 #ifdef ALLBSD
430 tdb 1.65 struct ifaddrs *net, *net_ptr;
431 pajs 1.26 struct ifmediareq ifmed;
432 pajs 1.36 struct ifreq ifr;
433 ats 1.37 int sock;
434 pajs 1.26 int x;
435     #endif
436 pajs 1.27 #ifdef LINUX
437 tdb 1.65 FILE *f;
438     /* Horrible big enough, but it should be easily big enough */
439     char line[8096];
440 pajs 1.27 int sock;
441     #endif
442 ats 1.43
443 pajs 1.26 #ifdef ALLBSD
444 tdb 1.65 if(getifaddrs(&net) != 0){
445 ats 1.69 sg_set_error_with_errno(SG_ERROR_GETIFADDRS, NULL);
446 tdb 1.65 return NULL;
447     }
448 pajs 1.26
449 ats 1.37 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return NULL;
450 pajs 1.26
451     for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){
452 tdb 1.65 if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
453 ats 1.59
454     if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) {
455 tdb 1.65 return NULL;
456     }
457     network_iface_stat_ptr = network_iface_stats + ifaces;
458 pajs 1.26
459 ats 1.40 memset(&ifr, 0, sizeof(ifr));
460     strncpy(ifr.ifr_name, net_ptr->ifa_name, sizeof(ifr.ifr_name));
461    
462     if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0){
463     continue;
464     }
465     if((ifr.ifr_flags & IFF_UP) != 0){
466     network_iface_stat_ptr->up = 1;
467     }else{
468     network_iface_stat_ptr->up = 0;
469     }
470    
471 ats 1.62 if (sg_update_string(&network_iface_stat_ptr->interface_name,
472 tdb 1.65 net_ptr->ifa_name) < 0) {
473 ats 1.60 return NULL;
474     }
475 ats 1.40
476     network_iface_stat_ptr->speed = 0;
477 tdb 1.74 network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
478 ats 1.40 ifaces++;
479    
480 pajs 1.26 memset(&ifmed, 0, sizeof(struct ifmediareq));
481 ats 1.62 sg_strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name));
482 ats 1.37 if(ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){
483 ats 1.40 /* Not all interfaces support the media ioctls. */
484 pajs 1.26 continue;
485     }
486    
487     /* We may need to change this if we start doing wireless devices too */
488     if( (ifmed.ifm_active | IFM_ETHER) != ifmed.ifm_active ){
489     /* Not a ETHER device */
490     continue;
491     }
492    
493     /* Only intrested in the first 4 bits) - Assuming only ETHER devices */
494     x = ifmed.ifm_active & 0x0f;
495     switch(x){
496     /* 10 Mbit connections. Speedy :) */
497     case(IFM_10_T):
498     case(IFM_10_2):
499     case(IFM_10_5):
500     case(IFM_10_STP):
501     case(IFM_10_FL):
502     network_iface_stat_ptr->speed = 10;
503     break;
504     /* 100 Mbit conneections */
505     case(IFM_100_TX):
506     case(IFM_100_FX):
507     case(IFM_100_T4):
508     case(IFM_100_VG):
509     case(IFM_100_T2):
510     network_iface_stat_ptr->speed = 100;
511     break;
512     /* 1000 Mbit connections */
513     case(IFM_1000_SX):
514     case(IFM_1000_LX):
515     case(IFM_1000_CX):
516 tdb 1.54 #if defined(IFM_1000_TX) && !defined(OPENBSD)
517     case(IFM_1000_TX): /* FreeBSD 4 and others (but NOT OpenBSD)? */
518 tdb 1.45 #endif
519     #ifdef IFM_1000_FX
520     case(IFM_1000_FX): /* FreeBSD 4 */
521     #endif
522     #ifdef IFM_1000_T
523     case(IFM_1000_T): /* FreeBSD 5 */
524 tdb 1.28 #endif
525 pajs 1.26 network_iface_stat_ptr->speed = 1000;
526     break;
527     /* We don't know what it is */
528     default:
529     network_iface_stat_ptr->speed = 0;
530     break;
531     }
532    
533     if( (ifmed.ifm_active | IFM_FDX) == ifmed.ifm_active ){
534 tdb 1.74 network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_FULL;
535 pajs 1.26 }else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){
536 tdb 1.74 network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_HALF;
537 pajs 1.26 }else{
538 tdb 1.74 network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
539 pajs 1.26 }
540 pajs 1.36
541 pajs 1.26 }
542     freeifaddrs(net);
543 ats 1.37 close(sock);
544 pajs 1.26 #endif
545 pajs 1.25
546     #ifdef SOLARIS
547 ats 1.44 if ((kc = kstat_open()) == NULL) {
548 tdb 1.66 sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
549 ats 1.44 return NULL;
550     }
551    
552     if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) {
553 ats 1.69 sg_set_error_with_errno(SG_ERROR_SOCKET, NULL);
554 ats 1.44 return NULL;
555     }
556 pajs 1.25
557 ats 1.44 for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
558     if (!strcmp(ksp->ks_class, "net")) {
559     struct ifreq ifr;
560    
561     kstat_read(kc, ksp, NULL);
562    
563     strncpy(ifr.ifr_name, ksp->ks_name, sizeof ifr.ifr_name);
564     if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
565     /* Not a network interface. */
566 pajs 1.25 continue;
567     }
568 ats 1.44
569 ats 1.59 if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) {
570 pajs 1.25 return NULL;
571     }
572 pajs 1.26 network_iface_stat_ptr = network_iface_stats + ifaces;
573 ats 1.44 ifaces++;
574    
575 ats 1.62 if (sg_update_string(&network_iface_stat_ptr->interface_name,
576 tdb 1.65 ksp->ks_name) < 0) {
577 ats 1.60 return NULL;
578     }
579 pajs 1.25
580 ats 1.44 if ((ifr.ifr_flags & IFF_UP) != 0) {
581     network_iface_stat_ptr->up = 1;
582     } else {
583     network_iface_stat_ptr->up = 1;
584     }
585 pajs 1.35
586 ats 1.44 if ((knp = kstat_data_lookup(ksp, "ifspeed")) != NULL) {
587     network_iface_stat_ptr->speed = knp->value.ui64 / (1000 * 1000);
588     } else {
589     network_iface_stat_ptr->speed = 0;
590 pajs 1.25 }
591    
592 tdb 1.74 network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
593 ats 1.44 if ((knp = kstat_data_lookup(ksp, "link_duplex")) != NULL) {
594     switch (knp->value.ui32) {
595     case 1:
596 tdb 1.74 network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_HALF;
597 ats 1.44 break;
598     case 2:
599 tdb 1.74 network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_FULL;
600 ats 1.44 break;
601     }
602 pajs 1.33 }
603 pajs 1.25 }
604     }
605 ats 1.44
606     close(sock);
607 pajs 1.25 kstat_close(kc);
608     #endif
609 pajs 1.27 #ifdef LINUX
610     f = fopen("/proc/net/dev", "r");
611 tdb 1.65 if(f == NULL){
612 ats 1.69 sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/net/dev");
613 tdb 1.65 return NULL;
614     }
615 pajs 1.27
616     /* Setup stuff so we can do the ioctl to get the info */
617     if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
618 ats 1.69 sg_set_error_with_errno(SG_ERROR_SOCKET, NULL);
619 pajs 1.27 return NULL;
620     }
621    
622     /* Ignore first 2 lines.. Just headings */
623 tdb 1.66 if((fgets(line, sizeof(line), f)) == NULL) {
624     sg_set_error(SG_ERROR_PARSE, NULL);
625     return NULL;
626     }
627     if((fgets(line, sizeof(line), f)) == NULL) {
628     sg_set_error(SG_ERROR_PARSE, NULL);
629     return NULL;
630     }
631 pajs 1.27
632 tdb 1.65 while((fgets(line, sizeof(line), f)) != NULL){
633     char *name, *ptr;
634     struct ifreq ifr;
635     struct ethtool_cmd ethcmd;
636     int err;
637 pajs 1.27
638     /* Get the interface name */
639 tdb 1.65 ptr = strchr(line, ':');
640     if (ptr == NULL) continue;
641     *ptr='\0';
642     name = line;
643     while(isspace(*(name))){
644     name++;
645     }
646 pajs 1.27
647 tdb 1.65 memset(&ifr, 0, sizeof ifr);
648     strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
649 pajs 1.27
650 ats 1.38 if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
651     continue;
652     }
653 pajs 1.27
654     /* We have a good interface to add */
655 ats 1.59 if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) {
656 pajs 1.27 return NULL;
657     }
658     network_iface_stat_ptr = network_iface_stats + ifaces;
659 ats 1.60
660 ats 1.62 if (sg_update_string(&network_iface_stat_ptr->interface_name,
661 tdb 1.65 name) < 0) {
662 ats 1.60 return NULL;
663     }
664 ats 1.38 if ((ifr.ifr_flags & IFF_UP) != 0) {
665 pajs 1.35 network_iface_stat_ptr->up = 1;
666 ats 1.38 } else {
667 pajs 1.35 network_iface_stat_ptr->up = 0;
668     }
669    
670 tdb 1.65 memset(&ethcmd, 0, sizeof ethcmd);
671     ethcmd.cmd = ETHTOOL_GSET;
672     ifr.ifr_data = (caddr_t) &ethcmd;
673 ats 1.38
674 tdb 1.65 err = ioctl(sock, SIOCETHTOOL, &ifr);
675     if (err == 0) {
676 ats 1.38 network_iface_stat_ptr->speed = ethcmd.speed;
677    
678     switch (ethcmd.duplex) {
679 tdb 1.72 case DUPLEX_FULL:
680 tdb 1.74 network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_FULL;
681 ats 1.38 break;
682 tdb 1.72 case DUPLEX_HALF:
683 tdb 1.74 network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_HALF;
684 ats 1.38 break;
685     default:
686 tdb 1.74 network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
687 ats 1.38 }
688     } else {
689     /* Not all interfaces support the ethtool ioctl. */
690 ats 1.39 network_iface_stat_ptr->speed = 0;
691 tdb 1.74 network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
692 pajs 1.27 }
693 ats 1.38
694 pajs 1.27 ifaces++;
695     }
696 pajs 1.30 close(sock);
697 ats 1.38 fclose(f);
698 pajs 1.27 #endif
699 ats 1.71 #ifdef CYGWIN
700     sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin");
701     return NULL;
702     #endif
703     #ifdef HPUX
704     sg_set_error(SG_ERROR_UNSUPPORTED, "HP-UX");
705     return NULL;
706 tdb 1.74 #endif
707    
708     #ifdef SG_ENABLE_DEPRECATED
709     network_iface_stat_ptr->dup = network_iface_stat_ptr->duplex;
710 ats 1.71 #endif
711    
712 pajs 1.26 *entries = ifaces;
713 pajs 1.25 return network_iface_stats;
714 ats 1.68 }
715    
716     int sg_network_iface_compare_name(const void *va, const void *vb) {
717     const sg_network_iface_stats *a = (const sg_network_iface_stats *)va;
718     const sg_network_iface_stats *b = (const sg_network_iface_stats *)vb;
719    
720     return strcmp(a->interface_name, b->interface_name);
721 pajs 1.25 }
722