diff options
Diffstat (limited to 'src/messenger/gnunet-service-messenger_ego_store.c')
-rw-r--r-- | src/messenger/gnunet-service-messenger_ego_store.c | 175 |
1 files changed, 151 insertions, 24 deletions
diff --git a/src/messenger/gnunet-service-messenger_ego_store.c b/src/messenger/gnunet-service-messenger_ego_store.c index c460ac1c7..bcc301e95 100644 --- a/src/messenger/gnunet-service-messenger_ego_store.c +++ b/src/messenger/gnunet-service-messenger_ego_store.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | Copyright (C) 2020--2021 GNUnet e.V. | 3 | Copyright (C) 2020--2022 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software: you can redistribute it and/or modify it | 5 | GNUnet is free software: you can redistribute it and/or modify it |
6 | under the terms of the GNU Affero General Public License as published | 6 | under the terms of the GNU Affero General Public License as published |
@@ -33,14 +33,21 @@ callback_update_ego (void *cls, | |||
33 | void **ctx, | 33 | void **ctx, |
34 | const char *identifier) | 34 | const char *identifier) |
35 | { | 35 | { |
36 | if ((!ego) || (!identifier)) | 36 | if ((!ctx) || (!identifier)) |
37 | return; | 37 | return; |
38 | 38 | ||
39 | struct GNUNET_MESSENGER_EgoStore *store = cls; | 39 | struct GNUNET_MESSENGER_EgoStore *store = cls; |
40 | 40 | ||
41 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "New ego in use: '%s'\n", identifier); | 41 | if (ego) |
42 | 42 | { | |
43 | update_store_ego (store, identifier, GNUNET_IDENTITY_ego_get_private_key (ego)); | 43 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New ego in use: '%s'\n", identifier); |
44 | update_store_ego (store, identifier, GNUNET_IDENTITY_ego_get_private_key (ego)); | ||
45 | } | ||
46 | else | ||
47 | { | ||
48 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ego got deleted: '%s'\n", identifier); | ||
49 | delete_store_ego (store, identifier); | ||
50 | } | ||
44 | } | 51 | } |
45 | 52 | ||
46 | void | 53 | void |
@@ -52,6 +59,7 @@ init_ego_store(struct GNUNET_MESSENGER_EgoStore *store, | |||
52 | store->cfg = config; | 59 | store->cfg = config; |
53 | store->identity = GNUNET_IDENTITY_connect (config, &callback_update_ego, store); | 60 | store->identity = GNUNET_IDENTITY_connect (config, &callback_update_ego, store); |
54 | store->egos = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO); | 61 | store->egos = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO); |
62 | store->handles = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO); | ||
55 | 63 | ||
56 | store->lu_start = NULL; | 64 | store->lu_start = NULL; |
57 | store->lu_end = NULL; | 65 | store->lu_end = NULL; |
@@ -60,7 +68,6 @@ init_ego_store(struct GNUNET_MESSENGER_EgoStore *store, | |||
60 | store->op_end = NULL; | 68 | store->op_end = NULL; |
61 | } | 69 | } |
62 | 70 | ||
63 | |||
64 | static int | 71 | static int |
65 | iterate_destroy_egos (void *cls, | 72 | iterate_destroy_egos (void *cls, |
66 | const struct GNUNET_HashCode *key, | 73 | const struct GNUNET_HashCode *key, |
@@ -109,6 +116,8 @@ clear_ego_store(struct GNUNET_MESSENGER_EgoStore *store) | |||
109 | GNUNET_CONTAINER_multihashmap_iterate (store->egos, iterate_destroy_egos, NULL); | 116 | GNUNET_CONTAINER_multihashmap_iterate (store->egos, iterate_destroy_egos, NULL); |
110 | GNUNET_CONTAINER_multihashmap_destroy (store->egos); | 117 | GNUNET_CONTAINER_multihashmap_destroy (store->egos); |
111 | 118 | ||
119 | GNUNET_CONTAINER_multihashmap_destroy (store->handles); | ||
120 | |||
112 | if (store->identity) | 121 | if (store->identity) |
113 | { | 122 | { |
114 | GNUNET_IDENTITY_disconnect (store->identity); | 123 | GNUNET_IDENTITY_disconnect (store->identity); |
@@ -117,6 +126,16 @@ clear_ego_store(struct GNUNET_MESSENGER_EgoStore *store) | |||
117 | } | 126 | } |
118 | } | 127 | } |
119 | 128 | ||
129 | static int | ||
130 | iterate_create_ego (void *cls, | ||
131 | const struct GNUNET_HashCode *key, | ||
132 | void *value) | ||
133 | { | ||
134 | struct GNUNET_MESSENGER_SrvHandle *handle = value; | ||
135 | set_handle_ego (handle, (struct GNUNET_MESSENGER_Ego*) cls); | ||
136 | return GNUNET_YES; | ||
137 | } | ||
138 | |||
120 | static void | 139 | static void |
121 | callback_ego_create (void *cls, | 140 | callback_ego_create (void *cls, |
122 | const struct GNUNET_IDENTITY_PrivateKey *key, | 141 | const struct GNUNET_IDENTITY_PrivateKey *key, |
@@ -125,21 +144,22 @@ callback_ego_create (void *cls, | |||
125 | struct GNUNET_MESSENGER_EgoOperation *element = cls; | 144 | struct GNUNET_MESSENGER_EgoOperation *element = cls; |
126 | struct GNUNET_MESSENGER_EgoStore *store = element->store; | 145 | struct GNUNET_MESSENGER_EgoStore *store = element->store; |
127 | 146 | ||
128 | GNUNET_assert(element->identifier); | 147 | GNUNET_assert (element->identifier); |
129 | 148 | ||
130 | if (emsg) | 149 | if (emsg) |
131 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s\n", emsg); | 150 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%s\n", emsg); |
132 | 151 | ||
133 | if (key) | 152 | if (key) |
134 | { | 153 | { |
135 | struct GNUNET_MESSENGER_SrvHandle *handle = element->handle; | ||
136 | |||
137 | struct GNUNET_MESSENGER_Ego *msg_ego = update_store_ego (store, element->identifier, key); | 154 | struct GNUNET_MESSENGER_Ego *msg_ego = update_store_ego (store, element->identifier, key); |
138 | 155 | ||
139 | set_handle_ego (handle, msg_ego); | 156 | struct GNUNET_HashCode hash; |
157 | GNUNET_CRYPTO_hash (element->identifier, strlen (element->identifier), &hash); | ||
158 | |||
159 | GNUNET_CONTAINER_multihashmap_get_multiple (store->handles, &hash, iterate_create_ego, msg_ego); | ||
140 | } | 160 | } |
141 | else | 161 | else |
142 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Creating ego failed!\n"); | 162 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Creating ego failed!\n"); |
143 | 163 | ||
144 | GNUNET_CONTAINER_DLL_remove (store->op_start, store->op_end, element); | 164 | GNUNET_CONTAINER_DLL_remove (store->op_start, store->op_end, element); |
145 | GNUNET_free (element->identifier); | 165 | GNUNET_free (element->identifier); |
@@ -148,15 +168,16 @@ callback_ego_create (void *cls, | |||
148 | 168 | ||
149 | void | 169 | void |
150 | create_store_ego (struct GNUNET_MESSENGER_EgoStore *store, | 170 | create_store_ego (struct GNUNET_MESSENGER_EgoStore *store, |
151 | const char *identifier, | 171 | const char *identifier) |
152 | void *handle) | ||
153 | { | 172 | { |
154 | GNUNET_assert ((store) && (identifier)); | 173 | GNUNET_assert ((store) && (identifier)); |
155 | 174 | ||
175 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Store create ego: %s\n", identifier); | ||
176 | |||
156 | struct GNUNET_MESSENGER_EgoOperation *element = GNUNET_new (struct GNUNET_MESSENGER_EgoOperation); | 177 | struct GNUNET_MESSENGER_EgoOperation *element = GNUNET_new (struct GNUNET_MESSENGER_EgoOperation); |
157 | 178 | ||
158 | element->store = store; | 179 | element->store = store; |
159 | element->handle = handle; | 180 | element->cls = NULL; |
160 | 181 | ||
161 | element->identifier = GNUNET_strdup (identifier); | 182 | element->identifier = GNUNET_strdup (identifier); |
162 | 183 | ||
@@ -166,6 +187,38 @@ create_store_ego (struct GNUNET_MESSENGER_EgoStore *store, | |||
166 | GNUNET_CONTAINER_DLL_insert (store->op_start, store->op_end, element); | 187 | GNUNET_CONTAINER_DLL_insert (store->op_start, store->op_end, element); |
167 | } | 188 | } |
168 | 189 | ||
190 | void | ||
191 | bind_store_ego (struct GNUNET_MESSENGER_EgoStore *store, | ||
192 | const char *identifier, | ||
193 | void *handle) | ||
194 | { | ||
195 | GNUNET_assert ((store) && (identifier) && (handle)); | ||
196 | |||
197 | struct GNUNET_HashCode hash; | ||
198 | GNUNET_CRYPTO_hash (identifier, strlen (identifier), &hash); | ||
199 | |||
200 | if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains_value(store->handles, &hash, handle)) | ||
201 | return; | ||
202 | |||
203 | GNUNET_CONTAINER_multihashmap_put(store->handles, &hash, handle, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); | ||
204 | } | ||
205 | |||
206 | void | ||
207 | unbind_store_ego (struct GNUNET_MESSENGER_EgoStore *store, | ||
208 | const char *identifier, | ||
209 | void *handle) | ||
210 | { | ||
211 | GNUNET_assert ((store) && (identifier) && (handle)); | ||
212 | |||
213 | struct GNUNET_HashCode hash; | ||
214 | GNUNET_CRYPTO_hash (identifier, strlen (identifier), &hash); | ||
215 | |||
216 | if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains_value(store->handles, &hash, handle)) | ||
217 | return; | ||
218 | |||
219 | GNUNET_CONTAINER_multihashmap_remove(store->handles, &hash, handle); | ||
220 | } | ||
221 | |||
169 | static void | 222 | static void |
170 | callback_ego_lookup (void *cls, | 223 | callback_ego_lookup (void *cls, |
171 | struct GNUNET_IDENTITY_Ego *ego) | 224 | struct GNUNET_IDENTITY_Ego *ego) |
@@ -173,7 +226,7 @@ callback_ego_lookup (void *cls, | |||
173 | struct GNUNET_MESSENGER_EgoLookup *element = cls; | 226 | struct GNUNET_MESSENGER_EgoLookup *element = cls; |
174 | struct GNUNET_MESSENGER_EgoStore *store = element->store; | 227 | struct GNUNET_MESSENGER_EgoStore *store = element->store; |
175 | 228 | ||
176 | GNUNET_assert(element->identifier); | 229 | GNUNET_assert (element->identifier); |
177 | 230 | ||
178 | struct GNUNET_MESSENGER_Ego *msg_ego; | 231 | struct GNUNET_MESSENGER_Ego *msg_ego; |
179 | 232 | ||
@@ -200,6 +253,8 @@ lookup_store_ego(struct GNUNET_MESSENGER_EgoStore *store, | |||
200 | { | 253 | { |
201 | GNUNET_assert (store); | 254 | GNUNET_assert (store); |
202 | 255 | ||
256 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Store lookup ego: %s\n", identifier); | ||
257 | |||
203 | if (!identifier) | 258 | if (!identifier) |
204 | { | 259 | { |
205 | lookup(cls, identifier, NULL); | 260 | lookup(cls, identifier, NULL); |
@@ -231,12 +286,14 @@ lookup_store_ego(struct GNUNET_MESSENGER_EgoStore *store, | |||
231 | } | 286 | } |
232 | 287 | ||
233 | struct GNUNET_MESSENGER_Ego* | 288 | struct GNUNET_MESSENGER_Ego* |
234 | update_store_ego(struct GNUNET_MESSENGER_EgoStore *store, | 289 | update_store_ego (struct GNUNET_MESSENGER_EgoStore *store, |
235 | const char *identifier, | 290 | const char *identifier, |
236 | const struct GNUNET_IDENTITY_PrivateKey *key) | 291 | const struct GNUNET_IDENTITY_PrivateKey *key) |
237 | { | 292 | { |
238 | GNUNET_assert ((store) && (identifier) && (key)); | 293 | GNUNET_assert ((store) && (identifier) && (key)); |
239 | 294 | ||
295 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Store update ego: %s\n", identifier); | ||
296 | |||
240 | struct GNUNET_HashCode hash; | 297 | struct GNUNET_HashCode hash; |
241 | GNUNET_CRYPTO_hash (identifier, strlen (identifier), &hash); | 298 | GNUNET_CRYPTO_hash (identifier, strlen (identifier), &hash); |
242 | 299 | ||
@@ -256,6 +313,29 @@ update_store_ego(struct GNUNET_MESSENGER_EgoStore *store, | |||
256 | return ego; | 313 | return ego; |
257 | } | 314 | } |
258 | 315 | ||
316 | void | ||
317 | delete_store_ego (struct GNUNET_MESSENGER_EgoStore *store, | ||
318 | const char *identifier) | ||
319 | { | ||
320 | GNUNET_assert ((store) && (identifier)); | ||
321 | |||
322 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Store delete ego: %s\n", identifier); | ||
323 | |||
324 | struct GNUNET_HashCode hash; | ||
325 | GNUNET_CRYPTO_hash (identifier, strlen (identifier), &hash); | ||
326 | |||
327 | struct GNUNET_MESSENGER_Ego *ego = GNUNET_CONTAINER_multihashmap_get (store->egos, &hash); | ||
328 | |||
329 | if (ego) | ||
330 | { | ||
331 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Ego is not stored!\n"); | ||
332 | return; | ||
333 | } | ||
334 | |||
335 | GNUNET_CONTAINER_multihashmap_remove (store->egos, &hash, ego); | ||
336 | GNUNET_free(ego); | ||
337 | } | ||
338 | |||
259 | static void | 339 | static void |
260 | callback_ego_rename (void *cls, | 340 | callback_ego_rename (void *cls, |
261 | const char *emsg) | 341 | const char *emsg) |
@@ -263,19 +343,24 @@ callback_ego_rename (void *cls, | |||
263 | struct GNUNET_MESSENGER_EgoOperation *element = cls; | 343 | struct GNUNET_MESSENGER_EgoOperation *element = cls; |
264 | struct GNUNET_MESSENGER_EgoStore *store = element->store; | 344 | struct GNUNET_MESSENGER_EgoStore *store = element->store; |
265 | 345 | ||
266 | GNUNET_assert(element->identifier); | 346 | GNUNET_assert (element->identifier); |
267 | 347 | ||
268 | if (emsg) | 348 | if (emsg) |
269 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s\n", emsg); | 349 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%s\n", emsg); |
270 | 350 | ||
271 | struct GNUNET_HashCode hash; | 351 | struct GNUNET_HashCode hash; |
272 | GNUNET_CRYPTO_hash (element->identifier, strlen (element->identifier), &hash); | 352 | GNUNET_CRYPTO_hash (element->identifier, strlen (element->identifier), &hash); |
273 | 353 | ||
274 | struct GNUNET_MESSENGER_Ego *ego = GNUNET_CONTAINER_multihashmap_get (store->egos, &hash); | 354 | struct GNUNET_MESSENGER_Ego *ego = GNUNET_CONTAINER_multihashmap_get (store->egos, &hash); |
275 | 355 | ||
356 | if (!ego) | ||
357 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Ego is not stored!\n"); | ||
358 | |||
359 | char *identifier = (char*) element->cls; | ||
360 | |||
276 | if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (store->egos, &hash, ego)) | 361 | if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (store->egos, &hash, ego)) |
277 | { | 362 | { |
278 | GNUNET_CRYPTO_hash ((char*) element->handle, strlen ((char*) element->handle), &hash); | 363 | GNUNET_CRYPTO_hash (identifier, strlen (identifier), &hash); |
279 | 364 | ||
280 | GNUNET_CONTAINER_multihashmap_put (store->egos, &hash, ego, | 365 | GNUNET_CONTAINER_multihashmap_put (store->egos, &hash, ego, |
281 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); | 366 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); |
@@ -283,7 +368,7 @@ callback_ego_rename (void *cls, | |||
283 | else | 368 | else |
284 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Renaming ego failed!\n"); | 369 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Renaming ego failed!\n"); |
285 | 370 | ||
286 | GNUNET_free (element->handle); | 371 | GNUNET_free (identifier); |
287 | 372 | ||
288 | GNUNET_CONTAINER_DLL_remove (store->op_start, store->op_end, element); | 373 | GNUNET_CONTAINER_DLL_remove (store->op_start, store->op_end, element); |
289 | GNUNET_free (element->identifier); | 374 | GNUNET_free (element->identifier); |
@@ -297,10 +382,12 @@ rename_store_ego (struct GNUNET_MESSENGER_EgoStore *store, | |||
297 | { | 382 | { |
298 | GNUNET_assert ((store) && (old_identifier) && (new_identifier)); | 383 | GNUNET_assert ((store) && (old_identifier) && (new_identifier)); |
299 | 384 | ||
385 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Store rename ego: %s -> %s\n", old_identifier, new_identifier); | ||
386 | |||
300 | struct GNUNET_MESSENGER_EgoOperation *element = GNUNET_new (struct GNUNET_MESSENGER_EgoOperation); | 387 | struct GNUNET_MESSENGER_EgoOperation *element = GNUNET_new (struct GNUNET_MESSENGER_EgoOperation); |
301 | 388 | ||
302 | element->store = store; | 389 | element->store = store; |
303 | element->handle = GNUNET_strdup (new_identifier); | 390 | element->cls = GNUNET_strdup (new_identifier); |
304 | 391 | ||
305 | element->identifier = GNUNET_strdup (old_identifier); | 392 | element->identifier = GNUNET_strdup (old_identifier); |
306 | 393 | ||
@@ -308,3 +395,43 @@ rename_store_ego (struct GNUNET_MESSENGER_EgoStore *store, | |||
308 | 395 | ||
309 | GNUNET_CONTAINER_DLL_insert (store->op_start, store->op_end, element); | 396 | GNUNET_CONTAINER_DLL_insert (store->op_start, store->op_end, element); |
310 | } | 397 | } |
398 | |||
399 | |||
400 | static void | ||
401 | callback_ego_delete (void *cls, | ||
402 | const char *emsg) | ||
403 | { | ||
404 | struct GNUNET_MESSENGER_EgoOperation *element = cls; | ||
405 | struct GNUNET_MESSENGER_EgoStore *store = element->store; | ||
406 | |||
407 | GNUNET_assert (element->identifier); | ||
408 | |||
409 | if (emsg) | ||
410 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%s\n", emsg); | ||
411 | |||
412 | create_store_ego (store, element->identifier); | ||
413 | |||
414 | GNUNET_CONTAINER_DLL_remove (store->op_start, store->op_end, element); | ||
415 | GNUNET_free (element->identifier); | ||
416 | GNUNET_free (element); | ||
417 | } | ||
418 | |||
419 | void | ||
420 | renew_store_ego (struct GNUNET_MESSENGER_EgoStore *store, | ||
421 | const char *identifier) | ||
422 | { | ||
423 | GNUNET_assert ((store) && (identifier)); | ||
424 | |||
425 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Store renew ego: %s\n", identifier); | ||
426 | |||
427 | struct GNUNET_MESSENGER_EgoOperation *element = GNUNET_new (struct GNUNET_MESSENGER_EgoOperation); | ||
428 | |||
429 | element->store = store; | ||
430 | element->cls = NULL; | ||
431 | |||
432 | element->identifier = GNUNET_strdup (identifier); | ||
433 | |||
434 | element->operation = GNUNET_IDENTITY_delete(store->identity, identifier, callback_ego_delete, element); | ||
435 | |||
436 | GNUNET_CONTAINER_DLL_insert (store->op_start, store->op_end, element); | ||
437 | } | ||