IgnoreMessage.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Globalization;
  3. using Renci.SshNet.Abstractions;
  4. using Renci.SshNet.Common;
  5. namespace Renci.SshNet.Messages.Transport
  6. {
  7. /// <summary>
  8. /// Represents SSH_MSG_IGNORE message.
  9. /// </summary>
  10. [Message("SSH_MSG_IGNORE", MessageNumber)]
  11. public class IgnoreMessage : Message
  12. {
  13. internal const byte MessageNumber = 2;
  14. /// <summary>
  15. /// Gets ignore message data if any.
  16. /// </summary>
  17. public byte[] Data { get; private set; }
  18. /// <summary>
  19. /// Initializes a new instance of the <see cref="IgnoreMessage"/> class
  20. /// </summary>
  21. public IgnoreMessage()
  22. {
  23. Data = Array<byte>.Empty;
  24. }
  25. /// <summary>
  26. /// Gets the size of the message in bytes.
  27. /// </summary>
  28. /// <value>
  29. /// The size of the messages in bytes.
  30. /// </value>
  31. protected override int BufferCapacity
  32. {
  33. get
  34. {
  35. var capacity = base.BufferCapacity;
  36. capacity += 4; // Data length
  37. capacity += Data.Length; // Data
  38. return capacity;
  39. }
  40. }
  41. /// <summary>
  42. /// Initializes a new instance of the <see cref="IgnoreMessage"/> class.
  43. /// </summary>
  44. /// <param name="data">The data.</param>
  45. public IgnoreMessage(byte[] data)
  46. {
  47. if (data == null)
  48. throw new ArgumentNullException("data");
  49. Data = data;
  50. }
  51. /// <summary>
  52. /// Called when type specific data need to be loaded.
  53. /// </summary>
  54. protected override void LoadData()
  55. {
  56. var dataLength = ReadUInt32();
  57. if (dataLength > int.MaxValue)
  58. {
  59. throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Data longer than {0} is not supported.", int.MaxValue));
  60. }
  61. if (dataLength > (DataStream.Length - DataStream.Position))
  62. {
  63. DiagnosticAbstraction.Log("SSH_MSG_IGNORE: Length exceeds data bytes, data ignored.");
  64. Data = Array<byte>.Empty;
  65. }
  66. else
  67. {
  68. Data = ReadBytes((int) dataLength);
  69. }
  70. }
  71. /// <summary>
  72. /// Called when type specific data need to be saved.
  73. /// </summary>
  74. protected override void SaveData()
  75. {
  76. WriteBinaryString(Data);
  77. }
  78. internal override void Process(Session session)
  79. {
  80. session.OnIgnoreReceived(this);
  81. }
  82. }
  83. }