2 This file is part of GNUnet.
3 Copyright (C) 2009-2013, 2016 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.
17 * @file transport/transport_api_core.c
18 * @brief library to access the transport service for message exchange
19 * @author Christian Grothoff
22 #include "gnunet_util_lib.h"
23 #include "gnunet_constants.h"
24 #include "gnunet_arm_service.h"
25 #include "gnunet_hello_lib.h"
26 #include "gnunet_protocols.h"
27 #include "gnunet_transport_core_service.h"
28 #include "transport.h"
30 #define LOG(kind,...) GNUNET_log_from (kind, "transport-api-core",__VA_ARGS__)
33 * If we could not send any payload to a peer for this amount of
34 * time, we print a warning.
36 #define UNREADY_WARN_TIME GNUNET_TIME_UNIT_MINUTES
39 * How large to start with for the hashmap of neighbours.
41 #define STARTING_NEIGHBOURS_SIZE 16
45 * Entry in hash table of all of our current (connected) neighbours.
50 * Overall transport handle.
52 struct GNUNET_TRANSPORT_CoreHandle *h;
55 * Active message queue for the peer.
57 struct GNUNET_MQ_Handle *mq;
60 * Envelope with the message we are currently transmitting (or NULL).
62 struct GNUNET_MQ_Envelope *env;
65 * Closure for @e mq handlers.
70 * Identity of this neighbour.
72 struct GNUNET_PeerIdentity id;
75 * Outbound bandwidh tracker.
77 struct GNUNET_BANDWIDTH_Tracker out_tracker;
80 * Entry in our readyness heap (which is sorted by @e next_ready
81 * value). NULL if there is no pending transmission request for
82 * this neighbour or if we're waiting for @e is_ready to become
83 * true AFTER the @e out_tracker suggested that this peer's quota
84 * has been satisfied (so once @e is_ready goes to #GNUNET_YES,
85 * we should immediately go back into the heap).
87 struct GNUNET_CONTAINER_HeapNode *hn;
90 * Task to trigger MQ when we have enough bandwidth for the
93 struct GNUNET_SCHEDULER_Task *timeout_task;
96 * Sending consumed more bytes on wire than payload was announced
97 * This overhead is added to the delay of next sending operation
99 unsigned long long traffic_overhead;
102 * Is this peer currently ready to receive a message?
107 * Size of the message in @e env.
116 * Handle for the transport service (includes all of the
117 * state for the transport service).
119 struct GNUNET_TRANSPORT_CoreHandle
123 * Closure for the callbacks.
128 * Functions to call for received data (template for
129 * new message queues).
131 struct GNUNET_MQ_MessageHandler *handlers;
134 * function to call on connect events
136 GNUNET_TRANSPORT_NotifyConnecT nc_cb;
139 * function to call on disconnect events
141 GNUNET_TRANSPORT_NotifyDisconnecT nd_cb;
144 * function to call on excess bandwidth events
146 GNUNET_TRANSPORT_NotifyExcessBandwidtH neb_cb;
149 * My client connection to the transport service.
151 struct GNUNET_MQ_Handle *mq;
156 const struct GNUNET_CONFIGURATION_Handle *cfg;
159 * Hash map of the current connected neighbours of this peer.
160 * Maps peer identities to `struct Neighbour` entries.
162 struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
165 * Peer identity as assumed by this process, or all zeros.
167 struct GNUNET_PeerIdentity self;
170 * ID of the task trying to reconnect to the service.
172 struct GNUNET_SCHEDULER_Task *reconnect_task;
175 * Delay until we try to reconnect.
177 struct GNUNET_TIME_Relative reconnect_delay;
180 * Should we check that @e self matches what the service thinks?
181 * (if #GNUNET_NO, then @e self is all zeros!).
189 * Function that will schedule the job that will try
190 * to connect us again to the client.
192 * @param h transport service to reconnect
195 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_CoreHandle *h);
199 * Get the neighbour list entry for the given peer
201 * @param h our context
202 * @param peer peer to look up
203 * @return NULL if no such peer entry exists
205 static struct Neighbour *
206 neighbour_find (struct GNUNET_TRANSPORT_CoreHandle *h,
207 const struct GNUNET_PeerIdentity *peer)
209 return GNUNET_CONTAINER_multipeermap_get (h->neighbours,
215 * Function called by the bandwidth tracker if we have excess
218 * @param cls the `struct Neighbour` that has excess bandwidth
221 notify_excess_cb (void *cls)
223 struct Neighbour *n = cls;
224 struct GNUNET_TRANSPORT_CoreHandle *h = n->h;
226 LOG (GNUNET_ERROR_TYPE_DEBUG,
227 "Notifying CORE that more bandwidth is available for %s\n",
228 GNUNET_i2s (&n->id));
230 if (NULL != h->neb_cb)
238 * Iterator over hash map entries, for deleting state of a neighbour.
240 * @param cls the `struct GNUNET_TRANSPORT_CoreHandle *`
241 * @param key peer identity
242 * @param value value in the hash map, the neighbour entry to delete
243 * @return #GNUNET_YES if we should continue to
248 neighbour_delete (void *cls,
249 const struct GNUNET_PeerIdentity *key,
252 struct GNUNET_TRANSPORT_CoreHandle *handle = cls;
253 struct Neighbour *n = value;
255 LOG (GNUNET_ERROR_TYPE_DEBUG,
256 "Dropping entry for neighbour `%s'.\n",
258 GNUNET_BANDWIDTH_tracker_notification_stop (&n->out_tracker);
259 if (NULL != handle->nd_cb)
260 handle->nd_cb (handle->cls,
263 if (NULL != n->timeout_task)
265 GNUNET_SCHEDULER_cancel (n->timeout_task);
266 n->timeout_task = NULL;
270 GNUNET_MQ_send_cancel (n->env);
273 GNUNET_MQ_destroy (n->mq);
274 GNUNET_assert (NULL == n->mq);
275 GNUNET_assert (GNUNET_YES ==
276 GNUNET_CONTAINER_multipeermap_remove (handle->neighbours,
285 * Generic error handler, called with the appropriate
286 * error code and the same closure specified at the creation of
288 * Not every message queue implementation supports an error handler.
290 * @param cls closure with the `struct GNUNET_TRANSPORT_CoreHandle *`
291 * @param error error code
294 mq_error_handler (void *cls,
295 enum GNUNET_MQ_Error error)
297 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
299 LOG (GNUNET_ERROR_TYPE_DEBUG,
300 "Error receiving from transport service, disconnecting temporarily.\n");
301 disconnect_and_schedule_reconnect (h);
306 * Function we use for checking incoming HELLO messages.
308 * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
309 * @param msg message received
310 * @return #GNUNET_OK if message is well-formed
313 check_hello (void *cls,
314 const struct GNUNET_MessageHeader *msg)
316 struct GNUNET_PeerIdentity me;
319 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg,
323 return GNUNET_SYSERR;
330 * Function we use for handling incoming HELLO messages.
332 * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
333 * @param msg message received
336 handle_hello (void *cls,
337 const struct GNUNET_MessageHeader *msg)
339 /* we do not care => FIXME: signal in options to NEVER send HELLOs! */
344 * A message from the handler's message queue to a neighbour was
345 * transmitted. Now trigger (possibly delayed) notification of the
346 * neighbour's message queue that we are done and thus ready for
349 * @param cls the `struct Neighbour` where the message was sent
352 notify_send_done_fin (void *cls)
354 struct Neighbour *n = cls;
356 n->timeout_task = NULL;
357 n->is_ready = GNUNET_YES;
358 GNUNET_MQ_impl_send_continue (n->mq);
363 * A message from the handler's message queue to a neighbour was
364 * transmitted. Now trigger (possibly delayed) notification of the
365 * neighbour's message queue that we are done and thus ready for
368 * @param cls the `struct Neighbour` where the message was sent
371 notify_send_done (void *cls)
373 struct Neighbour *n = cls;
374 struct GNUNET_TIME_Relative delay;
376 n->timeout_task = NULL;
379 GNUNET_BANDWIDTH_tracker_consume (&n->out_tracker,
380 n->env_size + n->traffic_overhead);
382 n->traffic_overhead = 0;
384 delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
386 if (0 == delay.rel_value_us)
388 n->is_ready = GNUNET_YES;
389 GNUNET_MQ_impl_send_continue (n->mq);
392 GNUNET_MQ_impl_send_in_flight (n->mq);
393 /* cannot send even a small message without violating
394 quota, wait a before allowing MQ to send next message */
395 n->timeout_task = GNUNET_SCHEDULER_add_delayed (delay,
396 ¬ify_send_done_fin,
402 * Implement sending functionality of a message queue.
403 * Called one message at a time. Should send the @a msg
404 * to the transport service and then notify the queue
405 * once we are ready for the next one.
407 * @param mq the message queue
408 * @param msg the message to send
409 * @param impl_state state of the implementation
412 mq_send_impl (struct GNUNET_MQ_Handle *mq,
413 const struct GNUNET_MessageHeader *msg,
416 struct Neighbour *n = impl_state;
417 struct GNUNET_TRANSPORT_CoreHandle *h = n->h;
418 struct OutboundMessage *obm;
421 GNUNET_assert (GNUNET_YES == n->is_ready);
422 msize = ntohs (msg->size);
423 if (msize >= GNUNET_MAX_MESSAGE_SIZE - sizeof (*obm))
426 GNUNET_MQ_impl_send_continue (mq);
429 GNUNET_assert (NULL == n->env);
430 n->env = GNUNET_MQ_msg_nested_mh (obm,
431 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND,
433 obm->reserved = htonl (0);
434 obm->timeout = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_MINUTES); /* FIXME: to be removed */
436 GNUNET_assert (NULL == n->timeout_task);
437 n->is_ready = GNUNET_NO;
438 n->env_size = ntohs (msg->size);
439 GNUNET_MQ_notify_sent (n->env,
442 GNUNET_MQ_send (h->mq,
444 LOG (GNUNET_ERROR_TYPE_DEBUG,
445 "Queued message of type %u for neighbour `%s'.\n",
447 GNUNET_i2s (&n->id));
452 * Handle destruction of a message queue. Implementations must not
453 * free @a mq, but should take care of @a impl_state.
455 * @param mq the message queue to destroy
456 * @param impl_state state of the implementation
459 mq_destroy_impl (struct GNUNET_MQ_Handle *mq,
462 struct Neighbour *n = impl_state;
464 GNUNET_assert (mq == n->mq);
470 * Implementation function that cancels the currently sent message.
471 * Should basically undo whatever #mq_send_impl() did.
473 * @param mq message queue
474 * @param impl_state state specific to the implementation
477 mq_cancel_impl (struct GNUNET_MQ_Handle *mq,
480 struct Neighbour *n = impl_state;
482 GNUNET_assert (GNUNET_NO == n->is_ready);
485 GNUNET_MQ_send_cancel (n->env);
489 n->is_ready = GNUNET_YES;
494 * We had an error processing a message we forwarded from a peer to
495 * the CORE service. We should just complain about it but otherwise
496 * continue processing.
499 * @param error error code
502 peer_mq_error_handler (void *cls,
503 enum GNUNET_MQ_Error error)
505 /* struct Neighbour *n = cls; */
512 * The outbound quota has changed in a way that may require
513 * us to reset the timeout. Update the timeout.
515 * @param cls the `struct Neighbour` for which the timeout changed
518 outbound_bw_tracker_update (void *cls)
520 struct Neighbour *n = cls;
521 struct GNUNET_TIME_Relative delay;
523 if (NULL == n->timeout_task)
525 delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
527 GNUNET_SCHEDULER_cancel (n->timeout_task);
528 n->timeout_task = GNUNET_SCHEDULER_add_delayed (delay,
535 * Function we use for handling incoming connect messages.
537 * @param cls closure, a `struct GNUNET_TRANSPORT_Handle *`
538 * @param cim message received
541 handle_connect (void *cls,
542 const struct ConnectInfoMessage *cim)
544 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
547 LOG (GNUNET_ERROR_TYPE_DEBUG,
548 "Receiving CONNECT message for `%s' with quota %u\n",
549 GNUNET_i2s (&cim->id),
550 ntohl (cim->quota_out.value__));
551 n = neighbour_find (h, &cim->id);
555 disconnect_and_schedule_reconnect (h);
558 n = GNUNET_new (struct Neighbour);
561 n->is_ready = GNUNET_YES;
562 n->traffic_overhead = 0;
563 GNUNET_BANDWIDTH_tracker_init2 (&n->out_tracker,
564 &outbound_bw_tracker_update,
566 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
567 MAX_BANDWIDTH_CARRY_S,
570 GNUNET_assert (GNUNET_OK ==
571 GNUNET_CONTAINER_multipeermap_put (h->neighbours,
574 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
576 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker,
578 n->mq = GNUNET_MQ_queue_for_callbacks (&mq_send_impl,
583 &peer_mq_error_handler,
585 if (NULL != h->nc_cb)
587 n->handlers_cls = h->nc_cb (h->cls,
590 GNUNET_MQ_set_handlers_closure (n->mq,
597 * Function we use for handling incoming disconnect messages.
599 * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
600 * @param dim message received
603 handle_disconnect (void *cls,
604 const struct DisconnectInfoMessage *dim)
606 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
609 GNUNET_break (ntohl (dim->reserved) == 0);
610 LOG (GNUNET_ERROR_TYPE_DEBUG,
611 "Receiving DISCONNECT message for `%s'.\n",
612 GNUNET_i2s (&dim->peer));
613 n = neighbour_find (h, &dim->peer);
617 disconnect_and_schedule_reconnect (h);
620 GNUNET_assert (GNUNET_YES ==
628 * Function we use for handling incoming send-ok messages.
630 * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
631 * @param okm message received
634 handle_send_ok (void *cls,
635 const struct SendOkMessage *okm)
637 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
640 uint32_t bytes_physical;
642 bytes_msg = ntohl (okm->bytes_msg);
643 bytes_physical = ntohl (okm->bytes_physical);
644 LOG (GNUNET_ERROR_TYPE_DEBUG,
645 "Receiving SEND_OK message, transmission to %s %s.\n",
646 GNUNET_i2s (&okm->peer),
647 ntohl (okm->success) == GNUNET_OK ? "succeeded" : "failed");
648 n = neighbour_find (h,
652 /* We should never get a 'SEND_OK' for a peer that we are not
655 disconnect_and_schedule_reconnect (h);
658 if (bytes_physical > bytes_msg)
660 LOG (GNUNET_ERROR_TYPE_DEBUG,
661 "Overhead for %u byte message was %u\n",
663 bytes_physical - bytes_msg);
664 n->traffic_overhead += bytes_physical - bytes_msg;
670 * Function we use for checking incoming "inbound" messages.
672 * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
673 * @param im message received
676 check_recv (void *cls,
677 const struct InboundMessage *im)
679 const struct GNUNET_MessageHeader *imm;
682 size = ntohs (im->header.size) - sizeof (*im);
683 if (size < sizeof (struct GNUNET_MessageHeader))
686 return GNUNET_SYSERR;
688 imm = (const struct GNUNET_MessageHeader *) &im[1];
689 if (ntohs (imm->size) != size)
692 return GNUNET_SYSERR;
699 * Function we use for handling incoming messages.
701 * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
702 * @param im message received
705 handle_recv (void *cls,
706 const struct InboundMessage *im)
708 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
709 const struct GNUNET_MessageHeader *imm
710 = (const struct GNUNET_MessageHeader *) &im[1];
713 LOG (GNUNET_ERROR_TYPE_DEBUG,
714 "Received message of type %u with %u bytes from `%s'.\n",
715 (unsigned int) ntohs (imm->type),
716 (unsigned int) ntohs (imm->size),
717 GNUNET_i2s (&im->peer));
718 n = neighbour_find (h, &im->peer);
722 disconnect_and_schedule_reconnect (h);
725 GNUNET_MQ_inject_message (n->mq,
731 * Function we use for handling incoming set quota messages.
733 * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
734 * @param msg message received
737 handle_set_quota (void *cls,
738 const struct QuotaSetMessage *qm)
740 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
743 n = neighbour_find (h,
748 disconnect_and_schedule_reconnect (h);
751 LOG (GNUNET_ERROR_TYPE_DEBUG,
752 "Receiving SET_QUOTA message for `%s' with quota %u\n",
753 GNUNET_i2s (&qm->peer),
754 ntohl (qm->quota.value__));
755 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker,
761 * Try again to connect to transport service.
763 * @param cls the handle to the transport service
766 reconnect (void *cls)
768 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
769 struct GNUNET_MQ_MessageHandler handlers[] = {
770 GNUNET_MQ_hd_var_size (hello,
771 GNUNET_MESSAGE_TYPE_HELLO,
772 struct GNUNET_MessageHeader,
774 GNUNET_MQ_hd_fixed_size (connect,
775 GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT,
776 struct ConnectInfoMessage,
778 GNUNET_MQ_hd_fixed_size (disconnect,
779 GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT,
780 struct DisconnectInfoMessage,
782 GNUNET_MQ_hd_fixed_size (send_ok,
783 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK,
784 struct SendOkMessage,
786 GNUNET_MQ_hd_var_size (recv,
787 GNUNET_MESSAGE_TYPE_TRANSPORT_RECV,
788 struct InboundMessage,
790 GNUNET_MQ_hd_fixed_size (set_quota,
791 GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA,
792 struct QuotaSetMessage,
794 GNUNET_MQ_handler_end ()
796 struct GNUNET_MQ_Envelope *env;
797 struct StartMessage *s;
800 h->reconnect_task = NULL;
801 LOG (GNUNET_ERROR_TYPE_DEBUG,
802 "Connecting to transport service.\n");
803 GNUNET_assert (NULL == h->mq);
804 h->mq = GNUNET_CLIENT_connect (h->cfg,
811 env = GNUNET_MQ_msg (s,
812 GNUNET_MESSAGE_TYPE_TRANSPORT_START);
816 if (NULL != h->handlers)
818 s->options = htonl (options);
820 GNUNET_MQ_send (h->mq,
826 * Function that will schedule the job that will try
827 * to connect us again to the client.
829 * @param h transport service to reconnect
832 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_CoreHandle *h)
834 GNUNET_assert (NULL == h->reconnect_task);
835 /* Forget about all neighbours that we used to be connected to */
836 GNUNET_CONTAINER_multipeermap_iterate (h->neighbours,
841 GNUNET_MQ_destroy (h->mq);
844 LOG (GNUNET_ERROR_TYPE_DEBUG,
845 "Scheduling task to reconnect to transport service in %s.\n",
846 GNUNET_STRINGS_relative_time_to_string (h->reconnect_delay,
849 GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
852 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
857 * Checks if a given peer is connected to us and get the message queue.
859 * @param handle connection to transport service
860 * @param peer the peer to check
861 * @return NULL if disconnected, otherwise message queue for @a peer
863 struct GNUNET_MQ_Handle *
864 GNUNET_TRANSPORT_core_get_mq (struct GNUNET_TRANSPORT_CoreHandle *handle,
865 const struct GNUNET_PeerIdentity *peer)
869 n = neighbour_find (handle,
878 * Connect to the transport service. Note that the connection may
879 * complete (or fail) asynchronously.
881 * @param cfg configuration to use
882 * @param self our own identity (API should check that it matches
883 * the identity found by transport), or NULL (no check)
884 * @param cls closure for the callbacks
885 * @param rec receive function to call
886 * @param nc function to call on connect events
887 * @param nd function to call on disconnect events
888 * @param neb function to call if we have excess bandwidth to a peer
889 * @return NULL on error
891 struct GNUNET_TRANSPORT_CoreHandle *
892 GNUNET_TRANSPORT_core_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
893 const struct GNUNET_PeerIdentity *self,
894 const struct GNUNET_MQ_MessageHandler *handlers,
896 GNUNET_TRANSPORT_NotifyConnecT nc,
897 GNUNET_TRANSPORT_NotifyDisconnecT nd,
898 GNUNET_TRANSPORT_NotifyExcessBandwidtH neb)
900 struct GNUNET_TRANSPORT_CoreHandle *h;
903 h = GNUNET_new (struct GNUNET_TRANSPORT_CoreHandle);
907 h->check_self = GNUNET_YES;
914 h->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
915 if (NULL != handlers)
917 for (i=0;NULL != handlers[i].cb; i++) ;
918 h->handlers = GNUNET_new_array (i + 1,
919 struct GNUNET_MQ_MessageHandler);
920 GNUNET_memcpy (h->handlers,
922 i * sizeof (struct GNUNET_MQ_MessageHandler));
924 LOG (GNUNET_ERROR_TYPE_DEBUG,
925 "Connecting to transport service\n");
929 GNUNET_free_non_null (h->handlers);
934 GNUNET_CONTAINER_multipeermap_create (STARTING_NEIGHBOURS_SIZE,
941 * Disconnect from the transport service.
943 * @param handle handle to the service as returned from #GNUNET_TRANSPORT_core_connect()
946 GNUNET_TRANSPORT_core_disconnect (struct GNUNET_TRANSPORT_CoreHandle *handle)
948 LOG (GNUNET_ERROR_TYPE_DEBUG,
949 "Transport disconnect called!\n");
950 /* this disconnects all neighbours... */
951 if (NULL == handle->reconnect_task)
952 disconnect_and_schedule_reconnect (handle);
953 /* and now we stop trying to connect again... */
954 if (NULL != handle->reconnect_task)
956 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
957 handle->reconnect_task = NULL;
959 GNUNET_CONTAINER_multipeermap_destroy (handle->neighbours);
960 handle->neighbours = NULL;
961 GNUNET_free_non_null (handle->handlers);
962 handle->handlers = NULL;
963 GNUNET_free (handle);
967 /* end of transport_api_core.c */