ChannelOpenMessage.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Globalization;
  3. namespace Renci.SshNet.Messages.Connection
  4. {
  5. /// <summary>
  6. /// Represents SSH_MSG_CHANNEL_OPEN message.
  7. /// </summary>
  8. [Message("SSH_MSG_CHANNEL_OPEN", 90)]
  9. public class ChannelOpenMessage : ChannelMessage
  10. {
  11. /// <summary>
  12. /// Gets the type of the channel.
  13. /// </summary>
  14. /// <value>
  15. /// The type of the channel.
  16. /// </value>
  17. public string ChannelType
  18. {
  19. get
  20. {
  21. return this.Info.ChannelType;
  22. }
  23. }
  24. /// <summary>
  25. /// Gets the initial size of the window.
  26. /// </summary>
  27. /// <value>
  28. /// The initial size of the window.
  29. /// </value>
  30. public uint InitialWindowSize { get; private set; }
  31. /// <summary>
  32. /// Gets the maximum size of the packet.
  33. /// </summary>
  34. /// <value>
  35. /// The maximum size of the packet.
  36. /// </value>
  37. public uint MaximumPacketSize { get; private set; }
  38. /// <summary>
  39. /// Gets channel specific open information.
  40. /// </summary>
  41. public ChannelOpenInfo Info { get; private set; }
  42. /// <summary>
  43. /// Initializes a new instance of the <see cref="ChannelOpenMessage"/> class.
  44. /// </summary>
  45. public ChannelOpenMessage()
  46. {
  47. // Required for dynamicly loading request type when it comes from the server
  48. }
  49. /// <summary>
  50. /// Initializes a new instance of the <see cref="ChannelOpenMessage"/> class.
  51. /// </summary>
  52. /// <param name="channelNumber">The channel number.</param>
  53. /// <param name="initialWindowSize">Initial size of the window.</param>
  54. /// <param name="maximumPacketSize">Maximum size of the packet.</param>
  55. /// <param name="info">The info.</param>
  56. public ChannelOpenMessage(uint channelNumber, uint initialWindowSize, uint maximumPacketSize, ChannelOpenInfo info)
  57. {
  58. this.LocalChannelNumber = channelNumber;
  59. this.InitialWindowSize = initialWindowSize;
  60. this.MaximumPacketSize = maximumPacketSize;
  61. this.Info = info;
  62. }
  63. /// <summary>
  64. /// Called when type specific data need to be loaded.
  65. /// </summary>
  66. protected override void LoadData()
  67. {
  68. var channelName = this.ReadAsciiString();
  69. this.LocalChannelNumber = this.ReadUInt32();
  70. this.InitialWindowSize = this.ReadUInt32();
  71. this.MaximumPacketSize = this.ReadUInt32();
  72. var bytes = this.ReadBytes();
  73. if (channelName == SessionChannelOpenInfo.NAME)
  74. {
  75. this.Info = new SessionChannelOpenInfo();
  76. }
  77. else if (channelName == X11ChannelOpenInfo.NAME)
  78. {
  79. this.Info = new X11ChannelOpenInfo();
  80. }
  81. else if (channelName == DirectTcpipChannelInfo.NAME)
  82. {
  83. this.Info = new DirectTcpipChannelInfo();
  84. }
  85. else if (channelName == ForwardedTcpipChannelInfo.NAME)
  86. {
  87. this.Info = new ForwardedTcpipChannelInfo();
  88. }
  89. else
  90. {
  91. throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Channel type '{0}' is not supported.", channelName));
  92. }
  93. this.Info.Load(bytes);
  94. }
  95. /// <summary>
  96. /// Called when type specific data need to be saved.
  97. /// </summary>
  98. protected override void SaveData()
  99. {
  100. this.WriteAscii(this.ChannelType);
  101. this.Write(this.LocalChannelNumber);
  102. this.Write(this.InitialWindowSize);
  103. this.Write(this.MaximumPacketSize);
  104. this.Write(this.Info.GetBytes());
  105. }
  106. }
  107. }