From: Alan T. DeKok Date: Tue, 5 Mar 2024 20:18:08 +0000 (-0500) Subject: simplify more bio buffer functions X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8784fe636e74cd3383c5ec4f67ebd99eb7e651c2;p=thirdparty%2Ffreeradius-server.git simplify more bio buffer functions --- diff --git a/src/lib/bio/buf.c b/src/lib/bio/buf.c index 0d113cd4d4c..652f82e254f 100644 --- a/src/lib/bio/buf.c +++ b/src/lib/bio/buf.c @@ -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; +} diff --git a/src/lib/bio/buf.h b/src/lib/bio/buf.h index c9fc45d8304..deae54c1bda 100644 --- a/src/lib/bio/buf.h +++ b/src/lib/bio/buf.h @@ -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); diff --git a/src/lib/bio/haproxy.c b/src/lib/bio/haproxy.c index 294f5834016..607403de7b4 100644 --- a/src/lib/bio/haproxy.c +++ b/src/lib/bio/haproxy.c @@ -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; diff --git a/src/lib/bio/mem.c b/src/lib/bio/mem.c index f626ee553ad..48dfb421a57 100644 --- a/src/lib/bio/mem.c +++ b/src/lib/bio/mem.c @@ -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; } diff --git a/src/lib/bio/retry.c b/src/lib/bio/retry.c index 300502244c7..e2add9e4bfa 100644 --- a/src/lib/bio/retry.c +++ b/src/lib/bio/retry.c @@ -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);