ESPHome  2023.11.6
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 readv(const struct iovec *iov, int iovcnt) override {
90 #if defined(USE_ESP32)
91  return ::lwip_readv(fd_, iov, iovcnt);
92 #else
93  return ::readv(fd_, iov, iovcnt);
94 #endif
95  }
96  ssize_t write(const void *buf, size_t len) override { return ::write(fd_, buf, len); }
97  ssize_t send(void *buf, size_t len, int flags) { return ::send(fd_, buf, len, flags); }
98  ssize_t writev(const struct iovec *iov, int iovcnt) override {
99 #if defined(USE_ESP32)
100  return ::lwip_writev(fd_, iov, iovcnt);
101 #else
102  return ::writev(fd_, iov, iovcnt);
103 #endif
104  }
105 
106  ssize_t sendto(const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen) override {
107  return ::sendto(fd_, buf, len, flags, to, tolen);
108  }
109 
110  int setblocking(bool blocking) override {
111  int fl = ::fcntl(fd_, F_GETFL, 0);
112  if (blocking) {
113  fl &= ~O_NONBLOCK;
114  } else {
115  fl |= O_NONBLOCK;
116  }
117  ::fcntl(fd_, F_SETFL, fl);
118  return 0;
119  }
120 
121  protected:
122  int fd_;
123  bool closed_ = false;
124 };
125 
126 std::unique_ptr<Socket> socket(int domain, int type, int protocol) {
127  int ret = ::socket(domain, type, protocol);
128  if (ret == -1)
129  return nullptr;
130  return std::unique_ptr<Socket>{new BSDSocketImpl(ret)};
131 }
132 
133 } // namespace socket
134 } // namespace esphome
135 
136 #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:292
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.