From dcf94a8b2352e01a2c4c70cb407f7cc234abbdb3 Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Wed, 29 Nov 2023 13:55:01 +0000 Subject: [PATCH] [+] (dummy!) ISocketServerLimits.hpp [+] (dummy!) ISocketChannelLimits.hpp [+] ISocketChannel::GetChannelLimits [+] ISocketBase::GetLockedWorkerThread [+] ISocket::GetSocketServer --- Include/Aurora/IO/Net/ISocket.hpp | 3 + Include/Aurora/IO/Net/ISocketBase.hpp | 4 + Include/Aurora/IO/Net/ISocketChannel.hpp | 3 + .../Aurora/IO/Net/ISocketChannelLimits.hpp | 26 ++++ Include/Aurora/IO/Net/ISocketServer.hpp | 2 + Include/Aurora/IO/Net/ISocketServerLimits.hpp | 44 +++++++ Include/Aurora/IO/Net/NetExperimental.hpp | 2 + Source/IO/Net/AuNetChannelLimits.cpp | 52 ++++++++ Source/IO/Net/AuNetChannelLimits.hpp | 31 +++++ Source/IO/Net/AuNetSocket.NT.cpp | 6 +- Source/IO/Net/AuNetSocket.NT.hpp | 4 +- Source/IO/Net/AuNetSocket.Unix.cpp | 5 +- Source/IO/Net/AuNetSocket.Unix.hpp | 4 +- Source/IO/Net/AuNetSocket.cpp | 37 +++++- Source/IO/Net/AuNetSocket.hpp | 15 ++- Source/IO/Net/AuNetSocketChannel.cpp | 5 + Source/IO/Net/AuNetSocketChannel.hpp | 5 + Source/IO/Net/AuNetSocketServer.NT.cpp | 4 +- Source/IO/Net/AuNetSocketServer.Unix.cpp | 2 +- Source/IO/Net/AuNetSocketServer.cpp | 41 +++++++ Source/IO/Net/AuNetSocketServer.hpp | 16 ++- ...uNetSocketServerAcceptReadOperation.NT.cpp | 4 +- ...etSocketServerAcceptReadOperation.Unix.cpp | 4 +- Source/IO/Net/AuNetSocketServerLimits.cpp | 115 ++++++++++++++++++ Source/IO/Net/AuNetSocketServerLimits.hpp | 55 +++++++++ Source/IO/Net/AuNetSrvSockets.cpp | 5 + Source/IO/Net/AuNetWorker.hpp | 4 +- .../AuNetDatagramEmulatorISocket.cpp | 37 +++++- .../AuNetDatagramEmulatorISocket.hpp | 4 + .../AuNetDatagramEmulatorISocketServer.cpp | 57 ++++++++- .../AuNetDatagramEmulatorISocketServer.hpp | 3 + .../AuNetDatagramSocketServer.cpp | 34 ++++++ .../AuNetDatagramSocketServer.hpp | 2 + .../AuNetDatagramSocketServerChannel.cpp | 5 + .../AuNetDatagramSocketServerChannel.hpp | 2 + 35 files changed, 615 insertions(+), 27 deletions(-) create mode 100644 Include/Aurora/IO/Net/ISocketChannelLimits.hpp create mode 100644 Include/Aurora/IO/Net/ISocketServerLimits.hpp create mode 100644 Source/IO/Net/AuNetChannelLimits.cpp create mode 100644 Source/IO/Net/AuNetChannelLimits.hpp create mode 100644 Source/IO/Net/AuNetSocketServerLimits.cpp create mode 100644 Source/IO/Net/AuNetSocketServerLimits.hpp diff --git a/Include/Aurora/IO/Net/ISocket.hpp b/Include/Aurora/IO/Net/ISocket.hpp index 43e18302..0ee8ffad 100644 --- a/Include/Aurora/IO/Net/ISocket.hpp +++ b/Include/Aurora/IO/Net/ISocket.hpp @@ -11,6 +11,7 @@ namespace Aurora::IO::Net { struct ISocketChannel; struct ISocketDriver; + struct ISocketServer; struct ISocket : virtual ISocketBase { @@ -19,5 +20,7 @@ namespace Aurora::IO::Net virtual const NetEndpoint &GetRemoteEndpoint() = 0; virtual AuSPtr GetUserDriver() = 0; + + virtual AuSPtr GetSocketServer() = 0; }; } \ No newline at end of file diff --git a/Include/Aurora/IO/Net/ISocketBase.hpp b/Include/Aurora/IO/Net/ISocketBase.hpp index 8f8842b8..82dc7174 100644 --- a/Include/Aurora/IO/Net/ISocketBase.hpp +++ b/Include/Aurora/IO/Net/ISocketBase.hpp @@ -9,6 +9,8 @@ namespace Aurora::IO::Net { + struct INetWorker; + struct ISocketBase { virtual const NetError &GetError() = 0; @@ -19,6 +21,8 @@ namespace Aurora::IO::Net virtual AuUInt ToPlatformHandle() = 0; + virtual AuSPtr GetLockedWorkerThread() = 0; + AURT_ADD_USR_DATA; }; } \ No newline at end of file diff --git a/Include/Aurora/IO/Net/ISocketChannel.hpp b/Include/Aurora/IO/Net/ISocketChannel.hpp index a77f9360..69e7104a 100644 --- a/Include/Aurora/IO/Net/ISocketChannel.hpp +++ b/Include/Aurora/IO/Net/ISocketChannel.hpp @@ -15,6 +15,7 @@ namespace Aurora::IO::Protocol namespace Aurora::IO::Net { struct ISocket; + struct ISocketChannelLimits; struct ISocketChannelEventListener; struct ISocketChannel : Protocol::IProtocolBaseReader, Protocol::IProtocolBaseWriter @@ -231,6 +232,8 @@ namespace Aurora::IO::Net */ virtual void RemoveEventListener(const AuSPtr &pListener) = 0; + virtual AuSPtr GetChannelLimits() = 0; + AURT_ADD_USR_DATA; }; } \ No newline at end of file diff --git a/Include/Aurora/IO/Net/ISocketChannelLimits.hpp b/Include/Aurora/IO/Net/ISocketChannelLimits.hpp new file mode 100644 index 00000000..9118addf --- /dev/null +++ b/Include/Aurora/IO/Net/ISocketChannelLimits.hpp @@ -0,0 +1,26 @@ +/*** + Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved. + + File: ISocketChannelLimits.hpp + Date: 2023-11-29 + Author: Reece +***/ +#pragma once + +namespace Aurora::IO::Net +{ + struct ISocketChannelLimits + { + virtual AuOptional GetBeforeDDoSDCAverageThroughputLimit() = 0; + virtual AuOptional SetBeforeDDoSDCAverageThroughputLimit(AuOptional uNextValue) = 0; + + virtual AuOptional GetBeforeDDoSDCFrameTimeMS() = 0; + virtual AuOptional SetBeforeDDoSDCFrameTimeMS(AuOptional uNextValue) = 0; + + virtual AuOptional GetBeforeDDoSTickFrameLimitCount() = 0; + virtual AuOptional SetBeforeDDoSTickFrameLimitCount(AuOptional uNextValue) = 0; + + virtual AuOptional GetBeforeDDoSTickFrameTimeMS() = 0; + virtual AuOptional SetBeforeDDoSTickFrameTimeMS(AuOptional uNextValue) = 0; + }; +} \ No newline at end of file diff --git a/Include/Aurora/IO/Net/ISocketServer.hpp b/Include/Aurora/IO/Net/ISocketServer.hpp index 2f653077..9c42be84 100644 --- a/Include/Aurora/IO/Net/ISocketServer.hpp +++ b/Include/Aurora/IO/Net/ISocketServer.hpp @@ -11,6 +11,8 @@ namespace Aurora::IO::Net { struct ISocketServer : virtual ISocketBase { + virtual AuList> GetChildren() = 0; + virtual AuSPtr GetLimits() = 0; virtual AuSPtr GetServerDriver() = 0; }; } \ No newline at end of file diff --git a/Include/Aurora/IO/Net/ISocketServerLimits.hpp b/Include/Aurora/IO/Net/ISocketServerLimits.hpp new file mode 100644 index 00000000..b7f77790 --- /dev/null +++ b/Include/Aurora/IO/Net/ISocketServerLimits.hpp @@ -0,0 +1,44 @@ +/*** + Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved. + + File: ISocketServerLimits.hpp + Date: 2023-11-29 + Author: Reece +***/ +#pragma once + +namespace Aurora::IO::Net +{ + struct ISocketServerLimits + { + virtual AuOptional GetMaxConnectionsPerIp() = 0; + virtual AuOptional SetMaxConnectionsPerIp(AuOptional optNextValue) = 0; + + virtual AuOptional GetMaxConnections() = 0; + virtual AuOptional SetMaxConnections(AuOptional optNextValue) = 0; + + virtual AuList GetWhitelist() = 0; + virtual void SetWhitelist(AuList whitelist) = 0; + + virtual AuList GetBlacklist() = 0; + virtual void SetBlacklist(AuList blacklist) = 0; + + virtual AuOptional GetBeforeDDoSDCAverageThroughputLimit() = 0; + virtual AuOptional SetBeforeDDoSDCAverageThroughputLimit(AuOptional optNextValue) = 0; + + virtual AuOptional GetBeforeDDoSDCFrameTimeMS() = 0; + virtual AuOptional SetBeforeDDoSDCFrameTimeMS(AuOptional optNextValue) = 0; + + virtual AuOptional GetBeforeDDoSTickFrameLimitCount() = 0; + virtual AuOptional SetBeforeDDoSTickFrameLimitCount(AuOptional optNextValue) = 0; + + virtual AuOptional GetBeforeDDoSTickFrameTimeMS() = 0; + virtual AuOptional SetBeforeDDoSTickFrameTimeMS(AuOptional optNextValue) = 0; + + virtual AuOptional GetAutoBanOnDDoSDetectA() = 0; + virtual AuOptional SetAutoBanOnDDoSDetectA(AuOptional optNextValue) = 0; + + virtual AuOptional GetAutoBanTimeSecs() = 0; + virtual AuOptional SetAutoBanTimeSecs(AuOptional optNextValue) = 0; + }; +} \ No newline at end of file diff --git a/Include/Aurora/IO/Net/NetExperimental.hpp b/Include/Aurora/IO/Net/NetExperimental.hpp index 12318757..8303bc50 100644 --- a/Include/Aurora/IO/Net/NetExperimental.hpp +++ b/Include/Aurora/IO/Net/NetExperimental.hpp @@ -28,6 +28,8 @@ #include "ISocketChannel.hpp" #include "ISocketDriverFactory.hpp" #include "ISocketServerDriver.hpp" +#include "ISocketServerLimits.hpp" +#include "ISocketChannelLimits.hpp" #include "ISocketServer.hpp" #include "IDatagramDriver.hpp" diff --git a/Source/IO/Net/AuNetChannelLimits.cpp b/Source/IO/Net/AuNetChannelLimits.cpp new file mode 100644 index 00000000..cc6af8a9 --- /dev/null +++ b/Source/IO/Net/AuNetChannelLimits.cpp @@ -0,0 +1,52 @@ +/*** + Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved. + + File: AuNetChannelLimits.cpp + Date: 2023-11-29 + Author: Reece +***/ +#include "Networking.hpp" +#include "AuNetChannelLimits.hpp" + +namespace Aurora::IO::Net +{ + AuOptional NetChannelLimits::GetBeforeDDoSDCAverageThroughputLimit() + { + return this->optBeforeDDoSDCAverageThroughputLimit; + } + + AuOptional NetChannelLimits::SetBeforeDDoSDCAverageThroughputLimit(AuOptional uNextValue) + { + return AuExchange(this->optBeforeDDoSDCAverageThroughputLimit, uNextValue); + } + + AuOptional NetChannelLimits::GetBeforeDDoSDCFrameTimeMS() + { + return this->optBeforeDDoSDCFrameTimeMS; + } + + AuOptional NetChannelLimits::SetBeforeDDoSDCFrameTimeMS(AuOptional uNextValue) + { + return AuExchange(this->optBeforeDDoSDCFrameTimeMS, uNextValue); + } + + AuOptional NetChannelLimits::GetBeforeDDoSTickFrameLimitCount() + { + return this->optBeforeDDoSTickFrameLimitCount; + } + + AuOptional NetChannelLimits::SetBeforeDDoSTickFrameLimitCount(AuOptional uNextValue) + { + return AuExchange(this->optBeforeDDoSTickFrameLimitCount, uNextValue); + } + + AuOptional NetChannelLimits::GetBeforeDDoSTickFrameTimeMS() + { + return this->optBeforeDDoSTickFrameTimeMS; + } + + AuOptional NetChannelLimits::SetBeforeDDoSTickFrameTimeMS(AuOptional uNextValue) + { + return AuExchange(this->optBeforeDDoSTickFrameTimeMS, uNextValue); + } +} \ No newline at end of file diff --git a/Source/IO/Net/AuNetChannelLimits.hpp b/Source/IO/Net/AuNetChannelLimits.hpp new file mode 100644 index 00000000..b8890825 --- /dev/null +++ b/Source/IO/Net/AuNetChannelLimits.hpp @@ -0,0 +1,31 @@ +/*** + Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved. + + File: AuNetChannelLimits.hpp + Date: 2023-11-29 + Author: Reece +***/ +#pragma once + +namespace Aurora::IO::Net +{ + struct NetChannelLimits : ISocketChannelLimits + { + AuOptional GetBeforeDDoSDCAverageThroughputLimit() override; + AuOptional SetBeforeDDoSDCAverageThroughputLimit(AuOptional uNextValue) override; + + AuOptional GetBeforeDDoSDCFrameTimeMS() override; + AuOptional SetBeforeDDoSDCFrameTimeMS(AuOptional uNextValue) override; + + AuOptional GetBeforeDDoSTickFrameLimitCount() override; + AuOptional SetBeforeDDoSTickFrameLimitCount(AuOptional uNextValue) override; + + AuOptional GetBeforeDDoSTickFrameTimeMS() override; + AuOptional SetBeforeDDoSTickFrameTimeMS(AuOptional uNextValue) override; + + AuOptional optBeforeDDoSTickFrameTimeMS; + AuOptional optBeforeDDoSTickFrameLimitCount; + AuOptional optBeforeDDoSDCFrameTimeMS; + AuOptional optBeforeDDoSDCAverageThroughputLimit; + }; +} \ No newline at end of file diff --git a/Source/IO/Net/AuNetSocket.NT.cpp b/Source/IO/Net/AuNetSocket.NT.cpp index 1ec58de0..33091a15 100644 --- a/Source/IO/Net/AuNetSocket.NT.cpp +++ b/Source/IO/Net/AuNetSocket.NT.cpp @@ -24,8 +24,10 @@ namespace Aurora::IO::Net Socket::Socket(struct NetInterface *pInterface, struct NetWorker *pWorker, const AuSPtr &pSocketDriver, - AuUInt osHandle) : - SocketBase(pInterface, pWorker, pSocketDriver, osHandle) + AuUInt osHandle, + AuSPtr pParent, + SocketServer *pParent2) : + SocketBase(pInterface, pWorker, pSocketDriver, osHandle, pParent, pParent2) { } diff --git a/Source/IO/Net/AuNetSocket.NT.hpp b/Source/IO/Net/AuNetSocket.NT.hpp index 7573479b..597ee1ed 100644 --- a/Source/IO/Net/AuNetSocket.NT.hpp +++ b/Source/IO/Net/AuNetSocket.NT.hpp @@ -27,7 +27,9 @@ namespace Aurora::IO::Net Socket(struct NetInterface *pInterface, struct NetWorker *pWorker, const AuSPtr &pSocketDriver, - AuUInt osHandle); + AuUInt osHandle, + AuSPtr pParent = {}, + SocketServer *pParent2 = {}); Socket(struct NetInterface *pInterface, struct NetWorker *pWorker, diff --git a/Source/IO/Net/AuNetSocket.Unix.cpp b/Source/IO/Net/AuNetSocket.Unix.cpp index 4206473f..54e97b1b 100644 --- a/Source/IO/Net/AuNetSocket.Unix.cpp +++ b/Source/IO/Net/AuNetSocket.Unix.cpp @@ -28,8 +28,9 @@ namespace Aurora::IO::Net Socket::Socket(struct NetInterface *pInterface, struct NetWorker *pWorker, const AuSPtr &pSocketDriver, - AuUInt osHandle) : - SocketBase(pInterface, pWorker, pSocketDriver, osHandle) + AuUInt osHandle, + AuSPtr pParent) : + SocketBase(pInterface, pWorker, pSocketDriver, osHandle, pParent) { } diff --git a/Source/IO/Net/AuNetSocket.Unix.hpp b/Source/IO/Net/AuNetSocket.Unix.hpp index 90dd5035..5fa7cbfb 100644 --- a/Source/IO/Net/AuNetSocket.Unix.hpp +++ b/Source/IO/Net/AuNetSocket.Unix.hpp @@ -27,7 +27,9 @@ namespace Aurora::IO::Net Socket(struct NetInterface *pInterface, struct NetWorker *pWorker, const AuSPtr &pSocketDriver, - AuUInt osHandle); + AuUInt osHandle, + AuSPtr pParent = {}, + SocketServer *pParent2 = {}); Socket(struct NetInterface *pInterface, struct NetWorker *pWorker, diff --git a/Source/IO/Net/AuNetSocket.cpp b/Source/IO/Net/AuNetSocket.cpp index 1a0f201a..60bcbc29 100644 --- a/Source/IO/Net/AuNetSocket.cpp +++ b/Source/IO/Net/AuNetSocket.cpp @@ -30,15 +30,18 @@ namespace Aurora::IO::Net } SocketBase::SocketBase(struct NetInterface *pInterface, - struct NetWorker *pWorker, - const AuSPtr &pSocketDriver, - AuUInt osHandle) : + struct NetWorker *pWorker, + const AuSPtr &pSocketDriver, + AuUInt osHandle, + AuSPtr pParent, + SocketServer *pParent2) : connectOperation(this), socketChannel_(this), pInterface_(pInterface), pWorker_(pWorker), pSocketDriver_(pSocketDriver), - osHandle_(osHandle) + osHandle_(osHandle), + wpParent2_(pParent2) { this->pWorker_->AddSocket(this); @@ -55,6 +58,10 @@ namespace Aurora::IO::Net this->osHandleOwner_->InitFromPairMove((int)this->osHandle_, (int)this->osHandle_); #endif + if (pParent) + { + pParent2->OnNotifyChildCreated(this); + } } SocketBase::SocketBase(struct NetInterface *pInterface, @@ -262,6 +269,11 @@ namespace Aurora::IO::Net return this->pSocketDriver_; } + AuSPtr SocketBase::GetLockedWorkerThread() + { + return this->pWorker_->SharedFromThis(); + } + const NetEndpoint &SocketBase::GetRemoteEndpoint() { return this->remoteEndpoint_; @@ -272,6 +284,11 @@ namespace Aurora::IO::Net return this->localEndpoint_; } + AuSPtr SocketBase::GetSocketServer() + { + return AuTryLockMemoryType(this->wpParent_); + } + void SocketBase::ConnectFinished() { this->UpdateLocalEndpoint(); @@ -523,6 +540,11 @@ namespace Aurora::IO::Net void SocketBase::Destroy() { + if (auto pParent = AuTryLockMemoryType(this->wpParent_)) + { + this->wpParent2_->OnNotifyChildRemoved(this); + } + this->SendFinalize(); } @@ -626,7 +648,12 @@ namespace Aurora::IO::Net this->pSocketDriver_.reset(); } - + + if (auto pParent = AuTryLockMemoryType(this->wpParent_)) + { + this->wpParent2_->OnNotifyChildRemoved(this); + } + auto pWriteTransaction = this->socketChannel_.outputChannel.ToWriteTransaction(); #if defined(AURORA_IS_MODERNNT_DERIVED) diff --git a/Source/IO/Net/AuNetSocket.hpp b/Source/IO/Net/AuNetSocket.hpp index e6cd840d..0774ffc8 100644 --- a/Source/IO/Net/AuNetSocket.hpp +++ b/Source/IO/Net/AuNetSocket.hpp @@ -22,15 +22,18 @@ namespace Aurora::IO::Net struct SocketBase; struct SocketConnectOperation; struct SocketServerAcceptReadOperation; + struct SocketServer; struct SocketBase : virtual ISocket, AuEnableSharedFromThis { friend struct SocketServerAcceptReadOperation; SocketBase(struct NetInterface *pInterface, - struct NetWorker *pWorker, - const AuSPtr &pSocketDriver, - AuUInt osHandle); + struct NetWorker *pWorker, + const AuSPtr &pSocketDriver, + AuUInt osHandle, + AuSPtr pParent, + SocketServer *pParent2); SocketBase(struct NetInterface *pInterface, struct NetWorker *pWorker, @@ -59,10 +62,14 @@ namespace Aurora::IO::Net AuSPtr GetUserDriver() override; + AuSPtr GetLockedWorkerThread(); + const NetEndpoint &GetRemoteEndpoint() override; const NetEndpoint &GetLocalEndpoint() override; const NetError &GetError() override; + + AuSPtr GetSocketServer() override; void ConnectFinished(); void ConnectFailed(const NetError &error); @@ -128,6 +135,8 @@ namespace Aurora::IO::Net NetEndpoint remoteEndpoint_; NetEndpoint localEndpoint_; + AuWPtr wpParent_ {}; + SocketServer *wpParent2_ {}; bool bForceFailConstruct_ {}; diff --git a/Source/IO/Net/AuNetSocketChannel.cpp b/Source/IO/Net/AuNetSocketChannel.cpp index b69fa6d3..bf6a4d9d 100644 --- a/Source/IO/Net/AuNetSocketChannel.cpp +++ b/Source/IO/Net/AuNetSocketChannel.cpp @@ -349,6 +349,11 @@ namespace Aurora::IO::Net bool(this->inputChannel.IsValid()); } + AuSPtr SocketChannel::GetChannelLimits() + { + return AuSPtr(this->pParent_->SharedFromThis(), &this->channelSecurity_); + } + bool SocketChannel::SpecifyBufferSize(AuUInt uBytes, const AuSPtr> &pCallbackOptional) { diff --git a/Source/IO/Net/AuNetSocketChannel.hpp b/Source/IO/Net/AuNetSocketChannel.hpp index 04fa0f2d..3b6a4db4 100644 --- a/Source/IO/Net/AuNetSocketChannel.hpp +++ b/Source/IO/Net/AuNetSocketChannel.hpp @@ -11,6 +11,7 @@ #include "AuNetSocketChannelInput.hpp" #include "AuNetSocketChannelOutput.hpp" #include "AuSocketStats.hpp" +#include "AuNetChannelLimits.hpp" namespace Aurora::IO::Net { @@ -81,6 +82,8 @@ namespace Aurora::IO::Net void RemoveEventListener(const AuSPtr &pListener) override; + AuSPtr GetChannelLimits() override; + SocketStats & GetSendStatsEx(); SocketStats & GetRecvStatsEx(); @@ -116,6 +119,7 @@ namespace Aurora::IO::Net AuSPtr pCachedReader; AuSPtr pCachedWriter; + AuThreadPrimitives::SpinLock spinLock; AuList> eventListeners; @@ -128,5 +132,6 @@ namespace Aurora::IO::Net SocketBase * pParent_; SocketStats sendStats_; SocketStats recvStats_; + NetChannelLimits channelSecurity_; }; } \ No newline at end of file diff --git a/Source/IO/Net/AuNetSocketServer.NT.cpp b/Source/IO/Net/AuNetSocketServer.NT.cpp index ca6f9280..abbe72de 100644 --- a/Source/IO/Net/AuNetSocketServer.NT.cpp +++ b/Source/IO/Net/AuNetSocketServer.NT.cpp @@ -17,7 +17,7 @@ namespace Aurora::IO::Net const AuSPtr &pDriver, const AuSPtr &pSocketDriverFactory, AuUInt32 uMaxConnections, - AuUInt32 uDefaultInputStreamSize, + AuUInt32 uDefaultInputStreamSize, bool bMultiThreaded) : SocketServer(pInterface, pWorker, @@ -104,7 +104,7 @@ namespace Aurora::IO::Net We need a sane number between the two. 512 seems reasonable */ - if (::listen(this->osHandle_, 512) != 0) + if (::listen(this->osHandle_, this->uBacklog) != 0) { NetError error; NetError_SetCurrent(error); diff --git a/Source/IO/Net/AuNetSocketServer.Unix.cpp b/Source/IO/Net/AuNetSocketServer.Unix.cpp index 2b3266ed..97872d3e 100644 --- a/Source/IO/Net/AuNetSocketServer.Unix.cpp +++ b/Source/IO/Net/AuNetSocketServer.Unix.cpp @@ -122,7 +122,7 @@ namespace Aurora::IO::Net We need a sane number between the two. 512 seems reasonable */ - if (::listen(this->osHandle_, 512) != 0) + if (::listen(this->osHandle_, this->uBacklog) != 0) { NetError error; NetError_SetCurrent(error); diff --git a/Source/IO/Net/AuNetSocketServer.cpp b/Source/IO/Net/AuNetSocketServer.cpp index fe0b99f5..aa21562e 100644 --- a/Source/IO/Net/AuNetSocketServer.cpp +++ b/Source/IO/Net/AuNetSocketServer.cpp @@ -130,6 +130,47 @@ namespace Aurora::IO::Net return this->pFactory_; } + AuSPtr SocketServer::GetLimits() + { + return AuSPtr(this->SharedFromThis(), &this->serverLimits_); + } + + AuList> SocketServer::GetChildren() + { + AuList> ret; + AU_LOCK_GUARD(this->childrenMutex); + for (const auto &pChild : this->childrenSockets) + { + try + { + ret.push_back(pChild->SharedFromThis()); + } + catch (...) + { + + } + } + return ret; + } + + void SocketServer::OnNotifyChildCreated(SocketBase *pSocket) + { + { + AU_LOCK_GUARD(this->childrenMutex); + this->childrenSockets.push_back(pSocket); + } + } + + void SocketServer::OnNotifyChildRemoved(SocketBase *pSocket) + { + { + AU_LOCK_GUARD(this->childrenMutex); + AuTryRemove(this->childrenSockets, pSocket); + } + + this->serverLimits_.NotifyChildRemoved(); + } + ///////////////////////////////////////////////////////////////////////////////////// // ISocketDriver diff --git a/Source/IO/Net/AuNetSocketServer.hpp b/Source/IO/Net/AuNetSocketServer.hpp index bb050ab0..04be29da 100644 --- a/Source/IO/Net/AuNetSocketServer.hpp +++ b/Source/IO/Net/AuNetSocketServer.hpp @@ -13,9 +13,12 @@ #pragma warning(disable: 4250) #endif +#include "AuNetSocketServerLimits.hpp" + namespace Aurora::IO::Net { struct SocketServer; + struct SocketBase; struct SocketServer : virtual Socket, virtual ISocketDriver, virtual ISocketServer { @@ -33,6 +36,8 @@ namespace Aurora::IO::Net AuSPtr GetServerDriver() override; AuSPtr GetFactory(); + AuSPtr GetLimits() override; + AuList> GetChildren() override; virtual void FinishConstructAsync() override; @@ -51,6 +56,7 @@ namespace Aurora::IO::Net const bool bMultiThreaded; const AuUInt32 uDefaultInputStreamSize; + AuUInt32 uBacklog { 512 }; protected: // INTERFACE: base os socket @@ -79,11 +85,19 @@ namespace Aurora::IO::Net virtual void OnFinalize() override; + // + void OnNotifyChildCreated(SocketBase *pSocket); + void OnNotifyChildRemoved(SocketBase *pSocket); + private: + friend struct SocketBase; + AuSPtr pDriver_; AuUInt32 uMaxConnections_; AuSPtr pFactory_; - + NetSocketServerLimits serverLimits_; + AuMutex childrenMutex; + AuList childrenSockets; }; } diff --git a/Source/IO/Net/AuNetSocketServerAcceptReadOperation.NT.cpp b/Source/IO/Net/AuNetSocketServerAcceptReadOperation.NT.cpp index bad72242..b6435b72 100644 --- a/Source/IO/Net/AuNetSocketServerAcceptReadOperation.NT.cpp +++ b/Source/IO/Net/AuNetSocketServerAcceptReadOperation.NT.cpp @@ -220,7 +220,9 @@ namespace Aurora::IO::Net nextSocketPtr = AuMakeShared(this->pInterface_, pWorker, pNewDriver, - (AuUInt)nextSocket); + (AuUInt)nextSocket, + AuSPtr(this->pParent_->SharedFromThis(), this->pParent_), + this->pParent_); if (!bool(nextSocketPtr)) { // TODO: schedule retry diff --git a/Source/IO/Net/AuNetSocketServerAcceptReadOperation.Unix.cpp b/Source/IO/Net/AuNetSocketServerAcceptReadOperation.Unix.cpp index c8da9337..1b18cd2a 100644 --- a/Source/IO/Net/AuNetSocketServerAcceptReadOperation.Unix.cpp +++ b/Source/IO/Net/AuNetSocketServerAcceptReadOperation.Unix.cpp @@ -97,7 +97,9 @@ namespace Aurora::IO::Net this->nextSocketPtr = AuMakeShared(this->pInterface_, pWorker, pNewDriver, - (AuUInt)this->nextSocket); + (AuUInt)this->nextSocket, + AuSPtr(this->pParent_->SharedFromThis(), this->pParent_), + this->pParent_); if (!bool(this->nextSocketPtr)) { // TODO: schedule retry diff --git a/Source/IO/Net/AuNetSocketServerLimits.cpp b/Source/IO/Net/AuNetSocketServerLimits.cpp new file mode 100644 index 00000000..7b487f30 --- /dev/null +++ b/Source/IO/Net/AuNetSocketServerLimits.cpp @@ -0,0 +1,115 @@ +/*** + Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved. + + File: AuNetSocketServerLimits.cpp + Date: 2023-11-29 + Author: Reece +***/ +#include "Networking.hpp" +#include "AuNetSocketServerLimits.hpp" + +namespace Aurora::IO::Net +{ + void NetSocketServerLimits::NotifyChildRemoved() + { + + } + + AuOptional NetSocketServerLimits::GetMaxConnectionsPerIp() + { + return this->optMaxConnectionsPerIp; + } + + AuOptional NetSocketServerLimits::SetMaxConnectionsPerIp(AuOptional optNextValue) + { + return AuExchange(this->optMaxConnectionsPerIp, optNextValue); + } + + AuOptional NetSocketServerLimits::GetMaxConnections() + { + return this->optMaxConnections; + } + + AuOptional NetSocketServerLimits::SetMaxConnections(AuOptional optNextValue) + { + return AuExchange(this->optMaxConnections, optNextValue); + } + + AuList NetSocketServerLimits::GetWhitelist() + { + return {}; + } + + void NetSocketServerLimits::SetWhitelist(AuList whitelist) + { } + + AuList NetSocketServerLimits::GetBlacklist() + { + return {}; + } + + void NetSocketServerLimits::SetBlacklist(AuList blacklist) + { + + } + + AuOptional NetSocketServerLimits::GetBeforeDDoSDCAverageThroughputLimit() + { + return this->optBeforeDDoSDCAverageThroughputLimit; + } + + AuOptional NetSocketServerLimits::SetBeforeDDoSDCAverageThroughputLimit(AuOptional optNextValue) + { + return AuExchange(this->optBeforeDDoSDCAverageThroughputLimit, optNextValue); + } + + AuOptional NetSocketServerLimits::GetBeforeDDoSDCFrameTimeMS() + { + return this->optBeforeDDoSDCFrameTimeMS; + } + + AuOptional NetSocketServerLimits::SetBeforeDDoSDCFrameTimeMS(AuOptional optNextValue) + { + return AuExchange(this->optBeforeDDoSDCFrameTimeMS, optNextValue); + } + + AuOptional NetSocketServerLimits::GetBeforeDDoSTickFrameLimitCount() + { + return this->optBeforeDDoSTickFrameLimitCount; + } + + AuOptional NetSocketServerLimits::SetBeforeDDoSTickFrameLimitCount(AuOptional optNextValue) + { + return AuExchange(this->optBeforeDDoSTickFrameLimitCount, optNextValue); + } + + AuOptional NetSocketServerLimits::GetBeforeDDoSTickFrameTimeMS() + { + return this->optBeforeDDoSTickFrameTimeMS; + } + + AuOptional NetSocketServerLimits::SetBeforeDDoSTickFrameTimeMS(AuOptional optNextValue) + { + return AuExchange(this->optBeforeDDoSTickFrameTimeMS, optNextValue); + } + + AuOptional NetSocketServerLimits::GetAutoBanOnDDoSDetectA() + { + return this->optAutoBanOnDDoSDetectA; + } + + AuOptional NetSocketServerLimits::SetAutoBanOnDDoSDetectA(AuOptional optNextValue) + { + return AuExchange(this->optAutoBanOnDDoSDetectA, optNextValue); + } + + AuOptional NetSocketServerLimits::GetAutoBanTimeSecs() + { + return this->optAutoBanOnDDoSDetectA; + } + + AuOptional NetSocketServerLimits::SetAutoBanTimeSecs(AuOptional optNextValue) + { + return AuExchange(this->optAutoBanOnDDoSDetectA, optNextValue); + } +} \ No newline at end of file diff --git a/Source/IO/Net/AuNetSocketServerLimits.hpp b/Source/IO/Net/AuNetSocketServerLimits.hpp new file mode 100644 index 00000000..72d6dc96 --- /dev/null +++ b/Source/IO/Net/AuNetSocketServerLimits.hpp @@ -0,0 +1,55 @@ +/*** + Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved. + + File: AuNetSocketServerLimits.hpp + Date: 2023-11-29 + Author: Reece +***/ +#pragma once + +namespace Aurora::IO::Net +{ + struct NetSocketServerLimits : ISocketServerLimits + { + void NotifyChildRemoved(); + + AuOptional GetMaxConnectionsPerIp() override; + AuOptional SetMaxConnectionsPerIp(AuOptional uNextValue) override; + + AuOptional GetMaxConnections() override; + AuOptional SetMaxConnections(AuOptional uNextValue) override; + + AuList GetWhitelist() override; + void SetWhitelist(AuList whitelist) override; + + AuList GetBlacklist() override; + void SetBlacklist(AuList blacklist) override; + + AuOptional GetBeforeDDoSDCAverageThroughputLimit() override; + AuOptional SetBeforeDDoSDCAverageThroughputLimit(AuOptional uNextValue) override; + + AuOptional GetBeforeDDoSDCFrameTimeMS() override; + AuOptional SetBeforeDDoSDCFrameTimeMS(AuOptional uNextValue) override; + + AuOptional GetBeforeDDoSTickFrameLimitCount() override; + AuOptional SetBeforeDDoSTickFrameLimitCount(AuOptional uNextValue) override; + + AuOptional GetBeforeDDoSTickFrameTimeMS() override; + AuOptional SetBeforeDDoSTickFrameTimeMS(AuOptional uNextValue) override; + + AuOptional GetAutoBanOnDDoSDetectA() override; + AuOptional SetAutoBanOnDDoSDetectA(AuOptional optNextValue) override; + + AuOptional GetAutoBanTimeSecs() override; + AuOptional SetAutoBanTimeSecs(AuOptional optNextValue) override; + + AuOptional optBeforeDDoSTickFrameTimeMS; + AuOptional optBeforeDDoSTickFrameLimitCount; + AuOptional optBeforeDDoSDCFrameTimeMS; + AuOptional optBeforeDDoSDCAverageThroughputLimit; + AuOptional optMaxConnectionsPerIp; + AuOptional optMaxConnections; + AuOptional optAutoBanOnDDoSDetectA; + AuOptional optAutoBanTimeSecs; + }; +} \ No newline at end of file diff --git a/Source/IO/Net/AuNetSrvSockets.cpp b/Source/IO/Net/AuNetSrvSockets.cpp index 99c96d85..9aabb64c 100644 --- a/Source/IO/Net/AuNetSrvSockets.cpp +++ b/Source/IO/Net/AuNetSrvSockets.cpp @@ -209,6 +209,11 @@ namespace Aurora::IO::Net return {}; } + if (netBind.uMaxAcceptBacklog) + { + pSocket->uBacklog = netBind.uMaxAcceptBacklog; + } + if (!pWorker->TryScheduleInternalTemplate([=](const AuSPtr> &info) { pSocket->FinishConstructAsync(); diff --git a/Source/IO/Net/AuNetWorker.hpp b/Source/IO/Net/AuNetWorker.hpp index 4a2c5218..ae0859f7 100644 --- a/Source/IO/Net/AuNetWorker.hpp +++ b/Source/IO/Net/AuNetWorker.hpp @@ -4,7 +4,9 @@ namespace Aurora::IO::Net { struct NetSrvWorkers; - struct NetWorker : INetWorker + struct NetWorker : + INetWorker, + AuEnableSharedFromThis { NetWorker(NetSrvWorkers *pParent, AuUInt8 workerIndex, diff --git a/Source/IO/Net/SocketOverDatagram/AuNetDatagramEmulatorISocket.cpp b/Source/IO/Net/SocketOverDatagram/AuNetDatagramEmulatorISocket.cpp index 805d5c7e..d5de6143 100644 --- a/Source/IO/Net/SocketOverDatagram/AuNetDatagramEmulatorISocket.cpp +++ b/Source/IO/Net/SocketOverDatagram/AuNetDatagramEmulatorISocket.cpp @@ -39,7 +39,12 @@ namespace Aurora::IO::Net AuSPtr NetDatagramEmulatorISocket::ToChannel() { - return this->pParent->ToChannel(); + if (auto pParent = this->pParent) + { + return pParent->ToChannel(); + } + + return {}; } const NetEndpoint &NetDatagramEmulatorISocket::GetRemoteEndpoint() @@ -49,6 +54,34 @@ namespace Aurora::IO::Net AuSPtr NetDatagramEmulatorISocket::GetUserDriver() { - return this->pParent->GetUserDriver(); + if (auto pParent = this->pParent) + { + return pParent->GetUserDriver(); + } + + return {}; + } + + AuSPtr NetDatagramEmulatorISocket::GetLockedWorkerThread() + { + if (auto pParent = this->pParent) + { + return pParent->pWorker; + } + + return {}; + } + + AuSPtr NetDatagramEmulatorISocket::GetSocketServer() + { + if (auto pParent = this->pParent) + { + if (auto pParent2 = AuTryLockMemoryType(pParent->pParentServer)) + { + return pParent2->ToSocketServer(); + } + } + + return {}; } } \ No newline at end of file diff --git a/Source/IO/Net/SocketOverDatagram/AuNetDatagramEmulatorISocket.hpp b/Source/IO/Net/SocketOverDatagram/AuNetDatagramEmulatorISocket.hpp index 645ed2c9..090c0531 100644 --- a/Source/IO/Net/SocketOverDatagram/AuNetDatagramEmulatorISocket.hpp +++ b/Source/IO/Net/SocketOverDatagram/AuNetDatagramEmulatorISocket.hpp @@ -32,5 +32,9 @@ namespace Aurora::IO::Net const NetEndpoint &GetRemoteEndpoint() override; AuSPtr GetUserDriver() override; + + AuSPtr GetSocketServer() override; + + AuSPtr GetLockedWorkerThread() override; }; } \ No newline at end of file diff --git a/Source/IO/Net/SocketOverDatagram/AuNetDatagramEmulatorISocketServer.cpp b/Source/IO/Net/SocketOverDatagram/AuNetDatagramEmulatorISocketServer.cpp index a8fd932b..5ccb9685 100644 --- a/Source/IO/Net/SocketOverDatagram/AuNetDatagramEmulatorISocketServer.cpp +++ b/Source/IO/Net/SocketOverDatagram/AuNetDatagramEmulatorISocketServer.cpp @@ -22,21 +22,70 @@ namespace Aurora::IO::Net void NetDatagramEmulatorISocketServer::Shutdown(bool bNow) { - this->pParent->Shutdown(bNow); + if (auto pParent = this->pParent) + { + return pParent->Shutdown(bNow); + } } void NetDatagramEmulatorISocketServer::Destroy() { - this->pParent->Destroy(); + if (auto pParent = this->pParent) + { + return pParent->Destroy(); + } } AuUInt NetDatagramEmulatorISocketServer::ToPlatformHandle() { - return this->pParent->ToPlatformHandle(); + if (auto pParent = this->pParent) + { + return pParent->ToPlatformHandle(); + } + else + { + return {}; + } } AuSPtr NetDatagramEmulatorISocketServer::GetServerDriver() { - return this->pParent->GetUserServerDriver(); + if (auto pParent = this->pParent) + { + return pParent->GetUserServerDriver(); + } + else + { + return {}; + } + } + + AuSPtr NetDatagramEmulatorISocketServer::GetLimits() + { + return {}; + } + + AuList> NetDatagramEmulatorISocketServer::GetChildren() + { + if (auto pParent = this->pParent) + { + return pParent->GetAllChildren(); + } + else + { + return {}; + } + } + + AuSPtr NetDatagramEmulatorISocketServer::GetLockedWorkerThread() + { + if (auto pParent = this->pParent) + { + return pParent->GetLockedWorkerThread(); + } + else + { + return {}; + } } } \ No newline at end of file diff --git a/Source/IO/Net/SocketOverDatagram/AuNetDatagramEmulatorISocketServer.hpp b/Source/IO/Net/SocketOverDatagram/AuNetDatagramEmulatorISocketServer.hpp index 8aa3c0d8..1d49a280 100644 --- a/Source/IO/Net/SocketOverDatagram/AuNetDatagramEmulatorISocketServer.hpp +++ b/Source/IO/Net/SocketOverDatagram/AuNetDatagramEmulatorISocketServer.hpp @@ -28,5 +28,8 @@ namespace Aurora::IO::Net AuUInt ToPlatformHandle() override; AuSPtr GetServerDriver() override; + AuSPtr GetLimits() override; + AuList> GetChildren() override; + AuSPtr GetLockedWorkerThread() override; }; } \ No newline at end of file diff --git a/Source/IO/Net/SocketOverDatagram/AuNetDatagramSocketServer.cpp b/Source/IO/Net/SocketOverDatagram/AuNetDatagramSocketServer.cpp index c30cd06e..a05c32c6 100644 --- a/Source/IO/Net/SocketOverDatagram/AuNetDatagramSocketServer.cpp +++ b/Source/IO/Net/SocketOverDatagram/AuNetDatagramSocketServer.cpp @@ -204,6 +204,40 @@ namespace Aurora::IO::Net return ret; } + AuList> NetDatagramSocketServer::GetAllChildren() + { + AuList> ret; + + { + AU_LOCK_GUARD(this->mutex_); + + for (const auto &[a, list] : this->sessions) + { + for (const auto &pItem : list) + { + if (pItem) + { + ret.push_back(pItem->ToSocket()); + } + } + } + } + + return ret; + } + + AuSPtr NetDatagramSocketServer::GetLockedWorkerThread() + { + if (auto pWorker = this->pWorker_) + { + return pWorker->SharedFromThis(); + } + else + { + return {}; + } + } + void NetDatagramSocketServer::Shutdown(bool bNow) { this->eviction.EvictAll(); diff --git a/Source/IO/Net/SocketOverDatagram/AuNetDatagramSocketServer.hpp b/Source/IO/Net/SocketOverDatagram/AuNetDatagramSocketServer.hpp index 29b8adb6..ba04da0d 100644 --- a/Source/IO/Net/SocketOverDatagram/AuNetDatagramSocketServer.hpp +++ b/Source/IO/Net/SocketOverDatagram/AuNetDatagramSocketServer.hpp @@ -38,12 +38,14 @@ namespace Aurora::IO::Net AuSPtr GetSession(const NetEndpoint &endpoint); void Cleanup(const AuSPtr &pSession); AuList> GetAllSessions(); + AuList> GetAllChildren(); AuSPtr ToDriver(); AuSPtr ToSocketServer(); const NetError &GetError() override; const NetEndpoint &GetLocalEndpoint() override; + AuSPtr GetLockedWorkerThread() override; void OnError(const NetError &error); void OnFatalErrorReported(const NetError &error); diff --git a/Source/IO/Net/SocketOverDatagram/AuNetDatagramSocketServerChannel.cpp b/Source/IO/Net/SocketOverDatagram/AuNetDatagramSocketServerChannel.cpp index 9bc1e63d..7fca19f6 100644 --- a/Source/IO/Net/SocketOverDatagram/AuNetDatagramSocketServerChannel.cpp +++ b/Source/IO/Net/SocketOverDatagram/AuNetDatagramSocketServerChannel.cpp @@ -425,6 +425,11 @@ namespace Aurora::IO::Net return this->pBuffer->RemainingBytes(); } + AuSPtr NetDatagramSocketServerChannel::GetChannelLimits() + { + return {}; + } + SocketStats &NetDatagramSocketServerChannel::GetSendStatsEx() { return this->sendStats_; diff --git a/Source/IO/Net/SocketOverDatagram/AuNetDatagramSocketServerChannel.hpp b/Source/IO/Net/SocketOverDatagram/AuNetDatagramSocketServerChannel.hpp index a996f73f..b60e1292 100644 --- a/Source/IO/Net/SocketOverDatagram/AuNetDatagramSocketServerChannel.hpp +++ b/Source/IO/Net/SocketOverDatagram/AuNetDatagramSocketServerChannel.hpp @@ -74,6 +74,8 @@ namespace Aurora::IO::Net AuUInt GetNextFrameTargetLength() override; + AuSPtr GetChannelLimits() override; + SocketStats &GetSendStatsEx(); SocketStats &GetRecvStatsEx();