2 This file is part of GNUnet.
3 Copyright (C) 2009-2013, 2016, 2018 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file transport/transport_api_core.c
23 * @brief library to access the transport service for message exchange
24 * @author Christian Grothoff
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"
35 #define LOG(kind, ...) GNUNET_log_from (kind, "transport-api-core", __VA_ARGS__)
38 * How large to start with for the hashmap of neighbours.
40 #define STARTING_NEIGHBOURS_SIZE 16
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.
47 * FIXME-OPTIMIZE: find out what good values are experimentally,
48 * maybe set adaptively (i.e. to observed available bandwidth).
50 #define SEND_WINDOW_SIZE 4
54 * Entry in hash table of all of our current (connected) neighbours.
59 * Identity of this neighbour.
61 struct GNUNET_PeerIdentity id;
64 * Overall transport handle.
66 struct GNUNET_TRANSPORT_CoreHandle *h;
69 * Active message queue for the peer.
71 struct GNUNET_MQ_Handle *mq;
74 * Envelope with the message we are currently transmitting (or NULL).
76 struct GNUNET_MQ_Envelope *env;
79 * Closure for @e mq handlers.
84 * How many messages can we still send to this peer before we should
87 unsigned int ready_window;
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?
96 int16_t awaiting_done;
99 * Size of the message in @e env.
106 * Handle for the transport service (includes all of the
107 * state for the transport service).
109 struct GNUNET_TRANSPORT_CoreHandle
112 * Closure for the callbacks.
117 * Functions to call for received data (template for
118 * new message queues).
120 struct GNUNET_MQ_MessageHandler *handlers;
123 * function to call on connect events
125 GNUNET_TRANSPORT_NotifyConnect nc_cb;
128 * function to call on disconnect events
130 GNUNET_TRANSPORT_NotifyDisconnect nd_cb;
133 * My client connection to the transport service.
135 struct GNUNET_MQ_Handle *mq;
140 const struct GNUNET_CONFIGURATION_Handle *cfg;
143 * Hash map of the current connected neighbours of this peer.
144 * Maps peer identities to `struct Neighbour` entries.
146 struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
149 * Peer identity as assumed by this process, or all zeros.
151 struct GNUNET_PeerIdentity self;
154 * ID of the task trying to reconnect to the service.
156 struct GNUNET_SCHEDULER_Task *reconnect_task;
159 * Delay until we try to reconnect.
161 struct GNUNET_TIME_Relative reconnect_delay;
164 * Should we check that @e self matches what the service thinks?
165 * (if #GNUNET_NO, then @e self is all zeros!).
172 * Function that will schedule the job that will try
173 * to connect us again to the client.
175 * @param h transport service to reconnect
178 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_CoreHandle *h);
182 * Get the neighbour list entry for the given peer
184 * @param h our context
185 * @param peer peer to look up
186 * @return NULL if no such peer entry exists
188 static struct Neighbour *
189 neighbour_find (struct GNUNET_TRANSPORT_CoreHandle *h,
190 const struct GNUNET_PeerIdentity *peer)
192 return GNUNET_CONTAINER_multipeermap_get (h->neighbours, peer);
197 * Iterator over hash map entries, for deleting state of a neighbour.
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
207 neighbour_delete (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
209 struct GNUNET_TRANSPORT_CoreHandle *handle = cls;
210 struct Neighbour *n = value;
212 LOG (GNUNET_ERROR_TYPE_DEBUG,
213 "Dropping entry for neighbour `%s'.\n",
215 if (NULL != handle->nd_cb)
216 handle->nd_cb (handle->cls, &n->id, n->handlers_cls);
219 GNUNET_MQ_send_cancel (n->env);
222 GNUNET_MQ_destroy (n->mq);
223 GNUNET_assert (NULL == n->mq);
226 GNUNET_CONTAINER_multipeermap_remove (handle->neighbours, key, n));
233 * Generic error handler, called with the appropriate
234 * error code and the same closure specified at the creation of
236 * Not every message queue implementation supports an error handler.
238 * @param cls closure with the `struct GNUNET_TRANSPORT_CoreHandle *`
239 * @param error error code
242 mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
244 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
246 LOG (GNUNET_ERROR_TYPE_DEBUG,
247 "Error receiving from transport service, disconnecting temporarily.\n");
248 disconnect_and_schedule_reconnect (h);
253 * A message from the handler's message queue to a neighbour was
254 * transmitted. Now trigger (possibly delayed) notification of the
255 * neighbour's message queue that we are done and thus ready for
256 * the next message. Note that the MQ being ready is independent
257 * of the send window, as we may queue many messages and simply
258 * not pass them to TRANSPORT if the send window is insufficient.
260 * @param cls the `struct Neighbour` where the message was sent
263 notify_send_done (void *cls)
265 struct Neighbour *n = cls;
267 n->awaiting_done = GNUNET_NO;
269 GNUNET_MQ_impl_send_continue (n->mq);
274 * We have an envelope waiting for transmission at @a n, and
275 * our transmission window is positive. Perform the transmission.
277 * @param n neighbour to perform transmission for
280 do_send (struct Neighbour *n)
282 GNUNET_assert (0 < n->ready_window);
283 GNUNET_assert (NULL != n->env);
285 n->awaiting_done = GNUNET_YES;
286 GNUNET_MQ_notify_sent (n->env, ¬ify_send_done, n);
287 GNUNET_MQ_send (n->h->mq, n->env);
288 LOG (GNUNET_ERROR_TYPE_DEBUG,
289 "Passed message of type %u for neighbour `%s' to TRANSPORT.\n",
290 ntohs (GNUNET_MQ_env_get_msg (n->env)->type),
291 GNUNET_i2s (&n->id));
296 * Implement sending functionality of a message queue.
297 * Called one message at a time. Should send the @a msg
298 * to the transport service and then notify the queue
299 * once we are ready for the next one.
301 * @param mq the message queue
302 * @param msg the message to send
303 * @param impl_state state of the implementation
306 mq_send_impl (struct GNUNET_MQ_Handle *mq,
307 const struct GNUNET_MessageHeader *msg,
310 struct Neighbour *n = impl_state;
311 struct OutboundMessage *obm;
314 msize = ntohs (msg->size);
315 if (msize >= GNUNET_MAX_MESSAGE_SIZE - sizeof(*obm))
318 GNUNET_MQ_impl_send_continue (mq);
321 LOG (GNUNET_ERROR_TYPE_DEBUG,
322 "CORE requested transmission of message of type %u to neighbour `%s'.\n",
324 GNUNET_i2s (&n->id));
326 GNUNET_assert (NULL == n->env);
328 GNUNET_MQ_msg_nested_mh (obm, GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, msg);
329 n->env_size = ntohs (msg->size);
331 struct GNUNET_MQ_Envelope *env;
333 env = GNUNET_MQ_get_current_envelope (mq);
334 obm->priority = htonl ((uint32_t) GNUNET_MQ_env_get_options (env));
337 if (0 == n->ready_window)
339 LOG (GNUNET_ERROR_TYPE_DEBUG,
340 "Flow control delays transmission to CORE until we see SEND_OK.\n");
341 return; /* can't send yet, need to wait for SEND_OK */
348 * Handle destruction of a message queue. Implementations must not
349 * free @a mq, but should take care of @a impl_state.
351 * @param mq the message queue to destroy
352 * @param impl_state state of the implementation
355 mq_destroy_impl (struct GNUNET_MQ_Handle *mq, void *impl_state)
357 struct Neighbour *n = impl_state;
359 GNUNET_assert (mq == n->mq);
365 * Implementation function that cancels the currently sent message.
366 * Should basically undo whatever #mq_send_impl() did.
368 * @param mq message queue
369 * @param impl_state state specific to the implementation
372 mq_cancel_impl (struct GNUNET_MQ_Handle *mq, void *impl_state)
374 struct Neighbour *n = impl_state;
377 if (GNUNET_YES == n->awaiting_done)
379 GNUNET_MQ_send_cancel (n->env);
381 n->awaiting_done = GNUNET_NO;
385 GNUNET_assert (0 == n->ready_window);
392 * We had an error processing a message we forwarded from a peer to
393 * the CORE service. We should just complain about it but otherwise
394 * continue processing.
397 * @param error error code
400 peer_mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
402 /* struct Neighbour *n = cls; */
409 * Function we use for handling incoming connect messages.
411 * @param cls closure, a `struct GNUNET_TRANSPORT_Handle *`
412 * @param cim message received
415 handle_connect (void *cls, const struct ConnectInfoMessage *cim)
417 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
420 LOG (GNUNET_ERROR_TYPE_DEBUG,
421 "Receiving CONNECT message for `%s'\n",
422 GNUNET_i2s (&cim->id));
423 n = neighbour_find (h, &cim->id);
427 disconnect_and_schedule_reconnect (h);
430 n = GNUNET_new (struct Neighbour);
433 n->ready_window = SEND_WINDOW_SIZE;
434 GNUNET_assert (GNUNET_OK ==
435 GNUNET_CONTAINER_multipeermap_put (
439 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
441 n->mq = GNUNET_MQ_queue_for_callbacks (&mq_send_impl,
446 &peer_mq_error_handler,
448 if (NULL != h->nc_cb)
450 n->handlers_cls = h->nc_cb (h->cls, &n->id, n->mq);
451 GNUNET_MQ_set_handlers_closure (n->mq, n->handlers_cls);
457 * Function we use for handling incoming disconnect messages.
459 * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
460 * @param dim message received
463 handle_disconnect (void *cls, const struct DisconnectInfoMessage *dim)
465 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
468 GNUNET_break (ntohl (dim->reserved) == 0);
469 LOG (GNUNET_ERROR_TYPE_DEBUG,
470 "Receiving DISCONNECT message for `%s'.\n",
471 GNUNET_i2s (&dim->peer));
472 n = neighbour_find (h, &dim->peer);
476 disconnect_and_schedule_reconnect (h);
479 GNUNET_assert (GNUNET_YES == neighbour_delete (h, &dim->peer, n));
484 * Function we use for handling incoming send-ok messages.
486 * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
487 * @param okm message received
490 handle_send_ok (void *cls, const struct SendOkMessage *okm)
492 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
495 LOG (GNUNET_ERROR_TYPE_DEBUG,
496 "Receiving SEND_OK message for transmission to %s\n",
497 GNUNET_i2s (&okm->peer));
498 n = neighbour_find (h, &okm->peer);
501 /* We should never get a 'SEND_OK' for a peer that we are not
504 disconnect_and_schedule_reconnect (h);
508 if ((NULL != n->env) && (1 == n->ready_window))
514 * Function we use for checking incoming "inbound" messages.
516 * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
517 * @param im message received
520 check_recv (void *cls, const struct InboundMessage *im)
522 const struct GNUNET_MessageHeader *imm;
525 size = ntohs (im->header.size) - sizeof(*im);
526 if (size < sizeof(struct GNUNET_MessageHeader))
529 return GNUNET_SYSERR;
531 imm = (const struct GNUNET_MessageHeader *) &im[1];
532 if (ntohs (imm->size) != size)
535 return GNUNET_SYSERR;
542 * Function we use for handling incoming messages.
544 * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
545 * @param im message received
548 handle_recv (void *cls, const struct InboundMessage *im)
550 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
551 const struct GNUNET_MessageHeader *imm =
552 (const struct GNUNET_MessageHeader *) &im[1];
555 LOG (GNUNET_ERROR_TYPE_DEBUG,
556 "Received message of type %u with %u bytes from `%s'.\n",
557 (unsigned int) ntohs (imm->type),
558 (unsigned int) ntohs (imm->size),
559 GNUNET_i2s (&im->peer));
560 n = neighbour_find (h, &im->peer);
564 disconnect_and_schedule_reconnect (h);
567 GNUNET_MQ_inject_message (n->mq, imm);
572 * Try again to connect to transport service.
574 * @param cls the handle to the transport service
577 reconnect (void *cls)
579 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
580 struct GNUNET_MQ_MessageHandler handlers[] =
581 { GNUNET_MQ_hd_fixed_size (connect,
582 GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT,
583 struct ConnectInfoMessage,
585 GNUNET_MQ_hd_fixed_size (disconnect,
586 GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT,
587 struct DisconnectInfoMessage,
589 GNUNET_MQ_hd_fixed_size (send_ok,
590 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK,
591 struct SendOkMessage,
593 GNUNET_MQ_hd_var_size (recv,
594 GNUNET_MESSAGE_TYPE_TRANSPORT_RECV,
595 struct InboundMessage,
597 GNUNET_MQ_handler_end () };
598 struct GNUNET_MQ_Envelope *env;
599 struct StartMessage *s;
602 h->reconnect_task = NULL;
603 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n");
604 GNUNET_assert (NULL == h->mq);
606 GNUNET_CLIENT_connect (h->cfg, "transport", handlers, &mq_error_handler, h);
609 env = GNUNET_MQ_msg (s, GNUNET_MESSAGE_TYPE_TRANSPORT_START);
613 if (NULL != h->handlers)
615 s->options = htonl (options);
617 GNUNET_MQ_send (h->mq, env);
622 * Disconnect from the transport service.
624 * @param h transport service to reconnect
627 disconnect (struct GNUNET_TRANSPORT_CoreHandle *h)
629 GNUNET_CONTAINER_multipeermap_iterate (h->neighbours, &neighbour_delete, h);
632 GNUNET_MQ_destroy (h->mq);
639 * Function that will schedule the job that will try
640 * to connect us again to the client.
642 * @param h transport service to reconnect
645 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_CoreHandle *h)
647 GNUNET_assert (NULL == h->reconnect_task);
649 LOG (GNUNET_ERROR_TYPE_DEBUG,
650 "Scheduling task to reconnect to transport service in %s.\n",
651 GNUNET_STRINGS_relative_time_to_string (h->reconnect_delay, GNUNET_YES));
653 GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
654 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
659 * Checks if a given peer is connected to us and get the message queue.
661 * @param handle connection to transport service
662 * @param peer the peer to check
663 * @return NULL if disconnected, otherwise message queue for @a peer
665 struct GNUNET_MQ_Handle *
666 GNUNET_TRANSPORT_core_get_mq (struct GNUNET_TRANSPORT_CoreHandle *handle,
667 const struct GNUNET_PeerIdentity *peer)
671 n = neighbour_find (handle, peer);
679 * Notification from the CORE service to the TRANSPORT service
680 * that the CORE service has finished processing a message from
681 * TRANSPORT (via the @code{handlers} of #GNUNET_TRANSPORT_core_connect())
682 * and that it is thus now OK for TRANSPORT to send more messages
685 * Used to provide flow control, this is our equivalent to
686 * #GNUNET_SERVICE_client_continue() of an ordinary service.
688 * Note that due to the use of a window, TRANSPORT may send multiple
689 * messages destined for the same peer even without an intermediate
690 * call to this function. However, CORE must still call this function
691 * once per message received, as otherwise eventually the window will
692 * be full and TRANSPORT will stop providing messages to CORE for @a
695 * @param ch core handle
696 * @param pid which peer was the message from that was fully processed by CORE
699 GNUNET_TRANSPORT_core_receive_continue (struct GNUNET_TRANSPORT_CoreHandle *ch,
700 const struct GNUNET_PeerIdentity *pid)
702 struct GNUNET_MQ_Envelope *env;
703 struct RecvOkMessage *rok;
705 LOG (GNUNET_ERROR_TYPE_DEBUG,
706 "Message for %s finished CORE processing, sending RECV_OK.\n",
710 env = GNUNET_MQ_msg (rok, GNUNET_MESSAGE_TYPE_TRANSPORT_RECV_OK);
711 rok->increase_window_delta = htonl (1);
713 GNUNET_MQ_send (ch->mq, env);
718 * Connect to the transport service. Note that the connection may
719 * complete (or fail) asynchronously.
721 * @param cfg configuration to use
722 * @param self our own identity (API should check that it matches
723 * the identity found by transport), or NULL (no check)
724 * @param cls closure for the callbacks
725 * @param rec receive function to call
726 * @param nc function to call on connect events
727 * @param nd function to call on disconnect events
728 * @return NULL on error
730 struct GNUNET_TRANSPORT_CoreHandle *
731 GNUNET_TRANSPORT_core_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
732 const struct GNUNET_PeerIdentity *self,
733 const struct GNUNET_MQ_MessageHandler *handlers,
735 GNUNET_TRANSPORT_NotifyConnect nc,
736 GNUNET_TRANSPORT_NotifyDisconnect nd)
738 struct GNUNET_TRANSPORT_CoreHandle *h;
741 h = GNUNET_new (struct GNUNET_TRANSPORT_CoreHandle);
745 h->check_self = GNUNET_YES;
751 h->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
752 if (NULL != handlers)
754 for (i = 0; NULL != handlers[i].cb; i++)
756 h->handlers = GNUNET_new_array (i + 1, struct GNUNET_MQ_MessageHandler);
757 GNUNET_memcpy (h->handlers,
759 i * sizeof(struct GNUNET_MQ_MessageHandler));
761 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service\n");
765 GNUNET_free_non_null (h->handlers);
770 GNUNET_CONTAINER_multipeermap_create (STARTING_NEIGHBOURS_SIZE, GNUNET_YES);
776 * Disconnect from the transport service.
778 * @param handle handle to the service as returned from
779 * #GNUNET_TRANSPORT_core_connect()
782 GNUNET_TRANSPORT_core_disconnect (struct GNUNET_TRANSPORT_CoreHandle *handle)
784 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transport disconnect called!\n");
785 /* this disconnects all neighbours... */
787 /* and now we stop trying to connect again... */
788 if (NULL != handle->reconnect_task)
790 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
791 handle->reconnect_task = NULL;
793 GNUNET_CONTAINER_multipeermap_destroy (handle->neighbours);
794 handle->neighbours = NULL;
795 GNUNET_free_non_null (handle->handlers);
796 handle->handlers = NULL;
797 GNUNET_free (handle);
801 /* end of transport_api_core.c */