ChannelForwardedTcpip.cs 5.0 KB

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