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