QUIC CHANNEL: Cleanup poll descriptor management
authorHugo Landau <hlandau@openssl.org>
Wed, 9 Aug 2023 16:46:32 +0000 (17:46 +0100)
committerHugo Landau <hlandau@openssl.org>
Fri, 1 Sep 2023 09:45:34 +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)

include/internal/quic_channel.h
ssl/quic/quic_channel.c

index 44009d1c209ed1633266c0c1c9fc33a39b964854..1624870865c74fb2b7a685f044170490b722558b 100644 (file)
@@ -303,6 +303,12 @@ BIO *ossl_quic_channel_get_net_wbio(QUIC_CHANNEL *ch);
 int ossl_quic_channel_set_net_rbio(QUIC_CHANNEL *ch, BIO *net_rbio);
 int ossl_quic_channel_set_net_wbio(QUIC_CHANNEL *ch, BIO *net_wbio);
 
+/*
+ * Re-poll the network BIOs already set to determine if their support
+ * for polling has changed.
+ */
+int ossl_quic_channel_update_poll_descriptors(QUIC_CHANNEL *ch);
+
 /*
  * Returns an existing stream by stream ID. Returns NULL if the stream does not
  * exist.
index 844ddc137c552eb2e69b86c7a1ee652a06340546..275d5f576beb3c9731ccd58c8a9d8f6498d65c1e 100644 (file)
@@ -2616,6 +2616,40 @@ BIO *ossl_quic_channel_get_net_wbio(QUIC_CHANNEL *ch)
     return ch->net_wbio;
 }
 
+static int ch_update_poll_desc(QUIC_CHANNEL *ch, BIO *net_bio, int for_write)
+{
+    BIO_POLL_DESCRIPTOR d = {0};
+
+    if (net_bio == NULL
+        || (!for_write && !BIO_get_rpoll_descriptor(net_bio, &d))
+        || (for_write && !BIO_get_wpoll_descriptor(net_bio, &d)))
+        /* Non-pollable BIO */
+        d.type = BIO_POLL_DESCRIPTOR_TYPE_NONE;
+
+    if (!validate_poll_descriptor(&d))
+        return 0;
+
+    if (for_write)
+        ossl_quic_reactor_set_poll_w(&ch->rtor, &d);
+    else
+        ossl_quic_reactor_set_poll_r(&ch->rtor, &d);
+
+    return 1;
+}
+
+int ossl_quic_channel_update_poll_descriptors(QUIC_CHANNEL *ch)
+{
+    int ok = 1;
+
+    if (!ch_update_poll_desc(ch, ch->net_rbio, /*for_write=*/0))
+        ok = 0;
+
+    if (!ch_update_poll_desc(ch, ch->net_wbio, /*for_write=*/1))
+        ok = 0;
+
+    return ok;
+}
+
 /*
  * QUIC_CHANNEL does not ref any BIO it is provided with, nor is any ref
  * transferred to it. The caller (i.e., QUIC_CONNECTION) is responsible for
@@ -2624,21 +2658,12 @@ BIO *ossl_quic_channel_get_net_wbio(QUIC_CHANNEL *ch)
  */
 int ossl_quic_channel_set_net_rbio(QUIC_CHANNEL *ch, BIO *net_rbio)
 {
-    BIO_POLL_DESCRIPTOR d = {0};
-
     if (ch->net_rbio == net_rbio)
         return 1;
 
-    if (net_rbio != NULL) {
-        if (!BIO_get_rpoll_descriptor(net_rbio, &d))
-            /* Non-pollable BIO */
-            d.type = BIO_POLL_DESCRIPTOR_TYPE_NONE;
-
-        if (!validate_poll_descriptor(&d))
-            return 0;
-    }
+    if (!ch_update_poll_desc(ch, net_rbio, /*for_write=*/0))
+        return 0;
 
-    ossl_quic_reactor_set_poll_r(&ch->rtor, &d);
     ossl_quic_demux_set_bio(ch->demux, net_rbio);
     ch->net_rbio = net_rbio;
     return 1;
@@ -2646,21 +2671,12 @@ int ossl_quic_channel_set_net_rbio(QUIC_CHANNEL *ch, BIO *net_rbio)
 
 int ossl_quic_channel_set_net_wbio(QUIC_CHANNEL *ch, BIO *net_wbio)
 {
-    BIO_POLL_DESCRIPTOR d = {0};
-
     if (ch->net_wbio == net_wbio)
         return 1;
 
-    if (net_wbio != NULL) {
-        if (!BIO_get_wpoll_descriptor(net_wbio, &d))
-            /* Non-pollable BIO */
-            d.type = BIO_POLL_DESCRIPTOR_TYPE_NONE;
-
-        if (!validate_poll_descriptor(&d))
-            return 0;
-    }
+    if (!ch_update_poll_desc(ch, net_wbio, /*for_write=*/1))
+        return 0;
 
-    ossl_quic_reactor_set_poll_w(&ch->rtor, &d);
     ossl_qtx_set_bio(ch->qtx, net_wbio);
     ch->net_wbio = net_wbio;
     return 1;