ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/network_stats.c
Revision: 1.60
Committed: Mon Apr 5 00:16:24 2004 UTC (20 years, 1 month ago) by ats
Content type: text/plain
Branch: MAIN
Changes since 1.59: +25 -18 lines
Log Message:
Use update_string.

File Contents

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