11#include <promeki/config.h>
12#if PROMEKI_ENABLE_CORE
21#if defined(PROMEKI_PLATFORM_WINDOWS)
24#elif !defined(PROMEKI_PLATFORM_EMSCRIPTEN)
25#include <netinet/in.h>
28PROMEKI_NAMESPACE_BEGIN
61 using List = ::promeki::List<Ipv4Address>;
69 static Result<Ipv4Address> fromString(
const String &str);
76 static Ipv4Address fromUint32(uint32_t networkOrder) {
return Ipv4Address(networkOrder); }
79 static Ipv4Address any() {
return Ipv4Address(); }
82 static Ipv4Address loopback() {
return Ipv4Address(127, 0, 0, 1); }
85 static Ipv4Address broadcast() {
return Ipv4Address(255, 255, 255, 255); }
88 Ipv4Address() : _addr(0) {}
94 explicit Ipv4Address(uint32_t networkOrder) : _addr(networkOrder) {}
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)) {}
108 bool isNull()
const {
return _addr == 0; }
111 bool isLoopback()
const {
return octet(0) == 127; }
114 bool isMulticast()
const {
return (octet(0) & 0xF0) == 0xE0; }
117 bool isLinkLocal()
const {
return octet(0) == 169 && octet(1) == 254; }
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;
134 bool isBroadcast()
const {
return _addr == 0xFFFFFFFF; }
142 bool isInSubnet(Ipv4Address network, Ipv4Address mask)
const {
143 return (_addr & mask._addr) == (network._addr & mask._addr);
152 bool isInSubnet(Ipv4Address network,
int prefixLen)
const;
158 uint32_t toUint32()
const {
return _addr; }
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);
174 String toString()
const;
180 Ipv6Address toIpv6Mapped()
const;
196 MacAddress multicastMac()
const;
198#if !defined(PROMEKI_PLATFORM_EMSCRIPTEN)
205 static Result<Ipv4Address> fromSockAddr(
const struct sockaddr_in *sa);
216 Error toSockAddr(
struct sockaddr_in *sa)
const;
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; }
231TextStream &operator<<(TextStream &stream,
const Ipv4Address &addr);
235PROMEKI_FORMAT_VIA_TOSTRING(promeki::Ipv4Address);