diff --git a/SMBLibrary/SMBLibrary.csproj b/SMBLibrary/SMBLibrary.csproj index a65adf9..e86a238 100644 --- a/SMBLibrary/SMBLibrary.csproj +++ b/SMBLibrary/SMBLibrary.csproj @@ -211,6 +211,7 @@ + diff --git a/SMBLibrary/Server/ConnectionRequestEventArgs.cs b/SMBLibrary/Server/ConnectionRequestEventArgs.cs new file mode 100644 index 0000000..16ba04b --- /dev/null +++ b/SMBLibrary/Server/ConnectionRequestEventArgs.cs @@ -0,0 +1,23 @@ +/* Copyright (C) 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, + * either version 3 of the License, or (at your option) any later version. + */ +using System; +using System.Collections.Generic; +using System.Net; + +namespace SMBLibrary.Server +{ + public class ConnectionRequestEventArgs : EventArgs + { + public IPEndPoint IPEndPoint; + public bool Accept = true; + + public ConnectionRequestEventArgs(IPEndPoint ipEndPoint) + { + IPEndPoint = ipEndPoint; + } + } +} diff --git a/SMBLibrary/Server/SMBServer.cs b/SMBLibrary/Server/SMBServer.cs index 3788b11..cad97e1 100644 --- a/SMBLibrary/Server/SMBServer.cs +++ b/SMBLibrary/Server/SMBServer.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, @@ -42,6 +42,7 @@ namespace SMBLibrary.Server private bool m_listening; private DateTime m_serverStartTime; + public event EventHandler ConnectionRequested; public event EventHandler LogEntryAdded; public SMBServer(SMBShareCollection shares, GSSProvider securityProvider) @@ -148,27 +149,45 @@ namespace SMBLibrary.Server // Disable the Nagle Algorithm for this tcp socket: clientSocket.NoDelay = true; IPEndPoint clientEndPoint = (IPEndPoint)clientSocket.RemoteEndPoint; - ConnectionState state = new ConnectionState(clientSocket, clientEndPoint, Log); - state.LogToServer(Severity.Verbose, "New connection request"); - Thread senderThread = new Thread(delegate() + EventHandler handler = ConnectionRequested; + bool acceptConnection = true; + if (handler != null) { - ProcessSendQueue(state); - }); - senderThread.IsBackground = true; - senderThread.Start(); + ConnectionRequestEventArgs connectionRequestArgs = new ConnectionRequestEventArgs(clientEndPoint); + handler(this, connectionRequestArgs); + acceptConnection = connectionRequestArgs.Accept; + } - try + if (acceptConnection) { - // Direct TCP transport packet is actually an NBT Session Message Packet, - // So in either case (NetBios over TCP or Direct TCP Transport) we will receive an NBT packet. - clientSocket.BeginReceive(state.ReceiveBuffer.Buffer, state.ReceiveBuffer.WriteOffset, state.ReceiveBuffer.AvailableLength, 0, ReceiveCallback, state); + ConnectionState state = new ConnectionState(clientSocket, clientEndPoint, Log); + state.LogToServer(Severity.Verbose, "New connection request accepted"); + Thread senderThread = new Thread(delegate() + { + ProcessSendQueue(state); + }); + senderThread.IsBackground = true; + senderThread.Start(); + + try + { + // Direct TCP transport packet is actually an NBT Session Message Packet, + // So in either case (NetBios over TCP or Direct TCP Transport) we will receive an NBT packet. + clientSocket.BeginReceive(state.ReceiveBuffer.Buffer, state.ReceiveBuffer.WriteOffset, state.ReceiveBuffer.AvailableLength, 0, ReceiveCallback, state); + } + catch (ObjectDisposedException) + { + } + catch (SocketException) + { + } } - catch (ObjectDisposedException) - { - } - catch (SocketException) + else { + Log(Severity.Verbose, "[{0}:{1}] New connection request rejected", clientEndPoint.Address, clientEndPoint.Port); + clientSocket.Close(); } + listenerSocket.BeginAccept(ConnectRequestCallback, listenerSocket); }