Skip to content

Commit 5902b13

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 Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
1 parent 5f0e907 commit 5902b13

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

libraries/SocketWrapper/SocketWrapper.h

Lines changed: 23 additions & 16 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;
@@ -105,21 +105,24 @@ class ZephyrSocketWrapper {
105105
bool connectSSL(const char *host, uint16_t port, 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

0 commit comments

Comments
 (0)