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 2018/02/07 07:58:33 UTC

[5/5] mina-sshd git commit: [SSHD-801] Expose channel type information via public methods

[SSHD-801] Expose channel type information via public methods


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

Branch: refs/heads/master
Commit: 17331a8fc5efe91b2918ad97beded8dcbd7a116c
Parents: ad35d53
Author: Goldstein Lyor <ly...@c-b4.com>
Authored: Sun Feb 4 09:37:54 2018 +0200
Committer: Goldstein Lyor <ly...@c-b4.com>
Committed: Wed Feb 7 09:41:25 2018 +0200

----------------------------------------------------------------------
 .../client/channel/AbstractClientChannel.java   | 11 ++++-
 .../sshd/client/channel/ChannelDirectTcpip.java |  3 +-
 .../sshd/client/channel/ClientChannel.java      |  5 +++
 .../common/forward/DefaultForwardingFilter.java |  2 +-
 .../sshd/common/forward/TcpipClientChannel.java | 47 +++++++++++++++-----
 .../sshd/server/forward/TcpipServerChannel.java | 27 ++++++-----
 .../sshd/server/x11/ChannelForwardedX11.java    |  3 +-
 7 files changed, 71 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17331a8f/sshd-core/src/main/java/org/apache/sshd/client/channel/AbstractClientChannel.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/channel/AbstractClientChannel.java b/sshd-core/src/main/java/org/apache/sshd/client/channel/AbstractClientChannel.java
index 437a87a..59a3d73 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/channel/AbstractClientChannel.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/channel/AbstractClientChannel.java
@@ -62,7 +62,6 @@ import org.apache.sshd.common.util.io.IoUtils;
 public abstract class AbstractClientChannel extends AbstractChannel implements ClientChannel {
 
     protected final AtomicBoolean opened = new AtomicBoolean();
-    protected final String type;
 
     protected Streaming streaming;
 
@@ -83,13 +82,15 @@ public abstract class AbstractClientChannel extends AbstractChannel implements C
     protected String openFailureLang;
     protected OpenFuture openFuture;
 
+    private final String channelType;
+
     protected AbstractClientChannel(String type) {
         this(type, Collections.emptyList());
     }
 
     protected AbstractClientChannel(String type, Collection<? extends RequestHandler<Channel>> handlers) {
         super(true, handlers);
-        this.type = ValidateUtils.checkNotNullAndNotEmpty(type, "No channel type specified");
+        this.channelType = ValidateUtils.checkNotNullAndNotEmpty(type, "No channel type specified");
         this.streaming = Streaming.Sync;
 
         addChannelSignalRequestHandlers(event -> {
@@ -106,6 +107,11 @@ public abstract class AbstractClientChannel extends AbstractChannel implements C
     }
 
     @Override
+    public String getChannelType() {
+        return channelType;
+    }
+
+    @Override
     public Streaming getStreaming() {
         return streaming;
     }
@@ -299,6 +305,7 @@ public abstract class AbstractClientChannel extends AbstractChannel implements C
         }
 
         openFuture = new DefaultOpenFuture(this.toString(), lock);
+        String type = getChannelType();
         if (log.isDebugEnabled()) {
             log.debug("open({}) Send SSH_MSG_CHANNEL_OPEN - type={}", this, type);
         }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17331a8f/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelDirectTcpip.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelDirectTcpip.java b/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelDirectTcpip.java
index 4ecfbf4..b37cef0 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelDirectTcpip.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelDirectTcpip.java
@@ -79,8 +79,9 @@ public class ChannelDirectTcpip extends AbstractClientChannel {
         String remoteName = remote.getHostName();
         String localName = local.getHostName();
         Window wLocal = getLocalWindow();
+        String type = getChannelType();
         Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN,
-                            type.length() + remoteName.length() + localName.length() + Long.SIZE);
+            type.length() + remoteName.length() + localName.length() + Long.SIZE);
         buffer.putString(type);
         buffer.putInt(getId());
         buffer.putInt(wLocal.getSize());

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17331a8f/sshd-core/src/main/java/org/apache/sshd/client/channel/ClientChannel.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/channel/ClientChannel.java b/sshd-core/src/main/java/org/apache/sshd/client/channel/ClientChannel.java
index 6da4a4f..286baeb 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/channel/ClientChannel.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/channel/ClientChannel.java
@@ -45,6 +45,11 @@ public interface ClientChannel extends Channel {
         Sync
     }
 
+    /**
+     * @return The type of channel reported when it was created
+     */
+    String getChannelType();
+
     Streaming getStreaming();
 
     void setStreaming(Streaming streaming);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17331a8f/sshd-core/src/main/java/org/apache/sshd/common/forward/DefaultForwardingFilter.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/forward/DefaultForwardingFilter.java b/sshd-core/src/main/java/org/apache/sshd/common/forward/DefaultForwardingFilter.java
index 1017f64..a6f46fa 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/forward/DefaultForwardingFilter.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/forward/DefaultForwardingFilter.java
@@ -934,7 +934,7 @@ public class DefaultForwardingFilter
                 log.debug("sessionCreated({}) remote={}", session, remote);
             }
 
-            final TcpipClientChannel channel;
+            TcpipClientChannel channel;
             if (remote != null) {
                 channel = new TcpipClientChannel(TcpipClientChannel.Type.Direct, session, remote);
             } else {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17331a8f/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipClientChannel.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipClientChannel.java b/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipClientChannel.java
index f55dca1..9c19a9d 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipClientChannel.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipClientChannel.java
@@ -21,11 +21,16 @@ package org.apache.sshd.common.forward;
 import java.io.IOException;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
+import java.util.Collections;
+import java.util.EnumSet;
+import java.util.Objects;
+import java.util.Set;
 
 import org.apache.sshd.client.channel.AbstractClientChannel;
 import org.apache.sshd.client.future.DefaultOpenFuture;
 import org.apache.sshd.client.future.OpenFuture;
 import org.apache.sshd.common.Closeable;
+import org.apache.sshd.common.NamedResource;
 import org.apache.sshd.common.SshConstants;
 import org.apache.sshd.common.SshException;
 import org.apache.sshd.common.channel.ChannelOutputStream;
@@ -43,13 +48,27 @@ import org.apache.sshd.common.util.net.SshdSocketAddress;
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
 public class TcpipClientChannel extends AbstractClientChannel {
-
     /**
-     * Type of channel being created
+     * Type of channel being created. The type's {@link #getName()}
+     * method returns the SSH request type
      */
-    public enum Type {
-        Direct,
-        Forwarded
+    public enum Type implements NamedResource {
+        Direct("direct-tcpip"),
+        Forwarded("forwarded-tcpip");
+
+        public static final Set<Type> VALUES =
+                Collections.unmodifiableSet(EnumSet.allOf(Type.class));
+
+        private final String channelType;
+
+        Type(String channelType) {
+            this.channelType = channelType;
+        }
+
+        @Override
+        public String getName() {
+            return channelType;
+        }
     }
 
     private final Type typeEnum;
@@ -57,9 +76,9 @@ public class TcpipClientChannel extends AbstractClientChannel {
     private final SshdSocketAddress remote;
 
     public TcpipClientChannel(Type type, IoSession serverSession, SshdSocketAddress remote) {
-        super(type == Type.Direct ? "direct-tcpip" : "forwarded-tcpip");
+        super(Objects.requireNonNull(type, "No type specified").getName());
         this.typeEnum = type;
-        this.serverSession = serverSession;
+        this.serverSession = Objects.requireNonNull(serverSession, "No server session provided");
         this.remote = remote;
     }
 
@@ -67,11 +86,16 @@ public class TcpipClientChannel extends AbstractClientChannel {
         return openFuture;
     }
 
+    public Type getTcpipChannelType() {
+        return typeEnum;
+    }
+
     @Override
     public synchronized OpenFuture open() throws IOException {
         InetSocketAddress src;
         InetSocketAddress dst;
-        switch (typeEnum) {
+        Type openType = getTcpipChannelType();
+        switch (openType) {
             case Direct:
                 src = (InetSocketAddress) serverSession.getRemoteAddress();
                 dst = this.remote.toInetSocketAddress();
@@ -81,7 +105,7 @@ public class TcpipClientChannel extends AbstractClientChannel {
                 dst = (InetSocketAddress) serverSession.getLocalAddress();
                 break;
             default:
-                throw new SshException("Unknown client channel type: " + typeEnum);
+                throw new SshException("Unknown client channel type: " + openType);
         }
 
         if (closeFuture.isClosed()) {
@@ -99,8 +123,9 @@ public class TcpipClientChannel extends AbstractClientChannel {
         InetAddress dstAddress = dst.getAddress();
         String dstHost = dstAddress.getHostAddress();
         Window wLocal = getLocalWindow();
+        String type = getChannelType();
         Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN,
-                type.length() + srcHost.length() + dstHost.length() + Long.SIZE);
+            type.length() + srcHost.length() + dstHost.length() + Long.SIZE);
         buffer.putString(type);
         buffer.putInt(getId());
         buffer.putInt(wLocal.getSize());
@@ -139,6 +164,6 @@ public class TcpipClientChannel extends AbstractClientChannel {
 
     @Override
     protected void doWriteExtendedData(byte[] data, int off, long len) throws IOException {
-        throw new UnsupportedOperationException(type + "Tcpip channel does not support extended data");
+        throw new UnsupportedOperationException(getChannelType() + "Tcpip channel does not support extended data");
     }
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17331a8f/sshd-core/src/main/java/org/apache/sshd/server/forward/TcpipServerChannel.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/forward/TcpipServerChannel.java b/sshd-core/src/main/java/org/apache/sshd/server/forward/TcpipServerChannel.java
index 8b8d201..b333170 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/forward/TcpipServerChannel.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/forward/TcpipServerChannel.java
@@ -39,6 +39,7 @@ import org.apache.sshd.common.future.CloseFuture;
 import org.apache.sshd.common.io.IoConnectFuture;
 import org.apache.sshd.common.io.IoConnector;
 import org.apache.sshd.common.io.IoHandler;
+import org.apache.sshd.common.io.IoServiceFactory;
 import org.apache.sshd.common.io.IoSession;
 import org.apache.sshd.common.session.Session;
 import org.apache.sshd.common.util.GenericUtils;
@@ -50,6 +51,7 @@ import org.apache.sshd.common.util.net.SshdSocketAddress;
 import org.apache.sshd.common.util.threads.ExecutorServiceCarrier;
 import org.apache.sshd.common.util.threads.ThreadUtils;
 import org.apache.sshd.server.channel.AbstractServerChannel;
+import org.apache.sshd.server.forward.TcpForwardingFilter.Type;
 
 /**
  * TODO Add javadoc
@@ -98,10 +100,10 @@ public class TcpipServerChannel extends AbstractServerChannel {
     private OutputStream out;
 
     public TcpipServerChannel(ForwardingFilter.Type type) {
-        this.type = type;
+        this.type = Objects.requireNonNull(type, "No channel type specified");
     }
 
-    public final ForwardingFilter.Type getChannelType() {
+    public ForwardingFilter.Type getTcpipChannelType() {
         return type;
     }
 
@@ -116,16 +118,19 @@ public class TcpipServerChannel extends AbstractServerChannel {
                       this, hostToConnect, portToConnect, originatorIpAddress, originatorPort);
         }
 
-        final SshdSocketAddress address;
+        SshdSocketAddress address;
+        Type channelType = getTcpipChannelType();
         switch (type) {
             case Direct:
                 address = new SshdSocketAddress(hostToConnect, portToConnect);
                 break;
-            case Forwarded:
-                address = service.getForwardingFilter().getForwardedPort(portToConnect);
+            case Forwarded: {
+                org.apache.sshd.common.forward.ForwardingFilter ff = service.getForwardingFilter();
+                address = ff.getForwardedPort(portToConnect);
                 break;
+            }
             default:
-                throw new IllegalStateException("Unknown server channel type: " + type);
+                throw new IllegalStateException("Unknown server channel type: " + channelType);
         }
 
         Session session = getSession();
@@ -133,7 +138,7 @@ public class TcpipServerChannel extends AbstractServerChannel {
         TcpForwardingFilter filter = manager.getTcpForwardingFilter();
         OpenFuture f = new DefaultOpenFuture(this, this);
         try {
-            if ((address == null) || (filter == null) || (!filter.canConnect(type, address, session))) {
+            if ((address == null) || (filter == null) || (!filter.canConnect(channelType, address, session))) {
                 if (log.isDebugEnabled()) {
                     log.debug("doInit(" + this + ")[" + type + "][haveFilter=" + (filter != null) + "] filtered out " + address);
                 }
@@ -143,7 +148,7 @@ public class TcpipServerChannel extends AbstractServerChannel {
             }
         } catch (Error e) {
             log.warn("doInit({})[{}] failed ({}) to consult forwarding filter: {}",
-                     session, type, e.getClass().getSimpleName(), e.getMessage());
+                     session, channelType, e.getClass().getSimpleName(), e.getMessage());
             if (log.isDebugEnabled()) {
                 log.debug("doInit(" + this + ")[" + type + "] filter consultation failure details", e);
             }
@@ -184,7 +189,8 @@ public class TcpipServerChannel extends AbstractServerChannel {
             }
         };
 
-        connector = manager.getIoServiceFactory().createConnector(handler);
+        IoServiceFactory ioServiceFactory = manager.getIoServiceFactory();
+        connector = ioServiceFactory.createConnector(handler);
         IoConnectFuture future = connector.connect(address.toInetSocketAddress());
         future.addListener(future1 -> handleChannelConnectResult(f, future1));
         return f;
@@ -239,7 +245,6 @@ public class TcpipServerChannel extends AbstractServerChannel {
         } else {
             f.setException(problem);
         }
-
     }
 
     @Override
@@ -296,7 +301,7 @@ public class TcpipServerChannel extends AbstractServerChannel {
 
     @Override
     protected void doWriteExtendedData(byte[] data, int off, long len) throws IOException {
-        throw new UnsupportedOperationException(type + "Tcpip channel does not support extended data");
+        throw new UnsupportedOperationException(getTcpipChannelType() + "Tcpip channel does not support extended data");
     }
 
     protected void handleWriteDataSuccess(byte cmd, byte[] data, int off, int len) {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/17331a8f/sshd-core/src/main/java/org/apache/sshd/server/x11/ChannelForwardedX11.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/x11/ChannelForwardedX11.java b/sshd-core/src/main/java/org/apache/sshd/server/x11/ChannelForwardedX11.java
index 42c3c38..8865854 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/x11/ChannelForwardedX11.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/x11/ChannelForwardedX11.java
@@ -63,8 +63,9 @@ public class ChannelForwardedX11 extends AbstractClientChannel {
         InetAddress remoteAddress = remote.getAddress();
         String remoteHost = remoteAddress.getHostAddress();
         Window wLocal = getLocalWindow();
+        String type = getChannelType();
         Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN,
-                remoteHost.length() + type.length() + Integer.SIZE);
+            remoteHost.length() + type.length() + Integer.SIZE);
         buffer.putString(type);
         buffer.putInt(getId());
         buffer.putInt(wLocal.getSize());