Close Sockets after testing for availablity (#6516)

Signed-off-by: stefan.pingel@consensys.net <stefan.pingel@consensys.net>
This commit is contained in:
Stefan Pingel
2024-02-02 17:45:57 +10:00
committed by GitHub
parent a35b05fe1b
commit 630ac85541
3 changed files with 31 additions and 24 deletions

View File

@@ -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;
}
private static boolean isPortAvailableForUdp(final int port) {
try (final DatagramSocket datagramSocket = new DatagramSocket(null)) {
datagramSocket.setReuseAddress(true);
datagramSocket.bind(new InetSocketAddress(port));
return true;
} catch (IOException ex) {
LOG.trace(String.format("failed to open port %d for UDP", port), ex);
}
return false;
return true;
}
/**
* Is port available.
* Is port unavailable for udp.
*
* @param port the port
* @return the boolean
* @return true if the port is unavailable for UDP
*/
public static boolean isPortAvailable(final int port) {
return isPortAvailableForTcp(port) && isPortAvailableForUdp(port);
public static boolean isPortUnavailableForUdp(final int port) {
try (final DatagramSocket datagramSocket = new DatagramSocket(null)) {
datagramSocket.setReuseAddress(true);
datagramSocket.bind(new InetSocketAddress(port));
datagramSocket.close();
return false;
} catch (IOException ex) {
LOG.trace(String.format("failed to open port %d for UDP", port), ex);
}
return true;
}
/**

View File

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