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.io.*;
033:
034: public class ChannelDirectTCPIP extends Channel {
035:
036: static private final int LOCAL_WINDOW_SIZE_MAX = 0x20000;
037: static private final int LOCAL_MAXIMUM_PACKET_SIZE = 0x4000;
038:
039: String host;
040: int port;
041:
042: String originator_IP_address = "127.0.0.1";
043: int originator_port = 0;
044:
045: ChannelDirectTCPIP() {
046: super ();
047: setLocalWindowSizeMax(LOCAL_WINDOW_SIZE_MAX);
048: setLocalWindowSize(LOCAL_WINDOW_SIZE_MAX);
049: setLocalPacketSize(LOCAL_MAXIMUM_PACKET_SIZE);
050: }
051:
052: void init() {
053: try {
054: io = new IO();
055: } catch (Exception e) {
056: System.err.println(e);
057: }
058: }
059:
060: public void connect() throws JSchException {
061: try {
062: if (!session.isConnected()) {
063: throw new JSchException("session is down");
064: }
065: Buffer buf = new Buffer(150);
066: Packet packet = new Packet(buf);
067: // send
068: // byte SSH_MSG_CHANNEL_OPEN(90)
069: // string channel type //
070: // uint32 sender channel // 0
071: // uint32 initial window size // 0x100000(65536)
072: // uint32 maxmum packet size // 0x4000(16384)
073:
074: packet.reset();
075: buf.putByte((byte) 90);
076: buf.putString("direct-tcpip".getBytes());
077: buf.putInt(id);
078: buf.putInt(lwsize);
079: buf.putInt(lmpsize);
080: buf.putString(host.getBytes());
081: buf.putInt(port);
082: buf.putString(originator_IP_address.getBytes());
083: buf.putInt(originator_port);
084: session.write(packet);
085:
086: int retry = 1000;
087: try {
088: while (this .getRecipient() == -1
089: && session.isConnected() && retry > 0
090: && !eof_remote) {
091: //Thread.sleep(500);
092: Thread.sleep(50);
093: retry--;
094: }
095: } catch (Exception ee) {
096: }
097: if (!session.isConnected()) {
098: throw new JSchException("session is down");
099: }
100: if (retry == 0 || this .eof_remote) {
101: throw new JSchException("channel is not opened.");
102: }
103: /*
104: if(this.eof_remote){ // failed to open
105: disconnect();
106: return;
107: }
108: */
109:
110: connected = true;
111:
112: if (io.in != null) {
113: thread = new Thread(this );
114: thread.setName("DirectTCPIP thread "
115: + session.getHost());
116: if (session.daemon_thread) {
117: thread.setDaemon(session.daemon_thread);
118: }
119: thread.start();
120: }
121: } catch (Exception e) {
122: io.close();
123: io = null;
124: Channel.del(this );
125: if (e instanceof JSchException) {
126: throw (JSchException) e;
127: }
128: }
129: }
130:
131: public void run() {
132:
133: Buffer buf = new Buffer(rmpsize);
134: Packet packet = new Packet(buf);
135: int i = 0;
136:
137: try {
138: while (isConnected() && thread != null && io != null
139: && io.in != null) {
140: i = io.in.read(buf.buffer, 14,
141: buf.buffer.length - 14 - 32 - 20 // padding and mac
142: );
143:
144: if (i <= 0) {
145: eof();
146: break;
147: }
148: if (close)
149: break;
150: packet.reset();
151: buf.putByte((byte) Session.SSH_MSG_CHANNEL_DATA);
152: buf.putInt(recipient);
153: buf.putInt(i);
154: buf.skip(i);
155: session.write(packet, this , i);
156: }
157: } catch (Exception e) {
158: }
159: disconnect();
160: //System.err.println("connect end");
161: }
162:
163: public void setInputStream(InputStream in) {
164: io.setInputStream(in);
165: }
166:
167: public void setOutputStream(OutputStream out) {
168: io.setOutputStream(out);
169: }
170:
171: public void setHost(String host) {
172: this .host = host;
173: }
174:
175: public void setPort(int port) {
176: this .port = port;
177: }
178:
179: public void setOrgIPAddress(String foo) {
180: this .originator_IP_address = foo;
181: }
182:
183: public void setOrgPort(int foo) {
184: this.originator_port = foo;
185: }
186: }
|