diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-06-07 09:29:59 +0200 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-06-07 09:30:33 +0200 |
| commit | 142e7465b98b01eef5c592b054f1f80b8eb47779 (patch) | |
| tree | cb413d5ea2e3cc7ad3b3ac81e05b5f2982fa838c /dev/kernel | |
| parent | dbf1190718c1a5adb4b2dc56e4d6da45429bb65e (diff) | |
fix/feat: NetworkKit: Better IPAddr.cc implementation for both IP6 and IP4.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'dev/kernel')
| -rw-r--r-- | dev/kernel/NetworkKit/IP.h | 16 | ||||
| -rw-r--r-- | dev/kernel/src/Network/IPAddr.cc | 34 |
2 files changed, 28 insertions, 22 deletions
diff --git a/dev/kernel/NetworkKit/IP.h b/dev/kernel/NetworkKit/IP.h index 0b872352..0468d579 100644 --- a/dev/kernel/NetworkKit/IP.h +++ b/dev/kernel/NetworkKit/IP.h @@ -18,22 +18,22 @@ class IPFactory; class RawIPAddress final { private: - explicit RawIPAddress(char bytes[4]); + explicit RawIPAddress(UInt8 bytes[4]); ~RawIPAddress() = default; RawIPAddress& operator=(const RawIPAddress&) = delete; RawIPAddress(const RawIPAddress&) = default; public: - Char* Address(); + UInt8* Address(); - Char& operator[](const Size& index); + UInt8& operator[](const Size& index); BOOL operator==(const RawIPAddress& ipv6); BOOL operator!=(const RawIPAddress& ipv6); private: - Char fAddr[4]; + UInt8 fAddr[4]; friend IPFactory; // it is the one creating these addresses, thus this // is why the constructors are private. @@ -44,22 +44,22 @@ class RawIPAddress final { */ class RawIPAddress6 final { private: - explicit RawIPAddress6(char Bytes[8]); + explicit RawIPAddress6(UInt8 Bytes[16]); ~RawIPAddress6() = default; RawIPAddress6& operator=(const RawIPAddress6&) = delete; RawIPAddress6(const RawIPAddress6&) = default; public: - char* Address() { return fAddr; } + UInt8* Address() { return fAddr; } - char& operator[](const Size& index); + UInt8& operator[](const Size& index); bool operator==(const RawIPAddress6& ipv6); bool operator!=(const RawIPAddress6& ipv6); private: - char fAddr[8]; + UInt8 fAddr[16]; friend IPFactory; }; diff --git a/dev/kernel/src/Network/IPAddr.cc b/dev/kernel/src/Network/IPAddr.cc index beb69470..b341af8f 100644 --- a/dev/kernel/src/Network/IPAddr.cc +++ b/dev/kernel/src/Network/IPAddr.cc @@ -8,11 +8,11 @@ #include <NetworkKit/IP.h> namespace Kernel { -Char* RawIPAddress::Address() { +UInt8* RawIPAddress::Address() { return fAddr; } -RawIPAddress::RawIPAddress(char bytes[4]) { +RawIPAddress::RawIPAddress(UInt8 bytes[4]) { rt_copy_memory(bytes, fAddr, 4); } @@ -32,30 +32,30 @@ BOOL RawIPAddress::operator!=(const RawIPAddress& ipv4) { return true; } -Char& RawIPAddress::operator[](const Size& index) { +UInt8& RawIPAddress::operator[](const Size& index) { kout << "[RawIPAddress::operator[]] Fetching Index...\r"; - static char IP_PLACEHOLDER = '0'; - if (index > 4) return IP_PLACEHOLDER; + static UInt8 IP_PLACEHOLDER = '0'; + if (index >= 4) return IP_PLACEHOLDER; return fAddr[index]; } -RawIPAddress6::RawIPAddress6(char bytes[8]) { - rt_copy_memory(bytes, fAddr, 8); +RawIPAddress6::RawIPAddress6(UInt8 bytes[16]) { + rt_copy_memory(bytes, fAddr, 16); } -char& RawIPAddress6::operator[](const Size& index) { +UInt8& RawIPAddress6::operator[](const Size& index) { kout << "[RawIPAddress6::operator[]] Fetching Index...\r"; - static char IP_PLACEHOLDER = '0'; - if (index > 8) return IP_PLACEHOLDER; + static UInt8 IP_PLACEHOLDER = '0'; + if (index >= 16) return IP_PLACEHOLDER; return fAddr[index]; } bool RawIPAddress6::operator==(const RawIPAddress6& ipv6) { - for (SizeT index = 0; index < 8; ++index) { + for (SizeT index = 0; index < 16; ++index) { if (ipv6.fAddr[index] != fAddr[index]) return false; } @@ -63,23 +63,28 @@ bool RawIPAddress6::operator==(const RawIPAddress6& ipv6) { } bool RawIPAddress6::operator!=(const RawIPAddress6& ipv6) { - for (SizeT index = 0; index < 8; ++index) { + for (SizeT index = 0; index < 16; ++index) { if (ipv6.fAddr[index] == fAddr[index]) return false; } return true; } +/// @todo ErrorOr<KString> IPFactory::ToKString(Ref<RawIPAddress6>& ipv6) { - auto str = KStringBuilder::Construct(ipv6.Leak().Address()); + NE_UNUSED(ipv6); + auto str = KStringBuilder::Construct(0); return str; } +/// @todo ErrorOr<KString> IPFactory::ToKString(Ref<RawIPAddress>& ipv4) { - auto str = KStringBuilder::Construct(ipv4.Leak().Address()); + NE_UNUSED(ipv4); + auto str = KStringBuilder::Construct(0); return str; } +/// @note Doesn't catch IPs such as 256.999.0.1, UNSAFE! bool IPFactory::IpCheckVersion4(const Char* ip) { if (!ip) return NO; @@ -89,6 +94,7 @@ bool IPFactory::IpCheckVersion4(const Char* ip) { if (ip[base] == '.') { cnter = 0; } else { + if (!rt_is_alnum(ip[base])) return false; if (cnter == 3) return false; ++cnter; |
