Beispiel #1
0
        /// <summary>
        /// Requests that the remote computer accepts socket connections and forward them to the local
        /// computer. The <see cref="Maverick.SSH.ForwardingRequestListener"/> provides callback methods to create
        /// the connections and to initialize the tunnel.
        /// </summary>
        /// <param name="bindAddress"></param>
        /// <param name="bindPort"></param>
        /// <param name="hostToConnect"></param>
        /// <param name="portToConnect"></param>
        /// <param name="listener"></param>
        /// <returns></returns>
        public bool RequestRemoteForwarding(String bindAddress,
                                            int bindPort,
                                            String hostToConnect,
                                            int portToConnect,
                                            ForwardingRequestListener listener)
        {
            if (listener == null)
            {
                throw new SSHException(
                          "You must specify a listener to receive connection requests",
                          SSHException.BAD_API_USAGE);
            }

            ByteBuffer baw = new ByteBuffer();

            baw.WriteString(bindAddress);
            baw.WriteInt(bindPort);
            GlobalRequest request = new GlobalRequest("tcpip-forward",
                                                      baw.ToByteArray());

            if (SendGlobalRequest(request, true))
            {
                forwardingListeners.Add((bindAddress + ":" + bindPort),
                                        listener);
                forwardingDestinations.Add((bindAddress + ":" + bindPort),
                                           (hostToConnect + ":" + portToConnect));
                // Setup the forwarding listener
                return(true);
            }
            else
            {
                return(false);
            }
        }
Beispiel #2
0
        internal void  ProcessGlobalRequest(System.String requestname, bool wantreply, byte[] requestdata)
        {
#if DEBUG
            System.Diagnostics.Trace.WriteLine("Processing global request " + requestname);
#endif
            try
            {
                bool          success = false;
                GlobalRequest request = new GlobalRequest(requestname, requestdata);
                if (requesthandlers.ContainsKey(requestname))
                {
#if DEBUG
                    System.Diagnostics.Trace.WriteLine("Found handler for request " + requestname);
#endif
                    success = ((GlobalRequestHandler)requesthandlers[requestname]).ProcessGlobalRequest(request);
                }
#if DEBUG
                else
                {
                    System.Diagnostics.Trace.WriteLine("Cannot find handler for request " + requestname);
                }
#endif
                if (wantreply)
                {
                    SSHPacket packet = GetPacket();

                    if (success)
                    {
                        packet.WriteByte(SSH_MSG_REQUEST_SUCCESS);
                        if (request.Data != null)
                        {
                            packet.WriteBytes(requestdata);
                        }

#if DEBUG
                        System.Diagnostics.Trace.WriteLine("Sending SSH_MSG_REQUEST_SUCCESS");
#endif
                        transport.SendMessage(packet);
                    }
                    else
                    {
                        // Return a response
                        packet.WriteByte(SSH_MSG_REQUEST_FAILURE);
#if DEBUG
                        System.Diagnostics.Trace.WriteLine("Sending SSH_MSG_REQUEST_FAILURE");
#endif

                        transport.SendMessage(packet);
                    }
                }
            }
            catch (System.IO.IOException ex)
            {
                throw new SSHException(ex.Message, SSHException.INTERNAL_ERROR);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Sends a global request.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="wantreply"></param>
        /// <returns></returns>
        public bool SendGlobalRequest(GlobalRequest request, bool wantreply)
        {
            try
            {
                SSHPacket packet = GetPacket();
                packet.WriteByte(SSH_MSG_GLOBAL_REQUEST);
                packet.WriteString(request.Name);
                packet.WriteBool(wantreply);
                if (request.Data != null)
                {
                    packet.WriteBytes(request.Data);
                }

#if DEBUG
                System.Diagnostics.Trace.WriteLine("Sending SSH_MSG_GLOBAL_REQUEST");
                System.Diagnostics.Trace.WriteLine(request.Name);
#endif
                SendMessage(packet);

                if (wantreply)
                {
                    packet = GlobalMessages.NextMessage(GLOBAL_REQUEST_MESSAGES);
                    if (packet.MessageID == SSH_MSG_REQUEST_SUCCESS)
                    {
                        if (packet.Available > 1)
                        {
                            byte[] tmp = new byte[packet.Available];
                            packet.ReadBytes(tmp);
                            request.Data = tmp;
                        }
                        else
                        {
                            request.Data = null;
                        }
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    return(true);
                }
            }
            catch (System.IO.IOException ex)
            {
                throw new SSHException(ex.Message, SSHException.INTERNAL_ERROR);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Requests that the remote computer cancel an existing remote forwarding request.
        /// </summary>
        /// <param name="bindAddress"></param>
        /// <param name="bindPort"></param>
        /// <returns></returns>
        public bool CancelRemoteForwarding(String bindAddress,
                                           int bindPort)
        {
            ByteBuffer baw = new ByteBuffer();

            baw.WriteString(bindAddress);
            baw.WriteInt(bindPort);

            GlobalRequest request = new GlobalRequest("cancel-tcpip-forward",
                                                      baw.ToByteArray());

            if (SendGlobalRequest(request, true))
            {
                forwardingListeners.Remove(bindAddress + ":" + bindPort);
                forwardingDestinations.Remove(bindAddress + ":" + bindPort);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Beispiel #5
0
 /// <summary>
 /// Send a global request
 /// </summary>
 /// <param name="request"></param>
 /// <param name="wantreply"></param>
 /// <returns></returns>
 public bool SendGlobalRequest(GlobalRequest request, bool wantreply)
 {
     VerifyConnection(true);
     return(connection.SendGlobalRequest(request, wantreply));
 }