ChannelForwardedTcpip.cs 5.0 KB

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