-
Notifications
You must be signed in to change notification settings - Fork 883
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
Add port conflict exception #4565
Merged
jframe
merged 16 commits into
hyperledger:main
from
gfukushima:add_port_conflict_exception
Nov 2, 2022
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
c504b13
Add method to check port availability
gfukushima 3ec630e
Check port availability
gfukushima de1afc7
Add tests to check port availability
gfukushima 4a69660
Merge branch 'hyperledger:main' into add_port_conflict_exception
gfukushima feeaf2a
Fix broken test
gfukushima 69961ba
Merge remote-tracking branch 'origin/add_port_conflict_exception' int…
gfukushima b91487f
Spotless
gfukushima b749733
Rename method
gfukushima 8752282
Rollback changes
gfukushima 817286e
Add general port verification
gfukushima d1cb28c
Fix test
gfukushima 7ba61a7
Add unit test
gfukushima 489ce6f
Refactor method
gfukushima 49555f5
Add more specific validation for ports/services
gfukushima 896ebf2
Merge branch 'main' into add_port_conflict_exception
gfukushima 9229afc
Merge branch 'main' into add_port_conflict_exception
jframe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,30 @@ 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) { | ||
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; | ||
} | ||
|
||
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; | ||
} | ||
|
||
public static boolean isPortAvailable(final int port) { | ||
jframe marked this conversation as resolved.
Show resolved
Hide resolved
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 |
||
return isPortAvailableForTcp(port) && isPortAvailableForUdp(port); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
any reason to add the extra empty line?