You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
4.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2008, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/base/nethelpers.h"
  28. #if defined(WIN32)
  29. #include <ws2spi.h>
  30. #include <ws2tcpip.h>
  31. #include "talk/base/win32.h"
  32. #endif
  33. #include "talk/base/byteorder.h"
  34. #include "talk/base/signalthread.h"
  35. namespace talk_base {
  36. int ResolveHostname(const std::string& hostname, int family,
  37. std::vector<IPAddress>* addresses) {
  38. if (!addresses) {
  39. return -1;
  40. }
  41. addresses->clear();
  42. struct addrinfo* result = NULL;
  43. struct addrinfo hints = {0};
  44. // TODO(djw): For now this is IPv4 only so existing users remain unaffected.
  45. hints.ai_family = AF_INET;
  46. hints.ai_flags = AI_ADDRCONFIG;
  47. int ret = getaddrinfo(hostname.c_str(), NULL, &hints, &result);
  48. if (ret != 0) {
  49. return ret;
  50. }
  51. struct addrinfo* cursor = result;
  52. for (; cursor; cursor = cursor->ai_next) {
  53. if (family == AF_UNSPEC || cursor->ai_family == family) {
  54. IPAddress ip;
  55. if (IPFromAddrInfo(cursor, &ip)) {
  56. addresses->push_back(ip);
  57. }
  58. }
  59. }
  60. freeaddrinfo(result);
  61. return 0;
  62. }
  63. // AsyncResolver
  64. AsyncResolver::AsyncResolver() : error_(0) {
  65. }
  66. void AsyncResolver::DoWork() {
  67. error_ = ResolveHostname(addr_.hostname().c_str(), addr_.family(),
  68. &addresses_);
  69. }
  70. void AsyncResolver::OnWorkDone() {
  71. if (addresses_.size() > 0) {
  72. addr_.SetIP(addresses_[0]);
  73. }
  74. }
  75. const char* inet_ntop(int af, const void *src, char* dst, socklen_t size) {
  76. #ifdef WIN32
  77. return win32_inet_ntop(af, src, dst, size);
  78. #else
  79. return ::inet_ntop(af, src, dst, size);
  80. #endif
  81. }
  82. int inet_pton(int af, const char* src, void *dst) {
  83. #ifdef WIN32
  84. return win32_inet_pton(af, src, dst);
  85. #else
  86. return ::inet_pton(af, src, dst);
  87. #endif
  88. }
  89. bool HasIPv6Enabled() {
  90. #ifndef WIN32
  91. // We only need to check this for Windows XP (so far).
  92. return true;
  93. #else
  94. if (IsWindowsVistaOrLater()) {
  95. return true;
  96. }
  97. if (!IsWindowsXpOrLater()) {
  98. return false;
  99. }
  100. DWORD protbuff_size = 4096;
  101. scoped_array<char> protocols;
  102. LPWSAPROTOCOL_INFOW protocol_infos = NULL;
  103. int requested_protocols[2] = {AF_INET6, 0};
  104. int err = 0;
  105. int ret = 0;
  106. // Check for protocols in a do-while loop until we provide a buffer large
  107. // enough. (WSCEnumProtocols sets protbuff_size to its desired value).
  108. // It is extremely unlikely that this will loop more than once.
  109. do {
  110. protocols.reset(new char[protbuff_size]);
  111. protocol_infos = reinterpret_cast<LPWSAPROTOCOL_INFOW>(protocols.get());
  112. ret = WSCEnumProtocols(requested_protocols, protocol_infos,
  113. &protbuff_size, &err);
  114. } while (ret == SOCKET_ERROR && err == WSAENOBUFS);
  115. if (ret == SOCKET_ERROR) {
  116. return false;
  117. }
  118. // Even if ret is positive, check specifically for IPv6.
  119. // Non-IPv6 enabled WinXP will still return a RAW protocol.
  120. for (int i = 0; i < ret; ++i) {
  121. if (protocol_infos[i].iAddressFamily == AF_INET6) {
  122. return true;
  123. }
  124. }
  125. return false;
  126. #endif
  127. }
  128. } // namespace talk_base