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.c418
1 files changed, 0 insertions, 418 deletions
diff --git a/src/messenger/gnunet-service-messenger.c b/src/messenger/gnunet-service-messenger.c
deleted file mode 100644
index 506ab7443..000000000
--- a/src/messenger/gnunet-service-messenger.c
+++ /dev/null
@@ -1,418 +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_srv_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_srv_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_srv_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_srv_handle_room (msg_client->handle, &(msg->key)))
116 {
117 const struct GNUNET_ShortHashCode *member_id = get_srv_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_srv_handle_room (msg_client->handle, &(msg->door), &(msg->key)))
143 {
144 const struct GNUNET_ShortHashCode *member_id = get_srv_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_srv_handle_room (msg_client->handle, &(msg->key)))
172 {
173 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Closing room succeeded: %s\n", GNUNET_h2s (&(msg->key)));
174
175 struct GNUNET_MESSENGER_RoomMessage *response;
176 struct GNUNET_MQ_Envelope *env;
177
178 env = GNUNET_MQ_msg(response, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_CLOSE);
179 GNUNET_memcpy(&(response->key), &(msg->key), sizeof(msg->key));
180 GNUNET_MQ_send (msg_client->handle->mq, env);
181 }
182 else
183 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Closing room failed: %s\n", GNUNET_h2s (&(msg->key)));
184
185 GNUNET_SERVICE_client_continue (msg_client->client);
186}
187
188static int
189check_send_message (void *cls,
190 const struct GNUNET_MESSENGER_SendMessage *msg)
191{
192 const uint16_t full_length = ntohs (msg->header.size);
193
194 if (full_length < sizeof(*msg))
195 return GNUNET_NO;
196
197 const enum GNUNET_MESSENGER_MessageFlags flags = (
198 (enum GNUNET_MESSENGER_MessageFlags) (msg->flags)
199 );
200
201 const uint16_t length = full_length - sizeof(*msg);
202 const char *buffer = ((const char*) msg) + sizeof(*msg);
203
204 ssize_t key_length = 0;
205
206 if (!(flags & GNUNET_MESSENGER_FLAG_PRIVATE))
207 goto check_for_message;
208
209 struct GNUNET_IDENTITY_PublicKey public_key;
210
211 key_length = GNUNET_IDENTITY_read_key_from_buffer(&public_key, buffer, length);
212
213check_for_message:
214 if (key_length < 0)
215 return GNUNET_NO;
216
217 const uint16_t msg_length = length - key_length;
218 const char* msg_buffer = buffer + key_length;
219
220 struct GNUNET_MESSENGER_Message message;
221
222 if (length < get_message_kind_size(GNUNET_MESSENGER_KIND_UNKNOWN, GNUNET_NO))
223 return GNUNET_NO;
224
225 if (GNUNET_YES != decode_message (&message, msg_length, msg_buffer, GNUNET_NO, NULL))
226 return GNUNET_NO;
227
228 const int allowed = filter_message_sending(&message);
229
230 cleanup_message(&message);
231 return GNUNET_YES == allowed? GNUNET_OK : GNUNET_NO;
232}
233
234static void
235handle_send_message (void *cls,
236 const struct GNUNET_MESSENGER_SendMessage *msg)
237{
238 struct GNUNET_MESSENGER_Client *msg_client = cls;
239
240 const enum GNUNET_MESSENGER_MessageFlags flags = (
241 (enum GNUNET_MESSENGER_MessageFlags) (msg->flags)
242 );
243
244 const struct GNUNET_HashCode *key = &(msg->key);
245 const char *buffer = ((const char*) msg) + sizeof(*msg);
246
247 const uint16_t length = ntohs (msg->header.size) - sizeof(*msg);
248 ssize_t key_length = 0;
249
250 struct GNUNET_IDENTITY_PublicKey public_key;
251
252 if (flags & GNUNET_MESSENGER_FLAG_PRIVATE)
253 key_length = GNUNET_IDENTITY_read_key_from_buffer(
254 &public_key, buffer, length
255 );
256
257 const uint16_t msg_length = length - key_length;
258 const char* msg_buffer = buffer + key_length;
259
260 struct GNUNET_MESSENGER_Message message;
261 decode_message (&message, msg_length, msg_buffer, GNUNET_NO, NULL);
262
263 if ((flags & GNUNET_MESSENGER_FLAG_PRIVATE) &&
264 (GNUNET_YES != encrypt_message(&message, &public_key)))
265 {
266 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Encrypting message failed: Message got dropped!\n");
267
268 goto end_handling;
269 }
270
271 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending message: %s to %s\n",
272 GNUNET_MESSENGER_name_of_kind (message.header.kind), GNUNET_h2s (key));
273
274 if (GNUNET_YES != send_srv_handle_message (msg_client->handle, key, &message))
275 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sending message failed: %s to %s\n",
276 GNUNET_MESSENGER_name_of_kind (message.header.kind), GNUNET_h2s (key));
277
278end_handling:
279 cleanup_message(&message);
280
281 GNUNET_SERVICE_client_continue (msg_client->client);
282}
283
284static void
285callback_found_message (void *cls,
286 struct GNUNET_MESSENGER_SrvRoom *room,
287 const struct GNUNET_MESSENGER_Message *message,
288 const struct GNUNET_HashCode *hash)
289{
290 struct GNUNET_MESSENGER_Client *msg_client = cls;
291
292 if (!message)
293 {
294 send_srv_room_message(room, msg_client->handle, create_message_request(hash));
295 return;
296 }
297
298 struct GNUNET_MESSENGER_MemberStore *store = get_srv_room_member_store(room);
299
300 struct GNUNET_MESSENGER_Member *member = get_store_member_of(store, message);
301
302 if (!member)
303 {
304 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sender of message (%s) unknown!\n", GNUNET_h2s (hash));
305 return;
306 }
307
308 struct GNUNET_MESSENGER_MemberSession *session = get_member_session_of(member, message, hash);
309
310 if (session)
311 notify_srv_handle_message (msg_client->handle, room, session, message, hash);
312}
313
314static void
315handle_get_message (void *cls,
316 const struct GNUNET_MESSENGER_GetMessage *msg)
317{
318 struct GNUNET_MESSENGER_Client *msg_client = cls;
319
320 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Requesting message from room: %s\n", GNUNET_h2s (&(msg->key)));
321
322 struct GNUNET_MESSENGER_SrvRoom *room = get_service_room (messenger, &(msg->key));
323
324 if (!room)
325 {
326 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Room not found: %s\n", GNUNET_h2s (&(msg->key)));
327 goto end_handling;
328 }
329
330 struct GNUNET_MESSENGER_MemberStore *member_store = get_srv_room_member_store(room);
331
332 struct GNUNET_MESSENGER_Member *member = get_store_member(member_store, get_srv_handle_member_id(
333 msg_client->handle, &(msg->key)
334 ));
335
336 if (!member)
337 {
338 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Member not valid to request a message!\n");
339 goto end_handling;
340 }
341
342 struct GNUNET_MESSENGER_MemberSession *session = get_member_session(member, &(get_srv_handle_ego(msg_client->handle)->pub));
343
344 if (!session)
345 {
346 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Session not valid to request a message!\n");
347 goto end_handling;
348 }
349
350 request_srv_room_message (room, &(msg->hash), session, callback_found_message, msg_client);
351
352end_handling:
353 GNUNET_SERVICE_client_continue (msg_client->client);
354}
355
356static void*
357callback_client_connect (void *cls,
358 struct GNUNET_SERVICE_Client *client,
359 struct GNUNET_MQ_Handle *mq)
360{
361 struct GNUNET_MESSENGER_Client *msg_client = GNUNET_new(struct GNUNET_MESSENGER_Client);
362
363 msg_client->client = client;
364 msg_client->handle = add_service_handle (messenger, mq);
365
366 return msg_client;
367}
368
369static void
370callback_client_disconnect (void *cls,
371 struct GNUNET_SERVICE_Client *client,
372 void *internal_cls)
373{
374 struct GNUNET_MESSENGER_Client *msg_client = internal_cls;
375
376 remove_service_handle (messenger, msg_client->handle);
377
378 GNUNET_free(msg_client);
379}
380
381/**
382 * Setup MESSENGER internals.
383 *
384 * @param[in/out] cls closure
385 * @param[in] config configuration to use
386 * @param[in/out] service the initialized service
387 */
388static void
389run (void *cls,
390 const struct GNUNET_CONFIGURATION_Handle *config,
391 struct GNUNET_SERVICE_Handle *service)
392{
393 messenger = create_service (config, service);
394
395 if (!messenger)
396 GNUNET_SCHEDULER_shutdown ();
397}
398
399/**
400 * Define "main" method using service macro.
401 */
402GNUNET_SERVICE_MAIN(
403 GNUNET_MESSENGER_SERVICE_NAME,
404 GNUNET_SERVICE_OPTION_NONE,
405 &run,
406 &callback_client_connect,
407 &callback_client_disconnect,
408 NULL,
409 GNUNET_MQ_hd_var_size( create, GNUNET_MESSAGE_TYPE_MESSENGER_CONNECTION_CREATE, struct GNUNET_MESSENGER_CreateMessage, NULL ),
410 GNUNET_MQ_hd_fixed_size( update, GNUNET_MESSAGE_TYPE_MESSENGER_CONNECTION_UPDATE, struct GNUNET_MESSENGER_UpdateMessage, NULL ),
411 GNUNET_MQ_hd_fixed_size( destroy, GNUNET_MESSAGE_TYPE_MESSENGER_CONNECTION_DESTROY, struct GNUNET_MESSENGER_DestroyMessage, NULL ),
412 GNUNET_MQ_hd_var_size( set_name, GNUNET_MESSAGE_TYPE_MESSENGER_CONNECTION_SET_NAME, struct GNUNET_MESSENGER_NameMessage, NULL ),
413 GNUNET_MQ_hd_fixed_size( room_open, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_OPEN, struct GNUNET_MESSENGER_RoomMessage, NULL ),
414 GNUNET_MQ_hd_fixed_size( room_entry, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_ENTRY, struct GNUNET_MESSENGER_RoomMessage, NULL ),
415 GNUNET_MQ_hd_fixed_size( room_close, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_CLOSE, struct GNUNET_MESSENGER_RoomMessage, NULL ),
416 GNUNET_MQ_hd_var_size( send_message, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_SEND_MESSAGE, struct GNUNET_MESSENGER_SendMessage, NULL ),
417 GNUNET_MQ_hd_fixed_size( get_message, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_GET_MESSAGE, struct GNUNET_MESSENGER_GetMessage, NULL ),
418 GNUNET_MQ_handler_end());