Skip to content

Commit 9bc3022

Browse files
authored
Merge pull request #572 from padelsbach/minor-fenrir-fixes
Add error checking and cleanup in examples
2 parents 6437dcd + 6d31699 commit 9bc3022

4 files changed

Lines changed: 38 additions & 4 deletions

File tree

dtls/server-dtls-ipv6.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ int main(int argc, char** argv)
118118
if ((listenfd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0 ) {
119119
printf("Cannot create socket.\n");
120120
cleanup = 1;
121+
cont = 1;
122+
break;
121123
}
122124
printf("Socket allocated\n");
123125

@@ -135,13 +137,17 @@ int main(int argc, char** argv)
135137
printf("Setsockopt SO_REUSEADDR failed.\n");
136138
cleanup = 1;
137139
cont = 1;
140+
close(listenfd);
141+
break;
138142
}
139143

140144
/*Bind Socket*/
141145
if (bind(listenfd, (struct sockaddr*)&servAddr, sizeof(servAddr)) < 0) {
142146
printf("Bind failed.\n");
143147
cleanup = 1;
144148
cont = 1;
149+
close(listenfd);
150+
break;
145151
}
146152

147153
printf("Awaiting client connection on port %d\n", SERV_PORT);
@@ -151,7 +157,8 @@ int main(int argc, char** argv)
151157
(struct sockaddr*)&cliaddr, &cliLen);
152158

153159
if (bytesReceived < 0) {
154-
printf("No clients in que, enter idle state\n");
160+
printf("No clients in queue, enter idle state\n");
161+
close(listenfd);
155162
continue;
156163
}
157164
else if (bytesReceived > 0) {
@@ -160,12 +167,16 @@ int main(int argc, char** argv)
160167
printf("Udp connect failed.\n");
161168
cleanup = 1;
162169
cont = 1;
170+
close(listenfd);
171+
break;
163172
}
164173
}
165174
else {
166175
printf("Recvfrom failed.\n");
167176
cleanup = 1;
168177
cont = 1;
178+
close(listenfd);
179+
break;
169180
}
170181
printf("Connected!\n");
171182

@@ -174,6 +185,8 @@ int main(int argc, char** argv)
174185
printf("wolfSSL_new error.\n");
175186
cleanup = 1;
176187
cont = 1;
188+
close(listenfd);
189+
break;
177190
}
178191

179192
/* set the session ssl to client connection port */
@@ -185,6 +198,8 @@ int main(int argc, char** argv)
185198

186199
printf("error = %d, %s\n", e, wolfSSL_ERR_reason_error_string(e));
187200
printf("SSL_accept failed.\n");
201+
wolfSSL_free(ssl);
202+
close(listenfd);
188203
continue;
189204
}
190205
if ((recvLen = wolfSSL_read(ssl, buff, sizeof(buff)-1)) > 0) {
@@ -215,6 +230,7 @@ int main(int argc, char** argv)
215230
wolfSSL_set_fd(ssl, 0);
216231
wolfSSL_shutdown(ssl);
217232
wolfSSL_free(ssl);
233+
close(listenfd);
218234

219235
printf("Client left cont to idle state\n");
220236
cont = 0;

dtls/server-dtls-nonblocking.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ int main(int argc, char** argv)
147147
if ((listenfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
148148
printf("Cannot create socket.\n");
149149
cont = 1;
150+
break;
150151
}
151152

152153
printf("Socket allocated\n");
@@ -162,6 +163,11 @@ int main(int argc, char** argv)
162163
cleanup = 1;
163164
}
164165

166+
if (cleanup == 1) {
167+
close(listenfd);
168+
break;
169+
}
170+
165171
/* Clear servAddr each loop */
166172
memset((char *)&servAddr, 0, sizeof(servAddr));
167173

@@ -176,13 +182,17 @@ int main(int argc, char** argv)
176182
if (res < 0) {
177183
printf("Setsockopt SO_REUSEADDR failed.\n");
178184
cont = 1;
185+
close(listenfd);
186+
break;
179187
}
180188

181189
/*Bind Socket*/
182190
if (bind(listenfd,
183191
(struct sockaddr *)&servAddr, sizeof(servAddr)) < 0) {
184192
printf("Bind failed.\n");
185193
cont = 1;
194+
close(listenfd);
195+
break;
186196
}
187197

188198
printf("Awaiting client connection on port %d\n", SERV_PORT);
@@ -192,12 +202,17 @@ int main(int argc, char** argv)
192202
do {
193203
if (cleanup == 1) {
194204
cont = 1;
205+
close(listenfd);
195206
break;
196207
}
197208
bytesRecvd = (int)recvfrom(listenfd, (char*)b, sizeof(b), MSG_PEEK,
198209
(struct sockaddr*)&cliAddr, &clilen);
199210
} while (bytesRecvd <= 0);
200211

212+
if (cont == 1) {
213+
break;
214+
}
215+
201216
if (bytesRecvd > 0) {
202217
if (connect(listenfd, (const struct sockaddr*)&cliAddr,
203218
sizeof(cliAddr)) != 0) {
@@ -217,6 +232,8 @@ int main(int argc, char** argv)
217232
if (( ssl = wolfSSL_new(ctx)) == NULL) {
218233
printf("wolfSSL_new error.\n");
219234
cont = 1;
235+
close(listenfd);
236+
break;
220237
}
221238

222239
/* set clilen to |cliAddr| */

psk/server-psk-threaded.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ void* wolfssl_thread(void* fd)
8282

8383
/* create WOLFSSL object */
8484
if ((ssl = wolfSSL_new(ctx)) == NULL) {
85-
printf("Fatal error : wolfSSL_new error");
86-
/* place signal for forced error exit here */
85+
printf("Fatal error : wolfSSL_new error\n");
86+
close(connfd);
87+
pthread_exit(NULL);
8788
}
8889

8990
wolfSSL_set_fd(ssl, connfd);

tls/client-tls13-resume.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ int main(int argc, char** argv)
358358
}
359359

360360
#ifdef HAVE_SECRET_CALLBACK
361-
wolfSSL_FreeArrays(ssl);
361+
wolfSSL_FreeArrays(sslRes);
362362
#endif
363363

364364

0 commit comments

Comments
 (0)