11#include <promeki/config.h>
12#if PROMEKI_ENABLE_CORE
22#if defined(PROMEKI_PLATFORM_WINDOWS)
25#elif !defined(PROMEKI_PLATFORM_EMSCRIPTEN)
26#include <netinet/in.h>
29PROMEKI_NAMESPACE_BEGIN
63 PROMEKI_DATATYPE(Ipv4Address, DataTypeIpv4Address, 1)
66 Error writeToStream(DataStream &s) const;
68 template <uint32_t V> static Result<Ipv4Address> readFromStream(DataStream &s);
71 using List = ::promeki::List<Ipv4Address>;
79 static Result<Ipv4Address> fromString(const String &str);
86 static Ipv4Address fromUint32(uint32_t networkOrder) {
return Ipv4Address(networkOrder); }
89 static Ipv4Address any() {
return Ipv4Address(); }
92 static Ipv4Address loopback() {
return Ipv4Address(127, 0, 0, 1); }
95 static Ipv4Address broadcast() {
return Ipv4Address(255, 255, 255, 255); }
98 Ipv4Address() : _addr(0) {}
104 explicit Ipv4Address(uint32_t networkOrder) : _addr(networkOrder) {}
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)) {}
118 bool isNull()
const {
return _addr == 0; }
121 bool isLoopback()
const {
return octet(0) == 127; }
124 bool isMulticast()
const {
return (octet(0) & 0xF0) == 0xE0; }
127 bool isLinkLocal()
const {
return octet(0) == 169 && octet(1) == 254; }
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;
144 bool isBroadcast()
const {
return _addr == 0xFFFFFFFF; }
152 bool isInSubnet(Ipv4Address network, Ipv4Address mask)
const {
153 return (_addr & mask._addr) == (network._addr & mask._addr);
162 bool isInSubnet(Ipv4Address network,
int prefixLen)
const;
168 uint32_t toUint32()
const {
return _addr; }
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);
184 String toString()
const;
190 Ipv6Address toIpv6Mapped()
const;
206 MacAddress multicastMac()
const;
208#if !defined(PROMEKI_PLATFORM_EMSCRIPTEN)
215 static Result<Ipv4Address> fromSockAddr(
const struct sockaddr_in *sa);
226 Error toSockAddr(
struct sockaddr_in *sa)
const;
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; }
241TextStream &operator<<(TextStream &stream,
const Ipv4Address &addr);
245PROMEKI_FORMAT_VIA_TOSTRING(promeki::Ipv4Address);