TreeConnectRequest.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 TREE_CONNECT Request
  14. /// </summary>
  15. public class TreeConnectRequest : SMB2Command
  16. {
  17. public const int FixedSize = 8;
  18. public const int DeclaredSize = 9;
  19. private ushort StructureSize;
  20. public ushort Reserved;
  21. private ushort PathOffset;
  22. private ushort PathLength;
  23. public string Path = String.Empty;
  24. public TreeConnectRequest() : base(SMB2CommandName.TreeConnect)
  25. {
  26. StructureSize = DeclaredSize;
  27. }
  28. public TreeConnectRequest(byte[] buffer, int offset) : base(buffer, offset)
  29. {
  30. StructureSize = LittleEndianConverter.ToUInt16(buffer, offset + SMB2Header.Length + 0);
  31. Reserved = LittleEndianConverter.ToUInt16(buffer, offset + SMB2Header.Length + 2);
  32. PathOffset = LittleEndianConverter.ToUInt16(buffer, offset + SMB2Header.Length + 4);
  33. PathLength = LittleEndianConverter.ToUInt16(buffer, offset + SMB2Header.Length + 6);
  34. if (PathLength > 0)
  35. {
  36. Path = ByteReader.ReadUTF16String(buffer, offset + PathOffset, PathLength / 2);
  37. }
  38. }
  39. public override void WriteCommandBytes(byte[] buffer, int offset)
  40. {
  41. PathOffset = 0;
  42. PathLength = (ushort)(Path.Length * 2);
  43. if (Path.Length > 0)
  44. {
  45. PathOffset = SMB2Header.Length + 8;
  46. }
  47. LittleEndianWriter.WriteUInt16(buffer, offset + 0, StructureSize);
  48. LittleEndianWriter.WriteUInt16(buffer, offset + 2, Reserved);
  49. LittleEndianWriter.WriteUInt16(buffer, offset + 4, PathOffset);
  50. LittleEndianWriter.WriteUInt16(buffer, offset + 6, PathLength);
  51. if (Path.Length > 0)
  52. {
  53. ByteWriter.WriteUTF16String(buffer, offset + 8, Path);
  54. }
  55. }
  56. public override int CommandLength
  57. {
  58. get
  59. {
  60. return 8 + Path.Length * 2;
  61. }
  62. }
  63. }
  64. }