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 |
|
* |
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> |
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> |
45 |
– |
#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; |
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 |
< |
|
76 |
< |
for(net_stats+=start; start<end; start++){ |
77 |
< |
net_stats->interface_name=NULL; |
78 |
< |
net_stats->tx=0; |
79 |
< |
net_stats->rx=0; |
80 |
< |
net_stats->ipackets=0; |
81 |
< |
net_stats->opackets=0; |
82 |
< |
net_stats->ierrors=0; |
83 |
< |
net_stats->oerrors=0; |
84 |
< |
net_stats->collisions=0; |
85 |
< |
net_stats++; |
86 |
< |
} |
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); |
97 |
< |
*cur_entries=needed_entries; |
98 |
< |
|
99 |
< |
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); |
109 |
< |
*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 |
|
|
116 |
– |
network_stat_t *get_network_stats(int *entries){ |
117 |
– |
|
118 |
– |
static int sizeof_network_stats=0; |
119 |
– |
network_stat_t *network_stat_ptr; |
120 |
– |
|
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 |
|
|
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 |
|
|
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; |
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 LRX "rbytes" |
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 |
|
/* Create new network_stats */ |
232 |
< |
network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats); |
207 |
< |
if(network_stats==NULL){ |
232 |
> |
if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) { |
233 |
|
return NULL; |
234 |
|
} |
235 |
|
network_stat_ptr=network_stats+interfaces; |
274 |
|
network_stat_ptr->collisions=knp->value.ui32; |
275 |
|
|
276 |
|
/* Read interface name */ |
277 |
< |
if(network_stat_ptr->interface_name!=NULL){ |
278 |
< |
free(network_stat_ptr->interface_name); |
277 |
> |
if (sg_update_string(&network_stat_ptr->interface_name, |
278 |
> |
ksp->ks_name) < 0) { |
279 |
> |
return NULL; |
280 |
|
} |
255 |
– |
network_stat_ptr->interface_name=strdup(ksp->ks_name); |
281 |
|
|
282 |
|
/* Store systime */ |
283 |
|
network_stat_ptr->systime=time(NULL); |
291 |
|
#ifdef LINUX |
292 |
|
f=fopen("/proc/net/dev", "r"); |
293 |
|
if(f==NULL){ |
294 |
+ |
sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/net/dev"); |
295 |
|
return NULL; |
296 |
|
} |
297 |
|
/* read the 2 lines.. Its the title, so we dont care :) */ |
300 |
|
|
301 |
|
|
302 |
|
if((regcomp(®ex, "^ *([^:]+): *([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){ |
303 |
+ |
sg_set_error(SG_ERROR_PARSE, NULL); |
304 |
|
return NULL; |
305 |
|
} |
306 |
|
|
310 |
|
if((regexec(®ex, line, 9, line_match, 0))!=0){ |
311 |
|
continue; |
312 |
|
} |
313 |
< |
network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats); |
314 |
< |
if(network_stats==NULL){ |
315 |
< |
return NULL; |
313 |
> |
|
314 |
> |
if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) { |
315 |
> |
return NULL; |
316 |
|
} |
317 |
< |
network_stat_ptr=network_stats+interfaces; |
317 |
> |
network_stat_ptr=network_stats+interfaces; |
318 |
|
|
319 |
|
if(network_stat_ptr->interface_name!=NULL){ |
320 |
|
free(network_stat_ptr->interface_name); |
321 |
|
} |
322 |
|
|
323 |
< |
network_stat_ptr->interface_name=get_string_match(line, &line_match[1]); |
324 |
< |
network_stat_ptr->rx=get_ll_match(line, &line_match[2]); |
325 |
< |
network_stat_ptr->tx=get_ll_match(line, &line_match[5]); |
326 |
< |
network_stat_ptr->ipackets=get_ll_match(line, &line_match[3]); |
327 |
< |
network_stat_ptr->opackets=get_ll_match(line, &line_match[6]); |
328 |
< |
network_stat_ptr->ierrors=get_ll_match(line, &line_match[4]); |
329 |
< |
network_stat_ptr->oerrors=get_ll_match(line, &line_match[7]); |
330 |
< |
network_stat_ptr->collisions=get_ll_match(line, &line_match[8]); |
323 |
> |
network_stat_ptr->interface_name=sg_get_string_match(line, &line_match[1]); |
324 |
> |
network_stat_ptr->rx=sg_get_ll_match(line, &line_match[2]); |
325 |
> |
network_stat_ptr->tx=sg_get_ll_match(line, &line_match[5]); |
326 |
> |
network_stat_ptr->ipackets=sg_get_ll_match(line, &line_match[3]); |
327 |
> |
network_stat_ptr->opackets=sg_get_ll_match(line, &line_match[6]); |
328 |
> |
network_stat_ptr->ierrors=sg_get_ll_match(line, &line_match[4]); |
329 |
> |
network_stat_ptr->oerrors=sg_get_ll_match(line, &line_match[7]); |
330 |
> |
network_stat_ptr->collisions=sg_get_ll_match(line, &line_match[8]); |
331 |
|
network_stat_ptr->systime=time(NULL); |
332 |
|
|
333 |
|
interfaces++; |
338 |
|
#endif |
339 |
|
|
340 |
|
#ifdef CYGWIN |
341 |
+ |
sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin"); |
342 |
|
return NULL; |
343 |
|
#endif |
344 |
+ |
#ifdef HPUX |
345 |
+ |
sg_set_error(SG_ERROR_UNSUPPORTED, "HP-UX"); |
346 |
+ |
return NULL; |
347 |
+ |
#endif |
348 |
|
|
349 |
+ |
#ifdef WIN32 |
350 |
+ |
interfaces = 0; |
351 |
+ |
|
352 |
+ |
if((if_table = win32_get_devices()) == NULL) { |
353 |
+ |
sg_set_error(SG_ERROR_DEVICES, "network"); |
354 |
+ |
return NULL; |
355 |
+ |
} |
356 |
+ |
|
357 |
+ |
if(VECTOR_RESIZE(network_stats, if_table->dwNumEntries) < 0) { |
358 |
+ |
free(if_table); |
359 |
+ |
return NULL; |
360 |
+ |
} |
361 |
+ |
|
362 |
+ |
for (i=0; i<if_table->dwNumEntries; i++) { |
363 |
+ |
network_stat_ptr=network_stats+i; |
364 |
+ |
if_row = if_table->table[i]; |
365 |
+ |
|
366 |
+ |
if(sg_update_string(&network_stat_ptr->interface_name, |
367 |
+ |
if_row.bDescr) < 0) { |
368 |
+ |
free(if_table); |
369 |
+ |
return NULL; |
370 |
+ |
} |
371 |
+ |
network_stat_ptr->tx = if_row.dwOutOctets; |
372 |
+ |
network_stat_ptr->rx = if_row.dwInOctets; |
373 |
+ |
network_stat_ptr->ipackets = if_row.dwInUcastPkts + if_row.dwInNUcastPkts; |
374 |
+ |
network_stat_ptr->opackets = if_row.dwOutUcastPkts + if_row.dwOutNUcastPkts; |
375 |
+ |
network_stat_ptr->ierrors = if_row.dwInErrors; |
376 |
+ |
network_stat_ptr->oerrors = if_row.dwOutErrors; |
377 |
+ |
network_stat_ptr->collisions = 0; /* can't do that */ |
378 |
+ |
network_stat_ptr->systime = time(NULL); |
379 |
+ |
|
380 |
+ |
interfaces++; |
381 |
+ |
} |
382 |
+ |
free(if_table); |
383 |
+ |
|
384 |
+ |
/* Please say there's a nicer way to do this... If windows has two (or |
385 |
+ |
* more) identical network cards, GetIfTable returns them with the same |
386 |
+ |
* name, not like in Device Manager where the other has a #2 etc after |
387 |
+ |
* it. So, add the #number here. Should we be doing this? Or should the |
388 |
+ |
* end programs be dealing with duplicate names? Currently breaks |
389 |
+ |
* watch.pl in rrdgraphing. But Unix does not have the issue of |
390 |
+ |
* duplicate net device names. |
391 |
+ |
*/ |
392 |
+ |
for (i=0; i<interfaces; i++) { |
393 |
+ |
no = 2; |
394 |
+ |
for(j=i+1; j<interfaces; j++) { |
395 |
+ |
network_stat_ptr=network_stats+j; |
396 |
+ |
if(strcmp(network_stats[i].interface_name, |
397 |
+ |
network_stat_ptr->interface_name) == 0) { |
398 |
+ |
if(snprintf(buf, sizeof(buf), " #%d", no) < 0) { |
399 |
+ |
break; |
400 |
+ |
} |
401 |
+ |
if(sg_concat_string(&network_stat_ptr->interface_name, buf) != 0) { |
402 |
+ |
return NULL; |
403 |
+ |
} |
404 |
+ |
|
405 |
+ |
no++; |
406 |
+ |
} |
407 |
+ |
} |
408 |
+ |
} |
409 |
+ |
#endif |
410 |
+ |
|
411 |
|
*entries=interfaces; |
412 |
|
|
413 |
|
return network_stats; |
414 |
|
} |
415 |
|
|
416 |
< |
long long transfer_diff(long long new, long long old){ |
417 |
< |
#if defined(SOL7) || defined(LINUX) || defined(FREEBSD) || defined(DFBSD) |
418 |
< |
#define MAXVAL 0xffffffffLL |
416 |
> |
static long long transfer_diff(long long new, long long old){ |
417 |
> |
#if defined(SOL7) || defined(LINUX) || defined(FREEBSD) || defined(DFBSD) || defined(OPENBSD) || defined(WIN32) |
418 |
> |
/* 32-bit quantities, so we must explicitly deal with wraparound. */ |
419 |
> |
#define MAXVAL 0x100000000LL |
420 |
> |
if (new >= old) { |
421 |
> |
return new - old; |
422 |
> |
} else { |
423 |
> |
return MAXVAL + new - old; |
424 |
> |
} |
425 |
|
#else |
426 |
< |
#define MAXVAL 0xffffffffffffffffLL |
426 |
> |
/* 64-bit quantities, so plain subtraction works. */ |
427 |
> |
return new - old; |
428 |
|
#endif |
328 |
– |
long long result; |
329 |
– |
if(new>=old){ |
330 |
– |
result = (new-old); |
331 |
– |
}else{ |
332 |
– |
result = (MAXVAL+(new-old)); |
333 |
– |
} |
334 |
– |
|
335 |
– |
return result; |
336 |
– |
|
429 |
|
} |
430 |
|
|
431 |
< |
network_stat_t *get_network_stats_diff(int *entries) { |
432 |
< |
static network_stat_t *diff = NULL; |
433 |
< |
static int diff_count = 0; |
434 |
< |
network_stat_t *src, *dest; |
435 |
< |
int i, j, new_count; |
431 |
> |
sg_network_io_stats *sg_get_network_io_stats_diff(int *entries) { |
432 |
> |
VECTOR_DECLARE_STATIC(diff, sg_network_io_stats, 1, |
433 |
> |
network_stat_init, network_stat_destroy); |
434 |
> |
sg_network_io_stats *src = NULL, *dest; |
435 |
> |
int i, j, diff_count, new_count; |
436 |
|
|
437 |
|
if (network_stats == NULL) { |
438 |
|
/* No previous stats, so we can't calculate a difference. */ |
439 |
< |
return get_network_stats(entries); |
439 |
> |
return sg_get_network_io_stats(entries); |
440 |
|
} |
441 |
|
|
442 |
|
/* Resize the results array to match the previous stats. */ |
443 |
< |
diff = network_stat_malloc(interfaces, &diff_count, diff); |
444 |
< |
if (diff == NULL) { |
443 |
> |
diff_count = VECTOR_SIZE(network_stats); |
444 |
> |
if (VECTOR_RESIZE(diff, diff_count) < 0) { |
445 |
|
return NULL; |
446 |
|
} |
447 |
|
|
450 |
|
src = &network_stats[i]; |
451 |
|
dest = &diff[i]; |
452 |
|
|
453 |
< |
if (dest->interface_name != NULL) { |
454 |
< |
free(dest->interface_name); |
453 |
> |
if (sg_update_string(&dest->interface_name, |
454 |
> |
src->interface_name) < 0) { |
455 |
> |
return NULL; |
456 |
|
} |
364 |
– |
dest->interface_name = strdup(src->interface_name); |
457 |
|
dest->rx = src->rx; |
458 |
|
dest->tx = src->tx; |
459 |
|
dest->ipackets = src->ipackets; |
465 |
|
} |
466 |
|
|
467 |
|
/* Get a new set of stats. */ |
468 |
< |
if (get_network_stats(&new_count) == NULL) { |
468 |
> |
if (sg_get_network_io_stats(&new_count) == NULL) { |
469 |
|
return NULL; |
470 |
|
} |
471 |
|
|
502 |
|
*entries = diff_count; |
503 |
|
return diff; |
504 |
|
} |
413 |
– |
/* NETWORK INTERFACE STATS */ |
505 |
|
|
506 |
< |
void network_iface_stat_init(int start, int end, network_iface_stat_t *net_stats){ |
506 |
> |
int sg_network_io_compare_name(const void *va, const void *vb) { |
507 |
> |
const sg_network_io_stats *a = (const sg_network_io_stats *)va; |
508 |
> |
const sg_network_io_stats *b = (const sg_network_io_stats *)vb; |
509 |
|
|
510 |
< |
for(net_stats+=start; start<end; start++){ |
418 |
< |
net_stats->interface_name=NULL; |
419 |
< |
net_stats->speed=0; |
420 |
< |
net_stats->dup=UNKNOWN_DUPLEX; |
421 |
< |
net_stats++; |
422 |
< |
} |
510 |
> |
return strcmp(a->interface_name, b->interface_name); |
511 |
|
} |
512 |
|
|
513 |
< |
network_iface_stat_t *network_iface_stat_malloc(int needed_entries, int *cur_entries, network_iface_stat_t *net_stats){ |
513 |
> |
/* NETWORK INTERFACE STATS */ |
514 |
|
|
515 |
< |
if(net_stats==NULL){ |
515 |
> |
static void network_iface_stat_init(sg_network_iface_stats *s) { |
516 |
> |
s->interface_name = NULL; |
517 |
> |
s->speed = 0; |
518 |
> |
s->duplex = SG_IFACE_DUPLEX_UNKNOWN; |
519 |
> |
} |
520 |
|
|
521 |
< |
if((net_stats=malloc(needed_entries * sizeof(network_iface_stat_t)))==NULL){ |
522 |
< |
return NULL; |
431 |
< |
} |
432 |
< |
network_iface_stat_init(0, needed_entries, net_stats); |
433 |
< |
*cur_entries=needed_entries; |
434 |
< |
|
435 |
< |
return net_stats; |
436 |
< |
} |
437 |
< |
|
438 |
< |
|
439 |
< |
if(*cur_entries<needed_entries){ |
440 |
< |
net_stats=realloc(net_stats, (sizeof(network_iface_stat_t)*needed_entries)); |
441 |
< |
if(net_stats==NULL){ |
442 |
< |
return NULL; |
443 |
< |
} |
444 |
< |
network_iface_stat_init(*cur_entries, needed_entries, net_stats); |
445 |
< |
*cur_entries=needed_entries; |
446 |
< |
} |
447 |
< |
|
448 |
< |
return net_stats; |
521 |
> |
static void network_iface_stat_destroy(sg_network_iface_stats *s) { |
522 |
> |
free(s->interface_name); |
523 |
|
} |
524 |
|
|
525 |
< |
network_iface_stat_t *get_network_iface_stats(int *entries){ |
526 |
< |
static network_iface_stat_t *network_iface_stats; |
527 |
< |
network_iface_stat_t *network_iface_stat_ptr; |
528 |
< |
static int sizeof_network_iface_stats=0; |
525 |
> |
sg_network_iface_stats *sg_get_network_iface_stats(int *entries){ |
526 |
> |
VECTOR_DECLARE_STATIC(network_iface_stats, sg_network_iface_stats, 5, |
527 |
> |
network_iface_stat_init, network_iface_stat_destroy); |
528 |
> |
sg_network_iface_stats *network_iface_stat_ptr; |
529 |
|
int ifaces = 0; |
530 |
|
|
531 |
|
#ifdef SOLARIS |
532 |
< |
kstat_ctl_t *kc; |
533 |
< |
kstat_t *ksp; |
532 |
> |
kstat_ctl_t *kc; |
533 |
> |
kstat_t *ksp; |
534 |
|
kstat_named_t *knp; |
535 |
|
int sock; |
536 |
|
#endif |
537 |
|
#ifdef ALLBSD |
538 |
< |
struct ifaddrs *net, *net_ptr; |
538 |
> |
struct ifaddrs *net, *net_ptr; |
539 |
|
struct ifmediareq ifmed; |
540 |
|
struct ifreq ifr; |
541 |
|
int sock; |
542 |
|
int x; |
543 |
|
#endif |
544 |
|
#ifdef LINUX |
545 |
< |
FILE *f; |
546 |
< |
/* Horrible big enough, but it should be easily big enough */ |
547 |
< |
char line[8096]; |
545 |
> |
FILE *f; |
546 |
> |
/* Horrible big enough, but it should be easily big enough */ |
547 |
> |
char line[8096]; |
548 |
|
int sock; |
549 |
|
#endif |
550 |
+ |
#ifdef WIN32 |
551 |
+ |
PMIB_IFTABLE if_table; |
552 |
+ |
MIB_IFROW if_row; |
553 |
+ |
int i,j,no; |
554 |
+ |
char buf[5]; |
555 |
+ |
#endif |
556 |
|
|
557 |
|
#ifdef ALLBSD |
558 |
< |
if(getifaddrs(&net) != 0){ |
559 |
< |
return NULL; |
560 |
< |
} |
558 |
> |
if(getifaddrs(&net) != 0){ |
559 |
> |
sg_set_error_with_errno(SG_ERROR_GETIFADDRS, NULL); |
560 |
> |
return NULL; |
561 |
> |
} |
562 |
|
|
563 |
|
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return NULL; |
564 |
|
|
565 |
|
for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){ |
566 |
< |
if(net_ptr->ifa_addr->sa_family != AF_LINK) continue; |
486 |
< |
network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats); |
487 |
< |
if(network_iface_stats==NULL){ |
488 |
< |
return NULL; |
489 |
< |
} |
490 |
< |
network_iface_stat_ptr = network_iface_stats + ifaces; |
566 |
> |
if(net_ptr->ifa_addr->sa_family != AF_LINK) continue; |
567 |
|
|
568 |
+ |
if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) { |
569 |
+ |
return NULL; |
570 |
+ |
} |
571 |
+ |
network_iface_stat_ptr = network_iface_stats + ifaces; |
572 |
+ |
|
573 |
|
memset(&ifr, 0, sizeof(ifr)); |
574 |
|
strncpy(ifr.ifr_name, net_ptr->ifa_name, sizeof(ifr.ifr_name)); |
575 |
|
|
582 |
|
network_iface_stat_ptr->up = 0; |
583 |
|
} |
584 |
|
|
585 |
< |
if (network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name); |
586 |
< |
network_iface_stat_ptr->interface_name = strdup(net_ptr->ifa_name); |
587 |
< |
if (network_iface_stat_ptr->interface_name == NULL) return NULL; |
585 |
> |
if (sg_update_string(&network_iface_stat_ptr->interface_name, |
586 |
> |
net_ptr->ifa_name) < 0) { |
587 |
> |
return NULL; |
588 |
> |
} |
589 |
|
|
590 |
|
network_iface_stat_ptr->speed = 0; |
591 |
< |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
591 |
> |
network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN; |
592 |
|
ifaces++; |
593 |
|
|
594 |
|
memset(&ifmed, 0, sizeof(struct ifmediareq)); |
595 |
< |
strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name)); |
595 |
> |
sg_strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name)); |
596 |
|
if(ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){ |
597 |
|
/* Not all interfaces support the media ioctls. */ |
598 |
|
continue; |
627 |
|
case(IFM_1000_SX): |
628 |
|
case(IFM_1000_LX): |
629 |
|
case(IFM_1000_CX): |
630 |
< |
#ifdef IFM_1000_TX |
631 |
< |
case(IFM_1000_TX): /* FreeBSD 4 and others? */ |
630 |
> |
#if defined(IFM_1000_TX) && !defined(OPENBSD) |
631 |
> |
case(IFM_1000_TX): /* FreeBSD 4 and others (but NOT OpenBSD)? */ |
632 |
|
#endif |
633 |
|
#ifdef IFM_1000_FX |
634 |
|
case(IFM_1000_FX): /* FreeBSD 4 */ |
645 |
|
} |
646 |
|
|
647 |
|
if( (ifmed.ifm_active | IFM_FDX) == ifmed.ifm_active ){ |
648 |
< |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
648 |
> |
network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_FULL; |
649 |
|
}else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){ |
650 |
< |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
650 |
> |
network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_HALF; |
651 |
|
}else{ |
652 |
< |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
652 |
> |
network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN; |
653 |
|
} |
654 |
|
|
655 |
|
} |
659 |
|
|
660 |
|
#ifdef SOLARIS |
661 |
|
if ((kc = kstat_open()) == NULL) { |
662 |
+ |
sg_set_error(SG_ERROR_KSTAT_OPEN, NULL); |
663 |
|
return NULL; |
664 |
|
} |
665 |
|
|
666 |
|
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) { |
667 |
+ |
sg_set_error_with_errno(SG_ERROR_SOCKET, NULL); |
668 |
|
return NULL; |
669 |
|
} |
670 |
|
|
671 |
|
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
672 |
< |
if (!strcmp(ksp->ks_class, "net")) { |
672 |
> |
if (strcmp(ksp->ks_class, "net") == 0) { |
673 |
|
struct ifreq ifr; |
674 |
|
|
675 |
|
kstat_read(kc, ksp, NULL); |
680 |
|
continue; |
681 |
|
} |
682 |
|
|
683 |
< |
network_iface_stats = network_iface_stat_malloc(ifaces + 1, &sizeof_network_iface_stats, network_iface_stats); |
600 |
< |
if (network_iface_stats == NULL) { |
683 |
> |
if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) { |
684 |
|
return NULL; |
685 |
|
} |
686 |
|
network_iface_stat_ptr = network_iface_stats + ifaces; |
687 |
|
ifaces++; |
688 |
|
|
689 |
< |
if (network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name); |
690 |
< |
network_iface_stat_ptr->interface_name = strdup(ksp->ks_name); |
691 |
< |
if (network_iface_stat_ptr->interface_name == NULL) return NULL; |
689 |
> |
if (sg_update_string(&network_iface_stat_ptr->interface_name, |
690 |
> |
ksp->ks_name) < 0) { |
691 |
> |
return NULL; |
692 |
> |
} |
693 |
|
|
694 |
|
if ((ifr.ifr_flags & IFF_UP) != 0) { |
695 |
|
network_iface_stat_ptr->up = 1; |
696 |
|
} else { |
697 |
< |
network_iface_stat_ptr->up = 1; |
697 |
> |
network_iface_stat_ptr->up = 0; |
698 |
|
} |
699 |
|
|
700 |
|
if ((knp = kstat_data_lookup(ksp, "ifspeed")) != NULL) { |
703 |
|
network_iface_stat_ptr->speed = 0; |
704 |
|
} |
705 |
|
|
706 |
< |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
706 |
> |
network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN; |
707 |
|
if ((knp = kstat_data_lookup(ksp, "link_duplex")) != NULL) { |
708 |
|
switch (knp->value.ui32) { |
709 |
|
case 1: |
710 |
< |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
710 |
> |
network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_HALF; |
711 |
|
break; |
712 |
|
case 2: |
713 |
< |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
713 |
> |
network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_FULL; |
714 |
|
break; |
715 |
|
} |
716 |
|
} |
719 |
|
|
720 |
|
close(sock); |
721 |
|
kstat_close(kc); |
722 |
< |
#endif |
722 |
> |
#endif |
723 |
|
#ifdef LINUX |
724 |
|
f = fopen("/proc/net/dev", "r"); |
725 |
< |
if(f == NULL){ |
726 |
< |
return NULL; |
727 |
< |
} |
725 |
> |
if(f == NULL){ |
726 |
> |
sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/net/dev"); |
727 |
> |
return NULL; |
728 |
> |
} |
729 |
|
|
730 |
|
/* Setup stuff so we can do the ioctl to get the info */ |
731 |
|
if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){ |
732 |
+ |
sg_set_error_with_errno(SG_ERROR_SOCKET, NULL); |
733 |
|
return NULL; |
734 |
|
} |
735 |
|
|
736 |
|
/* Ignore first 2 lines.. Just headings */ |
737 |
< |
if((fgets(line, sizeof(line), f)) == NULL) return NULL; |
738 |
< |
if((fgets(line, sizeof(line), f)) == NULL) return NULL; |
737 |
> |
if((fgets(line, sizeof(line), f)) == NULL) { |
738 |
> |
sg_set_error(SG_ERROR_PARSE, NULL); |
739 |
> |
return NULL; |
740 |
> |
} |
741 |
> |
if((fgets(line, sizeof(line), f)) == NULL) { |
742 |
> |
sg_set_error(SG_ERROR_PARSE, NULL); |
743 |
> |
return NULL; |
744 |
> |
} |
745 |
|
|
746 |
< |
while((fgets(line, sizeof(line), f)) != NULL){ |
747 |
< |
char *name, *ptr; |
748 |
< |
struct ifreq ifr; |
749 |
< |
struct ethtool_cmd ethcmd; |
750 |
< |
int err; |
746 |
> |
while((fgets(line, sizeof(line), f)) != NULL){ |
747 |
> |
char *name, *ptr; |
748 |
> |
struct ifreq ifr; |
749 |
> |
struct ethtool_cmd ethcmd; |
750 |
> |
int err; |
751 |
|
|
752 |
|
/* Get the interface name */ |
753 |
< |
ptr = strchr(line, ':'); |
754 |
< |
if (ptr == NULL) continue; |
755 |
< |
*ptr='\0'; |
756 |
< |
name = line; |
757 |
< |
while(isspace(*(name))){ |
758 |
< |
name++; |
759 |
< |
} |
753 |
> |
ptr = strchr(line, ':'); |
754 |
> |
if (ptr == NULL) continue; |
755 |
> |
*ptr='\0'; |
756 |
> |
name = line; |
757 |
> |
while(isspace(*(name))){ |
758 |
> |
name++; |
759 |
> |
} |
760 |
|
|
761 |
< |
memset(&ifr, 0, sizeof ifr); |
762 |
< |
strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); |
761 |
> |
memset(&ifr, 0, sizeof ifr); |
762 |
> |
strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); |
763 |
|
|
764 |
|
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) { |
765 |
|
continue; |
766 |
|
} |
767 |
|
|
768 |
|
/* We have a good interface to add */ |
769 |
< |
network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats); |
678 |
< |
if(network_iface_stats==NULL){ |
769 |
> |
if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) { |
770 |
|
return NULL; |
771 |
|
} |
772 |
|
network_iface_stat_ptr = network_iface_stats + ifaces; |
773 |
< |
network_iface_stat_ptr->interface_name = strdup(name); |
773 |
> |
|
774 |
> |
if (sg_update_string(&network_iface_stat_ptr->interface_name, |
775 |
> |
name) < 0) { |
776 |
> |
return NULL; |
777 |
> |
} |
778 |
|
if ((ifr.ifr_flags & IFF_UP) != 0) { |
779 |
|
network_iface_stat_ptr->up = 1; |
780 |
|
} else { |
781 |
|
network_iface_stat_ptr->up = 0; |
782 |
|
} |
783 |
|
|
784 |
< |
memset(ðcmd, 0, sizeof ethcmd); |
785 |
< |
ethcmd.cmd = ETHTOOL_GSET; |
786 |
< |
ifr.ifr_data = (caddr_t) ðcmd; |
784 |
> |
memset(ðcmd, 0, sizeof ethcmd); |
785 |
> |
ethcmd.cmd = ETHTOOL_GSET; |
786 |
> |
ifr.ifr_data = (caddr_t) ðcmd; |
787 |
|
|
788 |
< |
err = ioctl(sock, SIOCETHTOOL, &ifr); |
789 |
< |
if (err == 0) { |
788 |
> |
err = ioctl(sock, SIOCETHTOOL, &ifr); |
789 |
> |
if (err == 0) { |
790 |
|
network_iface_stat_ptr->speed = ethcmd.speed; |
791 |
|
|
792 |
|
switch (ethcmd.duplex) { |
793 |
< |
case 0x00: |
794 |
< |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
793 |
> |
case DUPLEX_FULL: |
794 |
> |
network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_FULL; |
795 |
|
break; |
796 |
< |
case 0x01: |
797 |
< |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
796 |
> |
case DUPLEX_HALF: |
797 |
> |
network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_HALF; |
798 |
|
break; |
799 |
|
default: |
800 |
< |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
800 |
> |
network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN; |
801 |
|
} |
802 |
|
} else { |
803 |
|
/* Not all interfaces support the ethtool ioctl. */ |
804 |
|
network_iface_stat_ptr->speed = 0; |
805 |
< |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
805 |
> |
network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN; |
806 |
|
} |
807 |
|
|
808 |
|
ifaces++; |
810 |
|
close(sock); |
811 |
|
fclose(f); |
812 |
|
#endif |
813 |
+ |
#ifdef CYGWIN |
814 |
+ |
sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin"); |
815 |
+ |
return NULL; |
816 |
+ |
#endif |
817 |
+ |
#ifdef HPUX |
818 |
+ |
sg_set_error(SG_ERROR_UNSUPPORTED, "HP-UX"); |
819 |
+ |
return NULL; |
820 |
+ |
#endif |
821 |
+ |
#ifdef WIN32 |
822 |
+ |
ifaces = 0; |
823 |
+ |
|
824 |
+ |
if((if_table = win32_get_devices()) == NULL) { |
825 |
+ |
sg_set_error(SG_ERROR_DEVICES, "network interfaces"); |
826 |
+ |
return NULL; |
827 |
+ |
} |
828 |
+ |
|
829 |
+ |
if(VECTOR_RESIZE(network_iface_stats, if_table->dwNumEntries) < 0) { |
830 |
+ |
free(if_table); |
831 |
+ |
return NULL; |
832 |
+ |
} |
833 |
+ |
|
834 |
+ |
for(i=0; i<if_table->dwNumEntries; i++) { |
835 |
+ |
network_iface_stat_ptr=network_iface_stats+i; |
836 |
+ |
if_row = if_table->table[i]; |
837 |
+ |
|
838 |
+ |
if(sg_update_string(&network_iface_stat_ptr->interface_name, |
839 |
+ |
if_row.bDescr) < 0) { |
840 |
+ |
free(if_table); |
841 |
+ |
return NULL; |
842 |
+ |
} |
843 |
+ |
network_iface_stat_ptr->speed = if_row.dwSpeed /1000000; |
844 |
+ |
|
845 |
+ |
if((if_row.dwOperStatus == MIB_IF_OPER_STATUS_CONNECTED || |
846 |
+ |
if_row.dwOperStatus == |
847 |
+ |
MIB_IF_OPER_STATUS_OPERATIONAL) && |
848 |
+ |
if_row.dwAdminStatus == 1) { |
849 |
+ |
network_iface_stat_ptr->up = 1; |
850 |
+ |
} else { |
851 |
+ |
network_iface_stat_ptr->up = 0; |
852 |
+ |
} |
853 |
+ |
|
854 |
+ |
ifaces++; |
855 |
+ |
} |
856 |
+ |
free(if_table); |
857 |
+ |
|
858 |
+ |
/* again with the renumbering */ |
859 |
+ |
for (i=0; i<ifaces; i++) { |
860 |
+ |
no = 2; |
861 |
+ |
for(j=i+1; j<ifaces; j++) { |
862 |
+ |
network_iface_stat_ptr=network_iface_stats+j; |
863 |
+ |
if(strcmp(network_iface_stats[i].interface_name, |
864 |
+ |
network_iface_stat_ptr->interface_name) == 0) { |
865 |
+ |
if(snprintf(buf, sizeof(buf), " #%d", no) < 0) { |
866 |
+ |
break; |
867 |
+ |
} |
868 |
+ |
if(sg_concat_string(&network_iface_stat_ptr->interface_name, buf) != 0) { |
869 |
+ |
return NULL; |
870 |
+ |
} |
871 |
+ |
|
872 |
+ |
no++; |
873 |
+ |
} |
874 |
+ |
} |
875 |
+ |
} |
876 |
+ |
#endif |
877 |
+ |
|
878 |
+ |
#ifdef SG_ENABLE_DEPRECATED |
879 |
+ |
network_iface_stat_ptr->dup = network_iface_stat_ptr->duplex; |
880 |
+ |
#endif |
881 |
+ |
|
882 |
|
*entries = ifaces; |
883 |
|
return network_iface_stats; |
884 |
+ |
} |
885 |
+ |
|
886 |
+ |
int sg_network_iface_compare_name(const void *va, const void *vb) { |
887 |
+ |
const sg_network_iface_stats *a = (const sg_network_iface_stats *)va; |
888 |
+ |
const sg_network_iface_stats *b = (const sg_network_iface_stats *)vb; |
889 |
+ |
|
890 |
+ |
return strcmp(a->interface_name, b->interface_name); |
891 |
|
} |
892 |
|
|