summaryrefslogtreecommitdiffhomepage
path: root/dev/zka/src/Network
diff options
context:
space:
mode:
authorAmlal <amlal@el-mahrouss-logic.com>2024-09-22 17:46:11 +0200
committerAmlal <amlal@el-mahrouss-logic.com>2024-09-22 17:46:11 +0200
commit8719b4570a2d10dd49a0d3a47e24f5c55bdda85e (patch)
treeba095740888f3768e08b2ea058b0ea6da2d0403d /dev/zka/src/Network
parent45944b3d2dab04b763fcc6d10164fe8069e60b08 (diff)
:boom: A big refactor on the filesystem structure.
Signed-off-by: Amlal <amlal@el-mahrouss-logic.com>
Diffstat (limited to 'dev/zka/src/Network')
-rw-r--r--dev/zka/src/Network/IP.cxx126
-rw-r--r--dev/zka/src/Network/IPC.cxx114
-rw-r--r--dev/zka/src/Network/NetworkDevice.cxx35
3 files changed, 275 insertions, 0 deletions
diff --git a/dev/zka/src/Network/IP.cxx b/dev/zka/src/Network/IP.cxx
new file mode 100644
index 00000000..77571bc7
--- /dev/null
+++ b/dev/zka/src/Network/IP.cxx
@@ -0,0 +1,126 @@
+/* -------------------------------------------
+
+ Copyright ZKA Technologies.
+
+------------------------------------------- */
+
+#include <NetworkKit/IP.hxx>
+#include <NewKit/Utils.hxx>
+
+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<StringView> IPFactory::ToStringView(Ref<RawIPAddress6>& ipv6)
+ {
+ auto str = StringBuilder::Construct(ipv6.Leak().Address());
+ return str;
+ }
+
+ ErrorOr<StringView> IPFactory::ToStringView(Ref<RawIPAddress>& 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/zka/src/Network/IPC.cxx b/dev/zka/src/Network/IPC.cxx
new file mode 100644
index 00000000..42b0fcb9
--- /dev/null
+++ b/dev/zka/src/Network/IPC.cxx
@@ -0,0 +1,114 @@
+/* -------------------------------------------
+
+ Copyright ZKA Technologies.
+
+------------------------------------------- */
+
+#include <NetworkKit/IPC.hxx>
+#include <KernelKit/LPC.hxx>
+#include <KernelKit/UserProcessScheduler.hxx>
+
+using namespace Kernel;
+
+/// @internal
+/// @brief The internal sanitize function.
+Bool ipc_int_sanitize_packet(IPC_MESSAGE_STRUCT* pckt)
+{
+ auto endian = DEDUCE_ENDIAN(pckt, ((Char*)pckt)[0]);
+
+ switch (endian)
+ {
+ case Endian::kEndianBig: {
+ if (pckt->IpcEndianess == eIPCEPLittleEndian)
+ goto ipc_check_failed;
+
+ break;
+ }
+ case Endian::kEndianLittle: {
+ if (pckt->IpcEndianess == eIPCEPBigEndian)
+ goto ipc_check_failed;
+
+ break;
+ }
+ case Endian::kEndianMixed: {
+ if (pckt->IpcEndianess == eIPCEPMixedEndian)
+ goto ipc_check_failed;
+
+ break;
+ }
+ default:
+ goto ipc_check_failed;
+ }
+
+ if (pckt->IpcFrom == pckt->IpcTo ||
+ pckt->IpcPacketSize > cXPCOMMsgSize)
+ {
+ goto ipc_check_failed;
+ }
+
+ return pckt->IpcPacketSize > 1 && pckt->IpcHeaderMagic == cXPCOMHeaderMagic;
+
+ipc_check_failed:
+ ErrLocal() = 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(IPC_MESSAGE_STRUCT* pckt)
+ {
+ if (!pckt ||
+ !ipc_int_sanitize_packet(pckt))
+ {
+ UserProcessScheduler::The().CurrentProcess().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_MESSAGE_STRUCT** pckt_in)
+ {
+ // 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().CurrentProcess().Leak().Crash();
+ return false;
+ }
+
+ *pckt_in = new IPC_MESSAGE_STRUCT();
+
+ MUST_PASS((*pckt_in));
+
+ if (*pckt_in)
+ {
+ auto endian = DEDUCE_ENDIAN((*pckt_in), ((Char*)(*pckt_in))[0]);
+
+ (*pckt_in)->IpcHeaderMagic = cXPCOMHeaderMagic;
+
+ (*pckt_in)->IpcEndianess = static_cast<UInt8>(endian);
+ (*pckt_in)->IpcPacketSize = sizeof(IPC_MESSAGE_STRUCT);
+
+ (*pckt_in)->IpcTo.UserProcessID = 0;
+ (*pckt_in)->IpcTo.UserProcessTeam = 0;
+
+ (*pckt_in)->IpcFrom.UserProcessID = Kernel::UserProcessScheduler::The().CurrentProcess().Leak().ProcessId;
+ (*pckt_in)->IpcFrom.UserProcessTeam = Kernel::UserProcessScheduler::The().CurrentTeam().mTeamId;
+
+ return true;
+ }
+
+ UserProcessScheduler::The().CurrentProcess().Leak().Crash();
+ return false;
+ }
+} // namespace Kernel
diff --git a/dev/zka/src/Network/NetworkDevice.cxx b/dev/zka/src/Network/NetworkDevice.cxx
new file mode 100644
index 00000000..afb3e468
--- /dev/null
+++ b/dev/zka/src/Network/NetworkDevice.cxx
@@ -0,0 +1,35 @@
+/* -------------------------------------------
+
+ Copyright ZKA Technologies.
+
+------------------------------------------- */
+
+#include <NetworkKit/NetworkDevice.hxx>
+#include <NewKit/Utils.hxx>
+
+namespace Kernel
+{
+ /// \brief Getter for fNetworkName.
+ const Char* NetworkDevice::Name() const
+ {
+ return this->fNetworkName;
+ }
+
+ /// \brief Setter for fNetworkName.
+ Boolean NetworkDevice::Name(const Char* strView)
+ {
+ if (strView == nullptr)
+ return false;
+
+ if (*strView == 0)
+ return false;
+
+ if (rt_string_len(strView) > cNetworkNameLen)
+ return false;
+
+ rt_copy_memory((VoidPtr)strView,
+ (VoidPtr)this->fNetworkName, rt_string_len(strView));
+
+ return true;
+ }
+} // namespace Kernel