ChannelForwardedTcpip.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Linq;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Threading;
  6. using Renci.SshNet.Common;
  7. using Renci.SshNet.Messages.Connection;
  8. namespace Renci.SshNet.Channels
  9. {
  10. /// <summary>
  11. /// Implements "forwarded-tcpip" SSH channel.
  12. /// </summary>
  13. internal partial class ChannelForwardedTcpip : ServerChannel, IChannelForwardedTcpip
  14. {
  15. private Socket _socket;
  16. /// <summary>
  17. /// Gets the type of the channel.
  18. /// </summary>
  19. /// <value>
  20. /// The type of the channel.
  21. /// </value>
  22. public override ChannelTypes ChannelType
  23. {
  24. get { return ChannelTypes.ForwardedTcpip; }
  25. }
  26. /// <summary>
  27. /// Binds channel to specified connected host.
  28. /// </summary>
  29. /// <param name="connectedHost">The connected host.</param>
  30. /// <param name="connectedPort">The connected port.</param>
  31. public void Bind(IPAddress connectedHost, uint connectedPort)
  32. {
  33. byte[] buffer;
  34. if (!this.IsConnected)
  35. {
  36. throw new SshException("Session is not connected.");
  37. }
  38. // Try to connect to the socket
  39. try
  40. {
  41. // Get buffer in memory for data exchange
  42. buffer = new byte[this.RemotePacketSize];
  43. this.OpenSocket(connectedHost, connectedPort);
  44. // Send channel open confirmation message
  45. this.SendMessage(new ChannelOpenConfirmationMessage(this.RemoteChannelNumber, this.LocalWindowSize, this.LocalPacketSize, this.LocalChannelNumber));
  46. }
  47. catch (Exception exp)
  48. {
  49. // Send channel open failure message
  50. this.SendMessage(new ChannelOpenFailureMessage(this.RemoteChannelNumber, exp.ToString(), 2));
  51. throw;
  52. }
  53. // Start reading data from the port and send to channel
  54. while (this._socket != null && this._socket.CanRead())
  55. {
  56. try
  57. {
  58. var read = 0;
  59. this.InternalSocketReceive(buffer, ref read);
  60. if (read > 0)
  61. {
  62. this.SendMessage(new ChannelDataMessage(this.RemoteChannelNumber, buffer.Take(read).ToArray()));
  63. }
  64. else
  65. {
  66. // Zero bytes received when remote host shuts down the connection
  67. break;
  68. }
  69. }
  70. catch (SocketException exp)
  71. {
  72. if (exp.SocketErrorCode == SocketError.WouldBlock ||
  73. exp.SocketErrorCode == SocketError.IOPending ||
  74. exp.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
  75. {
  76. // socket buffer is probably empty, wait and try again
  77. Thread.Sleep(30);
  78. }
  79. else if (exp.SocketErrorCode == SocketError.ConnectionAborted)
  80. {
  81. break;
  82. }
  83. else
  84. throw; // throw any other error
  85. }
  86. }
  87. this.Close();
  88. }
  89. partial void OpenSocket(IPAddress connectedHost, uint connectedPort);
  90. public override void Close()
  91. {
  92. // Send EOF message first when channel need to be closed
  93. this.SendMessage(new ChannelEofMessage(this.RemoteChannelNumber));
  94. base.Close();
  95. }
  96. /// <summary>
  97. /// Called when channel data is received.
  98. /// </summary>
  99. /// <param name="data">The data.</param>
  100. protected override void OnData(byte[] data)
  101. {
  102. base.OnData(data);
  103. // Read data from the channel and send it to the port
  104. this.InternalSocketSend(data);
  105. }
  106. partial void InternalSocketSend(byte[] data);
  107. partial void InternalSocketReceive(byte[] buffer, ref int read);
  108. /// <summary>
  109. /// Releases unmanaged and - optionally - managed resources
  110. /// </summary>
  111. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  112. protected override void Dispose(bool disposing)
  113. {
  114. if (this._socket != null)
  115. {
  116. this._socket.Dispose();
  117. this._socket = null;
  118. }
  119. base.Dispose(disposing);
  120. }
  121. }
  122. }