3#if defined(USE_SOCKET_IMPL_LWIP_TCP) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || defined(USE_SOCKET_IMPL_BSD_SOCKETS)
14static constexpr int LISTEN_BACKLOG = 1;
15#ifdef USE_SOCKET_IPV4_ALLOW
16static constexpr uint32_t REJECT_LOG_INTERVAL_MS = 5000;
24 this->
listen_->setsockopt(SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(yes));
29 if (this->
listen_->setblocking(
false) == 0 && local_len != 0 &&
30 this->listen_->bind(
reinterpret_cast<struct
sockaddr *
>(&local), local_len) == 0 &&
31 this->listen_->listen(LISTEN_BACKLOG) == 0) {
32 ESP_LOGI(this->
tag_,
"Listening on %u", link.
port());
39 ESP_LOGW(this->
tag_,
"Listen on %u failed: %d", link.
port(), err);
46 auto client = this->
listen_->accept_loop_monitored(
reinterpret_cast<struct
sockaddr *
>(&peer), &peer_len);
47 if (client ==
nullptr) {
49 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == ECONNABORTED || errno == EINTR) {
55 ESP_LOGW(this->
tag_,
"Accept failed: %d", err);
59 const auto *sa =
reinterpret_cast<const struct
sockaddr *
>(&peer);
60 char text[SOCKADDR_STR_LEN];
62#ifdef USE_SOCKET_IPV4_ALLOW
67 ESP_LOGW(this->
tag_,
"Rejected %s", text);
72 link.
adopt(std::move(client));
73 ESP_LOGI(this->
tag_,
"Client connected from %s", text);
77#ifdef USE_SOCKET_IPV4_ALLOW
78 for (
size_t i = 0; i < this->
allow_.
size(); i++) {
81 const auto *b =
reinterpret_cast<const uint8_t *
>(&e.
addr);
82 ESP_LOGCONFIG(this->
tag_,
" Allowed IP: %u.%u.%u.%u/%u", b[0], b[1], b[2], b[3],
83 static_cast<unsigned>(__builtin_popcount(e.
mask)));
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.
Ipv4AllowEntry entry(size_t i) const
A copy of entry i, read from flash.
bool allows(const struct sockaddr *peer) const
A v4 mapped IPv6 peer is unwrapped; any other family fails a non empty list.
A reconnecting TCP stream driven from loop().
void note_attempt()
Shared retry clock, also usable for a listen socket.
void adopt(std::unique_ptr< Socket > sock)
Take over an accepted socket (the server side of a bridge).
uint32_t last_reject_log_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)
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)
Application App
Global storage of Application pointer - only one Application can exist.
One allowed IPv4 network, network byte order, host bits cleared.