From bce9420fb8dd066188900d44248453a11a3d08d5 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 26 Dec 2024 21:49:37 +0100 Subject: META: Overall improvements and fixes to ZkaOS. Signed-off-by: Amlal El Mahrouss --- dev/Kernel/src/Network/IP.cc | 126 ------------------------------------- dev/Kernel/src/Network/IPAddr.cc | 129 ++++++++++++++++++++++++++++++++++++++ dev/Kernel/src/Network/IPC.cc | 115 --------------------------------- dev/Kernel/src/Network/IPCAddr.cc | 0 dev/Kernel/src/Network/IPCMsg.cc | 113 +++++++++++++++++++++++++++++++++ 5 files changed, 242 insertions(+), 241 deletions(-) delete mode 100644 dev/Kernel/src/Network/IP.cc create mode 100644 dev/Kernel/src/Network/IPAddr.cc delete mode 100644 dev/Kernel/src/Network/IPC.cc create mode 100644 dev/Kernel/src/Network/IPCAddr.cc create mode 100644 dev/Kernel/src/Network/IPCMsg.cc (limited to 'dev/Kernel/src/Network') diff --git a/dev/Kernel/src/Network/IP.cc b/dev/Kernel/src/Network/IP.cc deleted file mode 100644 index 2a07bc62..00000000 --- a/dev/Kernel/src/Network/IP.cc +++ /dev/null @@ -1,126 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2024, Theater Quality Inc, all rights reserved. - -------------------------------------------- */ - -#include -#include - -namespace Kernel -{ - char* RawIPAddress::Address() - { - return fAddr; - } - - RawIPAddress::RawIPAddress(char bytes[4]) - { - rt_copy_memory(bytes, fAddr, 4); - } - - bool RawIPAddress::operator==(const RawIPAddress& ipv4) - { - for (Size index = 0; index < 4; ++index) - { - if (ipv4.fAddr[index] != fAddr[index]) - return false; - } - - return true; - } - - bool RawIPAddress::operator!=(const RawIPAddress& ipv4) - { - for (Size index = 0; index < 4; ++index) - { - if (ipv4.fAddr[index] == fAddr[index]) - return false; - } - - return true; - } - - char& RawIPAddress::operator[](const Size& index) - { - kcout << "[RawIPAddress::operator[]] Fetching Index...\r"; - - static char IP_PLACEHOLDER = '0'; - if (index > 4) - return IP_PLACEHOLDER; - - return fAddr[index]; - } - - RawIPAddress6::RawIPAddress6(char bytes[8]) - { - rt_copy_memory(bytes, fAddr, 8); - } - - char& RawIPAddress6::operator[](const Size& index) - { - kcout << "[RawIPAddress6::operator[]] Fetching Index...\r"; - - static char IP_PLACEHOLDER = '0'; - if (index > 8) - return IP_PLACEHOLDER; - - return fAddr[index]; - } - - bool RawIPAddress6::operator==(const RawIPAddress6& ipv6) - { - for (SizeT index = 0; index < 8; ++index) - { - if (ipv6.fAddr[index] != fAddr[index]) - return false; - } - - return true; - } - - bool RawIPAddress6::operator!=(const RawIPAddress6& ipv6) - { - for (SizeT index = 0; index < 8; ++index) - { - if (ipv6.fAddr[index] == fAddr[index]) - return false; - } - - return true; - } - - ErrorOr IPFactory::ToKString(Ref& ipv6) - { - auto str = StringBuilder::Construct(ipv6.Leak().Address()); - return str; - } - - ErrorOr IPFactory::ToKString(Ref& ipv4) - { - auto str = StringBuilder::Construct(ipv4.Leak().Address()); - return str; - } - - bool IPFactory::IpCheckVersion4(const Char* ip) - { - int cnter = 0; - - for (Size base = 0; base < rt_string_len(ip); ++base) - { - if (ip[base] == '.') - { - cnter = 0; - } - else - { - if (cnter == 3) - return false; - - ++cnter; - } - } - - return true; - } -} // namespace Kernel diff --git a/dev/Kernel/src/Network/IPAddr.cc b/dev/Kernel/src/Network/IPAddr.cc new file mode 100644 index 00000000..f8e3e903 --- /dev/null +++ b/dev/Kernel/src/Network/IPAddr.cc @@ -0,0 +1,129 @@ +/* ------------------------------------------- + + Copyright (C) 2024, Theater Quality Inc, all rights reserved. + +------------------------------------------- */ + +#include +#include + +namespace Kernel +{ + char* RawIPAddress::Address() + { + return fAddr; + } + + RawIPAddress::RawIPAddress(char bytes[4]) + { + rt_copy_memory(bytes, fAddr, 4); + } + + bool RawIPAddress::operator==(const RawIPAddress& ipv4) + { + for (Size index = 0; index < 4; ++index) + { + if (ipv4.fAddr[index] != fAddr[index]) + return false; + } + + return true; + } + + bool RawIPAddress::operator!=(const RawIPAddress& ipv4) + { + for (Size index = 0; index < 4; ++index) + { + if (ipv4.fAddr[index] == fAddr[index]) + return false; + } + + return true; + } + + char& RawIPAddress::operator[](const Size& index) + { + kcout << "[RawIPAddress::operator[]] Fetching Index...\r"; + + static char IP_PLACEHOLDER = '0'; + if (index > 4) + return IP_PLACEHOLDER; + + return fAddr[index]; + } + + RawIPAddress6::RawIPAddress6(char bytes[8]) + { + rt_copy_memory(bytes, fAddr, 8); + } + + char& RawIPAddress6::operator[](const Size& index) + { + kcout << "[RawIPAddress6::operator[]] Fetching Index...\r"; + + static char IP_PLACEHOLDER = '0'; + if (index > 8) + return IP_PLACEHOLDER; + + return fAddr[index]; + } + + bool RawIPAddress6::operator==(const RawIPAddress6& ipv6) + { + for (SizeT index = 0; index < 8; ++index) + { + if (ipv6.fAddr[index] != fAddr[index]) + return false; + } + + return true; + } + + bool RawIPAddress6::operator!=(const RawIPAddress6& ipv6) + { + for (SizeT index = 0; index < 8; ++index) + { + if (ipv6.fAddr[index] == fAddr[index]) + return false; + } + + return true; + } + + ErrorOr IPFactory::ToKString(Ref& ipv6) + { + auto str = StringBuilder::Construct(ipv6.Leak().Address()); + return str; + } + + ErrorOr IPFactory::ToKString(Ref& ipv4) + { + auto str = StringBuilder::Construct(ipv4.Leak().Address()); + return str; + } + + bool IPFactory::IpCheckVersion4(const Char* ip) + { + if (!ip) + return NO; + + Int32 cnter = 0; + + for (SizeT base = 0; base < rt_string_len(ip); ++base) + { + if (ip[base] == '.') + { + cnter = 0; + } + else + { + if (cnter == 3) + return false; + + ++cnter; + } + } + + return true; + } +} // namespace Kernel diff --git a/dev/Kernel/src/Network/IPC.cc b/dev/Kernel/src/Network/IPC.cc deleted file mode 100644 index 44b4665c..00000000 --- a/dev/Kernel/src/Network/IPC.cc +++ /dev/null @@ -1,115 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2024, Theater Quality Inc, all rights reserved. - -------------------------------------------- */ - -#include -#include -#include - -using namespace Kernel; - -/// @internal -/// @brief The internal sanitize function. -Bool ipc_int_sanitize_packet(IPCMessage* pckt) -{ - auto endian = rtl_deduce_endianess(pckt, ((Char*)pckt)[0]); - - switch (endian) - { - case Endian::kEndianBig: { - if (pckt->IpcEndianess == kIPCLittleEndian) - goto ipc_check_failed; - - break; - } - case Endian::kEndianLittle: { - if (pckt->IpcEndianess == kIPCBigEndian) - goto ipc_check_failed; - - break; - } - case Endian::kEndianMixed: { - if (pckt->IpcEndianess == kIPCMixedEndian) - goto ipc_check_failed; - - break; - } - default: - goto ipc_check_failed; - } - - if (pckt->IpcFrom == pckt->IpcTo || - pckt->IpcPacketSize > kIPCMsgSize) - { - goto ipc_check_failed; - } - - return pckt->IpcPacketSize > 1 && pckt->IpcHeaderMagic == kIPCHeaderMagic; - -ipc_check_failed: - err_local_get() = kErrorIPC; - return false; -} - -namespace Kernel -{ - /// @brief Sanitize packet function - /// @retval true packet is correct. - /// @retval false packet is incorrect and process has crashed. - Bool ipc_sanitize_packet(IPCMessage* pckt) - { - if (!pckt || - !ipc_int_sanitize_packet(pckt)) - { - UserProcessScheduler::The().GetCurrentProcess().Leak().Crash(); - return false; - } - - return true; - } - - /// @brief Construct packet function - /// @retval true packet is correct. - /// @retval false packet is incorrect and process has crashed. - Bool ipc_construct_packet(_Output IPCMessage** pckt_in) - { - // don't act if it's not even valid. - if (!pckt_in) - return false; - - // don't do anything if it's valid already. - if (*pckt_in) - return true; - - // crash process if the packet pointer of pointer is NULL. - if (!pckt_in) - { - UserProcessScheduler::The().GetCurrentProcess().Leak().Crash(); - return false; - } - - *pckt_in = new IPCMessage(); - - if (*pckt_in) - { - auto endian = rtl_deduce_endianess((*pckt_in), ((Char*)(*pckt_in))[0]); - - (*pckt_in)->IpcHeaderMagic = kIPCHeaderMagic; - - (*pckt_in)->IpcEndianess = static_cast(endian); - (*pckt_in)->IpcPacketSize = sizeof(IPCMessage); - - (*pckt_in)->IpcTo.UserProcessID = 0; - (*pckt_in)->IpcTo.UserProcessTeam = 0; - - (*pckt_in)->IpcFrom.UserProcessID = Kernel::UserProcessScheduler::The().GetCurrentProcess().Leak().ProcessId; - (*pckt_in)->IpcFrom.UserProcessTeam = Kernel::UserProcessScheduler::The().CurrentTeam().mTeamId; - - return Yes; - } - - return No; - } -} // namespace Kernel diff --git a/dev/Kernel/src/Network/IPCAddr.cc b/dev/Kernel/src/Network/IPCAddr.cc new file mode 100644 index 00000000..e69de29b diff --git a/dev/Kernel/src/Network/IPCMsg.cc b/dev/Kernel/src/Network/IPCMsg.cc new file mode 100644 index 00000000..c93f7e7a --- /dev/null +++ b/dev/Kernel/src/Network/IPCMsg.cc @@ -0,0 +1,113 @@ +/* ------------------------------------------- + + Copyright (C) 2024, Theater Quality Inc, all rights reserved. + +------------------------------------------- */ + +#include +#include +#include + +namespace Kernel +{ + /// @internal + /// @brief The internal sanitize function. + Bool ipc_int_sanitize_packet(IPC_MSG* pckt) + { + auto endian = rtl_deduce_endianess(pckt, ((Char*)pckt)[0]); + + switch (endian) + { + case Endian::kEndianBig: { + if (pckt->IpcEndianess == kIPCLittleEndian) + goto ipc_check_failed; + + break; + } + case Endian::kEndianLittle: { + if (pckt->IpcEndianess == kIPCBigEndian) + goto ipc_check_failed; + + break; + } + case Endian::kEndianMixed: { + if (pckt->IpcEndianess == kIPCMixedEndian) + goto ipc_check_failed; + + break; + } + default: + goto ipc_check_failed; + } + + if (pckt->IpcFrom == pckt->IpcTo || + pckt->IpcPacketSize > kIPCMsgSize) + { + goto ipc_check_failed; + } + + return pckt->IpcPacketSize > 1 && pckt->IpcHeaderMagic == kIPCHeaderMagic; + + ipc_check_failed: + err_local_get() = kErrorIPC; + return false; + } + + /// @brief Sanitize packet function + /// @retval true packet is correct. + /// @retval false packet is incorrect and process has crashed. + Bool ipc_sanitize_packet(IPC_MSG* pckt) + { + if (!pckt || + !ipc_int_sanitize_packet(pckt)) + { + UserProcessScheduler::The().GetCurrentProcess().Leak().Crash(); + return false; + } + + return true; + } + + /// @brief Construct packet function + /// @retval true packet is correct. + /// @retval false packet is incorrect and process has crashed. + Bool ipc_construct_packet(_Output IPC_MSG** pckt_in) + { + // don't act if it's not even valid. + if (!pckt_in) + return false; + + // don't do anything if it's valid already. + if (*pckt_in) + return true; + + // crash process if the packet pointer of pointer is NULL. + if (!pckt_in) + { + UserProcessScheduler::The().GetCurrentProcess().Leak().Crash(); + return false; + } + + *pckt_in = new IPC_MSG(); + + if (*pckt_in) + { + auto endian = rtl_deduce_endianess((*pckt_in), ((Char*)(*pckt_in))[0]); + + (*pckt_in)->IpcHeaderMagic = kIPCHeaderMagic; + + (*pckt_in)->IpcEndianess = static_cast(endian); + (*pckt_in)->IpcPacketSize = sizeof(IPC_MSG); + + (*pckt_in)->IpcTo.UserProcessID = 0; + (*pckt_in)->IpcTo.UserProcessTeam = 0; + + (*pckt_in)->IpcFrom.UserProcessID = Kernel::UserProcessScheduler::The().GetCurrentProcess().Leak().ProcessId; + (*pckt_in)->IpcFrom.UserProcessTeam = Kernel::UserProcessScheduler::The().CurrentTeam().mTeamId; + + return Yes; + } + + return No; + } +} // namespace Kernel -- cgit v1.2.3