| 32 |
|
#ifdef SOLARIS |
| 33 |
|
#include <kstat.h> |
| 34 |
|
#include <sys/sysinfo.h> |
| 35 |
+ |
#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 |
|
#endif |
| 42 |
|
#ifdef LINUX |
| 43 |
|
#include <stdio.h> |
| 54 |
|
typedef __uint8_t u8; |
| 55 |
|
typedef __uint16_t u16; |
| 56 |
|
typedef __uint32_t u32; |
| 57 |
+ |
typedef __uint64_t u64; |
| 58 |
|
#include <linux/ethtool.h> |
| 59 |
|
#include <linux/sockios.h> |
| 60 |
+ |
#include <unistd.h> |
| 61 |
|
#endif |
| 62 |
|
#ifdef ALLBSD |
| 63 |
|
#include <sys/types.h> |
| 77 |
|
net_stats->interface_name=NULL; |
| 78 |
|
net_stats->tx=0; |
| 79 |
|
net_stats->rx=0; |
| 80 |
+ |
net_stats->ipackets=0; |
| 81 |
+ |
net_stats->opackets=0; |
| 82 |
+ |
net_stats->ierrors=0; |
| 83 |
+ |
net_stats->oerrors=0; |
| 84 |
+ |
net_stats->collisions=0; |
| 85 |
|
net_stats++; |
| 86 |
|
} |
| 87 |
|
} |
| 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 |
< |
network_stat_ptr->tx=net_data->ifi_obytes; |
| 159 |
> |
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 |
|
network_stat_ptr->systime=time(NULL); |
| 166 |
|
interfaces++; |
| 167 |
|
} |
| 190 |
|
#endif |
| 191 |
|
|
| 192 |
|
if((knp=kstat_data_lookup(ksp, RLOOKUP))==NULL){ |
| 193 |
< |
/* Not a network interface, so skip to the next entry */ |
| 193 |
> |
/* 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 |
|
continue; |
| 198 |
|
} |
| 199 |
|
|
| 205 |
|
network_stat_ptr->rx=knp->VALTYPE; |
| 206 |
|
|
| 207 |
|
if((knp=kstat_data_lookup(ksp, WLOOKUP))==NULL){ |
| 187 |
– |
/* Not a network interface, so skip to the next entry */ |
| 208 |
|
continue; |
| 209 |
|
} |
| 210 |
|
network_stat_ptr->tx=knp->VALTYPE; |
| 272 |
|
} |
| 273 |
|
|
| 274 |
|
long long transfer_diff(long long new, long long old){ |
| 275 |
< |
#if defined(SOL7) || defined(LINUX) || defined(FREEBSD) |
| 275 |
> |
#if defined(SOL7) || defined(LINUX) || defined(FREEBSD) || defined(DFBSD) |
| 276 |
|
#define MAXVAL 4294967296LL |
| 277 |
|
#else |
| 278 |
|
#define MAXVAL 18446744073709551616LL |
| 316 |
|
dest->interface_name = strdup(src->interface_name); |
| 317 |
|
dest->rx = src->rx; |
| 318 |
|
dest->tx = src->tx; |
| 319 |
+ |
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 |
|
dest->systime = src->systime; |
| 325 |
|
} |
| 326 |
|
|
| 351 |
|
difference. */ |
| 352 |
|
dest->rx = transfer_diff(src->rx, dest->rx); |
| 353 |
|
dest->tx = transfer_diff(src->tx, dest->tx); |
| 354 |
+ |
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 |
|
dest->systime = src->systime - dest->systime; |
| 360 |
|
} |
| 361 |
|
|
| 369 |
|
for(net_stats+=start; start<end; start++){ |
| 370 |
|
net_stats->interface_name=NULL; |
| 371 |
|
net_stats->speed=0; |
| 372 |
< |
net_stats->dup=NO_DUPLEX; |
| 372 |
> |
net_stats->dup=UNKNOWN_DUPLEX; |
| 373 |
|
net_stats++; |
| 374 |
|
} |
| 375 |
|
} |
| 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 |
< |
static int ifaces; |
| 407 |
> |
int ifaces = 0; |
| 408 |
|
|
| 409 |
|
#ifdef SOLARIS |
| 410 |
|
kstat_ctl_t *kc; |
| 411 |
|
kstat_t *ksp; |
| 412 |
|
kstat_named_t *knp; |
| 413 |
+ |
int sock; |
| 414 |
|
#endif |
| 415 |
|
#ifdef ALLBSD |
| 416 |
|
struct ifaddrs *net, *net_ptr; |
| 417 |
|
struct ifmediareq ifmed; |
| 418 |
< |
int s; |
| 418 |
> |
struct ifreq ifr; |
| 419 |
> |
int sock; |
| 420 |
|
int x; |
| 421 |
|
#endif |
| 422 |
|
#ifdef LINUX |
| 423 |
|
FILE *f; |
| 424 |
|
/* Horrible big enough, but it should be easily big enough */ |
| 425 |
|
char line[8096]; |
| 394 |
– |
void *eth_tool_cmd_buf; |
| 395 |
– |
int buf_size; |
| 426 |
|
int sock; |
| 427 |
|
#endif |
| 428 |
< |
ifaces = 0; |
| 428 |
> |
|
| 429 |
|
#ifdef ALLBSD |
| 430 |
|
if(getifaddrs(&net) != 0){ |
| 431 |
|
return NULL; |
| 432 |
|
} |
| 433 |
|
|
| 434 |
< |
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == NULL) return NULL; |
| 434 |
> |
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return NULL; |
| 435 |
|
|
| 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; |
| 441 |
|
} |
| 442 |
|
network_iface_stat_ptr = network_iface_stats + ifaces; |
| 443 |
|
|
| 444 |
+ |
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 |
|
memset(&ifmed, 0, sizeof(struct ifmediareq)); |
| 465 |
|
strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name)); |
| 466 |
< |
if(ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){ |
| 466 |
> |
if(ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){ |
| 467 |
> |
/* Not all interfaces support the media ioctls. */ |
| 468 |
|
continue; |
| 469 |
|
} |
| 470 |
|
|
| 474 |
|
continue; |
| 475 |
|
} |
| 476 |
|
|
| 426 |
– |
if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name); |
| 427 |
– |
network_iface_stat_ptr->interface_name = strdup(net_ptr->ifa_name); |
| 428 |
– |
if(network_iface_stat_ptr->interface_name == NULL) return NULL; |
| 429 |
– |
|
| 477 |
|
/* Only intrested in the first 4 bits) - Assuming only ETHER devices */ |
| 478 |
|
x = ifmed.ifm_active & 0x0f; |
| 479 |
|
switch(x){ |
| 497 |
|
case(IFM_1000_SX): |
| 498 |
|
case(IFM_1000_LX): |
| 499 |
|
case(IFM_1000_CX): |
| 500 |
< |
case(IFM_1000_TX): |
| 500 |
> |
#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 |
> |
#endif |
| 509 |
|
network_iface_stat_ptr->speed = 1000; |
| 510 |
|
break; |
| 511 |
|
/* We don't know what it is */ |
| 519 |
|
}else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){ |
| 520 |
|
network_iface_stat_ptr->dup = HALF_DUPLEX; |
| 521 |
|
}else{ |
| 522 |
< |
network_iface_stat_ptr->dup = NO_DUPLEX; |
| 522 |
> |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
| 523 |
|
} |
| 524 |
< |
ifaces++; |
| 524 |
> |
|
| 525 |
|
} |
| 526 |
|
freeifaddrs(net); |
| 527 |
+ |
close(sock); |
| 528 |
|
#endif |
| 529 |
|
|
| 530 |
|
#ifdef SOLARIS |
| 531 |
< |
if ((kc = kstat_open()) == NULL) { |
| 532 |
< |
return NULL; |
| 533 |
< |
} |
| 531 |
> |
if ((kc = kstat_open()) == NULL) { |
| 532 |
> |
return NULL; |
| 533 |
> |
} |
| 534 |
|
|
| 535 |
< |
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
| 536 |
< |
if (!strcmp(ksp->ks_class, "net")) { |
| 537 |
< |
kstat_read(kc, ksp, NULL); |
| 538 |
< |
if((knp=kstat_data_lookup(ksp, "ifspeed"))==NULL){ |
| 539 |
< |
/* Not a network interface, so skip to the next entry */ |
| 535 |
> |
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) { |
| 536 |
> |
return NULL; |
| 537 |
> |
} |
| 538 |
> |
|
| 539 |
> |
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 |
|
continue; |
| 549 |
|
} |
| 550 |
< |
network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats); |
| 551 |
< |
if(network_iface_stats==NULL){ |
| 550 |
> |
|
| 551 |
> |
network_iface_stats = network_iface_stat_malloc(ifaces + 1, &sizeof_network_iface_stats, network_iface_stats); |
| 552 |
> |
if (network_iface_stats == NULL) { |
| 553 |
|
return NULL; |
| 554 |
|
} |
| 555 |
|
network_iface_stat_ptr = network_iface_stats + ifaces; |
| 556 |
< |
network_iface_stat_ptr->speed = knp->value.ui64 / (1000*1000); |
| 556 |
> |
ifaces++; |
| 557 |
|
|
| 558 |
< |
if((knp=kstat_data_lookup(ksp, "link_duplex"))==NULL){ |
| 559 |
< |
/* Not a network interface, so skip to the next entry */ |
| 560 |
< |
continue; |
| 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 |
> |
|
| 562 |
> |
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 |
|
|
| 568 |
< |
if(knp->value.ui64 == 0){ |
| 569 |
< |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
| 570 |
< |
}else{ |
| 571 |
< |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
| 568 |
> |
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 |
|
} |
| 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; |
| 577 |
< |
ifaces++; |
| 574 |
> |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
| 575 |
> |
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 |
> |
} |
| 585 |
|
} |
| 586 |
|
} |
| 587 |
+ |
|
| 588 |
+ |
close(sock); |
| 589 |
|
kstat_close(kc); |
| 511 |
– |
|
| 590 |
|
#endif |
| 591 |
|
#ifdef LINUX |
| 514 |
– |
|
| 592 |
|
f = fopen("/proc/net/dev", "r"); |
| 593 |
|
if(f == NULL){ |
| 594 |
|
return NULL; |
| 599 |
|
return NULL; |
| 600 |
|
} |
| 601 |
|
|
| 525 |
– |
buf_size = sizeof(struct ethtool_cmd); |
| 526 |
– |
eth_tool_cmd_buf = malloc(buf_size); |
| 527 |
– |
if(eth_tool_cmd_buf == NULL) return NULL; |
| 528 |
– |
|
| 602 |
|
/* Ignore first 2 lines.. Just headings */ |
| 603 |
< |
fgets(line, sizeof(line), f); |
| 604 |
< |
fgets(line, sizeof(line), f); |
| 603 |
> |
if((fgets(line, sizeof(line), f)) == NULL) return NULL; |
| 604 |
> |
if((fgets(line, sizeof(line), f)) == NULL) return NULL; |
| 605 |
|
|
| 606 |
|
while((fgets(line, sizeof(line), f)) != NULL){ |
| 607 |
|
char *name, *ptr; |
| 608 |
|
struct ifreq ifr; |
| 609 |
< |
struct ethtool_cmd *ethcmd; |
| 609 |
> |
struct ethtool_cmd ethcmd; |
| 610 |
|
int err; |
| 611 |
|
|
| 612 |
|
/* Get the interface name */ |
| 618 |
|
name++; |
| 619 |
|
} |
| 620 |
|
|
| 621 |
< |
memset(&ifr, 0, sizeof(ifr)); |
| 622 |
< |
memset(eth_tool_cmd_buf, 0, buf_size); |
| 550 |
< |
ifr.ifr_data = (caddr_t) eth_tool_cmd_buf; |
| 551 |
< |
strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); |
| 621 |
> |
memset(&ifr, 0, sizeof ifr); |
| 622 |
> |
strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); |
| 623 |
|
|
| 624 |
< |
ethcmd = (struct ethtool_cmd *)(&ifr)->ifr_data; |
| 625 |
< |
ethcmd->cmd = ETHTOOL_GSET; |
| 624 |
> |
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) { |
| 625 |
> |
continue; |
| 626 |
> |
} |
| 627 |
|
|
| 556 |
– |
err = ioctl(sock, SIOCETHTOOL, &ifr); |
| 557 |
– |
if(err < 0){ |
| 558 |
– |
/* This could fail if the interface doesn't support the command. Carry |
| 559 |
– |
* on to the next :) |
| 560 |
– |
*/ |
| 561 |
– |
continue; |
| 562 |
– |
} |
| 563 |
– |
|
| 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){ |
| 632 |
|
} |
| 633 |
|
network_iface_stat_ptr = network_iface_stats + ifaces; |
| 634 |
|
network_iface_stat_ptr->interface_name = strdup(name); |
| 635 |
< |
network_iface_stat_ptr->speed = ethcmd->speed; |
| 636 |
< |
network_iface_stat_ptr->dup = NO_DUPLEX; |
| 637 |
< |
if(ethcmd->duplex == 0x00){ |
| 638 |
< |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
| 635 |
> |
if ((ifr.ifr_flags & IFF_UP) != 0) { |
| 636 |
> |
network_iface_stat_ptr->up = 1; |
| 637 |
> |
} else { |
| 638 |
> |
network_iface_stat_ptr->up = 0; |
| 639 |
|
} |
| 640 |
< |
if(ethcmd->duplex == 0x01){ |
| 641 |
< |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
| 640 |
> |
|
| 641 |
> |
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 |
> |
network_iface_stat_ptr->speed = 0; |
| 662 |
> |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
| 663 |
|
} |
| 664 |
+ |
|
| 665 |
|
ifaces++; |
| 666 |
|
} |
| 667 |
< |
|
| 668 |
< |
free(eth_tool_cmd_buf); |
| 667 |
> |
close(sock); |
| 668 |
> |
fclose(f); |
| 669 |
|
#endif |
| 670 |
|
*entries = ifaces; |
| 671 |
|
return network_iface_stats; |