ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/network_stats.c
Revision: 1.46
Committed: Sat Mar 6 19:04:29 2004 UTC (20 years, 2 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.45: +7 -2 lines
Log Message:
Add getting errors, packets, and collisions for network interfaces on BSD.

File Contents

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