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.33 by pajs, Fri Feb 13 15:29:16 2004 UTC vs.
Revision 1.75 by ats, Sat Jul 30 13:23:46 2005 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  
74 < static network_stat_t *network_stats=NULL;
75 < static int interfaces=0;
76 <
77 < void network_stat_init(int start, int end, network_stat_t *net_stats){
78 <
79 <        for(net_stats+=start; start<end; start++){
80 <                net_stats->interface_name=NULL;
81 <                net_stats->tx=0;
82 <                net_stats->rx=0;
73 <                net_stats++;
74 <        }
74 > static void network_stat_init(sg_network_io_stats *s) {
75 >        s->interface_name = NULL;
76 >        s->tx = 0;
77 >        s->rx = 0;
78 >        s->ipackets = 0;
79 >        s->opackets = 0;
80 >        s->ierrors = 0;
81 >        s->oerrors = 0;
82 >        s->collisions = 0;
83   }
84  
85 < network_stat_t *network_stat_malloc(int needed_entries, int *cur_entries, network_stat_t *net_stats){
86 <
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;
85 > static void network_stat_destroy(sg_network_io_stats *s) {
86 >        free(s->interface_name);
87   }
88  
89 + VECTOR_DECLARE_STATIC(network_stats, sg_network_io_stats, 5,
90 +                      network_stat_init, network_stat_destroy);
91  
92 < network_stat_t *get_network_stats(int *entries){
92 > sg_network_io_stats *sg_get_network_io_stats(int *entries){
93 >        int interfaces;
94 >        sg_network_io_stats *network_stat_ptr;
95  
106        static int sizeof_network_stats=0;      
107        network_stat_t *network_stat_ptr;
108
96   #ifdef SOLARIS
97 <        kstat_ctl_t *kc;
98 <        kstat_t *ksp;
97 >        kstat_ctl_t *kc;
98 >        kstat_t *ksp;
99          kstat_named_t *knp;
100   #endif
101  
# Line 117 | Line 104 | network_stat_t *get_network_stats(int *entries){
104          /* Horrible big enough, but it should be easily big enough */
105          char line[8096];
106          regex_t regex;
107 <        regmatch_t line_match[4];
107 >        regmatch_t line_match[9];
108   #endif
109   #ifdef ALLBSD
110          struct ifaddrs *net, *net_ptr;
# Line 126 | Line 113 | network_stat_t *get_network_stats(int *entries){
113  
114   #ifdef ALLBSD
115          if(getifaddrs(&net) != 0){
116 +                sg_set_error_with_errno(SG_ERROR_GETIFADDRS, NULL);
117                  return NULL;
118          }
119  
# Line 133 | Line 121 | network_stat_t *get_network_stats(int *entries){
121          
122          for(net_ptr=net;net_ptr!=NULL;net_ptr=net_ptr->ifa_next){
123                  if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
124 <                network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats);
125 <                if(network_stats==NULL){
124 >
125 >                if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) {
126                          return NULL;
127                  }
128                  network_stat_ptr=network_stats+interfaces;
129                  
130 <                if(network_stat_ptr->interface_name!=NULL) free(network_stat_ptr->interface_name);
131 <                network_stat_ptr->interface_name=strdup(net_ptr->ifa_name);
132 <                if(network_stat_ptr->interface_name==NULL) return NULL;
130 >                if (sg_update_string(&network_stat_ptr->interface_name,
131 >                                     net_ptr->ifa_name) < 0) {
132 >                        return NULL;
133 >                }
134                  net_data=(struct if_data *)net_ptr->ifa_data;
135                  network_stat_ptr->rx=net_data->ifi_ibytes;
136 <                network_stat_ptr->tx=net_data->ifi_obytes;                      
136 >                network_stat_ptr->tx=net_data->ifi_obytes;
137 >                network_stat_ptr->ipackets=net_data->ifi_ipackets;
138 >                network_stat_ptr->opackets=net_data->ifi_opackets;
139 >                network_stat_ptr->ierrors=net_data->ifi_ierrors;
140 >                network_stat_ptr->oerrors=net_data->ifi_oerrors;
141 >                network_stat_ptr->collisions=net_data->ifi_collisions;
142                  network_stat_ptr->systime=time(NULL);
143                  interfaces++;
144          }
# Line 152 | Line 146 | network_stat_t *get_network_stats(int *entries){
146   #endif
147  
148   #ifdef SOLARIS
149 <        if ((kc = kstat_open()) == NULL) {
150 <                return NULL;
151 <        }
149 >        if ((kc = kstat_open()) == NULL) {
150 >                sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
151 >                return NULL;
152 >        }
153  
154          interfaces=0;
155  
156 <        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
157 <                if (!strcmp(ksp->ks_class, "net")) {
158 <                        kstat_read(kc, ksp, NULL);
156 >        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
157 >                if (!strcmp(ksp->ks_class, "net")) {
158 >                        kstat_read(kc, ksp, NULL);
159  
160   #ifdef SOL7
161 < #define RLOOKUP "rbytes"
162 < #define WLOOKUP "obytes"
161 > #define LRX "rbytes"
162 > #define LTX "obytes"
163 > #define LIPACKETS "ipackets"
164 > #define LOPACKETS "opackets"
165   #define VALTYPE value.ui32
166   #else
167 < #define RLOOKUP "rbytes64"
168 < #define WLOOKUP "obytes64"
167 > #define LRX "rbytes64"
168 > #define LTX "obytes64"
169 > #define LIPACKETS "ipackets64"
170 > #define LOPACKETS "opackets64"
171   #define VALTYPE value.ui64
172   #endif
173  
174 <                        if((knp=kstat_data_lookup(ksp, RLOOKUP))==NULL){
175 <                                /* Not a network interface, so skip to the next entry */
174 >                        /* Read rx */
175 >                        if((knp=kstat_data_lookup(ksp, LRX))==NULL){
176 >                                /* This is a network interface, but it doesn't
177 >                                 * have the rbytes/obytes values; for instance,
178 >                                 * the loopback devices have this behaviour
179 >                                 * (although they do track packets in/out). */
180 >                                /* FIXME: Show packet counts when byte counts
181 >                                 * not available. */
182                                  continue;
183                          }
184  
185 <                        network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats);
186 <                        if(network_stats==NULL){
185 >                        /* Create new network_stats */
186 >                        if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) {
187                                  return NULL;
188                          }
189                          network_stat_ptr=network_stats+interfaces;
190 +
191 +                        /* Finish reading rx */
192                          network_stat_ptr->rx=knp->VALTYPE;
193  
194 <                        if((knp=kstat_data_lookup(ksp, WLOOKUP))==NULL){
195 <                                /* Not a network interface, so skip to the next entry */
194 >                        /* Read tx */
195 >                        if((knp=kstat_data_lookup(ksp, LTX))==NULL){
196                                  continue;
197                          }
198                          network_stat_ptr->tx=knp->VALTYPE;
199 <                        if(network_stat_ptr->interface_name!=NULL){
200 <                                free(network_stat_ptr->interface_name);
199 >
200 >                        /* Read ipackets */
201 >                        if((knp=kstat_data_lookup(ksp, LIPACKETS))==NULL){
202 >                                continue;
203                          }
204 <                        network_stat_ptr->interface_name=strdup(ksp->ks_name);
204 >                        network_stat_ptr->ipackets=knp->VALTYPE;
205  
206 +                        /* Read opackets */
207 +                        if((knp=kstat_data_lookup(ksp, LOPACKETS))==NULL){
208 +                                continue;
209 +                        }
210 +                        network_stat_ptr->opackets=knp->VALTYPE;
211 +
212 +                        /* Read ierrors */
213 +                        if((knp=kstat_data_lookup(ksp, "ierrors"))==NULL){
214 +                                continue;
215 +                        }
216 +                        network_stat_ptr->ierrors=knp->value.ui32;
217 +
218 +                        /* Read oerrors */
219 +                        if((knp=kstat_data_lookup(ksp, "oerrors"))==NULL){
220 +                                continue;
221 +                        }
222 +                        network_stat_ptr->oerrors=knp->value.ui32;
223 +
224 +                        /* Read collisions */
225 +                        if((knp=kstat_data_lookup(ksp, "collisions"))==NULL){
226 +                                continue;
227 +                        }
228 +                        network_stat_ptr->collisions=knp->value.ui32;
229 +
230 +                        /* Read interface name */
231 +                        if (sg_update_string(&network_stat_ptr->interface_name,
232 +                                             ksp->ks_name) < 0) {
233 +                                return NULL;
234 +                        }
235 +
236 +                        /* Store systime */
237                          network_stat_ptr->systime=time(NULL);
238 +
239                          interfaces++;
240                  }
241          }
# Line 204 | Line 245 | network_stat_t *get_network_stats(int *entries){
245   #ifdef LINUX
246          f=fopen("/proc/net/dev", "r");
247          if(f==NULL){
248 +                sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/net/dev");
249                  return NULL;
250          }
251          /* read the 2 lines.. Its the title, so we dont care :) */
# Line 211 | Line 253 | network_stat_t *get_network_stats(int *entries){
253          fgets(line, sizeof(line), f);
254  
255  
256 <        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){
256 >        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){
257 >                sg_set_error(SG_ERROR_PARSE, NULL);
258                  return NULL;
259          }
260  
261          interfaces=0;
262  
263          while((fgets(line, sizeof(line), f)) != NULL){
264 <                if((regexec(&regex, line, 4, line_match, 0))!=0){
264 >                if((regexec(&regex, line, 9, line_match, 0))!=0){
265                          continue;
266                  }
267 <                network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats);
268 <                if(network_stats==NULL){
269 <                        return NULL;
267 >
268 >                if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) {
269 >                        return NULL;
270                  }
271 <                network_stat_ptr=network_stats+interfaces;
271 >                network_stat_ptr=network_stats+interfaces;
272  
273                  if(network_stat_ptr->interface_name!=NULL){
274                          free(network_stat_ptr->interface_name);
275                  }
276  
277 <                network_stat_ptr->interface_name=get_string_match(line, &line_match[1]);
278 <                network_stat_ptr->rx=get_ll_match(line, &line_match[2]);
279 <                network_stat_ptr->tx=get_ll_match(line, &line_match[3]);
277 >                network_stat_ptr->interface_name=sg_get_string_match(line, &line_match[1]);
278 >                network_stat_ptr->rx=sg_get_ll_match(line, &line_match[2]);
279 >                network_stat_ptr->tx=sg_get_ll_match(line, &line_match[5]);
280 >                network_stat_ptr->ipackets=sg_get_ll_match(line, &line_match[3]);
281 >                network_stat_ptr->opackets=sg_get_ll_match(line, &line_match[6]);
282 >                network_stat_ptr->ierrors=sg_get_ll_match(line, &line_match[4]);
283 >                network_stat_ptr->oerrors=sg_get_ll_match(line, &line_match[7]);
284 >                network_stat_ptr->collisions=sg_get_ll_match(line, &line_match[8]);
285                  network_stat_ptr->systime=time(NULL);
286  
287                  interfaces++;
# Line 244 | Line 292 | network_stat_t *get_network_stats(int *entries){
292   #endif
293  
294   #ifdef CYGWIN
295 +        sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin");
296          return NULL;
297   #endif
298 + #ifdef HPUX
299 +        sg_set_error(SG_ERROR_UNSUPPORTED, "HP-UX");
300 +        return NULL;
301 + #endif
302  
303          *entries=interfaces;
304  
305          return network_stats;  
306   }
307  
308 < long long transfer_diff(long long new, long long old){
309 < #if defined(SOL7) || defined(LINUX) || defined(FREEBSD)
310 < #define MAXVAL 4294967296LL
308 > static long long transfer_diff(long long new, long long old){
309 > #if defined(SOL7) || defined(LINUX) || defined(FREEBSD) || defined(DFBSD) || defined(OPENBSD)
310 >        /* 32-bit quantities, so we must explicitly deal with wraparound. */
311 > #define MAXVAL 0x100000000LL
312 >        if (new >= old) {
313 >                return new - old;
314 >        } else {
315 >                return MAXVAL + new - old;
316 >        }
317   #else
318 < #define MAXVAL 18446744073709551616LL
318 >        /* 64-bit quantities, so plain subtraction works. */
319 >        return new - old;
320   #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
321   }
322  
323 < network_stat_t *get_network_stats_diff(int *entries) {
324 <        static network_stat_t *diff = NULL;
325 <        static int diff_count = 0;
326 <        network_stat_t *src, *dest;
327 <        int i, j, new_count;
323 > sg_network_io_stats *sg_get_network_io_stats_diff(int *entries) {
324 >        VECTOR_DECLARE_STATIC(diff, sg_network_io_stats, 1,
325 >                              network_stat_init, network_stat_destroy);
326 >        sg_network_io_stats *src = NULL, *dest;
327 >        int i, j, diff_count, new_count;
328  
329          if (network_stats == NULL) {
330                  /* No previous stats, so we can't calculate a difference. */
331 <                return get_network_stats(entries);
331 >                return sg_get_network_io_stats(entries);
332          }
333  
334          /* Resize the results array to match the previous stats. */
335 <        diff = network_stat_malloc(interfaces, &diff_count, diff);
336 <        if (diff == NULL) {
335 >        diff_count = VECTOR_SIZE(network_stats);
336 >        if (VECTOR_RESIZE(diff, diff_count) < 0) {
337                  return NULL;
338          }
339  
# Line 291 | Line 342 | network_stat_t *get_network_stats_diff(int *entries) {
342                  src = &network_stats[i];
343                  dest = &diff[i];
344  
345 <                if (dest->interface_name != NULL) {
346 <                        free(dest->interface_name);
345 >                if (sg_update_string(&dest->interface_name,
346 >                                     src->interface_name) < 0) {
347 >                        return NULL;
348                  }
297                dest->interface_name = strdup(src->interface_name);
349                  dest->rx = src->rx;
350                  dest->tx = src->tx;
351 +                dest->ipackets = src->ipackets;
352 +                dest->opackets = src->opackets;
353 +                dest->ierrors = src->ierrors;
354 +                dest->oerrors = src->oerrors;
355 +                dest->collisions = src->collisions;
356                  dest->systime = src->systime;
357          }
358  
359          /* Get a new set of stats. */
360 <        if (get_network_stats(&new_count) == NULL) {
360 >        if (sg_get_network_io_stats(&new_count) == NULL) {
361                  return NULL;
362          }
363  
# Line 327 | Line 383 | network_stat_t *get_network_stats_diff(int *entries) {
383                     difference. */
384                  dest->rx = transfer_diff(src->rx, dest->rx);
385                  dest->tx = transfer_diff(src->tx, dest->tx);
386 +                dest->ipackets = transfer_diff(src->ipackets, dest->ipackets);
387 +                dest->opackets = transfer_diff(src->opackets, dest->opackets);
388 +                dest->ierrors = transfer_diff(src->ierrors, dest->ierrors);
389 +                dest->oerrors = transfer_diff(src->oerrors, dest->oerrors);
390 +                dest->collisions = transfer_diff(src->collisions, dest->collisions);
391                  dest->systime = src->systime - dest->systime;
392          }
393  
394          *entries = diff_count;
395          return diff;
396   }
336 /* NETWORK INTERFACE STATS */
397  
398 < void network_iface_stat_init(int start, int end, network_iface_stat_t *net_stats){
398 > int sg_network_io_compare_name(const void *va, const void *vb) {
399 >        const sg_network_io_stats *a = (const sg_network_io_stats *)va;
400 >        const sg_network_io_stats *b = (const sg_network_io_stats *)vb;
401  
402 <        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 <        }
402 >        return strcmp(a->interface_name, b->interface_name);
403   }
404  
405 < network_iface_stat_t *network_iface_stat_malloc(int needed_entries, int *cur_entries, network_iface_stat_t *net_stats){
405 > /* NETWORK INTERFACE STATS */
406  
407 <        if(net_stats==NULL){
407 > static void network_iface_stat_init(sg_network_iface_stats *s) {
408 >        s->interface_name = NULL;
409 >        s->speed = 0;
410 >        s->duplex = SG_IFACE_DUPLEX_UNKNOWN;
411 > }
412  
413 <                if((net_stats=malloc(needed_entries * sizeof(network_iface_stat_t)))==NULL){
414 <                        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;
413 > static void network_iface_stat_destroy(sg_network_iface_stats *s) {
414 >        free(s->interface_name);
415   }
416  
417 < network_iface_stat_t *get_network_iface_stats(int *entries){
418 <        static network_iface_stat_t *network_iface_stats;
419 <        network_iface_stat_t *network_iface_stat_ptr;
420 <        static int sizeof_network_iface_stats=0;        
421 <        static int ifaces;
417 > sg_network_iface_stats *sg_get_network_iface_stats(int *entries){
418 >        VECTOR_DECLARE_STATIC(network_iface_stats, sg_network_iface_stats, 5,
419 >                              network_iface_stat_init, network_iface_stat_destroy);
420 >        sg_network_iface_stats *network_iface_stat_ptr;
421 >        int ifaces = 0;
422  
423   #ifdef SOLARIS
424 <        kstat_ctl_t *kc;
425 <        kstat_t *ksp;
424 >        kstat_ctl_t *kc;
425 >        kstat_t *ksp;
426          kstat_named_t *knp;
427 +        int sock;
428   #endif
429   #ifdef ALLBSD
430 <        struct ifaddrs *net, *net_ptr;
430 >        struct ifaddrs *net, *net_ptr;
431          struct ifmediareq ifmed;
432 <        int s;
432 >        struct ifreq ifr;
433 >        int sock;
434          int x;
435   #endif
436   #ifdef LINUX
437 <        FILE *f;
438 <        /* Horrible big enough, but it should be easily big enough */
439 <        char line[8096];
395 <        void *eth_tool_cmd_buf;
396 <        int buf_size;
437 >        FILE *f;
438 >        /* Horrible big enough, but it should be easily big enough */
439 >        char line[8096];
440          int sock;
441   #endif
442 <        ifaces = 0;
442 >
443   #ifdef ALLBSD
444 <        if(getifaddrs(&net) != 0){
445 <                return NULL;
446 <        }
444 >        if(getifaddrs(&net) != 0){
445 >                sg_set_error_with_errno(SG_ERROR_GETIFADDRS, NULL);
446 >                return NULL;
447 >        }
448  
449 <        if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == NULL) return NULL;
449 >        if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return NULL;
450  
451          for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){
452 <                if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
409 <                network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
410 <                if(network_iface_stats==NULL){
411 <                        return NULL;
412 <                }
413 <                network_iface_stat_ptr = network_iface_stats + ifaces;
452 >                if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
453  
454 +                if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) {
455 +                        return NULL;
456 +                }
457 +                network_iface_stat_ptr = network_iface_stats + ifaces;
458 +
459 +                memset(&ifr, 0, sizeof(ifr));
460 +                strncpy(ifr.ifr_name, net_ptr->ifa_name, sizeof(ifr.ifr_name));
461 +
462 +                if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0){
463 +                        continue;
464 +                }      
465 +                if((ifr.ifr_flags & IFF_UP) != 0){
466 +                        network_iface_stat_ptr->up = 1;
467 +                }else{
468 +                        network_iface_stat_ptr->up = 0;
469 +                }
470 +
471 +                if (sg_update_string(&network_iface_stat_ptr->interface_name,
472 +                                     net_ptr->ifa_name) < 0) {
473 +                        return NULL;
474 +                }
475 +
476 +                network_iface_stat_ptr->speed = 0;
477 +                network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
478 +                ifaces++;
479 +
480                  memset(&ifmed, 0, sizeof(struct ifmediareq));
481 <                strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name));
482 <                if(ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){
481 >                sg_strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name));
482 >                if(ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){
483 >                        /* Not all interfaces support the media ioctls. */
484                          continue;
485                  }
486  
# Line 424 | Line 490 | network_iface_stat_t *get_network_iface_stats(int *ent
490                          continue;
491                  }
492  
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
493                  /* Only intrested in the first 4 bits)  - Assuming only ETHER devices */
494                  x = ifmed.ifm_active & 0x0f;    
495                  switch(x){
# Line 451 | Line 513 | network_iface_stat_t *get_network_iface_stats(int *ent
513                          case(IFM_1000_SX):
514                          case(IFM_1000_LX):
515                          case(IFM_1000_CX):
516 < #if defined(FREEBSD) && !defined(FREEBSD5)
517 <                        case(IFM_1000_TX):
456 <                        case(IFM_1000_FX):
457 < #else
458 <                        case(IFM_1000_T):
516 > #if defined(IFM_1000_TX) && !defined(OPENBSD)
517 >                        case(IFM_1000_TX): /* FreeBSD 4 and others (but NOT OpenBSD)? */
518   #endif
519 + #ifdef IFM_1000_FX
520 +                        case(IFM_1000_FX): /* FreeBSD 4 */
521 + #endif
522 + #ifdef IFM_1000_T
523 +                        case(IFM_1000_T): /* FreeBSD 5 */
524 + #endif
525                                  network_iface_stat_ptr->speed = 1000;
526                                  break;
527                          /* We don't know what it is */
# Line 466 | Line 531 | network_iface_stat_t *get_network_iface_stats(int *ent
531                  }
532  
533                  if( (ifmed.ifm_active | IFM_FDX) == ifmed.ifm_active ){
534 <                        network_iface_stat_ptr->dup = FULL_DUPLEX;
534 >                        network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_FULL;
535                  }else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){
536 <                        network_iface_stat_ptr->dup = HALF_DUPLEX;
536 >                        network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_HALF;
537                  }else{
538 <                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
538 >                        network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
539                  }
540 <                ifaces++;
540 >
541          }      
542          freeifaddrs(net);
543 <        close(s);
543 >        close(sock);
544   #endif
545  
546   #ifdef SOLARIS
547 <        if ((kc = kstat_open()) == NULL) {
548 <                return NULL;
549 <        }
547 >        if ((kc = kstat_open()) == NULL) {
548 >                sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
549 >                return NULL;
550 >        }
551  
552 <        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
553 <                if (!strcmp(ksp->ks_class, "net")) {
554 <                        kstat_read(kc, ksp, NULL);
555 <                        if((knp=kstat_data_lookup(ksp, "ifspeed"))==NULL){
556 <                                /* Not a network interface, so skip to the next entry */
552 >        if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) {
553 >                sg_set_error_with_errno(SG_ERROR_SOCKET, NULL);
554 >                return NULL;
555 >        }
556 >
557 >        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
558 >                if (!strcmp(ksp->ks_class, "net")) {
559 >                        struct ifreq ifr;
560 >
561 >                        kstat_read(kc, ksp, NULL);
562 >
563 >                        strncpy(ifr.ifr_name, ksp->ks_name, sizeof ifr.ifr_name);
564 >                        if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
565 >                                /* Not a network interface. */
566                                  continue;
567                          }
568 <                        network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
569 <                        if(network_iface_stats==NULL){
568 >
569 >                        if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) {
570                                  return NULL;
571                          }
572                          network_iface_stat_ptr = network_iface_stats + ifaces;
573 <                        network_iface_stat_ptr->speed = knp->value.ui64 / (1000*1000);
573 >                        ifaces++;
574  
575 <                        if((knp=kstat_data_lookup(ksp, "link_duplex"))==NULL){
576 <                                /* Not a network interface, so skip to the next entry */
577 <                                continue;
575 >                        if (sg_update_string(&network_iface_stat_ptr->interface_name,
576 >                                             ksp->ks_name) < 0) {
577 >                                return NULL;
578                          }
579  
580 <                        network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
581 <                        if(knp->value.ui64 == 2){
582 <                                network_iface_stat_ptr->dup = FULL_DUPLEX;
580 >                        if ((ifr.ifr_flags & IFF_UP) != 0) {
581 >                                network_iface_stat_ptr->up = 1;
582 >                        } else {
583 >                                network_iface_stat_ptr->up = 1;
584                          }
585 <                        if(knp->value.ui64 == 1){
586 <                                network_iface_stat_ptr->dup = HALF_DUPLEX;
585 >
586 >                        if ((knp = kstat_data_lookup(ksp, "ifspeed")) != NULL) {
587 >                                network_iface_stat_ptr->speed = knp->value.ui64 / (1000 * 1000);
588 >                        } else {
589 >                                network_iface_stat_ptr->speed = 0;
590                          }
591  
592 <                        if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name);
593 <                        network_iface_stat_ptr->interface_name = strdup(ksp->ks_name);
594 <                        if(network_iface_stat_ptr->interface_name == NULL) return NULL;
595 <                        ifaces++;
592 >                        network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
593 >                        if ((knp = kstat_data_lookup(ksp, "link_duplex")) != NULL) {
594 >                                switch (knp->value.ui32) {
595 >                                case 1:
596 >                                        network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_HALF;
597 >                                        break;
598 >                                case 2:
599 >                                        network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_FULL;
600 >                                        break;
601 >                                }
602 >                        }
603                  }
604          }
605 +
606 +        close(sock);
607          kstat_close(kc);
520        
608   #endif  
609   #ifdef LINUX
523
610          f = fopen("/proc/net/dev", "r");
611 <        if(f == NULL){
612 <                return NULL;
613 <        }
611 >        if(f == NULL){
612 >                sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/net/dev");
613 >                return NULL;
614 >        }
615  
616          /* Setup stuff so we can do the ioctl to get the info */
617          if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
618 +                sg_set_error_with_errno(SG_ERROR_SOCKET, NULL);
619                  return NULL;
620          }
621  
534        buf_size = sizeof(struct ethtool_cmd);
535        eth_tool_cmd_buf = malloc(buf_size);
536        if(eth_tool_cmd_buf == NULL) return NULL;
537
622          /* Ignore first 2 lines.. Just headings */
623 <        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
624 <        if((fgets(line, sizeof(line), f)) == NULL) return NULL;
623 >        if((fgets(line, sizeof(line), f)) == NULL) {
624 >                sg_set_error(SG_ERROR_PARSE, NULL);
625 >                return NULL;
626 >        }
627 >        if((fgets(line, sizeof(line), f)) == NULL) {
628 >                sg_set_error(SG_ERROR_PARSE, NULL);
629 >                return NULL;
630 >        }
631  
632 <        while((fgets(line, sizeof(line), f)) != NULL){
633 <                char *name, *ptr;
634 <                struct ifreq ifr;
635 <                struct ethtool_cmd *ethcmd;
636 <                int err;
632 >        while((fgets(line, sizeof(line), f)) != NULL){
633 >                char *name, *ptr;
634 >                struct ifreq ifr;
635 >                struct ethtool_cmd ethcmd;
636 >                int err;
637  
638                  /* Get the interface name */
639 <                ptr = strchr(line, ':');
640 <                if (ptr == NULL) continue;
641 <                *ptr='\0';
642 <                name = line;
643 <                while(isspace(*(name))){
644 <                        name++;
645 <                }
639 >                ptr = strchr(line, ':');
640 >                if (ptr == NULL) continue;
641 >                *ptr='\0';
642 >                name = line;
643 >                while(isspace(*(name))){
644 >                        name++;
645 >                }
646  
647 <                memset(&ifr, 0, sizeof(ifr));
648 <                memset(eth_tool_cmd_buf, 0, buf_size);
559 <                ifr.ifr_data = (caddr_t) eth_tool_cmd_buf;
560 <                strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
647 >                memset(&ifr, 0, sizeof ifr);
648 >                strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
649  
650 <                ethcmd = (struct ethtool_cmd *) ifr.ifr_data;
651 <                ethcmd->cmd = ETHTOOL_GSET;
650 >                if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
651 >                        continue;
652 >                }
653  
565                err = ioctl(sock, SIOCETHTOOL, &ifr);
566                if(err < 0){
567                        /* This could fail if the interface doesn't support the command. Carry
568                         * on to the next :)
569                         */
570                        continue;
571                }
572
654                  /* We have a good interface to add */
655 <                network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
575 <                if(network_iface_stats==NULL){
655 >                if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) {
656                          return NULL;
657                  }
658                  network_iface_stat_ptr = network_iface_stats + ifaces;
659 <                network_iface_stat_ptr->interface_name = strdup(name);
660 <                network_iface_stat_ptr->speed = ethcmd->speed;
661 <                network_iface_stat_ptr->dup = UNKNOWN_DUPLEX;
662 <                if(ethcmd->duplex == 0x00){
583 <                        network_iface_stat_ptr->dup = FULL_DUPLEX;
659 >                
660 >                if (sg_update_string(&network_iface_stat_ptr->interface_name,
661 >                                     name) < 0) {
662 >                        return NULL;
663                  }
664 <                if(ethcmd->duplex == 0x01){
665 <                        network_iface_stat_ptr->dup = HALF_DUPLEX;
664 >                if ((ifr.ifr_flags & IFF_UP) != 0) {
665 >                        network_iface_stat_ptr->up = 1;
666 >                } else {
667 >                        network_iface_stat_ptr->up = 0;
668                  }
669 +
670 +                memset(&ethcmd, 0, sizeof ethcmd);
671 +                ethcmd.cmd = ETHTOOL_GSET;
672 +                ifr.ifr_data = (caddr_t) &ethcmd;
673 +
674 +                err = ioctl(sock, SIOCETHTOOL, &ifr);
675 +                if (err == 0) {
676 +                        network_iface_stat_ptr->speed = ethcmd.speed;
677 +
678 +                        switch (ethcmd.duplex) {
679 +                        case DUPLEX_FULL:
680 +                                network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_FULL;
681 +                                break;
682 +                        case DUPLEX_HALF:
683 +                                network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_HALF;
684 +                                break;
685 +                        default:
686 +                                network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
687 +                        }
688 +                } else {
689 +                        /* Not all interfaces support the ethtool ioctl. */
690 +                        network_iface_stat_ptr->speed = 0;
691 +                        network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN;
692 +                }
693 +
694                  ifaces++;
695          }
696          close(sock);
697 <        free(eth_tool_cmd_buf);
697 >        fclose(f);
698   #endif
699 + #ifdef CYGWIN
700 +        sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin");
701 +        return NULL;
702 + #endif
703 + #ifdef HPUX
704 +        sg_set_error(SG_ERROR_UNSUPPORTED, "HP-UX");
705 +        return NULL;
706 + #endif
707 +
708 + #ifdef SG_ENABLE_DEPRECATED
709 +        network_iface_stat_ptr->dup = network_iface_stat_ptr->duplex;
710 + #endif
711 +
712          *entries = ifaces;
713          return network_iface_stats;
714 + }
715 +
716 + int sg_network_iface_compare_name(const void *va, const void *vb) {
717 +        const sg_network_iface_stats *a = (const sg_network_iface_stats *)va;
718 +        const sg_network_iface_stats *b = (const sg_network_iface_stats *)vb;
719 +
720 +        return strcmp(a->interface_name, b->interface_name);
721   }
722  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines