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