mirror of
https://github.com/wolfSSL/wolfssl.git
synced 2026-03-06 15:34:02 +01:00
Merge pull request #1566 from ejohnstown/tcp-timeout
Fix TCP with Timeout
This commit is contained in:
@@ -6318,8 +6318,8 @@ retry:
|
||||
ssl->options.isClosed = 1;
|
||||
return -1;
|
||||
|
||||
#ifdef WOLFSSL_DTLS
|
||||
case WOLFSSL_CBIO_ERR_TIMEOUT:
|
||||
#ifdef WOLFSSL_DTLS
|
||||
if (IsDtlsNotSctpMode(ssl) &&
|
||||
!ssl->options.handShakeDone &&
|
||||
DtlsMsgPoolTimeout(ssl) == 0 &&
|
||||
@@ -6327,8 +6327,8 @@ retry:
|
||||
|
||||
goto retry;
|
||||
}
|
||||
#endif
|
||||
return -1;
|
||||
#endif
|
||||
|
||||
default:
|
||||
return recvd;
|
||||
|
||||
53
src/ssl.c
53
src/ssl.c
@@ -736,14 +736,6 @@ int wolfSSL_get_fd(const WOLFSSL* ssl)
|
||||
}
|
||||
|
||||
|
||||
int wolfSSL_get_using_nonblock(WOLFSSL* ssl)
|
||||
{
|
||||
WOLFSSL_ENTER("wolfSSL_get_using_nonblock");
|
||||
WOLFSSL_LEAVE("wolfSSL_get_using_nonblock", ssl->options.usingNonblock);
|
||||
return ssl->options.usingNonblock;
|
||||
}
|
||||
|
||||
|
||||
int wolfSSL_dtls(WOLFSSL* ssl)
|
||||
{
|
||||
return ssl->options.dtls;
|
||||
@@ -751,13 +743,6 @@ int wolfSSL_dtls(WOLFSSL* ssl)
|
||||
|
||||
|
||||
#ifndef WOLFSSL_LEANPSK
|
||||
void wolfSSL_set_using_nonblock(WOLFSSL* ssl, int nonblock)
|
||||
{
|
||||
WOLFSSL_ENTER("wolfSSL_set_using_nonblock");
|
||||
ssl->options.usingNonblock = (nonblock != 0);
|
||||
}
|
||||
|
||||
|
||||
int wolfSSL_dtls_set_peer(WOLFSSL* ssl, void* peer, unsigned int peerSz)
|
||||
{
|
||||
#ifdef WOLFSSL_DTLS
|
||||
@@ -8272,13 +8257,47 @@ int wolfSSL_set_cipher_list(WOLFSSL* ssl, const char* list)
|
||||
}
|
||||
|
||||
|
||||
int wolfSSL_dtls_get_using_nonblock(WOLFSSL* ssl)
|
||||
{
|
||||
int useNb = 0;
|
||||
|
||||
WOLFSSL_ENTER("wolfSSL_dtls_get_using_nonblock");
|
||||
if (ssl->options.dtls) {
|
||||
#ifdef WOLFSSL_DTLS
|
||||
useNb = ssl->options.dtlsUseNonblock;
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
WOLFSSL_MSG("wolfSSL_dtls_get_using_nonblock() is "
|
||||
"DEPRECATED for non-DTLS use.");
|
||||
}
|
||||
return useNb;
|
||||
}
|
||||
|
||||
|
||||
#ifndef WOLFSSL_LEANPSK
|
||||
|
||||
void wolfSSL_dtls_set_using_nonblock(WOLFSSL* ssl, int nonblock)
|
||||
{
|
||||
(void)nonblock;
|
||||
|
||||
WOLFSSL_ENTER("wolfSSL_dtls_set_using_nonblock");
|
||||
if (ssl->options.dtls) {
|
||||
#ifdef WOLFSSL_DTLS
|
||||
ssl->options.dtlsUseNonblock = (nonblock != 0);
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
WOLFSSL_MSG("wolfSSL_dtls_set_using_nonblock() is "
|
||||
"DEPRECATED for non-DTLS use.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#ifdef WOLFSSL_DTLS
|
||||
|
||||
int wolfSSL_dtls_get_current_timeout(WOLFSSL* ssl)
|
||||
{
|
||||
(void)ssl;
|
||||
|
||||
return ssl->dtls_timeout;
|
||||
}
|
||||
|
||||
|
||||
47
src/wolfio.c
47
src/wolfio.c
@@ -194,41 +194,14 @@ int EmbedReceive(WOLFSSL *ssl, char *buf, int sz, void *ctx)
|
||||
int sd = *(int*)ctx;
|
||||
int recvd;
|
||||
|
||||
#ifdef WOLFSSL_DTLS
|
||||
{
|
||||
int dtls_timeout = wolfSSL_dtls_get_current_timeout(ssl);
|
||||
if (wolfSSL_dtls(ssl)
|
||||
&& !wolfSSL_get_using_nonblock(ssl)
|
||||
&& dtls_timeout != 0) {
|
||||
#ifdef USE_WINDOWS_API
|
||||
DWORD timeout = dtls_timeout * 1000;
|
||||
#else
|
||||
struct timeval timeout;
|
||||
XMEMSET(&timeout, 0, sizeof(timeout));
|
||||
timeout.tv_sec = dtls_timeout;
|
||||
#endif
|
||||
if (setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout,
|
||||
sizeof(timeout)) != 0) {
|
||||
WOLFSSL_MSG("setsockopt rcvtimeo failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
recvd = wolfIO_Recv(sd, buf, sz, ssl->rflags);
|
||||
if (recvd < 0) {
|
||||
int err = wolfSSL_LastError();
|
||||
WOLFSSL_MSG("Embed Receive error");
|
||||
|
||||
if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) {
|
||||
if (!wolfSSL_dtls(ssl) || wolfSSL_get_using_nonblock(ssl)) {
|
||||
WOLFSSL_MSG("\tWould block");
|
||||
return WOLFSSL_CBIO_ERR_WANT_READ;
|
||||
}
|
||||
else {
|
||||
WOLFSSL_MSG("\tSocket timeout");
|
||||
return WOLFSSL_CBIO_ERR_TIMEOUT;
|
||||
}
|
||||
WOLFSSL_MSG("\tWould block");
|
||||
return WOLFSSL_CBIO_ERR_WANT_READ;
|
||||
}
|
||||
else if (err == SOCKET_ECONNRESET) {
|
||||
WOLFSSL_MSG("\tConnection reset");
|
||||
@@ -238,10 +211,6 @@ int EmbedReceive(WOLFSSL *ssl, char *buf, int sz, void *ctx)
|
||||
WOLFSSL_MSG("\tSocket interrupted");
|
||||
return WOLFSSL_CBIO_ERR_ISR;
|
||||
}
|
||||
else if (err == SOCKET_ECONNREFUSED) {
|
||||
WOLFSSL_MSG("\tConnection refused");
|
||||
return WOLFSSL_CBIO_ERR_WANT_READ;
|
||||
}
|
||||
else if (err == SOCKET_ECONNABORTED) {
|
||||
WOLFSSL_MSG("\tConnection aborted");
|
||||
return WOLFSSL_CBIO_ERR_CONN_CLOSE;
|
||||
@@ -348,7 +317,7 @@ int EmbedReceiveFrom(WOLFSSL *ssl, char *buf, int sz, void *ctx)
|
||||
WOLFSSL_MSG("Embed Receive From error");
|
||||
|
||||
if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) {
|
||||
if (wolfSSL_get_using_nonblock(ssl)) {
|
||||
if (wolfSSL_dtls_get_using_nonblock(ssl)) {
|
||||
WOLFSSL_MSG("\tWould block");
|
||||
return WOLFSSL_CBIO_ERR_WANT_READ;
|
||||
}
|
||||
@@ -459,7 +428,7 @@ int EmbedReceiveFromMcast(WOLFSSL *ssl, char *buf, int sz, void *ctx)
|
||||
WOLFSSL_MSG("Embed Receive From error");
|
||||
|
||||
if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) {
|
||||
if (wolfSSL_get_using_nonblock(ssl)) {
|
||||
if (wolfSSL_dtls_get_using_nonblock(ssl)) {
|
||||
WOLFSSL_MSG("\tWould block");
|
||||
return WOLFSSL_CBIO_ERR_WANT_READ;
|
||||
}
|
||||
@@ -1715,7 +1684,7 @@ int MicriumReceive(WOLFSSL *ssl, char *buf, int sz, void *ctx)
|
||||
{
|
||||
int dtls_timeout = wolfSSL_dtls_get_current_timeout(ssl);
|
||||
if (wolfSSL_dtls(ssl)
|
||||
&& !wolfSSL_get_using_nonblock(ssl)
|
||||
&& !wolfSSL_dtls_get_using_nonblock(ssl)
|
||||
&& dtls_timeout != 0) {
|
||||
/* needs timeout in milliseconds */
|
||||
NetSock_CfgTimeoutRxQ_Set(sd, dtls_timeout * 1000, &err);
|
||||
@@ -1732,7 +1701,7 @@ int MicriumReceive(WOLFSSL *ssl, char *buf, int sz, void *ctx)
|
||||
|
||||
if (err == NET_ERR_RX || err == NET_SOCK_ERR_RX_Q_EMPTY ||
|
||||
err == NET_ERR_FAULT_LOCK_ACQUIRE) {
|
||||
if (!wolfSSL_dtls(ssl) || wolfSSL_get_using_nonblock(ssl)) {
|
||||
if (!wolfSSL_dtls(ssl) || wolfSSL_dtls_get_using_nonblock(ssl)) {
|
||||
WOLFSSL_MSG("\tWould block");
|
||||
return WOLFSSL_CBIO_ERR_WANT_READ;
|
||||
}
|
||||
@@ -1772,7 +1741,7 @@ int MicriumReceiveFrom(WOLFSSL *ssl, char *buf, int sz, void *ctx)
|
||||
if (ssl->options.handShakeDone)
|
||||
dtls_timeout = 0;
|
||||
|
||||
if (!wolfSSL_get_using_nonblock(ssl)) {
|
||||
if (!wolfSSL_dtls_get_using_nonblock(ssl)) {
|
||||
/* needs timeout in milliseconds */
|
||||
NetSock_CfgTimeoutRxQ_Set(sd, dtls_timeout * 1000, &err);
|
||||
if (err != NET_SOCK_ERR_NONE) {
|
||||
@@ -1787,7 +1756,7 @@ int MicriumReceiveFrom(WOLFSSL *ssl, char *buf, int sz, void *ctx)
|
||||
|
||||
if (err == NET_ERR_RX || err == NET_SOCK_ERR_RX_Q_EMPTY ||
|
||||
err == NET_ERR_FAULT_LOCK_ACQUIRE) {
|
||||
if (wolfSSL_get_using_nonblock(ssl)) {
|
||||
if (wolfSSL_dtls_get_using_nonblock(ssl)) {
|
||||
WOLFSSL_MSG("\tWould block");
|
||||
return WOLFSSL_CBIO_ERR_WANT_READ;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user