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.c247
1 files changed, 119 insertions, 128 deletions
diff --git a/src/messenger/gnunet-service-messenger.c b/src/messenger/gnunet-service-messenger.c
index f0bb853d7..0b447f3cf 100644
--- a/src/messenger/gnunet-service-messenger.c
+++ b/src/messenger/gnunet-service-messenger.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--2023 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
@@ -27,9 +27,9 @@
27#include "gnunet-service-messenger.h" 27#include "gnunet-service-messenger.h"
28 28
29#include "gnunet-service-messenger_handle.h" 29#include "gnunet-service-messenger_handle.h"
30#include "gnunet-service-messenger_message_kind.h"
31#include "gnunet-service-messenger_service.h" 30#include "gnunet-service-messenger_service.h"
32#include "messenger_api_message.h" 31#include "messenger_api_message.h"
32#include "messenger_api_message_kind.h"
33 33
34struct GNUNET_MESSENGER_Client 34struct GNUNET_MESSENGER_Client
35{ 35{
@@ -39,36 +39,13 @@ struct GNUNET_MESSENGER_Client
39 39
40struct GNUNET_MESSENGER_Service *messenger; 40struct GNUNET_MESSENGER_Service *messenger;
41 41
42static int
43check_create (void *cls,
44 const struct GNUNET_MESSENGER_CreateMessage *msg)
45{
46 GNUNET_MQ_check_zero_termination (msg);
47 return GNUNET_OK;
48}
49
50static void 42static void
51handle_create (void *cls, 43handle_create (void *cls,
52 const struct GNUNET_MESSENGER_CreateMessage *msg) 44 const struct GNUNET_MESSENGER_CreateMessage *msg)
53{ 45{
54 struct GNUNET_MESSENGER_Client *msg_client = cls; 46 struct GNUNET_MESSENGER_Client *msg_client = cls;
55 47
56 const char *name = ((const char*) msg) + sizeof(*msg); 48 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Handle created\n");
57
58 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Handle created with name: %s\n", name);
59
60 setup_srv_handle_name (msg_client->handle, strlen (name) > 0 ? name : NULL);
61
62 GNUNET_SERVICE_client_continue (msg_client->client);
63}
64
65static void
66handle_update (void *cls,
67 const struct GNUNET_MESSENGER_UpdateMessage *msg)
68{
69 struct GNUNET_MESSENGER_Client *msg_client = cls;
70
71 update_srv_handle (msg_client->handle);
72 49
73 GNUNET_SERVICE_client_continue (msg_client->client); 50 GNUNET_SERVICE_client_continue (msg_client->client);
74} 51}
@@ -79,30 +56,63 @@ handle_destroy (void *cls,
79{ 56{
80 struct GNUNET_MESSENGER_Client *msg_client = cls; 57 struct GNUNET_MESSENGER_Client *msg_client = cls;
81 58
59 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Handle destroyed\n");
60
82 GNUNET_SERVICE_client_drop (msg_client->client); 61 GNUNET_SERVICE_client_drop (msg_client->client);
83} 62}
84 63
85static int 64static int
86check_set_name (void *cls, 65check_room_initial_key (const struct GNUNET_MESSENGER_RoomMessage *msg)
87 const struct GNUNET_MESSENGER_NameMessage *msg)
88{ 66{
89 GNUNET_MQ_check_zero_termination (msg); 67 const uint16_t full_length = ntohs (msg->header.size);
90 return GNUNET_OK; 68
69 if (full_length < sizeof(*msg))
70 return GNUNET_NO;
71
72 const uint16_t msg_length = full_length - sizeof(*msg);
73 const char *msg_buffer = ((const char*) msg) + sizeof(*msg);
74
75 if (0 == msg_length)
76 return GNUNET_OK;
77
78 struct GNUNET_IDENTITY_PublicKey key;
79 size_t key_len;
80
81 if (GNUNET_OK != GNUNET_IDENTITY_read_public_key_from_buffer(msg_buffer, msg_length, &key, &key_len))
82 return GNUNET_NO;
83
84 return key_len == msg_length ? GNUNET_OK : GNUNET_NO;
91} 85}
92 86
93static void 87static void
94handle_set_name (void *cls, 88initialize_handle_via_key (struct GNUNET_MESSENGER_SrvHandle *handle,
95 const struct GNUNET_MESSENGER_NameMessage *msg) 89 const struct GNUNET_MESSENGER_RoomMessage *msg)
96{ 90{
97 struct GNUNET_MESSENGER_Client *msg_client = cls; 91 GNUNET_assert (handle);
98 92
99 const char *name = ((const char*) msg) + sizeof(*msg); 93 const uint16_t full_length = ntohs (msg->header.size);
94 const uint16_t msg_length = full_length - sizeof(*msg);
95 const char *msg_buffer = ((const char*) msg) + sizeof(*msg);
100 96
101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Handles name is now: %s\n", name); 97 if (msg_length > 0)
98 {
99 struct GNUNET_IDENTITY_PublicKey key;
100 size_t key_len;
102 101
103 set_srv_handle_name (msg_client->handle, name); 102 if (GNUNET_OK == GNUNET_IDENTITY_read_public_key_from_buffer(msg_buffer, msg_length, &key, &key_len))
103 set_srv_handle_key(handle, &key);
104 else
105 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Initialization failed while reading invalid key!\n");
106 }
107 else
108 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Initialization is missing key!\n");
109}
104 110
105 GNUNET_SERVICE_client_continue (msg_client->client); 111static int
112check_room_open (void *cls,
113 const struct GNUNET_MESSENGER_RoomMessage *msg)
114{
115 return check_room_initial_key (msg);
106} 116}
107 117
108static void 118static void
@@ -111,6 +121,8 @@ handle_room_open (void *cls,
111{ 121{
112 struct GNUNET_MESSENGER_Client *msg_client = cls; 122 struct GNUNET_MESSENGER_Client *msg_client = cls;
113 123
124 initialize_handle_via_key (msg_client->handle, msg);
125
114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Opening room: %s\n", GNUNET_h2s ( 126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Opening room: %s\n", GNUNET_h2s (
115 &(msg->key))); 127 &(msg->key)));
116 128
@@ -136,12 +148,21 @@ handle_room_open (void *cls,
136 GNUNET_SERVICE_client_continue (msg_client->client); 148 GNUNET_SERVICE_client_continue (msg_client->client);
137} 149}
138 150
151static int
152check_room_entry (void *cls,
153 const struct GNUNET_MESSENGER_RoomMessage *msg)
154{
155 return check_room_initial_key (msg);
156}
157
139static void 158static void
140handle_room_entry (void *cls, 159handle_room_entry (void *cls,
141 const struct GNUNET_MESSENGER_RoomMessage *msg) 160 const struct GNUNET_MESSENGER_RoomMessage *msg)
142{ 161{
143 struct GNUNET_MESSENGER_Client *msg_client = cls; 162 struct GNUNET_MESSENGER_Client *msg_client = cls;
144 163
164 initialize_handle_via_key (msg_client->handle, msg);
165
145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Entering room: %s, %s\n", GNUNET_h2s ( 166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Entering room: %s, %s\n", GNUNET_h2s (
146 &(msg->key)), GNUNET_i2s (&(msg->door))); 167 &(msg->key)), GNUNET_i2s (&(msg->door)));
147 168
@@ -207,40 +228,22 @@ check_send_message (void *cls,
207 if (full_length < sizeof(*msg)) 228 if (full_length < sizeof(*msg))
208 return GNUNET_NO; 229 return GNUNET_NO;
209 230
210 const enum GNUNET_MESSENGER_MessageFlags flags = ( 231 const uint16_t msg_length = full_length - sizeof(*msg);
211 (enum GNUNET_MESSENGER_MessageFlags) (msg->flags) 232 const char *msg_buffer = ((const char*) msg) + sizeof(*msg);
212 );
213
214 const uint16_t length = full_length - sizeof(*msg);
215 const char *buffer = ((const char*) msg) + sizeof(*msg);
216 struct GNUNET_IDENTITY_PublicKey public_key;
217
218
219 size_t key_length = 0;
220
221 if ((flags & GNUNET_MESSENGER_FLAG_PRIVATE))
222 if (GNUNET_SYSERR ==
223 GNUNET_IDENTITY_read_public_key_from_buffer (buffer, length,
224 &public_key,
225 &key_length))
226 return GNUNET_NO;
227
228 const uint16_t msg_length = length - key_length;
229 const char *msg_buffer = buffer + key_length;
230 233
231 struct GNUNET_MESSENGER_Message message; 234 struct GNUNET_MESSENGER_Message message;
232 235
233 if (length < get_message_kind_size (GNUNET_MESSENGER_KIND_UNKNOWN, GNUNET_NO)) 236 if (msg_length < get_message_kind_size (GNUNET_MESSENGER_KIND_UNKNOWN, GNUNET_YES))
234 return GNUNET_NO; 237 return GNUNET_NO;
235 238
236 if (GNUNET_YES != decode_message (&message, msg_length, msg_buffer, GNUNET_NO, 239 if (GNUNET_YES != decode_message (&message, msg_length, msg_buffer, GNUNET_YES,
237 NULL)) 240 NULL))
238 return GNUNET_NO; 241 return GNUNET_NO;
239 242
240 const int allowed = filter_message_sending (&message); 243 const int allowed = filter_message_sending (&message);
241 244
242 cleanup_message (&message); 245 cleanup_message (&message);
243 return GNUNET_YES == allowed? GNUNET_OK : GNUNET_NO; 246 return GNUNET_SYSERR != allowed? GNUNET_OK : GNUNET_NO;
244} 247}
245 248
246static void 249static void
@@ -249,44 +252,17 @@ handle_send_message (void *cls,
249{ 252{
250 struct GNUNET_MESSENGER_Client *msg_client = cls; 253 struct GNUNET_MESSENGER_Client *msg_client = cls;
251 254
252 const enum GNUNET_MESSENGER_MessageFlags flags = (
253 (enum GNUNET_MESSENGER_MessageFlags) (msg->flags)
254 );
255
256 const struct GNUNET_HashCode *key = &(msg->key); 255 const struct GNUNET_HashCode *key = &(msg->key);
257 const char *buffer = ((const char*) msg) + sizeof(*msg); 256 const char *msg_buffer = ((const char*) msg) + sizeof(*msg);
258 257 const uint16_t msg_length = ntohs (msg->header.size) - sizeof(*msg);
259 const uint16_t length = ntohs (msg->header.size) - sizeof(*msg);
260 size_t key_length = 0;
261
262 struct GNUNET_IDENTITY_PublicKey public_key;
263
264 if (flags & GNUNET_MESSENGER_FLAG_PRIVATE)
265 {
266 GNUNET_assert (GNUNET_SYSERR !=
267 GNUNET_IDENTITY_read_public_key_from_buffer (buffer,
268 length,
269 &public_key,
270 &key_length));
271 }
272 const uint16_t msg_length = length - key_length;
273 const char*msg_buffer = buffer + key_length;
274 258
275 struct GNUNET_MESSENGER_Message message; 259 struct GNUNET_MESSENGER_Message message;
276 decode_message (&message, msg_length, msg_buffer, GNUNET_NO, NULL); 260 decode_message (&message, msg_length, msg_buffer, GNUNET_YES, NULL);
277
278 if ((flags & GNUNET_MESSENGER_FLAG_PRIVATE) &&
279 (GNUNET_YES != encrypt_message (&message, &public_key)))
280 {
281 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
282 "Encrypting message failed: Message got dropped!\n");
283 261
284 goto end_handling; 262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message: %s to %s (by %s)\n",
285 } 263 GNUNET_MESSENGER_name_of_kind (message.header.kind),
286 264 GNUNET_h2s (key),
287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message: %s to %s\n", 265 GNUNET_sh2s (&(message.header.sender_id)));
288 GNUNET_MESSENGER_name_of_kind (message.header.kind), GNUNET_h2s (
289 key));
290 266
291 if (GNUNET_YES != send_srv_handle_message (msg_client->handle, key, &message)) 267 if (GNUNET_YES != send_srv_handle_message (msg_client->handle, key, &message))
292 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Sending message failed: %s to %s\n", 268 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Sending message failed: %s to %s\n",
@@ -307,30 +283,49 @@ callback_found_message (void *cls,
307{ 283{
308 struct GNUNET_MESSENGER_Client *msg_client = cls; 284 struct GNUNET_MESSENGER_Client *msg_client = cls;
309 285
310 if (! message) 286 if (!message)
311 { 287 {
312 send_srv_room_message (room, msg_client->handle, create_message_request ( 288 struct GNUNET_MESSENGER_GetMessage *response;
313 hash)); 289 struct GNUNET_MQ_Envelope *env;
290
291 env = GNUNET_MQ_msg (response, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_GET_MESSAGE);
292 GNUNET_memcpy(&(response->key), &(room->key), sizeof(room->key));
293 GNUNET_memcpy(&(response->hash), hash, sizeof(*hash));
294 GNUNET_MQ_send (msg_client->handle->mq, env);
314 return; 295 return;
315 } 296 }
316 297
317 struct GNUNET_MESSENGER_MemberStore *store = get_srv_room_member_store (room); 298 struct GNUNET_MESSENGER_SenderSession session;
318
319 struct GNUNET_MESSENGER_Member *member = get_store_member_of (store, message);
320 299
321 if (! member) 300 if (GNUNET_YES == is_peer_message (message))
322 { 301 {
323 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Sender of message (%s) unknown!\n", 302 struct GNUNET_MESSENGER_PeerStore *store = get_srv_room_peer_store (room);
324 GNUNET_h2s (hash)); 303
325 return; 304 session.peer = get_store_peer_of (store, message, hash);
305
306 if (!session.peer)
307 return;
326 } 308 }
309 else
310 {
311 struct GNUNET_MESSENGER_MemberStore *store = get_srv_room_member_store (room);
312 struct GNUNET_MESSENGER_Member *member = get_store_member_of (store, message);
313
314 if (!member)
315 {
316 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Sender of message (%s) unknown!\n",
317 GNUNET_h2s (hash));
318 return;
319 }
327 320
328 struct GNUNET_MESSENGER_MemberSession *session = get_member_session_of ( 321 session.member = get_member_session_of (member, message, hash);
329 member, message, hash);
330 322
331 if (session) 323 if (!session.member)
332 notify_srv_handle_message (msg_client->handle, room, session, message, 324 return;
333 hash); 325 }
326
327 notify_srv_handle_message (msg_client->handle, room, &session, message,
328 hash);
334} 329}
335 330
336static void 331static void
@@ -369,13 +364,17 @@ handle_get_message (void *cls,
369 goto end_handling; 364 goto end_handling;
370 } 365 }
371 366
367 const struct GNUNET_IDENTITY_PublicKey* pubkey = get_srv_handle_key(msg_client->handle);
368
369 if (! pubkey)
370 {
371 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
372 "Handle needs to have a public key to request a message!\n");
373 goto end_handling;
374 }
375
372 struct GNUNET_MESSENGER_MemberSession *session = get_member_session (member, 376 struct GNUNET_MESSENGER_MemberSession *session = get_member_session (member,
373 &( 377 pubkey);
374 get_srv_handle_ego (
375 msg_client
376 ->
377 handle)
378 ->pub));
379 378
380 if (! session) 379 if (! session)
381 { 380 {
@@ -445,24 +444,16 @@ GNUNET_SERVICE_MAIN (
445 &callback_client_connect, 444 &callback_client_connect,
446 &callback_client_disconnect, 445 &callback_client_disconnect,
447 NULL, 446 NULL,
448 GNUNET_MQ_hd_var_size (create, 447 GNUNET_MQ_hd_fixed_size (create,
449 GNUNET_MESSAGE_TYPE_MESSENGER_CONNECTION_CREATE, struct 448 GNUNET_MESSAGE_TYPE_MESSENGER_CONNECTION_CREATE, struct
450 GNUNET_MESSENGER_CreateMessage, NULL), 449 GNUNET_MESSENGER_CreateMessage, NULL),
451 GNUNET_MQ_hd_fixed_size (update,
452 GNUNET_MESSAGE_TYPE_MESSENGER_CONNECTION_UPDATE,
453 struct
454 GNUNET_MESSENGER_UpdateMessage, NULL),
455 GNUNET_MQ_hd_fixed_size (destroy, 450 GNUNET_MQ_hd_fixed_size (destroy,
456 GNUNET_MESSAGE_TYPE_MESSENGER_CONNECTION_DESTROY, 451 GNUNET_MESSAGE_TYPE_MESSENGER_CONNECTION_DESTROY,
457 struct 452 struct
458 GNUNET_MESSENGER_DestroyMessage, NULL), 453 GNUNET_MESSENGER_DestroyMessage, NULL),
459 GNUNET_MQ_hd_var_size (set_name, 454 GNUNET_MQ_hd_var_size (room_open, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_OPEN,
460 GNUNET_MESSAGE_TYPE_MESSENGER_CONNECTION_SET_NAME,
461 struct
462 GNUNET_MESSENGER_NameMessage, NULL),
463 GNUNET_MQ_hd_fixed_size (room_open, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_OPEN,
464 struct GNUNET_MESSENGER_RoomMessage, NULL), 455 struct GNUNET_MESSENGER_RoomMessage, NULL),
465 GNUNET_MQ_hd_fixed_size (room_entry, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_ENTRY, 456 GNUNET_MQ_hd_var_size (room_entry, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_ENTRY,
466 struct GNUNET_MESSENGER_RoomMessage, NULL), 457 struct GNUNET_MESSENGER_RoomMessage, NULL),
467 GNUNET_MQ_hd_fixed_size (room_close, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_CLOSE, 458 GNUNET_MQ_hd_fixed_size (room_close, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_CLOSE,
468 struct GNUNET_MESSENGER_RoomMessage, NULL), 459 struct GNUNET_MESSENGER_RoomMessage, NULL),