00001 //using System; 00002 //using System.Net; 00003 //using System.IO; 00004 //using Tamir.Streams; 00005 // 00006 //namespace Tamir.SharpSsh.jsch 00007 //{ 00008 // /* -*-mode:java; c-basic-offset:2; -*- */ 00009 // /* 00010 // Copyright (c) 2002,2003,2004 ymnk, JCraft,Inc. All rights reserved. 00011 // 00012 // Redistribution and use in source and binary forms, with or without 00013 // modification, are permitted provided that the following conditions are met: 00014 // 00015 // 1. Redistributions of source code must retain the above copyright notice, 00016 // this list of conditions and the following disclaimer. 00017 // 00018 // 2. Redistributions in binary form must reproduce the above copyright 00019 // notice, this list of conditions and the following disclaimer in 00020 // the documentation and/or other materials provided with the distribution. 00021 // 00022 // 3. The names of the authors may not be used to endorse or promote products 00023 // derived from this software without specific prior written permission. 00024 // 00025 // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 00026 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 00027 // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT, 00028 // INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, 00029 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00030 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 00031 // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00032 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00033 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 00034 // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00035 // */ 00036 // 00037 // 00038 // public abstract class Channel 00039 // { 00040 // internal static int index=0; 00041 // private static System.Collections.ArrayList pool=new System.Collections.ArrayList(); 00042 // internal static Channel getChannel(String type) 00043 // { 00044 // if(type.Equals("session")) 00045 // { 00046 // return new ChannelSession(); 00047 // } 00048 // if(type.Equals("shell")) 00049 // { 00050 // return new ChannelShell(); 00051 // } 00052 // if(type.Equals("exec")) 00053 // { 00054 // return new ChannelExec(); 00055 // } 00056 // if(type.Equals("x11")) 00057 // { 00058 // return new ChannelX11(); 00059 // } 00060 // if(type.Equals("direct-tcpip")) 00061 // { 00062 // return new ChannelDirectTCPIP(); 00063 // } 00064 // if(type.Equals("forwarded-tcpip")) 00065 // { 00066 // return new ChannelForwardedTCPIP(); 00067 // } 00068 // if(type.Equals("sftp")) 00069 // { 00070 // return new ChannelSftp(); 00071 // } 00072 // return null; 00073 // } 00074 // internal static Channel getChannel(int id, Session session) 00075 // { 00076 // lock(pool) 00077 // { 00078 // for(int i=0; i<pool.Count; i++) 00079 // { 00080 // Channel c=(Channel)(pool[i]); 00081 // if(c.id==id && c.session==session) return c; 00082 // } 00083 // } 00084 // return null; 00085 // } 00086 // internal static void del(Channel c) 00087 // { 00088 // lock(pool) 00089 // { 00090 // pool.Remove(c); 00091 // } 00092 // } 00093 // 00094 // internal int id; 00095 // internal int recipient=-1; 00096 // internal byte[] type= Util.getBytes( "foo" ); 00097 // internal int lwsize_max=0x100000; 00098 // internal int lwsize=0x100000; // local initial window size 00099 // internal int lmpsize=0x4000; // local maximum packet size 00100 // 00101 // internal int rwsize=0; // remote initial window size 00102 // internal int rmpsize=0; // remote maximum packet size 00103 // 00104 // internal IO io=null; 00105 // internal System.Threading.Thread thread=null; 00106 // 00107 // internal bool eof_local=false; 00108 // internal bool eof_remote=false; 00109 // 00110 // internal bool _close=false; 00111 // 00112 // internal int exitstatus=-1; 00113 // 00114 // internal int reply=0; 00115 // 00116 // internal Session session; 00117 // 00118 // internal Channel() 00119 // { 00120 // lock(pool) 00121 // { 00122 // id=index++; 00123 // pool.Add(this); 00124 // } 00125 // } 00126 // internal void setRecipient(int foo) 00127 // { 00128 // this.recipient=foo; 00129 // } 00130 // internal virtual int getRecipient() 00131 // { 00132 // return recipient; 00133 // } 00134 // 00135 // public virtual void init() 00136 // { 00137 // } 00138 // 00139 // public virtual void connect() 00140 // { 00141 // if(!isConnected()) 00142 // { 00143 // throw new JSchException("session is down"); 00144 // } 00145 // try 00146 // { 00147 // Buffer buf=new Buffer(100); 00148 // Packet packet=new Packet(buf); 00149 // // send 00150 // // byte SSH_MSG_CHANNEL_OPEN(90) 00151 // // string channel type // 00152 // // uint32 sender channel // 0 00153 // // uint32 initial window size // 0x100000(65536) 00154 // // uint32 maxmum packet size // 0x4000(16384) 00155 // packet.reset(); 00156 // buf.putByte((byte)90); 00157 // buf.putString(this.type); 00158 // buf.putInt(this.id); 00159 // buf.putInt(this.lwsize); 00160 // buf.putInt(this.lmpsize); 00161 // session.write(packet); 00162 // 00163 // int retry=1000; 00164 // while(this.getRecipient()==-1 && 00165 // session.IsConnected() && 00166 // retry>0) 00167 // { 00168 // try{System.Threading.Thread.Sleep(50);} 00169 // catch{} 00170 // retry--; 00171 // } 00172 // if(!session.IsConnected()) 00173 // { 00174 // throw new JSchException("session is down"); 00175 // } 00176 // if(retry==0) 00177 // { 00178 // throw new JSchException("channel is not opened."); 00179 // } 00180 // start(); 00181 // } 00182 // catch(Exception e) 00183 // { 00184 // if(e is JSchException) throw (JSchException)e; 00185 // } 00186 // } 00187 // 00188 // public virtual void setXForwarding(bool foo) 00189 // { 00190 // } 00191 // 00192 // public virtual void start() {} 00193 // 00194 // public bool isEOF() {return eof_remote;} 00195 // 00196 // internal virtual void getData(Buffer buf) 00197 // { 00198 // setRecipient(buf.getInt()); 00199 // setRemoteWindowSize(buf.getInt()); 00200 // setRemotePacketSize(buf.getInt()); 00201 // } 00202 // 00203 // public RequestWindowChange getRequestWindowChange() 00204 // { 00205 // return new RequestWindowChange(); 00206 // } 00207 // 00208 // public virtual void setInputStream(Stream ins) 00209 // { 00210 // io.setInputStream(ins); 00211 // } 00212 // public virtual void setOutputStream(Stream outs) 00213 // { 00214 // io.setOutputStream(outs); 00215 // } 00216 // public virtual void setExtOutputStream(Stream outs) 00217 // { 00218 // io.setExtOutputStream(outs); 00219 // } 00220 // public virtual Stream getInputStream() 00221 // { 00222 // PipedInputStream ins= 00223 // new MyPipedInputStream( 00224 // 32*1024 // this value should be customizable. 00225 // ); 00226 // io.setOutputStream(new PassiveOutputStream(ins)); 00227 // return ins; 00228 // } 00229 // public virtual Stream getExtInputStream() 00230 // { 00231 // PipedInputStream ins= 00232 // new MyPipedInputStream( 00233 // 32*1024 // this value should be customizable. 00234 // ); 00235 // io.setExtOutputStream(new PassiveOutputStream(ins)); 00236 // return ins; 00237 // } 00238 // public virtual Stream getOutputStream() 00239 // { 00240 // PipedOutputStream outs=new PipedOutputStream(); 00241 // io.setInputStream(new PassiveInputStream(outs)); 00242 // return outs; 00243 // } 00244 // 00245 // internal void setLocalWindowSizeMax(int foo){ this.lwsize_max=foo; } 00246 // internal void setLocalWindowSize(int foo){ this.lwsize=foo; } 00247 // internal void setLocalPacketSize(int foo){ this.lmpsize=foo; } 00248 // internal void setRemoteWindowSize(int foo){ this.rwsize=foo; } 00249 // internal void addRemoteWindowSize(int foo){ this.rwsize+=foo; } 00250 // internal void setRemotePacketSize(int foo){ this.rmpsize=foo; } 00251 // 00252 // public virtual void run() 00253 // { 00254 // } 00255 // 00256 // internal virtual void write(byte[] foo) 00257 // { 00258 // write(foo, 0, foo.Length); 00259 // } 00260 // internal virtual void write(byte[] foo, int s, int l) 00261 // { 00262 // //if(eof_remote)return; 00263 // if(io.outs!=null) 00264 // io.put(foo, s, l); 00265 // } 00266 // internal void write_ext(byte[] foo, int s, int l) 00267 // { 00268 // //if(eof_remote)return; 00269 // if(io.out_ext!=null) 00270 // io.put_ext(foo, s, l); 00271 // } 00272 // 00273 // internal void eof() 00274 // { 00275 // //System.out.println("EOF!!!! "+this); 00276 // //Thread.dumpStack(); 00277 // if(eof_local)return; 00278 // eof_local=true; 00279 // //close=eof; 00280 // try 00281 // { 00282 // Buffer buf=new Buffer(100); 00283 // Packet packet=new Packet(buf); 00284 // packet.reset(); 00285 // buf.putByte((byte)Session.SSH_MSG_CHANNEL_EOF); 00286 // buf.putInt(getRecipient()); 00287 // session.write(packet); 00288 // } 00289 // catch 00290 // { 00291 // //System.out.println("Channel.eof"); 00292 // //e.printStackTrace(); 00293 // } 00294 // if(!isConnected()) 00295 // { 00296 // disconnect(); 00297 // } 00298 // } 00299 // 00300 // internal void close() 00301 // { 00302 // //System.out.println("close!!!!"); 00303 // if(_close)return; 00304 // _close=true; 00305 // try 00306 // { 00307 // Buffer buf=new Buffer(100); 00308 // Packet packet=new Packet(buf); 00309 // packet.reset(); 00310 // buf.putByte((byte)Session.SSH_MSG_CHANNEL_CLOSE); 00311 // buf.putInt(getRecipient()); 00312 // session.write(packet); 00313 // session.disconnect(); 00314 // } 00315 // catch 00316 // { 00317 // //e.printStackTrace(); 00318 // } 00319 // } 00320 // internal static void eof(Session session) 00321 // { 00322 // Channel[] channels=null; 00323 // int count=0; 00324 // lock(pool) 00325 // { 00326 // channels=new Channel[pool.Count]; 00327 // for(int i=0; i<pool.Count; i++) 00328 // { 00329 // try 00330 // { 00331 // Channel c=((Channel)(pool[i])); 00332 // if(c.session==session) 00333 // { 00334 // channels[count++]=c; 00335 // } 00336 // } 00337 // catch 00338 // { 00339 // } 00340 // } 00341 // } 00342 // for(int i=0; i<count; i++) 00343 // { 00344 // channels[i].eof(); 00345 // } 00346 // } 00347 // 00348 // //public void finalize() { 00349 // public virtual void finalize() 00350 // { 00351 // disconnect(); 00352 // //super.finalize(); 00353 // session=null; 00354 // } 00355 // 00356 // public virtual void disconnect() 00357 // { 00358 // //System.out.println(this+":disconnect "+((ChannelExec)this).command+" "+io.in); 00359 // //System.out.println(this+":disconnect "+io+" "+io.in); 00360 // close(); 00361 // //System.out.println("$1"); 00362 // 00363 // thread=null; 00364 // try 00365 // { 00366 // if(io!=null) 00367 // { 00368 // try 00369 // { 00370 // //System.out.println(" io.in="+io.in); 00371 // if(io.ins!=null && 00372 // (io.ins is PassiveInputStream) 00373 // ) 00374 // io.ins.Close(); 00375 // } 00376 // catch{} 00377 // try 00378 // { 00379 // //System.out.println(" io.out="+io.out); 00380 // if(io.outs!=null && 00381 // (io.outs is PassiveOutputStream) 00382 // ) 00383 // io.outs.Close(); 00384 // } 00385 // catch{} 00386 // } 00387 // } 00388 // catch 00389 // { 00390 // //e.printStackTrace(); 00391 // } 00392 // //System.out.println("$2"); 00393 // io=null; 00394 // Channel.del(this); 00395 // } 00396 // 00397 // public bool isConnected() 00398 // { 00399 // if(this.session!=null) 00400 // { 00401 // return session.IsConnected(); 00402 // } 00403 // return false; 00404 // } 00405 // 00406 // public void sendSignal(String foo) 00407 // { 00408 // RequestSignal request=new RequestSignal(); 00409 // request.setSignal(foo); 00410 // request.request(session, this); 00411 // } 00412 // 00413 // // public String toString(){ 00414 // // return "Channel: type="+new String(type)+",id="+id+",recipient="+recipient+",window_size="+window_size+",packet_size="+packet_size; 00415 // // } 00416 // 00417 // /* 00418 // class OutputThread extends Thread{ 00419 // Channel c; 00420 // OutputThread(Channel c){ this.c=c;} 00421 // public void run(){c.output_thread();} 00422 // } 00423 // */ 00424 // 00425 // internal class PassiveInputStream : PipedInputStream 00426 // { 00427 // PipedOutputStream outs; 00428 // internal PassiveInputStream(PipedOutputStream outs) :base(outs) 00429 // { 00430 // this.outs=outs; 00431 // } 00432 // public override void close() 00433 // { 00434 // if(outs!=null) 00435 // { 00436 // this.outs.close(); 00437 // } 00438 // outs=null; 00439 // } 00440 // } 00441 // internal class PassiveOutputStream : PipedOutputStream 00442 // { 00443 // internal PassiveOutputStream(PipedInputStream ins):base(ins) 00444 // { 00445 // 00446 // } 00447 // } 00448 // 00449 // internal void setExitStatus(int foo){ exitstatus=foo; } 00450 // public int getExitStatus(){ return exitstatus; } 00451 // 00452 // internal void setSession(Session session) 00453 // { 00454 // this.session=session; 00455 // } 00456 // } 00457 // 00458 // public class MyPipedInputStream : Tamir.Streams.PipedInputStream 00459 // { 00460 // public MyPipedInputStream():base(){} 00461 // 00462 // public MyPipedInputStream(int size):base() 00463 // { 00464 // buffer=new byte[size]; 00465 // } 00466 // public MyPipedInputStream(PipedOutputStream outs):base(outs) { } 00467 // public MyPipedInputStream(PipedOutputStream outs, int size):base(outs) 00468 // { 00469 // buffer=new byte[size]; 00470 // } 00471 // } 00472 //}
1.5.9