diff options
author | 2017-01-12 16:08:49 +0000 | |
---|---|---|
committer | 2017-01-12 16:08:49 +0000 | |
commit | 11756bf8c6f59bfb63f831c93b11fcf394885337 (patch) | |
tree | aed23559a933a0d3a6214d131fdfe257e8ea6ad1 | |
parent | If tls_get_new_cb_bio() fails, an error will already be set. (diff) | |
download | wireguard-openbsd-11756bf8c6f59bfb63f831c93b11fcf394885337.tar.xz wireguard-openbsd-11756bf8c6f59bfb63f831c93b11fcf394885337.zip |
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 <markokr at gmail dot com> - thanks!
-rw-r--r-- | lib/libtls/tls_bio_cb.c | 73 |
1 files changed, 7 insertions, 66 deletions
diff --git a/lib/libtls/tls_bio_cb.c b/lib/libtls/tls_bio_cb.c index f53039fa009..75d9685cb32 100644 --- a/lib/libtls/tls_bio_cb.c +++ b/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 <tobias@netshed.de> * @@ -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 * @@ -55,52 +45,6 @@ bio_s_cb(void) } 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) { int n; @@ -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); } |