Added the capability for custom encryption's to be added to EHC.

This commit is contained in:
2024-10-06 22:54:29 -07:00
parent 1feff0a25c
commit 8b01ee3c46
15 changed files with 392 additions and 182 deletions

View File

@@ -1,10 +1,10 @@
#include "ehs/io/socket/EHC.h"
#include "ehs/io/socket/ehc/NetSystem.h"
#include "ehs/io/socket/ehc/NetEnc.h"
#include "ehs/io/socket/ehc/NetSys.h"
#include "ehs/io/socket/ehc/NetEnd.h"
#include "ehs/Log.h"
#include "ehs/PRNG.h"
#include <ehe/Encryption.h>
namespace ehs
{
@@ -54,11 +54,15 @@ namespace ehs
EHC::EHC(EHC &&sock) noexcept
: udp((UDP&&)sock.udp), appVer(sock.appVer), disposition(sock.disposition), hashName(sock.hashName),
name((Str_8&&)sock.name), dropPackets(sock.dropPackets), buffer(sock.buffer), bufferSize(sock.bufferSize),
systems((Array<NetSystem *> &&)sock.systems), maxEndpoints(sock.maxEndpoints), lastTSC(sock.lastTSC),
delta(sock.delta), maxTimeout(sock.maxTimeout), resendRate(sock.resendRate), connectCb(sock.connectCb),
connectedCb(sock.connectedCb), activeCb(sock.activeCb), disconnectCb(sock.disconnectCb),
disconnectedCb(sock.disconnectedCb), timeoutCb(sock.timeoutCb), endpoints((Vector<NetEnd *> &&)sock.endpoints)
encryptions((Array<NetEnc *> &&)sock.encryptions), systems((Array<NetSys *> &&)sock.systems),
maxEndpoints(sock.maxEndpoints), lastTSC(sock.lastTSC), delta(sock.delta), maxTimeout(sock.maxTimeout),
resendRate(sock.resendRate), connectCb(sock.connectCb), connectedCb(sock.connectedCb), activeCb(sock.activeCb),
disconnectCb(sock.disconnectCb), disconnectedCb(sock.disconnectedCb), timeoutCb(sock.timeoutCb),
endpoints((Vector<NetEnd *> &&)sock.endpoints)
{
for (UInt_64 i = 0; i < encryptions.Size(); ++i)
endpoints[i]->owner = this;
for (UInt_64 i = 0; i < endpoints.Size(); ++i)
endpoints[i]->owner = this;
@@ -107,7 +111,12 @@ namespace ehs
buffer = sock.buffer;
bufferSize = sock.bufferSize;
systems = (Array<NetSystem *> &&)sock.systems;
encryptions = (Array<NetEnc *> &&)sock.encryptions;
for (UInt_64 i = 0; i < encryptions.Size(); ++i)
encryptions[i]->owner = this;
systems = (Array<NetSys *> &&)sock.systems;
maxEndpoints = sock.maxEndpoints;
lastTSC = sock.lastTSC;
delta = sock.delta;
@@ -161,7 +170,7 @@ namespace ehs
buffer = nullptr;
bufferSize = 0;
systems = Array<NetSystem *>();
systems = Array<NetSys *>();
maxEndpoints = sock.maxEndpoints;
lastTSC = 0;
delta = 0.0f;
@@ -273,7 +282,7 @@ namespace ehs
return true;
}
void EHC::Broadcast(const EndDisp endDisp, const Status endStatus, const bool deltaLocked, const bool encrypted,
void EHC::Broadcast(const EndDisp endDisp, const Status endStatus, const bool deltaLocked, const UInt_64 encHashId,
const bool ensure, const UInt_64 sysHashId, const UInt_64 opHashId, const Serializer<UInt_64> &payload)
{
if (!udp.IsValid())
@@ -285,15 +294,15 @@ namespace ehs
continue;
if (endpoints[i]->GetStatus() == endStatus)
endpoints[i]->Send(deltaLocked, encrypted, ensure, sysHashId, opHashId, payload);
endpoints[i]->Send(deltaLocked, encHashId, ensure, sysHashId, opHashId, payload);
}
}
void EHC::Broadcast(const EndDisp endDisp, const Status endStatus, const bool deltaLocked, const bool encrypted,
void EHC::Broadcast(const EndDisp endDisp, const Status endStatus, const bool deltaLocked, const Str_8 &encId,
const bool ensure, const Str_8 &sysId, const Str_8 &opId,
const Serializer<UInt_64> &payload)
{
Broadcast(endDisp, endStatus, deltaLocked, encrypted, ensure, sysId.Hash_64(), opId.Hash_64(), payload);
Broadcast(endDisp, endStatus, deltaLocked, encId.Hash_64(), ensure, sysId.Hash_64(), opId.Hash_64(), payload);
}
void EHC::Poll()
@@ -315,9 +324,15 @@ namespace ehs
{
Serializer<> payload(Endianness::LE, buffer, received);
bool encrypted = payload.Read<bool>();
if (encrypted)
Encryption::Encrypt_64(payload.Size() - payload.GetOffset(), &payload[payload.GetOffset()]);
UInt_64 encHashId = payload.Read<UInt_64>();
if (encHashId)
{
NetEnc* enc = GetEncryption(encHashId);
if (!enc)
continue;
enc->Encrypt(&payload[payload.GetOffset()], payload.Size() - payload.GetOffset());
}
payload.SetOffset(0);
@@ -609,6 +624,45 @@ namespace ehs
return dropPackets;
}
bool EHC::HasEncryption(UInt_64 encHashId) const
{
for (UInt_64 i = 0; i < encryptions.Size(); ++i)
if (encryptions[i]->GetHashId() == encHashId)
return true;
return false;
}
bool EHC::HasEncryption(const Str_8& encId) const
{
return HasEncryption(encId.Hash_64());
}
bool EHC::AddEncryption(NetEnc* enc)
{
if (HasEncryption(enc->GetHashId()))
return false;
enc->owner = this;
encryptions.Push(enc);
return true;
}
NetEnc* EHC::GetEncryption(UInt_64 encHashId) const
{
for (UInt_64 i = 0; i < encryptions.Size(); ++i)
if (encryptions[i]->GetHashId() == encHashId)
return encryptions[i];
return nullptr;
}
NetEnc* EHC::GetEncryption(const Str_8& encId) const
{
return GetEncryption(encId.Hash_64());
}
bool EHC::HasSystem(const UInt_64 sysHashId) const
{
if (internalSys == sysHashId)
@@ -626,7 +680,7 @@ namespace ehs
return HasSystem(sysId.Hash_64());
}
bool EHC::AddSystem(NetSystem *sys)
bool EHC::AddSystem(NetSys *sys)
{
if (HasSystem(sys->GetHashId()))
return false;
@@ -636,7 +690,7 @@ namespace ehs
return true;
}
NetSystem* EHC::GetSystem(const UInt_64 sysHashId) const
NetSys* EHC::GetSystem(const UInt_64 sysHashId) const
{
for (UInt_64 i = 0; i < systems.Size(); ++i)
if (systems[i]->GetHashId() == sysHashId)
@@ -645,7 +699,7 @@ namespace ehs
return nullptr;
}
NetSystem* EHC::GetSystem(const Str_8& sysId) const
NetSys* EHC::GetSystem(const Str_8& sysId) const
{
return GetSystem(sysId.Hash_64());
}
@@ -1121,7 +1175,7 @@ namespace ehs
continue;
}
Vector<Fragments>* frags = endpoints[i]->GetReceived();
Vector<NetFrags>* frags = endpoints[i]->GetReceived();
UInt_64 f = 0;
while (f < frags->Size())
@@ -1134,7 +1188,7 @@ namespace ehs
Packet packet = (*frags)[f].Combine();
NetSystem* sys = GetSystem(packet.header.system);
NetSys* sys = GetSystem(packet.header.system);
if (!sys)
{
++f;