Browse Source

Binder for Windows, and use Binder to get local interface addresses to advertise.

Adam Ierymenko 9 years ago
parent
commit
504738b183
2 changed files with 50 additions and 72 deletions
  1. 40 3
      osdep/Binder.hpp
  2. 10 69
      service/OneService.cpp

+ 40 - 3
osdep/Binder.hpp

@@ -136,11 +136,37 @@ public:
 
 
 #ifdef __WINDOWS__
 #ifdef __WINDOWS__
 
 
+		char aabuf[32768];
+		ULONG aalen = sizeof(aabuf);
+		if (GetAdaptersAddresses(AF_UNSPEC,GAA_FLAG_SKIP_ANYCAST|GAA_FLAG_SKIP_MULTICAST|GAA_FLAG_SKIP_DNS_SERVER,(void *)0,reinterpret_cast<PIP_ADAPTER_ADDRESSES>(aabuf),&aalen) == NO_ERROR) {
+			PIP_ADAPTER_ADDRESSES a = reinterpret_cast<PIP_ADAPTER_ADDRESSES>(aabuf);
+			while (a) {
+				PIP_ADAPTER_UNICAST_ADDRESS ua = a->FirstUnicastAddress;
+				while (ua) {
+					InetAddress ip(ua->Address.lpSockaddr);
+					if (ifChecker.shouldBindInterface("",ip)) {
+						switch(ip.ipScope()) {
+							default: break;
+							case InetAddress::IP_SCOPE_PSEUDOPRIVATE:
+							case InetAddress::IP_SCOPE_GLOBAL:
+							//case InetAddress::IP_SCOPE_LINK_LOCAL:
+							case InetAddress::IP_SCOPE_SHARED:
+							case InetAddress::IP_SCOPE_PRIVATE:
+								ip.setPort(port);
+								localIfAddrs.push_back(ip);
+								break;
+						}
+					}
+					ua = ua->Next;
+				}
+				a = a->Next;
+			}
+		}
+
 #else // not __WINDOWS__
 #else // not __WINDOWS__
 
 
 		struct ifaddrs *ifatbl = (struct ifaddrs *)0;
 		struct ifaddrs *ifatbl = (struct ifaddrs *)0;
 		struct ifaddrs *ifa;
 		struct ifaddrs *ifa;
-
 		if ((getifaddrs(&ifatbl) == 0)&&(ifatbl)) {
 		if ((getifaddrs(&ifatbl) == 0)&&(ifatbl)) {
 			ifa = ifatbl;
 			ifa = ifatbl;
 			while (ifa) {
 			while (ifa) {
@@ -162,10 +188,9 @@ public:
 				}
 				}
 				ifa = ifa->ifa_next;
 				ifa = ifa->ifa_next;
 			}
 			}
+			freeifaddrs(ifatbl);
 		}
 		}
 
 
-		freeifaddrs(ifatbl);
-
 #endif
 #endif
 
 
 		// Default to binding to wildcard if we can't enumerate addresses
 		// Default to binding to wildcard if we can't enumerate addresses
@@ -274,6 +299,18 @@ public:
 		}
 		}
 	}
 	}
 
 
+	/**
+	 * @return All currently bound local interface addresses
+	 */
+	inline std::vector<InetAddress> allBoundLocalInterfaceAddresses()
+	{
+		Mutex::Lock _l(_lock);
+		std::vector<InetAddress> aa;
+		for(std::vector<_Binding>::const_iterator i(_bindings.begin());i!=_bindings.end();++i)
+			aa.push_back(i->address);
+		return aa;
+	}
+
 private:
 private:
 	std::vector<_Binding> _bindings;
 	std::vector<_Binding> _bindings;
 	Mutex _lock;
 	Mutex _lock;

+ 10 - 69
service/OneService.cpp

@@ -877,78 +877,19 @@ public:
 				if ((now - lastLocalInterfaceAddressCheck) >= ZT_LOCAL_INTERFACE_CHECK_INTERVAL) {
 				if ((now - lastLocalInterfaceAddressCheck) >= ZT_LOCAL_INTERFACE_CHECK_INTERVAL) {
 					lastLocalInterfaceAddressCheck = now;
 					lastLocalInterfaceAddressCheck = now;
 
 
-				_node->clearLocalInterfaceAddresses();
-#ifdef ZT_USE_MINIUPNPC
-				if (_portMapper) {
-					std::vector<InetAddress> mappedAddresses(_portMapper->get());
-					for(std::vector<InetAddress>::const_iterator ext(mappedAddresses.begin());ext!=mappedAddresses.end();++ext)
-						_node->addLocalInterfaceAddress(reinterpret_cast<const struct sockaddr_storage *>(&(*ext)));
-				}
-#endif
+					_node->clearLocalInterfaceAddresses();
 
 
-#ifdef __UNIX_LIKE__
-					std::vector<std::string> ztDevices;
-					{
-						Mutex::Lock _l(_taps_m);
-						for(std::map< uint64_t,EthernetTap *>::const_iterator t(_taps.begin());t!=_taps.end();++t)
-							ztDevices.push_back(t->second->deviceName());
-					}
-					struct ifaddrs *ifatbl = (struct ifaddrs *)0;
-					if ((getifaddrs(&ifatbl) == 0)&&(ifatbl)) {
-						struct ifaddrs *ifa = ifatbl;
-						while (ifa) {
-							if ((ifa->ifa_name)&&(ifa->ifa_addr)&&(!isBlacklistedLocalInterfaceForZeroTierTraffic(ifa->ifa_name))) {
-								bool isZT = false;
-								for(std::vector<std::string>::const_iterator d(ztDevices.begin());d!=ztDevices.end();++d) {
-									if (*d == ifa->ifa_name) {
-										isZT = true;
-										break;
-									}
-								}
-								if (!isZT) {
-									InetAddress ip(ifa->ifa_addr);
-									ip.setPort(_ports[0]);
-									_node->addLocalInterfaceAddress(reinterpret_cast<const struct sockaddr_storage *>(&ip));
-								}
-							}
-							ifa = ifa->ifa_next;
-						}
-						freeifaddrs(ifatbl);
+#ifdef ZT_USE_MINIUPNPC
+					if (_portMapper) {
+						std::vector<InetAddress> mappedAddresses(_portMapper->get());
+						for(std::vector<InetAddress>::const_iterator ext(mappedAddresses.begin());ext!=mappedAddresses.end();++ext)
+							_node->addLocalInterfaceAddress(reinterpret_cast<const struct sockaddr_storage *>(&(*ext)));
 					}
 					}
-#endif // __UNIX_LIKE__
+#endif
 
 
-#ifdef __WINDOWS__
-					std::vector<NET_LUID> ztDevices;
-					{
-						Mutex::Lock _l(_taps_m);
-						for(std::map< uint64_t,EthernetTap *>::const_iterator t(_taps.begin());t!=_taps.end();++t)
-							ztDevices.push_back(t->second->luid());
-					}
-					char aabuf[16384];
-					ULONG aalen = sizeof(aabuf);
-					if (GetAdaptersAddresses(AF_UNSPEC,GAA_FLAG_SKIP_ANYCAST|GAA_FLAG_SKIP_MULTICAST|GAA_FLAG_SKIP_DNS_SERVER,(void *)0,reinterpret_cast<PIP_ADAPTER_ADDRESSES>(aabuf),&aalen) == NO_ERROR) {
-						PIP_ADAPTER_ADDRESSES a = reinterpret_cast<PIP_ADAPTER_ADDRESSES>(aabuf);
-						while (a) {
-							bool isZT = false;
-							for(std::vector<NET_LUID>::const_iterator d(ztDevices.begin());d!=ztDevices.end();++d) {
-								if (a->Luid.Value == d->Value) {
-									isZT = true;
-									break;
-								}
-							}
-							if (!isZT) {
-								PIP_ADAPTER_UNICAST_ADDRESS ua = a->FirstUnicastAddress;
-								while (ua) {
-									InetAddress ip(ua->Address.lpSockaddr);
-									ip.setPort(_port);
-									_node->addLocalInterfaceAddress(reinterpret_cast<const struct sockaddr_storage *>(&ip));
-									ua = ua->Next;
-								}
-							}
-							a = a->Next;
-						}
-					}
-#endif // __WINDOWS__
+					std::vector<InetAddress> boundAddrs(_bindings[0].allBoundLocalInterfaceAddresses());
+					for(std::vector<InetAddress>::const_iterator i(boundAddrs.begin());i!=boundAddrs.end();++i)
+						_node->addLocalInterfaceAddress(reinterpret_cast<const struct sockaddr_storage *>(&(*i)));
 				}
 				}
 
 
 				const unsigned long delay = (dl > now) ? (unsigned long)(dl - now) : 100;
 				const unsigned long delay = (dl > now) ? (unsigned long)(dl - now) : 100;