3 * Copyright 2008, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #include "talk/base/nethelpers.h"
33 #include "talk/base/win32.h"
36 #include "talk/base/byteorder.h"
37 #include "talk/base/signalthread.h"
41 int ResolveHostname(const std::string& hostname, int family,
42 std::vector<IPAddress>* addresses) {
47 struct addrinfo* result = NULL;
48 struct addrinfo hints = {0};
49 // TODO(djw): For now this is IPv4 only so existing users remain unaffected.
50 hints.ai_family = AF_INET;
51 hints.ai_flags = AI_ADDRCONFIG;
52 int ret = getaddrinfo(hostname.c_str(), NULL, &hints, &result);
56 struct addrinfo* cursor = result;
57 for (; cursor; cursor = cursor->ai_next) {
58 if (family == AF_UNSPEC || cursor->ai_family == family) {
60 if (IPFromAddrInfo(cursor, &ip)) {
61 addresses->push_back(ip);
70 AsyncResolver::AsyncResolver() : error_(0) {
73 void AsyncResolver::DoWork() {
74 error_ = ResolveHostname(addr_.hostname().c_str(), addr_.family(),
78 void AsyncResolver::OnWorkDone() {
79 if (addresses_.size() > 0) {
80 addr_.SetIP(addresses_[0]);
84 const char* inet_ntop(int af, const void *src, char* dst, socklen_t size) {
86 return win32_inet_ntop(af, src, dst, size);
88 return ::inet_ntop(af, src, dst, size);
92 int inet_pton(int af, const char* src, void *dst) {
94 return win32_inet_pton(af, src, dst);
96 return ::inet_pton(af, src, dst);
100 bool HasIPv6Enabled() {
102 // We only need to check this for Windows XP (so far).
105 if (IsWindowsVistaOrLater()) {
108 if (!IsWindowsXpOrLater()) {
111 DWORD protbuff_size = 4096;
112 scoped_ptr<char[]> protocols;
113 LPWSAPROTOCOL_INFOW protocol_infos = NULL;
114 int requested_protocols[2] = {AF_INET6, 0};
118 // Check for protocols in a do-while loop until we provide a buffer large
119 // enough. (WSCEnumProtocols sets protbuff_size to its desired value).
120 // It is extremely unlikely that this will loop more than once.
122 protocols.reset(new char[protbuff_size]);
123 protocol_infos = reinterpret_cast<LPWSAPROTOCOL_INFOW>(protocols.get());
124 ret = WSCEnumProtocols(requested_protocols, protocol_infos,
125 &protbuff_size, &err);
126 } while (ret == SOCKET_ERROR && err == WSAENOBUFS);
128 if (ret == SOCKET_ERROR) {
132 // Even if ret is positive, check specifically for IPv6.
133 // Non-IPv6 enabled WinXP will still return a RAW protocol.
134 for (int i = 0; i < ret; ++i) {
135 if (protocol_infos[i].iAddressFamily == AF_INET6) {
142 } // namespace talk_base