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.59 by ats, Sun Apr 4 22:52:16 2004 UTC

# Line 28 | 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>
# Line 48 | Line 55
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;
# Line 144 | Line 130 | network_stat_t *get_network_stats(int *entries){
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;                      
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          }
# Line 163 | 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 211 | 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 233 | 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++;
# Line 253 | Line 295 | network_stat_t *get_network_stats(int *entries){
295   }
296  
297   long long transfer_diff(long long new, long long old){
298 < #if defined(SOL7) || defined(LINUX) || defined(FREEBSD)
299 < #define MAXVAL 4294967296LL
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 18446744073709551616LL
307 >        /* 64-bit quantities, so plain subtraction works. */
308 >        return new - old;
309   #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
310   }
311  
312   network_stat_t *get_network_stats_diff(int *entries) {
313 <        static network_stat_t *diff = NULL;
314 <        static int diff_count = 0;
315 <        network_stat_t *src, *dest;
316 <        int i, j, new_count;
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          if (network_stats == NULL) {
319                  /* No previous stats, so we can't calculate a difference. */
# Line 281 | Line 321 | network_stat_t *get_network_stats_diff(int *entries) {
321          }
322  
323          /* Resize the results array to match the previous stats. */
324 <        diff = network_stat_malloc(interfaces, &diff_count, diff);
325 <        if (diff == NULL) {
324 >        diff_count = VECTOR_SIZE(network_stats);
325 >        if (VECTOR_RESIZE(diff, diff_count) < 0) {
326                  return NULL;
327          }
328  
# Line 297 | Line 337 | network_stat_t *get_network_stats_diff(int *entries) {
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  
# Line 327 | Line 372 | network_stat_t *get_network_stats_diff(int *entries) {
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          }
382  
383          *entries = diff_count;
384          return diff;
385   }
386 +
387   /* NETWORK INTERFACE STATS */
388  
389 < void network_iface_stat_init(int start, int end, network_iface_stat_t *net_stats){
390 <
391 <        for(net_stats+=start; start<end; start++){
392 <                net_stats->interface_name=NULL;
342 <                net_stats->speed=0;
343 <                net_stats->dup=UNKNOWN_DUPLEX;
344 <                net_stats++;
345 <        }
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 < network_iface_stat_t *network_iface_stat_malloc(int needed_entries, int *cur_entries, network_iface_stat_t *net_stats){
396 <
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;
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 <        static network_iface_stat_t *network_iface_stats;
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 <        static int sizeof_network_iface_stats=0;        
378 <        static int ifaces;
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 <        int s;
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];
395        void *eth_tool_cmd_buf;
396        int buf_size;
422          int sock;
423   #endif
424 <        ifaces = 0;
424 >
425   #ifdef ALLBSD
426          if(getifaddrs(&net) != 0){
427                  return NULL;
428          }
429  
430 <        if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == NULL) return NULL;
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 <                network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
435 <                if(network_iface_stats==NULL){
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 +                        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(s, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){
462 >                if(ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){
463 >                        /* Not all interfaces support the media ioctls. */
464                          continue;
465                  }
466  
# Line 424 | Line 470 | network_iface_stat_t *get_network_iface_stats(int *ent
470                          continue;
471                  }
472  
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
473                  /* Only intrested in the first 4 bits)  - Assuming only ETHER devices */
474                  x = ifmed.ifm_active & 0x0f;    
475                  switch(x){
# Line 451 | Line 493 | network_iface_stat_t *get_network_iface_stats(int *ent
493                          case(IFM_1000_SX):
494                          case(IFM_1000_LX):
495                          case(IFM_1000_CX):
496 < #if defined(FREEBSD) && !defined(FREEBSD5)
497 <                        case(IFM_1000_TX):
456 <                        case(IFM_1000_FX):
457 < #else
458 <                        case(IFM_1000_T):
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 */
# Line 472 | Line 517 | network_iface_stat_t *get_network_iface_stats(int *ent
517                  }else{
518                          network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
519                  }
520 <                ifaces++;
520 >
521          }      
522          freeifaddrs(net);
523 <        close(s);
523 >        close(sock);
524   #endif
525  
526   #ifdef SOLARIS
527 <        if ((kc = kstat_open()) == NULL) {
528 <                return NULL;
529 <        }
527 >        if ((kc = kstat_open()) == NULL) {
528 >                return NULL;
529 >        }
530  
531 <        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
532 <                if (!strcmp(ksp->ks_class, "net")) {
533 <                        kstat_read(kc, ksp, NULL);
534 <                        if((knp=kstat_data_lookup(ksp, "ifspeed"))==NULL){
535 <                                /* Not a network interface, so skip to the next entry */
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 <                        network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
547 <                        if(network_iface_stats==NULL){
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 <                        network_iface_stat_ptr->speed = knp->value.ui64 / (1000*1000);
551 >                        ifaces++;
552  
553 <                        if((knp=kstat_data_lookup(ksp, "link_duplex"))==NULL){
554 <                                /* Not a network interface, so skip to the next entry */
555 <                                continue;
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->value.ui64 == 0){
564 <                                network_iface_stat_ptr->dup = FULL_DUPLEX;
565 <                        }else{
566 <                                network_iface_stat_ptr->dup = HALF_DUPLEX;
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 <                        if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
570 <                        network_iface_stat_ptr->interface_name = strdup(ksp->ks_name);
571 <                        if(network_iface_stat_ptr->interface_name == NULL) return NULL;
572 <                        ifaces++;
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 >                        }
580                  }
581          }
582 +
583 +        close(sock);
584          kstat_close(kc);
518        
585   #endif  
586   #ifdef LINUX
521
587          f = fopen("/proc/net/dev", "r");
588          if(f == NULL){
589                  return NULL;
# Line 529 | Line 594 | network_iface_stat_t *get_network_iface_stats(int *ent
594                  return NULL;
595          }
596  
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
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;
# Line 540 | Line 601 | network_iface_stat_t *get_network_iface_stats(int *ent
601          while((fgets(line, sizeof(line), f)) != NULL){
602                  char *name, *ptr;
603                  struct ifreq ifr;
604 <                struct ethtool_cmd *ethcmd;
604 >                struct ethtool_cmd ethcmd;
605                  int err;
606  
607                  /* Get the interface name */
# Line 552 | Line 613 | network_iface_stat_t *get_network_iface_stats(int *ent
613                          name++;
614                  }
615  
616 <                memset(&ifr, 0, sizeof(ifr));
617 <                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));
616 >                memset(&ifr, 0, sizeof ifr);
617 >                strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
618  
619 <                ethcmd = (struct ethtool_cmd *) ifr.ifr_data;
620 <                ethcmd->cmd = ETHTOOL_GSET;
619 >                if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
620 >                        continue;
621 >                }
622  
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
623                  /* We have a good interface to add */
624 <                network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
573 <                if(network_iface_stats==NULL){
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 <                network_iface_stat_ptr->speed = ethcmd->speed;
630 <                network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
631 <                if(ethcmd->duplex == 0x00){
632 <                        network_iface_stat_ptr->dup = FULL_DUPLEX;
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 <                if(ethcmd->duplex == 0x01){
635 <                        network_iface_stat_ptr->dup = HALF_DUPLEX;
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 <        free(eth_tool_cmd_buf);
662 >        fclose(f);
663   #endif
664          *entries = ifaces;
665          return network_iface_stats;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines