summaryrefslogtreecommitdiffhomepage
path: root/NetworkKit/IP.hpp
blob: 870ebbc39224ea0ec488c638549999fcbce81048 (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
/*
 *	========================================================
 *
 *	hCore
 * 	Copyright XPX Corp, all rights reserved.
 *
 * 	========================================================
 */

#pragma once

#include <NewKit/Defines.hpp>
#include <KernelKit/DebugOutput.hpp>
#include <NewKit/String.hpp>
#include <NewKit/Ref.hpp>

namespace hCore
{
class RawIPAddress6;
class RawIPAddress;
class NetworkManager;

class RawIPAddress final
{
  private:
    RawIPAddress(char bytes[4]);
    ~RawIPAddress() = default;

    RawIPAddress &operator=(const RawIPAddress &) = delete;
    RawIPAddress(const RawIPAddress &) = default;

  public:
    char *Address()
    {
        return m_Addr;
    }

    char &operator[](const Size &index);

    bool operator==(const RawIPAddress &ipv6);
    bool operator!=(const RawIPAddress &ipv6);

  private:
    char m_Addr[4];

    friend NetworkManager; // it is the one creating these addresses, thus this
                           // is why the constructors are private.
};

class RawIPAddress6 final
{
  private:
    RawIPAddress6(char Bytes[8]);
    ~RawIPAddress6() = default;

    RawIPAddress6 &operator=(const RawIPAddress6 &) = delete;
    RawIPAddress6(const RawIPAddress6 &) = default;

  public:
    char *Address()
    {
        return m_Addr;
    }

    char &operator[](const Size &index);

    bool operator==(const RawIPAddress6 &ipv6);
    bool operator!=(const RawIPAddress6 &ipv6);

  private:
    char m_Addr[8];

    friend NetworkManager;
};

class IPHelper
{
  public:
    static ErrorOr<StringView> ToStringView(Ref<RawIPAddress6> ipv6);
    static ErrorOr<StringView> ToStringView(Ref<RawIPAddress> ipv4);
    static bool IpCheckV4(const char *ip);

};
} // namespace hCore