Skip to content

Commit af6ee6b

Browse files
committed
libraries/SocketWrapper: Fix agrs and improve error handling.
- Fix socket timeout arg to use proper struct timeval - Initialize addrinfo structs to prevent undefined behavior - Add error checking for tls_credential_add() and setsockopt() calls - Centralize socket cleanup in error path - Change default return value to false for safer error handling - Change cert args to const Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
1 parent 69260c8 commit af6ee6b

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

libraries/SocketWrapper/SocketWrapper.h

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class ZephyrSocketWrapper {
3030
bool connect(const char *host, uint16_t port) {
3131

3232
// Resolve address
33-
struct addrinfo hints;
34-
struct addrinfo *res;
33+
struct addrinfo hints = {0};
34+
struct addrinfo *res = nullptr;
3535
bool rv = true;
3636

3737
hints.ai_family = AF_INET;
@@ -102,24 +102,27 @@ class ZephyrSocketWrapper {
102102
}
103103

104104
#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
105-
bool connectSSL(const char *host, uint16_t port, char *ca_certificate_pem = nullptr) {
105+
bool connectSSL(const char *host, uint16_t port, const char *ca_certificate_pem = nullptr) {
106106

107107
// Resolve address
108-
struct addrinfo hints;
109-
struct addrinfo *res;
108+
struct addrinfo hints = {0};
109+
struct addrinfo *res = nullptr;
110110

111111
hints.ai_family = AF_INET;
112112
hints.ai_socktype = SOCK_STREAM;
113113

114114
int resolve_attempts = 100;
115115
int ret;
116-
bool rv = true;
116+
bool rv = false;
117117

118118
sec_tag_t sec_tag_opt[] = {
119119
CA_CERTIFICATE_TAG,
120120
};
121121

122-
uint32_t timeo_optval = 100;
122+
struct timeval timeout_opt = {
123+
.tv_sec = 0,
124+
.tv_usec = 100000,
125+
};
123126

124127
while (resolve_attempts--) {
125128
ret = getaddrinfo(host, String(port).c_str(), &hints, &res);
@@ -132,33 +135,33 @@ class ZephyrSocketWrapper {
132135
}
133136

134137
if (ret != 0) {
135-
rv = false;
136138
goto exit;
137139
}
138140

139141
if (ca_certificate_pem != nullptr) {
140142
ret = tls_credential_add(CA_CERTIFICATE_TAG, TLS_CREDENTIAL_CA_CERTIFICATE,
141143
ca_certificate_pem, strlen(ca_certificate_pem) + 1);
144+
if (ret != 0) {
145+
goto exit;
146+
}
142147
}
143148

144149
sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TLS_1_2);
145150
if (sock_fd < 0) {
146-
rv = false;
147151
goto exit;
148152
}
149153

150-
setsockopt(sock_fd, SOL_TLS, TLS_SEC_TAG_LIST, sec_tag_opt, sizeof(sec_tag_opt));
151-
152-
setsockopt(sock_fd, SOL_TLS, TLS_HOSTNAME, host, strlen(host));
153-
154-
setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &timeo_optval, sizeof(timeo_optval));
154+
if (setsockopt(sock_fd, SOL_TLS, TLS_HOSTNAME, host, strlen(host)) ||
155+
setsockopt(sock_fd, SOL_TLS, TLS_SEC_TAG_LIST, sec_tag_opt, sizeof(sec_tag_opt)) ||
156+
setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &timeout_opt, sizeof(timeout_opt))) {
157+
goto exit;
158+
}
155159

156160
if (::connect(sock_fd, res->ai_addr, res->ai_addrlen) < 0) {
157-
::close(sock_fd);
158-
sock_fd = -1;
159-
rv = false;
160161
goto exit;
161162
}
163+
164+
rv = true;
162165
is_ssl = true;
163166

164167
exit:
@@ -167,6 +170,10 @@ class ZephyrSocketWrapper {
167170
res = nullptr;
168171
}
169172

173+
if (!rv && sock_fd >= 0) {
174+
::close(sock_fd);
175+
sock_fd = -1;
176+
}
170177
return rv;
171178
}
172179
#endif

libraries/SocketWrapper/ZephyrClient.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ class ZephyrClient : public arduino::Client, ZephyrSocketWrapper {
3232
return ret;
3333
}
3434
#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
35-
int connectSSL(const char *host, uint16_t port, char *cert) {
36-
auto ret = ZephyrSocketWrapper::connectSSL((char *)host, port, cert);
35+
int connectSSL(const char *host, uint16_t port, const char *cert) {
36+
auto ret = ZephyrSocketWrapper::connectSSL(host, port, cert);
3737
if (ret) {
3838
_connected = true;
3939
}

libraries/SocketWrapper/ZephyrSSLClient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ZephyrSSLClient : public ZephyrClient {
1414
return connectSSL(host, port, nullptr);
1515
}
1616

17-
int connect(const char *host, uint16_t port, char *cert) {
17+
int connect(const char *host, uint16_t port, const char *cert) {
1818
return connectSSL(host, port, cert);
1919
}
2020
};

0 commit comments

Comments
 (0)