QueryDirectoryRequest.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 QUERY_DIRECTORY Request
  14. /// </summary>
  15. public class QueryDirectoryRequest : SMB2Command
  16. {
  17. public const int FixedLength = 32;
  18. public const int DeclaredSize = 33;
  19. private ushort StructureSize;
  20. public FileInformationClass FileInformationClass;
  21. public QueryDirectoryFlags Flags;
  22. public uint FileIndex;
  23. public FileID FileId;
  24. private ushort FileNameOffset;
  25. private ushort FileNameLength;
  26. public uint OutputBufferLength;
  27. public string FileName = String.Empty;
  28. public QueryDirectoryRequest() : base(SMB2CommandName.QueryDirectory)
  29. {
  30. StructureSize = DeclaredSize;
  31. }
  32. public QueryDirectoryRequest(byte[] buffer, int offset) : base(buffer, offset)
  33. {
  34. StructureSize = LittleEndianConverter.ToUInt16(buffer, offset + SMB2Header.Length + 0);
  35. FileInformationClass = (FileInformationClass)ByteReader.ReadByte(buffer, offset + SMB2Header.Length + 2);
  36. Flags = (QueryDirectoryFlags)ByteReader.ReadByte(buffer, offset + SMB2Header.Length + 3);
  37. FileIndex = LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 4);
  38. FileId = new FileID(buffer, offset + SMB2Header.Length + 8);
  39. FileNameOffset = LittleEndianConverter.ToUInt16(buffer, offset + SMB2Header.Length + 24);
  40. FileNameLength = LittleEndianConverter.ToUInt16(buffer, offset + SMB2Header.Length + 26);
  41. OutputBufferLength = LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 28);
  42. FileName = ByteReader.ReadUTF16String(buffer, offset + FileNameOffset, FileNameLength / 2);
  43. }
  44. public override void WriteCommandBytes(byte[] buffer, int offset)
  45. {
  46. FileNameOffset = 0;
  47. FileNameLength = (ushort)(FileName.Length * 2);
  48. if (FileName.Length > 0)
  49. {
  50. FileNameOffset = SMB2Header.Length + FixedLength;
  51. }
  52. LittleEndianWriter.WriteUInt16(buffer, offset + 0, StructureSize);
  53. ByteWriter.WriteByte(buffer, offset + 2, (byte)FileInformationClass);
  54. ByteWriter.WriteByte(buffer, offset + 3, (byte)Flags);
  55. LittleEndianWriter.WriteUInt32(buffer, offset + 4, FileIndex);
  56. FileId.WriteBytes(buffer, offset + 8);
  57. LittleEndianWriter.WriteUInt16(buffer, offset + 24, FileNameOffset);
  58. LittleEndianWriter.WriteUInt16(buffer, offset + 26, FileNameLength);
  59. LittleEndianWriter.WriteUInt32(buffer, offset + 28, OutputBufferLength);
  60. ByteWriter.WriteUTF16String(buffer, offset + 32, FileName);
  61. }
  62. public bool Restart
  63. {
  64. get
  65. {
  66. return ((this.Flags & QueryDirectoryFlags.SMB2_RESTART_SCANS) > 0);
  67. }
  68. set
  69. {
  70. if (value)
  71. {
  72. Flags |= QueryDirectoryFlags.SMB2_RESTART_SCANS;
  73. }
  74. else
  75. {
  76. Flags &= ~QueryDirectoryFlags.SMB2_RESTART_SCANS;
  77. }
  78. }
  79. }
  80. public bool ReturnSingleEntry
  81. {
  82. get
  83. {
  84. return ((this.Flags & QueryDirectoryFlags.SMB2_RETURN_SINGLE_ENTRY) > 0);
  85. }
  86. set
  87. {
  88. if (value)
  89. {
  90. Flags |= QueryDirectoryFlags.SMB2_RETURN_SINGLE_ENTRY;
  91. }
  92. else
  93. {
  94. Flags &= ~QueryDirectoryFlags.SMB2_RETURN_SINGLE_ENTRY;
  95. }
  96. }
  97. }
  98. public bool Reopen
  99. {
  100. get
  101. {
  102. return ((this.Flags & QueryDirectoryFlags.SMB2_REOPEN) > 0);
  103. }
  104. set
  105. {
  106. if (value)
  107. {
  108. Flags |= QueryDirectoryFlags.SMB2_REOPEN;
  109. }
  110. else
  111. {
  112. Flags &= ~QueryDirectoryFlags.SMB2_REOPEN;
  113. }
  114. }
  115. }
  116. public override int CommandLength
  117. {
  118. get
  119. {
  120. return FixedLength + FileName.Length * 2;
  121. }
  122. }
  123. }
  124. }