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/datatype.h>
16#include <promeki/namespace.h>
17#include <promeki/string.h>
18#include <promeki/result.h>
19#include <promeki/platform.h>
20#include <promeki/list.h>
21
22#if defined(PROMEKI_PLATFORM_WINDOWS)
23#include <winsock2.h>
24#include <ws2tcpip.h>
25#elif !defined(PROMEKI_PLATFORM_EMSCRIPTEN)
26#include <netinet/in.h>
27#endif
28
29PROMEKI_NAMESPACE_BEGIN
30
31class Ipv4Address;
32class Ipv6Address;
33class MacAddress;
34class TextStream;
35
59class DataStream;
60
61class Ipv4Address {
62 public:
63 PROMEKI_DATATYPE(Ipv4Address, DataTypeIpv4Address, 1)
64
65
66 Error writeToStream(DataStream &s) const;
68 template <uint32_t V> static Result<Ipv4Address> readFromStream(DataStream &s);
69
71 using List = ::promeki::List<Ipv4Address>;
72
79 static Result<Ipv4Address> fromString(const String &str);
80
86 static Ipv4Address fromUint32(uint32_t networkOrder) { return Ipv4Address(networkOrder); }
87
89 static Ipv4Address any() { return Ipv4Address(); }
90
92 static Ipv4Address loopback() { return Ipv4Address(127, 0, 0, 1); }
93
95 static Ipv4Address broadcast() { return Ipv4Address(255, 255, 255, 255); }
96
98 Ipv4Address() : _addr(0) {}
99
104 explicit Ipv4Address(uint32_t networkOrder) : _addr(networkOrder) {}
105
113 Ipv4Address(uint8_t a, uint8_t b, uint8_t c, uint8_t d)
114 : _addr(static_cast<uint32_t>(a) << 24 | static_cast<uint32_t>(b) << 16 |
115 static_cast<uint32_t>(c) << 8 | static_cast<uint32_t>(d)) {}
116
118 bool isNull() const { return _addr == 0; }
119
121 bool isLoopback() const { return octet(0) == 127; }
122
124 bool isMulticast() const { return (octet(0) & 0xF0) == 0xE0; }
125
127 bool isLinkLocal() const { return octet(0) == 169 && octet(1) == 254; }
128
134 bool isPrivate() const {
135 uint8_t a = octet(0);
136 uint8_t b = octet(1);
137 if (a == 10) return true;
138 if (a == 172 && (b & 0xF0) == 16) return true;
139 if (a == 192 && b == 168) return true;
140 return false;
141 }
142
144 bool isBroadcast() const { return _addr == 0xFFFFFFFF; }
145
152 bool isInSubnet(Ipv4Address network, Ipv4Address mask) const {
153 return (_addr & mask._addr) == (network._addr & mask._addr);
154 }
155
162 bool isInSubnet(Ipv4Address network, int prefixLen) const;
163
168 uint32_t toUint32() const { return _addr; }
169
175 uint8_t octet(int index) const {
176 if (index < 0 || index > 3) return 0;
177 return static_cast<uint8_t>((_addr >> (24 - index * 8)) & 0xFF);
178 }
179
184 String toString() const;
185
190 Ipv6Address toIpv6Mapped() const;
191
206 MacAddress multicastMac() const;
207
208#if !defined(PROMEKI_PLATFORM_EMSCRIPTEN)
215 static Result<Ipv4Address> fromSockAddr(const struct sockaddr_in *sa);
216
226 Error toSockAddr(struct sockaddr_in *sa) const;
227#endif
228
230 bool operator==(const Ipv4Address &other) const { return _addr == other._addr; }
232 bool operator!=(const Ipv4Address &other) const { return _addr != other._addr; }
234 bool operator<(const Ipv4Address &other) const { return _addr < other._addr; }
235
236 private:
237 uint32_t _addr;
238};
239
241TextStream &operator<<(TextStream &stream, const Ipv4Address &addr);
242
243PROMEKI_NAMESPACE_END
244
245PROMEKI_FORMAT_VIA_TOSTRING(promeki::Ipv4Address);
246
247#endif // PROMEKI_ENABLE_CORE