util.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. #include <cstdlib>
  2. #include <string>
  3. #include <boost/asio.hpp>
  4. #include "util.h"
  5. #include "Log.h"
  6. #ifdef WIN32
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <sysinfoapi.h>
  11. #include <winsock2.h>
  12. #include <ws2tcpip.h>
  13. #include <iphlpapi.h>
  14. #include <shlobj.h>
  15. #ifdef _MSC_VER
  16. #pragma comment(lib, "IPHLPAPI.lib")
  17. #endif // _MSC_VER
  18. #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
  19. #define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
  20. // inet_pton exists Windows since Vista, but XP haven't that function!
  21. // This function was written by Petar Korponai?. See http://stackoverflow.com/questions/15660203/inet-pton-identifier-not-found
  22. int inet_pton_xp(int af, const char *src, void *dst)
  23. {
  24. struct sockaddr_storage ss;
  25. int size = sizeof (ss);
  26. char src_copy[INET6_ADDRSTRLEN + 1];
  27. ZeroMemory (&ss, sizeof (ss));
  28. strncpy (src_copy, src, INET6_ADDRSTRLEN + 1);
  29. src_copy[INET6_ADDRSTRLEN] = 0;
  30. if (WSAStringToAddress (src_copy, af, NULL, (struct sockaddr *)&ss, &size) == 0)
  31. {
  32. switch (af)
  33. {
  34. case AF_INET:
  35. *(struct in_addr *)dst = ((struct sockaddr_in *)&ss)->sin_addr;
  36. return 1;
  37. case AF_INET6:
  38. *(struct in6_addr *)dst = ((struct sockaddr_in6 *)&ss)->sin6_addr;
  39. return 1;
  40. }
  41. }
  42. return 0;
  43. }
  44. #else /* !WIN32 => UNIX */
  45. #include <sys/types.h>
  46. #include <ifaddrs.h>
  47. #endif
  48. namespace i2p
  49. {
  50. namespace util
  51. {
  52. namespace net
  53. {
  54. #ifdef WIN32
  55. bool IsWindowsXPorLater()
  56. {
  57. OSVERSIONINFO osvi;
  58. ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
  59. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  60. GetVersionEx(&osvi);
  61. if (osvi.dwMajorVersion <= 5)
  62. return true;
  63. else
  64. return false;
  65. }
  66. int GetMTUWindowsIpv4(sockaddr_in inputAddress, int fallback)
  67. {
  68. ULONG outBufLen = 0;
  69. PIP_ADAPTER_ADDRESSES pAddresses = nullptr;
  70. PIP_ADAPTER_ADDRESSES pCurrAddresses = nullptr;
  71. PIP_ADAPTER_UNICAST_ADDRESS pUnicast = nullptr;
  72. if(GetAdaptersAddresses(AF_INET, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen)
  73. == ERROR_BUFFER_OVERFLOW)
  74. {
  75. FREE(pAddresses);
  76. pAddresses = (IP_ADAPTER_ADDRESSES*) MALLOC(outBufLen);
  77. }
  78. DWORD dwRetVal = GetAdaptersAddresses(
  79. AF_INET, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen
  80. );
  81. if(dwRetVal != NO_ERROR)
  82. {
  83. LogPrint(eLogError, "NetIface: GetMTU(): enclosed GetAdaptersAddresses() call has failed");
  84. FREE(pAddresses);
  85. return fallback;
  86. }
  87. pCurrAddresses = pAddresses;
  88. while(pCurrAddresses)
  89. {
  90. PIP_ADAPTER_UNICAST_ADDRESS firstUnicastAddress = pCurrAddresses->FirstUnicastAddress;
  91. pUnicast = pCurrAddresses->FirstUnicastAddress;
  92. if(pUnicast == nullptr)
  93. LogPrint(eLogError, "NetIface: GetMTU(): not a unicast ipv4 address, this is not supported");
  94. for(int i = 0; pUnicast != nullptr; ++i)
  95. {
  96. LPSOCKADDR lpAddr = pUnicast->Address.lpSockaddr;
  97. sockaddr_in* localInterfaceAddress = (sockaddr_in*) lpAddr;
  98. if(localInterfaceAddress->sin_addr.S_un.S_addr == inputAddress.sin_addr.S_un.S_addr)
  99. {
  100. auto result = pAddresses->Mtu;
  101. FREE(pAddresses);
  102. return result;
  103. }
  104. pUnicast = pUnicast->Next;
  105. }
  106. pCurrAddresses = pCurrAddresses->Next;
  107. }
  108. LogPrint(eLogError, "NetIface: GetMTU(): no usable unicast ipv4 addresses found");
  109. FREE(pAddresses);
  110. return fallback;
  111. }
  112. int GetMTUWindowsIpv6(sockaddr_in6 inputAddress, int fallback)
  113. {
  114. ULONG outBufLen = 0;
  115. PIP_ADAPTER_ADDRESSES pAddresses = nullptr;
  116. PIP_ADAPTER_ADDRESSES pCurrAddresses = nullptr;
  117. PIP_ADAPTER_UNICAST_ADDRESS pUnicast = nullptr;
  118. if(GetAdaptersAddresses(AF_INET6, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen)
  119. == ERROR_BUFFER_OVERFLOW)
  120. {
  121. FREE(pAddresses);
  122. pAddresses = (IP_ADAPTER_ADDRESSES*) MALLOC(outBufLen);
  123. }
  124. DWORD dwRetVal = GetAdaptersAddresses(
  125. AF_INET6, GAA_FLAG_INCLUDE_PREFIX, nullptr, pAddresses, &outBufLen
  126. );
  127. if(dwRetVal != NO_ERROR)
  128. {
  129. LogPrint(eLogError, "NetIface: GetMTU(): enclosed GetAdaptersAddresses() call has failed");
  130. FREE(pAddresses);
  131. return fallback;
  132. }
  133. bool found_address = false;
  134. pCurrAddresses = pAddresses;
  135. while(pCurrAddresses)
  136. {
  137. PIP_ADAPTER_UNICAST_ADDRESS firstUnicastAddress = pCurrAddresses->FirstUnicastAddress;
  138. pUnicast = pCurrAddresses->FirstUnicastAddress;
  139. if(pUnicast == nullptr)
  140. LogPrint(eLogError, "NetIface: GetMTU(): not a unicast ipv6 address, this is not supported");
  141. for(int i = 0; pUnicast != nullptr; ++i)
  142. {
  143. LPSOCKADDR lpAddr = pUnicast->Address.lpSockaddr;
  144. sockaddr_in6 *localInterfaceAddress = (sockaddr_in6*) lpAddr;
  145. for (int j = 0; j != 8; ++j)
  146. {
  147. if (localInterfaceAddress->sin6_addr.u.Word[j] != inputAddress.sin6_addr.u.Word[j])
  148. break;
  149. else
  150. found_address = true;
  151. }
  152. if (found_address)
  153. {
  154. auto result = pAddresses->Mtu;
  155. FREE(pAddresses);
  156. pAddresses = nullptr;
  157. return result;
  158. }
  159. pUnicast = pUnicast->Next;
  160. }
  161. pCurrAddresses = pCurrAddresses->Next;
  162. }
  163. LogPrint(eLogError, "NetIface: GetMTU(): no usable unicast ipv6 addresses found");
  164. FREE(pAddresses);
  165. return fallback;
  166. }
  167. int GetMTUWindows(const boost::asio::ip::address& localAddress, int fallback)
  168. {
  169. #ifdef UNICODE
  170. string localAddress_temporary = localAddress.to_string();
  171. wstring localAddressUniversal(localAddress_temporary.begin(), localAddress_temporary.end());
  172. #else
  173. std::string localAddressUniversal = localAddress.to_string();
  174. #endif
  175. if (IsWindowsXPorLater())
  176. {
  177. #define inet_pton inet_pton_xp
  178. }
  179. if(localAddress.is_v4())
  180. {
  181. sockaddr_in inputAddress;
  182. inet_pton(AF_INET, localAddressUniversal.c_str(), &(inputAddress.sin_addr));
  183. return GetMTUWindowsIpv4(inputAddress, fallback);
  184. }
  185. else if(localAddress.is_v6())
  186. {
  187. sockaddr_in6 inputAddress;
  188. inet_pton(AF_INET6, localAddressUniversal.c_str(), &(inputAddress.sin6_addr));
  189. return GetMTUWindowsIpv6(inputAddress, fallback);
  190. } else {
  191. LogPrint(eLogError, "NetIface: GetMTU(): address family is not supported");
  192. return fallback;
  193. }
  194. }
  195. #else // assume unix
  196. int GetMTUUnix(const boost::asio::ip::address& localAddress, int fallback)
  197. {
  198. ifaddrs* ifaddr, *ifa = nullptr;
  199. if(getifaddrs(&ifaddr) == -1)
  200. {
  201. LogPrint(eLogError, "NetIface: Can't call getifaddrs(): ", strerror(errno));
  202. return fallback;
  203. }
  204. int family = 0;
  205. // look for interface matching local address
  206. for(ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next)
  207. {
  208. if(!ifa->ifa_addr)
  209. continue;
  210. family = ifa->ifa_addr->sa_family;
  211. if(family == AF_INET && localAddress.is_v4())
  212. {
  213. sockaddr_in* sa = (sockaddr_in*) ifa->ifa_addr;
  214. if(!memcmp(&sa->sin_addr, localAddress.to_v4().to_bytes().data(), 4))
  215. break; // address matches
  216. }
  217. else if(family == AF_INET6 && localAddress.is_v6())
  218. {
  219. sockaddr_in6* sa = (sockaddr_in6*) ifa->ifa_addr;
  220. if(!memcmp(&sa->sin6_addr, localAddress.to_v6().to_bytes().data(), 16))
  221. break; // address matches
  222. }
  223. }
  224. int mtu = fallback;
  225. if(ifa && family)
  226. { // interface found?
  227. int fd = socket(family, SOCK_DGRAM, 0);
  228. if(fd > 0)
  229. {
  230. ifreq ifr;
  231. strncpy(ifr.ifr_name, ifa->ifa_name, IFNAMSIZ); // set interface for query
  232. if(ioctl(fd, SIOCGIFMTU, &ifr) >= 0)
  233. mtu = ifr.ifr_mtu; // MTU
  234. else
  235. LogPrint (eLogError, "NetIface: Failed to run ioctl: ", strerror(errno));
  236. close(fd);
  237. }
  238. else
  239. LogPrint(eLogError, "NetIface: Failed to create datagram socket");
  240. }
  241. else
  242. LogPrint(eLogWarning, "NetIface: interface for local address", localAddress.to_string(), " not found");
  243. freeifaddrs(ifaddr);
  244. return mtu;
  245. }
  246. #endif // WIN32
  247. int GetMTU(const boost::asio::ip::address& localAddress)
  248. {
  249. const int fallback = 576; // fallback MTU
  250. #ifdef WIN32
  251. return GetMTUWindows(localAddress, fallback);
  252. #else
  253. return GetMTUUnix(localAddress, fallback);
  254. #endif
  255. return fallback;
  256. }
  257. const boost::asio::ip::address GetInterfaceAddress(const std::string & ifname, bool ipv6)
  258. {
  259. #ifdef WIN32
  260. LogPrint(eLogError, "NetIface: cannot get address by interface name, not implemented on WIN32");
  261. if(ipv6)
  262. return boost::asio::ip::address::from_string("::1");
  263. else
  264. return boost::asio::ip::address::from_string("127.0.0.1");
  265. #else
  266. int af = (ipv6 ? AF_INET6 : AF_INET);
  267. ifaddrs * addrs = nullptr;
  268. if(getifaddrs(&addrs) == 0)
  269. {
  270. // got ifaddrs
  271. ifaddrs * cur = addrs;
  272. while(cur)
  273. {
  274. std::string cur_ifname(cur->ifa_name);
  275. if (cur_ifname == ifname && cur->ifa_addr && cur->ifa_addr->sa_family == af)
  276. {
  277. // match
  278. char * addr = new char[INET6_ADDRSTRLEN];
  279. bzero(addr, INET6_ADDRSTRLEN);
  280. if(af == AF_INET)
  281. inet_ntop(af, &((sockaddr_in *)cur->ifa_addr)->sin_addr, addr, INET6_ADDRSTRLEN);
  282. else
  283. inet_ntop(af, &((sockaddr_in6 *)cur->ifa_addr)->sin6_addr, addr, INET6_ADDRSTRLEN);
  284. freeifaddrs(addrs);
  285. std::string cur_ifaddr(addr);
  286. delete[] addr;
  287. return boost::asio::ip::address::from_string(cur_ifaddr);
  288. }
  289. cur = cur->ifa_next;
  290. }
  291. }
  292. if(addrs) freeifaddrs(addrs);
  293. std::string fallback;
  294. if(ipv6)
  295. {
  296. fallback = "::1";
  297. LogPrint(eLogWarning, "NetIface: cannot find ipv6 address for interface ", ifname);
  298. } else {
  299. fallback = "127.0.0.1";
  300. LogPrint(eLogWarning, "NetIface: cannot find ipv4 address for interface ", ifname);
  301. }
  302. return boost::asio::ip::address::from_string(fallback);
  303. #endif
  304. }
  305. }
  306. } // util
  307. } // i2p