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 public class ChannelDirectTCPIP extends Channel{
36 static private final int LOCAL_WINDOW_SIZE_MAX=0x20000;
37 static private final int LOCAL_MAXIMUM_PACKET_SIZE=0x4000;
42 String originator_IP_address="127.0.0.1";
43 int originator_port=0;
47 setLocalWindowSizeMax(LOCAL_WINDOW_SIZE_MAX);
48 setLocalWindowSize(LOCAL_WINDOW_SIZE_MAX);
49 setLocalPacketSize(LOCAL_MAXIMUM_PACKET_SIZE);
57 System.err.println(e);
61 public void connect() throws JSchException{
63 Session _session=getSession();
64 if(!_session.isConnected()){
65 throw new JSchException("session is down");
67 Buffer buf=new Buffer(150);
68 Packet packet=new Packet(buf);
70 // byte SSH_MSG_CHANNEL_OPEN(90)
71 // string channel type //
72 // uint32 sender channel // 0
73 // uint32 initial window size // 0x100000(65536)
74 // uint32 maxmum packet size // 0x4000(16384)
77 buf.putByte((byte)90);
78 buf.putString(Util.str2byte("direct-tcpip"));
82 buf.putString(Util.str2byte(host));
84 buf.putString(Util.str2byte(originator_IP_address));
85 buf.putInt(originator_port);
86 _session.write(packet);
90 while(this.getRecipient()==-1 &&
91 _session.isConnected() &&
101 if(!_session.isConnected()){
102 throw new JSchException("session is down");
104 if(retry==0 || this.eof_remote){
105 throw new JSchException("channel is not opened.");
108 if(this.eof_remote){ // failed to open
117 thread=new Thread(this);
118 thread.setName("DirectTCPIP thread "+_session.getHost());
119 if(_session.daemon_thread){
120 thread.setDaemon(_session.daemon_thread);
129 if (e instanceof JSchException) {
130 throw (JSchException) e;
137 Buffer buf=new Buffer(rmpsize);
138 Packet packet=new Packet(buf);
142 Session _session=getSession();
143 while(isConnected() &&
147 i=io.in.read(buf.buffer,
150 -32 -20 // padding and mac
159 buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA);
160 buf.putInt(recipient);
163 _session.write(packet, this, i);
169 //System.err.println("connect end");
172 public void setInputStream(InputStream in){
173 io.setInputStream(in);
175 public void setOutputStream(OutputStream out){
176 io.setOutputStream(out);
179 public void setHost(String host){this.host=host;}
180 public void setPort(int port){this.port=port;}
181 public void setOrgIPAddress(String foo){this.originator_IP_address=foo;}
182 public void setOrgPort(int foo){this.originator_port=foo;}