diff --git a/SMBLibrary/Client/ConnectionState.cs b/SMBLibrary/Client/ConnectionState.cs index 4b851c6..dfd3e87 100644 --- a/SMBLibrary/Client/ConnectionState.cs +++ b/SMBLibrary/Client/ConnectionState.cs @@ -1,4 +1,4 @@ -/* Copyright (C) 2017 Tal Aloni . All rights reserved. +/* Copyright (C) 2017-2020 Tal Aloni . All rights reserved. * * 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, @@ -16,6 +16,29 @@ namespace SMBLibrary.Client { public class ConnectionState { - public NBTConnectionReceiveBuffer ReceiveBuffer = new NBTConnectionReceiveBuffer(); + private Socket m_clientSocket; + private NBTConnectionReceiveBuffer m_receiveBuffer; + + public ConnectionState(Socket clientSocket) + { + m_clientSocket = clientSocket; + m_receiveBuffer = new NBTConnectionReceiveBuffer(); + } + + public Socket ClientSocket + { + get + { + return m_clientSocket; + } + } + + public NBTConnectionReceiveBuffer ReceiveBuffer + { + get + { + return m_receiveBuffer; + } + } } } diff --git a/SMBLibrary/Client/SMB1Client.cs b/SMBLibrary/Client/SMB1Client.cs index f018b37..e990b84 100644 --- a/SMBLibrary/Client/SMB1Client.cs +++ b/SMBLibrary/Client/SMB1Client.cs @@ -92,7 +92,7 @@ namespace SMBLibrary.Client SessionPacket sessionResponsePacket = WaitForSessionResponsePacket(); if (!(sessionResponsePacket is PositiveSessionResponsePacket)) { - m_clientSocket.Close(); + m_clientSocket.Disconnect(false); if (!ConnectSocket(serverAddress, port)) { return false; @@ -142,7 +142,7 @@ namespace SMBLibrary.Client return false; } - ConnectionState state = new ConnectionState(); + ConnectionState state = new ConnectionState(m_clientSocket); NBTConnectionReceiveBuffer buffer = state.ReceiveBuffer; m_clientSocket.BeginReceive(buffer.Buffer, buffer.WriteOffset, buffer.AvailableLength, SocketFlags.None, new AsyncCallback(OnClientSocketReceive), state); return true; @@ -399,8 +399,9 @@ namespace SMBLibrary.Client private void OnClientSocketReceive(IAsyncResult ar) { ConnectionState state = (ConnectionState)ar.AsyncState; + Socket clientSocket = state.ClientSocket; - if (!m_clientSocket.Connected) + if (!clientSocket.Connected) { return; } @@ -408,7 +409,7 @@ namespace SMBLibrary.Client int numberOfBytesReceived = 0; try { - numberOfBytesReceived = m_clientSocket.EndReceive(ar); + numberOfBytesReceived = clientSocket.EndReceive(ar); } catch (ArgumentException) // The IAsyncResult object was not returned from the corresponding synchronous method on this class. { @@ -437,7 +438,7 @@ namespace SMBLibrary.Client try { - m_clientSocket.BeginReceive(buffer.Buffer, buffer.WriteOffset, buffer.AvailableLength, SocketFlags.None, new AsyncCallback(OnClientSocketReceive), state); + clientSocket.BeginReceive(buffer.Buffer, buffer.WriteOffset, buffer.AvailableLength, SocketFlags.None, new AsyncCallback(OnClientSocketReceive), state); } catch (ObjectDisposedException) { @@ -464,7 +465,7 @@ namespace SMBLibrary.Client } catch (Exception) { - m_clientSocket.Close(); + state.ClientSocket.Close(); break; } @@ -496,7 +497,7 @@ namespace SMBLibrary.Client catch (Exception ex) { Log("Invalid SMB1 message: " + ex.Message); - m_clientSocket.Close(); + state.ClientSocket.Close(); m_isConnected = false; return; } diff --git a/SMBLibrary/Client/SMB2Client.cs b/SMBLibrary/Client/SMB2Client.cs index edb9003..5739bf6 100644 --- a/SMBLibrary/Client/SMB2Client.cs +++ b/SMBLibrary/Client/SMB2Client.cs @@ -84,7 +84,7 @@ namespace SMBLibrary.Client SessionPacket sessionResponsePacket = WaitForSessionResponsePacket(); if (!(sessionResponsePacket is PositiveSessionResponsePacket)) { - m_clientSocket.Close(); + m_clientSocket.Disconnect(false); if (!ConnectSocket(serverAddress, port)) { return false; @@ -134,7 +134,7 @@ namespace SMBLibrary.Client return false; } - ConnectionState state = new ConnectionState(); + ConnectionState state = new ConnectionState(m_clientSocket); NBTConnectionReceiveBuffer buffer = state.ReceiveBuffer; m_clientSocket.BeginReceive(buffer.Buffer, buffer.WriteOffset, buffer.AvailableLength, SocketFlags.None, new AsyncCallback(OnClientSocketReceive), state); return true; @@ -294,8 +294,9 @@ namespace SMBLibrary.Client private void OnClientSocketReceive(IAsyncResult ar) { ConnectionState state = (ConnectionState)ar.AsyncState; + Socket clientSocket = state.ClientSocket; - if (!m_clientSocket.Connected) + if (!clientSocket.Connected) { return; } @@ -303,7 +304,7 @@ namespace SMBLibrary.Client int numberOfBytesReceived = 0; try { - numberOfBytesReceived = m_clientSocket.EndReceive(ar); + numberOfBytesReceived = clientSocket.EndReceive(ar); } catch (ArgumentException) // The IAsyncResult object was not returned from the corresponding synchronous method on this class. { @@ -332,7 +333,7 @@ namespace SMBLibrary.Client try { - m_clientSocket.BeginReceive(buffer.Buffer, buffer.WriteOffset, buffer.AvailableLength, SocketFlags.None, new AsyncCallback(OnClientSocketReceive), state); + clientSocket.BeginReceive(buffer.Buffer, buffer.WriteOffset, buffer.AvailableLength, SocketFlags.None, new AsyncCallback(OnClientSocketReceive), state); } catch (ObjectDisposedException) { @@ -359,7 +360,7 @@ namespace SMBLibrary.Client } catch (Exception) { - m_clientSocket.Close(); + state.ClientSocket.Close(); break; } @@ -391,7 +392,7 @@ namespace SMBLibrary.Client catch (Exception ex) { Log("Invalid SMB2 response: " + ex.Message); - m_clientSocket.Close(); + state.ClientSocket.Close(); m_isConnected = false; return; }