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,8 +1,7 @@
#include "ehs/io/socket/ehc/NetEnd.h"
#include "ehs/io/socket/EHC.h"
#include <ehs/system/CPU.h>
#include <ehe/Encryption.h>
#include "ehs/io/socket/ehc/NetEnc.h"
#include "ehs/system/CPU.h"
namespace ehs
{
@@ -33,10 +32,10 @@ namespace ehs
NetEnd::NetEnd(NetEnd &&end) noexcept
: owner(end.owner), disposition(end.disposition), hashName(end.hashName), name((Str_8&&)end.name), status(end.status), arch(end.arch), token{},
nextSendId(end.nextSendId), sent((Vector<Insurance>&&)end.sent), nextRecvId(end.nextRecvId),
received((Vector<Fragments>&&)end.received), type(end.type), address((Str_8&&)end.address), port(end.port),
deltaDuration(end.deltaDuration), deltaRate(end.deltaRate), timeout(end.timeout), lastPing(end.lastPing),
oldLatency(end.oldLatency), latency(end.latency), queueSlot(end.queueSlot)
nextSendId(end.nextSendId), sent((Vector<Insurance>&&)end.sent), nextRecvId(end.nextRecvId),
received((Vector<NetFrags>&&)end.received), type(end.type), address((Str_8&&)end.address), port(end.port),
deltaDuration(end.deltaDuration), deltaRate(end.deltaRate), timeout(end.timeout), lastPing(end.lastPing),
oldLatency(end.oldLatency), latency(end.latency), queueSlot(end.queueSlot)
{
end.owner = nullptr;
end.disposition = EndDisp::ENDPOINT;
@@ -80,7 +79,7 @@ namespace ehs
nextSendId = end.nextSendId;
sent = (Vector<Insurance>&&)end.sent;
nextRecvId = end.nextRecvId;
received = (Vector<Fragments>&&)end.received;
received = (Vector<NetFrags>&&)end.received;
type = end.type;
address = (Str_8&&)end.address;
port = end.port;
@@ -173,14 +172,14 @@ namespace ehs
return nextSendId;
}
void NetEnd::Send(const bool deltaLocked, const bool encrypted, const bool ensure, const UInt_64 sys,
const UInt_64 op, const Serializer<>& payload)
void NetEnd::Send(const bool deltaLocked, const UInt_64 encHashId, const bool ensure, const UInt_64 sys,
const UInt_64 op, const Serializer<UInt_64> &payload)
{
if (deltaLocked && deltaDuration < deltaRate)
return;
Header header = {
encrypted,
encHashId,
nextSendId++,
1,
0,
@@ -193,9 +192,9 @@ namespace ehs
Util::Copy(header.token, token, 64);
if ((owner->GetLocalAddressType() == AddrType::IPV6 && payload.Size() > COMMS_IPV6_PAYLOAD) || (owner->GetLocalAddressType() == AddrType::IPV4 && payload.Size() > COMMS_IPV4_PAYLOAD))
if ((owner->GetLocalAddressType() == AddrType::IPV6 && payload.Size() > EHC_IPV6_PAYLOAD) || (owner->GetLocalAddressType() == AddrType::IPV4 && payload.Size() > EHC_IPV4_PAYLOAD))
{
Fragments frags = FragmentData(header, payload);
NetFrags frags = FragmentData(header, payload);
for (UInt_64 i = 0; i < frags.Size(); ++i)
{
Header newHeader = frags.GetHeader();
@@ -210,10 +209,10 @@ namespace ehs
}
}
void NetEnd::Send(const bool deltaLocked, const bool encrypted, const bool ensure, const Str_8& sys,
const Str_8& op, const Serializer<>& payload)
void NetEnd::Send(const bool deltaLocked, const Str_8 &encId, const bool ensure, const Str_8& sys,
const Str_8& op, const Serializer<>& payload)
{
Send(deltaLocked, encrypted, ensure, sys.Hash_64(), op.Hash_64(), payload);
Send(deltaLocked, encId.Hash_64(), ensure, sys.Hash_64(), op.Hash_64(), payload);
}
UInt_64 NetEnd::GetNextRecvId() const
@@ -278,8 +277,19 @@ namespace ehs
result.Write(sent[i].header);
result.WriteSer(sent[i].payload);
if (sent[i].header.encrypted)
Encryption::Encrypt_64(result.Size() - sizeof(bool), &result[sizeof(bool)]);
if (sent[i].header.encHashId)
{
NetEnc *enc = owner->GetEncryption(sent[i].header.encHashId);
if (!enc)
{
EHS_LOG_INT(LogType::WARN, 0, "The network encryption with the hash id " +
Str_8::FromNum(sent[i].header.encHashId) + ", does not exist.");
continue;
}
enc->Encrypt(&result[sizeof(bool)], result.Size() - sizeof(bool));
}
owner->udp.Send(type, address, port, result, result.Size());
@@ -294,6 +304,8 @@ namespace ehs
if (lastPing >= 1.0f)
Ping(delta);
}
EHS_LOG_SUCCESS();
}
void NetEnd::SetStatus(const Status newStatus)
@@ -317,7 +329,7 @@ namespace ehs
void NetEnd::AddReceived(const Header& header, const Serializer<>& payload)
{
Fragments* frags = nullptr;
NetFrags* frags = nullptr;
for (UInt_64 i = 0; i < received.Size(); ++i)
{
@@ -342,7 +354,7 @@ namespace ehs
timeout = 0.0f;
}
Vector<Fragments>* NetEnd::GetReceived()
Vector<NetFrags>* NetEnd::GetReceived()
{
return &received;
}
@@ -395,46 +407,46 @@ namespace ehs
queueSlot = slot;
}
Fragments NetEnd::FragmentData(const Header& header, const Serializer<>& data)
NetFrags NetEnd::FragmentData(const Header& header, const Serializer<>& data)
{
Fragments result;
NetFrags result;
if (owner->GetLocalAddressType() == AddrType::IPV6)
{
UInt_64 frags = data.Size() / COMMS_IPV6_PAYLOAD;
if (data.Size() % COMMS_IPV6_PAYLOAD)
UInt_64 frags = data.Size() / EHC_IPV6_PAYLOAD;
if (data.Size() % EHC_IPV6_PAYLOAD)
++frags;
result = Fragments(header, frags);
result = NetFrags(header, frags);
UInt_64 size = COMMS_IPV6_PAYLOAD;
UInt_64 size = EHC_IPV6_PAYLOAD;
for (UInt_64 i = 0; i < result.Size(); ++i)
{
size = COMMS_IPV6_PAYLOAD;
size = EHC_IPV6_PAYLOAD;
if (i == result.Size() - 1)
size = data.Size() % COMMS_IPV6_PAYLOAD;
size = data.Size() % EHC_IPV6_PAYLOAD;
result[i] = {data.GetEndianness(), &data[i * COMMS_IPV6_PAYLOAD], size};
result[i] = {data.GetEndianness(), &data[i * EHC_IPV6_PAYLOAD], size};
}
}
else if (owner->GetLocalAddressType() == AddrType::IPV4)
{
UInt_64 frags = data.Size() / COMMS_IPV4_PAYLOAD;
if (data.Size() % COMMS_IPV4_PAYLOAD)
UInt_64 frags = data.Size() / EHC_IPV4_PAYLOAD;
if (data.Size() % EHC_IPV4_PAYLOAD)
++frags;
result = Fragments(header, frags);
result = NetFrags(header, frags);
UInt_64 size = COMMS_IPV4_PAYLOAD;
UInt_64 size = EHC_IPV4_PAYLOAD;
for (UInt_64 i = 0; i < result.Size(); ++i)
{
size = COMMS_IPV4_PAYLOAD;
size = EHC_IPV4_PAYLOAD;
if (i == result.Size() - 1)
size = data.Size() % COMMS_IPV4_PAYLOAD;
size = data.Size() % EHC_IPV4_PAYLOAD;
result[i] = {data.GetEndianness(), &data[i * COMMS_IPV4_PAYLOAD], size};
result[i] = {data.GetEndianness(), &data[i * EHC_IPV4_PAYLOAD], size};
}
}
@@ -447,8 +459,19 @@ namespace ehs
result.Write(header);
result.WriteSer(payload);
if (header.encrypted)
Encryption::Encrypt_64(result.Size() - sizeof(bool), &result[sizeof(bool)]);
if (header.encHashId)
{
NetEnc *enc = owner->GetEncryption(header.encHashId);
if (!enc)
{
EHS_LOG_INT(LogType::WARN, 0, "The network encryption with the hash id " +
Str_8::FromNum(header.encHashId) + ", does not exist.");
return;
}
enc->Encrypt(&result[sizeof(bool)], result.Size() - sizeof(bool));
}
if (header.ensure)
sent.Push({header, payload});
@@ -476,7 +499,7 @@ namespace ehs
if (!SortingNeeded())
return;
Vector<Fragments> sorted(0, received.Stride());
Vector<NetFrags> sorted(0, received.Stride());
for (UInt_64 a = 0; a < received.Size(); ++a)
{