ESPHome 2026.3.0-dev
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>
10
11#if defined(USE_ESP32) || defined(USE_LIBRETINY) || USE_ARDUINO_VERSION_CODE > VERSION_CODE(3, 0, 0)
12#include <lwip/ip_addr.h>
13#endif
14
15#if USE_ARDUINO
16#include <Arduino.h>
17#include <IPAddress.h>
18#endif /* USE_ADRDUINO */
19
20#ifdef USE_HOST
21#include <arpa/inet.h>
22using ip_addr_t = in_addr;
23using ip4_addr_t = in_addr;
24#define ipaddr_aton(x, y) inet_aton((x), (y))
25#endif
26
27#if USE_ESP32_FRAMEWORK_ARDUINO
28#define arduino_ns Arduino_h
29#elif USE_LIBRETINY
30#define arduino_ns arduino
31#elif USE_ARDUINO
32#define arduino_ns
33#endif
34
35#ifdef USE_ESP32
36#include <cstring>
37#include <esp_netif.h>
38#endif
39
41
43static constexpr size_t IP_ADDRESS_BUFFER_SIZE = 40;
44
46inline void lowercase_ip_str(char *buf) {
47 for (char *p = buf; *p; ++p) {
48 if (*p >= 'A' && *p <= 'F')
49 *p += 32;
50 }
51}
52
53struct IPAddress {
54 public:
55#ifdef USE_HOST
56 IPAddress() { ip_addr_.s_addr = 0; }
57 IPAddress(uint8_t first, uint8_t second, uint8_t third, uint8_t fourth) {
58 this->ip_addr_.s_addr = htonl((first << 24) | (second << 16) | (third << 8) | fourth);
59 }
60 IPAddress(const std::string &in_address) { inet_aton(in_address.c_str(), &ip_addr_); }
61 IPAddress(const ip_addr_t *other_ip) { ip_addr_ = *other_ip; }
62 // Remove before 2026.8.0
64 "str() is deprecated: use 'char buf[IP_ADDRESS_BUFFER_SIZE]; ip.str_to(buf);' instead. Removed in 2026.8.0",
65 "2026.2.0")
66 std::string str() const {
67 char buf[IP_ADDRESS_BUFFER_SIZE];
68 this->str_to(buf);
69 return buf;
70 }
72 char *str_to(char *buf) const {
73 inet_ntop(AF_INET, &ip_addr_, buf, IP_ADDRESS_BUFFER_SIZE);
74 return buf; // IPv4 only, no hex letters to lowercase
75 }
76#else
77 IPAddress() { ip_addr_set_zero(&ip_addr_); }
78 IPAddress(uint8_t first, uint8_t second, uint8_t third, uint8_t fourth) {
79 IP_ADDR4(&ip_addr_, first, second, third, fourth);
80 }
81 IPAddress(const ip_addr_t *other_ip) { ip_addr_copy(ip_addr_, *other_ip); }
82 IPAddress(const char *in_address) { ipaddr_aton(in_address, &ip_addr_); }
83 IPAddress(const std::string &in_address) { ipaddr_aton(in_address.c_str(), &ip_addr_); }
84 IPAddress(ip4_addr_t *other_ip) {
85 memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(ip4_addr_t));
86#if USE_ESP32 && LWIP_IPV6
87 ip_addr_.type = IPADDR_TYPE_V4;
88#endif
89 }
90#if USE_ARDUINO
91 IPAddress(const arduino_ns::IPAddress &other_ip) { ip_addr_set_ip4_u32(&ip_addr_, other_ip); }
92#endif
93#if LWIP_IPV6
94 IPAddress(ip6_addr_t *other_ip) {
95 memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(ip6_addr_t));
96 ip_addr_.type = IPADDR_TYPE_V6;
97 }
98#endif /* LWIP_IPV6 */
99
100#ifdef USE_ESP32
101#if LWIP_IPV6
102 IPAddress(esp_ip6_addr_t *other_ip) {
103 memcpy((void *) &ip_addr_.u_addr.ip6, (void *) other_ip, sizeof(esp_ip6_addr_t));
104 ip_addr_.type = IPADDR_TYPE_V6;
105 }
106#endif /* LWIP_IPV6 */
107 IPAddress(esp_ip4_addr_t *other_ip) {
108 memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(esp_ip4_addr_t));
109#if LWIP_IPV6
110 ip_addr_.type = IPADDR_TYPE_V4;
111#endif
112 }
113 IPAddress(esp_ip_addr_t *other_ip) {
114#if LWIP_IPV6
115 memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(ip_addr_));
116#else
117 memcpy((void *) &ip_addr_, (void *) &other_ip->u_addr.ip4, sizeof(ip_addr_));
118#endif
119 }
120 operator esp_ip_addr_t() const {
121 esp_ip_addr_t tmp;
122#if LWIP_IPV6
123 memcpy((void *) &tmp, (void *) &ip_addr_, sizeof(ip_addr_));
124#else
125 memcpy((void *) &tmp.u_addr.ip4, (void *) &ip_addr_, sizeof(ip_addr_));
126#endif /* LWIP_IPV6 */
127 return tmp;
128 }
129 operator esp_ip4_addr_t() const {
130 esp_ip4_addr_t tmp;
131#if LWIP_IPV6
132 memcpy((void *) &tmp, (void *) &ip_addr_.u_addr.ip4, sizeof(esp_ip4_addr_t));
133#else
134 memcpy((void *) &tmp, (void *) &ip_addr_, sizeof(ip_addr_));
135#endif /* LWIP_IPV6 */
136 return tmp;
137 }
138#endif /* USE_ESP32 */
139
140 operator ip_addr_t() const { return ip_addr_; }
141#if LWIP_IPV6
142 operator ip4_addr_t() const { return *ip_2_ip4(&ip_addr_); }
143#endif /* LWIP_IPV6 */
144
145#if USE_ARDUINO
146 operator arduino_ns::IPAddress() const { return ip_addr_get_ip4_u32(&ip_addr_); }
147#endif
148
149 bool is_set() const { return !ip_addr_isany(&ip_addr_); } // NOLINT(readability-simplify-boolean-expr)
150 bool is_ip4() const { return IP_IS_V4(&ip_addr_); }
151 bool is_ip6() const { return IP_IS_V6(&ip_addr_); }
152 bool is_multicast() const { return ip_addr_ismulticast(&ip_addr_); }
153 // Remove before 2026.8.0
155 "str() is deprecated: use 'char buf[IP_ADDRESS_BUFFER_SIZE]; ip.str_to(buf);' instead. Removed in 2026.8.0",
156 "2026.2.0")
157 std::string str() const {
158 char buf[IP_ADDRESS_BUFFER_SIZE];
159 this->str_to(buf);
160 return buf;
161 }
164 char *str_to(char *buf) const {
165 ipaddr_ntoa_r(&ip_addr_, buf, IP_ADDRESS_BUFFER_SIZE);
166 lowercase_ip_str(buf);
167 return buf;
168 }
169 bool operator==(const IPAddress &other) const { return ip_addr_cmp(&ip_addr_, &other.ip_addr_); }
170 bool operator!=(const IPAddress &other) const { return !ip_addr_cmp(&ip_addr_, &other.ip_addr_); }
171 IPAddress &operator+=(uint8_t increase) {
172 if (IP_IS_V4(&ip_addr_)) {
173#if LWIP_IPV6
174 (((u8_t *) (&ip_addr_.u_addr.ip4))[3]) += increase;
175#else
176 (((u8_t *) (&ip_addr_.addr))[3]) += increase;
177#endif /* LWIP_IPV6 */
178 }
179 return *this;
180 }
181#endif
182
183 protected:
185};
186
187using IPAddresses = std::array<IPAddress, 5>;
188
189} // namespace esphome::network
190#endif
uint8_t second
in_addr ip_addr_t
Definition ip_address.h:22
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:46
std::array< IPAddress, 5 > IPAddresses
Definition ip_address.h:187
bool operator==(optional< T > const &x, optional< U > const &y)
Definition optional.h:118
bool operator!=(optional< T > const &x, optional< U > const &y)
Definition optional.h:122
std::string & operator+=(std::string &lhs, const StringRef &rhs)
Definition string_ref.h:176
IPAddress(const char *in_address)
Definition ip_address.h:82
IPAddress(ip6_addr_t *other_ip)
Definition ip_address.h:94
ESPDEPRECATED("str() is deprecated: use 'char buf[IP_ADDRESS_BUFFER_SIZE]; ip.str_to(buf);' instead. Removed in 2026.8.0", "2026.2.0") std
Definition ip_address.h:63
IPAddress(esp_ip_addr_t *other_ip)
Definition ip_address.h:113
IPAddress(const ip_addr_t *other_ip)
Definition ip_address.h:61
IPAddress(esp_ip6_addr_t *other_ip)
Definition ip_address.h:102
IPAddress(uint8_t first, uint8_t second, uint8_t third, uint8_t fourth)
Definition ip_address.h:57
IPAddress(esp_ip4_addr_t *other_ip)
Definition ip_address.h:107
IPAddress(ip4_addr_t *other_ip)
Definition ip_address.h:84
IPAddress(const std::string &in_address)
Definition ip_address.h:60
IPAddress(const arduino_ns::IPAddress &other_ip)
Definition ip_address.h:91