From 504738b1839ca7f38ea4421dbe142a6192f6c2d4 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Tue, 5 Apr 2016 15:53:19 -0700 Subject: Binder for Windows, and use Binder to get local interface addresses to advertise. --- osdep/Binder.hpp | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) (limited to 'osdep') diff --git a/osdep/Binder.hpp b/osdep/Binder.hpp index 3f2749d7..b68e6dac 100644 --- a/osdep/Binder.hpp +++ b/osdep/Binder.hpp @@ -136,11 +136,37 @@ public: #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(aabuf),&aalen) == NO_ERROR) { + PIP_ADAPTER_ADDRESSES a = reinterpret_cast(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__ struct ifaddrs *ifatbl = (struct ifaddrs *)0; struct ifaddrs *ifa; - if ((getifaddrs(&ifatbl) == 0)&&(ifatbl)) { ifa = ifatbl; while (ifa) { @@ -162,10 +188,9 @@ public: } ifa = ifa->ifa_next; } + freeifaddrs(ifatbl); } - freeifaddrs(ifatbl); - #endif // 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 allBoundLocalInterfaceAddresses() + { + Mutex::Lock _l(_lock); + std::vector aa; + for(std::vector<_Binding>::const_iterator i(_bindings.begin());i!=_bindings.end();++i) + aa.push_back(i->address); + return aa; + } + private: std::vector<_Binding> _bindings; Mutex _lock; -- cgit v1.2.3