From c7aba5979c820958aa08947903afb47ace496a16 Mon Sep 17 00:00:00 2001 From: Patrick Date: Fri, 21 Jul 2023 23:04:48 +0200 Subject: share, save, load, init, print megolm out sessions --- src/matrix.c | 23 +++++++++++++++++++---- src/matrix.h | 8 +++++++- 2 files changed, 26 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/matrix.c b/src/matrix.c index fa7303f..de936de 100644 --- a/src/matrix.c +++ b/src/matrix.c @@ -192,7 +192,7 @@ MatrixMegolmOutSessionInit( static uint8_t random[MEGOLM_INIT_RANDOM_SIZE]; Randomize(random, MEGOLM_INIT_RANDOM_SIZE); - session->roomId = roomId; + strncpy(session->roomId, roomId, ROOM_ID_SIZE); session->session = olm_outbound_group_session(session->memory); @@ -268,6 +268,8 @@ MatrixMegolmOutSessionLoad( size_t roomIdLen; fread(&roomIdLen, sizeof(size_t), 1, f); fread(session->roomId, 1, roomIdLen, f); + for (int i = roomIdLen; i < ROOM_ID_SIZE; i++) + session->roomId[i] = '\0'; size_t pickleBufferLen; fread(&pickleBufferLen, sizeof(size_t), 1, f); @@ -282,6 +284,9 @@ MatrixMegolmOutSessionLoad( free(pickleBuffer); + olm_outbound_group_session_id(session->session, (uint8_t *)session->id, MEGOLM_SESSION_ID_SIZE); + olm_outbound_group_session_key(session->session, (uint8_t *)session->key, MEGOLM_SESSION_KEY_SIZE); + fclose(f); return true; @@ -816,19 +821,29 @@ MatrixClientGetMegolmOutSession( } } + if (MatrixClientInitMegolmOutSession(client, roomId)) { + *outSession = &client->megolmOutSessions[client->numMegolmOutSessions-1]; + return true; + } + + return false; +} + +bool +MatrixClientInitMegolmOutSession( + MatrixClient * client, + const char * roomId) +{ if (client->numMegolmOutSessions < NUM_MEGOLM_SESSIONS) { MatrixMegolmOutSessionInit( &client->megolmOutSessions[client->numMegolmOutSessions], roomId); - - *outSession = &client->megolmOutSessions[client->numMegolmOutSessions]; client->numMegolmOutSessions++; return true; } - return false; } diff --git a/src/matrix.h b/src/matrix.h index 073f610..3614b6a 100644 --- a/src/matrix.h +++ b/src/matrix.h @@ -11,6 +11,7 @@ #define USER_ID_SIZE 64 +#define ROOM_ID_SIZE 128 #define SERVER_SIZE 20 #define ACCESS_TOKEN_SIZE 40 #define DEVICE_ID_SIZE 20 @@ -102,7 +103,7 @@ typedef struct MatrixMegolmInSession { } MatrixMegolmInSession; typedef struct MatrixMegolmOutSession { - const char * roomId; + char roomId[ROOM_ID_SIZE]; OlmOutboundGroupSession * session; char memory[MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE]; @@ -264,6 +265,11 @@ MatrixClientSetMegolmOutSession( const char * roomId, MatrixMegolmOutSession session); +bool +MatrixClientInitMegolmOutSession( + MatrixClient * client, + const char * roomId); + bool MatrixClientGetOlmSession( MatrixClient * client, -- cgit v1.2.3