PositiveNameQueryResponse.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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.IO;
  10. using System.Text;
  11. using Utilities;
  12. namespace SMBLibrary.NetBios
  13. {
  14. /// <summary>
  15. /// [RFC 1002] 4.2.13. POSITIVE NAME QUERY RESPONSE
  16. /// </summary>
  17. public class PositiveNameQueryResponse
  18. {
  19. public const int EntryLength = 6;
  20. public NameServicePacketHeader Header;
  21. public ResourceRecord Resource;
  22. public KeyValuePairList<byte[], NameFlags> Addresses = new KeyValuePairList<byte[], NameFlags>();
  23. public PositiveNameQueryResponse()
  24. {
  25. Header = new NameServicePacketHeader();
  26. Header.Flags = OperationFlags.AuthoritativeAnswer | OperationFlags.RecursionDesired;
  27. Header.OpCode = NameServiceOperation.QueryResponse;
  28. Header.ANCount = 1;
  29. Resource = new ResourceRecord();
  30. }
  31. public byte[] GetBytes()
  32. {
  33. Resource.Data = GetData();
  34. MemoryStream stream = new MemoryStream();
  35. Header.WriteBytes(stream);
  36. Resource.WriteBytes(stream);
  37. return stream.ToArray();
  38. }
  39. public byte[] GetData()
  40. {
  41. byte[] data = new byte[EntryLength * Addresses.Count];
  42. int offset = 0;
  43. foreach (KeyValuePair<byte[], NameFlags> entry in Addresses)
  44. {
  45. BigEndianWriter.WriteUInt16(data, ref offset, (ushort)entry.Value);
  46. ByteWriter.WriteBytes(data, ref offset, entry.Key, 4);
  47. }
  48. return data;
  49. }
  50. }
  51. }