ESPHome  2022.11.3
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 AF_INET6 10
19 #define PF_INET AF_INET
20 #define PF_INET6 AF_INET6
21 #define PF_UNSPEC AF_UNSPEC
22 #define IPPROTO_IP 0
23 #define IPPROTO_TCP 6
24 #define IPPROTO_IPV6 41
25 #define IPPROTO_ICMPV6 58
26 
27 #define TCP_NODELAY 0x01
28 
29 #define F_GETFL 3
30 #define F_SETFL 4
31 #define O_NONBLOCK 1
32 
33 #define SHUT_RD 0
34 #define SHUT_WR 1
35 #define SHUT_RDWR 2
36 
37 /* Socket protocol types (TCP/UDP/RAW) */
38 #define SOCK_STREAM 1
39 #define SOCK_DGRAM 2
40 #define SOCK_RAW 3
41 
42 #define SO_REUSEADDR 0x0004 /* Allow local address reuse */
43 #define SO_KEEPALIVE 0x0008 /* keep connections alive */
44 #define SO_BROADCAST 0x0020 /* permit to send and to receive broadcast messages (see IP_SOF_BROADCAST option) */
45 
46 #define SOL_SOCKET 0xfff /* options for socket level */
47 
48 using sa_family_t = uint8_t;
49 using in_port_t = uint16_t;
50 
51 // NOLINTNEXTLINE(readability-identifier-naming)
52 struct sockaddr_in {
53  uint8_t sin_len;
56  struct in_addr sin_addr;
57 #define SIN_ZERO_LEN 8
58  char sin_zero[SIN_ZERO_LEN];
59 };
60 
61 // NOLINTNEXTLINE(readability-identifier-naming)
62 struct sockaddr_in6 {
63  uint8_t sin6_len; /* length of this structure */
64  sa_family_t sin6_family; /* AF_INET6 */
65  in_port_t sin6_port; /* Transport layer port # */
66  uint32_t sin6_flowinfo; /* IPv6 flow information */
67  struct in6_addr sin6_addr; /* IPv6 address */
68  uint32_t sin6_scope_id; /* Set of interfaces for scope */
69 };
70 
71 // NOLINTNEXTLINE(readability-identifier-naming)
72 struct sockaddr {
73  uint8_t sa_len;
75  char sa_data[14];
76 };
77 
78 // NOLINTNEXTLINE(readability-identifier-naming)
80  uint8_t s2_len;
82  char s2_data1[2];
83  uint32_t s2_data2[3];
84  uint32_t s2_data3[3];
85 };
86 using socklen_t = uint32_t;
87 
88 // NOLINTNEXTLINE(readability-identifier-naming)
89 struct iovec {
90  void *iov_base;
91  size_t iov_len;
92 };
93 
94 #if defined(USE_ESP8266) || defined(USE_RP2040)
95 // arduino-esp8266 declares a global vars called INADDR_NONE/ANY which are invalid with the define
96 #ifdef INADDR_ANY
97 #undef INADDR_ANY
98 #endif
99 #ifdef INADDR_NONE
100 #undef INADDR_NONE
101 #endif
102 
103 #define ESPHOME_INADDR_ANY ((uint32_t) 0x00000000UL)
104 #define ESPHOME_INADDR_NONE ((uint32_t) 0xFFFFFFFFUL)
105 #else // !USE_ESP8266
106 #define ESPHOME_INADDR_ANY INADDR_ANY
107 #define ESPHOME_INADDR_NONE INADDR_NONE
108 #endif
109 
110 #endif // USE_SOCKET_IMPL_LWIP_TCP
111 
112 #ifdef USE_SOCKET_IMPL_BSD_SOCKETS
113 
114 #include <cstdint>
115 #include <fcntl.h>
116 #include <sys/ioctl.h>
117 #include <sys/socket.h>
118 #include <sys/types.h>
119 #include <sys/uio.h>
120 #include <unistd.h>
121 
122 #ifdef USE_ARDUINO
123 // arduino-esp32 declares a global var called INADDR_NONE which is replaced
124 // by the define
125 #ifdef INADDR_NONE
126 #undef INADDR_NONE
127 #endif
128 // not defined for ESP32
129 using socklen_t = uint32_t;
130 
131 #define ESPHOME_INADDR_ANY ((uint32_t) 0x00000000UL)
132 #define ESPHOME_INADDR_NONE ((uint32_t) 0xFFFFFFFFUL)
133 #else // !USE_ESP32
134 #define ESPHOME_INADDR_ANY INADDR_ANY
135 #define ESPHOME_INADDR_NONE INADDR_NONE
136 #endif
137 
138 #endif // USE_SOCKET_IMPL_BSD_SOCKETS
size_t iov_len
Definition: headers.h:91
void * iov_base
Definition: headers.h:90
sa_family_t sa_family
Definition: headers.h:74
uint32_t sin6_flowinfo
Definition: headers.h:66
uint8_t sin6_len
Definition: headers.h:63
sa_family_t ss_family
Definition: headers.h:81
uint32_t socklen_t
Definition: headers.h:86
sa_family_t sin_family
Definition: headers.h:54
uint16_t in_port_t
Definition: headers.h:49
in_port_t sin_port
Definition: headers.h:55
uint32_t sin6_scope_id
Definition: headers.h:68
uint8_t sa_len
Definition: headers.h:73
Definition: headers.h:89
struct in_addr sin_addr
Definition: headers.h:56
uint8_t s2_len
Definition: headers.h:80
uint8_t sa_family_t
Definition: headers.h:48
sa_family_t sin6_family
Definition: headers.h:64
uint8_t sin_len
Definition: headers.h:53
char sin_zero[SIN_ZERO_LEN]
Definition: headers.h:58
in_port_t sin6_port
Definition: headers.h:65