ESPHome  2024.11.0
bsd_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_BSD_SOCKETS
6 
7 #include <cstring>
8 
9 #ifdef USE_ESP32
10 #include <esp_idf_version.h>
11 #include <lwip/sockets.h>
12 #endif
13 
14 namespace esphome {
15 namespace socket {
16 
17 std::string format_sockaddr(const struct sockaddr_storage &storage) {
18  if (storage.ss_family == AF_INET) {
19  const struct sockaddr_in *addr = reinterpret_cast<const struct sockaddr_in *>(&storage);
20  char buf[INET_ADDRSTRLEN];
21  if (inet_ntop(AF_INET, &addr->sin_addr, buf, sizeof(buf)) != nullptr)
22  return std::string{buf};
23  }
24 #if LWIP_IPV6
25  else if (storage.ss_family == AF_INET6) {
26  const struct sockaddr_in6 *addr = reinterpret_cast<const struct sockaddr_in6 *>(&storage);
27  char buf[INET6_ADDRSTRLEN];
28  // Format IPv4-mapped IPv6 addresses as regular IPv4 addresses
29  if (addr->sin6_addr.un.u32_addr[0] == 0 && addr->sin6_addr.un.u32_addr[1] == 0 &&
30  addr->sin6_addr.un.u32_addr[2] == htonl(0xFFFF) &&
31  inet_ntop(AF_INET, &addr->sin6_addr.un.u32_addr[3], buf, sizeof(buf)) != nullptr) {
32  return std::string{buf};
33  }
34  if (inet_ntop(AF_INET6, &addr->sin6_addr, buf, sizeof(buf)) != nullptr)
35  return std::string{buf};
36  }
37 #endif
38  return {};
39 }
40 
41 class BSDSocketImpl : public Socket {
42  public:
43  BSDSocketImpl(int fd) : fd_(fd) {}
44  ~BSDSocketImpl() override {
45  if (!closed_) {
46  close(); // NOLINT(clang-analyzer-optin.cplusplus.VirtualCall)
47  }
48  }
49  std::unique_ptr<Socket> accept(struct sockaddr *addr, socklen_t *addrlen) override {
50  int fd = ::accept(fd_, addr, addrlen);
51  if (fd == -1)
52  return {};
53  return make_unique<BSDSocketImpl>(fd);
54  }
55  int bind(const struct sockaddr *addr, socklen_t addrlen) override { return ::bind(fd_, addr, addrlen); }
56  int close() override {
57  int ret = ::close(fd_);
58  closed_ = true;
59  return ret;
60  }
61  int shutdown(int how) override { return ::shutdown(fd_, how); }
62 
63  int getpeername(struct sockaddr *addr, socklen_t *addrlen) override { return ::getpeername(fd_, addr, addrlen); }
64  std::string getpeername() override {
65  struct sockaddr_storage storage;
66  socklen_t len = sizeof(storage);
67  int err = this->getpeername((struct sockaddr *) &storage, &len);
68  if (err != 0)
69  return {};
70  return format_sockaddr(storage);
71  }
72  int getsockname(struct sockaddr *addr, socklen_t *addrlen) override { return ::getsockname(fd_, addr, addrlen); }
73  std::string getsockname() override {
74  struct sockaddr_storage storage;
75  socklen_t len = sizeof(storage);
76  int err = this->getsockname((struct sockaddr *) &storage, &len);
77  if (err != 0)
78  return {};
79  return format_sockaddr(storage);
80  }
81  int getsockopt(int level, int optname, void *optval, socklen_t *optlen) override {
82  return ::getsockopt(fd_, level, optname, optval, optlen);
83  }
84  int setsockopt(int level, int optname, const void *optval, socklen_t optlen) override {
85  return ::setsockopt(fd_, level, optname, optval, optlen);
86  }
87  int listen(int backlog) override { return ::listen(fd_, backlog); }
88  ssize_t read(void *buf, size_t len) override { return ::read(fd_, buf, len); }
89  ssize_t recvfrom(void *buf, size_t len, sockaddr *addr, socklen_t *addr_len) override {
90 #if defined(USE_ESP32) || defined(USE_HOST)
91  return ::recvfrom(this->fd_, buf, len, 0, addr, addr_len);
92 #else
93  return ::lwip_recvfrom(this->fd_, buf, len, 0, addr, addr_len);
94 #endif
95  }
96  ssize_t readv(const struct iovec *iov, int iovcnt) override {
97 #if defined(USE_ESP32)
98  return ::lwip_readv(fd_, iov, iovcnt);
99 #else
100  return ::readv(fd_, iov, iovcnt);
101 #endif
102  }
103  ssize_t write(const void *buf, size_t len) override { return ::write(fd_, buf, len); }
104  ssize_t send(void *buf, size_t len, int flags) { return ::send(fd_, buf, len, flags); }
105  ssize_t writev(const struct iovec *iov, int iovcnt) override {
106 #if defined(USE_ESP32)
107  return ::lwip_writev(fd_, iov, iovcnt);
108 #else
109  return ::writev(fd_, iov, iovcnt);
110 #endif
111  }
112 
113  ssize_t sendto(const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen) override {
114  return ::sendto(fd_, buf, len, flags, to, tolen);
115  }
116 
117  int setblocking(bool blocking) override {
118  int fl = ::fcntl(fd_, F_GETFL, 0);
119  if (blocking) {
120  fl &= ~O_NONBLOCK;
121  } else {
122  fl |= O_NONBLOCK;
123  }
124  ::fcntl(fd_, F_SETFL, fl);
125  return 0;
126  }
127 
128  protected:
129  int fd_;
130  bool closed_ = false;
131 };
132 
133 std::unique_ptr<Socket> socket(int domain, int type, int protocol) {
134  int ret = ::socket(domain, type, protocol);
135  if (ret == -1)
136  return nullptr;
137  return std::unique_ptr<Socket>{new BSDSocketImpl(ret)};
138 }
139 
140 } // namespace socket
141 } // namespace esphome
142 
143 #endif // USE_SOCKET_IMPL_BSD_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.