ForwardedPortRemote.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using System;
  2. using System.Threading;
  3. using Renci.SshNet.Channels;
  4. using Renci.SshNet.Messages.Connection;
  5. using Renci.SshNet.Common;
  6. using System.Globalization;
  7. using System.Net;
  8. namespace Renci.SshNet
  9. {
  10. /// <summary>
  11. /// Provides functionality for remote port forwarding
  12. /// </summary>
  13. public partial class ForwardedPortRemote : ForwardedPort, IDisposable
  14. {
  15. private bool _requestStatus;
  16. private EventWaitHandle _globalRequestResponse = new AutoResetEvent(false);
  17. /// <summary>
  18. /// Gets the bound host.
  19. /// </summary>
  20. public IPAddress BoundHostAddress { get; protected set; }
  21. /// <summary>
  22. /// Gets the bound host.
  23. /// </summary>
  24. public string BoundHost
  25. {
  26. get
  27. {
  28. return this.BoundHostAddress.ToString();
  29. }
  30. }
  31. /// <summary>
  32. /// Gets the bound port.
  33. /// </summary>
  34. public uint BoundPort { get; protected set; }
  35. /// <summary>
  36. /// Gets the forwarded host.
  37. /// </summary>
  38. public IPAddress HostAddress { get; protected set; }
  39. /// <summary>
  40. /// Gets the forwarded host.
  41. /// </summary>
  42. public string Host
  43. {
  44. get
  45. {
  46. return this.HostAddress.ToString();
  47. }
  48. }
  49. /// <summary>
  50. /// Gets the forwarded port.
  51. /// </summary>
  52. public uint Port { get; protected set; }
  53. /// <summary>
  54. /// Initializes a new instance of the <see cref="ForwardedPortRemote" /> class.
  55. /// </summary>
  56. /// <param name="boundHostAddress">The bound host address.</param>
  57. /// <param name="boundPort">The bound port.</param>
  58. /// <param name="hostAddress">The host address.</param>
  59. /// <param name="port">The port.</param>
  60. /// <exception cref="ArgumentNullException"><paramref name="boundHostAddress"/> is <c>null</c>.</exception>
  61. /// <exception cref="ArgumentNullException"><paramref name="hostAddress"/> is <c>null</c>.</exception>
  62. /// <exception cref="ArgumentOutOfRangeException"><paramref name="boundPort"/> is not a valid port.</exception>
  63. /// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not a valid port.</exception>
  64. public ForwardedPortRemote(IPAddress boundHostAddress, uint boundPort, IPAddress hostAddress, uint port)
  65. {
  66. if (boundHostAddress == null)
  67. throw new ArgumentNullException("boundHostAddress");
  68. if (hostAddress == null)
  69. throw new ArgumentNullException("hostAddress");
  70. if (!boundPort.IsValidPort())
  71. throw new ArgumentOutOfRangeException("boundPort");
  72. if (!port.IsValidPort())
  73. throw new ArgumentOutOfRangeException("port");
  74. this.BoundHostAddress = boundHostAddress;
  75. this.BoundPort = boundPort;
  76. this.HostAddress = hostAddress;
  77. this.Port = port;
  78. }
  79. /// <summary>
  80. /// Starts remote port forwarding.
  81. /// </summary>
  82. public override void Start()
  83. {
  84. base.Start();
  85. // If port already started don't start it again
  86. if (this.IsStarted)
  87. return;
  88. this.Session.RegisterMessage("SSH_MSG_REQUEST_FAILURE");
  89. this.Session.RegisterMessage("SSH_MSG_REQUEST_SUCCESS");
  90. this.Session.RegisterMessage("SSH_MSG_CHANNEL_OPEN");
  91. this.Session.RequestSuccessReceived += Session_RequestSuccess;
  92. this.Session.RequestFailureReceived += Session_RequestFailure;
  93. this.Session.ChannelOpenReceived += Session_ChannelOpening;
  94. // Send global request to start direct tcpip
  95. this.Session.SendMessage(new GlobalRequestMessage(GlobalRequestName.TcpIpForward, true, this.BoundHost, this.BoundPort));
  96. this.Session.WaitHandle(this._globalRequestResponse);
  97. if (!this._requestStatus)
  98. {
  99. // If request failed don't handle channel opening for this request
  100. this.Session.ChannelOpenReceived -= Session_ChannelOpening;
  101. throw new SshException(string.Format(CultureInfo.CurrentCulture, "Port forwarding for '{0}' port '{1}' failed to start.", this.Host, this.Port));
  102. }
  103. this.IsStarted = true;
  104. }
  105. /// <summary>
  106. /// Stops remote port forwarding.
  107. /// </summary>
  108. public override void Stop()
  109. {
  110. base.Stop();
  111. // If port not started you cant stop it
  112. if (!this.IsStarted)
  113. return;
  114. // Send global request to cancel direct tcpip
  115. this.Session.SendMessage(new GlobalRequestMessage(GlobalRequestName.CancelTcpIpForward, true, this.BoundHost, this.BoundPort));
  116. this.Session.WaitHandle(this._globalRequestResponse);
  117. this.Session.RequestSuccessReceived -= Session_RequestSuccess;
  118. this.Session.RequestFailureReceived -= Session_RequestFailure;
  119. this.Session.ChannelOpenReceived -= Session_ChannelOpening;
  120. this.IsStarted = false;
  121. }
  122. private void Session_ChannelOpening(object sender, MessageEventArgs<ChannelOpenMessage> e)
  123. {
  124. // Ensure that this is corresponding request
  125. var info = e.Message.Info as ForwardedTcpipChannelInfo;
  126. if (info != null)
  127. {
  128. if (info.ConnectedAddress == this.BoundHost && info.ConnectedPort == this.BoundPort)
  129. {
  130. this.ExecuteThread(() =>
  131. {
  132. try
  133. {
  134. this.RaiseRequestReceived(info.OriginatorAddress, info.OriginatorPort);
  135. var channel = this.Session.CreateChannel<ChannelForwardedTcpip>(e.Message.LocalChannelNumber, e.Message.InitialWindowSize, e.Message.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. }