ISession.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 or sets 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. /// <c>true</c> if the session is connected; otherwise, <c>false</c>.
  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. SemaphoreLight 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. /// <c>null</c> 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. /// <returns>
  80. /// A new "forwarded-tcpip" SSH channel.
  81. /// </returns>
  82. IChannelForwardedTcpip CreateChannelForwardedTcpip(uint remoteChannelNumber, uint remoteWindowSize, uint remoteChannelDataPacketSize);
  83. /// <summary>
  84. /// Disconnects from the server.
  85. /// </summary>
  86. /// <remarks>
  87. /// This sends a <b>SSH_MSG_DISCONNECT</b> message to the server, waits for the
  88. /// server to close the socket on its end and subsequently closes the client socket.
  89. /// </remarks>
  90. void Disconnect();
  91. /// <summary>
  92. /// Called when client is disconnecting from the server.
  93. /// </summary>
  94. void OnDisconnecting();
  95. /// <summary>
  96. /// Registers SSH message with the session.
  97. /// </summary>
  98. /// <param name="messageName">The name of the message to register with the session.</param>
  99. void RegisterMessage(string messageName);
  100. /// <summary>
  101. /// Sends a message to the server.
  102. /// </summary>
  103. /// <param name="message">The message to send.</param>
  104. /// <exception cref="SshConnectionException">The client is not connected.</exception>
  105. /// <exception cref="SshOperationTimeoutException">The operation timed out.</exception>
  106. /// <exception cref="InvalidOperationException">The size of the packet exceeds the maximum size defined by the protocol.</exception>
  107. void SendMessage(Message message);
  108. /// <summary>
  109. /// Sends a message to the server.
  110. /// </summary>
  111. /// <param name="message">The message to send.</param>
  112. /// <returns>
  113. /// <c>true</c> if the message was sent to the server; otherwise, <c>false</c>.
  114. /// </returns>
  115. /// <exception cref="InvalidOperationException">The size of the packet exceeds the maximum size defined by the protocol.</exception>
  116. /// <remarks>
  117. /// This methods returns <c>false</c> when the attempt to send the message results in a
  118. /// <see cref="SocketException"/> or a <see cref="SshException"/>.
  119. /// </remarks>
  120. bool TrySendMessage(Message message);
  121. /// <summary>
  122. /// Unregister SSH message from the session.
  123. /// </summary>
  124. /// <param name="messageName">The name of the message to unregister with the session.</param>
  125. void UnRegisterMessage(string messageName);
  126. /// <summary>
  127. /// Waits for the specified handle or the exception handle for the receive thread
  128. /// to signal within the connection timeout.
  129. /// </summary>
  130. /// <param name="waitHandle">The wait handle.</param>
  131. /// <exception cref="SshConnectionException">A received package was invalid or failed the message integrity check.</exception>
  132. /// <exception cref="SshOperationTimeoutException">None of the handles are signaled in time and the session is not disconnecting.</exception>
  133. /// <exception cref="SocketException">A socket error was signaled while receiving messages from the server.</exception>
  134. /// <remarks>
  135. /// When neither handles are signaled in time and the session is not closing, then the
  136. /// session is disconnected.
  137. /// </remarks>
  138. void WaitOnHandle(WaitHandle waitHandle);
  139. /// <summary>
  140. /// Waits for the specified handle or the exception handle for the receive thread
  141. /// to signal within the specified timeout.
  142. /// </summary>
  143. /// <param name="waitHandle">The wait handle.</param>
  144. /// <param name="timeout">The time to wait for any of the handles to become signaled.</param>
  145. /// <exception cref="SshConnectionException">A received package was invalid or failed the message integrity check.</exception>
  146. /// <exception cref="SshOperationTimeoutException">None of the handles are signaled in time and the session is not disconnecting.</exception>
  147. /// <exception cref="SocketException">A socket error was signaled while receiving messages from the server.</exception>
  148. /// <remarks>
  149. /// When neither handles are signaled in time and the session is not closing, then the
  150. /// session is disconnected.
  151. /// </remarks>
  152. void WaitOnHandle(WaitHandle waitHandle, TimeSpan timeout);
  153. /// <summary>
  154. /// Waits for the specified <seec ref="WaitHandle"/> to receive a signal, using a <see cref="TimeSpan"/>
  155. /// to specify the time interval.
  156. /// </summary>
  157. /// <param name="waitHandle">The <see cref="WaitHandle"/> that should be signaled.</param>
  158. /// <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>
  159. /// <param name="exception">When this method returns <see cref="WaitResult.Failed"/>, contains the <see cref="Exception"/>.</param>
  160. /// <returns>
  161. /// A <see cref="WaitResult"/>.
  162. /// </returns>
  163. WaitResult TryWait(WaitHandle waitHandle, TimeSpan timeout, out Exception exception);
  164. /// <summary>
  165. /// Waits for the specified <seec ref="WaitHandle"/> to receive a signal, using a <see cref="TimeSpan"/>
  166. /// to specify the time interval.
  167. /// </summary>
  168. /// <param name="waitHandle">The <see cref="WaitHandle"/> that should be signaled.</param>
  169. /// <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>
  170. /// <returns>
  171. /// A <see cref="WaitResult"/>.
  172. /// </returns>
  173. WaitResult TryWait(WaitHandle waitHandle, TimeSpan timeout);
  174. /// <summary>
  175. /// Occurs when <see cref="ChannelCloseMessage"/> message received
  176. /// </summary>
  177. event EventHandler<MessageEventArgs<ChannelCloseMessage>> ChannelCloseReceived;
  178. /// <summary>
  179. /// Occurs when <see cref="ChannelDataMessage"/> message received
  180. /// </summary>
  181. event EventHandler<MessageEventArgs<ChannelDataMessage>> ChannelDataReceived;
  182. /// <summary>
  183. /// Occurs when <see cref="ChannelEofMessage"/> message received
  184. /// </summary>
  185. event EventHandler<MessageEventArgs<ChannelEofMessage>> ChannelEofReceived;
  186. /// <summary>
  187. /// Occurs when <see cref="ChannelExtendedDataMessage"/> message received
  188. /// </summary>
  189. event EventHandler<MessageEventArgs<ChannelExtendedDataMessage>> ChannelExtendedDataReceived;
  190. /// <summary>
  191. /// Occurs when <see cref="ChannelFailureMessage"/> message received
  192. /// </summary>
  193. event EventHandler<MessageEventArgs<ChannelFailureMessage>> ChannelFailureReceived;
  194. /// <summary>
  195. /// Occurs when <see cref="ChannelOpenConfirmationMessage"/> message received
  196. /// </summary>
  197. event EventHandler<MessageEventArgs<ChannelOpenConfirmationMessage>> ChannelOpenConfirmationReceived;
  198. /// <summary>
  199. /// Occurs when <see cref="ChannelOpenFailureMessage"/> message received
  200. /// </summary>
  201. event EventHandler<MessageEventArgs<ChannelOpenFailureMessage>> ChannelOpenFailureReceived;
  202. /// <summary>
  203. /// Occurs when <see cref="ChannelOpenMessage"/> message received
  204. /// </summary>
  205. event EventHandler<MessageEventArgs<ChannelOpenMessage>> ChannelOpenReceived;
  206. /// <summary>
  207. /// Occurs when <see cref="ChannelRequestMessage"/> message received
  208. /// </summary>
  209. event EventHandler<MessageEventArgs<ChannelRequestMessage>> ChannelRequestReceived;
  210. /// <summary>
  211. /// Occurs when <see cref="ChannelSuccessMessage"/> message received
  212. /// </summary>
  213. event EventHandler<MessageEventArgs<ChannelSuccessMessage>> ChannelSuccessReceived;
  214. /// <summary>
  215. /// Occurs when <see cref="ChannelWindowAdjustMessage"/> message received
  216. /// </summary>
  217. event EventHandler<MessageEventArgs<ChannelWindowAdjustMessage>> ChannelWindowAdjustReceived;
  218. /// <summary>
  219. /// Occurs when session has been disconnected from the server.
  220. /// </summary>
  221. event EventHandler<EventArgs> Disconnected;
  222. /// <summary>
  223. /// Occurs when an error occurred.
  224. /// </summary>
  225. event EventHandler<ExceptionEventArgs> ErrorOccured;
  226. /// <summary>
  227. /// Occurs when host key received.
  228. /// </summary>
  229. event EventHandler<HostKeyEventArgs> HostKeyReceived;
  230. /// <summary>
  231. /// Occurs when <see cref="RequestSuccessMessage"/> message received
  232. /// </summary>
  233. event EventHandler<MessageEventArgs<RequestSuccessMessage>> RequestSuccessReceived;
  234. /// <summary>
  235. /// Occurs when <see cref="RequestFailureMessage"/> message received
  236. /// </summary>
  237. event EventHandler<MessageEventArgs<RequestFailureMessage>> RequestFailureReceived;
  238. /// <summary>
  239. /// Occurs when <see cref="BannerMessage"/> message is received from the server.
  240. /// </summary>
  241. event EventHandler<MessageEventArgs<BannerMessage>> UserAuthenticationBannerReceived;
  242. }
  243. }