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