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++; |
81 |
< |
} |
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 |
< |
|
86 |
< |
if(net_stats==NULL){ |
87 |
< |
|
88 |
< |
if((net_stats=malloc(needed_entries * sizeof(network_stat_t)))==NULL){ |
89 |
< |
return NULL; |
90 |
< |
} |
91 |
< |
network_stat_init(0, needed_entries, net_stats); |
92 |
< |
*cur_entries=needed_entries; |
93 |
< |
|
94 |
< |
return net_stats; |
95 |
< |
} |
96 |
< |
|
97 |
< |
|
98 |
< |
if(*cur_entries<needed_entries){ |
99 |
< |
net_stats=realloc(net_stats, (sizeof(network_stat_t)*needed_entries)); |
100 |
< |
if(net_stats==NULL){ |
101 |
< |
return NULL; |
102 |
< |
} |
103 |
< |
network_stat_init(*cur_entries, needed_entries, net_stats); |
104 |
< |
*cur_entries=needed_entries; |
105 |
< |
} |
106 |
< |
|
107 |
< |
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 |
|
|
113 |
– |
static int sizeof_network_stats=0; |
114 |
– |
network_stat_t *network_stat_ptr; |
115 |
– |
|
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(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; |
135 |
> |
network_stat_ptr->tx=net_data->ifi_obytes; |
136 |
> |
network_stat_ptr->ipackets=net_data->ifi_ipackets; |
137 |
> |
network_stat_ptr->opackets=net_data->ifi_opackets; |
138 |
> |
network_stat_ptr->ierrors=net_data->ifi_ierrors; |
139 |
> |
network_stat_ptr->oerrors=net_data->ifi_oerrors; |
140 |
> |
network_stat_ptr->collisions=net_data->ifi_collisions; |
141 |
|
network_stat_ptr->systime=time(NULL); |
142 |
|
interfaces++; |
143 |
|
} |
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(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 |
|
|
300 |
|
return network_stats; |
301 |
|
} |
302 |
|
|
303 |
< |
long long transfer_diff(long long new, long long old){ |
304 |
< |
#if defined(SOL7) || defined(LINUX) || defined(FREEBSD) |
305 |
< |
#define MAXVAL 4294967296LL |
303 |
> |
static long long transfer_diff(long long new, long long old){ |
304 |
> |
#if defined(SOL7) || defined(LINUX) || defined(FREEBSD) || defined(DFBSD) || defined(OPENBSD) |
305 |
> |
/* 32-bit quantities, so we must explicitly deal with wraparound. */ |
306 |
> |
#define MAXVAL 0x100000000LL |
307 |
> |
if (new >= old) { |
308 |
> |
return new - old; |
309 |
> |
} else { |
310 |
> |
return MAXVAL + new - old; |
311 |
> |
} |
312 |
|
#else |
313 |
< |
#define MAXVAL 18446744073709551616LL |
313 |
> |
/* 64-bit quantities, so plain subtraction works. */ |
314 |
> |
return new - old; |
315 |
|
#endif |
270 |
– |
long long result; |
271 |
– |
if(new>=old){ |
272 |
– |
result = (new-old); |
273 |
– |
}else{ |
274 |
– |
result = (MAXVAL+(new-old)); |
275 |
– |
} |
276 |
– |
|
277 |
– |
return result; |
278 |
– |
|
316 |
|
} |
317 |
|
|
318 |
< |
network_stat_t *get_network_stats_diff(int *entries) { |
319 |
< |
static network_stat_t *diff = NULL; |
320 |
< |
static int diff_count = 0; |
321 |
< |
network_stat_t *src, *dest; |
322 |
< |
int i, j, new_count; |
318 |
> |
sg_network_io_stats *sg_get_network_io_stats_diff(int *entries) { |
319 |
> |
VECTOR_DECLARE_STATIC(diff, sg_network_io_stats, 1, |
320 |
> |
network_stat_init, network_stat_destroy); |
321 |
> |
sg_network_io_stats *src = NULL, *dest; |
322 |
> |
int i, j, diff_count, new_count; |
323 |
|
|
324 |
|
if (network_stats == NULL) { |
325 |
|
/* No previous stats, so we can't calculate a difference. */ |
326 |
< |
return get_network_stats(entries); |
326 |
> |
return sg_get_network_io_stats(entries); |
327 |
|
} |
328 |
|
|
329 |
|
/* Resize the results array to match the previous stats. */ |
330 |
< |
diff = network_stat_malloc(interfaces, &diff_count, diff); |
331 |
< |
if (diff == NULL) { |
330 |
> |
diff_count = VECTOR_SIZE(network_stats); |
331 |
> |
if (VECTOR_RESIZE(diff, diff_count) < 0) { |
332 |
|
return NULL; |
333 |
|
} |
334 |
|
|
337 |
|
src = &network_stats[i]; |
338 |
|
dest = &diff[i]; |
339 |
|
|
340 |
< |
if (dest->interface_name != NULL) { |
341 |
< |
free(dest->interface_name); |
340 |
> |
if (sg_update_string(&dest->interface_name, |
341 |
> |
src->interface_name) < 0) { |
342 |
> |
return NULL; |
343 |
|
} |
306 |
– |
dest->interface_name = strdup(src->interface_name); |
344 |
|
dest->rx = src->rx; |
345 |
|
dest->tx = src->tx; |
346 |
+ |
dest->ipackets = src->ipackets; |
347 |
+ |
dest->opackets = src->opackets; |
348 |
+ |
dest->ierrors = src->ierrors; |
349 |
+ |
dest->oerrors = src->oerrors; |
350 |
+ |
dest->collisions = src->collisions; |
351 |
|
dest->systime = src->systime; |
352 |
|
} |
353 |
|
|
354 |
|
/* Get a new set of stats. */ |
355 |
< |
if (get_network_stats(&new_count) == NULL) { |
355 |
> |
if (sg_get_network_io_stats(&new_count) == NULL) { |
356 |
|
return NULL; |
357 |
|
} |
358 |
|
|
378 |
|
difference. */ |
379 |
|
dest->rx = transfer_diff(src->rx, dest->rx); |
380 |
|
dest->tx = transfer_diff(src->tx, dest->tx); |
381 |
+ |
dest->ipackets = transfer_diff(src->ipackets, dest->ipackets); |
382 |
+ |
dest->opackets = transfer_diff(src->opackets, dest->opackets); |
383 |
+ |
dest->ierrors = transfer_diff(src->ierrors, dest->ierrors); |
384 |
+ |
dest->oerrors = transfer_diff(src->oerrors, dest->oerrors); |
385 |
+ |
dest->collisions = transfer_diff(src->collisions, dest->collisions); |
386 |
|
dest->systime = src->systime - dest->systime; |
387 |
|
} |
388 |
|
|
389 |
|
*entries = diff_count; |
390 |
|
return diff; |
391 |
|
} |
392 |
+ |
|
393 |
|
/* NETWORK INTERFACE STATS */ |
394 |
|
|
395 |
< |
void network_iface_stat_init(int start, int end, network_iface_stat_t *net_stats){ |
396 |
< |
|
397 |
< |
for(net_stats+=start; start<end; start++){ |
398 |
< |
net_stats->interface_name=NULL; |
351 |
< |
net_stats->speed=0; |
352 |
< |
net_stats->dup=UNKNOWN_DUPLEX; |
353 |
< |
net_stats++; |
354 |
< |
} |
395 |
> |
static void network_iface_stat_init(sg_network_iface_stats *s) { |
396 |
> |
s->interface_name = NULL; |
397 |
> |
s->speed = 0; |
398 |
> |
s->dup = SG_IFACE_DUPLEX_UNKNOWN; |
399 |
|
} |
400 |
|
|
401 |
< |
network_iface_stat_t *network_iface_stat_malloc(int needed_entries, int *cur_entries, network_iface_stat_t *net_stats){ |
402 |
< |
|
359 |
< |
if(net_stats==NULL){ |
360 |
< |
|
361 |
< |
if((net_stats=malloc(needed_entries * sizeof(network_iface_stat_t)))==NULL){ |
362 |
< |
return NULL; |
363 |
< |
} |
364 |
< |
network_iface_stat_init(0, needed_entries, net_stats); |
365 |
< |
*cur_entries=needed_entries; |
366 |
< |
|
367 |
< |
return net_stats; |
368 |
< |
} |
369 |
< |
|
370 |
< |
|
371 |
< |
if(*cur_entries<needed_entries){ |
372 |
< |
net_stats=realloc(net_stats, (sizeof(network_iface_stat_t)*needed_entries)); |
373 |
< |
if(net_stats==NULL){ |
374 |
< |
return NULL; |
375 |
< |
} |
376 |
< |
network_iface_stat_init(*cur_entries, needed_entries, net_stats); |
377 |
< |
*cur_entries=needed_entries; |
378 |
< |
} |
379 |
< |
|
380 |
< |
return net_stats; |
401 |
> |
static void network_iface_stat_destroy(sg_network_iface_stats *s) { |
402 |
> |
free(s->interface_name); |
403 |
|
} |
404 |
|
|
405 |
< |
network_iface_stat_t *get_network_iface_stats(int *entries){ |
406 |
< |
static network_iface_stat_t *network_iface_stats; |
407 |
< |
network_iface_stat_t *network_iface_stat_ptr; |
408 |
< |
static int sizeof_network_iface_stats=0; |
405 |
> |
sg_network_iface_stats *sg_get_network_iface_stats(int *entries){ |
406 |
> |
VECTOR_DECLARE_STATIC(network_iface_stats, sg_network_iface_stats, 5, |
407 |
> |
network_iface_stat_init, network_iface_stat_destroy); |
408 |
> |
sg_network_iface_stats *network_iface_stat_ptr; |
409 |
|
int ifaces = 0; |
410 |
|
|
411 |
|
#ifdef SOLARIS |
412 |
< |
kstat_ctl_t *kc; |
413 |
< |
kstat_t *ksp; |
412 |
> |
kstat_ctl_t *kc; |
413 |
> |
kstat_t *ksp; |
414 |
|
kstat_named_t *knp; |
415 |
|
int sock; |
416 |
|
#endif |
417 |
|
#ifdef ALLBSD |
418 |
< |
struct ifaddrs *net, *net_ptr; |
418 |
> |
struct ifaddrs *net, *net_ptr; |
419 |
|
struct ifmediareq ifmed; |
420 |
|
struct ifreq ifr; |
421 |
|
int sock; |
422 |
|
int x; |
423 |
|
#endif |
424 |
|
#ifdef LINUX |
425 |
< |
FILE *f; |
426 |
< |
/* Horrible big enough, but it should be easily big enough */ |
427 |
< |
char line[8096]; |
425 |
> |
FILE *f; |
426 |
> |
/* Horrible big enough, but it should be easily big enough */ |
427 |
> |
char line[8096]; |
428 |
|
int sock; |
429 |
|
#endif |
430 |
|
|
431 |
|
#ifdef ALLBSD |
432 |
< |
if(getifaddrs(&net) != 0){ |
433 |
< |
return NULL; |
434 |
< |
} |
432 |
> |
if(getifaddrs(&net) != 0){ |
433 |
> |
sg_set_error(SG_ERROR_GETIFADDRS, NULL); |
434 |
> |
return NULL; |
435 |
> |
} |
436 |
|
|
437 |
|
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return NULL; |
438 |
|
|
439 |
|
for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){ |
440 |
< |
if(net_ptr->ifa_addr->sa_family != AF_LINK) continue; |
418 |
< |
network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats); |
419 |
< |
if(network_iface_stats==NULL){ |
420 |
< |
return NULL; |
421 |
< |
} |
422 |
< |
network_iface_stat_ptr = network_iface_stats + ifaces; |
440 |
> |
if(net_ptr->ifa_addr->sa_family != AF_LINK) continue; |
441 |
|
|
442 |
+ |
if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) { |
443 |
+ |
return NULL; |
444 |
+ |
} |
445 |
+ |
network_iface_stat_ptr = network_iface_stats + ifaces; |
446 |
+ |
|
447 |
|
memset(&ifr, 0, sizeof(ifr)); |
448 |
|
strncpy(ifr.ifr_name, net_ptr->ifa_name, sizeof(ifr.ifr_name)); |
449 |
|
|
456 |
|
network_iface_stat_ptr->up = 0; |
457 |
|
} |
458 |
|
|
459 |
< |
if (network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name); |
460 |
< |
network_iface_stat_ptr->interface_name = strdup(net_ptr->ifa_name); |
461 |
< |
if (network_iface_stat_ptr->interface_name == NULL) return NULL; |
459 |
> |
if (sg_update_string(&network_iface_stat_ptr->interface_name, |
460 |
> |
net_ptr->ifa_name) < 0) { |
461 |
> |
return NULL; |
462 |
> |
} |
463 |
|
|
464 |
|
network_iface_stat_ptr->speed = 0; |
465 |
< |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
465 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_UNKNOWN; |
466 |
|
ifaces++; |
467 |
|
|
468 |
|
memset(&ifmed, 0, sizeof(struct ifmediareq)); |
469 |
< |
strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name)); |
469 |
> |
sg_strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name)); |
470 |
|
if(ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){ |
471 |
|
/* Not all interfaces support the media ioctls. */ |
472 |
|
continue; |
501 |
|
case(IFM_1000_SX): |
502 |
|
case(IFM_1000_LX): |
503 |
|
case(IFM_1000_CX): |
504 |
< |
#if defined(FREEBSD) && !defined(FREEBSD5) |
505 |
< |
case(IFM_1000_TX): |
482 |
< |
case(IFM_1000_FX): |
483 |
< |
#else |
484 |
< |
case(IFM_1000_T): |
504 |
> |
#if defined(IFM_1000_TX) && !defined(OPENBSD) |
505 |
> |
case(IFM_1000_TX): /* FreeBSD 4 and others (but NOT OpenBSD)? */ |
506 |
|
#endif |
507 |
+ |
#ifdef IFM_1000_FX |
508 |
+ |
case(IFM_1000_FX): /* FreeBSD 4 */ |
509 |
+ |
#endif |
510 |
+ |
#ifdef IFM_1000_T |
511 |
+ |
case(IFM_1000_T): /* FreeBSD 5 */ |
512 |
+ |
#endif |
513 |
|
network_iface_stat_ptr->speed = 1000; |
514 |
|
break; |
515 |
|
/* We don't know what it is */ |
519 |
|
} |
520 |
|
|
521 |
|
if( (ifmed.ifm_active | IFM_FDX) == ifmed.ifm_active ){ |
522 |
< |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
522 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_FULL; |
523 |
|
}else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){ |
524 |
< |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
524 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_HALF; |
525 |
|
}else{ |
526 |
< |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
526 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_UNKNOWN; |
527 |
|
} |
528 |
|
|
529 |
|
} |
533 |
|
|
534 |
|
#ifdef SOLARIS |
535 |
|
if ((kc = kstat_open()) == NULL) { |
536 |
+ |
sg_set_error(SG_ERROR_KSTAT_OPEN, NULL); |
537 |
|
return NULL; |
538 |
|
} |
539 |
|
|
540 |
|
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) { |
541 |
+ |
sg_set_error(SG_ERROR_SOCKET, NULL); |
542 |
|
return NULL; |
543 |
|
} |
544 |
|
|
554 |
|
continue; |
555 |
|
} |
556 |
|
|
557 |
< |
network_iface_stats = network_iface_stat_malloc(ifaces + 1, &sizeof_network_iface_stats, network_iface_stats); |
529 |
< |
if (network_iface_stats == NULL) { |
557 |
> |
if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) { |
558 |
|
return NULL; |
559 |
|
} |
560 |
|
network_iface_stat_ptr = network_iface_stats + ifaces; |
561 |
|
ifaces++; |
562 |
|
|
563 |
< |
if (network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name); |
564 |
< |
network_iface_stat_ptr->interface_name = strdup(ksp->ks_name); |
565 |
< |
if (network_iface_stat_ptr->interface_name == NULL) return NULL; |
563 |
> |
if (sg_update_string(&network_iface_stat_ptr->interface_name, |
564 |
> |
ksp->ks_name) < 0) { |
565 |
> |
return NULL; |
566 |
> |
} |
567 |
|
|
568 |
|
if ((ifr.ifr_flags & IFF_UP) != 0) { |
569 |
|
network_iface_stat_ptr->up = 1; |
577 |
|
network_iface_stat_ptr->speed = 0; |
578 |
|
} |
579 |
|
|
580 |
< |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
580 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_UNKNOWN; |
581 |
|
if ((knp = kstat_data_lookup(ksp, "link_duplex")) != NULL) { |
582 |
|
switch (knp->value.ui32) { |
583 |
|
case 1: |
584 |
< |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
584 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_HALF; |
585 |
|
break; |
586 |
|
case 2: |
587 |
< |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
587 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_FULL; |
588 |
|
break; |
589 |
|
} |
590 |
|
} |
596 |
|
#endif |
597 |
|
#ifdef LINUX |
598 |
|
f = fopen("/proc/net/dev", "r"); |
599 |
< |
if(f == NULL){ |
600 |
< |
return NULL; |
601 |
< |
} |
599 |
> |
if(f == NULL){ |
600 |
> |
sg_set_error(SG_ERROR_OPEN, "/proc/net/dev"); |
601 |
> |
return NULL; |
602 |
> |
} |
603 |
|
|
604 |
|
/* Setup stuff so we can do the ioctl to get the info */ |
605 |
|
if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){ |
606 |
+ |
sg_set_error(SG_ERROR_SOCKET, NULL); |
607 |
|
return NULL; |
608 |
|
} |
609 |
|
|
610 |
|
/* Ignore first 2 lines.. Just headings */ |
611 |
< |
if((fgets(line, sizeof(line), f)) == NULL) return NULL; |
612 |
< |
if((fgets(line, sizeof(line), f)) == NULL) return NULL; |
611 |
> |
if((fgets(line, sizeof(line), f)) == NULL) { |
612 |
> |
sg_set_error(SG_ERROR_PARSE, NULL); |
613 |
> |
return NULL; |
614 |
> |
} |
615 |
> |
if((fgets(line, sizeof(line), f)) == NULL) { |
616 |
> |
sg_set_error(SG_ERROR_PARSE, NULL); |
617 |
> |
return NULL; |
618 |
> |
} |
619 |
|
|
620 |
< |
while((fgets(line, sizeof(line), f)) != NULL){ |
621 |
< |
char *name, *ptr; |
622 |
< |
struct ifreq ifr; |
623 |
< |
struct ethtool_cmd ethcmd; |
624 |
< |
int err; |
620 |
> |
while((fgets(line, sizeof(line), f)) != NULL){ |
621 |
> |
char *name, *ptr; |
622 |
> |
struct ifreq ifr; |
623 |
> |
struct ethtool_cmd ethcmd; |
624 |
> |
int err; |
625 |
|
|
626 |
|
/* Get the interface name */ |
627 |
< |
ptr = strchr(line, ':'); |
628 |
< |
if (ptr == NULL) continue; |
629 |
< |
*ptr='\0'; |
630 |
< |
name = line; |
631 |
< |
while(isspace(*(name))){ |
632 |
< |
name++; |
633 |
< |
} |
627 |
> |
ptr = strchr(line, ':'); |
628 |
> |
if (ptr == NULL) continue; |
629 |
> |
*ptr='\0'; |
630 |
> |
name = line; |
631 |
> |
while(isspace(*(name))){ |
632 |
> |
name++; |
633 |
> |
} |
634 |
|
|
635 |
< |
memset(&ifr, 0, sizeof ifr); |
636 |
< |
strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); |
635 |
> |
memset(&ifr, 0, sizeof ifr); |
636 |
> |
strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); |
637 |
|
|
638 |
|
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) { |
639 |
|
continue; |
640 |
|
} |
641 |
|
|
642 |
|
/* We have a good interface to add */ |
643 |
< |
network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats); |
607 |
< |
if(network_iface_stats==NULL){ |
643 |
> |
if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) { |
644 |
|
return NULL; |
645 |
|
} |
646 |
|
network_iface_stat_ptr = network_iface_stats + ifaces; |
647 |
< |
network_iface_stat_ptr->interface_name = strdup(name); |
647 |
> |
|
648 |
> |
if (sg_update_string(&network_iface_stat_ptr->interface_name, |
649 |
> |
name) < 0) { |
650 |
> |
return NULL; |
651 |
> |
} |
652 |
|
if ((ifr.ifr_flags & IFF_UP) != 0) { |
653 |
|
network_iface_stat_ptr->up = 1; |
654 |
|
} else { |
655 |
|
network_iface_stat_ptr->up = 0; |
656 |
|
} |
657 |
|
|
658 |
< |
memset(ðcmd, 0, sizeof ethcmd); |
659 |
< |
ethcmd.cmd = ETHTOOL_GSET; |
660 |
< |
ifr.ifr_data = (caddr_t) ðcmd; |
658 |
> |
memset(ðcmd, 0, sizeof ethcmd); |
659 |
> |
ethcmd.cmd = ETHTOOL_GSET; |
660 |
> |
ifr.ifr_data = (caddr_t) ðcmd; |
661 |
|
|
662 |
< |
err = ioctl(sock, SIOCETHTOOL, &ifr); |
663 |
< |
if (err == 0) { |
662 |
> |
err = ioctl(sock, SIOCETHTOOL, &ifr); |
663 |
> |
if (err == 0) { |
664 |
|
network_iface_stat_ptr->speed = ethcmd.speed; |
665 |
|
|
666 |
|
switch (ethcmd.duplex) { |
667 |
|
case 0x00: |
668 |
< |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
668 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_FULL; |
669 |
|
break; |
670 |
|
case 0x01: |
671 |
< |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
671 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_HALF; |
672 |
|
break; |
673 |
|
default: |
674 |
< |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
674 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_UNKNOWN; |
675 |
|
} |
676 |
|
} else { |
677 |
|
/* Not all interfaces support the ethtool ioctl. */ |
678 |
|
network_iface_stat_ptr->speed = 0; |
679 |
< |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
679 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_UNKNOWN; |
680 |
|
} |
681 |
|
|
682 |
|
ifaces++; |