ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
bsd_sockets_impl.cpp
Go to the documentation of this file.
1#include "socket.h"
4
5#ifdef USE_SOCKET_IMPL_BSD_SOCKETS
6
7#include <cstring>
9
10#ifdef USE_ESP32
11#include <esp_idf_version.h>
12#include <lwip/sockets.h>
13#endif
14
15namespace esphome::socket {
16
17std::string format_sockaddr(const struct sockaddr_storage &storage) {
18 if (storage.ss_family == AF_INET) {
19 const struct sockaddr_in *addr = reinterpret_cast<const struct sockaddr_in *>(&storage);
20 char buf[INET_ADDRSTRLEN];
21 if (inet_ntop(AF_INET, &addr->sin_addr, buf, sizeof(buf)) != nullptr)
22 return std::string{buf};
23 }
24#if LWIP_IPV6
25 else if (storage.ss_family == AF_INET6) {
26 const struct sockaddr_in6 *addr = reinterpret_cast<const struct sockaddr_in6 *>(&storage);
27 char buf[INET6_ADDRSTRLEN];
28 // Format IPv4-mapped IPv6 addresses as regular IPv4 addresses
29 if (addr->sin6_addr.un.u32_addr[0] == 0 && addr->sin6_addr.un.u32_addr[1] == 0 &&
30 addr->sin6_addr.un.u32_addr[2] == htonl(0xFFFF) &&
31 inet_ntop(AF_INET, &addr->sin6_addr.un.u32_addr[3], buf, sizeof(buf)) != nullptr) {
32 return std::string{buf};
33 }
34 if (inet_ntop(AF_INET6, &addr->sin6_addr, buf, sizeof(buf)) != nullptr)
35 return std::string{buf};
36 }
37#endif
38 return {};
39}
40
41class BSDSocketImpl : public Socket {
42 public:
43 BSDSocketImpl(int fd, bool monitor_loop = false) : fd_(fd) {
44#ifdef USE_SOCKET_SELECT_SUPPORT
45 // Register new socket with the application for select() if monitoring requested
46 if (monitor_loop && this->fd_ >= 0) {
47 // Only set loop_monitored_ to true if registration succeeds
48 this->loop_monitored_ = App.register_socket_fd(this->fd_);
49 } else {
50 this->loop_monitored_ = false;
51 }
52#else
53 // Without select support, ignore monitor_loop parameter
54 (void) monitor_loop;
55#endif
56 }
57 ~BSDSocketImpl() override {
58 if (!this->closed_) {
59 this->close(); // NOLINT(clang-analyzer-optin.cplusplus.VirtualCall)
60 }
61 }
62 int connect(const struct sockaddr *addr, socklen_t addrlen) override { return ::connect(this->fd_, addr, addrlen); }
63 std::unique_ptr<Socket> accept(struct sockaddr *addr, socklen_t *addrlen) override {
64 int fd = ::accept(this->fd_, addr, addrlen);
65 if (fd == -1)
66 return {};
67 return make_unique<BSDSocketImpl>(fd, false);
68 }
69 std::unique_ptr<Socket> accept_loop_monitored(struct sockaddr *addr, socklen_t *addrlen) override {
70 int fd = ::accept(this->fd_, addr, addrlen);
71 if (fd == -1)
72 return {};
73 return make_unique<BSDSocketImpl>(fd, true);
74 }
75
76 int bind(const struct sockaddr *addr, socklen_t addrlen) override { return ::bind(this->fd_, addr, addrlen); }
77 int close() override {
78 if (!this->closed_) {
79#ifdef USE_SOCKET_SELECT_SUPPORT
80 // Unregister from select() before closing if monitored
81 if (this->loop_monitored_) {
82 App.unregister_socket_fd(this->fd_);
83 }
84#endif
85 int ret = ::close(this->fd_);
86 this->closed_ = true;
87 return ret;
88 }
89 return 0;
90 }
91 int shutdown(int how) override { return ::shutdown(this->fd_, how); }
92
93 int getpeername(struct sockaddr *addr, socklen_t *addrlen) override {
94 return ::getpeername(this->fd_, addr, addrlen);
95 }
96 std::string getpeername() override {
97 struct sockaddr_storage storage;
98 socklen_t len = sizeof(storage);
99 if (::getpeername(this->fd_, (struct sockaddr *) &storage, &len) != 0)
100 return {};
101 return format_sockaddr(storage);
102 }
103 int getsockname(struct sockaddr *addr, socklen_t *addrlen) override {
104 return ::getsockname(this->fd_, addr, addrlen);
105 }
106 std::string getsockname() override {
107 struct sockaddr_storage storage;
108 socklen_t len = sizeof(storage);
109 if (::getsockname(this->fd_, (struct sockaddr *) &storage, &len) != 0)
110 return {};
111 return format_sockaddr(storage);
112 }
113 int getsockopt(int level, int optname, void *optval, socklen_t *optlen) override {
114 return ::getsockopt(this->fd_, level, optname, optval, optlen);
115 }
116 int setsockopt(int level, int optname, const void *optval, socklen_t optlen) override {
117 return ::setsockopt(this->fd_, level, optname, optval, optlen);
118 }
119 int listen(int backlog) override { return ::listen(this->fd_, backlog); }
120 ssize_t read(void *buf, size_t len) override { return ::read(this->fd_, buf, len); }
121 ssize_t recvfrom(void *buf, size_t len, sockaddr *addr, socklen_t *addr_len) override {
122#if defined(USE_ESP32) || defined(USE_HOST)
123 return ::recvfrom(this->fd_, buf, len, 0, addr, addr_len);
124#else
125 return ::lwip_recvfrom(this->fd_, buf, len, 0, addr, addr_len);
126#endif
127 }
128 ssize_t readv(const struct iovec *iov, int iovcnt) override {
129#if defined(USE_ESP32)
130 return ::lwip_readv(this->fd_, iov, iovcnt);
131#else
132 return ::readv(this->fd_, iov, iovcnt);
133#endif
134 }
135 ssize_t write(const void *buf, size_t len) override { return ::write(this->fd_, buf, len); }
136 ssize_t send(void *buf, size_t len, int flags) { return ::send(this->fd_, buf, len, flags); }
137 ssize_t writev(const struct iovec *iov, int iovcnt) override {
138#if defined(USE_ESP32)
139 return ::lwip_writev(this->fd_, iov, iovcnt);
140#else
141 return ::writev(this->fd_, iov, iovcnt);
142#endif
143 }
144
145 ssize_t sendto(const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen) override {
146 return ::sendto(this->fd_, buf, len, flags, to, tolen); // NOLINT(readability-suspicious-call-argument)
147 }
148
149 int setblocking(bool blocking) override {
150 int fl = ::fcntl(this->fd_, F_GETFL, 0);
151 if (blocking) {
152 fl &= ~O_NONBLOCK;
153 } else {
154 fl |= O_NONBLOCK;
155 }
156 ::fcntl(this->fd_, F_SETFL, fl);
157 return 0;
158 }
159
160 int get_fd() const override { return this->fd_; }
161
162#ifdef USE_SOCKET_SELECT_SUPPORT
163 bool ready() const override {
164 if (!this->loop_monitored_)
165 return true;
166 return App.is_socket_ready(this->fd_);
167 }
168#endif
169
170 protected:
171 int fd_;
172 bool closed_{false};
173#ifdef USE_SOCKET_SELECT_SUPPORT
174 bool loop_monitored_{false};
175#endif
176};
177
178// Helper to create a socket with optional monitoring
179static std::unique_ptr<Socket> create_socket(int domain, int type, int protocol, bool loop_monitored = false) {
180 int ret = ::socket(domain, type, protocol);
181 if (ret == -1)
182 return nullptr;
183 return std::unique_ptr<Socket>{new BSDSocketImpl(ret, loop_monitored)};
184}
185
186std::unique_ptr<Socket> socket(int domain, int type, int protocol) {
187 return create_socket(domain, type, protocol, false);
188}
189
190std::unique_ptr<Socket> socket_loop_monitored(int domain, int type, int protocol) {
191 return create_socket(domain, type, protocol, true);
192}
193
194} // namespace esphome::socket
195
196#endif // USE_SOCKET_IMPL_BSD_SOCKETS
bool is_socket_ready(int fd) const
Check if there's data available on a socket without blocking This function is thread-safe for reading...
void unregister_socket_fd(int fd)
bool register_socket_fd(int fd)
Register/unregister a socket file descriptor to be monitored for read events.
uint16_t type
uint16_t flags
uint16_t addr_len
uint32_t socklen_t
Definition headers.h:97
__int64 ssize_t
Definition httplib.h:178
std::string format_sockaddr(const struct sockaddr_storage &storage)
std::unique_ptr< Socket > socket(int domain, int type, int protocol)
Create a socket of the given domain, type and protocol.
std::unique_ptr< Socket > socket_loop_monitored(int domain, int type, int protocol)
Create a socket and monitor it for data in the main loop.
std::string size_t len
Definition helpers.h:533
Application App
Global storage of Application pointer - only one Application can exist.
struct in6_addr sin6_addr
Definition headers.h:77
struct in_addr sin_addr
Definition headers.h:65
sa_family_t ss_family
Definition headers.h:92