master
h 91 lines 2.4 KB
Raw
1 // Copyright (C) Microsoft Corporation. All rights reserved.
2 #pragma once
3
4 #include <string>
5 #include <array>
6 #include <arpa/inet.h>
7
8 using MacAddress = std::array<std::uint8_t, 6>;
9
10 constexpr unsigned int Ipv6MaxPrefixLen = 128;
11 constexpr unsigned int Ipv4MaxPrefixLen = 32;
12
13 #define MAX_PREFIX_LEN(AddressFamily) (((AddressFamily) == AF_INET) ? Ipv4MaxPrefixLen : Ipv6MaxPrefixLen)
14
15 enum class IpPrefixOrigin
16 {
17 Other = 0,
18 Manual,
19 WellKnown,
20 Dhcp,
21 RouterAdvertisement,
22 };
23
24 enum class IpSuffixOrigin
25 {
26 Other = 0,
27 Manual,
28 WellKnown,
29 Dhcp,
30 LinkLayerAddress,
31 Random,
32 };
33
34 class Address
35 {
36 public:
37 Address(short family, size_t prefixLength, const std::string& address);
38 Address(short family, size_t prefixLength, const std::string& address, IpPrefixOrigin prefixOrigin, IpSuffixOrigin suffixOrigin, int preferredLifetime);
39
40 bool operator==(const Address& other) const;
41 bool operator!=(const Address& other) const;
42
43 void ConvertToBytes(void* ptr) const;
44
45 void SetIsPrefixRouteAutogenerationDisabled(bool disable) noexcept
46 {
47 m_disableAutogeneratedPrefixRoute = disable;
48 }
49
50 template <typename T>
51 T AsBytes() const
52 {
53 T output;
54 ConvertToBytes(&output);
55
56 return output;
57 }
58
59 template <typename T>
60 static Address FromBytes(int family, int prefixLength, const T& address)
61 {
62 return FromBytes(family, prefixLength, static_cast<const void*>(&address));
63 }
64
65 static Address FromBytes(int family, int prefixLength, const void* ptr);
66
67 static Address FromPrefixString(int family, const std::string& address);
68
69 static Address FromBinary(int family, int prefixLength, const void* data);
70
71 short Family() const noexcept;
72 size_t PrefixLength() const noexcept;
73 const std::string Addr() const noexcept;
74 int Flags() const noexcept;
75 int Scope() const noexcept;
76 int PreferredLifetime() const noexcept;
77 bool IsIpv4() const noexcept;
78 bool IsLinkLocal() const;
79 bool IsPrefixRouteAutogenerationDisabled() const noexcept;
80
81 private:
82 short m_family = 0;
83 uint32_t m_prefixLength = 0;
84 std::string m_address = "";
85 IpPrefixOrigin m_prefixOrigin = IpPrefixOrigin::Other;
86 IpSuffixOrigin m_suffixOrigin = IpSuffixOrigin::Other;
87 uint32_t m_preferredLifetime = 0;
88 bool m_disableAutogeneratedPrefixRoute = false;
89 };
90
91 std::ostream& operator<<(std::ostream& out, const Address& address);