BindAckPDU.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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_bind_ack_hdr_t
  15. /// </summary>
  16. public class BindAckPDU : RPCPDU
  17. {
  18. public const int BindAckFieldsFixedLength = 8;
  19. public ushort MaxTransmitFragmentSize; // max_xmit_frag
  20. public ushort MaxReceiveFragmentSize; // max_recv_frag
  21. public uint AssociationGroupID; // assoc_group_id
  22. public string SecondaryAddress; // sec_addr (port_any_t)
  23. // Padding (alignment to 4 byte boundary)
  24. public ResultList ResultList; // p_result_list
  25. public byte[] AuthVerifier;
  26. public BindAckPDU() : base()
  27. {
  28. PacketType = PacketTypeName.BindAck;
  29. SecondaryAddress = String.Empty;
  30. ResultList = new ResultList();
  31. AuthVerifier = new byte[0];
  32. }
  33. public BindAckPDU(byte[] buffer, int offset) : base(buffer, offset)
  34. {
  35. int startOffset = offset;
  36. offset += CommonFieldsLength;
  37. MaxTransmitFragmentSize = LittleEndianReader.ReadUInt16(buffer, ref offset);
  38. MaxReceiveFragmentSize = LittleEndianReader.ReadUInt16(buffer, ref offset);
  39. AssociationGroupID = LittleEndianReader.ReadUInt32(buffer, ref offset);
  40. SecondaryAddress = RPCHelper.ReadPortAddress(buffer, ref offset);
  41. int padding = (4 - ((offset - startOffset) % 4)) % 4;
  42. offset += padding;
  43. ResultList = new ResultList(buffer, offset);
  44. offset += ResultList.Length;
  45. AuthVerifier = ByteReader.ReadBytes(buffer, offset, AuthLength);
  46. }
  47. public override byte[] GetBytes()
  48. {
  49. AuthLength = (ushort)AuthVerifier.Length;
  50. int padding = (4 - ((SecondaryAddress.Length + 3) % 4)) % 4;
  51. byte[] buffer = new byte[Length];
  52. WriteCommonFieldsBytes(buffer);
  53. int offset = CommonFieldsLength;
  54. LittleEndianWriter.WriteUInt16(buffer, ref offset, MaxTransmitFragmentSize);
  55. LittleEndianWriter.WriteUInt16(buffer, ref offset, MaxReceiveFragmentSize);
  56. LittleEndianWriter.WriteUInt32(buffer, ref offset, AssociationGroupID);
  57. RPCHelper.WritePortAddress(buffer, ref offset, SecondaryAddress);
  58. offset += padding;
  59. ResultList.WriteBytes(buffer, ref offset);
  60. ByteWriter.WriteBytes(buffer, offset, AuthVerifier);
  61. return buffer;
  62. }
  63. public override int Length
  64. {
  65. get
  66. {
  67. int padding = (4 - ((SecondaryAddress.Length + 3) % 4)) % 4;
  68. return CommonFieldsLength + BindAckFieldsFixedLength + SecondaryAddress.Length + 3 + padding + ResultList.Length + AuthLength;
  69. }
  70. }
  71. }
  72. }