SessionPacket.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* Copyright (C) 2014-2017 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 Utilities;
  11. namespace SMBLibrary.NetBios
  12. {
  13. /// <summary>
  14. /// [RFC 1002] 4.3.1. SESSION PACKET
  15. /// </summary>
  16. public abstract class SessionPacket
  17. {
  18. public const int HeaderLength = 4;
  19. public const int MaxSessionPacketLength = 131075;
  20. public SessionPacketTypeName Type;
  21. public byte Flags;
  22. private int TrailerLength; // 2 bytes + length extension bit
  23. public byte[] Trailer;
  24. public SessionPacket()
  25. {
  26. }
  27. public SessionPacket(byte[] buffer, int offset)
  28. {
  29. Type = (SessionPacketTypeName)ByteReader.ReadByte(buffer, offset + 0);
  30. Flags = ByteReader.ReadByte(buffer, offset + 1);
  31. TrailerLength = (Flags & 0x01) << 16 | BigEndianConverter.ToUInt16(buffer, offset + 2);
  32. Trailer = ByteReader.ReadBytes(buffer, offset + 4, TrailerLength);
  33. }
  34. public virtual byte[] GetBytes()
  35. {
  36. TrailerLength = this.Trailer.Length;
  37. if (TrailerLength > 0x1FFFF)
  38. {
  39. throw new ArgumentException("Invalid NBT packet length");
  40. }
  41. Flags = Convert.ToByte(TrailerLength > 0xFFFF);
  42. byte[] buffer = new byte[HeaderLength + Trailer.Length];
  43. ByteWriter.WriteByte(buffer, 0, (byte)Type);
  44. ByteWriter.WriteByte(buffer, 1, Flags);
  45. BigEndianWriter.WriteUInt16(buffer, 2, (ushort)(TrailerLength & 0xFFFF));
  46. ByteWriter.WriteBytes(buffer, 4, Trailer);
  47. return buffer;
  48. }
  49. public virtual int Length
  50. {
  51. get
  52. {
  53. return HeaderLength + Trailer.Length;
  54. }
  55. }
  56. public static SessionPacket GetSessionPacket(byte[] buffer, int offset)
  57. {
  58. SessionPacketTypeName type = (SessionPacketTypeName)ByteReader.ReadByte(buffer, offset);
  59. switch (type)
  60. {
  61. case SessionPacketTypeName.SessionMessage:
  62. return new SessionMessagePacket(buffer, offset);
  63. case SessionPacketTypeName.SessionRequest:
  64. return new SessionRequestPacket(buffer, offset);
  65. case SessionPacketTypeName.PositiveSessionResponse:
  66. return new PositiveSessionResponsePacket(buffer, offset);
  67. case SessionPacketTypeName.NegativeSessionResponse:
  68. return new NegativeSessionResponsePacket(buffer, offset);
  69. case SessionPacketTypeName.RetargetSessionResponse:
  70. return new SessionRetargetResponsePacket(buffer, offset);
  71. case SessionPacketTypeName.SessionKeepAlive:
  72. return new SessionKeepAlivePacket(buffer, offset);
  73. default:
  74. throw new InvalidDataException("Invalid NetBIOS session packet type: 0x" + type.ToString("X"));
  75. }
  76. }
  77. }
  78. }