Skip to content
45 changes: 28 additions & 17 deletions libraries/SocketWrapper/SocketWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,40 @@
#endif

#include <zephyr/net/socket.h>
#include <memory>
#include <cstring>

class ZephyrSocketWrapper {
protected:
int* sock_fd = nullptr;
std::shared_ptr<int> sock_fd;
bool is_ssl = false;
int ssl_sock_temp_char = -1;

public:
ZephyrSocketWrapper() {
sock_fd = new int(-1);
// custom deleter for shared_ptr to close automatically the socket
static auto socket_deleter() {
return [](int *fd) {
if (fd && *fd != -1) {
::close(*fd);
delete fd;
}
};
}

ZephyrSocketWrapper(int fd) {
sock_fd = new int(fd);
}
public:
ZephyrSocketWrapper() = default;

~ZephyrSocketWrapper() {
if (sock_fd && *sock_fd != -1) {
::close(*sock_fd);
}
delete sock_fd;
sock_fd = nullptr;
ZephyrSocketWrapper(int fd) : sock_fd(std::shared_ptr<int>(fd<0 ? nullptr : new int(fd), socket_deleter())) {
}

~ZephyrSocketWrapper() = default; // socket close managed by shared_ptr

bool connect(const char *host, uint16_t port) {

// Resolve address
struct addrinfo hints = {0};
struct addrinfo *res = nullptr;
bool rv = true;
int raw_sock_fd;

hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
Expand All @@ -59,7 +63,8 @@ class ZephyrSocketWrapper {
goto exit;
}

*sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
raw_sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sock_fd = std::shared_ptr<int>(raw_sock_fd < 0 ? nullptr : new int(raw_sock_fd), socket_deleter());
if (!sock_fd || *sock_fd < 0) {
rv = false;

Expand All @@ -85,13 +90,15 @@ class ZephyrSocketWrapper {
bool connect(IPAddress host, uint16_t port) {

const char *_host = host.toString().c_str();
int raw_sock_fd;

struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
inet_pton(AF_INET, _host, &addr.sin_addr);

*sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
raw_sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sock_fd = std::shared_ptr<int>(raw_sock_fd < 0 ? nullptr : new int(raw_sock_fd), socket_deleter());
if (!sock_fd || *sock_fd < 0) {
return false;
}
Expand All @@ -118,6 +125,7 @@ class ZephyrSocketWrapper {
int resolve_attempts = 100;
int ret;
bool rv = false;
int raw_sock_fd;

sec_tag_t sec_tag_opt[] = {
CA_CERTIFICATE_TAG,
Expand Down Expand Up @@ -150,7 +158,8 @@ class ZephyrSocketWrapper {
}
}

*sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TLS_1_2);
raw_sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TLS_1_2);
sock_fd = std::shared_ptr<int>(raw_sock_fd < 0 ? nullptr : new int(raw_sock_fd), socket_deleter());
if (!sock_fd || *sock_fd < 0) {
goto exit;
}
Expand Down Expand Up @@ -238,11 +247,13 @@ class ZephyrSocketWrapper {

bool bind(uint16_t port) {
struct sockaddr_in addr;
int raw_sock_fd;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY;

*sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
raw_sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sock_fd = std::shared_ptr<int>(raw_sock_fd < 0 ? nullptr : new int(raw_sock_fd), socket_deleter());
if (!sock_fd || *sock_fd < 0) {
return false;
}
Expand Down
6 changes: 1 addition & 5 deletions libraries/SocketWrapper/ZephyrClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ class ZephyrClient : public arduino::Client, ZephyrSocketWrapper {

protected:
void setSocket(int sock) {
if (sock_fd) {
*sock_fd = sock;
} else {
sock_fd = new int(sock);
}
sock_fd = std::shared_ptr<int>(sock < 0 ? nullptr : new int(sock), socket_deleter());
_connected = true;
}

Expand Down