Skip to content

Commit bf1d5c7

Browse files
committed
Improve the TLS bidirectional shutdown
1 parent cb1e647 commit bf1d5c7

3 files changed

Lines changed: 48 additions & 10 deletions

File tree

examples/tls/tls_client.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -639,9 +639,17 @@ int TPM2_TLS_ClientArgs(void* userCtx, int argc, char *argv[])
639639
printf("Failure %d (0x%x): %s\n", rc, rc, wolfTPM2_GetRCString(rc));
640640
}
641641

642-
/* Bidirectional shutdown */
643-
while (wolfSSL_shutdown(ssl) == WOLFSSL_SHUTDOWN_NOT_DONE) {
644-
printf("Shutdown not complete\n");
642+
if (wolfSSL_shutdown(ssl) == WOLFSSL_SHUTDOWN_NOT_DONE) {
643+
/* Bidirectional shutdown */
644+
if (SocketWaitData(&sockIoCtx, 2 /* seconds */) == 1) {
645+
int ret = wolfSSL_shutdown(ssl);
646+
if (ret == WOLFSSL_SUCCESS) {
647+
printf("Bidirectional shutdown complete\n");
648+
}
649+
else if (ret != WOLFSSL_SHUTDOWN_NOT_DONE) {
650+
fprintf(stderr, "Bidirectional shutdown failed\n");
651+
}
652+
}
645653
}
646654

647655
wolfSSL_free(ssl);

examples/tls/tls_common.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,32 @@ static inline int SetupSocketAndConnect(SockIoCbCtx* sockIoCtx, const char* host
322322
return 0;
323323
}
324324

325+
static inline int SocketWaitData(SockIoCbCtx* sockIoCtx, int timeout_sec)
326+
{
327+
int res;
328+
struct timeval timeout;
329+
fd_set fds, errfds;
330+
FD_ZERO(&fds);
331+
FD_ZERO(&errfds);
332+
FD_SET(sockIoCtx->fd, &fds);
333+
FD_SET(sockIoCtx->fd, &errfds);
334+
timeout.tv_sec = timeout_sec;
335+
timeout.tv_usec = 0;
336+
res = select(sockIoCtx->fd + 1, &fds, NULL, &errfds, &timeout);
337+
if (res == 0) {
338+
return 0; /* timeout */
339+
}
340+
else if (res > 0) {
341+
if (FD_ISSET(sockIoCtx->fd, &fds)) {
342+
return 1; /* ready to read */
343+
}
344+
else if (FD_ISSET(sockIoCtx->fd, &errfds)) {
345+
return -1; /* error */
346+
}
347+
}
348+
return 0; /* select failed */
349+
}
350+
325351
static inline void CloseAndCleanupSocket(SockIoCbCtx* sockIoCtx)
326352
{
327353
if (sockIoCtx->fd != -1) {
@@ -343,6 +369,7 @@ static inline void CloseAndCleanupSocket(SockIoCbCtx* sockIoCtx)
343369

344370
int SetupSocketAndListen(SockIoCbCtx* sockIoCtx, word32 port);
345371
int SocketWaitClient(SockIoCbCtx* sockIoCtx);
372+
int SocketWaitData(SockIoCbCtx* sockIoCtx, int timeout_sec);
346373
#endif /* !WOLFSSL_USER_IO */
347374

348375
/******************************************************************************/

examples/tls/tls_server.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -672,9 +672,17 @@ int TPM2_TLS_ServerArgs(void* userCtx, int argc, char *argv[])
672672
}
673673
}
674674

675+
if (wolfSSL_shutdown(ssl) == WOLFSSL_SHUTDOWN_NOT_DONE) {
675676
/* Bidirectional shutdown */
676-
while (wolfSSL_shutdown(ssl) == WOLFSSL_SHUTDOWN_NOT_DONE) {
677-
printf("Shutdown not complete\n");
677+
if (SocketWaitData(&sockIoCtx, 2 /* seconds */) == 1) {
678+
int ret = wolfSSL_shutdown(ssl);
679+
if (ret == WOLFSSL_SUCCESS) {
680+
printf("Bidirectional shutdown complete\n");
681+
}
682+
else if (ret != WOLFSSL_SHUTDOWN_NOT_DONE) {
683+
fprintf(stderr, "Bidirectional shutdown failed\n");
684+
}
685+
}
678686
}
679687

680688
wolfSSL_free(ssl);
@@ -691,11 +699,6 @@ int TPM2_TLS_ServerArgs(void* userCtx, int argc, char *argv[])
691699
}
692700

693701
if (ssl != NULL) {
694-
/* Bidirectional shutdown */
695-
while (wolfSSL_shutdown(ssl) == WOLFSSL_SHUTDOWN_NOT_DONE) {
696-
printf("Shutdown not complete\n");
697-
}
698-
699702
wolfSSL_free(ssl);
700703
}
701704
wolfSSL_CTX_free(ctx);

0 commit comments

Comments
 (0)