From 519cf295f31d77bec45644424f7212e9fb2692e8 Mon Sep 17 00:00:00 2001 From: Paul Fariello Date: Tue, 19 Feb 2019 19:38:40 +0140 Subject: Add signal-protocol locking --- src/omemo/omemo.c | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) (limited to 'src/omemo/omemo.c') diff --git a/src/omemo/omemo.c b/src/omemo/omemo.c index 871f3bfc..96e07d1c 100644 --- a/src/omemo/omemo.c +++ b/src/omemo/omemo.c @@ -1,13 +1,24 @@ +#include #include #include "config/account.h" #include "ui/ui.h" +#include "omemo/omemo.h" #include "omemo/crypto.h" +static void lock(void *user_data); +static void unlock(void *user_data); + +struct omemo_context_t { + pthread_mutexattr_t attr; + pthread_mutex_t lock; +}; + void omemo_init(ProfAccount *account) { - signal_context *global_context; + signal_context *signal_ctx; + omemo_context *ctx = malloc(sizeof(omemo_context)); signal_crypto_provider crypto_provider = { .random_func = omemo_random_func, .hmac_sha256_init_func = omemo_hmac_sha256_init_func, @@ -27,14 +38,33 @@ omemo_init(ProfAccount *account) cons_show("Error initializing Omemo crypto"); } - if (signal_context_create(&global_context, NULL) != 0) { + pthread_mutexattr_init(&ctx->attr); + pthread_mutexattr_settype(&ctx->attr, PTHREAD_MUTEX_RECURSIVE); + pthread_mutex_init(&ctx->lock, &ctx->attr); + + if (signal_context_create(&signal_ctx, ctx) != 0) { cons_show("Error initializing Omemo context"); return; } - if (signal_context_set_crypto_provider(global_context, &crypto_provider) != 0) { + if (signal_context_set_crypto_provider(signal_ctx, &crypto_provider) != 0) { cons_show("Error initializing Omemo crypto"); return; } - //signal_context_set_locking_functions(global_context, lock_function, unlock_function); + + signal_context_set_locking_functions(signal_ctx, lock, unlock); +} + +static void +lock(void *user_data) +{ + omemo_context *ctx = (omemo_context *)user_data; + pthread_mutex_lock(&ctx->lock); +} + +static void +unlock(void *user_data) +{ + omemo_context *ctx = (omemo_context *)user_data; + pthread_mutex_unlock(&ctx->lock); } -- cgit 1.4.1-2-gfad0