blob: 7ca654949ce5e1767d17e501c5622e0d2822e25b [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
11#include "webrtc/base/nethelpers.h"
12
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>
18#include "webrtc/base/win32.h"
19#endif
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -070020#if defined(WEBRTC_POSIX) && !defined(__native_client__)
21#if defined(WEBRTC_ANDROID)
22#include "webrtc/base/ifaddrs-android.h"
23#else
24#include <ifaddrs.h>
25#endif
26#endif // defined(WEBRTC_POSIX) && !defined(__native_client__)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000027
28#include "webrtc/base/byteorder.h"
nissec80e7412017-01-11 05:56:46 -080029#include "webrtc/base/checks.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000030#include "webrtc/base/logging.h"
31#include "webrtc/base/signalthread.h"
32
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();
46 struct addrinfo* result = NULL;
47 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;
67 int ret = getaddrinfo(hostname.c_str(), NULL, &hints, &result);
68 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
magjedd9c7f8d2016-07-26 03:03:31 -070086AsyncResolver::AsyncResolver()
87 : SignalThread(false /* use_socket_server */), error_(-1) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000088
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000089AsyncResolver::~AsyncResolver() = default;
90
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000091void AsyncResolver::Start(const SocketAddress& addr) {
92 addr_ = addr;
93 // SignalThred Start will kickoff the resolve process.
94 SignalThread::Start();
95}
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) {
116 SignalThread::Destroy(wait);
117}
118
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000119void AsyncResolver::DoWork() {
120 error_ = ResolveHostname(addr_.hostname().c_str(), addr_.family(),
121 &addresses_);
122}
123
124void AsyncResolver::OnWorkDone() {
125 SignalDone(this);
126}
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
144bool HasIPv6Enabled() {
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700145#if defined(WEBRTC_WIN)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000146 if (IsWindowsVistaOrLater()) {
147 return true;
148 }
149 if (!IsWindowsXpOrLater()) {
150 return false;
151 }
152 DWORD protbuff_size = 4096;
jbauch555604a2016-04-26 03:13:22 -0700153 std::unique_ptr<char[]> protocols;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000154 LPWSAPROTOCOL_INFOW protocol_infos = NULL;
155 int requested_protocols[2] = {AF_INET6, 0};
156
157 int err = 0;
158 int ret = 0;
159 // Check for protocols in a do-while loop until we provide a buffer large
160 // enough. (WSCEnumProtocols sets protbuff_size to its desired value).
161 // It is extremely unlikely that this will loop more than once.
162 do {
163 protocols.reset(new char[protbuff_size]);
164 protocol_infos = reinterpret_cast<LPWSAPROTOCOL_INFOW>(protocols.get());
165 ret = WSCEnumProtocols(requested_protocols, protocol_infos,
166 &protbuff_size, &err);
167 } while (ret == SOCKET_ERROR && err == WSAENOBUFS);
168
169 if (ret == SOCKET_ERROR) {
170 return false;
171 }
172
173 // Even if ret is positive, check specifically for IPv6.
174 // Non-IPv6 enabled WinXP will still return a RAW protocol.
175 for (int i = 0; i < ret; ++i) {
176 if (protocol_infos[i].iAddressFamily == AF_INET6) {
177 return true;
178 }
179 }
180 return false;
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700181#elif defined(WEBRTC_POSIX) && !defined(__native_client__)
182 bool has_ipv6 = false;
183 struct ifaddrs* ifa;
184 if (getifaddrs(&ifa) < 0) {
185 return false;
186 }
187 for (struct ifaddrs* cur = ifa; cur != nullptr; cur = cur->ifa_next) {
188 if (cur->ifa_addr->sa_family == AF_INET6) {
189 has_ipv6 = true;
190 break;
191 }
192 }
193 freeifaddrs(ifa);
194 return has_ipv6;
195#else
196 return true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000197#endif
198}
199} // namespace rtc