RequestPDU.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. FragmentLength = (ushort)(CommonFieldsLength + RequestFieldsFixedLength + Data.Length + AuthVerifier.Length);
  48. if ((Flags & PacketFlags.ObjectUUID) > 0)
  49. {
  50. FragmentLength += 16;
  51. }
  52. byte[] buffer = new byte[FragmentLength];
  53. WriteCommonFieldsBytes(buffer);
  54. int offset = CommonFieldsLength;
  55. LittleEndianWriter.WriteUInt32(buffer, ref offset, AllocationHint);
  56. LittleEndianWriter.WriteUInt16(buffer, ref offset, ContextID);
  57. LittleEndianWriter.WriteUInt16(buffer, ref offset, OpNum);
  58. if ((Flags & PacketFlags.ObjectUUID) > 0)
  59. {
  60. LittleEndianWriter.WriteGuidBytes(buffer, ref offset, ObjectGuid);
  61. }
  62. ByteWriter.WriteBytes(buffer, ref offset, Data);
  63. ByteWriter.WriteBytes(buffer, ref offset, AuthVerifier);
  64. return buffer;
  65. }
  66. }
  67. }