libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
ipv6address.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_CORE
13#include <cstdint>
14#include <cstring>
15#include <promeki/datatype.h>
16#include <promeki/namespace.h>
17#include <promeki/string.h>
18#include <promeki/result.h>
19#include <promeki/array.h>
20#include <promeki/platform.h>
21#include <promeki/list.h>
22
23#if defined(PROMEKI_PLATFORM_WINDOWS)
24#include <winsock2.h>
25#include <ws2tcpip.h>
26#elif !defined(PROMEKI_PLATFORM_EMSCRIPTEN)
27#include <netinet/in.h>
28#endif
29
30PROMEKI_NAMESPACE_BEGIN
31
32class Ipv4Address;
33class Ipv6Address;
34class MacAddress;
35class TextStream;
36class DataStream;
37
61class Ipv6Address {
62 public:
63 PROMEKI_DATATYPE(Ipv6Address, DataTypeIpv6Address, 1)
64
65
66 Error writeToStream(DataStream &s) const;
68 template <uint32_t V> static Result<Ipv6Address> readFromStream(DataStream &s);
69
71 using DataFormat = Array<uint8_t, 16>;
72
74 using List = ::promeki::List<Ipv6Address>;
75
87 static Result<Ipv6Address> fromString(const String &str);
88
90 static Ipv6Address any() { return Ipv6Address(); }
91
93 static Ipv6Address loopback();
94
96 Ipv6Address() : _addr{}, _scopeId(0) {}
97
102 explicit Ipv6Address(const DataFormat &bytes) : _addr(bytes), _scopeId(0) {}
103
108 explicit Ipv6Address(const uint8_t *bytes) : _scopeId(0) {
109 for (size_t i = 0; i < 16; ++i) _addr[i] = bytes[i];
110 }
111
113 bool isNull() const { return _addr.isZero() && _scopeId == 0; }
114
116 bool isLoopback() const;
117
119 bool isMulticast() const { return _addr[0] == 0xFF; }
120
122 bool isLinkLocal() const { return _addr[0] == 0xFE && (_addr[1] & 0xC0) == 0x80; }
123
125 bool isV4Mapped() const;
126
128 bool isSiteLocal() const { return _addr[0] == 0xFE && (_addr[1] & 0xC0) == 0xC0; }
129
134 uint32_t scopeId() const { return _scopeId; }
135
140 void setScopeId(uint32_t id) { _scopeId = id; }
141
146 const DataFormat &data() const { return _addr; }
147
152 const uint8_t *raw() const { return _addr.data(); }
153
161 String toString() const;
162
170 Ipv4Address toIpv4() const;
171
181 MacAddress multicastMac() const;
182
183#if !defined(PROMEKI_PLATFORM_EMSCRIPTEN)
190 static Result<Ipv6Address> fromSockAddr(const struct sockaddr_in6 *sa);
191
202 Error toSockAddr(struct sockaddr_in6 *sa) const;
203#endif
204
206 bool operator==(const Ipv6Address &other) const {
207 return _addr == other._addr && _scopeId == other._scopeId;
208 }
210 bool operator!=(const Ipv6Address &other) const { return !(*this == other); }
212 bool operator<(const Ipv6Address &other) const {
213 int cmp = std::memcmp(_addr.data(), other._addr.data(), 16);
214 if (cmp != 0) return cmp < 0;
215 return _scopeId < other._scopeId;
216 }
217
218 private:
219 DataFormat _addr;
220 uint32_t _scopeId;
221};
222
224TextStream &operator<<(TextStream &stream, const Ipv6Address &addr);
225
226PROMEKI_NAMESPACE_END
227
228PROMEKI_FORMAT_VIA_TOSTRING(promeki::Ipv6Address);
229
230#endif // PROMEKI_ENABLE_CORE