ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
headers.h
Go to the documentation of this file.
1#pragma once
3
4// Helper file to include all socket-related system headers (or use our own
5// definitions where system ones don't exist)
6
7#ifdef USE_SOCKET_IMPL_LWIP_TCP
8
9#define LWIP_INTERNAL
10#include "lwip/inet.h"
11#include <cerrno>
12#include <cstdint>
13#include <sys/types.h>
14
15/* Address families. */
16#define AF_UNSPEC 0
17#define AF_INET 2
18#define PF_INET AF_INET
19#define PF_UNSPEC AF_UNSPEC
20
21#define IPPROTO_IP 0
22#define IPPROTO_TCP 6
23#define IPPROTO_UDP 17
24
25#define IP_ADD_MEMBERSHIP 3
26#define IP_DROP_MEMBERSHIP 4
27
28// NOLINTNEXTLINE(readability-identifier-naming)
29struct ip_mreq {
30 struct in_addr imr_multiaddr;
31 struct in_addr imr_interface;
32};
33
34#if LWIP_IPV6
35#define AF_INET6 10
36#define PF_INET6 AF_INET6
37
38#define IPPROTO_IPV6 41
39#define IPPROTO_ICMPV6 58
40#endif
41
42#define TCP_NODELAY 0x01
43
44#define F_GETFL 3
45#define F_SETFL 4
46
47#ifdef O_NONBLOCK
48#undef O_NONBLOCK
49#endif
50#define O_NONBLOCK 1
51
52#define SHUT_RD 0
53#define SHUT_WR 1
54#define SHUT_RDWR 2
55
56/* Socket protocol types (TCP/UDP/RAW) */
57#define SOCK_STREAM 1
58#define SOCK_DGRAM 2
59#define SOCK_RAW 3
60
61#define SO_REUSEADDR 0x0004 /* Allow local address reuse */
62#define SO_KEEPALIVE 0x0008 /* keep connections alive */
63#define SO_BROADCAST 0x0020 /* permit to send and to receive broadcast messages (see IP_SOF_BROADCAST option) */
64#define SO_RCVTIMEO 0x1006 /* receive timeout */
65#define SO_SNDTIMEO 0x1005 /* send timeout */
66
67#define SOL_SOCKET 0xfff /* options for socket level */
68
69using sa_family_t = uint8_t;
70using in_port_t = uint16_t;
71
72// NOLINTNEXTLINE(readability-identifier-naming)
74 uint8_t sin_len;
77 struct in_addr sin_addr;
78#define SIN_ZERO_LEN 8
79 char sin_zero[SIN_ZERO_LEN];
80};
81
82#if LWIP_IPV6
83// NOLINTNEXTLINE(readability-identifier-naming)
85 uint8_t sin6_len; /* length of this structure */
86 sa_family_t sin6_family; /* AF_INET6 */
87 in_port_t sin6_port; /* Transport layer port # */
88 uint32_t sin6_flowinfo; /* IPv6 flow information */
89 struct in6_addr sin6_addr; /* IPv6 address */
90 uint32_t sin6_scope_id; /* Set of interfaces for scope */
91};
92#endif
93
94// NOLINTNEXTLINE(readability-identifier-naming)
95struct sockaddr {
96 uint8_t sa_len;
98 char sa_data[14];
99};
100
101// NOLINTNEXTLINE(readability-identifier-naming)
110
111// NOLINTNEXTLINE(readability-identifier-naming)
112struct iovec {
113 void *iov_base;
114 size_t iov_len;
115};
116
117#if defined(USE_ESP8266) || defined(USE_RP2)
118// arduino-esp8266 declares a global vars called INADDR_NONE/ANY which are invalid with the define
119#ifdef INADDR_ANY
120#undef INADDR_ANY
121#endif
122#ifdef INADDR_NONE
123#undef INADDR_NONE
124#endif
125
126#define ESPHOME_INADDR_ANY ((uint32_t) 0x00000000UL)
127#define ESPHOME_INADDR_NONE ((uint32_t) 0xFFFFFFFFUL)
128#else // !USE_ESP8266
129#define ESPHOME_INADDR_ANY INADDR_ANY
130#define ESPHOME_INADDR_NONE INADDR_NONE
131#endif
132
133#endif // USE_SOCKET_IMPL_LWIP_TCP
134
135#ifdef USE_SOCKET_IMPL_LWIP_SOCKETS
136
137// standard lwIP's compatibility macros will interfere
138// with Socket class function names - disable the macros
139// and use real function names instead
140#undef LWIP_COMPAT_SOCKETS
141#define LWIP_COMPAT_SOCKETS 0
142
143#include "lwip/sockets.h"
144#include <sys/types.h>
145
146#ifdef USE_ARDUINO
147// arduino-esp32 declares a global var called INADDR_NONE which is replaced
148// by the define
149#ifdef INADDR_NONE
150#undef INADDR_NONE
151#endif
152// not defined for ESP32
153using socklen_t = uint32_t;
154
155#define ESPHOME_INADDR_ANY ((uint32_t) 0x00000000UL)
156#define ESPHOME_INADDR_NONE ((uint32_t) 0xFFFFFFFFUL)
157#else // !USE_ESP32
158#define ESPHOME_INADDR_ANY INADDR_ANY
159#define ESPHOME_INADDR_NONE INADDR_NONE
160#endif
161
162#endif // USE_SOCKET_IMPL_LWIP_SOCKETS
163
164#ifdef USE_SOCKET_IMPL_BSD_SOCKETS
165
166#include <cstdint>
167#include <fcntl.h>
168#include <sys/ioctl.h>
169#include <sys/socket.h>
170#include <sys/types.h>
171#ifndef USE_ZEPHYR
172#include <sys/uio.h>
173#endif
174#include <unistd.h>
175
176#ifdef USE_HOST
177#include <arpa/inet.h>
178#include <netinet/in.h>
179#include <netinet/ip.h>
180#include <netinet/tcp.h>
181#endif // USE_HOST
182#ifdef USE_ZEPHYR
183#include <arpa/inet.h>
184#include <netinet/in.h>
185#endif // USE_ZEPHYR
186
187#ifdef USE_ARDUINO
188// arduino-esp32 declares a global var called INADDR_NONE which is replaced
189// by the define
190#ifdef INADDR_NONE
191#undef INADDR_NONE
192#endif
193// not defined for ESP32
194using socklen_t = uint32_t;
195
196#define ESPHOME_INADDR_ANY ((uint32_t) 0x00000000UL)
197#define ESPHOME_INADDR_NONE ((uint32_t) 0xFFFFFFFFUL)
198#else // !USE_ESP32
199#define ESPHOME_INADDR_ANY INADDR_ANY
200#define ESPHOME_INADDR_NONE INADDR_NONE
201#endif
202
203#endif // USE_SOCKET_IMPL_BSD_SOCKETS
204
205#if defined(USE_SOCKET_IMPL_LWIP_TCP) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || defined(USE_SOCKET_IMPL_BSD_SOCKETS)
206
207// Build tests compile socket without network, which normally defines this
208#ifndef USE_NETWORK_IPV4
209#define USE_NETWORK_IPV4 1
210#endif
211
212namespace esphome::socket {
213
214// Maximum length for formatted socket address string (IP address without port)
215// IPv4: "255.255.255.255" = 15 chars + null = 16
216// IPv6: full address = 45 chars + null = 46
217#if USE_NETWORK_IPV6
218static constexpr size_t SOCKADDR_STR_LEN = 46; // INET6_ADDRSTRLEN
219#else
220static constexpr size_t SOCKADDR_STR_LEN = 16; // INET_ADDRSTRLEN
221#endif
222
229
230} // namespace esphome::socket
231
232#endif
uint16_t in_port_t
Definition headers.h:70
uint32_t socklen_t
Definition headers.h:109
uint8_t sa_family_t
Definition headers.h:69
ConnectPollResult
Outcome of polling a non-blocking connect(); see socket::poll_connect().
Definition headers.h:224
static void uint32_t
void * iov_base
Definition headers.h:113
size_t iov_len
Definition headers.h:114
struct in_addr imr_interface
Definition headers.h:31
struct in_addr imr_multiaddr
Definition headers.h:30
uint8_t sin6_len
Definition headers.h:85
in_port_t sin6_port
Definition headers.h:87
struct in6_addr sin6_addr
Definition headers.h:89
uint32_t sin6_flowinfo
Definition headers.h:88
uint32_t sin6_scope_id
Definition headers.h:90
sa_family_t sin6_family
Definition headers.h:86
struct in_addr sin_addr
Definition headers.h:77
uint8_t sin_len
Definition headers.h:74
char sin_zero[SIN_ZERO_LEN]
Definition headers.h:79
sa_family_t sin_family
Definition headers.h:75
in_port_t sin_port
Definition headers.h:76
sa_family_t ss_family
Definition headers.h:104
uint32_t s2_data3[3]
Definition headers.h:107
char s2_data1[2]
Definition headers.h:105
uint32_t s2_data2[3]
Definition headers.h:106
uint8_t s2_len
Definition headers.h:103
uint8_t sa_len
Definition headers.h:96
sa_family_t sa_family
Definition headers.h:97
char sa_data[14]
Definition headers.h:98