ForwardedPortRemote.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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.WaitOnHandle(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.WaitOnHandle(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. var channelOpenMessage = e.Message;
  125. var info = channelOpenMessage.Info as ForwardedTcpipChannelInfo;
  126. if (info != null)
  127. {
  128. // Ensure this is the corresponding request
  129. if (info.ConnectedAddress == this.BoundHost && info.ConnectedPort == this.BoundPort)
  130. {
  131. this.ExecuteThread(() =>
  132. {
  133. try
  134. {
  135. this.RaiseRequestReceived(info.OriginatorAddress, info.OriginatorPort);
  136. var channel = this.Session.CreateServerChannel<ChannelForwardedTcpip>(
  137. channelOpenMessage.LocalChannelNumber, channelOpenMessage.InitialWindowSize,
  138. channelOpenMessage.MaximumPacketSize);
  139. channel.Bind(this.HostAddress, this.Port);
  140. }
  141. catch (Exception exp)
  142. {
  143. this.RaiseExceptionEvent(exp);
  144. }
  145. });
  146. }
  147. }
  148. }
  149. private void Session_RequestFailure(object sender, EventArgs e)
  150. {
  151. this._requestStatus = false;
  152. this._globalRequestResponse.Set();
  153. }
  154. private void Session_RequestSuccess(object sender, MessageEventArgs<RequestSuccessMessage> e)
  155. {
  156. this._requestStatus = true;
  157. if (this.BoundPort == 0)
  158. {
  159. this.BoundPort = (e.Message.BoundPort == null) ? 0 : e.Message.BoundPort.Value;
  160. }
  161. this._globalRequestResponse.Set();
  162. }
  163. partial void ExecuteThread(Action action);
  164. #region IDisposable Members
  165. private bool _isDisposed;
  166. /// <summary>
  167. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  168. /// </summary>
  169. public void Dispose()
  170. {
  171. Dispose(true);
  172. GC.SuppressFinalize(this);
  173. }
  174. /// <summary>
  175. /// Releases unmanaged and - optionally - managed resources
  176. /// </summary>
  177. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  178. protected virtual void Dispose(bool disposing)
  179. {
  180. // Check to see if Dispose has already been called.
  181. if (!this._isDisposed)
  182. {
  183. // If disposing equals true, dispose all managed
  184. // and unmanaged resources.
  185. if (disposing)
  186. {
  187. // Dispose managed resources.
  188. if (this._globalRequestResponse != null)
  189. {
  190. this._globalRequestResponse.Dispose();
  191. this._globalRequestResponse = null;
  192. }
  193. }
  194. // Note disposing has been done.
  195. _isDisposed = true;
  196. }
  197. }
  198. /// <summary>
  199. /// Releases unmanaged resources and performs other cleanup operations before the
  200. /// <see cref="ForwardedPortRemote"/> is reclaimed by garbage collection.
  201. /// </summary>
  202. ~ForwardedPortRemote()
  203. {
  204. // Do not re-create Dispose clean-up code here.
  205. // Calling Dispose(false) is optimal in terms of
  206. // readability and maintainability.
  207. Dispose(false);
  208. }
  209. #endregion
  210. }
  211. }