Author: gnodet Date: Thu Feb 11 14:32:17 2010 New Revision: 908998 URL: http://svn.apache.org/viewvc?rev=908998&view=rev Log: SSHD-68: Support subsystem on the client side
Added: mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSubsystem.java Modified: mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/ClientChannel.java mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/ClientSession.java mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelExec.java mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java Modified: mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/ClientChannel.java URL: http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/ClientChannel.java?rev=908998&r1=908997&r2=908998&view=diff ============================================================================== --- mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/ClientChannel.java (original) +++ mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/ClientChannel.java Thu Feb 11 14:32:17 2010 @@ -35,6 +35,7 @@ String CHANNEL_EXEC = "exec"; String CHANNEL_SHELL = "shell"; + String CHANNEL_SUBSYSTEM = "subsystem"; int TIMEOUT = 0x0001; int CLOSED = 0x0002; Modified: mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/ClientSession.java URL: http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/ClientSession.java?rev=908998&r1=908997&r2=908998&view=diff ============================================================================== --- mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/ClientSession.java (original) +++ mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/ClientSession.java Thu Feb 11 14:32:17 2010 @@ -22,6 +22,9 @@ import java.security.KeyPair; import java.security.PublicKey; +import org.apache.sshd.client.channel.ChannelExec; +import org.apache.sshd.client.channel.ChannelSession; +import org.apache.sshd.client.channel.ChannelSubsystem; import org.apache.sshd.client.future.AuthFuture; import org.apache.sshd.common.future.CloseFuture; @@ -61,6 +64,14 @@ ClientChannel createChannel(String type) throws Exception; + ClientChannel createChannel(String type, String subType) throws Exception; + + ChannelSession createShellChannel() throws Exception; + + ChannelExec createExecChannel(String command) throws Exception; + + ChannelSubsystem createSubsystemChannel(String subsystem) throws Exception; + int waitFor(int mask, long timeout); CloseFuture close(boolean immediately); Modified: mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelExec.java URL: http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelExec.java?rev=908998&r1=908997&r2=908998&view=diff ============================================================================== --- mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelExec.java (original) +++ mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelExec.java Thu Feb 11 14:32:17 2010 @@ -18,8 +18,6 @@ */ package org.apache.sshd.client.channel; -import java.io.ByteArrayOutputStream; - import org.apache.sshd.common.SshConstants; import org.apache.sshd.common.util.Buffer; @@ -30,59 +28,23 @@ */ public class ChannelExec extends ChannelSession { - protected void doOpen() throws Exception { - super.doOpen(); - - Buffer buffer; + private final String command; -// log.info("Send SSH_MSG_CHANNEL_REQUEST pty-req"); -// buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST); -// buffer.putInt(recipient); -// buffer.putString("pty-req"); -// buffer.putBoolean(false); -// buffer.putString(System.getProperty("TERM", "dummy")); -// buffer.putInt(80); -// buffer.putInt(24); -// buffer.putInt(640); -// buffer.putInt(480); -// Buffer modes = new Buffer(); -// modes.putByte((byte) 50); // ISIG -// modes.putInt(1); -// modes.putByte((byte) 51); // ICANON -// modes.putInt(1); -// modes.putByte((byte) 53); // ECHO -// modes.putInt(1); -// modes.putByte((byte) 54); // ECHOE -// modes.putInt(1); -// modes.putByte((byte) 55); // ECHOK -// modes.putInt(1); -// modes.putByte((byte) 56); // ECHONL -// modes.putInt(0); -// modes.putByte((byte) 57); // NOFLSH -// modes.putInt(0); -// modes.putByte((byte) 0); -// buffer.putBytes(modes.getCompactData()); -// session.writePacket(buffer); - -// log.info("Send SSH_MSG_CHANNEL_REQUEST env"); -// buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST); -// buffer.putInt(recipient); -// buffer.putString("env"); -// session.writePacket(buffer); - - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - byte[] buf = new byte[8192]; - while (in.available() > 0) { - int l = in.read(buf); - baos.write(buf, 0, l); + public ChannelExec(String command) { + if (command == null) { + throw new IllegalArgumentException("command must not be null"); } + this.command = command; + } + protected void doOpen() throws Exception { + super.doOpen(); log.info("Send SSH_MSG_CHANNEL_REQUEST exec"); - buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST, 0); + Buffer buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST, 0); buffer.putInt(recipient); buffer.putString("exec"); buffer.putBoolean(false); - buffer.putString(baos.toString()); + buffer.putString(command); session.writePacket(buffer); } Added: mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSubsystem.java URL: http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSubsystem.java?rev=908998&view=auto ============================================================================== --- mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSubsystem.java (added) +++ mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSubsystem.java Thu Feb 11 14:32:17 2010 @@ -0,0 +1,51 @@ +/* + * 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.client.channel; + +import org.apache.sshd.common.SshConstants; +import org.apache.sshd.common.util.Buffer; + +/** + * Client channel to run a subsystem + * + * @author <a href="mailto:d...@mina.apache.org">Apache MINA SSHD Project</a> + */ +public class ChannelSubsystem extends ChannelSession { + + private final String subsystem; + + public ChannelSubsystem(String subsystem) { + if (subsystem == null) { + throw new IllegalArgumentException("subsystem must not be null"); + } + this.subsystem = subsystem; + } + + protected void doOpen() throws Exception { + super.doOpen(); + log.info("Send SSH_MSG_CHANNEL_REQUEST exec"); + Buffer buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST, 0); + buffer.putInt(recipient); + buffer.putString("subsystem"); + buffer.putBoolean(false); + buffer.putString(subsystem); + session.writePacket(buffer); + + } +} Modified: mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java URL: http://svn.apache.org/viewvc/mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java?rev=908998&r1=908997&r2=908998&view=diff ============================================================================== --- mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java (original) +++ mina/sshd/trunk/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java Thu Feb 11 14:32:17 2010 @@ -31,6 +31,7 @@ import org.apache.sshd.client.channel.AbstractClientChannel; import org.apache.sshd.client.channel.ChannelShell; import org.apache.sshd.client.channel.ChannelExec; +import org.apache.sshd.client.channel.ChannelSubsystem; import org.apache.sshd.client.future.AuthFuture; import org.apache.sshd.client.future.DefaultAuthFuture; import org.apache.sshd.client.future.OpenFuture; @@ -134,15 +135,35 @@ } public ClientChannel createChannel(String type) throws Exception { - // TODO: use NamedFactory to create channel - AbstractClientChannel channel; + return createChannel(type, null); + } + + public ClientChannel createChannel(String type, String subType) throws Exception { if (ClientChannel.CHANNEL_SHELL.equals(type)) { - channel = new ChannelShell(); + return createShellChannel(); } else if (ClientChannel.CHANNEL_EXEC.equals(type)) { - channel = new ChannelExec(); + return createExecChannel(subType); + } else if (ClientChannel.CHANNEL_SUBSYSTEM.equals(type)) { + return createSubsystemChannel(subType); } else { throw new IllegalArgumentException("Unsupported channel type " + type); } + } + + public ChannelShell createShellChannel() throws Exception { + ChannelShell channel = new ChannelShell(); + registerChannel(channel); + return channel; + } + + public ChannelExec createExecChannel(String command) throws Exception { + ChannelExec channel = new ChannelExec(command); + registerChannel(channel); + return channel; + } + + public ChannelSubsystem createSubsystemChannel(String subsystem) throws Exception { + ChannelSubsystem channel = new ChannelSubsystem(subsystem); registerChannel(channel); return channel; }