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