aboutsummaryrefslogtreecommitdiff
path: root/src/messenger/gnunet-service-messenger.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/messenger/gnunet-service-messenger.c')
-rw-r--r--src/messenger/gnunet-service-messenger.c496
1 files changed, 0 insertions, 496 deletions
diff --git a/src/messenger/gnunet-service-messenger.c b/src/messenger/gnunet-service-messenger.c
deleted file mode 100644
index bebadb943..000000000
--- a/src/messenger/gnunet-service-messenger.c
+++ /dev/null
@@ -1,496 +0,0 @@
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/gnunet-service-messenger.c
23 * @brief GNUnet MESSENGER service
24 */
25
26#include "platform.h"
27#include "gnunet-service-messenger.h"
28
29#include "gnunet-service-messenger_handle.h"
30#include "gnunet-service-messenger_service.h"
31#include "messenger_api_message.h"
32#include "messenger_api_message_kind.h"
33
34struct GNUNET_MESSENGER_Client
35{
36 struct GNUNET_SERVICE_Client *client;
37 struct GNUNET_MESSENGER_SrvHandle *handle;
38};
39
40struct GNUNET_MESSENGER_Service *messenger;
41
42static void
43handle_create (void *cls,
44 const struct GNUNET_MESSENGER_CreateMessage *msg)
45{
46 struct GNUNET_MESSENGER_Client *msg_client = cls;
47
48 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Handle created\n");
49
50 GNUNET_SERVICE_client_continue (msg_client->client);
51}
52
53
54static void
55handle_destroy (void *cls,
56 const struct GNUNET_MESSENGER_DestroyMessage *msg)
57{
58 struct GNUNET_MESSENGER_Client *msg_client = cls;
59
60 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Handle destroyed\n");
61
62 GNUNET_SERVICE_client_drop (msg_client->client);
63}
64
65
66static int
67check_room_initial_key (const struct GNUNET_MESSENGER_RoomMessage *msg)
68{
69 const uint16_t full_length = ntohs (msg->header.size);
70
71 if (full_length < sizeof(*msg))
72 return GNUNET_NO;
73
74 const uint16_t msg_length = full_length - sizeof(*msg);
75 const char *msg_buffer = ((const char*) msg) + sizeof(*msg);
76
77 if (0 == msg_length)
78 return GNUNET_OK;
79
80 struct GNUNET_IDENTITY_PublicKey key;
81 size_t key_len;
82
83 if (GNUNET_OK != GNUNET_IDENTITY_read_public_key_from_buffer (msg_buffer,
84 msg_length,
85 &key, &key_len))
86 return GNUNET_NO;
87
88 return key_len == msg_length ? GNUNET_OK : GNUNET_NO;
89}
90
91
92static void
93initialize_handle_via_key (struct GNUNET_MESSENGER_SrvHandle *handle,
94 const struct GNUNET_MESSENGER_RoomMessage *msg)
95{
96 GNUNET_assert (handle);
97
98 const uint16_t full_length = ntohs (msg->header.size);
99 const uint16_t msg_length = full_length - sizeof(*msg);
100 const char *msg_buffer = ((const char*) msg) + sizeof(*msg);
101
102 if (msg_length > 0)
103 {
104 struct GNUNET_IDENTITY_PublicKey key;
105 size_t key_len;
106
107 if (GNUNET_OK == GNUNET_IDENTITY_read_public_key_from_buffer (msg_buffer,
108 msg_length,
109 &key,
110 &key_len))
111 set_srv_handle_key (handle, &key);
112 else
113 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
114 "Initialization failed while reading invalid key!\n");
115 }
116 else
117 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Initialization is missing key!\n");
118}
119
120
121static int
122check_room_open (void *cls,
123 const struct GNUNET_MESSENGER_RoomMessage *msg)
124{
125 return check_room_initial_key (msg);
126}
127
128
129static void
130handle_room_open (void *cls,
131 const struct GNUNET_MESSENGER_RoomMessage *msg)
132{
133 struct GNUNET_MESSENGER_Client *msg_client = cls;
134
135 initialize_handle_via_key (msg_client->handle, msg);
136
137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Opening room: %s\n", GNUNET_h2s (
138 &(msg->key)));
139
140 if (GNUNET_YES == open_srv_handle_room (msg_client->handle, &(msg->key)))
141 {
142 const struct GNUNET_ShortHashCode *member_id = get_srv_handle_member_id (
143 msg_client->handle, &(msg->key));
144
145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Opening room with member id: %s\n",
146 GNUNET_sh2s (member_id));
147
148 struct GNUNET_MESSENGER_RoomMessage *response;
149 struct GNUNET_MQ_Envelope *env;
150
151 env = GNUNET_MQ_msg (response, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_OPEN);
152 GNUNET_memcpy (&(response->key), &(msg->key), sizeof(msg->key));
153 GNUNET_MQ_send (msg_client->handle->mq, env);
154 }
155 else
156 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Opening room failed: %s\n",
157 GNUNET_h2s (&(msg->key)));
158
159 GNUNET_SERVICE_client_continue (msg_client->client);
160}
161
162
163static int
164check_room_entry (void *cls,
165 const struct GNUNET_MESSENGER_RoomMessage *msg)
166{
167 return check_room_initial_key (msg);
168}
169
170
171static void
172handle_room_entry (void *cls,
173 const struct GNUNET_MESSENGER_RoomMessage *msg)
174{
175 struct GNUNET_MESSENGER_Client *msg_client = cls;
176
177 initialize_handle_via_key (msg_client->handle, msg);
178
179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Entering room: %s, %s\n", GNUNET_h2s (
180 &(msg->key)), GNUNET_i2s (&(msg->door)));
181
182 if (GNUNET_YES == entry_srv_handle_room (msg_client->handle, &(msg->door),
183 &(msg->key)))
184 {
185 const struct GNUNET_ShortHashCode *member_id = get_srv_handle_member_id (
186 msg_client->handle, &(msg->key));
187
188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Entering room with member id: %s\n",
189 GNUNET_sh2s (member_id));
190
191 struct GNUNET_MESSENGER_RoomMessage *response;
192 struct GNUNET_MQ_Envelope *env;
193
194 env = GNUNET_MQ_msg (response, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_ENTRY);
195 GNUNET_memcpy (&(response->door), &(msg->door), sizeof(msg->door));
196 GNUNET_memcpy (&(response->key), &(msg->key), sizeof(msg->key));
197 GNUNET_MQ_send (msg_client->handle->mq, env);
198 }
199 else
200 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Entrance into room failed: %s, %s\n",
201 GNUNET_h2s (&(msg->key)),
202 GNUNET_i2s (&(msg->door)));
203
204 GNUNET_SERVICE_client_continue (msg_client->client);
205}
206
207
208static void
209handle_room_close (void *cls,
210 const struct GNUNET_MESSENGER_RoomMessage *msg)
211{
212 struct GNUNET_MESSENGER_Client *msg_client = cls;
213
214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Closing room: %s\n", GNUNET_h2s (
215 &(msg->key)));
216
217 if (GNUNET_YES == close_srv_handle_room (msg_client->handle, &(msg->key)))
218 {
219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Closing room succeeded: %s\n",
220 GNUNET_h2s (&(msg->key)));
221
222 struct GNUNET_MESSENGER_RoomMessage *response;
223 struct GNUNET_MQ_Envelope *env;
224
225 env = GNUNET_MQ_msg (response, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_CLOSE);
226 GNUNET_memcpy (&(response->key), &(msg->key), sizeof(msg->key));
227 GNUNET_MQ_send (msg_client->handle->mq, env);
228 }
229 else
230 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Closing room failed: %s\n",
231 GNUNET_h2s (&(msg->key)));
232
233 GNUNET_SERVICE_client_continue (msg_client->client);
234}
235
236
237static int
238check_send_message (void *cls,
239 const struct GNUNET_MESSENGER_SendMessage *msg)
240{
241 const uint16_t full_length = ntohs (msg->header.size);
242
243 if (full_length < sizeof(*msg))
244 return GNUNET_NO;
245
246 const uint16_t msg_length = full_length - sizeof(*msg);
247 const char *msg_buffer = ((const char*) msg) + sizeof(*msg);
248
249 struct GNUNET_MESSENGER_Message message;
250
251 if (msg_length < get_message_kind_size (GNUNET_MESSENGER_KIND_UNKNOWN,
252 GNUNET_YES))
253 return GNUNET_NO;
254
255 if (GNUNET_YES != decode_message (&message, msg_length, msg_buffer,
256 GNUNET_YES,
257 NULL))
258 return GNUNET_NO;
259
260 const int allowed = filter_message_sending (&message);
261
262 cleanup_message (&message);
263 return GNUNET_SYSERR != allowed? GNUNET_OK : GNUNET_NO;
264}
265
266
267static void
268handle_send_message (void *cls,
269 const struct GNUNET_MESSENGER_SendMessage *msg)
270{
271 struct GNUNET_MESSENGER_Client *msg_client = cls;
272
273 const struct GNUNET_HashCode *key = &(msg->key);
274 const char *msg_buffer = ((const char*) msg) + sizeof(*msg);
275 const uint16_t msg_length = ntohs (msg->header.size) - sizeof(*msg);
276
277 struct GNUNET_MESSENGER_Message message;
278 decode_message (&message, msg_length, msg_buffer, GNUNET_YES, NULL);
279
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message: %s to %s (by %s)\n",
281 GNUNET_MESSENGER_name_of_kind (message.header.kind),
282 GNUNET_h2s (key),
283 GNUNET_sh2s (&(message.header.sender_id)));
284
285 if (GNUNET_YES != send_srv_handle_message (msg_client->handle, key, &message))
286 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Sending message failed: %s to %s\n",
287 GNUNET_MESSENGER_name_of_kind (message.header.kind),
288 GNUNET_h2s (key));
289
290end_handling:
291 cleanup_message (&message);
292
293 GNUNET_SERVICE_client_continue (msg_client->client);
294}
295
296
297static void
298callback_found_message (void *cls,
299 struct GNUNET_MESSENGER_SrvRoom *room,
300 const struct GNUNET_MESSENGER_Message *message,
301 const struct GNUNET_HashCode *hash)
302{
303 struct GNUNET_MESSENGER_Client *msg_client = cls;
304
305 if (! message)
306 {
307 struct GNUNET_MESSENGER_GetMessage *response;
308 struct GNUNET_MQ_Envelope *env;
309
310 env = GNUNET_MQ_msg (response,
311 GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_GET_MESSAGE);
312 GNUNET_memcpy (&(response->key), &(room->key), sizeof(room->key));
313 GNUNET_memcpy (&(response->hash), hash, sizeof(*hash));
314 GNUNET_MQ_send (msg_client->handle->mq, env);
315 return;
316 }
317
318 struct GNUNET_MESSENGER_SenderSession session;
319
320 if (GNUNET_YES == is_peer_message (message))
321 {
322 struct GNUNET_MESSENGER_PeerStore *store = get_srv_room_peer_store (room);
323
324 session.peer = get_store_peer_of (store, message, hash);
325
326 if (! session.peer)
327 return;
328 }
329 else
330 {
331 struct GNUNET_MESSENGER_MemberStore *store = get_srv_room_member_store (
332 room);
333 struct GNUNET_MESSENGER_Member *member = get_store_member_of (store,
334 message);
335
336 if (! member)
337 {
338 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Sender of message (%s) unknown!\n",
339 GNUNET_h2s (hash));
340 return;
341 }
342
343 session.member = get_member_session_of (member, message, hash);
344
345 if (! session.member)
346 return;
347 }
348
349 notify_srv_handle_message (msg_client->handle, room, &session, message,
350 hash);
351}
352
353
354static void
355handle_get_message (void *cls,
356 const struct GNUNET_MESSENGER_GetMessage *msg)
357{
358 struct GNUNET_MESSENGER_Client *msg_client = cls;
359
360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Requesting message from room: %s\n",
361 GNUNET_h2s (&(msg->key)));
362
363 struct GNUNET_MESSENGER_SrvRoom *room = get_service_room (messenger,
364 &(msg->key));
365
366 if (! room)
367 {
368 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Room not found: %s\n", GNUNET_h2s (
369 &(msg->key)));
370 goto end_handling;
371 }
372
373 struct GNUNET_MESSENGER_MemberStore *member_store =
374 get_srv_room_member_store (room);
375
376 struct GNUNET_MESSENGER_Member *member = get_store_member (member_store,
377 get_srv_handle_member_id (
378 msg_client->
379 handle,
380 &(msg->key)
381 ));
382
383 if (! member)
384 {
385 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
386 "Member not valid to request a message!\n");
387 goto end_handling;
388 }
389
390 const struct GNUNET_IDENTITY_PublicKey *pubkey = get_srv_handle_key (
391 msg_client->handle);
392
393 if (! pubkey)
394 {
395 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
396 "Handle needs to have a public key to request a message!\n");
397 goto end_handling;
398 }
399
400 struct GNUNET_MESSENGER_MemberSession *session = get_member_session (member,
401 pubkey);
402
403 if (! session)
404 {
405 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
406 "Session not valid to request a message!\n");
407 goto end_handling;
408 }
409
410 request_srv_room_message (room, &(msg->hash), session, callback_found_message,
411 msg_client);
412
413end_handling:
414 GNUNET_SERVICE_client_continue (msg_client->client);
415}
416
417
418static void*
419callback_client_connect (void *cls,
420 struct GNUNET_SERVICE_Client *client,
421 struct GNUNET_MQ_Handle *mq)
422{
423 struct GNUNET_MESSENGER_Client *msg_client = GNUNET_new (struct
424 GNUNET_MESSENGER_Client);
425
426 msg_client->client = client;
427 msg_client->handle = add_service_handle (messenger, mq);
428
429 return msg_client;
430}
431
432
433static void
434callback_client_disconnect (void *cls,
435 struct GNUNET_SERVICE_Client *client,
436 void *internal_cls)
437{
438 struct GNUNET_MESSENGER_Client *msg_client = internal_cls;
439
440 remove_service_handle (messenger, msg_client->handle);
441
442 GNUNET_free (msg_client);
443}
444
445
446/**
447 * Setup MESSENGER internals.
448 *
449 * @param[in/out] cls closure
450 * @param[in] config configuration to use
451 * @param[in/out] service the initialized service
452 */
453static void
454run (void *cls,
455 const struct GNUNET_CONFIGURATION_Handle *config,
456 struct GNUNET_SERVICE_Handle *service)
457{
458 messenger = create_service (config, service);
459
460 if (! messenger)
461 GNUNET_SCHEDULER_shutdown ();
462}
463
464
465/**
466 * Define "main" method using service macro.
467 */
468GNUNET_SERVICE_MAIN (
469 GNUNET_MESSENGER_SERVICE_NAME,
470 GNUNET_SERVICE_OPTION_NONE,
471 &run,
472 &callback_client_connect,
473 &callback_client_disconnect,
474 NULL,
475 GNUNET_MQ_hd_fixed_size (create,
476 GNUNET_MESSAGE_TYPE_MESSENGER_CONNECTION_CREATE,
477 struct
478 GNUNET_MESSENGER_CreateMessage, NULL),
479 GNUNET_MQ_hd_fixed_size (destroy,
480 GNUNET_MESSAGE_TYPE_MESSENGER_CONNECTION_DESTROY,
481 struct
482 GNUNET_MESSENGER_DestroyMessage, NULL),
483 GNUNET_MQ_hd_var_size (room_open, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_OPEN,
484 struct GNUNET_MESSENGER_RoomMessage, NULL),
485 GNUNET_MQ_hd_var_size (room_entry, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_ENTRY,
486 struct GNUNET_MESSENGER_RoomMessage, NULL),
487 GNUNET_MQ_hd_fixed_size (room_close, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_CLOSE,
488 struct GNUNET_MESSENGER_RoomMessage, NULL),
489 GNUNET_MQ_hd_var_size (send_message,
490 GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_SEND_MESSAGE, struct
491 GNUNET_MESSENGER_SendMessage, NULL),
492 GNUNET_MQ_hd_fixed_size (get_message,
493 GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_GET_MESSAGE,
494 struct
495 GNUNET_MESSENGER_GetMessage, NULL),
496 GNUNET_MQ_handler_end ());