ESPHome  2024.12.2
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  std::unique_ptr<Socket> accept(struct sockaddr *addr, socklen_t *addrlen) override {
43  int fd = lwip_accept(fd_, addr, addrlen);
44  if (fd == -1)
45  return {};
46  return make_unique<LwIPSocketImpl>(fd);
47  }
48  int bind(const struct sockaddr *addr, socklen_t addrlen) override { return lwip_bind(fd_, addr, addrlen); }
49  int close() override {
50  int ret = lwip_close(fd_);
51  closed_ = true;
52  return ret;
53  }
54  int shutdown(int how) override { return lwip_shutdown(fd_, how); }
55 
56  int getpeername(struct sockaddr *addr, socklen_t *addrlen) override { return lwip_getpeername(fd_, addr, addrlen); }
57  std::string getpeername() override {
58  struct sockaddr_storage storage;
59  socklen_t len = sizeof(storage);
60  int err = this->getpeername((struct sockaddr *) &storage, &len);
61  if (err != 0)
62  return {};
63  return format_sockaddr(storage);
64  }
65  int getsockname(struct sockaddr *addr, socklen_t *addrlen) override { return lwip_getsockname(fd_, addr, addrlen); }
66  std::string getsockname() override {
67  struct sockaddr_storage storage;
68  socklen_t len = sizeof(storage);
69  int err = this->getsockname((struct sockaddr *) &storage, &len);
70  if (err != 0)
71  return {};
72  return format_sockaddr(storage);
73  }
74  int getsockopt(int level, int optname, void *optval, socklen_t *optlen) override {
75  return lwip_getsockopt(fd_, level, optname, optval, optlen);
76  }
77  int setsockopt(int level, int optname, const void *optval, socklen_t optlen) override {
78  return lwip_setsockopt(fd_, level, optname, optval, optlen);
79  }
80  int listen(int backlog) override { return lwip_listen(fd_, backlog); }
81  ssize_t read(void *buf, size_t len) override { return lwip_read(fd_, buf, len); }
82  ssize_t readv(const struct iovec *iov, int iovcnt) override { return lwip_readv(fd_, iov, iovcnt); }
83  ssize_t write(const void *buf, size_t len) override { return lwip_write(fd_, buf, len); }
84  ssize_t send(void *buf, size_t len, int flags) { return lwip_send(fd_, buf, len, flags); }
85  ssize_t writev(const struct iovec *iov, int iovcnt) override { return lwip_writev(fd_, iov, iovcnt); }
86  ssize_t sendto(const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen) override {
87  return lwip_sendto(fd_, buf, len, flags, to, tolen);
88  }
89  int setblocking(bool blocking) override {
90  int fl = lwip_fcntl(fd_, F_GETFL, 0);
91  if (blocking) {
92  fl &= ~O_NONBLOCK;
93  } else {
94  fl |= O_NONBLOCK;
95  }
96  lwip_fcntl(fd_, F_SETFL, fl);
97  return 0;
98  }
99 
100  protected:
101  int fd_;
102  bool closed_ = false;
103 };
104 
105 std::unique_ptr<Socket> socket(int domain, int type, int protocol) {
106  int ret = lwip_socket(domain, type, protocol);
107  if (ret == -1)
108  return nullptr;
109  return std::unique_ptr<Socket>{new LwIPSocketImpl(ret)};
110 }
111 
112 } // namespace socket
113 } // namespace esphome
114 
115 #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:293
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.