ChannelDirectTCPIP.java

Go to the documentation of this file.
00001 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
00002 /*
00003 Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007 
00008   1. Redistributions of source code must retain the above copyright notice,
00009      this list of conditions and the following disclaimer.
00010 
00011   2. Redistributions in binary form must reproduce the above copyright 
00012      notice, this list of conditions and the following disclaimer in 
00013      the documentation and/or other materials provided with the distribution.
00014 
00015   3. The names of the authors may not be used to endorse or promote products
00016      derived from this software without specific prior written permission.
00017 
00018 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
00019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
00020 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
00021 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
00022 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00023 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00025 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00026 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00027 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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       // send
00086       // byte   SSH_MSG_CHANNEL_OPEN(90)
00087       // string channel type         //
00088       // uint32 sender channel       // 0
00089       // uint32 initial window size  // 0x100000(65536)
00090       // uint32 maxmum packet size   // 0x4000(16384)
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){  // timeout
00141         throw new JSchException("channel is not opened (timeout).");
00142       }
00143       if(this.open_confirmation==false){  // SSH_MSG_CHANNEL_OPEN_FAILURE
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     //System.err.println("connect end");
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 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1