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.36 by pajs, Fri Feb 13 18:54:29 2004 UTC vs.
Revision 1.78 by tdb, Mon Oct 9 14:09:38 2006 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>
47   #include <sys/types.h>
39 #include <regex.h>
48   #include <sys/ioctl.h>
49   #include <sys/socket.h>
50   #include <net/if.h>
51   #include <ctype.h>
52 < #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 < */
52 > #include <linux/version.h>
53 > #include <asm/types.h>
54 > /* These aren't defined by asm/types.h unless the kernel is being
55 >   compiled, but some versions of ethtool.h need them. */
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>
# Line 59 | 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 + #ifdef WIN32
74 + #include <windows.h>
75 + #include <Iphlpapi.h>
76 + #include "win32.h"
77 + #endif
78  
79 < static network_stat_t *network_stats=NULL;
80 < static int interfaces=0;
79 > static void network_stat_init(sg_network_io_stats *s) {
80 >        s->interface_name = NULL;
81 >        s->tx = 0;
82 >        s->rx = 0;
83 >        s->ipackets = 0;
84 >        s->opackets = 0;
85 >        s->ierrors = 0;
86 >        s->oerrors = 0;
87 >        s->collisions = 0;
88 > }
89  
90 < void network_stat_init(int start, int end, network_stat_t *net_stats){
91 <
69 <        for(net_stats+=start; start<end; start++){
70 <                net_stats->interface_name=NULL;
71 <                net_stats->tx=0;
72 <                net_stats->rx=0;
73 <                net_stats++;
74 <        }
90 > static void network_stat_destroy(sg_network_io_stats *s) {
91 >        free(s->interface_name);
92   }
93  
94 < network_stat_t *network_stat_malloc(int needed_entries, int *cur_entries, network_stat_t *net_stats){
94 > VECTOR_DECLARE_STATIC(network_stats, sg_network_io_stats, 5,
95 >                      network_stat_init, network_stat_destroy);
96  
97 <        if(net_stats==NULL){
97 > #ifdef WIN32
98 > static PMIB_IFTABLE win32_get_devices()
99 > {
100 >        PMIB_IFTABLE if_table;
101 >        PMIB_IFTABLE tmp;
102 >        unsigned long dwSize = 0;
103  
104 <                if((net_stats=malloc(needed_entries * sizeof(network_stat_t)))==NULL){
105 <                        return NULL;
106 <                }
107 <                network_stat_init(0, needed_entries, net_stats);
85 <                *cur_entries=needed_entries;
86 <
87 <                return net_stats;
104 >        // Allocate memory for pointers
105 >        if_table = sg_malloc(sizeof(MIB_IFTABLE));
106 >        if(if_table == NULL) {
107 >                return NULL;
108          }
109  
110 <
111 <        if(*cur_entries<needed_entries){
112 <                net_stats=realloc(net_stats, (sizeof(network_stat_t)*needed_entries));
113 <                if(net_stats==NULL){
110 >        // Get necessary size for the buffer
111 >        if(GetIfTable(if_table, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) {
112 >                tmp = sg_realloc(if_table, dwSize);
113 >                if(tmp == NULL) {
114 >                        free(if_table);
115                          return NULL;
116                  }
117 <                network_stat_init(*cur_entries, needed_entries, net_stats);
97 <                *cur_entries=needed_entries;
117 >                if_table = tmp;
118          }
119  
120 <        return net_stats;
120 >        // Get the data
121 >        if(GetIfTable(if_table, &dwSize, 0) != NO_ERROR) {
122 >                free(if_table);
123 >                return NULL;
124 >        }
125 >        return if_table;
126   }
127 + #endif /* WIN32 */
128  
129 + sg_network_io_stats *sg_get_network_io_stats(int *entries){
130 +        int interfaces;
131 +        sg_network_io_stats *network_stat_ptr;
132  
104 network_stat_t *get_network_stats(int *entries){
105
106        static int sizeof_network_stats=0;      
107        network_stat_t *network_stat_ptr;
108
133   #ifdef SOLARIS
134 <        kstat_ctl_t *kc;
135 <        kstat_t *ksp;
134 >        kstat_ctl_t *kc;
135 >        kstat_t *ksp;
136          kstat_named_t *knp;
137   #endif
138  
# Line 117 | Line 141 | network_stat_t *get_network_stats(int *entries){
141          /* Horrible big enough, but it should be easily big enough */
142          char line[8096];
143          regex_t regex;
144 <        regmatch_t line_match[4];
144 >        regmatch_t line_match[9];
145   #endif
146   #ifdef ALLBSD
147          struct ifaddrs *net, *net_ptr;
148          struct if_data *net_data;
149   #endif
150 + #ifdef WIN32
151 +        PMIB_IFTABLE if_table;
152 +        MIB_IFROW if_row;
153 +        int i, no, j;
154  
155 +        /* used for duplicate interface names. 5 for space, hash, up to two
156 +         * numbers and terminating slash */
157 +        char buf[5];
158 + #endif
159 +
160   #ifdef ALLBSD
161          if(getifaddrs(&net) != 0){
162 +                sg_set_error_with_errno(SG_ERROR_GETIFADDRS, NULL);
163                  return NULL;
164          }
165  
# Line 133 | Line 167 | network_stat_t *get_network_stats(int *entries){
167          
168          for(net_ptr=net;net_ptr!=NULL;net_ptr=net_ptr->ifa_next){
169                  if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
170 <                network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats);
171 <                if(network_stats==NULL){
170 >
171 >                if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) {
172                          return NULL;
173                  }
174                  network_stat_ptr=network_stats+interfaces;
175                  
176 <                if(network_stat_ptr->interface_name!=NULL) free(network_stat_ptr->interface_name);
177 <                network_stat_ptr->interface_name=strdup(net_ptr->ifa_name);
178 <                if(network_stat_ptr->interface_name==NULL) return NULL;
176 >                if (sg_update_string(&network_stat_ptr->interface_name,
177 >                                     net_ptr->ifa_name) < 0) {
178 >                        return NULL;
179 >                }
180                  net_data=(struct if_data *)net_ptr->ifa_data;
181                  network_stat_ptr->rx=net_data->ifi_ibytes;
182 <                network_stat_ptr->tx=net_data->ifi_obytes;                      
182 >                network_stat_ptr->tx=net_data->ifi_obytes;
183 >                network_stat_ptr->ipackets=net_data->ifi_ipackets;
184 >                network_stat_ptr->opackets=net_data->ifi_opackets;
185 >                network_stat_ptr->ierrors=net_data->ifi_ierrors;
186 >                network_stat_ptr->oerrors=net_data->ifi_oerrors;
187 >                network_stat_ptr->collisions=net_data->ifi_collisions;
188                  network_stat_ptr->systime=time(NULL);
189                  interfaces++;
190          }
# Line 152 | Line 192 | network_stat_t *get_network_stats(int *entries){
192   #endif
193  
194   #ifdef SOLARIS
195 <        if ((kc = kstat_open()) == NULL) {
196 <                return NULL;
197 <        }
195 >        if ((kc = kstat_open()) == NULL) {
196 >                sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
197 >                return NULL;
198 >        }
199  
200          interfaces=0;
201  
202 <        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
203 <                if (!strcmp(ksp->ks_class, "net")) {
204 <                        kstat_read(kc, ksp, NULL);
202 >        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
203 >                if (strcmp(ksp->ks_class, "net") == 0) {
204 >                        kstat_read(kc, ksp, NULL);
205  
206   #ifdef SOL7
207 < #define RLOOKUP "rbytes"
208 < #define WLOOKUP "obytes"
207 > #define LRX "rbytes"
208 > #define LTX "obytes"
209 > #define LIPACKETS "ipackets"
210 > #define LOPACKETS "opackets"
211   #define VALTYPE value.ui32
212   #else
213 < #define RLOOKUP "rbytes64"
214 < #define WLOOKUP "obytes64"
213 > #define LRX "rbytes64"
214 > #define LTX "obytes64"
215 > #define LIPACKETS "ipackets64"
216 > #define LOPACKETS "opackets64"
217   #define VALTYPE value.ui64
218   #endif
219  
220 <                        if((knp=kstat_data_lookup(ksp, RLOOKUP))==NULL){
221 <                                /* Not a network interface, so skip to the next entry */
220 >                        /* Read rx */
221 >                        if((knp=kstat_data_lookup(ksp, LRX))==NULL){
222 >                                /* This is a network interface, but it doesn't
223 >                                 * have the rbytes/obytes values; for instance,
224 >                                 * the loopback devices have this behaviour
225 >                                 * (although they do track packets in/out). */
226 >                                /* FIXME: Show packet counts when byte counts
227 >                                 * not available. */
228                                  continue;
229                          }
230  
231 <                        network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats);
232 <                        if(network_stats==NULL){
231 >                        /* Create new network_stats */
232 >                        if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) {
233 >                                kstat_close(kc);
234                                  return NULL;
235                          }
236                          network_stat_ptr=network_stats+interfaces;
237 +
238 +                        /* Finish reading rx */
239                          network_stat_ptr->rx=knp->VALTYPE;
240  
241 <                        if((knp=kstat_data_lookup(ksp, WLOOKUP))==NULL){
242 <                                /* Not a network interface, so skip to the next entry */
241 >                        /* Read tx */
242 >                        if((knp=kstat_data_lookup(ksp, LTX))==NULL){
243                                  continue;
244                          }
245                          network_stat_ptr->tx=knp->VALTYPE;
246 <                        if(network_stat_ptr->interface_name!=NULL){
247 <                                free(network_stat_ptr->interface_name);
246 >
247 >                        /* Read ipackets */
248 >                        if((knp=kstat_data_lookup(ksp, LIPACKETS))==NULL){
249 >                                continue;
250                          }
251 <                        network_stat_ptr->interface_name=strdup(ksp->ks_name);
251 >                        network_stat_ptr->ipackets=knp->VALTYPE;
252  
253 +                        /* Read opackets */
254 +                        if((knp=kstat_data_lookup(ksp, LOPACKETS))==NULL){
255 +                                continue;
256 +                        }
257 +                        network_stat_ptr->opackets=knp->VALTYPE;
258 +
259 +                        /* Read ierrors */
260 +                        if((knp=kstat_data_lookup(ksp, "ierrors"))==NULL){
261 +                                continue;
262 +                        }
263 +                        network_stat_ptr->ierrors=knp->value.ui32;
264 +
265 +                        /* Read oerrors */
266 +                        if((knp=kstat_data_lookup(ksp, "oerrors"))==NULL){
267 +                                continue;
268 +                        }
269 +                        network_stat_ptr->oerrors=knp->value.ui32;
270 +
271 +                        /* Read collisions */
272 +                        if((knp=kstat_data_lookup(ksp, "collisions"))==NULL){
273 +                                continue;
274 +                        }
275 +                        network_stat_ptr->collisions=knp->value.ui32;
276 +
277 +                        /* Read interface name */
278 +                        if (sg_update_string(&network_stat_ptr->interface_name,
279 +                                             ksp->ks_name) < 0) {
280 +                                kstat_close(kc);
281 +                                return NULL;
282 +                        }
283 +
284 +                        /* Store systime */
285                          network_stat_ptr->systime=time(NULL);
286 +
287                          interfaces++;
288                  }
289          }
# Line 204 | Line 293 | network_stat_t *get_network_stats(int *entries){
293   #ifdef LINUX
294          f=fopen("/proc/net/dev", "r");
295          if(f==NULL){
296 +                sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/net/dev");
297                  return NULL;
298          }
299          /* read the 2 lines.. Its the title, so we dont care :) */
# Line 211 | Line 301 | network_stat_t *get_network_stats(int *entries){
301          fgets(line, sizeof(line), f);
302  
303  
304 <        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){
304 >        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){
305 >                sg_set_error(SG_ERROR_PARSE, NULL);
306                  return NULL;
307          }
308  
309          interfaces=0;
310  
311          while((fgets(line, sizeof(line), f)) != NULL){
312 <                if((regexec(&regex, line, 4, line_match, 0))!=0){
312 >                if((regexec(&regex, line, 9, line_match, 0))!=0){
313                          continue;
314                  }
315 <                network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats);
316 <                if(network_stats==NULL){
317 <                        return NULL;
315 >
316 >                if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) {
317 >                        return NULL;
318                  }
319 <                network_stat_ptr=network_stats+interfaces;
319 >                network_stat_ptr=network_stats+interfaces;
320  
321                  if(network_stat_ptr->interface_name!=NULL){
322                          free(network_stat_ptr->interface_name);
323                  }
324  
325 <                network_stat_ptr->interface_name=get_string_match(line, &line_match[1]);
326 <                network_stat_ptr->rx=get_ll_match(line, &line_match[2]);
327 <                network_stat_ptr->tx=get_ll_match(line, &line_match[3]);
325 >                network_stat_ptr->interface_name=sg_get_string_match(line, &line_match[1]);
326 >                network_stat_ptr->rx=sg_get_ll_match(line, &line_match[2]);
327 >                network_stat_ptr->tx=sg_get_ll_match(line, &line_match[5]);
328 >                network_stat_ptr->ipackets=sg_get_ll_match(line, &line_match[3]);
329 >                network_stat_ptr->opackets=sg_get_ll_match(line, &line_match[6]);
330 >                network_stat_ptr->ierrors=sg_get_ll_match(line, &line_match[4]);
331 >                network_stat_ptr->oerrors=sg_get_ll_match(line, &line_match[7]);
332 >                network_stat_ptr->collisions=sg_get_ll_match(line, &line_match[8]);
333                  network_stat_ptr->systime=time(NULL);
334  
335                  interfaces++;
# Line 244 | Line 340 | network_stat_t *get_network_stats(int *entries){
340   #endif
341  
342   #ifdef CYGWIN
343 +        sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin");
344          return NULL;
345   #endif
346 + #ifdef HPUX
347 +        sg_set_error(SG_ERROR_UNSUPPORTED, "HP-UX");
348 +        return NULL;
349 + #endif
350  
351 + #ifdef WIN32
352 +        interfaces = 0;
353 +
354 +        if((if_table = win32_get_devices()) == NULL) {
355 +                sg_set_error(SG_ERROR_DEVICES, "network");
356 +                return NULL;
357 +        }
358 +
359 +        if(VECTOR_RESIZE(network_stats, if_table->dwNumEntries) < 0) {
360 +                free(if_table);
361 +                return NULL;
362 +        }
363 +
364 +        for (i=0; i<if_table->dwNumEntries; i++) {
365 +                network_stat_ptr=network_stats+i;
366 +                if_row = if_table->table[i];
367 +
368 +                if(sg_update_string(&network_stat_ptr->interface_name,
369 +                                        if_row.bDescr) < 0) {
370 +                        free(if_table);
371 +                        return NULL;
372 +                }
373 +                network_stat_ptr->tx = if_row.dwOutOctets;
374 +                network_stat_ptr->rx = if_row.dwInOctets;
375 +                network_stat_ptr->ipackets = if_row.dwInUcastPkts + if_row.dwInNUcastPkts;
376 +                network_stat_ptr->opackets = if_row.dwOutUcastPkts + if_row.dwOutNUcastPkts;
377 +                network_stat_ptr->ierrors = if_row.dwInErrors;
378 +                network_stat_ptr->oerrors = if_row.dwOutErrors;
379 +                network_stat_ptr->collisions = 0; /* can't do that */
380 +                network_stat_ptr->systime = time(NULL);
381 +
382 +                interfaces++;
383 +        }
384 +        free(if_table);
385 +
386 +        /* Please say there's a nicer way to do this...  If windows has two (or
387 +         * more) identical network cards, GetIfTable returns them with the same
388 +         * name, not like in Device Manager where the other has a #2 etc after
389 +         * it. So, add the #number here. Should we be doing this? Or should the
390 +         * end programs be dealing with duplicate names? Currently breaks
391 +         * watch.pl in rrdgraphing. But Unix does not have the issue of
392 +         * duplicate net device names.
393 +         */
394 +        for (i=0; i<interfaces; i++) {
395 +                no = 2;
396 +                for(j=i+1; j<interfaces; j++) {
397 +                        network_stat_ptr=network_stats+j;
398 +                        if(strcmp(network_stats[i].interface_name,
399 +                                        network_stat_ptr->interface_name) == 0) {
400 +                                if(snprintf(buf, sizeof(buf), " #%d", no) < 0) {
401 +                                        break;
402 +                                }
403 +                                if(sg_concat_string(&network_stat_ptr->interface_name, buf) != 0) {
404 +                                        return NULL;
405 +                                }
406 +
407 +                                no++;
408 +                        }
409 +                }
410 +        }
411 + #endif
412 +
413          *entries=interfaces;
414  
415          return network_stats;  
416   }
417  
418 < long long transfer_diff(long long new, long long old){
419 < #if defined(SOL7) || defined(LINUX) || defined(FREEBSD)
420 < #define MAXVAL 4294967296LL
418 > static long long transfer_diff(long long new, long long old){
419 > #if defined(SOL7) || defined(LINUX) || defined(FREEBSD) || defined(DFBSD) || defined(OPENBSD) || defined(WIN32)
420 >        /* 32-bit quantities, so we must explicitly deal with wraparound. */
421 > #define MAXVAL 0x100000000LL
422 >        if (new >= old) {
423 >                return new - old;
424 >        } else {
425 >                return MAXVAL + new - old;
426 >        }
427   #else
428 < #define MAXVAL 18446744073709551616LL
428 >        /* 64-bit quantities, so plain subtraction works. */
429 >        return new - old;
430   #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
431   }
432  
433 < network_stat_t *get_network_stats_diff(int *entries) {
434 <        static network_stat_t *diff = NULL;
435 <        static int diff_count = 0;
436 <        network_stat_t *src, *dest;
437 <        int i, j, new_count;
433 > sg_network_io_stats *sg_get_network_io_stats_diff(int *entries) {
434 >        VECTOR_DECLARE_STATIC(diff, sg_network_io_stats, 1,
435 >                              network_stat_init, network_stat_destroy);
436 >        sg_network_io_stats *src = NULL, *dest;
437 >        int i, j, diff_count, new_count;
438  
439          if (network_stats == NULL) {
440                  /* No previous stats, so we can't calculate a difference. */
441 <                return get_network_stats(entries);
441 >                return sg_get_network_io_stats(entries);
442          }
443  
444          /* Resize the results array to match the previous stats. */
445 <        diff = network_stat_malloc(interfaces, &diff_count, diff);
446 <        if (diff == NULL) {
445 >        diff_count = VECTOR_SIZE(network_stats);
446 >        if (VECTOR_RESIZE(diff, diff_count) < 0) {
447                  return NULL;
448          }
449  
# Line 291 | Line 452 | network_stat_t *get_network_stats_diff(int *entries) {
452                  src = &network_stats[i];
453                  dest = &diff[i];
454  
455 <                if (dest->interface_name != NULL) {
456 <                        free(dest->interface_name);
455 >                if (sg_update_string(&dest->interface_name,
456 >                                     src->interface_name) < 0) {
457 >                        return NULL;
458                  }
297                dest->interface_name = strdup(src->interface_name);
459                  dest->rx = src->rx;
460                  dest->tx = src->tx;
461 +                dest->ipackets = src->ipackets;
462 +                dest->opackets = src->opackets;
463 +                dest->ierrors = src->ierrors;
464 +                dest->oerrors = src->oerrors;
465 +                dest->collisions = src->collisions;
466                  dest->systime = src->systime;
467          }
468  
469          /* Get a new set of stats. */
470 <        if (get_network_stats(&new_count) == NULL) {
470 >        if (sg_get_network_io_stats(&new_count) == NULL) {
471                  return NULL;
472          }
473  
# Line 327 | Line 493 | network_stat_t *get_network_stats_diff(int *entries) {
493                     difference. */
494                  dest->rx = transfer_diff(src->rx, dest->rx);
495                  dest->tx = transfer_diff(src->tx, dest->tx);
496 +                dest->ipackets = transfer_diff(src->ipackets, dest->ipackets);
497 +                dest->opackets = transfer_diff(src->opackets, dest->opackets);
498 +                dest->ierrors = transfer_diff(src->ierrors, dest->ierrors);
499 +                dest->oerrors = transfer_diff(src->oerrors, dest->oerrors);
500 +                dest->collisions = transfer_diff(src->collisions, dest->collisions);
501                  dest->systime = src->systime - dest->systime;
502          }
503  
504          *entries = diff_count;
505          return diff;
506   }
336 /* NETWORK INTERFACE STATS */
507  
508 < void network_iface_stat_init(int start, int end, network_iface_stat_t *net_stats){
508 > int sg_network_io_compare_name(const void *va, const void *vb) {
509 >        const sg_network_io_stats *a = (const sg_network_io_stats *)va;
510 >        const sg_network_io_stats *b = (const sg_network_io_stats *)vb;
511  
512 <        for(net_stats+=start; start<end; start++){
341 <                net_stats->interface_name=NULL;
342 <                net_stats->speed=0;
343 <                net_stats->dup=UNKNOWN_DUPLEX;
344 <                net_stats++;
345 <        }
512 >        return strcmp(a->interface_name, b->interface_name);
513   }
514  
515 < network_iface_stat_t *network_iface_stat_malloc(int needed_entries, int *cur_entries, network_iface_stat_t *net_stats){
515 > /* NETWORK INTERFACE STATS */
516  
517 <        if(net_stats==NULL){
517 > static void network_iface_stat_init(sg_network_iface_stats *s) {
518 >        s->interface_name = NULL;
519 >        s->speed = 0;
520 >        s->duplex = SG_IFACE_DUPLEX_UNKNOWN;
521 > }
522  
523 <                if((net_stats=malloc(needed_entries * sizeof(network_iface_stat_t)))==NULL){
524 <                        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;
523 > static void network_iface_stat_destroy(sg_network_iface_stats *s) {
524 >        free(s->interface_name);
525   }
526  
527 < network_iface_stat_t *get_network_iface_stats(int *entries){
528 <        static network_iface_stat_t *network_iface_stats;
529 <        network_iface_stat_t *network_iface_stat_ptr;
530 <        static int sizeof_network_iface_stats=0;        
531 <        static int ifaces;
527 > sg_network_iface_stats *sg_get_network_iface_stats(int *entries){
528 >        VECTOR_DECLARE_STATIC(network_iface_stats, sg_network_iface_stats, 5,
529 >                              network_iface_stat_init, network_iface_stat_destroy);
530 >        sg_network_iface_stats *network_iface_stat_ptr;
531 >        int ifaces = 0;
532  
533   #ifdef SOLARIS
534 <        kstat_ctl_t *kc;
535 <        kstat_t *ksp;
534 >        kstat_ctl_t *kc;
535 >        kstat_t *ksp;
536          kstat_named_t *knp;
537 +        int sock;
538   #endif
539   #ifdef ALLBSD
540 <        struct ifaddrs *net, *net_ptr;
540 >        struct ifaddrs *net, *net_ptr;
541          struct ifmediareq ifmed;
542          struct ifreq ifr;
543 <        int s;
543 >        int sock;
544          int x;
545   #endif
546   #ifdef LINUX
547 <        FILE *f;
548 <        /* Horrible big enough, but it should be easily big enough */
549 <        char line[8096];
396 <        void *eth_tool_cmd_buf;
397 <        int buf_size;
547 >        FILE *f;
548 >        /* Horrible big enough, but it should be easily big enough */
549 >        char line[8096];
550          int sock;
551   #endif
552 <        ifaces = 0;
552 > #ifdef WIN32
553 >        PMIB_IFTABLE if_table;
554 >        MIB_IFROW if_row;
555 >        int i,j,no;
556 >        char buf[5];
557 > #endif
558 >
559   #ifdef ALLBSD
560 <        if(getifaddrs(&net) != 0){
561 <                return NULL;
562 <        }
560 >        if(getifaddrs(&net) != 0){
561 >                sg_set_error_with_errno(SG_ERROR_GETIFADDRS, NULL);
562 >                return NULL;
563 >        }
564  
565 <        if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == NULL) return NULL;
565 >        if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return NULL;
566  
567          for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){
568 <                if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
410 <                network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
411 <                if(network_iface_stats==NULL){
412 <                        return NULL;
413 <                }
414 <                network_iface_stat_ptr = network_iface_stats + ifaces;
568 >                if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
569  
570 +                if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) {
571 +                        return NULL;
572 +                }
573 +                network_iface_stat_ptr = network_iface_stats + ifaces;
574 +
575 +                memset(&ifr, 0, sizeof(ifr));
576 +                strncpy(ifr.ifr_name, net_ptr->ifa_name, sizeof(ifr.ifr_name));
577 +
578 +                if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0){
579 +                        continue;
580 +                }      
581 +                if((ifr.ifr_flags & IFF_UP) != 0){
582 +                        network_iface_stat_ptr->up = 1;
583 +                }else{
584 +                        network_iface_stat_ptr->up = 0;
585 +                }
586 +
587 +                if (sg_update_string(&network_iface_stat_ptr->interface_name,
588 +                                     net_ptr->ifa_name) < 0) {
589 +                        return NULL;
590 +                }
591 +
592 +                network_iface_stat_ptr->speed = 0;
593 +                network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
594 +                ifaces++;
595 +
596                  memset(&ifmed, 0, sizeof(struct ifmediareq));
597 <                strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name));
598 <                if(ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){
597 >                sg_strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name));
598 >                if(ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){
599 >                        /* Not all interfaces support the media ioctls. */
600                          continue;
601                  }
602  
# Line 425 | Line 606 | network_iface_stat_t *get_network_iface_stats(int *ent
606                          continue;
607                  }
608  
428                if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
429                network_iface_stat_ptr->interface_name = strdup(net_ptr->ifa_name);
430                if(network_iface_stat_ptr->interface_name == NULL) return NULL;
431
609                  /* Only intrested in the first 4 bits)  - Assuming only ETHER devices */
610                  x = ifmed.ifm_active & 0x0f;    
611                  switch(x){
# Line 452 | Line 629 | network_iface_stat_t *get_network_iface_stats(int *ent
629                          case(IFM_1000_SX):
630                          case(IFM_1000_LX):
631                          case(IFM_1000_CX):
632 < #if defined(FREEBSD) && !defined(FREEBSD5)
633 <                        case(IFM_1000_TX):
457 <                        case(IFM_1000_FX):
458 < #else
459 <                        case(IFM_1000_T):
632 > #if defined(IFM_1000_TX) && !defined(OPENBSD)
633 >                        case(IFM_1000_TX): /* FreeBSD 4 and others (but NOT OpenBSD)? */
634   #endif
635 + #ifdef IFM_1000_FX
636 +                        case(IFM_1000_FX): /* FreeBSD 4 */
637 + #endif
638 + #ifdef IFM_1000_T
639 +                        case(IFM_1000_T): /* FreeBSD 5 */
640 + #endif
641                                  network_iface_stat_ptr->speed = 1000;
642                                  break;
643                          /* We don't know what it is */
# Line 467 | Line 647 | network_iface_stat_t *get_network_iface_stats(int *ent
647                  }
648  
649                  if( (ifmed.ifm_active | IFM_FDX) == ifmed.ifm_active ){
650 <                        network_iface_stat_ptr->dup = FULL_DUPLEX;
650 >                        network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_FULL;
651                  }else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){
652 <                        network_iface_stat_ptr->dup = HALF_DUPLEX;
652 >                        network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_HALF;
653                  }else{
654 <                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
654 >                        network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
655                  }
656  
477                if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0){
478                        continue;
479                }      
480                if((ifr.ifr_flags & IFF_UP) != 0){
481                        network_iface_stat_ptr->up = 1;
482                }else{
483                        network_iface_stat_ptr->up = 0;
484                }
485
486                ifaces++;
657          }      
658          freeifaddrs(net);
659 <        close(s);
659 >        close(sock);
660   #endif
661  
662   #ifdef SOLARIS
663 <        if ((kc = kstat_open()) == NULL) {
664 <                return NULL;
665 <        }
663 >        if ((kc = kstat_open()) == NULL) {
664 >                sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
665 >                return NULL;
666 >        }
667  
668 <        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
669 <                if (!strcmp(ksp->ks_class, "net")) {
670 <                        kstat_read(kc, ksp, NULL);
671 <                        if((knp=kstat_data_lookup(ksp, "ifspeed"))==NULL){
672 <                                /* Not a network interface, so skip to the next entry */
668 >        if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) {
669 >                sg_set_error_with_errno(SG_ERROR_SOCKET, NULL);
670 >                kstat_close(kc);
671 >                return NULL;
672 >        }
673 >
674 >        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
675 >                if (strcmp(ksp->ks_class, "net") == 0) {
676 >                        struct ifreq ifr;
677 >
678 >                        kstat_read(kc, ksp, NULL);
679 >
680 >                        strncpy(ifr.ifr_name, ksp->ks_name, sizeof ifr.ifr_name);
681 >                        if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
682 >                                /* Not a network interface. */
683                                  continue;
684                          }
685 <                        network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
686 <                        if(network_iface_stats==NULL){
685 >
686 >                        if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) {
687 >                                kstat_close(kc);
688                                  return NULL;
689                          }
690                          network_iface_stat_ptr = network_iface_stats + ifaces;
691 <                        network_iface_stat_ptr->speed = knp->value.ui64 / (1000*1000);
691 >                        ifaces++;
692  
693 <                        if((knp=kstat_data_lookup(ksp, "link_up"))==NULL){
694 <                                /* Not a network interface, so skip to the next entry */
695 <                                continue;
696 <                        }
697 <                        /* Solaris has 1 for up, and 0 for not. As we do too */
516 <                        network_iface_stat_ptr->up = value.ui32;
693 >                        if (sg_update_string(&network_iface_stat_ptr->interface_name,
694 >                                             ksp->ks_name) < 0) {
695 >                                kstat_close(kc);
696 >                                return NULL;
697 >                        }
698  
699 <                        if((knp=kstat_data_lookup(ksp, "link_duplex"))==NULL){
700 <                                /* Not a network interface, so skip to the next entry */
701 <                                continue;
699 >                        if ((ifr.ifr_flags & IFF_UP) != 0) {
700 >                                network_iface_stat_ptr->up = 1;
701 >                        } else {
702 >                                network_iface_stat_ptr->up = 0;
703                          }
704  
705 <                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
706 <                        if(knp->value.ui32 == 2){
707 <                                network_iface_stat_ptr->dup = FULL_DUPLEX;
705 >                        if ((knp = kstat_data_lookup(ksp, "ifspeed")) != NULL) {
706 >                                network_iface_stat_ptr->speed = knp->value.ui64 / (1000 * 1000);
707 >                        } else {
708 >                                network_iface_stat_ptr->speed = 0;
709                          }
527                        if(knp->value.ui32 == 1){
528                                network_iface_stat_ptr->dup = HALF_DUPLEX;
529                        }
710  
711 <                        if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
712 <                        network_iface_stat_ptr->interface_name = strdup(ksp->ks_name);
713 <                        if(network_iface_stat_ptr->interface_name == NULL) return NULL;
714 <                        ifaces++;
711 >                        network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
712 >                        if ((knp = kstat_data_lookup(ksp, "link_duplex")) != NULL) {
713 >                                switch (knp->value.ui32) {
714 >                                case 1:
715 >                                        network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_HALF;
716 >                                        break;
717 >                                case 2:
718 >                                        network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_FULL;
719 >                                        break;
720 >                                }
721 >                        }
722                  }
723          }
724 +
725 +        close(sock);
726          kstat_close(kc);
727 <        
539 < #endif  
727 > #endif
728   #ifdef LINUX
541
729          f = fopen("/proc/net/dev", "r");
730 <        if(f == NULL){
731 <                return NULL;
732 <        }
730 >        if(f == NULL){
731 >                sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/net/dev");
732 >                return NULL;
733 >        }
734  
735          /* Setup stuff so we can do the ioctl to get the info */
736          if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
737 +                sg_set_error_with_errno(SG_ERROR_SOCKET, NULL);
738                  return NULL;
739          }
740  
552        buf_size = sizeof(struct ethtool_cmd);
553        eth_tool_cmd_buf = malloc(buf_size);
554        if(eth_tool_cmd_buf == NULL) return NULL;
555
741          /* Ignore first 2 lines.. Just headings */
742 <        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
743 <        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
742 >        if((fgets(line, sizeof(line), f)) == NULL) {
743 >                sg_set_error(SG_ERROR_PARSE, NULL);
744 >                return NULL;
745 >        }
746 >        if((fgets(line, sizeof(line), f)) == NULL) {
747 >                sg_set_error(SG_ERROR_PARSE, NULL);
748 >                return NULL;
749 >        }
750  
751 <        while((fgets(line, sizeof(line), f)) != NULL){
752 <                char *name, *ptr;
753 <                struct ifreq ifr;
754 <                struct ethtool_cmd *ethcmd;
755 <                int err;
751 >        while((fgets(line, sizeof(line), f)) != NULL){
752 >                char *name, *ptr;
753 >                struct ifreq ifr;
754 >                struct ethtool_cmd ethcmd;
755 >                int err;
756  
757                  /* Get the interface name */
758 <                ptr = strchr(line, ':');
759 <                if (ptr == NULL) continue;
760 <                *ptr='\0';
761 <                name = line;
762 <                while(isspace(*(name))){
763 <                        name++;
764 <                }
758 >                ptr = strchr(line, ':');
759 >                if (ptr == NULL) continue;
760 >                *ptr='\0';
761 >                name = line;
762 >                while(isspace(*(name))){
763 >                        name++;
764 >                }
765  
766 <                memset(&ifr, 0, sizeof(ifr));
767 <                memset(eth_tool_cmd_buf, 0, buf_size);
577 <                ifr.ifr_data = (caddr_t) eth_tool_cmd_buf;
578 <                strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
766 >                memset(&ifr, 0, sizeof ifr);
767 >                strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
768  
769 <                ethcmd = (struct ethtool_cmd *) ifr.ifr_data;
770 <                ethcmd->cmd = ETHTOOL_GSET;
769 >                if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
770 >                        continue;
771 >                }
772  
583                err = ioctl(sock, SIOCETHTOOL, &ifr);
584                if(err < 0){
585                        /* This could fail if the interface doesn't support the command. Carry
586                         * on to the next :)
587                         */
588                        continue;
589                }
590
773                  /* We have a good interface to add */
774 <                network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
593 <                if(network_iface_stats==NULL){
774 >                if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) {
775                          return NULL;
776                  }
777                  network_iface_stat_ptr = network_iface_stats + ifaces;
778 <                network_iface_stat_ptr->interface_name = strdup(name);
779 <                network_iface_stat_ptr->speed = ethcmd->speed;
780 <                if((ifr.ifr_flags & IFF_UP) != 0){
778 >                
779 >                if (sg_update_string(&network_iface_stat_ptr->interface_name,
780 >                                     name) < 0) {
781 >                        return NULL;
782 >                }
783 >                if ((ifr.ifr_flags & IFF_UP) != 0) {
784                          network_iface_stat_ptr->up = 1;
785 <                }else{
785 >                } else {
786                          network_iface_stat_ptr->up = 0;
787                  }
788  
789 <                network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
790 <                if(ethcmd->duplex == 0x00){
791 <                        network_iface_stat_ptr->dup = FULL_DUPLEX;
789 >                memset(&ethcmd, 0, sizeof ethcmd);
790 >                ethcmd.cmd = ETHTOOL_GSET;
791 >                ifr.ifr_data = (caddr_t) &ethcmd;
792 >
793 >                err = ioctl(sock, SIOCETHTOOL, &ifr);
794 >                if (err == 0) {
795 >                        network_iface_stat_ptr->speed = ethcmd.speed;
796 >
797 >                        switch (ethcmd.duplex) {
798 >                        case DUPLEX_FULL:
799 >                                network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_FULL;
800 >                                break;
801 >                        case DUPLEX_HALF:
802 >                                network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_HALF;
803 >                                break;
804 >                        default:
805 >                                network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
806 >                        }
807 >                } else {
808 >                        /* Not all interfaces support the ethtool ioctl. */
809 >                        network_iface_stat_ptr->speed = 0;
810 >                        network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
811                  }
812 <                if(ethcmd->duplex == 0x01){
610 <                        network_iface_stat_ptr->dup = HALF_DUPLEX;
611 <                }
812 >
813                  ifaces++;
814          }
815          close(sock);
816 <        free(eth_tool_cmd_buf);
816 >        fclose(f);
817   #endif
818 + #ifdef CYGWIN
819 +        sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin");
820 +        return NULL;
821 + #endif
822 + #ifdef HPUX
823 +        sg_set_error(SG_ERROR_UNSUPPORTED, "HP-UX");
824 +        return NULL;
825 + #endif
826 + #ifdef WIN32
827 +        ifaces = 0;
828 +
829 +        if((if_table = win32_get_devices()) == NULL) {
830 +                sg_set_error(SG_ERROR_DEVICES, "network interfaces");
831 +                return NULL;
832 +        }
833 +
834 +        if(VECTOR_RESIZE(network_iface_stats, if_table->dwNumEntries) < 0) {
835 +                free(if_table);
836 +                return NULL;
837 +        }
838 +
839 +        for(i=0; i<if_table->dwNumEntries; i++) {
840 +                network_iface_stat_ptr=network_iface_stats+i;
841 +                if_row = if_table->table[i];
842 +
843 +                if(sg_update_string(&network_iface_stat_ptr->interface_name,
844 +                                        if_row.bDescr) < 0) {
845 +                        free(if_table);
846 +                        return NULL;
847 +                }
848 +                network_iface_stat_ptr->speed = if_row.dwSpeed /1000000;
849 +
850 +                if((if_row.dwOperStatus == MIB_IF_OPER_STATUS_CONNECTED ||
851 +                                if_row.dwOperStatus ==
852 +                                        MIB_IF_OPER_STATUS_OPERATIONAL) &&
853 +                                if_row.dwAdminStatus == 1) {
854 +                        network_iface_stat_ptr->up = 1;
855 +                } else {
856 +                        network_iface_stat_ptr->up = 0;
857 +                }
858 +
859 +                ifaces++;
860 +        }
861 +        free(if_table);
862 +
863 +        /* again with the renumbering */
864 +        for (i=0; i<ifaces; i++) {
865 +                no = 2;
866 +                for(j=i+1; j<ifaces; j++) {
867 +                        network_iface_stat_ptr=network_iface_stats+j;
868 +                        if(strcmp(network_iface_stats[i].interface_name,
869 +                                        network_iface_stat_ptr->interface_name) == 0) {
870 +                                if(snprintf(buf, sizeof(buf), " #%d", no) < 0) {
871 +                                        break;
872 +                                }
873 +                                if(sg_concat_string(&network_iface_stat_ptr->interface_name, buf) != 0) {
874 +                                        return NULL;
875 +                                }
876 +
877 +                                no++;
878 +                        }
879 +                }
880 +        }
881 + #endif
882 +
883 + #ifdef SG_ENABLE_DEPRECATED
884 +        network_iface_stat_ptr->dup = network_iface_stat_ptr->duplex;
885 + #endif
886 +
887          *entries = ifaces;
888          return network_iface_stats;
889 + }
890 +
891 + int sg_network_iface_compare_name(const void *va, const void *vb) {
892 +        const sg_network_iface_stats *a = (const sg_network_iface_stats *)va;
893 +        const sg_network_iface_stats *b = (const sg_network_iface_stats *)vb;
894 +
895 +        return strcmp(a->interface_name, b->interface_name);
896   }
897  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines