QUIC DDD: ddd-04-fd-nonblocking: Planned changes
authorHugo Landau <hlandau@openssl.org>
Wed, 9 Aug 2023 16:46:33 +0000 (17:46 +0100)
committerHugo Landau <hlandau@openssl.org>
Fri, 1 Sep 2023 09:45:35 +0000 (10:45 +0100)
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/21715)

doc/designs/ddd/ddd-04-fd-nonblocking.c

index f8a5162a2e7a50518f064635dab59bf0e21b7592..ef15ecb29a0cef0149605fb7d4b35c00234af45d 100644 (file)
@@ -27,7 +27,11 @@ SSL_CTX *create_ssl_ctx(void)
 {
     SSL_CTX *ctx;
 
+#ifdef USE_QUIC
+    ctx = SSL_CTX_new(QUIC_client_method());
+#else
     ctx = SSL_CTX_new(TLS_client_method());
+#endif
     if (ctx == NULL)
         return NULL;
 
@@ -180,7 +184,11 @@ int get_conn_fd(APP_CONN *conn)
  */
 int get_conn_pending_tx(APP_CONN *conn)
 {
+#ifdef USE_QUIC
+    return POLLIN | POLLOUT | POLLERR;
+#else
     return (conn->tx_need_rx ? POLLIN : 0) | POLLOUT | POLLERR;
+#endif
 }
 
 int get_conn_pending_rx(APP_CONN *conn)
@@ -188,6 +196,28 @@ int get_conn_pending_rx(APP_CONN *conn)
     return (conn->rx_need_tx ? POLLOUT : 0) | POLLIN | POLLERR;
 }
 
+#ifdef USE_QUIC
+/*
+ * Returns the number of milliseconds after which some call to libssl must be
+ * made. Any call (SSL_read/SSL_write/SSL_pump) will do. Returns -1 if there is
+ * no need for such a call. This may change after the next call
+ * to libssl.
+ */
+int get_conn_pump_timeout(APP_CONN *conn)
+{
+    return SSL_get_timeout(conn->ssl);
+}
+
+/*
+ * Called to advance internals of libssl state machines without having to
+ * perform an application-level read/write.
+ */
+void pump(APP_CONN *conn)
+{
+    SSL_pump(conn->ssl);
+}
+#endif
+
 /*
  * The application wants to close the connection and free bookkeeping
  * structures.
@@ -216,10 +246,28 @@ void teardown_ctx(SSL_CTX *ctx)
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <sys/signal.h>
+#ifdef USE_QUIC
+# include <sys/time.h>
+#endif
 #include <netdb.h>
 #include <unistd.h>
 #include <fcntl.h>
 
+#ifdef USE_QUIC
+
+static inline void ms_to_timeval(struct timeval *t, int ms)
+{
+    t->tv_sec   = ms < 0 ? -1 : ms/1000;
+    t->tv_usec  = ms < 0 ? 0 : (ms%1000)*1000;
+}
+
+static inline int timeval_to_ms(const struct timeval *t)
+{
+    return t->tv_sec*1000 + t->tv_usec/1000;
+}
+
+#endif
+
 int main(int argc, char **argv)
 {
     int rc, fd = -1, res = 1;
@@ -227,11 +275,19 @@ int main(int argc, char **argv)
     const char *tx_p = tx_msg;
     char rx_buf[2048];
     int l, tx_len = sizeof(tx_msg)-1;
+#ifdef USE_QUIC
+    struct timeval timeout;
+#else
     int timeout = 2000 /* ms */;
+#endif
     APP_CONN *conn = NULL;
     struct addrinfo hints = {0}, *result = NULL;
     SSL_CTX *ctx = NULL;
 
+#ifdef USE_QUIC
+    ms_to_timeval(&timeout, 2000);
+#endif
+
     if (argc < 3) {
         fprintf(stderr, "usage: %s host port\n", argv[0]);
         goto fail;
@@ -257,7 +313,11 @@ int main(int argc, char **argv)
 
     signal(SIGPIPE, SIG_IGN);
 
+#ifdef USE_QUIC
+    fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+#else
     fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+#endif
     if (fd < 0) {
         fprintf(stderr, "cannot create socket\n");
         goto fail;
@@ -291,12 +351,38 @@ int main(int argc, char **argv)
             fprintf(stderr, "tx error\n");
             goto fail;
         } else if (l == -2) {
+#ifdef USE_QUIC
+            struct timeval start, now, deadline, t;
+#endif
             struct pollfd pfd = {0};
+
+#ifdef USE_QUIC
+            ms_to_timeval(&t, get_conn_pump_timeout(conn));
+            if (t.tv_sec < 0 || timercmp(&t, &timeout, >))
+                t = timeout;
+
+            gettimeofday(&start, NULL);
+            timeradd(&start, &timeout, &deadline);
+#endif
+
             pfd.fd = get_conn_fd(conn);
             pfd.events = get_conn_pending_tx(conn);
-            if (poll(&pfd, 1, timeout) == 0) {
-                fprintf(stderr, "tx timeout\n");
-                goto fail;
+#ifdef USE_QUIC
+            if (poll(&pfd, 1, timeval_to_ms(&t)) == 0)
+#else
+            if (poll(&pfd, 1, timeout) == 0)
+#endif
+            {
+#ifdef USE_QUIC
+                pump(conn);
+
+                gettimeofday(&now, NULL);
+                if (timercmp(&now, &deadline, >=))
+#endif
+                {
+                    fprintf(stderr, "tx timeout\n");
+                    goto fail;
+                }
             }
         }
     }
@@ -309,12 +395,37 @@ int main(int argc, char **argv)
         } else if (l == -1) {
             break;
         } else if (l == -2) {
+#ifdef USE_QUIC
+            struct timeval start, now, deadline, t;
+#endif
             struct pollfd pfd = {0};
+
+#ifdef USE_QUIC
+            ms_to_timeval(&t, get_conn_pump_timeout(conn));
+            if (t.tv_sec < 0 || timercmp(&t, &timeout, >))
+                t = timeout;
+
+            gettimeofday(&start, NULL);
+            timeradd(&start, &timeout, &deadline);
+#endif
+
             pfd.fd = get_conn_fd(conn);
             pfd.events = get_conn_pending_rx(conn);
-            if (poll(&pfd, 1, timeout) == 0) {
-                fprintf(stderr, "rx timeout\n");
-                goto fail;
+#ifdef USE_QUIC
+            if (poll(&pfd, 1, timeval_to_ms(&t)) == 0)
+#else
+            if (poll(&pfd, 1, timeout) == 0)
+#endif
+            {
+#ifdef USE_QUIC
+                pump(conn);
+                gettimeofday(&now, NULL);
+                if (timercmp(&now, &deadline, >=))
+#endif
+                {
+                    fprintf(stderr, "rx timeout\n");
+                    goto fail;
+                }
             }
         }
     }