GNUnet  0.19.2
gnunet-service-messenger_ego_store.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2020--2022 GNUnet e.V.
4 
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
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
26 #include "platform.h"
28 
30 
31 static void
33  struct GNUNET_IDENTITY_Ego *ego,
34  void **ctx,
35  const char *identifier)
36 {
37  if ((!ctx) || (!identifier))
38  return;
39 
40  struct GNUNET_MESSENGER_EgoStore *store = cls;
41 
42  if (ego)
43  {
44  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New ego in use: '%s'\n", identifier);
45  update_store_ego (store, identifier, GNUNET_IDENTITY_ego_get_private_key (ego));
46  }
47  else
48  {
49  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ego got deleted: '%s'\n", identifier);
50  delete_store_ego (store, identifier);
51  }
52 }
53 
54 void
56  const struct GNUNET_CONFIGURATION_Handle *config)
57 {
58  GNUNET_assert ((store) && (config));
59 
60  store->cfg = config;
64 
65  store->lu_start = NULL;
66  store->lu_end = NULL;
67 
68  store->op_start = NULL;
69  store->op_end = NULL;
70 }
71 
72 static int
74  const struct GNUNET_HashCode *key,
75  void *value)
76 {
77  struct GNUNET_MESSENGER_Ego *ego = value;
78  GNUNET_free(ego);
79  return GNUNET_YES;
80 }
81 
82 void
84 {
85  GNUNET_assert (store);
86 
88 
89  while (store->op_start)
90  {
91  op = store->op_start;
92 
93  GNUNET_IDENTITY_cancel (op->operation);
95 
96  if (op->identifier)
97  GNUNET_free (op->identifier);
98 
99  GNUNET_free (op);
100  }
101 
102  struct GNUNET_MESSENGER_EgoLookup *lu;
103 
104  while (store->lu_start)
105  {
106  lu = store->lu_start;
107 
110 
111  if (lu->identifier)
112  GNUNET_free(lu->identifier);
113 
114  GNUNET_free (lu);
115  }
116 
119 
121 
122  if (store->identity)
123  {
125 
126  store->identity = NULL;
127  }
128 }
129 
130 static int
132  const struct GNUNET_HashCode *key,
133  void *value)
134 {
137  return GNUNET_YES;
138 }
139 
140 static void
142  const struct GNUNET_IDENTITY_PrivateKey *key,
143  enum GNUNET_ErrorCode ec)
144 {
145  struct GNUNET_MESSENGER_EgoOperation *element = cls;
146  struct GNUNET_MESSENGER_EgoStore *store = element->store;
147 
148  GNUNET_assert (element->identifier);
149 
153  if (GNUNET_EC_NONE != ec)
156 
157  if (key)
158  {
159  struct GNUNET_MESSENGER_Ego *msg_ego = update_store_ego (store, element->identifier, key);
160 
161  struct GNUNET_HashCode hash;
162  GNUNET_CRYPTO_hash (element->identifier, strlen (element->identifier), &hash);
163 
165  }
166  else
167  GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Creating ego failed!\n");
168 
169  GNUNET_CONTAINER_DLL_remove (store->op_start, store->op_end, element);
170  GNUNET_free (element->identifier);
171  GNUNET_free (element);
172 }
173 
174 void
176  const char *identifier)
177 {
178  GNUNET_assert ((store) && (identifier));
179 
180  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Store create ego: %s\n", identifier);
181 
183 
184  element->store = store;
185  element->cls = NULL;
186 
187  element->identifier = GNUNET_strdup (identifier);
188 
189  element->operation = GNUNET_IDENTITY_create (
190  store->identity,
191  identifier,
192  NULL,
195  element
196  );
197 
199 }
200 
201 void
203  const char *identifier,
204  void *handle)
205 {
206  GNUNET_assert ((store) && (identifier) && (handle));
207 
208  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Store bind ego: %s\n", identifier);
209 
210  struct GNUNET_HashCode hash;
211  GNUNET_CRYPTO_hash (identifier, strlen (identifier), &hash);
212 
214  return;
215 
218  GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Putting handle binding to ego store failed!\n");
219 }
220 
221 void
223  const char *identifier,
224  void *handle)
225 {
226  GNUNET_assert ((store) && (identifier) && (handle));
227 
228  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Store unbind ego: %s\n", identifier);
229 
230  struct GNUNET_HashCode hash;
231  GNUNET_CRYPTO_hash (identifier, strlen (identifier), &hash);
232 
234  return;
235 
237  GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Removing handle binding from ego store failed!\n");
238 }
239 
240 static void
242  struct GNUNET_IDENTITY_Ego *ego)
243 {
244  struct GNUNET_MESSENGER_EgoLookup *element = cls;
245  struct GNUNET_MESSENGER_EgoStore *store = element->store;
246 
247  GNUNET_assert (element->identifier);
248 
249  struct GNUNET_MESSENGER_Ego *msg_ego = NULL;
250 
251  if (ego)
252  {
253  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New ego looked up: '%s'\n", element->identifier);
254  msg_ego = update_store_ego (
255  store,
256  element->identifier,
258  );
259  }
260  else
261  {
262  struct GNUNET_HashCode hash;
263  GNUNET_CRYPTO_hash (element->identifier, strlen (element->identifier), &hash);
264 
265  if (GNUNET_CONTAINER_multihashmap_get (store->egos, &hash))
266  {
267  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looked up ego got deleted: '%s'\n", element->identifier);
268  delete_store_ego(store, element->identifier);
269  }
270  }
271 
272  if (element->cb)
273  element->cb(element->cls, element->identifier, msg_ego);
274 
275  GNUNET_CONTAINER_DLL_remove (store->lu_start, store->lu_end, element);
276  GNUNET_free (element->identifier);
277  GNUNET_free (element);
278 }
279 
280 void
282  const char *identifier,
284  void *cls)
285 {
286  GNUNET_assert (store);
287 
288  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Store lookup ego: %s\n", identifier);
289 
290  if (!identifier)
291  {
292  lookup(cls, identifier, NULL);
293  return;
294  }
295 
297 
298  element->store = store;
299 
300  element->cb = lookup;
301  element->cls = cls;
302 
303  element->identifier = GNUNET_strdup (identifier);
304 
306 
308 }
309 
310 struct GNUNET_MESSENGER_Ego*
312  const char *identifier,
313  const struct GNUNET_IDENTITY_PrivateKey *key)
314 {
315  GNUNET_assert ((store) && (identifier) && (key));
316 
317  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Store update ego: %s\n", identifier);
318 
319  struct GNUNET_HashCode hash;
320  GNUNET_CRYPTO_hash (identifier, strlen (identifier), &hash);
321 
322  struct GNUNET_MESSENGER_Ego *ego = GNUNET_CONTAINER_multihashmap_get (store->egos, &hash);
323 
324  if (!ego)
325  {
326  ego = GNUNET_new(struct GNUNET_MESSENGER_Ego);
328  }
329 
330  GNUNET_memcpy(&(ego->priv), key, sizeof(*key));
331 
333  GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Updating invalid ego key failed!\n");
334 
335  return ego;
336 }
337 
338 void
340  const char *identifier)
341 {
342  GNUNET_assert ((store) && (identifier));
343 
344  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Store delete ego: %s\n", identifier);
345 
346  struct GNUNET_HashCode hash;
347  GNUNET_CRYPTO_hash (identifier, strlen (identifier), &hash);
348 
349  struct GNUNET_MESSENGER_Ego *ego = GNUNET_CONTAINER_multihashmap_get (store->egos, &hash);
350 
351  if (ego)
352  {
353  GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Ego is not stored!\n");
354  return;
355  }
356 
357  if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (store->egos, &hash, ego))
358  {
359  GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Removing ego from store failed!\n");
360  return;
361  }
362 
363  GNUNET_free(ego);
364 }
365 
366 static void
368  enum GNUNET_ErrorCode ec)
369 {
370  struct GNUNET_MESSENGER_EgoOperation *element = cls;
371  struct GNUNET_MESSENGER_EgoStore *store = element->store;
372 
373  GNUNET_assert (element->identifier);
374 
378  if (GNUNET_EC_NONE != ec)
381 
382  struct GNUNET_HashCode hash;
383  GNUNET_CRYPTO_hash (element->identifier, strlen (element->identifier), &hash);
384 
385  struct GNUNET_MESSENGER_Ego *ego = GNUNET_CONTAINER_multihashmap_get (store->egos, &hash);
386 
387  if (!ego)
388  GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Ego is not stored!\n");
389 
390  char *identifier = (char*) element->cls;
391 
392  if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (store->egos, &hash, ego))
393  {
394  GNUNET_CRYPTO_hash (identifier, strlen (identifier), &hash);
395 
396  GNUNET_CONTAINER_multihashmap_put (store->egos, &hash, ego,
398  }
399  else
400  GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Renaming ego failed!\n");
401 
402  GNUNET_free (identifier);
403 
404  GNUNET_CONTAINER_DLL_remove (store->op_start, store->op_end, element);
405  GNUNET_free (element->identifier);
406  GNUNET_free (element);
407 }
408 
409 void
411  const char *old_identifier,
412  const char *new_identifier)
413 {
414  GNUNET_assert ((store) && (old_identifier) && (new_identifier));
415 
416  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Store rename ego: %s -> %s\n", old_identifier, new_identifier);
417 
419 
420  element->store = store;
421  element->cls = GNUNET_strdup (new_identifier);
422 
423  element->identifier = GNUNET_strdup (old_identifier);
424 
425  element->operation = GNUNET_IDENTITY_rename (
426  store->identity,
427  old_identifier,
428  new_identifier,
430  element
431  );
432 
434 }
435 
436 static void
438  enum GNUNET_ErrorCode ec)
439 {
440  struct GNUNET_MESSENGER_EgoOperation *element = cls;
441  struct GNUNET_MESSENGER_EgoStore *store = element->store;
442 
443  GNUNET_assert (element->identifier);
444 
448  if (GNUNET_EC_NONE != ec)
451 
452  create_store_ego (store, element->identifier);
453 
454  GNUNET_CONTAINER_DLL_remove (store->op_start, store->op_end, element);
455  GNUNET_free (element->identifier);
456  GNUNET_free (element);
457 }
458 
459 void
461  const char *identifier)
462 {
463  GNUNET_assert ((store) && (identifier));
464 
465  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Store renew ego: %s\n", identifier);
466 
468 
469  element->store = store;
470  element->cls = NULL;
471 
472  element->identifier = GNUNET_strdup (identifier);
473 
475  store->identity,
476  identifier,
478  element
479  );
480 
482 }
const char * GNUNET_ErrorCode_get_hint(enum GNUNET_ErrorCode ec)
Returns a hint for a given error code.
GNUNET_ErrorCode
Taler error codes.
@ GNUNET_EC_NONE
No error (success).
static struct GNUNET_ARM_Operation * op
Current operation.
Definition: gnunet-arm.c:144
struct GNUNET_HashCode key
The key used in the DHT.
static struct GNUNET_DNS_Handle * handle
Handle to transport service.
static char * value
Value of the record to add/remove.
void renew_store_ego(struct GNUNET_MESSENGER_EgoStore *store, const char *identifier)
Replaces the registered EGO in a store under a specific identifier with a newly created one.
static int iterate_create_ego(void *cls, const struct GNUNET_HashCode *key, void *value)
static void callback_ego_delete(void *cls, enum GNUNET_ErrorCode ec)
static void callback_ego_rename(void *cls, enum GNUNET_ErrorCode ec)
void delete_store_ego(struct GNUNET_MESSENGER_EgoStore *store, const char *identifier)
Deletes the registration of an EGO in a store under a specific identifier.
void bind_store_ego(struct GNUNET_MESSENGER_EgoStore *store, const char *identifier, void *handle)
Binds an EGO which was registered to a store under a specific identifier to a given handle
struct GNUNET_MESSENGER_Ego * update_store_ego(struct GNUNET_MESSENGER_EgoStore *store, const char *identifier, const struct GNUNET_IDENTITY_PrivateKey *key)
Updates the registration of an EGO to a store under a specific identifier with a new key.
void lookup_store_ego(struct GNUNET_MESSENGER_EgoStore *store, const char *identifier, GNUNET_MESSENGER_EgoLookupCallback lookup, void *cls)
Lookups an EGO which was registered to a store under a specific identifier.
static int iterate_destroy_egos(void *cls, const struct GNUNET_HashCode *key, void *value)
void clear_ego_store(struct GNUNET_MESSENGER_EgoStore *store)
Clears an EGO-store, wipes its content and deallocates its memory.
void create_store_ego(struct GNUNET_MESSENGER_EgoStore *store, const char *identifier)
Creates a new EGO which will be registered to a store under a specific identifier.
void unbind_store_ego(struct GNUNET_MESSENGER_EgoStore *store, const char *identifier, void *handle)
Binds an EGO which was registered to a store under a specific identifier to a given handle
static void callback_ego_create(void *cls, const struct GNUNET_IDENTITY_PrivateKey *key, enum GNUNET_ErrorCode ec)
static void callback_ego_lookup(void *cls, struct GNUNET_IDENTITY_Ego *ego)
void init_ego_store(struct GNUNET_MESSENGER_EgoStore *store, const struct GNUNET_CONFIGURATION_Handle *config)
Initializes an EGO-store as fully empty.
void rename_store_ego(struct GNUNET_MESSENGER_EgoStore *store, const char *old_identifier, const char *new_identifier)
Updates the location of a registered EGO in a store to a different one under a specific new_identifie...
static void callback_update_ego(void *cls, struct GNUNET_IDENTITY_Ego *ego, void **ctx, const char *identifier)
GNUnet MESSENGER service.
void(* GNUNET_MESSENGER_EgoLookupCallback)(void *cls, const char *identifier, const struct GNUNET_MESSENGER_Ego *ego)
void set_srv_handle_ego(struct GNUNET_MESSENGER_SrvHandle *handle, const struct GNUNET_MESSENGER_Ego *ego)
Sets the EGO used by a given handle.
GNUnet MESSENGER service.
static const struct GNUNET_CONFIGURATION_Handle * config
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:41
int GNUNET_CONTAINER_multihashmap_get_multiple(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, GNUNET_CONTAINER_MulitHashMapIteratorCallback it, void *it_cls)
Iterate over all entries in the map that match a particular key.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_contains_value(const struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, const void *value)
Check if the map contains the given value under the given key.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_remove(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, const void *value)
Remove the given key-value pair from the map.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_put(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
struct GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
int GNUNET_CONTAINER_multihashmap_iterate(struct GNUNET_CONTAINER_MultiHashMap *map, GNUNET_CONTAINER_MulitHashMapIteratorCallback it, void *it_cls)
Iterate over all entries in the map.
void GNUNET_CONTAINER_multihashmap_destroy(struct GNUNET_CONTAINER_MultiHashMap *map)
Destroy a hash map.
void * GNUNET_CONTAINER_multihashmap_get(const struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key)
Given a key find a value in the map matching the key.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST
, ' bother checking if a value already exists (faster than GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE...
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE
Allow multiple values with the same key.
const struct GNUNET_IDENTITY_PrivateKey * GNUNET_IDENTITY_ego_get_private_key(const struct GNUNET_IDENTITY_Ego *ego)
Obtain the ECC key associated with a ego.
Definition: identity_api.c:560
struct GNUNET_IDENTITY_Operation * GNUNET_IDENTITY_rename(struct GNUNET_IDENTITY_Handle *h, const char *old_name, const char *new_name, GNUNET_IDENTITY_Continuation cb, void *cb_cls)
Renames an existing identity.
Definition: identity_api.c:646
enum GNUNET_GenericReturnValue GNUNET_IDENTITY_key_get_public(const struct GNUNET_IDENTITY_PrivateKey *privkey, struct GNUNET_IDENTITY_PublicKey *key)
Retrieves the public key representation of a private key.
Definition: identity_api.c:179
struct GNUNET_IDENTITY_Operation * GNUNET_IDENTITY_create(struct GNUNET_IDENTITY_Handle *h, const char *name, const struct GNUNET_IDENTITY_PrivateKey *privkey, enum GNUNET_IDENTITY_KeyType ktype, GNUNET_IDENTITY_CreateContinuation cont, void *cont_cls)
Create a new ego with the given name.
Definition: identity_api.c:586
void GNUNET_IDENTITY_cancel(struct GNUNET_IDENTITY_Operation *op)
Cancel an identity operation.
Definition: identity_api.c:740
void GNUNET_IDENTITY_ego_lookup_cancel(struct GNUNET_IDENTITY_EgoLookup *el)
Abort ego lookup attempt.
struct GNUNET_IDENTITY_Handle * GNUNET_IDENTITY_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_IDENTITY_Callback cb, void *cb_cls)
Connect to the identity service.
Definition: identity_api.c:531
void GNUNET_IDENTITY_disconnect(struct GNUNET_IDENTITY_Handle *h)
Disconnect from identity service.
Definition: identity_api.c:757
struct GNUNET_IDENTITY_EgoLookup * GNUNET_IDENTITY_ego_lookup(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *name, GNUNET_IDENTITY_EgoCallback cb, void *cb_cls)
Lookup an ego by name.
struct GNUNET_IDENTITY_Operation * GNUNET_IDENTITY_delete(struct GNUNET_IDENTITY_Handle *h, const char *name, GNUNET_IDENTITY_Continuation cb, void *cb_cls)
Delete an existing identity.
Definition: identity_api.c:699
@ GNUNET_IDENTITY_TYPE_ECDSA
The identity type.
#define GNUNET_log(kind,...)
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
A 512-bit hashcode.
Handle for an ego.
Definition: identity.h:37
A private key for an identity as per LSD0001.
struct GNUNET_IDENTITY_EgoLookup * lookup
struct GNUNET_MESSENGER_EgoStore * store
GNUNET_MESSENGER_EgoLookupCallback cb
struct GNUNET_MESSENGER_EgoStore * store
struct GNUNET_IDENTITY_Operation * operation
struct GNUNET_CONTAINER_MultiHashMap * handles
struct GNUNET_MESSENGER_EgoLookup * lu_end
const struct GNUNET_CONFIGURATION_Handle * cfg
struct GNUNET_MESSENGER_EgoLookup * lu_start
struct GNUNET_IDENTITY_Handle * identity
struct GNUNET_CONTAINER_MultiHashMap * egos
struct GNUNET_MESSENGER_EgoOperation * op_end
struct GNUNET_MESSENGER_EgoOperation * op_start
struct GNUNET_IDENTITY_PublicKey pub
struct GNUNET_IDENTITY_PrivateKey priv