blob: b1221c3d0014db634418bbdbfb1d7e65878ce85b [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/nethelpers.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)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#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
Oleh Prypin96a0f612018-10-05 13:13:38 +000028#include "rtc_base/byteorder.h"
29#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "rtc_base/logging.h"
31#include "rtc_base/signalthread.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000032
33namespace rtc {
34
Yves Gerey665174f2018-06-19 15:03:05 +020035int ResolveHostname(const std::string& hostname,
36 int family,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000037 std::vector<IPAddress>* addresses) {
38#ifdef __native_client__
nissec80e7412017-01-11 05:56:46 -080039 RTC_NOTREACHED();
Mirko Bonadei675513b2017-11-09 11:09:25 +010040 RTC_LOG(LS_WARNING) << "ResolveHostname() is not implemented for NaCl";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000041 return -1;
Yves Gerey665174f2018-06-19 15:03:05 +020042#else // __native_client__
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000043 if (!addresses) {
44 return -1;
45 }
46 addresses->clear();
deadbeef37f5ecf2017-02-27 14:06:41 -080047 struct addrinfo* result = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000048 struct addrinfo hints = {0};
Honghai Zhang56c0b202016-06-26 22:11:12 -070049 hints.ai_family = family;
50 // |family| here will almost always be AF_UNSPEC, because |family| comes from
51 // AsyncResolver::addr_.family(), which comes from a SocketAddress constructed
52 // with a hostname. When a SocketAddress is constructed with a hostname, its
53 // family is AF_UNSPEC. However, if someday in the future we construct
54 // a SocketAddress with both a hostname and a family other than AF_UNSPEC,
55 // then it would be possible to get a specific family value here.
56
57 // The behavior of AF_UNSPEC is roughly "get both ipv4 and ipv6", as
58 // documented by the various operating systems:
59 // Linux: http://man7.org/linux/man-pages/man3/getaddrinfo.3.html
60 // Windows: https://msdn.microsoft.com/en-us/library/windows/desktop/
61 // ms738520(v=vs.85).aspx
62 // Mac: https://developer.apple.com/legacy/library/documentation/Darwin/
63 // Reference/ManPages/man3/getaddrinfo.3.html
64 // Android (source code, not documentation):
65 // https://android.googlesource.com/platform/bionic/+/
66 // 7e0bfb511e85834d7c6cb9631206b62f82701d60/libc/netbsd/net/getaddrinfo.c#1657
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000067 hints.ai_flags = AI_ADDRCONFIG;
deadbeef37f5ecf2017-02-27 14:06:41 -080068 int ret = getaddrinfo(hostname.c_str(), nullptr, &hints, &result);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000069 if (ret != 0) {
70 return ret;
71 }
72 struct addrinfo* cursor = result;
73 for (; cursor; cursor = cursor->ai_next) {
74 if (family == AF_UNSPEC || cursor->ai_family == family) {
75 IPAddress ip;
76 if (IPFromAddrInfo(cursor, &ip)) {
77 addresses->push_back(ip);
78 }
79 }
80 }
81 freeaddrinfo(result);
82 return 0;
83#endif // !__native_client__
84}
85
86// AsyncResolver
Yves Gerey665174f2018-06-19 15:03:05 +020087AsyncResolver::AsyncResolver() : SignalThread(), error_(-1) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000088
deadbeef8290ddf2017-07-11 16:56:05 -070089AsyncResolver::~AsyncResolver() = default;
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000090
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000091void AsyncResolver::Start(const SocketAddress& addr) {
92 addr_ = addr;
deadbeef8290ddf2017-07-11 16:56:05 -070093 // SignalThred Start will kickoff the resolve process.
94 SignalThread::Start();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000095}
96
97bool 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.org67186fe2015-03-09 22:21:53 +0000111int AsyncResolver::GetError() const {
112 return error_;
113}
114
115void AsyncResolver::Destroy(bool wait) {
deadbeef8290ddf2017-07-11 16:56:05 -0700116 SignalThread::Destroy(wait);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000117}
118
deadbeef8290ddf2017-07-11 16:56:05 -0700119void AsyncResolver::DoWork() {
Yves Gerey665174f2018-06-19 15:03:05 +0200120 error_ =
121 ResolveHostname(addr_.hostname().c_str(), addr_.family(), &addresses_);
deadbeef8290ddf2017-07-11 16:56:05 -0700122}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000123
deadbeef8290ddf2017-07-11 16:56:05 -0700124void AsyncResolver::OnWorkDone() {
125 SignalDone(this);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000126}
127
Yves Gerey665174f2018-06-19 15:03:05 +0200128const char* inet_ntop(int af, const void* src, char* dst, socklen_t size) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000129#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
Yves Gerey665174f2018-06-19 15:03:05 +0200136int inet_pton(int af, const char* src, void* dst) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000137#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
deadbeef9a6f4d42017-05-15 19:43:33 -0700144bool 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.orgf0488722014-05-13 18:00:26 +0000164bool HasIPv6Enabled() {
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700165#if defined(WEBRTC_WIN)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000166 if (IsWindowsVistaOrLater()) {
167 return true;
168 }
169 if (!IsWindowsXpOrLater()) {
170 return false;
171 }
172 DWORD protbuff_size = 4096;
jbauch555604a2016-04-26 03:13:22 -0700173 std::unique_ptr<char[]> protocols;
deadbeef37f5ecf2017-02-27 14:06:41 -0800174 LPWSAPROTOCOL_INFOW protocol_infos = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000175 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());
Yves Gerey665174f2018-06-19 15:03:05 +0200185 ret = WSCEnumProtocols(requested_protocols, protocol_infos, &protbuff_size,
186 &err);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000187 } 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 Brandstetter2b3bf6b2016-05-19 14:57:31 -0700201#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.orgf0488722014-05-13 18:00:26 +0000217#endif
218}
219} // namespace rtc