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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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 * If we could not send any payload to a peer for this amount of
39 * time, we print a warning.
41 #define UNREADY_WARN_TIME GNUNET_TIME_UNIT_MINUTES
44 * How large to start with for the hashmap of neighbours.
46 #define STARTING_NEIGHBOURS_SIZE 16
50 * Entry in hash table of all of our current (connected) neighbours.
55 * Overall transport handle.
57 struct GNUNET_TRANSPORT_CoreHandle *h;
60 * Active message queue for the peer.
62 struct GNUNET_MQ_Handle *mq;
65 * Envelope with the message we are currently transmitting (or NULL).
67 struct GNUNET_MQ_Envelope *env;
70 * Closure for @e mq handlers.
75 * Identity of this neighbour.
77 struct GNUNET_PeerIdentity id;
80 * Outbound bandwidh tracker.
82 struct GNUNET_BANDWIDTH_Tracker out_tracker;
85 * Entry in our readyness heap (which is sorted by @e next_ready
86 * value). NULL if there is no pending transmission request for
87 * this neighbour or if we're waiting for @e is_ready to become
88 * true AFTER the @e out_tracker suggested that this peer's quota
89 * has been satisfied (so once @e is_ready goes to #GNUNET_YES,
90 * we should immediately go back into the heap).
92 struct GNUNET_CONTAINER_HeapNode *hn;
95 * Task to trigger MQ when we have enough bandwidth for the
98 struct GNUNET_SCHEDULER_Task *timeout_task;
101 * Sending consumed more bytes on wire than payload was announced
102 * This overhead is added to the delay of next sending operation
104 unsigned long long traffic_overhead;
107 * Is this peer currently ready to receive a message?
112 * Size of the message in @e env.
121 * Handle for the transport service (includes all of the
122 * state for the transport service).
124 struct GNUNET_TRANSPORT_CoreHandle
128 * Closure for the callbacks.
133 * Functions to call for received data (template for
134 * new message queues).
136 struct GNUNET_MQ_MessageHandler *handlers;
139 * function to call on connect events
141 GNUNET_TRANSPORT_NotifyConnecT nc_cb;
144 * function to call on disconnect events
146 GNUNET_TRANSPORT_NotifyDisconnecT nd_cb;
149 * function to call on excess bandwidth events
151 GNUNET_TRANSPORT_NotifyExcessBandwidtH neb_cb;
154 * My client connection to the transport service.
156 struct GNUNET_MQ_Handle *mq;
161 const struct GNUNET_CONFIGURATION_Handle *cfg;
164 * Hash map of the current connected neighbours of this peer.
165 * Maps peer identities to `struct Neighbour` entries.
167 struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
170 * Peer identity as assumed by this process, or all zeros.
172 struct GNUNET_PeerIdentity self;
175 * ID of the task trying to reconnect to the service.
177 struct GNUNET_SCHEDULER_Task *reconnect_task;
180 * Delay until we try to reconnect.
182 struct GNUNET_TIME_Relative reconnect_delay;
185 * Should we check that @e self matches what the service thinks?
186 * (if #GNUNET_NO, then @e self is all zeros!).
194 * Function that will schedule the job that will try
195 * to connect us again to the client.
197 * @param h transport service to reconnect
200 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_CoreHandle *h);
204 * Get the neighbour list entry for the given peer
206 * @param h our context
207 * @param peer peer to look up
208 * @return NULL if no such peer entry exists
210 static struct Neighbour *
211 neighbour_find (struct GNUNET_TRANSPORT_CoreHandle *h,
212 const struct GNUNET_PeerIdentity *peer)
214 return GNUNET_CONTAINER_multipeermap_get (h->neighbours,
220 * Function called by the bandwidth tracker if we have excess
223 * @param cls the `struct Neighbour` that has excess bandwidth
226 notify_excess_cb (void *cls)
228 struct Neighbour *n = cls;
229 struct GNUNET_TRANSPORT_CoreHandle *h = n->h;
231 LOG (GNUNET_ERROR_TYPE_DEBUG,
232 "Notifying CORE that more bandwidth is available for %s\n",
233 GNUNET_i2s (&n->id));
235 if (NULL != h->neb_cb)
243 * Iterator over hash map entries, for deleting state of a neighbour.
245 * @param cls the `struct GNUNET_TRANSPORT_CoreHandle *`
246 * @param key peer identity
247 * @param value value in the hash map, the neighbour entry to delete
248 * @return #GNUNET_YES if we should continue to
253 neighbour_delete (void *cls,
254 const struct GNUNET_PeerIdentity *key,
257 struct GNUNET_TRANSPORT_CoreHandle *handle = cls;
258 struct Neighbour *n = value;
260 LOG (GNUNET_ERROR_TYPE_DEBUG,
261 "Dropping entry for neighbour `%s'.\n",
263 GNUNET_BANDWIDTH_tracker_notification_stop (&n->out_tracker);
264 if (NULL != handle->nd_cb)
265 handle->nd_cb (handle->cls,
268 if (NULL != n->timeout_task)
270 GNUNET_SCHEDULER_cancel (n->timeout_task);
271 n->timeout_task = NULL;
275 GNUNET_MQ_send_cancel (n->env);
278 GNUNET_MQ_destroy (n->mq);
279 GNUNET_assert (NULL == n->mq);
280 GNUNET_assert (GNUNET_YES ==
281 GNUNET_CONTAINER_multipeermap_remove (handle->neighbours,
290 * Generic error handler, called with the appropriate
291 * error code and the same closure specified at the creation of
293 * Not every message queue implementation supports an error handler.
295 * @param cls closure with the `struct GNUNET_TRANSPORT_CoreHandle *`
296 * @param error error code
299 mq_error_handler (void *cls,
300 enum GNUNET_MQ_Error error)
302 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
304 LOG (GNUNET_ERROR_TYPE_DEBUG,
305 "Error receiving from transport service, disconnecting temporarily.\n");
306 disconnect_and_schedule_reconnect (h);
311 * Function we use for checking incoming HELLO messages.
313 * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
314 * @param msg message received
315 * @return #GNUNET_OK if message is well-formed
318 check_hello (void *cls,
319 const struct GNUNET_MessageHeader *msg)
321 struct GNUNET_PeerIdentity me;
324 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg,
328 return GNUNET_SYSERR;
335 * Function we use for handling incoming HELLO messages.
337 * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
338 * @param msg message received
341 handle_hello (void *cls,
342 const struct GNUNET_MessageHeader *msg)
344 /* we do not care => FIXME: signal in options to NEVER send HELLOs! */
349 * A message from the handler's message queue to a neighbour was
350 * transmitted. Now trigger (possibly delayed) notification of the
351 * neighbour's message queue that we are done and thus ready for
354 * @param cls the `struct Neighbour` where the message was sent
357 notify_send_done_fin (void *cls)
359 struct Neighbour *n = cls;
361 n->timeout_task = NULL;
362 n->is_ready = GNUNET_YES;
363 GNUNET_MQ_impl_send_continue (n->mq);
368 * A message from the handler's message queue to a neighbour was
369 * transmitted. Now trigger (possibly delayed) notification of the
370 * neighbour's message queue that we are done and thus ready for
373 * @param cls the `struct Neighbour` where the message was sent
376 notify_send_done (void *cls)
378 struct Neighbour *n = cls;
379 struct GNUNET_TIME_Relative delay;
381 n->timeout_task = NULL;
384 GNUNET_BANDWIDTH_tracker_consume (&n->out_tracker,
385 n->env_size + n->traffic_overhead);
387 n->traffic_overhead = 0;
389 delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
391 if (0 == delay.rel_value_us)
393 n->is_ready = GNUNET_YES;
394 GNUNET_MQ_impl_send_continue (n->mq);
397 GNUNET_MQ_impl_send_in_flight (n->mq);
398 /* cannot send even a small message without violating
399 quota, wait a before allowing MQ to send next message */
400 n->timeout_task = GNUNET_SCHEDULER_add_delayed (delay,
401 ¬ify_send_done_fin,
407 * Implement sending functionality of a message queue.
408 * Called one message at a time. Should send the @a msg
409 * to the transport service and then notify the queue
410 * once we are ready for the next one.
412 * @param mq the message queue
413 * @param msg the message to send
414 * @param impl_state state of the implementation
417 mq_send_impl (struct GNUNET_MQ_Handle *mq,
418 const struct GNUNET_MessageHeader *msg,
421 struct Neighbour *n = impl_state;
422 struct GNUNET_TRANSPORT_CoreHandle *h = n->h;
423 struct OutboundMessage *obm;
426 GNUNET_assert (GNUNET_YES == n->is_ready);
427 msize = ntohs (msg->size);
428 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (*obm))
431 GNUNET_MQ_impl_send_continue (mq);
434 GNUNET_assert (NULL == n->env);
435 n->env = GNUNET_MQ_msg_nested_mh (obm,
436 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND,
438 obm->reserved = htonl (0);
439 obm->timeout = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_MINUTES); /* FIXME: to be removed */
441 GNUNET_assert (NULL == n->timeout_task);
442 n->is_ready = GNUNET_NO;
443 n->env_size = ntohs (msg->size);
444 GNUNET_MQ_notify_sent (n->env,
447 GNUNET_MQ_send (h->mq,
449 LOG (GNUNET_ERROR_TYPE_DEBUG,
450 "Queued message for neighbour `%s'.\n",
451 GNUNET_i2s (&n->id));
456 * Handle destruction of a message queue. Implementations must not
457 * free @a mq, but should take care of @a impl_state.
459 * @param mq the message queue to destroy
460 * @param impl_state state of the implementation
463 mq_destroy_impl (struct GNUNET_MQ_Handle *mq,
466 struct Neighbour *n = impl_state;
468 GNUNET_assert (mq == n->mq);
474 * Implementation function that cancels the currently sent message.
475 * Should basically undo whatever #mq_send_impl() did.
477 * @param mq message queue
478 * @param impl_state state specific to the implementation
481 mq_cancel_impl (struct GNUNET_MQ_Handle *mq,
484 struct Neighbour *n = impl_state;
486 GNUNET_assert (GNUNET_NO == n->is_ready);
489 GNUNET_MQ_send_cancel (n->env);
493 n->is_ready = GNUNET_YES;
498 * We had an error processing a message we forwarded from a peer to
499 * the CORE service. We should just complain about it but otherwise
500 * continue processing.
503 * @param error error code
506 peer_mq_error_handler (void *cls,
507 enum GNUNET_MQ_Error error)
509 /* struct Neighbour *n = cls; */
516 * The outbound quota has changed in a way that may require
517 * us to reset the timeout. Update the timeout.
519 * @param cls the `struct Neighbour` for which the timeout changed
522 outbound_bw_tracker_update (void *cls)
524 struct Neighbour *n = cls;
525 struct GNUNET_TIME_Relative delay;
527 if (NULL == n->timeout_task)
529 delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
531 GNUNET_SCHEDULER_cancel (n->timeout_task);
532 n->timeout_task = GNUNET_SCHEDULER_add_delayed (delay,
539 * Function we use for handling incoming connect messages.
541 * @param cls closure, a `struct GNUNET_TRANSPORT_Handle *`
542 * @param cim message received
545 handle_connect (void *cls,
546 const struct ConnectInfoMessage *cim)
548 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
551 LOG (GNUNET_ERROR_TYPE_DEBUG,
552 "Receiving CONNECT message for `%s' with quota %u\n",
553 GNUNET_i2s (&cim->id),
554 ntohl (cim->quota_out.value__));
555 n = neighbour_find (h, &cim->id);
559 disconnect_and_schedule_reconnect (h);
562 n = GNUNET_new (struct Neighbour);
565 n->is_ready = GNUNET_YES;
566 n->traffic_overhead = 0;
567 GNUNET_BANDWIDTH_tracker_init2 (&n->out_tracker,
568 &outbound_bw_tracker_update,
570 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
571 MAX_BANDWIDTH_CARRY_S,
574 GNUNET_assert (GNUNET_OK ==
575 GNUNET_CONTAINER_multipeermap_put (h->neighbours,
578 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
580 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker,
582 n->mq = GNUNET_MQ_queue_for_callbacks (&mq_send_impl,
587 &peer_mq_error_handler,
589 if (NULL != h->nc_cb)
591 n->handlers_cls = h->nc_cb (h->cls,
594 GNUNET_MQ_set_handlers_closure (n->mq,
601 * Function we use for handling incoming disconnect messages.
603 * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
604 * @param dim message received
607 handle_disconnect (void *cls,
608 const struct DisconnectInfoMessage *dim)
610 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
613 GNUNET_break (ntohl (dim->reserved) == 0);
614 LOG (GNUNET_ERROR_TYPE_DEBUG,
615 "Receiving DISCONNECT message for `%s'.\n",
616 GNUNET_i2s (&dim->peer));
617 n = neighbour_find (h, &dim->peer);
621 disconnect_and_schedule_reconnect (h);
624 GNUNET_assert (GNUNET_YES ==
632 * Function we use for handling incoming send-ok messages.
634 * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
635 * @param okm message received
638 handle_send_ok (void *cls,
639 const struct SendOkMessage *okm)
641 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
644 uint32_t bytes_physical;
646 bytes_msg = ntohl (okm->bytes_msg);
647 bytes_physical = ntohl (okm->bytes_physical);
648 LOG (GNUNET_ERROR_TYPE_DEBUG,
649 "Receiving SEND_OK message, transmission to %s %s.\n",
650 GNUNET_i2s (&okm->peer),
651 ntohl (okm->success) == GNUNET_OK ? "succeeded" : "failed");
652 n = neighbour_find (h,
656 /* We should never get a 'SEND_OK' for a peer that we are not
659 disconnect_and_schedule_reconnect (h);
662 if (bytes_physical > bytes_msg)
664 LOG (GNUNET_ERROR_TYPE_DEBUG,
665 "Overhead for %u byte message was %u\n",
667 bytes_physical - bytes_msg);
668 n->traffic_overhead += bytes_physical - bytes_msg;
674 * Function we use for checking incoming "inbound" messages.
676 * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
677 * @param im message received
680 check_recv (void *cls,
681 const struct InboundMessage *im)
683 const struct GNUNET_MessageHeader *imm;
686 size = ntohs (im->header.size) - sizeof (*im);
687 if (size < sizeof (struct GNUNET_MessageHeader))
690 return GNUNET_SYSERR;
692 imm = (const struct GNUNET_MessageHeader *) &im[1];
693 if (ntohs (imm->size) != size)
696 return GNUNET_SYSERR;
703 * Function we use for handling incoming messages.
705 * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
706 * @param im message received
709 handle_recv (void *cls,
710 const struct InboundMessage *im)
712 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
713 const struct GNUNET_MessageHeader *imm
714 = (const struct GNUNET_MessageHeader *) &im[1];
717 LOG (GNUNET_ERROR_TYPE_DEBUG,
718 "Received message of type %u with %u bytes from `%s'.\n",
719 (unsigned int) ntohs (imm->type),
720 (unsigned int) ntohs (imm->size),
721 GNUNET_i2s (&im->peer));
722 n = neighbour_find (h, &im->peer);
726 disconnect_and_schedule_reconnect (h);
729 GNUNET_MQ_inject_message (n->mq,
735 * Function we use for handling incoming set quota messages.
737 * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
738 * @param msg message received
741 handle_set_quota (void *cls,
742 const struct QuotaSetMessage *qm)
744 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
747 n = neighbour_find (h,
752 disconnect_and_schedule_reconnect (h);
755 LOG (GNUNET_ERROR_TYPE_DEBUG,
756 "Receiving SET_QUOTA message for `%s' with quota %u\n",
757 GNUNET_i2s (&qm->peer),
758 ntohl (qm->quota.value__));
759 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker,
765 * Try again to connect to transport service.
767 * @param cls the handle to the transport service
770 reconnect (void *cls)
772 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
773 struct GNUNET_MQ_MessageHandler handlers[] = {
774 GNUNET_MQ_hd_var_size (hello,
775 GNUNET_MESSAGE_TYPE_HELLO,
776 struct GNUNET_MessageHeader,
778 GNUNET_MQ_hd_fixed_size (connect,
779 GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT,
780 struct ConnectInfoMessage,
782 GNUNET_MQ_hd_fixed_size (disconnect,
783 GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT,
784 struct DisconnectInfoMessage,
786 GNUNET_MQ_hd_fixed_size (send_ok,
787 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK,
788 struct SendOkMessage,
790 GNUNET_MQ_hd_var_size (recv,
791 GNUNET_MESSAGE_TYPE_TRANSPORT_RECV,
792 struct InboundMessage,
794 GNUNET_MQ_hd_fixed_size (set_quota,
795 GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA,
796 struct QuotaSetMessage,
798 GNUNET_MQ_handler_end ()
800 struct GNUNET_MQ_Envelope *env;
801 struct StartMessage *s;
804 h->reconnect_task = NULL;
805 LOG (GNUNET_ERROR_TYPE_DEBUG,
806 "Connecting to transport service.\n");
807 GNUNET_assert (NULL == h->mq);
808 h->mq = GNUNET_CLIENT_connecT (h->cfg,
815 env = GNUNET_MQ_msg (s,
816 GNUNET_MESSAGE_TYPE_TRANSPORT_START);
820 if (NULL != h->handlers)
822 s->options = htonl (options);
824 GNUNET_MQ_send (h->mq,
830 * Function that will schedule the job that will try
831 * to connect us again to the client.
833 * @param h transport service to reconnect
836 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_CoreHandle *h)
838 GNUNET_assert (NULL == h->reconnect_task);
839 /* Forget about all neighbours that we used to be connected to */
840 GNUNET_CONTAINER_multipeermap_iterate (h->neighbours,
845 GNUNET_MQ_destroy (h->mq);
848 LOG (GNUNET_ERROR_TYPE_DEBUG,
849 "Scheduling task to reconnect to transport service in %s.\n",
850 GNUNET_STRINGS_relative_time_to_string (h->reconnect_delay,
853 GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
856 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
861 * Checks if a given peer is connected to us and get the message queue.
863 * @param handle connection to transport service
864 * @param peer the peer to check
865 * @return NULL if disconnected, otherwise message queue for @a peer
867 struct GNUNET_MQ_Handle *
868 GNUNET_TRANSPORT_core_get_mq (struct GNUNET_TRANSPORT_CoreHandle *handle,
869 const struct GNUNET_PeerIdentity *peer)
873 n = neighbour_find (handle,
882 * Connect to the transport service. Note that the connection may
883 * complete (or fail) asynchronously.
885 * @param cfg configuration to use
886 * @param self our own identity (API should check that it matches
887 * the identity found by transport), or NULL (no check)
888 * @param cls closure for the callbacks
889 * @param rec receive function to call
890 * @param nc function to call on connect events
891 * @param nd function to call on disconnect events
892 * @param neb function to call if we have excess bandwidth to a peer
893 * @return NULL on error
895 struct GNUNET_TRANSPORT_CoreHandle *
896 GNUNET_TRANSPORT_core_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
897 const struct GNUNET_PeerIdentity *self,
898 const struct GNUNET_MQ_MessageHandler *handlers,
900 GNUNET_TRANSPORT_NotifyConnecT nc,
901 GNUNET_TRANSPORT_NotifyDisconnecT nd,
902 GNUNET_TRANSPORT_NotifyExcessBandwidtH neb)
904 struct GNUNET_TRANSPORT_CoreHandle *h;
907 h = GNUNET_new (struct GNUNET_TRANSPORT_CoreHandle);
911 h->check_self = GNUNET_YES;
918 h->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
919 if (NULL != handlers)
921 for (i=0;NULL != handlers[i].cb; i++) ;
922 h->handlers = GNUNET_new_array (i + 1,
923 struct GNUNET_MQ_MessageHandler);
924 GNUNET_memcpy (h->handlers,
926 i * sizeof (struct GNUNET_MQ_MessageHandler));
928 LOG (GNUNET_ERROR_TYPE_DEBUG,
929 "Connecting to transport service\n");
933 GNUNET_free_non_null (h->handlers);
938 GNUNET_CONTAINER_multipeermap_create (STARTING_NEIGHBOURS_SIZE,
945 * Disconnect from the transport service.
947 * @param handle handle to the service as returned from #GNUNET_TRANSPORT_core_connect()
950 GNUNET_TRANSPORT_core_disconnect (struct GNUNET_TRANSPORT_CoreHandle *handle)
952 LOG (GNUNET_ERROR_TYPE_DEBUG,
953 "Transport disconnect called!\n");
954 /* this disconnects all neighbours... */
955 if (NULL == handle->reconnect_task)
956 disconnect_and_schedule_reconnect (handle);
957 /* and now we stop trying to connect again... */
958 if (NULL != handle->reconnect_task)
960 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
961 handle->reconnect_task = NULL;
963 GNUNET_CONTAINER_multipeermap_destroy (handle->neighbours);
964 handle->neighbours = NULL;
965 GNUNET_free_non_null (handle->handlers);
966 handle->handlers = NULL;
967 GNUNET_free (handle);
971 /* end of transport_api_core.c */