cache getifaddrs - bsd

This commit is contained in:
travis laduke 2023-05-17 07:16:26 -07:00 committed by Travis LaDuke
parent 259ee610a6
commit 60d2138f30
2 changed files with 13 additions and 1 deletions

View File

@ -74,7 +74,8 @@ BSDEthernetTap::BSDEthernetTap(
_mtu(mtu), _mtu(mtu),
_metric(metric), _metric(metric),
_fd(0), _fd(0),
_enabled(true) _enabled(true),
_lastIfAddrsUpdate(0)
{ {
static Mutex globalTapCreateLock; static Mutex globalTapCreateLock;
char devpath[64],ethaddr[64],mtustr[32],metstr[32],tmpdevname[32]; char devpath[64],ethaddr[64],mtustr[32],metstr[32],tmpdevname[32];
@ -287,6 +288,13 @@ bool BSDEthernetTap::removeIp(const InetAddress &ip)
std::vector<InetAddress> BSDEthernetTap::ips() const std::vector<InetAddress> BSDEthernetTap::ips() const
{ {
uint64_t now = OSUtils::now();
if ((now - _lastIfAddrsUpdate) <= GETIFADDRS_CACHE_TIME) {
return _ifaddrs;
}
_lastIfAddrsUpdate = now;
struct ifaddrs *ifa = (struct ifaddrs *)0; struct ifaddrs *ifa = (struct ifaddrs *)0;
if (getifaddrs(&ifa)) if (getifaddrs(&ifa))
return std::vector<InetAddress>(); return std::vector<InetAddress>();
@ -320,6 +328,8 @@ std::vector<InetAddress> BSDEthernetTap::ips() const
std::sort(r.begin(),r.end()); std::sort(r.begin(),r.end());
std::unique(r.begin(),r.end()); std::unique(r.begin(),r.end());
_ifaddrs = r;
return r; return r;
} }

View File

@ -71,6 +71,8 @@ private:
int _fd; int _fd;
int _shutdownSignalPipe[2]; int _shutdownSignalPipe[2];
volatile bool _enabled; volatile bool _enabled;
mutable std::vector<InetAddress> _ifaddrs;
mutable uint64_t _lastIfAddrsUpdate;
}; };
} // namespace ZeroTier } // namespace ZeroTier