ESPHome 2026.2.3
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
40namespace esphome {
41namespace network {
42
44static constexpr size_t IP_ADDRESS_BUFFER_SIZE = 40;
45
47inline void lowercase_ip_str(char *buf) {
48 for (char *p = buf; *p; ++p) {
49 if (*p >= 'A' && *p <= 'F')
50 *p += 32;
51 }
52}
53
54struct IPAddress {
55 public:
56#ifdef USE_HOST
57 IPAddress() { ip_addr_.s_addr = 0; }
58 IPAddress(uint8_t first, uint8_t second, uint8_t third, uint8_t fourth) {
59 this->ip_addr_.s_addr = htonl((first << 24) | (second << 16) | (third << 8) | fourth);
60 }
61 IPAddress(const std::string &in_address) { inet_aton(in_address.c_str(), &ip_addr_); }
62 IPAddress(const ip_addr_t *other_ip) { ip_addr_ = *other_ip; }
63 // Remove before 2026.8.0
65 "str() is deprecated: use 'char buf[IP_ADDRESS_BUFFER_SIZE]; ip.str_to(buf);' instead. Removed in 2026.8.0",
66 "2026.2.0")
67 std::string str() const {
68 char buf[IP_ADDRESS_BUFFER_SIZE];
69 this->str_to(buf);
70 return buf;
71 }
73 char *str_to(char *buf) const {
74 inet_ntop(AF_INET, &ip_addr_, buf, IP_ADDRESS_BUFFER_SIZE);
75 return buf; // IPv4 only, no hex letters to lowercase
76 }
77#else
78 IPAddress() { ip_addr_set_zero(&ip_addr_); }
79 IPAddress(uint8_t first, uint8_t second, uint8_t third, uint8_t fourth) {
80 IP_ADDR4(&ip_addr_, first, second, third, fourth);
81 }
82 IPAddress(const ip_addr_t *other_ip) { ip_addr_copy(ip_addr_, *other_ip); }
83 IPAddress(const char *in_address) { ipaddr_aton(in_address, &ip_addr_); }
84 IPAddress(const std::string &in_address) { ipaddr_aton(in_address.c_str(), &ip_addr_); }
85 IPAddress(ip4_addr_t *other_ip) {
86 memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(ip4_addr_t));
87#if USE_ESP32 && LWIP_IPV6
88 ip_addr_.type = IPADDR_TYPE_V4;
89#endif
90 }
91#if USE_ARDUINO
92 IPAddress(const arduino_ns::IPAddress &other_ip) { ip_addr_set_ip4_u32(&ip_addr_, other_ip); }
93#endif
94#if LWIP_IPV6
95 IPAddress(ip6_addr_t *other_ip) {
96 memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(ip6_addr_t));
97 ip_addr_.type = IPADDR_TYPE_V6;
98 }
99#endif /* LWIP_IPV6 */
100
101#ifdef USE_ESP32
102#if LWIP_IPV6
103 IPAddress(esp_ip6_addr_t *other_ip) {
104 memcpy((void *) &ip_addr_.u_addr.ip6, (void *) other_ip, sizeof(esp_ip6_addr_t));
105 ip_addr_.type = IPADDR_TYPE_V6;
106 }
107#endif /* LWIP_IPV6 */
108 IPAddress(esp_ip4_addr_t *other_ip) {
109 memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(esp_ip4_addr_t));
110#if LWIP_IPV6
111 ip_addr_.type = IPADDR_TYPE_V4;
112#endif
113 }
114 IPAddress(esp_ip_addr_t *other_ip) {
115#if LWIP_IPV6
116 memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(ip_addr_));
117#else
118 memcpy((void *) &ip_addr_, (void *) &other_ip->u_addr.ip4, sizeof(ip_addr_));
119#endif
120 }
121 operator esp_ip_addr_t() const {
122 esp_ip_addr_t tmp;
123#if LWIP_IPV6
124 memcpy((void *) &tmp, (void *) &ip_addr_, sizeof(ip_addr_));
125#else
126 memcpy((void *) &tmp.u_addr.ip4, (void *) &ip_addr_, sizeof(ip_addr_));
127#endif /* LWIP_IPV6 */
128 return tmp;
129 }
130 operator esp_ip4_addr_t() const {
131 esp_ip4_addr_t tmp;
132#if LWIP_IPV6
133 memcpy((void *) &tmp, (void *) &ip_addr_.u_addr.ip4, sizeof(esp_ip4_addr_t));
134#else
135 memcpy((void *) &tmp, (void *) &ip_addr_, sizeof(ip_addr_));
136#endif /* LWIP_IPV6 */
137 return tmp;
138 }
139#endif /* USE_ESP32 */
140
141 operator ip_addr_t() const { return ip_addr_; }
142#if LWIP_IPV6
143 operator ip4_addr_t() const { return *ip_2_ip4(&ip_addr_); }
144#endif /* LWIP_IPV6 */
145
146#if USE_ARDUINO
147 operator arduino_ns::IPAddress() const { return ip_addr_get_ip4_u32(&ip_addr_); }
148#endif
149
150 bool is_set() const { return !ip_addr_isany(&ip_addr_); } // NOLINT(readability-simplify-boolean-expr)
151 bool is_ip4() const { return IP_IS_V4(&ip_addr_); }
152 bool is_ip6() const { return IP_IS_V6(&ip_addr_); }
153 bool is_multicast() const { return ip_addr_ismulticast(&ip_addr_); }
154 // Remove before 2026.8.0
156 "str() is deprecated: use 'char buf[IP_ADDRESS_BUFFER_SIZE]; ip.str_to(buf);' instead. Removed in 2026.8.0",
157 "2026.2.0")
158 std::string str() const {
159 char buf[IP_ADDRESS_BUFFER_SIZE];
160 this->str_to(buf);
161 return buf;
162 }
165 char *str_to(char *buf) const {
166 ipaddr_ntoa_r(&ip_addr_, buf, IP_ADDRESS_BUFFER_SIZE);
167 lowercase_ip_str(buf);
168 return buf;
169 }
170 bool operator==(const IPAddress &other) const { return ip_addr_cmp(&ip_addr_, &other.ip_addr_); }
171 bool operator!=(const IPAddress &other) const { return !ip_addr_cmp(&ip_addr_, &other.ip_addr_); }
172 IPAddress &operator+=(uint8_t increase) {
173 if (IP_IS_V4(&ip_addr_)) {
174#if LWIP_IPV6
175 (((u8_t *) (&ip_addr_.u_addr.ip4))[3]) += increase;
176#else
177 (((u8_t *) (&ip_addr_.addr))[3]) += increase;
178#endif /* LWIP_IPV6 */
179 }
180 return *this;
181 }
182#endif
183
184 protected:
186};
187
188using IPAddresses = std::array<IPAddress, 5>;
189
190} // namespace network
191} // namespace esphome
192#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:47
std::array< IPAddress, 5 > IPAddresses
Definition ip_address.h:188
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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:162
IPAddress(const char *in_address)
Definition ip_address.h:83
IPAddress(ip6_addr_t *other_ip)
Definition ip_address.h:95
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:64
IPAddress(esp_ip_addr_t *other_ip)
Definition ip_address.h:114
IPAddress(const ip_addr_t *other_ip)
Definition ip_address.h:62
IPAddress(esp_ip6_addr_t *other_ip)
Definition ip_address.h:103
IPAddress(uint8_t first, uint8_t second, uint8_t third, uint8_t fourth)
Definition ip_address.h:58
IPAddress(esp_ip4_addr_t *other_ip)
Definition ip_address.h:108
IPAddress(ip4_addr_t *other_ip)
Definition ip_address.h:85
IPAddress(const std::string &in_address)
Definition ip_address.h:61
IPAddress(const arduino_ns::IPAddress &other_ip)
Definition ip_address.h:92