From: Patrick Date: Mon, 3 Jul 2023 11:39:12 +0000 (+0200) Subject: upload device/onetime keys, fix recurring http accesses X-Git-Url: https://gitweb.ps.run/matrix_esp_thesis/commitdiff_plain/a6eff84624ab1f3786d02aa2ec740b9a88090d94 upload device/onetime keys, fix recurring http accesses --- diff --git a/examples/Keys.c b/examples/Keys.c index 6dc2cc0..f59a1d5 100644 --- a/examples/Keys.c +++ b/examples/Keys.c @@ -21,10 +21,10 @@ main(void) MatrixClientSetUserId(&client, USER_ID); MatrixClientGenerateOnetimeKeys(&client, - 10); + 2); - MatrixClientUploadDeviceKeys(&client); MatrixClientUploadOnetimeKeys(&client); + MatrixClientUploadDeviceKeys(&client); MatrixHttpDeinit(&client); diff --git a/src/matrix.c b/src/matrix.c index d1878c5..1f5ed2d 100644 --- a/src/matrix.c +++ b/src/matrix.c @@ -22,8 +22,10 @@ #define KEYS_QUERY_REQUEST_SIZE 256 #define KEYS_QUERY_RESPONSE_SIZE 1024 -#define UPLOAD_KEYS_REQUEST_SIZE 512 -#define UPLOAD_KEYS_REQUEST_SIGNED_SIZE 1024 +#define KEYS_UPLOAD_URL "/_matrix/client/v3/keys/upload" +#define KEYS_UPLOAD_REQUEST_SIZE 1024 +#define KEYS_UPLOAD_REQUEST_SIGNED_SIZE 2048 +#define KEYS_UPLOAD_RESPONSE_SIZE 2048 #define JSON_QUERY_SIZE 128 @@ -301,13 +303,14 @@ MatrixClientGenerateOnetimeKeys( return res != olm_error(); } +// https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv3keysupload bool MatrixClientUploadOnetimeKeys( MatrixClient * client) { - static char requestBuffer[UPLOAD_KEYS_REQUEST_SIZE]; + static char requestBuffer[KEYS_UPLOAD_REQUEST_SIZE]; - mjson_snprintf(requestBuffer, UPLOAD_KEYS_REQUEST_SIZE, + mjson_snprintf(requestBuffer, KEYS_UPLOAD_REQUEST_SIZE, "{\"one_time_keys\":{"); static char onetimeKeysBuffer[1024]; @@ -332,27 +335,33 @@ MatrixClientUploadOnetimeKeys( keyJson, JSON_ONETIME_KEY_SIZE, keyJsonSigned, JSON_ONETIME_KEY_SIGNED_SIZE); - mjson_snprintf(requestBuffer+strlen(requestBuffer), UPLOAD_KEYS_REQUEST_SIZE-strlen(requestBuffer), + mjson_snprintf(requestBuffer+strlen(requestBuffer), KEYS_UPLOAD_REQUEST_SIZE-strlen(requestBuffer), "\"signed_curve25519:%.*s\":%s,", klen-2, keys + koff+1, keyJsonSigned); } - mjson_snprintf(requestBuffer+strlen(requestBuffer), UPLOAD_KEYS_REQUEST_SIZE-strlen(requestBuffer), + mjson_snprintf(requestBuffer+strlen(requestBuffer)-1, KEYS_UPLOAD_REQUEST_SIZE-strlen(requestBuffer), "}}"); - printf("%s\n", requestBuffer); + static char responseBuffer[KEYS_UPLOAD_RESPONSE_SIZE]; + MatrixHttpPost(client, + KEYS_UPLOAD_URL, + requestBuffer, + responseBuffer, KEYS_UPLOAD_RESPONSE_SIZE, + true); return true; } +// https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv3keysupload bool MatrixClientUploadDeviceKeys( MatrixClient * client) { - static char deviceKeysBuffer[UPLOAD_KEYS_REQUEST_SIZE]; + static char deviceKeysBuffer[KEYS_UPLOAD_REQUEST_SIZE]; - mjson_snprintf(deviceKeysBuffer, UPLOAD_KEYS_REQUEST_SIZE, + mjson_snprintf(deviceKeysBuffer, KEYS_UPLOAD_REQUEST_SIZE, "{\"device_keys\":{" "\"algorithms\":[\"m.olm.v1.curve25519-aes-sha2\",\"m.megolm.v1.aes-sha2\"]," "\"device_id\":\"%s\"," @@ -367,12 +376,18 @@ MatrixClientUploadDeviceKeys( client->deviceId, client->signingKey, client->userId); - static char deviceKeysSignedBuffer[UPLOAD_KEYS_REQUEST_SIGNED_SIZE]; + static char deviceKeysSignedBuffer[KEYS_UPLOAD_REQUEST_SIGNED_SIZE]; JsonSign(client, - deviceKeysBuffer, UPLOAD_KEYS_REQUEST_SIZE, - deviceKeysSignedBuffer, UPLOAD_KEYS_REQUEST_SIZE); + deviceKeysBuffer, KEYS_UPLOAD_REQUEST_SIZE, + deviceKeysSignedBuffer, KEYS_UPLOAD_REQUEST_SIZE); - printf("%s\n", deviceKeysSignedBuffer); + + static char responseBuffer[KEYS_UPLOAD_RESPONSE_SIZE]; + MatrixHttpPost(client, + KEYS_UPLOAD_URL, + deviceKeysSignedBuffer, + responseBuffer, KEYS_UPLOAD_RESPONSE_SIZE, + true); return true; } diff --git a/src/matrix.h b/src/matrix.h index 38fb767..72e865f 100644 --- a/src/matrix.h +++ b/src/matrix.h @@ -285,6 +285,10 @@ bool MatrixHttpInit( MatrixClient * client); +bool +MatrixHttpConnect( + MatrixClient * client); + bool MatrixHttpDeinit( MatrixClient * client); diff --git a/src/matrix_http_mongoose.c b/src/matrix_http_mongoose.c index 2e500d6..17edbc8 100644 --- a/src/matrix_http_mongoose.c +++ b/src/matrix_http_mongoose.c @@ -58,6 +58,11 @@ MatrixHttpCallback( printf("received[%d]:\n%.*s\n", conn->dataLen, conn->dataLen, conn->data); } + if (ev == MG_EV_CLOSE) + { + conn->connection = NULL; + conn->connected = false; + } } bool @@ -71,6 +76,16 @@ MatrixHttpInit( mg_mgr_init(&conn->mgr); + return MatrixHttpConnect(client); +} + +bool +MatrixHttpConnect( + MatrixClient * client) +{ + MatrixHttpConnection * conn = + (MatrixHttpConnection *)client->httpUserData; + struct mg_connection * c = mg_http_connect(&conn->mgr, client->server, MatrixHttpCallback, client); @@ -101,6 +116,8 @@ MatrixHttpGet( bool authenticated) { MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData; + if (! conn->connected) + MatrixHttpConnect(client); conn->dataReceived = false; @@ -142,6 +159,8 @@ MatrixHttpPost( bool authenticated) { MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData; + if (! conn->connected) + MatrixHttpConnect(client); conn->dataReceived = false; @@ -187,6 +206,8 @@ MatrixHttpPut( bool authenticated) { MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData; + if (! conn->connected) + MatrixHttpConnect(client); conn->dataReceived = false;