2
0

ForwardedPortLocal.NET.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System;
  2. using System.Diagnostics;
  3. using System.Net.Sockets;
  4. using System.Net;
  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
  12. {
  13. private TcpListener _listener;
  14. private readonly object _listenerLocker = new object();
  15. private int _pendingRequests;
  16. partial void InternalStart()
  17. {
  18. // If port already started don't start it again
  19. if (this.IsStarted)
  20. return;
  21. IPAddress addr = this.BoundHost.GetIPAddress();
  22. var ep = new IPEndPoint(addr, (int)this.BoundPort);
  23. this._listener = new TcpListener(ep);
  24. this._listener.Start();
  25. // Update bound port if original was passed as zero
  26. this.BoundPort = (uint)((IPEndPoint)_listener.LocalEndpoint).Port;
  27. this.Session.ErrorOccured += Session_ErrorOccured;
  28. this.Session.Disconnected += Session_Disconnected;
  29. this._listenerTaskCompleted = new ManualResetEvent(false);
  30. this.ExecuteThread(() =>
  31. {
  32. try
  33. {
  34. while (true)
  35. {
  36. lock (this._listenerLocker)
  37. {
  38. if (this._listener == null)
  39. break;
  40. }
  41. var socket = this._listener.AcceptSocket();
  42. this.ExecuteThread(() =>
  43. {
  44. try
  45. {
  46. Interlocked.Increment(ref _pendingRequests);
  47. var originatorEndPoint = (IPEndPoint) socket.RemoteEndPoint;
  48. this.RaiseRequestReceived(originatorEndPoint.Address.ToString(),
  49. (uint) originatorEndPoint.Port);
  50. using (var channel = this.Session.CreateChannelDirectTcpip())
  51. {
  52. channel.Open(this.Host, this.Port, this, socket);
  53. channel.Bind();
  54. channel.Close();
  55. }
  56. }
  57. catch (Exception exp)
  58. {
  59. this.RaiseExceptionEvent(exp);
  60. }
  61. finally
  62. {
  63. Interlocked.Decrement(ref _pendingRequests);
  64. }
  65. });
  66. }
  67. }
  68. catch (SocketException exp)
  69. {
  70. if (exp.SocketErrorCode != SocketError.Interrupted)
  71. {
  72. this.RaiseExceptionEvent(exp);
  73. }
  74. }
  75. catch (Exception exp)
  76. {
  77. this.RaiseExceptionEvent(exp);
  78. }
  79. finally
  80. {
  81. this._listenerTaskCompleted.Set();
  82. }
  83. });
  84. this.IsStarted = true;
  85. }
  86. partial void InternalStop()
  87. {
  88. // If port not started you cant stop it
  89. if (!this.IsStarted)
  90. return;
  91. this.Session.Disconnected -= Session_Disconnected;
  92. this.Session.ErrorOccured -= Session_ErrorOccured;
  93. this.StopListener();
  94. this._listenerTaskCompleted.WaitOne(this.Session.ConnectionInfo.Timeout);
  95. this._listenerTaskCompleted.Dispose();
  96. this._listenerTaskCompleted = null;
  97. var stopWatch = new Stopwatch();
  98. stopWatch.Start();
  99. while (stopWatch.Elapsed < this.Session.ConnectionInfo.Timeout || this.Session.ConnectionInfo.Timeout == SshNet.Session.Infinite)
  100. {
  101. if (Interlocked.CompareExchange(ref _pendingRequests, 0, 0) == 0)
  102. break;
  103. Thread.Sleep(50);
  104. }
  105. stopWatch.Stop();
  106. this.IsStarted = false;
  107. }
  108. private void StopListener()
  109. {
  110. lock (this._listenerLocker)
  111. {
  112. if (this._listener != null)
  113. {
  114. this._listener.Stop();
  115. this._listener = null;
  116. }
  117. }
  118. }
  119. private void Session_ErrorOccured(object sender, Common.ExceptionEventArgs e)
  120. {
  121. this.StopListener();
  122. }
  123. private void Session_Disconnected(object sender, EventArgs e)
  124. {
  125. this.StopListener();
  126. }
  127. }
  128. }