49 |
|
#include <sys/socket.h> |
50 |
|
#include <net/if.h> |
51 |
|
#include <ctype.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 |
– |
*/ |
52 |
|
#include <linux/version.h> |
53 |
< |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0) |
54 |
< |
typedef __uint8_t __u8; |
55 |
< |
typedef __uint16_t __u16; |
59 |
< |
typedef __uint32_t __u32; |
60 |
< |
typedef __uint64_t __u64; |
61 |
< |
#else |
53 |
> |
#include <asm/types.h> |
54 |
> |
/* These aren't defined by asm/types.h unless the kernel is being |
55 |
> |
compiled, but some versions of ethtool.h need them. */ |
56 |
|
typedef __uint8_t u8; |
57 |
|
typedef __uint16_t u16; |
58 |
|
typedef __uint32_t u32; |
59 |
|
typedef __uint64_t u64; |
66 |
– |
#endif |
60 |
|
#include <linux/ethtool.h> |
61 |
|
#include <linux/sockios.h> |
62 |
|
#include <unistd.h> |
70 |
|
#include <sys/ioctl.h> |
71 |
|
#include <unistd.h> |
72 |
|
#endif |
73 |
+ |
#ifdef AIX |
74 |
+ |
#include <sys/types.h> |
75 |
+ |
#include <sys/socket.h> |
76 |
+ |
#include <net/if.h> |
77 |
+ |
#include <errno.h> |
78 |
+ |
#include <strings.h> |
79 |
+ |
#include <sys/ioctl.h> |
80 |
+ |
#include <unistd.h> |
81 |
+ |
#include <libperfstat.h> |
82 |
+ |
#endif |
83 |
+ |
#ifdef HPUX |
84 |
+ |
#include <fcntl.h> |
85 |
+ |
#include <sys/types.h> |
86 |
+ |
#include <sys/socket.h> |
87 |
+ |
#include <net/if.h> |
88 |
+ |
#include <errno.h> |
89 |
+ |
#include <strings.h> |
90 |
+ |
#include <sys/ioctl.h> |
91 |
+ |
#include <unistd.h> |
92 |
+ |
#include <sys/un.h> |
93 |
+ |
#include <sys/stropts.h> |
94 |
+ |
#include <sys/dlpi.h> |
95 |
+ |
#include <sys/dlpi_ext.h> |
96 |
+ |
#include <sys/mib.h> |
97 |
+ |
#define HP_UX11 /* Currently no HP-UX below 11 is supported, fix here if this changes */ |
98 |
+ |
#endif |
99 |
+ |
#ifdef WIN32 |
100 |
+ |
#include <windows.h> |
101 |
+ |
#include <Iphlpapi.h> |
102 |
+ |
#include "win32.h" |
103 |
+ |
#endif |
104 |
|
|
105 |
|
static void network_stat_init(sg_network_io_stats *s) { |
106 |
|
s->interface_name = NULL; |
120 |
|
VECTOR_DECLARE_STATIC(network_stats, sg_network_io_stats, 5, |
121 |
|
network_stat_init, network_stat_destroy); |
122 |
|
|
123 |
+ |
#ifdef WIN32 |
124 |
+ |
static PMIB_IFTABLE win32_get_devices() |
125 |
+ |
{ |
126 |
+ |
PMIB_IFTABLE if_table; |
127 |
+ |
PMIB_IFTABLE tmp; |
128 |
+ |
unsigned long dwSize = 0; |
129 |
+ |
|
130 |
+ |
// Allocate memory for pointers |
131 |
+ |
if_table = sg_malloc(sizeof(MIB_IFTABLE)); |
132 |
+ |
if(if_table == NULL) { |
133 |
+ |
return NULL; |
134 |
+ |
} |
135 |
+ |
|
136 |
+ |
// Get necessary size for the buffer |
137 |
+ |
if(GetIfTable(if_table, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) { |
138 |
+ |
tmp = sg_realloc(if_table, dwSize); |
139 |
+ |
if(tmp == NULL) { |
140 |
+ |
free(if_table); |
141 |
+ |
return NULL; |
142 |
+ |
} |
143 |
+ |
if_table = tmp; |
144 |
+ |
} |
145 |
+ |
|
146 |
+ |
// Get the data |
147 |
+ |
if(GetIfTable(if_table, &dwSize, 0) != NO_ERROR) { |
148 |
+ |
free(if_table); |
149 |
+ |
return NULL; |
150 |
+ |
} |
151 |
+ |
return if_table; |
152 |
+ |
} |
153 |
+ |
#endif /* WIN32 */ |
154 |
+ |
|
155 |
|
sg_network_io_stats *sg_get_network_io_stats(int *entries){ |
156 |
|
int interfaces; |
157 |
+ |
#ifndef AIX |
158 |
|
sg_network_io_stats *network_stat_ptr; |
159 |
+ |
#endif |
160 |
+ |
#ifdef HPUX |
161 |
+ |
/* |
162 |
+ |
* talk to Data Link Provider Interface aka /dev/dlpi |
163 |
+ |
* see: http://docs.hp.com/hpux/onlinedocs/B2355-90139/B2355-90139.html |
164 |
+ |
*/ |
165 |
+ |
#define BUF_SIZE 40960 |
166 |
|
|
167 |
+ |
u_long *ctrl_area = malloc(BUF_SIZE); |
168 |
+ |
u_long *data_area = malloc(BUF_SIZE); |
169 |
+ |
u_long *ppa_area = malloc(BUF_SIZE); |
170 |
+ |
struct strbuf ctrl_buf = {BUF_SIZE, 0, (char*) ctrl_area}; |
171 |
+ |
struct strbuf data_buf = {BUF_SIZE, 0, (char*) data_area}; |
172 |
+ |
|
173 |
+ |
dl_hp_ppa_info_t *ppa_info; |
174 |
+ |
dl_hp_ppa_req_t *ppa_req; |
175 |
+ |
dl_hp_ppa_ack_t *ppa_ack; |
176 |
+ |
char name_buf[24]; |
177 |
+ |
int fd = -1, ppa_count, count, flags; |
178 |
+ |
#endif |
179 |
+ |
|
180 |
|
#ifdef SOLARIS |
181 |
|
kstat_ctl_t *kc; |
182 |
|
kstat_t *ksp; |
194 |
|
struct ifaddrs *net, *net_ptr; |
195 |
|
struct if_data *net_data; |
196 |
|
#endif |
197 |
+ |
#ifdef WIN32 |
198 |
+ |
PMIB_IFTABLE if_table; |
199 |
+ |
MIB_IFROW if_row; |
200 |
+ |
int i, no, j; |
201 |
|
|
202 |
+ |
/* used for duplicate interface names. 5 for space, hash, up to two |
203 |
+ |
* numbers and terminating slash */ |
204 |
+ |
char buf[5]; |
205 |
+ |
#endif |
206 |
+ |
#ifdef AIX |
207 |
+ |
int i; |
208 |
+ |
perfstat_netinterface_t *statp; |
209 |
+ |
perfstat_id_t first; |
210 |
+ |
#endif |
211 |
+ |
|
212 |
+ |
#ifdef HPUX |
213 |
+ |
interfaces=0; |
214 |
+ |
|
215 |
+ |
if ((fd = open("/dev/dlpi", O_RDWR)) < 0) { |
216 |
+ |
sg_set_error_with_errno(SG_ERROR_OPEN, "/dev/dlpi"); |
217 |
+ |
return NULL; |
218 |
+ |
} |
219 |
+ |
|
220 |
+ |
ppa_req = (dl_hp_ppa_req_t *) ctrl_area; |
221 |
+ |
ppa_ack = (dl_hp_ppa_ack_t *) ctrl_area; |
222 |
+ |
|
223 |
+ |
ppa_req->dl_primitive = DL_HP_PPA_REQ; |
224 |
+ |
|
225 |
+ |
ctrl_buf.len = sizeof(dl_hp_ppa_req_t); |
226 |
+ |
if(putmsg(fd, &ctrl_buf, 0, 0) < 0) { |
227 |
+ |
sg_set_error_with_errno(SG_ERROR_PUTMSG, "DL_HP_PPA_REQ"); |
228 |
+ |
return NULL; |
229 |
+ |
} |
230 |
+ |
|
231 |
+ |
flags = 0; |
232 |
+ |
ctrl_area[0] = 0; |
233 |
+ |
|
234 |
+ |
if (getmsg(fd, &ctrl_buf, &data_buf, &flags) < 0) { |
235 |
+ |
sg_set_error_with_errno(SG_ERROR_GETMSG, "DL_HP_PPA_REQ"); |
236 |
+ |
return NULL; |
237 |
+ |
} |
238 |
+ |
|
239 |
+ |
if (ppa_ack->dl_length == 0) { |
240 |
+ |
sg_set_error_with_errno(SG_ERROR_PARSE, "DL_HP_PPA_REQ"); |
241 |
+ |
return NULL; |
242 |
+ |
} |
243 |
+ |
|
244 |
+ |
// save all the PPA information |
245 |
+ |
memcpy ((u_char*) ppa_area, (u_char*) ctrl_area + ppa_ack->dl_offset, ppa_ack->dl_length); |
246 |
+ |
ppa_count = ppa_ack->dl_count; |
247 |
+ |
|
248 |
+ |
for (count = 0, ppa_info = (dl_hp_ppa_info_t*) ppa_area; |
249 |
+ |
count < ppa_count; |
250 |
+ |
count++) { |
251 |
+ |
dl_get_statistics_req_t *get_statistics_req = (dl_get_statistics_req_t*) ctrl_area; |
252 |
+ |
dl_get_statistics_ack_t *get_statistics_ack = (dl_get_statistics_ack_t*) ctrl_area; |
253 |
+ |
|
254 |
+ |
dl_attach_req_t *attach_req; |
255 |
+ |
dl_detach_req_t *detach_req; |
256 |
+ |
mib_ifEntry *mib_ptr; |
257 |
+ |
#ifdef HP_UX11 |
258 |
+ |
mib_Dot3StatsEntry *mib_Dot3_ptr; |
259 |
+ |
#endif |
260 |
+ |
|
261 |
+ |
attach_req = (dl_attach_req_t*) ctrl_area; |
262 |
+ |
attach_req->dl_primitive = DL_ATTACH_REQ; |
263 |
+ |
attach_req->dl_ppa = ppa_info[count].dl_ppa; |
264 |
+ |
ctrl_buf.len = sizeof(dl_attach_req_t); |
265 |
+ |
if (putmsg(fd, &ctrl_buf, 0, 0) < 0) { |
266 |
+ |
sg_set_error_with_errno(SG_ERROR_PUTMSG, "DL_ATTACH_REQ"); |
267 |
+ |
return NULL; |
268 |
+ |
} |
269 |
+ |
|
270 |
+ |
ctrl_area[0] = 0; |
271 |
+ |
if (getmsg(fd, &ctrl_buf, &data_buf, &flags) < 0) { |
272 |
+ |
sg_set_error_with_errno(SG_ERROR_GETMSG, "DL_ATTACH_REQ"); |
273 |
+ |
return NULL; |
274 |
+ |
} |
275 |
+ |
|
276 |
+ |
get_statistics_req->dl_primitive = DL_GET_STATISTICS_REQ; |
277 |
+ |
ctrl_buf.len = sizeof(dl_get_statistics_req_t); |
278 |
+ |
|
279 |
+ |
if (putmsg(fd, &ctrl_buf, NULL, 0) < 0) { |
280 |
+ |
sg_set_error_with_errno(SG_ERROR_PUTMSG, "DL_GET_STATISTICS_REQ"); |
281 |
+ |
return NULL; |
282 |
+ |
} |
283 |
+ |
|
284 |
+ |
flags = 0; |
285 |
+ |
ctrl_area[0] = 0; |
286 |
+ |
|
287 |
+ |
if (getmsg(fd, &ctrl_buf, NULL, &flags) < 0) { |
288 |
+ |
sg_set_error_with_errno(SG_ERROR_GETMSG, "DL_GET_STATISTICS_REQ"); |
289 |
+ |
return NULL; |
290 |
+ |
} |
291 |
+ |
if (get_statistics_ack->dl_primitive != DL_GET_STATISTICS_ACK) { |
292 |
+ |
sg_set_error_with_errno(SG_ERROR_PARSE, "DL_GET_STATISTICS_ACK"); |
293 |
+ |
return NULL; |
294 |
+ |
} |
295 |
+ |
|
296 |
+ |
mib_ptr = (mib_ifEntry*) ((u_char*) ctrl_area + get_statistics_ack->dl_stat_offset); |
297 |
+ |
|
298 |
+ |
if (0 == (mib_ptr->ifOper & 1)) { |
299 |
+ |
continue; |
300 |
+ |
} |
301 |
+ |
|
302 |
+ |
if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) { |
303 |
+ |
return NULL; |
304 |
+ |
} |
305 |
+ |
network_stat_ptr=network_stats+interfaces; |
306 |
+ |
|
307 |
+ |
snprintf( name_buf, sizeof(name_buf), "lan%d", ppa_info[count].dl_ppa ); |
308 |
+ |
|
309 |
+ |
if (sg_update_string(&network_stat_ptr->interface_name, name_buf ) < 0 ) { |
310 |
+ |
return NULL; |
311 |
+ |
} |
312 |
+ |
network_stat_ptr->rx = mib_ptr->ifInOctets; |
313 |
+ |
network_stat_ptr->tx = mib_ptr->ifOutOctets; |
314 |
+ |
network_stat_ptr->ipackets = mib_ptr->ifInUcastPkts + mib_ptr->ifInNUcastPkts; |
315 |
+ |
network_stat_ptr->opackets = mib_ptr->ifOutUcastPkts + mib_ptr->ifOutNUcastPkts; |
316 |
+ |
network_stat_ptr->ierrors = mib_ptr->ifInErrors; |
317 |
+ |
network_stat_ptr->oerrors = mib_ptr->ifOutErrors; |
318 |
+ |
#ifdef HP_UX11 |
319 |
+ |
mib_Dot3_ptr = (mib_Dot3StatsEntry *) (void *) ((int) mib_ptr + sizeof (mib_ifEntry)); |
320 |
+ |
network_stat_ptr->collisions = mib_Dot3_ptr->dot3StatsSingleCollisionFrames |
321 |
+ |
+ mib_Dot3_ptr->dot3StatsMultipleCollisionFrames; |
322 |
+ |
#else |
323 |
+ |
network_stat_ptr->collisions = 0; /* currently unknown */ |
324 |
+ |
#endif |
325 |
+ |
network_stat_ptr->systime = time(NULL); |
326 |
+ |
interfaces++; |
327 |
+ |
|
328 |
+ |
detach_req = (dl_detach_req_t*) ctrl_area; |
329 |
+ |
detach_req->dl_primitive = DL_DETACH_REQ; |
330 |
+ |
ctrl_buf.len = sizeof(dl_detach_req_t); |
331 |
+ |
if (putmsg(fd, &ctrl_buf, 0, 0) < 0) { |
332 |
+ |
sg_set_error_with_errno(SG_ERROR_PUTMSG, "DL_DETACH_REQ"); |
333 |
+ |
return NULL; |
334 |
+ |
} |
335 |
+ |
|
336 |
+ |
ctrl_area[0] = 0; |
337 |
+ |
flags = 0; |
338 |
+ |
if (getmsg(fd, &ctrl_buf, &data_buf, &flags) < 0) { |
339 |
+ |
sg_set_error_with_errno(SG_ERROR_GETMSG, "DL_DETACH_REQ"); |
340 |
+ |
return NULL; |
341 |
+ |
} |
342 |
+ |
} |
343 |
+ |
|
344 |
+ |
close(fd); |
345 |
+ |
|
346 |
+ |
free(ctrl_area); |
347 |
+ |
free(data_area); |
348 |
+ |
free(ppa_area); |
349 |
+ |
|
350 |
+ |
#endif |
351 |
+ |
|
352 |
|
#ifdef ALLBSD |
353 |
< |
if(getifaddrs(&net) != 0){ |
353 |
> |
if (getifaddrs(&net) != 0){ |
354 |
|
sg_set_error_with_errno(SG_ERROR_GETIFADDRS, NULL); |
355 |
|
return NULL; |
356 |
|
} |
358 |
|
interfaces=0; |
359 |
|
|
360 |
|
for(net_ptr=net;net_ptr!=NULL;net_ptr=net_ptr->ifa_next){ |
361 |
< |
if(net_ptr->ifa_addr->sa_family != AF_LINK) continue; |
361 |
> |
if (net_ptr->ifa_addr->sa_family != AF_LINK) continue; |
362 |
|
|
363 |
|
if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) { |
364 |
|
return NULL; |
382 |
|
} |
383 |
|
freeifaddrs(net); |
384 |
|
#endif |
385 |
+ |
#ifdef AIX |
386 |
+ |
/* check how many perfstat_netinterface_t structures are available */ |
387 |
+ |
interfaces = perfstat_netinterface(NULL, NULL, sizeof(perfstat_netinterface_t), 0); |
388 |
|
|
389 |
+ |
/* allocate enough memory for all the structures */ |
390 |
+ |
statp = calloc(interfaces, sizeof(perfstat_netinterface_t)); |
391 |
+ |
|
392 |
+ |
/* set name to first interface */ |
393 |
+ |
strcpy(first.name, FIRST_NETINTERFACE); |
394 |
+ |
|
395 |
+ |
/* ask to get all the structures available in one call */ |
396 |
+ |
/* return code is number of structures returned */ |
397 |
+ |
interfaces = perfstat_netinterface(&first, statp, sizeof(perfstat_netinterface_t), interfaces); |
398 |
+ |
if (-1 == interfaces) { |
399 |
+ |
sg_set_error(SG_ERROR_SYSCTLBYNAME, "perfstat_netinterface"); |
400 |
+ |
free(statp); |
401 |
+ |
return NULL; |
402 |
+ |
} |
403 |
+ |
|
404 |
+ |
if (VECTOR_RESIZE(network_stats, interfaces) < 0) { |
405 |
+ |
sg_set_error(SG_ERROR_MALLOC, NULL); |
406 |
+ |
free(statp); |
407 |
+ |
return NULL; |
408 |
+ |
} |
409 |
+ |
|
410 |
+ |
/* print statistics for each of the interfaces */ |
411 |
+ |
for (i = 0; i < interfaces; i++) { |
412 |
+ |
if (sg_update_string(&network_stats[i].interface_name, statp[i].name) < 0) { |
413 |
+ |
free(statp); |
414 |
+ |
return NULL; |
415 |
+ |
} |
416 |
+ |
network_stats[i].tx = statp[i].obytes; |
417 |
+ |
network_stats[i].rx = statp[i].ibytes; |
418 |
+ |
network_stats[i].opackets = statp[i].opackets; |
419 |
+ |
network_stats[i].ipackets = statp[i].ipackets; |
420 |
+ |
network_stats[i].oerrors = statp[i].oerrors; |
421 |
+ |
network_stats[i].ierrors = statp[i].ierrors; |
422 |
+ |
network_stats[i].collisions = statp[i].collisions; |
423 |
+ |
network_stats[i].systime = time(NULL); |
424 |
+ |
} |
425 |
+ |
|
426 |
+ |
free(statp); |
427 |
+ |
#endif |
428 |
|
#ifdef SOLARIS |
429 |
|
if ((kc = kstat_open()) == NULL) { |
430 |
|
sg_set_error(SG_ERROR_KSTAT_OPEN, NULL); |
434 |
|
interfaces=0; |
435 |
|
|
436 |
|
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
437 |
< |
if (!strcmp(ksp->ks_class, "net")) { |
437 |
> |
if (strcmp(ksp->ks_class, "net") == 0) { |
438 |
|
kstat_read(kc, ksp, NULL); |
439 |
|
|
440 |
|
#ifdef SOL7 |
464 |
|
|
465 |
|
/* Create new network_stats */ |
466 |
|
if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) { |
467 |
+ |
kstat_close(kc); |
468 |
|
return NULL; |
469 |
|
} |
470 |
|
network_stat_ptr=network_stats+interfaces; |
471 |
|
|
472 |
+ |
/* Read interface name */ |
473 |
+ |
if (sg_update_string(&network_stat_ptr->interface_name, |
474 |
+ |
ksp->ks_name) < 0) { |
475 |
+ |
kstat_close(kc); |
476 |
+ |
return NULL; |
477 |
+ |
} |
478 |
+ |
|
479 |
|
/* Finish reading rx */ |
480 |
|
network_stat_ptr->rx=knp->VALTYPE; |
481 |
|
|
515 |
|
} |
516 |
|
network_stat_ptr->collisions=knp->value.ui32; |
517 |
|
|
237 |
– |
/* Read interface name */ |
238 |
– |
if (sg_update_string(&network_stat_ptr->interface_name, |
239 |
– |
ksp->ks_name) < 0) { |
240 |
– |
return NULL; |
241 |
– |
} |
518 |
|
|
519 |
|
/* Store systime */ |
520 |
|
network_stat_ptr->systime=time(NULL); |
578 |
|
sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin"); |
579 |
|
return NULL; |
580 |
|
#endif |
581 |
< |
#ifdef HPUX |
582 |
< |
sg_set_error(SG_ERROR_UNSUPPORTED, "HP-UX"); |
583 |
< |
return NULL; |
581 |
> |
|
582 |
> |
#ifdef WIN32 |
583 |
> |
interfaces = 0; |
584 |
> |
|
585 |
> |
if((if_table = win32_get_devices()) == NULL) { |
586 |
> |
sg_set_error(SG_ERROR_DEVICES, "network"); |
587 |
> |
return NULL; |
588 |
> |
} |
589 |
> |
|
590 |
> |
if(VECTOR_RESIZE(network_stats, if_table->dwNumEntries) < 0) { |
591 |
> |
free(if_table); |
592 |
> |
return NULL; |
593 |
> |
} |
594 |
> |
|
595 |
> |
for (i=0; i<if_table->dwNumEntries; i++) { |
596 |
> |
network_stat_ptr=network_stats+i; |
597 |
> |
if_row = if_table->table[i]; |
598 |
> |
|
599 |
> |
if(sg_update_string(&network_stat_ptr->interface_name, |
600 |
> |
if_row.bDescr) < 0) { |
601 |
> |
free(if_table); |
602 |
> |
return NULL; |
603 |
> |
} |
604 |
> |
network_stat_ptr->tx = if_row.dwOutOctets; |
605 |
> |
network_stat_ptr->rx = if_row.dwInOctets; |
606 |
> |
network_stat_ptr->ipackets = if_row.dwInUcastPkts + if_row.dwInNUcastPkts; |
607 |
> |
network_stat_ptr->opackets = if_row.dwOutUcastPkts + if_row.dwOutNUcastPkts; |
608 |
> |
network_stat_ptr->ierrors = if_row.dwInErrors; |
609 |
> |
network_stat_ptr->oerrors = if_row.dwOutErrors; |
610 |
> |
network_stat_ptr->collisions = 0; /* can't do that */ |
611 |
> |
network_stat_ptr->systime = time(NULL); |
612 |
> |
|
613 |
> |
interfaces++; |
614 |
> |
} |
615 |
> |
free(if_table); |
616 |
> |
|
617 |
> |
/* Please say there's a nicer way to do this... If windows has two (or |
618 |
> |
* more) identical network cards, GetIfTable returns them with the same |
619 |
> |
* name, not like in Device Manager where the other has a #2 etc after |
620 |
> |
* it. So, add the #number here. Should we be doing this? Or should the |
621 |
> |
* end programs be dealing with duplicate names? Currently breaks |
622 |
> |
* watch.pl in rrdgraphing. But Unix does not have the issue of |
623 |
> |
* duplicate net device names. |
624 |
> |
*/ |
625 |
> |
for (i=0; i<interfaces; i++) { |
626 |
> |
no = 2; |
627 |
> |
for(j=i+1; j<interfaces; j++) { |
628 |
> |
network_stat_ptr=network_stats+j; |
629 |
> |
if(strcmp(network_stats[i].interface_name, |
630 |
> |
network_stat_ptr->interface_name) == 0) { |
631 |
> |
if(snprintf(buf, sizeof(buf), " #%d", no) < 0) { |
632 |
> |
break; |
633 |
> |
} |
634 |
> |
if(sg_concat_string(&network_stat_ptr->interface_name, buf) != 0) { |
635 |
> |
return NULL; |
636 |
> |
} |
637 |
> |
|
638 |
> |
no++; |
639 |
> |
} |
640 |
> |
} |
641 |
> |
} |
642 |
|
#endif |
643 |
|
|
644 |
|
*entries=interfaces; |
647 |
|
} |
648 |
|
|
649 |
|
static long long transfer_diff(long long new, long long old){ |
650 |
< |
#if defined(SOL7) || defined(LINUX) || defined(FREEBSD) || defined(DFBSD) || defined(OPENBSD) |
650 |
> |
#if defined(SOL7) || defined(LINUX) || defined(FREEBSD) || defined(DFBSD) || defined(OPENBSD) || defined(WIN32) |
651 |
|
/* 32-bit quantities, so we must explicitly deal with wraparound. */ |
652 |
|
#define MAXVAL 0x100000000LL |
653 |
|
if (new >= old) { |
768 |
|
int sock; |
769 |
|
#endif |
770 |
|
#ifdef ALLBSD |
437 |
– |
struct ifaddrs *net, *net_ptr; |
771 |
|
struct ifmediareq ifmed; |
772 |
+ |
struct ifaddrs *net, *net_ptr; |
773 |
|
struct ifreq ifr; |
774 |
|
int sock; |
775 |
|
int x; |
780 |
|
char line[8096]; |
781 |
|
int sock; |
782 |
|
#endif |
783 |
+ |
#ifdef WIN32 |
784 |
+ |
PMIB_IFTABLE if_table; |
785 |
+ |
MIB_IFROW if_row; |
786 |
+ |
int i,j,no; |
787 |
+ |
char buf[5]; |
788 |
+ |
#endif |
789 |
+ |
#ifdef AIX |
790 |
+ |
int fd, n; |
791 |
+ |
size_t pagesize; |
792 |
+ |
struct ifreq *ifr, *lifr, iff; |
793 |
+ |
struct ifconf ifc; |
794 |
+ |
#endif |
795 |
+ |
#ifdef HPUX |
796 |
+ |
int fd; |
797 |
+ |
struct ifreq *ifr, *lifr, iff; |
798 |
+ |
struct ifconf ifc; |
799 |
+ |
#endif |
800 |
|
|
801 |
|
#ifdef ALLBSD |
802 |
|
if(getifaddrs(&net) != 0){ |
848 |
|
continue; |
849 |
|
} |
850 |
|
|
851 |
< |
/* Only intrested in the first 4 bits) - Assuming only ETHER devices */ |
852 |
< |
x = ifmed.ifm_active & 0x0f; |
851 |
> |
/* Assuming only ETHER devices */ |
852 |
> |
x = IFM_SUBTYPE(ifmed.ifm_active); |
853 |
|
switch(x){ |
854 |
|
/* 10 Mbit connections. Speedy :) */ |
855 |
|
case(IFM_10_T): |
859 |
|
case(IFM_10_FL): |
860 |
|
network_iface_stat_ptr->speed = 10; |
861 |
|
break; |
862 |
< |
/* 100 Mbit conneections */ |
862 |
> |
/* 100 Mbit connections */ |
863 |
|
case(IFM_100_TX): |
864 |
|
case(IFM_100_FX): |
865 |
|
case(IFM_100_T4): |
895 |
|
}else{ |
896 |
|
network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN; |
897 |
|
} |
547 |
– |
|
898 |
|
} |
899 |
|
freeifaddrs(net); |
900 |
|
close(sock); |
901 |
|
#endif |
902 |
|
|
903 |
+ |
#ifdef AIX |
904 |
+ |
/* |
905 |
+ |
* Fix up variable length of struct ifr |
906 |
+ |
*/ |
907 |
+ |
#ifndef _SIZEOF_ADDR_IFREQ |
908 |
+ |
#define _SIZEOF_ADDR_IFREQ(ifr) \ |
909 |
+ |
(((ifr).ifr_addr.sa_len > sizeof(struct sockaddr)) \ |
910 |
+ |
? sizeof(struct ifreq) - sizeof(struct sockaddr) + (ifr).ifr_addr.sa_len \ |
911 |
+ |
: sizeof(struct ifreq)) |
912 |
+ |
#endif |
913 |
+ |
|
914 |
+ |
if ((pagesize = sysconf(_SC_PAGESIZE)) == ((size_t)-1)) { |
915 |
+ |
sg_set_error_with_errno(SG_ERROR_SYSCONF, "_SC_PAGESIZE"); |
916 |
+ |
return NULL; |
917 |
+ |
} |
918 |
+ |
|
919 |
+ |
if ((fd = socket (PF_INET, SOCK_DGRAM, 0)) == -1) { |
920 |
+ |
return NULL; |
921 |
+ |
} |
922 |
+ |
|
923 |
+ |
n = 2; |
924 |
+ |
ifr = (struct ifreq *)malloc( n*pagesize ); |
925 |
+ |
if( NULL == ifr ) { |
926 |
+ |
sg_set_error_with_errno(SG_ERROR_MALLOC, NULL); |
927 |
+ |
close (fd); |
928 |
+ |
return NULL; |
929 |
+ |
} |
930 |
+ |
bzero(&ifc, sizeof(ifc)); |
931 |
+ |
ifc.ifc_req = ifr; |
932 |
+ |
ifc.ifc_len = n * pagesize; |
933 |
+ |
while( ( ioctl( fd, SIOCGIFCONF, &ifc ) == -1 ) || |
934 |
+ |
( ((size_t)ifc.ifc_len) >= ( (n-1) * pagesize)) ) |
935 |
+ |
{ |
936 |
+ |
n *= 2; |
937 |
+ |
ifr = (struct ifreq *)realloc( ifr, n*pagesize ); |
938 |
+ |
if( NULL == ifr ) { |
939 |
+ |
free( ifc.ifc_req ); |
940 |
+ |
close (fd); |
941 |
+ |
sg_set_error_with_errno(SG_ERROR_MALLOC, NULL); |
942 |
+ |
return NULL; |
943 |
+ |
} |
944 |
+ |
ifc.ifc_req = ifr; |
945 |
+ |
ifc.ifc_len = n * pagesize; |
946 |
+ |
} |
947 |
+ |
|
948 |
+ |
lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len]; |
949 |
+ |
for ( ifr = ifc.ifc_req; |
950 |
+ |
ifr < lifr; |
951 |
+ |
ifr = (struct ifreq *)&(((char *)ifr)[_SIZEOF_ADDR_IFREQ(*ifr)]) ) |
952 |
+ |
{ |
953 |
+ |
struct sockaddr *sa = &ifr->ifr_addr; |
954 |
+ |
|
955 |
+ |
if(sa->sa_family != AF_LINK) |
956 |
+ |
continue; |
957 |
+ |
|
958 |
+ |
if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) { |
959 |
+ |
free( ifc.ifc_req ); |
960 |
+ |
close (fd); |
961 |
+ |
return NULL; |
962 |
+ |
} |
963 |
+ |
network_iface_stat_ptr = network_iface_stats + ifaces; |
964 |
+ |
|
965 |
+ |
memset(&iff, 0, sizeof(iff)); |
966 |
+ |
strncpy(iff.ifr_name, ifr->ifr_name, sizeof(iff.ifr_name)); |
967 |
+ |
|
968 |
+ |
if (ioctl(fd, SIOCGIFFLAGS, &iff) < 0) { |
969 |
+ |
continue; |
970 |
+ |
} |
971 |
+ |
|
972 |
+ |
if ((iff.ifr_flags & IFF_UP) != 0) { |
973 |
+ |
network_iface_stat_ptr->up = 1; |
974 |
+ |
} else { |
975 |
+ |
network_iface_stat_ptr->up = 0; |
976 |
+ |
} |
977 |
+ |
if (sg_update_string(&network_iface_stat_ptr->interface_name, |
978 |
+ |
ifr->ifr_name) < 0) { |
979 |
+ |
close (fd); |
980 |
+ |
free( ifc.ifc_req ); |
981 |
+ |
return NULL; |
982 |
+ |
} |
983 |
+ |
|
984 |
+ |
network_iface_stat_ptr->speed = 0; |
985 |
+ |
network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN; |
986 |
+ |
ifaces++; |
987 |
+ |
} |
988 |
+ |
|
989 |
+ |
close (fd); |
990 |
+ |
free( ifc.ifc_req ); |
991 |
+ |
#endif |
992 |
+ |
|
993 |
+ |
#ifdef HPUX |
994 |
+ |
/* |
995 |
+ |
* Fix up variable length of struct ifr |
996 |
+ |
*/ |
997 |
+ |
#ifndef _SA_LEN |
998 |
+ |
#define _SA_LEN(sa) \ |
999 |
+ |
((sa).sa_family == AF_UNIX ? sizeof(struct sockaddr_un) : \ |
1000 |
+ |
(sa).sa_family == AF_INET6 ? sizeof(struct sockaddr_in6) : \ |
1001 |
+ |
sizeof(struct sockaddr)) |
1002 |
+ |
#endif |
1003 |
+ |
|
1004 |
+ |
#ifndef _SIZEOF_ADDR_IFREQ |
1005 |
+ |
#define _SIZEOF_ADDR_IFREQ(ifr) \ |
1006 |
+ |
((_SA_LEN((ifr).ifr_addr) > sizeof(struct sockaddr)) \ |
1007 |
+ |
? sizeof(struct ifreq) - sizeof(struct sockaddr) + _SA_LEN((ifr).ifr_addr) \ |
1008 |
+ |
: sizeof(struct ifreq)) |
1009 |
+ |
#endif |
1010 |
+ |
if ((fd = socket (PF_INET, SOCK_DGRAM, 0)) == -1) { |
1011 |
+ |
return NULL; |
1012 |
+ |
} |
1013 |
+ |
|
1014 |
+ |
bzero(&ifc, sizeof(ifc)); |
1015 |
+ |
if (ioctl (fd, SIOCGIFNUM, &ifc) != -1) { |
1016 |
+ |
ifr = calloc(sizeof (*ifr), ifc.ifc_len); |
1017 |
+ |
if( NULL == ifr ) { |
1018 |
+ |
close (fd); |
1019 |
+ |
sg_set_error_with_errno(SG_ERROR_MALLOC, NULL); |
1020 |
+ |
return NULL; |
1021 |
+ |
} |
1022 |
+ |
ifc.ifc_req = ifr; |
1023 |
+ |
ifc.ifc_len *= sizeof (*ifr); |
1024 |
+ |
if (ioctl (fd, SIOCGIFCONF, &ifc) == -1) { |
1025 |
+ |
free (ifr); |
1026 |
+ |
close (fd); |
1027 |
+ |
sg_set_error_with_errno(SG_ERROR_GETIFADDRS, NULL); |
1028 |
+ |
return NULL; |
1029 |
+ |
} |
1030 |
+ |
} else { |
1031 |
+ |
close (fd); |
1032 |
+ |
sg_set_error_with_errno(SG_ERROR_GETIFADDRS, NULL); |
1033 |
+ |
return NULL; |
1034 |
+ |
} |
1035 |
+ |
|
1036 |
+ |
lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len]; |
1037 |
+ |
for ( ifr = ifc.ifc_req; |
1038 |
+ |
ifr < lifr; |
1039 |
+ |
ifr = (struct ifreq *)(((char *)ifr) + _SIZEOF_ADDR_IFREQ(*ifr)) ) |
1040 |
+ |
{ |
1041 |
+ |
if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) { |
1042 |
+ |
free( ifc.ifc_req ); |
1043 |
+ |
close (fd); |
1044 |
+ |
return NULL; |
1045 |
+ |
} |
1046 |
+ |
network_iface_stat_ptr = network_iface_stats + ifaces; |
1047 |
+ |
|
1048 |
+ |
bzero(&iff, sizeof(iff)); |
1049 |
+ |
strncpy(iff.ifr_name, ifr->ifr_name, sizeof(iff.ifr_name)); |
1050 |
+ |
|
1051 |
+ |
if (ioctl(fd, SIOCGIFFLAGS, &iff) < 0) { |
1052 |
+ |
continue; |
1053 |
+ |
} |
1054 |
+ |
|
1055 |
+ |
if ((iff.ifr_flags & IFF_UP) != 0) { |
1056 |
+ |
network_iface_stat_ptr->up = 1; |
1057 |
+ |
} else { |
1058 |
+ |
network_iface_stat_ptr->up = 0; |
1059 |
+ |
} |
1060 |
+ |
if (sg_update_string(&network_iface_stat_ptr->interface_name, |
1061 |
+ |
ifr->ifr_name) < 0) { |
1062 |
+ |
close (fd); |
1063 |
+ |
free( ifc.ifc_req ); |
1064 |
+ |
return NULL; |
1065 |
+ |
} |
1066 |
+ |
|
1067 |
+ |
/** |
1068 |
+ |
* for physical devices we now could ask /dev/dlpi for speed and type, |
1069 |
+ |
* but for monitoring we don't care |
1070 |
+ |
*/ |
1071 |
+ |
network_iface_stat_ptr->speed = 0; |
1072 |
+ |
network_iface_stat_ptr->duplex = SG_IFACE_DUPLEX_UNKNOWN; |
1073 |
+ |
ifaces++; |
1074 |
+ |
} |
1075 |
+ |
|
1076 |
+ |
close (fd); |
1077 |
+ |
free( ifc.ifc_req ); |
1078 |
+ |
#endif |
1079 |
+ |
|
1080 |
|
#ifdef SOLARIS |
1081 |
|
if ((kc = kstat_open()) == NULL) { |
1082 |
|
sg_set_error(SG_ERROR_KSTAT_OPEN, NULL); |
1085 |
|
|
1086 |
|
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) { |
1087 |
|
sg_set_error_with_errno(SG_ERROR_SOCKET, NULL); |
1088 |
+ |
kstat_close(kc); |
1089 |
|
return NULL; |
1090 |
|
} |
1091 |
|
|
1092 |
|
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
1093 |
< |
if (!strcmp(ksp->ks_class, "net")) { |
1093 |
> |
if (strcmp(ksp->ks_class, "net") == 0) { |
1094 |
|
struct ifreq ifr; |
1095 |
|
|
1096 |
|
kstat_read(kc, ksp, NULL); |
1102 |
|
} |
1103 |
|
|
1104 |
|
if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) { |
1105 |
+ |
kstat_close(kc); |
1106 |
|
return NULL; |
1107 |
|
} |
1108 |
|
network_iface_stat_ptr = network_iface_stats + ifaces; |
1110 |
|
|
1111 |
|
if (sg_update_string(&network_iface_stat_ptr->interface_name, |
1112 |
|
ksp->ks_name) < 0) { |
1113 |
+ |
kstat_close(kc); |
1114 |
|
return NULL; |
1115 |
|
} |
1116 |
|
|
1117 |
|
if ((ifr.ifr_flags & IFF_UP) != 0) { |
1118 |
< |
network_iface_stat_ptr->up = 1; |
1118 |
> |
if ((knp = kstat_data_lookup(ksp, "link_up")) != NULL) { |
1119 |
> |
/* take in to account if link |
1120 |
> |
* is up as well as interface */ |
1121 |
> |
if (knp->value.ui32 != 0u) { |
1122 |
> |
network_iface_stat_ptr->up = 1; |
1123 |
> |
} else { |
1124 |
> |
network_iface_stat_ptr->up = 0; |
1125 |
> |
} |
1126 |
> |
} |
1127 |
> |
else { |
1128 |
> |
/* maintain compatibility */ |
1129 |
> |
network_iface_stat_ptr->up = 1; |
1130 |
> |
} |
1131 |
|
} else { |
1132 |
< |
network_iface_stat_ptr->up = 1; |
1132 |
> |
network_iface_stat_ptr->up = 0; |
1133 |
|
} |
1134 |
|
|
1135 |
|
if ((knp = kstat_data_lookup(ksp, "ifspeed")) != NULL) { |
1154 |
|
|
1155 |
|
close(sock); |
1156 |
|
kstat_close(kc); |
1157 |
< |
#endif |
1157 |
> |
#endif |
1158 |
|
#ifdef LINUX |
1159 |
|
f = fopen("/proc/net/dev", "r"); |
1160 |
|
if(f == NULL){ |
1249 |
|
sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin"); |
1250 |
|
return NULL; |
1251 |
|
#endif |
1252 |
< |
#ifdef HPUX |
1253 |
< |
sg_set_error(SG_ERROR_UNSUPPORTED, "HP-UX"); |
1254 |
< |
return NULL; |
1252 |
> |
#ifdef WIN32 |
1253 |
> |
ifaces = 0; |
1254 |
> |
|
1255 |
> |
if((if_table = win32_get_devices()) == NULL) { |
1256 |
> |
sg_set_error(SG_ERROR_DEVICES, "network interfaces"); |
1257 |
> |
return NULL; |
1258 |
> |
} |
1259 |
> |
|
1260 |
> |
if(VECTOR_RESIZE(network_iface_stats, if_table->dwNumEntries) < 0) { |
1261 |
> |
free(if_table); |
1262 |
> |
return NULL; |
1263 |
> |
} |
1264 |
> |
|
1265 |
> |
for(i=0; i<if_table->dwNumEntries; i++) { |
1266 |
> |
network_iface_stat_ptr=network_iface_stats+i; |
1267 |
> |
if_row = if_table->table[i]; |
1268 |
> |
|
1269 |
> |
if(sg_update_string(&network_iface_stat_ptr->interface_name, |
1270 |
> |
if_row.bDescr) < 0) { |
1271 |
> |
free(if_table); |
1272 |
> |
return NULL; |
1273 |
> |
} |
1274 |
> |
network_iface_stat_ptr->speed = if_row.dwSpeed /1000000; |
1275 |
> |
|
1276 |
> |
if((if_row.dwOperStatus == MIB_IF_OPER_STATUS_CONNECTED || |
1277 |
> |
if_row.dwOperStatus == |
1278 |
> |
MIB_IF_OPER_STATUS_OPERATIONAL) && |
1279 |
> |
if_row.dwAdminStatus == 1) { |
1280 |
> |
network_iface_stat_ptr->up = 1; |
1281 |
> |
} else { |
1282 |
> |
network_iface_stat_ptr->up = 0; |
1283 |
> |
} |
1284 |
> |
|
1285 |
> |
ifaces++; |
1286 |
> |
} |
1287 |
> |
free(if_table); |
1288 |
> |
|
1289 |
> |
/* again with the renumbering */ |
1290 |
> |
for (i=0; i<ifaces; i++) { |
1291 |
> |
no = 2; |
1292 |
> |
for(j=i+1; j<ifaces; j++) { |
1293 |
> |
network_iface_stat_ptr=network_iface_stats+j; |
1294 |
> |
if(strcmp(network_iface_stats[i].interface_name, |
1295 |
> |
network_iface_stat_ptr->interface_name) == 0) { |
1296 |
> |
if(snprintf(buf, sizeof(buf), " #%d", no) < 0) { |
1297 |
> |
break; |
1298 |
> |
} |
1299 |
> |
if(sg_concat_string(&network_iface_stat_ptr->interface_name, buf) != 0) { |
1300 |
> |
return NULL; |
1301 |
> |
} |
1302 |
> |
|
1303 |
> |
no++; |
1304 |
> |
} |
1305 |
> |
} |
1306 |
> |
} |
1307 |
|
#endif |
1308 |
|
|
1309 |
|
#ifdef SG_ENABLE_DEPRECATED |