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/>.
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 * How large to start with for the hashmap of neighbours.
38 #define STARTING_NEIGHBOURS_SIZE 16
42 * Entry in hash table of all of our current (connected) neighbours.
48 * Identity of this neighbour.
50 struct GNUNET_PeerIdentity id;
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 * Entry in our readyness heap (which is sorted by @e next_ready
74 * value). NULL if there is no pending transmission request for
75 * this neighbour or if we're waiting for @e is_ready to become
76 * true AFTER the @e out_tracker suggested that this peer's quota
77 * has been satisfied (so once @e is_ready goes to #GNUNET_YES,
78 * we should immediately go back into the heap).
80 struct GNUNET_CONTAINER_HeapNode *hn;
83 * Task to trigger MQ when we have enough bandwidth for the
86 struct GNUNET_SCHEDULER_Task *timeout_task;
89 * Outbound bandwidh tracker.
91 struct GNUNET_BANDWIDTH_Tracker out_tracker;
94 * Sending consumed more bytes on wire than payload was announced
95 * This overhead is added to the delay of next sending operation
97 unsigned long long traffic_overhead;
100 * Is this peer currently ready to receive a message?
105 * Size of the message in @e env.
114 * Handle for the transport service (includes all of the
115 * state for the transport service).
117 struct GNUNET_TRANSPORT_CoreHandle
121 * Closure for the callbacks.
126 * Functions to call for received data (template for
127 * new message queues).
129 struct GNUNET_MQ_MessageHandler *handlers;
132 * function to call on connect events
134 GNUNET_TRANSPORT_NotifyConnect nc_cb;
137 * function to call on disconnect events
139 GNUNET_TRANSPORT_NotifyDisconnect nd_cb;
142 * function to call on excess bandwidth events
144 GNUNET_TRANSPORT_NotifyExcessBandwidth neb_cb;
147 * My client connection to the transport service.
149 struct GNUNET_MQ_Handle *mq;
154 const struct GNUNET_CONFIGURATION_Handle *cfg;
157 * Hash map of the current connected neighbours of this peer.
158 * Maps peer identities to `struct Neighbour` entries.
160 struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
163 * Peer identity as assumed by this process, or all zeros.
165 struct GNUNET_PeerIdentity self;
168 * ID of the task trying to reconnect to the service.
170 struct GNUNET_SCHEDULER_Task *reconnect_task;
173 * Delay until we try to reconnect.
175 struct GNUNET_TIME_Relative reconnect_delay;
178 * Should we check that @e self matches what the service thinks?
179 * (if #GNUNET_NO, then @e self is all zeros!).
187 * Function that will schedule the job that will try
188 * to connect us again to the client.
190 * @param h transport service to reconnect
193 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_CoreHandle *h);
197 * Get the neighbour list entry for the given peer
199 * @param h our context
200 * @param peer peer to look up
201 * @return NULL if no such peer entry exists
203 static struct Neighbour *
204 neighbour_find (struct GNUNET_TRANSPORT_CoreHandle *h,
205 const struct GNUNET_PeerIdentity *peer)
207 return GNUNET_CONTAINER_multipeermap_get (h->neighbours,
213 * Function called by the bandwidth tracker if we have excess
216 * @param cls the `struct Neighbour` that has excess bandwidth
219 notify_excess_cb (void *cls)
221 struct Neighbour *n = cls;
222 struct GNUNET_TRANSPORT_CoreHandle *h = n->h;
224 LOG (GNUNET_ERROR_TYPE_DEBUG,
225 "Notifying CORE that more bandwidth is available for %s\n",
226 GNUNET_i2s (&n->id));
228 if (NULL != h->neb_cb)
236 * Iterator over hash map entries, for deleting state of a neighbour.
238 * @param cls the `struct GNUNET_TRANSPORT_CoreHandle *`
239 * @param key peer identity
240 * @param value value in the hash map, the neighbour entry to delete
241 * @return #GNUNET_YES if we should continue to
246 neighbour_delete (void *cls,
247 const struct GNUNET_PeerIdentity *key,
250 struct GNUNET_TRANSPORT_CoreHandle *handle = cls;
251 struct Neighbour *n = value;
253 LOG (GNUNET_ERROR_TYPE_DEBUG,
254 "Dropping entry for neighbour `%s'.\n",
256 GNUNET_BANDWIDTH_tracker_notification_stop (&n->out_tracker);
257 if (NULL != handle->nd_cb)
258 handle->nd_cb (handle->cls,
261 if (NULL != n->timeout_task)
263 GNUNET_SCHEDULER_cancel (n->timeout_task);
264 n->timeout_task = NULL;
268 GNUNET_MQ_send_cancel (n->env);
271 GNUNET_MQ_destroy (n->mq);
272 GNUNET_assert (NULL == n->mq);
273 GNUNET_assert (GNUNET_YES ==
274 GNUNET_CONTAINER_multipeermap_remove (handle->neighbours,
283 * Generic error handler, called with the appropriate
284 * error code and the same closure specified at the creation of
286 * Not every message queue implementation supports an error handler.
288 * @param cls closure with the `struct GNUNET_TRANSPORT_CoreHandle *`
289 * @param error error code
292 mq_error_handler (void *cls,
293 enum GNUNET_MQ_Error error)
295 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
297 LOG (GNUNET_ERROR_TYPE_DEBUG,
298 "Error receiving from transport service, disconnecting temporarily.\n");
299 disconnect_and_schedule_reconnect (h);
304 * A message from the handler's message queue to a neighbour was
305 * transmitted. Now trigger (possibly delayed) notification of the
306 * neighbour's message queue that we are done and thus ready for
309 * @param cls the `struct Neighbour` where the message was sent
312 notify_send_done_fin (void *cls)
314 struct Neighbour *n = cls;
316 n->timeout_task = NULL;
317 n->is_ready = GNUNET_YES;
318 GNUNET_MQ_impl_send_continue (n->mq);
323 * A message from the handler's message queue to a neighbour was
324 * transmitted. Now trigger (possibly delayed) notification of the
325 * neighbour's message queue that we are done and thus ready for
328 * @param cls the `struct Neighbour` where the message was sent
331 notify_send_done (void *cls)
333 struct Neighbour *n = cls;
334 struct GNUNET_TIME_Relative delay;
336 n->timeout_task = NULL;
339 GNUNET_BANDWIDTH_tracker_consume (&n->out_tracker,
340 n->env_size + n->traffic_overhead);
342 n->traffic_overhead = 0;
344 delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
346 if (0 == delay.rel_value_us)
348 n->is_ready = GNUNET_YES;
349 GNUNET_MQ_impl_send_continue (n->mq);
352 GNUNET_MQ_impl_send_in_flight (n->mq);
353 /* cannot send even a small message without violating
354 quota, wait a before allowing MQ to send next message */
355 n->timeout_task = GNUNET_SCHEDULER_add_delayed (delay,
356 ¬ify_send_done_fin,
362 * Implement sending functionality of a message queue.
363 * Called one message at a time. Should send the @a msg
364 * to the transport service and then notify the queue
365 * once we are ready for the next one.
367 * @param mq the message queue
368 * @param msg the message to send
369 * @param impl_state state of the implementation
372 mq_send_impl (struct GNUNET_MQ_Handle *mq,
373 const struct GNUNET_MessageHeader *msg,
376 struct Neighbour *n = impl_state;
377 struct GNUNET_TRANSPORT_CoreHandle *h = n->h;
378 struct OutboundMessage *obm;
381 GNUNET_assert (GNUNET_YES == n->is_ready);
382 msize = ntohs (msg->size);
383 if (msize >= GNUNET_MAX_MESSAGE_SIZE - sizeof (*obm))
386 GNUNET_MQ_impl_send_continue (mq);
389 GNUNET_assert (NULL == n->env);
390 n->env = GNUNET_MQ_msg_nested_mh (obm,
391 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND,
393 obm->reserved = htonl (0);
394 obm->timeout = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_MINUTES); /* FIXME: to be removed */
396 GNUNET_assert (NULL == n->timeout_task);
397 n->is_ready = GNUNET_NO;
398 n->env_size = ntohs (msg->size);
399 GNUNET_MQ_notify_sent (n->env,
402 GNUNET_MQ_send (h->mq,
404 LOG (GNUNET_ERROR_TYPE_DEBUG,
405 "Queued message of type %u for neighbour `%s'.\n",
407 GNUNET_i2s (&n->id));
412 * Handle destruction of a message queue. Implementations must not
413 * free @a mq, but should take care of @a impl_state.
415 * @param mq the message queue to destroy
416 * @param impl_state state of the implementation
419 mq_destroy_impl (struct GNUNET_MQ_Handle *mq,
422 struct Neighbour *n = impl_state;
424 GNUNET_assert (mq == n->mq);
430 * Implementation function that cancels the currently sent message.
431 * Should basically undo whatever #mq_send_impl() did.
433 * @param mq message queue
434 * @param impl_state state specific to the implementation
437 mq_cancel_impl (struct GNUNET_MQ_Handle *mq,
440 struct Neighbour *n = impl_state;
442 GNUNET_assert (GNUNET_NO == n->is_ready);
445 GNUNET_MQ_send_cancel (n->env);
449 n->is_ready = GNUNET_YES;
454 * We had an error processing a message we forwarded from a peer to
455 * the CORE service. We should just complain about it but otherwise
456 * continue processing.
459 * @param error error code
462 peer_mq_error_handler (void *cls,
463 enum GNUNET_MQ_Error error)
465 /* struct Neighbour *n = cls; */
472 * The outbound quota has changed in a way that may require
473 * us to reset the timeout. Update the timeout.
475 * @param cls the `struct Neighbour` for which the timeout changed
478 outbound_bw_tracker_update (void *cls)
480 struct Neighbour *n = cls;
481 struct GNUNET_TIME_Relative delay;
483 if (NULL == n->timeout_task)
485 delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
487 GNUNET_SCHEDULER_cancel (n->timeout_task);
488 n->timeout_task = GNUNET_SCHEDULER_add_delayed (delay,
495 * Function we use for handling incoming connect messages.
497 * @param cls closure, a `struct GNUNET_TRANSPORT_Handle *`
498 * @param cim message received
501 handle_connect (void *cls,
502 const struct ConnectInfoMessage *cim)
504 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
507 LOG (GNUNET_ERROR_TYPE_DEBUG,
508 "Receiving CONNECT message for `%s' with quota %u\n",
509 GNUNET_i2s (&cim->id),
510 ntohl (cim->quota_out.value__));
511 n = neighbour_find (h,
516 disconnect_and_schedule_reconnect (h);
519 n = GNUNET_new (struct Neighbour);
522 n->is_ready = GNUNET_YES;
523 n->traffic_overhead = 0;
524 GNUNET_BANDWIDTH_tracker_init2 (&n->out_tracker,
525 &outbound_bw_tracker_update,
527 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
528 MAX_BANDWIDTH_CARRY_S,
531 GNUNET_assert (GNUNET_OK ==
532 GNUNET_CONTAINER_multipeermap_put (h->neighbours,
535 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
537 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker,
539 n->mq = GNUNET_MQ_queue_for_callbacks (&mq_send_impl,
544 &peer_mq_error_handler,
546 if (NULL != h->nc_cb)
548 n->handlers_cls = h->nc_cb (h->cls,
551 GNUNET_MQ_set_handlers_closure (n->mq,
558 * Function we use for handling incoming disconnect messages.
560 * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
561 * @param dim message received
564 handle_disconnect (void *cls,
565 const struct DisconnectInfoMessage *dim)
567 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
570 GNUNET_break (ntohl (dim->reserved) == 0);
571 LOG (GNUNET_ERROR_TYPE_DEBUG,
572 "Receiving DISCONNECT message for `%s'.\n",
573 GNUNET_i2s (&dim->peer));
574 n = neighbour_find (h,
579 disconnect_and_schedule_reconnect (h);
582 GNUNET_assert (GNUNET_YES ==
590 * Function we use for handling incoming send-ok messages.
592 * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
593 * @param okm message received
596 handle_send_ok (void *cls,
597 const struct SendOkMessage *okm)
599 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
602 uint32_t bytes_physical;
604 bytes_msg = ntohl (okm->bytes_msg);
605 bytes_physical = ntohl (okm->bytes_physical);
606 LOG (GNUNET_ERROR_TYPE_DEBUG,
607 "Receiving SEND_OK message, transmission to %s %s.\n",
608 GNUNET_i2s (&okm->peer),
609 (GNUNET_OK == ntohl (okm->success))
612 n = neighbour_find (h,
616 /* We should never get a 'SEND_OK' for a peer that we are not
619 disconnect_and_schedule_reconnect (h);
622 if (bytes_physical > bytes_msg)
624 LOG (GNUNET_ERROR_TYPE_DEBUG,
625 "Overhead for %u byte message was %u\n",
626 (unsigned int) bytes_msg,
627 (unsigned int) (bytes_physical - bytes_msg));
628 n->traffic_overhead += bytes_physical - bytes_msg;
634 * Function we use for checking incoming "inbound" messages.
636 * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
637 * @param im message received
640 check_recv (void *cls,
641 const struct InboundMessage *im)
643 const struct GNUNET_MessageHeader *imm;
646 size = ntohs (im->header.size) - sizeof (*im);
647 if (size < sizeof (struct GNUNET_MessageHeader))
650 return GNUNET_SYSERR;
652 imm = (const struct GNUNET_MessageHeader *) &im[1];
653 if (ntohs (imm->size) != size)
656 return GNUNET_SYSERR;
663 * Function we use for handling incoming messages.
665 * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
666 * @param im message received
669 handle_recv (void *cls,
670 const struct InboundMessage *im)
672 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
673 const struct GNUNET_MessageHeader *imm
674 = (const struct GNUNET_MessageHeader *) &im[1];
677 LOG (GNUNET_ERROR_TYPE_DEBUG,
678 "Received message of type %u with %u bytes from `%s'.\n",
679 (unsigned int) ntohs (imm->type),
680 (unsigned int) ntohs (imm->size),
681 GNUNET_i2s (&im->peer));
682 n = neighbour_find (h,
687 disconnect_and_schedule_reconnect (h);
690 GNUNET_MQ_inject_message (n->mq,
696 * Function we use for handling incoming set quota messages.
698 * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
699 * @param msg message received
702 handle_set_quota (void *cls,
703 const struct QuotaSetMessage *qm)
705 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
708 n = neighbour_find (h,
713 disconnect_and_schedule_reconnect (h);
716 LOG (GNUNET_ERROR_TYPE_DEBUG,
717 "Receiving SET_QUOTA message for `%s' with quota %u\n",
718 GNUNET_i2s (&qm->peer),
719 (unsigned int) ntohl (qm->quota.value__));
720 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker,
726 * Try again to connect to transport service.
728 * @param cls the handle to the transport service
731 reconnect (void *cls)
733 struct GNUNET_TRANSPORT_CoreHandle *h = cls;
734 struct GNUNET_MQ_MessageHandler handlers[] = {
735 GNUNET_MQ_hd_fixed_size (connect,
736 GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT,
737 struct ConnectInfoMessage,
739 GNUNET_MQ_hd_fixed_size (disconnect,
740 GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT,
741 struct DisconnectInfoMessage,
743 GNUNET_MQ_hd_fixed_size (send_ok,
744 GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK,
745 struct SendOkMessage,
747 GNUNET_MQ_hd_var_size (recv,
748 GNUNET_MESSAGE_TYPE_TRANSPORT_RECV,
749 struct InboundMessage,
751 GNUNET_MQ_hd_fixed_size (set_quota,
752 GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA,
753 struct QuotaSetMessage,
755 GNUNET_MQ_handler_end ()
757 struct GNUNET_MQ_Envelope *env;
758 struct StartMessage *s;
761 h->reconnect_task = NULL;
762 LOG (GNUNET_ERROR_TYPE_DEBUG,
763 "Connecting to transport service.\n");
764 GNUNET_assert (NULL == h->mq);
765 h->mq = GNUNET_CLIENT_connect (h->cfg,
772 env = GNUNET_MQ_msg (s,
773 GNUNET_MESSAGE_TYPE_TRANSPORT_START);
777 if (NULL != h->handlers)
779 s->options = htonl (options);
781 GNUNET_MQ_send (h->mq,
787 * Disconnect from the transport service.
789 * @param h transport service to reconnect
792 disconnect (struct GNUNET_TRANSPORT_CoreHandle *h)
794 GNUNET_CONTAINER_multipeermap_iterate (h->neighbours,
799 GNUNET_MQ_destroy (h->mq);
806 * Function that will schedule the job that will try
807 * to connect us again to the client.
809 * @param h transport service to reconnect
812 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_CoreHandle *h)
814 GNUNET_assert (NULL == h->reconnect_task);
816 LOG (GNUNET_ERROR_TYPE_DEBUG,
817 "Scheduling task to reconnect to transport service in %s.\n",
818 GNUNET_STRINGS_relative_time_to_string (h->reconnect_delay,
821 GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
824 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
829 * Checks if a given peer is connected to us and get the message queue.
831 * @param handle connection to transport service
832 * @param peer the peer to check
833 * @return NULL if disconnected, otherwise message queue for @a peer
835 struct GNUNET_MQ_Handle *
836 GNUNET_TRANSPORT_core_get_mq (struct GNUNET_TRANSPORT_CoreHandle *handle,
837 const struct GNUNET_PeerIdentity *peer)
841 n = neighbour_find (handle,
850 * Connect to the transport service. Note that the connection may
851 * complete (or fail) asynchronously.
853 * @param cfg configuration to use
854 * @param self our own identity (API should check that it matches
855 * the identity found by transport), or NULL (no check)
856 * @param cls closure for the callbacks
857 * @param rec receive function to call
858 * @param nc function to call on connect events
859 * @param nd function to call on disconnect events
860 * @param neb function to call if we have excess bandwidth to a peer
861 * @return NULL on error
863 struct GNUNET_TRANSPORT_CoreHandle *
864 GNUNET_TRANSPORT_core_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
865 const struct GNUNET_PeerIdentity *self,
866 const struct GNUNET_MQ_MessageHandler *handlers,
868 GNUNET_TRANSPORT_NotifyConnect nc,
869 GNUNET_TRANSPORT_NotifyDisconnect nd,
870 GNUNET_TRANSPORT_NotifyExcessBandwidth neb)
872 struct GNUNET_TRANSPORT_CoreHandle *h;
875 h = GNUNET_new (struct GNUNET_TRANSPORT_CoreHandle);
879 h->check_self = GNUNET_YES;
886 h->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
887 if (NULL != handlers)
889 for (i=0;NULL != handlers[i].cb; i++) ;
890 h->handlers = GNUNET_new_array (i + 1,
891 struct GNUNET_MQ_MessageHandler);
892 GNUNET_memcpy (h->handlers,
894 i * sizeof (struct GNUNET_MQ_MessageHandler));
896 LOG (GNUNET_ERROR_TYPE_DEBUG,
897 "Connecting to transport service\n");
901 GNUNET_free_non_null (h->handlers);
906 GNUNET_CONTAINER_multipeermap_create (STARTING_NEIGHBOURS_SIZE,
913 * Disconnect from the transport service.
915 * @param handle handle to the service as returned from #GNUNET_TRANSPORT_core_connect()
918 GNUNET_TRANSPORT_core_disconnect (struct GNUNET_TRANSPORT_CoreHandle *handle)
920 LOG (GNUNET_ERROR_TYPE_DEBUG,
921 "Transport disconnect called!\n");
922 /* this disconnects all neighbours... */
924 /* and now we stop trying to connect again... */
925 if (NULL != handle->reconnect_task)
927 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
928 handle->reconnect_task = NULL;
930 GNUNET_CONTAINER_multipeermap_destroy (handle->neighbours);
931 handle->neighbours = NULL;
932 GNUNET_free_non_null (handle->handlers);
933 handle->handlers = NULL;
934 GNUNET_free (handle);
938 /* end of transport_api_core.c */