blob: 057f34ddc151bea44cf92d9d8c5595a42ebb21f9 [file] [log] [blame]
henrike@webrtc.org0e118e72013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2008, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/base/nethelpers.h"
29
30#if defined(WIN32)
31#include <ws2spi.h>
32#include <ws2tcpip.h>
33#include "talk/base/win32.h"
34#endif
35
36#include "talk/base/byteorder.h"
sergeyu@chromium.org849bc5c2014-01-09 21:26:50 +000037#include "talk/base/logging.h"
henrike@webrtc.org0e118e72013-07-10 00:45:36 +000038#include "talk/base/signalthread.h"
39
40namespace talk_base {
41
42int ResolveHostname(const std::string& hostname, int family,
43 std::vector<IPAddress>* addresses) {
wu@webrtc.org2a81a382014-01-03 22:08:47 +000044#ifdef __native_client__
45 ASSERT(false);
46 LOG(LS_WARNING) << "ResolveHostname() is not implemented for NaCl";
47 return -1;
48#else // __native_client__
henrike@webrtc.org0e118e72013-07-10 00:45:36 +000049 if (!addresses) {
50 return -1;
51 }
52 addresses->clear();
53 struct addrinfo* result = NULL;
54 struct addrinfo hints = {0};
55 // TODO(djw): For now this is IPv4 only so existing users remain unaffected.
56 hints.ai_family = AF_INET;
57 hints.ai_flags = AI_ADDRCONFIG;
58 int ret = getaddrinfo(hostname.c_str(), NULL, &hints, &result);
59 if (ret != 0) {
60 return ret;
61 }
62 struct addrinfo* cursor = result;
63 for (; cursor; cursor = cursor->ai_next) {
64 if (family == AF_UNSPEC || cursor->ai_family == family) {
65 IPAddress ip;
66 if (IPFromAddrInfo(cursor, &ip)) {
67 addresses->push_back(ip);
68 }
69 }
70 }
71 freeaddrinfo(result);
72 return 0;
wu@webrtc.org2a81a382014-01-03 22:08:47 +000073#endif // !__native_client__
henrike@webrtc.org0e118e72013-07-10 00:45:36 +000074}
75
76// AsyncResolver
sergeyu@chromium.org19da4652013-11-13 22:48:52 +000077AsyncResolver::AsyncResolver() : error_(-1) {
78}
79
80void AsyncResolver::Start(const SocketAddress& addr) {
81 addr_ = addr;
82 // SignalThred Start will kickoff the resolve process.
83 SignalThread::Start();
84}
85
86bool AsyncResolver::GetResolvedAddress(int family, SocketAddress* addr) const {
87 if (error_ != 0 || addresses_.empty())
88 return false;
89
90 *addr = addr_;
91 for (size_t i = 0; i < addresses_.size(); ++i) {
92 if (family == addresses_[i].family()) {
wu@webrtc.orgd8f165f2014-03-24 17:01:50 +000093 addr->SetResolvedIP(addresses_[i]);
sergeyu@chromium.org19da4652013-11-13 22:48:52 +000094 return true;
95 }
96 }
97 return false;
henrike@webrtc.org0e118e72013-07-10 00:45:36 +000098}
99
100void AsyncResolver::DoWork() {
101 error_ = ResolveHostname(addr_.hostname().c_str(), addr_.family(),
102 &addresses_);
103}
104
105void AsyncResolver::OnWorkDone() {
sergeyu@chromium.org19da4652013-11-13 22:48:52 +0000106 SignalDone(this);
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000107}
108
109const char* inet_ntop(int af, const void *src, char* dst, socklen_t size) {
110#ifdef WIN32
111 return win32_inet_ntop(af, src, dst, size);
112#else
113 return ::inet_ntop(af, src, dst, size);
114#endif
115}
116
117int inet_pton(int af, const char* src, void *dst) {
118#ifdef WIN32
119 return win32_inet_pton(af, src, dst);
120#else
121 return ::inet_pton(af, src, dst);
122#endif
123}
124
125bool HasIPv6Enabled() {
126#ifndef WIN32
127 // We only need to check this for Windows XP (so far).
128 return true;
129#else
130 if (IsWindowsVistaOrLater()) {
131 return true;
132 }
133 if (!IsWindowsXpOrLater()) {
134 return false;
135 }
136 DWORD protbuff_size = 4096;
wu@webrtc.org5c9dd592013-10-25 21:18:33 +0000137 scoped_ptr<char[]> protocols;
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000138 LPWSAPROTOCOL_INFOW protocol_infos = NULL;
139 int requested_protocols[2] = {AF_INET6, 0};
140
141 int err = 0;
142 int ret = 0;
143 // Check for protocols in a do-while loop until we provide a buffer large
144 // enough. (WSCEnumProtocols sets protbuff_size to its desired value).
145 // It is extremely unlikely that this will loop more than once.
146 do {
147 protocols.reset(new char[protbuff_size]);
148 protocol_infos = reinterpret_cast<LPWSAPROTOCOL_INFOW>(protocols.get());
149 ret = WSCEnumProtocols(requested_protocols, protocol_infos,
150 &protbuff_size, &err);
151 } while (ret == SOCKET_ERROR && err == WSAENOBUFS);
152
153 if (ret == SOCKET_ERROR) {
154 return false;
155 }
156
157 // Even if ret is positive, check specifically for IPv6.
158 // Non-IPv6 enabled WinXP will still return a RAW protocol.
159 for (int i = 0; i < ret; ++i) {
160 if (protocol_infos[i].iAddressFamily == AF_INET6) {
161 return true;
162 }
163 }
164 return false;
165#endif
166}
167} // namespace talk_base