NodeStatusResponse.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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.IO;
  10. using System.Text;
  11. using Utilities;
  12. namespace SMBLibrary.NetBios
  13. {
  14. /// <summary>
  15. /// [RFC 1002] 4.2.18. NODE STATUS RESPONSE
  16. /// </summary>
  17. public class NodeStatusResponse
  18. {
  19. public NameServicePacketHeader Header;
  20. public ResourceRecord Resource;
  21. // byte NumberOfNames;
  22. public KeyValuePairList<string, NameFlags> Names = new KeyValuePairList<string, NameFlags>();
  23. public NodeStatistics Statistics;
  24. public NodeStatusResponse()
  25. {
  26. Header = new NameServicePacketHeader();
  27. Header.OpCode = NameServiceOperation.QueryResponse;
  28. Header.Flags = OperationFlags.AuthoritativeAnswer | OperationFlags.RecursionAvailable;
  29. Header.ANCount = 1;
  30. Resource = new ResourceRecord();
  31. Resource.Type = NameRecordType.NBStat;
  32. Statistics = new NodeStatistics();
  33. }
  34. public byte[] GetBytes()
  35. {
  36. Resource.Data = GetData();
  37. MemoryStream stream = new MemoryStream();
  38. Header.WriteBytes(stream);
  39. Resource.WriteBytes(stream);
  40. return stream.ToArray();
  41. }
  42. public byte[] GetData()
  43. {
  44. MemoryStream stream = new MemoryStream();
  45. stream.WriteByte((byte)Names.Count);
  46. foreach (KeyValuePair<string, NameFlags> entry in Names)
  47. {
  48. ByteWriter.WriteAnsiString(stream, entry.Key);
  49. //byte[] encodedName = NetBiosUtils.EncodeName(entry.Key, String.Empty);
  50. //ByteWriter.WriteBytes(stream, encodedName);
  51. BigEndianWriter.WriteUInt16(stream, entry.Value.Value);
  52. }
  53. ByteWriter.WriteBytes(stream, Statistics.GetBytes());
  54. return stream.ToArray();
  55. }
  56. }
  57. }