mirror of
https://github.com/wolfSSL/wolfssl.git
synced 2025-08-02 12:14:38 +02:00
test client and server use select in non-blocking mode
This commit is contained in:
@@ -401,6 +401,40 @@ static INLINE void udp_connect(SOCKET_T* sockfd, void* addr, int addrSz)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
enum {
|
||||||
|
TEST_SELECT_FAIL,
|
||||||
|
TEST_TIMEOUT,
|
||||||
|
TEST_RECV_READY,
|
||||||
|
TEST_ERROR_READY
|
||||||
|
};
|
||||||
|
|
||||||
|
static INLINE int tcp_select(SOCKET_T socketfd, unsigned int to_sec)
|
||||||
|
{
|
||||||
|
fd_set recvfds, errfds;
|
||||||
|
SOCKET_T nfds = socketfd + 1;
|
||||||
|
struct timeval timeout = {to_sec, 0};
|
||||||
|
int result;
|
||||||
|
|
||||||
|
FD_ZERO(&recvfds);
|
||||||
|
FD_SET(socketfd, &recvfds);
|
||||||
|
FD_ZERO(&errfds);
|
||||||
|
FD_SET(socketfd, &errfds);
|
||||||
|
|
||||||
|
result = select(nfds, &recvfds, NULL, &errfds, &timeout);
|
||||||
|
|
||||||
|
if (result == 0)
|
||||||
|
return TEST_TIMEOUT;
|
||||||
|
else if (result > 0) {
|
||||||
|
if (FD_ISSET(socketfd, &recvfds))
|
||||||
|
return TEST_RECV_READY;
|
||||||
|
else if(FD_ISSET(socketfd, &errfds))
|
||||||
|
return TEST_ERROR_READY;
|
||||||
|
}
|
||||||
|
|
||||||
|
return TEST_SELECT_FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static INLINE void tcp_listen(SOCKET_T* sockfd, int port, int useAnyAddr,
|
static INLINE void tcp_listen(SOCKET_T* sockfd, int port, int useAnyAddr,
|
||||||
int udp)
|
int udp)
|
||||||
{
|
{
|
||||||
|
@@ -48,43 +48,40 @@
|
|||||||
int ret = CyaSSL_connect_ex(ssl, handShakeCB, timeoutCB, timeout);
|
int ret = CyaSSL_connect_ex(ssl, handShakeCB, timeoutCB, timeout);
|
||||||
#endif
|
#endif
|
||||||
int error = CyaSSL_get_error(ssl, 0);
|
int error = CyaSSL_get_error(ssl, 0);
|
||||||
int timeout_count = CyaSSL_dtls_get_current_timeout(ssl) * 10;
|
SOCKET_T sockfd = (SOCKET_T)CyaSSL_get_fd(ssl);
|
||||||
|
int select_ret;
|
||||||
|
|
||||||
while (ret != SSL_SUCCESS && (error == SSL_ERROR_WANT_READ ||
|
while (ret != SSL_SUCCESS && (error == SSL_ERROR_WANT_READ ||
|
||||||
error == SSL_ERROR_WANT_WRITE)) {
|
error == SSL_ERROR_WANT_WRITE)) {
|
||||||
(void)timeout_count;
|
|
||||||
if (error == SSL_ERROR_WANT_READ)
|
if (error == SSL_ERROR_WANT_READ)
|
||||||
printf("... client would read block\n");
|
printf("... client would read block\n");
|
||||||
else
|
else
|
||||||
printf("... client would write block\n");
|
printf("... client would write block\n");
|
||||||
#ifdef USE_WINDOWS_API
|
|
||||||
Sleep(100);
|
if (CyaSSL_dtls(ssl))
|
||||||
#else
|
select_ret = tcp_select(sockfd,
|
||||||
#ifdef CYASSL_DTLS
|
CyaSSL_dtls_get_current_timeout(ssl));
|
||||||
usleep(100000); /* 100ms */
|
else
|
||||||
#else
|
select_ret = tcp_select(sockfd, 1);
|
||||||
sleep(1);
|
|
||||||
#endif
|
if ((select_ret == TEST_RECV_READY) ||
|
||||||
#endif
|
(select_ret == TEST_ERROR_READY)) {
|
||||||
#ifndef CYASSL_CALLBACKS
|
#ifndef CYASSL_CALLBACKS
|
||||||
ret = CyaSSL_connect(ssl);
|
ret = CyaSSL_connect(ssl);
|
||||||
#else
|
#else
|
||||||
ret = CyaSSL_connect_ex(ssl,handShakeCB,timeoutCB,timeout);
|
ret = CyaSSL_connect_ex(ssl,handShakeCB,timeoutCB,timeout);
|
||||||
#endif
|
#endif
|
||||||
error = CyaSSL_get_error(ssl, 0);
|
error = CyaSSL_get_error(ssl, 0);
|
||||||
#ifdef CYASSL_DTLS
|
}
|
||||||
if (timeout_count-- <= 0) {
|
else if (select_ret == TEST_TIMEOUT &&
|
||||||
timeout_count = CyaSSL_dtls_got_timeout(ssl);
|
(!CyaSSL_dtls(ssl) ||
|
||||||
if (timeout_count < 0) {
|
(CyaSSL_dtls_got_timeout(ssl) >= 0))) {
|
||||||
error = SSL_FATAL_ERROR;
|
error = SSL_ERROR_WANT_READ;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
printf("... updating timeout\n");
|
error = SSL_FATAL_ERROR;
|
||||||
timeout_count =
|
|
||||||
CyaSSL_dtls_get_current_timeout(ssl) * 10;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
}
|
|
||||||
if (ret != SSL_SUCCESS)
|
if (ret != SSL_SUCCESS)
|
||||||
err_sys("SSL_connect failed");
|
err_sys("SSL_connect failed");
|
||||||
}
|
}
|
||||||
|
@@ -44,21 +44,38 @@
|
|||||||
int ret = CyaSSL_accept_ex(ssl, srvHandShakeCB, srvTimeoutCB, srvTo);
|
int ret = CyaSSL_accept_ex(ssl, srvHandShakeCB, srvTimeoutCB, srvTo);
|
||||||
#endif
|
#endif
|
||||||
int error = SSL_get_error(ssl, 0);
|
int error = SSL_get_error(ssl, 0);
|
||||||
|
SOCKET_T sockfd = (SOCKET_T)CyaSSL_get_fd(ssl);
|
||||||
|
int select_ret;
|
||||||
|
|
||||||
while (ret != SSL_SUCCESS && (error == SSL_ERROR_WANT_READ ||
|
while (ret != SSL_SUCCESS && (error == SSL_ERROR_WANT_READ ||
|
||||||
error == SSL_ERROR_WANT_WRITE)) {
|
error == SSL_ERROR_WANT_WRITE)) {
|
||||||
printf("... server would block\n");
|
printf("... server would block\n");
|
||||||
#ifdef USE_WINDOWS_API
|
|
||||||
Sleep(1000);
|
if (CyaSSL_dtls(ssl))
|
||||||
#else
|
select_ret = tcp_select(sockfd,
|
||||||
sleep(1);
|
CyaSSL_dtls_get_current_timeout(ssl));
|
||||||
#endif
|
else
|
||||||
|
select_ret = tcp_select(sockfd, 1);
|
||||||
|
|
||||||
|
if ((select_ret == TEST_RECV_READY) ||
|
||||||
|
(select_ret == TEST_ERROR_READY)) {
|
||||||
#ifndef CYASSL_CALLBACKS
|
#ifndef CYASSL_CALLBACKS
|
||||||
ret = SSL_accept(ssl);
|
ret = SSL_accept(ssl);
|
||||||
#else
|
#else
|
||||||
ret = CyaSSL_accept_ex(ssl, srvHandShakeCB, srvTimeoutCB,srvTo);
|
ret = CyaSSL_accept_ex(ssl,
|
||||||
|
srvHandShakeCB, srvTimeoutCB, srvTo);
|
||||||
#endif
|
#endif
|
||||||
error = SSL_get_error(ssl, 0);
|
error = SSL_get_error(ssl, 0);
|
||||||
}
|
}
|
||||||
|
else if (select_ret == TEST_TIMEOUT &&
|
||||||
|
(!CyaSSL_dtls(ssl) ||
|
||||||
|
(CyaSSL_dtls_got_timeout(ssl) >= 0))) {
|
||||||
|
error = SSL_ERROR_WANT_READ;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
error = SSL_FATAL_ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
if (ret != SSL_SUCCESS)
|
if (ret != SSL_SUCCESS)
|
||||||
err_sys("SSL_accept failed");
|
err_sys("SSL_accept failed");
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user