ChannelForwardedTcpip.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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
  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. /// Initializes a new instance of the <see cref="ChannelForwardedTcpip"/> class.
  28. /// </summary>
  29. public ChannelForwardedTcpip()
  30. {
  31. }
  32. /// <summary>
  33. /// Binds channel to specified connected host.
  34. /// </summary>
  35. /// <param name="connectedHost">The connected host.</param>
  36. /// <param name="connectedPort">The connected port.</param>
  37. public void Bind(IPAddress connectedHost, uint connectedPort)
  38. {
  39. byte[] buffer;
  40. if (!this.IsConnected)
  41. {
  42. throw new SshException("Session is not connected.");
  43. }
  44. // Try to connect to the socket
  45. try
  46. {
  47. // Get buffer in memory for data exchange
  48. buffer = new byte[this.RemotePacketSize];
  49. this.OpenSocket(connectedHost, connectedPort);
  50. // Send channel open confirmation message
  51. this.SendMessage(new ChannelOpenConfirmationMessage(this.RemoteChannelNumber, this.LocalWindowSize, this.LocalPacketSize, this.LocalChannelNumber));
  52. }
  53. catch (Exception exp)
  54. {
  55. // Send channel open failure message
  56. this.SendMessage(new ChannelOpenFailureMessage(this.RemoteChannelNumber, exp.ToString(), 2));
  57. throw;
  58. }
  59. // Start reading data from the port and send to channel
  60. while (this._socket != null && this._socket.CanRead())
  61. {
  62. try
  63. {
  64. var read = 0;
  65. this.InternalSocketReceive(buffer, ref read);
  66. if (read > 0)
  67. {
  68. this.SendMessage(new ChannelDataMessage(this.RemoteChannelNumber, buffer.Take(read).ToArray()));
  69. }
  70. else
  71. {
  72. // Zero bytes received when remote host shuts down the connection
  73. break;
  74. }
  75. }
  76. catch (SocketException exp)
  77. {
  78. if (exp.SocketErrorCode == SocketError.WouldBlock ||
  79. exp.SocketErrorCode == SocketError.IOPending ||
  80. exp.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
  81. {
  82. // socket buffer is probably empty, wait and try again
  83. Thread.Sleep(30);
  84. }
  85. else if (exp.SocketErrorCode == SocketError.ConnectionAborted)
  86. {
  87. break;
  88. }
  89. else
  90. throw; // throw any other error
  91. }
  92. }
  93. this.Close();
  94. }
  95. partial void OpenSocket(IPAddress connectedHost, uint connectedPort);
  96. public override void Close()
  97. {
  98. // Send EOF message first when channel need to be closed
  99. this.SendMessage(new ChannelEofMessage(this.RemoteChannelNumber));
  100. base.Close();
  101. }
  102. /// <summary>
  103. /// Called when channel data is received.
  104. /// </summary>
  105. /// <param name="data">The data.</param>
  106. protected override void OnData(byte[] data)
  107. {
  108. base.OnData(data);
  109. // Read data from the channel and send it to the port
  110. this.InternalSocketSend(data);
  111. }
  112. partial void InternalSocketSend(byte[] data);
  113. partial void InternalSocketReceive(byte[] buffer, ref int read);
  114. /// <summary>
  115. /// Releases unmanaged and - optionally - managed resources
  116. /// </summary>
  117. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  118. protected override void Dispose(bool disposing)
  119. {
  120. if (this._socket != null)
  121. {
  122. this._socket.Dispose();
  123. this._socket = null;
  124. }
  125. base.Dispose(disposing);
  126. }
  127. }
  128. }