ForwardedPortLocal.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using Renci.SshNet.Channels;
  5. using System.Threading;
  6. namespace Renci.SshNet
  7. {
  8. /// <summary>
  9. /// Provides functionality for local port forwarding
  10. /// </summary>
  11. public partial class ForwardedPortLocal : ForwardedPort, IDisposable
  12. {
  13. private TcpListener _listener;
  14. private EventWaitHandle _listenerTaskCompleted;
  15. /// <summary>
  16. /// Starts local port forwarding.
  17. /// </summary>
  18. public override void Start()
  19. {
  20. // If port already started don't start it again
  21. if (this.IsStarted)
  22. return;
  23. var ep = new IPEndPoint(Dns.GetHostAddresses(this.BoundHost)[0], (int)this.BoundPort);
  24. this._listener = new TcpListener(ep);
  25. this._listener.Start();
  26. this._listenerTaskCompleted = new ManualResetEvent(false);
  27. this.ExecuteThread(() =>
  28. {
  29. try
  30. {
  31. while (true)
  32. {
  33. var socket = this._listener.AcceptSocket();
  34. this.ExecuteThread(() =>
  35. {
  36. try
  37. {
  38. IPEndPoint originatorEndPoint = socket.RemoteEndPoint as IPEndPoint;
  39. this.RaiseRequestReceived(originatorEndPoint.Address.ToString(), (uint)originatorEndPoint.Port);
  40. var channel = this.Session.CreateChannel<ChannelDirectTcpip>();
  41. channel.Bind(this.Host, this.Port, socket);
  42. }
  43. catch (Exception exp)
  44. {
  45. this.RaiseExceptionEvent(exp);
  46. }
  47. });
  48. }
  49. }
  50. catch (SocketException exp)
  51. {
  52. if (!(exp.SocketErrorCode == SocketError.Interrupted))
  53. {
  54. this.RaiseExceptionEvent(exp);
  55. }
  56. }
  57. catch (Exception exp)
  58. {
  59. this.RaiseExceptionEvent(exp);
  60. }
  61. finally
  62. {
  63. this._listenerTaskCompleted.Set();
  64. }
  65. });
  66. this.IsStarted = true;
  67. }
  68. /// <summary>
  69. /// Stops local port forwarding.
  70. /// </summary>
  71. public override void Stop()
  72. {
  73. base.Stop();
  74. // If port not started you cant stop it
  75. if (!this.IsStarted)
  76. return;
  77. this._listener.Stop();
  78. // TODO: Add timeout to WaitOne method
  79. this._listenerTaskCompleted.WaitOne();
  80. this._listenerTaskCompleted.Dispose();
  81. this._listenerTaskCompleted = null;
  82. this.IsStarted = false;
  83. }
  84. partial void ExecuteThread(Action action);
  85. #region IDisposable Members
  86. private bool _isDisposed = false;
  87. /// <summary>
  88. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged ResourceMessages.
  89. /// </summary>
  90. public void Dispose()
  91. {
  92. Dispose(true);
  93. GC.SuppressFinalize(this);
  94. }
  95. /// <summary>
  96. /// Releases unmanaged and - optionally - managed resources
  97. /// </summary>
  98. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged ResourceMessages.</param>
  99. protected virtual void Dispose(bool disposing)
  100. {
  101. // Check to see if Dispose has already been called.
  102. if (!this._isDisposed)
  103. {
  104. // If disposing equals true, dispose all managed
  105. // and unmanaged ResourceMessages.
  106. if (disposing)
  107. {
  108. // Dispose managed ResourceMessages.
  109. if (this._listenerTaskCompleted != null)
  110. {
  111. this._listenerTaskCompleted.Dispose();
  112. this._listenerTaskCompleted = null;
  113. }
  114. }
  115. // Note disposing has been done.
  116. _isDisposed = true;
  117. }
  118. }
  119. /// <summary>
  120. /// Releases unmanaged resources and performs other cleanup operations before the
  121. /// <see cref="Channel"/> is reclaimed by garbage collection.
  122. /// </summary>
  123. ~ForwardedPortLocal()
  124. {
  125. // Do not re-create Dispose clean-up code here.
  126. // Calling Dispose(false) is optimal in terms of
  127. // readability and maintainability.
  128. Dispose(false);
  129. }
  130. #endregion
  131. }
  132. }