ResourceRecord.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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.1.3. RESOURCE RECORD
  16. /// </summary>
  17. public class ResourceRecord
  18. {
  19. public string Name;
  20. public NameRecordType Type = NameRecordType.NB; // NB
  21. public ushort Class = 0x0001; // IN
  22. public uint TTL;
  23. // ushort DataLength
  24. public byte[] Data;
  25. public ResourceRecord()
  26. {
  27. Name = String.Empty;
  28. TTL = (uint)new TimeSpan(7, 0, 0, 0).TotalSeconds;
  29. Data = new byte[0];
  30. }
  31. public void WriteBytes(Stream stream)
  32. {
  33. WriteBytes(stream, null);
  34. }
  35. public void WriteBytes(Stream stream, int? nameOffset)
  36. {
  37. if (nameOffset.HasValue)
  38. {
  39. NetBiosUtils.WriteNamePointer(stream, nameOffset.Value);
  40. }
  41. else
  42. {
  43. byte[] encodedName = NetBiosUtils.EncodeName(Name, String.Empty);
  44. ByteWriter.WriteBytes(stream, encodedName);
  45. }
  46. BigEndianWriter.WriteUInt16(stream, (ushort)Type);
  47. BigEndianWriter.WriteUInt16(stream, Class);
  48. BigEndianWriter.WriteUInt32(stream, TTL);
  49. BigEndianWriter.WriteUInt16(stream, (ushort)Data.Length);
  50. ByteWriter.WriteBytes(stream, Data);
  51. }
  52. }
  53. }