From c3aedc940fffbbeacdff3f336e5543b3f48a0301 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Fri, 21 Sep 2012 09:36:01 -0700 Subject: [PATCH 1/2] improved dtls retry on connect --- examples/client/client.c | 20 +++++++++++++++++++- src/io.c | 7 +++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/examples/client/client.c b/examples/client/client.c index 3ed70b481..e5ac197d4 100644 --- a/examples/client/client.c +++ b/examples/client/client.c @@ -46,6 +46,7 @@ int ret = CyaSSL_connect_ex(ssl, handShakeCB, timeoutCB, timeout); #endif int error = CyaSSL_get_error(ssl, 0); + int timeout_count = CyaSSL_dtls_get_current_timeout(ssl) * 10; while (ret != SSL_SUCCESS && (error == SSL_ERROR_WANT_READ || error == SSL_ERROR_WANT_WRITE)) { if (error == SSL_ERROR_WANT_READ) @@ -55,7 +56,11 @@ #ifdef USE_WINDOWS_API Sleep(100); #else - sleep(1); + #ifdef CYASSL_DTLS + usleep(100000); /* 100ms */ + #else + sleep(1); + #endif #endif #ifndef CYASSL_CALLBACKS ret = CyaSSL_connect(ssl); @@ -63,6 +68,19 @@ ret = CyaSSL_connect_ex(ssl, handShakeCB, timeoutCB, timeout); #endif error = CyaSSL_get_error(ssl, 0); + #ifdef CYASSL_DTLS + if (timeout_count-- <= 0) { + timeout_count = CyaSSL_dtls_got_timeout(ssl); + if (timeout_count < 0) { + error = SSL_FATAL_ERROR; + } + else { + printf("... updating timeout\n"); + timeout_count = + CyaSSL_dtls_get_current_timeout(ssl) * 10; + } + } + #endif } if (ret != SSL_SUCCESS) err_sys("SSL_connect failed"); diff --git a/src/io.c b/src/io.c index 5fefbe051..1c146e5ab 100644 --- a/src/io.c +++ b/src/io.c @@ -90,18 +90,21 @@ #define SOCKET_ECONNRESET WSAECONNRESET #define SOCKET_EINTR WSAEINTR #define SOCKET_EPIPE WSAEPIPE + #define SOCKET_ECONNREFUSED WSAENOTCONN #elif defined(__PPU) #define SOCKET_EWOULDBLOCK SYS_NET_EWOULDBLOCK #define SOCKET_EAGAIN SYS_NET_EAGAIN #define SOCKET_ECONNRESET SYS_NET_ECONNRESET #define SOCKET_EINTR SYS_NET_EINTR #define SOCKET_EPIPE SYS_NET_EPIPE + #define SOCKET_ECONNREFUSED SYS_NET_ECONNREFUSED #else #define SOCKET_EWOULDBLOCK EWOULDBLOCK #define SOCKET_EAGAIN EAGAIN #define SOCKET_ECONNRESET ECONNRESET #define SOCKET_EINTR EINTR #define SOCKET_EPIPE EPIPE + #define SOCKET_ECONNREFUSED ECONNREFUSED #endif /* USE_WINDOWS_API */ @@ -176,6 +179,10 @@ int EmbedReceive(CYASSL *ssl, char *buf, int sz, void *ctx) CYASSL_MSG(" Socket interrupted"); return IO_ERR_ISR; } + else if (err == SOCKET_ECONNREFUSED) { + CYASSL_MSG(" Connection refused"); + return IO_ERR_WANT_READ; + } else { CYASSL_MSG(" General error"); return IO_ERR_GENERAL; From 9643e58dada2d4e46d7600eeb3596bccd6818bd0 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Fri, 21 Sep 2012 16:36:48 -0700 Subject: [PATCH 2/2] fixed bug for Windows build --- cyassl/test.h | 2 +- src/internal.c | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/cyassl/test.h b/cyassl/test.h index c1d51e00a..b2bae5bc8 100644 --- a/cyassl/test.h +++ b/cyassl/test.h @@ -495,7 +495,6 @@ static INLINE void tcp_accept(SOCKET_T* sockfd, int* clientfd, func_args* args, static INLINE void tcp_set_nonblocking(SOCKET_T* sockfd) { - (void)sockfd; #ifdef NON_BLOCKING #ifdef USE_WINDOWS_API unsigned long blocking = 1; @@ -505,6 +504,7 @@ static INLINE void tcp_set_nonblocking(SOCKET_T* sockfd) int ret = fcntl(*sockfd, F_SETFL, flags | O_NONBLOCK); #endif #endif + (void)sockfd; } diff --git a/src/internal.c b/src/internal.c index 7038ad0a4..21c292fc5 100644 --- a/src/internal.c +++ b/src/internal.c @@ -1588,6 +1588,11 @@ retry: return WANT_READ; case IO_ERR_CONN_RST: /* connection reset */ + #ifdef USE_WINDOWS_API + if (ssl->options.dtls) { + return WANT_READ; + } + #endif ssl->options.connReset = 1; return -1;