internal override void GetData(Buffer buf) { SetRecipient(buf.GetInt()); SetRemoteWindowSize(buf.GetUInt()); SetRemotePacketSize(buf.GetInt()); byte[] addr = buf.GetString(); int port = buf.GetInt(); byte[] orgaddr = buf.GetString(); int orgport = buf.GetInt(); Session _session = null; try { _session = GetSession(); } catch (JSchException) { } // session has been already down. lock (pool) { for (int i = 0; i < pool.Count; i++) { object[] foo = (object[])(pool[i]); if (foo[0] != _session) { continue; } if (((int)foo[1]) != port) { continue; } this.rport = port; this.target = (string)foo[2]; if (foo[3] == null || (foo[3] is object[])) { this.lport = -1; } else { this.lport = ((int)foo[3]); } if (foo.Length >= 6) { this.factory = ((SocketFactory)foo[5]); } break; } if (target == null) { } } }
public virtual void Run() { thread = this; byte[] foo; Buffer buf = new Buffer(); Packet packet = new Packet(buf); int i = 0; Channel channel; int[] start = new int[1]; int[] length = new int[1]; KeyExchange kex = null; int stimeout = 0; try { while (isConnected && thread != null) { try { buf = Read(buf); stimeout = 0; } catch (ThreadInterruptedException ee) { if (!in_kex && stimeout < serverAliveCountMax) { SendKeepAliveMsg(); stimeout++; continue; } throw; } int msgType = buf.GetCommand() & unchecked((int)(0xff)); if (kex != null && kex.GetState() == msgType) { kex_start_time = Runtime.CurrentTimeMillis(); bool result = kex.Next(buf); if (!result) { throw new JSchException("verify: " + result); } continue; } switch (msgType) { case SSH_MSG_KEXINIT: { //System.err.println("KEXINIT"); kex = Receive_kexinit(buf); break; } case SSH_MSG_NEWKEYS: { //System.err.println("NEWKEYS"); Send_newkeys(); Receive_newkeys(buf, kex); kex = null; break; } case SSH_MSG_CHANNEL_DATA: { buf.GetInt(); buf.GetByte(); buf.GetByte(); i = buf.GetInt(); channel = Channel.GetChannel(i, this); foo = buf.GetString(start, length); if (channel == null) { break; } if (length[0] == 0) { break; } try { channel.Write(foo, start[0], length[0]); } catch (Exception) { //System.err.println(e); try { channel.Disconnect(); } catch (Exception) { } break; } int len = length[0]; channel.SetLocalWindowSize(channel.lwsize - len); if (channel.lwsize < channel.lwsize_max / 2) { packet.Reset(); buf.PutByte(unchecked((byte)SSH_MSG_CHANNEL_WINDOW_ADJUST)); buf.PutInt(channel.GetRecipient()); buf.PutInt(channel.lwsize_max - channel.lwsize); Write(packet); channel.SetLocalWindowSize(channel.lwsize_max); } break; } case SSH_MSG_CHANNEL_EXTENDED_DATA: { buf.GetInt(); buf.GetShort(); i = buf.GetInt(); channel = Channel.GetChannel(i, this); buf.GetInt(); // data_type_code == 1 foo = buf.GetString(start, length); //System.err.println("stderr: "+new String(foo,start[0],length[0])); if (channel == null) { break; } if (length[0] == 0) { break; } channel.Write_ext(foo, start[0], length[0]); int len = length[0]; channel.SetLocalWindowSize(channel.lwsize - len); if (channel.lwsize < channel.lwsize_max / 2) { packet.Reset(); buf.PutByte(unchecked((byte)SSH_MSG_CHANNEL_WINDOW_ADJUST)); buf.PutInt(channel.GetRecipient()); buf.PutInt(channel.lwsize_max - channel.lwsize); Write(packet); channel.SetLocalWindowSize(channel.lwsize_max); } break; } case SSH_MSG_CHANNEL_WINDOW_ADJUST: { buf.GetInt(); buf.GetShort(); i = buf.GetInt(); channel = Channel.GetChannel(i, this); if (channel == null) { break; } channel.AddRemoteWindowSize(buf.GetInt()); break; } case SSH_MSG_CHANNEL_EOF: { buf.GetInt(); buf.GetShort(); i = buf.GetInt(); channel = Channel.GetChannel(i, this); if (channel != null) { //channel.eof_remote=true; //channel.eof(); channel.Eof_remote(); } break; } case SSH_MSG_CHANNEL_CLOSE: { buf.GetInt(); buf.GetShort(); i = buf.GetInt(); channel = Channel.GetChannel(i, this); if (channel != null) { // channel.close(); channel.Disconnect(); } break; } case SSH_MSG_CHANNEL_OPEN_CONFIRMATION: { buf.GetInt(); buf.GetShort(); i = buf.GetInt(); channel = Channel.GetChannel(i, this); if (channel == null) { } //break; int r = buf.GetInt(); long rws = buf.GetUInt(); int rps = buf.GetInt(); channel.SetRemoteWindowSize(rws); channel.SetRemotePacketSize(rps); channel.SetRecipient(r); break; } case SSH_MSG_CHANNEL_OPEN_FAILURE: { buf.GetInt(); buf.GetShort(); i = buf.GetInt(); channel = Channel.GetChannel(i, this); if (channel == null) { } //break; int reason_code = buf.GetInt(); //foo=buf.getString(); // additional textual information //foo=buf.getString(); // language tag channel.exitstatus = reason_code; channel.close = true; channel.eof_remote = true; channel.SetRecipient(0); break; } case SSH_MSG_CHANNEL_REQUEST: { buf.GetInt(); buf.GetShort(); i = buf.GetInt(); foo = buf.GetString(); bool reply = (buf.GetByte() != 0); channel = Channel.GetChannel(i, this); if (channel != null) { byte reply_type = unchecked((byte)SSH_MSG_CHANNEL_FAILURE); if ((Util.Byte2str(foo)).Equals("exit-status")) { i = buf.GetInt(); // exit-status channel.SetExitStatus(i); reply_type = unchecked((byte)SSH_MSG_CHANNEL_SUCCESS); } if (reply) { packet.Reset(); buf.PutByte(reply_type); buf.PutInt(channel.GetRecipient()); Write(packet); } } break; } case SSH_MSG_CHANNEL_OPEN: { buf.GetInt(); buf.GetShort(); foo = buf.GetString(); string ctyp = Util.Byte2str(foo); if (!"forwarded-tcpip".Equals(ctyp) && !("x11".Equals(ctyp) && x11_forwarding) && !("*****@*****.**".Equals(ctyp) && agent_forwarding)) { //System.err.println("Session.run: CHANNEL OPEN "+ctyp); //throw new IOException("Session.run: CHANNEL OPEN "+ctyp); packet.Reset(); buf.PutByte(unchecked((byte)SSH_MSG_CHANNEL_OPEN_FAILURE)); buf.PutInt(buf.GetInt()); buf.PutInt(Channel.SSH_OPEN_ADMINISTRATIVELY_PROHIBITED); buf.PutString(Util.empty); buf.PutString(Util.empty); Write(packet); } else { channel = Channel.GetChannel(ctyp); AddChannel(channel); channel.GetData(buf); channel.Init(); Sharpen.Thread tmp = new Sharpen.Thread(channel); tmp.SetName("Channel " + ctyp + " " + host); if (daemon_thread) { tmp.SetDaemon(daemon_thread); } tmp.Start(); break; } goto case SSH_MSG_CHANNEL_SUCCESS; } case SSH_MSG_CHANNEL_SUCCESS: { buf.GetInt(); buf.GetShort(); i = buf.GetInt(); channel = Channel.GetChannel(i, this); if (channel == null) { break; } channel.reply = 1; break; } case SSH_MSG_CHANNEL_FAILURE: { buf.GetInt(); buf.GetShort(); i = buf.GetInt(); channel = Channel.GetChannel(i, this); if (channel == null) { break; } channel.reply = 0; break; } case SSH_MSG_GLOBAL_REQUEST: { buf.GetInt(); buf.GetShort(); foo = buf.GetString(); // request name bool reply = (buf.GetByte() != 0); if (reply) { packet.Reset(); buf.PutByte(unchecked((byte)SSH_MSG_REQUEST_FAILURE)); Write(packet); } break; } case SSH_MSG_REQUEST_FAILURE: case SSH_MSG_REQUEST_SUCCESS: { Sharpen.Thread t = grr.GetThread(); if (t != null) { grr.SetReply(msgType == SSH_MSG_REQUEST_SUCCESS ? 1 : 0); t.Interrupt(); } break; } default: { //System.err.println("Session.run: unsupported type "+msgType); throw new IOException("Unknown SSH message type " + msgType); } } } } catch (Exception e) { in_kex = false; if (JSch.GetLogger().IsEnabled(Logger.INFO)) { JSch.GetLogger().Log(Logger.INFO, "Caught an exception, leaving main loop due to " + e.Message); } } //System.err.println("# Session.run"); //e.printStackTrace(); try { Disconnect(); } catch (ArgumentNullException) { } catch (Exception) { } //System.err.println("@1"); //e.printStackTrace(); //System.err.println("@2"); //e.printStackTrace(); isConnected = false; }
internal virtual void GetData(Buffer buf) { SetRecipient(buf.GetInt()); SetRemoteWindowSize(buf.GetUInt()); SetRemotePacketSize(buf.GetInt()); }