diff options
| author | Amlal El Mahrouss <amlal.elmahrouss@icloud.com> | 2024-01-06 09:14:11 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal.elmahrouss@icloud.com> | 2024-01-06 09:14:11 +0100 |
| commit | 5339d016c07bf717ee388f4feb73544087324af0 (patch) | |
| tree | 94be6f67ed626091f24aee24ec3b3be03d01e4e7 /Source/Network/IP.cpp | |
git: port from mercurial repo.
Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'Source/Network/IP.cpp')
| -rw-r--r-- | Source/Network/IP.cpp | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/Source/Network/IP.cpp b/Source/Network/IP.cpp new file mode 100644 index 00000000..482b5b7b --- /dev/null +++ b/Source/Network/IP.cpp @@ -0,0 +1,122 @@ +/* + * ======================================================== + * + * NewKit + * Copyright 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 |
