ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
tcp_listener.cpp
Go to the documentation of this file.
1#include "tcp_listener.h"
2
3#if defined(USE_SOCKET_IMPL_LWIP_TCP) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || defined(USE_SOCKET_IMPL_BSD_SOCKETS)
4
6#include "esphome/core/log.h"
7
8#include <cerrno>
9#include <span>
10
11namespace esphome::socket {
12
13// One client at a time; a second connection waits in the stack until the first drops.
14static constexpr int LISTEN_BACKLOG = 1;
15
17 this->listen_ = socket_ip_loop_monitored(SOCK_STREAM, IPPROTO_TCP);
18 int err = errno;
19 if (this->listen_ != nullptr) {
20 int yes = 1;
21 this->listen_->setsockopt(SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
22 struct sockaddr_storage local;
23 socklen_t local_len = set_sockaddr_any(reinterpret_cast<struct sockaddr *>(&local), sizeof(local), link.port());
24 // A blocking listener would stall loop() inside accept(), so its
25 // setblocking result is part of the success condition.
26 if (this->listen_->setblocking(false) == 0 && local_len != 0 &&
27 this->listen_->bind(reinterpret_cast<struct sockaddr *>(&local), local_len) == 0 &&
28 this->listen_->listen(LISTEN_BACKLOG) == 0) {
29 ESP_LOGI(this->tag_, "Listening on %u", link.port());
30 return;
31 }
32 // Captured before reset(); the close inside can overwrite errno.
33 err = errno;
34 this->listen_.reset();
35 }
36 ESP_LOGW(this->tag_, "Listen on %u failed: %d", link.port(), err);
37 link.note_attempt();
38}
39
41 struct sockaddr_storage peer{};
42 socklen_t peer_len = sizeof(peer);
43 auto client = this->listen_->accept_loop_monitored(reinterpret_cast<struct sockaddr *>(&peer), &peer_len);
44 if (client == nullptr) {
45 // A reset during the handshake or a signal only affects that connection.
46 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == ECONNABORTED || errno == EINTR) {
47 return;
48 }
49 // Rebuild the listener after the backoff instead of spinning on it.
50 int err = errno;
51 this->listen_.reset();
52 ESP_LOGW(this->tag_, "Accept failed: %d", err);
53 link.note_attempt();
54 return;
55 }
56 const auto *sa = reinterpret_cast<const struct sockaddr *>(&peer);
57#ifdef USE_SOCKET_IPV4_ALLOW
58 if (!this->allow_.admit(sa, peer_len, this->tag_, App.get_loop_component_start_time())) {
59 return;
60 }
61#endif
62 char text[SOCKADDR_STR_LEN];
63 format_sockaddr_to(sa, peer_len, std::span<char, SOCKADDR_STR_LEN>(text));
64 link.adopt(std::move(client));
65 ESP_LOGI(this->tag_, "Client connected from %s", text);
66}
67
69#ifdef USE_SOCKET_IPV4_ALLOW
70 this->allow_.dump_config(this->tag_);
71#endif
72}
73
74} // namespace esphome::socket
75
76#endif
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void dump_config(const char *tag) const
One config line per allowed network.
bool admit(const struct sockaddr *peer, socklen_t len, const char *tag, uint32_t now)
allows(peer), logging a refused peer at most once every REJECT_LOG_INTERVAL_MS.
void dump_config() const
One config line per allowed network.
std::unique_ptr< ListenSocket > listen_
void try_listen_(TcpClientLink &link)
void accept_(TcpClientLink &link)
uint32_t socklen_t
Definition headers.h:109
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)
socklen_t set_sockaddr_any(struct sockaddr *addr, socklen_t addrlen, uint16_t port)
Set a sockaddr to the any address and specified port for the IP version used by socket_ip().
std::unique_ptr< ListenSocket > socket_ip_loop_monitored(int type, int protocol)
Definition socket.h:175
Application App
Global storage of Application pointer - only one Application can exist.