Beispiel #1
0
        internal void InternalDisconnect()
        {
#if DEBUG
            System.Diagnostics.Trace.WriteLine("Performing disconnection");
#endif
            try
            {
                transport.Close();
            }
            catch (IOException ex)
            {
#if DEBUG
                System.Diagnostics.Trace.WriteLine("Exception during transport.Close()");
                System.Diagnostics.Trace.WriteLine(ex.StackTrace);
#endif
            }
            finally
            {
                if (currentState != TransportProtocolState.DISCONNECTED)
                {
                    FireStateChange(TransportProtocolState.DISCONNECTED);
                }
                currentState = TransportProtocolState.DISCONNECTED;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Determine the version of a server.
        /// </summary>
        /// <param name="transport">A newly created transport connection</param>
        /// <returns></returns>
        public int DetermineVersion(SSHTransport transport)
        {
            int version = SelectVersion(GetRemoteIdentification(transport));

            try
            {
                transport.Close();
            }
            catch (System.IO.IOException)
            {
            }
            return(version);
        }
Beispiel #3
0
        /// <summary>
        /// Create a new connection to an SSH server. This method takes a newly created <see cref="Maverick.SSH.SSHTransport"/>
        /// instance and performs the initial protocol negotiation to determine which type of client to
        /// create.
        /// </summary>
        /// <param name="transport">The transport layer to use</param>
        /// <param name="username">The name of the user making the connection</param>
        /// <param name="threaded">Should the client buffer input to ensure the smooth running of events. If you do not buffer input
        /// and require the use of state or data events they may not operate correctly as the API will operate in single threaded
        /// mode.</param>
        /// <param name="context">The configuration context to use for the connection</param>
        /// <param name="events">A delegate state listener for receiving state change events</param>
        /// <returns></returns>
        internal SSHClient Connect(SSHTransport transport,
                                   System.String username, bool threaded, SSHContext context, SSHStateListener events)
        {
            System.String localIdentification;
            System.String remoteIdentification = GetRemoteIdentification(transport);
            // Lets try SSH2 first because its a better protocol
            SSHClient client;

            System.Exception lastError       = null;
            bool             pointOfNoReturn = false;

            Stream stream = transport.GetStream();

            if ((ssh2Context != null || (context != null &&
                                         context is SSH2Context)) &&
                (supportedVersions & SSH2) != 0)
            {
                //			Send our identification depending upon which versions we can support
                if ((SelectVersion(remoteIdentification) & SSH2) != 0)
                {
                    try
                    {
                        if (context == null)
                        {
                            context = ssh2Context;
                        }

                        client = (SSHClient) new SSH2Client();
                        localIdentification = "SSH-2.0-" + softwareComments + "\n";
                        pointOfNoReturn     = true;
                        byte[] tempArray = SupportClass.ToByteArray(localIdentification);
                        stream.Write(tempArray, 0, tempArray.Length);

                        client.StateChange += events;
                        // Now get the client to connect using the selected protocol
                        client.Connect(transport,
                                       context,
                                       this,
                                       username,
                                       localIdentification.Trim(),
                                       remoteIdentification,
                                       threaded);
                        return(client);
                    }
                    catch (System.Exception t)
                    {
                        lastError = t;
                    }
                    finally
                    {
                        if (lastError != null && pointOfNoReturn)
                        {
                            if (lastError is SSHException)
                            {
                                throw (SSHException)lastError;
                            }
                            else
                            {
                                throw new SSHException(lastError.Message != null?lastError.Message:lastError.GetType().FullName, SSHException.CONNECT_FAILED);
                            }
                        }
                    }
                }
            }



            try
            {
                transport.Close();
            }
            catch (System.IO.IOException)
            {
            }

            if (lastError == null)
            {
                throw new SSHException("Failed to negotiate a version with the server! " + remoteIdentification, SSHException.CONNECT_FAILED);
            }
            else
            {
                if (lastError is SSHException)
                {
                    throw (SSHException)lastError;
                }
                else
                {
                    throw new SSHException(lastError.Message != null?lastError.Message:lastError.GetType().FullName, SSHException.CONNECT_FAILED);
                }
            }
        }