Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing select usage #283

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 29 additions & 30 deletions tls/server-tls-nonblocking.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,36 +52,34 @@ enum {

static int tcp_select(SOCKET_T socketfd, int to_sec, int rx)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we do not check the return of tcp_select, it would be better to change the return to a void. Alternatively, the return of this function should be checked in the main loop.

Copy link
Contributor Author

@kojo1 kojo1 Jan 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to poll() version as Daniele suggested. It looks like behaving as expected.

Will eliminate the return value as soon as debugging is completed.

{
fd_set fds, errfds;
fd_set* recvfds = NULL;
fd_set* sendfds = NULL;
fd_set errfds;
fd_set recvfds;
fd_set sendfds;
SOCKET_T nfds = socketfd + 1;
struct timeval timeout;
int result;

FD_ZERO(&fds);
FD_SET(socketfd, &fds);
timeout.tv_sec = to_sec;

FD_ZERO(&recvfds);
FD_SET(socketfd, &recvfds);
FD_ZERO(&sendfds);
FD_SET(socketfd, &sendfds);
FD_ZERO(&errfds);
FD_SET(socketfd, &errfds);

if (rx)
recvfds = &fds;
else
sendfds = &fds;

result = select(nfds, recvfds, sendfds, &errfds, &timeout);

result = select(nfds, &recvfds, &sendfds, &errfds, &timeout);
printf("fd = %d, select = %d\n", socketfd, result);
sleep(1);
if (result == 0)
return TEST_TIMEOUT;
else if (result > 0) {
if (FD_ISSET(socketfd, &fds)) {
if (rx)
return TEST_RECV_READY;
else
return TEST_SEND_READY;
}
else if (FD_ISSET(socketfd, &errfds))
return TEST_ERROR_READY;
if (FD_ISSET(socketfd, &recvfds))
printf("Socket is ready for recv\n");
if (FD_ISSET(socketfd, &sendfds))
printf("Socket is ready for recv\n");
danielinux marked this conversation as resolved.
Show resolved Hide resolved
if (FD_ISSET(socketfd, &errfds))
printf("Socket is ready for error\n");
}

return TEST_SELECT_FAIL;
Expand Down Expand Up @@ -128,8 +126,6 @@ int main()
goto exit;
}



/* Create and initialize WOLFSSL_CTX */
if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method())) == NULL) {
fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n");
Expand All @@ -154,8 +150,6 @@ int main()
goto exit;
}



/* Initialize the server address struct with zeros */
memset(&servAddr, 0, sizeof(servAddr));

Expand Down Expand Up @@ -200,6 +194,13 @@ int main()
goto exit;
}

/* Set the socket options to use nonblocking I/O */
if (fcntl(connd, F_SETFL, O_NONBLOCK) == -1) {
fprintf(stderr, "ERROR: failed to set socket options\n");
ret = -1;
goto exit;
}

/* Create a WOLFSSL object */
if ((ssl = wolfSSL_new(ctx)) == NULL) {
fprintf(stderr, "ERROR: failed to create WOLFSSL object\n");
Expand All @@ -220,7 +221,7 @@ int main()
ret = wolfSSL_accept(ssl);
err = wolfSSL_get_error(ssl, ret);
if (err == WOLFSSL_ERROR_WANT_READ)
tcp_select(sockfd, SELECT_WAIT_SEC, 1);
tcp_select(connd, SELECT_WAIT_SEC, 1);
} while (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE);
if (ret != WOLFSSL_SUCCESS) {
fprintf(stderr, "wolfSSL_accept error %d (%d)\n", err, ret);
Expand All @@ -234,9 +235,8 @@ int main()
do {
ret = wolfSSL_read(ssl, buff, sizeof(buff)-1);
err = wolfSSL_get_error(ssl, ret);

if (err == WOLFSSL_ERROR_WANT_READ)
tcp_select(sockfd, SELECT_WAIT_SEC, 1);
tcp_select(connd, SELECT_WAIT_SEC, 1);
}
while (err == WOLFSSL_ERROR_WANT_READ);
if (ret < 0) {
Expand All @@ -253,8 +253,6 @@ int main()
shutdown = 1;
}



/* Write our reply into buff */
memset(buff, 0, sizeof(buff));
memcpy(buff, reply, strlen(reply));
Expand All @@ -264,7 +262,8 @@ int main()
do {
ret = wolfSSL_write(ssl, reply, len);
err = wolfSSL_get_error(ssl, ret);
sleep(1);
if (err == WOLFSSL_ERROR_WANT_WRITE)
tcp_select(connd, SELECT_WAIT_SEC, 0);
}
while (err == WOLFSSL_ERROR_WANT_WRITE);
if (ret < 0) {
Expand Down