ESPHome 2026.2.1
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
64 ESPDEPRECATED("Use str_to() instead. Removed in 2026.8.0", "2026.2.0")
65 std::string str() const {
66 char buf[IP_ADDRESS_BUFFER_SIZE];
67 this->str_to(buf);
68 return buf;
69 }
71 char *str_to(char *buf) const {
72 inet_ntop(AF_INET, &ip_addr_, buf, IP_ADDRESS_BUFFER_SIZE);
73 return buf; // IPv4 only, no hex letters to lowercase
74 }
75#else
76 IPAddress() { ip_addr_set_zero(&ip_addr_); }
77 IPAddress(uint8_t first, uint8_t second, uint8_t third, uint8_t fourth) {
78 IP_ADDR4(&ip_addr_, first, second, third, fourth);
79 }
80 IPAddress(const ip_addr_t *other_ip) { ip_addr_copy(ip_addr_, *other_ip); }
81 IPAddress(const char *in_address) { ipaddr_aton(in_address, &ip_addr_); }
82 IPAddress(const std::string &in_address) { ipaddr_aton(in_address.c_str(), &ip_addr_); }
83 IPAddress(ip4_addr_t *other_ip) {
84 memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(ip4_addr_t));
85#if USE_ESP32 && LWIP_IPV6
86 ip_addr_.type = IPADDR_TYPE_V4;
87#endif
88 }
89#if USE_ARDUINO
90 IPAddress(const arduino_ns::IPAddress &other_ip) { ip_addr_set_ip4_u32(&ip_addr_, other_ip); }
91#endif
92#if LWIP_IPV6
93 IPAddress(ip6_addr_t *other_ip) {
94 memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(ip6_addr_t));
95 ip_addr_.type = IPADDR_TYPE_V6;
96 }
97#endif /* LWIP_IPV6 */
98
99#ifdef USE_ESP32
100#if LWIP_IPV6
101 IPAddress(esp_ip6_addr_t *other_ip) {
102 memcpy((void *) &ip_addr_.u_addr.ip6, (void *) other_ip, sizeof(esp_ip6_addr_t));
103 ip_addr_.type = IPADDR_TYPE_V6;
104 }
105#endif /* LWIP_IPV6 */
106 IPAddress(esp_ip4_addr_t *other_ip) {
107 memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(esp_ip4_addr_t));
108#if LWIP_IPV6
109 ip_addr_.type = IPADDR_TYPE_V4;
110#endif
111 }
112 IPAddress(esp_ip_addr_t *other_ip) {
113#if LWIP_IPV6
114 memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(ip_addr_));
115#else
116 memcpy((void *) &ip_addr_, (void *) &other_ip->u_addr.ip4, sizeof(ip_addr_));
117#endif
118 }
119 operator esp_ip_addr_t() const {
120 esp_ip_addr_t tmp;
121#if LWIP_IPV6
122 memcpy((void *) &tmp, (void *) &ip_addr_, sizeof(ip_addr_));
123#else
124 memcpy((void *) &tmp.u_addr.ip4, (void *) &ip_addr_, sizeof(ip_addr_));
125#endif /* LWIP_IPV6 */
126 return tmp;
127 }
128 operator esp_ip4_addr_t() const {
129 esp_ip4_addr_t tmp;
130#if LWIP_IPV6
131 memcpy((void *) &tmp, (void *) &ip_addr_.u_addr.ip4, sizeof(esp_ip4_addr_t));
132#else
133 memcpy((void *) &tmp, (void *) &ip_addr_, sizeof(ip_addr_));
134#endif /* LWIP_IPV6 */
135 return tmp;
136 }
137#endif /* USE_ESP32 */
138
139 operator ip_addr_t() const { return ip_addr_; }
140#if LWIP_IPV6
141 operator ip4_addr_t() const { return *ip_2_ip4(&ip_addr_); }
142#endif /* LWIP_IPV6 */
143
144#if USE_ARDUINO
145 operator arduino_ns::IPAddress() const { return ip_addr_get_ip4_u32(&ip_addr_); }
146#endif
147
148 bool is_set() const { return !ip_addr_isany(&ip_addr_); } // NOLINT(readability-simplify-boolean-expr)
149 bool is_ip4() const { return IP_IS_V4(&ip_addr_); }
150 bool is_ip6() const { return IP_IS_V6(&ip_addr_); }
151 bool is_multicast() const { return ip_addr_ismulticast(&ip_addr_); }
152 // Remove before 2026.8.0
153 ESPDEPRECATED("Use str_to() instead. Removed in 2026.8.0", "2026.2.0")
154 std::string str() const {
155 char buf[IP_ADDRESS_BUFFER_SIZE];
156 this->str_to(buf);
157 return buf;
158 }
161 char *str_to(char *buf) const {
162 ipaddr_ntoa_r(&ip_addr_, buf, IP_ADDRESS_BUFFER_SIZE);
163 lowercase_ip_str(buf);
164 return buf;
165 }
166 bool operator==(const IPAddress &other) const { return ip_addr_cmp(&ip_addr_, &other.ip_addr_); }
167 bool operator!=(const IPAddress &other) const { return !ip_addr_cmp(&ip_addr_, &other.ip_addr_); }
168 IPAddress &operator+=(uint8_t increase) {
169 if (IP_IS_V4(&ip_addr_)) {
170#if LWIP_IPV6
171 (((u8_t *) (&ip_addr_.u_addr.ip4))[3]) += increase;
172#else
173 (((u8_t *) (&ip_addr_.addr))[3]) += increase;
174#endif /* LWIP_IPV6 */
175 }
176 return *this;
177 }
178#endif
179
180 protected:
182};
183
184using IPAddresses = std::array<IPAddress, 5>;
185
186} // namespace network
187} // namespace esphome
188#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:184
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:81
IPAddress(ip6_addr_t *other_ip)
Definition ip_address.h:93
IPAddress(esp_ip_addr_t *other_ip)
Definition ip_address.h:112
IPAddress(const ip_addr_t *other_ip)
Definition ip_address.h:62
IPAddress(esp_ip6_addr_t *other_ip)
Definition ip_address.h:101
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:106
IPAddress(ip4_addr_t *other_ip)
Definition ip_address.h:83
IPAddress(const std::string &in_address)
Definition ip_address.h:61
IPAddress(const arduino_ns::IPAddress &other_ip)
Definition ip_address.h:90
ESPDEPRECATED("Use str_to() instead. Removed in 2026.8.0", "2026.2.0") std
Definition ip_address.h:64