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 int connect(
const struct sockaddr *addr,
socklen_t addrlen)
override { return ::connect(fd_, addr, addrlen); }
50 std::unique_ptr<Socket> accept(
struct sockaddr *addr,
socklen_t *addrlen)
override {
51 int fd = ::accept(fd_, addr, addrlen);
54 return make_unique<BSDSocketImpl>(fd);
56 int bind(
const struct sockaddr *addr,
socklen_t addrlen)
override { return ::bind(fd_, addr, addrlen); }
57 int close()
override {
58 int ret = ::close(fd_);
62 int shutdown(
int how)
override { return ::shutdown(fd_, how); }
64 int getpeername(
struct sockaddr *addr,
socklen_t *addrlen)
override { return ::getpeername(fd_, addr, addrlen); }
65 std::string getpeername()
override {
68 int err = this->getpeername((
struct sockaddr *) &storage, &len);
73 int getsockname(
struct sockaddr *addr,
socklen_t *addrlen)
override { return ::getsockname(fd_, addr, addrlen); }
74 std::string getsockname()
override {
77 int err = this->getsockname((
struct sockaddr *) &storage, &len);
82 int getsockopt(
int level,
int optname,
void *optval,
socklen_t *optlen)
override {
83 return ::getsockopt(fd_, level, optname, optval, optlen);
85 int setsockopt(
int level,
int optname,
const void *optval,
socklen_t optlen)
override {
86 return ::setsockopt(fd_, level, optname, optval, optlen);
88 int listen(
int backlog)
override { return ::listen(fd_, backlog); }
89 ssize_t read(
void *buf,
size_t len)
override { return ::read(fd_, buf, len); }
90 ssize_t recvfrom(
void *buf,
size_t len,
sockaddr *addr,
socklen_t *addr_len)
override {
91 #if defined(USE_ESP32) || defined(USE_HOST) 92 return ::recvfrom(this->fd_, buf, len, 0, addr, addr_len);
94 return ::lwip_recvfrom(this->fd_, buf, len, 0, addr, addr_len);
97 ssize_t readv(
const struct iovec *iov,
int iovcnt)
override {
98 #if defined(USE_ESP32) 99 return ::lwip_readv(fd_, iov, iovcnt);
101 return ::readv(fd_, iov, iovcnt);
104 ssize_t write(
const void *buf,
size_t len)
override { return ::write(fd_, buf, len); }
105 ssize_t send(
void *buf,
size_t len,
int flags) { return ::send(fd_, buf, len, flags); }
106 ssize_t writev(
const struct iovec *iov,
int iovcnt)
override {
107 #if defined(USE_ESP32) 108 return ::lwip_writev(fd_, iov, iovcnt);
110 return ::writev(fd_, iov, iovcnt);
114 ssize_t sendto(
const void *buf,
size_t len,
int flags,
const struct sockaddr *to,
socklen_t tolen)
override {
115 return ::sendto(fd_, buf, len, flags, to, tolen);
118 int setblocking(
bool blocking)
override {
119 int fl = ::fcntl(fd_, F_GETFL, 0);
125 ::fcntl(fd_, F_SETFL, fl);
131 bool closed_ =
false;
134 std::unique_ptr<Socket>
socket(
int domain,
int type,
int protocol) {
135 int ret =
::socket(domain, type, protocol);
138 return std::unique_ptr<Socket>{
new BSDSocketImpl(ret)};
144 #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.