Close Sockets after testing for availablity (#6516)

Signed-off-by: stefan.pingel@consensys.net <stefan.pingel@consensys.net>
pull/6521/head
Stefan Pingel 10 months ago committed by GitHub
parent a35b05fe1b
commit 630ac85541
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 5
      besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
  2. 34
      util/src/main/java/org/hyperledger/besu/util/NetworkUtility.java
  3. 12
      util/src/test/java/org/hyperledger/besu/util/NetworkUtilityTest.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);
}
});

@ -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;
}
/**

@ -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();

Loading…
Cancel
Save