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