blob: 81cd1afcef4a069e6a76a076da20d1ce1071b315 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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 Bonadei92ea95e2017-09-15 04:47:3111#include "rtc_base/nethelpers.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2612
jbauch555604a2016-04-26 10:13:2213#include <memory>
14
henrike@webrtc.orgf0488722014-05-13 18:00:2615#if defined(WEBRTC_WIN)
16#include <ws2spi.h>
17#include <ws2tcpip.h>
Mirko Bonadei92ea95e2017-09-15 04:47:3118#include "rtc_base/win32.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2619#endif
Taylor Brandstetter2b3bf6b2016-05-19 21:57:3120#if defined(WEBRTC_POSIX) && !defined(__native_client__)
21#if defined(WEBRTC_ANDROID)
Mirko Bonadei92ea95e2017-09-15 04:47:3122#include "rtc_base/ifaddrs-android.h"
Taylor Brandstetter2b3bf6b2016-05-19 21:57:3123#else
24#include <ifaddrs.h>
25#endif
26#endif // defined(WEBRTC_POSIX) && !defined(__native_client__)
henrike@webrtc.orgf0488722014-05-13 18:00:2627
Mirko Bonadei92ea95e2017-09-15 04:47:3128#include "rtc_base/logging.h"
29#include "rtc_base/signalthread.h"
Yves Gerey2e00abc2018-10-05 13:39:2430#include "rtc_base/third_party/sigslot/sigslot.h" // for signal_with_thread...
henrike@webrtc.orgf0488722014-05-13 18:00:2631
32namespace rtc {
33
Yves Gerey665174f2018-06-19 13:03:0534int ResolveHostname(const std::string& hostname,
35 int family,
henrike@webrtc.orgf0488722014-05-13 18:00:2636 std::vector<IPAddress>* addresses) {
37#ifdef __native_client__
nissec80e7412017-01-11 13:56:4638 RTC_NOTREACHED();
Mirko Bonadei675513b2017-11-09 10:09:2539 RTC_LOG(LS_WARNING) << "ResolveHostname() is not implemented for NaCl";
henrike@webrtc.orgf0488722014-05-13 18:00:2640 return -1;
Yves Gerey665174f2018-06-19 13:03:0541#else // __native_client__
henrike@webrtc.orgf0488722014-05-13 18:00:2642 if (!addresses) {
43 return -1;
44 }
45 addresses->clear();
deadbeef37f5ecf2017-02-27 22:06:4146 struct addrinfo* result = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:2647 struct addrinfo hints = {0};
Honghai Zhang56c0b202016-06-27 05:11:1248 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:2666 hints.ai_flags = AI_ADDRCONFIG;
deadbeef37f5ecf2017-02-27 22:06:4167 int ret = getaddrinfo(hostname.c_str(), nullptr, &hints, &result);
henrike@webrtc.orgf0488722014-05-13 18:00:2668 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 13:03:0586AsyncResolver::AsyncResolver() : SignalThread(), error_(-1) {}
henrike@webrtc.orgf0488722014-05-13 18:00:2687
deadbeef8290ddf2017-07-11 23:56:0588AsyncResolver::~AsyncResolver() = default;
kwiberg@webrtc.org67186fe2015-03-09 22:21:5389
henrike@webrtc.orgf0488722014-05-13 18:00:2690void AsyncResolver::Start(const SocketAddress& addr) {
91 addr_ = addr;
deadbeef8290ddf2017-07-11 23:56:0592 // SignalThred Start will kickoff the resolve process.
93 SignalThread::Start();
henrike@webrtc.orgf0488722014-05-13 18:00:2694}
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:53110int AsyncResolver::GetError() const {
111 return error_;
112}
113
114void AsyncResolver::Destroy(bool wait) {
deadbeef8290ddf2017-07-11 23:56:05115 SignalThread::Destroy(wait);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53116}
117
deadbeef8290ddf2017-07-11 23:56:05118void AsyncResolver::DoWork() {
Yves Gerey665174f2018-06-19 13:03:05119 error_ =
120 ResolveHostname(addr_.hostname().c_str(), addr_.family(), &addresses_);
deadbeef8290ddf2017-07-11 23:56:05121}
henrike@webrtc.orgf0488722014-05-13 18:00:26122
deadbeef8290ddf2017-07-11 23:56:05123void AsyncResolver::OnWorkDone() {
124 SignalDone(this);
henrike@webrtc.orgf0488722014-05-13 18:00:26125}
126
Yves Gerey665174f2018-06-19 13:03:05127const char* inet_ntop(int af, const void* src, char* dst, socklen_t size) {
henrike@webrtc.orgf0488722014-05-13 18:00:26128#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 13:03:05135int inet_pton(int af, const char* src, void* dst) {
henrike@webrtc.orgf0488722014-05-13 18:00:26136#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-16 02:43:33143bool 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:26163bool HasIPv6Enabled() {
Taylor Brandstetter2b3bf6b2016-05-19 21:57:31164#if defined(WEBRTC_WIN)
henrike@webrtc.orgf0488722014-05-13 18:00:26165 if (IsWindowsVistaOrLater()) {
166 return true;
167 }
168 if (!IsWindowsXpOrLater()) {
169 return false;
170 }
171 DWORD protbuff_size = 4096;
jbauch555604a2016-04-26 10:13:22172 std::unique_ptr<char[]> protocols;
deadbeef37f5ecf2017-02-27 22:06:41173 LPWSAPROTOCOL_INFOW protocol_infos = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26174 int requested_protocols[2] = {AF_INET6, 0};
175
176 int err = 0;
177 int ret = 0;
178 // Check for protocols in a do-while loop until we provide a buffer large
179 // enough. (WSCEnumProtocols sets protbuff_size to its desired value).
180 // It is extremely unlikely that this will loop more than once.
181 do {
182 protocols.reset(new char[protbuff_size]);
183 protocol_infos = reinterpret_cast<LPWSAPROTOCOL_INFOW>(protocols.get());
Yves Gerey665174f2018-06-19 13:03:05184 ret = WSCEnumProtocols(requested_protocols, protocol_infos, &protbuff_size,
185 &err);
henrike@webrtc.orgf0488722014-05-13 18:00:26186 } while (ret == SOCKET_ERROR && err == WSAENOBUFS);
187
188 if (ret == SOCKET_ERROR) {
189 return false;
190 }
191
192 // Even if ret is positive, check specifically for IPv6.
193 // Non-IPv6 enabled WinXP will still return a RAW protocol.
194 for (int i = 0; i < ret; ++i) {
195 if (protocol_infos[i].iAddressFamily == AF_INET6) {
196 return true;
197 }
198 }
199 return false;
Taylor Brandstetter2b3bf6b2016-05-19 21:57:31200#elif defined(WEBRTC_POSIX) && !defined(__native_client__)
201 bool has_ipv6 = false;
202 struct ifaddrs* ifa;
203 if (getifaddrs(&ifa) < 0) {
204 return false;
205 }
206 for (struct ifaddrs* cur = ifa; cur != nullptr; cur = cur->ifa_next) {
207 if (cur->ifa_addr->sa_family == AF_INET6) {
208 has_ipv6 = true;
209 break;
210 }
211 }
212 freeifaddrs(ifa);
213 return has_ipv6;
214#else
215 return true;
henrike@webrtc.orgf0488722014-05-13 18:00:26216#endif
217}
218} // namespace rtc