]> PatchyGit - patchyircd.git/commitdiff
tls: add proper blocking with select/poll
authorEgor <egor@patchysicp.xyz>
Fri, 19 May 2023 21:29:05 +0000 (00:29 +0300)
committerEgor <egor@patchysicp.xyz>
Fri, 19 May 2023 21:29:05 +0000 (00:29 +0300)
net.c

diff --git a/net.c b/net.c
index 12f06d2..a6601cb 100644 (file)
--- a/net.c
+++ b/net.c
@@ -2,6 +2,9 @@
 #include "irc.h"
 #include "ircfmt.h"
 #include <stdbool.h>
+#ifndef _WIN32
+#include <poll.h>
+#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;
                }