ESPHome  2025.3.3
lwip_sockets_impl.cpp
Go to the documentation of this file.
1 #include "socket.h"
2 #include "esphome/core/defines.h"
3 #include "esphome/core/helpers.h"
4 
5 #ifdef USE_SOCKET_IMPL_LWIP_SOCKETS
6 
7 #include <cstring>
8 
9 namespace esphome {
10 namespace socket {
11 
12 std::string format_sockaddr(const struct sockaddr_storage &storage) {
13  if (storage.ss_family == AF_INET) {
14  const struct sockaddr_in *addr = reinterpret_cast<const struct sockaddr_in *>(&storage);
15  char buf[INET_ADDRSTRLEN];
16  const char *ret = lwip_inet_ntop(AF_INET, &addr->sin_addr, buf, sizeof(buf));
17  if (ret == nullptr)
18  return {};
19  return std::string{buf};
20  }
21 #if LWIP_IPV6
22  else if (storage.ss_family == AF_INET6) {
23  const struct sockaddr_in6 *addr = reinterpret_cast<const struct sockaddr_in6 *>(&storage);
24  char buf[INET6_ADDRSTRLEN];
25  const char *ret = lwip_inet_ntop(AF_INET6, &addr->sin6_addr, buf, sizeof(buf));
26  if (ret == nullptr)
27  return {};
28  return std::string{buf};
29  }
30 #endif
31  return {};
32 }
33 
34 class LwIPSocketImpl : public Socket {
35  public:
36  LwIPSocketImpl(int fd) : fd_(fd) {}
37  ~LwIPSocketImpl() override {
38  if (!closed_) {
39  close(); // NOLINT(clang-analyzer-optin.cplusplus.VirtualCall)
40  }
41  }
42  int connect(const struct sockaddr *addr, socklen_t addrlen) override { return lwip_connect(fd_, addr, addrlen); }
43  std::unique_ptr<Socket> accept(struct sockaddr *addr, socklen_t *addrlen) override {
44  int fd = lwip_accept(fd_, addr, addrlen);
45  if (fd == -1)
46  return {};
47  return make_unique<LwIPSocketImpl>(fd);
48  }
49  int bind(const struct sockaddr *addr, socklen_t addrlen) override { return lwip_bind(fd_, addr, addrlen); }
50  int close() override {
51  int ret = lwip_close(fd_);
52  closed_ = true;
53  return ret;
54  }
55  int shutdown(int how) override { return lwip_shutdown(fd_, how); }
56 
57  int getpeername(struct sockaddr *addr, socklen_t *addrlen) override { return lwip_getpeername(fd_, addr, addrlen); }
58  std::string getpeername() override {
59  struct sockaddr_storage storage;
60  socklen_t len = sizeof(storage);
61  int err = this->getpeername((struct sockaddr *) &storage, &len);
62  if (err != 0)
63  return {};
64  return format_sockaddr(storage);
65  }
66  int getsockname(struct sockaddr *addr, socklen_t *addrlen) override { return lwip_getsockname(fd_, addr, addrlen); }
67  std::string getsockname() override {
68  struct sockaddr_storage storage;
69  socklen_t len = sizeof(storage);
70  int err = this->getsockname((struct sockaddr *) &storage, &len);
71  if (err != 0)
72  return {};
73  return format_sockaddr(storage);
74  }
75  int getsockopt(int level, int optname, void *optval, socklen_t *optlen) override {
76  return lwip_getsockopt(fd_, level, optname, optval, optlen);
77  }
78  int setsockopt(int level, int optname, const void *optval, socklen_t optlen) override {
79  return lwip_setsockopt(fd_, level, optname, optval, optlen);
80  }
81  int listen(int backlog) override { return lwip_listen(fd_, backlog); }
82  ssize_t read(void *buf, size_t len) override { return lwip_read(fd_, buf, len); }
83  ssize_t readv(const struct iovec *iov, int iovcnt) override { return lwip_readv(fd_, iov, iovcnt); }
84  ssize_t write(const void *buf, size_t len) override { return lwip_write(fd_, buf, len); }
85  ssize_t send(void *buf, size_t len, int flags) { return lwip_send(fd_, buf, len, flags); }
86  ssize_t writev(const struct iovec *iov, int iovcnt) override { return lwip_writev(fd_, iov, iovcnt); }
87  ssize_t sendto(const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen) override {
88  return lwip_sendto(fd_, buf, len, flags, to, tolen);
89  }
90  int setblocking(bool blocking) override {
91  int fl = lwip_fcntl(fd_, F_GETFL, 0);
92  if (blocking) {
93  fl &= ~O_NONBLOCK;
94  } else {
95  fl |= O_NONBLOCK;
96  }
97  lwip_fcntl(fd_, F_SETFL, fl);
98  return 0;
99  }
100 
101  protected:
102  int fd_;
103  bool closed_ = false;
104 };
105 
106 std::unique_ptr<Socket> socket(int domain, int type, int protocol) {
107  int ret = lwip_socket(domain, type, protocol);
108  if (ret == -1)
109  return nullptr;
110  return std::unique_ptr<Socket>{new LwIPSocketImpl(ret)};
111 }
112 
113 } // namespace socket
114 } // namespace esphome
115 
116 #endif // USE_SOCKET_IMPL_LWIP_SOCKETS
sa_family_t ss_family
Definition: headers.h:92
uint32_t socklen_t
Definition: headers.h:97
uint8_t type
Definition: headers.h:100
const uint32_t flags
Definition: stm32flash.h:85
struct in_addr sin_addr
Definition: headers.h:65
std::string size_t len
Definition: helpers.h:301
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
std::string format_sockaddr(const struct sockaddr_storage &storage)
struct in6_addr sin6_addr
Definition: headers.h:77
std::unique_ptr< Socket > socket(int domain, int type, int protocol)
Create a socket of the given domain, type and protocol.