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.32 by ats, Fri Feb 13 15:24:16 2004 UTC vs.
Revision 1.60 by ats, Mon Apr 5 00:16:24 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines