From 5339d016c07bf717ee388f4feb73544087324af0 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sat, 6 Jan 2024 09:14:11 +0100 Subject: git: port from mercurial repo. Signed-off-by: Amlal El Mahrouss --- Source/Network/IP.cpp | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 Source/Network/IP.cpp (limited to 'Source/Network/IP.cpp') 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 +#include + +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 IPHelper::ToStringView(Ref ipv6) + { + auto str = StringBuilder::Construct(ipv6.Leak().Address()); + return str; + } + + ErrorOr IPHelper::ToStringView(Ref 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 -- cgit v1.2.3