]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
simplify more bio buffer functions
authorAlan T. DeKok <aland@freeradius.org>
Tue, 5 Mar 2024 20:18:08 +0000 (15:18 -0500)
committerAlan T. DeKok <aland@freeradius.org>
Wed, 6 Mar 2024 14:21:55 +0000 (09:21 -0500)
src/lib/bio/buf.c
src/lib/bio/buf.h
src/lib/bio/haproxy.c
src/lib/bio/mem.c
src/lib/bio/retry.c

index 0d113cd4d4c11662c09505f26ab728e0317c7b2f..652f82e254f00e2df041b0e1488de7551d54becb 100644 (file)
@@ -110,3 +110,17 @@ ssize_t    fr_bio_buf_write(fr_bio_buf_t *bio_buf, const void *buffer, size_t size)
 
        return size;
 }
+
+int fr_bio_buf_alloc(TALLOC_CTX *ctx, fr_bio_buf_t *bio_buf, size_t size)
+{
+       void *ptr;
+
+       ptr = talloc_array(ctx, uint8_t, size);
+       if (!ptr) return -1;
+
+       if (bio_buf->start) talloc_free(bio_buf->start);
+
+       fr_bio_buf_init(bio_buf, ptr, size);
+
+       return 0;
+}
index c9fc45d8304eae231995485143ecb031338f87f1..deae54c1bda80d1163fabb64e8db5e53373a10cb 100644 (file)
@@ -147,3 +147,12 @@ static inline void CC_HINT(nonnull) fr_bio_buf_write_update(fr_bio_buf_t *bio_bu
        }
 }
 #endif
+
+static inline size_t CC_HINT(nonnull) fr_bio_buf_size(fr_bio_buf_t const *bio_buf)
+{
+       fr_bio_buf_verify(bio_buf);
+
+       return (bio_buf->end - bio_buf->start);
+}
+
+int    fr_bio_buf_alloc(TALLOC_CTX *ctx, fr_bio_buf_t *bio_buf, size_t size) CC_HINT(nonnull);
index 294f5834016af89daaee08858f90372d8fe7ac81..607403de7b495f9d47a4eb8b9044ccef724fbc2a 100644 (file)
@@ -228,19 +228,15 @@ static ssize_t fr_bio_haproxy_read(fr_bio_t *bio, void *packet_ctx, void *buffer
 fr_bio_t *fr_bio_haproxy_alloc(TALLOC_CTX *ctx, fr_bio_cb_funcs_t *cb, fr_bio_t *next)
 {
        fr_bio_haproxy_t *my;
-       uint8_t *data;
 
        my = talloc_zero(ctx, fr_bio_haproxy_t);
        if (!my) return NULL;
 
-       data = talloc_array(my, uint8_t, HAPROXY_HEADER_V1_SIZE);
-       if (!data) {
+       if (fr_bio_buf_alloc(my, &my->buffer, HAPROXY_HEADER_V1_SIZE) < 0) {
                talloc_free(my);
                return NULL;
        }
 
-       fr_bio_buf_init(&my->buffer, data, HAPROXY_HEADER_V1_SIZE);
-
        my->bio.read = fr_bio_haproxy_read;
        my->bio.write = fr_bio_null_write; /* can't write to this bio */
        my->cb = *cb;
index f626ee553ad30ea3e7057842df55a861afb96bc2..48dfb421a571871324c792f2f17c873c395cc421 100644 (file)
@@ -625,18 +625,14 @@ static int fr_bio_mem_call_verify(fr_bio_t *bio, void *packet_ctx, size_t *size)
  */
 static bool fr_bio_mem_buf_alloc(fr_bio_mem_t *my, fr_bio_buf_t *buf, size_t size)
 {
-       uint8_t *data;
-
        if (size < 1024) size = 1024;
        if (size > (1 << 20)) size = 1 << 20;
 
-       data = talloc_array(my, uint8_t, size);
-       if (!data) {
+       if (fr_bio_buf_alloc(my, buf, size) < 0) {
                talloc_free(my);
                return false;
        }
 
-       fr_bio_buf_init(buf, data, size);
        return true;
 }
 
index 300502244c790e172a4df57067cd510d3b4a6a00..e2add9e4bfaf82c0e1c000aaef2485e2d4088f21 100644 (file)
@@ -167,7 +167,9 @@ static ssize_t fr_bio_retry_write_cancelled(fr_bio_t *bio, void *packet_ctx, con
        rcode = next->write(next, NULL, my->cancelled.read, used);
        if (rcode <= 0) return rcode;
 
-       if ((size_t) rcode == used) {
+       my->cancelled.read += rcode;
+
+       if (fr_bio_buf_used(&my->cancelled) == 0) {
                my->blocked = false;
                my->bio.write = fr_bio_retry_write;
 
@@ -175,9 +177,9 @@ static ssize_t fr_bio_retry_write_cancelled(fr_bio_t *bio, void *packet_ctx, con
        }
 
        /*
-        *      We didn't write any of the partial packet, so we can't write out this one, either.
+        *      We didn't write any of the saved partial packet, so we can't write out the current one,
+        *      either.
         */
-       my->cancelled.read += rcode;
        return 0;
 }
 
@@ -552,29 +554,21 @@ int fr_bio_retry_cancel(fr_bio_t *bio, fr_bio_retry_entry_t *item)
        if (item->cancelled) return 0;
 
        /*
-        *      If we've written a partial packet, then we cannot cancel this item.
-        *
-        *      @todo - cache the rest of the packet data and send it, even if the item has been cancelled!
+        *      If we've written a partial packet, jump through a bunch of hoops to cache the partial packet
+        *      data.  This lets the application cancel any pending packet, while still making sure that we
+        *      don't break packet boundaries.
         */
        if (my->partial == item) {
                if (item->partial > 0) {
-                       uint8_t *ptr;
                        size_t size;
 
                        size = item->size - item->partial;
 
                        if (!my->cancelled.start) {
-                               ptr = talloc_array(my, uint8_t, size);
-                               if (!ptr) return -1;
-
-                               fr_bio_buf_init(&my->cancelled, ptr, size);
-
-                       } else if (size > (size_t) (my->cancelled.end - my->cancelled.start)) {
-                               ptr = talloc_array(my, uint8_t, size);
-                               if (!ptr) return -1;
+                               if (fr_bio_buf_alloc(my, &my->cancelled, size)) return -1;
 
-                               talloc_free(my->cancelled.start);
-                               fr_bio_buf_init(&my->cancelled, ptr, size);
+                       } else if (size > fr_bio_buf_size(&my->cancelled)) {
+                               if (fr_bio_buf_alloc(my, &my->cancelled, size)) return -1;
                        }
 
                        fr_assert(fr_bio_buf_used(&my->cancelled) == 0);