ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/network_stats.c
(Generate patch)

Comparing projects/libstatgrab/src/libstatgrab/network_stats.c (file contents):
Revision 1.11 by tdb, Sat Mar 29 18:51:13 2003 UTC vs.
Revision 1.59 by ats, Sun Apr 4 22:52:16 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines