BindAckPDU.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. FragmentLength = (ushort)(CommonFieldsLength + BindAckFieldsFixedLength + SecondaryAddress.Length + 3 + padding + ResultList.Length + AuthLength);
  52. byte[] buffer = new byte[FragmentLength];
  53. WriteCommonFieldsBytes(buffer);
  54. int offset = CommonFieldsLength;
  55. LittleEndianWriter.WriteUInt16(buffer, ref offset, MaxTransmitFragmentSize);
  56. LittleEndianWriter.WriteUInt16(buffer, ref offset, MaxReceiveFragmentSize);
  57. LittleEndianWriter.WriteUInt32(buffer, ref offset, AssociationGroupID);
  58. RPCHelper.WritePortAddress(buffer, ref offset, SecondaryAddress);
  59. offset += padding;
  60. ResultList.WriteBytes(buffer, ref offset);
  61. ByteWriter.WriteBytes(buffer, offset, AuthVerifier);
  62. return buffer;
  63. }
  64. }
  65. }