Do not expand unusable v4 addresses.

PiperOrigin-RevId: 605627652
pull/35872/head
gRPC Team Bot 12 months ago committed by Copybara-Service
parent 8f2245d0a7
commit f27fb07c84
  1. 12
      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<socklen_t>(sizeof(grpc_sockaddr_in));
} else if (ifa_it->ifa_addr->sa_family == AF_INET6) {
addr.len = static_cast<socklen_t>(sizeof(grpc_sockaddr_in6));

Loading…
Cancel
Save