henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "webrtc/base/nethelpers.h" |
| 12 | |
jbauch | 1286d0e | 2016-04-26 10:13:22 | [diff] [blame] | 13 | #include <memory> |
| 14 | |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 15 | #if defined(WEBRTC_WIN) |
| 16 | #include <ws2spi.h> |
| 17 | #include <ws2tcpip.h> |
| 18 | #include "webrtc/base/win32.h" |
| 19 | #endif |
Taylor Brandstetter | f382d5d | 2016-05-19 21:57:31 | [diff] [blame] | 20 | #if defined(WEBRTC_POSIX) && !defined(__native_client__) |
| 21 | #if defined(WEBRTC_ANDROID) |
| 22 | #include "webrtc/base/ifaddrs-android.h" |
| 23 | #else |
| 24 | #include <ifaddrs.h> |
| 25 | #endif |
| 26 | #endif // defined(WEBRTC_POSIX) && !defined(__native_client__) |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 27 | |
| 28 | #include "webrtc/base/byteorder.h" |
| 29 | #include "webrtc/base/logging.h" |
| 30 | #include "webrtc/base/signalthread.h" |
| 31 | |
| 32 | namespace rtc { |
| 33 | |
| 34 | int ResolveHostname(const std::string& hostname, int family, |
| 35 | std::vector<IPAddress>* addresses) { |
| 36 | #ifdef __native_client__ |
| 37 | ASSERT(false); |
| 38 | LOG(LS_WARNING) << "ResolveHostname() is not implemented for NaCl"; |
| 39 | return -1; |
| 40 | #else // __native_client__ |
| 41 | if (!addresses) { |
| 42 | return -1; |
| 43 | } |
| 44 | addresses->clear(); |
| 45 | struct addrinfo* result = NULL; |
| 46 | struct addrinfo hints = {0}; |
Honghai Zhang | c2c83d8 | 2016-06-27 05:11:12 | [diff] [blame] | 47 | hints.ai_family = family; |
| 48 | // |family| here will almost always be AF_UNSPEC, because |family| comes from |
| 49 | // AsyncResolver::addr_.family(), which comes from a SocketAddress constructed |
| 50 | // with a hostname. When a SocketAddress is constructed with a hostname, its |
| 51 | // family is AF_UNSPEC. However, if someday in the future we construct |
| 52 | // a SocketAddress with both a hostname and a family other than AF_UNSPEC, |
| 53 | // then it would be possible to get a specific family value here. |
| 54 | |
| 55 | // The behavior of AF_UNSPEC is roughly "get both ipv4 and ipv6", as |
| 56 | // documented by the various operating systems: |
| 57 | // Linux: http://man7.org/linux/man-pages/man3/getaddrinfo.3.html |
| 58 | // Windows: https://msdn.microsoft.com/en-us/library/windows/desktop/ |
| 59 | // ms738520(v=vs.85).aspx |
| 60 | // Mac: https://developer.apple.com/legacy/library/documentation/Darwin/ |
| 61 | // Reference/ManPages/man3/getaddrinfo.3.html |
| 62 | // Android (source code, not documentation): |
| 63 | // https://android.googlesource.com/platform/bionic/+/ |
| 64 | // 7e0bfb511e85834d7c6cb9631206b62f82701d60/libc/netbsd/net/getaddrinfo.c#1657 |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 65 | hints.ai_flags = AI_ADDRCONFIG; |
| 66 | int ret = getaddrinfo(hostname.c_str(), NULL, &hints, &result); |
| 67 | if (ret != 0) { |
| 68 | return ret; |
| 69 | } |
| 70 | struct addrinfo* cursor = result; |
| 71 | for (; cursor; cursor = cursor->ai_next) { |
| 72 | if (family == AF_UNSPEC || cursor->ai_family == family) { |
| 73 | IPAddress ip; |
| 74 | if (IPFromAddrInfo(cursor, &ip)) { |
| 75 | addresses->push_back(ip); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | freeaddrinfo(result); |
| 80 | return 0; |
| 81 | #endif // !__native_client__ |
| 82 | } |
| 83 | |
| 84 | // AsyncResolver |
| 85 | AsyncResolver::AsyncResolver() : error_(-1) { |
| 86 | } |
| 87 | |
kwiberg@webrtc.org | 786b634 | 2015-03-09 22:21:53 | [diff] [blame] | 88 | AsyncResolver::~AsyncResolver() = default; |
| 89 | |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 90 | void AsyncResolver::Start(const SocketAddress& addr) { |
| 91 | addr_ = addr; |
| 92 | // SignalThred Start will kickoff the resolve process. |
| 93 | SignalThread::Start(); |
| 94 | } |
| 95 | |
| 96 | bool AsyncResolver::GetResolvedAddress(int family, SocketAddress* addr) const { |
| 97 | if (error_ != 0 || addresses_.empty()) |
| 98 | return false; |
| 99 | |
| 100 | *addr = addr_; |
| 101 | for (size_t i = 0; i < addresses_.size(); ++i) { |
| 102 | if (family == addresses_[i].family()) { |
| 103 | addr->SetResolvedIP(addresses_[i]); |
| 104 | return true; |
| 105 | } |
| 106 | } |
| 107 | return false; |
| 108 | } |
| 109 | |
kwiberg@webrtc.org | 786b634 | 2015-03-09 22:21:53 | [diff] [blame] | 110 | int AsyncResolver::GetError() const { |
| 111 | return error_; |
| 112 | } |
| 113 | |
| 114 | void AsyncResolver::Destroy(bool wait) { |
| 115 | SignalThread::Destroy(wait); |
| 116 | } |
| 117 | |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 118 | void AsyncResolver::DoWork() { |
| 119 | error_ = ResolveHostname(addr_.hostname().c_str(), addr_.family(), |
| 120 | &addresses_); |
| 121 | } |
| 122 | |
| 123 | void AsyncResolver::OnWorkDone() { |
| 124 | SignalDone(this); |
| 125 | } |
| 126 | |
| 127 | const char* inet_ntop(int af, const void *src, char* dst, socklen_t size) { |
| 128 | #if defined(WEBRTC_WIN) |
| 129 | return win32_inet_ntop(af, src, dst, size); |
| 130 | #else |
| 131 | return ::inet_ntop(af, src, dst, size); |
| 132 | #endif |
| 133 | } |
| 134 | |
| 135 | int inet_pton(int af, const char* src, void *dst) { |
| 136 | #if defined(WEBRTC_WIN) |
| 137 | return win32_inet_pton(af, src, dst); |
| 138 | #else |
| 139 | return ::inet_pton(af, src, dst); |
| 140 | #endif |
| 141 | } |
| 142 | |
| 143 | bool HasIPv6Enabled() { |
Taylor Brandstetter | f382d5d | 2016-05-19 21:57:31 | [diff] [blame] | 144 | #if defined(WEBRTC_WIN) |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 145 | if (IsWindowsVistaOrLater()) { |
| 146 | return true; |
| 147 | } |
| 148 | if (!IsWindowsXpOrLater()) { |
| 149 | return false; |
| 150 | } |
| 151 | DWORD protbuff_size = 4096; |
jbauch | 1286d0e | 2016-04-26 10:13:22 | [diff] [blame] | 152 | std::unique_ptr<char[]> protocols; |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 153 | LPWSAPROTOCOL_INFOW protocol_infos = NULL; |
| 154 | int requested_protocols[2] = {AF_INET6, 0}; |
| 155 | |
| 156 | int err = 0; |
| 157 | int ret = 0; |
| 158 | // Check for protocols in a do-while loop until we provide a buffer large |
| 159 | // enough. (WSCEnumProtocols sets protbuff_size to its desired value). |
| 160 | // It is extremely unlikely that this will loop more than once. |
| 161 | do { |
| 162 | protocols.reset(new char[protbuff_size]); |
| 163 | protocol_infos = reinterpret_cast<LPWSAPROTOCOL_INFOW>(protocols.get()); |
| 164 | ret = WSCEnumProtocols(requested_protocols, protocol_infos, |
| 165 | &protbuff_size, &err); |
| 166 | } while (ret == SOCKET_ERROR && err == WSAENOBUFS); |
| 167 | |
| 168 | if (ret == SOCKET_ERROR) { |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | // Even if ret is positive, check specifically for IPv6. |
| 173 | // Non-IPv6 enabled WinXP will still return a RAW protocol. |
| 174 | for (int i = 0; i < ret; ++i) { |
| 175 | if (protocol_infos[i].iAddressFamily == AF_INET6) { |
| 176 | return true; |
| 177 | } |
| 178 | } |
| 179 | return false; |
Taylor Brandstetter | f382d5d | 2016-05-19 21:57:31 | [diff] [blame] | 180 | #elif defined(WEBRTC_POSIX) && !defined(__native_client__) |
| 181 | bool has_ipv6 = false; |
| 182 | struct ifaddrs* ifa; |
| 183 | if (getifaddrs(&ifa) < 0) { |
| 184 | return false; |
| 185 | } |
| 186 | for (struct ifaddrs* cur = ifa; cur != nullptr; cur = cur->ifa_next) { |
| 187 | if (cur->ifa_addr->sa_family == AF_INET6) { |
| 188 | has_ipv6 = true; |
| 189 | break; |
| 190 | } |
| 191 | } |
| 192 | freeifaddrs(ifa); |
| 193 | return has_ipv6; |
| 194 | #else |
| 195 | return true; |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 196 | #endif |
| 197 | } |
| 198 | } // namespace rtc |