CreateResponse.cs 4.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 CREATE Response
  14. /// </summary>
  15. public class CreateResponse : SMB2Command
  16. {
  17. public const int DeclaredSize = 89;
  18. private ushort StructureSize;
  19. public OplockLevel OplockLevel;
  20. public CreateResponseFlags Flags;
  21. public CreateAction CreateAction;
  22. public DateTime? CreationTime;
  23. public DateTime? LastAccessTime;
  24. public DateTime? LastWriteTime;
  25. public DateTime? ChangeTime;
  26. public long AllocationSize;
  27. public long EndofFile;
  28. public FileAttributes FileAttributes;
  29. public uint Reserved2;
  30. public FileID FileId;
  31. private uint CreateContextsOffsets;
  32. private uint CreateContextsLength;
  33. public List<CreateContext> CreateContexts = new List<CreateContext>();
  34. public CreateResponse() : base(SMB2CommandName.Create)
  35. {
  36. Header.IsResponse = true;
  37. StructureSize = DeclaredSize;
  38. }
  39. public CreateResponse(byte[] buffer, int offset) : base(buffer, offset)
  40. {
  41. StructureSize = LittleEndianConverter.ToUInt16(buffer, offset + SMB2Header.Length + 0);
  42. OplockLevel = (OplockLevel)ByteReader.ReadByte(buffer, offset + SMB2Header.Length + 2);
  43. Flags = (CreateResponseFlags)ByteReader.ReadByte(buffer, offset + SMB2Header.Length + 3);
  44. CreateAction = (CreateAction)LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 4);
  45. CreationTime = FileTimeHelper.ReadNullableFileTime(buffer, offset + SMB2Header.Length + 8);
  46. LastAccessTime = FileTimeHelper.ReadNullableFileTime(buffer, offset + SMB2Header.Length + 16);
  47. LastWriteTime = FileTimeHelper.ReadNullableFileTime(buffer, offset + SMB2Header.Length + 24);
  48. ChangeTime = FileTimeHelper.ReadNullableFileTime(buffer, offset + SMB2Header.Length + 32);
  49. AllocationSize = LittleEndianConverter.ToInt64(buffer, offset + SMB2Header.Length + 40);
  50. EndofFile = LittleEndianConverter.ToInt64(buffer, offset + SMB2Header.Length + 48);
  51. FileAttributes = (FileAttributes)LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 56);
  52. Reserved2 = LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 60);
  53. FileId = new FileID(buffer, offset + SMB2Header.Length + 64);
  54. CreateContextsOffsets = LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 80);
  55. CreateContextsLength = LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 84);
  56. if (CreateContextsLength > 0)
  57. {
  58. CreateContexts = CreateContext.ReadCreateContextList(buffer, offset + (int)CreateContextsOffsets);
  59. }
  60. }
  61. public override void WriteCommandBytes(byte[] buffer, int offset)
  62. {
  63. LittleEndianWriter.WriteUInt16(buffer, offset + 0, StructureSize);
  64. ByteWriter.WriteByte(buffer, offset + 2, (byte)OplockLevel);
  65. ByteWriter.WriteByte(buffer, offset + 3, (byte)Flags);
  66. LittleEndianWriter.WriteUInt32(buffer, offset + 4, (uint)CreateAction);
  67. FileTimeHelper.WriteFileTime(buffer, offset + 8, CreationTime);
  68. FileTimeHelper.WriteFileTime(buffer, offset + 16, LastAccessTime);
  69. FileTimeHelper.WriteFileTime(buffer, offset + 24, LastWriteTime);
  70. FileTimeHelper.WriteFileTime(buffer, offset + 32, ChangeTime);
  71. LittleEndianWriter.WriteInt64(buffer, offset + 40, AllocationSize);
  72. LittleEndianWriter.WriteInt64(buffer, offset + 48, EndofFile);
  73. LittleEndianWriter.WriteUInt32(buffer, offset + 56, (uint)FileAttributes);
  74. LittleEndianWriter.WriteUInt32(buffer, offset + 60, Reserved2);
  75. FileId.WriteBytes(buffer, offset + 64);
  76. CreateContextsOffsets = 0;
  77. CreateContextsLength = (uint)CreateContext.GetCreateContextListLength(CreateContexts);
  78. if (CreateContexts.Count > 0)
  79. {
  80. CreateContextsOffsets = SMB2Header.Length + 88;
  81. CreateContext.WriteCreateContextList(buffer, 88, CreateContexts);
  82. }
  83. }
  84. public override int CommandLength
  85. {
  86. get
  87. {
  88. return 88 + CreateContext.GetCreateContextListLength(CreateContexts);
  89. }
  90. }
  91. }
  92. }