Added ConnectionManager class

This commit is contained in:
Tal Aloni 2017-03-04 12:51:39 +02:00
parent 644e7d87d5
commit 30627e72d4
5 changed files with 57 additions and 1 deletions

View file

@ -182,6 +182,7 @@
<Compile Include="RPC\Structures\ResultElement.cs" /> <Compile Include="RPC\Structures\ResultElement.cs" />
<Compile Include="RPC\Structures\ResultList.cs" /> <Compile Include="RPC\Structures\ResultList.cs" />
<Compile Include="RPC\Structures\SyntaxID.cs" /> <Compile Include="RPC\Structures\SyntaxID.cs" />
<Compile Include="Server\ConnectionManager.cs" />
<Compile Include="Server\ConnectionState\ConnectionState.cs" /> <Compile Include="Server\ConnectionState\ConnectionState.cs" />
<Compile Include="Server\ConnectionState\OpenFileObject.cs" /> <Compile Include="Server\ConnectionState\OpenFileObject.cs" />
<Compile Include="Server\ConnectionState\OpenSearch.cs" /> <Compile Include="Server\ConnectionState\OpenSearch.cs" />

View file

@ -0,0 +1,45 @@
/* Copyright (C) 2017 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,
* either version 3 of the License, or (at your option) any later version.
*/
using System;
using System.Collections.Generic;
using Utilities;
namespace SMBLibrary.Server
{
internal class ConnectionManager
{
private List<ConnectionState> m_activeConnections = new List<ConnectionState>();
public void AddConnection(ConnectionState connection)
{
lock (m_activeConnections)
{
m_activeConnections.Add(connection);
}
}
public bool RemoveConnection(ConnectionState connection)
{
lock (m_activeConnections)
{
int connectionIndex = m_activeConnections.IndexOf(connection);
if (connectionIndex >= 0)
{
m_activeConnections.RemoveAt(connectionIndex);
return true;
}
return false;
}
}
public void ReleaseConnection(ConnectionState connection)
{
SocketUtils.ReleaseSocket(connection.ClientSocket);
RemoveConnection(connection);
}
}
}

View file

@ -76,6 +76,7 @@ namespace SMBLibrary.Server
{ {
state = new SMB1ConnectionState(state); state = new SMB1ConnectionState(state);
state.Dialect = SMBDialect.NTLM012; state.Dialect = SMBDialect.NTLM012;
m_connectionManager.AddConnection(state);
if (EnableExtendedSecurity && header.ExtendedSecurityFlag) if (EnableExtendedSecurity && header.ExtendedSecurityFlag)
{ {
return NegotiateHelper.GetNegotiateResponseExtended(request, m_serverGuid); return NegotiateHelper.GetNegotiateResponseExtended(request, m_serverGuid);

View file

@ -78,6 +78,7 @@ namespace SMBLibrary.Server
if (state.Dialect != SMBDialect.NotSet) if (state.Dialect != SMBDialect.NotSet)
{ {
state = new SMB2ConnectionState(state, AllocatePersistentFileID); state = new SMB2ConnectionState(state, AllocatePersistentFileID);
m_connectionManager.AddConnection(state);
} }
return response; return response;
} }

View file

@ -29,6 +29,8 @@ namespace SMBLibrary.Server
private NamedPipeShare m_services; // Named pipes private NamedPipeShare m_services; // Named pipes
private Guid m_serverGuid; private Guid m_serverGuid;
private ConnectionManager m_connectionManager;
private IPAddress m_serverAddress; private IPAddress m_serverAddress;
private SMBTransportType m_transport; private SMBTransportType m_transport;
private bool m_enableSMB1; private bool m_enableSMB1;
@ -45,6 +47,7 @@ namespace SMBLibrary.Server
m_securityProvider = securityProvider; m_securityProvider = securityProvider;
m_services = new NamedPipeShare(shares.ListShares()); m_services = new NamedPipeShare(shares.ListShares());
m_serverGuid = Guid.NewGuid(); m_serverGuid = Guid.NewGuid();
m_connectionManager = new ConnectionManager();
} }
public void Start(IPAddress serverAddress, SMBTransportType transport) public void Start(IPAddress serverAddress, SMBTransportType transport)
@ -147,10 +150,12 @@ namespace SMBLibrary.Server
} }
catch (ObjectDisposedException) catch (ObjectDisposedException)
{ {
m_connectionManager.ReleaseConnection(state);
return; return;
} }
catch (SocketException) catch (SocketException)
{ {
m_connectionManager.ReleaseConnection(state);
return; return;
} }
@ -158,7 +163,7 @@ namespace SMBLibrary.Server
{ {
// The other side has closed the connection // The other side has closed the connection
state.LogToServer(Severity.Debug, "The other side closed the connection"); state.LogToServer(Severity.Debug, "The other side closed the connection");
clientSocket.Close(); m_connectionManager.ReleaseConnection(state);
return; return;
} }
@ -174,9 +179,11 @@ namespace SMBLibrary.Server
} }
catch (ObjectDisposedException) catch (ObjectDisposedException)
{ {
m_connectionManager.ReleaseConnection(state);
} }
catch (SocketException) catch (SocketException)
{ {
m_connectionManager.ReleaseConnection(state);
} }
} }
} }
@ -256,6 +263,7 @@ namespace SMBLibrary.Server
if (state.Dialect != SMBDialect.NotSet) if (state.Dialect != SMBDialect.NotSet)
{ {
state = new SMB2ConnectionState(state, AllocatePersistentFileID); state = new SMB2ConnectionState(state, AllocatePersistentFileID);
m_connectionManager.AddConnection(state);
} }
TrySendResponse(state, response); TrySendResponse(state, response);
return; return;