From 2caf390c4178eee55d9d238574e64d70c8410ef3 Mon Sep 17 00:00:00 2001 From: Tal Aloni Date: Sat, 10 Aug 2019 21:22:41 +0300 Subject: [PATCH] Client: Fixed a bug related to localhost communication --- SMBLibrary/Client/SMB1Client.cs | 18 +++++++----------- SMBLibrary/Client/SMB2Client.cs | 16 ++++++---------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/SMBLibrary/Client/SMB1Client.cs b/SMBLibrary/Client/SMB1Client.cs index 6a225f5..271c56b 100644 --- a/SMBLibrary/Client/SMB1Client.cs +++ b/SMBLibrary/Client/SMB1Client.cs @@ -1,4 +1,4 @@ -/* Copyright (C) 2014-2018 Tal Aloni . All rights reserved. +/* Copyright (C) 2014-2019 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, @@ -31,7 +31,6 @@ namespace SMBLibrary.Client private bool m_isConnected; private bool m_isLoggedIn; private Socket m_clientSocket; - private IAsyncResult m_currentAsyncResult; private bool m_forceExtendedSecurity; private bool m_unicode; private bool m_largeFiles; @@ -87,7 +86,7 @@ namespace SMBLibrary.Client ConnectionState state = new ConnectionState(); NBTConnectionReceiveBuffer buffer = state.ReceiveBuffer; - m_currentAsyncResult = 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), state); bool supportsDialect = NegotiateDialect(m_forceExtendedSecurity); if (!supportsDialect) { @@ -358,13 +357,6 @@ namespace SMBLibrary.Client private void OnClientSocketReceive(IAsyncResult ar) { - if (ar != m_currentAsyncResult) - { - // We ignore calls for old sockets which we no longer use - // See: http://rajputyh.blogspot.co.il/2010/04/solve-exception-message-iasyncresult.html - return; - } - ConnectionState state = (ConnectionState)ar.AsyncState; if (!m_clientSocket.Connected) @@ -377,6 +369,10 @@ namespace SMBLibrary.Client { numberOfBytesReceived = m_clientSocket.EndReceive(ar); } + catch (ArgumentException) // The IAsyncResult object was not returned from the corresponding synchronous method on this class. + { + return; + } catch (ObjectDisposedException) { Log("[ReceiveCallback] EndReceive ObjectDisposedException"); @@ -400,7 +396,7 @@ namespace SMBLibrary.Client try { - m_currentAsyncResult = 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), state); } catch (ObjectDisposedException) { diff --git a/SMBLibrary/Client/SMB2Client.cs b/SMBLibrary/Client/SMB2Client.cs index a0b6551..b256037 100644 --- a/SMBLibrary/Client/SMB2Client.cs +++ b/SMBLibrary/Client/SMB2Client.cs @@ -32,7 +32,6 @@ namespace SMBLibrary.Client private bool m_isConnected; private bool m_isLoggedIn; private Socket m_clientSocket; - private IAsyncResult m_currentAsyncResult; private object m_incomingQueueLock = new object(); private List m_incomingQueue = new List(); @@ -79,7 +78,7 @@ namespace SMBLibrary.Client ConnectionState state = new ConnectionState(); NBTConnectionReceiveBuffer buffer = state.ReceiveBuffer; - m_currentAsyncResult = 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), state); bool supportsDialect = NegotiateDialect(); if (!supportsDialect) { @@ -246,13 +245,6 @@ namespace SMBLibrary.Client private void OnClientSocketReceive(IAsyncResult ar) { - if (ar != m_currentAsyncResult) - { - // We ignore calls for old sockets which we no longer use - // See: http://rajputyh.blogspot.co.il/2010/04/solve-exception-message-iasyncresult.html - return; - } - ConnectionState state = (ConnectionState)ar.AsyncState; if (!m_clientSocket.Connected) @@ -265,6 +257,10 @@ namespace SMBLibrary.Client { numberOfBytesReceived = m_clientSocket.EndReceive(ar); } + catch (ArgumentException) // The IAsyncResult object was not returned from the corresponding synchronous method on this class. + { + return; + } catch (ObjectDisposedException) { Log("[ReceiveCallback] EndReceive ObjectDisposedException"); @@ -288,7 +284,7 @@ namespace SMBLibrary.Client try { - m_currentAsyncResult = 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), state); } catch (ObjectDisposedException) {