4#ifdef USE_SOCKET_IMPL_LWIP_TCP
16#elif defined(USE_RP2040)
17#include <hardware/sync.h>
40#define LWIP_LOCK() esphome::LwIPLock lwip_lock_guard
42static const char *
const TAG =
"socket.lwip";
46#define LWIP_LOG(msg, ...) ESP_LOGVV(TAG, "socket %p: " msg, this, ##__VA_ARGS__)
48#define LWIP_LOG(msg, ...)
58static void pcb_detach_abort(
struct tcp_pcb *pcb) {
59 tcp_arg(pcb,
nullptr);
60 tcp_recv(pcb,
nullptr);
61 tcp_err(pcb,
nullptr);
73static err_t pcb_detach_close(
struct tcp_pcb *pcb) {
74 tcp_arg(pcb,
nullptr);
75 tcp_recv(pcb,
nullptr);
76 tcp_err(pcb,
nullptr);
77 err_t err = tcp_close(pcb);
88 if (this->
pcb_ !=
nullptr) {
89 LWIP_LOG(
"tcp_abort(%p)", this->
pcb_);
90 pcb_detach_abort(this->
pcb_);
97 if (this->
pcb_ ==
nullptr) {
101 if (name ==
nullptr) {
108 if (this->
family_ == AF_INET) {
113 auto *addr4 =
reinterpret_cast<const sockaddr_in *
>(name);
114 port = ntohs(addr4->sin_port);
115 ip.type = IPADDR_TYPE_V4;
116 ip.u_addr.ip4.addr = addr4->sin_addr.s_addr;
117 LWIP_LOG(
"tcp_bind(%p ip=%s port=%u)", this->
pcb_, ip4addr_ntoa(&ip.u_addr.ip4), port);
118 }
else if (this->
family_ == AF_INET6) {
123 auto *addr6 =
reinterpret_cast<const sockaddr_in6 *
>(name);
124 port = ntohs(addr6->sin6_port);
125 ip.type = IPADDR_TYPE_ANY;
126 memcpy(&ip.u_addr.ip6.addr, &addr6->sin6_addr.un.u8_addr, 16);
127 LWIP_LOG(
"tcp_bind(%p ip=%s port=%u)", this->
pcb_, ip6addr_ntoa(&ip.u_addr.ip6), port);
133 if (this->
family_ != AF_INET) {
137 auto *addr4 =
reinterpret_cast<const sockaddr_in *
>(name);
138 port = ntohs(addr4->sin_port);
139 ip.addr = addr4->sin_addr.s_addr;
140 LWIP_LOG(
"tcp_bind(%p ip=%u port=%u)", this->
pcb_, ip.addr, port);
142 err_t err = tcp_bind(this->
pcb_, &ip, port);
143 if (err == ERR_USE) {
144 LWIP_LOG(
" -> err ERR_USE");
148 if (err == ERR_VAL) {
149 LWIP_LOG(
" -> err ERR_VAL");
154 LWIP_LOG(
" -> err %d", err);
163 if (this->
pcb_ ==
nullptr) {
167 LWIP_LOG(
"tcp_close(%p)", this->
pcb_);
168 err_t err = pcb_detach_close(this->
pcb_);
169 this->
pcb_ =
nullptr;
171 LWIP_LOG(
" -> err %d", err);
172 errno = err == ERR_MEM ? ENOMEM : EIO;
180 if (this->
pcb_ ==
nullptr) {
184 bool shut_rx =
false, shut_tx =
false;
185 if (how == SHUT_RD) {
187 }
else if (how == SHUT_WR) {
189 }
else if (how == SHUT_RDWR) {
190 shut_rx = shut_tx =
true;
195 LWIP_LOG(
"tcp_shutdown(%p shut_rx=%d shut_tx=%d)", this->
pcb_, shut_rx ? 1 : 0, shut_tx ? 1 : 0);
196 err_t err = tcp_shutdown(this->
pcb_, shut_rx, shut_tx);
198 LWIP_LOG(
" -> err %d", err);
199 errno = err == ERR_MEM ? ENOMEM : EIO;
207 if (this->
pcb_ ==
nullptr) {
211 if (name ==
nullptr || addrlen ==
nullptr) {
215 return this->
ip2sockaddr_(&this->
pcb_->remote_ip, this->pcb_->remote_port, name, addrlen);
220 if (this->
pcb_ ==
nullptr) {
224 if (name ==
nullptr || addrlen ==
nullptr) {
228 return this->
ip2sockaddr_(&this->
pcb_->local_ip, this->pcb_->local_port, name, addrlen);
253 if (this->
pcb_ ==
nullptr) {
257 if (optlen ==
nullptr || optval ==
nullptr) {
261 if (level == SOL_SOCKET && optname == SO_REUSEADDR) {
268 *
reinterpret_cast<int *
>(optval) = 1;
272 if (level == SOL_SOCKET && optname == SO_RCVTIMEO) {
273 if (*optlen <
sizeof(
struct timeval)) {
278 auto *tv =
reinterpret_cast<struct timeval *
>(optval);
279 tv->tv_sec = ms / 1000;
280 tv->tv_usec = (ms % 1000) * 1000;
281 *optlen =
sizeof(
struct timeval);
284 if (level == IPPROTO_TCP && optname == TCP_NODELAY) {
289 *
reinterpret_cast<int *
>(optval) = this->
nodelay_;
300 if (this->
pcb_ ==
nullptr) {
304 if (level == SOL_SOCKET && optname == SO_REUSEADDR) {
313 if (level == SOL_SOCKET && optname == SO_RCVTIMEO) {
314 if (optlen <
sizeof(
struct timeval)) {
318 const auto *tv =
reinterpret_cast<const struct timeval *
>(optval);
319 uint32_t ms = tv->tv_sec * 1000 + tv->tv_usec / 1000;
324 if (level == SOL_SOCKET && optname == SO_SNDTIMEO) {
328 if (level == IPPROTO_TCP && optname == TCP_NODELAY) {
333 int val = *
reinterpret_cast<const int *
>(optval);
343 if (this->
family_ == AF_INET) {
353 inet_addr_from_ip4addr(&addr->
sin_addr, ip_2_ip4(ip));
357 else if (this->
family_ == AF_INET6) {
371 ip4_2_ipv4_mapped_ipv6(ip_2_ip6(&mapped), ip_2_ip4(ip));
372 inet6_addr_from_ip6addr(&addr->
sin6_addr, ip_2_ip6(&mapped));
374 inet6_addr_from_ip6addr(&addr->
sin6_addr, ip_2_ip6(ip));
389 if (this->
rx_buf_ !=
nullptr) {
398 LWIP_LOG(
"init(%p)", this->
pcb_);
399 tcp_arg(this->
pcb_,
this);
402 if (initial_rx !=
nullptr) {
417 auto *arg_this =
reinterpret_cast<LWIPRawImpl *
>(arg);
418 ESP_LOGVV(TAG,
"socket %p: err(err=%d)", arg_this, err);
419 arg_this->pcb_ =
nullptr;
423 auto *arg_this =
reinterpret_cast<LWIPRawImpl *
>(arg);
424 return arg_this->
recv_fn(pb, err);
430 LWIP_LOG(
"recv(pb=%p err=%d)", pb, err);
444 if (this->
rx_buf_ ==
nullptr) {
467 if (elapsed >= timeout_ms)
475 if (this->
pcb_ ==
nullptr) {
485 if (this->
rx_buf_ ==
nullptr) {
491 uint8_t *buf8 =
reinterpret_cast<uint8_t *
>(buf);
493 size_t pb_len = this->
rx_buf_->len;
497 size_t copysize = std::min(
len, pb_left);
498 memcpy(buf8,
reinterpret_cast<uint8_t *
>(this->
rx_buf_->payload) + this->rx_buf_offset_, copysize);
500 if (pb_left == copysize) {
502 if (this->
rx_buf_->next ==
nullptr) {
506 this->rx_buf_offset_ = 0;
512 this->rx_buf_offset_ = 0;
515 this->rx_buf_offset_ += copysize;
517 LWIP_LOG(
"tcp_recved(%p %u)", this->
pcb_, copysize);
518 tcp_recved(this->
pcb_, copysize);
551 for (
int i = 0; i < iovcnt; i++) {
552 ssize_t err = this->
read_locked_(
reinterpret_cast<uint8_t *
>(iov[i].iov_base), iov[i].iov_len);
561 if ((
size_t) err != iov[i].iov_len)
569 if (this->
pcb_ ==
nullptr) {
575 if (buf ==
nullptr) {
579 auto space = tcp_sndbuf(this->
pcb_);
584 size_t to_send = std::min((
size_t) space,
len);
585 LWIP_LOG(
"tcp_write(%p buf=%p %u)", this->
pcb_, buf, to_send);
586 err_t err = tcp_write(this->
pcb_, buf, to_send, TCP_WRITE_FLAG_COPY);
587 if (err == ERR_MEM) {
588 LWIP_LOG(
" -> err ERR_MEM");
593 LWIP_LOG(
" -> err %d", err);
602 if (this->
pcb_ ==
nullptr) {
606 LWIP_LOG(
"tcp_output(%p)", this->
pcb_);
607 err_t err = tcp_output(this->
pcb_);
608 if (err == ERR_ABRT) {
613 LWIP_LOG(
" -> err ERR_ABRT");
617 LWIP_LOG(
" -> err %d", err);
644 for (
int i = 0; i < iovcnt; i++) {
654 if ((
size_t) err != iov[i].iov_len)
674 for (uint8_t i = 0; i < this->accepted_socket_count_; i++) {
675 auto &entry = this->accepted_pcbs_[i];
676 if (entry.pcb !=
nullptr) {
677 pcb_detach_abort(entry.pcb);
680 if (entry.rx_buf !=
nullptr) {
681 pbuf_free(entry.rx_buf);
682 entry.rx_buf =
nullptr;
685 this->accepted_socket_count_ = 0;
694 if (this->
pcb_ !=
nullptr) {
695 tcp_close(this->
pcb_);
696 this->
pcb_ =
nullptr;
702 LWIP_LOG(
"init(%p)", this->
pcb_);
703 tcp_arg(this->
pcb_,
this);
704 tcp_accept(this->
pcb_, LWIPRawListenImpl::s_accept_fn);
712 ESP_LOGVV(TAG,
"socket %p: err(err=%d)", arg_this, err);
713 arg_this->pcb_ =
nullptr;
716void LWIPRawListenImpl::s_queued_err_fn(
void *arg, err_t err) {
722 auto *entry =
reinterpret_cast<QueuedPcb *
>(arg);
723 entry->pcb =
nullptr;
727err_t LWIPRawListenImpl::s_queued_recv_fn(
void *arg,
struct tcp_pcb *pcb,
struct pbuf *pb, err_t err) {
734 auto *entry =
reinterpret_cast<QueuedPcb *
>(arg);
735 if (pb ==
nullptr || err != ERR_OK) {
740 entry->rx_closed =
true;
744 if (entry->rx_buf ==
nullptr) {
747 pbuf_cat(entry->rx_buf, pb);
752err_t LWIPRawListenImpl::s_accept_fn(
void *arg,
struct tcp_pcb *newpcb, err_t err) {
753 auto *arg_this =
reinterpret_cast<LWIPRawListenImpl *
>(arg);
754 return arg_this->accept_fn_(newpcb, err);
759 if (this->
pcb_ ==
nullptr) {
765 while (this->accepted_socket_count_ > 0) {
766 QueuedPcb entry = this->accepted_pcbs_[0];
769 for (uint8_t i = 1; i < this->accepted_socket_count_; i++) {
770 this->accepted_pcbs_[i - 1] = this->accepted_pcbs_[i];
771 if (this->accepted_pcbs_[i - 1].pcb !=
nullptr) {
772 tcp_arg(this->accepted_pcbs_[i - 1].pcb, &this->accepted_pcbs_[i - 1]);
775 this->accepted_pcbs_[this->accepted_socket_count_ - 1] = {};
776 this->accepted_socket_count_--;
777 if (entry.pcb ==
nullptr) {
779 if (entry.rx_buf !=
nullptr) {
780 pbuf_free(entry.rx_buf);
784 LWIP_LOG(
"Connection accepted by application, queue size: %d", this->accepted_socket_count_);
787 auto sock = make_unique<LWIPRawImpl>(this->
family_, entry.pcb);
788 sock->init(entry.rx_buf, entry.rx_closed);
789 if (addr !=
nullptr) {
790 sock->getpeername(addr, addrlen);
792 LWIP_LOG(
"accept(%p)", sock.get());
801 if (this->
pcb_ ==
nullptr) {
805 LWIP_LOG(
"tcp_listen_with_backlog(%p backlog=%d)", this->
pcb_, backlog);
806 struct tcp_pcb *listen_pcb = tcp_listen_with_backlog(this->
pcb_, backlog);
807 if (listen_pcb ==
nullptr) {
808 tcp_abort(this->
pcb_);
809 this->
pcb_ =
nullptr;
814 this->
pcb_ = listen_pcb;
816 LWIP_LOG(
"tcp_arg(%p)", this->
pcb_);
817 tcp_arg(this->
pcb_,
this);
818 tcp_accept(this->
pcb_, LWIPRawListenImpl::s_accept_fn);
825err_t LWIPRawListenImpl::accept_fn_(
struct tcp_pcb *newpcb, err_t err) {
828 LWIP_LOG(
"accept(newpcb=%p err=%d)", newpcb, err);
829 if (err != ERR_OK || newpcb ==
nullptr) {
837 if (this->accepted_socket_count_ >= MAX_ACCEPTED_SOCKETS) {
838 LWIP_LOG(
"Rejecting connection, queue full (%d)", this->accepted_socket_count_);
846 uint8_t idx = this->accepted_socket_count_++;
847 this->accepted_pcbs_[idx] = {newpcb,
nullptr,
false};
853 tcp_arg(newpcb, &this->accepted_pcbs_[idx]);
854 tcp_err(newpcb, LWIPRawListenImpl::s_queued_err_fn);
855 tcp_recv(newpcb, LWIPRawListenImpl::s_queued_recv_fn);
856 LWIP_LOG(
"Accepted connection, queue size: %d", this->accepted_socket_count_);
864std::unique_ptr<Socket>
socket(
int domain,
int type,
int protocol) {
865 if (
type != SOCK_STREAM) {
866 ESP_LOGE(TAG,
"UDP sockets not supported on this platform, use WiFiUDP");
871 auto *pcb = tcp_new();
874 auto *sock =
new LWIPRawImpl((
sa_family_t) domain, pcb);
876 return std::unique_ptr<Socket>{sock};
885 if (
type != SOCK_STREAM) {
886 ESP_LOGE(TAG,
"UDP sockets not supported on this platform, use WiFiUDP");
891 auto *pcb = tcp_new();
896 return std::unique_ptr<ListenSocket>{sock};
int getsockname(struct sockaddr *name, 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)
int bind(const struct sockaddr *name, socklen_t addrlen)
int ip2sockaddr_(ip_addr_t *ip, uint16_t port, struct sockaddr *name, socklen_t *addrlen)
int setsockopt(int level, int optname, const void *optval, socklen_t optlen)
int getsockopt(int level, int optname, void *optval, socklen_t *optlen)
int getpeername(struct sockaddr *name, 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)
Connected socket implementation for LWIP raw TCP.
ssize_t read_locked_(void *buf, size_t len)
static err_t s_recv_fn(void *arg, struct tcp_pcb *pcb, struct pbuf *pb, err_t err)
void init(struct pbuf *initial_rx=nullptr, bool initial_rx_closed=false)
bool waiting_for_data_() const
ssize_t readv(const struct iovec *iov, int iovcnt)
static void s_err_fn(void *arg, err_t err)
err_t recv_fn(struct pbuf *pb, err_t err)
ssize_t internal_write_(const void *buf, size_t len)
ssize_t write(const void *buf, size_t len)
ssize_t read(void *buf, size_t len)
ssize_t writev(const struct iovec *iov, int iovcnt)
Listening socket implementation for LWIP raw TCP.
static void s_err_fn(void *arg, err_t err)
std::unique_ptr< LWIPRawImpl > accept(struct sockaddr *addr, socklen_t *addrlen)
void wakeable_delay(uint32_t ms)
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)
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)
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.
uint32_t IRAM_ATTR HOT millis()
void IRAM_ATTR wake_loop_any_context()
IRAM_ATTR entry point — defined in wake.cpp.
struct in6_addr sin6_addr
Platform-specific main loop wake primitives.