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.c420
1 files changed, 0 insertions, 420 deletions
diff --git a/src/messenger/gnunet-service-messenger.c b/src/messenger/gnunet-service-messenger.c
deleted file mode 100644
index 546f4c0d2..000000000
--- a/src/messenger/gnunet-service-messenger.c
+++ /dev/null
@@ -1,420 +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.c
23 * @brief GNUnet MESSENGER service
24 */
25
26#include "gnunet-service-messenger.h"
27
28#include "gnunet-service-messenger_handle.h"
29#include "gnunet-service-messenger_message_kind.h"
30#include "gnunet-service-messenger_service.h"
31#include "messenger_api_message.h"
32
33struct GNUNET_MESSENGER_Client
34{
35 struct GNUNET_SERVICE_Client *client;
36 struct GNUNET_MESSENGER_SrvHandle *handle;
37};
38
39struct GNUNET_MESSENGER_Service *messenger;
40
41static int
42check_create (void *cls,
43 const struct GNUNET_MESSENGER_CreateMessage *msg)
44{
45 GNUNET_MQ_check_zero_termination(msg);
46 return GNUNET_OK;
47}
48
49static void
50handle_create (void *cls,
51 const struct GNUNET_MESSENGER_CreateMessage *msg)
52{
53 struct GNUNET_MESSENGER_Client *msg_client = cls;
54
55 const char *name = ((const char*) msg) + sizeof(*msg);
56
57 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Handle created with name: %s\n", name);
58
59 setup_handle_name (msg_client->handle, strlen (name) > 0 ? name : NULL);
60
61 GNUNET_SERVICE_client_continue (msg_client->client);
62}
63
64static void
65handle_update (void *cls,
66 const struct GNUNET_MESSENGER_UpdateMessage *msg)
67{
68 struct GNUNET_MESSENGER_Client *msg_client = cls;
69
70 update_handle (msg_client->handle);
71
72 GNUNET_SERVICE_client_continue (msg_client->client);
73}
74
75static void
76handle_destroy (void *cls,
77 const struct GNUNET_MESSENGER_DestroyMessage *msg)
78{
79 struct GNUNET_MESSENGER_Client *msg_client = cls;
80
81 GNUNET_SERVICE_client_drop (msg_client->client);
82}
83
84static int
85check_set_name (void *cls,
86 const struct GNUNET_MESSENGER_NameMessage *msg)
87{
88 GNUNET_MQ_check_zero_termination(msg);
89 return GNUNET_OK;
90}
91
92static void
93handle_set_name (void *cls,
94 const struct GNUNET_MESSENGER_NameMessage *msg)
95{
96 struct GNUNET_MESSENGER_Client *msg_client = cls;
97
98 const char *name = ((const char*) msg) + sizeof(*msg);
99
100 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Handles name is now: %s\n", name);
101
102 set_handle_name (msg_client->handle, name);
103
104 GNUNET_SERVICE_client_continue (msg_client->client);
105}
106
107static void
108handle_room_open (void *cls,
109 const struct GNUNET_MESSENGER_RoomMessage *msg)
110{
111 struct GNUNET_MESSENGER_Client *msg_client = cls;
112
113 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Opening room: %s\n", GNUNET_h2s (&(msg->key)));
114
115 if (GNUNET_YES == open_handle_room (msg_client->handle, &(msg->key)))
116 {
117 const struct GNUNET_ShortHashCode *member_id = get_handle_member_id (msg_client->handle, &(msg->key));
118
119 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Opening room with member id: %s\n", GNUNET_sh2s (member_id));
120
121 struct GNUNET_MESSENGER_RoomMessage *response;
122 struct GNUNET_MQ_Envelope *env;
123
124 env = GNUNET_MQ_msg(response, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_OPEN);
125 GNUNET_memcpy(&(response->key), &(msg->key), sizeof(msg->key));
126 GNUNET_MQ_send (msg_client->handle->mq, env);
127 }
128 else
129 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Opening room failed: %s\n", GNUNET_h2s (&(msg->key)));
130
131 GNUNET_SERVICE_client_continue (msg_client->client);
132}
133
134static void
135handle_room_entry (void *cls,
136 const struct GNUNET_MESSENGER_RoomMessage *msg)
137{
138 struct GNUNET_MESSENGER_Client *msg_client = cls;
139
140 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Entering room: %s, %s\n", GNUNET_h2s (&(msg->key)), GNUNET_i2s (&(msg->door)));
141
142 if (GNUNET_YES == entry_handle_room (msg_client->handle, &(msg->door), &(msg->key)))
143 {
144 const struct GNUNET_ShortHashCode *member_id = get_handle_member_id (msg_client->handle, &(msg->key));
145
146 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Entering room with member id: %s\n", 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_ENTRY);
152 GNUNET_memcpy(&(response->door), &(msg->door), sizeof(msg->door));
153 GNUNET_memcpy(&(response->key), &(msg->key), sizeof(msg->key));
154 GNUNET_MQ_send (msg_client->handle->mq, env);
155 }
156 else
157 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Entrance into room failed: %s, %s\n", GNUNET_h2s (&(msg->key)),
158 GNUNET_i2s (&(msg->door)));
159
160 GNUNET_SERVICE_client_continue (msg_client->client);
161}
162
163static void
164handle_room_close (void *cls,
165 const struct GNUNET_MESSENGER_RoomMessage *msg)
166{
167 struct GNUNET_MESSENGER_Client *msg_client = cls;
168
169 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Closing room: %s\n", GNUNET_h2s (&(msg->key)));
170
171 if (GNUNET_YES == close_handle_room (msg_client->handle, &(msg->key)))
172 {
173 const struct GNUNET_ShortHashCode *member_id = get_handle_member_id (msg_client->handle, &(msg->key));
174
175 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Closing room with member id: %s\n", GNUNET_sh2s (member_id));
176
177 struct GNUNET_MESSENGER_RoomMessage *response;
178 struct GNUNET_MQ_Envelope *env;
179
180 env = GNUNET_MQ_msg(response, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_CLOSE);
181 GNUNET_memcpy(&(response->key), &(msg->key), sizeof(msg->key));
182 GNUNET_MQ_send (msg_client->handle->mq, env);
183 }
184 else
185 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Closing room failed: %s\n", GNUNET_h2s (&(msg->key)));
186
187 GNUNET_SERVICE_client_continue (msg_client->client);
188}
189
190static int
191check_send_message (void *cls,
192 const struct GNUNET_MESSENGER_SendMessage *msg)
193{
194 const uint16_t full_length = ntohs (msg->header.size);
195
196 if (full_length < sizeof(*msg))
197 return GNUNET_NO;
198
199 const enum GNUNET_MESSENGER_MessageFlags flags = (
200 (enum GNUNET_MESSENGER_MessageFlags) (msg->flags)
201 );
202
203 const uint16_t length = full_length - sizeof(*msg);
204 const char *buffer = ((const char*) msg) + sizeof(*msg);
205
206 ssize_t key_length = 0;
207
208 if (!(flags & GNUNET_MESSENGER_FLAG_PRIVATE))
209 goto check_for_message;
210
211 struct GNUNET_IDENTITY_PublicKey public_key;
212
213 key_length = GNUNET_IDENTITY_read_key_from_buffer(&public_key, buffer, length);
214
215check_for_message:
216 if (key_length < 0)
217 return GNUNET_NO;
218
219 const uint16_t msg_length = length - key_length;
220 const char* msg_buffer = buffer + key_length;
221
222 struct GNUNET_MESSENGER_Message message;
223
224 if (length < get_message_kind_size(GNUNET_MESSENGER_KIND_UNKNOWN, GNUNET_NO))
225 return GNUNET_NO;
226
227 if (GNUNET_YES != decode_message (&message, msg_length, msg_buffer, GNUNET_NO, NULL))
228 return GNUNET_NO;
229
230 const int allowed = filter_message_sending(&message);
231
232 cleanup_message(&message);
233 return GNUNET_YES == allowed? GNUNET_OK : GNUNET_NO;
234}
235
236static void
237handle_send_message (void *cls,
238 const struct GNUNET_MESSENGER_SendMessage *msg)
239{
240 struct GNUNET_MESSENGER_Client *msg_client = cls;
241
242 const enum GNUNET_MESSENGER_MessageFlags flags = (
243 (enum GNUNET_MESSENGER_MessageFlags) (msg->flags)
244 );
245
246 const struct GNUNET_HashCode *key = &(msg->key);
247 const char *buffer = ((const char*) msg) + sizeof(*msg);
248
249 const uint16_t length = ntohs (msg->header.size) - sizeof(*msg);
250 ssize_t key_length = 0;
251
252 struct GNUNET_IDENTITY_PublicKey public_key;
253
254 if (flags & GNUNET_MESSENGER_FLAG_PRIVATE)
255 key_length = GNUNET_IDENTITY_read_key_from_buffer(
256 &public_key, buffer, length
257 );
258
259 const uint16_t msg_length = length - key_length;
260 const char* msg_buffer = buffer + key_length;
261
262 struct GNUNET_MESSENGER_Message message;
263 decode_message (&message, msg_length, msg_buffer, GNUNET_NO, NULL);
264
265 if ((flags & GNUNET_MESSENGER_FLAG_PRIVATE) &&
266 (GNUNET_YES != encrypt_message(&message, &public_key)))
267 {
268 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Encrypting message failed: Message got dropped!\n");
269
270 goto end_handling;
271 }
272
273 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending message: %s to %s\n",
274 GNUNET_MESSENGER_name_of_kind (message.header.kind), GNUNET_h2s (key));
275
276 if (GNUNET_YES != send_handle_message (msg_client->handle, key, &message))
277 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sending message failed: %s to %s\n",
278 GNUNET_MESSENGER_name_of_kind (message.header.kind), GNUNET_h2s (key));
279
280end_handling:
281 cleanup_message(&message);
282
283 GNUNET_SERVICE_client_continue (msg_client->client);
284}
285
286static void
287callback_found_message (void *cls,
288 struct GNUNET_MESSENGER_SrvRoom *room,
289 const struct GNUNET_MESSENGER_Message *message,
290 const struct GNUNET_HashCode *hash)
291{
292 struct GNUNET_MESSENGER_Client *msg_client = cls;
293
294 if (!message)
295 {
296 send_room_message(room, msg_client->handle, create_message_request(hash));
297 return;
298 }
299
300 struct GNUNET_MESSENGER_MemberStore *store = get_room_member_store(room);
301
302 struct GNUNET_MESSENGER_Member *member = get_store_member_of(store, message);
303
304 if (!member)
305 {
306 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sender of message (%s) unknown!\n", GNUNET_h2s (hash));
307 return;
308 }
309
310 struct GNUNET_MESSENGER_MemberSession *session = get_member_session_of(member, message, hash);
311
312 if (session)
313 notify_handle_message (msg_client->handle, room, session, message, hash);
314}
315
316static void
317handle_get_message (void *cls,
318 const struct GNUNET_MESSENGER_GetMessage *msg)
319{
320 struct GNUNET_MESSENGER_Client *msg_client = cls;
321
322 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Requesting message from room: %s\n", GNUNET_h2s (&(msg->key)));
323
324 struct GNUNET_MESSENGER_SrvRoom *room = get_service_room (messenger, &(msg->key));
325
326 if (!room)
327 {
328 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Room not found: %s\n", GNUNET_h2s (&(msg->key)));
329 goto end_handling;
330 }
331
332 struct GNUNET_MESSENGER_MemberStore *member_store = get_room_member_store(room);
333
334 struct GNUNET_MESSENGER_Member *member = get_store_member(member_store, get_handle_member_id(
335 msg_client->handle, &(msg->key)
336 ));
337
338 if (!member)
339 {
340 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Member not valid to request a message!\n");
341 goto end_handling;
342 }
343
344 struct GNUNET_MESSENGER_MemberSession *session = get_member_session(member, &(get_handle_ego(msg_client->handle)->pub));
345
346 if (!session)
347 {
348 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Session not valid to request a message!\n");
349 goto end_handling;
350 }
351
352 request_room_message (room, &(msg->hash), session, callback_found_message, msg_client);
353
354end_handling:
355 GNUNET_SERVICE_client_continue (msg_client->client);
356}
357
358static void*
359callback_client_connect (void *cls,
360 struct GNUNET_SERVICE_Client *client,
361 struct GNUNET_MQ_Handle *mq)
362{
363 struct GNUNET_MESSENGER_Client *msg_client = GNUNET_new(struct GNUNET_MESSENGER_Client);
364
365 msg_client->client = client;
366 msg_client->handle = add_service_handle (messenger, mq);
367
368 return msg_client;
369}
370
371static void
372callback_client_disconnect (void *cls,
373 struct GNUNET_SERVICE_Client *client,
374 void *internal_cls)
375{
376 struct GNUNET_MESSENGER_Client *msg_client = internal_cls;
377
378 remove_service_handle (messenger, msg_client->handle);
379
380 GNUNET_free(msg_client);
381}
382
383/**
384 * Setup MESSENGER internals.
385 *
386 * @param[in/out] cls closure
387 * @param[in] config configuration to use
388 * @param[in/out] service the initialized service
389 */
390static void
391run (void *cls,
392 const struct GNUNET_CONFIGURATION_Handle *config,
393 struct GNUNET_SERVICE_Handle *service)
394{
395 messenger = create_service (config, service);
396
397 if (!messenger)
398 GNUNET_SCHEDULER_shutdown ();
399}
400
401/**
402 * Define "main" method using service macro.
403 */
404GNUNET_SERVICE_MAIN(
405 GNUNET_MESSENGER_SERVICE_NAME,
406 GNUNET_SERVICE_OPTION_NONE,
407 &run,
408 &callback_client_connect,
409 &callback_client_disconnect,
410 NULL,
411 GNUNET_MQ_hd_var_size( create, GNUNET_MESSAGE_TYPE_MESSENGER_CONNECTION_CREATE, struct GNUNET_MESSENGER_CreateMessage, NULL ),
412 GNUNET_MQ_hd_fixed_size( update, GNUNET_MESSAGE_TYPE_MESSENGER_CONNECTION_UPDATE, struct GNUNET_MESSENGER_UpdateMessage, NULL ),
413 GNUNET_MQ_hd_fixed_size( destroy, GNUNET_MESSAGE_TYPE_MESSENGER_CONNECTION_DESTROY, struct GNUNET_MESSENGER_DestroyMessage, NULL ),
414 GNUNET_MQ_hd_var_size( set_name, GNUNET_MESSAGE_TYPE_MESSENGER_CONNECTION_SET_NAME, struct GNUNET_MESSENGER_NameMessage, NULL ),
415 GNUNET_MQ_hd_fixed_size( room_open, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_OPEN, struct GNUNET_MESSENGER_RoomMessage, NULL ),
416 GNUNET_MQ_hd_fixed_size( room_entry, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_ENTRY, struct GNUNET_MESSENGER_RoomMessage, NULL ),
417 GNUNET_MQ_hd_fixed_size( room_close, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_CLOSE, struct GNUNET_MESSENGER_RoomMessage, NULL ),
418 GNUNET_MQ_hd_var_size( send_message, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_SEND_MESSAGE, struct GNUNET_MESSENGER_SendMessage, NULL ),
419 GNUNET_MQ_hd_fixed_size( get_message, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_GET_MESSAGE, struct GNUNET_MESSENGER_GetMessage, NULL ),
420 GNUNET_MQ_handler_end());