Browse Source

cache getifaddrs - bsd

travis laduke 2 years ago
parent
commit
60d2138f30
2 changed files with 13 additions and 1 deletions
  1. 11 1
      osdep/BSDEthernetTap.cpp
  2. 2 0
      osdep/BSDEthernetTap.hpp

+ 11 - 1
osdep/BSDEthernetTap.cpp

@@ -74,7 +74,8 @@ BSDEthernetTap::BSDEthernetTap(
 	_mtu(mtu),
 	_metric(metric),
 	_fd(0),
-	_enabled(true)
+	_enabled(true),
+	_lastIfAddrsUpdate(0)
 {
 	static Mutex globalTapCreateLock;
 	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
 {
+	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>();
@@ -320,6 +328,8 @@ std::vector<InetAddress> BSDEthernetTap::ips() const
 	std::sort(r.begin(),r.end());
 	std::unique(r.begin(),r.end());
 
+	_ifaddrs = r;
+
 	return r;
 }
 

+ 2 - 0
osdep/BSDEthernetTap.hpp

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