From: Alan T. DeKok Date: Wed, 21 Feb 2024 16:21:46 +0000 (-0500) Subject: move verify to set_verify callback X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8d56c59b94922ecb8b1305118dfd1fcd15f6fa54;p=thirdparty%2Ffreeradius-server.git move verify to set_verify callback so that we can add verification to bios which have both read and write buffers --- diff --git a/src/lib/bio/mem.c b/src/lib/bio/mem.c index dbc8cb8778c..23ca923be5a 100644 --- a/src/lib/bio/mem.c +++ b/src/lib/bio/mem.c @@ -686,34 +686,6 @@ fr_bio_t *fr_bio_mem_alloc(TALLOC_CTX *ctx, size_t read_size, size_t write_size, return (fr_bio_t *) my; } -/** Only return verified packets. - * - * Like fr_bio_mem_alloc(), but only returns packets. - * - * Writes pass straight through to the next bio. - */ -fr_bio_t *fr_bio_mem_packet_alloc(TALLOC_CTX *ctx, size_t read_size, fr_bio_verify_t verify, bool datagram, fr_bio_t *next) -{ - fr_bio_mem_t *my; - - if (!datagram) { - my = (fr_bio_mem_t *) fr_bio_mem_sink_alloc(ctx, read_size); - } else { - my = talloc_zero(ctx, fr_bio_mem_t); - if (!my) return NULL; - - talloc_set_destructor((fr_bio_t *) my, fr_bio_destructor); - } - if (!my) return NULL; - - my->verify = verify; - my->bio.read = datagram ? fr_bio_mem_read_packet_datagram : fr_bio_mem_read_packet; - my->bio.write = fr_bio_next_write; - - fr_bio_chain(&my->bio, next); - - return (fr_bio_t *) my; -} /** Allocate a memory buffer which sources data from the callers application into the bio system. * @@ -804,3 +776,29 @@ fr_bio_t *fr_bio_mem_sink_alloc(TALLOC_CTX *ctx, size_t read_size) talloc_set_destructor((fr_bio_t *) my, fr_bio_destructor); return (fr_bio_t *) my; } + +/** Set the verification function for memory bios. + * + * It is possible to add a verification function. It is not currently possible to remove one. + * + * @param bio the binary IO handler + * @param verify the verification function + * @param datagram whether or not this bio is a datagram one. + * @return + * - <0 on error + * - 0 on success + */ +int fr_bio_mem_set_verify(fr_bio_t *bio, fr_bio_verify_t verify, bool datagram) +{ + fr_bio_mem_t *my = talloc_get_type_abort(bio, fr_bio_mem_t); + + if (my->bio.read != fr_bio_mem_read) { + fr_strerror_const("Cannot add verify to a memory sink bio"); + return fr_bio_error(GENERIC); + } + + my->verify = verify; + my->bio.read = datagram ? fr_bio_mem_read_packet_datagram : fr_bio_mem_read_packet; + + return 0; +} diff --git a/src/lib/bio/mem.h b/src/lib/bio/mem.h index 60478cecaaf..d0fcac39711 100644 --- a/src/lib/bio/mem.h +++ b/src/lib/bio/mem.h @@ -51,8 +51,6 @@ typedef fr_bio_verify_action_t (*fr_bio_verify_t)(fr_bio_t *bio, void *packet_ct fr_bio_t *fr_bio_mem_alloc(TALLOC_CTX *ctx, size_t read_size, size_t write_size, fr_bio_t *next) CC_HINT(nonnull); -fr_bio_t *fr_bio_mem_packet_alloc(TALLOC_CTX *ctx, size_t read_size, fr_bio_verify_t verify, bool datagram, fr_bio_t *next) CC_HINT(nonnull); - fr_bio_t *fr_bio_mem_source_alloc(TALLOC_CTX *ctx, size_t buffer_size, fr_bio_t *next) CC_HINT(nonnull); fr_bio_t *fr_bio_mem_sink_alloc(TALLOC_CTX *ctx, size_t buffer_size) CC_HINT(nonnull); @@ -60,3 +58,5 @@ fr_bio_t *fr_bio_mem_sink_alloc(TALLOC_CTX *ctx, size_t buffer_size) CC_HINT(non uint8_t const *fr_bio_mem_read_peek(fr_bio_t *bio, size_t *size) CC_HINT(nonnull); void fr_bio_mem_read_discard(fr_bio_t *bio, size_t size) CC_HINT(nonnull); + +int fr_bio_mem_set_verify(fr_bio_t *bio, fr_bio_verify_t verify, bool datagram) CC_HINT(nonnull);