ChannelDirectTCPIP.java
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 package com.jcraft.jsch;
00031
00032 import java.io.*;
00033
00047 public class ChannelDirectTCPIP extends Channel{
00048
00049 static private final int LOCAL_WINDOW_SIZE_MAX=0x20000;
00050 static private final int LOCAL_MAXIMUM_PACKET_SIZE=0x4000;
00051
00052 String host;
00053 int port;
00054
00055 String originator_IP_address="127.0.0.1";
00056 int originator_port=0;
00057
00058 ChannelDirectTCPIP(){
00059 super();
00060 setLocalWindowSizeMax(LOCAL_WINDOW_SIZE_MAX);
00061 setLocalWindowSize(LOCAL_WINDOW_SIZE_MAX);
00062 setLocalPacketSize(LOCAL_MAXIMUM_PACKET_SIZE);
00063 }
00064
00065 void init (){
00066 try{
00067 io=new IO();
00068 }
00069 catch(Exception e){
00070 System.err.println(e);
00071 }
00072 }
00073
00077 public void connect() throws JSchException{
00078 try{
00079 Session _session=getSession();
00080 if(!_session.isConnected()){
00081 throw new JSchException("session is down");
00082 }
00083 Buffer buf=new Buffer(150);
00084 Packet packet=new Packet(buf);
00085
00086
00087
00088
00089
00090
00091
00092 packet.reset();
00093 buf.putByte((byte)90);
00094 buf.putString(Util.str2byte("direct-tcpip"));
00095 buf.putInt(id);
00096 buf.putInt(lwsize);
00097 buf.putInt(lmpsize);
00098 buf.putString(Util.str2byte(host));
00099 buf.putInt(port);
00100 buf.putString(Util.str2byte(originator_IP_address));
00101 buf.putInt(originator_port);
00102 _session.write(packet);
00103
00104 int retry=10;
00105 long start=System.currentTimeMillis();
00106 long timeout=connectTimeout;
00107 if(timeout!=0L) retry = 1;
00108 synchronized(this){
00109 while(this.getRecipient()==-1 &&
00110 _session.isConnected() &&
00111 retry>0){
00112 if(timeout>0L){
00113 if((System.currentTimeMillis()-start)>timeout){
00114 retry=0;
00115 continue;
00116 }
00117 }
00118 try{
00119 long t = timeout==0L ? 5000L : timeout;
00120 if(_session.jsch.getLogger().isEnabled(Logger.DEBUG)) {
00121 _session.jsch.getLogger().log(Logger.DEBUG, "waiting max. "+t+" ms for channel reply ...");
00122 }
00123 this.notifyme=1;
00124 wait(t);
00125 }
00126 catch(java.lang.InterruptedException e){
00127 }
00128 finally{
00129 this.notifyme=0;
00130 }
00131 retry--;
00132 }
00133 if(_session.jsch.getLogger().isEnabled(Logger.DEBUG)) {
00134 _session.jsch.getLogger().log(Logger.DEBUG, "... finished waiting for channel reply");
00135 }
00136 }
00137 if(!_session.isConnected()){
00138 throw new JSchException("session is down");
00139 }
00140 if(this.getRecipient()==-1){
00141 throw new JSchException("channel is not opened (timeout).");
00142 }
00143 if(this.open_confirmation==false){
00144 throw new JSchException("channel is not opened (failure).");
00145 }
00146
00147 connected=true;
00148
00149 if(io.in!=null){
00150 thread=new Thread(this);
00151 thread.setName("DirectTCPIP thread "+_session.getHost());
00152 if(_session.daemon_thread){
00153 thread.setDaemon(_session.daemon_thread);
00154 }
00155 thread.start();
00156 }
00157 }
00158 catch(Exception e){
00159 io.close();
00160 io=null;
00161 Channel.del(this);
00162 if (e instanceof JSchException) {
00163 throw (JSchException) e;
00164 }
00165 }
00166 }
00167
00168
00172 public void run(){
00173
00174 Buffer buf=new Buffer(rmpsize);
00175 Packet packet=new Packet(buf);
00176 int i=0;
00177
00178 try{
00179 Session _session=getSession();
00180 while(isConnected() &&
00181 thread!=null &&
00182 io!=null &&
00183 io.in!=null){
00184 i=io.in.read(buf.buffer,
00185 14,
00186 buf.buffer.length-14
00187 -Session.buffer_margin
00188 );
00189
00190 if(i<=0){
00191 eof();
00192 break;
00193 }
00194 if(close)break;
00195 packet.reset();
00196 buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA);
00197 buf.putInt(recipient);
00198 buf.putInt(i);
00199 buf.skip(i);
00200 _session.write(packet, this, i);
00201 }
00202 }
00203 catch(Exception e){
00204 }
00205 disconnect();
00206
00207 }
00208
00215 public void setInputStream(InputStream in){
00216 io.setInputStream(in);
00217 }
00218
00225 public void setOutputStream(OutputStream out){
00226 io.setOutputStream(out);
00227 }
00228
00236 public void setHost(String host){this.host=host;}
00237
00244 public void setPort(int port){this.port=port;}
00245
00253 public void setOrgIPAddress(String foo){this.originator_IP_address=foo;}
00254
00262 public void setOrgPort(int foo){this.originator_port=foo;}
00263 }