cache getifaddrs - linux
This commit is contained in:
parent
4192f6a6d9
commit
259ee610a6
2 changed files with 18 additions and 1 deletions
|
@ -126,12 +126,14 @@ LinuxEthernetTap::LinuxEthernetTap(
|
|||
_mtu(mtu),
|
||||
_fd(0),
|
||||
_enabled(true),
|
||||
_run(true)
|
||||
_run(true),
|
||||
_lastIfAddrsUpdate(0)
|
||||
{
|
||||
static std::mutex s_tapCreateLock;
|
||||
char procpath[128],nwids[32];
|
||||
struct stat sbuf;
|
||||
|
||||
|
||||
// Create only one tap at a time globally.
|
||||
std::lock_guard<std::mutex> tapCreateLock(s_tapCreateLock);
|
||||
|
||||
|
@ -438,6 +440,14 @@ bool LinuxEthernetTap::removeIp(const InetAddress &ip)
|
|||
|
||||
std::vector<InetAddress> LinuxEthernetTap::ips() const
|
||||
{
|
||||
|
||||
uint64_t now = OSUtils::now();
|
||||
|
||||
if ((now - _lastIfAddrsUpdate) <= GETIFADDRS_CACHE_TIME) {
|
||||
return _ifaddrs;
|
||||
}
|
||||
_lastIfAddrsUpdate = now;
|
||||
|
||||
struct ifaddrs *ifa = (struct ifaddrs *)0;
|
||||
if (getifaddrs(&ifa))
|
||||
return std::vector<InetAddress>();
|
||||
|
@ -471,6 +481,8 @@ std::vector<InetAddress> LinuxEthernetTap::ips() const
|
|||
std::sort(r.begin(),r.end());
|
||||
r.erase(std::unique(r.begin(),r.end()),r.end());
|
||||
|
||||
_ifaddrs = r;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue