ESPHome 2025.5.0
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_ESP_IDF) || 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
43struct IPAddress {
44 public:
45#ifdef USE_HOST
46 IPAddress() { ip_addr_.s_addr = 0; }
47 IPAddress(uint8_t first, uint8_t second, uint8_t third, uint8_t fourth) {
48 this->ip_addr_.s_addr = htonl((first << 24) | (second << 16) | (third << 8) | fourth);
49 }
50 IPAddress(const std::string &in_address) { inet_aton(in_address.c_str(), &ip_addr_); }
51 IPAddress(const ip_addr_t *other_ip) { ip_addr_ = *other_ip; }
52 std::string str() const { return str_lower_case(inet_ntoa(ip_addr_)); }
53#else
54 IPAddress() { ip_addr_set_zero(&ip_addr_); }
55 IPAddress(uint8_t first, uint8_t second, uint8_t third, uint8_t fourth) {
56 IP_ADDR4(&ip_addr_, first, second, third, fourth);
57 }
58 IPAddress(const ip_addr_t *other_ip) { ip_addr_copy(ip_addr_, *other_ip); }
59 IPAddress(const std::string &in_address) { ipaddr_aton(in_address.c_str(), &ip_addr_); }
60 IPAddress(ip4_addr_t *other_ip) {
61 memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(ip4_addr_t));
62#if USE_ESP32 && LWIP_IPV6
63 ip_addr_.type = IPADDR_TYPE_V4;
64#endif
65 }
66#if USE_ARDUINO
67 IPAddress(const arduino_ns::IPAddress &other_ip) { ip_addr_set_ip4_u32(&ip_addr_, other_ip); }
68#endif
69#if LWIP_IPV6
70 IPAddress(ip6_addr_t *other_ip) {
71 memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(ip6_addr_t));
72 ip_addr_.type = IPADDR_TYPE_V6;
73 }
74#endif /* LWIP_IPV6 */
75
76#ifdef USE_ESP32
77#if LWIP_IPV6
78 IPAddress(esp_ip6_addr_t *other_ip) {
79 memcpy((void *) &ip_addr_.u_addr.ip6, (void *) other_ip, sizeof(esp_ip6_addr_t));
80 ip_addr_.type = IPADDR_TYPE_V6;
81 }
82#endif /* LWIP_IPV6 */
83 IPAddress(esp_ip4_addr_t *other_ip) { memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(esp_ip4_addr_t)); }
84 IPAddress(esp_ip_addr_t *other_ip) {
85#if LWIP_IPV6
86 memcpy((void *) &ip_addr_, (void *) other_ip, sizeof(ip_addr_));
87#else
88 memcpy((void *) &ip_addr_, (void *) &other_ip->u_addr.ip4, sizeof(ip_addr_));
89#endif
90 }
91 operator esp_ip_addr_t() const {
92 esp_ip_addr_t tmp;
93#if LWIP_IPV6
94 memcpy((void *) &tmp, (void *) &ip_addr_, sizeof(ip_addr_));
95#else
96 memcpy((void *) &tmp.u_addr.ip4, (void *) &ip_addr_, sizeof(ip_addr_));
97#endif /* LWIP_IPV6 */
98 return tmp;
99 }
100 operator esp_ip4_addr_t() const {
101 esp_ip4_addr_t tmp;
102#if LWIP_IPV6
103 memcpy((void *) &tmp, (void *) &ip_addr_.u_addr.ip4, sizeof(esp_ip4_addr_t));
104#else
105 memcpy((void *) &tmp, (void *) &ip_addr_, sizeof(ip_addr_));
106#endif /* LWIP_IPV6 */
107 return tmp;
108 }
109#endif /* USE_ESP32 */
110
111 operator ip_addr_t() const { return ip_addr_; }
112#if LWIP_IPV6
113 operator ip4_addr_t() const { return *ip_2_ip4(&ip_addr_); }
114#endif /* LWIP_IPV6 */
115
116#if USE_ARDUINO
117 operator arduino_ns::IPAddress() const { return ip_addr_get_ip4_u32(&ip_addr_); }
118#endif
119
120 bool is_set() { return !ip_addr_isany(&ip_addr_); } // NOLINT(readability-simplify-boolean-expr)
121 bool is_ip4() { return IP_IS_V4(&ip_addr_); }
122 bool is_ip6() { return IP_IS_V6(&ip_addr_); }
123 bool is_multicast() { return ip_addr_ismulticast(&ip_addr_); }
124 std::string str() const { return str_lower_case(ipaddr_ntoa(&ip_addr_)); }
125 bool operator==(const IPAddress &other) const { return ip_addr_cmp(&ip_addr_, &other.ip_addr_); }
126 bool operator!=(const IPAddress &other) const { return !ip_addr_cmp(&ip_addr_, &other.ip_addr_); }
127 IPAddress &operator+=(uint8_t increase) {
128 if (IP_IS_V4(&ip_addr_)) {
129#if LWIP_IPV6
130 (((u8_t *) (&ip_addr_.u_addr.ip4))[3]) += increase;
131#else
132 (((u8_t *) (&ip_addr_.addr))[3]) += increase;
133#endif /* LWIP_IPV6 */
134 }
135 return *this;
136 }
137#endif
138
139 protected:
141};
142
143using IPAddresses = std::array<IPAddress, 5>;
144
145} // namespace network
146} // namespace esphome
147#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:143
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:290
IPAddress(ip6_addr_t *other_ip)
Definition ip_address.h:70
bool operator!=(const IPAddress &other) const
Definition ip_address.h:126
bool operator==(const IPAddress &other) const
Definition ip_address.h:125
std::string str() const
Definition ip_address.h:52
IPAddress(esp_ip_addr_t *other_ip)
Definition ip_address.h:84
IPAddress(const ip_addr_t *other_ip)
Definition ip_address.h:51
IPAddress(esp_ip6_addr_t *other_ip)
Definition ip_address.h:78
IPAddress(uint8_t first, uint8_t second, uint8_t third, uint8_t fourth)
Definition ip_address.h:47
IPAddress & operator+=(uint8_t increase)
Definition ip_address.h:127
IPAddress(esp_ip4_addr_t *other_ip)
Definition ip_address.h:83
IPAddress(ip4_addr_t *other_ip)
Definition ip_address.h:60
IPAddress(const std::string &in_address)
Definition ip_address.h:50
IPAddress(const arduino_ns::IPAddress &other_ip)
Definition ip_address.h:67