using System; using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; using System.Threading; using Renci.SshNet.Channels; using Renci.SshNet.Common; using Renci.SshNet.Compression; using Renci.SshNet.Messages; using Renci.SshNet.Messages.Authentication; using Renci.SshNet.Messages.Connection; using Renci.SshNet.Messages.Transport; using Renci.SshNet.Security; using System.Globalization; using System.Linq; using Renci.SshNet.Abstractions; using Renci.SshNet.Security.Cryptography; namespace Renci.SshNet { /// /// Provides functionality to connect and interact with SSH server. /// public partial class Session : ISession { private const byte Null = 0x00; private const byte CarriageReturn = 0x0d; private const byte LineFeed = 0x0a; /// /// Specifies an infinite waiting period. /// /// /// The value of this field is -1 millisecond. /// internal static readonly TimeSpan InfiniteTimeSpan = new TimeSpan(0, 0, 0, 0, -1); /// /// Specifies an infinite waiting period. /// /// /// The value of this field is -1. /// internal static readonly int Infinite = -1; /// /// Specifies maximum packet size defined by the protocol. /// private const int MaximumSshPacketSize = LocalChannelDataPacketSize + 3000; /// /// Holds the initial local window size for the channels. /// /// /// 2 MB. /// private const int InitialLocalWindowSize = LocalChannelDataPacketSize * 32; /// /// Holds the maximum size of channel data packets that we receive. /// /// /// 64 KB. /// private const int LocalChannelDataPacketSize = 1024*64; #if FEATURE_REGEX_COMPILE private static readonly Regex ServerVersionRe = new Regex("^SSH-(?[^-]+)-(?.+)( SP.+)?$", RegexOptions.Compiled); #else private static readonly Regex ServerVersionRe = new Regex("^SSH-(?[^-]+)-(?.+)( SP.+)?$"); #endif /// /// Controls how many authentication attempts can take place at the same time. /// /// /// Some server may restrict number to prevent authentication attacks /// private static readonly SemaphoreLight AuthenticationConnection = new SemaphoreLight(3); /// /// Holds metada about session messages /// private SshMessageFactory _sshMessageFactory; /// /// Holds connection socket. /// private Socket _socket; /// /// Holds locker object for the socket /// private readonly object _socketLock = new object(); /// /// Holds a that is signaled when the message listener loop has completed. /// private EventWaitHandle _messageListenerCompleted; /// /// Specifies outbound packet number /// private volatile uint _outboundPacketSequence; /// /// Specifies incoming packet number /// private uint _inboundPacketSequence; /// /// WaitHandle to signal that last service request was accepted /// private EventWaitHandle _serviceAccepted = new AutoResetEvent(false); /// /// WaitHandle to signal that exception was thrown by another thread. /// private EventWaitHandle _exceptionWaitHandle = new ManualResetEvent(false); /// /// WaitHandle to signal that key exchange was completed. /// private EventWaitHandle _keyExchangeCompletedWaitHandle = new ManualResetEvent(false); /// /// WaitHandle to signal that bytes have been read from the socket. /// private EventWaitHandle _bytesReadFromSocket = new ManualResetEvent(false); /// /// WaitHandle to signal that key exchange is in progress. /// private bool _keyExchangeInProgress; /// /// Exception that need to be thrown by waiting thread /// private Exception _exception; /// /// Specifies whether connection is authenticated /// private bool _isAuthenticated; /// /// Specifies whether user issued Disconnect command or not /// private bool _isDisconnecting; private IKeyExchange _keyExchange; private HashAlgorithm _serverMac; private HashAlgorithm _clientMac; private Cipher _clientCipher; private Cipher _serverCipher; private Compressor _serverDecompression; private Compressor _clientCompression; private SemaphoreLight _sessionSemaphore; /// /// Holds the factory to use for creating new services. /// private readonly IServiceFactory _serviceFactory; /// /// Gets the session semaphore that controls session channels. /// /// /// The session semaphore. /// public SemaphoreLight SessionSemaphore { get { if (_sessionSemaphore == null) { lock (this) { if (_sessionSemaphore == null) { _sessionSemaphore = new SemaphoreLight(ConnectionInfo.MaxSessions); } } } return _sessionSemaphore; } } private bool _isDisconnectMessageSent; private uint _nextChannelNumber; /// /// Gets the next channel number. /// /// /// The next channel number. /// private uint NextChannelNumber { get { uint result; lock (this) { result = _nextChannelNumber++; } return result; } } /// /// Gets a value indicating whether the session is connected. /// /// /// true if the session is connected; otherwise, false. /// /// /// This methods returns true in all but the following cases: /// /// /// The is disposed. /// /// /// The SSH_MSG_DISCONNECT message - which is used to disconnect from the server - has been sent. /// /// /// The client has not been authenticated successfully. /// /// /// The listener thread - which is used to receive messages from the server - has stopped. /// /// /// The socket used to communicate with the server is no longer connected. /// /// /// public bool IsConnected { get { if (_disposed || _isDisconnectMessageSent || !_isAuthenticated) return false; if (_messageListenerCompleted == null || _messageListenerCompleted.WaitOne(0)) return false; var isSocketConnected = false; IsSocketConnected(ref isSocketConnected); return isSocketConnected; } } /// /// Gets the session id. /// /// /// The session id, or null if the client has not been authenticated. /// public byte[] SessionId { get; private set; } private Message _clientInitMessage; /// /// Gets the client init message. /// /// The client init message. public Message ClientInitMessage { get { if (_clientInitMessage == null) { _clientInitMessage = new KeyExchangeInitMessage { KeyExchangeAlgorithms = ConnectionInfo.KeyExchangeAlgorithms.Keys.ToArray(), ServerHostKeyAlgorithms = ConnectionInfo.HostKeyAlgorithms.Keys.ToArray(), EncryptionAlgorithmsClientToServer = ConnectionInfo.Encryptions.Keys.ToArray(), EncryptionAlgorithmsServerToClient = ConnectionInfo.Encryptions.Keys.ToArray(), MacAlgorithmsClientToServer = ConnectionInfo.HmacAlgorithms.Keys.ToArray(), MacAlgorithmsServerToClient = ConnectionInfo.HmacAlgorithms.Keys.ToArray(), CompressionAlgorithmsClientToServer = ConnectionInfo.CompressionAlgorithms.Keys.ToArray(), CompressionAlgorithmsServerToClient = ConnectionInfo.CompressionAlgorithms.Keys.ToArray(), LanguagesClientToServer = new[] {string.Empty}, LanguagesServerToClient = new[] {string.Empty}, FirstKexPacketFollows = false, Reserved = 0 }; } return _clientInitMessage; } } /// /// Gets or sets the server version string. /// /// The server version. public string ServerVersion { get; private set; } /// /// Gets or sets the client version string. /// /// The client version. public string ClientVersion { get; private set; } /// /// Gets or sets the connection info. /// /// The connection info. public ConnectionInfo ConnectionInfo { get; private set; } /// /// Occurs when an error occurred. /// public event EventHandler ErrorOccured; /// /// Occurs when session has been disconnected from the server. /// public event EventHandler Disconnected; /// /// Occurs when host key received. /// public event EventHandler HostKeyReceived; /// /// Occurs when message is received from the server. /// public event EventHandler> UserAuthenticationBannerReceived; #region Message events /// /// Occurs when message received /// internal event EventHandler> DisconnectReceived; /// /// Occurs when message received /// internal event EventHandler> IgnoreReceived; /// /// Occurs when message received /// internal event EventHandler> UnimplementedReceived; /// /// Occurs when message received /// internal event EventHandler> DebugReceived; /// /// Occurs when message received /// internal event EventHandler> ServiceRequestReceived; /// /// Occurs when message received /// internal event EventHandler> ServiceAcceptReceived; /// /// Occurs when message received /// internal event EventHandler> KeyExchangeInitReceived; /// /// Occurs when message received /// internal event EventHandler> NewKeysReceived; /// /// Occurs when message received /// internal event EventHandler> UserAuthenticationRequestReceived; /// /// Occurs when message received /// internal event EventHandler> UserAuthenticationFailureReceived; /// /// Occurs when message received /// internal event EventHandler> UserAuthenticationSuccessReceived; /// /// Occurs when message received /// internal event EventHandler> GlobalRequestReceived; /// /// Occurs when message received /// public event EventHandler> RequestSuccessReceived; /// /// Occurs when message received /// public event EventHandler> RequestFailureReceived; /// /// Occurs when message received /// public event EventHandler> ChannelOpenReceived; /// /// Occurs when message received /// public event EventHandler> ChannelOpenConfirmationReceived; /// /// Occurs when message received /// public event EventHandler> ChannelOpenFailureReceived; /// /// Occurs when message received /// public event EventHandler> ChannelWindowAdjustReceived; /// /// Occurs when message received /// public event EventHandler> ChannelDataReceived; /// /// Occurs when message received /// public event EventHandler> ChannelExtendedDataReceived; /// /// Occurs when message received /// public event EventHandler> ChannelEofReceived; /// /// Occurs when message received /// public event EventHandler> ChannelCloseReceived; /// /// Occurs when message received /// public event EventHandler> ChannelRequestReceived; /// /// Occurs when message received /// public event EventHandler> ChannelSuccessReceived; /// /// Occurs when message received /// public event EventHandler> ChannelFailureReceived; /// /// Occurs when message received and is not handled by any of the event handlers /// internal event EventHandler> MessageReceived; #endregion /// /// Initializes a new instance of the class. /// /// The connection info. /// The factory to use for creating new services. /// is null. /// is null. internal Session(ConnectionInfo connectionInfo, IServiceFactory serviceFactory) { if (connectionInfo == null) throw new ArgumentNullException("connectionInfo"); if (serviceFactory == null) throw new ArgumentNullException("serviceFactory"); ClientVersion = "SSH-2.0-Renci.SshNet.SshClient.0.0.1"; ConnectionInfo = connectionInfo; _serviceFactory = serviceFactory; _messageListenerCompleted = new ManualResetEvent(true); } /// /// Connects to the server. /// /// Socket connection to the SSH server or proxy server could not be established, or an error occurred while resolving the hostname. /// SSH session could not be established. /// Authentication of SSH session failed. /// Failed to establish proxy connection. public void Connect() { if (IsConnected) return; try { AuthenticationConnection.Wait(); if (IsConnected) return; lock (this) { // If connected don't connect again if (IsConnected) return; // reset connection specific information Reset(); // Build list of available messages while connecting _sshMessageFactory = new SshMessageFactory(); switch (ConnectionInfo.ProxyType) { case ProxyTypes.None: SocketConnect(ConnectionInfo.Host, ConnectionInfo.Port); break; case ProxyTypes.Socks4: SocketConnect(ConnectionInfo.ProxyHost, ConnectionInfo.ProxyPort); ConnectSocks4(); break; case ProxyTypes.Socks5: SocketConnect(ConnectionInfo.ProxyHost, ConnectionInfo.ProxyPort); ConnectSocks5(); break; case ProxyTypes.Http: SocketConnect(ConnectionInfo.ProxyHost, ConnectionInfo.ProxyPort); ConnectHttp(); break; } Match versionMatch; // Get server version from the server, // ignore text lines which are sent before if any while (true) { var serverVersion = SocketReadLine(ConnectionInfo.Timeout); if (serverVersion == null) throw new SshConnectionException("Server response does not contain SSH protocol identification.", DisconnectReason.ProtocolError); versionMatch = ServerVersionRe.Match(serverVersion); if (versionMatch.Success) { ServerVersion = serverVersion; break; } } // Set connection versions ConnectionInfo.ServerVersion = ServerVersion; ConnectionInfo.ClientVersion = ClientVersion; // Get server SSH version var version = versionMatch.Result("${protoversion}"); var softwareName = versionMatch.Result("${softwareversion}"); DiagnosticAbstraction.Log(string.Format("Server version '{0}' on '{1}'.", version, softwareName)); if (!(version.Equals("2.0") || version.Equals("1.99"))) { throw new SshConnectionException(string.Format(CultureInfo.CurrentCulture, "Server version '{0}' is not supported.", version), DisconnectReason.ProtocolVersionNotSupported); } SocketAbstraction.Send(_socket, Encoding.UTF8.GetBytes(string.Format(CultureInfo.InvariantCulture, "{0}\x0D\x0A", ClientVersion))); // Register Transport response messages RegisterMessage("SSH_MSG_DISCONNECT"); RegisterMessage("SSH_MSG_IGNORE"); RegisterMessage("SSH_MSG_UNIMPLEMENTED"); RegisterMessage("SSH_MSG_DEBUG"); RegisterMessage("SSH_MSG_SERVICE_ACCEPT"); RegisterMessage("SSH_MSG_KEXINIT"); RegisterMessage("SSH_MSG_NEWKEYS"); // Some server implementations might sent this message first, prior establishing encryption algorithm RegisterMessage("SSH_MSG_USERAUTH_BANNER"); // mark the message listener threads as started _messageListenerCompleted.Reset(); // Start incoming request listener ThreadAbstraction.ExecuteThread(MessageListener); // Wait for key exchange to be completed WaitOnHandle(_keyExchangeCompletedWaitHandle); // If sessionId is not set then its not connected if (SessionId == null) { Disconnect(); return; } // Request user authorization service SendMessage(new ServiceRequestMessage(ServiceName.UserAuthentication)); // Wait for service to be accepted WaitOnHandle(_serviceAccepted); if (string.IsNullOrEmpty(ConnectionInfo.Username)) { throw new SshException("Username is not specified."); } // Some servers send a global request immediately after successful authentication // Avoid race condition by already enabling SSH_MSG_GLOBAL_REQUEST before authentication RegisterMessage("SSH_MSG_GLOBAL_REQUEST"); ConnectionInfo.Authenticate(this, _serviceFactory); _isAuthenticated = true; // Register Connection messages RegisterMessage("SSH_MSG_REQUEST_SUCCESS"); RegisterMessage("SSH_MSG_REQUEST_FAILURE"); RegisterMessage("SSH_MSG_CHANNEL_OPEN_CONFIRMATION"); RegisterMessage("SSH_MSG_CHANNEL_OPEN_FAILURE"); RegisterMessage("SSH_MSG_CHANNEL_WINDOW_ADJUST"); RegisterMessage("SSH_MSG_CHANNEL_EXTENDED_DATA"); RegisterMessage("SSH_MSG_CHANNEL_REQUEST"); RegisterMessage("SSH_MSG_CHANNEL_SUCCESS"); RegisterMessage("SSH_MSG_CHANNEL_FAILURE"); RegisterMessage("SSH_MSG_CHANNEL_DATA"); RegisterMessage("SSH_MSG_CHANNEL_EOF"); RegisterMessage("SSH_MSG_CHANNEL_CLOSE"); Monitor.Pulse(this); } } finally { AuthenticationConnection.Release(); } } /// /// Disconnects from the server. /// /// /// This sends a SSH_MSG_DISCONNECT message to the server, waits for the /// server to close the socket on its end and subsequently closes the client socket. /// public void Disconnect() { Disconnect(DisconnectReason.ByApplication, "Connection terminated by the client."); // at this point, we are sure that the listener thread will stop as we've // disconnected the socket, so lets wait until the message listener thread // has completed if (_messageListenerCompleted != null) { _messageListenerCompleted.WaitOne(); } } private void Disconnect(DisconnectReason reason, string message) { _isDisconnecting = true; // send disconnect message to the server if the connection is still open // and the disconnect message has not yet been sent // // note that this should also cause the listener thread to be stopped as // the server should respond by closing the socket if (reason == DisconnectReason.ByApplication) { SendDisconnect(reason, message); } // disconnect socket, and dispose it SocketDisconnectAndDispose(); } /// /// Waits for the specified handle or the exception handle for the receive thread /// to signal within the connection timeout. /// /// The wait handle. /// A received package was invalid or failed the message integrity check. /// None of the handles are signaled in time and the session is not disconnecting. /// A socket error was signaled while receiving messages from the server. /// /// When neither handles are signaled in time and the session is not closing, then the /// session is disconnected. /// void ISession.WaitOnHandle(WaitHandle waitHandle) { WaitOnHandle(waitHandle, ConnectionInfo.Timeout); } /// /// Waits for the specified handle or the exception handle for the receive thread /// to signal within the connection timeout. /// /// The wait handle. /// A received package was invalid or failed the message integrity check. /// None of the handles are signaled in time and the session is not disconnecting. /// A socket error was signaled while receiving messages from the server. /// /// When neither handles are signaled in time and the session is not closing, then the /// session is disconnected. /// internal void WaitOnHandle(WaitHandle waitHandle) { WaitOnHandle(waitHandle, ConnectionInfo.Timeout); } /// /// Waits for the specified handle or the exception handle for the receive thread /// to signal within the specified timeout. /// /// The wait handle. /// The time to wait for any of the handles to become signaled. /// A received package was invalid or failed the message integrity check. /// None of the handles are signaled in time and the session is not disconnecting. /// A socket error was signaled while receiving messages from the server. internal void WaitOnHandle(WaitHandle waitHandle, TimeSpan timeout) { if (waitHandle == null) throw new ArgumentNullException("waitHandle"); var waitHandles = new[] { _exceptionWaitHandle, _messageListenerCompleted, waitHandle }; switch (WaitHandle.WaitAny(waitHandles, timeout)) { case 0: throw _exception; case 1: throw new SshConnectionException("Client not connected."); case WaitHandle.WaitTimeout: // when the session is disconnecting, a timeout is likely when no // network connectivity is available; depending on the configured // timeout either the WaitAny times out first or a SocketException // detailing a timeout thrown hereby completing the listener thread // (which makes us end up in case 1). Either way, we do not want to // report an exception to the client when we're disconnecting anyway if (!_isDisconnecting) { throw new SshOperationTimeoutException("Session operation has timed out"); } break; } } /// /// Sends a message to the server. /// /// The message to send. /// The client is not connected. /// The operation timed out. /// The size of the packet exceeds the maximum size defined by the protocol. internal void SendMessage(Message message) { if (_socket == null || !_socket.CanWrite()) throw new SshConnectionException("Client not connected."); if (_keyExchangeInProgress && !(message is IKeyExchangedAllowed)) { // Wait for key exchange to be completed WaitOnHandle(_keyExchangeCompletedWaitHandle); } DiagnosticAbstraction.Log(string.Format("SendMessage to server '{0}': '{1}'.", message.GetType().Name, message)); // Messages can be sent by different thread so we need to synchronize it var paddingMultiplier = _clientCipher == null ? (byte)8 : Math.Max((byte)8, _serverCipher.MinimumSize); // Should be recalculate base on cipher min length if cipher specified var packetData = message.GetPacket(paddingMultiplier, _clientCompression); // Lock handling of _outboundPacketSequence since it must be sent sequently to server lock (_socketLock) { if (_socket == null || !_socket.Connected) throw new SshConnectionException("Client not connected."); byte[] hash = null; var packetDataOffset = 4; // first four bytes are reserved for outbound packet sequence if (_clientMac != null) { // write outbound packet sequence to start of packet data _outboundPacketSequence.Write(packetData, 0); // calculate packet hash hash = _clientMac.ComputeHash(packetData); } // Encrypt packet data if (_clientCipher != null) { packetData = _clientCipher.Encrypt(packetData, packetDataOffset, (packetData.Length - packetDataOffset)); packetDataOffset = 0; } if (packetData.Length > MaximumSshPacketSize) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Packet is too big. Maximum packet size is {0} bytes.", MaximumSshPacketSize)); } var packetLength = packetData.Length - packetDataOffset; if (hash == null) { SocketAbstraction.Send(_socket, packetData, packetDataOffset, packetLength); } else { var data = new byte[packetLength + (_clientMac.HashSize / 8)]; Buffer.BlockCopy(packetData, packetDataOffset, data, 0, packetLength); Buffer.BlockCopy(hash, 0, data, packetLength, hash.Length); SocketAbstraction.Send(_socket, data, 0, data.Length); } _outboundPacketSequence++; Monitor.Pulse(_socketLock); } } /// /// Sends a message to the server. /// /// The message to send. /// /// true if the message was sent to the server; otherwise, false. /// /// The size of the packet exceeds the maximum size defined by the protocol. /// /// This methods returns false when the attempt to send the message results in a /// or a . /// private bool TrySendMessage(Message message) { try { SendMessage(message); return true; } catch (SshException ex) { DiagnosticAbstraction.Log(string.Format("Failure sending message server '{0}': '{1}' => {2}", message.GetType().Name, message, ex)); return false; } catch (SocketException ex) { DiagnosticAbstraction.Log(string.Format("Failure sending message server '{0}': '{1}' => {2}", message.GetType().Name, message, ex)); return false; } } /// /// Receives the message from the server. /// /// Incoming SSH message. /// private Message ReceiveMessage() { const int inboundPacketSequenceLength = 4; // No lock needed since all messages read by only one thread var blockSize = _serverCipher == null ? (byte)8 : Math.Max((byte)8, _serverCipher.MinimumSize); // Read packet length first var firstBlock = Read(blockSize); if (_serverCipher != null) { firstBlock = _serverCipher.Decrypt(firstBlock); } var packetLength = (uint)(firstBlock[0] << 24 | firstBlock[1] << 16 | firstBlock[2] << 8 | firstBlock[3]); // Test packet minimum and maximum boundaries if (packetLength < Math.Max((byte)16, blockSize) - 4 || packetLength > MaximumSshPacketSize - 4) throw new SshConnectionException(string.Format(CultureInfo.CurrentCulture, "Bad packet length: {0}.", packetLength), DisconnectReason.ProtocolError); // Read rest of the packet data var bytesToRead = (int)(packetLength - (blockSize - 4)); var data = new byte[bytesToRead + blockSize + inboundPacketSequenceLength]; _inboundPacketSequence.Write(data, 0); Buffer.BlockCopy(firstBlock, 0, data, inboundPacketSequenceLength, firstBlock.Length); byte[] serverHash = null; if (_serverMac != null) { serverHash = new byte[_serverMac.HashSize / 8]; bytesToRead += serverHash.Length; } if (bytesToRead > 0) { var nextBlocks = Read(bytesToRead); if (serverHash != null) { Buffer.BlockCopy(nextBlocks, nextBlocks.Length - serverHash.Length, serverHash, 0, serverHash.Length); nextBlocks = nextBlocks.Take(nextBlocks.Length - serverHash.Length); } if (nextBlocks.Length > 0) { if (_serverCipher != null) { nextBlocks = _serverCipher.Decrypt(nextBlocks); } nextBlocks.CopyTo(data, blockSize + inboundPacketSequenceLength); } } var paddingLength = data[inboundPacketSequenceLength + 4]; var messagePayloadLength = (int) (packetLength - paddingLength - 1); const int messagePayloadOffset = inboundPacketSequenceLength + 4 + 1; // Validate message against MAC if (_serverMac != null) { var clientHash = _serverMac.ComputeHash(data); if (!serverHash.IsEqualTo(clientHash)) { throw new SshConnectionException("MAC error", DisconnectReason.MacError); } } if (_serverDecompression != null) { data = _serverDecompression.Decompress(data, inboundPacketSequenceLength + 4 + 1, messagePayloadLength); } _inboundPacketSequence++; return LoadMessage(data, messagePayloadOffset); } private void SendDisconnect(DisconnectReason reasonCode, string message) { // only send a disconnect message if it wasn't already sent, and we're // still connected if (_isDisconnectMessageSent || !IsConnected) return; var disconnectMessage = new DisconnectMessage(reasonCode, message); // send the disconnect message, but ignore the outcome TrySendMessage(disconnectMessage); _isDisconnectMessageSent = true; } partial void HandleMessageCore(Message message); /// /// Handles the message. /// /// /// The message. private void HandleMessage(T message) where T : Message { OnMessageReceived(message); } #region Handle transport messages /// /// Invoked via reflection. /// private void HandleMessage(DisconnectMessage message) { OnDisconnectReceived(message); Disconnect(message.ReasonCode, message.Description); } /// /// Invoked via reflection. /// private void HandleMessage(IgnoreMessage message) { OnIgnoreReceived(message); } /// /// Invoked via reflection. /// private void HandleMessage(UnimplementedMessage message) { OnUnimplementedReceived(message); } /// /// Invoked via reflection. /// private void HandleMessage(DebugMessage message) { OnDebugReceived(message); } /// /// Invoked via reflection. /// private void HandleMessage(ServiceRequestMessage message) { OnServiceRequestReceived(message); } /// /// Invoked via reflection. /// private void HandleMessage(ServiceAcceptMessage message) { // TODO: Refactor to avoid this method here OnServiceAcceptReceived(message); _serviceAccepted.Set(); } /// /// Invoked via reflection. /// private void HandleMessage(KeyExchangeInitMessage message) { OnKeyExchangeInitReceived(message); } /// /// Invoked via reflection. /// private void HandleMessage(NewKeysMessage message) { OnNewKeysReceived(message); } #endregion #region Handle User Authentication messages /// /// Invoked via reflection. /// private void HandleMessage(RequestMessage message) { OnUserAuthenticationRequestReceived(message); } /// /// Invoked via reflection. /// private void HandleMessage(FailureMessage message) { OnUserAuthenticationFailureReceived(message); } /// /// Invoked via reflection. /// private void HandleMessage(SuccessMessage message) { OnUserAuthenticationSuccessReceived(message); } /// /// Invoked via reflection. /// private void HandleMessage(BannerMessage message) { OnUserAuthenticationBannerReceived(message); } #endregion #region Handle connection messages /// /// Invoked via reflection. /// private void HandleMessage(GlobalRequestMessage message) { OnGlobalRequestReceived(message); } /// /// Invoked via reflection. /// private void HandleMessage(RequestSuccessMessage message) { OnRequestSuccessReceived(message); } /// /// Invoked via reflection. /// private void HandleMessage(RequestFailureMessage message) { OnRequestFailureReceived(message); } /// /// Invoked via reflection. /// private void HandleMessage(ChannelOpenMessage message) { OnChannelOpenReceived(message); } /// /// Invoked via reflection. /// private void HandleMessage(ChannelOpenConfirmationMessage message) { OnChannelOpenConfirmationReceived(message); } /// /// Invoked via reflection. /// private void HandleMessage(ChannelOpenFailureMessage message) { OnChannelOpenFailureReceived(message); } /// /// Invoked via reflection. /// private void HandleMessage(ChannelWindowAdjustMessage message) { OnChannelWindowAdjustReceived(message); } /// /// Invoked via reflection. /// private void HandleMessage(ChannelDataMessage message) { OnChannelDataReceived(message); } /// /// Invoked via reflection. /// private void HandleMessage(ChannelExtendedDataMessage message) { OnChannelExtendedDataReceived(message); } /// /// Invoked via reflection. /// private void HandleMessage(ChannelEofMessage message) { OnChannelEofReceived(message); } /// /// Invoked via reflection. /// private void HandleMessage(ChannelCloseMessage message) { OnChannelCloseReceived(message); } /// /// Invoked via reflection. /// private void HandleMessage(ChannelRequestMessage message) { OnChannelRequestReceived(message); } /// /// Invoked via reflection. /// private void HandleMessage(ChannelSuccessMessage message) { OnChannelSuccessReceived(message); } /// /// Invoked via reflection. /// private void HandleMessage(ChannelFailureMessage message) { OnChannelFailureReceived(message); } #endregion #region Handle received message events /// /// Called when received. /// /// message. protected virtual void OnDisconnectReceived(DisconnectMessage message) { DiagnosticAbstraction.Log(string.Format("Disconnect received: {0} {1}", message.ReasonCode, message.Description)); _exception = new SshConnectionException(string.Format(CultureInfo.InvariantCulture, "The connection was closed by the server: {0} ({1}).", message.Description, message.ReasonCode), message.ReasonCode); _exceptionWaitHandle.Set(); var disconnectReceived = DisconnectReceived; if (disconnectReceived != null) disconnectReceived(this, new MessageEventArgs(message)); var disconnected = Disconnected; if (disconnected != null) disconnected(this, new EventArgs()); } /// /// Called when received. /// /// message. protected virtual void OnIgnoreReceived(IgnoreMessage message) { var handlers = IgnoreReceived; if (handlers != null) handlers(this, new MessageEventArgs(message)); } /// /// Called when message received. /// /// message. protected virtual void OnUnimplementedReceived(UnimplementedMessage message) { var handlers = UnimplementedReceived; if (handlers != null) handlers(this, new MessageEventArgs(message)); } /// /// Called when message received. /// /// message. protected virtual void OnDebugReceived(DebugMessage message) { var handlers = DebugReceived; if (handlers != null) handlers(this, new MessageEventArgs(message)); } /// /// Called when message received. /// /// message. protected virtual void OnServiceRequestReceived(ServiceRequestMessage message) { var handlers = ServiceRequestReceived; if (handlers != null) handlers(this, new MessageEventArgs(message)); } /// /// Called when message received. /// /// message. protected virtual void OnServiceAcceptReceived(ServiceAcceptMessage message) { var handlers = ServiceAcceptReceived; if (handlers != null) handlers(this, new MessageEventArgs(message)); } /// /// Called when message received. /// /// message. protected virtual void OnKeyExchangeInitReceived(KeyExchangeInitMessage message) { _keyExchangeInProgress = true; _keyExchangeCompletedWaitHandle.Reset(); // Disable messages that are not key exchange related _sshMessageFactory.DisableNonKeyExchangeMessages(); _keyExchange = _serviceFactory.CreateKeyExchange(ConnectionInfo.KeyExchangeAlgorithms, message.KeyExchangeAlgorithms); ConnectionInfo.CurrentKeyExchangeAlgorithm = _keyExchange.Name; _keyExchange.HostKeyReceived += KeyExchange_HostKeyReceived; // Start the algorithm implementation _keyExchange.Start(this, message); var keyExchangeInitReceived = KeyExchangeInitReceived; if (keyExchangeInitReceived != null) keyExchangeInitReceived(this, new MessageEventArgs(message)); } /// /// Called when message received. /// /// message. protected virtual void OnNewKeysReceived(NewKeysMessage message) { // Update sessionId if (SessionId == null) { SessionId = _keyExchange.ExchangeHash; } // Dispose of old ciphers and hash algorithms if (_serverMac != null) { _serverMac.Dispose(); _serverMac = null; } if (_clientMac != null) { _clientMac.Dispose(); _clientMac = null; } // Update negotiated algorithms _serverCipher = _keyExchange.CreateServerCipher(); _clientCipher = _keyExchange.CreateClientCipher(); _serverMac = _keyExchange.CreateServerHash(); _clientMac = _keyExchange.CreateClientHash(); _clientCompression = _keyExchange.CreateCompressor(); _serverDecompression = _keyExchange.CreateDecompressor(); // Dispose of old KeyExchange object as it is no longer needed. if (_keyExchange != null) { _keyExchange.HostKeyReceived -= KeyExchange_HostKeyReceived; _keyExchange.Dispose(); _keyExchange = null; } // Enable activated messages that are not key exchange related _sshMessageFactory.EnableActivatedMessages(); var newKeysReceived = NewKeysReceived; if (newKeysReceived != null) newKeysReceived(this, new MessageEventArgs(message)); // Signal that key exchange completed _keyExchangeCompletedWaitHandle.Set(); _keyExchangeInProgress = false; } /// /// Called when client is disconnecting from the server. /// void ISession.OnDisconnecting() { _isDisconnecting = true; } /// /// Called when message received. /// /// message. protected virtual void OnUserAuthenticationRequestReceived(RequestMessage message) { var handlers = UserAuthenticationRequestReceived; if (handlers != null) handlers(this, new MessageEventArgs(message)); } /// /// Called when message received. /// /// message. protected virtual void OnUserAuthenticationFailureReceived(FailureMessage message) { var handlers = UserAuthenticationFailureReceived; if (handlers != null) handlers(this, new MessageEventArgs(message)); } /// /// Called when message received. /// /// message. protected virtual void OnUserAuthenticationSuccessReceived(SuccessMessage message) { var handlers = UserAuthenticationSuccessReceived; if (handlers != null) handlers(this, new MessageEventArgs(message)); } /// /// Called when message received. /// /// message. protected virtual void OnUserAuthenticationBannerReceived(BannerMessage message) { var handlers = UserAuthenticationBannerReceived; if (handlers != null) handlers(this, new MessageEventArgs(message)); } /// /// Called when message received. /// /// message. protected virtual void OnGlobalRequestReceived(GlobalRequestMessage message) { var handlers = GlobalRequestReceived; if (handlers != null) handlers(this, new MessageEventArgs(message)); } /// /// Called when message received. /// /// message. protected virtual void OnRequestSuccessReceived(RequestSuccessMessage message) { var handlers = RequestSuccessReceived; if (handlers != null) handlers(this, new MessageEventArgs(message)); } /// /// Called when message received. /// /// message. protected virtual void OnRequestFailureReceived(RequestFailureMessage message) { var handlers = RequestFailureReceived; if (handlers != null) handlers(this, new MessageEventArgs(message)); } /// /// Called when message received. /// /// message. protected virtual void OnChannelOpenReceived(ChannelOpenMessage message) { var handlers = ChannelOpenReceived; if (handlers != null) handlers(this, new MessageEventArgs(message)); } /// /// Called when message received. /// /// message. protected virtual void OnChannelOpenConfirmationReceived(ChannelOpenConfirmationMessage message) { var handlers = ChannelOpenConfirmationReceived; if (handlers != null) handlers(this, new MessageEventArgs(message)); } /// /// Called when message received. /// /// message. protected virtual void OnChannelOpenFailureReceived(ChannelOpenFailureMessage message) { var handlers = ChannelOpenFailureReceived; if (handlers != null) handlers(this, new MessageEventArgs(message)); } /// /// Called when message received. /// /// message. protected virtual void OnChannelWindowAdjustReceived(ChannelWindowAdjustMessage message) { var handlers = ChannelWindowAdjustReceived; if (handlers != null) handlers(this, new MessageEventArgs(message)); } /// /// Called when message received. /// /// message. protected virtual void OnChannelDataReceived(ChannelDataMessage message) { var handlers = ChannelDataReceived; if (handlers != null) handlers(this, new MessageEventArgs(message)); } /// /// Called when message received. /// /// message. protected virtual void OnChannelExtendedDataReceived(ChannelExtendedDataMessage message) { var handlers = ChannelExtendedDataReceived; if (handlers != null) handlers(this, new MessageEventArgs(message)); } /// /// Called when message received. /// /// message. protected virtual void OnChannelEofReceived(ChannelEofMessage message) { var handlers = ChannelEofReceived; if (handlers != null) handlers(this, new MessageEventArgs(message)); } /// /// Called when message received. /// /// message. protected virtual void OnChannelCloseReceived(ChannelCloseMessage message) { var handlers = ChannelCloseReceived; if (handlers != null) handlers(this, new MessageEventArgs(message)); } /// /// Called when message received. /// /// message. protected virtual void OnChannelRequestReceived(ChannelRequestMessage message) { var handlers = ChannelRequestReceived; if (handlers != null) handlers(this, new MessageEventArgs(message)); } /// /// Called when message received. /// /// message. protected virtual void OnChannelSuccessReceived(ChannelSuccessMessage message) { var handlers = ChannelSuccessReceived; if (handlers != null) handlers(this, new MessageEventArgs(message)); } /// /// Called when message received. /// /// message. protected virtual void OnChannelFailureReceived(ChannelFailureMessage message) { var handlers = ChannelFailureReceived; if (handlers != null) handlers(this, new MessageEventArgs(message)); } /// /// Called when message received. /// /// message. protected virtual void OnMessageReceived(Message message) { var handlers = MessageReceived; if (handlers != null) handlers(this, new MessageEventArgs(message)); } #endregion private void KeyExchange_HostKeyReceived(object sender, HostKeyEventArgs e) { var handlers = HostKeyReceived; if (handlers != null) handlers(this, e); } /// /// Reads the specified length of bytes from the server. /// /// The length. /// /// The bytes read from the server. /// private byte[] Read(int length) { var buffer = new byte[length]; SocketRead(length, buffer); return buffer; } #region Message loading functions /// /// Registers SSH message with the session. /// /// The name of the message to register with the session. public void RegisterMessage(string messageName) { _sshMessageFactory.EnableAndActivateMessage(messageName); } /// /// Unregister SSH message from the session. /// /// The name of the message to unregister with the session. public void UnRegisterMessage(string messageName) { _sshMessageFactory.DisableAndDeactivateMessage(messageName); } /// /// Loads a message from a given buffer. /// /// An array of bytes from which to construct the message. /// The zero-based byte offset in at which to begin reading. /// /// A message constructed from . /// /// The type of the message is not supported. private Message LoadMessage(byte[] data, int offset) { var messageType = data[offset]; var message = _sshMessageFactory.Create(messageType); message.Load(data, offset); DiagnosticAbstraction.Log(string.Format("ReceiveMessage from server: '{0}': '{1}'.", message.GetType().Name, message)); return message; } #endregion /// /// Gets a value indicating whether the socket is connected. /// /// /// true if the socket is connected; otherwise, false. /// partial void IsSocketConnected(ref bool isConnected); /// /// Establishes a socket connection to the specified host and port. /// /// The host name of the server to connect to. /// The port to connect to. /// The connection failed to establish within the configured . /// An error occurred trying to establish the connection. private void SocketConnect(string host, int port) { var ipAddress = DnsAbstraction.GetHostAddresses(host)[0]; var ep = new IPEndPoint(ipAddress, port); DiagnosticAbstraction.Log(string.Format("Initiating connect to '{0}:{1}'.", host, port)); _socket = SocketAbstraction.Connect(ep, ConnectionInfo.Timeout); const int socketBufferSize = 2 * MaximumSshPacketSize; _socket.SendBufferSize = socketBufferSize; _socket.ReceiveBufferSize = socketBufferSize; } /// /// Performs a blocking read on the socket until bytes are received. /// /// The number of bytes to read. /// The buffer to read to. /// The socket is closed. /// The read has timed-out. /// The read failed. private void SocketRead(int length, byte[] buffer) { if (SocketAbstraction.Read(_socket, buffer, 0, length, InfiniteTimeSpan) > 0) { // signal that bytes have been read from the socket // this is used to improve accuracy of Session.IsSocketConnected _bytesReadFromSocket.Set(); return; } // 2012-09-11: Kenneth_aa // When Disconnect or Dispose is called, this throws SshConnectionException(), which... // 1 - goes up to ReceiveMessage() // 2 - up again to MessageListener() // which is where there is a catch-all exception block so it can notify event listeners. // 3 - MessageListener then again calls RaiseError(). // There the exception is checked for the exception thrown here (ConnectionLost), and if it matches it will not call Session.SendDisconnect(). // // Adding a check for _isDisconnecting causes ReceiveMessage() to throw SshConnectionException: "Bad packet length {0}". // if (_isDisconnecting) throw new SshConnectionException( "An established connection was aborted by the software in your host machine.", DisconnectReason.ConnectionLost); throw new SshConnectionException("An established connection was aborted by the server.", DisconnectReason.ConnectionLost); } /// /// Performs a blocking read on the socket until a line is read. /// /// A that represents the time to wait until a line is read. /// The read has timed-out. /// An error occurred when trying to access the socket. /// /// The line read from the socket, or null when the remote server has shutdown and all data has been received. /// private string SocketReadLine(TimeSpan timeout) { var encoding = SshData.Ascii; var buffer = new List(); var data = new byte[1]; // read data one byte at a time to find end of line and leave any unhandled information in the buffer // to be processed by subsequent invocations do { var bytesRead = SocketAbstraction.Read(_socket, data, 0, data.Length, timeout); if (bytesRead == 0) // the remote server shut down the socket break; buffer.Add(data[0]); } while (!(buffer.Count > 0 && (buffer[buffer.Count - 1] == LineFeed || buffer[buffer.Count - 1] == Null))); if (buffer.Count == 0) return null; if (buffer.Count == 1 && buffer[buffer.Count - 1] == 0x00) // return an empty version string if the buffer consists of only a 0x00 character return string.Empty; if (buffer.Count > 1 && buffer[buffer.Count - 2] == CarriageReturn) // strip trailing CRLF return encoding.GetString(buffer.ToArray(), 0, buffer.Count - 2); if (buffer.Count > 1 && buffer[buffer.Count - 1] == LineFeed) // strip trailing LF return encoding.GetString(buffer.ToArray(), 0, buffer.Count - 1); return encoding.GetString(buffer.ToArray(), 0, buffer.Count); } /// /// Disconnects and disposes the socket. /// private void SocketDisconnectAndDispose() { if (_socket != null) { lock (_socketLock) { if (_socket != null) { if (_socket.Connected) { _socket.Shutdown(SocketShutdown.Send); SocketAbstraction.ClearReadBuffer(_socket); } _socket.Dispose(); _socket = null; } } } } /// /// Listens for incoming message from the server and handles them. This method run as a task on separate thread. /// private void MessageListener() { try { while (_socket != null && _socket.Connected) { var message = ReceiveMessage(); HandleMessageCore(message); } } catch (SocketException ex) { RaiseError(new SshConnectionException(ex.Message, DisconnectReason.ConnectionLost, ex)); } catch (Exception exp) { RaiseError(exp); } finally { // signal that the message listener thread has stopped _messageListenerCompleted.Set(); } } private byte SocketReadByte() { var buffer = new byte[1]; SocketRead(1, buffer); return buffer[0]; } private void SocketWriteByte(byte data) { SocketAbstraction.Send(_socket, new[] {data}); } private void ConnectSocks4() { // Send socks version number SocketWriteByte(0x04); // Send command code SocketWriteByte(0x01); // Send port SocketWriteByte((byte)(ConnectionInfo.Port / 0xFF)); SocketWriteByte((byte)(ConnectionInfo.Port % 0xFF)); // Send IP var ipAddress = DnsAbstraction.GetHostAddresses(ConnectionInfo.Host)[0]; SocketAbstraction.Send(_socket, ipAddress.GetAddressBytes()); // Send username var username = SshData.Ascii.GetBytes(ConnectionInfo.ProxyUsername); SocketAbstraction.Send(_socket, username); SocketWriteByte(0x00); // Read 0 if (SocketReadByte() != 0) { throw new ProxyException("SOCKS4: Null is expected."); } // Read response code var code = SocketReadByte(); switch (code) { case 0x5a: break; case 0x5b: throw new ProxyException("SOCKS4: Connection rejected."); case 0x5c: throw new ProxyException("SOCKS4: Client is not running identd or not reachable from the server."); case 0x5d: throw new ProxyException("SOCKS4: Client's identd could not confirm the user ID string in the request."); default: throw new ProxyException("SOCKS4: Not valid response."); } var dummyBuffer = new byte[4]; // Read 2 bytes to be ignored SocketRead(2, dummyBuffer); // Read 4 bytes to be ignored SocketRead(4, dummyBuffer); } private void ConnectSocks5() { // Send socks version number SocketWriteByte(0x05); // Send number of supported authentication methods SocketWriteByte(0x02); // Send supported authentication methods SocketWriteByte(0x00); // No authentication SocketWriteByte(0x02); // Username/Password var socksVersion = SocketReadByte(); if (socksVersion != 0x05) throw new ProxyException(string.Format("SOCKS Version '{0}' is not supported.", socksVersion)); var authenticationMethod = SocketReadByte(); switch (authenticationMethod) { case 0x00: break; case 0x02: // Send version SocketWriteByte(0x01); var username = SshData.Ascii.GetBytes(ConnectionInfo.ProxyUsername); if (username.Length > byte.MaxValue) throw new ProxyException("Proxy username is too long."); // Send username length SocketWriteByte((byte)username.Length); // Send username SocketAbstraction.Send(_socket, username); var password = SshData.Ascii.GetBytes(ConnectionInfo.ProxyPassword); if (password.Length > byte.MaxValue) throw new ProxyException("Proxy password is too long."); // Send username length SocketWriteByte((byte)password.Length); // Send username SocketAbstraction.Send(_socket, password); var serverVersion = SocketReadByte(); if (serverVersion != 1) throw new ProxyException("SOCKS5: Server authentication version is not valid."); var statusCode = SocketReadByte(); if (statusCode != 0) throw new ProxyException("SOCKS5: Username/Password authentication failed."); break; case 0xFF: throw new ProxyException("SOCKS5: No acceptable authentication methods were offered."); } // Send socks version number SocketWriteByte(0x05); // Send command code SocketWriteByte(0x01); // establish a TCP/IP stream connection // Send reserved, must be 0x00 SocketWriteByte(0x00); var ip = DnsAbstraction.GetHostAddresses(ConnectionInfo.Host)[0]; // Send address type and address if (ip.AddressFamily == AddressFamily.InterNetwork) { SocketWriteByte(0x01); var address = ip.GetAddressBytes(); SocketAbstraction.Send(_socket, address); } else if (ip.AddressFamily == AddressFamily.InterNetworkV6) { SocketWriteByte(0x04); var address = ip.GetAddressBytes(); SocketAbstraction.Send(_socket, address); } else { throw new ProxyException(string.Format("SOCKS5: IP address '{0}' is not supported.", ip)); } // Send port SocketWriteByte((byte)(ConnectionInfo.Port / 0xFF)); SocketWriteByte((byte)(ConnectionInfo.Port % 0xFF)); // Read Server SOCKS5 version if (SocketReadByte() != 5) { throw new ProxyException("SOCKS5: Version 5 is expected."); } // Read response code var status = SocketReadByte(); switch (status) { case 0x00: break; case 0x01: throw new ProxyException("SOCKS5: General failure."); case 0x02: throw new ProxyException("SOCKS5: Connection not allowed by ruleset."); case 0x03: throw new ProxyException("SOCKS5: Network unreachable."); case 0x04: throw new ProxyException("SOCKS5: Host unreachable."); case 0x05: throw new ProxyException("SOCKS5: Connection refused by destination host."); case 0x06: throw new ProxyException("SOCKS5: TTL expired."); case 0x07: throw new ProxyException("SOCKS5: Command not supported or protocol error."); case 0x08: throw new ProxyException("SOCKS5: Address type not supported."); default: throw new ProxyException("SOCKS4: Not valid response."); } // Read 0 if (SocketReadByte() != 0) { throw new ProxyException("SOCKS5: 0 byte is expected."); } var addressType = SocketReadByte(); var responseIp = new byte[16]; switch (addressType) { case 0x01: SocketRead(4, responseIp); break; case 0x04: SocketRead(16, responseIp); break; default: throw new ProxyException(string.Format("Address type '{0}' is not supported.", addressType)); } var port = new byte[2]; // Read 2 bytes to be ignored SocketRead(2, port); } private void ConnectHttp() { var httpResponseRe = new Regex(@"HTTP/(?\d[.]\d) (?\d{3}) (?.+)$"); var httpHeaderRe = new Regex(@"(?[^\[\]()<>@,;:\""/?={} \t]+):(?.+)?"); SocketAbstraction.Send(_socket, SshData.Ascii.GetBytes(string.Format("CONNECT {0}:{1} HTTP/1.0\r\n", ConnectionInfo.Host, ConnectionInfo.Port))); // Sent proxy authorization is specified if (!string.IsNullOrEmpty(ConnectionInfo.ProxyUsername)) { var authorization = string.Format("Proxy-Authorization: Basic {0}\r\n", Convert.ToBase64String(SshData.Ascii.GetBytes(string.Format("{0}:{1}", ConnectionInfo.ProxyUsername, ConnectionInfo.ProxyPassword))) ); SocketAbstraction.Send(_socket, SshData.Ascii.GetBytes(authorization)); } SocketAbstraction.Send(_socket, SshData.Ascii.GetBytes("\r\n")); HttpStatusCode? statusCode = null; var contentLength = 0; while (true) { var response = SocketReadLine(ConnectionInfo.Timeout); if (response == null) // server shut down socket break; if (statusCode == null) { var statusMatch = httpResponseRe.Match(response); if (statusMatch.Success) { var httpStatusCode = statusMatch.Result("${statusCode}"); statusCode = (HttpStatusCode) int.Parse(httpStatusCode); if (statusCode != HttpStatusCode.OK) { var reasonPhrase = statusMatch.Result("${reasonPhrase}"); throw new ProxyException(string.Format("HTTP: Status code {0}, \"{1}\"", httpStatusCode, reasonPhrase)); } } continue; } // continue on parsing message headers coming from the server var headerMatch = httpHeaderRe.Match(response); if (headerMatch.Success) { var fieldName = headerMatch.Result("${fieldName}"); if (fieldName.Equals("Content-Length", StringComparison.OrdinalIgnoreCase)) { contentLength = int.Parse(headerMatch.Result("${fieldValue}")); } continue; } // check if we've reached the CRLF which separates request line and headers from the message body if (response.Length == 0) { // read response body if specified if (contentLength > 0) { var contentBody = new byte[contentLength]; SocketRead(contentLength, contentBody); } break; } } if (statusCode == null) throw new ProxyException("HTTP response does not contain status line."); } /// /// Raises the event. /// /// The exp. private void RaiseError(Exception exp) { var connectionException = exp as SshConnectionException; if (_isDisconnecting) { // a connection exception which is raised while isDisconnecting is normal and // should be ignored if (connectionException != null) return; // any timeout while disconnecting can be caused by loss of connectivity // altogether and should be ignored var socketException = exp as SocketException; if (socketException != null && socketException.SocketErrorCode == SocketError.TimedOut) return; } _exception = exp; _exceptionWaitHandle.Set(); var errorOccured = ErrorOccured; if (errorOccured != null) errorOccured(this, new ExceptionEventArgs(exp)); if (connectionException != null) { Disconnect(connectionException.DisconnectReason, exp.ToString()); } } /// /// Resets connection-specific information to ensure state of a previous connection /// does not affect new connections. /// private void Reset() { if (_exceptionWaitHandle != null) _exceptionWaitHandle.Reset(); if (_keyExchangeCompletedWaitHandle != null) _keyExchangeCompletedWaitHandle.Reset(); if (_messageListenerCompleted != null) _messageListenerCompleted.Set(); SessionId = null; _isDisconnectMessageSent = false; _isDisconnecting = false; _isAuthenticated = false; _exception = null; _keyExchangeInProgress = false; } #region IDisposable implementation private bool _disposed; /// /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } /// /// Releases unmanaged and - optionally - managed resources /// /// true to release both managed and unmanaged resources; false to release only unmanaged resources. protected virtual void Dispose(bool disposing) { if (_disposed) return; if (disposing) { Disconnect(); var serviceAccepted = _serviceAccepted; if (serviceAccepted != null) { serviceAccepted.Dispose(); _serviceAccepted = null; } var exceptionWaitHandle = _exceptionWaitHandle; if (exceptionWaitHandle != null) { exceptionWaitHandle.Dispose(); _exceptionWaitHandle = null; } var keyExchangeCompletedWaitHandle = _keyExchangeCompletedWaitHandle; if (keyExchangeCompletedWaitHandle != null) { keyExchangeCompletedWaitHandle.Dispose(); _keyExchangeCompletedWaitHandle = null; } var serverMac = _serverMac; if (serverMac != null) { serverMac.Dispose(); _serverMac = null; } var clientMac = _clientMac; if (clientMac != null) { clientMac.Dispose(); _clientMac = null; } var keyExchange = _keyExchange; if (keyExchange != null) { keyExchange.HostKeyReceived -= KeyExchange_HostKeyReceived; keyExchange.Dispose(); _keyExchange = null; } var bytesReadFromSocket = _bytesReadFromSocket; if (bytesReadFromSocket != null) { bytesReadFromSocket.Dispose(); _bytesReadFromSocket = null; } var messageListenerCompleted = _messageListenerCompleted; if (messageListenerCompleted != null) { messageListenerCompleted.Dispose(); _messageListenerCompleted = null; } _disposed = true; } } /// /// Releases unmanaged resources and performs other cleanup operations before the /// is reclaimed by garbage collection. /// ~Session() { Dispose(false); } #endregion IDisposable implementation #region ISession implementation /// /// Gets or sets the connection info. /// /// The connection info. IConnectionInfo ISession.ConnectionInfo { get { return ConnectionInfo; } } WaitHandle ISession.MessageListenerCompleted { get { return _messageListenerCompleted; } } /// /// Create a new SSH session channel. /// /// /// A new SSH session channel. /// IChannelSession ISession.CreateChannelSession() { return new ChannelSession(this, NextChannelNumber, InitialLocalWindowSize, LocalChannelDataPacketSize); } /// /// Create a new channel for a locally forwarded TCP/IP port. /// /// /// A new channel for a locally forwarded TCP/IP port. /// IChannelDirectTcpip ISession.CreateChannelDirectTcpip() { return new ChannelDirectTcpip(this, NextChannelNumber, InitialLocalWindowSize, LocalChannelDataPacketSize); } /// /// Creates a "forwarded-tcpip" SSH channel. /// /// /// A new "forwarded-tcpip" SSH channel. /// IChannelForwardedTcpip ISession.CreateChannelForwardedTcpip(uint remoteChannelNumber, uint remoteWindowSize, uint remoteChannelDataPacketSize) { return new ChannelForwardedTcpip(this, NextChannelNumber, InitialLocalWindowSize, LocalChannelDataPacketSize, remoteChannelNumber, remoteWindowSize, remoteChannelDataPacketSize); } /// /// Sends a message to the server. /// /// The message to send. /// The client is not connected. /// The operation timed out. /// The size of the packet exceeds the maximum size defined by the protocol. void ISession.SendMessage(Message message) { SendMessage(message); } /// /// Sends a message to the server. /// /// The message to send. /// /// true if the message was sent to the server; otherwise, false. /// /// The size of the packet exceeds the maximum size defined by the protocol. /// /// This methods returns false when the attempt to send the message results in a /// or a . /// bool ISession.TrySendMessage(Message message) { return TrySendMessage(message); } #endregion ISession implementation } }