Skip to content

Added missing call to freeaddrinfo #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libraries/SocketWrapper/SocketHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,4 @@ class NetworkInterface {
// TODO: manual functions for setting IP address, subnet mask, gateway, etc.
// net_if_ipv4_set_netmask_by_addr(iface, &addr4, &nm);
// net_if_ipv4_addr_add(iface, &addr4, NET_ADDR_MANUAL, 0);
};
};
49 changes: 36 additions & 13 deletions libraries/SocketWrapper/SocketWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ZephyrSocketWrapper {
// Resolve address
struct addrinfo hints;
struct addrinfo *res;
bool rv = true;

hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
Expand All @@ -47,21 +48,31 @@ class ZephyrSocketWrapper {
}

if (ret != 0) {
return false;
rv = false;
goto exit;
}

sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock_fd < 0) {
return false;
rv = false;

goto exit;
}

if (::connect(sock_fd, res->ai_addr, res->ai_addrlen) < 0) {
::close(sock_fd);
sock_fd = -1;
return false;
rv = false;
goto exit;
}

return true;
exit:
if(res != nullptr) {
freeaddrinfo(res);
res = nullptr;
}

return rv;
}

bool connect(IPAddress host, uint16_t port) {
Expand Down Expand Up @@ -99,6 +110,13 @@ class ZephyrSocketWrapper {

int resolve_attempts = 100;
int ret;
bool rv = true;

sec_tag_t sec_tag_opt[] = {
CA_CERTIFICATE_TAG,
};

uint32_t timeo_optval = 100;

while (resolve_attempts--) {
ret = getaddrinfo(host, String(port).c_str(), &hints, &res);
Expand All @@ -111,7 +129,8 @@ class ZephyrSocketWrapper {
}

if (ret != 0) {
return false;
rv = false;
goto exit;
}

if (ca_certificate_pem != nullptr) {
Expand All @@ -121,28 +140,32 @@ class ZephyrSocketWrapper {

sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TLS_1_2);
if (sock_fd < 0) {
return false;
rv = false;
goto exit;
}

sec_tag_t sec_tag_opt[] = {
CA_CERTIFICATE_TAG,
};
setsockopt(sock_fd, SOL_TLS, TLS_SEC_TAG_LIST,
sec_tag_opt, sizeof(sec_tag_opt));

setsockopt(sock_fd, SOL_TLS, TLS_HOSTNAME, host, strlen(host));

uint32_t timeo_optval = 100;
setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &timeo_optval, sizeof(timeo_optval));

if (::connect(sock_fd, res->ai_addr, res->ai_addrlen) < 0) {
::close(sock_fd);
sock_fd = -1;
return false;
rv = false;
goto exit;
}
is_ssl = true;

return true;
exit:
if(res != nullptr) {
freeaddrinfo(res);
res = nullptr;
}

return rv;
}
#endif

Expand Down Expand Up @@ -243,4 +266,4 @@ class ZephyrSocketWrapper {
}

friend class ZephyrClient;
};
};
1 change: 1 addition & 0 deletions loader/llext_exports.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ FORCE_EXPORT_SYM(shared_multi_heap_free);

#if defined(CONFIG_NET_SOCKETS)
FORCE_EXPORT_SYM(getaddrinfo);
FORCE_EXPORT_SYM(freeaddrinfo)
FORCE_EXPORT_SYM(socket);
FORCE_EXPORT_SYM(connect);
FORCE_EXPORT_SYM(send);
Expand Down