1 |
tdb |
1.22 |
/* |
2 |
pajs |
1.1 |
* i-scream central monitoring system |
3 |
tdb |
1.15 |
* http://www.i-scream.org |
4 |
tdb |
1.22 |
* Copyright (C) 2000-2004 i-scream |
5 |
pajs |
1.1 |
* |
6 |
tdb |
1.22 |
* This library is free software; you can redistribute it and/or |
7 |
|
|
* modify it under the terms of the GNU Lesser General Public |
8 |
|
|
* License as published by the Free Software Foundation; either |
9 |
|
|
* version 2.1 of the License, or (at your option) any later version. |
10 |
pajs |
1.1 |
* |
11 |
tdb |
1.22 |
* This library is distributed in the hope that it will be useful, |
12 |
pajs |
1.1 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
tdb |
1.22 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
|
|
* Lesser General Public License for more details. |
15 |
pajs |
1.1 |
* |
16 |
tdb |
1.22 |
* You should have received a copy of the GNU Lesser General Public |
17 |
|
|
* License along with this library; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
19 |
|
|
* 02111-1307 USA |
20 |
tdb |
1.23 |
* |
21 |
tdb |
1.47 |
* $Id: network_stats.c,v 1.46 2004/03/06 19:04:29 tdb Exp $ |
22 |
pajs |
1.1 |
*/ |
23 |
|
|
|
24 |
|
|
#ifdef HAVE_CONFIG_H |
25 |
|
|
#include "config.h" |
26 |
|
|
#endif |
27 |
|
|
|
28 |
tdb |
1.4 |
#include <stdlib.h> |
29 |
pajs |
1.6 |
#include <string.h> |
30 |
pajs |
1.1 |
#include "statgrab.h" |
31 |
tdb |
1.11 |
#include <time.h> |
32 |
pajs |
1.1 |
#ifdef SOLARIS |
33 |
|
|
#include <kstat.h> |
34 |
|
|
#include <sys/sysinfo.h> |
35 |
ats |
1.44 |
#include <sys/types.h> |
36 |
|
|
#include <sys/socket.h> |
37 |
|
|
#include <sys/ioctl.h> |
38 |
|
|
#include <net/if.h> |
39 |
|
|
#include <netinet/in.h> |
40 |
|
|
#include <sys/sockio.h> |
41 |
pajs |
1.6 |
#endif |
42 |
|
|
#ifdef LINUX |
43 |
|
|
#include <stdio.h> |
44 |
|
|
#include <sys/types.h> |
45 |
|
|
#include <regex.h> |
46 |
pajs |
1.27 |
#include <sys/ioctl.h> |
47 |
|
|
#include <sys/socket.h> |
48 |
|
|
#include <net/if.h> |
49 |
|
|
#include <ctype.h> |
50 |
pajs |
1.6 |
#include "tools.h" |
51 |
pajs |
1.27 |
/* Stuff which could be defined by defining KERNEL, but |
52 |
|
|
* that would be a bad idea, so we'll just declare it here |
53 |
|
|
*/ |
54 |
|
|
typedef __uint8_t u8; |
55 |
|
|
typedef __uint16_t u16; |
56 |
|
|
typedef __uint32_t u32; |
57 |
ats |
1.42 |
typedef __uint64_t u64; |
58 |
pajs |
1.27 |
#include <linux/ethtool.h> |
59 |
|
|
#include <linux/sockios.h> |
60 |
ats |
1.32 |
#include <unistd.h> |
61 |
pajs |
1.1 |
#endif |
62 |
ats |
1.18 |
#ifdef ALLBSD |
63 |
pajs |
1.14 |
#include <sys/types.h> |
64 |
|
|
#include <sys/socket.h> |
65 |
|
|
#include <ifaddrs.h> |
66 |
|
|
#include <net/if.h> |
67 |
pajs |
1.26 |
#include <net/if_media.h> |
68 |
|
|
#include <sys/ioctl.h> |
69 |
pajs |
1.14 |
#endif |
70 |
pajs |
1.1 |
|
71 |
pajs |
1.3 |
static network_stat_t *network_stats=NULL; |
72 |
|
|
static int interfaces=0; |
73 |
pajs |
1.1 |
|
74 |
pajs |
1.3 |
void network_stat_init(int start, int end, network_stat_t *net_stats){ |
75 |
|
|
|
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 |
tdb |
1.47 |
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 |
pajs |
1.3 |
net_stats++; |
86 |
|
|
} |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
network_stat_t *network_stat_malloc(int needed_entries, int *cur_entries, network_stat_t *net_stats){ |
90 |
|
|
|
91 |
|
|
if(net_stats==NULL){ |
92 |
|
|
|
93 |
|
|
if((net_stats=malloc(needed_entries * sizeof(network_stat_t)))==NULL){ |
94 |
pajs |
1.1 |
return NULL; |
95 |
|
|
} |
96 |
pajs |
1.3 |
network_stat_init(0, needed_entries, net_stats); |
97 |
|
|
*cur_entries=needed_entries; |
98 |
|
|
|
99 |
pajs |
1.1 |
return net_stats; |
100 |
|
|
} |
101 |
pajs |
1.3 |
|
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 |
pajs |
1.1 |
return NULL; |
107 |
|
|
} |
108 |
pajs |
1.3 |
network_stat_init(*cur_entries, needed_entries, net_stats); |
109 |
|
|
*cur_entries=needed_entries; |
110 |
pajs |
1.1 |
} |
111 |
|
|
|
112 |
|
|
return net_stats; |
113 |
|
|
} |
114 |
|
|
|
115 |
pajs |
1.3 |
|
116 |
pajs |
1.1 |
network_stat_t *get_network_stats(int *entries){ |
117 |
pajs |
1.6 |
|
118 |
|
|
static int sizeof_network_stats=0; |
119 |
|
|
network_stat_t *network_stat_ptr; |
120 |
|
|
|
121 |
|
|
#ifdef SOLARIS |
122 |
pajs |
1.1 |
kstat_ctl_t *kc; |
123 |
|
|
kstat_t *ksp; |
124 |
|
|
kstat_named_t *knp; |
125 |
pajs |
1.6 |
#endif |
126 |
pajs |
1.3 |
|
127 |
pajs |
1.6 |
#ifdef LINUX |
128 |
|
|
FILE *f; |
129 |
pajs |
1.12 |
/* Horrible big enough, but it should be easily big enough */ |
130 |
pajs |
1.6 |
char line[8096]; |
131 |
|
|
regex_t regex; |
132 |
|
|
regmatch_t line_match[4]; |
133 |
pajs |
1.14 |
#endif |
134 |
ats |
1.18 |
#ifdef ALLBSD |
135 |
pajs |
1.14 |
struct ifaddrs *net, *net_ptr; |
136 |
|
|
struct if_data *net_data; |
137 |
|
|
#endif |
138 |
|
|
|
139 |
ats |
1.18 |
#ifdef ALLBSD |
140 |
pajs |
1.14 |
if(getifaddrs(&net) != 0){ |
141 |
|
|
return NULL; |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
interfaces=0; |
145 |
|
|
|
146 |
|
|
for(net_ptr=net;net_ptr!=NULL;net_ptr=net_ptr->ifa_next){ |
147 |
|
|
if(net_ptr->ifa_addr->sa_family != AF_LINK) continue; |
148 |
|
|
network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats); |
149 |
|
|
if(network_stats==NULL){ |
150 |
|
|
return NULL; |
151 |
|
|
} |
152 |
|
|
network_stat_ptr=network_stats+interfaces; |
153 |
|
|
|
154 |
|
|
if(network_stat_ptr->interface_name!=NULL) free(network_stat_ptr->interface_name); |
155 |
|
|
network_stat_ptr->interface_name=strdup(net_ptr->ifa_name); |
156 |
|
|
if(network_stat_ptr->interface_name==NULL) return NULL; |
157 |
|
|
net_data=(struct if_data *)net_ptr->ifa_data; |
158 |
|
|
network_stat_ptr->rx=net_data->ifi_ibytes; |
159 |
tdb |
1.46 |
network_stat_ptr->tx=net_data->ifi_obytes; |
160 |
|
|
network_stat_ptr->ipackets=net_data->ifi_ipackets; |
161 |
|
|
network_stat_ptr->opackets=net_data->ifi_opackets; |
162 |
|
|
network_stat_ptr->ierrors=net_data->ifi_ierrors; |
163 |
|
|
network_stat_ptr->oerrors=net_data->ifi_oerrors; |
164 |
|
|
network_stat_ptr->collisions=net_data->ifi_collisions; |
165 |
pajs |
1.14 |
network_stat_ptr->systime=time(NULL); |
166 |
|
|
interfaces++; |
167 |
|
|
} |
168 |
|
|
freeifaddrs(net); |
169 |
pajs |
1.6 |
#endif |
170 |
pajs |
1.1 |
|
171 |
pajs |
1.6 |
#ifdef SOLARIS |
172 |
pajs |
1.1 |
if ((kc = kstat_open()) == NULL) { |
173 |
|
|
return NULL; |
174 |
|
|
} |
175 |
|
|
|
176 |
pajs |
1.6 |
interfaces=0; |
177 |
pajs |
1.3 |
|
178 |
pajs |
1.1 |
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
179 |
|
|
if (!strcmp(ksp->ks_class, "net")) { |
180 |
|
|
kstat_read(kc, ksp, NULL); |
181 |
|
|
|
182 |
pajs |
1.7 |
#ifdef SOL7 |
183 |
|
|
#define RLOOKUP "rbytes" |
184 |
|
|
#define WLOOKUP "obytes" |
185 |
|
|
#define VALTYPE value.ui32 |
186 |
|
|
#else |
187 |
|
|
#define RLOOKUP "rbytes64" |
188 |
|
|
#define WLOOKUP "obytes64" |
189 |
|
|
#define VALTYPE value.ui64 |
190 |
|
|
#endif |
191 |
|
|
|
192 |
|
|
if((knp=kstat_data_lookup(ksp, RLOOKUP))==NULL){ |
193 |
ats |
1.44 |
/* This is a network interface, but it doesn't |
194 |
|
|
* have the rbytes/obytes values; for instance, |
195 |
|
|
* the loopback devices have this behaviour |
196 |
|
|
* (although they do track packets in/out). */ |
197 |
pajs |
1.1 |
continue; |
198 |
|
|
} |
199 |
pajs |
1.3 |
|
200 |
|
|
network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats); |
201 |
pajs |
1.1 |
if(network_stats==NULL){ |
202 |
|
|
return NULL; |
203 |
|
|
} |
204 |
|
|
network_stat_ptr=network_stats+interfaces; |
205 |
pajs |
1.7 |
network_stat_ptr->rx=knp->VALTYPE; |
206 |
pajs |
1.1 |
|
207 |
pajs |
1.7 |
if((knp=kstat_data_lookup(ksp, WLOOKUP))==NULL){ |
208 |
pajs |
1.1 |
continue; |
209 |
|
|
} |
210 |
pajs |
1.7 |
network_stat_ptr->tx=knp->VALTYPE; |
211 |
pajs |
1.1 |
if(network_stat_ptr->interface_name!=NULL){ |
212 |
|
|
free(network_stat_ptr->interface_name); |
213 |
|
|
} |
214 |
|
|
network_stat_ptr->interface_name=strdup(ksp->ks_name); |
215 |
pajs |
1.3 |
|
216 |
|
|
network_stat_ptr->systime=time(NULL); |
217 |
pajs |
1.1 |
interfaces++; |
218 |
|
|
} |
219 |
|
|
} |
220 |
pajs |
1.2 |
|
221 |
|
|
kstat_close(kc); |
222 |
pajs |
1.6 |
#endif |
223 |
|
|
#ifdef LINUX |
224 |
|
|
f=fopen("/proc/net/dev", "r"); |
225 |
|
|
if(f==NULL){ |
226 |
|
|
return NULL; |
227 |
|
|
} |
228 |
|
|
/* read the 2 lines.. Its the title, so we dont care :) */ |
229 |
|
|
fgets(line, sizeof(line), f); |
230 |
|
|
fgets(line, sizeof(line), f); |
231 |
|
|
|
232 |
|
|
|
233 |
|
|
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){ |
234 |
|
|
return NULL; |
235 |
|
|
} |
236 |
|
|
|
237 |
|
|
interfaces=0; |
238 |
pajs |
1.1 |
|
239 |
pajs |
1.6 |
while((fgets(line, sizeof(line), f)) != NULL){ |
240 |
|
|
if((regexec(®ex, line, 4, line_match, 0))!=0){ |
241 |
|
|
continue; |
242 |
|
|
} |
243 |
|
|
network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats); |
244 |
|
|
if(network_stats==NULL){ |
245 |
|
|
return NULL; |
246 |
|
|
} |
247 |
|
|
network_stat_ptr=network_stats+interfaces; |
248 |
|
|
|
249 |
|
|
if(network_stat_ptr->interface_name!=NULL){ |
250 |
|
|
free(network_stat_ptr->interface_name); |
251 |
|
|
} |
252 |
|
|
|
253 |
|
|
network_stat_ptr->interface_name=get_string_match(line, &line_match[1]); |
254 |
|
|
network_stat_ptr->rx=get_ll_match(line, &line_match[2]); |
255 |
|
|
network_stat_ptr->tx=get_ll_match(line, &line_match[3]); |
256 |
|
|
network_stat_ptr->systime=time(NULL); |
257 |
|
|
|
258 |
|
|
interfaces++; |
259 |
|
|
} |
260 |
pajs |
1.12 |
fclose(f); |
261 |
pajs |
1.13 |
regfree(®ex); |
262 |
pajs |
1.6 |
|
263 |
|
|
#endif |
264 |
ats |
1.20 |
|
265 |
|
|
#ifdef CYGWIN |
266 |
|
|
return NULL; |
267 |
|
|
#endif |
268 |
|
|
|
269 |
pajs |
1.1 |
*entries=interfaces; |
270 |
|
|
|
271 |
|
|
return network_stats; |
272 |
pajs |
1.3 |
} |
273 |
|
|
|
274 |
pajs |
1.8 |
long long transfer_diff(long long new, long long old){ |
275 |
tdb |
1.45 |
#if defined(SOL7) || defined(LINUX) || defined(FREEBSD) || defined(DFBSD) |
276 |
ats |
1.19 |
#define MAXVAL 4294967296LL |
277 |
pajs |
1.8 |
#else |
278 |
ats |
1.19 |
#define MAXVAL 18446744073709551616LL |
279 |
pajs |
1.8 |
#endif |
280 |
|
|
long long result; |
281 |
pajs |
1.10 |
if(new>=old){ |
282 |
pajs |
1.8 |
result = (new-old); |
283 |
|
|
}else{ |
284 |
|
|
result = (MAXVAL+(new-old)); |
285 |
|
|
} |
286 |
|
|
|
287 |
|
|
return result; |
288 |
|
|
|
289 |
|
|
} |
290 |
|
|
|
291 |
ats |
1.24 |
network_stat_t *get_network_stats_diff(int *entries) { |
292 |
|
|
static network_stat_t *diff = NULL; |
293 |
|
|
static int diff_count = 0; |
294 |
|
|
network_stat_t *src, *dest; |
295 |
|
|
int i, j, new_count; |
296 |
|
|
|
297 |
|
|
if (network_stats == NULL) { |
298 |
|
|
/* No previous stats, so we can't calculate a difference. */ |
299 |
|
|
return get_network_stats(entries); |
300 |
pajs |
1.3 |
} |
301 |
|
|
|
302 |
ats |
1.24 |
/* Resize the results array to match the previous stats. */ |
303 |
|
|
diff = network_stat_malloc(interfaces, &diff_count, diff); |
304 |
|
|
if (diff == NULL) { |
305 |
pajs |
1.3 |
return NULL; |
306 |
|
|
} |
307 |
|
|
|
308 |
ats |
1.24 |
/* Copy the previous stats into the result. */ |
309 |
|
|
for (i = 0; i < diff_count; i++) { |
310 |
|
|
src = &network_stats[i]; |
311 |
|
|
dest = &diff[i]; |
312 |
pajs |
1.3 |
|
313 |
ats |
1.24 |
if (dest->interface_name != NULL) { |
314 |
|
|
free(dest->interface_name); |
315 |
pajs |
1.3 |
} |
316 |
ats |
1.24 |
dest->interface_name = strdup(src->interface_name); |
317 |
|
|
dest->rx = src->rx; |
318 |
|
|
dest->tx = src->tx; |
319 |
tdb |
1.47 |
dest->ipackets = src->ipackets; |
320 |
|
|
dest->opackets = src->opackets; |
321 |
|
|
dest->ierrors = src->ierrors; |
322 |
|
|
dest->oerrors = src->oerrors; |
323 |
|
|
dest->collisions = src->collisions; |
324 |
ats |
1.24 |
dest->systime = src->systime; |
325 |
|
|
} |
326 |
pajs |
1.3 |
|
327 |
ats |
1.24 |
/* Get a new set of stats. */ |
328 |
|
|
if (get_network_stats(&new_count) == NULL) { |
329 |
ats |
1.21 |
return NULL; |
330 |
|
|
} |
331 |
pajs |
1.3 |
|
332 |
ats |
1.24 |
/* For each previous stat... */ |
333 |
|
|
for (i = 0; i < diff_count; i++) { |
334 |
|
|
dest = &diff[i]; |
335 |
|
|
|
336 |
|
|
/* ... find the corresponding new stat ... */ |
337 |
|
|
for (j = 0; j < new_count; j++) { |
338 |
|
|
/* Try the new stat in the same position first, |
339 |
|
|
since that's most likely to be it. */ |
340 |
|
|
src = &network_stats[(i + j) % new_count]; |
341 |
|
|
if (strcmp(src->interface_name, dest->interface_name) == 0) { |
342 |
|
|
break; |
343 |
|
|
} |
344 |
|
|
} |
345 |
|
|
if (j == new_count) { |
346 |
|
|
/* No match found. */ |
347 |
|
|
continue; |
348 |
pajs |
1.3 |
} |
349 |
|
|
|
350 |
ats |
1.24 |
/* ... and subtract the previous stat from it to get the |
351 |
|
|
difference. */ |
352 |
|
|
dest->rx = transfer_diff(src->rx, dest->rx); |
353 |
|
|
dest->tx = transfer_diff(src->tx, dest->tx); |
354 |
tdb |
1.47 |
dest->ipackets = transfer_diff(src->ipackets, dest->ipackets); |
355 |
|
|
dest->opackets = transfer_diff(src->opackets, dest->opackets); |
356 |
|
|
dest->ierrors = transfer_diff(src->ierrors, dest->ierrors); |
357 |
|
|
dest->oerrors = transfer_diff(src->oerrors, dest->oerrors); |
358 |
|
|
dest->collisions = transfer_diff(src->collisions, dest->collisions); |
359 |
ats |
1.24 |
dest->systime = src->systime - dest->systime; |
360 |
pajs |
1.3 |
} |
361 |
|
|
|
362 |
ats |
1.24 |
*entries = diff_count; |
363 |
|
|
return diff; |
364 |
|
|
} |
365 |
pajs |
1.25 |
/* NETWORK INTERFACE STATS */ |
366 |
|
|
|
367 |
|
|
void network_iface_stat_init(int start, int end, network_iface_stat_t *net_stats){ |
368 |
|
|
|
369 |
|
|
for(net_stats+=start; start<end; start++){ |
370 |
|
|
net_stats->interface_name=NULL; |
371 |
|
|
net_stats->speed=0; |
372 |
ats |
1.31 |
net_stats->dup=UNKNOWN_DUPLEX; |
373 |
pajs |
1.25 |
net_stats++; |
374 |
|
|
} |
375 |
|
|
} |
376 |
|
|
|
377 |
|
|
network_iface_stat_t *network_iface_stat_malloc(int needed_entries, int *cur_entries, network_iface_stat_t *net_stats){ |
378 |
|
|
|
379 |
|
|
if(net_stats==NULL){ |
380 |
|
|
|
381 |
|
|
if((net_stats=malloc(needed_entries * sizeof(network_iface_stat_t)))==NULL){ |
382 |
|
|
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; |
401 |
|
|
} |
402 |
|
|
|
403 |
|
|
network_iface_stat_t *get_network_iface_stats(int *entries){ |
404 |
|
|
static network_iface_stat_t *network_iface_stats; |
405 |
|
|
network_iface_stat_t *network_iface_stat_ptr; |
406 |
|
|
static int sizeof_network_iface_stats=0; |
407 |
ats |
1.43 |
int ifaces = 0; |
408 |
pajs |
1.25 |
|
409 |
|
|
#ifdef SOLARIS |
410 |
|
|
kstat_ctl_t *kc; |
411 |
|
|
kstat_t *ksp; |
412 |
|
|
kstat_named_t *knp; |
413 |
ats |
1.44 |
int sock; |
414 |
pajs |
1.25 |
#endif |
415 |
pajs |
1.26 |
#ifdef ALLBSD |
416 |
|
|
struct ifaddrs *net, *net_ptr; |
417 |
|
|
struct ifmediareq ifmed; |
418 |
pajs |
1.36 |
struct ifreq ifr; |
419 |
ats |
1.37 |
int sock; |
420 |
pajs |
1.26 |
int x; |
421 |
|
|
#endif |
422 |
pajs |
1.27 |
#ifdef LINUX |
423 |
|
|
FILE *f; |
424 |
|
|
/* Horrible big enough, but it should be easily big enough */ |
425 |
|
|
char line[8096]; |
426 |
|
|
int sock; |
427 |
|
|
#endif |
428 |
ats |
1.43 |
|
429 |
pajs |
1.26 |
#ifdef ALLBSD |
430 |
|
|
if(getifaddrs(&net) != 0){ |
431 |
|
|
return NULL; |
432 |
|
|
} |
433 |
|
|
|
434 |
ats |
1.37 |
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return NULL; |
435 |
pajs |
1.26 |
|
436 |
|
|
for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){ |
437 |
|
|
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; |
443 |
|
|
|
444 |
ats |
1.40 |
memset(&ifr, 0, sizeof(ifr)); |
445 |
|
|
strncpy(ifr.ifr_name, net_ptr->ifa_name, sizeof(ifr.ifr_name)); |
446 |
|
|
|
447 |
|
|
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0){ |
448 |
|
|
continue; |
449 |
|
|
} |
450 |
|
|
if((ifr.ifr_flags & IFF_UP) != 0){ |
451 |
|
|
network_iface_stat_ptr->up = 1; |
452 |
|
|
}else{ |
453 |
|
|
network_iface_stat_ptr->up = 0; |
454 |
|
|
} |
455 |
|
|
|
456 |
|
|
if (network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name); |
457 |
|
|
network_iface_stat_ptr->interface_name = strdup(net_ptr->ifa_name); |
458 |
|
|
if (network_iface_stat_ptr->interface_name == NULL) return NULL; |
459 |
|
|
|
460 |
|
|
network_iface_stat_ptr->speed = 0; |
461 |
|
|
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
462 |
|
|
ifaces++; |
463 |
|
|
|
464 |
pajs |
1.26 |
memset(&ifmed, 0, sizeof(struct ifmediareq)); |
465 |
|
|
strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name)); |
466 |
ats |
1.37 |
if(ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){ |
467 |
ats |
1.40 |
/* Not all interfaces support the media ioctls. */ |
468 |
pajs |
1.26 |
continue; |
469 |
|
|
} |
470 |
|
|
|
471 |
|
|
/* We may need to change this if we start doing wireless devices too */ |
472 |
|
|
if( (ifmed.ifm_active | IFM_ETHER) != ifmed.ifm_active ){ |
473 |
|
|
/* Not a ETHER device */ |
474 |
|
|
continue; |
475 |
|
|
} |
476 |
|
|
|
477 |
|
|
/* Only intrested in the first 4 bits) - Assuming only ETHER devices */ |
478 |
|
|
x = ifmed.ifm_active & 0x0f; |
479 |
|
|
switch(x){ |
480 |
|
|
/* 10 Mbit connections. Speedy :) */ |
481 |
|
|
case(IFM_10_T): |
482 |
|
|
case(IFM_10_2): |
483 |
|
|
case(IFM_10_5): |
484 |
|
|
case(IFM_10_STP): |
485 |
|
|
case(IFM_10_FL): |
486 |
|
|
network_iface_stat_ptr->speed = 10; |
487 |
|
|
break; |
488 |
|
|
/* 100 Mbit conneections */ |
489 |
|
|
case(IFM_100_TX): |
490 |
|
|
case(IFM_100_FX): |
491 |
|
|
case(IFM_100_T4): |
492 |
|
|
case(IFM_100_VG): |
493 |
|
|
case(IFM_100_T2): |
494 |
|
|
network_iface_stat_ptr->speed = 100; |
495 |
|
|
break; |
496 |
|
|
/* 1000 Mbit connections */ |
497 |
|
|
case(IFM_1000_SX): |
498 |
|
|
case(IFM_1000_LX): |
499 |
|
|
case(IFM_1000_CX): |
500 |
tdb |
1.45 |
#ifdef IFM_1000_TX |
501 |
|
|
case(IFM_1000_TX): /* FreeBSD 4 and others? */ |
502 |
|
|
#endif |
503 |
|
|
#ifdef IFM_1000_FX |
504 |
|
|
case(IFM_1000_FX): /* FreeBSD 4 */ |
505 |
|
|
#endif |
506 |
|
|
#ifdef IFM_1000_T |
507 |
|
|
case(IFM_1000_T): /* FreeBSD 5 */ |
508 |
tdb |
1.28 |
#endif |
509 |
pajs |
1.26 |
network_iface_stat_ptr->speed = 1000; |
510 |
|
|
break; |
511 |
|
|
/* We don't know what it is */ |
512 |
|
|
default: |
513 |
|
|
network_iface_stat_ptr->speed = 0; |
514 |
|
|
break; |
515 |
|
|
} |
516 |
|
|
|
517 |
|
|
if( (ifmed.ifm_active | IFM_FDX) == ifmed.ifm_active ){ |
518 |
|
|
network_iface_stat_ptr->dup = FULL_DUPLEX; |
519 |
|
|
}else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){ |
520 |
|
|
network_iface_stat_ptr->dup = HALF_DUPLEX; |
521 |
|
|
}else{ |
522 |
ats |
1.31 |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
523 |
pajs |
1.26 |
} |
524 |
pajs |
1.36 |
|
525 |
pajs |
1.26 |
} |
526 |
|
|
freeifaddrs(net); |
527 |
ats |
1.37 |
close(sock); |
528 |
pajs |
1.26 |
#endif |
529 |
pajs |
1.25 |
|
530 |
|
|
#ifdef SOLARIS |
531 |
ats |
1.44 |
if ((kc = kstat_open()) == NULL) { |
532 |
|
|
return NULL; |
533 |
|
|
} |
534 |
|
|
|
535 |
|
|
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) { |
536 |
|
|
return NULL; |
537 |
|
|
} |
538 |
pajs |
1.25 |
|
539 |
ats |
1.44 |
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
540 |
|
|
if (!strcmp(ksp->ks_class, "net")) { |
541 |
|
|
struct ifreq ifr; |
542 |
|
|
|
543 |
|
|
kstat_read(kc, ksp, NULL); |
544 |
|
|
|
545 |
|
|
strncpy(ifr.ifr_name, ksp->ks_name, sizeof ifr.ifr_name); |
546 |
|
|
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) { |
547 |
|
|
/* Not a network interface. */ |
548 |
pajs |
1.25 |
continue; |
549 |
|
|
} |
550 |
ats |
1.44 |
|
551 |
|
|
network_iface_stats = network_iface_stat_malloc(ifaces + 1, &sizeof_network_iface_stats, network_iface_stats); |
552 |
|
|
if (network_iface_stats == NULL) { |
553 |
pajs |
1.25 |
return NULL; |
554 |
|
|
} |
555 |
pajs |
1.26 |
network_iface_stat_ptr = network_iface_stats + ifaces; |
556 |
ats |
1.44 |
ifaces++; |
557 |
|
|
|
558 |
|
|
if (network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name); |
559 |
|
|
network_iface_stat_ptr->interface_name = strdup(ksp->ks_name); |
560 |
|
|
if (network_iface_stat_ptr->interface_name == NULL) return NULL; |
561 |
pajs |
1.25 |
|
562 |
ats |
1.44 |
if ((ifr.ifr_flags & IFF_UP) != 0) { |
563 |
|
|
network_iface_stat_ptr->up = 1; |
564 |
|
|
} else { |
565 |
|
|
network_iface_stat_ptr->up = 1; |
566 |
|
|
} |
567 |
pajs |
1.35 |
|
568 |
ats |
1.44 |
if ((knp = kstat_data_lookup(ksp, "ifspeed")) != NULL) { |
569 |
|
|
network_iface_stat_ptr->speed = knp->value.ui64 / (1000 * 1000); |
570 |
|
|
} else { |
571 |
|
|
network_iface_stat_ptr->speed = 0; |
572 |
pajs |
1.25 |
} |
573 |
|
|
|
574 |
pajs |
1.33 |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
575 |
ats |
1.44 |
if ((knp = kstat_data_lookup(ksp, "link_duplex")) != NULL) { |
576 |
|
|
switch (knp->value.ui32) { |
577 |
|
|
case 1: |
578 |
|
|
network_iface_stat_ptr->dup = HALF_DUPLEX; |
579 |
|
|
break; |
580 |
|
|
case 2: |
581 |
|
|
network_iface_stat_ptr->dup = FULL_DUPLEX; |
582 |
|
|
break; |
583 |
|
|
} |
584 |
pajs |
1.33 |
} |
585 |
pajs |
1.25 |
} |
586 |
|
|
} |
587 |
ats |
1.44 |
|
588 |
|
|
close(sock); |
589 |
pajs |
1.25 |
kstat_close(kc); |
590 |
|
|
#endif |
591 |
pajs |
1.27 |
#ifdef LINUX |
592 |
|
|
f = fopen("/proc/net/dev", "r"); |
593 |
|
|
if(f == NULL){ |
594 |
|
|
return NULL; |
595 |
|
|
} |
596 |
|
|
|
597 |
|
|
/* Setup stuff so we can do the ioctl to get the info */ |
598 |
|
|
if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){ |
599 |
|
|
return NULL; |
600 |
|
|
} |
601 |
|
|
|
602 |
|
|
/* Ignore first 2 lines.. Just headings */ |
603 |
pajs |
1.29 |
if((fgets(line, sizeof(line), f)) == NULL) return NULL; |
604 |
|
|
if((fgets(line, sizeof(line), f)) == NULL) return NULL; |
605 |
pajs |
1.27 |
|
606 |
|
|
while((fgets(line, sizeof(line), f)) != NULL){ |
607 |
|
|
char *name, *ptr; |
608 |
|
|
struct ifreq ifr; |
609 |
ats |
1.38 |
struct ethtool_cmd ethcmd; |
610 |
pajs |
1.27 |
int err; |
611 |
|
|
|
612 |
|
|
/* Get the interface name */ |
613 |
|
|
ptr = strchr(line, ':'); |
614 |
|
|
if (ptr == NULL) continue; |
615 |
|
|
*ptr='\0'; |
616 |
|
|
name = line; |
617 |
|
|
while(isspace(*(name))){ |
618 |
|
|
name++; |
619 |
|
|
} |
620 |
|
|
|
621 |
ats |
1.38 |
memset(&ifr, 0, sizeof ifr); |
622 |
|
|
strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); |
623 |
pajs |
1.27 |
|
624 |
ats |
1.38 |
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) { |
625 |
|
|
continue; |
626 |
|
|
} |
627 |
pajs |
1.27 |
|
628 |
|
|
/* We have a good interface to add */ |
629 |
|
|
network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats); |
630 |
|
|
if(network_iface_stats==NULL){ |
631 |
|
|
return NULL; |
632 |
|
|
} |
633 |
|
|
network_iface_stat_ptr = network_iface_stats + ifaces; |
634 |
|
|
network_iface_stat_ptr->interface_name = strdup(name); |
635 |
ats |
1.38 |
if ((ifr.ifr_flags & IFF_UP) != 0) { |
636 |
pajs |
1.35 |
network_iface_stat_ptr->up = 1; |
637 |
ats |
1.38 |
} else { |
638 |
pajs |
1.35 |
network_iface_stat_ptr->up = 0; |
639 |
|
|
} |
640 |
|
|
|
641 |
ats |
1.38 |
memset(ðcmd, 0, sizeof ethcmd); |
642 |
|
|
ethcmd.cmd = ETHTOOL_GSET; |
643 |
|
|
ifr.ifr_data = (caddr_t) ðcmd; |
644 |
|
|
|
645 |
|
|
err = ioctl(sock, SIOCETHTOOL, &ifr); |
646 |
|
|
if (err == 0) { |
647 |
|
|
network_iface_stat_ptr->speed = ethcmd.speed; |
648 |
|
|
|
649 |
|
|
switch (ethcmd.duplex) { |
650 |
|
|
case 0x00: |
651 |
|
|
network_iface_stat_ptr->dup = FULL_DUPLEX; |
652 |
|
|
break; |
653 |
|
|
case 0x01: |
654 |
|
|
network_iface_stat_ptr->dup = HALF_DUPLEX; |
655 |
|
|
break; |
656 |
|
|
default: |
657 |
|
|
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
658 |
|
|
} |
659 |
|
|
} else { |
660 |
|
|
/* Not all interfaces support the ethtool ioctl. */ |
661 |
ats |
1.39 |
network_iface_stat_ptr->speed = 0; |
662 |
ats |
1.38 |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
663 |
pajs |
1.27 |
} |
664 |
ats |
1.38 |
|
665 |
pajs |
1.27 |
ifaces++; |
666 |
|
|
} |
667 |
pajs |
1.30 |
close(sock); |
668 |
ats |
1.38 |
fclose(f); |
669 |
pajs |
1.27 |
#endif |
670 |
pajs |
1.26 |
*entries = ifaces; |
671 |
pajs |
1.25 |
return network_iface_stats; |
672 |
|
|
} |
673 |
|
|
|