Client: Correctly open a second NetBios session

This commit is contained in:
Tal Aloni 2020-01-27 23:16:55 +02:00
parent 0a89a84434
commit 4ac07b6ea3
3 changed files with 41 additions and 16 deletions

View file

@ -1,4 +1,4 @@
/* Copyright (C) 2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
/* Copyright (C) 2017-2020 Tal Aloni <tal.aloni.il@gmail.com>. 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;
}
}
}
}

View file

@ -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;
}

View file

@ -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;
}