]> gitweb.ps.run Git - matrix_esp_thesis/commitdiff
upload device/onetime keys, fix recurring http accesses
authorPatrick <patrick.schoenberger@posteo.de>
Mon, 3 Jul 2023 11:39:12 +0000 (13:39 +0200)
committerPatrick <patrick.schoenberger@posteo.de>
Mon, 3 Jul 2023 11:39:12 +0000 (13:39 +0200)
examples/Keys.c
src/matrix.c
src/matrix.h
src/matrix_http_mongoose.c

index 6dc2cc0f2d150749fc087bf3b7c3e084fb789949..f59a1d55067caeba3cf737ebe86e0d2206ece155 100644 (file)
@@ -21,10 +21,10 @@ main(void)
     MatrixClientSetUserId(&client, USER_ID);\r
 \r
     MatrixClientGenerateOnetimeKeys(&client,\r
-        10);\r
+        2);\r
     \r
-    MatrixClientUploadDeviceKeys(&client);\r
     MatrixClientUploadOnetimeKeys(&client);\r
+    MatrixClientUploadDeviceKeys(&client);\r
         \r
     MatrixHttpDeinit(&client);\r
 \r
index d1878c5057f0d5bcc93f2b69e54190c262b99f0c..1f5ed2de70135ea20d2f215b8f251156380c3f34 100644 (file)
 #define KEYS_QUERY_REQUEST_SIZE 256\r
 #define KEYS_QUERY_RESPONSE_SIZE 1024\r
 \r
-#define UPLOAD_KEYS_REQUEST_SIZE 512\r
-#define UPLOAD_KEYS_REQUEST_SIGNED_SIZE 1024\r
+#define KEYS_UPLOAD_URL "/_matrix/client/v3/keys/upload"\r
+#define KEYS_UPLOAD_REQUEST_SIZE 1024\r
+#define KEYS_UPLOAD_REQUEST_SIGNED_SIZE 2048\r
+#define KEYS_UPLOAD_RESPONSE_SIZE 2048\r
 \r
 #define JSON_QUERY_SIZE 128\r
 \r
@@ -301,13 +303,14 @@ MatrixClientGenerateOnetimeKeys(
     return res != olm_error();\r
 }\r
 \r
+// https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv3keysupload\r
 bool\r
 MatrixClientUploadOnetimeKeys(\r
     MatrixClient * client)\r
 {\r
-    static char requestBuffer[UPLOAD_KEYS_REQUEST_SIZE];\r
+    static char requestBuffer[KEYS_UPLOAD_REQUEST_SIZE];\r
 \r
-    mjson_snprintf(requestBuffer, UPLOAD_KEYS_REQUEST_SIZE,\r
+    mjson_snprintf(requestBuffer, KEYS_UPLOAD_REQUEST_SIZE,\r
         "{\"one_time_keys\":{");\r
 \r
     static char onetimeKeysBuffer[1024];\r
@@ -332,27 +335,33 @@ MatrixClientUploadOnetimeKeys(
             keyJson, JSON_ONETIME_KEY_SIZE,\r
             keyJsonSigned, JSON_ONETIME_KEY_SIGNED_SIZE);\r
         \r
-        mjson_snprintf(requestBuffer+strlen(requestBuffer), UPLOAD_KEYS_REQUEST_SIZE-strlen(requestBuffer),\r
+        mjson_snprintf(requestBuffer+strlen(requestBuffer), KEYS_UPLOAD_REQUEST_SIZE-strlen(requestBuffer),\r
             "\"signed_curve25519:%.*s\":%s,",\r
             klen-2, keys + koff+1,\r
             keyJsonSigned);\r
     }\r
 \r
-    mjson_snprintf(requestBuffer+strlen(requestBuffer), UPLOAD_KEYS_REQUEST_SIZE-strlen(requestBuffer),\r
+    mjson_snprintf(requestBuffer+strlen(requestBuffer)-1, KEYS_UPLOAD_REQUEST_SIZE-strlen(requestBuffer),\r
         "}}");\r
 \r
-    printf("%s\n", requestBuffer);\r
+    static char responseBuffer[KEYS_UPLOAD_RESPONSE_SIZE];\r
+    MatrixHttpPost(client,\r
+        KEYS_UPLOAD_URL,\r
+        requestBuffer,\r
+        responseBuffer, KEYS_UPLOAD_RESPONSE_SIZE,\r
+        true);\r
 \r
     return true;\r
 }\r
 \r
+// https://spec.matrix.org/v1.7/client-server-api/#post_matrixclientv3keysupload\r
 bool\r
 MatrixClientUploadDeviceKeys(\r
     MatrixClient * client)\r
 {\r
-    static char deviceKeysBuffer[UPLOAD_KEYS_REQUEST_SIZE];\r
+    static char deviceKeysBuffer[KEYS_UPLOAD_REQUEST_SIZE];\r
 \r
-    mjson_snprintf(deviceKeysBuffer, UPLOAD_KEYS_REQUEST_SIZE,\r
+    mjson_snprintf(deviceKeysBuffer, KEYS_UPLOAD_REQUEST_SIZE,\r
         "{\"device_keys\":{"\r
             "\"algorithms\":[\"m.olm.v1.curve25519-aes-sha2\",\"m.megolm.v1.aes-sha2\"],"\r
             "\"device_id\":\"%s\","\r
@@ -367,12 +376,18 @@ MatrixClientUploadDeviceKeys(
         client->deviceId, client->signingKey,\r
         client->userId);\r
 \r
-    static char deviceKeysSignedBuffer[UPLOAD_KEYS_REQUEST_SIGNED_SIZE];\r
+    static char deviceKeysSignedBuffer[KEYS_UPLOAD_REQUEST_SIGNED_SIZE];\r
     JsonSign(client,\r
-        deviceKeysBuffer, UPLOAD_KEYS_REQUEST_SIZE,\r
-        deviceKeysSignedBuffer, UPLOAD_KEYS_REQUEST_SIZE);\r
+        deviceKeysBuffer, KEYS_UPLOAD_REQUEST_SIZE,\r
+        deviceKeysSignedBuffer, KEYS_UPLOAD_REQUEST_SIZE);\r
 \r
-    printf("%s\n", deviceKeysSignedBuffer);\r
+\r
+    static char responseBuffer[KEYS_UPLOAD_RESPONSE_SIZE];\r
+    MatrixHttpPost(client,\r
+        KEYS_UPLOAD_URL,\r
+        deviceKeysSignedBuffer,\r
+        responseBuffer, KEYS_UPLOAD_RESPONSE_SIZE,\r
+        true);\r
 \r
     return true;\r
 }\r
index 38fb767742cdaf23948313d06ac47a6cfe09d9e3..72e865ff81c8e2f35c39c9fb358aa90dfaefa498 100644 (file)
@@ -285,6 +285,10 @@ bool
 MatrixHttpInit(\r
     MatrixClient * client);\r
 \r
+bool\r
+MatrixHttpConnect(\r
+    MatrixClient * client);\r
+\r
 bool\r
 MatrixHttpDeinit(\r
     MatrixClient * client);\r
index 2e500d69c5557ae35dff29bacb2a89bd4b8e4845..17edbc8cdcfabe7b53e453a05b057ae71520429f 100644 (file)
@@ -58,6 +58,11 @@ MatrixHttpCallback(
 \r
         printf("received[%d]:\n%.*s\n", conn->dataLen, conn->dataLen, conn->data);\r
     }\r
+    if (ev == MG_EV_CLOSE)\r
+    {\r
+        conn->connection = NULL;\r
+        conn->connected = false;\r
+    }\r
 }\r
 \r
 bool\r
@@ -71,6 +76,16 @@ MatrixHttpInit(
     \r
     mg_mgr_init(&conn->mgr);\r
 \r
+    return MatrixHttpConnect(client);\r
+}\r
+\r
+bool\r
+MatrixHttpConnect(\r
+    MatrixClient * client)\r
+{\r
+    MatrixHttpConnection * conn =\r
+        (MatrixHttpConnection *)client->httpUserData;\r
+    \r
     struct mg_connection * c =\r
         mg_http_connect(&conn->mgr, client->server, MatrixHttpCallback, client);\r
 \r
@@ -101,6 +116,8 @@ MatrixHttpGet(
     bool authenticated)\r
 {\r
     MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
+    if (! conn->connected)\r
+        MatrixHttpConnect(client);\r
 \r
     conn->dataReceived = false;\r
 \r
@@ -142,6 +159,8 @@ MatrixHttpPost(
     bool authenticated)\r
 {\r
     MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
+    if (! conn->connected)\r
+        MatrixHttpConnect(client);\r
 \r
     conn->dataReceived = false;\r
 \r
@@ -187,6 +206,8 @@ MatrixHttpPut(
     bool authenticated)\r
 {\r
     MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
+    if (! conn->connected)\r
+        MatrixHttpConnect(client);\r
 \r
     conn->dataReceived = false;\r
 \r