libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
ipv6address.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <cstdint>
11#include <cstring>
13#include <promeki/core/string.h>
14#include <promeki/core/result.h>
15#include <promeki/core/array.h>
17
18#if defined(PROMEKI_PLATFORM_WINDOWS)
19# include <winsock2.h>
20# include <ws2tcpip.h>
21#elif !defined(PROMEKI_PLATFORM_EMSCRIPTEN)
22# include <netinet/in.h>
23#endif
24
26
27class Ipv4Address;
28class MacAddress;
29class TextStream;
30
53 public:
56
69
71 static Ipv6Address any() { return Ipv6Address(); }
72
75
77 Ipv6Address() : _addr{}, _scopeId(0) { }
78
83 explicit Ipv6Address(const DataFormat &bytes) : _addr(bytes), _scopeId(0) { }
84
89 explicit Ipv6Address(const uint8_t *bytes) : _scopeId(0) {
90 for(size_t i = 0; i < 16; ++i) _addr[i] = bytes[i];
91 }
92
94 bool isNull() const { return _addr.isZero() && _scopeId == 0; }
95
97 bool isLoopback() const;
98
100 bool isMulticast() const { return _addr[0] == 0xFF; }
101
103 bool isLinkLocal() const { return _addr[0] == 0xFE && (_addr[1] & 0xC0) == 0x80; }
104
106 bool isV4Mapped() const;
107
109 bool isSiteLocal() const { return _addr[0] == 0xFE && (_addr[1] & 0xC0) == 0xC0; }
110
115 uint32_t scopeId() const { return _scopeId; }
116
121 void setScopeId(uint32_t id) { _scopeId = id; }
122
127 const DataFormat &data() const { return _addr; }
128
133 const uint8_t *raw() const { return _addr.data(); }
134
143
152
163
164#if !defined(PROMEKI_PLATFORM_EMSCRIPTEN)
172
184#endif
185
187 bool operator==(const Ipv6Address &other) const {
188 return _addr == other._addr && _scopeId == other._scopeId;
189 }
191 bool operator!=(const Ipv6Address &other) const { return !(*this == other); }
193 bool operator<(const Ipv6Address &other) const {
194 int cmp = std::memcmp(_addr.data(), other._addr.data(), 16);
195 if(cmp != 0) return cmp < 0;
196 return _scopeId < other._scopeId;
197 }
198
199 private:
200 DataFormat _addr;
201 uint32_t _scopeId;
202};
203
206
bool isZero() const
Returns true if all elements are zero.
Definition array.h:297
T * data()
Returns a mutable pointer to the underlying contiguous storage.
Definition array.h:281
Lightweight error code wrapper for the promeki library.
Definition error.h:39
IPv4 network address.
Definition ipv4address.h:51
IPv6 network address.
Definition ipv6address.h:52
uint32_t scopeId() const
Returns the scope ID.
Definition ipv6address.h:115
Ipv4Address toIpv4() const
Extracts the IPv4 address from an IPv4-mapped IPv6 address.
const uint8_t * raw() const
Returns a pointer to the raw byte data.
Definition ipv6address.h:133
static Ipv6Address any()
Returns the any address (::).
Definition ipv6address.h:71
Ipv6Address(const uint8_t *bytes)
Constructs from a raw byte pointer (copies 16 bytes).
Definition ipv6address.h:89
bool operator!=(const Ipv6Address &other) const
Returns true if the addresses are not equal.
Definition ipv6address.h:191
Ipv6Address(const DataFormat &bytes)
Constructs from raw 16-byte data.
Definition ipv6address.h:83
static Result< Ipv6Address > fromString(const String &str)
Parses an IPv6 address from colon-hex notation.
void setScopeId(uint32_t id)
Sets the scope ID.
Definition ipv6address.h:121
bool isSiteLocal() const
Returns true if this is a site-local address (fec0::/10, deprecated).
Definition ipv6address.h:109
bool operator==(const Ipv6Address &other) const
Returns true if both addresses are equal (includes scope ID).
Definition ipv6address.h:187
const DataFormat & data() const
Returns a const reference to the raw 16-byte data.
Definition ipv6address.h:127
String toString() const
Returns a canonical string representation.
bool isLinkLocal() const
Returns true if this is a link-local address (fe80::/10).
Definition ipv6address.h:103
Array< uint8_t, 16 > DataFormat
Raw 16-byte storage format for an IPv6 address.
Definition ipv6address.h:55
bool isNull() const
Returns true if all bytes are zero and scope ID is zero.
Definition ipv6address.h:94
Error toSockAddr(struct sockaddr_in6 *sa) const
Fills a sockaddr_in6 with this address.
static Ipv6Address loopback()
Returns the loopback address (::1).
MacAddress multicastMac() const
Returns the Ethernet multicast MAC address for this IPv6 multicast address.
static Result< Ipv6Address > fromSockAddr(const struct sockaddr_in6 *sa)
Constructs from a sockaddr_in6 structure.
Ipv6Address()
Default constructor. Creates a null (all-zero) address.
Definition ipv6address.h:77
bool isV4Mapped() const
Returns true if this is an IPv4-mapped address (::%ffff:0:0/96).
bool isMulticast() const
Returns true if this is a multicast address (ff00::/8).
Definition ipv6address.h:100
bool isLoopback() const
Returns true if this is the loopback address (::1).
bool operator<(const Ipv6Address &other) const
Less-than comparison for ordering (lexicographic, then scope).
Definition ipv6address.h:193
Dynamic array container wrapping std::vector.
Definition list.h:40
T * data() noexcept
Returns a pointer to the underlying contiguous storage.
Definition list.h:286
IEEE 802 MAC (Ethernet hardware) address.
Definition macaddress.h:44
Encoding-aware string class with copy-on-write semantics.
Definition string.h:35
Formatted text I/O with encoding awareness.
Definition textstream.h:54
TextStream & operator<<(TextStream &stream, const Ipv6Address &addr)
Writes a colon-hex IPv6 address to the stream.
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19