re-factor iface screen code (M. Dolze)
This commit is contained in:
1 parent
6b9bfd085d
commit
2ff1b58edb
11 files changed
+244
-176
No files matched your search
@@ -48,6 +48,9 @@
|
||||
#include <machine/apm_bios.h>
|
||||
#include <kvm.h>
|
||||
#include <errno.h>
|
||||
#include <sys/socket.h>
|
||||
#include <net/if.h>
|
||||
#include <net/if_mib.h>
|
||||
|
||||
#include "main.h"
|
||||
#include "machine.h"
|
||||
@@ -416,4 +419,63 @@ static int swapmode(int *retavail, int *retfree)
|
||||
return(n);
|
||||
}
|
||||
|
||||
/*************************************************************************
|
||||
* Read interface statistics from system and store in the struct
|
||||
* passed as a pointer. If there are no errors, it returns 1. If errors,
|
||||
* returns 0.
|
||||
*************************************************************************
|
||||
*/
|
||||
int machine_get_iface_stats (IfaceInfo *interface)
|
||||
{
|
||||
static int first_time = 1; /* is it first time we call this function? */
|
||||
int rows;
|
||||
int name[6] = {CTL_NET, PF_LINK, NETLINK_GENERIC, IFMIB_IFDATA, 0, IFDATA_GENERAL};
|
||||
size_t len;
|
||||
struct ifmibdata ifmd; /* ifmibdata contains the network statistics */
|
||||
|
||||
len = sizeof(rows);
|
||||
/* get number of interfaces */
|
||||
if (sysctlbyname("net.link.generic.system.ifcount", &rows, &len, NULL, 0) == 0) {
|
||||
interface->status = down; /* set status down by default */
|
||||
|
||||
len = sizeof(ifmd);
|
||||
/* walk through all interfaces in the ifmib table from last to first */
|
||||
for ( ; rows > 0; rows--) {
|
||||
name[4] = rows; /* set the interface index */
|
||||
/* retrive the ifmibdata for the current index */
|
||||
if (sysctl(name, 6, &ifmd, &len, NULL, 0) == -1) {
|
||||
perror("read sysctl");
|
||||
break;
|
||||
}
|
||||
/* check if its interface name matches */
|
||||
if (strcmp(ifmd.ifmd_name, interface->name) == 0) {
|
||||
interface->last_online = time(NULL); /* save actual time */
|
||||
|
||||
if ((ifmd.ifmd_flags & IFF_UP) == IFF_UP)
|
||||
interface->status = up; /* is up */
|
||||
|
||||
interface->rc_byte = ifmd.ifmd_data.ifi_ibytes;
|
||||
interface->tr_byte = ifmd.ifmd_data.ifi_obytes;
|
||||
interface->rc_pkt = ifmd.ifmd_data.ifi_ipackets;
|
||||
interface->tr_pkt = ifmd.ifmd_data.ifi_opackets;
|
||||
|
||||
if (first_time) {
|
||||
interface->rc_byte_old = interface->rc_byte;
|
||||
interface->tr_byte_old = interface->tr_byte;
|
||||
interface->rc_pkt_old = interface->rc_pkt;
|
||||
interface->tr_pkt_old = interface->tr_pkt;
|
||||
first_time = 0; /* now it isn't first time */
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
/* if we are here there is no interface with the given name */
|
||||
return 0;
|
||||
} else {
|
||||
perror("read sysctlbyname");
|
||||
return 0;
|
||||
}
|
||||
} /* get_iface_stats() */
|
||||
|
||||
|
||||
#endif /* __FreeBSD__ */
|
||||
Reference in new issue
Block a user