blob: e5e4ea6c7dc278159e02e941dc654139c67467cc [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
kjellandere96c45b2017-06-30 10:45:21 -070011#include "webrtc/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>
kjellandere96c45b2017-06-30 10:45:21 -070018#include "webrtc/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)
kjellandere96c45b2017-06-30 10:45:21 -070022#include "webrtc/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
kjellandere96c45b2017-06-30 10:45:21 -070028#include "webrtc/rtc_base/byteorder.h"
29#include "webrtc/rtc_base/checks.h"
30#include "webrtc/rtc_base/logging.h"
deadbeef8290ddf2017-07-11 16:56:05 -070031#include "webrtc/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()
jonasoc251cb12017-08-29 03:20:58 -070087 : SignalThread(false /* use_socket_server */),
88 error_(-1), resolve_time_ms_(0) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000089
deadbeef8290ddf2017-07-11 16:56:05 -070090AsyncResolver::~AsyncResolver() = default;
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000091
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000092void AsyncResolver::Start(const SocketAddress& addr) {
93 addr_ = addr;
deadbeef8290ddf2017-07-11 16:56:05 -070094 // SignalThred Start will kickoff the resolve process.
95 SignalThread::Start();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000096}
97
98bool AsyncResolver::GetResolvedAddress(int family, SocketAddress* addr) const {
99 if (error_ != 0 || addresses_.empty())
100 return false;
101
102 *addr = addr_;
103 for (size_t i = 0; i < addresses_.size(); ++i) {
104 if (family == addresses_[i].family()) {
105 addr->SetResolvedIP(addresses_[i]);
106 return true;
107 }
108 }
109 return false;
110}
111
jonasoc251cb12017-08-29 03:20:58 -0700112int64_t AsyncResolver::GetResolveElapsedTimeMilliseconds() const {
113 return resolve_time_ms_;
114}
115
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000116int AsyncResolver::GetError() const {
117 return error_;
118}
119
120void AsyncResolver::Destroy(bool wait) {
deadbeef8290ddf2017-07-11 16:56:05 -0700121 SignalThread::Destroy(wait);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000122}
123
deadbeef8290ddf2017-07-11 16:56:05 -0700124void AsyncResolver::DoWork() {
jonasoc251cb12017-08-29 03:20:58 -0700125 int64_t start = rtc::TimeMillis();
deadbeef8290ddf2017-07-11 16:56:05 -0700126 error_ = ResolveHostname(addr_.hostname().c_str(), addr_.family(),
127 &addresses_);
jonasoc251cb12017-08-29 03:20:58 -0700128 int64_t stop = rtc::TimeMillis();
129 resolve_time_ms_ = stop - start;
deadbeef8290ddf2017-07-11 16:56:05 -0700130}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000131
deadbeef8290ddf2017-07-11 16:56:05 -0700132void AsyncResolver::OnWorkDone() {
133 SignalDone(this);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000134}
135
136const char* inet_ntop(int af, const void *src, char* dst, socklen_t size) {
137#if defined(WEBRTC_WIN)
138 return win32_inet_ntop(af, src, dst, size);
139#else
140 return ::inet_ntop(af, src, dst, size);
141#endif
142}
143
144int inet_pton(int af, const char* src, void *dst) {
145#if defined(WEBRTC_WIN)
146 return win32_inet_pton(af, src, dst);
147#else
148 return ::inet_pton(af, src, dst);
149#endif
150}
151
deadbeef9a6f4d42017-05-15 19:43:33 -0700152bool HasIPv4Enabled() {
153#if defined(WEBRTC_POSIX) && !defined(__native_client__)
154 bool has_ipv4 = false;
155 struct ifaddrs* ifa;
156 if (getifaddrs(&ifa) < 0) {
157 return false;
158 }
159 for (struct ifaddrs* cur = ifa; cur != nullptr; cur = cur->ifa_next) {
160 if (cur->ifa_addr->sa_family == AF_INET) {
161 has_ipv4 = true;
162 break;
163 }
164 }
165 freeifaddrs(ifa);
166 return has_ipv4;
167#else
168 return true;
169#endif
170}
171
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000172bool HasIPv6Enabled() {
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700173#if defined(WEBRTC_WIN)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000174 if (IsWindowsVistaOrLater()) {
175 return true;
176 }
177 if (!IsWindowsXpOrLater()) {
178 return false;
179 }
180 DWORD protbuff_size = 4096;
jbauch555604a2016-04-26 03:13:22 -0700181 std::unique_ptr<char[]> protocols;
deadbeef37f5ecf2017-02-27 14:06:41 -0800182 LPWSAPROTOCOL_INFOW protocol_infos = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000183 int requested_protocols[2] = {AF_INET6, 0};
184
185 int err = 0;
186 int ret = 0;
187 // Check for protocols in a do-while loop until we provide a buffer large
188 // enough. (WSCEnumProtocols sets protbuff_size to its desired value).
189 // It is extremely unlikely that this will loop more than once.
190 do {
191 protocols.reset(new char[protbuff_size]);
192 protocol_infos = reinterpret_cast<LPWSAPROTOCOL_INFOW>(protocols.get());
193 ret = WSCEnumProtocols(requested_protocols, protocol_infos,
194 &protbuff_size, &err);
195 } while (ret == SOCKET_ERROR && err == WSAENOBUFS);
196
197 if (ret == SOCKET_ERROR) {
198 return false;
199 }
200
201 // Even if ret is positive, check specifically for IPv6.
202 // Non-IPv6 enabled WinXP will still return a RAW protocol.
203 for (int i = 0; i < ret; ++i) {
204 if (protocol_infos[i].iAddressFamily == AF_INET6) {
205 return true;
206 }
207 }
208 return false;
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700209#elif defined(WEBRTC_POSIX) && !defined(__native_client__)
210 bool has_ipv6 = false;
211 struct ifaddrs* ifa;
212 if (getifaddrs(&ifa) < 0) {
213 return false;
214 }
215 for (struct ifaddrs* cur = ifa; cur != nullptr; cur = cur->ifa_next) {
216 if (cur->ifa_addr->sa_family == AF_INET6) {
217 has_ipv6 = true;
218 break;
219 }
220 }
221 freeifaddrs(ifa);
222 return has_ipv6;
223#else
224 return true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000225#endif
226}
227} // namespace rtc