#pragma once #include "common.h" #include #include #include // // There are no less than 3 different types for IP addresses used // by different APIs. This class attempts to unify them to mask the // differences. // class IP4 { private: union { uint8_t b[4]; uint32_t l; }; public: constexpr IP4() : l{0} { } constexpr IP4(nullptr_t) : l{0} { } constexpr IP4(int ll) : l{(uint32_t)ll} { } constexpr IP4(uint32_t ll) : l{ll} { } constexpr IP4(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3) : b{b0,b1,b2,b3} { } IP4(const IPAddress & ip) : l{ip} { } constexpr IP4(const ip_addr_t & ip) : l{ip.type == IPADDR_TYPE_V4 ? ip.u_addr.ip4.addr : 0} { } constexpr IP4(const esp_ip_addr_t & ip) : l{ip.type == IPADDR_TYPE_V4 ? ip.u_addr.ip4.addr : 0} { } IP4(const char *str); constexpr operator uint32_t () { return l; } operator IPAddress () { return IPAddress(l); } constexpr operator ip_addr_t () { return ip_addr_t { .u_addr = {.ip4 = {.addr = l}}, .type = IPADDR_TYPE_V4 }; } constexpr operator esp_ip_addr_t () { return esp_ip_addr_t { .u_addr = {.ip4 = {.addr = l}}, .type = IPADDR_TYPE_V4 }; } constexpr operator bool () { return l != 0; } constexpr bool operator ! () { return l == 0; } constexpr bool operator == (const IP4 &b) { return l == b.l; } constexpr bool operator != (const IP4 &b) { return l != b.l; } constexpr uint8_t operator [] (size_t n) { return b[n]; } uint8_t & operator [] (size_t n) { return b[n]; } const char *cstr(); }; constexpr IP4 null_ip(0);