BindAckPDU.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* Copyright (C) 2014 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) : base(buffer)
  33. {
  34. int offset = RPCPDU.CommonFieldsLength;
  35. MaxTransmitFragmentSize = LittleEndianReader.ReadUInt16(buffer, ref offset);
  36. MaxReceiveFragmentSize = LittleEndianReader.ReadUInt16(buffer, ref offset);
  37. AssociationGroupID = LittleEndianReader.ReadUInt32(buffer, ref offset);
  38. SecondaryAddress = RPCHelper.ReadPortAddress(buffer, ref offset);
  39. int padding = (4 - (offset % 4)) % 4;
  40. offset += padding;
  41. ResultList = new ResultList(buffer, offset);
  42. offset += ResultList.Length;
  43. AuthVerifier = ByteReader.ReadBytes(buffer, offset, AuthLength);
  44. }
  45. public override byte[] GetBytes()
  46. {
  47. AuthLength = (ushort)AuthVerifier.Length;
  48. int padding = (4 - ((SecondaryAddress.Length + 3) % 4)) % 4;
  49. FragmentLength = (ushort)(RPCPDU.CommonFieldsLength + 8 + SecondaryAddress.Length + 3 + padding + ResultList.Length + AuthLength);
  50. byte[] buffer = new byte[FragmentLength];
  51. WriteCommonFieldsBytes(buffer);
  52. int offset = RPCPDU.CommonFieldsLength;
  53. LittleEndianWriter.WriteUInt16(buffer, ref offset, MaxTransmitFragmentSize);
  54. LittleEndianWriter.WriteUInt16(buffer, ref offset, MaxReceiveFragmentSize);
  55. LittleEndianWriter.WriteUInt32(buffer, ref offset, AssociationGroupID);
  56. RPCHelper.WritePortAddress(buffer, ref offset, SecondaryAddress);
  57. offset += padding;
  58. ResultList.WriteBytes(buffer, ref offset);
  59. ByteWriter.WriteBytes(buffer, offset, AuthVerifier);
  60. return buffer;
  61. }
  62. }
  63. }