00001 using System;
00002 using System.IO;
00003 using Tamir.SharpSsh.java.lang;
00004
00005 namespace Tamir.SharpSsh.jsch
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
00031
00032
00033
00034
00035
00036 public class ChannelDirectTCPIP : Channel
00037 {
00038
00039 private const int LOCAL_WINDOW_SIZE_MAX=0x20000;
00040 private const int LOCAL_MAXIMUM_PACKET_SIZE=0x4000;
00041
00042 internal String host;
00043 internal int port;
00044
00045 internal String originator_IP_address="127.0.0.1";
00046 internal int originator_port=0;
00047
00048 internal ChannelDirectTCPIP() : base()
00049 {
00050 setLocalWindowSizeMax(LOCAL_WINDOW_SIZE_MAX);
00051 setLocalWindowSize(LOCAL_WINDOW_SIZE_MAX);
00052 setLocalPacketSize(LOCAL_MAXIMUM_PACKET_SIZE);
00053 }
00054
00055 public override void init ()
00056 {
00057 try
00058 {
00059 io=new IO();
00060 }
00061 catch(Exception e)
00062 {
00063 Console.WriteLine(e);
00064 }
00065 }
00066
00067 public override void connect()
00068 {
00069 try
00070 {
00071 if(!session.isConnected())
00072 {
00073 throw new JSchException("session is down");
00074 }
00075 Buffer buf=new Buffer(150);
00076 Packet packet=new Packet(buf);
00077
00078
00079
00080
00081
00082
00083
00084 packet.reset();
00085 buf.putByte((byte)90);
00086 buf.putString(Util.getBytes("direct-tcpip"));
00087 buf.putInt(id);
00088 buf.putInt(lwsize);
00089 buf.putInt(lmpsize);
00090 buf.putString(Util.getBytes(host));
00091 buf.putInt(port);
00092 buf.putString(Util.getBytes(originator_IP_address));
00093 buf.putInt(originator_port);
00094 session.write(packet);
00095
00096 int retry=1000;
00097 try
00098 {
00099 while(this.getRecipient()==-1 &&
00100 session.isConnected() &&
00101 retry>0 &&
00102 !_eof_remote)
00103 {
00104
00105 Thread.Sleep(50);
00106 retry--;
00107 }
00108 }
00109 catch
00110 {
00111 }
00112
00113 if(!session.isConnected())
00114 {
00115 throw new JSchException("session is down");
00116 }
00117 if(retry==0 || this._eof_remote)
00118 {
00119 throw new JSchException("channel is not opened.");
00120 }
00121
00122
00123
00124
00125
00126
00127
00128 connected=true;
00129
00130 thread=new Thread(this);
00131 thread.start();
00132 }
00133 catch(Exception e)
00134 {
00135 io.close();
00136 io=null;
00137 Channel.del(this);
00138 if (e is JSchException)
00139 {
00140 throw (JSchException) e;
00141 }
00142 }
00143 }
00144
00145 public override void run()
00146 {
00147
00148
00149 Buffer buf=new Buffer(rmpsize);
00150
00151 Packet packet=new Packet(buf);
00152 int i=0;
00153 try
00154 {
00155 while(isConnected() &&
00156 thread!=null &&
00157 io!=null &&
00158 io.ins!=null)
00159 {
00160 i=io.ins.Read(buf.buffer,
00161 14,
00162 buf.buffer.Length-14
00163 -32 -20
00164 );
00165 if(i<=0)
00166 {
00167 eof();
00168 break;
00169 }
00170 if(_close)break;
00171 packet.reset();
00172 buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA);
00173 buf.putInt(recipient);
00174 buf.putInt(i);
00175 buf.skip(i);
00176 session.write(packet, this, i);
00177 }
00178 }
00179 catch
00180 {
00181 }
00182 disconnect();
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196 }
00197
00198 public override void setInputStream(Stream ins)
00199 {
00200 io.setInputStream(ins);
00201 }
00202 public override void setOutputStream(Stream outs)
00203 {
00204 io.setOutputStream(outs);
00205 }
00206
00207 public void setHost(String host){this.host=host;}
00208 public void setPort(int port){this.port=port;}
00209 public void setOrgIPAddress(String foo){this.originator_IP_address=foo;}
00210 public void setOrgPort(int foo){this.originator_port=foo;}
00211 }
00212
00213 }