Skip to content

Commit 2f6d926

Browse files
committed
fixing select usage
1 parent 48b7d5a commit 2f6d926

File tree

1 file changed

+29
-30
lines changed

1 file changed

+29
-30
lines changed

tls/server-tls-nonblocking.c

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -52,36 +52,34 @@ enum {
5252

5353
static int tcp_select(SOCKET_T socketfd, int to_sec, int rx)
5454
{
55-
fd_set fds, errfds;
56-
fd_set* recvfds = NULL;
57-
fd_set* sendfds = NULL;
55+
fd_set errfds;
56+
fd_set recvfds;
57+
fd_set sendfds;
5858
SOCKET_T nfds = socketfd + 1;
5959
struct timeval timeout;
6060
int result;
6161

62-
FD_ZERO(&fds);
63-
FD_SET(socketfd, &fds);
62+
timeout.tv_sec = to_sec;
63+
64+
FD_ZERO(&recvfds);
65+
FD_SET(socketfd, &recvfds);
66+
FD_ZERO(&sendfds);
67+
FD_SET(socketfd, &sendfds);
6468
FD_ZERO(&errfds);
6569
FD_SET(socketfd, &errfds);
6670

67-
if (rx)
68-
recvfds = &fds;
69-
else
70-
sendfds = &fds;
71-
72-
result = select(nfds, recvfds, sendfds, &errfds, &timeout);
73-
71+
result = select(nfds, &recvfds, &sendfds, &errfds, &timeout);
72+
printf("fd = %d, select = %d\n", socketfd, result);
73+
sleep(1);
7474
if (result == 0)
7575
return TEST_TIMEOUT;
7676
else if (result > 0) {
77-
if (FD_ISSET(socketfd, &fds)) {
78-
if (rx)
79-
return TEST_RECV_READY;
80-
else
81-
return TEST_SEND_READY;
82-
}
83-
else if (FD_ISSET(socketfd, &errfds))
84-
return TEST_ERROR_READY;
77+
if (FD_ISSET(socketfd, &recvfds))
78+
printf("Socket is ready for recv\n");
79+
if (FD_ISSET(socketfd, &sendfds))
80+
printf("Socket is ready for recv\n");
81+
if (FD_ISSET(socketfd, &errfds))
82+
printf("Socket is ready for error\n");
8583
}
8684

8785
return TEST_SELECT_FAIL;
@@ -128,8 +126,6 @@ int main()
128126
goto exit;
129127
}
130128

131-
132-
133129
/* Create and initialize WOLFSSL_CTX */
134130
if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method())) == NULL) {
135131
fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n");
@@ -154,8 +150,6 @@ int main()
154150
goto exit;
155151
}
156152

157-
158-
159153
/* Initialize the server address struct with zeros */
160154
memset(&servAddr, 0, sizeof(servAddr));
161155

@@ -200,6 +194,13 @@ int main()
200194
goto exit;
201195
}
202196

197+
/* Set the socket options to use nonblocking I/O */
198+
if (fcntl(connd, F_SETFL, O_NONBLOCK) == -1) {
199+
fprintf(stderr, "ERROR: failed to set socket options\n");
200+
ret = -1;
201+
goto exit;
202+
}
203+
203204
/* Create a WOLFSSL object */
204205
if ((ssl = wolfSSL_new(ctx)) == NULL) {
205206
fprintf(stderr, "ERROR: failed to create WOLFSSL object\n");
@@ -220,7 +221,7 @@ int main()
220221
ret = wolfSSL_accept(ssl);
221222
err = wolfSSL_get_error(ssl, ret);
222223
if (err == WOLFSSL_ERROR_WANT_READ)
223-
tcp_select(sockfd, SELECT_WAIT_SEC, 1);
224+
tcp_select(connd, SELECT_WAIT_SEC, 1);
224225
} while (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE);
225226
if (ret != WOLFSSL_SUCCESS) {
226227
fprintf(stderr, "wolfSSL_accept error %d (%d)\n", err, ret);
@@ -234,9 +235,8 @@ int main()
234235
do {
235236
ret = wolfSSL_read(ssl, buff, sizeof(buff)-1);
236237
err = wolfSSL_get_error(ssl, ret);
237-
238238
if (err == WOLFSSL_ERROR_WANT_READ)
239-
tcp_select(sockfd, SELECT_WAIT_SEC, 1);
239+
tcp_select(connd, SELECT_WAIT_SEC, 1);
240240
}
241241
while (err == WOLFSSL_ERROR_WANT_READ);
242242
if (ret < 0) {
@@ -253,8 +253,6 @@ int main()
253253
shutdown = 1;
254254
}
255255

256-
257-
258256
/* Write our reply into buff */
259257
memset(buff, 0, sizeof(buff));
260258
memcpy(buff, reply, strlen(reply));
@@ -264,7 +262,8 @@ int main()
264262
do {
265263
ret = wolfSSL_write(ssl, reply, len);
266264
err = wolfSSL_get_error(ssl, ret);
267-
sleep(1);
265+
if (err == WOLFSSL_ERROR_WANT_WRITE)
266+
tcp_select(connd, SELECT_WAIT_SEC, 0);
268267
}
269268
while (err == WOLFSSL_ERROR_WANT_WRITE);
270269
if (ret < 0) {

0 commit comments

Comments
 (0)