aboutsummaryrefslogtreecommitdiff
path: root/src/service/messenger/messenger_api_handle.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/service/messenger/messenger_api_handle.c')
-rw-r--r--src/service/messenger/messenger_api_handle.c270
1 files changed, 270 insertions, 0 deletions
diff --git a/src/service/messenger/messenger_api_handle.c b/src/service/messenger/messenger_api_handle.c
new file mode 100644
index 000000000..1cf2bccf0
--- /dev/null
+++ b/src/service/messenger/messenger_api_handle.c
@@ -0,0 +1,270 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2020--2023 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/messenger_api_handle.c
23 * @brief messenger api: client implementation of GNUnet MESSENGER service
24 */
25
26#include "messenger_api_handle.h"
27
28#include "messenger_api_room.h"
29#include "messenger_api_util.h"
30
31struct GNUNET_MESSENGER_Handle*
32create_handle (const struct GNUNET_CONFIGURATION_Handle *cfg,
33 GNUNET_MESSENGER_MessageCallback msg_callback,
34 void *msg_cls)
35{
36 GNUNET_assert (cfg);
37
38 struct GNUNET_MESSENGER_Handle *handle = GNUNET_new (struct
39 GNUNET_MESSENGER_Handle);
40
41 handle->cfg = cfg;
42 handle->mq = NULL;
43
44 handle->msg_callback = msg_callback;
45 handle->msg_cls = msg_cls;
46
47 handle->name = NULL;
48 handle->key = NULL;
49 handle->pubkey = NULL;
50
51 handle->reconnect_time = GNUNET_TIME_relative_get_zero_ ();
52 handle->reconnect_task = NULL;
53
54 handle->rooms = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
55
56 init_contact_store (get_handle_contact_store (handle));
57
58 return handle;
59}
60
61
62static enum GNUNET_GenericReturnValue
63iterate_destroy_room (void *cls,
64 const struct GNUNET_HashCode *key,
65 void *value)
66{
67 struct GNUNET_MESSENGER_Room *room = value;
68
69 destroy_room (room);
70
71 return GNUNET_YES;
72}
73
74
75void
76destroy_handle (struct GNUNET_MESSENGER_Handle *handle)
77{
78 GNUNET_assert (handle);
79
80 clear_contact_store (get_handle_contact_store (handle));
81
82 if (handle->rooms)
83 {
84 GNUNET_CONTAINER_multihashmap_iterate (handle->rooms, iterate_destroy_room,
85 NULL);
86
87 GNUNET_CONTAINER_multihashmap_destroy (handle->rooms);
88 }
89
90 if (handle->reconnect_task)
91 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
92
93 if (handle->mq)
94 GNUNET_MQ_destroy (handle->mq);
95
96 if (handle->name)
97 GNUNET_free (handle->name);
98
99 if (handle->key)
100 GNUNET_free (handle->key);
101
102 if (handle->pubkey)
103 GNUNET_free (handle->pubkey);
104
105 GNUNET_free (handle);
106}
107
108
109void
110set_handle_name (struct GNUNET_MESSENGER_Handle *handle,
111 const char *name)
112{
113 GNUNET_assert (handle);
114
115 if (handle->name)
116 GNUNET_free (handle->name);
117
118 handle->name = name ? GNUNET_strdup (name) : NULL;
119}
120
121
122const char*
123get_handle_name (const struct GNUNET_MESSENGER_Handle *handle)
124{
125 GNUNET_assert (handle);
126
127 return handle->name;
128}
129
130
131void
132set_handle_key (struct GNUNET_MESSENGER_Handle *handle,
133 const struct GNUNET_CRYPTO_PrivateKey *key)
134{
135 GNUNET_assert (handle);
136
137 if (! key)
138 {
139 if (handle->key)
140 GNUNET_free (handle->key);
141
142 if (handle->pubkey)
143 GNUNET_free (handle->pubkey);
144
145 handle->key = NULL;
146 handle->pubkey = NULL;
147 return;
148 }
149
150 if (! handle->key)
151 handle->key = GNUNET_new (struct GNUNET_CRYPTO_PrivateKey);
152
153 if (! handle->pubkey)
154 handle->pubkey = GNUNET_new (struct GNUNET_CRYPTO_PublicKey);
155
156 GNUNET_memcpy (handle->key, key, sizeof(*key));
157 GNUNET_CRYPTO_key_get_public (key, handle->pubkey);
158}
159
160
161const struct GNUNET_CRYPTO_PrivateKey*
162get_handle_key (const struct GNUNET_MESSENGER_Handle *handle)
163{
164 GNUNET_assert (handle);
165
166 if (handle->key)
167 return handle->key;
168
169 return get_anonymous_private_key ();
170}
171
172
173const struct GNUNET_CRYPTO_PublicKey*
174get_handle_pubkey (const struct GNUNET_MESSENGER_Handle *handle)
175{
176 GNUNET_assert (handle);
177
178 if (handle->pubkey)
179 return handle->pubkey;
180
181 return get_anonymous_public_key ();
182}
183
184
185struct GNUNET_MESSENGER_ContactStore*
186get_handle_contact_store (struct GNUNET_MESSENGER_Handle *handle)
187{
188 GNUNET_assert (handle);
189
190 return &(handle->contact_store);
191}
192
193
194struct GNUNET_MESSENGER_Contact*
195get_handle_contact (struct GNUNET_MESSENGER_Handle *handle,
196 const struct GNUNET_HashCode *key)
197{
198 GNUNET_assert ((handle) && (key));
199
200 struct GNUNET_MESSENGER_Room *room = GNUNET_CONTAINER_multihashmap_get (
201 handle->rooms, key);
202
203 if (! room)
204 return NULL;
205
206 const struct GNUNET_ShortHashCode *contact_id = get_room_sender_id (room);
207
208 if (! contact_id)
209 return NULL;
210
211 struct GNUNET_HashCode context;
212 get_context_from_member (key, contact_id, &context);
213
214 return get_store_contact (get_handle_contact_store (handle), &context,
215 get_handle_pubkey (handle));
216}
217
218
219void
220open_handle_room (struct GNUNET_MESSENGER_Handle *handle,
221 const struct GNUNET_HashCode *key)
222{
223 GNUNET_assert ((handle) && (key));
224
225 struct GNUNET_MESSENGER_Room *room = GNUNET_CONTAINER_multihashmap_get (
226 handle->rooms, key);
227
228 if (room)
229 room->opened = GNUNET_YES;
230}
231
232
233void
234entry_handle_room_at (struct GNUNET_MESSENGER_Handle *handle,
235 const struct GNUNET_PeerIdentity *door,
236 const struct GNUNET_HashCode *key)
237{
238 GNUNET_assert ((handle) && (door) && (key));
239
240 struct GNUNET_MESSENGER_Room *room = GNUNET_CONTAINER_multihashmap_get (
241 handle->rooms, key);
242
243 if (room)
244 add_to_list_tunnels (&(room->entries), door, NULL);
245}
246
247
248void
249close_handle_room (struct GNUNET_MESSENGER_Handle *handle,
250 const struct GNUNET_HashCode *key)
251{
252 GNUNET_assert ((handle) && (key));
253
254 struct GNUNET_MESSENGER_Room *room = GNUNET_CONTAINER_multihashmap_get (
255 handle->rooms, key);
256
257 if ((room) && (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (
258 handle->rooms, key, room)))
259 destroy_room (room);
260}
261
262
263struct GNUNET_MESSENGER_Room*
264get_handle_room (struct GNUNET_MESSENGER_Handle *handle,
265 const struct GNUNET_HashCode *key)
266{
267 GNUNET_assert ((handle) && (key));
268
269 return GNUNET_CONTAINER_multihashmap_get (handle->rooms, key);
270}