Skip to content

Commit

Permalink
Added workaround for networks with partial DNS
Browse files Browse the repository at this point in the history
- If the host name is not good enough, we will prefer chosen IPv4 address.
- Would be better to let it on the user which hostname or IP should represent
  the node and DAS, however that goes over this pull request.

Signed-off-by: David Matějček <[email protected]>
  • Loading branch information
dmatej committed Jan 17, 2025
1 parent 9750eef commit bb40fdd
Showing 1 changed file with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation.
* Copyright (c) 2024, 2025 Contributors to the Eclipse Foundation.
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -31,6 +31,7 @@
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.function.Predicate;

public final class NetUtils {

Expand Down Expand Up @@ -475,6 +476,24 @@ public static String getCanonicalHostName() throws UnknownHostException {
return defaultHostname;
}

// If the DNS is inconsistent between domain hosts, it is sometimes better to use
// any reachable public IP address. This check is not perfect - however usually if
// the host name doesn't contain any dots, it is already a bad name for wider networks.
if (!hostname.contains(".")) {
ThrowingPredicate<NetworkInterface> isLoopback = NetworkInterface::isLoopback;
try {
String host = NetworkInterface.networkInterfaces().filter(Predicate.not(isLoopback))
.flatMap(NetworkInterface::inetAddresses)
.map(InetAddress::getHostAddress)
.filter(name -> name.indexOf('.') > 0)
.findFirst().orElse(hostname);
return host;
} catch (SocketException e) {
e.printStackTrace();
}

}

return hostname;
}

Expand All @@ -495,4 +514,22 @@ private static String trimIP(String ip) {
public enum PortAvailability {
illegalNumber, noPermission, inUse, unknown, OK
}


@FunctionalInterface
private static interface ThrowingPredicate<T> extends Predicate<T> {

boolean throwing(T object) throws Exception;

@Override
public default boolean test(T object) {
try {
return throwing(object);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
}
}

0 comments on commit bb40fdd

Please sign in to comment.