henrike@webrtc.org | 28e2075 | 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" |
| 37 | #include "talk/base/signalthread.h" |
| 38 | |
| 39 | namespace talk_base { |
| 40 | |
| 41 | int ResolveHostname(const std::string& hostname, int family, |
| 42 | std::vector<IPAddress>* addresses) { |
| 43 | if (!addresses) { |
| 44 | return -1; |
| 45 | } |
| 46 | addresses->clear(); |
| 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); |
| 53 | if (ret != 0) { |
| 54 | return ret; |
| 55 | } |
| 56 | struct addrinfo* cursor = result; |
| 57 | for (; cursor; cursor = cursor->ai_next) { |
| 58 | if (family == AF_UNSPEC || cursor->ai_family == family) { |
| 59 | IPAddress ip; |
| 60 | if (IPFromAddrInfo(cursor, &ip)) { |
| 61 | addresses->push_back(ip); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | freeaddrinfo(result); |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | // AsyncResolver |
| 70 | AsyncResolver::AsyncResolver() : error_(0) { |
| 71 | } |
| 72 | |
| 73 | void AsyncResolver::DoWork() { |
| 74 | error_ = ResolveHostname(addr_.hostname().c_str(), addr_.family(), |
| 75 | &addresses_); |
| 76 | } |
| 77 | |
| 78 | void AsyncResolver::OnWorkDone() { |
| 79 | if (addresses_.size() > 0) { |
| 80 | addr_.SetIP(addresses_[0]); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | const char* inet_ntop(int af, const void *src, char* dst, socklen_t size) { |
| 85 | #ifdef WIN32 |
| 86 | return win32_inet_ntop(af, src, dst, size); |
| 87 | #else |
| 88 | return ::inet_ntop(af, src, dst, size); |
| 89 | #endif |
| 90 | } |
| 91 | |
| 92 | int inet_pton(int af, const char* src, void *dst) { |
| 93 | #ifdef WIN32 |
| 94 | return win32_inet_pton(af, src, dst); |
| 95 | #else |
| 96 | return ::inet_pton(af, src, dst); |
| 97 | #endif |
| 98 | } |
| 99 | |
| 100 | bool HasIPv6Enabled() { |
| 101 | #ifndef WIN32 |
| 102 | // We only need to check this for Windows XP (so far). |
| 103 | return true; |
| 104 | #else |
| 105 | if (IsWindowsVistaOrLater()) { |
| 106 | return true; |
| 107 | } |
| 108 | if (!IsWindowsXpOrLater()) { |
| 109 | return false; |
| 110 | } |
| 111 | DWORD protbuff_size = 4096; |
| 112 | scoped_array<char> protocols; |
| 113 | LPWSAPROTOCOL_INFOW protocol_infos = NULL; |
| 114 | int requested_protocols[2] = {AF_INET6, 0}; |
| 115 | |
| 116 | int err = 0; |
| 117 | int ret = 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. |
| 121 | do { |
| 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); |
| 127 | |
| 128 | if (ret == SOCKET_ERROR) { |
| 129 | return false; |
| 130 | } |
| 131 | |
| 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) { |
| 136 | return true; |
| 137 | } |
| 138 | } |
| 139 | return false; |
| 140 | #endif |
| 141 | } |
| 142 | } // namespace talk_base |