blob: 673af0ba63d61a6759d6584dbce2b63e5cfa552a [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#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.orgf0488722014-05-13 18:00:26 +000032
33namespace rtc {
34
35int ResolveHostname(const std::string& hostname, int family,
36 std::vector<IPAddress>* addresses) {
37#ifdef __native_client__
nissec80e7412017-01-11 05:56:46 -080038 RTC_NOTREACHED();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000039 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();
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
deadbeef8290ddf2017-07-11 16:56:05 -070086AsyncResolver::AsyncResolver()
maxmorine9ef9072017-08-29 04:49:00 -070087 : SignalThread(false /* use_socket_server */), 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() {
120 error_ = ResolveHostname(addr_.hostname().c_str(), addr_.family(),
121 &addresses_);
122}
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
128const 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
136int 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
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());
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 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