blob: fddfdaf58b5832318fe4f73d4f2ad7262f5b03bf [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"
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};
47 // TODO(djw): For now this is IPv4 only so existing users remain unaffected.
48 hints.ai_family = AF_INET;
49 hints.ai_flags = AI_ADDRCONFIG;
50 int ret = getaddrinfo(hostname.c_str(), NULL, &hints, &result);
51 if (ret != 0) {
52 return ret;
53 }
54 struct addrinfo* cursor = result;
55 for (; cursor; cursor = cursor->ai_next) {
56 if (family == AF_UNSPEC || cursor->ai_family == family) {
57 IPAddress ip;
58 if (IPFromAddrInfo(cursor, &ip)) {
59 addresses->push_back(ip);
60 }
61 }
62 }
63 freeaddrinfo(result);
64 return 0;
65#endif // !__native_client__
66}
67
68// AsyncResolver
69AsyncResolver::AsyncResolver() : error_(-1) {
70}
71
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000072AsyncResolver::~AsyncResolver() = default;
73
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000074void AsyncResolver::Start(const SocketAddress& addr) {
75 addr_ = addr;
76 // SignalThred Start will kickoff the resolve process.
77 SignalThread::Start();
78}
79
80bool AsyncResolver::GetResolvedAddress(int family, SocketAddress* addr) const {
81 if (error_ != 0 || addresses_.empty())
82 return false;
83
84 *addr = addr_;
85 for (size_t i = 0; i < addresses_.size(); ++i) {
86 if (family == addresses_[i].family()) {
87 addr->SetResolvedIP(addresses_[i]);
88 return true;
89 }
90 }
91 return false;
92}
93
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000094int AsyncResolver::GetError() const {
95 return error_;
96}
97
98void AsyncResolver::Destroy(bool wait) {
99 SignalThread::Destroy(wait);
100}
101
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000102void AsyncResolver::DoWork() {
103 error_ = ResolveHostname(addr_.hostname().c_str(), addr_.family(),
104 &addresses_);
105}
106
107void AsyncResolver::OnWorkDone() {
108 SignalDone(this);
109}
110
111const char* inet_ntop(int af, const void *src, char* dst, socklen_t size) {
112#if defined(WEBRTC_WIN)
113 return win32_inet_ntop(af, src, dst, size);
114#else
115 return ::inet_ntop(af, src, dst, size);
116#endif
117}
118
119int inet_pton(int af, const char* src, void *dst) {
120#if defined(WEBRTC_WIN)
121 return win32_inet_pton(af, src, dst);
122#else
123 return ::inet_pton(af, src, dst);
124#endif
125}
126
127bool HasIPv6Enabled() {
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700128#if defined(WEBRTC_WIN)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000129 if (IsWindowsVistaOrLater()) {
130 return true;
131 }
132 if (!IsWindowsXpOrLater()) {
133 return false;
134 }
135 DWORD protbuff_size = 4096;
jbauch555604a2016-04-26 03:13:22 -0700136 std::unique_ptr<char[]> protocols;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000137 LPWSAPROTOCOL_INFOW protocol_infos = NULL;
138 int requested_protocols[2] = {AF_INET6, 0};
139
140 int err = 0;
141 int ret = 0;
142 // Check for protocols in a do-while loop until we provide a buffer large
143 // enough. (WSCEnumProtocols sets protbuff_size to its desired value).
144 // It is extremely unlikely that this will loop more than once.
145 do {
146 protocols.reset(new char[protbuff_size]);
147 protocol_infos = reinterpret_cast<LPWSAPROTOCOL_INFOW>(protocols.get());
148 ret = WSCEnumProtocols(requested_protocols, protocol_infos,
149 &protbuff_size, &err);
150 } while (ret == SOCKET_ERROR && err == WSAENOBUFS);
151
152 if (ret == SOCKET_ERROR) {
153 return false;
154 }
155
156 // Even if ret is positive, check specifically for IPv6.
157 // Non-IPv6 enabled WinXP will still return a RAW protocol.
158 for (int i = 0; i < ret; ++i) {
159 if (protocol_infos[i].iAddressFamily == AF_INET6) {
160 return true;
161 }
162 }
163 return false;
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700164#elif defined(WEBRTC_POSIX) && !defined(__native_client__)
165 bool has_ipv6 = false;
166 struct ifaddrs* ifa;
167 if (getifaddrs(&ifa) < 0) {
168 return false;
169 }
170 for (struct ifaddrs* cur = ifa; cur != nullptr; cur = cur->ifa_next) {
171 if (cur->ifa_addr->sa_family == AF_INET6) {
172 has_ipv6 = true;
173 break;
174 }
175 }
176 freeifaddrs(ifa);
177 return has_ipv6;
178#else
179 return true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000180#endif
181}
182} // namespace rtc