NBTConnectionReceiveBuffer: Implement dispose pattern

This commit is contained in:
Tal Aloni 2023-12-14 19:47:35 +02:00
parent d6a1497076
commit 94577969cd
5 changed files with 39 additions and 9 deletions

View file

@ -34,6 +34,7 @@ namespace SMBLibrary.Client
private bool m_isConnected; private bool m_isConnected;
private bool m_isLoggedIn; private bool m_isLoggedIn;
private Socket m_clientSocket; private Socket m_clientSocket;
private ConnectionState m_connectionState;
private bool m_forceExtendedSecurity; private bool m_forceExtendedSecurity;
private bool m_unicode; private bool m_unicode;
private bool m_largeFiles; private bool m_largeFiles;
@ -154,9 +155,9 @@ namespace SMBLibrary.Client
return false; return false;
} }
ConnectionState state = new ConnectionState(m_clientSocket); m_connectionState = new ConnectionState(m_clientSocket);
NBTConnectionReceiveBuffer buffer = state.ReceiveBuffer; NBTConnectionReceiveBuffer buffer = m_connectionState.ReceiveBuffer;
m_clientSocket.BeginReceive(buffer.Buffer, buffer.WriteOffset, buffer.AvailableLength, SocketFlags.None, new AsyncCallback(OnClientSocketReceive), state); m_clientSocket.BeginReceive(buffer.Buffer, buffer.WriteOffset, buffer.AvailableLength, SocketFlags.None, new AsyncCallback(OnClientSocketReceive), m_connectionState);
return true; return true;
} }
@ -165,6 +166,7 @@ namespace SMBLibrary.Client
if (m_isConnected) if (m_isConnected)
{ {
m_clientSocket.Disconnect(false); m_clientSocket.Disconnect(false);
m_connectionState.ReceiveBuffer.Dispose();
m_isConnected = false; m_isConnected = false;
m_userID = 0; m_userID = 0;
} }
@ -418,6 +420,7 @@ namespace SMBLibrary.Client
if (!clientSocket.Connected) if (!clientSocket.Connected)
{ {
state.ReceiveBuffer.Dispose();
return; return;
} }
@ -428,22 +431,26 @@ namespace SMBLibrary.Client
} }
catch (ArgumentException) // The IAsyncResult object was not returned from the corresponding synchronous method on this class. catch (ArgumentException) // The IAsyncResult object was not returned from the corresponding synchronous method on this class.
{ {
state.ReceiveBuffer.Dispose();
return; return;
} }
catch (ObjectDisposedException) catch (ObjectDisposedException)
{ {
Log("[ReceiveCallback] EndReceive ObjectDisposedException"); Log("[ReceiveCallback] EndReceive ObjectDisposedException");
state.ReceiveBuffer.Dispose();
return; return;
} }
catch (SocketException ex) catch (SocketException ex)
{ {
Log("[ReceiveCallback] EndReceive SocketException: " + ex.Message); Log("[ReceiveCallback] EndReceive SocketException: " + ex.Message);
state.ReceiveBuffer.Dispose();
return; return;
} }
if (numberOfBytesReceived == 0) if (numberOfBytesReceived == 0)
{ {
m_isConnected = false; m_isConnected = false;
state.ReceiveBuffer.Dispose();
} }
else else
{ {
@ -458,11 +465,13 @@ namespace SMBLibrary.Client
catch (ObjectDisposedException) catch (ObjectDisposedException)
{ {
m_isConnected = false; m_isConnected = false;
buffer.Dispose();
Log("[ReceiveCallback] BeginReceive ObjectDisposedException"); Log("[ReceiveCallback] BeginReceive ObjectDisposedException");
} }
catch (SocketException ex) catch (SocketException ex)
{ {
m_isConnected = false; m_isConnected = false;
buffer.Dispose();
Log("[ReceiveCallback] BeginReceive SocketException: " + ex.Message); Log("[ReceiveCallback] BeginReceive SocketException: " + ex.Message);
} }
} }
@ -481,6 +490,7 @@ namespace SMBLibrary.Client
catch (Exception) catch (Exception)
{ {
state.ClientSocket.Close(); state.ClientSocket.Close();
state.ReceiveBuffer.Dispose();
break; break;
} }
@ -504,6 +514,7 @@ namespace SMBLibrary.Client
{ {
Log("Invalid SMB1 message: " + ex.Message); Log("Invalid SMB1 message: " + ex.Message);
state.ClientSocket.Close(); state.ClientSocket.Close();
state.ReceiveBuffer.Dispose();
m_isConnected = false; m_isConnected = false;
return; return;
} }
@ -534,6 +545,7 @@ namespace SMBLibrary.Client
{ {
Log("Inappropriate NetBIOS session packet"); Log("Inappropriate NetBIOS session packet");
state.ClientSocket.Close(); state.ClientSocket.Close();
state.ReceiveBuffer.Dispose();
} }
} }

View file

@ -33,6 +33,7 @@ namespace SMBLibrary.Client
private bool m_isConnected; private bool m_isConnected;
private bool m_isLoggedIn; private bool m_isLoggedIn;
private Socket m_clientSocket; private Socket m_clientSocket;
private ConnectionState m_connectionState;
private int m_responseTimeoutInMilliseconds; private int m_responseTimeoutInMilliseconds;
private object m_incomingQueueLock = new object(); private object m_incomingQueueLock = new object();
@ -169,9 +170,9 @@ namespace SMBLibrary.Client
return false; return false;
} }
ConnectionState state = new ConnectionState(m_clientSocket); m_connectionState = new ConnectionState(m_clientSocket);
NBTConnectionReceiveBuffer buffer = state.ReceiveBuffer; NBTConnectionReceiveBuffer buffer = m_connectionState.ReceiveBuffer;
m_clientSocket.BeginReceive(buffer.Buffer, buffer.WriteOffset, buffer.AvailableLength, SocketFlags.None, new AsyncCallback(OnClientSocketReceive), state); m_clientSocket.BeginReceive(buffer.Buffer, buffer.WriteOffset, buffer.AvailableLength, SocketFlags.None, new AsyncCallback(OnClientSocketReceive), m_connectionState);
return true; return true;
} }
@ -180,6 +181,7 @@ namespace SMBLibrary.Client
if (m_isConnected) if (m_isConnected)
{ {
m_clientSocket.Disconnect(false); m_clientSocket.Disconnect(false);
m_connectionState.ReceiveBuffer.Dispose();
m_isConnected = false; m_isConnected = false;
m_messageID = 0; m_messageID = 0;
m_sessionID = 0; m_sessionID = 0;
@ -356,6 +358,7 @@ namespace SMBLibrary.Client
if (!clientSocket.Connected) if (!clientSocket.Connected)
{ {
state.ReceiveBuffer.Dispose();
return; return;
} }
@ -366,22 +369,26 @@ namespace SMBLibrary.Client
} }
catch (ArgumentException) // The IAsyncResult object was not returned from the corresponding synchronous method on this class. catch (ArgumentException) // The IAsyncResult object was not returned from the corresponding synchronous method on this class.
{ {
state.ReceiveBuffer.Dispose();
return; return;
} }
catch (ObjectDisposedException) catch (ObjectDisposedException)
{ {
Log("[ReceiveCallback] EndReceive ObjectDisposedException"); Log("[ReceiveCallback] EndReceive ObjectDisposedException");
state.ReceiveBuffer.Dispose();
return; return;
} }
catch (SocketException ex) catch (SocketException ex)
{ {
Log("[ReceiveCallback] EndReceive SocketException: " + ex.Message); Log("[ReceiveCallback] EndReceive SocketException: " + ex.Message);
state.ReceiveBuffer.Dispose();
return; return;
} }
if (numberOfBytesReceived == 0) if (numberOfBytesReceived == 0)
{ {
m_isConnected = false; m_isConnected = false;
state.ReceiveBuffer.Dispose();
} }
else else
{ {
@ -397,11 +404,13 @@ namespace SMBLibrary.Client
{ {
m_isConnected = false; m_isConnected = false;
Log("[ReceiveCallback] BeginReceive ObjectDisposedException"); Log("[ReceiveCallback] BeginReceive ObjectDisposedException");
buffer.Dispose();
} }
catch (SocketException ex) catch (SocketException ex)
{ {
m_isConnected = false; m_isConnected = false;
Log("[ReceiveCallback] BeginReceive SocketException: " + ex.Message); Log("[ReceiveCallback] BeginReceive SocketException: " + ex.Message);
buffer.Dispose();
} }
} }
} }
@ -419,6 +428,7 @@ namespace SMBLibrary.Client
catch (Exception) catch (Exception)
{ {
state.ClientSocket.Close(); state.ClientSocket.Close();
state.ReceiveBuffer.Dispose();
break; break;
} }
@ -455,6 +465,7 @@ namespace SMBLibrary.Client
Log("Invalid SMB2 response: " + ex.Message); Log("Invalid SMB2 response: " + ex.Message);
state.ClientSocket.Close(); state.ClientSocket.Close();
m_isConnected = false; m_isConnected = false;
state.ReceiveBuffer.Dispose();
return; return;
} }
@ -501,6 +512,7 @@ namespace SMBLibrary.Client
{ {
Log("Inappropriate NetBIOS session packet"); Log("Inappropriate NetBIOS session packet");
state.ClientSocket.Close(); state.ClientSocket.Close();
state.ReceiveBuffer.Dispose();
} }
} }

View file

@ -1,17 +1,16 @@
/* Copyright (C) 2014-2020 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved. /* Copyright (C) 2014-2023 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
* *
* You can redistribute this program and/or modify it under the terms of * You can redistribute this program and/or modify it under the terms of
* the GNU Lesser Public License as published by the Free Software Foundation, * the GNU Lesser Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version. * either version 3 of the License, or (at your option) any later version.
*/ */
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using Utilities; using Utilities;
namespace SMBLibrary.NetBios namespace SMBLibrary.NetBios
{ {
public class NBTConnectionReceiveBuffer public class NBTConnectionReceiveBuffer : IDisposable
{ {
private byte[] m_buffer; private byte[] m_buffer;
private int m_readOffset = 0; private int m_readOffset = 0;
@ -110,6 +109,11 @@ namespace SMBLibrary.NetBios
} }
} }
public void Dispose()
{
m_buffer = null;
}
public byte[] Buffer public byte[] Buffer
{ {
get get

View file

@ -42,6 +42,7 @@ namespace SMBLibrary.Server
connection.SendQueue.Stop(); connection.SendQueue.Stop();
SocketUtils.ReleaseSocket(connection.ClientSocket); SocketUtils.ReleaseSocket(connection.ClientSocket);
connection.CloseSessions(); connection.CloseSessions();
connection.ReceiveBuffer.Dispose();
RemoveConnection(connection); RemoveConnection(connection);
} }

View file

@ -288,6 +288,7 @@ namespace SMBLibrary.Server
catch (Exception ex) catch (Exception ex)
{ {
state.ClientSocket.Close(); state.ClientSocket.Close();
state.ReceiveBuffer.Dispose();
state.LogToServer(Severity.Warning, "Rejected Invalid NetBIOS session packet: {0}", ex.Message); state.LogToServer(Severity.Warning, "Rejected Invalid NetBIOS session packet: {0}", ex.Message);
break; break;
} }