ErrorResponse.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* Copyright (C) 2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using Utilities;
  10. namespace SMBLibrary.SMB2
  11. {
  12. /// <summary>
  13. /// SMB2 ERROR Response
  14. /// </summary>
  15. public class ErrorResponse : SMB2Command
  16. {
  17. public const int FixedSize = 8;
  18. public const int DeclaredSize = 9;
  19. private ushort StructureSize;
  20. public byte ErrorContextCount;
  21. public byte Reserved;
  22. private uint ByteCount;
  23. public byte[] ErrorData = new byte[0];
  24. public ErrorResponse(SMB2CommandName commandName) : base(commandName)
  25. {
  26. Header.IsResponse = true;
  27. StructureSize = DeclaredSize;
  28. }
  29. public ErrorResponse(SMB2CommandName commandName, NTStatus status) : base(commandName)
  30. {
  31. Header.IsResponse = true;
  32. StructureSize = DeclaredSize;
  33. Header.Status = status;
  34. }
  35. public ErrorResponse(SMB2CommandName commandName, NTStatus status, byte[] errorData) : base(commandName)
  36. {
  37. Header.IsResponse = true;
  38. StructureSize = DeclaredSize;
  39. Header.Status = status;
  40. ErrorData = errorData;
  41. }
  42. public ErrorResponse(byte[] buffer, int offset) : base(buffer, offset)
  43. {
  44. StructureSize = LittleEndianConverter.ToUInt16(buffer, offset + SMB2Header.Length + 0);
  45. ErrorContextCount = ByteReader.ReadByte(buffer, offset + SMB2Header.Length + 2);
  46. Reserved = ByteReader.ReadByte(buffer, offset + SMB2Header.Length + 3);
  47. ByteCount = LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 4);
  48. ErrorData = ByteReader.ReadBytes(buffer, offset + SMB2Header.Length + 8, (int)ByteCount);
  49. }
  50. public override void WriteCommandBytes(byte[] buffer, int offset)
  51. {
  52. ByteCount = (uint)ErrorData.Length;
  53. LittleEndianWriter.WriteUInt16(buffer, offset + 0, StructureSize);
  54. ByteWriter.WriteByte(buffer, offset + 2, ErrorContextCount);
  55. ByteWriter.WriteByte(buffer, offset + 3, Reserved);
  56. LittleEndianWriter.WriteUInt32(buffer, offset + 4, ByteCount);
  57. if (ErrorData.Length > 0)
  58. {
  59. ByteWriter.WriteBytes(buffer, offset + 8, ErrorData);
  60. }
  61. else
  62. {
  63. // If the ByteCount field is zero then the server MUST supply an ErrorData field that is one byte in length, and SHOULD set that byte to zero
  64. ByteWriter.WriteBytes(buffer, offset + 8, new byte[1]);
  65. }
  66. }
  67. public override int CommandLength
  68. {
  69. get
  70. {
  71. // If the ByteCount field is zero then the server MUST supply an ErrorData field that is one byte in length
  72. return FixedSize + Math.Max(ErrorData.Length, 1);
  73. }
  74. }
  75. }
  76. }