ESPHome 2026.3.0
Loading...
Searching...
No Matches
lwip_sockets_impl.cpp
Go to the documentation of this file.
3#include "socket.h"
4
5#ifdef USE_SOCKET_IMPL_LWIP_SOCKETS
6
7#include <cstring>
9
10namespace esphome::socket {
11
12LwIPSocketImpl::LwIPSocketImpl(int fd, bool monitor_loop) {
13 this->fd_ = fd;
14 if (!monitor_loop || this->fd_ < 0)
15 return;
16#ifdef USE_LWIP_FAST_SELECT
17 // Cache lwip_sock pointer and register for monitoring (hooks callback internally)
20#else
22#endif
23}
24
26 if (!this->closed_) {
27 this->close();
28 }
29}
30
32 if (!this->closed_) {
33 // Unregister before closing to avoid dangling pointer in monitored set
34#ifdef USE_LWIP_FAST_SELECT
35 if (this->loop_monitored_) {
37 this->cached_sock_ = nullptr;
38 }
39#else
40 if (this->loop_monitored_) {
42 }
43#endif
44 int ret = lwip_close(this->fd_);
45 this->closed_ = true;
46 return ret;
47 }
48 return 0;
49}
50
51int LwIPSocketImpl::setblocking(bool blocking) {
52 int fl = lwip_fcntl(this->fd_, F_GETFL, 0);
53 if (blocking) {
54 fl &= ~O_NONBLOCK;
55 } else {
56 fl |= O_NONBLOCK;
57 }
58 lwip_fcntl(this->fd_, F_SETFL, fl);
59 return 0;
60}
61
62size_t LwIPSocketImpl::getpeername_to(std::span<char, SOCKADDR_STR_LEN> buf) {
63 struct sockaddr_storage storage;
64 socklen_t len = sizeof(storage);
65 if (this->getpeername(reinterpret_cast<struct sockaddr *>(&storage), &len) != 0) {
66 buf[0] = '\0';
67 return 0;
68 }
69 return format_sockaddr_to(reinterpret_cast<struct sockaddr *>(&storage), len, buf);
70}
71
72size_t LwIPSocketImpl::getsockname_to(std::span<char, SOCKADDR_STR_LEN> buf) {
73 struct sockaddr_storage storage;
74 socklen_t len = sizeof(storage);
75 if (this->getsockname(reinterpret_cast<struct sockaddr *>(&storage), &len) != 0) {
76 buf[0] = '\0';
77 return 0;
78 }
79 return format_sockaddr_to(reinterpret_cast<struct sockaddr *>(&storage), len, buf);
80}
81
82// Helper to create a socket with optional monitoring
83static std::unique_ptr<LwIPSocketImpl> create_socket(int domain, int type, int protocol, bool loop_monitored = false) {
84 int ret = lwip_socket(domain, type, protocol);
85 if (ret == -1)
86 return nullptr;
87 return make_unique<LwIPSocketImpl>(ret, loop_monitored);
88}
89
90std::unique_ptr<Socket> socket(int domain, int type, int protocol) {
91 return create_socket(domain, type, protocol, false);
92}
93
94std::unique_ptr<Socket> socket_loop_monitored(int domain, int type, int protocol) {
95 return create_socket(domain, type, protocol, true);
96}
97
98} // namespace esphome::socket
99
100#endif // USE_SOCKET_IMPL_LWIP_SOCKETS
bool register_socket(struct lwip_sock *sock)
Register/unregister a socket to be monitored for read events.
void unregister_socket_fd(int fd)
bool register_socket_fd(int fd)
Fallback select() path: monitors file descriptors.
void unregister_socket(struct lwip_sock *sock)
int getsockname(struct sockaddr *addr, socklen_t *addrlen)
size_t getsockname_to(std::span< char, SOCKADDR_STR_LEN > buf)
Format local address into a fixed-size buffer (no heap allocation)
LwIPSocketImpl(int fd, bool monitor_loop=false)
int getpeername(struct sockaddr *addr, socklen_t *addrlen)
size_t getpeername_to(std::span< char, SOCKADDR_STR_LEN > buf)
Format peer address into a fixed-size buffer (no heap allocation)
uint16_t type
uint32_t socklen_t
Definition headers.h:99
struct lwip_sock * esphome_lwip_get_sock(int fd)
Look up a LwIP socket struct from a file descriptor.
size_t format_sockaddr_to(const struct sockaddr *addr_ptr, socklen_t len, std::span< char, SOCKADDR_STR_LEN > buf)
Format sockaddr into caller-provided buffer, returns length written (excluding null)
Definition socket.cpp:53
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:892
Application App
Global storage of Application pointer - only one Application can exist.