2017-02-15 13:48:09 -08:00
|
|
|
#include "netstatus.h"
|
|
|
|
#if defined(__FreeBSD__) || defined(__DragonFly__)
|
|
|
|
# include <sys/socket.h>
|
|
|
|
# include <sys/sysctl.h>
|
|
|
|
# include <net/if.h>
|
|
|
|
# include <net/if_mib.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__OpenBSD__)
|
|
|
|
# include <sys/types.h>
|
|
|
|
# include <sys/socket.h>
|
|
|
|
# include <sys/sysctl.h>
|
|
|
|
# include <sys/sockio.h>
|
|
|
|
# include <sys/ioctl.h>
|
|
|
|
# include <net/if.h>
|
|
|
|
# include <net/if_types.h>
|
|
|
|
# include <ifaddrs.h>
|
2017-08-29 12:20:05 -07:00
|
|
|
#endif
|
2017-02-15 13:48:09 -08:00
|
|
|
|
|
|
|
#if defined(__FreeBSD__) || defined(__DragonFly__)
|
|
|
|
|
|
|
|
static int
|
|
|
|
get_ifmib_general(int row, struct ifmibdata *data)
|
|
|
|
{
|
|
|
|
int mib[6];
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
mib[0] = CTL_NET;
|
|
|
|
mib[1] = PF_LINK;
|
|
|
|
mib[2] = NETLINK_GENERIC;
|
|
|
|
mib[3] = IFMIB_IFDATA;
|
|
|
|
mib[4] = row;
|
|
|
|
mib[5] = IFDATA_GENERAL;
|
|
|
|
|
|
|
|
len = sizeof(*data);
|
|
|
|
|
|
|
|
return sysctl(mib, 6, data, &len, NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_freebsd_generic_network_status(unsigned long int *in, unsigned long int *out)
|
|
|
|
{
|
|
|
|
struct ifmibdata *ifmd;
|
|
|
|
size_t len;
|
|
|
|
int i, count;
|
|
|
|
|
|
|
|
len = sizeof(count);
|
|
|
|
if (sysctlbyname("net.link.generic.system.ifcount", &count, &len, NULL, 0) < 0)
|
|
|
|
return;
|
2017-08-29 12:20:05 -07:00
|
|
|
|
2017-02-15 13:48:09 -08:00
|
|
|
ifmd = malloc(sizeof(struct ifmibdata));
|
|
|
|
if (!ifmd)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 1; i <= count; i++)
|
2017-08-29 12:20:05 -07:00
|
|
|
{
|
|
|
|
get_ifmib_general(i, ifmd);
|
|
|
|
*in += ifmd->ifmd_data.ifi_ibytes;
|
|
|
|
*out += ifmd->ifmd_data.ifi_obytes;
|
|
|
|
}
|
2017-02-15 13:48:09 -08:00
|
|
|
|
|
|
|
free(ifmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__OpenBSD__)
|
|
|
|
static void
|
|
|
|
_openbsd_generic_network_status(unsigned long int *in, unsigned long int *out)
|
|
|
|
{
|
|
|
|
struct ifaddrs *interfaces, *ifa;
|
2017-08-29 12:20:05 -07:00
|
|
|
|
2017-02-15 13:48:09 -08:00
|
|
|
if (getifaddrs(&interfaces) < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int sock = socket(AF_INET, SOCK_STREAM, 0);
|
2017-08-29 12:20:05 -07:00
|
|
|
if (sock < 0)
|
2017-02-15 13:48:09 -08:00
|
|
|
return;
|
|
|
|
|
|
|
|
for (ifa = interfaces; ifa; ifa = ifa->ifa_next)
|
|
|
|
{
|
|
|
|
struct ifreq ifreq;
|
|
|
|
struct if_data if_data;
|
2017-08-29 12:20:05 -07:00
|
|
|
|
2017-02-15 13:48:09 -08:00
|
|
|
ifreq.ifr_data = (void *)&if_data;
|
2017-08-29 12:20:05 -07:00
|
|
|
strncpy(ifreq.ifr_name, ifa->ifa_name, IFNAMSIZ - 1);
|
2017-02-15 13:48:09 -08:00
|
|
|
if (ioctl(sock, SIOCGIFDATA, &ifreq) < 0)
|
|
|
|
return;
|
2017-08-29 12:20:05 -07:00
|
|
|
|
|
|
|
struct if_data *const ifi = &if_data;
|
|
|
|
if (ifi->ifi_type == IFT_ETHER || ifi->ifi_type == IFT_IEEE80211)
|
2017-02-15 13:48:09 -08:00
|
|
|
{
|
|
|
|
if (ifi->ifi_ibytes)
|
|
|
|
*in += ifi->ifi_ibytes;
|
|
|
|
else
|
|
|
|
*in += 0;
|
|
|
|
|
|
|
|
if (ifi->ifi_obytes)
|
|
|
|
*out += ifi->ifi_obytes;
|
|
|
|
else
|
|
|
|
*out += 0;
|
2017-08-29 12:20:05 -07:00
|
|
|
}
|
2017-02-15 13:48:09 -08:00
|
|
|
}
|
|
|
|
close(sock);
|
|
|
|
}
|
2017-08-29 12:20:05 -07:00
|
|
|
|
2017-02-15 13:48:09 -08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
2017-07-15 19:31:06 -07:00
|
|
|
_netstatus_sysctl_getrstatus(Eina_Bool automax,
|
2017-08-29 12:20:05 -07:00
|
|
|
unsigned long *prev_in,
|
|
|
|
unsigned long *prev_incurrent,
|
|
|
|
unsigned long *prev_inmax,
|
|
|
|
int *prev_inpercent)
|
2017-02-15 13:48:09 -08:00
|
|
|
{
|
2017-07-15 19:31:06 -07:00
|
|
|
unsigned long tot_in = 0, diffin;
|
2017-02-15 13:48:09 -08:00
|
|
|
int percent = 0;
|
|
|
|
unsigned long int incoming = 0, outgoing = 0;
|
|
|
|
#if defined(__OpenBSD__)
|
|
|
|
_openbsd_generic_network_status(&incoming, &outgoing);
|
|
|
|
#elif defined(__FreeBSD__) || defined(__DragonFly__)
|
|
|
|
_freebsd_generic_network_status(&incoming, &outgoing);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
tot_in = incoming;
|
2017-07-15 19:31:06 -07:00
|
|
|
diffin = tot_in - *prev_in;
|
|
|
|
if (!*prev_in)
|
|
|
|
*prev_in = tot_in;
|
2017-02-15 13:48:09 -08:00
|
|
|
else
|
|
|
|
{
|
2017-07-15 19:31:06 -07:00
|
|
|
*prev_in = tot_in;
|
|
|
|
if (automax)
|
2017-02-15 13:48:09 -08:00
|
|
|
{
|
2017-07-15 19:31:06 -07:00
|
|
|
if (diffin > *prev_inmax)
|
|
|
|
*prev_inmax = diffin;
|
2017-02-15 13:48:09 -08:00
|
|
|
}
|
2017-07-15 19:31:06 -07:00
|
|
|
*prev_incurrent = diffin;
|
|
|
|
if (*prev_inmax > 0)
|
|
|
|
percent = 100 * ((float)*prev_incurrent / (float)*prev_inmax);
|
2017-02-15 13:48:09 -08:00
|
|
|
if (percent > 100) percent = 100;
|
2017-08-29 12:20:05 -07:00
|
|
|
else if (percent < 0)
|
|
|
|
percent = 0;
|
2017-07-15 19:31:06 -07:00
|
|
|
*prev_inpercent = percent;
|
2017-02-15 13:48:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-07-15 19:31:06 -07:00
|
|
|
_netstatus_sysctl_gettstatus(Eina_Bool automax,
|
2017-08-29 12:20:05 -07:00
|
|
|
unsigned long *prev_out,
|
|
|
|
unsigned long *prev_outcurrent,
|
|
|
|
unsigned long *prev_outmax,
|
|
|
|
int *prev_outpercent)
|
2017-02-15 13:48:09 -08:00
|
|
|
{
|
2017-07-15 19:31:06 -07:00
|
|
|
unsigned long tot_out = 0, diffout;
|
2017-02-15 13:48:09 -08:00
|
|
|
int percent = 0;
|
|
|
|
unsigned long int incoming = 0, outgoing = 0;
|
|
|
|
#if defined(__OpenBSD__)
|
|
|
|
_openbsd_generic_network_status(&incoming, &outgoing);
|
|
|
|
#elif defined(__FreeBSD__) || defined(__DragonFly__)
|
|
|
|
_freebsd_generic_network_status(&incoming, &outgoing);
|
|
|
|
#endif
|
|
|
|
tot_out = outgoing;
|
|
|
|
|
2017-07-15 19:31:06 -07:00
|
|
|
diffout = tot_out - *prev_out;
|
|
|
|
if (!*prev_out)
|
|
|
|
*prev_out = tot_out;
|
2017-02-15 13:48:09 -08:00
|
|
|
else
|
|
|
|
{
|
2017-07-15 19:31:06 -07:00
|
|
|
*prev_out = tot_out;
|
|
|
|
if (automax)
|
2017-02-15 13:48:09 -08:00
|
|
|
{
|
2017-07-15 19:31:06 -07:00
|
|
|
if (diffout > *prev_outmax)
|
|
|
|
*prev_outmax = diffout;
|
2017-02-15 13:48:09 -08:00
|
|
|
}
|
2017-07-15 19:31:06 -07:00
|
|
|
*prev_outcurrent = diffout;
|
|
|
|
if (*prev_outcurrent > 0)
|
|
|
|
percent = 100 * ((float)*prev_outcurrent / (float)*prev_outmax);
|
2017-02-15 13:48:09 -08:00
|
|
|
if (percent > 100) percent = 100;
|
2017-08-29 12:20:05 -07:00
|
|
|
else if (percent < 0)
|
|
|
|
percent = 0;
|
2017-07-15 19:31:06 -07:00
|
|
|
*prev_outpercent = percent;
|
2017-02-15 13:48:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|