2
0

ISession.cs 11 KB

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