From 520ddb0ca98da513b73f48bf66bbb8dcb580274e Mon Sep 17 00:00:00 2001 From: Egor Date: Sat, 20 May 2023 00:29:05 +0300 Subject: [PATCH] tls: add proper blocking with select/poll --- net.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/net.c b/net.c index 12f06d2..a6601cb 100644 --- a/net.c +++ b/net.c @@ -2,6 +2,9 @@ #include "irc.h" #include "ircfmt.h" #include +#ifndef _WIN32 +#include +#endif struct client_data *allocClient(){ struct client_data *NewClient = malloc(sizeof(struct client_data)); @@ -75,6 +78,26 @@ void killConnection(struct client_data *cl, bool byChoice) { free(cl); } +#if HAS_TLS +static void block(struct client_data *cl, int out) +{ +#ifdef _WIN32 + fd_set fds; + FD_ZERO(&fds); + FD_SET(cl->FD, &fds); + if (out) + select(cl->FD+1, 0, &fds, 0, 0); + else + select(cl->FD+1, &fds, 0, 0, 0); +#else + struct pollfd pfd; + pfd.fd = cl->FD; + pfd.events = out ? POLLOUT : POLLIN; + poll(&pfd, 1, 0); +#endif +} +#endif + void recvProc(struct client_data *cl) { ssize_t bytesRead; char *x; @@ -98,6 +121,7 @@ void recvProc(struct client_data *cl) { case TLS_WANT_POLLIN: return; case TLS_WANT_POLLOUT: // FIXME: this blocks + block(cl, 1); continue; } } @@ -149,8 +173,10 @@ void sendClient(struct client_data *cl, const char *buf, size_t len) break; if (rv == -1) break; - if (rv == TLS_WANT_POLLIN || rv == TLS_WANT_POLLOUT) + if (rv == TLS_WANT_POLLIN || rv == TLS_WANT_POLLOUT) { + block(cl, rv == TLS_WANT_POLLOUT); continue; + } buf += rv; len -= rv; } -- 2.56.0