From 44be4e1cc8fac30f6bfe2c203c1d970fa0af5e35 Mon Sep 17 00:00:00 2001 From: Juliusz Sosinowicz Date: Thu, 12 May 2022 16:48:04 +0200 Subject: [PATCH] Reset ret in client and server after wolfSSL_dtls_got_timeout() - Do UDP connect only with simulateWantWrite to accommodate macOS that doesn't like sendto being called on connected UDP sockets - Call wolfSSL_dtls_get_current_timeout only on a DTLS connection --- examples/client/client.c | 14 +++++++++++--- examples/server/server.c | 4 +++- wolfssl/test.h | 12 ++++++++---- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/examples/client/client.c b/examples/client/client.c index acafd71a8..d0935fcfd 100644 --- a/examples/client/client.c +++ b/examples/client/client.c @@ -201,7 +201,8 @@ static int NonBlockingSSL_Connect(WOLFSSL* ssl) else { #ifdef WOLFSSL_DTLS - currTimeout = wolfSSL_dtls_get_current_timeout(ssl); + if (wolfSSL_dtls(ssl)) + currTimeout = wolfSSL_dtls_get_current_timeout(ssl); #endif select_ret = tcp_select(sockfd, currTimeout); } @@ -238,6 +239,7 @@ static int NonBlockingSSL_Connect(WOLFSSL* ssl) error = wolfSSL_get_error(ssl, ret); else error = WOLFSSL_ERROR_WANT_READ; + ret = WOLFSSL_FAILURE; /* Reset error so we loop */ } #endif else { @@ -3565,7 +3567,10 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args) } if (simulateWantWrite) { - wolfSSL_SetIOWriteCtx(ssl, (void*)&sockfd); + if (dtlsUDP) { + wolfSSL_SetIOWriteCtx(ssl, (void*)&sockfd); + udp_connect(&sockfd, host, port); + } } /* STARTTLS */ @@ -4105,7 +4110,10 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args) err_sys("error in setting fd"); } if (simulateWantWrite) { - wolfSSL_SetIOWriteCtx(ssl, (void*)&sockfd); + if (dtlsUDP) { + wolfSSL_SetIOWriteCtx(ssl, (void*)&sockfd); + udp_connect(&sockfd, host, port); + } } #ifdef HAVE_ALPN if (alpnList != NULL) { diff --git a/examples/server/server.c b/examples/server/server.c index bd7f6db4f..e69d8183b 100644 --- a/examples/server/server.c +++ b/examples/server/server.c @@ -330,7 +330,8 @@ static int NonBlockingSSL_Accept(SSL* ssl) } else { #ifdef WOLFSSL_DTLS - currTimeout = wolfSSL_dtls_get_current_timeout(ssl); + if (wolfSSL_dtls(ssl)) + currTimeout = wolfSSL_dtls_get_current_timeout(ssl); #endif select_ret = tcp_select(sockfd, currTimeout); } @@ -360,6 +361,7 @@ static int NonBlockingSSL_Accept(SSL* ssl) error = wolfSSL_get_error(ssl, ret); else error = WOLFSSL_ERROR_WANT_READ; + ret = WOLFSSL_FAILURE; /* Reset error so we loop */ } #endif else { diff --git a/wolfssl/test.h b/wolfssl/test.h index 471c990f0..daa739c4d 100644 --- a/wolfssl/test.h +++ b/wolfssl/test.h @@ -1847,16 +1847,20 @@ static WC_INLINE void tcp_connect(SOCKET_T* sockfd, const char* ip, word16 port, } tcp_socket(sockfd, udp, sctp); - if (connect(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0) - err_sys_with_errno("tcp connect failed"); + if (!udp) { + if (connect(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0) + err_sys_with_errno("tcp connect failed"); + } } #endif /* WOLFSSL_WOLFSENTRY_HOOKS */ -static WC_INLINE void udp_connect(SOCKET_T* sockfd, void* addr, int addrSz) +static WC_INLINE void udp_connect(SOCKET_T* sockfd, const char* ip, word16 port) { - if (connect(*sockfd, (const struct sockaddr*)addr, addrSz) != 0) + SOCKADDR_IN_T addr; + build_addr(&addr, ip, port, 1, 0); + if (connect(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0) err_sys_with_errno("tcp connect failed"); }