NodeStatusResponse.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* Copyright (C) 2014-2020 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. // Resource Data:
  22. // byte NumberOfNames;
  23. public KeyValuePairList<string, NameFlags> Names = new KeyValuePairList<string, NameFlags>();
  24. public NodeStatistics Statistics;
  25. public NodeStatusResponse()
  26. {
  27. Header = new NameServicePacketHeader();
  28. Header.OpCode = NameServiceOperation.QueryResponse;
  29. Header.Flags = OperationFlags.AuthoritativeAnswer | OperationFlags.RecursionAvailable;
  30. Header.ANCount = 1;
  31. Resource = new ResourceRecord(NameRecordType.NBStat);
  32. Statistics = new NodeStatistics();
  33. }
  34. public NodeStatusResponse(byte[] buffer, int offset)
  35. {
  36. Header = new NameServicePacketHeader(buffer, ref offset);
  37. Resource = new ResourceRecord(buffer, ref offset);
  38. int position = 0;
  39. byte numberOfNames = ByteReader.ReadByte(Resource.Data, ref position);
  40. for (int index = 0; index < numberOfNames; index++)
  41. {
  42. string name = ByteReader.ReadAnsiString(Resource.Data, ref position, 16);
  43. NameFlags nameFlags = (NameFlags)BigEndianReader.ReadUInt16(Resource.Data, ref position);
  44. Names.Add(name, nameFlags);
  45. }
  46. Statistics = new NodeStatistics(Resource.Data, ref position);
  47. }
  48. public byte[] GetBytes()
  49. {
  50. Resource.Data = GetData();
  51. MemoryStream stream = new MemoryStream();
  52. Header.WriteBytes(stream);
  53. Resource.WriteBytes(stream);
  54. return stream.ToArray();
  55. }
  56. private byte[] GetData()
  57. {
  58. MemoryStream stream = new MemoryStream();
  59. stream.WriteByte((byte)Names.Count);
  60. foreach (KeyValuePair<string, NameFlags> entry in Names)
  61. {
  62. ByteWriter.WriteAnsiString(stream, entry.Key);
  63. BigEndianWriter.WriteUInt16(stream, (ushort)entry.Value);
  64. }
  65. ByteWriter.WriteBytes(stream, Statistics.GetBytes());
  66. return stream.ToArray();
  67. }
  68. }
  69. }