ESPHome  2024.12.2
headers.h
Go to the documentation of this file.
1 #pragma once
2 #include "esphome/core/defines.h"
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 
24 #if LWIP_IPV6
25 #define AF_INET6 10
26 #define PF_INET6 AF_INET6
27 
28 #define IPPROTO_IPV6 41
29 #define IPPROTO_ICMPV6 58
30 #endif
31 
32 #define TCP_NODELAY 0x01
33 
34 #define F_GETFL 3
35 #define F_SETFL 4
36 
37 #ifdef O_NONBLOCK
38 #undef O_NONBLOCK
39 #endif
40 #define O_NONBLOCK 1
41 
42 #define SHUT_RD 0
43 #define SHUT_WR 1
44 #define SHUT_RDWR 2
45 
46 /* Socket protocol types (TCP/UDP/RAW) */
47 #define SOCK_STREAM 1
48 #define SOCK_DGRAM 2
49 #define SOCK_RAW 3
50 
51 #define SO_REUSEADDR 0x0004 /* Allow local address reuse */
52 #define SO_KEEPALIVE 0x0008 /* keep connections alive */
53 #define SO_BROADCAST 0x0020 /* permit to send and to receive broadcast messages (see IP_SOF_BROADCAST option) */
54 
55 #define SOL_SOCKET 0xfff /* options for socket level */
56 
57 using sa_family_t = uint8_t;
58 using in_port_t = uint16_t;
59 
60 // NOLINTNEXTLINE(readability-identifier-naming)
61 struct sockaddr_in {
62  uint8_t sin_len;
65  struct in_addr sin_addr;
66 #define SIN_ZERO_LEN 8
67  char sin_zero[SIN_ZERO_LEN];
68 };
69 
70 #if LWIP_IPV6
71 // NOLINTNEXTLINE(readability-identifier-naming)
72 struct sockaddr_in6 {
73  uint8_t sin6_len; /* length of this structure */
74  sa_family_t sin6_family; /* AF_INET6 */
75  in_port_t sin6_port; /* Transport layer port # */
76  uint32_t sin6_flowinfo; /* IPv6 flow information */
77  struct in6_addr sin6_addr; /* IPv6 address */
78  uint32_t sin6_scope_id; /* Set of interfaces for scope */
79 };
80 #endif
81 
82 // NOLINTNEXTLINE(readability-identifier-naming)
83 struct sockaddr {
84  uint8_t sa_len;
86  char sa_data[14];
87 };
88 
89 // NOLINTNEXTLINE(readability-identifier-naming)
91  uint8_t s2_len;
93  char s2_data1[2];
94  uint32_t s2_data2[3];
95  uint32_t s2_data3[3];
96 };
97 using socklen_t = uint32_t;
98 
99 // NOLINTNEXTLINE(readability-identifier-naming)
100 struct iovec {
101  void *iov_base;
102  size_t iov_len;
103 };
104 
105 #if defined(USE_ESP8266) || defined(USE_RP2040)
106 // arduino-esp8266 declares a global vars called INADDR_NONE/ANY which are invalid with the define
107 #ifdef INADDR_ANY
108 #undef INADDR_ANY
109 #endif
110 #ifdef INADDR_NONE
111 #undef INADDR_NONE
112 #endif
113 
114 #define ESPHOME_INADDR_ANY ((uint32_t) 0x00000000UL)
115 #define ESPHOME_INADDR_NONE ((uint32_t) 0xFFFFFFFFUL)
116 #else // !USE_ESP8266
117 #define ESPHOME_INADDR_ANY INADDR_ANY
118 #define ESPHOME_INADDR_NONE INADDR_NONE
119 #endif
120 
121 #endif // USE_SOCKET_IMPL_LWIP_TCP
122 
123 #ifdef USE_SOCKET_IMPL_LWIP_SOCKETS
124 
125 // standard lwIP's compatibility macros will interfere
126 // with Socket class function names - disable the macros
127 // and use real function names instead
128 #undef LWIP_COMPAT_SOCKETS
129 #define LWIP_COMPAT_SOCKETS 0
130 
131 #include "lwip/sockets.h"
132 #include <sys/types.h>
133 
134 #ifdef USE_ARDUINO
135 // arduino-esp32 declares a global var called INADDR_NONE which is replaced
136 // by the define
137 #ifdef INADDR_NONE
138 #undef INADDR_NONE
139 #endif
140 // not defined for ESP32
141 using socklen_t = uint32_t;
142 
143 #define ESPHOME_INADDR_ANY ((uint32_t) 0x00000000UL)
144 #define ESPHOME_INADDR_NONE ((uint32_t) 0xFFFFFFFFUL)
145 #else // !USE_ESP32
146 #define ESPHOME_INADDR_ANY INADDR_ANY
147 #define ESPHOME_INADDR_NONE INADDR_NONE
148 #endif
149 
150 #endif // USE_SOCKET_IMPL_LWIP_SOCKETS
151 
152 #ifdef USE_SOCKET_IMPL_BSD_SOCKETS
153 
154 #include <cstdint>
155 #include <fcntl.h>
156 #include <sys/ioctl.h>
157 #include <sys/socket.h>
158 #include <sys/types.h>
159 #include <sys/uio.h>
160 #include <unistd.h>
161 
162 #ifdef USE_HOST
163 #include <arpa/inet.h>
164 #include <netinet/in.h>
165 #include <netinet/ip.h>
166 #include <netinet/tcp.h>
167 #endif // USE_HOST
168 
169 #ifdef USE_ARDUINO
170 // arduino-esp32 declares a global var called INADDR_NONE which is replaced
171 // by the define
172 #ifdef INADDR_NONE
173 #undef INADDR_NONE
174 #endif
175 // not defined for ESP32
176 using socklen_t = uint32_t;
177 
178 #define ESPHOME_INADDR_ANY ((uint32_t) 0x00000000UL)
179 #define ESPHOME_INADDR_NONE ((uint32_t) 0xFFFFFFFFUL)
180 #else // !USE_ESP32
181 #define ESPHOME_INADDR_ANY INADDR_ANY
182 #define ESPHOME_INADDR_NONE INADDR_NONE
183 #endif
184 
185 #endif // USE_SOCKET_IMPL_BSD_SOCKETS
size_t iov_len
Definition: headers.h:102
void * iov_base
Definition: headers.h:101
sa_family_t sa_family
Definition: headers.h:85
uint32_t sin6_flowinfo
Definition: headers.h:76
uint8_t sin6_len
Definition: headers.h:73
sa_family_t ss_family
Definition: headers.h:92
uint32_t socklen_t
Definition: headers.h:97
sa_family_t sin_family
Definition: headers.h:63
uint16_t in_port_t
Definition: headers.h:58
in_port_t sin_port
Definition: headers.h:64
uint32_t sin6_scope_id
Definition: headers.h:78
uint8_t sa_len
Definition: headers.h:84
Definition: headers.h:100
struct in_addr sin_addr
Definition: headers.h:65
uint8_t s2_len
Definition: headers.h:91
uint8_t sa_family_t
Definition: headers.h:57
sa_family_t sin6_family
Definition: headers.h:74
uint8_t sin_len
Definition: headers.h:62
char sin_zero[SIN_ZERO_LEN]
Definition: headers.h:67
in_port_t sin6_port
Definition: headers.h:75