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.28 by tdb, Thu Feb 12 23:58:32 2004 UTC vs.
Revision 1.64 by tdb, Tue Apr 6 14:52:58 2004 UTC

# Line 1 | Line 1
1   /*
2 < * i-scream central monitoring system
2 > * i-scream libstatgrab
3   * http://www.i-scream.org
4   * Copyright (C) 2000-2004 i-scream
5   *
# Line 28 | Line 28
28   #include <stdlib.h>
29   #include <string.h>
30   #include "statgrab.h"
31 + #include "vector.h"
32 + #include "tools.h"
33   #include <time.h>
34   #ifdef SOLARIS
35   #include <kstat.h>
36   #include <sys/sysinfo.h>
37 + #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 + #include <unistd.h>
44   #endif
45   #ifdef LINUX
46   #include <stdio.h>
# Line 41 | Line 50
50   #include <sys/socket.h>
51   #include <net/if.h>
52   #include <ctype.h>
44 #include "tools.h"
53   /* Stuff which could be defined by defining KERNEL, but
54   * that would be a bad idea, so we'll just declare it here
55   */
56   typedef __uint8_t u8;
57   typedef __uint16_t u16;
58   typedef __uint32_t u32;
59 + typedef __uint64_t u64;
60   #include <linux/ethtool.h>
61   #include <linux/sockios.h>
62 + #include <unistd.h>
63   #endif
64   #ifdef ALLBSD
65   #include <sys/types.h>
# Line 58 | Line 68 | typedef __uint32_t u32;
68   #include <net/if.h>
69   #include <net/if_media.h>
70   #include <sys/ioctl.h>
71 + #include <unistd.h>
72   #endif
73  
74 < static network_stat_t *network_stats=NULL;
75 < static int interfaces=0;
76 <
77 < void network_stat_init(int start, int end, network_stat_t *net_stats){
78 <
79 <        for(net_stats+=start; start<end; start++){
80 <                net_stats->interface_name=NULL;
81 <                net_stats->tx=0;
82 <                net_stats->rx=0;
72 <                net_stats++;
73 <        }
74 > static void network_stat_init(sg_network_io_stats *s) {
75 >        s->interface_name = NULL;
76 >        s->tx = 0;
77 >        s->rx = 0;
78 >        s->ipackets = 0;
79 >        s->opackets = 0;
80 >        s->ierrors = 0;
81 >        s->oerrors = 0;
82 >        s->collisions = 0;
83   }
84  
85 < network_stat_t *network_stat_malloc(int needed_entries, int *cur_entries, network_stat_t *net_stats){
86 <
78 <        if(net_stats==NULL){
79 <
80 <                if((net_stats=malloc(needed_entries * sizeof(network_stat_t)))==NULL){
81 <                        return NULL;
82 <                }
83 <                network_stat_init(0, needed_entries, net_stats);
84 <                *cur_entries=needed_entries;
85 <
86 <                return net_stats;
87 <        }
88 <
89 <
90 <        if(*cur_entries<needed_entries){
91 <                net_stats=realloc(net_stats, (sizeof(network_stat_t)*needed_entries));
92 <                if(net_stats==NULL){
93 <                        return NULL;
94 <                }
95 <                network_stat_init(*cur_entries, needed_entries, net_stats);
96 <                *cur_entries=needed_entries;
97 <        }
98 <
99 <        return net_stats;
85 > static void network_stat_destroy(sg_network_io_stats *s) {
86 >        free(s->interface_name);
87   }
88  
89 + VECTOR_DECLARE_STATIC(network_stats, sg_network_io_stats, 5,
90 +                      network_stat_init, network_stat_destroy);
91  
92 < network_stat_t *get_network_stats(int *entries){
92 > sg_network_io_stats *sg_get_network_io_stats(int *entries){
93 >        int interfaces;
94 >        sg_network_io_stats *network_stat_ptr;
95  
105        static int sizeof_network_stats=0;      
106        network_stat_t *network_stat_ptr;
107
96   #ifdef SOLARIS
97          kstat_ctl_t *kc;
98          kstat_t *ksp;
# Line 116 | Line 104 | network_stat_t *get_network_stats(int *entries){
104          /* Horrible big enough, but it should be easily big enough */
105          char line[8096];
106          regex_t regex;
107 <        regmatch_t line_match[4];
107 >        regmatch_t line_match[9];
108   #endif
109   #ifdef ALLBSD
110          struct ifaddrs *net, *net_ptr;
# Line 132 | Line 120 | network_stat_t *get_network_stats(int *entries){
120          
121          for(net_ptr=net;net_ptr!=NULL;net_ptr=net_ptr->ifa_next){
122                  if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
123 <                network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats);
124 <                if(network_stats==NULL){
123 >
124 >                if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) {
125                          return NULL;
126                  }
127                  network_stat_ptr=network_stats+interfaces;
128                  
129 <                if(network_stat_ptr->interface_name!=NULL) free(network_stat_ptr->interface_name);
130 <                network_stat_ptr->interface_name=strdup(net_ptr->ifa_name);
131 <                if(network_stat_ptr->interface_name==NULL) return NULL;
129 >                if (sg_update_string(&network_stat_ptr->interface_name,
130 >                                     net_ptr->ifa_name) < 0) {
131 >                        return NULL;
132 >                }
133                  net_data=(struct if_data *)net_ptr->ifa_data;
134                  network_stat_ptr->rx=net_data->ifi_ibytes;
135 <                network_stat_ptr->tx=net_data->ifi_obytes;                      
135 >                network_stat_ptr->tx=net_data->ifi_obytes;
136 >                network_stat_ptr->ipackets=net_data->ifi_ipackets;
137 >                network_stat_ptr->opackets=net_data->ifi_opackets;
138 >                network_stat_ptr->ierrors=net_data->ifi_ierrors;
139 >                network_stat_ptr->oerrors=net_data->ifi_oerrors;
140 >                network_stat_ptr->collisions=net_data->ifi_collisions;
141                  network_stat_ptr->systime=time(NULL);
142                  interfaces++;
143          }
# Line 162 | Line 156 | network_stat_t *get_network_stats(int *entries){
156                          kstat_read(kc, ksp, NULL);
157  
158   #ifdef SOL7
159 < #define RLOOKUP "rbytes"
160 < #define WLOOKUP "obytes"
159 > #define LRX "rbytes"
160 > #define LTX "obytes"
161 > #define LIPACKETS "ipackets"
162 > #define LOPACKETS "opackets"
163   #define VALTYPE value.ui32
164   #else
165 < #define RLOOKUP "rbytes64"
166 < #define WLOOKUP "obytes64"
165 > #define LRX "rbytes64"
166 > #define LTX "obytes64"
167 > #define LIPACKETS "ipackets64"
168 > #define LOPACKETS "opackets64"
169   #define VALTYPE value.ui64
170   #endif
171  
172 <                        if((knp=kstat_data_lookup(ksp, RLOOKUP))==NULL){
173 <                                /* Not a network interface, so skip to the next entry */
172 >                        /* Read rx */
173 >                        if((knp=kstat_data_lookup(ksp, LRX))==NULL){
174 >                                /* This is a network interface, but it doesn't
175 >                                 * have the rbytes/obytes values; for instance,
176 >                                 * the loopback devices have this behaviour
177 >                                 * (although they do track packets in/out). */
178 >                                /* FIXME: Show packet counts when byte counts
179 >                                 * not available. */
180                                  continue;
181                          }
182  
183 <                        network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats);
184 <                        if(network_stats==NULL){
183 >                        /* Create new network_stats */
184 >                        if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) {
185                                  return NULL;
186                          }
187                          network_stat_ptr=network_stats+interfaces;
188 +
189 +                        /* Finish reading rx */
190                          network_stat_ptr->rx=knp->VALTYPE;
191  
192 <                        if((knp=kstat_data_lookup(ksp, WLOOKUP))==NULL){
193 <                                /* Not a network interface, so skip to the next entry */
192 >                        /* Read tx */
193 >                        if((knp=kstat_data_lookup(ksp, LTX))==NULL){
194                                  continue;
195                          }
196                          network_stat_ptr->tx=knp->VALTYPE;
197 <                        if(network_stat_ptr->interface_name!=NULL){
198 <                                free(network_stat_ptr->interface_name);
197 >
198 >                        /* Read ipackets */
199 >                        if((knp=kstat_data_lookup(ksp, LIPACKETS))==NULL){
200 >                                continue;
201                          }
202 <                        network_stat_ptr->interface_name=strdup(ksp->ks_name);
202 >                        network_stat_ptr->ipackets=knp->VALTYPE;
203  
204 +                        /* Read opackets */
205 +                        if((knp=kstat_data_lookup(ksp, LOPACKETS))==NULL){
206 +                                continue;
207 +                        }
208 +                        network_stat_ptr->opackets=knp->VALTYPE;
209 +
210 +                        /* Read ierrors */
211 +                        if((knp=kstat_data_lookup(ksp, "ierrors"))==NULL){
212 +                                continue;
213 +                        }
214 +                        network_stat_ptr->ierrors=knp->value.ui32;
215 +
216 +                        /* Read oerrors */
217 +                        if((knp=kstat_data_lookup(ksp, "oerrors"))==NULL){
218 +                                continue;
219 +                        }
220 +                        network_stat_ptr->oerrors=knp->value.ui32;
221 +
222 +                        /* Read collisions */
223 +                        if((knp=kstat_data_lookup(ksp, "collisions"))==NULL){
224 +                                continue;
225 +                        }
226 +                        network_stat_ptr->collisions=knp->value.ui32;
227 +
228 +                        /* Read interface name */
229 +                        if (sg_update_string(&network_stat_ptr->interface_name,
230 +                                             ksp->ks_name) < 0) {
231 +                                return NULL;
232 +                        }
233 +
234 +                        /* Store systime */
235                          network_stat_ptr->systime=time(NULL);
236 +
237                          interfaces++;
238                  }
239          }
# Line 210 | Line 250 | network_stat_t *get_network_stats(int *entries){
250          fgets(line, sizeof(line), f);
251  
252  
253 <        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){
253 >        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){
254                  return NULL;
255          }
256  
257          interfaces=0;
258  
259          while((fgets(line, sizeof(line), f)) != NULL){
260 <                if((regexec(&regex, line, 4, line_match, 0))!=0){
260 >                if((regexec(&regex, line, 9, line_match, 0))!=0){
261                          continue;
262                  }
263 <                network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats);
264 <                if(network_stats==NULL){
265 <                        return NULL;
263 >
264 >                if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) {
265 >                        return NULL;
266                  }
267                  network_stat_ptr=network_stats+interfaces;
268  
# Line 230 | Line 270 | network_stat_t *get_network_stats(int *entries){
270                          free(network_stat_ptr->interface_name);
271                  }
272  
273 <                network_stat_ptr->interface_name=get_string_match(line, &line_match[1]);
274 <                network_stat_ptr->rx=get_ll_match(line, &line_match[2]);
275 <                network_stat_ptr->tx=get_ll_match(line, &line_match[3]);
273 >                network_stat_ptr->interface_name=sg_get_string_match(line, &line_match[1]);
274 >                network_stat_ptr->rx=sg_get_ll_match(line, &line_match[2]);
275 >                network_stat_ptr->tx=sg_get_ll_match(line, &line_match[5]);
276 >                network_stat_ptr->ipackets=sg_get_ll_match(line, &line_match[3]);
277 >                network_stat_ptr->opackets=sg_get_ll_match(line, &line_match[6]);
278 >                network_stat_ptr->ierrors=sg_get_ll_match(line, &line_match[4]);
279 >                network_stat_ptr->oerrors=sg_get_ll_match(line, &line_match[7]);
280 >                network_stat_ptr->collisions=sg_get_ll_match(line, &line_match[8]);
281                  network_stat_ptr->systime=time(NULL);
282  
283                  interfaces++;
# Line 251 | Line 296 | network_stat_t *get_network_stats(int *entries){
296          return network_stats;  
297   }
298  
299 < long long transfer_diff(long long new, long long old){
300 < #if defined(SOL7) || defined(LINUX) || defined(FREEBSD)
301 < #define MAXVAL 4294967296LL
299 > static long long transfer_diff(long long new, long long old){
300 > #if defined(SOL7) || defined(LINUX) || defined(FREEBSD) || defined(DFBSD) || defined(OPENBSD)
301 >        /* 32-bit quantities, so we must explicitly deal with wraparound. */
302 > #define MAXVAL 0x100000000LL
303 >        if (new >= old) {
304 >                return new - old;
305 >        } else {
306 >                return MAXVAL + new - old;
307 >        }
308   #else
309 < #define MAXVAL 18446744073709551616LL
309 >        /* 64-bit quantities, so plain subtraction works. */
310 >        return new - old;
311   #endif
260        long long result;
261        if(new>=old){
262                result = (new-old);
263        }else{
264                result = (MAXVAL+(new-old));
265        }
266
267        return result;
268
312   }
313  
314 < network_stat_t *get_network_stats_diff(int *entries) {
315 <        static network_stat_t *diff = NULL;
316 <        static int diff_count = 0;
317 <        network_stat_t *src, *dest;
318 <        int i, j, new_count;
314 > sg_network_io_stats *sg_get_network_io_stats_diff(int *entries) {
315 >        VECTOR_DECLARE_STATIC(diff, sg_network_io_stats, 1,
316 >                              network_stat_init, network_stat_destroy);
317 >        sg_network_io_stats *src = NULL, *dest;
318 >        int i, j, diff_count, new_count;
319  
320          if (network_stats == NULL) {
321                  /* No previous stats, so we can't calculate a difference. */
322 <                return get_network_stats(entries);
322 >                return sg_get_network_io_stats(entries);
323          }
324  
325          /* Resize the results array to match the previous stats. */
326 <        diff = network_stat_malloc(interfaces, &diff_count, diff);
327 <        if (diff == NULL) {
326 >        diff_count = VECTOR_SIZE(network_stats);
327 >        if (VECTOR_RESIZE(diff, diff_count) < 0) {
328                  return NULL;
329          }
330  
# Line 290 | Line 333 | network_stat_t *get_network_stats_diff(int *entries) {
333                  src = &network_stats[i];
334                  dest = &diff[i];
335  
336 <                if (dest->interface_name != NULL) {
337 <                        free(dest->interface_name);
336 >                if (sg_update_string(&dest->interface_name,
337 >                                     src->interface_name) < 0) {
338 >                        return NULL;
339                  }
296                dest->interface_name = strdup(src->interface_name);
340                  dest->rx = src->rx;
341                  dest->tx = src->tx;
342 +                dest->ipackets = src->ipackets;
343 +                dest->opackets = src->opackets;
344 +                dest->ierrors = src->ierrors;
345 +                dest->oerrors = src->oerrors;
346 +                dest->collisions = src->collisions;
347                  dest->systime = src->systime;
348          }
349  
350          /* Get a new set of stats. */
351 <        if (get_network_stats(&new_count) == NULL) {
351 >        if (sg_get_network_io_stats(&new_count) == NULL) {
352                  return NULL;
353          }
354  
# Line 326 | Line 374 | network_stat_t *get_network_stats_diff(int *entries) {
374                     difference. */
375                  dest->rx = transfer_diff(src->rx, dest->rx);
376                  dest->tx = transfer_diff(src->tx, dest->tx);
377 +                dest->ipackets = transfer_diff(src->ipackets, dest->ipackets);
378 +                dest->opackets = transfer_diff(src->opackets, dest->opackets);
379 +                dest->ierrors = transfer_diff(src->ierrors, dest->ierrors);
380 +                dest->oerrors = transfer_diff(src->oerrors, dest->oerrors);
381 +                dest->collisions = transfer_diff(src->collisions, dest->collisions);
382                  dest->systime = src->systime - dest->systime;
383          }
384  
385          *entries = diff_count;
386          return diff;
387   }
388 +
389   /* NETWORK INTERFACE STATS */
390  
391 < void network_iface_stat_init(int start, int end, network_iface_stat_t *net_stats){
392 <
393 <        for(net_stats+=start; start<end; start++){
394 <                net_stats->interface_name=NULL;
341 <                net_stats->speed=0;
342 <                net_stats->dup=NO_DUPLEX;
343 <                net_stats++;
344 <        }
391 > static void network_iface_stat_init(sg_network_iface_stats *s) {
392 >        s->interface_name = NULL;
393 >        s->speed = 0;
394 >        s->dup = SG_IFACE_DUPLEX_UNKNOWN;
395   }
396  
397 < network_iface_stat_t *network_iface_stat_malloc(int needed_entries, int *cur_entries, network_iface_stat_t *net_stats){
398 <
349 <        if(net_stats==NULL){
350 <
351 <                if((net_stats=malloc(needed_entries * sizeof(network_iface_stat_t)))==NULL){
352 <                        return NULL;
353 <                }
354 <                network_iface_stat_init(0, needed_entries, net_stats);
355 <                *cur_entries=needed_entries;
356 <
357 <                return net_stats;
358 <        }
359 <
360 <
361 <        if(*cur_entries<needed_entries){
362 <                net_stats=realloc(net_stats, (sizeof(network_iface_stat_t)*needed_entries));
363 <                if(net_stats==NULL){
364 <                        return NULL;
365 <                }
366 <                network_iface_stat_init(*cur_entries, needed_entries, net_stats);
367 <                *cur_entries=needed_entries;
368 <        }
369 <
370 <        return net_stats;
397 > static void network_iface_stat_destroy(sg_network_iface_stats *s) {
398 >        free(s->interface_name);
399   }
400  
401 < network_iface_stat_t *get_network_iface_stats(int *entries){
402 <        static network_iface_stat_t *network_iface_stats;
403 <        network_iface_stat_t *network_iface_stat_ptr;
404 <        static int sizeof_network_iface_stats=0;        
405 <        static int ifaces;
401 > sg_network_iface_stats *sg_get_network_iface_stats(int *entries){
402 >        VECTOR_DECLARE_STATIC(network_iface_stats, sg_network_iface_stats, 5,
403 >                              network_iface_stat_init, network_iface_stat_destroy);
404 >        sg_network_iface_stats *network_iface_stat_ptr;
405 >        int ifaces = 0;
406  
407   #ifdef SOLARIS
408          kstat_ctl_t *kc;
409          kstat_t *ksp;
410          kstat_named_t *knp;
411 +        int sock;
412   #endif
413   #ifdef ALLBSD
414          struct ifaddrs *net, *net_ptr;
415          struct ifmediareq ifmed;
416 <        int s;
416 >        struct ifreq ifr;
417 >        int sock;
418          int x;
419   #endif
420   #ifdef LINUX
421          FILE *f;
422          /* Horrible big enough, but it should be easily big enough */
423          char line[8096];
394        void *eth_tool_cmd_buf;
395        int buf_size;
424          int sock;
425   #endif
426 <        ifaces = 0;
426 >
427   #ifdef ALLBSD
428          if(getifaddrs(&net) != 0){
429                  return NULL;
430          }
431  
432 <        if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == NULL) return NULL;
432 >        if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return NULL;
433  
434          for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){
435                  if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
436 <                network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
437 <                if(network_iface_stats==NULL){
436 >
437 >                if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) {
438                          return NULL;
439                  }
440                  network_iface_stat_ptr = network_iface_stats + ifaces;
441  
442 +                memset(&ifr, 0, sizeof(ifr));
443 +                strncpy(ifr.ifr_name, net_ptr->ifa_name, sizeof(ifr.ifr_name));
444 +
445 +                if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0){
446 +                        continue;
447 +                }      
448 +                if((ifr.ifr_flags & IFF_UP) != 0){
449 +                        network_iface_stat_ptr->up = 1;
450 +                }else{
451 +                        network_iface_stat_ptr->up = 0;
452 +                }
453 +
454 +                if (sg_update_string(&network_iface_stat_ptr->interface_name,
455 +                                     net_ptr->ifa_name) < 0) {
456 +                        return NULL;
457 +                }
458 +
459 +                network_iface_stat_ptr->speed = 0;
460 +                network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_UNKNOWN;
461 +                ifaces++;
462 +
463                  memset(&ifmed, 0, sizeof(struct ifmediareq));
464 <                strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name));
465 <                if(ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){
464 >                sg_strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name));
465 >                if(ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){
466 >                        /* Not all interfaces support the media ioctls. */
467                          continue;
468                  }
469  
# Line 423 | Line 473 | network_iface_stat_t *get_network_iface_stats(int *ent
473                          continue;
474                  }
475  
426                if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
427                network_iface_stat_ptr->interface_name = strdup(net_ptr->ifa_name);
428                if(network_iface_stat_ptr->interface_name == NULL) return NULL;
429
476                  /* Only intrested in the first 4 bits)  - Assuming only ETHER devices */
477                  x = ifmed.ifm_active & 0x0f;    
478                  switch(x){
# Line 450 | Line 496 | network_iface_stat_t *get_network_iface_stats(int *ent
496                          case(IFM_1000_SX):
497                          case(IFM_1000_LX):
498                          case(IFM_1000_CX):
499 < #ifdef FREEBSD5
500 <                        case(IFM_1000_T):
455 < #else
456 <                        case(IFM_1000_TX):
457 <                        case(IFM_1000_FX):
499 > #if defined(IFM_1000_TX) && !defined(OPENBSD)
500 >                        case(IFM_1000_TX): /* FreeBSD 4 and others (but NOT OpenBSD)? */
501   #endif
502 + #ifdef IFM_1000_FX
503 +                        case(IFM_1000_FX): /* FreeBSD 4 */
504 + #endif
505 + #ifdef IFM_1000_T
506 +                        case(IFM_1000_T): /* FreeBSD 5 */
507 + #endif
508                                  network_iface_stat_ptr->speed = 1000;
509                                  break;
510                          /* We don't know what it is */
# Line 465 | Line 514 | network_iface_stat_t *get_network_iface_stats(int *ent
514                  }
515  
516                  if( (ifmed.ifm_active | IFM_FDX) == ifmed.ifm_active ){
517 <                        network_iface_stat_ptr->dup = FULL_DUPLEX;
517 >                        network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_FULL;
518                  }else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){
519 <                        network_iface_stat_ptr->dup = HALF_DUPLEX;
519 >                        network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_HALF;
520                  }else{
521 <                        network_iface_stat_ptr->dup = NO_DUPLEX;
521 >                        network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_UNKNOWN;
522                  }
523 <                ifaces++;
523 >
524          }      
525          freeifaddrs(net);
526 +        close(sock);
527   #endif
528  
529   #ifdef SOLARIS
530 <        if ((kc = kstat_open()) == NULL) {
531 <                return NULL;
532 <        }
530 >        if ((kc = kstat_open()) == NULL) {
531 >                return NULL;
532 >        }
533  
534 <        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
535 <                if (!strcmp(ksp->ks_class, "net")) {
536 <                        kstat_read(kc, ksp, NULL);
537 <                        if((knp=kstat_data_lookup(ksp, "ifspeed"))==NULL){
538 <                                /* Not a network interface, so skip to the next entry */
534 >        if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) {
535 >                return NULL;
536 >        }
537 >
538 >        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
539 >                if (!strcmp(ksp->ks_class, "net")) {
540 >                        struct ifreq ifr;
541 >
542 >                        kstat_read(kc, ksp, NULL);
543 >
544 >                        strncpy(ifr.ifr_name, ksp->ks_name, sizeof ifr.ifr_name);
545 >                        if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
546 >                                /* Not a network interface. */
547                                  continue;
548                          }
549 <                        network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
550 <                        if(network_iface_stats==NULL){
549 >
550 >                        if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) {
551                                  return NULL;
552                          }
553                          network_iface_stat_ptr = network_iface_stats + ifaces;
554 <                        network_iface_stat_ptr->speed = knp->value.ui64 / (1000*1000);
554 >                        ifaces++;
555  
556 <                        if((knp=kstat_data_lookup(ksp, "link_duplex"))==NULL){
557 <                                /* Not a network interface, so skip to the next entry */
558 <                                continue;
556 >                        if (sg_update_string(&network_iface_stat_ptr->interface_name,
557 >                                             ksp->ks_name) < 0) {
558 >                                return NULL;
559                          }
560  
561 <                        if(knp->value.ui64 == 0){
562 <                                network_iface_stat_ptr->dup = FULL_DUPLEX;
563 <                        }else{
564 <                                network_iface_stat_ptr->dup = HALF_DUPLEX;
561 >                        if ((ifr.ifr_flags & IFF_UP) != 0) {
562 >                                network_iface_stat_ptr->up = 1;
563 >                        } else {
564 >                                network_iface_stat_ptr->up = 1;
565                          }
566  
567 <                        if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
568 <                        network_iface_stat_ptr->interface_name = strdup(ksp->ks_name);
569 <                        if(network_iface_stat_ptr->interface_name == NULL) return NULL;
570 <                        ifaces++;
567 >                        if ((knp = kstat_data_lookup(ksp, "ifspeed")) != NULL) {
568 >                                network_iface_stat_ptr->speed = knp->value.ui64 / (1000 * 1000);
569 >                        } else {
570 >                                network_iface_stat_ptr->speed = 0;
571 >                        }
572 >
573 >                        network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_UNKNOWN;
574 >                        if ((knp = kstat_data_lookup(ksp, "link_duplex")) != NULL) {
575 >                                switch (knp->value.ui32) {
576 >                                case 1:
577 >                                        network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_HALF;
578 >                                        break;
579 >                                case 2:
580 >                                        network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_FULL;
581 >                                        break;
582 >                                }
583 >                        }
584                  }
585          }
586 +
587 +        close(sock);
588          kstat_close(kc);
516        
589   #endif  
590   #ifdef LINUX
519
591          f = fopen("/proc/net/dev", "r");
592          if(f == NULL){
593                  return NULL;
# Line 527 | Line 598 | network_iface_stat_t *get_network_iface_stats(int *ent
598                  return NULL;
599          }
600  
530        buf_size = sizeof(struct ethtool_cmd);
531        eth_tool_cmd_buf = malloc(buf_size);
532        if(eth_tool_cmd_buf == NULL) return NULL;
533
601          /* Ignore first 2 lines.. Just headings */
602 <        fgets(line, sizeof(line), f);
603 <        fgets(line, sizeof(line), f);
602 >        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
603 >        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
604  
605          while((fgets(line, sizeof(line), f)) != NULL){
606                  char *name, *ptr;
607                  struct ifreq ifr;
608 <                struct ethtool_cmd *ethcmd;
608 >                struct ethtool_cmd ethcmd;
609                  int err;
610  
611                  /* Get the interface name */
# Line 550 | Line 617 | network_iface_stat_t *get_network_iface_stats(int *ent
617                          name++;
618                  }
619  
620 <                memset(&ifr, 0, sizeof(ifr));
621 <                memset(eth_tool_cmd_buf, 0, buf_size);
555 <                ifr.ifr_data = (caddr_t) eth_tool_cmd_buf;
556 <                strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
620 >                memset(&ifr, 0, sizeof ifr);
621 >                strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
622  
623 <                ethcmd = (struct ethtool_cmd *)(&ifr)->ifr_data;
624 <                ethcmd->cmd = ETHTOOL_GSET;
623 >                if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
624 >                        continue;
625 >                }
626  
561                err = ioctl(sock, SIOCETHTOOL, &ifr);
562                if(err < 0){
563                        /* This could fail if the interface doesn't support the command. Carry
564                         * on to the next :)
565                         */
566                        continue;
567                }
568
627                  /* We have a good interface to add */
628 <                network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
571 <                if(network_iface_stats==NULL){
628 >                if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) {
629                          return NULL;
630                  }
631                  network_iface_stat_ptr = network_iface_stats + ifaces;
632 <                network_iface_stat_ptr->interface_name = strdup(name);
633 <                network_iface_stat_ptr->speed = ethcmd->speed;
634 <                network_iface_stat_ptr->dup = NO_DUPLEX;
635 <                if(ethcmd->duplex == 0x00){
579 <                        network_iface_stat_ptr->dup = FULL_DUPLEX;
632 >                
633 >                if (sg_update_string(&network_iface_stat_ptr->interface_name,
634 >                                     name) < 0) {
635 >                        return NULL;
636                  }
637 <                if(ethcmd->duplex == 0x01){
638 <                        network_iface_stat_ptr->dup = HALF_DUPLEX;
637 >                if ((ifr.ifr_flags & IFF_UP) != 0) {
638 >                        network_iface_stat_ptr->up = 1;
639 >                } else {
640 >                        network_iface_stat_ptr->up = 0;
641                  }
642 +
643 +                memset(&ethcmd, 0, sizeof ethcmd);
644 +                ethcmd.cmd = ETHTOOL_GSET;
645 +                ifr.ifr_data = (caddr_t) &ethcmd;
646 +
647 +                err = ioctl(sock, SIOCETHTOOL, &ifr);
648 +                if (err == 0) {
649 +                        network_iface_stat_ptr->speed = ethcmd.speed;
650 +
651 +                        switch (ethcmd.duplex) {
652 +                        case 0x00:
653 +                                network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_FULL;
654 +                                break;
655 +                        case 0x01:
656 +                                network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_HALF;
657 +                                break;
658 +                        default:
659 +                                network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_UNKNOWN;
660 +                        }
661 +                } else {
662 +                        /* Not all interfaces support the ethtool ioctl. */
663 +                        network_iface_stat_ptr->speed = 0;
664 +                        network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_UNKNOWN;
665 +                }
666 +
667                  ifaces++;
668          }
669 <
670 <        free(eth_tool_cmd_buf);
669 >        close(sock);
670 >        fclose(f);
671   #endif
672          *entries = ifaces;
673          return network_iface_stats;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines