-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add port conflict exception #4565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
c504b13
3ec630e
de1afc7
4a69660
feeaf2a
69961ba
b91487f
b749733
8752282
817286e
d1cb28c
7ba61a7
489ce6f
49555f5
896ebf2
9229afc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -180,6 +180,7 @@ | |
| import org.hyperledger.besu.services.SecurityModuleServiceImpl; | ||
| import org.hyperledger.besu.services.StorageServiceImpl; | ||
| import org.hyperledger.besu.services.kvstore.InMemoryStoragePlugin; | ||
| import org.hyperledger.besu.util.InvalidConfigurationException; | ||
| import org.hyperledger.besu.util.Log4j2ConfiguratorUtil; | ||
| import org.hyperledger.besu.util.NetworkUtility; | ||
| import org.hyperledger.besu.util.PermissioningConfigurationValidator; | ||
|
|
@@ -1972,7 +1973,7 @@ private void issueOptionWarnings() { | |
|
|
||
| private void configure() throws Exception { | ||
| checkPortClash(); | ||
|
|
||
| checkIfRequiredPortsAreAvailable(); | ||
| syncMode = getDefaultSyncModeIfNotSet(syncMode); | ||
|
|
||
| ethNetworkConfig = updateNetworkConfig(network); | ||
|
|
@@ -3126,6 +3127,25 @@ private void checkPortClash() { | |
| }); | ||
| } | ||
|
|
||
| private void checkIfRequiredPortsAreAvailable() { | ||
| List<Integer> unavailablePorts = new ArrayList<>(); | ||
| getEffectivePorts().stream() | ||
| .filter(Objects::nonNull) | ||
| .filter(port -> port > 0) | ||
| .forEach( | ||
| port -> { | ||
| if (!NetworkUtility.isPortAvailable(port)) { | ||
| unavailablePorts.add(port); | ||
| } | ||
| }); | ||
| if (!unavailablePorts.isEmpty()) { | ||
| throw new InvalidConfigurationException( | ||
| "Port(s) '" | ||
| + unavailablePorts.toString() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: toString on unavailablePorts isn't necessary |
||
| + "' already in use. Check for other processes using the port(s)."); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * * Gets the list of effective ports (ports that are enabled). | ||
| * | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -219,6 +219,7 @@ public CompletableFuture<?> start() { | |
|
|
||
| final CompletableFuture<?> resultFuture = new CompletableFuture<>(); | ||
| try { | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any reason to add the extra empty line? |
||
| // Create the HTTP server and a router object. | ||
| httpServer = vertx.createHttpServer(getHttpServerOptions()); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,20 +14,27 @@ | |
| */ | ||
| package org.hyperledger.besu.util; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.DatagramSocket; | ||
| import java.net.Inet6Address; | ||
| import java.net.InetAddress; | ||
| import java.net.InetSocketAddress; | ||
| import java.net.NetworkInterface; | ||
| import java.net.ServerSocket; | ||
| import java.net.SocketException; | ||
| import java.net.UnknownHostException; | ||
| import java.util.function.Supplier; | ||
|
|
||
| import com.google.common.base.Suppliers; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class NetworkUtility { | ||
| public static final String INADDR_ANY = "0.0.0.0"; | ||
| public static final String INADDR6_ANY = "0:0:0:0:0:0:0:0"; | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(NetworkUtility.class); | ||
|
|
||
| private NetworkUtility() {} | ||
|
|
||
| private static final Supplier<Boolean> ipv6Available = | ||
|
|
@@ -98,4 +105,31 @@ public static void checkPort(final int port, final String portTypeName) { | |
| "%s port requires a value between 1 and 65535. Got %d.", portTypeName, port)); | ||
| } | ||
| } | ||
|
|
||
| public static boolean isPortAvailableForTcp(final int port) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could be private? |
||
| try (final ServerSocket serverSocket = new ServerSocket()) { | ||
| serverSocket.setReuseAddress(true); | ||
| serverSocket.bind(new InetSocketAddress(port)); | ||
| return true; | ||
| } catch (IOException ex) { | ||
| LOG.trace(String.format("Failed to open port %d for TCP", port), ex); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| public static boolean isPortAvailableForUdp(final int port) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could be private? |
||
| 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; | ||
| } | ||
|
|
||
| public static boolean isPortAvailable(final int port) { | ||
|
jframe marked this conversation as resolved.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: consider renaming to make to clear this is checking both udp and tcp. maybe rename to |
||
| if (isPortAvailableForTcp(port) && isPortAvailableForUdp(port)) return true; | ||
|
gfukushima marked this conversation as resolved.
Outdated
jframe marked this conversation as resolved.
Outdated
|
||
| return false; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: final