| OLD | NEW |
| (Empty) |
| 1 /* | |
| 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 | |
| 13 #include <memory> | |
| 14 | |
| 15 #if defined(WEBRTC_WIN) | |
| 16 #include <ws2spi.h> | |
| 17 #include <ws2tcpip.h> | |
| 18 #include "webrtc/base/win32.h" | |
| 19 #endif | |
| 20 #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__) | |
| 27 | |
| 28 #include "webrtc/base/byteorder.h" | |
| 29 #include "webrtc/base/checks.h" | |
| 30 #include "webrtc/base/logging.h" | |
| 31 #include "webrtc/base/signalthread.h" | |
| 32 | |
| 33 namespace rtc { | |
| 34 | |
| 35 int ResolveHostname(const std::string& hostname, int family, | |
| 36 std::vector<IPAddress>* addresses) { | |
| 37 #ifdef __native_client__ | |
| 38 RTC_NOTREACHED(); | |
| 39 LOG(LS_WARNING) << "ResolveHostname() is not implemented for NaCl"; | |
| 40 return -1; | |
| 41 #else // __native_client__ | |
| 42 if (!addresses) { | |
| 43 return -1; | |
| 44 } | |
| 45 addresses->clear(); | |
| 46 struct addrinfo* result = nullptr; | |
| 47 struct addrinfo hints = {0}; | |
| 48 hints.ai_family = family; | |
| 49 // |family| here will almost always be AF_UNSPEC, because |family| comes from | |
| 50 // AsyncResolver::addr_.family(), which comes from a SocketAddress constructed | |
| 51 // with a hostname. When a SocketAddress is constructed with a hostname, its | |
| 52 // family is AF_UNSPEC. However, if someday in the future we construct | |
| 53 // a SocketAddress with both a hostname and a family other than AF_UNSPEC, | |
| 54 // then it would be possible to get a specific family value here. | |
| 55 | |
| 56 // The behavior of AF_UNSPEC is roughly "get both ipv4 and ipv6", as | |
| 57 // documented by the various operating systems: | |
| 58 // Linux: http://man7.org/linux/man-pages/man3/getaddrinfo.3.html | |
| 59 // Windows: https://msdn.microsoft.com/en-us/library/windows/desktop/ | |
| 60 // ms738520(v=vs.85).aspx | |
| 61 // Mac: https://developer.apple.com/legacy/library/documentation/Darwin/ | |
| 62 // Reference/ManPages/man3/getaddrinfo.3.html | |
| 63 // Android (source code, not documentation): | |
| 64 // https://android.googlesource.com/platform/bionic/+/ | |
| 65 // 7e0bfb511e85834d7c6cb9631206b62f82701d60/libc/netbsd/net/getaddrinfo.c#1657 | |
| 66 hints.ai_flags = AI_ADDRCONFIG; | |
| 67 int ret = getaddrinfo(hostname.c_str(), nullptr, &hints, &result); | |
| 68 if (ret != 0) { | |
| 69 return ret; | |
| 70 } | |
| 71 struct addrinfo* cursor = result; | |
| 72 for (; cursor; cursor = cursor->ai_next) { | |
| 73 if (family == AF_UNSPEC || cursor->ai_family == family) { | |
| 74 IPAddress ip; | |
| 75 if (IPFromAddrInfo(cursor, &ip)) { | |
| 76 addresses->push_back(ip); | |
| 77 } | |
| 78 } | |
| 79 } | |
| 80 freeaddrinfo(result); | |
| 81 return 0; | |
| 82 #endif // !__native_client__ | |
| 83 } | |
| 84 | |
| 85 // AsyncResolver | |
| 86 AsyncResolver::AsyncResolver() | |
| 87 : SignalThread(false /* use_socket_server */), error_(-1) {} | |
| 88 | |
| 89 AsyncResolver::~AsyncResolver() = default; | |
| 90 | |
| 91 void AsyncResolver::Start(const SocketAddress& addr) { | |
| 92 addr_ = addr; | |
| 93 // SignalThred Start will kickoff the resolve process. | |
| 94 SignalThread::Start(); | |
| 95 } | |
| 96 | |
| 97 bool AsyncResolver::GetResolvedAddress(int family, SocketAddress* addr) const { | |
| 98 if (error_ != 0 || addresses_.empty()) | |
| 99 return false; | |
| 100 | |
| 101 *addr = addr_; | |
| 102 for (size_t i = 0; i < addresses_.size(); ++i) { | |
| 103 if (family == addresses_[i].family()) { | |
| 104 addr->SetResolvedIP(addresses_[i]); | |
| 105 return true; | |
| 106 } | |
| 107 } | |
| 108 return false; | |
| 109 } | |
| 110 | |
| 111 int AsyncResolver::GetError() const { | |
| 112 return error_; | |
| 113 } | |
| 114 | |
| 115 void AsyncResolver::Destroy(bool wait) { | |
| 116 SignalThread::Destroy(wait); | |
| 117 } | |
| 118 | |
| 119 void AsyncResolver::DoWork() { | |
| 120 error_ = ResolveHostname(addr_.hostname().c_str(), addr_.family(), | |
| 121 &addresses_); | |
| 122 } | |
| 123 | |
| 124 void AsyncResolver::OnWorkDone() { | |
| 125 SignalDone(this); | |
| 126 } | |
| 127 | |
| 128 const char* inet_ntop(int af, const void *src, char* dst, socklen_t size) { | |
| 129 #if defined(WEBRTC_WIN) | |
| 130 return win32_inet_ntop(af, src, dst, size); | |
| 131 #else | |
| 132 return ::inet_ntop(af, src, dst, size); | |
| 133 #endif | |
| 134 } | |
| 135 | |
| 136 int inet_pton(int af, const char* src, void *dst) { | |
| 137 #if defined(WEBRTC_WIN) | |
| 138 return win32_inet_pton(af, src, dst); | |
| 139 #else | |
| 140 return ::inet_pton(af, src, dst); | |
| 141 #endif | |
| 142 } | |
| 143 | |
| 144 bool HasIPv4Enabled() { | |
| 145 #if defined(WEBRTC_POSIX) && !defined(__native_client__) | |
| 146 bool has_ipv4 = false; | |
| 147 struct ifaddrs* ifa; | |
| 148 if (getifaddrs(&ifa) < 0) { | |
| 149 return false; | |
| 150 } | |
| 151 for (struct ifaddrs* cur = ifa; cur != nullptr; cur = cur->ifa_next) { | |
| 152 if (cur->ifa_addr->sa_family == AF_INET) { | |
| 153 has_ipv4 = true; | |
| 154 break; | |
| 155 } | |
| 156 } | |
| 157 freeifaddrs(ifa); | |
| 158 return has_ipv4; | |
| 159 #else | |
| 160 return true; | |
| 161 #endif | |
| 162 } | |
| 163 | |
| 164 bool HasIPv6Enabled() { | |
| 165 #if defined(WEBRTC_WIN) | |
| 166 if (IsWindowsVistaOrLater()) { | |
| 167 return true; | |
| 168 } | |
| 169 if (!IsWindowsXpOrLater()) { | |
| 170 return false; | |
| 171 } | |
| 172 DWORD protbuff_size = 4096; | |
| 173 std::unique_ptr<char[]> protocols; | |
| 174 LPWSAPROTOCOL_INFOW protocol_infos = nullptr; | |
| 175 int requested_protocols[2] = {AF_INET6, 0}; | |
| 176 | |
| 177 int err = 0; | |
| 178 int ret = 0; | |
| 179 // Check for protocols in a do-while loop until we provide a buffer large | |
| 180 // enough. (WSCEnumProtocols sets protbuff_size to its desired value). | |
| 181 // It is extremely unlikely that this will loop more than once. | |
| 182 do { | |
| 183 protocols.reset(new char[protbuff_size]); | |
| 184 protocol_infos = reinterpret_cast<LPWSAPROTOCOL_INFOW>(protocols.get()); | |
| 185 ret = WSCEnumProtocols(requested_protocols, protocol_infos, | |
| 186 &protbuff_size, &err); | |
| 187 } while (ret == SOCKET_ERROR && err == WSAENOBUFS); | |
| 188 | |
| 189 if (ret == SOCKET_ERROR) { | |
| 190 return false; | |
| 191 } | |
| 192 | |
| 193 // Even if ret is positive, check specifically for IPv6. | |
| 194 // Non-IPv6 enabled WinXP will still return a RAW protocol. | |
| 195 for (int i = 0; i < ret; ++i) { | |
| 196 if (protocol_infos[i].iAddressFamily == AF_INET6) { | |
| 197 return true; | |
| 198 } | |
| 199 } | |
| 200 return false; | |
| 201 #elif defined(WEBRTC_POSIX) && !defined(__native_client__) | |
| 202 bool has_ipv6 = false; | |
| 203 struct ifaddrs* ifa; | |
| 204 if (getifaddrs(&ifa) < 0) { | |
| 205 return false; | |
| 206 } | |
| 207 for (struct ifaddrs* cur = ifa; cur != nullptr; cur = cur->ifa_next) { | |
| 208 if (cur->ifa_addr->sa_family == AF_INET6) { | |
| 209 has_ipv6 = true; | |
| 210 break; | |
| 211 } | |
| 212 } | |
| 213 freeifaddrs(ifa); | |
| 214 return has_ipv6; | |
| 215 #else | |
| 216 return true; | |
| 217 #endif | |
| 218 } | |
| 219 } // namespace rtc | |
| OLD | NEW |