ChannelForwardedTcpip.cs 4.9 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.SshClient.Common;
  7. using Renci.SshClient.Messages.Connection;
  8. namespace Renci.SshClient.Channels
  9. {
  10. /// <summary>
  11. /// Implements "forwarded-tcpip" SSH channel.
  12. /// </summary>
  13. internal 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. var ep = new IPEndPoint(Dns.GetHostEntry(connectedHost).AddressList[0], (int)connectedPort);
  52. this._socket = new Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  53. this._socket.Connect(ep);
  54. this._socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, 1);
  55. // Send channel open confirmation message
  56. this.SendMessage(new ChannelOpenConfirmationMessage(this.RemoteChannelNumber, this.LocalWindowSize, this.PacketSize, this.LocalChannelNumber));
  57. }
  58. catch (Exception exp)
  59. {
  60. // Send channel open failure message
  61. this.SendMessage(new ChannelOpenFailureMessage(this.RemoteChannelNumber, exp.ToString(), 2));
  62. throw;
  63. }
  64. // Start reading data from the port and send to channel
  65. while (this._socket.Connected || this.IsConnected)
  66. {
  67. try
  68. {
  69. var read = this._socket.Receive(buffer);
  70. if (read > 0)
  71. {
  72. this.SendMessage(new ChannelDataMessage(this.RemoteChannelNumber, buffer.Take(read).ToArray()));
  73. }
  74. else
  75. {
  76. // Zero bytes received when remote host shuts down the connection
  77. break;
  78. }
  79. }
  80. catch (SocketException exp)
  81. {
  82. if (exp.SocketErrorCode == SocketError.WouldBlock ||
  83. exp.SocketErrorCode == SocketError.IOPending ||
  84. exp.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
  85. {
  86. // socket buffer is probably empty, wait and try again
  87. Thread.Sleep(30);
  88. }
  89. else if (exp.SocketErrorCode == SocketError.ConnectionAborted)
  90. {
  91. break;
  92. }
  93. else
  94. throw; // throw any other error
  95. }
  96. }
  97. this.SendMessage(new ChannelEofMessage(this.RemoteChannelNumber));
  98. this.Close();
  99. }
  100. /// <summary>
  101. /// Called when channel data is received.
  102. /// </summary>
  103. /// <param name="data">The data.</param>
  104. protected override void OnData(byte[] data)
  105. {
  106. base.OnData(data);
  107. // Read data from the channel and send it to the port
  108. this._socket.Send(data);
  109. }
  110. /// <summary>
  111. /// Releases unmanaged and - optionally - managed resources
  112. /// </summary>
  113. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  114. protected override void Dispose(bool disposing)
  115. {
  116. if (this._socket != null)
  117. {
  118. this._socket.Dispose();
  119. this._socket = null;
  120. }
  121. base.Dispose(disposing);
  122. }
  123. }
  124. }