diff options
Diffstat (limited to 'src/messenger/gnunet-service-messenger_tunnel.c')
-rw-r--r-- | src/messenger/gnunet-service-messenger_tunnel.c | 402 |
1 files changed, 0 insertions, 402 deletions
diff --git a/src/messenger/gnunet-service-messenger_tunnel.c b/src/messenger/gnunet-service-messenger_tunnel.c deleted file mode 100644 index 45c10c1af..000000000 --- a/src/messenger/gnunet-service-messenger_tunnel.c +++ /dev/null | |||
@@ -1,402 +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_tunnel.c | ||
23 | * @brief GNUnet MESSENGER service | ||
24 | */ | ||
25 | |||
26 | #include "gnunet-service-messenger_tunnel.h" | ||
27 | |||
28 | #include "gnunet-service-messenger_handle.h" | ||
29 | #include "gnunet-service-messenger_message_recv.h" | ||
30 | #include "gnunet-service-messenger_message_store.h" | ||
31 | #include "gnunet-service-messenger_operation_store.h" | ||
32 | #include "gnunet-service-messenger_operation.h" | ||
33 | #include "messenger_api_util.h" | ||
34 | |||
35 | struct GNUNET_MESSENGER_SrvTunnel* | ||
36 | create_tunnel (struct GNUNET_MESSENGER_SrvRoom *room, | ||
37 | const struct GNUNET_PeerIdentity *door) | ||
38 | { | ||
39 | GNUNET_assert((room) && (door)); | ||
40 | |||
41 | struct GNUNET_MESSENGER_SrvTunnel *tunnel = GNUNET_new(struct GNUNET_MESSENGER_SrvTunnel); | ||
42 | |||
43 | tunnel->room = room; | ||
44 | tunnel->channel = NULL; | ||
45 | |||
46 | tunnel->peer = GNUNET_PEER_intern (door); | ||
47 | |||
48 | tunnel->messenger_version = 0; | ||
49 | |||
50 | tunnel->peer_message = NULL; | ||
51 | |||
52 | init_message_state(&(tunnel->state)); | ||
53 | |||
54 | return tunnel; | ||
55 | } | ||
56 | |||
57 | void | ||
58 | destroy_tunnel (struct GNUNET_MESSENGER_SrvTunnel *tunnel) | ||
59 | { | ||
60 | GNUNET_assert(tunnel); | ||
61 | |||
62 | if (tunnel->channel) | ||
63 | GNUNET_CADET_channel_destroy (tunnel->channel); | ||
64 | |||
65 | GNUNET_PEER_change_rc (tunnel->peer, -1); | ||
66 | |||
67 | if (tunnel->peer_message) | ||
68 | GNUNET_free(tunnel->peer_message); | ||
69 | |||
70 | clear_message_state(&(tunnel->state)); | ||
71 | |||
72 | GNUNET_free(tunnel); | ||
73 | } | ||
74 | |||
75 | void | ||
76 | bind_tunnel (struct GNUNET_MESSENGER_SrvTunnel *tunnel, | ||
77 | struct GNUNET_CADET_Channel *channel) | ||
78 | { | ||
79 | GNUNET_assert(tunnel); | ||
80 | |||
81 | if (tunnel->channel) | ||
82 | delayed_disconnect_channel (tunnel->channel); | ||
83 | |||
84 | tunnel->channel = channel; | ||
85 | } | ||
86 | |||
87 | extern void | ||
88 | callback_room_disconnect (struct GNUNET_MESSENGER_SrvRoom *room, | ||
89 | void *cls); | ||
90 | |||
91 | void | ||
92 | callback_tunnel_disconnect (void *cls, | ||
93 | const struct GNUNET_CADET_Channel *channel) | ||
94 | { | ||
95 | struct GNUNET_MESSENGER_SrvTunnel *tunnel = cls; | ||
96 | |||
97 | if (tunnel) | ||
98 | { | ||
99 | tunnel->channel = NULL; | ||
100 | |||
101 | callback_room_disconnect (tunnel->room, cls); | ||
102 | } | ||
103 | } | ||
104 | |||
105 | extern int | ||
106 | callback_verify_room_message (struct GNUNET_MESSENGER_SrvRoom *room, | ||
107 | void *cls, | ||
108 | struct GNUNET_MESSENGER_Message *message, | ||
109 | struct GNUNET_HashCode *hash); | ||
110 | |||
111 | int | ||
112 | check_tunnel_message (void *cls, | ||
113 | const struct GNUNET_MessageHeader *header) | ||
114 | { | ||
115 | struct GNUNET_MESSENGER_SrvTunnel *tunnel = cls; | ||
116 | |||
117 | if (!tunnel) | ||
118 | return GNUNET_SYSERR; | ||
119 | |||
120 | const uint16_t length = ntohs (header->size) - sizeof(*header); | ||
121 | const char *buffer = (const char*) &header[1]; | ||
122 | |||
123 | struct GNUNET_MESSENGER_Message message; | ||
124 | |||
125 | if (length < get_message_kind_size(GNUNET_MESSENGER_KIND_UNKNOWN, GNUNET_YES)) | ||
126 | { | ||
127 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Tunnel error: Message too short! (%d)\n", length); | ||
128 | return GNUNET_SYSERR; | ||
129 | } | ||
130 | |||
131 | uint16_t padding = 0; | ||
132 | |||
133 | if (GNUNET_YES != decode_message (&message, length, buffer, GNUNET_YES, &padding)) | ||
134 | { | ||
135 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Tunnel error: Decoding failed!\n"); | ||
136 | return GNUNET_SYSERR; | ||
137 | } | ||
138 | |||
139 | struct GNUNET_HashCode hash; | ||
140 | hash_message (&message, length - padding, buffer, &hash); | ||
141 | |||
142 | return callback_verify_room_message (tunnel->room, cls, &message, &hash); | ||
143 | } | ||
144 | |||
145 | extern int | ||
146 | update_room_message (struct GNUNET_MESSENGER_SrvRoom *room, | ||
147 | struct GNUNET_MESSENGER_Message *message, | ||
148 | const struct GNUNET_HashCode *hash); | ||
149 | |||
150 | extern void | ||
151 | callback_room_handle_message (struct GNUNET_MESSENGER_SrvRoom *room, | ||
152 | struct GNUNET_MESSENGER_SrvHandle *handle, | ||
153 | const struct GNUNET_MESSENGER_Message *message, | ||
154 | const struct GNUNET_HashCode *hash); | ||
155 | |||
156 | static void | ||
157 | update_tunnel_last_message (struct GNUNET_MESSENGER_SrvTunnel *tunnel, | ||
158 | const struct GNUNET_HashCode *hash) | ||
159 | { | ||
160 | struct GNUNET_MESSENGER_OperationStore *operation_store = get_room_operation_store(tunnel->room); | ||
161 | |||
162 | const int requested = (GNUNET_MESSENGER_OP_REQUEST == get_store_operation_type(operation_store, hash)? | ||
163 | GNUNET_YES : GNUNET_NO | ||
164 | ); | ||
165 | |||
166 | struct GNUNET_MESSENGER_MessageStore *message_store = get_room_message_store(tunnel->room); | ||
167 | |||
168 | const struct GNUNET_MESSENGER_Message *message = get_store_message(message_store, hash); | ||
169 | |||
170 | if (message) | ||
171 | update_message_state(&(tunnel->state), requested, message, hash); | ||
172 | } | ||
173 | |||
174 | void | ||
175 | handle_tunnel_message (void *cls, const struct GNUNET_MessageHeader *header) | ||
176 | { | ||
177 | struct GNUNET_MESSENGER_SrvTunnel *tunnel = cls; | ||
178 | |||
179 | if (!tunnel) | ||
180 | return; | ||
181 | |||
182 | const uint16_t length = ntohs (header->size) - sizeof(*header); | ||
183 | const char *buffer = (const char*) &header[1]; | ||
184 | |||
185 | struct GNUNET_MESSENGER_Message message; | ||
186 | struct GNUNET_HashCode hash; | ||
187 | |||
188 | uint16_t padding = 0; | ||
189 | |||
190 | decode_message (&message, length, buffer, GNUNET_YES, &padding); | ||
191 | hash_message (&message, length - padding, buffer, &hash); | ||
192 | |||
193 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Got message of kind: %s!\n", | ||
194 | GNUNET_MESSENGER_name_of_kind(message.header.kind)); | ||
195 | |||
196 | const int new_message = update_room_message ( | ||
197 | tunnel->room, copy_message (&message), &hash | ||
198 | ); | ||
199 | |||
200 | if (GNUNET_YES != new_message) | ||
201 | goto receive_done; | ||
202 | |||
203 | update_tunnel_last_message (tunnel, &hash); | ||
204 | |||
205 | int forward_message = GNUNET_YES; | ||
206 | |||
207 | switch (message.header.kind) | ||
208 | { | ||
209 | case GNUNET_MESSENGER_KIND_INFO: | ||
210 | forward_message = recv_message_info (tunnel->room, tunnel, &message, &hash); | ||
211 | break; | ||
212 | case GNUNET_MESSENGER_KIND_PEER: | ||
213 | forward_message = recv_message_peer (tunnel->room, tunnel, &message, &hash); | ||
214 | break; | ||
215 | case GNUNET_MESSENGER_KIND_REQUEST: | ||
216 | forward_message = recv_message_request (tunnel->room, tunnel, &message, &hash); | ||
217 | break; | ||
218 | default: | ||
219 | break; | ||
220 | } | ||
221 | |||
222 | if (GNUNET_YES == forward_message) | ||
223 | { | ||
224 | forward_room_message (tunnel->room, tunnel, &message, &hash); | ||
225 | callback_room_handle_message (tunnel->room, NULL, &message, &hash); | ||
226 | } | ||
227 | |||
228 | receive_done: | ||
229 | cleanup_message(&message); | ||
230 | |||
231 | GNUNET_CADET_receive_done (tunnel->channel); | ||
232 | } | ||
233 | |||
234 | int | ||
235 | connect_tunnel (struct GNUNET_MESSENGER_SrvTunnel *tunnel) | ||
236 | { | ||
237 | GNUNET_assert(tunnel); | ||
238 | |||
239 | if (tunnel->channel) | ||
240 | return GNUNET_NO; | ||
241 | |||
242 | const struct GNUNET_PeerIdentity *door = GNUNET_PEER_resolve2 (tunnel->peer); | ||
243 | |||
244 | struct GNUNET_CADET_Handle *cadet = get_room_cadet (tunnel->room); | ||
245 | const struct GNUNET_HashCode *key = get_room_key (tunnel->room); | ||
246 | |||
247 | struct GNUNET_MQ_MessageHandler handlers[] = { GNUNET_MQ_hd_var_size(tunnel_message, GNUNET_MESSAGE_TYPE_CADET_CLI, | ||
248 | struct GNUNET_MessageHeader, NULL), | ||
249 | GNUNET_MQ_handler_end() }; | ||
250 | |||
251 | struct GNUNET_HashCode port; | ||
252 | convert_messenger_key_to_port(key, &port); | ||
253 | tunnel->channel = GNUNET_CADET_channel_create (cadet, tunnel, door, &port, NULL, callback_tunnel_disconnect, handlers); | ||
254 | |||
255 | return GNUNET_YES; | ||
256 | } | ||
257 | |||
258 | void | ||
259 | disconnect_tunnel (struct GNUNET_MESSENGER_SrvTunnel *tunnel) | ||
260 | { | ||
261 | GNUNET_assert(tunnel); | ||
262 | |||
263 | if (tunnel->channel) | ||
264 | { | ||
265 | delayed_disconnect_channel (tunnel->channel); | ||
266 | |||
267 | tunnel->channel = NULL; | ||
268 | } | ||
269 | } | ||
270 | |||
271 | int | ||
272 | is_tunnel_connected (const struct GNUNET_MESSENGER_SrvTunnel *tunnel) | ||
273 | { | ||
274 | GNUNET_assert(tunnel); | ||
275 | |||
276 | return (tunnel->channel ? GNUNET_YES : GNUNET_NO); | ||
277 | } | ||
278 | |||
279 | struct GNUNET_MESSENGER_MessageSent | ||
280 | { | ||
281 | struct GNUNET_MESSENGER_SrvTunnel *tunnel; | ||
282 | struct GNUNET_HashCode hash; | ||
283 | }; | ||
284 | |||
285 | static void | ||
286 | callback_tunnel_sent (void *cls) | ||
287 | { | ||
288 | struct GNUNET_MESSENGER_MessageSent *sent = cls; | ||
289 | |||
290 | if (sent->tunnel) | ||
291 | update_tunnel_last_message (sent->tunnel, &(sent->hash)); | ||
292 | |||
293 | GNUNET_free(sent); | ||
294 | } | ||
295 | |||
296 | void | ||
297 | send_tunnel_envelope (struct GNUNET_MESSENGER_SrvTunnel *tunnel, | ||
298 | struct GNUNET_MQ_Envelope *env, | ||
299 | const struct GNUNET_HashCode *hash) | ||
300 | { | ||
301 | GNUNET_assert((tunnel) && (env) && (hash)); | ||
302 | |||
303 | struct GNUNET_MQ_Handle *mq = GNUNET_CADET_get_mq (tunnel->channel); | ||
304 | |||
305 | struct GNUNET_MESSENGER_MessageSent *sent = GNUNET_new(struct GNUNET_MESSENGER_MessageSent); | ||
306 | |||
307 | GNUNET_memcpy(&(sent->hash), hash, sizeof(struct GNUNET_HashCode)); | ||
308 | |||
309 | sent->tunnel = tunnel; | ||
310 | |||
311 | GNUNET_MQ_notify_sent (env, callback_tunnel_sent, sent); | ||
312 | GNUNET_MQ_send (mq, env); | ||
313 | } | ||
314 | |||
315 | int | ||
316 | send_tunnel_message (struct GNUNET_MESSENGER_SrvTunnel *tunnel, | ||
317 | void *handle, | ||
318 | struct GNUNET_MESSENGER_Message *message) | ||
319 | { | ||
320 | GNUNET_assert((tunnel) && (handle)); | ||
321 | |||
322 | if (!message) | ||
323 | return GNUNET_NO; | ||
324 | |||
325 | struct GNUNET_HashCode hash; | ||
326 | struct GNUNET_MQ_Envelope *env = pack_room_message ( | ||
327 | tunnel->room, (struct GNUNET_MESSENGER_SrvHandle*) handle, | ||
328 | message, &hash, GNUNET_MESSENGER_PACK_MODE_ENVELOPE | ||
329 | ); | ||
330 | |||
331 | destroy_message(message); | ||
332 | |||
333 | if (!env) | ||
334 | return GNUNET_NO; | ||
335 | |||
336 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending tunnel message: %s\n", | ||
337 | GNUNET_h2s(&hash)); | ||
338 | |||
339 | send_tunnel_envelope (tunnel, env, &hash); | ||
340 | return GNUNET_YES; | ||
341 | } | ||
342 | |||
343 | void | ||
344 | forward_tunnel_message (struct GNUNET_MESSENGER_SrvTunnel *tunnel, | ||
345 | const struct GNUNET_MESSENGER_Message *message, | ||
346 | const struct GNUNET_HashCode *hash) | ||
347 | { | ||
348 | GNUNET_assert((tunnel) && (message) && (hash)); | ||
349 | |||
350 | struct GNUNET_MESSENGER_Message *copy = copy_message(message); | ||
351 | struct GNUNET_MQ_Envelope *env = pack_message (copy, NULL, NULL, GNUNET_MESSENGER_PACK_MODE_ENVELOPE); | ||
352 | |||
353 | destroy_message(copy); | ||
354 | |||
355 | if (!env) | ||
356 | return; | ||
357 | |||
358 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Forwarding tunnel message: %s\n", | ||
359 | GNUNET_h2s(hash)); | ||
360 | |||
361 | send_tunnel_envelope (tunnel, env, hash); | ||
362 | } | ||
363 | |||
364 | const struct GNUNET_HashCode* | ||
365 | get_tunnel_peer_message (const struct GNUNET_MESSENGER_SrvTunnel *tunnel) | ||
366 | { | ||
367 | GNUNET_assert(tunnel); | ||
368 | |||
369 | return tunnel->peer_message; | ||
370 | } | ||
371 | |||
372 | void | ||
373 | get_tunnel_peer_identity (const struct GNUNET_MESSENGER_SrvTunnel *tunnel, | ||
374 | struct GNUNET_PeerIdentity *peer) | ||
375 | { | ||
376 | GNUNET_assert(tunnel); | ||
377 | |||
378 | GNUNET_PEER_resolve(tunnel->peer, peer); | ||
379 | } | ||
380 | |||
381 | uint32_t | ||
382 | get_tunnel_messenger_version (const struct GNUNET_MESSENGER_SrvTunnel *tunnel) | ||
383 | { | ||
384 | GNUNET_assert(tunnel); | ||
385 | |||
386 | return tunnel->messenger_version; | ||
387 | } | ||
388 | |||
389 | int | ||
390 | update_tunnel_messenger_version (struct GNUNET_MESSENGER_SrvTunnel *tunnel, | ||
391 | uint32_t version) | ||
392 | { | ||
393 | GNUNET_assert(tunnel); | ||
394 | |||
395 | if (version != GNUNET_MESSENGER_VERSION) | ||
396 | return GNUNET_SYSERR; | ||
397 | |||
398 | if (version > tunnel->messenger_version) | ||
399 | tunnel->messenger_version = version; | ||
400 | |||
401 | return GNUNET_OK; | ||
402 | } | ||