BindAckPDU.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 ushort MaxTransmitFragmentSize; // max_xmit_frag
  19. public ushort MaxReceiveFragmentSize; // max_recv_frag
  20. public uint AssociationGroupID; // assoc_group_id
  21. public string SecondaryAddress; // sec_addr (port_any_t)
  22. // Padding (alignment to 4 byte boundary)
  23. public ResultList ResultList; // p_result_list
  24. public byte[] AuthVerifier;
  25. public BindAckPDU() : base()
  26. {
  27. PacketType = PacketTypeName.BindAck;
  28. SecondaryAddress = String.Empty;
  29. ResultList = new ResultList();
  30. AuthVerifier = new byte[0];
  31. }
  32. public BindAckPDU(byte[] buffer, int offset) : base(buffer, offset)
  33. {
  34. int startOffset = offset;
  35. offset += RPCPDU.CommonFieldsLength;
  36. MaxTransmitFragmentSize = LittleEndianReader.ReadUInt16(buffer, ref offset);
  37. MaxReceiveFragmentSize = LittleEndianReader.ReadUInt16(buffer, ref offset);
  38. AssociationGroupID = LittleEndianReader.ReadUInt32(buffer, ref offset);
  39. SecondaryAddress = RPCHelper.ReadPortAddress(buffer, ref offset);
  40. int padding = (4 - ((offset - startOffset) % 4)) % 4;
  41. offset += padding;
  42. ResultList = new ResultList(buffer, offset);
  43. offset += ResultList.Length;
  44. AuthVerifier = ByteReader.ReadBytes(buffer, offset, AuthLength);
  45. }
  46. public override byte[] GetBytes()
  47. {
  48. AuthLength = (ushort)AuthVerifier.Length;
  49. int padding = (4 - ((SecondaryAddress.Length + 3) % 4)) % 4;
  50. FragmentLength = (ushort)(RPCPDU.CommonFieldsLength + 8 + SecondaryAddress.Length + 3 + padding + ResultList.Length + AuthLength);
  51. byte[] buffer = new byte[FragmentLength];
  52. WriteCommonFieldsBytes(buffer);
  53. int offset = RPCPDU.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. }
  64. }