ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/network_stats.c
Revision: 1.49
Committed: Sat Mar 6 22:27:10 2004 UTC (20 years, 2 months ago) by ats
Content type: text/plain
Branch: MAIN
Changes since 1.48: +2 -2 lines
Log Message:
Make the regexp match array the right size for Linux.

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