mirror of
https://github.com/mollyim/webrtc.git
synced 2025-05-13 05:40:42 +01:00
Reland "Extract HasIPv4Enabled/HasIPv6Enabled."
This is a reland of commit 86cfe50c0e
The fix was to add a backwards compatible #include + build dep.
They will be removed once Chromium is migrated.
Original change's description:
> Extract HasIPv4Enabled/HasIPv6Enabled.
>
> Bug: b/292167110
> Change-Id: Idafa4ef23e87951bdd0276c29dee3e7f8be68476
> Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/312580
> Reviewed-by: Harald Alvestrand <hta@webrtc.org>
> Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org>
> Cr-Commit-Position: refs/heads/main@{#40478}
Bug: b/292167110
Change-Id: I9797f52adf15aba57e114d0a1efec0f757ead278
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/313264
Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#40491}
This commit is contained in:
parent
d212551ad3
commit
0d8b79eb40
12 changed files with 170 additions and 89 deletions
|
@ -202,6 +202,7 @@ if (rtc_include_tests) {
|
||||||
deps = [
|
deps = [
|
||||||
":rtc_p2p",
|
":rtc_p2p",
|
||||||
"../rtc_base:net_helpers",
|
"../rtc_base:net_helpers",
|
||||||
|
"../rtc_base:net_test_helpers",
|
||||||
"../rtc_base:task_queue_for_test",
|
"../rtc_base:task_queue_for_test",
|
||||||
"../rtc_base:threading",
|
"../rtc_base:threading",
|
||||||
"../rtc_base/memory:always_valid_pointer",
|
"../rtc_base/memory:always_valid_pointer",
|
||||||
|
@ -312,6 +313,7 @@ if (rtc_include_tests) {
|
||||||
"../rtc_base:mdns_responder_interface",
|
"../rtc_base:mdns_responder_interface",
|
||||||
"../rtc_base:net_helper",
|
"../rtc_base:net_helper",
|
||||||
"../rtc_base:net_helpers",
|
"../rtc_base:net_helpers",
|
||||||
|
"../rtc_base:net_test_helpers",
|
||||||
"../rtc_base:network",
|
"../rtc_base:network",
|
||||||
"../rtc_base:network_constants",
|
"../rtc_base:network_constants",
|
||||||
"../rtc_base:proxy_info",
|
"../rtc_base:proxy_info",
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
#include "p2p/base/udp_port.h"
|
#include "p2p/base/udp_port.h"
|
||||||
#include "rtc_base/memory/always_valid_pointer.h"
|
#include "rtc_base/memory/always_valid_pointer.h"
|
||||||
#include "rtc_base/net_helpers.h"
|
#include "rtc_base/net_helpers.h"
|
||||||
|
#include "rtc_base/net_test_helpers.h"
|
||||||
#include "rtc_base/task_queue_for_test.h"
|
#include "rtc_base/task_queue_for_test.h"
|
||||||
#include "rtc_base/thread.h"
|
#include "rtc_base/thread.h"
|
||||||
|
|
||||||
|
|
|
@ -34,6 +34,7 @@
|
||||||
#include "rtc_base/nat_types.h"
|
#include "rtc_base/nat_types.h"
|
||||||
#include "rtc_base/net_helper.h"
|
#include "rtc_base/net_helper.h"
|
||||||
#include "rtc_base/net_helpers.h"
|
#include "rtc_base/net_helpers.h"
|
||||||
|
#include "rtc_base/net_test_helpers.h"
|
||||||
#include "rtc_base/network.h"
|
#include "rtc_base/network.h"
|
||||||
#include "rtc_base/network_constants.h"
|
#include "rtc_base/network_constants.h"
|
||||||
#include "rtc_base/network_monitor.h"
|
#include "rtc_base/network_monitor.h"
|
||||||
|
|
|
@ -879,7 +879,10 @@ rtc_library("net_helpers") {
|
||||||
"net_helpers.cc",
|
"net_helpers.cc",
|
||||||
"net_helpers.h",
|
"net_helpers.h",
|
||||||
]
|
]
|
||||||
deps = [ "system:rtc_export" ]
|
deps = [
|
||||||
|
":net_test_helpers",
|
||||||
|
"system:rtc_export",
|
||||||
|
]
|
||||||
if (is_android) {
|
if (is_android) {
|
||||||
deps += [ ":ifaddrs_android" ]
|
deps += [ ":ifaddrs_android" ]
|
||||||
}
|
}
|
||||||
|
@ -892,6 +895,26 @@ rtc_library("net_helpers") {
|
||||||
absl_deps = [ "//third_party/abseil-cpp/absl/strings" ]
|
absl_deps = [ "//third_party/abseil-cpp/absl/strings" ]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rtc_library("net_test_helpers") {
|
||||||
|
# TODO(mbonadei): Enable once net_helpers don't depend
|
||||||
|
# on this target anymore.
|
||||||
|
# testonly = true
|
||||||
|
sources = [
|
||||||
|
"net_test_helpers.cc",
|
||||||
|
"net_test_helpers.h",
|
||||||
|
]
|
||||||
|
deps = [ "system:rtc_export" ]
|
||||||
|
if (is_android) {
|
||||||
|
deps += [ ":ifaddrs_android" ]
|
||||||
|
}
|
||||||
|
if (is_win) {
|
||||||
|
deps += [
|
||||||
|
":win32",
|
||||||
|
"win:windows_version",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
rtc_library("async_resolver_interface") {
|
rtc_library("async_resolver_interface") {
|
||||||
visibility = [ "*" ]
|
visibility = [ "*" ]
|
||||||
sources = [
|
sources = [
|
||||||
|
@ -1778,6 +1801,7 @@ if (rtc_include_tests) {
|
||||||
":logging",
|
":logging",
|
||||||
":macromagic",
|
":macromagic",
|
||||||
":net_helpers",
|
":net_helpers",
|
||||||
|
":net_test_helpers",
|
||||||
":null_socket_server",
|
":null_socket_server",
|
||||||
":platform_thread",
|
":platform_thread",
|
||||||
":rtc_base_tests_utils",
|
":rtc_base_tests_utils",
|
||||||
|
@ -2043,6 +2067,7 @@ if (rtc_include_tests) {
|
||||||
":logging",
|
":logging",
|
||||||
":macromagic",
|
":macromagic",
|
||||||
":net_helpers",
|
":net_helpers",
|
||||||
|
":net_test_helpers",
|
||||||
":network",
|
":network",
|
||||||
":network_route",
|
":network_route",
|
||||||
":null_socket_server",
|
":null_socket_server",
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
#include "rtc_base/nat_socket_factory.h"
|
#include "rtc_base/nat_socket_factory.h"
|
||||||
#include "rtc_base/nat_types.h"
|
#include "rtc_base/nat_types.h"
|
||||||
#include "rtc_base/net_helpers.h"
|
#include "rtc_base/net_helpers.h"
|
||||||
|
#include "rtc_base/net_test_helpers.h"
|
||||||
#include "rtc_base/network.h"
|
#include "rtc_base/network.h"
|
||||||
#include "rtc_base/physical_socket_server.h"
|
#include "rtc_base/physical_socket_server.h"
|
||||||
#include "rtc_base/socket.h"
|
#include "rtc_base/socket.h"
|
||||||
|
|
|
@ -19,15 +19,9 @@
|
||||||
#include <ws2spi.h>
|
#include <ws2spi.h>
|
||||||
#include <ws2tcpip.h>
|
#include <ws2tcpip.h>
|
||||||
|
|
||||||
#include "rtc_base/win/windows_version.h"
|
|
||||||
#endif
|
#endif
|
||||||
#if defined(WEBRTC_POSIX) && !defined(__native_client__)
|
#if defined(WEBRTC_POSIX) && !defined(__native_client__)
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#if defined(WEBRTC_ANDROID)
|
|
||||||
#include "rtc_base/ifaddrs_android.h"
|
|
||||||
#else
|
|
||||||
#include <ifaddrs.h>
|
|
||||||
#endif
|
|
||||||
#endif // defined(WEBRTC_POSIX) && !defined(__native_client__)
|
#endif // defined(WEBRTC_POSIX) && !defined(__native_client__)
|
||||||
|
|
||||||
namespace rtc {
|
namespace rtc {
|
||||||
|
@ -48,83 +42,4 @@ int inet_pton(int af, absl::string_view src, void* dst) {
|
||||||
return ::inet_pton(af, src_str.c_str(), dst);
|
return ::inet_pton(af, src_str.c_str(), dst);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HasIPv4Enabled() {
|
|
||||||
#if defined(WEBRTC_POSIX) && !defined(__native_client__)
|
|
||||||
bool has_ipv4 = false;
|
|
||||||
struct ifaddrs* ifa;
|
|
||||||
if (getifaddrs(&ifa) < 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
for (struct ifaddrs* cur = ifa; cur != nullptr; cur = cur->ifa_next) {
|
|
||||||
if (cur->ifa_addr != nullptr && cur->ifa_addr->sa_family == AF_INET) {
|
|
||||||
has_ipv4 = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
freeifaddrs(ifa);
|
|
||||||
return has_ipv4;
|
|
||||||
#else
|
|
||||||
return true;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
bool HasIPv6Enabled() {
|
|
||||||
#if defined(WINUWP)
|
|
||||||
// WinUWP always has IPv6 capability.
|
|
||||||
return true;
|
|
||||||
#elif defined(WEBRTC_WIN)
|
|
||||||
if (rtc::rtc_win::GetVersion() >= rtc::rtc_win::Version::VERSION_VISTA) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (rtc::rtc_win::GetVersion() < rtc::rtc_win::Version::VERSION_XP) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
DWORD protbuff_size = 4096;
|
|
||||||
std::unique_ptr<char[]> protocols;
|
|
||||||
LPWSAPROTOCOL_INFOW protocol_infos = nullptr;
|
|
||||||
int requested_protocols[2] = {AF_INET6, 0};
|
|
||||||
|
|
||||||
int err = 0;
|
|
||||||
int ret = 0;
|
|
||||||
// Check for protocols in a do-while loop until we provide a buffer large
|
|
||||||
// enough. (WSCEnumProtocols sets protbuff_size to its desired value).
|
|
||||||
// It is extremely unlikely that this will loop more than once.
|
|
||||||
do {
|
|
||||||
protocols.reset(new char[protbuff_size]);
|
|
||||||
protocol_infos = reinterpret_cast<LPWSAPROTOCOL_INFOW>(protocols.get());
|
|
||||||
ret = WSCEnumProtocols(requested_protocols, protocol_infos, &protbuff_size,
|
|
||||||
&err);
|
|
||||||
} while (ret == SOCKET_ERROR && err == WSAENOBUFS);
|
|
||||||
|
|
||||||
if (ret == SOCKET_ERROR) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Even if ret is positive, check specifically for IPv6.
|
|
||||||
// Non-IPv6 enabled WinXP will still return a RAW protocol.
|
|
||||||
for (int i = 0; i < ret; ++i) {
|
|
||||||
if (protocol_infos[i].iAddressFamily == AF_INET6) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
#elif defined(WEBRTC_POSIX) && !defined(__native_client__)
|
|
||||||
bool has_ipv6 = false;
|
|
||||||
struct ifaddrs* ifa;
|
|
||||||
if (getifaddrs(&ifa) < 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
for (struct ifaddrs* cur = ifa; cur != nullptr; cur = cur->ifa_next) {
|
|
||||||
if (cur->ifa_addr != nullptr && cur->ifa_addr->sa_family == AF_INET6) {
|
|
||||||
has_ipv6 = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
freeifaddrs(ifa);
|
|
||||||
return has_ipv6;
|
|
||||||
#else
|
|
||||||
return true;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
} // namespace rtc
|
} // namespace rtc
|
||||||
|
|
|
@ -21,6 +21,8 @@
|
||||||
|
|
||||||
#include "absl/strings/string_view.h"
|
#include "absl/strings/string_view.h"
|
||||||
#include "rtc_base/system/rtc_export.h"
|
#include "rtc_base/system/rtc_export.h"
|
||||||
|
// TODO(mbonadei): Remove this, after Chromium tests have been migrated.
|
||||||
|
#include "rtc_base/net_test_helpers.h"
|
||||||
|
|
||||||
namespace rtc {
|
namespace rtc {
|
||||||
|
|
||||||
|
@ -29,9 +31,6 @@ namespace rtc {
|
||||||
const char* inet_ntop(int af, const void* src, char* dst, socklen_t size);
|
const char* inet_ntop(int af, const void* src, char* dst, socklen_t size);
|
||||||
int inet_pton(int af, absl::string_view src, void* dst);
|
int inet_pton(int af, absl::string_view src, void* dst);
|
||||||
|
|
||||||
RTC_EXPORT bool HasIPv4Enabled();
|
|
||||||
RTC_EXPORT bool HasIPv6Enabled();
|
|
||||||
|
|
||||||
} // namespace rtc
|
} // namespace rtc
|
||||||
|
|
||||||
#endif // RTC_BASE_NET_HELPERS_H_
|
#endif // RTC_BASE_NET_HELPERS_H_
|
||||||
|
|
111
rtc_base/net_test_helpers.cc
Normal file
111
rtc_base/net_test_helpers.cc
Normal file
|
@ -0,0 +1,111 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2023 The WebRTC Project Authors. All rights reserved.
|
||||||
|
*
|
||||||
|
* Use of this source code is governed by a BSD-style license
|
||||||
|
* that can be found in the LICENSE file in the root of the source
|
||||||
|
* tree. An additional intellectual property rights grant can be found
|
||||||
|
* in the file PATENTS. All contributing project authors may
|
||||||
|
* be found in the AUTHORS file in the root of the source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "rtc_base/net_test_helpers.h"
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#if defined(WEBRTC_WIN)
|
||||||
|
#include <ws2spi.h>
|
||||||
|
#include <ws2tcpip.h>
|
||||||
|
|
||||||
|
#include "rtc_base/win/windows_version.h"
|
||||||
|
#endif
|
||||||
|
#if defined(WEBRTC_POSIX) && !defined(__native_client__)
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#if defined(WEBRTC_ANDROID)
|
||||||
|
#include "rtc_base/ifaddrs_android.h"
|
||||||
|
#else
|
||||||
|
#include <ifaddrs.h>
|
||||||
|
#endif
|
||||||
|
#endif // defined(WEBRTC_POSIX) && !defined(__native_client__)
|
||||||
|
|
||||||
|
namespace rtc {
|
||||||
|
|
||||||
|
bool HasIPv4Enabled() {
|
||||||
|
#if defined(WEBRTC_POSIX) && !defined(__native_client__)
|
||||||
|
bool has_ipv4 = false;
|
||||||
|
struct ifaddrs* ifa;
|
||||||
|
if (getifaddrs(&ifa) < 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (struct ifaddrs* cur = ifa; cur != nullptr; cur = cur->ifa_next) {
|
||||||
|
if (cur->ifa_addr != nullptr && cur->ifa_addr->sa_family == AF_INET) {
|
||||||
|
has_ipv4 = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
freeifaddrs(ifa);
|
||||||
|
return has_ipv4;
|
||||||
|
#else
|
||||||
|
return true;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
bool HasIPv6Enabled() {
|
||||||
|
#if defined(WINUWP)
|
||||||
|
// WinUWP always has IPv6 capability.
|
||||||
|
return true;
|
||||||
|
#elif defined(WEBRTC_WIN)
|
||||||
|
if (rtc::rtc_win::GetVersion() >= rtc::rtc_win::Version::VERSION_VISTA) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (rtc::rtc_win::GetVersion() < rtc::rtc_win::Version::VERSION_XP) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
DWORD protbuff_size = 4096;
|
||||||
|
std::unique_ptr<char[]> protocols;
|
||||||
|
LPWSAPROTOCOL_INFOW protocol_infos = nullptr;
|
||||||
|
int requested_protocols[2] = {AF_INET6, 0};
|
||||||
|
|
||||||
|
int err = 0;
|
||||||
|
int ret = 0;
|
||||||
|
// Check for protocols in a do-while loop until we provide a buffer large
|
||||||
|
// enough. (WSCEnumProtocols sets protbuff_size to its desired value).
|
||||||
|
// It is extremely unlikely that this will loop more than once.
|
||||||
|
do {
|
||||||
|
protocols.reset(new char[protbuff_size]);
|
||||||
|
protocol_infos = reinterpret_cast<LPWSAPROTOCOL_INFOW>(protocols.get());
|
||||||
|
ret = WSCEnumProtocols(requested_protocols, protocol_infos, &protbuff_size,
|
||||||
|
&err);
|
||||||
|
} while (ret == SOCKET_ERROR && err == WSAENOBUFS);
|
||||||
|
|
||||||
|
if (ret == SOCKET_ERROR) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Even if ret is positive, check specifically for IPv6.
|
||||||
|
// Non-IPv6 enabled WinXP will still return a RAW protocol.
|
||||||
|
for (int i = 0; i < ret; ++i) {
|
||||||
|
if (protocol_infos[i].iAddressFamily == AF_INET6) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
#elif defined(WEBRTC_POSIX) && !defined(__native_client__)
|
||||||
|
bool has_ipv6 = false;
|
||||||
|
struct ifaddrs* ifa;
|
||||||
|
if (getifaddrs(&ifa) < 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (struct ifaddrs* cur = ifa; cur != nullptr; cur = cur->ifa_next) {
|
||||||
|
if (cur->ifa_addr != nullptr && cur->ifa_addr->sa_family == AF_INET6) {
|
||||||
|
has_ipv6 = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
freeifaddrs(ifa);
|
||||||
|
return has_ipv6;
|
||||||
|
#else
|
||||||
|
return true;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
} // namespace rtc
|
23
rtc_base/net_test_helpers.h
Normal file
23
rtc_base/net_test_helpers.h
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2023 The WebRTC Project Authors. All rights reserved.
|
||||||
|
*
|
||||||
|
* Use of this source code is governed by a BSD-style license
|
||||||
|
* that can be found in the LICENSE file in the root of the source
|
||||||
|
* tree. An additional intellectual property rights grant can be found
|
||||||
|
* in the file PATENTS. All contributing project authors may
|
||||||
|
* be found in the AUTHORS file in the root of the source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef RTC_BASE_NET_TEST_HELPERS_H_
|
||||||
|
#define RTC_BASE_NET_TEST_HELPERS_H_
|
||||||
|
|
||||||
|
#include "rtc_base/system/rtc_export.h"
|
||||||
|
|
||||||
|
namespace rtc {
|
||||||
|
|
||||||
|
RTC_EXPORT bool HasIPv4Enabled();
|
||||||
|
RTC_EXPORT bool HasIPv6Enabled();
|
||||||
|
|
||||||
|
} // namespace rtc
|
||||||
|
|
||||||
|
#endif // RTC_BASE_NET_TEST_HELPERS_H_
|
|
@ -19,6 +19,7 @@
|
||||||
#include "rtc_base/ip_address.h"
|
#include "rtc_base/ip_address.h"
|
||||||
#include "rtc_base/logging.h"
|
#include "rtc_base/logging.h"
|
||||||
#include "rtc_base/net_helpers.h"
|
#include "rtc_base/net_helpers.h"
|
||||||
|
#include "rtc_base/net_test_helpers.h"
|
||||||
#include "rtc_base/network_monitor.h"
|
#include "rtc_base/network_monitor.h"
|
||||||
#include "rtc_base/socket_unittest.h"
|
#include "rtc_base/socket_unittest.h"
|
||||||
#include "rtc_base/test_utils.h"
|
#include "rtc_base/test_utils.h"
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
#include "rtc_base/gunit.h"
|
#include "rtc_base/gunit.h"
|
||||||
#include "rtc_base/logging.h"
|
#include "rtc_base/logging.h"
|
||||||
#include "rtc_base/net_helpers.h"
|
#include "rtc_base/net_helpers.h"
|
||||||
|
#include "rtc_base/net_test_helpers.h"
|
||||||
#include "rtc_base/socket_address.h"
|
#include "rtc_base/socket_address.h"
|
||||||
#include "rtc_base/socket_server.h"
|
#include "rtc_base/socket_server.h"
|
||||||
#include "rtc_base/socket_unittest.h"
|
#include "rtc_base/socket_unittest.h"
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
#include "rtc_base/async_udp_socket.h"
|
#include "rtc_base/async_udp_socket.h"
|
||||||
#include "rtc_base/logging.h"
|
#include "rtc_base/logging.h"
|
||||||
#include "rtc_base/net_helpers.h"
|
#include "rtc_base/net_helpers.h"
|
||||||
|
#include "rtc_base/net_test_helpers.h"
|
||||||
#include "rtc_base/physical_socket_server.h"
|
#include "rtc_base/physical_socket_server.h"
|
||||||
#include "rtc_base/socket.h"
|
#include "rtc_base/socket.h"
|
||||||
#include "rtc_base/test_echo_server.h"
|
#include "rtc_base/test_echo_server.h"
|
||||||
|
|
Loading…
Reference in a new issue