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