ESPHome 2026.3.0-dev
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 // Register new socket with the application for select() if monitoring requested
15 if (monitor_loop && this->fd_ >= 0) {
16 // Only set loop_monitored_ to true if registration succeeds
18 }
19}
20
22 if (!this->closed_) {
23 this->close();
24 }
25}
26
28 if (!this->closed_) {
29 // Unregister from select() before closing if monitored
30 if (this->loop_monitored_) {
32 }
33 int ret = lwip_close(this->fd_);
34 this->closed_ = true;
35 return ret;
36 }
37 return 0;
38}
39
40int LwIPSocketImpl::setblocking(bool blocking) {
41 int fl = lwip_fcntl(this->fd_, F_GETFL, 0);
42 if (blocking) {
43 fl &= ~O_NONBLOCK;
44 } else {
45 fl |= O_NONBLOCK;
46 }
47 lwip_fcntl(this->fd_, F_SETFL, fl);
48 return 0;
49}
50
51bool LwIPSocketImpl::ready() const { return socket_ready_fd(this->fd_, this->loop_monitored_); }
52
53size_t LwIPSocketImpl::getpeername_to(std::span<char, SOCKADDR_STR_LEN> buf) {
54 struct sockaddr_storage storage;
55 socklen_t len = sizeof(storage);
56 if (this->getpeername(reinterpret_cast<struct sockaddr *>(&storage), &len) != 0) {
57 buf[0] = '\0';
58 return 0;
59 }
60 return format_sockaddr_to(reinterpret_cast<struct sockaddr *>(&storage), len, buf);
61}
62
63size_t LwIPSocketImpl::getsockname_to(std::span<char, SOCKADDR_STR_LEN> buf) {
64 struct sockaddr_storage storage;
65 socklen_t len = sizeof(storage);
66 if (this->getsockname(reinterpret_cast<struct sockaddr *>(&storage), &len) != 0) {
67 buf[0] = '\0';
68 return 0;
69 }
70 return format_sockaddr_to(reinterpret_cast<struct sockaddr *>(&storage), len, buf);
71}
72
73// Helper to create a socket with optional monitoring
74static std::unique_ptr<LwIPSocketImpl> create_socket(int domain, int type, int protocol, bool loop_monitored = false) {
75 int ret = lwip_socket(domain, type, protocol);
76 if (ret == -1)
77 return nullptr;
78 return make_unique<LwIPSocketImpl>(ret, loop_monitored);
79}
80
81std::unique_ptr<Socket> socket(int domain, int type, int protocol) {
82 return create_socket(domain, type, protocol, false);
83}
84
85std::unique_ptr<Socket> socket_loop_monitored(int domain, int type, int protocol) {
86 return create_socket(domain, type, protocol, true);
87}
88
89std::unique_ptr<ListenSocket> socket_listen(int domain, int type, int protocol) {
90 return create_socket(domain, type, protocol, false);
91}
92
93std::unique_ptr<ListenSocket> socket_listen_loop_monitored(int domain, int type, int protocol) {
94 return create_socket(domain, type, protocol, true);
95}
96
97} // namespace esphome::socket
98
99#endif // USE_SOCKET_IMPL_LWIP_SOCKETS
void unregister_socket_fd(int fd)
bool register_socket_fd(int fd)
Register/unregister a socket file descriptor to be monitored for read events.
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:97
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< ListenSocket > socket_listen(int domain, int type, int protocol)
Create a listening socket of the given domain, type and protocol.
std::unique_ptr< ListenSocket > socket_listen_loop_monitored(int domain, int type, int protocol)
Create a listening socket and monitor it for data in the main loop.
bool socket_ready_fd(int fd, bool loop_monitored)
Shared ready() helper for fd-based socket implementations.
Definition socket.cpp:14
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:817
Application App
Global storage of Application pointer - only one Application can exist.