aboutsummaryrefslogtreecommitdiff
path: root/src/messenger/gnunet-service-messenger_ego_store.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/messenger/gnunet-service-messenger_ego_store.c')
-rw-r--r--src/messenger/gnunet-service-messenger_ego_store.c310
1 files changed, 0 insertions, 310 deletions
diff --git a/src/messenger/gnunet-service-messenger_ego_store.c b/src/messenger/gnunet-service-messenger_ego_store.c
deleted file mode 100644
index c460ac1c7..000000000
--- a/src/messenger/gnunet-service-messenger_ego_store.c
+++ /dev/null
@@ -1,310 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2020--2021 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 */
20/**
21 * @author Tobias Frisch
22 * @file src/messenger/gnunet-service-messenger_ego_store.c
23 * @brief GNUnet MESSENGER service
24 */
25
26#include "gnunet-service-messenger_ego_store.h"
27
28#include "gnunet-service-messenger_handle.h"
29
30static void
31callback_update_ego (void *cls,
32 struct GNUNET_IDENTITY_Ego *ego,
33 void **ctx,
34 const char *identifier)
35{
36 if ((!ego) || (!identifier))
37 return;
38
39 struct GNUNET_MESSENGER_EgoStore *store = cls;
40
41 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "New ego in use: '%s'\n", identifier);
42
43 update_store_ego (store, identifier, GNUNET_IDENTITY_ego_get_private_key (ego));
44}
45
46void
47init_ego_store(struct GNUNET_MESSENGER_EgoStore *store,
48 const struct GNUNET_CONFIGURATION_Handle *config)
49{
50 GNUNET_assert ((store) && (config));
51
52 store->cfg = config;
53 store->identity = GNUNET_IDENTITY_connect (config, &callback_update_ego, store);
54 store->egos = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
55
56 store->lu_start = NULL;
57 store->lu_end = NULL;
58
59 store->op_start = NULL;
60 store->op_end = NULL;
61}
62
63
64static int
65iterate_destroy_egos (void *cls,
66 const struct GNUNET_HashCode *key,
67 void *value)
68{
69 struct GNUNET_MESSENGER_Ego *ego = value;
70 GNUNET_free(ego);
71 return GNUNET_YES;
72}
73
74void
75clear_ego_store(struct GNUNET_MESSENGER_EgoStore *store)
76{
77 GNUNET_assert (store);
78
79 struct GNUNET_MESSENGER_EgoOperation *op;
80
81 while (store->op_start)
82 {
83 op = store->op_start;
84
85 GNUNET_IDENTITY_cancel (op->operation);
86 GNUNET_CONTAINER_DLL_remove (store->op_start, store->op_end, op);
87
88 if (op->identifier)
89 GNUNET_free (op->identifier);
90
91 GNUNET_free (op);
92 }
93
94 struct GNUNET_MESSENGER_EgoLookup *lu;
95
96 while (store->lu_start)
97 {
98 lu = store->lu_start;
99
100 GNUNET_IDENTITY_ego_lookup_cancel(lu->lookup);
101 GNUNET_CONTAINER_DLL_remove (store->lu_start, store->lu_end, lu);
102
103 if (lu->identifier)
104 GNUNET_free(lu->identifier);
105
106 GNUNET_free (lu);
107 }
108
109 GNUNET_CONTAINER_multihashmap_iterate (store->egos, iterate_destroy_egos, NULL);
110 GNUNET_CONTAINER_multihashmap_destroy (store->egos);
111
112 if (store->identity)
113 {
114 GNUNET_IDENTITY_disconnect (store->identity);
115
116 store->identity = NULL;
117 }
118}
119
120static void
121callback_ego_create (void *cls,
122 const struct GNUNET_IDENTITY_PrivateKey *key,
123 const char *emsg)
124{
125 struct GNUNET_MESSENGER_EgoOperation *element = cls;
126 struct GNUNET_MESSENGER_EgoStore *store = element->store;
127
128 GNUNET_assert(element->identifier);
129
130 if (emsg)
131 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s\n", emsg);
132
133 if (key)
134 {
135 struct GNUNET_MESSENGER_SrvHandle *handle = element->handle;
136
137 struct GNUNET_MESSENGER_Ego *msg_ego = update_store_ego (store, element->identifier, key);
138
139 set_handle_ego (handle, msg_ego);
140 }
141 else
142 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Creating ego failed!\n");
143
144 GNUNET_CONTAINER_DLL_remove (store->op_start, store->op_end, element);
145 GNUNET_free (element->identifier);
146 GNUNET_free (element);
147}
148
149void
150create_store_ego (struct GNUNET_MESSENGER_EgoStore *store,
151 const char *identifier,
152 void *handle)
153{
154 GNUNET_assert ((store) && (identifier));
155
156 struct GNUNET_MESSENGER_EgoOperation *element = GNUNET_new (struct GNUNET_MESSENGER_EgoOperation);
157
158 element->store = store;
159 element->handle = handle;
160
161 element->identifier = GNUNET_strdup (identifier);
162
163 element->operation = GNUNET_IDENTITY_create (store->identity, identifier, NULL,
164 GNUNET_IDENTITY_TYPE_ECDSA, callback_ego_create, element);
165
166 GNUNET_CONTAINER_DLL_insert (store->op_start, store->op_end, element);
167}
168
169static void
170callback_ego_lookup (void *cls,
171 struct GNUNET_IDENTITY_Ego *ego)
172{
173 struct GNUNET_MESSENGER_EgoLookup *element = cls;
174 struct GNUNET_MESSENGER_EgoStore *store = element->store;
175
176 GNUNET_assert(element->identifier);
177
178 struct GNUNET_MESSENGER_Ego *msg_ego;
179
180 if (ego)
181 msg_ego = update_store_ego (
182 store, element->identifier, GNUNET_IDENTITY_ego_get_private_key(ego)
183 );
184 else
185 msg_ego = NULL;
186
187 if (element->cb)
188 element->cb(element->cls, element->identifier, msg_ego);
189
190 GNUNET_CONTAINER_DLL_remove (store->lu_start, store->lu_end, element);
191 GNUNET_free (element->identifier);
192 GNUNET_free (element);
193}
194
195void
196lookup_store_ego(struct GNUNET_MESSENGER_EgoStore *store,
197 const char *identifier,
198 GNUNET_MESSENGER_EgoLookupCallback lookup,
199 void *cls)
200{
201 GNUNET_assert (store);
202
203 if (!identifier)
204 {
205 lookup(cls, identifier, NULL);
206 return;
207 }
208
209 struct GNUNET_HashCode hash;
210 GNUNET_CRYPTO_hash (identifier, strlen (identifier), &hash);
211
212 struct GNUNET_MESSENGER_Ego *ego = GNUNET_CONTAINER_multihashmap_get (store->egos, &hash);
213
214 if (ego)
215 lookup(cls, identifier, ego);
216 else
217 {
218 struct GNUNET_MESSENGER_EgoLookup *element = GNUNET_new (struct GNUNET_MESSENGER_EgoLookup);
219
220 element->store = store;
221
222 element->cb = lookup;
223 element->cls = cls;
224
225 element->identifier = GNUNET_strdup (identifier);
226
227 element->lookup = GNUNET_IDENTITY_ego_lookup(store->cfg, identifier, callback_ego_lookup, element);
228
229 GNUNET_CONTAINER_DLL_insert (store->lu_start, store->lu_end, element);
230 }
231}
232
233struct GNUNET_MESSENGER_Ego*
234update_store_ego(struct GNUNET_MESSENGER_EgoStore *store,
235 const char *identifier,
236 const struct GNUNET_IDENTITY_PrivateKey *key)
237{
238 GNUNET_assert ((store) && (identifier) && (key));
239
240 struct GNUNET_HashCode hash;
241 GNUNET_CRYPTO_hash (identifier, strlen (identifier), &hash);
242
243 struct GNUNET_MESSENGER_Ego *ego = GNUNET_CONTAINER_multihashmap_get (store->egos, &hash);
244
245 if (!ego)
246 {
247 ego = GNUNET_new(struct GNUNET_MESSENGER_Ego);
248 GNUNET_CONTAINER_multihashmap_put (store->egos, &hash, ego, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
249 }
250
251 GNUNET_memcpy(&(ego->priv), key, sizeof(*key));
252
253 if (GNUNET_OK != GNUNET_IDENTITY_key_get_public (key, &(ego->pub)))
254 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Updating invalid ego key failed!\n");
255
256 return ego;
257}
258
259static void
260callback_ego_rename (void *cls,
261 const char *emsg)
262{
263 struct GNUNET_MESSENGER_EgoOperation *element = cls;
264 struct GNUNET_MESSENGER_EgoStore *store = element->store;
265
266 GNUNET_assert(element->identifier);
267
268 if (emsg)
269 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s\n", emsg);
270
271 struct GNUNET_HashCode hash;
272 GNUNET_CRYPTO_hash (element->identifier, strlen (element->identifier), &hash);
273
274 struct GNUNET_MESSENGER_Ego *ego = GNUNET_CONTAINER_multihashmap_get (store->egos, &hash);
275
276 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (store->egos, &hash, ego))
277 {
278 GNUNET_CRYPTO_hash ((char*) element->handle, strlen ((char*) element->handle), &hash);
279
280 GNUNET_CONTAINER_multihashmap_put (store->egos, &hash, ego,
281 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
282 }
283 else
284 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Renaming ego failed!\n");
285
286 GNUNET_free (element->handle);
287
288 GNUNET_CONTAINER_DLL_remove (store->op_start, store->op_end, element);
289 GNUNET_free (element->identifier);
290 GNUNET_free (element);
291}
292
293void
294rename_store_ego (struct GNUNET_MESSENGER_EgoStore *store,
295 const char *old_identifier,
296 const char *new_identifier)
297{
298 GNUNET_assert ((store) && (old_identifier) && (new_identifier));
299
300 struct GNUNET_MESSENGER_EgoOperation *element = GNUNET_new (struct GNUNET_MESSENGER_EgoOperation);
301
302 element->store = store;
303 element->handle = GNUNET_strdup (new_identifier);
304
305 element->identifier = GNUNET_strdup (old_identifier);
306
307 element->operation = GNUNET_IDENTITY_rename (store->identity, old_identifier, new_identifier, callback_ego_rename, element);
308
309 GNUNET_CONTAINER_DLL_insert (store->op_start, store->op_end, element);
310}