diff --git a/SMBLibrary/Server/SMBConnectionReceiveBuffer.cs b/SMBLibrary/NetBios/NBTConnectionReceiveBuffer.cs similarity index 84% rename from SMBLibrary/Server/SMBConnectionReceiveBuffer.cs rename to SMBLibrary/NetBios/NBTConnectionReceiveBuffer.cs index e239589..045f1c3 100644 --- a/SMBLibrary/Server/SMBConnectionReceiveBuffer.cs +++ b/SMBLibrary/NetBios/NBTConnectionReceiveBuffer.cs @@ -6,22 +6,28 @@ */ using System; using System.Collections.Generic; -using System.Text; -using SMBLibrary.NetBios; using Utilities; -namespace SMBLibrary.Server +namespace SMBLibrary.NetBios { - public class SMBConnectionReceiveBuffer + public class NBTConnectionReceiveBuffer { private byte[] m_buffer; private int m_readOffset = 0; private int m_bytesInBuffer = 0; private int? m_packetLength; - /// Must be large enough to hold the largest possible packet - public SMBConnectionReceiveBuffer(int bufferLength) + public NBTConnectionReceiveBuffer() : this(SessionPacket.MaxSessionPacketLength) { + } + + /// Must be large enough to hold the largest possible NBT packet + public NBTConnectionReceiveBuffer(int bufferLength) + { + if (bufferLength < SessionPacket.MaxSessionPacketLength) + { + throw new ArgumentException("bufferLength must be large enough to hold the largest possible NBT packet"); + } m_buffer = new byte[bufferLength]; } @@ -36,8 +42,6 @@ namespace SMBLibrary.Server { if (!m_packetLength.HasValue) { - // The packet is either Direct TCP transport packet (which is an NBT Session Message - // Packet) or an NBT packet. byte flags = ByteReader.ReadByte(m_buffer, m_readOffset + 1); int trailerLength = (flags & 0x01) << 16 | BigEndianConverter.ToUInt16(m_buffer, m_readOffset + 2); m_packetLength = 4 + trailerLength; diff --git a/SMBLibrary/NetBios/SessionPackets/SessionPacket.cs b/SMBLibrary/NetBios/SessionPackets/SessionPacket.cs index a0ec47b..e5414f6 100644 --- a/SMBLibrary/NetBios/SessionPackets/SessionPacket.cs +++ b/SMBLibrary/NetBios/SessionPackets/SessionPacket.cs @@ -16,6 +16,8 @@ namespace SMBLibrary.NetBios /// public abstract class SessionPacket { + public const int MaxSessionPacketLength = 131075; + public SessionPacketTypeName Type; public byte Flags; public int Length; // 2 bytes + length extension bit diff --git a/SMBLibrary/SMBLibrary.csproj b/SMBLibrary/SMBLibrary.csproj index 8b7cd41..57db162 100644 --- a/SMBLibrary/SMBLibrary.csproj +++ b/SMBLibrary/SMBLibrary.csproj @@ -69,6 +69,7 @@ + @@ -129,7 +130,6 @@ - diff --git a/SMBLibrary/Server/ConnectionState/SMB1ConnectionState.cs b/SMBLibrary/Server/ConnectionState/SMB1ConnectionState.cs index 2b70039..2087afe 100644 --- a/SMBLibrary/Server/ConnectionState/SMB1ConnectionState.cs +++ b/SMBLibrary/Server/ConnectionState/SMB1ConnectionState.cs @@ -8,6 +8,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Net.Sockets; +using SMBLibrary.NetBios; using Utilities; namespace SMBLibrary.Server @@ -15,8 +16,7 @@ namespace SMBLibrary.Server public class SMB1ConnectionState { public Socket ClientSocket = null; - public const int ReceiveBufferSize = 131075; // Largest NBT Session Packet - public SMBConnectionReceiveBuffer ReceiveBuffer = new SMBConnectionReceiveBuffer(ReceiveBufferSize); + public NBTConnectionReceiveBuffer ReceiveBuffer = new NBTConnectionReceiveBuffer(); public int MaxBufferSize; public bool LargeRead; diff --git a/SMBLibrary/Server/SMBServer.cs b/SMBLibrary/Server/SMBServer.cs index 7d74600..03025be 100644 --- a/SMBLibrary/Server/SMBServer.cs +++ b/SMBLibrary/Server/SMBServer.cs @@ -100,6 +100,8 @@ namespace SMBLibrary.Server state.ClientSocket = clientSocket; try { + // Direct TCP transport packet is actually an NBT Session Message Packet, + // So in either case (NetBios over TCP or Direct TCP Transport) we will receive an NBT packet. clientSocket.BeginReceive(state.ReceiveBuffer.Buffer, state.ReceiveBuffer.WriteOffset, state.ReceiveBuffer.AvailableLength, 0, ReceiveCallback, state); } catch (ObjectDisposedException) @@ -144,7 +146,7 @@ namespace SMBLibrary.Server return; } - SMBConnectionReceiveBuffer receiveBuffer = state.ReceiveBuffer; + NBTConnectionReceiveBuffer receiveBuffer = state.ReceiveBuffer; receiveBuffer.SetNumberOfBytesReceived(numberOfBytesReceived); ProcessConnectionBuffer(state); @@ -167,7 +169,7 @@ namespace SMBLibrary.Server { Socket clientSocket = state.ClientSocket; - SMBConnectionReceiveBuffer receiveBuffer = state.ReceiveBuffer; + NBTConnectionReceiveBuffer receiveBuffer = state.ReceiveBuffer; while (receiveBuffer.HasCompletePacket()) { SessionPacket packet = null;