2011-09-28 19:13:20 +00:00
|
|
|
/* MiniUPnP project
|
|
|
|
* http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
|
2014-09-14 09:41:49 +00:00
|
|
|
* author: Gleb Smirnoff <glebius@FreeBSD.org>
|
2011-09-28 19:13:20 +00:00
|
|
|
* (c) 2006 Ryan Wagoner
|
2014-09-14 09:41:49 +00:00
|
|
|
* (c) 2014 Gleb Smirnoff
|
2011-09-28 19:13:20 +00:00
|
|
|
* This software is subject to the conditions detailed
|
|
|
|
* in the LICENCE file provided within the distribution */
|
|
|
|
|
|
|
|
#include <net/if.h>
|
2014-09-14 09:41:49 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <ifaddrs.h>
|
2011-09-28 19:13:20 +00:00
|
|
|
#include <string.h>
|
2014-09-14 09:41:49 +00:00
|
|
|
#include <syslog.h>
|
|
|
|
|
|
|
|
#ifdef ENABLE_GETIFSTATS_CACHING
|
|
|
|
#include <time.h>
|
|
|
|
#endif
|
2011-09-28 19:13:20 +00:00
|
|
|
|
|
|
|
#include "../getifstats.h"
|
|
|
|
#include "../config.h"
|
|
|
|
|
2012-03-01 01:44:38 +00:00
|
|
|
int
|
2014-09-14 09:41:49 +00:00
|
|
|
getifstats(const char *ifname, struct ifdata *data)
|
2011-09-28 19:13:20 +00:00
|
|
|
{
|
2014-09-14 09:41:49 +00:00
|
|
|
static struct ifaddrs *ifap, *ifa;
|
2011-09-28 19:13:20 +00:00
|
|
|
#ifdef ENABLE_GETIFSTATS_CACHING
|
2014-09-14 09:41:49 +00:00
|
|
|
static time_t cache_timestamp;
|
2011-09-28 19:13:20 +00:00
|
|
|
time_t current_time;
|
|
|
|
#endif
|
|
|
|
if(!data)
|
|
|
|
return -1;
|
|
|
|
data->baudrate = 4200000;
|
|
|
|
data->opackets = 0;
|
|
|
|
data->ipackets = 0;
|
|
|
|
data->obytes = 0;
|
|
|
|
data->ibytes = 0;
|
|
|
|
if(!ifname || ifname[0]=='\0')
|
|
|
|
return -1;
|
2014-09-14 09:41:49 +00:00
|
|
|
|
2011-09-28 19:13:20 +00:00
|
|
|
#ifdef ENABLE_GETIFSTATS_CACHING
|
|
|
|
current_time = time(NULL);
|
2014-09-14 09:41:49 +00:00
|
|
|
if (ifap != NULL &&
|
|
|
|
current_time < cache_timestamp + GETIFSTATS_CACHING_DURATION)
|
|
|
|
goto copy;
|
2011-09-28 19:13:20 +00:00
|
|
|
#endif
|
|
|
|
|
2014-09-14 09:41:49 +00:00
|
|
|
if (ifap != NULL) {
|
|
|
|
freeifaddrs(ifap);
|
|
|
|
ifap = NULL;
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
2014-09-14 09:41:49 +00:00
|
|
|
|
|
|
|
if (getifaddrs(&ifap) != 0) {
|
|
|
|
syslog (LOG_ERR, "getifstats() : getifaddrs(): %s",
|
|
|
|
strerror(errno));
|
|
|
|
return (-1);
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
|
2014-09-14 09:41:49 +00:00
|
|
|
for (ifa = ifap; ifa; ifa = ifa->ifa_next)
|
|
|
|
if (ifa->ifa_addr->sa_family == AF_LINK &&
|
|
|
|
strcmp(ifa->ifa_name, ifname) == 0) {
|
2011-09-28 19:13:20 +00:00
|
|
|
#ifdef ENABLE_GETIFSTATS_CACHING
|
2014-09-14 09:41:49 +00:00
|
|
|
cache_timestamp = current_time;
|
|
|
|
copy:
|
2011-09-28 19:13:20 +00:00
|
|
|
#endif
|
2014-09-14 09:41:49 +00:00
|
|
|
#define IFA_STAT(s) (((struct if_data *)ifa->ifa_data)->ifi_ ## s)
|
|
|
|
data->opackets = IFA_STAT(opackets);
|
|
|
|
data->ipackets = IFA_STAT(ipackets);
|
|
|
|
data->obytes = IFA_STAT(obytes);
|
|
|
|
data->ibytes = IFA_STAT(ibytes);
|
|
|
|
data->baudrate = IFA_STAT(baudrate);
|
|
|
|
return (0);
|
2011-09-28 19:13:20 +00:00
|
|
|
}
|
|
|
|
|
2014-09-14 09:41:49 +00:00
|
|
|
return (-1);
|
|
|
|
}
|