ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ip_address.h
Go to the documentation of this file.
1#pragma once
3#ifdef USE_NETWORK
4#include <cstdint>
5#include <string>
6#include <cstdio>
7#include <array>
8#include <cstring>
10#include "esphome/core/macros.h"
11
12#if defined(USE_ESP32) || defined(USE_LIBRETINY) || USE_ARDUINO_VERSION_CODE > VERSION_CODE(3, 0, 0)
13#include <lwip/ip_addr.h>
14#endif
15#if USE_ARDUINO
16#include <Arduino.h>
17#include <IPAddress.h>
18#endif /* USE_ARDUINO */
19
20#ifdef USE_HOST
21#include <arpa/inet.h>
22#if USE_NETWORK_IPV6
23using ip4_addr_t = struct in_addr;
24using ip6_addr_t = struct in6_addr;
25struct ip_addr_t { // NOLINT(readability-identifier-naming) mirrors the lwIP name the other platforms use
26 union {
27 struct in6_addr ip6;
28 struct in_addr ip4;
30 uint8_t type;
31};
32enum : uint8_t { IPADDR_TYPE_V4 = 0, IPADDR_TYPE_V6 = 6 };
33static inline int ipaddr_aton(const char *cp, ip_addr_t *addr) {
34 if (strchr(cp, ':') != nullptr) {
35 if (inet_pton(AF_INET6, cp, &addr->u_addr.ip6) != 1) {
36 return 0;
37 }
38 addr->type = IPADDR_TYPE_V6;
39 return 1;
40 }
41 if (inet_aton(cp, &addr->u_addr.ip4) != 1) {
42 return 0;
43 }
44 addr->type = IPADDR_TYPE_V4;
45 return 1;
46}
47#else
48using ip_addr_t = in_addr;
49using ip4_addr_t = in_addr;
50#define ipaddr_aton(x, y) inet_aton((x), (y))
51#endif // USE_NETWORK_IPV6
52#endif // USE_HOST
53
54#ifdef USE_ZEPHYR
55#include <zephyr/net/net_ip.h>
56#include <zephyr/net/socket.h>
57#ifndef CONFIG_NET_IPV6
58#error "Zephyr builds need CONFIG_NET_IPV6"
59#endif
60using ip_addr_t = struct in6_addr;
61static inline int ipaddr_aton(const char *cp, ip_addr_t *addr) {
62#if defined(CONFIG_NET_IPV4)
63 // Dual-stack: a plain IPv4 literal must still parse -- store it as a v4-mapped
64 // IPv6 address (::ffff:a.b.c.d), matching the 4-octet constructor below.
65 struct in_addr addr4;
66 if (net_addr_pton(AF_INET, cp, &addr4) == 0) {
67 net_ipv6_addr_create_v4_mapped(&addr4, addr);
68 return 1;
69 }
70#endif /* CONFIG_NET_IPV4 */
71 return net_addr_pton(AF_INET6, cp, addr) == 0 ? 1 : 0;
72}
73#endif /* USE_ZEPHYR */
74
75#if USE_ESP32_FRAMEWORK_ARDUINO
76#define arduino_ns Arduino_h
77#elif USE_LIBRETINY
78#define arduino_ns arduino
79#elif USE_ARDUINO
80#define arduino_ns
81#endif /* USE_ESP32_FRAMEWORK_ARDUINO */
82
83#ifdef USE_ESP32
84#include <esp_netif.h>
85#endif /* USE_ESP32 */
86
88
90static constexpr size_t IP_ADDRESS_BUFFER_SIZE =
91#if defined(USE_ZEPHYR)
92 // Mainline Zephyr's net_addr_ntop() (subsys/net/ip/utils.c) never checks
93 // its size argument, so an undersized buffer overflows instead of failing cleanly. Size
94 // for the worst case: an IPv4-mapped IPv6 address needs up to INET6_ADDRSTRLEN (46).
95 46;
96#else
97 40;
98#endif
99
101inline void lowercase_ip_str(char *buf) {
102 for (char *p = buf; *p; ++p) {
103 if (*p >= 'A' && *p <= 'F')
104 *p += 32;
105 }
106}
107
108struct IPAddress {
109 public:
110#if defined(USE_ZEPHYR)
111 // With CONFIG_NET_IPV4 (dual stack), IPv4 addresses are stored v4-mapped (::ffff:a.b.c.d).
112 IPAddress() { memset(&ip_addr_, 0, sizeof(ip_addr_)); }
113#if defined(CONFIG_NET_IPV4)
114 IPAddress(uint8_t first, uint8_t second, uint8_t third, uint8_t fourth) {
115 struct in_addr addr4{
116 .s_addr = htonl(((uint32_t) first << 24) | ((uint32_t) second << 16) | ((uint32_t) third << 8) | fourth)};
117 net_ipv6_addr_create_v4_mapped(&addr4, &ip_addr_);
118 }
119#endif /* CONFIG_NET_IPV4 */
120 IPAddress(const std::string &in_address) : ip_addr_{} { ipaddr_aton(in_address.c_str(), &ip_addr_); }
121 IPAddress(const struct in6_addr *other_ip) { ip_addr_ = *other_ip; }
122 IPAddress(const struct sockaddr_in6 *addr) { ip_addr_ = addr->sin6_addr; }
123
124 operator struct in6_addr() const { return ip_addr_; }
125
126 bool is_set() const {
127#if defined(CONFIG_NET_IPV4)
128 if (net_ipv6_addr_is_v4_mapped(&ip_addr_))
129 return ip_addr_.s6_addr32[3] != 0;
130#endif /* CONFIG_NET_IPV4 */
131 return !net_ipv6_is_addr_unspecified(&ip_addr_);
132 }
133#if defined(CONFIG_NET_IPV4)
134 // V4-mapped (::ffff:a.b.c.d) addresses represent IPv4 traffic carried over the AF_INET6 socket
135 // Zephyr always uses when CONFIG_NET_IPV6 is on; anything else is real IPv6.
136 bool is_ip4() const { return this->is_set() && net_ipv6_addr_is_v4_mapped(&ip_addr_); }
137 bool is_ip6() const { return this->is_set() && !net_ipv6_addr_is_v4_mapped(&ip_addr_); }
138#else
139 bool is_ip4() const { return false; }
140 bool is_ip6() const { return this->is_set(); }
141#endif /* CONFIG_NET_IPV4 */
142 bool is_multicast() const {
143#if defined(CONFIG_NET_IPV4)
144 if (net_ipv6_addr_is_v4_mapped(&ip_addr_)) {
145 struct in_addr addr4{};
146 addr4.s_addr = ip_addr_.s6_addr32[3];
147 return net_ipv4_is_addr_mcast(&addr4);
148 }
149#endif /* CONFIG_NET_IPV4 */
150 return net_ipv6_is_addr_mcast(&ip_addr_);
151 }
152 char *str_to(char *buf) const {
153#if defined(CONFIG_NET_IPV4)
154 if (this->is_ip4()) {
155 struct in_addr addr4{};
156 addr4.s_addr = ip_addr_.s6_addr32[3];
157 if (net_addr_ntop(AF_INET, &addr4, buf, IP_ADDRESS_BUFFER_SIZE) == nullptr)
158 buf[0] = '\0';
159 return buf;
160 }
161#endif /* CONFIG_NET_IPV4 */
162 if (net_addr_ntop(AF_INET6, &ip_addr_, buf, IP_ADDRESS_BUFFER_SIZE) == nullptr)
163 buf[0] = '\0';
164 return buf;
165 }
166 bool operator==(const IPAddress &other) const { return net_ipv6_addr_cmp(&ip_addr_, &other.ip_addr_); }
167 bool operator!=(const IPAddress &other) const { return !net_ipv6_addr_cmp(&ip_addr_, &other.ip_addr_); }
168
169#elif defined(USE_HOST)
170#if USE_NETWORK_IPV6
171 IPAddress() { memset(&this->ip_addr_, 0, sizeof(this->ip_addr_)); }
172 IPAddress(uint8_t first, uint8_t second, uint8_t third, uint8_t fourth) {
173 memset(&this->ip_addr_, 0, sizeof(this->ip_addr_));
174 this->ip_addr_.u_addr.ip4.s_addr =
175 htonl(((uint32_t) first << 24) | ((uint32_t) second << 16) | ((uint32_t) third << 8) | fourth);
177 }
178 IPAddress(const char *in_address) {
179 memset(&this->ip_addr_, 0, sizeof(this->ip_addr_));
180 ipaddr_aton(in_address, &this->ip_addr_);
181 }
182 IPAddress(const std::string &in_address) : IPAddress(in_address.c_str()) {}
183 IPAddress(const ip_addr_t *other_ip) { memcpy(&this->ip_addr_, other_ip, sizeof(ip_addr_t)); }
185 memset(&this->ip_addr_, 0, sizeof(this->ip_addr_));
186 this->ip_addr_.u_addr.ip4 = *other_ip;
188 }
190 memset(&this->ip_addr_, 0, sizeof(this->ip_addr_));
191 this->ip_addr_.u_addr.ip6 = *other_ip;
193 }
194 operator ip_addr_t() const { return this->ip_addr_; }
195 bool is_set() const {
196 if (this->ip_addr_.type == IPADDR_TYPE_V6) {
197 static constexpr uint8_t ZERO[sizeof(struct in6_addr)] = {};
198 return memcmp(this->ip_addr_.u_addr.ip6.s6_addr, ZERO, sizeof(ZERO)) != 0;
199 }
200 return this->ip_addr_.u_addr.ip4.s_addr != 0;
201 }
202 bool is_ip4() const { return this->ip_addr_.type == IPADDR_TYPE_V4; }
203 bool is_ip6() const { return this->ip_addr_.type == IPADDR_TYPE_V6; }
204 bool is_multicast() const {
205 if (this->ip_addr_.type == IPADDR_TYPE_V6) {
206 return this->ip_addr_.u_addr.ip6.s6_addr[0] == 0xff;
207 }
208 return (ntohl(this->ip_addr_.u_addr.ip4.s_addr) & 0xF0000000UL) == 0xE0000000UL;
209 }
210 char *str_to(char *buf) const {
211 if (this->ip_addr_.type == IPADDR_TYPE_V6) {
212 inet_ntop(AF_INET6, &this->ip_addr_.u_addr.ip6, buf, IP_ADDRESS_BUFFER_SIZE);
213 } else {
214 inet_ntop(AF_INET, &this->ip_addr_.u_addr.ip4, buf, IP_ADDRESS_BUFFER_SIZE);
215 }
216 lowercase_ip_str(buf);
217 return buf;
218 }
219 bool operator==(const IPAddress &other) const {
220 if (this->ip_addr_.type != other.ip_addr_.type) {
221 return false;
222 }
223 if (this->ip_addr_.type == IPADDR_TYPE_V6) {
224 return memcmp(&this->ip_addr_.u_addr.ip6, &other.ip_addr_.u_addr.ip6, sizeof(struct in6_addr)) == 0;
225 }
226 return this->ip_addr_.u_addr.ip4.s_addr == other.ip_addr_.u_addr.ip4.s_addr;
227 }
228 bool operator!=(const IPAddress &other) const { return !(*this == other); }
229 IPAddress &operator+=(uint8_t increase) {
230 if (this->ip_addr_.type == IPADDR_TYPE_V4) {
231 (((uint8_t *) (&this->ip_addr_.u_addr.ip4))[3]) += increase;
232 }
233 return *this;
234 }
235#else
236 IPAddress() { ip_addr_.s_addr = 0; }
237 IPAddress(uint8_t first, uint8_t second, uint8_t third, uint8_t fourth) {
238 this->ip_addr_.s_addr = htonl((first << 24) | (second << 16) | (third << 8) | fourth);
239 }
240 IPAddress(const std::string &in_address) { inet_aton(in_address.c_str(), &ip_addr_); }
241 IPAddress(const ip_addr_t *other_ip) { ip_addr_ = *other_ip; }
242 bool is_ip4() const { return true; }
243 bool is_ip6() const { return false; }
245 char *str_to(char *buf) const {
246 inet_ntop(AF_INET, &ip_addr_, buf, IP_ADDRESS_BUFFER_SIZE);
247 return buf; // IPv4 only, no hex letters to lowercase
248 }
249#endif // USE_NETWORK_IPV6
250#else // LWIP
251 IPAddress() { ip_addr_set_zero(&ip_addr_); }
252#if LWIP_IPV4
253 IPAddress(uint8_t first, uint8_t second, uint8_t third, uint8_t fourth) {
254 IP_ADDR4(&ip_addr_, first, second, third, fourth);
255 }
256#endif /* LWIP_IPV4 */
257 IPAddress(const ip_addr_t *other_ip) { ip_addr_copy(ip_addr_, *other_ip); }
258 IPAddress(const char *in_address) { ipaddr_aton(in_address, &ip_addr_); }
259 IPAddress(const std::string &in_address) { ipaddr_aton(in_address.c_str(), &ip_addr_); }
260#if LWIP_IPV4
262 memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(ip4_addr_t));
263#if LWIP_IPV6
265#endif /* LWIP_IPV6 */
266 }
267#endif /* LWIP_IPV4 */
268#if USE_ARDUINO
269 IPAddress(const arduino_ns::IPAddress &other_ip) { ip_addr_set_ip4_u32(&ip_addr_, other_ip); }
270#endif /* USE_ARDUINO */
271#if LWIP_IPV6
273 memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(ip6_addr_t));
274 IP_SET_TYPE_VAL(ip_addr_, IPADDR_TYPE_V6);
275 }
276#endif /* LWIP_IPV6 */
277
278#ifdef USE_ESP32
279#if LWIP_IPV6
280 IPAddress(esp_ip6_addr_t *other_ip) {
281 memcpy((void *) ip_2_ip6(&ip_addr_), (void *) other_ip, sizeof(esp_ip6_addr_t));
282 IP_SET_TYPE_VAL(ip_addr_, IPADDR_TYPE_V6);
283 }
284#endif /* LWIP_IPV6 */
285#if LWIP_IPV4
286 IPAddress(esp_ip4_addr_t *other_ip) {
287 memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(esp_ip4_addr_t));
288#if LWIP_IPV6
290#endif /* LWIP_IPV6 */
291 }
292#endif /* LWIP_IPV4 */
293 IPAddress(esp_ip_addr_t *other_ip) {
294#if LWIP_IPV6
295 memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(ip_addr_));
296#elif LWIP_IPV4
297 memcpy((void *) &ip_addr_, (void *) &other_ip->u_addr.ip4, sizeof(ip_addr_));
298#endif /* LWIP_IPV6 */
299 }
300 operator esp_ip_addr_t() const {
301 esp_ip_addr_t tmp{};
302#if LWIP_IPV6
303 memcpy((void *) &tmp, (void *) &ip_addr_, sizeof(ip_addr_));
304#if !LWIP_IPV4
305 tmp.type = IPADDR_TYPE_V6;
306#endif /* !LWIP_IPV4 */
307#elif LWIP_IPV4
308 memcpy((void *) &tmp.u_addr.ip4, (void *) &ip_addr_, sizeof(ip_addr_));
309 tmp.type = IPADDR_TYPE_V4;
310#else
311#error "Neither LWIP_IPV4 nor LWIP_IPV6 is enabled"
312#endif /* LWIP_IPV6 */
313 return tmp;
314 }
315#if LWIP_IPV4
316 operator esp_ip4_addr_t() const {
317 esp_ip4_addr_t tmp;
318#if LWIP_IPV6
319 memcpy((void *) &tmp, (void *) &ip_addr_.u_addr.ip4, sizeof(esp_ip4_addr_t));
320#else
321 memcpy((void *) &tmp, (void *) &ip_addr_, sizeof(ip_addr_));
322#endif /* LWIP_IPV6 */
323 return tmp;
324 }
325#endif /* LWIP_IPV4 */
326#endif /* USE_ESP32 */
327
328 operator ip_addr_t() const { return ip_addr_; }
329#if LWIP_IPV6 && LWIP_IPV4
330 operator ip4_addr_t() const { return *ip_2_ip4(&ip_addr_); }
331#endif /* LWIP_IPV6 && LWIP_IPV4 */
332
333#if USE_ARDUINO
334 operator arduino_ns::IPAddress() const { return ip_addr_get_ip4_u32(&ip_addr_); }
335#endif /* USE_ARDUINO */
336
337 bool is_set() const { return !ip_addr_isany(&ip_addr_); } // NOLINT(readability-simplify-boolean-expr)
338 bool is_ip4() const { return IP_IS_V4(&ip_addr_); }
339 bool is_ip6() const { return IP_IS_V6(&ip_addr_); }
340 bool is_multicast() const { return ip_addr_ismulticast(&ip_addr_); }
343 char *str_to(char *buf) const {
344 ipaddr_ntoa_r(&ip_addr_, buf, IP_ADDRESS_BUFFER_SIZE);
345 lowercase_ip_str(buf);
346 return buf;
347 }
348 bool operator==(const IPAddress &other) const { return ip_addr_cmp(&ip_addr_, &other.ip_addr_); }
349 bool operator!=(const IPAddress &other) const { return !ip_addr_cmp(&ip_addr_, &other.ip_addr_); }
350#if LWIP_IPV4
351 IPAddress &operator+=(uint8_t increase) {
352 if (IP_IS_V4(&ip_addr_)) {
353#if LWIP_IPV6
354 (((u8_t *) (&ip_addr_.u_addr.ip4))[3]) += increase;
355#else
356 (((u8_t *) (&ip_addr_.addr))[3]) += increase;
357#endif /* LWIP_IPV6 */
358 }
359 return *this;
360 }
361#endif /* LWIP_IPV4 */
362#endif // LWIP
363
364 protected:
366};
367
368using IPAddresses = std::array<IPAddress, 5>;
369
370} // namespace esphome::network
371#endif /* NETWORK */
uint8_t second
struct in6_addr ip6_addr_t
Definition ip_address.h:24
in_addr ip_addr_t
Definition ip_address.h:48
@ IPADDR_TYPE_V6
Definition ip_address.h:32
@ IPADDR_TYPE_V4
Definition ip_address.h:32
struct in_addr ip4_addr_t
Definition ip_address.h:23
void lowercase_ip_str(char *buf)
Lowercase hex digits in IP address string (A-F -> a-f for IPv6 per RFC 5952)
Definition ip_address.h:101
std::array< IPAddress, 5 > IPAddresses
Definition ip_address.h:368
static void uint32_t
IPAddress(const struct sockaddr_in6 *addr)
Definition ip_address.h:122
IPAddress(const char *in_address)
Definition ip_address.h:178
IPAddress(ip6_addr_t *other_ip)
Definition ip_address.h:189
bool operator!=(const IPAddress &other) const
Definition ip_address.h:167
bool operator==(const IPAddress &other) const
Definition ip_address.h:166
char * str_to(char *buf) const
Definition ip_address.h:152
IPAddress(esp_ip_addr_t *other_ip)
Definition ip_address.h:293
IPAddress(const ip_addr_t *other_ip)
Definition ip_address.h:183
IPAddress(esp_ip6_addr_t *other_ip)
Definition ip_address.h:280
IPAddress(uint8_t first, uint8_t second, uint8_t third, uint8_t fourth)
Definition ip_address.h:114
IPAddress(const struct in6_addr *other_ip)
Definition ip_address.h:121
IPAddress & operator+=(uint8_t increase)
Definition ip_address.h:229
IPAddress(esp_ip4_addr_t *other_ip)
Definition ip_address.h:286
IPAddress(ip4_addr_t *other_ip)
Definition ip_address.h:184
IPAddress(const std::string &in_address)
Definition ip_address.h:120
IPAddress(const arduino_ns::IPAddress &other_ip)
Definition ip_address.h:269
struct in6_addr ip6
Definition ip_address.h:27
union ip_addr_t::@155 u_addr
struct in_addr ip4
Definition ip_address.h:28
uint8_t type
Definition ip_address.h:30
struct in6_addr sin6_addr
Definition headers.h:89