diff --git a/src/core/lib/event_engine/posix_engine/posix_engine_listener_utils.cc b/src/core/lib/event_engine/posix_engine/posix_engine_listener_utils.cc index ed2321b2971..6f4bfe104e9 100644 --- a/src/core/lib/event_engine/posix_engine/posix_engine_listener_utils.cc +++ b/src/core/lib/event_engine/posix_engine/posix_engine_listener_utils.cc @@ -249,6 +249,13 @@ absl::StatusOr ListenerContainerAddAllLocalAddresses( return absl::FailedPreconditionError( absl::StrCat("getifaddrs: ", std::strerror(errno))); } + + static const bool is_ipv4_available = [] { + const int fd = socket(AF_INET, SOCK_DGRAM, 0); + if (fd >= 0) close(fd); + return fd >= 0; + }(); + for (ifa_it = ifa; ifa_it != nullptr; ifa_it = ifa_it->ifa_next) { ResolvedAddress addr; socklen_t len; @@ -256,6 +263,9 @@ absl::StatusOr ListenerContainerAddAllLocalAddresses( if (ifa_it->ifa_addr == nullptr) { continue; } else if (ifa_it->ifa_addr->sa_family == AF_INET) { + if (!is_ipv4_available) { + continue; + } len = static_cast(sizeof(sockaddr_in)); } else if (ifa_it->ifa_addr->sa_family == AF_INET6) { len = static_cast(sizeof(sockaddr_in6));