You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by gn...@apache.org on 2014/07/08 19:04:33 UTC
git commit: [SSHD-333] Ephemeral port handling is faulty
Repository: mina-sshd
Updated Branches:
refs/heads/master 97b38fb08 -> de0c923a4
[SSHD-333] Ephemeral port handling is faulty
Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/de0c923a
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/de0c923a
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/de0c923a
Branch: refs/heads/master
Commit: de0c923a4f87f93971e50bbbd4fe01d037de3936
Parents: 97b38fb
Author: Guillaume Nodet <gn...@apache.org>
Authored: Tue Jul 8 18:56:05 2014 +0200
Committer: Guillaume Nodet <gn...@apache.org>
Committed: Tue Jul 8 18:56:05 2014 +0200
----------------------------------------------------------------------
sshd-core/src/main/java/org/apache/sshd/SshServer.java | 10 ++++------
.../main/java/org/apache/sshd/common/util/IoUtils.java | 10 ++++++++++
.../src/test/java/org/apache/sshd/SshServerTest.java | 12 ++++++++++++
3 files changed, 26 insertions(+), 6 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/de0c923a/sshd-core/src/main/java/org/apache/sshd/SshServer.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/SshServer.java b/sshd-core/src/main/java/org/apache/sshd/SshServer.java
index 7d1b439..7074edc 100644
--- a/sshd-core/src/main/java/org/apache/sshd/SshServer.java
+++ b/sshd-core/src/main/java/org/apache/sshd/SshServer.java
@@ -84,6 +84,7 @@ import org.apache.sshd.common.signature.SignatureDSA;
import org.apache.sshd.common.signature.SignatureECDSA;
import org.apache.sshd.common.signature.SignatureRSA;
import org.apache.sshd.common.util.CloseableUtils;
+import org.apache.sshd.common.util.IoUtils;
import org.apache.sshd.common.util.OsUtils;
import org.apache.sshd.common.util.SecurityUtils;
import org.apache.sshd.common.util.ThreadUtils;
@@ -334,6 +335,9 @@ public class SshServer extends AbstractFactoryManager implements ServerFactoryMa
timeoutListenerFuture = getScheduledExecutorService()
.scheduleAtFixedRate(sessionTimeoutListener, 1, 1, TimeUnit.SECONDS);
+ if (port == 0) {
+ port = IoUtils.getFreePort();
+ }
if (host != null) {
String[] hosts = host.split(",");
LinkedList<InetSocketAddress> addresses = new LinkedList<InetSocketAddress>();
@@ -341,18 +345,12 @@ public class SshServer extends AbstractFactoryManager implements ServerFactoryMa
InetAddress[] inetAddresses = InetAddress.getAllByName(host);
for (InetAddress inetAddress : inetAddresses) {
InetSocketAddress inetSocketAddress = new InetSocketAddress(inetAddress, port);
- if (port == 0) {
- port = inetSocketAddress.getPort();
- }
addresses.add(inetSocketAddress);
}
}
acceptor.bind(addresses);
} else {
acceptor.bind(Collections.singleton(new InetSocketAddress(port)));
- if (port == 0) {
- port = ((InetSocketAddress) acceptor.getBoundAddresses().iterator().next()).getPort();
- }
}
}
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/de0c923a/sshd-core/src/main/java/org/apache/sshd/common/util/IoUtils.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/IoUtils.java b/sshd-core/src/main/java/org/apache/sshd/common/util/IoUtils.java
index da53e91..77f1de2 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/util/IoUtils.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/util/IoUtils.java
@@ -20,6 +20,7 @@ package org.apache.sshd.common.util;
import java.io.Closeable;
import java.io.IOException;
+import java.net.ServerSocket;
/**
* TODO Add javadoc
@@ -28,6 +29,15 @@ import java.io.IOException;
*/
public class IoUtils {
+ public static int getFreePort() throws IOException {
+ ServerSocket s = new ServerSocket(0);
+ try {
+ return s.getLocalPort();
+ } finally {
+ s.close();
+ }
+ }
+
public static void closeQuietly(Closeable... closeables) {
for (Closeable c : closeables) {
try {
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/de0c923a/sshd-core/src/test/java/org/apache/sshd/SshServerTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/SshServerTest.java b/sshd-core/src/test/java/org/apache/sshd/SshServerTest.java
index 247d9dc..2faf54f 100644
--- a/sshd-core/src/test/java/org/apache/sshd/SshServerTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/SshServerTest.java
@@ -28,6 +28,7 @@ import org.apache.sshd.util.Utils;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
/**
@@ -70,6 +71,17 @@ public class SshServerTest extends BaseTest {
assertTrue(executorService.isShutdown());
}
+ @Test
+ public void testDynamicPort() throws Exception {
+ SshServer sshd = createTestServer();
+ sshd.setHost("localhost");
+ sshd.start();
+
+ assertNotEquals(0, sshd.getPort());
+
+ sshd.stop();
+ }
+
private SshServer createTestServer() {
SshServer sshd = SshServer.setUpDefaultServer();