Example #1
0
        public void Run()
        {
            Buffer buf = new Buffer(300); // ??
            Packet packet = new Packet(buf);
            thread = this;
            try
            {
                while (thread != null)
                {
                    Socket socket = ss.Accept();
                    socket.setTcpNoDelay(true);
                    Stream In = socket.getInputStream();
                    Stream Out = socket.getOutputStream();
                    ChannelDirectTCPIP channel = new ChannelDirectTCPIP();
                    channel.Init();
                    channel.setInputStream(In);
                    channel.setOutputStream(Out);
                    session.addChannel(channel);
                    ((ChannelDirectTCPIP)channel).Host = host;
                    ((ChannelDirectTCPIP)channel).Port = rport;
                    ((ChannelDirectTCPIP)channel).OrgIPAddress = socket.getInetAddress().getHostAddress();
                    ((ChannelDirectTCPIP)channel).OrgPort = socket.Port;
                    channel.connect();
                    if (channel.ExitStatus != -1)
                    {
                    }
                }
            }
            catch { }

            delete();
        }
Example #2
0
        public override void Run()
        {
            m_thread = Thread.currentThread();
            Buffer buf = new Buffer(m_rmpsize);
            Packet packet = new Packet(buf);
            int i = 0;
            try
            {
                while (m_thread != null && m_io != null && m_io.m_ins != null)
                {
                    i = m_io.m_ins.Read(buf.m_buffer,
                        14,
                        buf.m_buffer.Length - 14
                        - 32 - 20 // padding and mac
                        );
                    if (i <= 0)
                    {
                        eof();
                        break;
                    }
                    packet.reset();
                    if (m_close)
                        break;
                    buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA);
                    buf.putInt(m_recipient);
                    buf.putInt(i);
                    buf.skip(i);
                    m_session.write(packet, this, i);
                }
            }
            catch (Exception)
            { }

            disconnect();
        }
Example #3
0
        public void request(Session session, Channel channel)
        {
            Buffer buf = new Buffer();
            Packet packet = new Packet(buf);

            bool reply = waitForReply();
            if (reply)
                channel.Replay = -1;

            packet.reset();
            buf.putByte((byte)Session.SSH_MSG_CHANNEL_REQUEST);
            buf.putInt(channel.Recipient);
            buf.putString("subsystem");
            buf.putByte((byte)(waitForReply() ? 1 : 0));
            buf.putString(m_subsystem);
            session.write(packet);

            if (reply)
            {
                while (channel.Replay == -1)
                {
                    try { Thread.sleep(10); }
                    catch { }
                }
                if (channel.Replay == 0)
                    throw new JSchException("failed to send subsystem request");
            }
        }
Example #4
0
        public void request(Session session, Channel channel)
        {
            Buffer buf = new Buffer();
            Packet packet = new Packet(buf);

            bool reply = waitForReply();
            if (reply)
            {
                channel.Replay = -1;
            }

            packet.reset();
            buf.putByte((byte)Session.SSH_MSG_CHANNEL_REQUEST);
            buf.putInt(channel.Recipient);
            buf.putString(Util.getBytes("subsystem"));
            buf.putByte((byte)(waitForReply() ? 1 : 0));
            buf.putString(Util.getBytes("sftp"));
            session.write(packet);

            if (reply)
            {
                while (channel.Replay == -1)
                {
                    try { System.Threading.Thread.Sleep(10); }
                    catch//(Exception ee)
                    {
                    }
                }
                if (channel.Replay == 0)
                {
                    throw new JSchException("failed to send sftp request");
                }
            }
        }
Example #5
0
        public override void connect()
        {
            try
            {
                if (!m_session.IsConnected())
                    throw new JSchException("session is down");

                Buffer buf = new Buffer(150);
                Packet packet = new Packet(buf);

                packet.reset();
                buf.putByte((byte)90);
                buf.putString(Util.getBytes("direct-tcpip"));
                buf.putInt(m_id);
                buf.putInt(m_lwsize);
                buf.putInt(m_lmpsize);
                buf.putString(Util.getBytes(m_host));
                buf.putInt(m_port);
                buf.putString(Util.getBytes(m_originator_IP_address));
                buf.putInt(m_originator_port);
                m_session.write(packet);

                int retry = 1000;
                try
                {
                    while (Recipient == -1
                        && m_session.IsConnected()
                        && retry > 0
                        && !m_eof_remote
                        )
                    {
                        Thread.Sleep(50);
                        retry--;
                    }
                }
                catch { }

                if (!m_session.IsConnected())
                    throw new JSchException("session is down");
                if (retry == 0 || this.m_eof_remote)
                    throw new JSchException("channel is not opened.");

                m_connected = true;
                m_thread = new Thread(this);
                m_thread.Start();
            }
            catch (Exception e)
            {
                m_io.close();
                m_io = null;
                Channel.Remove(this);
                if (e is JSchException)
                    throw (JSchException)e;
            }
        }
Example #6
0
        public void request(Session session, Channel channel)
        {
            Buffer buf = new Buffer();
            Packet packet = new Packet(buf);

            packet.reset();
            buf.putByte((byte)Session.SSH_MSG_CHANNEL_REQUEST);
            buf.putInt(channel.Recipient);
            buf.putString(Util.getBytes("signal"));
            buf.putByte((byte)(waitForReply() ? 1 : 0));
            buf.putString(Util.getBytes(m_signal));
            session.write(packet);
        }
Example #7
0
        public override void init(Session session, byte[] V_S, byte[] V_C, byte[] I_S, byte[] I_C)
        {
            m_session = session;
            m_V_S = V_S;
            m_V_C = V_C;
            m_I_S = I_S;
            m_I_C = I_C;
            try
            {
                Type t = Type.GetType(session.getConfig("sha-1"));
                m_sha = (HASH)(Activator.CreateInstance(t));
                m_sha.init();
            }
            catch (Exception ex)
            {
                throw ex;
            }

            m_buf = new Buffer();
            m_packet = new Packet(m_buf);

            try
            {
                Type t = Type.GetType(session.getConfig("dh"));
                m_dh = (DH)(Activator.CreateInstance(t));
                m_dh.init();
            }
            catch (Exception ee)
            {
                throw ee;
            }

            m_dh.P = m_p;
            m_dh.G = m_g;

            // The client responds with:
            // byte  SSH_MSG_KEXDH_INIT(30)
            // mpint e <- g^x mod p
            //         x is a random number (1 < x < (p-1)/2)

            m_e = m_dh.E;

            m_packet.reset();
            m_buf.putByte((byte)SSH_MSG_KEXDH_INIT);
            m_buf.putMPInt(m_e);
            session.write(m_packet);

            m_state = SSH_MSG_KEXDH_REPLY;
        }
Example #8
0
        public void request(Session session, Channel channel)
        {
            Buffer buf = new Buffer();
            Packet packet = new Packet(buf);

            // send
            // byte     SSH_MSG_CHANNEL_REQUEST(98)
            // uint32 recipient channel
            // string request type       // "shell"
            // boolean want reply        // 0
            packet.reset();
            buf.putByte((byte)Session.SSH_MSG_CHANNEL_REQUEST);
            buf.putInt(channel.Recipient);
            buf.putString(Util.getBytes("shell"));
            buf.putByte((byte)(waitForReply() ? 1 : 0));
            session.write(packet);
        }
Example #9
0
        public override void init(Session session, byte[] V_S, byte[] V_C, byte[] I_S, byte[] I_C)
        {
            m_session = session;
            m_V_S = V_S;
            m_V_C = V_C;
            m_I_S = I_S;
            m_I_C = I_C;

            try
            {
                Type t = Type.GetType(session.getConfig("sha-1"));
                m_sha = (HASH)(Activator.CreateInstance(t));
                m_sha.init();
            }
            catch (Exception) { }

            m_buf = new Buffer();
            m_packet = new Packet(m_buf);

            try
            {
                Type t = Type.GetType(session.getConfig("dh"));
                m_dh = (DH)(Activator.CreateInstance(t));
                m_dh.init();
            }
            catch (Exception e)
            {
                throw e;
            }

            m_packet.reset();
            m_buf.putByte((byte)0x22);
            m_buf.putInt(m_min);
            m_buf.putInt(m_preferred);
            m_buf.putInt(m_max);
            session.write(m_packet);

            m_state = SSH_MSG_KEX_DH_GEX_GROUP;
        }
Example #10
0
        public void request(Session session, Channel channel)
        {
            Buffer buf = new Buffer();
            Packet packet = new Packet(buf);

            //byte      SSH_MSG_CHANNEL_REQUEST
            //uint32    recipient_channel
            //string    "window-change"
            //boolean   FALSE
            //uint32    terminal width, columns
            //uint32    terminal height, rows
            //uint32    terminal width, pixels
            //uint32    terminal height, pixels
            packet.reset();
            buf.putByte((byte)Session.SSH_MSG_CHANNEL_REQUEST);
            buf.putInt(channel.Recipient);
            buf.putString(Util.getBytes("window-change"));
            buf.putByte((byte)(waitForReply() ? 1 : 0));
            buf.putInt(m_width_columns);
            buf.putInt(m_height_rows);
            buf.putInt(m_width_pixels);
            buf.putInt(m_height_pixels);
            session.write(packet);
        }
Example #11
0
        public void Run()
        {
            m_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;

            try
            {
                while (m_isConnected &&
                    m_thread != null)
                {
                    buf = read(buf);
                    int msgType = buf.m_buffer[5] & 0xff;

                    if (kex != null && kex.getState() == msgType)
                    {
                        bool result = kex.next(buf);
                        if (!result)
                            throw new JSchException("verify: " + result);
                        continue;
                    }

                    switch (msgType)
                    {
                        case SSH_MSG_KEXINIT:
                            kex = receive_kexinit(buf);
                            break;

                        case SSH_MSG_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.FindChannel(i, this);
                            foo = buf.getString(start, length);
                            if (channel == null)
                                break;

                            try
                            {
                                channel.write(foo, start[0], length[0]);
                            }
                            catch (Exception)
                            {
                                try
                                {
                                    channel.disconnect();
                                }
                                catch (Exception)
                                { }
                                break;
                            }
                            int len = length[0];
                            channel.LocalWindowSize = channel.LocalWindowSize - len;
                            if (channel.LocalWindowSize < channel.LocalWindowSizeMax / 2)
                            {
                                packet.reset();
                                buf.putByte((byte)SSH_MSG_CHANNEL_WINDOW_ADJUST);
                                buf.putInt(channel.Recipient);
                                buf.putInt(channel.LocalWindowSizeMax - channel.LocalWindowSize);
                                write(packet);
                                channel.LocalWindowSize = channel.LocalWindowSizeMax;
                            }
                            break;

                        case SSH_MSG_CHANNEL_EXTENDED_DATA:
                            buf.getInt();
                            buf.getShort();
                            i = buf.getInt();
                            channel = Channel.FindChannel(i, this);
                            buf.getInt();   // data_type_code == 1
                            foo = buf.getString(start, length);

                            if (channel == null)
                                break;

                            channel.write_ext(foo, start[0], length[0]);

                            len = length[0];
                            channel.LocalWindowSize = channel.LocalWindowSize - len;
                            if (channel.LocalWindowSize < channel.LocalWindowSizeMax / 2)
                            {
                                packet.reset();
                                buf.putByte((byte)SSH_MSG_CHANNEL_WINDOW_ADJUST);
                                buf.putInt(channel.Recipient);
                                buf.putInt(channel.LocalWindowSizeMax - channel.LocalWindowSize);
                                write(packet);
                                channel.LocalWindowSize = channel.LocalWindowSizeMax;
                            }
                            break;

                        case SSH_MSG_CHANNEL_WINDOW_ADJUST:
                            buf.getInt();
                            buf.getShort();
                            i = buf.getInt();
                            channel = Channel.FindChannel(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.FindChannel(i, this);
                            if (channel != null)
                                channel.eof_remote();
                            break;
                        case SSH_MSG_CHANNEL_CLOSE:
                            buf.getInt();
                            buf.getShort();
                            i = buf.getInt();
                            channel = Channel.FindChannel(i, this);
                            if (channel != null)
                                channel.disconnect();
                            break;
                        case SSH_MSG_CHANNEL_OPEN_CONFIRMATION:
                            buf.getInt();
                            buf.getShort();
                            i = buf.getInt();
                            channel = Channel.FindChannel(i, this);

                            channel.Recipient = buf.getInt();
                            channel.RemoteWindowSize = buf.getInt();
                            channel.RemotePacketSize = buf.getInt();
                            break;
                        case SSH_MSG_CHANNEL_OPEN_FAILURE:
                            buf.getInt();
                            buf.getShort();
                            i = buf.getInt();
                            channel = Channel.FindChannel(i, this);

                            int reason_code = buf.getInt();
                            channel.ExitStatus = reason_code;
                            channel.IsClosed = true;
                            channel.IsEofRemote = true;
                            channel.Recipient = 0;
                            break;
                        case SSH_MSG_CHANNEL_REQUEST:
                            buf.getInt();
                            buf.getShort();
                            i = buf.getInt();
                            foo = buf.getString();
                            bool reply = (buf.getByte() != 0);
                            channel = Channel.FindChannel(i, this);
                            if (channel != null)
                            {
                                byte reply_type = (byte)SSH_MSG_CHANNEL_FAILURE;
                                if (Util.getStringUTF8(foo) == "exit-status")
                                {
                                    i = buf.getInt();             // exit-status
                                    channel.ExitStatus = i;
                                    reply_type = (byte)SSH_MSG_CHANNEL_SUCCESS;
                                }
                                if (reply)
                                {
                                    packet.reset();
                                    buf.putByte(reply_type);
                                    buf.putInt(channel.Recipient);
                                    write(packet);
                                }
                            }
                            break;
                        case SSH_MSG_CHANNEL_OPEN:
                            buf.getInt();
                            buf.getShort();
                            string ctyp = Util.getStringUTF8(buf.getString());

                            if ("forwarded-tcpip" != ctyp && !("x11" == ctyp && m_x11_forwarding))
                            {
                                Console.WriteLine("Session.run: CHANNEL OPEN " + ctyp);
                                throw new IOException("Session.run: CHANNEL OPEN " + ctyp);
                            }
                            else
                            {
                                channel = Channel.getChannel(ctyp);
                                addChannel(channel);
                                channel.getData(buf);
                                channel.Init();

                                packet.reset();
                                buf.putByte((byte)SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
                                buf.putInt(channel.Recipient);
                                buf.putInt(channel.Id);
                                buf.putInt(channel.LocalWindowSize);
                                buf.putInt(channel.LocalPacketSize);
                                write(packet);
                                Thread tmp = new Thread(channel);
                                tmp.Name = "Channel " + ctyp + " " + m_host;
                                tmp.Start();
                                break;
                            }
                        case SSH_MSG_CHANNEL_SUCCESS:
                            buf.getInt();
                            buf.getShort();
                            i = buf.getInt();
                            channel = Channel.FindChannel(i, this);
                            if (channel == null)
                                break;

                            channel.Replay = 1;
                            break;
                        case SSH_MSG_CHANNEL_FAILURE:
                            buf.getInt();
                            buf.getShort();
                            i = buf.getInt();
                            channel = Channel.FindChannel(i, this);
                            if (channel == null)
                                break;

                            channel.Replay = 0;
                            break;
                        case SSH_MSG_GLOBAL_REQUEST:
                            buf.getInt();
                            buf.getShort();
                            foo = buf.getString();       // request name
                            reply = (buf.getByte() != 0);
                            if (reply)
                            {
                                packet.reset();
                                buf.putByte((byte)SSH_MSG_REQUEST_FAILURE);
                                write(packet);
                            }
                            break;
                        case SSH_MSG_REQUEST_FAILURE:
                        case SSH_MSG_REQUEST_SUCCESS:
                            Thread t = m_grr.getThread();
                            if (t != null)
                            {
                                m_grr.setReply(msgType == SSH_MSG_REQUEST_SUCCESS ? 1 : 0);
                                t.Interrupt();
                            }
                            break;
                        default:
                            throw new IOException("Unknown SSH message type " + msgType);
                    }
                }
            }
            catch (Exception)
            { }

            try
            {
                disconnect();
            }
            catch (NullReferenceException)
            { }
            catch (Exception)
            { }

            m_isConnected = false;
        }
Example #12
0
        // encode will bin invoked in write with synchronization.
        public void encode(Packet packet)
        {
            if (m_deflater != null)
                packet.m_buffer.m_index = m_deflater.compress(packet.m_buffer.m_buffer, 5, packet.m_buffer.m_index);

            if (m_c2scipher != null)
            {
                packet.padding(m_c2scipher.IVSize);
                int pad = packet.m_buffer.m_buffer[4];
                lock (m_random)
                {
                    m_random.fill(packet.m_buffer.m_buffer, packet.m_buffer.m_index - pad, pad);
                }
            }
            else
                packet.padding(8);

            byte[] mac = null;
            if (m_c2smac != null)
            {
                m_c2smac.update(m_seq_o);
                m_c2smac.update(packet.m_buffer.m_buffer, 0, packet.m_buffer.m_index);
                mac = m_c2smac.doFinal();
            }
            if (m_c2scipher != null)
            {
                byte[] buf = packet.m_buffer.m_buffer;
                m_c2scipher.update(buf, 0, packet.m_buffer.m_index, buf, 0);
            }
            if (mac != null)
                packet.m_buffer.putByte(mac);
        }
Example #13
0
 private void _write(Packet packet)
 {
     encode(packet);
     if (m_io != null)
     {
         m_io.put(packet);
         m_seq_o++;
     }
 }
Example #14
0
        private void setPortForwarding(int rport)
        {
            lock (m_grr)
            {
                Buffer buf = new Buffer(100); // ??
                Packet packet = new Packet(buf);

                try
                {
                    packet.reset();
                    buf.putByte((byte)SSH_MSG_GLOBAL_REQUEST);
                    buf.putString("tcpip-forward");
                    buf.putByte((byte)1);
                    buf.putString("0.0.0.0");
                    buf.putInt(rport);
                    write(packet);
                }
                catch (Exception e)
                {
                    throw new JSchException(e.ToString());
                }

                m_grr.setThread(Thread.currentThread());
                try
                {
                    Thread.Sleep(10000);
                }
                catch { }

                int reply = m_grr.getReply();
                m_grr.setThread(null);
                if (reply == 0)
                    throw new JSchException("remote port forwarding failed for listen port " + rport);
            }
        }
Example #15
0
        public void write(Packet packet)
        {
            while (m_in_kex)
            {
                byte command = packet.m_buffer.m_buffer[5];
                if (false
                || command == SSH_MSG_KEXINIT
                || command == SSH_MSG_NEWKEYS
                || command == SSH_MSG_KEXDH_INIT
                || command == SSH_MSG_KEXDH_REPLY
                || command == SSH_MSG_DISCONNECT
                || command == SSH_MSG_KEX_DH_GEX_GROUP
                || command == SSH_MSG_KEX_DH_GEX_INIT
                || command == SSH_MSG_KEX_DH_GEX_REPLY
                || command == SSH_MSG_KEX_DH_GEX_REQUEST
                    )
                    break;

                try
                {
                    Thread.Sleep(10);
                }
                catch (ThreadInterruptedException)
                { }
            }
            _write(packet);
        }
Example #16
0
 internal Session(JSch jsch)
 {
     m_jsch = jsch;
     m_buf = new Buffer();
     m_packet = new Packet(m_buf);
 }
Example #17
0
        public virtual void connect()
        {
            if (!m_session.IsConnected())
                throw new JSchException("session is down");
            try
            {
                Buffer buf = new Buffer(100);
                Packet packet = new Packet(buf);

                packet.reset();
                buf.putByte((byte)90);
                buf.putString(m_type);
                buf.putInt(m_id);
                buf.putInt(m_lwsize);
                buf.putInt(m_lmpsize);
                m_session.write(packet);

                int retry = 1000;
                while (Recipient == -1
                    && m_session.IsConnected()
                    && retry > 0
                    )
                {
                    try
                    {
                        Thread.sleep(50);
                    }
                    catch (Exception) { }
                    retry--;
                }

                if (!m_session.IsConnected())
                    throw new JSchException("session is down");
                if (retry == 0)
                    throw new JSchException("channel is not opened.");
                m_connected = true;
                start();
            }
            catch (Exception e)
            {
                m_connected = false;
                if (e is JSchException)
                    throw (JSchException)e;
            }
        }
Example #18
0
 public void sendIgnore()
 {
     Buffer buf = new Buffer();
     Packet packet = new Packet(buf);
     packet.reset();
     buf.putByte((byte)SSH_MSG_IGNORE);
     write(packet);
 }
Example #19
0
        public override void start()
        {
            try
            {

                PipedOutputStream pos = new PipedOutputStream();
                m_io.setOutputStream(pos);
                PipedInputStream pis = new ChannelPipedInputStream(pos, 32 * 1024);
                m_io.setInputStream(pis);

                Request request = new RequestSftp();
                request.request(m_session, this);

                m_buffer = new Buffer(m_rmpsize);
                m_packet = new Packet(m_buffer);
                int i = 0;
                int length;
                int type;
                byte[] str;

                // send SSH_FXP_INIT
                sendINIT();

                // receive SSH_FXP_VERSION
                Header header = new Header();
                header = fillHeader(m_buffer, header);
                length = header.Length;
                if (length > MAX_MSG_LENGTH)
                    throw new SftpException(ChannelSftpResult.SSH_FX_FAILURE, "Received message is too long: " + length);

                type = header.HeaderType;             // 2 -> SSH_FXP_VERSION
                m_server_version = header.RID;
                skip(length);

                // send SSH_FXP_REALPATH
                sendREALPATH(Util.getBytesUTF8("."));

                // receive SSH_FXP_NAME
                header = fillHeader(m_buffer, header);
                length = header.Length;
                type = header.HeaderType;           // 104 -> SSH_FXP_NAME
                m_buffer.rewind();
                fill(m_buffer.m_buffer, 0, length);
                i = m_buffer.getInt();              // count
                str = m_buffer.getString();         // filename
                m_home = m_cwd = Util.getStringUTF8(str);
                str = m_buffer.getString();         // logname
                // SftpATTRS.getATTR(buf);			// attrs
                m_lcwd = new File(".").CanonicalPath;
            }
            catch (Exception e)
            {
                if (e is JSchException)
                    throw (JSchException)e;
                throw new JSchException(e.ToString());
            }
        }
Example #20
0
        internal static void delPort(Session session, int rport)
        {
            lock (m_pool)
            {
                Object[] foo = null;
                for (int i = 0; i < m_pool.Count; i++)
                {
                    Object[] bar = (Object[])(m_pool[i]);
                    if (bar[0] != session)
                        continue;
                    if ((int)bar[1] != rport)
                        continue;
                    foo = bar;
                    break;
                }
                if (foo == null)
                    return;
                m_pool.Remove(foo);
            }

            Buffer buf = new Buffer(100);
            Packet packet = new Packet(buf);

            try
            {
                packet.reset();
                buf.putByte((byte)80 /*SSH_MSG_GLOBAL_REQUEST*/);
                buf.putString("cancel-tcpip-forward");
                buf.putByte((byte)0);
                buf.putString("0.0.0.0");
                buf.putInt(rport);
                session.write(packet);
            }
            catch (Exception)
            { }
        }
Example #21
0
        /*
        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()
        {
            if (m_close) return;
            m_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(Recipient);
                m_session.write(packet);
            }
            catch (Exception)
            { }
        }
Example #22
0
        internal virtual void eof()
        {
            // Thread.dumpStack();
            if (m_close || m_eof_local)
                return;
            m_eof_local = true;

            try
            {
                Buffer buf = new Buffer(100);
                Packet packet = new Packet(buf);
                packet.reset();
                buf.putByte((byte)Session.SSH_MSG_CHANNEL_EOF);
                buf.putInt(Recipient);
                m_session.write(packet);
            }
            catch (Exception)
            { }
        }
Example #23
0
 public void sendKeepAliveMsg()
 {
     Buffer buf = new Buffer();
     Packet packet = new Packet(buf);
     packet.reset();
     buf.putByte((byte)SSH_MSG_GLOBAL_REQUEST);
     buf.putString(m_keepalivemsg);
     buf.putByte((byte)1);
     write(packet);
 }
Example #24
0
File: IO.cs Project: x893/SharpSSH
 public void put(Packet p)
 {
     m_outs.Write(p.m_buffer.m_buffer, 0, p.m_buffer.m_index);
     m_outs.Flush();
 }
Example #25
0
        public void write(Packet packet, Channel c, int length)
        {
            while (true)
            {
                if (m_in_kex)
                {
                    try
                    {
                        Thread.Sleep(10);
                    }
                    catch (ThreadInterruptedException)
                    { }
                    continue;
                }
                lock (c)
                {
                    if (c.RemoteWindowSize >= length)
                    {
                        c.RemoteWindowSize -= length;
                        break;
                    }
                }

                if (c.IsClosed || !c.isConnected())
                    throw new IOException("channel is broken");

                bool sendit = false;
                int s = 0;
                byte command = 0;
                int recipient = -1;
                lock (c)
                {
                    if (c.RemoteWindowSize > 0)
                    {
                        int len = c.RemoteWindowSize;
                        if (len > length)
                            len = length;

                        if (len != length)
                            s = packet.shift(len, (m_c2smac != null ? m_c2smac.BlockSize : 0));

                        command = packet.m_buffer.m_buffer[5];
                        recipient = c.Recipient;
                        length -= len;
                        c.RemoteWindowSize -= len;
                        sendit = true;
                    }
                }
                if (sendit)
                {
                    _write(packet);
                    if (length == 0)
                        return;

                    packet.unshift(command, recipient, s, length);
                    lock (c)
                    {
                        if (c.RemoteWindowSize >= length)
                        {
                            c.RemoteWindowSize -= length;
                            break;
                        }
                    }
                }

                try
                {
                    Thread.Sleep(100);
                }
                catch (ThreadInterruptedException)
                { }
            }
            _write(packet);
        }
Example #26
0
 /*
 SSH_FX_OK
     Indicates successful completion of the operation.
 SSH_FX_EOF
     indicates end-of-file condition; for SSH_FX_READ it means that no
     more data is available in the file, and for SSH_FX_READDIR it
     indicates that no more files are contained in the directory.
 SSH_FX_NO_SUCH_FILE
     is returned when a reference is made to a file which should exist
     but doesn't.
 SSH_FX_PERMISSION_DENIED
     is returned when the authenticated user does not have sufficient
     permissions to perform the operation.
 SSH_FX_FAILURE
     is a generic catch-all error message; it should be returned if an
     error occurs for which there is no more specific error code
     defined.
 SSH_FX_BAD_MESSAGE
     may be returned if a badly formatted packet or protocol
     incompatibility is detected.
 SSH_FX_NO_CONNECTION
     is a pseudo-error which indicates that the client has no
     connection to the server (it can only be generated locally by the
     client, and MUST NOT be returned by servers).
 SSH_FX_CONNECTION_LOST
     is a pseudo-error which indicates that the connection to the
     server has been lost (it can only be generated locally by the
     client, and MUST NOT be returned by servers).
 SSH_FX_OP_UNSUPPORTED
     indicates that an attempt was made to perform an operation which
     is not supported for the server (it may be generated locally by
     the client if e.g.  the version number exchange indicates that a
     required feature is not supported by the server, or it may be
     returned by the server if the server does not implement an
     operation).
 */
 /*
 10. Changes from previous protocol versions
 The SSH File Transfer Protocol has changed over time, before it's
 standardization.  The following is a description of the incompatible
 changes between different versions.
 10.1 Changes between versions 3 and 2
 o  The SSH_FXP_READLINK and SSH_FXP_SYMLINK messages were added.
 o  The SSH_FXP_EXTENDED and SSH_FXP_EXTENDED_REPLY messages were added.
 o  The SSH_FXP_STATUS message was changed to include fields `error
 message' and `language tag'.
 10.2 Changes between versions 2 and 1
 o  The SSH_FXP_RENAME message was added.
 10.3 Changes between versions 1 and 0
 o  Implementation changes, no actual protocol changes.
 */
 internal ChannelSftp()
 {
     m_packet = new Packet(m_buffer);
 }