From 5ed90e00022ef392feaf95d9960e0cfaf01ed346 Mon Sep 17 00:00:00 2001 From: jsing <> Date: Thu, 12 Jan 2017 16:08:49 +0000 Subject: Simplify the TLS callback BIO code - a pointer to the tls context can be stored directly in bio->ptr, rather than allocating and deallocating an intermediate struct. Diff from Marko Kreen - thanks! --- src/lib/libtls/tls_bio_cb.c | 73 +++++---------------------------------------- 1 file changed, 7 insertions(+), 66 deletions(-) (limited to 'src') diff --git a/src/lib/libtls/tls_bio_cb.c b/src/lib/libtls/tls_bio_cb.c index f53039fa00..75d9685cb3 100644 --- a/src/lib/libtls/tls_bio_cb.c +++ b/src/lib/libtls/tls_bio_cb.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tls_bio_cb.c,v 1.16 2017/01/12 16:01:47 jsing Exp $ */ +/* $OpenBSD: tls_bio_cb.c,v 1.17 2017/01/12 16:08:49 jsing Exp $ */ /* * Copyright (c) 2016 Tobias Pape * @@ -28,14 +28,6 @@ static int bio_cb_write(BIO *bio, const char *buf, int num); static int bio_cb_read(BIO *bio, char *buf, int size); static int bio_cb_puts(BIO *bio, const char *str); static long bio_cb_ctrl(BIO *bio, int cmd, long num, void *ptr); -static int bio_cb_new(BIO *bio); -static int bio_cb_free(BIO *bio); - -struct bio_cb { - int (*write_cb)(BIO *bio, const char *buf, int num, void *cb_arg); - int (*read_cb)(BIO *bio, char *buf, int size, void *cb_arg); - void *cb_arg; -}; static BIO_METHOD bio_cb_method = { .type = BIO_TYPE_MEM, @@ -44,8 +36,6 @@ static BIO_METHOD bio_cb_method = { .bread = bio_cb_read, .bputs = bio_cb_puts, .ctrl = bio_cb_ctrl, - .create = bio_cb_new, - .destroy = bio_cb_free, }; static BIO_METHOD * @@ -54,52 +44,6 @@ bio_s_cb(void) return (&bio_cb_method); } -static int -bio_cb_new(BIO *bio) -{ - struct bio_cb *bcb; - - if ((bcb = calloc(1, sizeof(struct bio_cb))) == NULL) - return (0); - - bio->shutdown = 1; - bio->init = 1; - bio->num = -1; - bio->ptr = bcb; - - return (1); -} - -static int -bio_cb_free(BIO *bio) -{ - if (bio == NULL) - return (0); - - if (bio->shutdown) { - if ((bio->init) && (bio->ptr != NULL)) { - free(bio->ptr); - bio->ptr = NULL; - } - } - - return (1); -} - -static int -bio_cb_read(BIO *bio, char *buf, int size) -{ - struct bio_cb *bcb = bio->ptr; - return (bcb->read_cb(bio, buf, size, bcb->cb_arg)); -} - -static int -bio_cb_write(BIO *bio, const char *buf, int num) -{ - struct bio_cb *bcb = bio->ptr; - return (bcb->write_cb(bio, buf, num, bcb->cb_arg)); -} - static int bio_cb_puts(BIO *bio, const char *str) { @@ -135,9 +79,9 @@ bio_cb_ctrl(BIO *bio, int cmd, long num, void *ptr) } static int -tls_bio_write_cb(BIO *bio, const char *buf, int num, void *cb_arg) +bio_cb_write(BIO *bio, const char *buf, int num) { - struct tls *ctx = cb_arg; + struct tls *ctx = bio->ptr; int rv; BIO_clear_retry_flags(bio); @@ -153,9 +97,9 @@ tls_bio_write_cb(BIO *bio, const char *buf, int num, void *cb_arg) } static int -tls_bio_read_cb(BIO *bio, char *buf, int size, void *cb_arg) +bio_cb_read(BIO *bio, char *buf, int size) { - struct tls *ctx = cb_arg; + struct tls *ctx = bio->ptr; int rv; BIO_clear_retry_flags(bio); @@ -173,7 +117,6 @@ tls_bio_read_cb(BIO *bio, char *buf, int size, void *cb_arg) static BIO * tls_get_new_cb_bio(struct tls *ctx) { - struct bio_cb *bcb; BIO *bio; if (ctx->read_cb == NULL || ctx->write_cb == NULL) { @@ -185,10 +128,8 @@ tls_get_new_cb_bio(struct tls *ctx) return (NULL); } - bcb = (struct bio_cb *)bio->ptr; - bcb->read_cb = tls_bio_read_cb; - bcb->write_cb = tls_bio_write_cb; - bcb->cb_arg = ctx; + bio->ptr = ctx; + bio->init = 1; return (bio); } -- cgit v1.2.3-55-g6feb