1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
3 Copyright (c) 2002-2010 ymnk, JCraft,Inc. All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in
13 the documentation and/or other materials provided with the distribution.
15 3. The names of the authors may not be used to endorse or promote products
16 derived from this software without specific prior written permission.
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
21 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 package com.jcraft.jsch;
34 class ChannelSession extends Channel{
35 private static byte[] _session=Util.str2byte("session");
37 protected boolean agent_forwarding=false;
38 protected boolean xforwading=false;
39 protected Hashtable env=null;
41 protected boolean pty=false;
43 protected String ttype="vt100";
44 protected int tcol=80;
45 protected int trow=24;
46 protected int twp=640;
47 protected int thp=480;
48 protected byte[] terminal_mode=null;
57 * Enable the agent forwarding.
61 public void setAgentForwarding(boolean enable){
62 agent_forwarding=enable;
66 * Enable the X11 forwarding.
69 * @see RFC4254 6.3.1. Requesting X11 Forwarding
71 public void setXForwarding(boolean enable){
76 * @deprecated Use {@link #setEnv(String, String)} or {@link #setEnv(byte[], byte[])} instead.
77 * @see #setEnv(String, String)
78 * @see #setEnv(byte[], byte[])
80 public void setEnv(Hashtable env){
87 * Set the environment variable.
88 * If <code>name</code> and <code>value</code> are needed to be passed
89 * to the remote in your faivorite encoding,use
90 * {@link #setEnv(byte[], byte[])}.
92 * @param name A name for environment variable.
93 * @param value A value for environment variable.
94 * @see RFC4254 6.4 Environment Variable Passing
96 public void setEnv(String name, String value){
97 setEnv(Util.str2byte(name), Util.str2byte(value));
101 * Set the environment variable.
103 * @param name A name of environment variable.
104 * @param value A value of environment variable.
105 * @see #setEnv(String, String)
106 * @see RFC4254 6.4 Environment Variable Passing
108 public void setEnv(byte[] name, byte[] value){
110 getEnv().put(name, value);
114 private Hashtable getEnv(){
121 * Allocate a Pseudo-Terminal.
124 * @see RFC4254 6.2. Requesting a Pseudo-Terminal
126 public void setPty(boolean enable){
131 * Set the terminal mode.
133 * @param terminal_mode
135 public void setTerminalMode(byte[] terminal_mode){
136 this.terminal_mode=terminal_mode;
140 * Change the window dimension interactively.
142 * @param col terminal width, columns
143 * @param row terminal height, rows
144 * @param wp terminal width, pixels
145 * @param hp terminal height, pixels
146 * @see RFC4254 6.7. Window Dimension Change Message
148 public void setPtySize(int col, int row, int wp, int hp){
149 setPtyType(this.ttype, col, row, wp, hp);
150 if(!pty || !isConnected()){
154 RequestWindowChange request=new RequestWindowChange();
155 request.setSize(col, row, wp, hp);
156 request.request(getSession(), this);
159 //System.err.println("ChannelSessio.setPtySize: "+e);
164 * Set the terminal type.
165 * This method is not effective after Channel#connect().
167 * @param ttype terminal type(for example, "vt100")
168 * @see #setPtyType(String, int, int, int, int)
170 public void setPtyType(String ttype){
171 setPtyType(ttype, 80, 24, 640, 480);
175 * Set the terminal type.
176 * This method is not effective after Channel#connect().
178 * @param ttype terminal type(for example, "vt100")
179 * @param col terminal width, columns
180 * @param row terminal height, rows
181 * @param wp terminal width, pixels
182 * @param hp terminal height, pixels
184 public void setPtyType(String ttype, int col, int row, int wp, int hp){
192 protected void sendRequests() throws Exception{
193 Session _session=getSession();
195 if(agent_forwarding){
196 request=new RequestAgentForwarding();
197 request.request(_session, this);
201 request=new RequestX11();
202 request.request(_session, this);
206 request=new RequestPtyReq();
207 ((RequestPtyReq)request).setTType(ttype);
208 ((RequestPtyReq)request).setTSize(tcol, trow, twp, thp);
209 if(terminal_mode!=null){
210 ((RequestPtyReq)request).setTerminalMode(terminal_mode);
212 request.request(_session, this);
216 for(Enumeration _env=env.keys(); _env.hasMoreElements();){
217 Object name=_env.nextElement();
218 Object value=env.get(name);
219 request=new RequestEnv();
220 ((RequestEnv)request).setEnv(toByteArray(name),
222 request.request(_session, this);
227 private byte[] toByteArray(Object o){
228 if(o instanceof String){
229 return Util.str2byte((String)o);
235 //System.err.println(this+":run >");
237 Buffer buf=new Buffer(rmpsize);
238 Packet packet=new Packet(buf);
241 while(isConnected() &&
245 i=io.in.read(buf.buffer,
248 -32 -20 // padding and mac
256 //System.out.println("write: "+i);
258 buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA);
259 buf.putInt(recipient);
262 getSession().write(packet, this, i);
266 //System.err.println("# ChannelExec.run");
267 //e.printStackTrace();
269 Thread _thread=thread;
271 synchronized(_thread){ _thread.notifyAll(); }
274 //System.err.println(this+":run <");