blob: e4d0c52d9f6c8b02daf4abe6113e7ada767a259e [file] [log] [blame]
henrike@webrtc.org28e20752013-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"
37#include "talk/base/signalthread.h"
38
39namespace talk_base {
40
41int ResolveHostname(const std::string& hostname, int family,
42 std::vector<IPAddress>* addresses) {
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +000043#ifdef __native_client__
44 ASSERT(false);
45 LOG(LS_WARNING) << "ResolveHostname() is not implemented for NaCl";
46 return -1;
47#else // __native_client__
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048 if (!addresses) {
49 return -1;
50 }
51 addresses->clear();
52 struct addrinfo* result = NULL;
53 struct addrinfo hints = {0};
54 // TODO(djw): For now this is IPv4 only so existing users remain unaffected.
55 hints.ai_family = AF_INET;
56 hints.ai_flags = AI_ADDRCONFIG;
57 int ret = getaddrinfo(hostname.c_str(), NULL, &hints, &result);
58 if (ret != 0) {
59 return ret;
60 }
61 struct addrinfo* cursor = result;
62 for (; cursor; cursor = cursor->ai_next) {
63 if (family == AF_UNSPEC || cursor->ai_family == family) {
64 IPAddress ip;
65 if (IPFromAddrInfo(cursor, &ip)) {
66 addresses->push_back(ip);
67 }
68 }
69 }
70 freeaddrinfo(result);
71 return 0;
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +000072#endif // !__native_client__
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073}
74
75// AsyncResolver
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +000076AsyncResolver::AsyncResolver() : error_(-1) {
77}
78
79void AsyncResolver::Start(const SocketAddress& addr) {
80 addr_ = addr;
81 // SignalThred Start will kickoff the resolve process.
82 SignalThread::Start();
83}
84
85bool AsyncResolver::GetResolvedAddress(int family, SocketAddress* addr) const {
86 if (error_ != 0 || addresses_.empty())
87 return false;
88
89 *addr = addr_;
90 for (size_t i = 0; i < addresses_.size(); ++i) {
91 if (family == addresses_[i].family()) {
92 addr->SetIP(addresses_[i]);
93 return true;
94 }
95 }
96 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097}
98
99void AsyncResolver::DoWork() {
100 error_ = ResolveHostname(addr_.hostname().c_str(), addr_.family(),
101 &addresses_);
102}
103
104void AsyncResolver::OnWorkDone() {
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000105 SignalDone(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106}
107
108const char* inet_ntop(int af, const void *src, char* dst, socklen_t size) {
109#ifdef WIN32
110 return win32_inet_ntop(af, src, dst, size);
111#else
112 return ::inet_ntop(af, src, dst, size);
113#endif
114}
115
116int inet_pton(int af, const char* src, void *dst) {
117#ifdef WIN32
118 return win32_inet_pton(af, src, dst);
119#else
120 return ::inet_pton(af, src, dst);
121#endif
122}
123
124bool HasIPv6Enabled() {
125#ifndef WIN32
126 // We only need to check this for Windows XP (so far).
127 return true;
128#else
129 if (IsWindowsVistaOrLater()) {
130 return true;
131 }
132 if (!IsWindowsXpOrLater()) {
133 return false;
134 }
135 DWORD protbuff_size = 4096;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000136 scoped_ptr<char[]> protocols;
henrike@webrtc.org28e20752013-07-10 00:45:36 +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;
164#endif
165}
166} // namespace talk_base