blob: 1f6bb8df7b7f71628524df9bd1fa27240ab9fe7b [file] [log] [blame]
henrike@webrtc.org47be73b2014-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
11#include "webrtc/base/nethelpers.h"
12
jbauch1286d0e2016-04-26 10:13:2213#include <memory>
14
henrike@webrtc.org47be73b2014-05-13 18:00:2615#if defined(WEBRTC_WIN)
16#include <ws2spi.h>
17#include <ws2tcpip.h>
18#include "webrtc/base/win32.h"
19#endif
Taylor Brandstetterf382d5d2016-05-19 21:57:3120#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.org47be73b2014-05-13 18:00:2627
28#include "webrtc/base/byteorder.h"
29#include "webrtc/base/logging.h"
30#include "webrtc/base/signalthread.h"
31
32namespace rtc {
33
34int ResolveHostname(const std::string& hostname, int family,
35 std::vector<IPAddress>* addresses) {
36#ifdef __native_client__
37 ASSERT(false);
38 LOG(LS_WARNING) << "ResolveHostname() is not implemented for NaCl";
39 return -1;
40#else // __native_client__
41 if (!addresses) {
42 return -1;
43 }
44 addresses->clear();
45 struct addrinfo* result = NULL;
46 struct addrinfo hints = {0};
Honghai Zhangc2c83d82016-06-27 05:11:1247 hints.ai_family = family;
48 // |family| here will almost always be AF_UNSPEC, because |family| comes from
49 // AsyncResolver::addr_.family(), which comes from a SocketAddress constructed
50 // with a hostname. When a SocketAddress is constructed with a hostname, its
51 // family is AF_UNSPEC. However, if someday in the future we construct
52 // a SocketAddress with both a hostname and a family other than AF_UNSPEC,
53 // then it would be possible to get a specific family value here.
54
55 // The behavior of AF_UNSPEC is roughly "get both ipv4 and ipv6", as
56 // documented by the various operating systems:
57 // Linux: http://man7.org/linux/man-pages/man3/getaddrinfo.3.html
58 // Windows: https://msdn.microsoft.com/en-us/library/windows/desktop/
59 // ms738520(v=vs.85).aspx
60 // Mac: https://developer.apple.com/legacy/library/documentation/Darwin/
61 // Reference/ManPages/man3/getaddrinfo.3.html
62 // Android (source code, not documentation):
63 // https://android.googlesource.com/platform/bionic/+/
64 // 7e0bfb511e85834d7c6cb9631206b62f82701d60/libc/netbsd/net/getaddrinfo.c#1657
henrike@webrtc.org47be73b2014-05-13 18:00:2665 hints.ai_flags = AI_ADDRCONFIG;
66 int ret = getaddrinfo(hostname.c_str(), NULL, &hints, &result);
67 if (ret != 0) {
68 return ret;
69 }
70 struct addrinfo* cursor = result;
71 for (; cursor; cursor = cursor->ai_next) {
72 if (family == AF_UNSPEC || cursor->ai_family == family) {
73 IPAddress ip;
74 if (IPFromAddrInfo(cursor, &ip)) {
75 addresses->push_back(ip);
76 }
77 }
78 }
79 freeaddrinfo(result);
80 return 0;
81#endif // !__native_client__
82}
83
84// AsyncResolver
85AsyncResolver::AsyncResolver() : error_(-1) {
86}
87
kwiberg@webrtc.org786b6342015-03-09 22:21:5388AsyncResolver::~AsyncResolver() = default;
89
henrike@webrtc.org47be73b2014-05-13 18:00:2690void AsyncResolver::Start(const SocketAddress& addr) {
91 addr_ = addr;
92 // SignalThred Start will kickoff the resolve process.
93 SignalThread::Start();
94}
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.org786b6342015-03-09 22:21:53110int AsyncResolver::GetError() const {
111 return error_;
112}
113
114void AsyncResolver::Destroy(bool wait) {
115 SignalThread::Destroy(wait);
116}
117
henrike@webrtc.org47be73b2014-05-13 18:00:26118void AsyncResolver::DoWork() {
119 error_ = ResolveHostname(addr_.hostname().c_str(), addr_.family(),
120 &addresses_);
121}
122
123void AsyncResolver::OnWorkDone() {
124 SignalDone(this);
125}
126
127const char* inet_ntop(int af, const void *src, char* dst, socklen_t size) {
128#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
135int inet_pton(int af, const char* src, void *dst) {
136#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
143bool HasIPv6Enabled() {
Taylor Brandstetterf382d5d2016-05-19 21:57:31144#if defined(WEBRTC_WIN)
henrike@webrtc.org47be73b2014-05-13 18:00:26145 if (IsWindowsVistaOrLater()) {
146 return true;
147 }
148 if (!IsWindowsXpOrLater()) {
149 return false;
150 }
151 DWORD protbuff_size = 4096;
jbauch1286d0e2016-04-26 10:13:22152 std::unique_ptr<char[]> protocols;
henrike@webrtc.org47be73b2014-05-13 18:00:26153 LPWSAPROTOCOL_INFOW protocol_infos = NULL;
154 int requested_protocols[2] = {AF_INET6, 0};
155
156 int err = 0;
157 int ret = 0;
158 // Check for protocols in a do-while loop until we provide a buffer large
159 // enough. (WSCEnumProtocols sets protbuff_size to its desired value).
160 // It is extremely unlikely that this will loop more than once.
161 do {
162 protocols.reset(new char[protbuff_size]);
163 protocol_infos = reinterpret_cast<LPWSAPROTOCOL_INFOW>(protocols.get());
164 ret = WSCEnumProtocols(requested_protocols, protocol_infos,
165 &protbuff_size, &err);
166 } while (ret == SOCKET_ERROR && err == WSAENOBUFS);
167
168 if (ret == SOCKET_ERROR) {
169 return false;
170 }
171
172 // Even if ret is positive, check specifically for IPv6.
173 // Non-IPv6 enabled WinXP will still return a RAW protocol.
174 for (int i = 0; i < ret; ++i) {
175 if (protocol_infos[i].iAddressFamily == AF_INET6) {
176 return true;
177 }
178 }
179 return false;
Taylor Brandstetterf382d5d2016-05-19 21:57:31180#elif defined(WEBRTC_POSIX) && !defined(__native_client__)
181 bool has_ipv6 = false;
182 struct ifaddrs* ifa;
183 if (getifaddrs(&ifa) < 0) {
184 return false;
185 }
186 for (struct ifaddrs* cur = ifa; cur != nullptr; cur = cur->ifa_next) {
187 if (cur->ifa_addr->sa_family == AF_INET6) {
188 has_ipv6 = true;
189 break;
190 }
191 }
192 freeifaddrs(ifa);
193 return has_ipv6;
194#else
195 return true;
henrike@webrtc.org47be73b2014-05-13 18:00:26196#endif
197}
198} // namespace rtc