Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.Set;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.jspecify.annotations.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -54,14 +55,27 @@ record AddressResolver(DnsClient dnsClient, List<String> searchList) {

static {
try {
IP_V4_ONLY = Boolean.parseBoolean(System.getProperty("java.net.preferIPv4Stack", "false"));
LOCAL_ADDRESSES =
networkInterfaces()
.flatMap(
ni ->
ni.getInterfaceAddresses().stream()
// Need to do this InetAddress->byte[]->InetAddress dance to get rid of
// host-address suffixes as in `0:0:0:0:0:0:0:1%lo`
.map(InterfaceAddress::getAddress)
Stream.concat(
// localhost can be ipv6 when sysctl disable ipv6
// if ::1 is registered for localhost in /etc/hosts
// in this case java stack can still capture it
// and it will work (even if not great)
// this is a workaround to ensure at least localhost is
// in the list but this could be more general to all /etc/hosts
// mappings
IP_V4_ONLY || !"lo".equalsIgnoreCase(ni.getName())
? Stream.empty()
: findLocalhostAddresses(),
ni.getInterfaceAddresses().stream()
// Need to do this InetAddress->byte[]->InetAddress dance to get
// rid of
// host-address suffixes as in `0:0:0:0:0:0:0:1%lo`
.map(InterfaceAddress::getAddress))
.map(InetAddress::getAddress)
.map(
a -> {
Expand All @@ -75,13 +89,19 @@ record AddressResolver(DnsClient dnsClient, List<String> searchList) {
})
.map(InetAddress::getHostAddress))
.collect(toUnmodifiableSet());

IP_V4_ONLY = Boolean.parseBoolean(System.getProperty("java.net.preferIPv4Stack", "false"));
} catch (SocketException e) {
throw new RuntimeException(e);
}
}

private static @NonNull Stream<InetAddress> findLocalhostAddresses() {
try {
return Stream.of(InetAddress.getAllByName("localhost"));
} catch (final RuntimeException | UnknownHostException e) {
return Stream.empty();
}
}

/**
* Uses a "default" {@link DnsClient} using the first {@code nameserver} and the {@code search}
* list configured in {@code /etc/resolv.conf}.
Expand Down