ForwardedPortRemote.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 greater than <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
  63. /// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> is greater than <see cref="F:System.Net.IPEndPoint.MaxPort" />.</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. boundPort.ValidatePort("boundPort");
  71. port.ValidatePort("port");
  72. this.BoundHostAddress = boundHostAddress;
  73. this.BoundPort = boundPort;
  74. this.HostAddress = hostAddress;
  75. this.Port = port;
  76. }
  77. /// <summary>
  78. /// Starts remote port forwarding.
  79. /// </summary>
  80. public override void Start()
  81. {
  82. base.Start();
  83. // If port already started don't start it again
  84. if (this.IsStarted)
  85. return;
  86. this.Session.RegisterMessage("SSH_MSG_REQUEST_FAILURE");
  87. this.Session.RegisterMessage("SSH_MSG_REQUEST_SUCCESS");
  88. this.Session.RegisterMessage("SSH_MSG_CHANNEL_OPEN");
  89. this.Session.RequestSuccessReceived += Session_RequestSuccess;
  90. this.Session.RequestFailureReceived += Session_RequestFailure;
  91. this.Session.ChannelOpenReceived += Session_ChannelOpening;
  92. // Send global request to start direct tcpip
  93. this.Session.SendMessage(new GlobalRequestMessage(GlobalRequestName.TcpIpForward, true, this.BoundHost, this.BoundPort));
  94. this.Session.WaitOnHandle(this._globalRequestResponse);
  95. if (!this._requestStatus)
  96. {
  97. // If request failed don't handle channel opening for this request
  98. this.Session.ChannelOpenReceived -= Session_ChannelOpening;
  99. throw new SshException(string.Format(CultureInfo.CurrentCulture, "Port forwarding for '{0}' port '{1}' failed to start.", this.Host, this.Port));
  100. }
  101. this.IsStarted = true;
  102. }
  103. /// <summary>
  104. /// Stops remote port forwarding.
  105. /// </summary>
  106. public override void Stop()
  107. {
  108. base.Stop();
  109. // If port not started you cant stop it
  110. if (!this.IsStarted)
  111. return;
  112. // Send global request to cancel direct tcpip
  113. this.Session.SendMessage(new GlobalRequestMessage(GlobalRequestName.CancelTcpIpForward, true, this.BoundHost, this.BoundPort));
  114. this.Session.WaitOnHandle(this._globalRequestResponse);
  115. this.Session.RequestSuccessReceived -= Session_RequestSuccess;
  116. this.Session.RequestFailureReceived -= Session_RequestFailure;
  117. this.Session.ChannelOpenReceived -= Session_ChannelOpening;
  118. this.IsStarted = false;
  119. }
  120. private void Session_ChannelOpening(object sender, MessageEventArgs<ChannelOpenMessage> e)
  121. {
  122. var channelOpenMessage = e.Message;
  123. var info = channelOpenMessage.Info as ForwardedTcpipChannelInfo;
  124. if (info != null)
  125. {
  126. // Ensure this is the corresponding request
  127. if (info.ConnectedAddress == this.BoundHost && info.ConnectedPort == this.BoundPort)
  128. {
  129. this.ExecuteThread(() =>
  130. {
  131. try
  132. {
  133. this.RaiseRequestReceived(info.OriginatorAddress, info.OriginatorPort);
  134. var channel = this.Session.CreateServerChannel<ChannelForwardedTcpip>(
  135. channelOpenMessage.LocalChannelNumber, channelOpenMessage.InitialWindowSize,
  136. channelOpenMessage.MaximumPacketSize);
  137. channel.Bind(this.HostAddress, this.Port);
  138. }
  139. catch (Exception exp)
  140. {
  141. this.RaiseExceptionEvent(exp);
  142. }
  143. });
  144. }
  145. }
  146. }
  147. private void Session_RequestFailure(object sender, EventArgs e)
  148. {
  149. this._requestStatus = false;
  150. this._globalRequestResponse.Set();
  151. }
  152. private void Session_RequestSuccess(object sender, MessageEventArgs<RequestSuccessMessage> e)
  153. {
  154. this._requestStatus = true;
  155. if (this.BoundPort == 0)
  156. {
  157. this.BoundPort = (e.Message.BoundPort == null) ? 0 : e.Message.BoundPort.Value;
  158. }
  159. this._globalRequestResponse.Set();
  160. }
  161. partial void ExecuteThread(Action action);
  162. #region IDisposable Members
  163. private bool _isDisposed;
  164. /// <summary>
  165. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  166. /// </summary>
  167. public void Dispose()
  168. {
  169. Dispose(true);
  170. GC.SuppressFinalize(this);
  171. }
  172. /// <summary>
  173. /// Releases unmanaged and - optionally - managed resources
  174. /// </summary>
  175. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  176. protected virtual void Dispose(bool disposing)
  177. {
  178. // Check to see if Dispose has already been called.
  179. if (!this._isDisposed)
  180. {
  181. // If disposing equals true, dispose all managed
  182. // and unmanaged resources.
  183. if (disposing)
  184. {
  185. // Dispose managed resources.
  186. if (this._globalRequestResponse != null)
  187. {
  188. this._globalRequestResponse.Dispose();
  189. this._globalRequestResponse = null;
  190. }
  191. }
  192. // Note disposing has been done.
  193. _isDisposed = true;
  194. }
  195. }
  196. /// <summary>
  197. /// Releases unmanaged resources and performs other cleanup operations before the
  198. /// <see cref="ForwardedPortRemote"/> is reclaimed by garbage collection.
  199. /// </summary>
  200. ~ForwardedPortRemote()
  201. {
  202. // Do not re-create Dispose clean-up code here.
  203. // Calling Dispose(false) is optimal in terms of
  204. // readability and maintainability.
  205. Dispose(false);
  206. }
  207. #endregion
  208. }
  209. }