diff options
| author | Amlal El Mahrouss <amlal.elmahrouss@icloud.com> | 2024-01-26 22:26:48 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal.elmahrouss@icloud.com> | 2024-01-26 22:27:09 +0100 |
| commit | eba8b7ddd0a455d9e49f32dcae712c5612c0093c (patch) | |
| tree | 749a3d34546d055507a920bce4ab10e8a9945719 /Private/Source/Network/IP.cpp | |
| parent | dd192787a70a973f2474720aea49af3f6ddabb7a (diff) | |
Kernel: Major repository refactor.
Rework the repo into Private and Public modules.
Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'Private/Source/Network/IP.cpp')
| -rw-r--r-- | Private/Source/Network/IP.cpp | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/Private/Source/Network/IP.cpp b/Private/Source/Network/IP.cpp new file mode 100644 index 00000000..4a0b3011 --- /dev/null +++ b/Private/Source/Network/IP.cpp @@ -0,0 +1,122 @@ +/* + * ======================================================== + * + * NewKit + * Copyright 2024 Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#include <NetworkKit/IP.hpp> +#include <NewKit/Utils.hpp> + +namespace hCore +{ + RawIPAddress::RawIPAddress(char bytes[4]) + { + rt_copy_memory(bytes, m_Addr, 4); + } + + bool RawIPAddress::operator==(const RawIPAddress& ipv4) + { + for (Size index = 0; index < 4; ++index) + { + if (ipv4.m_Addr[index] != m_Addr[index]) + return false; + } + + return true; + } + + bool RawIPAddress::operator!=(const RawIPAddress& ipv4) + { + for (Size index = 0; index < 4; ++index) + { + if (ipv4.m_Addr[index] == m_Addr[index]) + return false; + } + + return true; + } + + char& RawIPAddress::operator[](const Size& index) + { + kcout << "[RawIPAddress::operator[]] Fetching Index...\r\n"; + + if (index > 4) + panic(RUNTIME_CHECK_EXPRESSION); + + return m_Addr[index]; + } + + RawIPAddress6::RawIPAddress6(char bytes[8]) + { + rt_copy_memory(bytes, m_Addr, 8); + } + + char &RawIPAddress6::operator[](const Size &index) + { + kcout << "[RawIPAddress6::operator[]] Fetching Index...\r\n"; + + if (index > 8) + panic(RUNTIME_CHECK_EXPRESSION); + + return m_Addr[index]; + } + + bool RawIPAddress6::operator==(const RawIPAddress6& ipv6) + { + for (SizeT index = 0; index < 8; ++index) + { + if (ipv6.m_Addr[index] != m_Addr[index]) + return false; + } + + return true; + } + + bool RawIPAddress6::operator!=(const RawIPAddress6& ipv6) + { + for (SizeT index = 0; index < 8; ++index) + { + if (ipv6.m_Addr[index] == m_Addr[index]) + return false; + } + + return true; + } + + ErrorOr<StringView> IPHelper::ToStringView(Ref<RawIPAddress6> ipv6) + { + auto str = StringBuilder::Construct(ipv6.Leak().Address()); + return str; + } + + ErrorOr<StringView> IPHelper::ToStringView(Ref<RawIPAddress> ipv4) + { + auto str = StringBuilder::Construct(ipv4.Leak().Address()); + return str; + } + + bool IPHelper::IpCheckV4(const char *ip) + { + int cnter = 0; + + for (Size base = 0; base < string_length(ip); ++base) + { + if (ip[base] == '.') + { + cnter = 0; + } + else + { + if (cnter == 3) + return false; + + ++cnter; + } + } + + return true; + } +} // namespace NewKit |
