NBTConnectionReceiveBuffer.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. public class NBTConnectionReceiveBuffer
  14. {
  15. private byte[] m_buffer;
  16. private int m_readOffset = 0;
  17. private int m_bytesInBuffer = 0;
  18. private int? m_packetLength;
  19. public NBTConnectionReceiveBuffer() : this(SessionPacket.MaxSessionPacketLength)
  20. {
  21. }
  22. /// <param name="bufferLength">Must be large enough to hold the largest possible NBT packet</param>
  23. public NBTConnectionReceiveBuffer(int bufferLength)
  24. {
  25. if (bufferLength < SessionPacket.MaxSessionPacketLength)
  26. {
  27. throw new ArgumentException("bufferLength must be large enough to hold the largest possible NBT packet");
  28. }
  29. m_buffer = new byte[bufferLength];
  30. }
  31. public void SetNumberOfBytesReceived(int numberOfBytesReceived)
  32. {
  33. m_bytesInBuffer += numberOfBytesReceived;
  34. }
  35. public bool HasCompletePacket()
  36. {
  37. if (m_bytesInBuffer >= 4)
  38. {
  39. if (!m_packetLength.HasValue)
  40. {
  41. byte flags = ByteReader.ReadByte(m_buffer, m_readOffset + 1);
  42. int trailerLength = (flags & 0x01) << 16 | BigEndianConverter.ToUInt16(m_buffer, m_readOffset + 2);
  43. m_packetLength = 4 + trailerLength;
  44. }
  45. return m_bytesInBuffer >= m_packetLength.Value;
  46. }
  47. return false;
  48. }
  49. /// <summary>
  50. /// HasCompletePacket must be called and return true before calling DequeuePacket
  51. /// </summary>
  52. /// <exception cref="System.IO.InvalidDataException"></exception>
  53. public SessionPacket DequeuePacket()
  54. {
  55. SessionPacket packet;
  56. try
  57. {
  58. packet = SessionPacket.GetSessionPacket(m_buffer, m_readOffset);
  59. }
  60. catch (IndexOutOfRangeException ex)
  61. {
  62. throw new InvalidDataException("Invalid NetBIOS session packet", ex);
  63. }
  64. RemovePacketBytes();
  65. return packet;
  66. }
  67. /// <summary>
  68. /// HasCompletePDU must be called and return true before calling DequeuePDUBytes
  69. /// </summary>
  70. public byte[] DequeuePacketBytes()
  71. {
  72. byte[] packetBytes = ByteReader.ReadBytes(m_buffer, m_readOffset, m_packetLength.Value);
  73. RemovePacketBytes();
  74. return packetBytes;
  75. }
  76. private void RemovePacketBytes()
  77. {
  78. m_bytesInBuffer -= m_packetLength.Value;
  79. if (m_bytesInBuffer == 0)
  80. {
  81. m_readOffset = 0;
  82. m_packetLength = null;
  83. }
  84. else
  85. {
  86. m_readOffset += m_packetLength.Value;
  87. m_packetLength = null;
  88. if (!HasCompletePacket())
  89. {
  90. Array.Copy(m_buffer, m_readOffset, m_buffer, 0, m_bytesInBuffer);
  91. m_readOffset = 0;
  92. }
  93. }
  94. }
  95. public byte[] Buffer
  96. {
  97. get
  98. {
  99. return m_buffer;
  100. }
  101. }
  102. public int WriteOffset
  103. {
  104. get
  105. {
  106. return m_readOffset + m_bytesInBuffer;
  107. }
  108. }
  109. public int BytesInBuffer
  110. {
  111. get
  112. {
  113. return m_bytesInBuffer;
  114. }
  115. }
  116. public int AvailableLength
  117. {
  118. get
  119. {
  120. return m_buffer.Length - (m_readOffset + m_bytesInBuffer);
  121. }
  122. }
  123. }
  124. }