From f27fb07c842639f62ee2a8540783b8d2083d6951 Mon Sep 17 00:00:00 2001 From: gRPC Team Bot Date: Fri, 9 Feb 2024 07:45:43 -0800 Subject: [PATCH] Do not expand unusable v4 addresses. PiperOrigin-RevId: 605627652 --- src/core/lib/iomgr/tcp_server_utils_posix_ifaddrs.cc | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/core/lib/iomgr/tcp_server_utils_posix_ifaddrs.cc b/src/core/lib/iomgr/tcp_server_utils_posix_ifaddrs.cc index a04f0a36a08..39f233a1079 100644 --- a/src/core/lib/iomgr/tcp_server_utils_posix_ifaddrs.cc +++ b/src/core/lib/iomgr/tcp_server_utils_posix_ifaddrs.cc @@ -89,6 +89,12 @@ static grpc_error_handle get_unused_port(int* port) { return *port <= 0 ? GRPC_ERROR_CREATE("Bad port") : absl::OkStatus(); } +static bool grpc_is_ipv4_availabile() { + const int fd = socket(AF_INET, SOCK_DGRAM, 0); + if (fd >= 0) close(fd); + return fd >= 0; +} + grpc_error_handle grpc_tcp_server_add_all_local_addrs(grpc_tcp_server* s, unsigned port_index, int requested_port, @@ -110,6 +116,9 @@ grpc_error_handle grpc_tcp_server_add_all_local_addrs(grpc_tcp_server* s, } gpr_log(GPR_DEBUG, "Picked unused port %d", requested_port); } + + static bool v4_available = grpc_is_ipv4_availabile(); + if (getifaddrs(&ifa) != 0 || ifa == nullptr) { return GRPC_OS_ERROR(errno, "getifaddrs"); } @@ -121,6 +130,9 @@ grpc_error_handle grpc_tcp_server_add_all_local_addrs(grpc_tcp_server* s, if (ifa_it->ifa_addr == nullptr) { continue; } else if (ifa_it->ifa_addr->sa_family == AF_INET) { + if (!v4_available) { + continue; + } addr.len = static_cast(sizeof(grpc_sockaddr_in)); } else if (ifa_it->ifa_addr->sa_family == AF_INET6) { addr.len = static_cast(sizeof(grpc_sockaddr_in6));