aboutsummaryrefslogtreecommitdiff
path: root/src/messenger/gnunet-service-messenger_service.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/messenger/gnunet-service-messenger_service.c')
-rw-r--r--src/messenger/gnunet-service-messenger_service.c306
1 files changed, 0 insertions, 306 deletions
diff --git a/src/messenger/gnunet-service-messenger_service.c b/src/messenger/gnunet-service-messenger_service.c
deleted file mode 100644
index 8c63e9bf4..000000000
--- a/src/messenger/gnunet-service-messenger_service.c
+++ /dev/null
@@ -1,306 +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_service.c
23 * @brief GNUnet MESSENGER service
24 */
25
26#include "gnunet-service-messenger_service.h"
27#include "gnunet-service-messenger_message_kind.h"
28#include "gnunet-service-messenger.h"
29
30static void
31callback_shutdown_service (void *cls)
32{
33 struct GNUNET_MESSENGER_Service *service = cls;
34
35 if (service)
36 {
37 service->shutdown = NULL;
38
39 destroy_service (service);
40 }
41}
42
43struct GNUNET_MESSENGER_Service*
44create_service (const struct GNUNET_CONFIGURATION_Handle *config, struct GNUNET_SERVICE_Handle *service_handle)
45{
46 GNUNET_assert((config) && (service_handle));
47
48 struct GNUNET_MESSENGER_Service *service = GNUNET_new(struct GNUNET_MESSENGER_Service);
49
50 service->config = config;
51 service->service = service_handle;
52
53 service->shutdown = GNUNET_SCHEDULER_add_shutdown (&callback_shutdown_service, service);
54
55 service->dir = NULL;
56
57 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (service->config,
58 GNUNET_MESSENGER_SERVICE_NAME,
59 "MESSENGER_DIR", &(service->dir)))
60 {
61 if (service->dir)
62 GNUNET_free(service->dir);
63
64 service->dir = NULL;
65 }
66 else
67 {
68 if ((GNUNET_YES != GNUNET_DISK_directory_test (service->dir, GNUNET_YES)) && (GNUNET_OK
69 != GNUNET_DISK_directory_create (service->dir)))
70 {
71 GNUNET_free(service->dir);
72
73 service->dir = NULL;
74 }
75 }
76
77 service->cadet = GNUNET_CADET_connect (service->config);
78
79 init_ego_store(get_service_ego_store(service), service->config);
80
81 init_list_handles (&(service->handles));
82
83 service->rooms = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
84
85 init_contact_store(get_service_contact_store(service));
86
87 return service;
88}
89
90static int
91iterate_destroy_rooms (void *cls, const struct GNUNET_HashCode *key, void *value)
92{
93 struct GNUNET_MESSENGER_SrvRoom *room = value;
94 destroy_room (room);
95 return GNUNET_YES;
96}
97
98void
99destroy_service (struct GNUNET_MESSENGER_Service *service)
100{
101 GNUNET_assert(service);
102
103 if (service->shutdown)
104 {
105 GNUNET_SCHEDULER_cancel (service->shutdown);
106
107 service->shutdown = NULL;
108 }
109
110 clear_ego_store(get_service_ego_store(service));
111 clear_list_handles (&(service->handles));
112
113 GNUNET_CONTAINER_multihashmap_iterate (service->rooms, iterate_destroy_rooms, NULL);
114 GNUNET_CONTAINER_multihashmap_destroy (service->rooms);
115
116 clear_contact_store(get_service_contact_store(service));
117
118 if (service->cadet)
119 {
120 GNUNET_CADET_disconnect (service->cadet);
121
122 service->cadet = NULL;
123 }
124
125 if (service->dir)
126 {
127 GNUNET_free(service->dir);
128
129 service->dir = NULL;
130 }
131
132 GNUNET_SERVICE_shutdown (service->service);
133
134 GNUNET_free(service);
135}
136
137struct GNUNET_MESSENGER_EgoStore*
138get_service_ego_store (struct GNUNET_MESSENGER_Service *service)
139{
140 GNUNET_assert(service);
141
142 return &(service->ego_store);
143}
144
145struct GNUNET_MESSENGER_ContactStore*
146get_service_contact_store (struct GNUNET_MESSENGER_Service *service)
147{
148 GNUNET_assert(service);
149
150 return &(service->contact_store);
151}
152
153struct GNUNET_MESSENGER_SrvHandle*
154add_service_handle (struct GNUNET_MESSENGER_Service *service, struct GNUNET_MQ_Handle *mq)
155{
156 GNUNET_assert((service) && (mq));
157
158 struct GNUNET_MESSENGER_SrvHandle *handle = create_handle (service, mq);
159
160 if (handle)
161 {
162 add_list_handle (&(service->handles), handle);
163 }
164
165 return handle;
166}
167
168void
169remove_service_handle (struct GNUNET_MESSENGER_Service *service, struct GNUNET_MESSENGER_SrvHandle *handle)
170{
171 GNUNET_assert((service) && (handle));
172
173 if (!handle)
174 return;
175
176 if (GNUNET_YES == remove_list_handle (&(service->handles), handle))
177 destroy_handle (handle);
178}
179
180int
181get_service_peer_identity (const struct GNUNET_MESSENGER_Service *service, struct GNUNET_PeerIdentity *peer)
182{
183 GNUNET_assert((service) && (peer));
184
185 return GNUNET_CRYPTO_get_peer_identity (service->config, peer);
186}
187
188struct GNUNET_MESSENGER_SrvRoom*
189get_service_room (const struct GNUNET_MESSENGER_Service *service, const struct GNUNET_HashCode *key)
190{
191 GNUNET_assert((service) && (key));
192
193 return GNUNET_CONTAINER_multihashmap_get (service->rooms, key);
194}
195
196int
197open_service_room (struct GNUNET_MESSENGER_Service *service, struct GNUNET_MESSENGER_SrvHandle *handle,
198 const struct GNUNET_HashCode *key)
199{
200 GNUNET_assert((service) && (handle) && (key));
201
202 struct GNUNET_MESSENGER_SrvRoom *room = get_service_room (service, key);
203
204 if (room)
205 return open_room (room, handle);
206
207 room = create_room (handle, key);
208
209 if ((GNUNET_YES == open_room (room, handle)) &&
210 (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (service->rooms,
211 key, room,
212 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)))
213 return GNUNET_YES;
214
215 destroy_room (room);
216 return GNUNET_NO;
217}
218
219int
220entry_service_room (struct GNUNET_MESSENGER_Service *service, struct GNUNET_MESSENGER_SrvHandle *handle,
221 const struct GNUNET_PeerIdentity *door, const struct GNUNET_HashCode *key)
222{
223 GNUNET_assert((service) && (handle) && (door) && (key));
224
225 struct GNUNET_MESSENGER_SrvRoom *room = get_service_room (service, key);
226
227 if (room)
228 {
229 if (GNUNET_YES == enter_room_at (room, handle, door))
230 return GNUNET_YES;
231 else
232 return GNUNET_NO;
233 }
234
235 room = create_room (handle, key);
236
237 if ((GNUNET_YES == enter_room_at (room, handle, door)) &&
238 (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (service->rooms,
239 key, room,
240 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)))
241 {
242 return GNUNET_YES;
243 }
244 else
245 {
246 destroy_room (room);
247 return GNUNET_NO;
248 }
249
250}
251
252int
253close_service_room (struct GNUNET_MESSENGER_Service *service, struct GNUNET_MESSENGER_SrvHandle *handle,
254 const struct GNUNET_HashCode *key)
255{
256 GNUNET_assert((service) && (handle) && (key));
257
258 struct GNUNET_MESSENGER_SrvRoom *room = get_service_room (service, key);
259
260 if (!room)
261 return GNUNET_NO;
262
263 send_room_message (room, handle, create_message_leave ());
264
265 const struct GNUNET_ShortHashCode *id = get_handle_member_id (handle, key);
266
267 GNUNET_assert(id);
268
269 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (handle->member_ids, key, id))
270 return GNUNET_NO;
271
272 struct GNUNET_MESSENGER_SrvHandle *member_handle = (struct GNUNET_MESSENGER_SrvHandle*) find_list_handle_by_member (
273 &(service->handles), key);
274
275 if (!member_handle)
276 {
277 if (GNUNET_OK == GNUNET_CONTAINER_multihashmap_remove (service->rooms, key, room))
278 {
279 destroy_room (room);
280 return GNUNET_YES;
281 }
282 else
283 return GNUNET_NO;
284 }
285
286 if (room->host == handle)
287 room->host = member_handle;
288
289 return GNUNET_YES;
290}
291
292void
293handle_service_message (struct GNUNET_MESSENGER_Service *service, struct GNUNET_MESSENGER_SrvRoom *room,
294 const struct GNUNET_MESSENGER_MemberSession *session,
295 const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
296{
297 GNUNET_assert((service) && (room) && (session) && (message) && (hash));
298
299 struct GNUNET_MESSENGER_ListHandle *element = service->handles.head;
300
301 while (element)
302 {
303 notify_handle_message (element->handle, get_room_key(room), session, message, hash);
304 element = element->next;
305 }
306}