ISession.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using System;
  2. using System.Net.Sockets;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using Renci.SshNet.Channels;
  6. using Renci.SshNet.Common;
  7. using Renci.SshNet.Messages;
  8. using Renci.SshNet.Messages.Authentication;
  9. using Renci.SshNet.Messages.Connection;
  10. namespace Renci.SshNet
  11. {
  12. /// <summary>
  13. /// Provides functionality to connect and interact with SSH server.
  14. /// </summary>
  15. internal interface ISession : IDisposable
  16. {
  17. /// <summary>
  18. /// Gets the connection info.
  19. /// </summary>
  20. /// <value>The connection info.</value>
  21. IConnectionInfo ConnectionInfo { get; }
  22. /// <summary>
  23. /// Gets a value indicating whether the session is connected.
  24. /// </summary>
  25. /// <value>
  26. /// <see langword="true"/> if the session is connected; otherwise, <see langword="false"/>.
  27. /// </value>
  28. bool IsConnected { get; }
  29. /// <summary>
  30. /// Gets the session semaphore that controls session channels.
  31. /// </summary>
  32. /// <value>
  33. /// The session semaphore.
  34. /// </value>
  35. SemaphoreSlim SessionSemaphore { get; }
  36. /// <summary>
  37. /// Gets a <see cref="WaitHandle"/> that can be used to wait for the message listener loop to complete.
  38. /// </summary>
  39. /// <value>
  40. /// A <see cref="WaitHandle"/> that can be used to wait for the message listener loop to complete, or
  41. /// <see langword="null"/> when the session has not been connected.
  42. /// </value>
  43. WaitHandle MessageListenerCompleted { get; }
  44. /// <summary>
  45. /// Connects to the server.
  46. /// </summary>
  47. /// <exception cref="SocketException">Socket connection to the SSH server or proxy server could not be established, or an error occurred while resolving the hostname.</exception>
  48. /// <exception cref="SshConnectionException">SSH session could not be established.</exception>
  49. /// <exception cref="SshAuthenticationException">Authentication of SSH session failed.</exception>
  50. /// <exception cref="ProxyException">Failed to establish proxy connection.</exception>
  51. void Connect();
  52. /// <summary>
  53. /// Asynchronously connects to the server.
  54. /// </summary>
  55. /// <param name="cancellationToken">The <see cref="CancellationToken"/> to observe.</param>
  56. /// <returns>A <see cref="Task"/> that represents the asynchronous connect operation.</returns>
  57. /// <exception cref="SocketException">Socket connection to the SSH server or proxy server could not be established, or an error occurred while resolving the hostname.</exception>
  58. /// <exception cref="SshConnectionException">SSH session could not be established.</exception>
  59. /// <exception cref="SshAuthenticationException">Authentication of SSH session failed.</exception>
  60. /// <exception cref="ProxyException">Failed to establish proxy connection.</exception>
  61. Task ConnectAsync(CancellationToken cancellationToken);
  62. /// <summary>
  63. /// Create a new SSH session channel.
  64. /// </summary>
  65. /// <returns>
  66. /// A new SSH session channel.
  67. /// </returns>
  68. IChannelSession CreateChannelSession();
  69. /// <summary>
  70. /// Create a new channel for a locally forwarded TCP/IP port.
  71. /// </summary>
  72. /// <returns>
  73. /// A new channel for a locally forwarded TCP/IP port.
  74. /// </returns>
  75. IChannelDirectTcpip CreateChannelDirectTcpip();
  76. /// <summary>
  77. /// Creates a "forwarded-tcpip" SSH channel.
  78. /// </summary>
  79. /// <param name="remoteChannelNumber">The number of the remote channel.</param>
  80. /// <param name="remoteWindowSize">The window size of the remote channel.</param>
  81. /// <param name="remoteChannelDataPacketSize">The data packet size of the remote channel.</param>
  82. /// <returns>
  83. /// A new "forwarded-tcpip" SSH channel.
  84. /// </returns>
  85. IChannelForwardedTcpip CreateChannelForwardedTcpip(uint remoteChannelNumber, uint remoteWindowSize, uint remoteChannelDataPacketSize);
  86. /// <summary>
  87. /// Disconnects from the server.
  88. /// </summary>
  89. /// <remarks>
  90. /// This sends a <b>SSH_MSG_DISCONNECT</b> message to the server, waits for the
  91. /// server to close the socket on its end and subsequently closes the client socket.
  92. /// </remarks>
  93. void Disconnect();
  94. /// <summary>
  95. /// Called when client is disconnecting from the server.
  96. /// </summary>
  97. void OnDisconnecting();
  98. /// <summary>
  99. /// Registers SSH message with the session.
  100. /// </summary>
  101. /// <param name="messageName">The name of the message to register with the session.</param>
  102. void RegisterMessage(string messageName);
  103. /// <summary>
  104. /// Sends a message to the server.
  105. /// </summary>
  106. /// <param name="message">The message to send.</param>
  107. /// <exception cref="SshConnectionException">The client is not connected.</exception>
  108. /// <exception cref="SshOperationTimeoutException">The operation timed out.</exception>
  109. /// <exception cref="InvalidOperationException">The size of the packet exceeds the maximum size defined by the protocol.</exception>
  110. void SendMessage(Message message);
  111. /// <summary>
  112. /// Sends a message to the server.
  113. /// </summary>
  114. /// <param name="message">The message to send.</param>
  115. /// <returns>
  116. /// <see langword="true"/> if the message was sent to the server; otherwise, <see langword="false"/>.
  117. /// </returns>
  118. /// <exception cref="InvalidOperationException">The size of the packet exceeds the maximum size defined by the protocol.</exception>
  119. /// <remarks>
  120. /// This methods returns <see langword="false"/> when the attempt to send the message results in a
  121. /// <see cref="SocketException"/> or a <see cref="SshException"/>.
  122. /// </remarks>
  123. bool TrySendMessage(Message message);
  124. /// <summary>
  125. /// Unregister SSH message from the session.
  126. /// </summary>
  127. /// <param name="messageName">The name of the message to unregister with the session.</param>
  128. void UnRegisterMessage(string messageName);
  129. /// <summary>
  130. /// Waits for the specified handle or the exception handle for the receive thread
  131. /// to signal within the connection timeout.
  132. /// </summary>
  133. /// <param name="waitHandle">The wait handle.</param>
  134. /// <exception cref="SshConnectionException">A received package was invalid or failed the message integrity check.</exception>
  135. /// <exception cref="SshOperationTimeoutException">None of the handles are signaled in time and the session is not disconnecting.</exception>
  136. /// <exception cref="SocketException">A socket error was signaled while receiving messages from the server.</exception>
  137. /// <remarks>
  138. /// When neither handles are signaled in time and the session is not closing, then the
  139. /// session is disconnected.
  140. /// </remarks>
  141. void WaitOnHandle(WaitHandle waitHandle);
  142. /// <summary>
  143. /// Waits for the specified handle or the exception handle for the receive thread
  144. /// to signal within the specified timeout.
  145. /// </summary>
  146. /// <param name="waitHandle">The wait handle.</param>
  147. /// <param name="timeout">The time to wait for any of the handles to become signaled.</param>
  148. /// <exception cref="SshConnectionException">A received package was invalid or failed the message integrity check.</exception>
  149. /// <exception cref="SshOperationTimeoutException">None of the handles are signaled in time and the session is not disconnecting.</exception>
  150. /// <exception cref="SocketException">A socket error was signaled while receiving messages from the server.</exception>
  151. /// <remarks>
  152. /// When neither handles are signaled in time and the session is not closing, then the
  153. /// session is disconnected.
  154. /// </remarks>
  155. void WaitOnHandle(WaitHandle waitHandle, TimeSpan timeout);
  156. /// <summary>
  157. /// Waits for the specified <seec ref="WaitHandle"/> to receive a signal, using a <see cref="TimeSpan"/>
  158. /// to specify the time interval.
  159. /// </summary>
  160. /// <param name="waitHandle">The <see cref="WaitHandle"/> that should be signaled.</param>
  161. /// <param name="timeout">A <see cref="TimeSpan"/> that represents the number of milliseconds to wait, or a <see cref="TimeSpan"/> that represents <c>-1</c> milliseconds to wait indefinitely.</param>
  162. /// <param name="exception">When this method returns <see cref="WaitResult.Failed"/>, contains the <see cref="Exception"/>.</param>
  163. /// <returns>
  164. /// A <see cref="WaitResult"/>.
  165. /// </returns>
  166. WaitResult TryWait(WaitHandle waitHandle, TimeSpan timeout, out Exception exception);
  167. /// <summary>
  168. /// Waits for the specified <seec ref="WaitHandle"/> to receive a signal, using a <see cref="TimeSpan"/>
  169. /// to specify the time interval.
  170. /// </summary>
  171. /// <param name="waitHandle">The <see cref="WaitHandle"/> that should be signaled.</param>
  172. /// <param name="timeout">A <see cref="TimeSpan"/> that represents the number of milliseconds to wait, or a <see cref="TimeSpan"/> that represents <c>-1</c> milliseconds to wait indefinitely.</param>
  173. /// <returns>
  174. /// A <see cref="WaitResult"/>.
  175. /// </returns>
  176. WaitResult TryWait(WaitHandle waitHandle, TimeSpan timeout);
  177. /// <summary>
  178. /// Occurs when <see cref="ChannelCloseMessage"/> message received
  179. /// </summary>
  180. event EventHandler<MessageEventArgs<ChannelCloseMessage>> ChannelCloseReceived;
  181. /// <summary>
  182. /// Occurs when <see cref="ChannelDataMessage"/> message received
  183. /// </summary>
  184. event EventHandler<MessageEventArgs<ChannelDataMessage>> ChannelDataReceived;
  185. /// <summary>
  186. /// Occurs when <see cref="ChannelEofMessage"/> message received
  187. /// </summary>
  188. event EventHandler<MessageEventArgs<ChannelEofMessage>> ChannelEofReceived;
  189. /// <summary>
  190. /// Occurs when <see cref="ChannelExtendedDataMessage"/> message received
  191. /// </summary>
  192. event EventHandler<MessageEventArgs<ChannelExtendedDataMessage>> ChannelExtendedDataReceived;
  193. /// <summary>
  194. /// Occurs when <see cref="ChannelFailureMessage"/> message received
  195. /// </summary>
  196. event EventHandler<MessageEventArgs<ChannelFailureMessage>> ChannelFailureReceived;
  197. /// <summary>
  198. /// Occurs when <see cref="ChannelOpenConfirmationMessage"/> message received
  199. /// </summary>
  200. event EventHandler<MessageEventArgs<ChannelOpenConfirmationMessage>> ChannelOpenConfirmationReceived;
  201. /// <summary>
  202. /// Occurs when <see cref="ChannelOpenFailureMessage"/> message received
  203. /// </summary>
  204. event EventHandler<MessageEventArgs<ChannelOpenFailureMessage>> ChannelOpenFailureReceived;
  205. /// <summary>
  206. /// Occurs when <see cref="ChannelOpenMessage"/> message received
  207. /// </summary>
  208. event EventHandler<MessageEventArgs<ChannelOpenMessage>> ChannelOpenReceived;
  209. /// <summary>
  210. /// Occurs when <see cref="ChannelRequestMessage"/> message received
  211. /// </summary>
  212. event EventHandler<MessageEventArgs<ChannelRequestMessage>> ChannelRequestReceived;
  213. /// <summary>
  214. /// Occurs when <see cref="ChannelSuccessMessage"/> message received
  215. /// </summary>
  216. event EventHandler<MessageEventArgs<ChannelSuccessMessage>> ChannelSuccessReceived;
  217. /// <summary>
  218. /// Occurs when <see cref="ChannelWindowAdjustMessage"/> message received
  219. /// </summary>
  220. event EventHandler<MessageEventArgs<ChannelWindowAdjustMessage>> ChannelWindowAdjustReceived;
  221. /// <summary>
  222. /// Occurs when session has been disconnected from the server.
  223. /// </summary>
  224. event EventHandler<EventArgs> Disconnected;
  225. /// <summary>
  226. /// Occurs when an error occurred.
  227. /// </summary>
  228. event EventHandler<ExceptionEventArgs> ErrorOccured;
  229. /// <summary>
  230. /// Occurs when server identification received.
  231. /// </summary>
  232. event EventHandler<SshIdentificationEventArgs> ServerIdentificationReceived;
  233. /// <summary>
  234. /// Occurs when host key received.
  235. /// </summary>
  236. event EventHandler<HostKeyEventArgs> HostKeyReceived;
  237. /// <summary>
  238. /// Occurs when <see cref="RequestSuccessMessage"/> message received
  239. /// </summary>
  240. event EventHandler<MessageEventArgs<RequestSuccessMessage>> RequestSuccessReceived;
  241. /// <summary>
  242. /// Occurs when <see cref="RequestFailureMessage"/> message received
  243. /// </summary>
  244. event EventHandler<MessageEventArgs<RequestFailureMessage>> RequestFailureReceived;
  245. /// <summary>
  246. /// Occurs when <see cref="BannerMessage"/> message is received from the server.
  247. /// </summary>
  248. event EventHandler<MessageEventArgs<BannerMessage>> UserAuthenticationBannerReceived;
  249. }
  250. }