blob: d9015283b0baef98f2a370acdf334f9603a8765d [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
20
21#include "webrtc/base/byteorder.h"
22#include "webrtc/base/logging.h"
23#include "webrtc/base/signalthread.h"
24
25namespace rtc {
26
27int ResolveHostname(const std::string& hostname, int family,
28 std::vector<IPAddress>* addresses) {
29#ifdef __native_client__
30 ASSERT(false);
31 LOG(LS_WARNING) << "ResolveHostname() is not implemented for NaCl";
32 return -1;
33#else // __native_client__
34 if (!addresses) {
35 return -1;
36 }
37 addresses->clear();
38 struct addrinfo* result = NULL;
39 struct addrinfo hints = {0};
40 // TODO(djw): For now this is IPv4 only so existing users remain unaffected.
41 hints.ai_family = AF_INET;
42 hints.ai_flags = AI_ADDRCONFIG;
43 int ret = getaddrinfo(hostname.c_str(), NULL, &hints, &result);
44 if (ret != 0) {
45 return ret;
46 }
47 struct addrinfo* cursor = result;
48 for (; cursor; cursor = cursor->ai_next) {
49 if (family == AF_UNSPEC || cursor->ai_family == family) {
50 IPAddress ip;
51 if (IPFromAddrInfo(cursor, &ip)) {
52 addresses->push_back(ip);
53 }
54 }
55 }
56 freeaddrinfo(result);
57 return 0;
58#endif // !__native_client__
59}
60
61// AsyncResolver
62AsyncResolver::AsyncResolver() : error_(-1) {
63}
64
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000065AsyncResolver::~AsyncResolver() = default;
66
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000067void AsyncResolver::Start(const SocketAddress& addr) {
68 addr_ = addr;
69 // SignalThred Start will kickoff the resolve process.
70 SignalThread::Start();
71}
72
73bool AsyncResolver::GetResolvedAddress(int family, SocketAddress* addr) const {
74 if (error_ != 0 || addresses_.empty())
75 return false;
76
77 *addr = addr_;
78 for (size_t i = 0; i < addresses_.size(); ++i) {
79 if (family == addresses_[i].family()) {
80 addr->SetResolvedIP(addresses_[i]);
81 return true;
82 }
83 }
84 return false;
85}
86
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000087int AsyncResolver::GetError() const {
88 return error_;
89}
90
91void AsyncResolver::Destroy(bool wait) {
92 SignalThread::Destroy(wait);
93}
94
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000095void AsyncResolver::DoWork() {
96 error_ = ResolveHostname(addr_.hostname().c_str(), addr_.family(),
97 &addresses_);
98}
99
100void AsyncResolver::OnWorkDone() {
101 SignalDone(this);
102}
103
104const char* inet_ntop(int af, const void *src, char* dst, socklen_t size) {
105#if defined(WEBRTC_WIN)
106 return win32_inet_ntop(af, src, dst, size);
107#else
108 return ::inet_ntop(af, src, dst, size);
109#endif
110}
111
112int inet_pton(int af, const char* src, void *dst) {
113#if defined(WEBRTC_WIN)
114 return win32_inet_pton(af, src, dst);
115#else
116 return ::inet_pton(af, src, dst);
117#endif
118}
119
120bool HasIPv6Enabled() {
121#if !defined(WEBRTC_WIN)
122 // We only need to check this for Windows XP (so far).
123 return true;
124#else
125 if (IsWindowsVistaOrLater()) {
126 return true;
127 }
128 if (!IsWindowsXpOrLater()) {
129 return false;
130 }
131 DWORD protbuff_size = 4096;
jbauch555604a2016-04-26 03:13:22 -0700132 std::unique_ptr<char[]> protocols;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000133 LPWSAPROTOCOL_INFOW protocol_infos = NULL;
134 int requested_protocols[2] = {AF_INET6, 0};
135
136 int err = 0;
137 int ret = 0;
138 // Check for protocols in a do-while loop until we provide a buffer large
139 // enough. (WSCEnumProtocols sets protbuff_size to its desired value).
140 // It is extremely unlikely that this will loop more than once.
141 do {
142 protocols.reset(new char[protbuff_size]);
143 protocol_infos = reinterpret_cast<LPWSAPROTOCOL_INFOW>(protocols.get());
144 ret = WSCEnumProtocols(requested_protocols, protocol_infos,
145 &protbuff_size, &err);
146 } while (ret == SOCKET_ERROR && err == WSAENOBUFS);
147
148 if (ret == SOCKET_ERROR) {
149 return false;
150 }
151
152 // Even if ret is positive, check specifically for IPv6.
153 // Non-IPv6 enabled WinXP will still return a RAW protocol.
154 for (int i = 0; i < ret; ++i) {
155 if (protocol_infos[i].iAddressFamily == AF_INET6) {
156 return true;
157 }
158 }
159 return false;
160#endif
161}
162} // namespace rtc