NameServicePacketHeader.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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.1. HEADER
  16. /// </summary>
  17. public class NameServicePacketHeader
  18. {
  19. public const int Length = 12;
  20. public ushort TransactionID;
  21. public NameServiceOperation OpCode;
  22. public OperationFlags Flags;
  23. public byte ResultCode;
  24. public ushort QDCount;
  25. public ushort ANCount;
  26. public ushort NSCount;
  27. public ushort ARCount;
  28. public NameServicePacketHeader()
  29. {
  30. }
  31. public NameServicePacketHeader(byte[] buffer, ref int offset) : this(buffer, offset)
  32. {
  33. offset += Length;
  34. }
  35. public NameServicePacketHeader(byte[] buffer, int offset)
  36. {
  37. TransactionID = BigEndianConverter.ToUInt16(buffer, offset + 0);
  38. ushort temp = BigEndianConverter.ToUInt16(buffer, offset + 2);
  39. ResultCode = (byte)(temp & 0xF);
  40. Flags = (OperationFlags)((temp >> 4) & 0x7F);
  41. OpCode = (NameServiceOperation)((temp >> 11) & 0x1F);
  42. QDCount = BigEndianConverter.ToUInt16(buffer, offset + 4);
  43. ANCount = BigEndianConverter.ToUInt16(buffer, offset + 6);
  44. NSCount = BigEndianConverter.ToUInt16(buffer, offset + 8);
  45. ARCount = BigEndianConverter.ToUInt16(buffer, offset + 10);
  46. }
  47. public void WriteBytes(Stream stream)
  48. {
  49. BigEndianWriter.WriteUInt16(stream, TransactionID);
  50. ushort temp = (ushort)(ResultCode & (0xF));
  51. temp |= (ushort)((byte)Flags << 4);
  52. temp |= (ushort)((byte)OpCode << 11);
  53. BigEndianWriter.WriteUInt16(stream, temp);
  54. BigEndianWriter.WriteUInt16(stream, QDCount);
  55. BigEndianWriter.WriteUInt16(stream, ANCount);
  56. BigEndianWriter.WriteUInt16(stream, NSCount);
  57. BigEndianWriter.WriteUInt16(stream, ARCount);
  58. }
  59. }
  60. }