henrike@webrtc.org | f048872 | 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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "rtc_base/net_helpers.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | |
Mirko Bonadei | e5f4c6b | 2021-01-15 10:41:01 +0100 | [diff] [blame] | 13 | #include <memory> |
Ali Tofigh | 2ab914c | 2022-04-13 12:55:15 +0200 | [diff] [blame] | 14 | #include <string> |
| 15 | |
| 16 | #include "absl/strings/string_view.h" |
Mirko Bonadei | e5f4c6b | 2021-01-15 10:41:01 +0100 | [diff] [blame] | 17 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 18 | #if defined(WEBRTC_WIN) |
| 19 | #include <ws2spi.h> |
| 20 | #include <ws2tcpip.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 21 | |
Nico Weber | 79bd4f1 | 2021-10-05 15:01:26 -0400 | [diff] [blame] | 22 | #include "rtc_base/win/windows_version.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 23 | #endif |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 24 | #if defined(WEBRTC_POSIX) && !defined(__native_client__) |
Mirko Bonadei | e5f4c6b | 2021-01-15 10:41:01 +0100 | [diff] [blame] | 25 | #include <arpa/inet.h> |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 26 | #if defined(WEBRTC_ANDROID) |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 27 | #include "rtc_base/ifaddrs_android.h" |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 28 | #else |
| 29 | #include <ifaddrs.h> |
| 30 | #endif |
| 31 | #endif // defined(WEBRTC_POSIX) && !defined(__native_client__) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 32 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 33 | namespace rtc { |
| 34 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 35 | const char* inet_ntop(int af, const void* src, char* dst, socklen_t size) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 36 | #if defined(WEBRTC_WIN) |
| 37 | return win32_inet_ntop(af, src, dst, size); |
| 38 | #else |
| 39 | return ::inet_ntop(af, src, dst, size); |
| 40 | #endif |
| 41 | } |
| 42 | |
Ali Tofigh | 2ab914c | 2022-04-13 12:55:15 +0200 | [diff] [blame] | 43 | int inet_pton(int af, absl::string_view src, void* dst) { |
Ali Tofigh | 98bfd99 | 2022-07-22 22:10:49 +0200 | [diff] [blame] | 44 | std::string src_str(src); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 45 | #if defined(WEBRTC_WIN) |
Ali Tofigh | 2ab914c | 2022-04-13 12:55:15 +0200 | [diff] [blame] | 46 | return win32_inet_pton(af, src_str.c_str(), dst); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 47 | #else |
Ali Tofigh | 2ab914c | 2022-04-13 12:55:15 +0200 | [diff] [blame] | 48 | return ::inet_pton(af, src_str.c_str(), dst); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 49 | #endif |
| 50 | } |
| 51 | |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 52 | bool HasIPv4Enabled() { |
| 53 | #if defined(WEBRTC_POSIX) && !defined(__native_client__) |
| 54 | bool has_ipv4 = false; |
| 55 | struct ifaddrs* ifa; |
| 56 | if (getifaddrs(&ifa) < 0) { |
| 57 | return false; |
| 58 | } |
| 59 | for (struct ifaddrs* cur = ifa; cur != nullptr; cur = cur->ifa_next) { |
Björn Terelius | ebd2010 | 2021-03-19 15:15:18 +0100 | [diff] [blame] | 60 | if (cur->ifa_addr != nullptr && cur->ifa_addr->sa_family == AF_INET) { |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 61 | has_ipv4 = true; |
| 62 | break; |
| 63 | } |
| 64 | } |
| 65 | freeifaddrs(ifa); |
| 66 | return has_ipv4; |
| 67 | #else |
| 68 | return true; |
| 69 | #endif |
| 70 | } |
| 71 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 72 | bool HasIPv6Enabled() { |
Robin Raymond | ce1b140 | 2018-11-22 20:10:11 -0500 | [diff] [blame] | 73 | #if defined(WINUWP) |
| 74 | // WinUWP always has IPv6 capability. |
| 75 | return true; |
| 76 | #elif defined(WEBRTC_WIN) |
Nico Weber | 79bd4f1 | 2021-10-05 15:01:26 -0400 | [diff] [blame] | 77 | if (rtc::rtc_win::GetVersion() >= rtc::rtc_win::Version::VERSION_VISTA) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 78 | return true; |
| 79 | } |
Nico Weber | 79bd4f1 | 2021-10-05 15:01:26 -0400 | [diff] [blame] | 80 | if (rtc::rtc_win::GetVersion() < rtc::rtc_win::Version::VERSION_XP) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 81 | return false; |
| 82 | } |
| 83 | DWORD protbuff_size = 4096; |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 84 | std::unique_ptr<char[]> protocols; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 85 | LPWSAPROTOCOL_INFOW protocol_infos = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 86 | int requested_protocols[2] = {AF_INET6, 0}; |
| 87 | |
| 88 | int err = 0; |
| 89 | int ret = 0; |
| 90 | // Check for protocols in a do-while loop until we provide a buffer large |
| 91 | // enough. (WSCEnumProtocols sets protbuff_size to its desired value). |
| 92 | // It is extremely unlikely that this will loop more than once. |
| 93 | do { |
| 94 | protocols.reset(new char[protbuff_size]); |
| 95 | protocol_infos = reinterpret_cast<LPWSAPROTOCOL_INFOW>(protocols.get()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 96 | ret = WSCEnumProtocols(requested_protocols, protocol_infos, &protbuff_size, |
| 97 | &err); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 98 | } while (ret == SOCKET_ERROR && err == WSAENOBUFS); |
| 99 | |
| 100 | if (ret == SOCKET_ERROR) { |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | // Even if ret is positive, check specifically for IPv6. |
| 105 | // Non-IPv6 enabled WinXP will still return a RAW protocol. |
| 106 | for (int i = 0; i < ret; ++i) { |
| 107 | if (protocol_infos[i].iAddressFamily == AF_INET6) { |
| 108 | return true; |
| 109 | } |
| 110 | } |
| 111 | return false; |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 112 | #elif defined(WEBRTC_POSIX) && !defined(__native_client__) |
| 113 | bool has_ipv6 = false; |
| 114 | struct ifaddrs* ifa; |
| 115 | if (getifaddrs(&ifa) < 0) { |
| 116 | return false; |
| 117 | } |
| 118 | for (struct ifaddrs* cur = ifa; cur != nullptr; cur = cur->ifa_next) { |
Björn Terelius | ebd2010 | 2021-03-19 15:15:18 +0100 | [diff] [blame] | 119 | if (cur->ifa_addr != nullptr && cur->ifa_addr->sa_family == AF_INET6) { |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 120 | has_ipv6 = true; |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | freeifaddrs(ifa); |
| 125 | return has_ipv6; |
| 126 | #else |
| 127 | return true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 128 | #endif |
| 129 | } |
| 130 | } // namespace rtc |