diff options
Diffstat (limited to 'src/transport/transport_api2_core.c')
-rw-r--r-- | src/transport/transport_api2_core.c | 804 |
1 files changed, 0 insertions, 804 deletions
diff --git a/src/transport/transport_api2_core.c b/src/transport/transport_api2_core.c deleted file mode 100644 index 8cd0b7c8c..000000000 --- a/src/transport/transport_api2_core.c +++ /dev/null | |||
@@ -1,804 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2009-2013, 2016, 2018 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 | /** | ||
22 | * @file transport/transport_api_core.c | ||
23 | * @brief library to access the transport service for message exchange | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_constants.h" | ||
29 | #include "gnunet_arm_service.h" | ||
30 | #include "gnunet_hello_lib.h" | ||
31 | #include "gnunet_protocols.h" | ||
32 | #include "gnunet_transport_core_service.h" | ||
33 | #include "transport.h" | ||
34 | |||
35 | #define LOG(kind, ...) GNUNET_log_from (kind, "transport-api-core", __VA_ARGS__) | ||
36 | |||
37 | /** | ||
38 | * How large to start with for the hashmap of neighbours. | ||
39 | */ | ||
40 | #define STARTING_NEIGHBOURS_SIZE 16 | ||
41 | |||
42 | /** | ||
43 | * Window size. How many messages to the same target do we pass | ||
44 | * to TRANSPORT without a SEND_OK in between? Small values limit | ||
45 | * thoughput, large values will increase latency. | ||
46 | * | ||
47 | * FIXME-OPTIMIZE: find out what good values are experimentally, | ||
48 | * maybe set adaptively (i.e. to observed available bandwidth). | ||
49 | */ | ||
50 | #define SEND_WINDOW_SIZE 4 | ||
51 | |||
52 | |||
53 | /** | ||
54 | * Entry in hash table of all of our current (connected) neighbours. | ||
55 | */ | ||
56 | struct Neighbour | ||
57 | { | ||
58 | /** | ||
59 | * Identity of this neighbour. | ||
60 | */ | ||
61 | struct GNUNET_PeerIdentity id; | ||
62 | |||
63 | /** | ||
64 | * Overall transport handle. | ||
65 | */ | ||
66 | struct GNUNET_TRANSPORT_CoreHandle *h; | ||
67 | |||
68 | /** | ||
69 | * Active message queue for the peer. | ||
70 | */ | ||
71 | struct GNUNET_MQ_Handle *mq; | ||
72 | |||
73 | /** | ||
74 | * Envelope with the message we are currently transmitting (or NULL). | ||
75 | */ | ||
76 | struct GNUNET_MQ_Envelope *env; | ||
77 | |||
78 | /** | ||
79 | * Closure for @e mq handlers. | ||
80 | */ | ||
81 | void *handlers_cls; | ||
82 | |||
83 | /** | ||
84 | * How many messages can we still send to this peer before we should | ||
85 | * throttle? | ||
86 | */ | ||
87 | unsigned int ready_window; | ||
88 | |||
89 | /** | ||
90 | * Used to indicate our status if @e env is non-NULL. Set to | ||
91 | * #GNUNET_YES if we did pass a message to the MQ and are waiting | ||
92 | * for the call to #notify_send_done(). Set to #GNUNET_NO if the @e | ||
93 | * ready_window is 0 and @e env is waiting for a | ||
94 | * #GNUNET_MESSAGE_TYPE_TRANSPORT_RECV_OK? | ||
95 | */ | ||
96 | int16_t awaiting_done; | ||
97 | |||
98 | /** | ||
99 | * Size of the message in @e env. | ||
100 | */ | ||
101 | uint16_t env_size; | ||
102 | }; | ||
103 | |||
104 | |||
105 | /** | ||
106 | * Handle for the transport service (includes all of the | ||
107 | * state for the transport service). | ||
108 | */ | ||
109 | struct GNUNET_TRANSPORT_CoreHandle | ||
110 | { | ||
111 | /** | ||
112 | * Closure for the callbacks. | ||
113 | */ | ||
114 | void *cls; | ||
115 | |||
116 | /** | ||
117 | * Functions to call for received data (template for | ||
118 | * new message queues). | ||
119 | */ | ||
120 | struct GNUNET_MQ_MessageHandler *handlers; | ||
121 | |||
122 | /** | ||
123 | * function to call on connect events | ||
124 | */ | ||
125 | GNUNET_TRANSPORT_NotifyConnect nc_cb; | ||
126 | |||
127 | /** | ||
128 | * function to call on disconnect events | ||
129 | */ | ||
130 | GNUNET_TRANSPORT_NotifyDisconnect nd_cb; | ||
131 | |||
132 | /** | ||
133 | * My client connection to the transport service. | ||
134 | */ | ||
135 | struct GNUNET_MQ_Handle *mq; | ||
136 | |||
137 | /** | ||
138 | * My configuration. | ||
139 | */ | ||
140 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
141 | |||
142 | /** | ||
143 | * Hash map of the current connected neighbours of this peer. | ||
144 | * Maps peer identities to `struct Neighbour` entries. | ||
145 | */ | ||
146 | struct GNUNET_CONTAINER_MultiPeerMap *neighbours; | ||
147 | |||
148 | /** | ||
149 | * Peer identity as assumed by this process, or all zeros. | ||
150 | */ | ||
151 | struct GNUNET_PeerIdentity self; | ||
152 | |||
153 | /** | ||
154 | * ID of the task trying to reconnect to the service. | ||
155 | */ | ||
156 | struct GNUNET_SCHEDULER_Task *reconnect_task; | ||
157 | |||
158 | /** | ||
159 | * Delay until we try to reconnect. | ||
160 | */ | ||
161 | struct GNUNET_TIME_Relative reconnect_delay; | ||
162 | |||
163 | /** | ||
164 | * Should we check that @e self matches what the service thinks? | ||
165 | * (if #GNUNET_NO, then @e self is all zeros!). | ||
166 | */ | ||
167 | int check_self; | ||
168 | }; | ||
169 | |||
170 | |||
171 | /** | ||
172 | * Function that will schedule the job that will try | ||
173 | * to connect us again to the client. | ||
174 | * | ||
175 | * @param h transport service to reconnect | ||
176 | */ | ||
177 | static void | ||
178 | disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_CoreHandle *h); | ||
179 | |||
180 | |||
181 | /** | ||
182 | * Get the neighbour list entry for the given peer | ||
183 | * | ||
184 | * @param h our context | ||
185 | * @param peer peer to look up | ||
186 | * @return NULL if no such peer entry exists | ||
187 | */ | ||
188 | static struct Neighbour * | ||
189 | neighbour_find (struct GNUNET_TRANSPORT_CoreHandle *h, | ||
190 | const struct GNUNET_PeerIdentity *peer) | ||
191 | { | ||
192 | return GNUNET_CONTAINER_multipeermap_get (h->neighbours, peer); | ||
193 | } | ||
194 | |||
195 | |||
196 | /** | ||
197 | * Iterator over hash map entries, for deleting state of a neighbour. | ||
198 | * | ||
199 | * @param cls the `struct GNUNET_TRANSPORT_CoreHandle *` | ||
200 | * @param key peer identity | ||
201 | * @param value value in the hash map, the neighbour entry to delete | ||
202 | * @return #GNUNET_YES if we should continue to | ||
203 | * iterate, | ||
204 | * #GNUNET_NO if not. | ||
205 | */ | ||
206 | static int | ||
207 | neighbour_delete (void *cls, const struct GNUNET_PeerIdentity *key, void *value) | ||
208 | { | ||
209 | struct GNUNET_TRANSPORT_CoreHandle *handle = cls; | ||
210 | struct Neighbour *n = value; | ||
211 | |||
212 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
213 | "Dropping entry for neighbour `%s'.\n", | ||
214 | GNUNET_i2s (key)); | ||
215 | if (NULL != handle->nd_cb) | ||
216 | handle->nd_cb (handle->cls, &n->id, n->handlers_cls); | ||
217 | if (NULL != n->env) | ||
218 | { | ||
219 | GNUNET_MQ_send_cancel (n->env); | ||
220 | n->env = NULL; | ||
221 | } | ||
222 | GNUNET_MQ_destroy (n->mq); | ||
223 | GNUNET_assert (NULL == n->mq); | ||
224 | GNUNET_assert ( | ||
225 | GNUNET_YES == | ||
226 | GNUNET_CONTAINER_multipeermap_remove (handle->neighbours, key, n)); | ||
227 | GNUNET_free (n); | ||
228 | return GNUNET_YES; | ||
229 | } | ||
230 | |||
231 | |||
232 | /** | ||
233 | * Generic error handler, called with the appropriate | ||
234 | * error code and the same closure specified at the creation of | ||
235 | * the message queue. | ||
236 | * Not every message queue implementation supports an error handler. | ||
237 | * | ||
238 | * @param cls closure with the `struct GNUNET_TRANSPORT_CoreHandle *` | ||
239 | * @param error error code | ||
240 | */ | ||
241 | static void | ||
242 | mq_error_handler (void *cls, enum GNUNET_MQ_Error error) | ||
243 | { | ||
244 | struct GNUNET_TRANSPORT_CoreHandle *h = cls; | ||
245 | |||
246 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
247 | "Error %u received from transport service, disconnecting temporarily.\n", | ||
248 | error); | ||
249 | disconnect_and_schedule_reconnect (h); | ||
250 | } | ||
251 | |||
252 | |||
253 | /** | ||
254 | * A message from the handler's message queue to a neighbour was | ||
255 | * transmitted. Now trigger (possibly delayed) notification of the | ||
256 | * neighbour's message queue that we are done and thus ready for | ||
257 | * the next message. Note that the MQ being ready is independent | ||
258 | * of the send window, as we may queue many messages and simply | ||
259 | * not pass them to TRANSPORT if the send window is insufficient. | ||
260 | * | ||
261 | * @param cls the `struct Neighbour` where the message was sent | ||
262 | */ | ||
263 | static void | ||
264 | notify_send_done (void *cls) | ||
265 | { | ||
266 | struct Neighbour *n = cls; | ||
267 | |||
268 | n->awaiting_done = GNUNET_NO; | ||
269 | n->env = NULL; | ||
270 | GNUNET_MQ_impl_send_continue (n->mq); | ||
271 | } | ||
272 | |||
273 | |||
274 | /** | ||
275 | * We have an envelope waiting for transmission at @a n, and | ||
276 | * our transmission window is positive. Perform the transmission. | ||
277 | * | ||
278 | * @param n neighbour to perform transmission for | ||
279 | */ | ||
280 | static void | ||
281 | do_send (struct Neighbour *n) | ||
282 | { | ||
283 | GNUNET_assert (0 < n->ready_window); | ||
284 | GNUNET_assert (NULL != n->env); | ||
285 | n->ready_window--; | ||
286 | n->awaiting_done = GNUNET_YES; | ||
287 | GNUNET_MQ_notify_sent (n->env, ¬ify_send_done, n); | ||
288 | GNUNET_MQ_send (n->h->mq, n->env); | ||
289 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
290 | "Passed message of type %u for neighbour `%s' to TRANSPORT.\n", | ||
291 | ntohs (GNUNET_MQ_env_get_msg (n->env)->type), | ||
292 | GNUNET_i2s (&n->id)); | ||
293 | } | ||
294 | |||
295 | |||
296 | /** | ||
297 | * Implement sending functionality of a message queue. | ||
298 | * Called one message at a time. Should send the @a msg | ||
299 | * to the transport service and then notify the queue | ||
300 | * once we are ready for the next one. | ||
301 | * | ||
302 | * @param mq the message queue | ||
303 | * @param msg the message to send | ||
304 | * @param impl_state state of the implementation | ||
305 | */ | ||
306 | static void | ||
307 | mq_send_impl (struct GNUNET_MQ_Handle *mq, | ||
308 | const struct GNUNET_MessageHeader *msg, | ||
309 | void *impl_state) | ||
310 | { | ||
311 | struct Neighbour *n = impl_state; | ||
312 | struct OutboundMessage *obm; | ||
313 | uint16_t msize; | ||
314 | |||
315 | msize = ntohs (msg->size); | ||
316 | if (msize >= GNUNET_MAX_MESSAGE_SIZE - sizeof(*obm)) | ||
317 | { | ||
318 | GNUNET_break (0); | ||
319 | GNUNET_MQ_impl_send_continue (mq); | ||
320 | return; | ||
321 | } | ||
322 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
323 | "CORE requested transmission of message of type %u to neighbour `%s'.\n", | ||
324 | ntohs (msg->type), | ||
325 | GNUNET_i2s (&n->id)); | ||
326 | |||
327 | GNUNET_assert (NULL == n->env); | ||
328 | n->env = | ||
329 | GNUNET_MQ_msg_nested_mh (obm, GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, msg); | ||
330 | n->env_size = ntohs (msg->size); | ||
331 | { | ||
332 | struct GNUNET_MQ_Envelope *env; | ||
333 | |||
334 | env = GNUNET_MQ_get_current_envelope (mq); | ||
335 | obm->priority = htonl ((uint32_t) GNUNET_MQ_env_get_options (env)); | ||
336 | } | ||
337 | obm->peer = n->id; | ||
338 | if (0 == n->ready_window) | ||
339 | { | ||
340 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
341 | "Flow control delays transmission to CORE until we see SEND_OK.\n"); | ||
342 | return; /* can't send yet, need to wait for SEND_OK */ | ||
343 | } | ||
344 | do_send (n); | ||
345 | } | ||
346 | |||
347 | |||
348 | /** | ||
349 | * Handle destruction of a message queue. Implementations must not | ||
350 | * free @a mq, but should take care of @a impl_state. | ||
351 | * | ||
352 | * @param mq the message queue to destroy | ||
353 | * @param impl_state state of the implementation | ||
354 | */ | ||
355 | static void | ||
356 | mq_destroy_impl (struct GNUNET_MQ_Handle *mq, void *impl_state) | ||
357 | { | ||
358 | struct Neighbour *n = impl_state; | ||
359 | |||
360 | GNUNET_assert (mq == n->mq); | ||
361 | n->mq = NULL; | ||
362 | } | ||
363 | |||
364 | |||
365 | /** | ||
366 | * Implementation function that cancels the currently sent message. | ||
367 | * Should basically undo whatever #mq_send_impl() did. | ||
368 | * | ||
369 | * @param mq message queue | ||
370 | * @param impl_state state specific to the implementation | ||
371 | */ | ||
372 | static void | ||
373 | mq_cancel_impl (struct GNUNET_MQ_Handle *mq, void *impl_state) | ||
374 | { | ||
375 | struct Neighbour *n = impl_state; | ||
376 | |||
377 | n->ready_window++; | ||
378 | if (GNUNET_YES == n->awaiting_done) | ||
379 | { | ||
380 | GNUNET_MQ_send_cancel (n->env); | ||
381 | n->env = NULL; | ||
382 | n->awaiting_done = GNUNET_NO; | ||
383 | } | ||
384 | else | ||
385 | { | ||
386 | GNUNET_assert (0 == n->ready_window); | ||
387 | n->env = NULL; | ||
388 | } | ||
389 | } | ||
390 | |||
391 | |||
392 | /** | ||
393 | * We had an error processing a message we forwarded from a peer to | ||
394 | * the CORE service. We should just complain about it but otherwise | ||
395 | * continue processing. | ||
396 | * | ||
397 | * @param cls closure | ||
398 | * @param error error code | ||
399 | */ | ||
400 | static void | ||
401 | peer_mq_error_handler (void *cls, enum GNUNET_MQ_Error error) | ||
402 | { | ||
403 | /* struct Neighbour *n = cls; */ | ||
404 | |||
405 | GNUNET_break_op (0); | ||
406 | } | ||
407 | |||
408 | |||
409 | /** | ||
410 | * Function we use for handling incoming connect messages. | ||
411 | * | ||
412 | * @param cls closure, a `struct GNUNET_TRANSPORT_Handle *` | ||
413 | * @param cim message received | ||
414 | */ | ||
415 | static void | ||
416 | handle_connect (void *cls, const struct ConnectInfoMessage *cim) | ||
417 | { | ||
418 | struct GNUNET_TRANSPORT_CoreHandle *h = cls; | ||
419 | struct Neighbour *n; | ||
420 | |||
421 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
422 | "Receiving CONNECT message for `%s'\n", | ||
423 | GNUNET_i2s (&cim->id)); | ||
424 | n = neighbour_find (h, &cim->id); | ||
425 | if (NULL != n) | ||
426 | { | ||
427 | GNUNET_break (0); | ||
428 | disconnect_and_schedule_reconnect (h); | ||
429 | return; | ||
430 | } | ||
431 | n = GNUNET_new (struct Neighbour); | ||
432 | n->id = cim->id; | ||
433 | n->h = h; | ||
434 | n->ready_window = SEND_WINDOW_SIZE; | ||
435 | GNUNET_assert (GNUNET_OK == | ||
436 | GNUNET_CONTAINER_multipeermap_put ( | ||
437 | h->neighbours, | ||
438 | &n->id, | ||
439 | n, | ||
440 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); | ||
441 | |||
442 | n->mq = GNUNET_MQ_queue_for_callbacks (&mq_send_impl, | ||
443 | &mq_destroy_impl, | ||
444 | &mq_cancel_impl, | ||
445 | n, | ||
446 | h->handlers, | ||
447 | &peer_mq_error_handler, | ||
448 | n); | ||
449 | if (NULL != h->nc_cb) | ||
450 | { | ||
451 | n->handlers_cls = h->nc_cb (h->cls, &n->id, n->mq); | ||
452 | GNUNET_MQ_set_handlers_closure (n->mq, n->handlers_cls); | ||
453 | } | ||
454 | } | ||
455 | |||
456 | |||
457 | /** | ||
458 | * Function we use for handling incoming disconnect messages. | ||
459 | * | ||
460 | * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *` | ||
461 | * @param dim message received | ||
462 | */ | ||
463 | static void | ||
464 | handle_disconnect (void *cls, const struct DisconnectInfoMessage *dim) | ||
465 | { | ||
466 | struct GNUNET_TRANSPORT_CoreHandle *h = cls; | ||
467 | struct Neighbour *n; | ||
468 | |||
469 | GNUNET_break (ntohl (dim->reserved) == 0); | ||
470 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
471 | "Receiving DISCONNECT message for `%s'.\n", | ||
472 | GNUNET_i2s (&dim->peer)); | ||
473 | n = neighbour_find (h, &dim->peer); | ||
474 | if (NULL == n) | ||
475 | { | ||
476 | GNUNET_break (0); | ||
477 | disconnect_and_schedule_reconnect (h); | ||
478 | return; | ||
479 | } | ||
480 | GNUNET_assert (GNUNET_YES == neighbour_delete (h, &dim->peer, n)); | ||
481 | } | ||
482 | |||
483 | |||
484 | /** | ||
485 | * Function we use for handling incoming send-ok messages. | ||
486 | * | ||
487 | * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *` | ||
488 | * @param okm message received | ||
489 | */ | ||
490 | static void | ||
491 | handle_send_ok (void *cls, const struct SendOkMessage *okm) | ||
492 | { | ||
493 | struct GNUNET_TRANSPORT_CoreHandle *h = cls; | ||
494 | struct Neighbour *n; | ||
495 | |||
496 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
497 | "Receiving SEND_OK message for transmission to %s\n", | ||
498 | GNUNET_i2s (&okm->peer)); | ||
499 | n = neighbour_find (h, &okm->peer); | ||
500 | if (NULL == n) | ||
501 | { | ||
502 | /* We should never get a 'SEND_OK' for a peer that we are not | ||
503 | connected to */ | ||
504 | GNUNET_break (0); | ||
505 | disconnect_and_schedule_reconnect (h); | ||
506 | return; | ||
507 | } | ||
508 | n->ready_window++; | ||
509 | if ((NULL != n->env) && (1 == n->ready_window)) | ||
510 | do_send (n); | ||
511 | } | ||
512 | |||
513 | |||
514 | /** | ||
515 | * Function we use for checking incoming "inbound" messages. | ||
516 | * | ||
517 | * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *` | ||
518 | * @param im message received | ||
519 | */ | ||
520 | static int | ||
521 | check_recv (void *cls, const struct InboundMessage *im) | ||
522 | { | ||
523 | const struct GNUNET_MessageHeader *imm; | ||
524 | uint16_t size; | ||
525 | |||
526 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
527 | "check_recv\n"); | ||
528 | size = ntohs (im->header.size) - sizeof(*im); | ||
529 | if (size < sizeof(struct GNUNET_MessageHeader)) | ||
530 | { | ||
531 | GNUNET_break (0); | ||
532 | return GNUNET_SYSERR; | ||
533 | } | ||
534 | imm = (const struct GNUNET_MessageHeader *) &im[1]; | ||
535 | if (ntohs (imm->size) != size) | ||
536 | { | ||
537 | GNUNET_break (0); | ||
538 | return GNUNET_SYSERR; | ||
539 | } | ||
540 | return GNUNET_OK; | ||
541 | } | ||
542 | |||
543 | |||
544 | /** | ||
545 | * Function we use for handling incoming messages. | ||
546 | * | ||
547 | * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *` | ||
548 | * @param im message received | ||
549 | */ | ||
550 | static void | ||
551 | handle_recv (void *cls, const struct InboundMessage *im) | ||
552 | { | ||
553 | struct GNUNET_TRANSPORT_CoreHandle *h = cls; | ||
554 | const struct GNUNET_MessageHeader *imm = | ||
555 | (const struct GNUNET_MessageHeader *) &im[1]; | ||
556 | struct Neighbour *n; | ||
557 | |||
558 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
559 | "Received message of type %u with %u bytes from `%s'.\n", | ||
560 | (unsigned int) ntohs (imm->type), | ||
561 | (unsigned int) ntohs (imm->size), | ||
562 | GNUNET_i2s (&im->peer)); | ||
563 | n = neighbour_find (h, &im->peer); | ||
564 | if (NULL == n) | ||
565 | { | ||
566 | GNUNET_break (0); | ||
567 | disconnect_and_schedule_reconnect (h); | ||
568 | return; | ||
569 | } | ||
570 | GNUNET_MQ_inject_message (n->mq, imm); | ||
571 | } | ||
572 | |||
573 | |||
574 | /** | ||
575 | * Try again to connect to transport service. | ||
576 | * | ||
577 | * @param cls the handle to the transport service | ||
578 | */ | ||
579 | static void | ||
580 | reconnect (void *cls) | ||
581 | { | ||
582 | struct GNUNET_TRANSPORT_CoreHandle *h = cls; | ||
583 | struct GNUNET_MQ_MessageHandler handlers[] = | ||
584 | { GNUNET_MQ_hd_fixed_size (connect, | ||
585 | GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT, | ||
586 | struct ConnectInfoMessage, | ||
587 | h), | ||
588 | GNUNET_MQ_hd_fixed_size (disconnect, | ||
589 | GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT, | ||
590 | struct DisconnectInfoMessage, | ||
591 | h), | ||
592 | GNUNET_MQ_hd_fixed_size (send_ok, | ||
593 | GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK, | ||
594 | struct SendOkMessage, | ||
595 | h), | ||
596 | GNUNET_MQ_hd_var_size (recv, | ||
597 | GNUNET_MESSAGE_TYPE_TRANSPORT_RECV, | ||
598 | struct InboundMessage, | ||
599 | h), | ||
600 | GNUNET_MQ_handler_end () }; | ||
601 | struct GNUNET_MQ_Envelope *env; | ||
602 | struct StartMessage *s; | ||
603 | uint32_t options; | ||
604 | |||
605 | h->reconnect_task = NULL; | ||
606 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n"); | ||
607 | GNUNET_assert (NULL == h->mq); | ||
608 | h->mq = | ||
609 | GNUNET_CLIENT_connect (h->cfg, "transport", handlers, &mq_error_handler, h); | ||
610 | if (NULL == h->mq) | ||
611 | return; | ||
612 | env = GNUNET_MQ_msg (s, GNUNET_MESSAGE_TYPE_TRANSPORT_START); | ||
613 | options = 0; | ||
614 | if (h->check_self) | ||
615 | options |= 1; | ||
616 | if (NULL != h->handlers) | ||
617 | options |= 2; | ||
618 | s->options = htonl (options); | ||
619 | s->self = h->self; | ||
620 | GNUNET_MQ_send (h->mq, env); | ||
621 | } | ||
622 | |||
623 | |||
624 | /** | ||
625 | * Disconnect from the transport service. | ||
626 | * | ||
627 | * @param h transport service to reconnect | ||
628 | */ | ||
629 | static void | ||
630 | disconnect (struct GNUNET_TRANSPORT_CoreHandle *h) | ||
631 | { | ||
632 | GNUNET_CONTAINER_multipeermap_iterate (h->neighbours, &neighbour_delete, h); | ||
633 | if (NULL != h->mq) | ||
634 | { | ||
635 | GNUNET_MQ_destroy (h->mq); | ||
636 | h->mq = NULL; | ||
637 | } | ||
638 | } | ||
639 | |||
640 | |||
641 | /** | ||
642 | * Function that will schedule the job that will try | ||
643 | * to connect us again to the client. | ||
644 | * | ||
645 | * @param h transport service to reconnect | ||
646 | */ | ||
647 | static void | ||
648 | disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_CoreHandle *h) | ||
649 | { | ||
650 | GNUNET_assert (NULL == h->reconnect_task); | ||
651 | disconnect (h); | ||
652 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
653 | "Scheduling task to reconnect to transport service in %s.\n", | ||
654 | GNUNET_STRINGS_relative_time_to_string (h->reconnect_delay, GNUNET_YES)); | ||
655 | h->reconnect_task = | ||
656 | GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h); | ||
657 | h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay); | ||
658 | } | ||
659 | |||
660 | |||
661 | /** | ||
662 | * Checks if a given peer is connected to us and get the message queue. | ||
663 | * | ||
664 | * @param handle connection to transport service | ||
665 | * @param peer the peer to check | ||
666 | * @return NULL if disconnected, otherwise message queue for @a peer | ||
667 | */ | ||
668 | struct GNUNET_MQ_Handle * | ||
669 | GNUNET_TRANSPORT_core_get_mq (struct GNUNET_TRANSPORT_CoreHandle *handle, | ||
670 | const struct GNUNET_PeerIdentity *peer) | ||
671 | { | ||
672 | struct Neighbour *n; | ||
673 | |||
674 | n = neighbour_find (handle, peer); | ||
675 | if (NULL == n) | ||
676 | return NULL; | ||
677 | return n->mq; | ||
678 | } | ||
679 | |||
680 | |||
681 | /** | ||
682 | * Notification from the CORE service to the TRANSPORT service | ||
683 | * that the CORE service has finished processing a message from | ||
684 | * TRANSPORT (via the @code{handlers} of #GNUNET_TRANSPORT_core_connect()) | ||
685 | * and that it is thus now OK for TRANSPORT to send more messages | ||
686 | * for @a pid. | ||
687 | * | ||
688 | * Used to provide flow control, this is our equivalent to | ||
689 | * #GNUNET_SERVICE_client_continue() of an ordinary service. | ||
690 | * | ||
691 | * Note that due to the use of a window, TRANSPORT may send multiple | ||
692 | * messages destined for the same peer even without an intermediate | ||
693 | * call to this function. However, CORE must still call this function | ||
694 | * once per message received, as otherwise eventually the window will | ||
695 | * be full and TRANSPORT will stop providing messages to CORE for @a | ||
696 | * pid. | ||
697 | * | ||
698 | * @param ch core handle | ||
699 | * @param pid which peer was the message from that was fully processed by CORE | ||
700 | */ | ||
701 | void | ||
702 | GNUNET_TRANSPORT_core_receive_continue (struct GNUNET_TRANSPORT_CoreHandle *ch, | ||
703 | const struct GNUNET_PeerIdentity *pid) | ||
704 | { | ||
705 | struct GNUNET_MQ_Envelope *env; | ||
706 | struct RecvOkMessage *rok; | ||
707 | |||
708 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
709 | "Message for %s finished CORE processing, sending RECV_OK.\n", | ||
710 | GNUNET_i2s (pid)); | ||
711 | if (NULL == ch->mq) | ||
712 | return; | ||
713 | env = GNUNET_MQ_msg (rok, GNUNET_MESSAGE_TYPE_TRANSPORT_RECV_OK); | ||
714 | rok->increase_window_delta = htonl (1); | ||
715 | rok->peer = *pid; | ||
716 | GNUNET_MQ_send (ch->mq, env); | ||
717 | } | ||
718 | |||
719 | |||
720 | /** | ||
721 | * Connect to the transport service. Note that the connection may | ||
722 | * complete (or fail) asynchronously. | ||
723 | * | ||
724 | * @param cfg configuration to use | ||
725 | * @param self our own identity (API should check that it matches | ||
726 | * the identity found by transport), or NULL (no check) | ||
727 | * @param cls closure for the callbacks | ||
728 | * @param rec receive function to call | ||
729 | * @param nc function to call on connect events | ||
730 | * @param nd function to call on disconnect events | ||
731 | * @return NULL on error | ||
732 | */ | ||
733 | struct GNUNET_TRANSPORT_CoreHandle * | ||
734 | GNUNET_TRANSPORT_core_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
735 | const struct GNUNET_PeerIdentity *self, | ||
736 | const struct GNUNET_MQ_MessageHandler *handlers, | ||
737 | void *cls, | ||
738 | GNUNET_TRANSPORT_NotifyConnect nc, | ||
739 | GNUNET_TRANSPORT_NotifyDisconnect nd) | ||
740 | { | ||
741 | struct GNUNET_TRANSPORT_CoreHandle *h; | ||
742 | unsigned int i; | ||
743 | |||
744 | h = GNUNET_new (struct GNUNET_TRANSPORT_CoreHandle); | ||
745 | if (NULL != self) | ||
746 | { | ||
747 | h->self = *self; | ||
748 | h->check_self = GNUNET_YES; | ||
749 | } | ||
750 | h->cfg = cfg; | ||
751 | h->cls = cls; | ||
752 | h->nc_cb = nc; | ||
753 | h->nd_cb = nd; | ||
754 | h->reconnect_delay = GNUNET_TIME_UNIT_ZERO; | ||
755 | if (NULL != handlers) | ||
756 | { | ||
757 | for (i = 0; NULL != handlers[i].cb; i++) | ||
758 | ; | ||
759 | h->handlers = GNUNET_new_array (i + 1, struct GNUNET_MQ_MessageHandler); | ||
760 | GNUNET_memcpy (h->handlers, | ||
761 | handlers, | ||
762 | i * sizeof(struct GNUNET_MQ_MessageHandler)); | ||
763 | } | ||
764 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service\n"); | ||
765 | reconnect (h); | ||
766 | if (NULL == h->mq) | ||
767 | { | ||
768 | GNUNET_free (h->handlers); | ||
769 | GNUNET_free (h); | ||
770 | return NULL; | ||
771 | } | ||
772 | h->neighbours = | ||
773 | GNUNET_CONTAINER_multipeermap_create (STARTING_NEIGHBOURS_SIZE, GNUNET_YES); | ||
774 | return h; | ||
775 | } | ||
776 | |||
777 | |||
778 | /** | ||
779 | * Disconnect from the transport service. | ||
780 | * | ||
781 | * @param handle handle to the service as returned from | ||
782 | * #GNUNET_TRANSPORT_core_connect() | ||
783 | */ | ||
784 | void | ||
785 | GNUNET_TRANSPORT_core_disconnect (struct GNUNET_TRANSPORT_CoreHandle *handle) | ||
786 | { | ||
787 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Transport disconnect called!\n"); | ||
788 | /* this disconnects all neighbours... */ | ||
789 | disconnect (handle); | ||
790 | /* and now we stop trying to connect again... */ | ||
791 | if (NULL != handle->reconnect_task) | ||
792 | { | ||
793 | GNUNET_SCHEDULER_cancel (handle->reconnect_task); | ||
794 | handle->reconnect_task = NULL; | ||
795 | } | ||
796 | GNUNET_CONTAINER_multipeermap_destroy (handle->neighbours); | ||
797 | handle->neighbours = NULL; | ||
798 | GNUNET_free (handle->handlers); | ||
799 | handle->handlers = NULL; | ||
800 | GNUNET_free (handle); | ||
801 | } | ||
802 | |||
803 | |||
804 | /* end of transport_api_core.c */ | ||