4#ifdef USE_SOCKET_IMPL_LWIP_TCP
14#ifdef USE_OTA_PLATFORM_ESPHOME
20#elif defined(USE_RP2040)
21#include <hardware/sync.h>
44#define LWIP_LOCK() esphome::LwIPLock lwip_lock_guard
46static const char *
const TAG =
"socket.lwip";
50#define LWIP_LOG(msg, ...) ESP_LOGVV(TAG, "socket %p: " msg, this, ##__VA_ARGS__)
52#define LWIP_LOG(msg, ...)
62static void pcb_detach_abort(
struct tcp_pcb *pcb) {
63 tcp_arg(pcb,
nullptr);
64 tcp_recv(pcb,
nullptr);
65 tcp_err(pcb,
nullptr);
77static err_t pcb_detach_close(
struct tcp_pcb *pcb) {
78 tcp_arg(pcb,
nullptr);
79 tcp_recv(pcb,
nullptr);
80 tcp_err(pcb,
nullptr);
81 err_t err = tcp_close(pcb);
92 if (this->
pcb_ !=
nullptr) {
93 LWIP_LOG(
"tcp_abort(%p)", this->
pcb_);
94 pcb_detach_abort(this->
pcb_);
101 if (this->
pcb_ ==
nullptr) {
105 if (name ==
nullptr) {
112 if (this->
family_ == AF_INET) {
117 auto *addr4 =
reinterpret_cast<const sockaddr_in *
>(name);
118 port = ntohs(addr4->sin_port);
119 ip.type = IPADDR_TYPE_V4;
120 ip.u_addr.ip4.addr = addr4->sin_addr.s_addr;
121 LWIP_LOG(
"tcp_bind(%p ip=%s port=%u)", this->
pcb_, ip4addr_ntoa(&ip.u_addr.ip4), port);
122 }
else if (this->
family_ == AF_INET6) {
127 auto *addr6 =
reinterpret_cast<const sockaddr_in6 *
>(name);
128 port = ntohs(addr6->sin6_port);
129 ip.type = IPADDR_TYPE_ANY;
130 memcpy(&ip.u_addr.ip6.addr, &addr6->sin6_addr.un.u8_addr, 16);
131 LWIP_LOG(
"tcp_bind(%p ip=%s port=%u)", this->
pcb_, ip6addr_ntoa(&ip.u_addr.ip6), port);
137 if (this->
family_ != AF_INET) {
141 auto *addr4 =
reinterpret_cast<const sockaddr_in *
>(name);
142 port = ntohs(addr4->sin_port);
143 ip.addr = addr4->sin_addr.s_addr;
144 LWIP_LOG(
"tcp_bind(%p ip=%u port=%u)", this->
pcb_, ip.addr, port);
146 err_t err = tcp_bind(this->
pcb_, &ip, port);
147 if (err == ERR_USE) {
148 LWIP_LOG(
" -> err ERR_USE");
152 if (err == ERR_VAL) {
153 LWIP_LOG(
" -> err ERR_VAL");
158 LWIP_LOG(
" -> err %d", err);
167 if (this->
pcb_ ==
nullptr) {
171 LWIP_LOG(
"tcp_close(%p)", this->
pcb_);
172 err_t err = pcb_detach_close(this->
pcb_);
173 this->
pcb_ =
nullptr;
175 LWIP_LOG(
" -> err %d", err);
176 errno = err == ERR_MEM ? ENOMEM : EIO;
184 if (this->
pcb_ ==
nullptr) {
188 bool shut_rx =
false, shut_tx =
false;
189 if (how == SHUT_RD) {
191 }
else if (how == SHUT_WR) {
193 }
else if (how == SHUT_RDWR) {
194 shut_rx = shut_tx =
true;
199 LWIP_LOG(
"tcp_shutdown(%p shut_rx=%d shut_tx=%d)", this->
pcb_, shut_rx ? 1 : 0, shut_tx ? 1 : 0);
200 err_t err = tcp_shutdown(this->
pcb_, shut_rx, shut_tx);
202 LWIP_LOG(
" -> err %d", err);
203 errno = err == ERR_MEM ? ENOMEM : EIO;
211 if (this->
pcb_ ==
nullptr) {
215 if (name ==
nullptr || addrlen ==
nullptr) {
219 return this->
ip2sockaddr_(&this->
pcb_->remote_ip, this->pcb_->remote_port, name, addrlen);
224 if (this->
pcb_ ==
nullptr) {
228 if (name ==
nullptr || addrlen ==
nullptr) {
232 return this->
ip2sockaddr_(&this->
pcb_->local_ip, this->pcb_->local_port, name, addrlen);
257 if (this->
pcb_ ==
nullptr) {
261 if (optlen ==
nullptr || optval ==
nullptr) {
265 if (level == SOL_SOCKET && optname == SO_REUSEADDR) {
272 *
reinterpret_cast<int *
>(optval) = 1;
276 if (level == SOL_SOCKET && optname == SO_RCVTIMEO) {
277 if (*optlen <
sizeof(
struct timeval)) {
282 auto *tv =
reinterpret_cast<struct timeval *
>(optval);
283 tv->tv_sec = ms / 1000;
284 tv->tv_usec = (ms % 1000) * 1000;
285 *optlen =
sizeof(
struct timeval);
288 if (level == IPPROTO_TCP && optname == TCP_NODELAY) {
293 *
reinterpret_cast<int *
>(optval) = this->
nodelay_;
304 if (this->
pcb_ ==
nullptr) {
308 if (level == SOL_SOCKET && optname == SO_REUSEADDR) {
317 if (level == SOL_SOCKET && optname == SO_RCVTIMEO) {
318 if (optlen <
sizeof(
struct timeval)) {
322 const auto *tv =
reinterpret_cast<const struct timeval *
>(optval);
323 uint32_t ms = tv->tv_sec * 1000 + tv->tv_usec / 1000;
328 if (level == SOL_SOCKET && optname == SO_SNDTIMEO) {
332 if (level == IPPROTO_TCP && optname == TCP_NODELAY) {
337 int val = *
reinterpret_cast<const int *
>(optval);
347 if (this->
family_ == AF_INET) {
357 inet_addr_from_ip4addr(&addr->
sin_addr, ip_2_ip4(ip));
361 else if (this->
family_ == AF_INET6) {
375 ip4_2_ipv4_mapped_ipv6(ip_2_ip6(&mapped), ip_2_ip4(ip));
376 inet6_addr_from_ip6addr(&addr->
sin6_addr, ip_2_ip6(&mapped));
378 inet6_addr_from_ip6addr(&addr->
sin6_addr, ip_2_ip6(ip));
393 if (this->
rx_buf_ !=
nullptr) {
402 LWIP_LOG(
"init(%p)", this->
pcb_);
403 tcp_arg(this->
pcb_,
this);
406 if (initial_rx !=
nullptr) {
421 auto *arg_this =
reinterpret_cast<LWIPRawImpl *
>(arg);
422 ESP_LOGVV(TAG,
"socket %p: err(err=%d)", arg_this, err);
423 arg_this->pcb_ =
nullptr;
427 auto *arg_this =
reinterpret_cast<LWIPRawImpl *
>(arg);
428 return arg_this->
recv_fn(pb, err);
434 LWIP_LOG(
"recv(pb=%p err=%d)", pb, err);
448 if (this->
rx_buf_ ==
nullptr) {
471 if (elapsed >= timeout_ms)
479 if (this->
pcb_ ==
nullptr) {
489 if (this->
rx_buf_ ==
nullptr) {
495 uint8_t *buf8 =
reinterpret_cast<uint8_t *
>(buf);
497 size_t pb_len = this->
rx_buf_->len;
501 size_t copysize = std::min(
len, pb_left);
502 memcpy(buf8,
reinterpret_cast<uint8_t *
>(this->
rx_buf_->payload) + this->rx_buf_offset_, copysize);
504 if (pb_left == copysize) {
506 if (this->
rx_buf_->next ==
nullptr) {
510 this->rx_buf_offset_ = 0;
516 this->rx_buf_offset_ = 0;
519 this->rx_buf_offset_ += copysize;
521 LWIP_LOG(
"tcp_recved(%p %u)", this->
pcb_, copysize);
522 tcp_recved(this->
pcb_, copysize);
555 for (
int i = 0; i < iovcnt; i++) {
556 ssize_t err = this->
read_locked_(
reinterpret_cast<uint8_t *
>(iov[i].iov_base), iov[i].iov_len);
565 if ((
size_t) err != iov[i].iov_len)
573 if (this->
pcb_ ==
nullptr) {
579 if (buf ==
nullptr) {
583 auto space = tcp_sndbuf(this->
pcb_);
588 size_t to_send = std::min((
size_t) space,
len);
589 LWIP_LOG(
"tcp_write(%p buf=%p %u)", this->
pcb_, buf, to_send);
590 err_t err = tcp_write(this->
pcb_, buf, to_send, TCP_WRITE_FLAG_COPY);
591 if (err == ERR_MEM) {
592 LWIP_LOG(
" -> err ERR_MEM");
597 LWIP_LOG(
" -> err %d", err);
606 if (this->
pcb_ ==
nullptr) {
610 LWIP_LOG(
"tcp_output(%p)", this->
pcb_);
611 err_t err = tcp_output(this->
pcb_);
612 if (err == ERR_ABRT) {
617 LWIP_LOG(
" -> err ERR_ABRT");
621 LWIP_LOG(
" -> err %d", err);
648 for (
int i = 0; i < iovcnt; i++) {
658 if ((
size_t) err != iov[i].iov_len)
678 for (uint8_t i = 0; i < this->accepted_socket_count_; i++) {
679 auto &entry = this->accepted_pcbs_[i];
680 if (entry.pcb !=
nullptr) {
681 pcb_detach_abort(entry.pcb);
684 if (entry.rx_buf !=
nullptr) {
685 pbuf_free(entry.rx_buf);
686 entry.rx_buf =
nullptr;
689 this->accepted_socket_count_ = 0;
698 if (this->
pcb_ !=
nullptr) {
699 tcp_close(this->
pcb_);
700 this->
pcb_ =
nullptr;
706 LWIP_LOG(
"init(%p)", this->
pcb_);
707 tcp_arg(this->
pcb_,
this);
708 tcp_accept(this->
pcb_, LWIPRawListenImpl::s_accept_fn);
716 ESP_LOGVV(TAG,
"socket %p: err(err=%d)", arg_this, err);
717 arg_this->pcb_ =
nullptr;
720void LWIPRawListenImpl::s_queued_err_fn(
void *arg, err_t err) {
726 auto *entry =
reinterpret_cast<QueuedPcb *
>(arg);
727 entry->pcb =
nullptr;
731err_t LWIPRawListenImpl::s_queued_recv_fn(
void *arg,
struct tcp_pcb *pcb,
struct pbuf *pb, err_t err) {
738 auto *entry =
reinterpret_cast<QueuedPcb *
>(arg);
739 if (pb ==
nullptr || err != ERR_OK) {
744 entry->rx_closed =
true;
748 if (entry->rx_buf ==
nullptr) {
751 pbuf_cat(entry->rx_buf, pb);
756err_t LWIPRawListenImpl::s_accept_fn(
void *arg,
struct tcp_pcb *newpcb, err_t err) {
757 auto *arg_this =
reinterpret_cast<LWIPRawListenImpl *
>(arg);
758 return arg_this->accept_fn_(newpcb, err);
763 if (this->
pcb_ ==
nullptr) {
769 while (this->accepted_socket_count_ > 0) {
770 QueuedPcb entry = this->accepted_pcbs_[0];
773 for (uint8_t i = 1; i < this->accepted_socket_count_; i++) {
774 this->accepted_pcbs_[i - 1] = this->accepted_pcbs_[i];
775 if (this->accepted_pcbs_[i - 1].pcb !=
nullptr) {
776 tcp_arg(this->accepted_pcbs_[i - 1].pcb, &this->accepted_pcbs_[i - 1]);
779 this->accepted_pcbs_[this->accepted_socket_count_ - 1] = {};
780 this->accepted_socket_count_--;
781 if (entry.pcb ==
nullptr) {
783 if (entry.rx_buf !=
nullptr) {
784 pbuf_free(entry.rx_buf);
788 LWIP_LOG(
"Connection accepted by application, queue size: %d", this->accepted_socket_count_);
791 auto sock = make_unique<LWIPRawImpl>(this->
family_, entry.pcb);
792 sock->init(entry.rx_buf, entry.rx_closed);
793 if (addr !=
nullptr) {
794 sock->getpeername(addr, addrlen);
796 LWIP_LOG(
"accept(%p)", sock.get());
805 if (this->
pcb_ ==
nullptr) {
809 LWIP_LOG(
"tcp_listen_with_backlog(%p backlog=%d)", this->
pcb_, backlog);
810 struct tcp_pcb *listen_pcb = tcp_listen_with_backlog(this->
pcb_, backlog);
811 if (listen_pcb ==
nullptr) {
812 tcp_abort(this->
pcb_);
813 this->
pcb_ =
nullptr;
818 this->
pcb_ = listen_pcb;
820 LWIP_LOG(
"tcp_arg(%p)", this->
pcb_);
821 tcp_arg(this->
pcb_,
this);
822 tcp_accept(this->
pcb_, LWIPRawListenImpl::s_accept_fn);
829err_t LWIPRawListenImpl::accept_fn_(
struct tcp_pcb *newpcb, err_t err) {
832 LWIP_LOG(
"accept(newpcb=%p err=%d)", newpcb, err);
833 if (err != ERR_OK || newpcb ==
nullptr) {
841 if (this->accepted_socket_count_ >= MAX_ACCEPTED_SOCKETS) {
842 LWIP_LOG(
"Rejecting connection, queue full (%d)", this->accepted_socket_count_);
850 uint8_t idx = this->accepted_socket_count_++;
851 this->accepted_pcbs_[idx] = {newpcb,
nullptr,
false};
857 tcp_arg(newpcb, &this->accepted_pcbs_[idx]);
858 tcp_err(newpcb, LWIPRawListenImpl::s_queued_err_fn);
859 tcp_recv(newpcb, LWIPRawListenImpl::s_queued_recv_fn);
860 LWIP_LOG(
"Accepted connection, queue size: %d", this->accepted_socket_count_);
861#ifdef USE_OTA_PLATFORM_ESPHOME
872std::unique_ptr<Socket>
socket(
int domain,
int type,
int protocol) {
873 if (
type != SOCK_STREAM) {
874 ESP_LOGE(TAG,
"UDP sockets not supported on this platform, use WiFiUDP");
879 auto *pcb = tcp_new();
882 auto *sock =
new LWIPRawImpl((
sa_family_t) domain, pcb);
884 return std::unique_ptr<Socket>{sock};
893 if (
type != SOCK_STREAM) {
894 ESP_LOGE(TAG,
"UDP sockets not supported on this platform, use WiFiUDP");
899 auto *pcb = tcp_new();
904 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 esphome_wake_ota_component_any_context()
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.
void esphome_wake_ota_component_any_context()
uint32_t IRAM_ATTR HOT millis()
void IRAM_ATTR wake_loop_any_context()
IRAM_ATTR entry point for ISR callers — defined in wake.cpp.
struct in6_addr sin6_addr
Platform-specific main loop wake primitives.