| 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 "rtc_base/net_helpers.h" |
| 12 |
| 13 | #include <memory> |
| 14 |
| 15 | #if defined(WEBRTC_WIN) |
| 16 | #include <ws2spi.h> |
| 17 | #include <ws2tcpip.h> |
| 18 |
| 19 | #include "rtc_base/win32.h" |
| 20 | #endif |
| 21 | #if defined(WEBRTC_POSIX) && !defined(__native_client__) |
| 22 | #include <arpa/inet.h> |
| 23 | #if defined(WEBRTC_ANDROID) |
| 24 | #include "rtc_base/ifaddrs_android.h" |
| 25 | #else |
| 26 | #include <ifaddrs.h> |
| 27 | #endif |
| 28 | #endif // defined(WEBRTC_POSIX) && !defined(__native_client__) |
| 29 |
| 30 | namespace rtc { |
| 31 |
| 32 | const char* inet_ntop(int af, const void* src, char* dst, socklen_t size) { |
| 33 | #if defined(WEBRTC_WIN) |
| 34 | return win32_inet_ntop(af, src, dst, size); |
| 35 | #else |
| 36 | return ::inet_ntop(af, src, dst, size); |
| 37 | #endif |
| 38 | } |
| 39 |
| 40 | int inet_pton(int af, const char* src, void* dst) { |
| 41 | #if defined(WEBRTC_WIN) |
| 42 | return win32_inet_pton(af, src, dst); |
| 43 | #else |
| 44 | return ::inet_pton(af, src, dst); |
| 45 | #endif |
| 46 | } |
| 47 |
| 48 | bool HasIPv4Enabled() { |
| 49 | #if defined(WEBRTC_POSIX) && !defined(__native_client__) |
| 50 | bool has_ipv4 = false; |
| 51 | struct ifaddrs* ifa; |
| 52 | if (getifaddrs(&ifa) < 0) { |
| 53 | return false; |
| 54 | } |
| 55 | for (struct ifaddrs* cur = ifa; cur != nullptr; cur = cur->ifa_next) { |
| 56 | if (cur->ifa_addr != nullptr && cur->ifa_addr->sa_family == AF_INET) { |
| 57 | has_ipv4 = true; |
| 58 | break; |
| 59 | } |
| 60 | } |
| 61 | freeifaddrs(ifa); |
| 62 | return has_ipv4; |
| 63 | #else |
| 64 | return true; |
| 65 | #endif |
| 66 | } |
| 67 |
| 68 | bool HasIPv6Enabled() { |
| 69 | #if defined(WINUWP) |
| 70 | // WinUWP always has IPv6 capability. |
| 71 | return true; |
| 72 | #elif defined(WEBRTC_WIN) |
| 73 | if (IsWindowsVistaOrLater()) { |
| 74 | return true; |
| 75 | } |
| 76 | if (!IsWindowsXpOrLater()) { |
| 77 | return false; |
| 78 | } |
| 79 | DWORD protbuff_size = 4096; |
| 80 | std::unique_ptr<char[]> protocols; |
| 81 | LPWSAPROTOCOL_INFOW protocol_infos = nullptr; |
| 82 | int requested_protocols[2] = {AF_INET6, 0}; |
| 83 |
| 84 | int err = 0; |
| 85 | int ret = 0; |
| 86 | // Check for protocols in a do-while loop until we provide a buffer large |
| 87 | // enough. (WSCEnumProtocols sets protbuff_size to its desired value). |
| 88 | // It is extremely unlikely that this will loop more than once. |
| 89 | do { |
| 90 | protocols.reset(new char[protbuff_size]); |
| 91 | protocol_infos = reinterpret_cast<LPWSAPROTOCOL_INFOW>(protocols.get()); |
| 92 | ret = WSCEnumProtocols(requested_protocols, protocol_infos, &protbuff_size, |
| 93 | &err); |
| 94 | } while (ret == SOCKET_ERROR && err == WSAENOBUFS); |
| 95 |
| 96 | if (ret == SOCKET_ERROR) { |
| 97 | return false; |
| 98 | } |
| 99 |
| 100 | // Even if ret is positive, check specifically for IPv6. |
| 101 | // Non-IPv6 enabled WinXP will still return a RAW protocol. |
| 102 | for (int i = 0; i < ret; ++i) { |
| 103 | if (protocol_infos[i].iAddressFamily == AF_INET6) { |
| 104 | return true; |
| 105 | } |
| 106 | } |
| 107 | return false; |
| 108 | #elif defined(WEBRTC_POSIX) && !defined(__native_client__) |
| 109 | bool has_ipv6 = false; |
| 110 | struct ifaddrs* ifa; |
| 111 | if (getifaddrs(&ifa) < 0) { |
| 112 | return false; |
| 113 | } |
| 114 | for (struct ifaddrs* cur = ifa; cur != nullptr; cur = cur->ifa_next) { |
| 115 | if (cur->ifa_addr != nullptr && cur->ifa_addr->sa_family == AF_INET6) { |
| 116 | has_ipv6 = true; |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | freeifaddrs(ifa); |
| 121 | return has_ipv6; |
| 122 | #else |
| 123 | return true; |
| 124 | #endif |
| 125 | } |
| 126 | } // namespace rtc |
| 127 |