blob: c662f1a77b5f4ac10a9712d23378f3c84cf0321f [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
Steve Anton10542f22019-01-11 09:11:00 -080011#include "rtc_base/net_helpers.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012
jbauch555604a2016-04-26 03:13:22 -070013#include <memory>
14
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015#if defined(WEBRTC_WIN)
16#include <ws2spi.h>
17#include <ws2tcpip.h>
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/win32.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019#endif
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -070020#if defined(WEBRTC_POSIX) && !defined(__native_client__)
21#if defined(WEBRTC_ANDROID)
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/ifaddrs_android.h"
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -070023#else
24#include <ifaddrs.h>
25#endif
26#endif // defined(WEBRTC_POSIX) && !defined(__native_client__)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000027
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080029#include "rtc_base/signal_thread.h"
Yves Gerey2e00abc2018-10-05 15:39:24 +020030#include "rtc_base/third_party/sigslot/sigslot.h" // for signal_with_thread...
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000031
32namespace rtc {
33
Yves Gerey665174f2018-06-19 15:03:05 +020034int ResolveHostname(const std::string& hostname,
35 int family,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000036 std::vector<IPAddress>* addresses) {
37#ifdef __native_client__
nissec80e7412017-01-11 05:56:46 -080038 RTC_NOTREACHED();
Mirko Bonadei675513b2017-11-09 11:09:25 +010039 RTC_LOG(LS_WARNING) << "ResolveHostname() is not implemented for NaCl";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000040 return -1;
Yves Gerey665174f2018-06-19 15:03:05 +020041#else // __native_client__
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000042 if (!addresses) {
43 return -1;
44 }
45 addresses->clear();
deadbeef37f5ecf2017-02-27 14:06:41 -080046 struct addrinfo* result = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000047 struct addrinfo hints = {0};
Honghai Zhang56c0b202016-06-26 22:11:12 -070048 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.orgf0488722014-05-13 18:00:26 +000066 hints.ai_flags = AI_ADDRCONFIG;
deadbeef37f5ecf2017-02-27 14:06:41 -080067 int ret = getaddrinfo(hostname.c_str(), nullptr, &hints, &result);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000068 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
Yves Gerey665174f2018-06-19 15:03:05 +020086AsyncResolver::AsyncResolver() : SignalThread(), error_(-1) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000087
deadbeef8290ddf2017-07-11 16:56:05 -070088AsyncResolver::~AsyncResolver() = default;
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000089
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000090void AsyncResolver::Start(const SocketAddress& addr) {
91 addr_ = addr;
deadbeef8290ddf2017-07-11 16:56:05 -070092 // SignalThred Start will kickoff the resolve process.
93 SignalThread::Start();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000094}
95
96bool 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.org67186fe2015-03-09 22:21:53 +0000110int AsyncResolver::GetError() const {
111 return error_;
112}
113
114void AsyncResolver::Destroy(bool wait) {
deadbeef8290ddf2017-07-11 16:56:05 -0700115 SignalThread::Destroy(wait);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000116}
117
deadbeef8290ddf2017-07-11 16:56:05 -0700118void AsyncResolver::DoWork() {
Yves Gerey665174f2018-06-19 15:03:05 +0200119 error_ =
120 ResolveHostname(addr_.hostname().c_str(), addr_.family(), &addresses_);
deadbeef8290ddf2017-07-11 16:56:05 -0700121}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000122
deadbeef8290ddf2017-07-11 16:56:05 -0700123void AsyncResolver::OnWorkDone() {
124 SignalDone(this);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000125}
126
Yves Gerey665174f2018-06-19 15:03:05 +0200127const char* inet_ntop(int af, const void* src, char* dst, socklen_t size) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000128#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
Yves Gerey665174f2018-06-19 15:03:05 +0200135int inet_pton(int af, const char* src, void* dst) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000136#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
deadbeef9a6f4d42017-05-15 19:43:33 -0700143bool HasIPv4Enabled() {
144#if defined(WEBRTC_POSIX) && !defined(__native_client__)
145 bool has_ipv4 = false;
146 struct ifaddrs* ifa;
147 if (getifaddrs(&ifa) < 0) {
148 return false;
149 }
150 for (struct ifaddrs* cur = ifa; cur != nullptr; cur = cur->ifa_next) {
151 if (cur->ifa_addr->sa_family == AF_INET) {
152 has_ipv4 = true;
153 break;
154 }
155 }
156 freeifaddrs(ifa);
157 return has_ipv4;
158#else
159 return true;
160#endif
161}
162
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000163bool HasIPv6Enabled() {
Robin Raymondce1b1402018-11-22 20:10:11 -0500164#if defined(WINUWP)
165 // WinUWP always has IPv6 capability.
166 return true;
167#elif defined(WEBRTC_WIN)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000168 if (IsWindowsVistaOrLater()) {
169 return true;
170 }
171 if (!IsWindowsXpOrLater()) {
172 return false;
173 }
174 DWORD protbuff_size = 4096;
jbauch555604a2016-04-26 03:13:22 -0700175 std::unique_ptr<char[]> protocols;
deadbeef37f5ecf2017-02-27 14:06:41 -0800176 LPWSAPROTOCOL_INFOW protocol_infos = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000177 int requested_protocols[2] = {AF_INET6, 0};
178
179 int err = 0;
180 int ret = 0;
181 // Check for protocols in a do-while loop until we provide a buffer large
182 // enough. (WSCEnumProtocols sets protbuff_size to its desired value).
183 // It is extremely unlikely that this will loop more than once.
184 do {
185 protocols.reset(new char[protbuff_size]);
186 protocol_infos = reinterpret_cast<LPWSAPROTOCOL_INFOW>(protocols.get());
Yves Gerey665174f2018-06-19 15:03:05 +0200187 ret = WSCEnumProtocols(requested_protocols, protocol_infos, &protbuff_size,
188 &err);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000189 } while (ret == SOCKET_ERROR && err == WSAENOBUFS);
190
191 if (ret == SOCKET_ERROR) {
192 return false;
193 }
194
195 // Even if ret is positive, check specifically for IPv6.
196 // Non-IPv6 enabled WinXP will still return a RAW protocol.
197 for (int i = 0; i < ret; ++i) {
198 if (protocol_infos[i].iAddressFamily == AF_INET6) {
199 return true;
200 }
201 }
202 return false;
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700203#elif defined(WEBRTC_POSIX) && !defined(__native_client__)
204 bool has_ipv6 = false;
205 struct ifaddrs* ifa;
206 if (getifaddrs(&ifa) < 0) {
207 return false;
208 }
209 for (struct ifaddrs* cur = ifa; cur != nullptr; cur = cur->ifa_next) {
210 if (cur->ifa_addr->sa_family == AF_INET6) {
211 has_ipv6 = true;
212 break;
213 }
214 }
215 freeifaddrs(ifa);
216 return has_ipv6;
217#else
218 return true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000219#endif
220}
221} // namespace rtc