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.
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/>.
20 * @file transport/transport_api_core.c
21 * @brief library to access the transport service for message exchange
22 * @author Christian Grothoff
25 #include "gnunet_util_lib.h"
26 #include "gnunet_constants.h"
27 #include "gnunet_arm_service.h"
28 #include "gnunet_hello_lib.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_transport_core_service.h"
31 #include "transport.h"
33 #define LOG(kind,...) GNUNET_log_from (kind, "transport-api-core",__VA_ARGS__)
36 * If we could not send any payload to a peer for this amount of
37 * time, we print a warning.
39 #define UNREADY_WARN_TIME GNUNET_TIME_UNIT_MINUTES
42 * How large to start with for the hashmap of neighbours.
44 #define STARTING_NEIGHBOURS_SIZE 16
48 * Entry in hash table of all of our current (connected) neighbours.
53 * Overall transport handle.
55 struct GNUNET_TRANSPORT_CoreHandle *h;
58 * Active message queue for the peer.
60 struct GNUNET_MQ_Handle *mq;
63 * Envelope with the message we are currently transmitting (or NULL).
65 struct GNUNET_MQ_Envelope *env;
68 * Closure for @e mq handlers.
73 * Identity of this neighbour.
75 struct GNUNET_PeerIdentity id;
78 * Outbound bandwidh tracker.
80 struct GNUNET_BANDWIDTH_Tracker out_tracker;
83 * Entry in our readyness heap (which is sorted by @e next_ready
84 * value). NULL if there is no pending transmission request for
85 * this neighbour or if we're waiting for @e is_ready to become
86 * true AFTER the @e out_tracker suggested that this peer's quota
87 * has been satisfied (so once @e is_ready goes to #GNUNET_YES,
88 * we should immediately go back into the heap).
90 struct GNUNET_CONTAINER_HeapNode *hn;
93 * Task to trigger MQ when we have enough bandwidth for the
96 struct GNUNET_SCHEDULER_Task *timeout_task;
99 * Sending consumed more bytes on wire than payload was announced
100 * This overhead is added to the delay of next sending operation
102 unsigned long long traffic_overhead;
105 * Is this peer currently ready to receive a message?
110 * Size of the message in @e env.
119 * Handle for the transport service (includes all of the
120 * state for the transport service).
122 struct GNUNET_TRANSPORT_CoreHandle
126 * Closure for the callbacks.
131 * Functions to call for received data (template for
132 * new message queues).
134 struct GNUNET_MQ_MessageHandler *handlers;
137 * function to call on connect events
139 GNUNET_TRANSPORT_NotifyConnecT nc_cb;
142 * function to call on disconnect events
144 GNUNET_TRANSPORT_NotifyDisconnecT nd_cb;
147 * function to call on excess bandwidth events
149 GNUNET_TRANSPORT_NotifyExcessBandwidtH neb_cb;
152 * My client connection to the transport service.
154 struct GNUNET_MQ_Handle *mq;
159 const struct GNUNET_CONFIGURATION_Handle *cfg;
162 * Hash map of the current connected neighbours of this peer.
163 * Maps peer identities to `struct Neighbour` entries.
165 struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
168 * Peer identity as assumed by this process, or all zeros.
170 struct GNUNET_PeerIdentity self;
173 * ID of the task trying to reconnect to the service.
175 struct GNUNET_SCHEDULER_Task *reconnect_task;
178 * Delay until we try to reconnect.
180 struct GNUNET_TIME_Relative reconnect_delay;
183 * Should we check that @e self matches what the service thinks?
184 * (if #GNUNET_NO, then @e self is all zeros!).
192 * Function that will schedule the job that will try
193 * to connect us again to the client.
195 * @param h transport service to reconnect
198 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_CoreHandle *h);
202 * Get the neighbour list entry for the given peer
204 * @param h our context
205 * @param peer peer to look up
206 * @return NULL if no such peer entry exists
208 static struct Neighbour *
209 neighbour_find (struct GNUNET_TRANSPORT_CoreHandle *h,
210 const struct GNUNET_PeerIdentity *peer)
212 return GNUNET_CONTAINER_multipeermap_get (h->neighbours,
218 * Function called by the bandwidth tracker if we have excess
221 * @param cls the `struct Neighbour` that has excess bandwidth
224 notify_excess_cb (void *cls)
226 struct Neighbour *n = cls;
227 struct GNUNET_TRANSPORT_CoreHandle *h = n->h;
229 LOG (GNUNET_ERROR_TYPE_DEBUG,
230 "Notifying CORE that more bandwidth is available for %s\n",
231 GNUNET_i2s (&n->id));
233 if (NULL != h->neb_cb)
241 * Iterator over hash map entries, for deleting state of a neighbour.
243 * @param cls the `struct GNUNET_TRANSPORT_CoreHandle *`
244 * @param key peer identity
245 * @param value value in the hash map, the neighbour entry to delete
246 * @return #GNUNET_YES if we should continue to
251 neighbour_delete (void *cls,
252 const struct GNUNET_PeerIdentity *key,
255 struct GNUNET_TRANSPORT_CoreHandle *handle = cls;
256 struct Neighbour *n = value;
258 LOG (GNUNET_ERROR_TYPE_DEBUG,
259 "Dropping entry for neighbour `%s'.\n",
261 GNUNET_BANDWIDTH_tracker_notification_stop (&n->out_tracker);
262 if (NULL != handle->nd_cb)
263 handle->nd_cb (handle->cls,
266 if (NULL != n->timeout_task)
268 GNUNET_SCHEDULER_cancel (n->timeout_task);
269 n->timeout_task = NULL;
273 GNUNET_MQ_send_cancel (n->env);
276 GNUNET_MQ_destroy (n->mq);
277 GNUNET_assert (NULL == n->mq);
278 GNUNET_assert (GNUNET_YES ==
279 GNUNET_CONTAINER_multipeermap_remove (handle->neighbours,
288 * Generic error handler, called with the appropriate
289 * error code and the same closure specified at the creation of
291 * Not every message queue implementation supports an error handler.
293 * @param cls closure with the `struct GNUNET_TRANSPORT_CoreHandle *`
294 * @param error error code
297 mq_error_handler (void *cls,
298 enum GNUNET_MQ_Error error)
300 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
302 LOG (GNUNET_ERROR_TYPE_DEBUG,
303 "Error receiving from transport service, disconnecting temporarily.\n");
304 disconnect_and_schedule_reconnect (h);
309 * Function we use for checking incoming HELLO messages.
311 * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
312 * @param msg message received
313 * @return #GNUNET_OK if message is well-formed
316 check_hello (void *cls,
317 const struct GNUNET_MessageHeader *msg)
319 struct GNUNET_PeerIdentity me;
322 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg,
326 return GNUNET_SYSERR;
333 * Function we use for handling incoming HELLO messages.
335 * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
336 * @param msg message received
339 handle_hello (void *cls,
340 const struct GNUNET_MessageHeader *msg)
342 /* we do not care => FIXME: signal in options to NEVER send HELLOs! */
347 * A message from the handler's message queue to a neighbour was
348 * transmitted. Now trigger (possibly delayed) notification of the
349 * neighbour's message queue that we are done and thus ready for
352 * @param cls the `struct Neighbour` where the message was sent
355 notify_send_done_fin (void *cls)
357 struct Neighbour *n = cls;
359 n->timeout_task = NULL;
360 n->is_ready = GNUNET_YES;
361 GNUNET_MQ_impl_send_continue (n->mq);
366 * A message from the handler's message queue to a neighbour was
367 * transmitted. Now trigger (possibly delayed) notification of the
368 * neighbour's message queue that we are done and thus ready for
371 * @param cls the `struct Neighbour` where the message was sent
374 notify_send_done (void *cls)
376 struct Neighbour *n = cls;
377 struct GNUNET_TIME_Relative delay;
379 n->timeout_task = NULL;
382 GNUNET_BANDWIDTH_tracker_consume (&n->out_tracker,
383 n->env_size + n->traffic_overhead);
385 n->traffic_overhead = 0;
387 delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
389 if (0 == delay.rel_value_us)
391 n->is_ready = GNUNET_YES;
392 GNUNET_MQ_impl_send_continue (n->mq);
395 GNUNET_MQ_impl_send_in_flight (n->mq);
396 /* cannot send even a small message without violating
397 quota, wait a before allowing MQ to send next message */
398 n->timeout_task = GNUNET_SCHEDULER_add_delayed (delay,
399 ¬ify_send_done_fin,
405 * Implement sending functionality of a message queue.
406 * Called one message at a time. Should send the @a msg
407 * to the transport service and then notify the queue
408 * once we are ready for the next one.
410 * @param mq the message queue
411 * @param msg the message to send
412 * @param impl_state state of the implementation
415 mq_send_impl (struct GNUNET_MQ_Handle *mq,
416 const struct GNUNET_MessageHeader *msg,
419 struct Neighbour *n = impl_state;
420 struct GNUNET_TRANSPORT_CoreHandle *h = n->h;
421 struct OutboundMessage *obm;
424 GNUNET_assert (GNUNET_YES == n->is_ready);
425 msize = ntohs (msg->size);
426 if (msize >= GNUNET_MAX_MESSAGE_SIZE - sizeof (*obm))
429 GNUNET_MQ_impl_send_continue (mq);
432 GNUNET_assert (NULL == n->env);
433 n->env = GNUNET_MQ_msg_nested_mh (obm,
434 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND,
436 obm->reserved = htonl (0);
437 obm->timeout = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_MINUTES); /* FIXME: to be removed */
439 GNUNET_assert (NULL == n->timeout_task);
440 n->is_ready = GNUNET_NO;
441 n->env_size = ntohs (msg->size);
442 GNUNET_MQ_notify_sent (n->env,
445 GNUNET_MQ_send (h->mq,
447 LOG (GNUNET_ERROR_TYPE_DEBUG,
448 "Queued message of type %u for neighbour `%s'.\n",
450 GNUNET_i2s (&n->id));
455 * Handle destruction of a message queue. Implementations must not
456 * free @a mq, but should take care of @a impl_state.
458 * @param mq the message queue to destroy
459 * @param impl_state state of the implementation
462 mq_destroy_impl (struct GNUNET_MQ_Handle *mq,
465 struct Neighbour *n = impl_state;
467 GNUNET_assert (mq == n->mq);
473 * Implementation function that cancels the currently sent message.
474 * Should basically undo whatever #mq_send_impl() did.
476 * @param mq message queue
477 * @param impl_state state specific to the implementation
480 mq_cancel_impl (struct GNUNET_MQ_Handle *mq,
483 struct Neighbour *n = impl_state;
485 GNUNET_assert (GNUNET_NO == n->is_ready);
488 GNUNET_MQ_send_cancel (n->env);
492 n->is_ready = GNUNET_YES;
497 * We had an error processing a message we forwarded from a peer to
498 * the CORE service. We should just complain about it but otherwise
499 * continue processing.
502 * @param error error code
505 peer_mq_error_handler (void *cls,
506 enum GNUNET_MQ_Error error)
508 /* struct Neighbour *n = cls; */
515 * The outbound quota has changed in a way that may require
516 * us to reset the timeout. Update the timeout.
518 * @param cls the `struct Neighbour` for which the timeout changed
521 outbound_bw_tracker_update (void *cls)
523 struct Neighbour *n = cls;
524 struct GNUNET_TIME_Relative delay;
526 if (NULL == n->timeout_task)
528 delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
530 GNUNET_SCHEDULER_cancel (n->timeout_task);
531 n->timeout_task = GNUNET_SCHEDULER_add_delayed (delay,
538 * Function we use for handling incoming connect messages.
540 * @param cls closure, a `struct GNUNET_TRANSPORT_Handle *`
541 * @param cim message received
544 handle_connect (void *cls,
545 const struct ConnectInfoMessage *cim)
547 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
550 LOG (GNUNET_ERROR_TYPE_DEBUG,
551 "Receiving CONNECT message for `%s' with quota %u\n",
552 GNUNET_i2s (&cim->id),
553 ntohl (cim->quota_out.value__));
554 n = neighbour_find (h, &cim->id);
558 disconnect_and_schedule_reconnect (h);
561 n = GNUNET_new (struct Neighbour);
564 n->is_ready = GNUNET_YES;
565 n->traffic_overhead = 0;
566 GNUNET_BANDWIDTH_tracker_init2 (&n->out_tracker,
567 &outbound_bw_tracker_update,
569 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
570 MAX_BANDWIDTH_CARRY_S,
573 GNUNET_assert (GNUNET_OK ==
574 GNUNET_CONTAINER_multipeermap_put (h->neighbours,
577 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
579 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker,
581 n->mq = GNUNET_MQ_queue_for_callbacks (&mq_send_impl,
586 &peer_mq_error_handler,
588 if (NULL != h->nc_cb)
590 n->handlers_cls = h->nc_cb (h->cls,
593 GNUNET_MQ_set_handlers_closure (n->mq,
600 * Function we use for handling incoming disconnect messages.
602 * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
603 * @param dim message received
606 handle_disconnect (void *cls,
607 const struct DisconnectInfoMessage *dim)
609 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
612 GNUNET_break (ntohl (dim->reserved) == 0);
613 LOG (GNUNET_ERROR_TYPE_DEBUG,
614 "Receiving DISCONNECT message for `%s'.\n",
615 GNUNET_i2s (&dim->peer));
616 n = neighbour_find (h, &dim->peer);
620 disconnect_and_schedule_reconnect (h);
623 GNUNET_assert (GNUNET_YES ==
631 * Function we use for handling incoming send-ok messages.
633 * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
634 * @param okm message received
637 handle_send_ok (void *cls,
638 const struct SendOkMessage *okm)
640 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
643 uint32_t bytes_physical;
645 bytes_msg = ntohl (okm->bytes_msg);
646 bytes_physical = ntohl (okm->bytes_physical);
647 LOG (GNUNET_ERROR_TYPE_DEBUG,
648 "Receiving SEND_OK message, transmission to %s %s.\n",
649 GNUNET_i2s (&okm->peer),
650 ntohl (okm->success) == GNUNET_OK ? "succeeded" : "failed");
651 n = neighbour_find (h,
655 /* We should never get a 'SEND_OK' for a peer that we are not
658 disconnect_and_schedule_reconnect (h);
661 if (bytes_physical > bytes_msg)
663 LOG (GNUNET_ERROR_TYPE_DEBUG,
664 "Overhead for %u byte message was %u\n",
666 bytes_physical - bytes_msg);
667 n->traffic_overhead += bytes_physical - bytes_msg;
673 * Function we use for checking incoming "inbound" messages.
675 * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
676 * @param im message received
679 check_recv (void *cls,
680 const struct InboundMessage *im)
682 const struct GNUNET_MessageHeader *imm;
685 size = ntohs (im->header.size) - sizeof (*im);
686 if (size < sizeof (struct GNUNET_MessageHeader))
689 return GNUNET_SYSERR;
691 imm = (const struct GNUNET_MessageHeader *) &im[1];
692 if (ntohs (imm->size) != size)
695 return GNUNET_SYSERR;
702 * Function we use for handling incoming messages.
704 * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
705 * @param im message received
708 handle_recv (void *cls,
709 const struct InboundMessage *im)
711 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
712 const struct GNUNET_MessageHeader *imm
713 = (const struct GNUNET_MessageHeader *) &im[1];
716 LOG (GNUNET_ERROR_TYPE_DEBUG,
717 "Received message of type %u with %u bytes from `%s'.\n",
718 (unsigned int) ntohs (imm->type),
719 (unsigned int) ntohs (imm->size),
720 GNUNET_i2s (&im->peer));
721 n = neighbour_find (h, &im->peer);
725 disconnect_and_schedule_reconnect (h);
728 GNUNET_MQ_inject_message (n->mq,
734 * Function we use for handling incoming set quota messages.
736 * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
737 * @param msg message received
740 handle_set_quota (void *cls,
741 const struct QuotaSetMessage *qm)
743 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
746 n = neighbour_find (h,
751 disconnect_and_schedule_reconnect (h);
754 LOG (GNUNET_ERROR_TYPE_DEBUG,
755 "Receiving SET_QUOTA message for `%s' with quota %u\n",
756 GNUNET_i2s (&qm->peer),
757 ntohl (qm->quota.value__));
758 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker,
764 * Try again to connect to transport service.
766 * @param cls the handle to the transport service
769 reconnect (void *cls)
771 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
772 struct GNUNET_MQ_MessageHandler handlers[] = {
773 GNUNET_MQ_hd_var_size (hello,
774 GNUNET_MESSAGE_TYPE_HELLO,
775 struct GNUNET_MessageHeader,
777 GNUNET_MQ_hd_fixed_size (connect,
778 GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT,
779 struct ConnectInfoMessage,
781 GNUNET_MQ_hd_fixed_size (disconnect,
782 GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT,
783 struct DisconnectInfoMessage,
785 GNUNET_MQ_hd_fixed_size (send_ok,
786 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK,
787 struct SendOkMessage,
789 GNUNET_MQ_hd_var_size (recv,
790 GNUNET_MESSAGE_TYPE_TRANSPORT_RECV,
791 struct InboundMessage,
793 GNUNET_MQ_hd_fixed_size (set_quota,
794 GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA,
795 struct QuotaSetMessage,
797 GNUNET_MQ_handler_end ()
799 struct GNUNET_MQ_Envelope *env;
800 struct StartMessage *s;
803 h->reconnect_task = NULL;
804 LOG (GNUNET_ERROR_TYPE_DEBUG,
805 "Connecting to transport service.\n");
806 GNUNET_assert (NULL == h->mq);
807 h->mq = GNUNET_CLIENT_connect (h->cfg,
814 env = GNUNET_MQ_msg (s,
815 GNUNET_MESSAGE_TYPE_TRANSPORT_START);
819 if (NULL != h->handlers)
821 s->options = htonl (options);
823 GNUNET_MQ_send (h->mq,
829 * Function that will schedule the job that will try
830 * to connect us again to the client.
832 * @param h transport service to reconnect
835 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_CoreHandle *h)
837 GNUNET_assert (NULL == h->reconnect_task);
838 /* Forget about all neighbours that we used to be connected to */
839 GNUNET_CONTAINER_multipeermap_iterate (h->neighbours,
844 GNUNET_MQ_destroy (h->mq);
847 LOG (GNUNET_ERROR_TYPE_DEBUG,
848 "Scheduling task to reconnect to transport service in %s.\n",
849 GNUNET_STRINGS_relative_time_to_string (h->reconnect_delay,
852 GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
855 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
860 * Checks if a given peer is connected to us and get the message queue.
862 * @param handle connection to transport service
863 * @param peer the peer to check
864 * @return NULL if disconnected, otherwise message queue for @a peer
866 struct GNUNET_MQ_Handle *
867 GNUNET_TRANSPORT_core_get_mq (struct GNUNET_TRANSPORT_CoreHandle *handle,
868 const struct GNUNET_PeerIdentity *peer)
872 n = neighbour_find (handle,
881 * Connect to the transport service. Note that the connection may
882 * complete (or fail) asynchronously.
884 * @param cfg configuration to use
885 * @param self our own identity (API should check that it matches
886 * the identity found by transport), or NULL (no check)
887 * @param cls closure for the callbacks
888 * @param rec receive function to call
889 * @param nc function to call on connect events
890 * @param nd function to call on disconnect events
891 * @param neb function to call if we have excess bandwidth to a peer
892 * @return NULL on error
894 struct GNUNET_TRANSPORT_CoreHandle *
895 GNUNET_TRANSPORT_core_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
896 const struct GNUNET_PeerIdentity *self,
897 const struct GNUNET_MQ_MessageHandler *handlers,
899 GNUNET_TRANSPORT_NotifyConnecT nc,
900 GNUNET_TRANSPORT_NotifyDisconnecT nd,
901 GNUNET_TRANSPORT_NotifyExcessBandwidtH neb)
903 struct GNUNET_TRANSPORT_CoreHandle *h;
906 h = GNUNET_new (struct GNUNET_TRANSPORT_CoreHandle);
910 h->check_self = GNUNET_YES;
917 h->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
918 if (NULL != handlers)
920 for (i=0;NULL != handlers[i].cb; i++) ;
921 h->handlers = GNUNET_new_array (i + 1,
922 struct GNUNET_MQ_MessageHandler);
923 GNUNET_memcpy (h->handlers,
925 i * sizeof (struct GNUNET_MQ_MessageHandler));
927 LOG (GNUNET_ERROR_TYPE_DEBUG,
928 "Connecting to transport service\n");
932 GNUNET_free_non_null (h->handlers);
937 GNUNET_CONTAINER_multipeermap_create (STARTING_NEIGHBOURS_SIZE,
944 * Disconnect from the transport service.
946 * @param handle handle to the service as returned from #GNUNET_TRANSPORT_core_connect()
949 GNUNET_TRANSPORT_core_disconnect (struct GNUNET_TRANSPORT_CoreHandle *handle)
951 LOG (GNUNET_ERROR_TYPE_DEBUG,
952 "Transport disconnect called!\n");
953 /* this disconnects all neighbours... */
954 if (NULL == handle->reconnect_task)
955 disconnect_and_schedule_reconnect (handle);
956 /* and now we stop trying to connect again... */
957 if (NULL != handle->reconnect_task)
959 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
960 handle->reconnect_task = NULL;
962 GNUNET_CONTAINER_multipeermap_destroy (handle->neighbours);
963 handle->neighbours = NULL;
964 GNUNET_free_non_null (handle->handlers);
965 handle->handlers = NULL;
966 GNUNET_free (handle);
970 /* end of transport_api_core.c */