ErrorResponse.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. public 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. Header.Status = status;
  33. StructureSize = DeclaredSize;
  34. }
  35. public ErrorResponse(byte[] buffer, int offset) : base(buffer, offset)
  36. {
  37. StructureSize = LittleEndianConverter.ToUInt16(buffer, offset + SMB2Header.Length + 0);
  38. ErrorContextCount = ByteReader.ReadByte(buffer, offset + SMB2Header.Length + 2);
  39. Reserved = ByteReader.ReadByte(buffer, offset + SMB2Header.Length + 3);
  40. ByteCount = LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 4);
  41. ErrorData = ByteReader.ReadBytes(buffer, offset + SMB2Header.Length + 8, (int)ByteCount);
  42. }
  43. public override void WriteCommandBytes(byte[] buffer, int offset)
  44. {
  45. ByteCount = (uint)ErrorData.Length;
  46. LittleEndianWriter.WriteUInt16(buffer, offset + 0, StructureSize);
  47. ByteWriter.WriteByte(buffer, offset + 2, ErrorContextCount);
  48. ByteWriter.WriteByte(buffer, offset + 3, Reserved);
  49. LittleEndianWriter.WriteUInt32(buffer, offset + 4, ByteCount);
  50. if (ErrorData.Length > 0)
  51. {
  52. ByteWriter.WriteBytes(buffer, offset + 8, ErrorData);
  53. }
  54. else
  55. {
  56. // 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
  57. ByteWriter.WriteBytes(buffer, offset + 8, new byte[1]);
  58. }
  59. }
  60. public override int CommandLength
  61. {
  62. get
  63. {
  64. // If the ByteCount field is zero then the server MUST supply an ErrorData field that is one byte in length
  65. return FixedSize + Math.Max(ErrorData.Length, 1);
  66. }
  67. }
  68. }
  69. }