ResourceRecord.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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.1.3. RESOURCE RECORD
  16. /// </summary>
  17. public class ResourceRecord
  18. {
  19. public string Name;
  20. public NameRecordType Type;
  21. public ResourceRecordClass Class;
  22. public uint TTL;
  23. // ushort DataLength
  24. public byte[] Data;
  25. public ResourceRecord(NameRecordType type)
  26. {
  27. Name = String.Empty;
  28. Type = type;
  29. Class = ResourceRecordClass.In;
  30. TTL = (uint)new TimeSpan(7, 0, 0, 0).TotalSeconds;
  31. Data = new byte[0];
  32. }
  33. public ResourceRecord(byte[] buffer, ref int offset)
  34. {
  35. Name = NetBiosUtils.DecodeName(buffer, ref offset);
  36. Type = (NameRecordType)BigEndianReader.ReadUInt16(buffer, ref offset);
  37. Class = (ResourceRecordClass)BigEndianReader.ReadUInt16(buffer, ref offset);
  38. TTL = BigEndianReader.ReadUInt32(buffer, ref offset);
  39. ushort dataLength = BigEndianReader.ReadUInt16(buffer, ref offset);
  40. Data = ByteReader.ReadBytes(buffer, ref offset, dataLength);
  41. }
  42. public void WriteBytes(Stream stream)
  43. {
  44. WriteBytes(stream, null);
  45. }
  46. public void WriteBytes(Stream stream, int? nameOffset)
  47. {
  48. if (nameOffset.HasValue)
  49. {
  50. NetBiosUtils.WriteNamePointer(stream, nameOffset.Value);
  51. }
  52. else
  53. {
  54. byte[] encodedName = NetBiosUtils.EncodeName(Name, String.Empty);
  55. ByteWriter.WriteBytes(stream, encodedName);
  56. }
  57. BigEndianWriter.WriteUInt16(stream, (ushort)Type);
  58. BigEndianWriter.WriteUInt16(stream, (ushort)Class);
  59. BigEndianWriter.WriteUInt32(stream, TTL);
  60. BigEndianWriter.WriteUInt16(stream, (ushort)Data.Length);
  61. ByteWriter.WriteBytes(stream, Data);
  62. }
  63. }
  64. }