@@ -29,6 +29,7 @@ class ZephyrSocketWrapper {
29
29
// Resolve address
30
30
struct addrinfo hints;
31
31
struct addrinfo *res;
32
+ bool rv = true ;
32
33
33
34
hints.ai_family = AF_INET;
34
35
hints.ai_socktype = SOCK_STREAM;
@@ -47,21 +48,31 @@ class ZephyrSocketWrapper {
47
48
}
48
49
49
50
if (ret != 0 ) {
50
- return false ;
51
+ rv = false ;
52
+ goto exit;
51
53
}
52
54
53
55
sock_fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
54
56
if (sock_fd < 0 ) {
55
- return false ;
57
+ rv = false ;
58
+
59
+ goto exit;
56
60
}
57
61
58
62
if (::connect (sock_fd, res->ai_addr , res->ai_addrlen ) < 0 ) {
59
63
::close (sock_fd);
60
64
sock_fd = -1 ;
61
- return false ;
65
+ rv = false ;
66
+ goto exit;
62
67
}
63
68
64
- return true ;
69
+ exit:
70
+ if (res != nullptr ) {
71
+ freeaddrinfo (res);
72
+ res = nullptr ;
73
+ }
74
+
75
+ return rv;
65
76
}
66
77
67
78
bool connect (IPAddress host, uint16_t port) {
@@ -99,6 +110,13 @@ class ZephyrSocketWrapper {
99
110
100
111
int resolve_attempts = 100 ;
101
112
int ret;
113
+ bool rv = true ;
114
+
115
+ sec_tag_t sec_tag_opt[] = {
116
+ CA_CERTIFICATE_TAG,
117
+ };
118
+
119
+ uint32_t timeo_optval = 100 ;
102
120
103
121
while (resolve_attempts--) {
104
122
ret = getaddrinfo (host, String (port).c_str (), &hints, &res);
@@ -111,7 +129,8 @@ class ZephyrSocketWrapper {
111
129
}
112
130
113
131
if (ret != 0 ) {
114
- return false ;
132
+ rv = false ;
133
+ goto exit;
115
134
}
116
135
117
136
if (ca_certificate_pem != nullptr ) {
@@ -121,28 +140,32 @@ class ZephyrSocketWrapper {
121
140
122
141
sock_fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TLS_1_2);
123
142
if (sock_fd < 0 ) {
124
- return false ;
143
+ rv = false ;
144
+ goto exit;
125
145
}
126
146
127
- sec_tag_t sec_tag_opt[] = {
128
- CA_CERTIFICATE_TAG,
129
- };
130
147
setsockopt (sock_fd, SOL_TLS, TLS_SEC_TAG_LIST,
131
148
sec_tag_opt, sizeof (sec_tag_opt));
132
149
133
150
setsockopt (sock_fd, SOL_TLS, TLS_HOSTNAME, host, strlen (host));
134
151
135
- uint32_t timeo_optval = 100 ;
136
152
setsockopt (sock_fd, SOL_SOCKET, SO_RCVTIMEO, &timeo_optval, sizeof (timeo_optval));
137
153
138
154
if (::connect (sock_fd, res->ai_addr , res->ai_addrlen ) < 0 ) {
139
155
::close (sock_fd);
140
156
sock_fd = -1 ;
141
- return false ;
157
+ rv = false ;
158
+ goto exit;
142
159
}
143
160
is_ssl = true ;
144
161
145
- return true ;
162
+ exit:
163
+ if (res != nullptr ) {
164
+ freeaddrinfo (res);
165
+ res = nullptr ;
166
+ }
167
+
168
+ return rv;
146
169
}
147
170
#endif
148
171
0 commit comments