ForwardedPortRemote.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using System;
  2. using System.Threading;
  3. using Renci.SshNet.Messages.Connection;
  4. using Renci.SshNet.Common;
  5. using System.Globalization;
  6. using System.Net;
  7. namespace Renci.SshNet
  8. {
  9. /// <summary>
  10. /// Provides functionality for remote port forwarding
  11. /// </summary>
  12. public partial class ForwardedPortRemote : ForwardedPort, IDisposable
  13. {
  14. private bool _requestStatus;
  15. private EventWaitHandle _globalRequestResponse = new AutoResetEvent(false);
  16. /// <summary>
  17. /// Gets the bound host.
  18. /// </summary>
  19. public IPAddress BoundHostAddress { get; protected set; }
  20. /// <summary>
  21. /// Gets the bound host.
  22. /// </summary>
  23. public string BoundHost
  24. {
  25. get
  26. {
  27. return this.BoundHostAddress.ToString();
  28. }
  29. }
  30. /// <summary>
  31. /// Gets the bound port.
  32. /// </summary>
  33. public uint BoundPort { get; protected set; }
  34. /// <summary>
  35. /// Gets the forwarded host.
  36. /// </summary>
  37. public IPAddress HostAddress { get; protected set; }
  38. /// <summary>
  39. /// Gets the forwarded host.
  40. /// </summary>
  41. public string Host
  42. {
  43. get
  44. {
  45. return this.HostAddress.ToString();
  46. }
  47. }
  48. /// <summary>
  49. /// Gets the forwarded port.
  50. /// </summary>
  51. public uint Port { get; protected set; }
  52. /// <summary>
  53. /// Initializes a new instance of the <see cref="ForwardedPortRemote" /> class.
  54. /// </summary>
  55. /// <param name="boundHostAddress">The bound host address.</param>
  56. /// <param name="boundPort">The bound port.</param>
  57. /// <param name="hostAddress">The host address.</param>
  58. /// <param name="port">The port.</param>
  59. /// <exception cref="ArgumentNullException"><paramref name="boundHostAddress"/> is <c>null</c>.</exception>
  60. /// <exception cref="ArgumentNullException"><paramref name="hostAddress"/> is <c>null</c>.</exception>
  61. /// <exception cref="ArgumentOutOfRangeException"><paramref name="boundPort" /> is greater than <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
  62. /// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> is greater than <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
  63. public ForwardedPortRemote(IPAddress boundHostAddress, uint boundPort, IPAddress hostAddress, uint port)
  64. {
  65. if (boundHostAddress == null)
  66. throw new ArgumentNullException("boundHostAddress");
  67. if (hostAddress == null)
  68. throw new ArgumentNullException("hostAddress");
  69. boundPort.ValidatePort("boundPort");
  70. port.ValidatePort("port");
  71. this.BoundHostAddress = boundHostAddress;
  72. this.BoundPort = boundPort;
  73. this.HostAddress = hostAddress;
  74. this.Port = port;
  75. }
  76. /// <summary>
  77. /// Starts remote port forwarding.
  78. /// </summary>
  79. public override void Start()
  80. {
  81. base.Start();
  82. // If port already started don't start it again
  83. if (this.IsStarted)
  84. return;
  85. this.Session.RegisterMessage("SSH_MSG_REQUEST_FAILURE");
  86. this.Session.RegisterMessage("SSH_MSG_REQUEST_SUCCESS");
  87. this.Session.RegisterMessage("SSH_MSG_CHANNEL_OPEN");
  88. this.Session.RequestSuccessReceived += Session_RequestSuccess;
  89. this.Session.RequestFailureReceived += Session_RequestFailure;
  90. this.Session.ChannelOpenReceived += Session_ChannelOpening;
  91. // Send global request to start direct tcpip
  92. this.Session.SendMessage(new GlobalRequestMessage(GlobalRequestName.TcpIpForward, true, this.BoundHost, this.BoundPort));
  93. this.Session.WaitOnHandle(this._globalRequestResponse);
  94. if (!this._requestStatus)
  95. {
  96. // If request failed don't handle channel opening for this request
  97. this.Session.ChannelOpenReceived -= Session_ChannelOpening;
  98. throw new SshException(string.Format(CultureInfo.CurrentCulture, "Port forwarding for '{0}' port '{1}' failed to start.", this.Host, this.Port));
  99. }
  100. this.IsStarted = true;
  101. }
  102. /// <summary>
  103. /// Stops remote port forwarding.
  104. /// </summary>
  105. public override void Stop()
  106. {
  107. base.Stop();
  108. // If port not started you cant stop it
  109. if (!this.IsStarted)
  110. return;
  111. // Send global request to cancel direct tcpip
  112. this.Session.SendMessage(new GlobalRequestMessage(GlobalRequestName.CancelTcpIpForward, true, this.BoundHost, this.BoundPort));
  113. this.Session.WaitOnHandle(this._globalRequestResponse);
  114. this.Session.RequestSuccessReceived -= Session_RequestSuccess;
  115. this.Session.RequestFailureReceived -= Session_RequestFailure;
  116. this.Session.ChannelOpenReceived -= Session_ChannelOpening;
  117. this.IsStarted = false;
  118. }
  119. private void Session_ChannelOpening(object sender, MessageEventArgs<ChannelOpenMessage> e)
  120. {
  121. var channelOpenMessage = e.Message;
  122. var info = channelOpenMessage.Info as ForwardedTcpipChannelInfo;
  123. if (info != null)
  124. {
  125. // Ensure this is the corresponding request
  126. if (info.ConnectedAddress == this.BoundHost && info.ConnectedPort == this.BoundPort)
  127. {
  128. this.ExecuteThread(() =>
  129. {
  130. try
  131. {
  132. this.RaiseRequestReceived(info.OriginatorAddress, info.OriginatorPort);
  133. var channel = this.Session.CreateChannelForwardedTcpip(
  134. channelOpenMessage.LocalChannelNumber, channelOpenMessage.InitialWindowSize,
  135. channelOpenMessage.MaximumPacketSize);
  136. channel.Bind(this.HostAddress, this.Port);
  137. }
  138. catch (Exception exp)
  139. {
  140. this.RaiseExceptionEvent(exp);
  141. }
  142. });
  143. }
  144. }
  145. }
  146. private void Session_RequestFailure(object sender, EventArgs e)
  147. {
  148. this._requestStatus = false;
  149. this._globalRequestResponse.Set();
  150. }
  151. private void Session_RequestSuccess(object sender, MessageEventArgs<RequestSuccessMessage> e)
  152. {
  153. this._requestStatus = true;
  154. if (this.BoundPort == 0)
  155. {
  156. this.BoundPort = (e.Message.BoundPort == null) ? 0 : e.Message.BoundPort.Value;
  157. }
  158. this._globalRequestResponse.Set();
  159. }
  160. partial void ExecuteThread(Action action);
  161. #region IDisposable Members
  162. private bool _isDisposed;
  163. /// <summary>
  164. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  165. /// </summary>
  166. public void Dispose()
  167. {
  168. Dispose(true);
  169. GC.SuppressFinalize(this);
  170. }
  171. /// <summary>
  172. /// Releases unmanaged and - optionally - managed resources
  173. /// </summary>
  174. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  175. protected virtual void Dispose(bool disposing)
  176. {
  177. // Check to see if Dispose has already been called.
  178. if (!this._isDisposed)
  179. {
  180. // If disposing equals true, dispose all managed
  181. // and unmanaged resources.
  182. if (disposing)
  183. {
  184. // Dispose managed resources.
  185. if (this._globalRequestResponse != null)
  186. {
  187. this._globalRequestResponse.Dispose();
  188. this._globalRequestResponse = null;
  189. }
  190. }
  191. // Note disposing has been done.
  192. _isDisposed = true;
  193. }
  194. }
  195. /// <summary>
  196. /// Releases unmanaged resources and performs other cleanup operations before the
  197. /// <see cref="ForwardedPortRemote"/> is reclaimed by garbage collection.
  198. /// </summary>
  199. ~ForwardedPortRemote()
  200. {
  201. // Do not re-create Dispose clean-up code here.
  202. // Calling Dispose(false) is optimal in terms of
  203. // readability and maintainability.
  204. Dispose(false);
  205. }
  206. #endregion
  207. }
  208. }