001: /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
002: /*
003: Copyright (c) 2002-2008 ymnk, JCraft,Inc. All rights reserved.
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions are met:
007:
008: 1. Redistributions of source code must retain the above copyright notice,
009: this list of conditions and the following disclaimer.
010:
011: 2. Redistributions in binary form must reproduce the above copyright
012: notice, this list of conditions and the following disclaimer in
013: the documentation and/or other materials provided with the distribution.
014:
015: 3. The names of the authors may not be used to endorse or promote products
016: derived from this software without specific prior written permission.
017:
018: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
019: INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
020: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
021: INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
022: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
023: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
024: OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
027: EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: */
029:
030: package com.jcraft.jsch;
031:
032: import java.util.*;
033:
034: class ChannelSession extends Channel {
035: private static byte[] _session = "session".getBytes();
036:
037: protected boolean agent_forwarding = false;
038: protected boolean xforwading = false;
039: protected Hashtable env = null;
040:
041: protected boolean pty = false;
042:
043: protected String ttype = "vt100";
044: protected int tcol = 80;
045: protected int trow = 24;
046: protected int twp = 640;
047: protected int thp = 480;
048: protected byte[] terminal_mode = null;
049:
050: ChannelSession() {
051: super ();
052: type = _session;
053: io = new IO();
054: }
055:
056: public void setAgentForwarding(boolean enable) {
057: agent_forwarding = enable;
058: }
059:
060: public void setXForwarding(boolean enable) {
061: xforwading = enable;
062: }
063:
064: public void setEnv(Hashtable env) {
065: this .env = env;
066: }
067:
068: public void setPty(boolean enable) {
069: pty = enable;
070: }
071:
072: public void setTerminalMode(byte[] terminal_mode) {
073: this .terminal_mode = terminal_mode;
074: }
075:
076: public void setPtySize(int col, int row, int wp, int hp) {
077: if (!pty) {
078: return;
079: }
080: try {
081: RequestWindowChange request = new RequestWindowChange();
082: request.setSize(col, row, wp, hp);
083: request.request(session, this );
084: } catch (Exception e) {
085: //System.err.println("ChannelSessio.setPtySize: "+e);
086: }
087: }
088:
089: public void setPtyType(String ttype) {
090: setPtyType(ttype, 80, 24, 640, 480);
091: }
092:
093: public void setPtyType(String ttype, int col, int row, int wp,
094: int hp) {
095: this .ttype = ttype;
096: this .tcol = col;
097: this .trow = row;
098: this .twp = wp;
099: this .thp = hp;
100: }
101:
102: protected void sendRequests() throws Exception {
103: Request request;
104: if (agent_forwarding) {
105: request = new RequestAgentForwarding();
106: request.request(session, this );
107: }
108:
109: if (xforwading) {
110: request = new RequestX11();
111: request.request(session, this );
112: }
113:
114: if (pty) {
115: request = new RequestPtyReq();
116: ((RequestPtyReq) request).setTType(ttype);
117: ((RequestPtyReq) request).setTSize(tcol, trow, twp, thp);
118: if (terminal_mode != null) {
119: ((RequestPtyReq) request)
120: .setTerminalMode(terminal_mode);
121: }
122: request.request(session, this );
123: }
124:
125: if (env != null) {
126: for (Enumeration _env = env.keys(); _env.hasMoreElements();) {
127: String name = (String) (_env.nextElement());
128: String value = (String) (env.get(name));
129: request = new RequestEnv();
130: ((RequestEnv) request).setEnv(name, value);
131: request.request(session, this );
132: }
133: }
134: }
135:
136: public void run() {
137: //System.err.println(this+":run >");
138: /*
139: if(thread!=null){ return; }
140: thread=Thread.currentThread();
141: */
142:
143: Buffer buf = new Buffer(rmpsize);
144: Packet packet = new Packet(buf);
145: int i = -1;
146: try {
147: while (isConnected() && thread != null && io != null
148: && io.in != null) {
149: i = io.in.read(buf.buffer, 14,
150: buf.buffer.length - 14 - 32 - 20 // padding and mac
151: );
152: if (i == 0)
153: continue;
154: if (i == -1) {
155: eof();
156: break;
157: }
158: if (close)
159: break;
160: //System.out.println("write: "+i);
161: packet.reset();
162: buf.putByte((byte) Session.SSH_MSG_CHANNEL_DATA);
163: buf.putInt(recipient);
164: buf.putInt(i);
165: buf.skip(i);
166: session.write(packet, this , i);
167: }
168: } catch (Exception e) {
169: //System.err.println("# ChannelExec.run");
170: //e.printStackTrace();
171: }
172: if (thread != null) {
173: synchronized (thread) {
174: thread.notifyAll();
175: }
176: }
177: thread = null;
178: //System.err.println(this+":run <");
179: }
180: }
|