ScanIpIfAddrs.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* Copyright 2016, Ableton AG, Berlin. All rights reserved.
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * If you would like to incorporate Link into a proprietary software application,
  17. * please contact <link-devs@ableton.com>.
  18. */
  19. #pragma once
  20. #include <ableton/discovery/AsioTypes.hpp>
  21. #include <iphlpapi.h>
  22. #include <map>
  23. #include <stdio.h>
  24. #include <string>
  25. #include <vector>
  26. #include <winsock2.h>
  27. #include <ws2tcpip.h>
  28. #pragma comment(lib, "iphlpapi.lib")
  29. #pragma comment(lib, "ws2_32.lib")
  30. namespace ableton
  31. {
  32. namespace platforms
  33. {
  34. namespace windows
  35. {
  36. namespace detail
  37. {
  38. // RAII type to make [get,free]ifaddrs function pairs exception safe
  39. class GetIfAddrs
  40. {
  41. public:
  42. GetIfAddrs()
  43. {
  44. const int MAX_TRIES = 3; // MSFT recommendation
  45. const int WORKING_BUFFER_SIZE = 15000; // MSFT recommendation
  46. DWORD adapter_addrs_buffer_size = WORKING_BUFFER_SIZE;
  47. for (int i = 0; i < MAX_TRIES; i++)
  48. {
  49. adapter_addrs = (IP_ADAPTER_ADDRESSES*)malloc(adapter_addrs_buffer_size);
  50. assert(adapter_addrs);
  51. DWORD error =
  52. ::GetAdaptersAddresses(AF_UNSPEC,
  53. GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST
  54. | GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_FRIENDLY_NAME,
  55. NULL,
  56. adapter_addrs,
  57. &adapter_addrs_buffer_size);
  58. if (error == ERROR_SUCCESS)
  59. {
  60. break;
  61. }
  62. // if buffer too small, use new buffer size in next iteration
  63. if (error == ERROR_BUFFER_OVERFLOW)
  64. {
  65. free(adapter_addrs);
  66. adapter_addrs = NULL;
  67. continue;
  68. }
  69. }
  70. }
  71. ~GetIfAddrs()
  72. {
  73. if (adapter_addrs)
  74. free(adapter_addrs);
  75. }
  76. // RAII must not copy
  77. GetIfAddrs(GetIfAddrs&) = delete;
  78. GetIfAddrs& operator=(GetIfAddrs&) = delete;
  79. template <typename Function>
  80. void withIfAddrs(Function f)
  81. {
  82. if (adapter_addrs)
  83. f(*adapter_addrs);
  84. }
  85. private:
  86. IP_ADAPTER_ADDRESSES* adapter_addrs;
  87. IP_ADAPTER_ADDRESSES* adapter;
  88. };
  89. } // namespace detail
  90. struct ScanIpIfAddrs
  91. {
  92. // Scan active network interfaces and return corresponding addresses
  93. // for all ip-based interfaces.
  94. std::vector<discovery::IpAddress> operator()()
  95. {
  96. std::vector<discovery::IpAddress> addrs;
  97. std::map<std::string, discovery::IpAddress> IpInterfaceNames;
  98. detail::GetIfAddrs getIfAddrs;
  99. getIfAddrs.withIfAddrs(
  100. [&](const IP_ADAPTER_ADDRESSES& interfaces)
  101. {
  102. const IP_ADAPTER_ADDRESSES* networkInterface;
  103. for (networkInterface = &interfaces; networkInterface;
  104. networkInterface = networkInterface->Next)
  105. {
  106. for (IP_ADAPTER_UNICAST_ADDRESS* address =
  107. networkInterface->FirstUnicastAddress;
  108. NULL != address;
  109. address = address->Next)
  110. {
  111. auto family = address->Address.lpSockaddr->sa_family;
  112. if (AF_INET == family)
  113. {
  114. // IPv4
  115. SOCKADDR_IN* addr4 =
  116. reinterpret_cast<SOCKADDR_IN*>(address->Address.lpSockaddr);
  117. const auto bytes = reinterpret_cast<const char*>(&addr4->sin_addr);
  118. const auto ipv4address =
  119. discovery::makeAddressFromBytes<discovery::IpAddressV4>(bytes);
  120. addrs.emplace_back(ipv4address);
  121. IpInterfaceNames.insert(
  122. std::make_pair(networkInterface->AdapterName, ipv4address));
  123. }
  124. }
  125. }
  126. });
  127. // Add IPv6 addresses as backup in case multicast on IPv4 fails
  128. getIfAddrs.withIfAddrs(
  129. [&](const IP_ADAPTER_ADDRESSES& interfaces)
  130. {
  131. const IP_ADAPTER_ADDRESSES* networkInterface;
  132. for (networkInterface = &interfaces; networkInterface;
  133. networkInterface = networkInterface->Next)
  134. {
  135. for (IP_ADAPTER_UNICAST_ADDRESS* address =
  136. networkInterface->FirstUnicastAddress;
  137. NULL != address;
  138. address = address->Next)
  139. {
  140. auto family = address->Address.lpSockaddr->sa_family;
  141. if (AF_INET6 == family
  142. && IpInterfaceNames.find(networkInterface->AdapterName)
  143. != IpInterfaceNames.end())
  144. {
  145. SOCKADDR_IN6* sockAddr =
  146. reinterpret_cast<SOCKADDR_IN6*>(address->Address.lpSockaddr);
  147. const auto scopeId = sockAddr->sin6_scope_id;
  148. const auto bytes = reinterpret_cast<const char*>(&sockAddr->sin6_addr);
  149. const auto addr6 =
  150. discovery::makeAddressFromBytes<discovery::IpAddressV6>(bytes, scopeId);
  151. if (!addr6.is_loopback() && addr6.is_link_local())
  152. {
  153. addrs.emplace_back(addr6);
  154. }
  155. }
  156. }
  157. }
  158. });
  159. return addrs;
  160. }
  161. };
  162. } // namespace windows
  163. } // namespace platforms
  164. } // namespace ableton