summaryrefslogtreecommitdiffhomepage
path: root/dev/kernel/src/Network/IPAddress.cc
blob: 7834cfa8edad79312eaaf17ea8e532a59a917963 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* -------------------------------------------

  Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license.

------------------------------------------- */

#include <NeKit/Utils.h>
#include <NetworkKit/IP.h>

namespace Kernel {
UInt8* RawIPAddress::Address() {
  return fAddr;
}

RawIPAddress::RawIPAddress(UInt8 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;
}

UInt8& RawIPAddress::operator[](const Size& index) {
  kout << "[RawIPAddress::operator[]] Fetching Index...\r";

  static UInt8 IP_PLACEHOLDER = '0';
  if (index >= 4) return IP_PLACEHOLDER;

  return fAddr[index];
}

RawIPAddress6::RawIPAddress6(UInt8 bytes[16]) {
  rt_copy_memory(bytes, fAddr, 16);
}

UInt8& RawIPAddress6::operator[](const Size& index) {
  kout << "[RawIPAddress6::operator[]] Fetching Index...\r";

  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 < 16; ++index) {
    if (ipv6.fAddr[index] != fAddr[index]) return false;
  }

  return true;
}

bool RawIPAddress6::operator!=(const RawIPAddress6& ipv6) {
  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) {
  NE_UNUSED(ipv6);
  auto str = KStringBuilder::Construct("");
  return str;
}

/// @todo
ErrorOr<KString> IPFactory::ToKString(Ref<RawIPAddress>& ipv4) {
  NE_UNUSED(ipv4);
  auto str = KStringBuilder::Construct("");
  return str;
}

/// @note Doesn't catch IPs such as 256.999.0.1, UNSAFE!
bool IPFactory::IpCheckVersion4(const Char* ip) {
  if (!ip) return NO;

  Int32 cnter     = 0;
  Int32 dot_cnter = 0;

  constexpr const auto kIP4DotCharacter = '.';

  for (SizeT base = 0; base < rt_string_len(ip); ++base) {
    if (ip[base] == kIP4DotCharacter) {
      cnter = 0;
      ++dot_cnter;
    } else {
      if (ip[base] > '5' || ip[base] < '0') return NO;
      if (!rt_is_alnum(ip[base])) return NO;
      if (cnter == 3) return NO;

      ++cnter;
    }
  }

  if (dot_cnter != 3) return NO;

  return YES;
}
}  // namespace Kernel