diff --git a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java index 9cdf15409d..f3b88576f1 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java @@ -2463,11 +2463,12 @@ public class BesuCommand implements DefaultCommandValues, Runnable { .forEach( port -> { if (port.equals(p2PDiscoveryOptionGroup.p2pPort) - && !NetworkUtility.isPortAvailable(port)) { + && (NetworkUtility.isPortUnavailableForTcp(port) + || NetworkUtility.isPortUnavailableForUdp(port))) { unavailablePorts.add(port); } if (!port.equals(p2PDiscoveryOptionGroup.p2pPort) - && !NetworkUtility.isPortAvailableForTcp(port)) { + && NetworkUtility.isPortUnavailableForTcp(port)) { unavailablePorts.add(port); } }); diff --git a/util/src/main/java/org/hyperledger/besu/util/NetworkUtility.java b/util/src/main/java/org/hyperledger/besu/util/NetworkUtility.java index 352ee25eb4..8a3e3252c7 100644 --- a/util/src/main/java/org/hyperledger/besu/util/NetworkUtility.java +++ b/util/src/main/java/org/hyperledger/besu/util/NetworkUtility.java @@ -160,41 +160,39 @@ public class NetworkUtility { } /** - * Is port available for tcp. + * Is port unavailable for tcp. * * @param port the port - * @return the boolean + * @return true if the port is unavailable for TCP */ - public static boolean isPortAvailableForTcp(final int port) { + public static boolean isPortUnavailableForTcp(final int port) { try (final ServerSocket serverSocket = new ServerSocket()) { serverSocket.setReuseAddress(true); serverSocket.bind(new InetSocketAddress(port)); - return true; + serverSocket.close(); + return false; } catch (IOException ex) { LOG.trace(String.format("Failed to open port %d for TCP", port), ex); } - return false; + return true; } - private static boolean isPortAvailableForUdp(final int port) { + /** + * Is port unavailable for udp. + * + * @param port the port + * @return true if the port is unavailable for UDP + */ + public static boolean isPortUnavailableForUdp(final int port) { try (final DatagramSocket datagramSocket = new DatagramSocket(null)) { datagramSocket.setReuseAddress(true); datagramSocket.bind(new InetSocketAddress(port)); - return true; + datagramSocket.close(); + return false; } catch (IOException ex) { LOG.trace(String.format("failed to open port %d for UDP", port), ex); } - return false; - } - - /** - * Is port available. - * - * @param port the port - * @return the boolean - */ - public static boolean isPortAvailable(final int port) { - return isPortAvailableForTcp(port) && isPortAvailableForUdp(port); + return true; } /** diff --git a/util/src/test/java/org/hyperledger/besu/util/NetworkUtilityTest.java b/util/src/test/java/org/hyperledger/besu/util/NetworkUtilityTest.java index 0f60eb6769..627212d1f3 100644 --- a/util/src/test/java/org/hyperledger/besu/util/NetworkUtilityTest.java +++ b/util/src/test/java/org/hyperledger/besu/util/NetworkUtilityTest.java @@ -17,6 +17,7 @@ package org.hyperledger.besu.util; import static org.assertj.core.api.Assertions.assertThat; import java.io.IOException; +import java.net.DatagramSocket; import java.net.InetSocketAddress; import java.net.ServerSocket; @@ -35,12 +36,19 @@ public class NetworkUtilityTest { } @Test - public void assertPortIsNotAvailable() throws IOException { + public void assertPortIsNotAvailableForTcp() throws IOException { final ServerSocket serverSocket = new ServerSocket(8541); - assertThat(!NetworkUtility.isPortAvailable(8541)).isEqualTo(true); + assertThat(NetworkUtility.isPortUnavailableForTcp(8541)).isEqualTo(true); serverSocket.close(); } + @Test + public void assertPortIsNotAvailableForUdp() throws IOException { + final DatagramSocket datagramSocket = new DatagramSocket(8541); + assertThat(NetworkUtility.isPortUnavailableForUdp(8541)).isEqualTo(true); + datagramSocket.close(); + } + @Test public void assertLocalhostIdentification() { assertThat(NetworkUtility.isLocalhostAddress("127.0.0.1")).isTrue();