RequestPDU.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* Copyright (C) 2014-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 System.Text;
  10. using Utilities;
  11. namespace SMBLibrary.RPC
  12. {
  13. /// <summary>
  14. /// rpcconn_request_hdr_t
  15. /// </summary>
  16. public class RequestPDU : RPCPDU
  17. {
  18. public const int RequestFieldsFixedLength = 8;
  19. public uint AllocationHint; // alloc_hint
  20. public ushort ContextID;
  21. public ushort OpNum;
  22. public Guid ObjectGuid; // Optional field
  23. public byte[] Data;
  24. public byte[] AuthVerifier;
  25. public RequestPDU() : base()
  26. {
  27. PacketType = PacketTypeName.Request;
  28. AuthVerifier = new byte[0];
  29. }
  30. public RequestPDU(byte[] buffer, int offset) : base(buffer, offset)
  31. {
  32. offset += CommonFieldsLength;
  33. AllocationHint = LittleEndianReader.ReadUInt32(buffer, ref offset);
  34. ContextID = LittleEndianReader.ReadUInt16(buffer, ref offset);
  35. OpNum = LittleEndianReader.ReadUInt16(buffer, ref offset);
  36. if ((Flags & PacketFlags.ObjectUUID) > 0)
  37. {
  38. ObjectGuid = LittleEndianReader.ReadGuid(buffer, ref offset);
  39. }
  40. int dataLength = FragmentLength - AuthLength - offset;
  41. Data = ByteReader.ReadBytes(buffer, ref offset, dataLength);
  42. AuthVerifier = ByteReader.ReadBytes(buffer, offset, AuthLength);
  43. }
  44. public override byte[] GetBytes()
  45. {
  46. AuthLength = (ushort)AuthVerifier.Length;
  47. byte[] buffer = new byte[Length];
  48. WriteCommonFieldsBytes(buffer);
  49. int offset = CommonFieldsLength;
  50. LittleEndianWriter.WriteUInt32(buffer, ref offset, AllocationHint);
  51. LittleEndianWriter.WriteUInt16(buffer, ref offset, ContextID);
  52. LittleEndianWriter.WriteUInt16(buffer, ref offset, OpNum);
  53. if ((Flags & PacketFlags.ObjectUUID) > 0)
  54. {
  55. LittleEndianWriter.WriteGuidBytes(buffer, ref offset, ObjectGuid);
  56. }
  57. ByteWriter.WriteBytes(buffer, ref offset, Data);
  58. ByteWriter.WriteBytes(buffer, ref offset, AuthVerifier);
  59. return buffer;
  60. }
  61. public override int Length
  62. {
  63. get
  64. {
  65. int length = CommonFieldsLength + RequestFieldsFixedLength + Data.Length + AuthVerifier.Length;
  66. if ((Flags & PacketFlags.ObjectUUID) > 0)
  67. {
  68. length += 16;
  69. }
  70. return length;
  71. }
  72. }
  73. }
  74. }