5 #ifdef USE_SOCKET_IMPL_BSD_SOCKETS 10 #include <esp_idf_version.h> 11 #include <lwip/sockets.h> 20 char buf[INET_ADDRSTRLEN];
21 if (inet_ntop(AF_INET, &addr->
sin_addr, buf,
sizeof(buf)) !=
nullptr)
22 return std::string{buf};
27 char buf[INET6_ADDRSTRLEN];
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};
34 if (inet_ntop(AF_INET6, &addr->
sin6_addr, buf,
sizeof(buf)) !=
nullptr)
35 return std::string{buf};
41 class BSDSocketImpl :
public Socket {
43 BSDSocketImpl(
int fd) : fd_(fd) {}
44 ~BSDSocketImpl()
override {
49 std::unique_ptr<Socket> accept(
struct sockaddr *addr,
socklen_t *addrlen)
override {
50 int fd = ::accept(fd_, addr, addrlen);
53 return make_unique<BSDSocketImpl>(fd);
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_);
61 int shutdown(
int how)
override { return ::shutdown(fd_, how); }
63 int getpeername(
struct sockaddr *addr,
socklen_t *addrlen)
override { return ::getpeername(fd_, addr, addrlen); }
64 std::string getpeername()
override {
67 int err = this->getpeername((
struct sockaddr *) &storage, &len);
72 int getsockname(
struct sockaddr *addr,
socklen_t *addrlen)
override { return ::getsockname(fd_, addr, addrlen); }
73 std::string getsockname()
override {
76 int err = this->getsockname((
struct sockaddr *) &storage, &len);
81 int getsockopt(
int level,
int optname,
void *optval,
socklen_t *optlen)
override {
82 return ::getsockopt(fd_, level, optname, optval, optlen);
84 int setsockopt(
int level,
int optname,
const void *optval,
socklen_t optlen)
override {
85 return ::setsockopt(fd_, level, optname, optval, optlen);
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);
93 return ::lwip_recvfrom(this->fd_, buf, len, 0, addr, addr_len);
96 ssize_t readv(
const struct iovec *iov,
int iovcnt)
override {
97 #if defined(USE_ESP32) 98 return ::lwip_readv(fd_, iov, iovcnt);
100 return ::readv(fd_, iov, iovcnt);
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);
109 return ::writev(fd_, iov, iovcnt);
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);
117 int setblocking(
bool blocking)
override {
118 int fl = ::fcntl(fd_, F_GETFL, 0);
124 ::fcntl(fd_, F_SETFL, fl);
130 bool closed_ =
false;
133 std::unique_ptr<Socket>
socket(
int domain,
int type,
int protocol) {
134 int ret =
::socket(domain, type, protocol);
137 return std::unique_ptr<Socket>{
new BSDSocketImpl(ret)};
143 #endif // USE_SOCKET_IMPL_BSD_SOCKETS
Implementation of SPI Controller mode.
std::string format_sockaddr(const struct sockaddr_storage &storage)
struct in6_addr sin6_addr
std::unique_ptr< Socket > socket(int domain, int type, int protocol)
Create a socket of the given domain, type and protocol.