GlobalRequestMessage.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. namespace Renci.SshNet.Messages.Connection
  3. {
  4. /// <summary>
  5. /// Represents SSH_MSG_GLOBAL_REQUEST message.
  6. /// </summary>
  7. [Message("SSH_MSG_GLOBAL_REQUEST", 80)]
  8. public class GlobalRequestMessage : Message
  9. {
  10. /// <summary>
  11. /// Gets the name of the request.
  12. /// </summary>
  13. /// <value>
  14. /// The name of the request.
  15. /// </value>
  16. public GlobalRequestName RequestName { get; private set; }
  17. /// <summary>
  18. /// Gets a value indicating whether message reply should be sent..
  19. /// </summary>
  20. /// <value>
  21. /// <c>true</c> if message reply should be sent; otherwise, <c>false</c>.
  22. /// </value>
  23. public bool WantReply { get; private set; }
  24. /// <summary>
  25. /// Gets the address to bind to.
  26. /// </summary>
  27. public string AddressToBind { get; private set; }
  28. // TODO: Extract AddressToBind property to be in different class and GlobalREquestMessage to be a base class fo it.
  29. /// <summary>
  30. /// Gets port number to bind to.
  31. /// </summary>
  32. public UInt32 PortToBind { get; private set; }
  33. /// <summary>
  34. /// Initializes a new instance of the <see cref="GlobalRequestMessage"/> class.
  35. /// </summary>
  36. public GlobalRequestMessage()
  37. {
  38. }
  39. /// <summary>
  40. /// Initializes a new instance of the <see cref="GlobalRequestMessage"/> class.
  41. /// </summary>
  42. /// <param name="requestName">Name of the request.</param>
  43. /// <param name="wantReply">if set to <c>true</c> [want reply].</param>
  44. public GlobalRequestMessage(GlobalRequestName requestName, bool wantReply)
  45. {
  46. this.RequestName = requestName;
  47. this.WantReply = wantReply;
  48. }
  49. /// <summary>
  50. /// Initializes a new instance of the <see cref="GlobalRequestMessage"/> class.
  51. /// </summary>
  52. /// <param name="requestName">Name of the request.</param>
  53. /// <param name="wantReply">if set to <c>true</c> [want reply].</param>
  54. /// <param name="addressToBind">The address to bind.</param>
  55. /// <param name="portToBind">The port to bind.</param>
  56. public GlobalRequestMessage(GlobalRequestName requestName, bool wantReply, string addressToBind, uint portToBind)
  57. : this(requestName, wantReply)
  58. {
  59. this.AddressToBind = addressToBind;
  60. this.PortToBind = portToBind;
  61. }
  62. /// <summary>
  63. /// Called when type specific data need to be loaded.
  64. /// </summary>
  65. protected override void LoadData()
  66. {
  67. var requestName = this.ReadAsciiString();
  68. switch (requestName)
  69. {
  70. case "tcpip-forward":
  71. this.RequestName = GlobalRequestName.TcpIpForward;
  72. break;
  73. case "cancel-tcpip-forward":
  74. this.RequestName = GlobalRequestName.CancelTcpIpForward;
  75. break;
  76. default:
  77. break;
  78. }
  79. this.WantReply = this.ReadBoolean();
  80. this.AddressToBind = this.ReadString();
  81. this.PortToBind = this.ReadUInt32();
  82. }
  83. /// <summary>
  84. /// Called when type specific data need to be saved.
  85. /// </summary>
  86. protected override void SaveData()
  87. {
  88. switch (this.RequestName)
  89. {
  90. case GlobalRequestName.TcpIpForward:
  91. this.WriteAscii("tcpip-forward");
  92. break;
  93. case GlobalRequestName.CancelTcpIpForward:
  94. this.WriteAscii("cancel-tcpip-forward");
  95. break;
  96. default:
  97. break;
  98. }
  99. this.Write(this.WantReply);
  100. switch (this.RequestName)
  101. {
  102. case GlobalRequestName.TcpIpForward:
  103. case GlobalRequestName.CancelTcpIpForward:
  104. this.Write(this.AddressToBind);
  105. this.Write(this.PortToBind);
  106. break;
  107. default:
  108. break;
  109. }
  110. }
  111. }
  112. }