A
download Channel.cs
Language: C#
Copyright: (c) 2002,2003,2004 ymnk, JCraft,Inc. All rights reserved.
LOC: 405
Project Info
labmanager - A C# application for administ...bs...(labmanager)
Server: Google
Type: svn
...er\trunk\lib\SharpSSH\jsch\
   Buffer.cs
   Channel.cs
   Channel.old.cs
   ChannelDirectTCPIP.cs
   ChannelExec.cs
   ChannelForwardedTCPIP.cs
   ChannelSession.cs
   ChannelSftp.cs
   ChannelSftpOld.cs
   ChannelSftpStreamGet.cs
   ChannelSftpStreamPut.cs
   ChannelShell.cs
   ChannelSubsystem.cs
   ChannelX11.cs
   Cipher.cs
   Compression.cs
   DH.cs
   DHG1.cs
   DHGEX.cs
   ForwardedTCPIPDaemon.cs
   HASH.cs
   HostKey.cs
   HostKeyRepository.cs
   Identity.cs
   IdentityFile.cs
   IO.cs
   JSch.cs
   JSchAuthCancelException.cs
   JSchException.cs
   ...PartialAuthException.cs
   KeyExchange.cs
   KeyPair.cs
   KeyPairDSA.cs
   KeyPairGenDSA.cs
   KeyPairGenRSA.cs
   KeyPairRSA.cs
   KnownHosts.cs
   MAC.cs
   Packet.cs
   PortWatcher.cs
   Proxy.cs
   Random.cs
   Request.cs
   RequestExec.cs
   RequestPtyReq.cs
   RequestSftp.cs
   RequestShell.cs
   RequestSignal.cs
   RequestSubsystem.cs
   RequestWindowChange.cs
   RequestX11.cs
   ServerSocketFactory.cs
   Session.cs
   Session.old.cs
   SftpATTRS.cs
   SftpException.cs
   SftpProgressMonitor.cs
   SignatureDSA.cs
   SignatureRSA.cs
   SocketFactory.cs
   UIKeyboardInteractive.cs
   UserAuth.cs
   ...hKeyboardInteractive.cs
   UserAuthNone.cs
   UserAuthPassword.cs
   UserAuthPublicKey.cs
   UserInfo.cs
   Util.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
using System;
using System.Net;
using System.IO;
using Tamir.Streams;
using System.Runtime.CompilerServices;
using Tamir.SharpSsh.java.lang;
using Str = Tamir.SharpSsh.java.String;

namespace Tamir.SharpSsh.jsch
{
	/* -*-mode:java; c-basic-offset:2; -*- */
	/*
	Copyright (c) 2002,2003,2004 ymnk, JCraft,Inc. All rights reserved.

	Redistribution and use In source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	  1. Redistributions of source code must retain the above copyright notice,
		 this list of conditions and the following disclaimer.

	  2. Redistributions In binary form must reproduce the above copyright 
		 notice, this list of conditions and the following disclaimer In 
		 the documentation and/or other materials provided with the distribution.

	  3. The names of the authors may not be used to endorse or promote products
		 derived from this software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
	FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
	INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
	INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
	LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
	OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
	LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
	NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
	EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
	*/


	public abstract class Channel : Tamir.SharpSsh.java.lang.Runnable
	{
		internal static int index=0; 
		private static java.util.Vector pool=new java.util.Vector();
		internal static Channel getChannel(String type)
		{
			if(type.Equals("session"))
			{
				return new ChannelSession();
			}
			if(type.Equals("shell"))
			{
				return new ChannelShell();
			}
			if(type.Equals("exec"))
			{
				return new ChannelExec();
			}
			if(type.Equals("x11"))
			{
				return new ChannelX11();
			}
			if(type.Equals("direct-tcpip"))
			{
				return new ChannelDirectTCPIP();
			}
			if(type.Equals("forwarded-tcpip"))
			{
				return new ChannelForwardedTCPIP();
			}
			if(type.Equals("sftp"))
			{
				return new ChannelSftp();
			}
			if(type.Equals("subsystem"))
			{
				return new ChannelSubsystem();
			}
			return null;
		}
		internal static Channel getChannel(int id, Session session)
		{
			lock(pool)
			{
				for(int i=0; i<pool.size(); i++)
				{
					Channel c=(Channel)(pool.elementAt(i));
					if(c.id==id && c.session==session) return c;
				}
			}
			return null;
		}
		internal static void del(Channel c)
		{
			lock(pool)
			{
				pool.removeElement(c);
			}
		}

		internal int id;
		internal int recipient=-1;
		internal byte[] type=new Str("foo").getBytes();
		internal int lwsize_max=0x100000;
		internal int lwsize=0x100000;  // local initial window size
		internal int lmpsize=0x4000;     // local maximum packet size

		internal int rwsize=0;         // remote initial window size
		internal int rmpsize=0;        // remote maximum packet size

		internal IO io=null;    
		internal Thread thread=null;

		internal bool eof_local=false;
		internal bool _eof_remote=false;

		internal bool _close=false;
		internal bool connected=false;

		internal int exitstatus=-1;

		internal int reply=0; 

		internal Session session;

		internal Channel()
		{
			lock(pool)
			{
				id=index++;
				pool.addElement(this);
			}
		}
		internal virtual void setRecipient(int foo)
		{
			this.recipient=foo;
		}
		internal virtual int getRecipient()
		{
			return recipient;
		}

		public virtual void init()
		{
		}

		public virtual void connect()
		{
			if(!session.isConnected())
			{
				throw new JSchException("session is down");
			}
			try
			{
				Buffer buf=new Buffer(100);
				Packet packet=new Packet(buf);
				// send
				// byte   SSH_MSG_CHANNEL_OPEN(90)
				// string channel type         //
				// uint32 sender channel       // 0
				// uint32 initial window size  // 0x100000(65536)
				// uint32 maxmum packet size   // 0x4000(16384)
				packet.reset();
				buf.putByte((byte)90);
				buf.putString(this.type);
				buf.putInt(this.id);
				buf.putInt(this.lwsize);
				buf.putInt(this.lmpsize);
				session.write(packet);

				int retry=1000;
				while(this.getRecipient()==-1 &&
					session.isConnected() &&
					retry>0)
				{
					try{Thread.sleep(50);}
					catch(Exception ee){}
					retry--;
				}
				if(!session.isConnected())
				{
					throw new JSchException("session is down");
				}
				if(retry==0)
				{
					throw new JSchException("channel is not opened.");
				}
				connected=true;
				start();
			}
			catch(Exception e)
			{
				connected=false;
				if(e is JSchException) throw (JSchException)e;
			}
		}

		public virtual void setXForwarding(bool foo)
		{
		}

		public virtual void start(){}

		public bool isEOF() {return _eof_remote;}

		internal virtual void getData(Buffer buf)
		{
			setRecipient(buf.getInt());
			setRemoteWindowSize(buf.getInt());
			setRemotePacketSize(buf.getInt());
		}

		public virtual void setInputStream(Stream In)
		{
			io.setInputStream(In, false);
		}
		public virtual void setInputStream(Stream In, bool dontclose)
		{
			io.setInputStream(In, dontclose);
		}
		public virtual void setOutputStream(Stream Out)
		{
			io.setOutputStream(Out, false);
		}
		public virtual void setOutputStream(Stream Out, bool dontclose)
		{
			io.setOutputStream(Out, dontclose);
		}
		public virtual void setExtOutputStream(Stream Out)
		{
			io.setExtOutputStream(Out, false);
		}
		public virtual void setExtOutputStream(Stream Out, bool dontclose)
		{
			io.setExtOutputStream(Out, dontclose);
		}
		public virtual java.io.InputStream getInputStream()  
		{
			PipedInputStream In=
				new MyPipedInputStream(
				32*1024  // this value should be customizable.
				);
			io.setOutputStream(new PassiveOutputStream(In), false);
			return In;
		}
		public virtual java.io.InputStream getExtInputStream()  
		{
			PipedInputStream In=
				new MyPipedInputStream(
				32*1024  // this value should be customizable.
				);
			io.setExtOutputStream(new PassiveOutputStream(In), false);
			return In;
		}
		public virtual Stream getOutputStream()  
		{
			PipedOutputStream Out=new PipedOutputStream();
			io.setInputStream(new PassiveInputStream(Out
				, 32*1024
				), false);
			//  io.setInputStream(new PassiveInputStream(Out), false);
			return Out;
		}
		internal class MyPipedInputStream : PipedInputStream
		{
			internal MyPipedInputStream():base() { ; }
			internal MyPipedInputStream(int size) :base()
			{
				buffer=new byte[size];
			}
			internal MyPipedInputStream(PipedOutputStream Out):base(Out) { }
			internal MyPipedInputStream(PipedOutputStream Out, int size):base(Out) 
			{
				buffer=new byte[size];
			}
		}
		internal virtual void setLocalWindowSizeMax(int foo){ this.lwsize_max=foo; }
		internal virtual void setLocalWindowSize(int foo){ this.lwsize=foo; }
		internal virtual void setLocalPacketSize(int foo){ this.lmpsize=foo; }
		[System.Runtime.CompilerServices.MethodImpl(MethodImplOptions.Synchronized)]
		internal virtual void setRemoteWindowSize(int foo){ this.rwsize=foo; }
		[System.Runtime.CompilerServices.MethodImpl(MethodImplOptions.Synchronized)]
		internal virtual void addRemoteWindowSize(int foo){ this.rwsize+=foo; }
		internal virtual void setRemotePacketSize(int foo){ this.rmpsize=foo; }

		public virtual void run()
		{
		}

		internal virtual void write(byte[] foo)  
		{
			write(foo, 0, foo.Length);
		}
		internal virtual void write(byte[] foo, int s, int l)  
		{
			try
			{
				//    if(io.outs!=null)
				io.put(foo, s, l);
			}
			catch(NullReferenceException e){}
		}
		internal virtual void write_ext(byte[] foo, int s, int l)  
		{
			try
			{
				//    if(io.out_ext!=null)
				io.put_ext(foo, s, l);
			}
			catch(NullReferenceException e){}
		}

		internal virtual void eof_remote()
		{
			_eof_remote=true;
			try
			{
				if(io.outs!=null)
				{
					io.outs.Close();
					io.outs=null;
				}
			}
			catch(NullReferenceException e){}
			catch(IOException e){}
		}

		internal virtual void eof()
		{
			//System.Out.println("EOF!!!! "+this);
			//Thread.dumpStack();
			if(_close)return;
			if(eof_local)return;
			eof_local=true;
			//close=eof;
			try
			{
				Buffer buf=new Buffer(100);
				Packet packet=new Packet(buf);
				packet.reset();
				buf.putByte((byte)Session.SSH_MSG_CHANNEL_EOF);
				buf.putInt(getRecipient());
				session.write(packet);
			}
			catch(Exception e)
			{
				//System.Out.println("Channel.eof");
				//e.printStackTrace();
			}
			/*
			if(!isConnected()){ disconnect(); }
			*/
		}

		/*
		http://www1.ietf.org/internet-drafts/draft-ietf-secsh-connect-24.txt

	  5.3  Closing a Channel
		When a party will no longer send more data to a channel, it SHOULD
		 send SSH_MSG_CHANNEL_EOF.

				  byte      SSH_MSG_CHANNEL_EOF
				  uint32    recipient_channel

		No explicit response is sent to this message.  However, the
		 application may send EOF to whatever is at the other end of the
		channel.  Note that the channel remains open after this message, and
		 more data may still be sent In the other direction.  This message
		 does not consume window space and can be sent even if no window space
		 is available.

		   When either party wishes to terminate the channel, it sends
		   SSH_MSG_CHANNEL_CLOSE.  Upon receiving this message, a party MUST
		 send back a SSH_MSG_CHANNEL_CLOSE unless it has already sent this
		 message for the channel.  The channel is considered closed for a
		   party when it has both sent and received SSH_MSG_CHANNEL_CLOSE, and
		 the party may then reuse the channel number.  A party MAY send
		 SSH_MSG_CHANNEL_CLOSE without having sent or received
		 SSH_MSG_CHANNEL_EOF.

				  byte      SSH_MSG_CHANNEL_CLOSE
				  uint32    recipient_channel

		 This message does not consume window space and can be sent even if no
		 window space is available.

		 It is recommended that any data sent before this message is delivered
		   to the actual destination, if possible.
		*/

		internal virtual void close()
		{
			//System.Out.println("close!!!!");
			if(_close)return;
			_close=true;
			try
			{
				Buffer buf=new Buffer(100);
				Packet packet=new Packet(buf);
				packet.reset();
				buf.putByte((byte)Session.SSH_MSG_CHANNEL_CLOSE);
				buf.putInt(getRecipient());
				session.write(packet);
			}
			catch(Exception e)
			{
				//e.printStackTrace();
			}
		}
		public virtual bool isClosed()
		{
			return _close;
		}
		internal static void disconnect(Session session)
		{
			Channel[] channels=null;
			int count=0;
			lock(pool)
			{
				channels=new Channel[pool.size()];
				for(int i=0; i<pool.size(); i++)
				{
					try
					{
						Channel c=((Channel)(pool.elementAt(i)));
						if(c.session==session)
						{
							channels[count++]=c;
						}
					}
					catch(Exception e)
					{
					}
				} 
			}
			for(int i=0; i<count; i++)
			{
				channels[i].disconnect();
			}
		}

		/*
		public void finalize() throws Throwable{
		  disconnect();
		  super.finalize();
		  session=null;
		}
		*/

		public virtual void disconnect()
		{
			//System.Out.println(this+":disconnect "+io+" "+io.in);
			if(!connected)
			{
				return;
			}
			connected=false;

			close();

			_eof_remote=eof_local=true;

			thread=null;

			try
			{
				if(io!=null)
				{
					io.close();
				}
			}
			catch(Exception e)
			{
				//e.printStackTrace();
			}
			io=null;
			Channel.del(this);
		}

		public virtual bool isConnected()
		{
			if(this.session!=null)
			{
				return session.isConnected() && connected;
			}
			return false;
		}

		public virtual void sendSignal(String foo)  
		{
			RequestSignal request=new RequestSignal();
			request.setSignal(foo);
			request.request(session, this);
		}

		//  public String toString(){
		//      return "Channel: type="+new String(type)+",id="+id+",recipient="+recipient+",window_size="+window_size+",packet_size="+packet_size;
		//  }

		/*
		  class OutputThread extends Thread{
			Channel c;
			OutputThread(Channel c){ this.c=c;}
			public void run(){c.output_thread();}
		  }
		*/

		internal class PassiveInputStream : MyPipedInputStream
		{
			internal PipedOutputStream Out;
			internal PassiveInputStream(PipedOutputStream Out, int size) :base(Out, size)
			{
				this.Out=Out;
			}
			internal PassiveInputStream(PipedOutputStream Out):base(Out) 
			{
				this.Out=Out;
			}
			public override void close() 
			{
				if(Out!=null)
				{
					this.Out.close();
				}
				Out=null;
			}
		}
		internal class PassiveOutputStream : PipedOutputStream
		{
			internal PassiveOutputStream(PipedInputStream In) :base(In)
			{
			}
		}

		internal virtual void setExitStatus(int foo){ exitstatus=foo; }
		public virtual int getExitStatus(){ return exitstatus; }

		internal virtual void setSession(Session session)
		{
			this.session=session;
		}
		public virtual Session getSession(){ return session; }
		public virtual int getId(){ return id; }
		//public int getRecipientId(){ return getRecipient(); }

	}
}

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us