ESPHome 2026.1.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
40namespace esphome {
41namespace network {
42
44static constexpr size_t IP_ADDRESS_BUFFER_SIZE = 40;
45
46struct IPAddress {
47 public:
48#ifdef USE_HOST
49 IPAddress() { ip_addr_.s_addr = 0; }
50 IPAddress(uint8_t first, uint8_t second, uint8_t third, uint8_t fourth) {
51 this->ip_addr_.s_addr = htonl((first << 24) | (second << 16) | (third << 8) | fourth);
52 }
53 IPAddress(const std::string &in_address) { inet_aton(in_address.c_str(), &ip_addr_); }
54 IPAddress(const ip_addr_t *other_ip) { ip_addr_ = *other_ip; }
55 std::string str() const { return str_lower_case(inet_ntoa(ip_addr_)); }
57 char *str_to(char *buf) const {
58 return const_cast<char *>(inet_ntop(AF_INET, &ip_addr_, buf, IP_ADDRESS_BUFFER_SIZE));
59 }
60#else
61 IPAddress() { ip_addr_set_zero(&ip_addr_); }
62 IPAddress(uint8_t first, uint8_t second, uint8_t third, uint8_t fourth) {
63 IP_ADDR4(&ip_addr_, first, second, third, fourth);
64 }
65 IPAddress(const ip_addr_t *other_ip) { ip_addr_copy(ip_addr_, *other_ip); }
66 IPAddress(const char *in_address) { ipaddr_aton(in_address, &ip_addr_); }
67 IPAddress(const std::string &in_address) { ipaddr_aton(in_address.c_str(), &ip_addr_); }
68 IPAddress(ip4_addr_t *other_ip) {
69 memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(ip4_addr_t));
70#if USE_ESP32 && LWIP_IPV6
71 ip_addr_.type = IPADDR_TYPE_V4;
72#endif
73 }
74#if USE_ARDUINO
75 IPAddress(const arduino_ns::IPAddress &other_ip) { ip_addr_set_ip4_u32(&ip_addr_, other_ip); }
76#endif
77#if LWIP_IPV6
78 IPAddress(ip6_addr_t *other_ip) {
79 memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(ip6_addr_t));
80 ip_addr_.type = IPADDR_TYPE_V6;
81 }
82#endif /* LWIP_IPV6 */
83
84#ifdef USE_ESP32
85#if LWIP_IPV6
86 IPAddress(esp_ip6_addr_t *other_ip) {
87 memcpy((void *) &ip_addr_.u_addr.ip6, (void *) other_ip, sizeof(esp_ip6_addr_t));
88 ip_addr_.type = IPADDR_TYPE_V6;
89 }
90#endif /* LWIP_IPV6 */
91 IPAddress(esp_ip4_addr_t *other_ip) {
92 memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(esp_ip4_addr_t));
93#if LWIP_IPV6
94 ip_addr_.type = IPADDR_TYPE_V4;
95#endif
96 }
97 IPAddress(esp_ip_addr_t *other_ip) {
98#if LWIP_IPV6
99 memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(ip_addr_));
100#else
101 memcpy((void *) &ip_addr_, (void *) &other_ip->u_addr.ip4, sizeof(ip_addr_));
102#endif
103 }
104 operator esp_ip_addr_t() const {
105 esp_ip_addr_t tmp;
106#if LWIP_IPV6
107 memcpy((void *) &tmp, (void *) &ip_addr_, sizeof(ip_addr_));
108#else
109 memcpy((void *) &tmp.u_addr.ip4, (void *) &ip_addr_, sizeof(ip_addr_));
110#endif /* LWIP_IPV6 */
111 return tmp;
112 }
113 operator esp_ip4_addr_t() const {
114 esp_ip4_addr_t tmp;
115#if LWIP_IPV6
116 memcpy((void *) &tmp, (void *) &ip_addr_.u_addr.ip4, sizeof(esp_ip4_addr_t));
117#else
118 memcpy((void *) &tmp, (void *) &ip_addr_, sizeof(ip_addr_));
119#endif /* LWIP_IPV6 */
120 return tmp;
121 }
122#endif /* USE_ESP32 */
123
124 operator ip_addr_t() const { return ip_addr_; }
125#if LWIP_IPV6
126 operator ip4_addr_t() const { return *ip_2_ip4(&ip_addr_); }
127#endif /* LWIP_IPV6 */
128
129#if USE_ARDUINO
130 operator arduino_ns::IPAddress() const { return ip_addr_get_ip4_u32(&ip_addr_); }
131#endif
132
133 bool is_set() const { return !ip_addr_isany(&ip_addr_); } // NOLINT(readability-simplify-boolean-expr)
134 bool is_ip4() const { return IP_IS_V4(&ip_addr_); }
135 bool is_ip6() const { return IP_IS_V6(&ip_addr_); }
136 bool is_multicast() const { return ip_addr_ismulticast(&ip_addr_); }
137 std::string str() const { return str_lower_case(ipaddr_ntoa(&ip_addr_)); }
139 char *str_to(char *buf) const { return ipaddr_ntoa_r(&ip_addr_, buf, IP_ADDRESS_BUFFER_SIZE); }
140 bool operator==(const IPAddress &other) const { return ip_addr_cmp(&ip_addr_, &other.ip_addr_); }
141 bool operator!=(const IPAddress &other) const { return !ip_addr_cmp(&ip_addr_, &other.ip_addr_); }
142 IPAddress &operator+=(uint8_t increase) {
143 if (IP_IS_V4(&ip_addr_)) {
144#if LWIP_IPV6
145 (((u8_t *) (&ip_addr_.u_addr.ip4))[3]) += increase;
146#else
147 (((u8_t *) (&ip_addr_.addr))[3]) += increase;
148#endif /* LWIP_IPV6 */
149 }
150 return *this;
151 }
152#endif
153
154 protected:
156};
157
158using IPAddresses = std::array<IPAddress, 5>;
159
160} // namespace network
161} // namespace esphome
162#endif
uint8_t second
in_addr ip_addr_t
Definition ip_address.h:22
in_addr ip4_addr_t
Definition ip_address.h:23
std::array< IPAddress, 5 > IPAddresses
Definition ip_address.h:158
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string str_lower_case(const std::string &str)
Convert the string to lower case.
Definition helpers.cpp:190
IPAddress(const char *in_address)
Definition ip_address.h:66
IPAddress(ip6_addr_t *other_ip)
Definition ip_address.h:78
bool operator!=(const IPAddress &other) const
Definition ip_address.h:141
bool operator==(const IPAddress &other) const
Definition ip_address.h:140
std::string str() const
Definition ip_address.h:55
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:57
IPAddress(esp_ip_addr_t *other_ip)
Definition ip_address.h:97
IPAddress(const ip_addr_t *other_ip)
Definition ip_address.h:54
IPAddress(esp_ip6_addr_t *other_ip)
Definition ip_address.h:86
IPAddress(uint8_t first, uint8_t second, uint8_t third, uint8_t fourth)
Definition ip_address.h:50
IPAddress & operator+=(uint8_t increase)
Definition ip_address.h:142
IPAddress(esp_ip4_addr_t *other_ip)
Definition ip_address.h:91
IPAddress(ip4_addr_t *other_ip)
Definition ip_address.h:68
IPAddress(const std::string &in_address)
Definition ip_address.h:53
IPAddress(const arduino_ns::IPAddress &other_ip)
Definition ip_address.h:75