libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
ipv4address.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/namespace.h>
16#include <promeki/string.h>
17#include <promeki/result.h>
18#include <promeki/platform.h>
19#include <promeki/list.h>
20
21#if defined(PROMEKI_PLATFORM_WINDOWS)
22#include <winsock2.h>
23#include <ws2tcpip.h>
24#elif !defined(PROMEKI_PLATFORM_EMSCRIPTEN)
25#include <netinet/in.h>
26#endif
27
28PROMEKI_NAMESPACE_BEGIN
29
30class Ipv4Address;
31class Ipv6Address;
32class MacAddress;
33class TextStream;
34
58class Ipv4Address {
59 public:
61 using List = ::promeki::List<Ipv4Address>;
62
69 static Result<Ipv4Address> fromString(const String &str);
70
76 static Ipv4Address fromUint32(uint32_t networkOrder) { return Ipv4Address(networkOrder); }
77
79 static Ipv4Address any() { return Ipv4Address(); }
80
82 static Ipv4Address loopback() { return Ipv4Address(127, 0, 0, 1); }
83
85 static Ipv4Address broadcast() { return Ipv4Address(255, 255, 255, 255); }
86
88 Ipv4Address() : _addr(0) {}
89
94 explicit Ipv4Address(uint32_t networkOrder) : _addr(networkOrder) {}
95
103 Ipv4Address(uint8_t a, uint8_t b, uint8_t c, uint8_t d)
104 : _addr(static_cast<uint32_t>(a) << 24 | static_cast<uint32_t>(b) << 16 |
105 static_cast<uint32_t>(c) << 8 | static_cast<uint32_t>(d)) {}
106
108 bool isNull() const { return _addr == 0; }
109
111 bool isLoopback() const { return octet(0) == 127; }
112
114 bool isMulticast() const { return (octet(0) & 0xF0) == 0xE0; }
115
117 bool isLinkLocal() const { return octet(0) == 169 && octet(1) == 254; }
118
124 bool isPrivate() const {
125 uint8_t a = octet(0);
126 uint8_t b = octet(1);
127 if (a == 10) return true;
128 if (a == 172 && (b & 0xF0) == 16) return true;
129 if (a == 192 && b == 168) return true;
130 return false;
131 }
132
134 bool isBroadcast() const { return _addr == 0xFFFFFFFF; }
135
142 bool isInSubnet(Ipv4Address network, Ipv4Address mask) const {
143 return (_addr & mask._addr) == (network._addr & mask._addr);
144 }
145
152 bool isInSubnet(Ipv4Address network, int prefixLen) const;
153
158 uint32_t toUint32() const { return _addr; }
159
165 uint8_t octet(int index) const {
166 if (index < 0 || index > 3) return 0;
167 return static_cast<uint8_t>((_addr >> (24 - index * 8)) & 0xFF);
168 }
169
174 String toString() const;
175
180 Ipv6Address toIpv6Mapped() const;
181
196 MacAddress multicastMac() const;
197
198#if !defined(PROMEKI_PLATFORM_EMSCRIPTEN)
205 static Result<Ipv4Address> fromSockAddr(const struct sockaddr_in *sa);
206
216 Error toSockAddr(struct sockaddr_in *sa) const;
217#endif
218
220 bool operator==(const Ipv4Address &other) const { return _addr == other._addr; }
222 bool operator!=(const Ipv4Address &other) const { return _addr != other._addr; }
224 bool operator<(const Ipv4Address &other) const { return _addr < other._addr; }
225
226 private:
227 uint32_t _addr;
228};
229
231TextStream &operator<<(TextStream &stream, const Ipv4Address &addr);
232
233PROMEKI_NAMESPACE_END
234
235PROMEKI_FORMAT_VIA_TOSTRING(promeki::Ipv4Address);
236
237#endif // PROMEKI_ENABLE_CORE