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