You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by lg...@apache.org on 2015/08/20 08:17:48 UTC

mina-sshd git commit: [SSHD-554] Cannot set SO_KEEPALIVE on SshServer

Repository: mina-sshd
Updated Branches:
  refs/heads/master f24cad74c -> 5245f431b


[SSHD-554] Cannot set SO_KEEPALIVE on SshServer


Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/5245f431
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/5245f431
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/5245f431

Branch: refs/heads/master
Commit: 5245f431babe568e1547be8a61be9ecd2e8beb88
Parents: f24cad7
Author: Lyor Goldstein <lg...@vmware.com>
Authored: Thu Aug 20 09:17:36 2015 +0300
Committer: Lyor Goldstein <lg...@vmware.com>
Committed: Thu Aug 20 09:17:36 2015 +0300

----------------------------------------------------------------------
 .../apache/sshd/common/FactoryManagerUtils.java |  3 +-
 .../apache/sshd/common/io/nio2/Nio2Service.java | 18 ++++-
 .../sshd/common/io/nio2/Nio2ServiceTest.java    | 72 ++++++++++++++++++++
 3 files changed, 90 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/5245f431/sshd-core/src/main/java/org/apache/sshd/common/FactoryManagerUtils.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/FactoryManagerUtils.java b/sshd-core/src/main/java/org/apache/sshd/common/FactoryManagerUtils.java
index d02f4aa..fe0b6fb 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/FactoryManagerUtils.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/FactoryManagerUtils.java
@@ -20,6 +20,7 @@
 package org.apache.sshd.common;
 
 import java.util.Map;
+import java.util.Objects;
 
 import org.apache.sshd.common.session.Session;
 import org.apache.sshd.common.util.GenericUtils;
@@ -246,7 +247,7 @@ public final class FactoryManagerUtils {
 
     public static String getStringProperty(Map<String, ?> props, String name, String defaultValue) {
         Object value = GenericUtils.isEmpty(props) ? null : props.get(name);
-        String s = (value == null) ? null : value.toString();
+        String s = Objects.toString(value);
         if (GenericUtils.isEmpty(s)) {
             return defaultValue;
         } else {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/5245f431/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Service.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Service.java b/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Service.java
index d633369..8b4829b 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Service.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Service.java
@@ -22,6 +22,7 @@ import java.io.IOException;
 import java.net.SocketOption;
 import java.nio.channels.AsynchronousChannelGroup;
 import java.nio.channels.NetworkChannel;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
@@ -89,11 +90,24 @@ public abstract class Nio2Service extends CloseableUtils.AbstractInnerCloseable
                 throw new IllegalStateException("Unsupported socket option type " + type);
             }
         }
+
         if (val != null) {
+            Collection<? extends SocketOption<?>> supported = socket.supportedOptions();
+            if ((GenericUtils.size(supported) <= 0) || (!supported.contains(option))) {
+                log.warn("Unsupported socket option (" + option + ") to set using property '" + property + "' value=" + val);
+                return;
+            }
+
             try {
                 socket.setOption(option, val);
-            } catch (IOException e) {
-                log.warn("Unable to set socket option " + option + " to " + val, e);
+                if (log.isDebugEnabled()) {
+                    log.debug("setOption({})[{}] from property={}", option, val, property);
+                }
+            } catch (IOException | RuntimeException e) {
+                log.warn("Unable (" + e.getClass().getSimpleName() + ")"
+                       + " to set socket option " + option
+                       + " using property '" + property + "' value=" + val
+                       + ": " + e.getMessage());
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/5245f431/sshd-core/src/test/java/org/apache/sshd/common/io/nio2/Nio2ServiceTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/io/nio2/Nio2ServiceTest.java b/sshd-core/src/test/java/org/apache/sshd/common/io/nio2/Nio2ServiceTest.java
new file mode 100644
index 0000000..f55a262
--- /dev/null
+++ b/sshd-core/src/test/java/org/apache/sshd/common/io/nio2/Nio2ServiceTest.java
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.sshd.common.io.nio2;
+
+import java.net.Socket;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.sshd.common.FactoryManager;
+import org.apache.sshd.common.FactoryManagerUtils;
+import org.apache.sshd.server.SshServer;
+import org.apache.sshd.server.ServerTest.TestEchoShellFactory;
+import org.apache.sshd.util.BaseTestSupport;
+import org.apache.sshd.util.BogusPasswordAuthenticator;
+import org.apache.sshd.util.Utils;
+import org.junit.FixMethodOrder;
+import org.junit.Test;
+import org.junit.runners.MethodSorters;
+
+/**
+ * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
+ */
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+public class Nio2ServiceTest extends BaseTestSupport {
+    public Nio2ServiceTest() {
+        super();
+    }
+
+    @Test   // see SSHD-554
+    public void testSetSocketOptions() throws Exception {
+        try(SshServer sshd = SshServer.setUpDefaultServer()) {
+            FactoryManagerUtils.updateProperty(sshd, FactoryManager.SOCKET_KEEPALIVE, true);
+            FactoryManagerUtils.updateProperty(sshd, FactoryManager.SOCKET_LINGER, 5);
+            FactoryManagerUtils.updateProperty(sshd, FactoryManager.SOCKET_RCVBUF, 1024);
+            FactoryManagerUtils.updateProperty(sshd, FactoryManager.SOCKET_REUSEADDR, true);
+            FactoryManagerUtils.updateProperty(sshd, FactoryManager.SOCKET_SNDBUF, 1024);
+            FactoryManagerUtils.updateProperty(sshd, FactoryManager.TCP_NODELAY, true);
+
+            sshd.setKeyPairProvider(Utils.createTestHostKeyProvider());
+            sshd.setShellFactory(new TestEchoShellFactory());
+            sshd.setPasswordAuthenticator(BogusPasswordAuthenticator.INSTANCE);
+            sshd.setSessionFactory(new org.apache.sshd.server.session.SessionFactory());
+            sshd.start();
+
+            int port = sshd.getPort();
+            long startTime = System.nanoTime();
+            try(Socket s = new Socket("localhost", port)) {
+                long endTime = System.nanoTime();
+                long duration = endTime - startTime;
+                assertTrue("Connect duration is too high: " + duration, duration <= TimeUnit.SECONDS.toNanos(15L));
+            } finally {
+                sshd.stop();
+            }
+        }
+    }
+}