Sfoglia il codice sorgente

cache getifaddrs - windows

travis laduke 2 anni fa
parent
commit
e11d70e408
2 ha cambiato i file con 15 aggiunte e 1 eliminazioni
  1. 12 1
      osdep/WindowsEthernetTap.cpp
  2. 3 0
      osdep/WindowsEthernetTap.hpp

+ 12 - 1
osdep/WindowsEthernetTap.cpp

@@ -467,7 +467,8 @@ WindowsEthernetTap::WindowsEthernetTap(
 	_pathToHelpers(hp),
 	_pathToHelpers(hp),
 	_run(true),
 	_run(true),
 	_initialized(false),
 	_initialized(false),
-	_enabled(true)
+	_enabled(true),
+	_lastIfAddrsUpdate(0)
 {
 {
 	char subkeyName[1024];
 	char subkeyName[1024];
 	char subkeyClass[1024];
 	char subkeyClass[1024];
@@ -749,6 +750,14 @@ std::vector<InetAddress> WindowsEthernetTap::ips() const
 	if (!_initialized)
 	if (!_initialized)
 		return addrs;
 		return addrs;
 
 
+	uint64_t now = OSUtils::now();
+
+	if ((now - _lastIfAddrsUpdate) <= GETIFADDRS_CACHE_TIME) {
+		return _ifaddrs;
+	}
+
+	_lastIfAddrsUpdate = now;
+
 	try {
 	try {
 		MIB_UNICASTIPADDRESS_TABLE *ipt = (MIB_UNICASTIPADDRESS_TABLE *)0;
 		MIB_UNICASTIPADDRESS_TABLE *ipt = (MIB_UNICASTIPADDRESS_TABLE *)0;
 		if (GetUnicastIpAddressTable(AF_UNSPEC,&ipt) == NO_ERROR) {
 		if (GetUnicastIpAddressTable(AF_UNSPEC,&ipt) == NO_ERROR) {
@@ -777,6 +786,8 @@ std::vector<InetAddress> WindowsEthernetTap::ips() const
 	std::sort(addrs.begin(),addrs.end());
 	std::sort(addrs.begin(),addrs.end());
 	addrs.erase(std::unique(addrs.begin(),addrs.end()),addrs.end());
 	addrs.erase(std::unique(addrs.begin(),addrs.end()),addrs.end());
 
 
+	_ifaddrs = addrs;
+
 	return addrs;
 	return addrs;
 }
 }
 
 

+ 3 - 0
osdep/WindowsEthernetTap.hpp

@@ -152,6 +152,9 @@ private:
 	volatile bool _run;
 	volatile bool _run;
 	volatile bool _initialized;
 	volatile bool _initialized;
 	volatile bool _enabled;
 	volatile bool _enabled;
+
+	mutable std::vector<InetAddress> _ifaddrs;
+	mutable uint64_t _lastIfAddrsUpdate;
 };
 };
 
 
 } // namespace ZeroTier
 } // namespace ZeroTier