2 This file is part of GNUnet.
3 Copyright (C) 2009-2013 Christian Grothoff (and other contributing authors)
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.c
23 * @brief library to access the low-level P2P IO service
24 * @author Christian Grothoff
30 #include "gnunet_util_lib.h"
31 #include "gnunet_constants.h"
32 #include "gnunet_arm_service.h"
33 #include "gnunet_hello_lib.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_transport_service.h"
36 #include "transport.h"
38 #define LOG(kind,...) GNUNET_log_from (kind, "transport-api",__VA_ARGS__)
41 * If we could not send any payload to a peer for this amount of
42 * time, we print a warning.
44 #define UNREADY_WARN_TIME GNUNET_TIME_UNIT_MINUTES
47 * How large to start with for the hashmap of neighbours.
49 #define STARTING_NEIGHBOURS_SIZE 16
52 * Handle for a message that should be transmitted to the service.
53 * Used for both control messages and normal messages.
55 struct GNUNET_TRANSPORT_TransmitHandle
59 * We keep all requests in a DLL.
61 struct GNUNET_TRANSPORT_TransmitHandle *next;
64 * We keep all requests in a DLL.
66 struct GNUNET_TRANSPORT_TransmitHandle *prev;
69 * Neighbour for this handle, NULL for control messages.
71 struct Neighbour *neighbour;
74 * Function to call when @e notify_size bytes are available
77 GNUNET_TRANSPORT_TransmitReadyNotify notify;
80 * Closure for @e notify.
85 * Time at which this request was originally scheduled.
87 struct GNUNET_TIME_Absolute request_start;
90 * Timeout for this request, 0 for control messages.
92 struct GNUNET_TIME_Absolute timeout;
95 * Task to trigger request timeout if the request is stalled due to
98 struct GNUNET_SCHEDULER_Task *timeout_task;
101 * How many bytes is our notify callback waiting for?
109 * Entry in hash table of all of our current (connected) neighbours.
114 * Overall transport handle.
116 struct GNUNET_TRANSPORT_Handle *h;
119 * Active transmit handle or NULL.
121 struct GNUNET_TRANSPORT_TransmitHandle *th;
124 * Identity of this neighbour.
126 struct GNUNET_PeerIdentity id;
129 * Outbound bandwidh tracker.
131 struct GNUNET_BANDWIDTH_Tracker out_tracker;
134 * Entry in our readyness heap (which is sorted by @e next_ready
135 * value). NULL if there is no pending transmission request for
136 * this neighbour or if we're waiting for @e is_ready to become
137 * true AFTER the @e out_tracker suggested that this peer's quota
138 * has been satisfied (so once @e is_ready goes to #GNUNET_YES,
139 * we should immediately go back into the heap).
141 struct GNUNET_CONTAINER_HeapNode *hn;
144 * Last time when this peer received payload from us.
146 struct GNUNET_TIME_Absolute last_payload;
149 * Task to trigger warnings if we do not get SEND_OK after a while.
151 struct GNUNET_SCHEDULER_Task *unready_warn_task;
154 * Is this peer currently ready to receive a message?
159 * Sending consumed more bytes on wire than payload was announced
160 * This overhead is added to the delay of next sending operation
162 size_t traffic_overhead;
167 * Linked list of functions to call whenever our HELLO is updated.
169 struct GNUNET_TRANSPORT_GetHelloHandle
173 * This is a doubly linked list.
175 struct GNUNET_TRANSPORT_GetHelloHandle *next;
178 * This is a doubly linked list.
180 struct GNUNET_TRANSPORT_GetHelloHandle *prev;
185 struct GNUNET_TRANSPORT_Handle *handle;
188 * Callback to call once we got our HELLO.
190 GNUNET_TRANSPORT_HelloUpdateCallback rec;
193 * Task for calling the HelloUpdateCallback when we already have a HELLO
195 struct GNUNET_SCHEDULER_Task *notify_task;
198 * Closure for @e rec.
206 * Entry in linked list for a try-connect request.
208 struct GNUNET_TRANSPORT_TryConnectHandle
213 struct GNUNET_TRANSPORT_TryConnectHandle *prev;
218 struct GNUNET_TRANSPORT_TryConnectHandle *next;
221 * Peer we should try to connect to.
223 struct GNUNET_PeerIdentity pid;
226 * Transport service handle this request is part of.
228 struct GNUNET_TRANSPORT_Handle *th;
231 * Message transmission request to communicate to service.
233 struct GNUNET_TRANSPORT_TransmitHandle *tth;
236 * Function to call upon completion (of request transmission).
238 GNUNET_TRANSPORT_TryConnectCallback cb;
249 * Entry in linked list for all try-disconnect requests
251 struct GNUNET_TRANSPORT_TryDisconnectHandle
256 struct GNUNET_TRANSPORT_TryDisconnectHandle *prev;
261 struct GNUNET_TRANSPORT_TryDisconnectHandle *next;
264 * Peer we should try to connect to.
266 struct GNUNET_PeerIdentity pid;
269 * Transport service handle this request is part of.
271 struct GNUNET_TRANSPORT_Handle *th;
274 * Message transmission request to communicate to service.
276 struct GNUNET_TRANSPORT_TransmitHandle *tth;
279 * Function to call upon completion (of request transmission).
281 GNUNET_TRANSPORT_TryDisconnectCallback cb;
292 * Entry in linked list for all offer-HELLO requests.
294 struct GNUNET_TRANSPORT_OfferHelloHandle
299 struct GNUNET_TRANSPORT_OfferHelloHandle *prev;
304 struct GNUNET_TRANSPORT_OfferHelloHandle *next;
307 * Transport service handle we use for transmission.
309 struct GNUNET_TRANSPORT_Handle *th;
312 * Transmission handle for this request.
314 struct GNUNET_TRANSPORT_TransmitHandle *tth;
317 * Function to call once we are done.
319 GNUNET_SCHEDULER_TaskCallback cont;
322 * Closure for @e cont
327 * The HELLO message to be transmitted.
329 struct GNUNET_MessageHeader *msg;
334 * Handle for the transport service (includes all of the
335 * state for the transport service).
337 struct GNUNET_TRANSPORT_Handle
341 * Closure for the callbacks.
346 * Function to call for received data.
348 GNUNET_TRANSPORT_ReceiveCallback rec;
351 * function to call on connect events
353 GNUNET_TRANSPORT_NotifyConnect nc_cb;
356 * function to call on disconnect events
358 GNUNET_TRANSPORT_NotifyDisconnect nd_cb;
361 * function to call on excess bandwidth events
363 GNUNET_TRANSPORT_NotifyExcessBandwidth neb_cb;
366 * Head of DLL of control messages.
368 struct GNUNET_TRANSPORT_TransmitHandle *control_head;
371 * Tail of DLL of control messages.
373 struct GNUNET_TRANSPORT_TransmitHandle *control_tail;
376 * The current HELLO message for this peer. Updated
377 * whenever transports change their addresses.
379 struct GNUNET_MessageHeader *my_hello;
382 * My client connection to the transport service.
384 struct GNUNET_CLIENT_Connection *client;
387 * Handle to our registration with the client for notification.
389 struct GNUNET_CLIENT_TransmitHandle *cth;
392 * Linked list of pending requests for our HELLO.
394 struct GNUNET_TRANSPORT_GetHelloHandle *hwl_head;
397 * Linked list of pending requests for our HELLO.
399 struct GNUNET_TRANSPORT_GetHelloHandle *hwl_tail;
402 * Linked list of pending try connect requests head
404 struct GNUNET_TRANSPORT_TryConnectHandle *tc_head;
407 * Linked list of pending try connect requests tail
409 struct GNUNET_TRANSPORT_TryConnectHandle *tc_tail;
412 * Linked list of pending try disconnect requests head
414 struct GNUNET_TRANSPORT_TryDisconnectHandle *td_head;
417 * Linked list of pending try connect requests tail
419 struct GNUNET_TRANSPORT_TryDisconnectHandle *td_tail;
422 * Linked list of pending offer HELLO requests head
424 struct GNUNET_TRANSPORT_OfferHelloHandle *oh_head;
427 * Linked list of pending offer HELLO requests tail
429 struct GNUNET_TRANSPORT_OfferHelloHandle *oh_tail;
434 const struct GNUNET_CONFIGURATION_Handle *cfg;
437 * Hash map of the current connected neighbours of this peer.
438 * Maps peer identities to `struct Neighbour` entries.
440 struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
443 * Heap sorting peers with pending messages by the timestamps that
444 * specify when we could next send a message to the respective peer.
445 * Excludes control messages (which can always go out immediately).
446 * Maps time stamps to `struct Neighbour` entries.
448 struct GNUNET_CONTAINER_Heap *ready_heap;
451 * Peer identity as assumed by this process, or all zeros.
453 struct GNUNET_PeerIdentity self;
456 * ID of the task trying to reconnect to the service.
458 struct GNUNET_SCHEDULER_Task *reconnect_task;
461 * ID of the task trying to trigger transmission for a peer while
462 * maintaining bandwidth quotas. In use if there are no control
463 * messages and the smallest entry in the @e ready_heap has a time
464 * stamp in the future.
466 struct GNUNET_SCHEDULER_Task *quota_task;
469 * Delay until we try to reconnect.
471 struct GNUNET_TIME_Relative reconnect_delay;
474 * Should we check that @e self matches what the service thinks?
475 * (if #GNUNET_NO, then @e self is all zeros!).
480 * Reconnect in progress
487 * Schedule the task to send one message, either from the control
488 * list or the peer message queues to the service.
490 * @param h transport service to schedule a transmission for
493 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h);
497 * Function that will schedule the job that will try
498 * to connect us again to the client.
500 * @param h transport service to reconnect
503 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h);
507 * A neighbour has not gotten a SEND_OK in a while. Print a warning.
509 * @param cls the `struct Neighbour`
510 * @param tc scheduler context
513 do_warn_unready (void *cls,
514 const struct GNUNET_SCHEDULER_TaskContext *tc)
516 struct Neighbour *n = cls;
517 struct GNUNET_TIME_Relative delay;
519 delay = GNUNET_TIME_absolute_get_duration (n->last_payload);
520 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
521 "Lacking SEND_OK, no payload could be send to %s for %s\n",
523 GNUNET_STRINGS_relative_time_to_string (delay,
526 = GNUNET_SCHEDULER_add_delayed (UNREADY_WARN_TIME,
533 * Get the neighbour list entry for the given peer
535 * @param h our context
536 * @param peer peer to look up
537 * @return NULL if no such peer entry exists
539 static struct Neighbour *
540 neighbour_find (struct GNUNET_TRANSPORT_Handle *h,
541 const struct GNUNET_PeerIdentity *peer)
543 return GNUNET_CONTAINER_multipeermap_get (h->neighbours,
549 * The outbound quota has changed in a way that may require
550 * us to reset the timeout. Update the timeout.
552 * @param cls the `struct Neighbour` for which the timeout changed
555 outbound_bw_tracker_update (void *cls)
557 struct Neighbour *n = cls;
558 struct GNUNET_TIME_Relative delay;
562 delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
563 n->th->notify_size + n->traffic_overhead);
564 LOG (GNUNET_ERROR_TYPE_DEBUG,
565 "New outbound delay %s us\n",
566 GNUNET_STRINGS_relative_time_to_string (delay,
568 GNUNET_CONTAINER_heap_update_cost (n->h->ready_heap,
569 n->hn, delay.rel_value_us);
570 schedule_transmission (n->h);
575 * Function called by the bandwidth tracker if we have excess
578 * @param cls the `struct Neighbour` that has excess bandwidth
581 notify_excess_cb (void *cls)
583 struct Neighbour *n = cls;
584 struct GNUNET_TRANSPORT_Handle *h = n->h;
586 if (NULL != h->neb_cb)
593 * Add neighbour to our list
595 * @return NULL if this API is currently disconnecting from the service
597 static struct Neighbour *
598 neighbour_add (struct GNUNET_TRANSPORT_Handle *h,
599 const struct GNUNET_PeerIdentity *pid)
603 LOG (GNUNET_ERROR_TYPE_DEBUG,
604 "Creating entry for neighbour `%s'.\n",
606 n = GNUNET_new (struct Neighbour);
609 n->is_ready = GNUNET_YES;
610 n->traffic_overhead = 0;
611 GNUNET_BANDWIDTH_tracker_init2 (&n->out_tracker,
612 &outbound_bw_tracker_update,
614 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
615 MAX_BANDWIDTH_CARRY_S,
618 GNUNET_assert (GNUNET_OK ==
619 GNUNET_CONTAINER_multipeermap_put (h->neighbours,
622 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
628 * Iterator over hash map entries, for deleting state of a neighbour.
630 * @param cls the `struct GNUNET_TRANSPORT_Handle *`
631 * @param key peer identity
632 * @param value value in the hash map, the neighbour entry to delete
633 * @return #GNUNET_YES if we should continue to
638 neighbour_delete (void *cls,
639 const struct GNUNET_PeerIdentity *key,
642 struct GNUNET_TRANSPORT_Handle *handle = cls;
643 struct Neighbour *n = value;
645 LOG (GNUNET_ERROR_TYPE_DEBUG,
646 "Dropping entry for neighbour `%s'.\n",
648 GNUNET_BANDWIDTH_tracker_notification_stop (&n->out_tracker);
649 if (NULL != handle->nd_cb)
650 handle->nd_cb (handle->cls,
652 if (NULL != n->unready_warn_task)
654 GNUNET_SCHEDULER_cancel (n->unready_warn_task);
655 n->unready_warn_task = NULL;
657 GNUNET_assert (NULL == n->th);
658 GNUNET_assert (NULL == n->hn);
659 GNUNET_assert (GNUNET_YES ==
660 GNUNET_CONTAINER_multipeermap_remove (handle->neighbours,
669 * Function we use for handling incoming messages.
671 * @param cls closure, a `struct GNUNET_TRANSPORT_Handle *`
672 * @param msg message received, NULL on timeout or fatal error
675 demultiplexer (void *cls,
676 const struct GNUNET_MessageHeader *msg)
678 struct GNUNET_TRANSPORT_Handle *h = cls;
679 const struct DisconnectInfoMessage *dim;
680 const struct ConnectInfoMessage *cim;
681 const struct InboundMessage *im;
682 const struct GNUNET_MessageHeader *imm;
683 const struct SendOkMessage *okm;
684 const struct QuotaSetMessage *qm;
685 struct GNUNET_TRANSPORT_GetHelloHandle *hwl;
686 struct GNUNET_TRANSPORT_GetHelloHandle *next_hwl;
688 struct GNUNET_PeerIdentity me;
691 uint32_t bytes_physical;
693 GNUNET_assert (NULL != h->client);
694 if (GNUNET_YES == h->reconnecting)
700 LOG (GNUNET_ERROR_TYPE_DEBUG,
701 "Error receiving from transport service, disconnecting temporarily.\n");
702 h->reconnecting = GNUNET_YES;
703 disconnect_and_schedule_reconnect (h);
706 GNUNET_CLIENT_receive (h->client,
709 GNUNET_TIME_UNIT_FOREVER_REL);
710 size = ntohs (msg->size);
711 switch (ntohs (msg->type))
713 case GNUNET_MESSAGE_TYPE_HELLO:
715 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg,
721 LOG (GNUNET_ERROR_TYPE_DEBUG,
722 "Receiving (my own) HELLO message (%u bytes), I am `%s'.\n",
725 GNUNET_free_non_null (h->my_hello);
727 if (size < sizeof (struct GNUNET_MessageHeader))
732 h->my_hello = GNUNET_copy_message (msg);
736 next_hwl = hwl->next;
737 hwl->rec (hwl->rec_cls,
742 case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
743 if (size < sizeof (struct ConnectInfoMessage))
748 cim = (const struct ConnectInfoMessage *) msg;
750 sizeof (struct ConnectInfoMessage))
755 LOG (GNUNET_ERROR_TYPE_DEBUG,
756 "Receiving CONNECT message for `%s'.\n",
757 GNUNET_i2s (&cim->id));
758 n = neighbour_find (h, &cim->id);
764 n = neighbour_add (h,
766 LOG (GNUNET_ERROR_TYPE_DEBUG,
767 "Receiving CONNECT message for `%s' with quota %u\n",
768 GNUNET_i2s (&cim->id),
769 ntohl (cim->quota_out.value__));
770 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker,
772 if (NULL != h->nc_cb)
776 case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
777 if (size != sizeof (struct DisconnectInfoMessage))
782 dim = (const struct DisconnectInfoMessage *) msg;
783 GNUNET_break (ntohl (dim->reserved) == 0);
784 LOG (GNUNET_ERROR_TYPE_DEBUG,
785 "Receiving DISCONNECT message for `%s'.\n",
786 GNUNET_i2s (&dim->peer));
787 n = neighbour_find (h, &dim->peer);
797 case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
798 if (size != sizeof (struct SendOkMessage))
803 okm = (const struct SendOkMessage *) msg;
804 bytes_msg = ntohl (okm->bytes_msg);
805 bytes_physical = ntohl (okm->bytes_physical);
806 LOG (GNUNET_ERROR_TYPE_DEBUG,
807 "Receiving SEND_OK message, transmission %s.\n",
808 ntohl (okm->success) == GNUNET_OK ? "succeeded" : "failed");
810 n = neighbour_find (h,
814 /* we should never get a 'SEND_OK' for a peer that we are not
819 if (bytes_physical >= bytes_msg)
821 LOG (GNUNET_ERROR_TYPE_DEBUG,
822 "Overhead for %u byte message: %u\n",
824 bytes_physical - bytes_msg);
825 n->traffic_overhead += bytes_physical - bytes_msg;
827 GNUNET_break (GNUNET_NO == n->is_ready);
828 n->is_ready = GNUNET_YES;
829 if (NULL != n->unready_warn_task)
831 GNUNET_SCHEDULER_cancel (n->unready_warn_task);
832 n->unready_warn_task = NULL;
834 if ((NULL != n->th) && (NULL == n->hn))
836 GNUNET_assert (NULL != n->th->timeout_task);
837 GNUNET_SCHEDULER_cancel (n->th->timeout_task);
838 n->th->timeout_task = NULL;
839 /* we've been waiting for this (congestion, not quota,
840 * caused delayed transmission) */
841 n->hn = GNUNET_CONTAINER_heap_insert (h->ready_heap, n, 0);
842 schedule_transmission (h);
845 case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
847 sizeof (struct InboundMessage) + sizeof (struct GNUNET_MessageHeader))
852 im = (const struct InboundMessage *) msg;
853 imm = (const struct GNUNET_MessageHeader *) &im[1];
854 if (ntohs (imm->size) + sizeof (struct InboundMessage) != size)
859 LOG (GNUNET_ERROR_TYPE_DEBUG,
860 "Received message of type %u from `%s'.\n",
861 ntohs (imm->type), GNUNET_i2s (&im->peer));
862 n = neighbour_find (h, &im->peer);
873 case GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA:
874 if (size != sizeof (struct QuotaSetMessage))
879 qm = (const struct QuotaSetMessage *) msg;
880 n = neighbour_find (h, &qm->peer);
883 LOG (GNUNET_ERROR_TYPE_DEBUG,
884 "Receiving SET_QUOTA message for `%s' with quota %u\n",
885 GNUNET_i2s (&qm->peer),
886 ntohl (qm->quota.value__));
887 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker,
891 LOG (GNUNET_ERROR_TYPE_ERROR,
892 _("Received unexpected message of type %u in %s:%u\n"),
903 * A transmission request could not be satisfied because of
904 * network congestion. Notify the initiator and clean up.
906 * @param cls the `struct GNUNET_TRANSPORT_TransmitHandle`
907 * @param tc scheduler context
910 timeout_request_due_to_congestion (void *cls,
911 const struct GNUNET_SCHEDULER_TaskContext *tc)
913 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
914 struct Neighbour *n = th->neighbour;
915 struct GNUNET_TIME_Relative delay;
917 n->th->timeout_task = NULL;
918 delay = GNUNET_TIME_absolute_get_duration (th->request_start);
919 LOG (GNUNET_ERROR_TYPE_WARNING,
920 "Discarding %u bytes of payload message after %s delay due to congestion\n",
922 GNUNET_STRINGS_relative_time_to_string (delay,
924 GNUNET_assert (th == n->th);
925 GNUNET_assert (NULL == n->hn);
927 th->notify (th->notify_cls,
935 * Transmit message(s) to service.
937 * @param cls handle to transport
938 * @param size number of bytes available in @a buf
939 * @param buf where to copy the message
940 * @return number of bytes copied to @a buf
943 transport_notify_ready (void *cls,
947 struct GNUNET_TRANSPORT_Handle *h = cls;
948 struct GNUNET_TRANSPORT_TransmitHandle *th;
949 struct GNUNET_TIME_Relative delay;
952 struct OutboundMessage obm;
957 GNUNET_assert (NULL != h->client);
961 /* transmission failed */
962 disconnect_and_schedule_reconnect (h);
968 /* first send control messages */
969 while ( (NULL != (th = h->control_head)) &&
970 (th->notify_size <= size) )
972 GNUNET_CONTAINER_DLL_remove (h->control_head,
975 nret = th->notify (th->notify_cls,
978 delay = GNUNET_TIME_absolute_get_duration (th->request_start);
979 if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
980 LOG (GNUNET_ERROR_TYPE_WARNING,
981 "Added %u bytes of control message at %u after %s delay\n",
984 GNUNET_STRINGS_relative_time_to_string (delay,
987 LOG (GNUNET_ERROR_TYPE_DEBUG,
988 "Added %u bytes of control message at %u after %s delay\n",
991 GNUNET_STRINGS_relative_time_to_string (delay,
998 /* then, if possible and no control messages pending, send data messages */
999 while ( (NULL == h->control_head) &&
1000 (NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap))) )
1002 if (GNUNET_YES != n->is_ready)
1004 /* peer not ready, wait for notification! */
1005 GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
1007 GNUNET_assert (NULL == n->th->timeout_task);
1009 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
1011 &timeout_request_due_to_congestion,
1016 if (th->notify_size + sizeof (struct OutboundMessage) > size)
1017 break; /* does not fit */
1018 if (GNUNET_BANDWIDTH_tracker_get_delay
1019 (&n->out_tracker, th->notify_size).rel_value_us > 0)
1020 break; /* too early */
1021 GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
1024 GNUNET_assert (size >= sizeof (struct OutboundMessage));
1025 mret = th->notify (th->notify_cls,
1026 size - sizeof (struct OutboundMessage),
1027 &cbuf[ret + sizeof (struct OutboundMessage)]);
1028 GNUNET_assert (mret <= size - sizeof (struct OutboundMessage));
1034 if (NULL != n->unready_warn_task)
1035 n->unready_warn_task
1036 = GNUNET_SCHEDULER_add_delayed (UNREADY_WARN_TIME,
1039 n->last_payload = GNUNET_TIME_absolute_get ();
1040 n->is_ready = GNUNET_NO;
1041 GNUNET_assert (mret + sizeof (struct OutboundMessage) <
1042 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1043 obm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
1044 obm.header.size = htons (mret + sizeof (struct OutboundMessage));
1045 obm.reserved = htonl (0);
1047 GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining
1052 sizeof (struct OutboundMessage));
1053 ret += (mret + sizeof (struct OutboundMessage));
1054 size -= (mret + sizeof (struct OutboundMessage));
1055 GNUNET_BANDWIDTH_tracker_consume (&n->out_tracker,
1057 delay = GNUNET_TIME_absolute_get_duration (th->request_start);
1058 if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
1059 LOG (GNUNET_ERROR_TYPE_WARNING,
1060 "Added %u bytes of payload message for %s after %s delay at %u b/s\n",
1062 GNUNET_i2s (&n->id),
1063 GNUNET_STRINGS_relative_time_to_string (delay,
1065 (unsigned int) n->out_tracker.available_bytes_per_s__);
1067 LOG (GNUNET_ERROR_TYPE_DEBUG,
1068 "Added %u bytes of payload message for %s after %s delay at %u b/s\n",
1070 GNUNET_i2s (&n->id),
1071 GNUNET_STRINGS_relative_time_to_string (delay,
1073 (unsigned int) n->out_tracker.available_bytes_per_s__);
1077 /* if there are more pending messages, try to schedule those */
1078 schedule_transmission (h);
1079 LOG (GNUNET_ERROR_TYPE_DEBUG,
1080 "Transmitting %u bytes to transport service\n",
1087 * Schedule the task to send one message, either from the control
1088 * list or the peer message queues to the service.
1090 * @param cls transport service to schedule a transmission for
1091 * @param tc scheduler context
1094 schedule_transmission_task (void *cls,
1095 const struct GNUNET_SCHEDULER_TaskContext *tc)
1097 struct GNUNET_TRANSPORT_Handle *h = cls;
1099 struct GNUNET_TRANSPORT_TransmitHandle *th;
1100 struct Neighbour *n;
1102 h->quota_task = NULL;
1103 GNUNET_assert (NULL != h->client);
1104 /* destroy all requests that have timed out */
1105 while ( (NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap))) &&
1106 (0 == GNUNET_TIME_absolute_get_remaining (n->th->timeout).rel_value_us) )
1108 /* notify client that the request could not be satisfied within
1109 * the given time constraints */
1112 GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
1114 LOG (GNUNET_ERROR_TYPE_DEBUG,
1115 "Signalling timeout for transmission to peer %s due to congestion\n",
1116 GNUNET_i2s (&n->id));
1117 GNUNET_assert (0 == th->notify (th->notify_cls,
1124 if (NULL != h->control_head)
1126 size = h->control_head->notify_size;
1130 n = GNUNET_CONTAINER_heap_peek (h->ready_heap);
1132 return; /* no pending messages */
1133 size = n->th->notify_size + sizeof (struct OutboundMessage);
1135 LOG (GNUNET_ERROR_TYPE_DEBUG,
1136 "Calling notify_transmit_ready\n");
1138 = GNUNET_CLIENT_notify_transmit_ready (h->client,
1140 GNUNET_TIME_UNIT_FOREVER_REL,
1142 &transport_notify_ready,
1144 GNUNET_assert (NULL != h->cth);
1149 * Schedule the task to send one message, either from the control
1150 * list or the peer message queues to the service.
1152 * @param h transport service to schedule a transmission for
1155 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
1157 struct GNUNET_TIME_Relative delay;
1158 struct Neighbour *n;
1160 GNUNET_assert (NULL != h->client);
1161 if (NULL != h->quota_task)
1163 GNUNET_SCHEDULER_cancel (h->quota_task);
1164 h->quota_task = NULL;
1166 if (NULL != h->control_head)
1167 delay = GNUNET_TIME_UNIT_ZERO;
1168 else if (NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap)))
1171 GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
1172 n->th->notify_size + n->traffic_overhead);
1173 n->traffic_overhead = 0;
1176 return; /* no work to be done */
1177 LOG (GNUNET_ERROR_TYPE_DEBUG,
1178 "Scheduling next transmission to service in %s\n",
1179 GNUNET_STRINGS_relative_time_to_string (delay,
1182 GNUNET_SCHEDULER_add_delayed (delay,
1183 &schedule_transmission_task,
1189 * Queue control request for transmission to the transport
1192 * @param h handle to the transport service
1193 * @param size number of bytes to be transmitted
1194 * @param notify function to call to get the content
1195 * @param notify_cls closure for @a notify
1196 * @return a `struct GNUNET_TRANSPORT_TransmitHandle`
1198 static struct GNUNET_TRANSPORT_TransmitHandle *
1199 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h,
1201 GNUNET_TRANSPORT_TransmitReadyNotify notify,
1204 struct GNUNET_TRANSPORT_TransmitHandle *th;
1206 LOG (GNUNET_ERROR_TYPE_DEBUG,
1207 "Control transmit of %u bytes requested\n",
1209 th = GNUNET_new (struct GNUNET_TRANSPORT_TransmitHandle);
1210 th->notify = notify;
1211 th->notify_cls = notify_cls;
1212 th->notify_size = size;
1213 th->request_start = GNUNET_TIME_absolute_get ();
1214 GNUNET_CONTAINER_DLL_insert_tail (h->control_head,
1217 schedule_transmission (h);
1223 * Transmit START message to service.
1226 * @param size number of bytes available in @a buf
1227 * @param buf where to copy the message
1228 * @return number of bytes copied to @a buf
1231 send_start (void *cls,
1235 struct GNUNET_TRANSPORT_Handle *h = cls;
1236 struct StartMessage s;
1241 /* Can only be shutdown, just give up */
1242 LOG (GNUNET_ERROR_TYPE_DEBUG,
1243 "Shutdown while trying to transmit START request.\n");
1246 LOG (GNUNET_ERROR_TYPE_DEBUG,
1247 "Transmitting START request.\n");
1248 GNUNET_assert (size >= sizeof (struct StartMessage));
1249 s.header.size = htons (sizeof (struct StartMessage));
1250 s.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
1256 s.options = htonl (options);
1258 memcpy (buf, &s, sizeof (struct StartMessage));
1259 GNUNET_CLIENT_receive (h->client, &demultiplexer, h,
1260 GNUNET_TIME_UNIT_FOREVER_REL);
1261 return sizeof (struct StartMessage);
1266 * Try again to connect to transport service.
1268 * @param cls the handle to the transport service
1269 * @param tc scheduler context
1272 reconnect (void *cls,
1273 const struct GNUNET_SCHEDULER_TaskContext *tc)
1275 struct GNUNET_TRANSPORT_Handle *h = cls;
1277 h->reconnect_task = NULL;
1278 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1280 /* shutdown, just give up */
1283 LOG (GNUNET_ERROR_TYPE_DEBUG,
1284 "Connecting to transport service.\n");
1285 GNUNET_assert (NULL == h->client);
1286 GNUNET_assert (NULL == h->control_head);
1287 GNUNET_assert (NULL == h->control_tail);
1288 h->reconnecting = GNUNET_NO;
1289 h->client = GNUNET_CLIENT_connect ("transport", h->cfg);
1291 GNUNET_assert (NULL != h->client);
1292 schedule_control_transmit (h, sizeof (struct StartMessage),
1298 * Function that will schedule the job that will try
1299 * to connect us again to the client.
1301 * @param h transport service to reconnect
1304 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
1306 struct GNUNET_TRANSPORT_TransmitHandle *th;
1308 GNUNET_assert (h->reconnect_task == NULL);
1311 GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
1314 if (NULL != h->client)
1316 GNUNET_CLIENT_disconnect (h->client);
1318 /* LOG (GNUNET_ERROR_TYPE_ERROR,
1319 "Client disconnect done \n");*/
1321 /* Forget about all neighbours that we used to be connected to */
1322 GNUNET_CONTAINER_multipeermap_iterate (h->neighbours,
1325 if (NULL != h->quota_task)
1327 GNUNET_SCHEDULER_cancel (h->quota_task);
1328 h->quota_task = NULL;
1330 while ((NULL != (th = h->control_head)))
1332 GNUNET_CONTAINER_DLL_remove (h->control_head,
1335 th->notify (th->notify_cls,
1340 LOG (GNUNET_ERROR_TYPE_DEBUG,
1341 "Scheduling task to reconnect to transport service in %s.\n",
1342 GNUNET_STRINGS_relative_time_to_string (h->reconnect_delay,
1345 GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
1348 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
1353 * Cancel control request for transmission to the transport service.
1355 * @param th handle to the transport service
1356 * @param tth transmit handle to cancel
1359 cancel_control_transmit (struct GNUNET_TRANSPORT_Handle *th,
1360 struct GNUNET_TRANSPORT_TransmitHandle *tth)
1362 LOG (GNUNET_ERROR_TYPE_DEBUG,
1363 "Canceling transmit of contral transmission requested\n");
1364 GNUNET_CONTAINER_DLL_remove (th->control_head,
1372 * Send #GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT message to the
1375 * @param cls the `struct GNUNET_TRANSPORT_TryConnectHandle`
1376 * @param size number of bytes available in @a buf
1377 * @param buf where to copy the message
1378 * @return number of bytes copied to @a buf
1381 send_try_connect (void *cls,
1385 struct GNUNET_TRANSPORT_TryConnectHandle *tch = cls;
1386 struct TransportRequestConnectMessage msg;
1391 LOG (GNUNET_ERROR_TYPE_DEBUG,
1392 "Discarding `%s' request to `%s' due to error in transport service connection.\n",
1394 GNUNET_i2s (&tch->pid));
1395 if (NULL != tch->cb)
1396 tch->cb (tch->cb_cls,
1398 GNUNET_TRANSPORT_try_connect_cancel (tch);
1401 LOG (GNUNET_ERROR_TYPE_DEBUG,
1402 "Transmitting `%s' request with respect to `%s'.\n",
1404 GNUNET_i2s (&tch->pid));
1405 GNUNET_assert (size >= sizeof (struct TransportRequestConnectMessage));
1406 msg.header.size = htons (sizeof (struct TransportRequestConnectMessage));
1407 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT);
1408 msg.reserved = htonl (0);
1409 msg.peer = tch->pid;
1410 memcpy (buf, &msg, sizeof (msg));
1411 if (NULL != tch->cb)
1412 tch->cb (tch->cb_cls, GNUNET_OK);
1413 GNUNET_TRANSPORT_try_connect_cancel (tch);
1414 return sizeof (struct TransportRequestConnectMessage);
1419 * Ask the transport service to establish a connection to
1422 * @param handle connection to transport service
1423 * @param target who we should try to connect to
1424 * @param cb callback to be called when request was transmitted to transport
1426 * @param cb_cls closure for the callback
1427 * @return a `struct GNUNET_TRANSPORT_TryConnectHandle` handle or
1428 * NULL on failure (cb will not be called)
1430 struct GNUNET_TRANSPORT_TryConnectHandle *
1431 GNUNET_TRANSPORT_try_connect (struct GNUNET_TRANSPORT_Handle *handle,
1432 const struct GNUNET_PeerIdentity *target,
1433 GNUNET_TRANSPORT_TryConnectCallback cb,
1436 struct GNUNET_TRANSPORT_TryConnectHandle *tch;
1438 if (NULL == handle->client)
1440 tch = GNUNET_new (struct GNUNET_TRANSPORT_TryConnectHandle);
1444 tch->cb_cls = cb_cls;
1445 tch->tth = schedule_control_transmit (handle,
1446 sizeof (struct TransportRequestConnectMessage),
1447 &send_try_connect, tch);
1448 GNUNET_CONTAINER_DLL_insert (handle->tc_head,
1456 * Cancel the request to transport to try a connect
1457 * Callback will not be called
1459 * @param tch the handle to cancel
1462 GNUNET_TRANSPORT_try_connect_cancel (struct GNUNET_TRANSPORT_TryConnectHandle *tch)
1464 struct GNUNET_TRANSPORT_Handle *th;
1467 if (NULL != tch->tth)
1468 cancel_control_transmit (th, tch->tth);
1469 GNUNET_CONTAINER_DLL_remove (th->tc_head,
1477 * Send #GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_DISCONNECT message to the
1480 * @param cls the `struct GNUNET_TRANSPORT_TryDisconnectHandle`
1481 * @param size number of bytes available in @a buf
1482 * @param buf where to copy the message
1483 * @return number of bytes copied to @a buf
1486 send_try_disconnect (void *cls,
1490 struct GNUNET_TRANSPORT_TryDisconnectHandle *tdh = cls;
1491 struct TransportRequestConnectMessage msg;
1496 LOG (GNUNET_ERROR_TYPE_DEBUG,
1497 "Discarding `%s' request to `%s' due to error in transport service connection.\n",
1498 "REQUEST_DISCONNECT",
1499 GNUNET_i2s (&tdh->pid));
1500 if (NULL != tdh->cb)
1501 tdh->cb (tdh->cb_cls,
1503 GNUNET_TRANSPORT_try_disconnect_cancel (tdh);
1506 LOG (GNUNET_ERROR_TYPE_DEBUG,
1507 "Transmitting `%s' request with respect to `%s'.\n",
1508 "REQUEST_DISCONNECT",
1509 GNUNET_i2s (&tdh->pid));
1510 GNUNET_assert (size >= sizeof (struct TransportRequestDisconnectMessage));
1511 msg.header.size = htons (sizeof (struct TransportRequestDisconnectMessage));
1512 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_DISCONNECT);
1513 msg.reserved = htonl (0);
1514 msg.peer = tdh->pid;
1515 memcpy (buf, &msg, sizeof (msg));
1516 if (NULL != tdh->cb)
1517 tdh->cb (tdh->cb_cls, GNUNET_OK);
1518 GNUNET_TRANSPORT_try_disconnect_cancel (tdh);
1519 return sizeof (struct TransportRequestDisconnectMessage);
1524 * Ask the transport service to shutdown a connection to
1527 * @param handle connection to transport service
1528 * @param target who we should try to connect to
1529 * @param cb callback to be called when request was transmitted to transport
1531 * @param cb_cls closure for the callback @a cb
1532 * @return a `struct GNUNET_TRANSPORT_TryDisconnectHandle` handle or
1533 * NULL on failure (cb will not be called)
1535 struct GNUNET_TRANSPORT_TryDisconnectHandle *
1536 GNUNET_TRANSPORT_try_disconnect (struct GNUNET_TRANSPORT_Handle *handle,
1537 const struct GNUNET_PeerIdentity *target,
1538 GNUNET_TRANSPORT_TryDisconnectCallback cb,
1541 struct GNUNET_TRANSPORT_TryDisconnectHandle *tdh;
1543 if (NULL == handle->client)
1545 tdh = GNUNET_new (struct GNUNET_TRANSPORT_TryDisconnectHandle);
1549 tdh->cb_cls = cb_cls;
1550 tdh->tth = schedule_control_transmit (handle,
1551 sizeof (struct TransportRequestDisconnectMessage),
1552 &send_try_disconnect, tdh);
1553 GNUNET_CONTAINER_DLL_insert (handle->td_head,
1561 * Cancel the request to transport to try a disconnect
1562 * Callback will not be called
1564 * @param tdh the handle to cancel
1567 GNUNET_TRANSPORT_try_disconnect_cancel (struct GNUNET_TRANSPORT_TryDisconnectHandle *tdh)
1569 struct GNUNET_TRANSPORT_Handle *th;
1572 if (NULL != tdh->tth)
1573 cancel_control_transmit (th, tdh->tth);
1574 GNUNET_CONTAINER_DLL_remove (th->td_head,
1582 * Send HELLO message to the service.
1584 * @param cls the HELLO message to send
1585 * @param size number of bytes available in @a buf
1586 * @param buf where to copy the message
1587 * @return number of bytes copied to @a buf
1590 send_hello (void *cls,
1594 struct GNUNET_TRANSPORT_OfferHelloHandle *ohh = cls;
1595 struct GNUNET_MessageHeader *msg = ohh->msg;
1597 struct GNUNET_SCHEDULER_TaskContext tc;
1599 tc.read_ready = NULL;
1600 tc.write_ready = NULL;
1601 tc.reason = GNUNET_SCHEDULER_REASON_TIMEOUT;
1604 LOG (GNUNET_ERROR_TYPE_DEBUG,
1605 "Timeout while trying to transmit `%s' request.\n",
1607 if (NULL != ohh->cont)
1608 ohh->cont (ohh->cls,
1611 GNUNET_CONTAINER_DLL_remove (ohh->th->oh_head,
1617 LOG (GNUNET_ERROR_TYPE_DEBUG,
1618 "Transmitting `%s' request.\n",
1620 ssize = ntohs (msg->size);
1621 GNUNET_assert (size >= ssize);
1626 tc.reason = GNUNET_SCHEDULER_REASON_READ_READY;
1627 if (NULL != ohh->cont)
1628 ohh->cont (ohh->cls,
1630 GNUNET_CONTAINER_DLL_remove (ohh->th->oh_head,
1639 * Send traffic metric message to the service.
1641 * @param cls the message to send
1642 * @param size number of bytes available in @a buf
1643 * @param buf where to copy the message
1644 * @return number of bytes copied to @a buf
1647 send_metric (void *cls,
1651 struct TrafficMetricMessage *msg = cls;
1656 LOG (GNUNET_ERROR_TYPE_DEBUG,
1657 "Timeout while trying to transmit TRAFFIC_METRIC request.\n");
1661 LOG (GNUNET_ERROR_TYPE_DEBUG,
1662 "Transmitting TRAFFIC_METRIC request.\n");
1663 ssize = ntohs (msg->header.size);
1664 GNUNET_assert (size >= ssize);
1665 memcpy (buf, msg, ssize);
1672 * Set transport metrics for a peer and a direction
1674 * @param handle transport handle
1675 * @param peer the peer to set the metric for
1676 * @param prop the performance metrics to set
1677 * @param delay_in inbound delay to introduce
1678 * @param delay_out outbound delay to introduce
1680 * Note: Delay restrictions in receiving direction will be enforced
1681 * with one message delay.
1684 GNUNET_TRANSPORT_set_traffic_metric (struct GNUNET_TRANSPORT_Handle *handle,
1685 const struct GNUNET_PeerIdentity *peer,
1686 const struct GNUNET_ATS_Properties *prop,
1687 struct GNUNET_TIME_Relative delay_in,
1688 struct GNUNET_TIME_Relative delay_out)
1690 struct TrafficMetricMessage *msg;
1692 msg = GNUNET_new (struct TrafficMetricMessage);
1693 msg->header.size = htons (sizeof (struct TrafficMetricMessage));
1694 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TRAFFIC_METRIC);
1695 msg->reserved = htonl (0);
1697 GNUNET_ATS_properties_hton (&msg->properties,
1699 msg->delay_in = GNUNET_TIME_relative_hton (delay_in);
1700 msg->delay_out = GNUNET_TIME_relative_hton (delay_out);
1701 schedule_control_transmit (handle,
1702 sizeof (struct TrafficMetricMessage),
1709 * Offer the transport service the HELLO of another peer. Note that
1710 * the transport service may just ignore this message if the HELLO is
1711 * malformed or useless due to our local configuration.
1713 * @param handle connection to transport service
1714 * @param hello the hello message
1715 * @param cont continuation to call when HELLO has been sent,
1716 * tc reason #GNUNET_SCHEDULER_REASON_TIMEOUT for fail
1717 * tc reasong #GNUNET_SCHEDULER_REASON_READ_READY for success
1718 * @param cls closure for continuation
1719 * @return a `struct GNUNET_TRANSPORT_OfferHelloHandle` handle or NULL on failure,
1720 * in case of failure cont will not be called
1723 struct GNUNET_TRANSPORT_OfferHelloHandle *
1724 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
1725 const struct GNUNET_MessageHeader *hello,
1726 GNUNET_SCHEDULER_TaskCallback cont,
1729 struct GNUNET_TRANSPORT_OfferHelloHandle *ohh;
1730 struct GNUNET_MessageHeader *msg;
1731 struct GNUNET_PeerIdentity peer;
1734 if (NULL == handle->client)
1736 GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
1737 size = ntohs (hello->size);
1738 GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
1740 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hello,
1747 msg = GNUNET_malloc (size);
1748 memcpy (msg, hello, size);
1749 LOG (GNUNET_ERROR_TYPE_DEBUG,
1750 "Offering HELLO message of `%s' to transport for validation.\n",
1751 GNUNET_i2s (&peer));
1753 ohh = GNUNET_new (struct GNUNET_TRANSPORT_OfferHelloHandle);
1758 ohh->tth = schedule_control_transmit (handle, size,
1760 GNUNET_CONTAINER_DLL_insert (handle->oh_head, handle->oh_tail, ohh);
1766 * Cancel the request to transport to offer the HELLO message
1768 * @param ohh the handle for the operation to cancel
1771 GNUNET_TRANSPORT_offer_hello_cancel (struct GNUNET_TRANSPORT_OfferHelloHandle *ohh)
1773 struct GNUNET_TRANSPORT_Handle *th = ohh->th;
1775 cancel_control_transmit (ohh->th, ohh->tth);
1776 GNUNET_CONTAINER_DLL_remove (th->oh_head,
1779 GNUNET_free (ohh->msg);
1785 * Checks if a given peer is connected to us
1787 * @param handle connection to transport service
1788 * @param peer the peer to check
1789 * @return #GNUNET_YES (connected) or #GNUNET_NO (disconnected)
1792 GNUNET_TRANSPORT_check_peer_connected (struct GNUNET_TRANSPORT_Handle *handle,
1793 const struct GNUNET_PeerIdentity *peer)
1796 GNUNET_CONTAINER_multipeermap_contains (handle->neighbours,
1804 * Task to call the HelloUpdateCallback of the GetHelloHandle
1806 * @param cls the `struct GNUNET_TRANSPORT_GetHelloHandle`
1807 * @param tc the scheduler task context
1810 call_hello_update_cb_async (void *cls,
1811 const struct GNUNET_SCHEDULER_TaskContext *tc)
1813 struct GNUNET_TRANSPORT_GetHelloHandle *ghh = cls;
1815 GNUNET_assert (NULL != ghh->handle->my_hello);
1816 GNUNET_assert (NULL != ghh->notify_task);
1817 ghh->notify_task = NULL;
1818 ghh->rec (ghh->rec_cls,
1819 ghh->handle->my_hello);
1824 * Obtain the HELLO message for this peer. The callback given in this function
1825 * is never called synchronously.
1827 * @param handle connection to transport service
1828 * @param rec function to call with the HELLO, sender will be our peer
1829 * identity; message and sender will be NULL on timeout
1830 * (handshake with transport service pending/failed).
1831 * cost estimate will be 0.
1832 * @param rec_cls closure for @a rec
1833 * @return handle to cancel the operation
1835 struct GNUNET_TRANSPORT_GetHelloHandle *
1836 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
1837 GNUNET_TRANSPORT_HelloUpdateCallback rec,
1840 struct GNUNET_TRANSPORT_GetHelloHandle *hwl;
1842 hwl = GNUNET_new (struct GNUNET_TRANSPORT_GetHelloHandle);
1844 hwl->rec_cls = rec_cls;
1845 hwl->handle = handle;
1846 GNUNET_CONTAINER_DLL_insert (handle->hwl_head,
1849 if (NULL != handle->my_hello)
1850 hwl->notify_task = GNUNET_SCHEDULER_add_now (&call_hello_update_cb_async,
1857 * Stop receiving updates about changes to our HELLO message.
1859 * @param ghh handle to cancel
1862 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_GetHelloHandle *ghh)
1864 struct GNUNET_TRANSPORT_Handle *handle = ghh->handle;
1866 if (NULL != ghh->notify_task)
1867 GNUNET_SCHEDULER_cancel (ghh->notify_task);
1868 GNUNET_CONTAINER_DLL_remove (handle->hwl_head, handle->hwl_tail, ghh);
1874 * Connect to the transport service. Note that the connection may
1875 * complete (or fail) asynchronously.
1877 * @param cfg configuration to use
1878 * @param self our own identity (API should check that it matches
1879 * the identity found by transport), or NULL (no check)
1880 * @param cls closure for the callbacks
1881 * @param rec receive function to call
1882 * @param nc function to call on connect events
1883 * @param nd function to call on disconnect events
1884 * @return NULL on error
1886 struct GNUNET_TRANSPORT_Handle *
1887 GNUNET_TRANSPORT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1888 const struct GNUNET_PeerIdentity *self,
1890 GNUNET_TRANSPORT_ReceiveCallback rec,
1891 GNUNET_TRANSPORT_NotifyConnect nc,
1892 GNUNET_TRANSPORT_NotifyDisconnect nd)
1894 return GNUNET_TRANSPORT_connect2 (cfg,
1905 * Connect to the transport service. Note that the connection may
1906 * complete (or fail) asynchronously.
1908 * @param cfg configuration to use
1909 * @param self our own identity (API should check that it matches
1910 * the identity found by transport), or NULL (no check)
1911 * @param cls closure for the callbacks
1912 * @param rec receive function to call
1913 * @param nc function to call on connect events
1914 * @param nd function to call on disconnect events
1915 * @param neb function to call if we have excess bandwidth to a peer
1916 * @return NULL on error
1918 struct GNUNET_TRANSPORT_Handle *
1919 GNUNET_TRANSPORT_connect2 (const struct GNUNET_CONFIGURATION_Handle *cfg,
1920 const struct GNUNET_PeerIdentity *self,
1922 GNUNET_TRANSPORT_ReceiveCallback rec,
1923 GNUNET_TRANSPORT_NotifyConnect nc,
1924 GNUNET_TRANSPORT_NotifyDisconnect nd,
1925 GNUNET_TRANSPORT_NotifyExcessBandwidth neb)
1927 struct GNUNET_TRANSPORT_Handle *ret;
1929 ret = GNUNET_new (struct GNUNET_TRANSPORT_Handle);
1933 ret->check_self = GNUNET_YES;
1941 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1942 LOG (GNUNET_ERROR_TYPE_DEBUG,
1943 "Connecting to transport service.\n");
1944 ret->client = GNUNET_CLIENT_connect ("transport",
1946 if (NULL == ret->client)
1952 GNUNET_CONTAINER_multipeermap_create (STARTING_NEIGHBOURS_SIZE,
1955 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1956 schedule_control_transmit (ret,
1957 sizeof (struct StartMessage),
1965 * Disconnect from the transport service.
1967 * @param handle handle to the service as returned from #GNUNET_TRANSPORT_connect()
1970 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1972 LOG (GNUNET_ERROR_TYPE_DEBUG,
1973 "Transport disconnect called!\n");
1974 /* this disconnects all neighbours... */
1975 if (NULL == handle->reconnect_task)
1976 disconnect_and_schedule_reconnect (handle);
1977 /* and now we stop trying to connect again... */
1978 if (NULL != handle->reconnect_task)
1980 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1981 handle->reconnect_task = NULL;
1983 GNUNET_CONTAINER_multipeermap_destroy (handle->neighbours);
1984 handle->neighbours = NULL;
1985 if (NULL != handle->quota_task)
1987 GNUNET_SCHEDULER_cancel (handle->quota_task);
1988 handle->quota_task = NULL;
1990 GNUNET_free_non_null (handle->my_hello);
1991 handle->my_hello = NULL;
1992 GNUNET_assert (NULL == handle->tc_head);
1993 GNUNET_assert (NULL == handle->tc_tail);
1994 GNUNET_assert (NULL == handle->hwl_head);
1995 GNUNET_assert (NULL == handle->hwl_tail);
1996 GNUNET_CONTAINER_heap_destroy (handle->ready_heap);
1997 handle->ready_heap = NULL;
1998 GNUNET_free (handle);
2003 * Check if we could queue a message of the given size for
2004 * transmission. The transport service will take both its
2005 * internal buffers and bandwidth limits imposed by the
2006 * other peer into consideration when answering this query.
2008 * @param handle connection to transport service
2009 * @param target who should receive the message
2010 * @param size how big is the message we want to transmit?
2011 * @param timeout after how long should we give up (and call
2012 * notify with buf NULL and size 0)?
2013 * @param notify function to call when we are ready to
2014 * send such a message
2015 * @param notify_cls closure for @a notify
2016 * @return NULL if someone else is already waiting to be notified
2017 * non-NULL if the notify callback was queued (can be used to cancel
2018 * using #GNUNET_TRANSPORT_notify_transmit_ready_cancel)
2020 struct GNUNET_TRANSPORT_TransmitHandle *
2021 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle *handle,
2022 const struct GNUNET_PeerIdentity *target,
2024 struct GNUNET_TIME_Relative timeout,
2025 GNUNET_TRANSPORT_TransmitReadyNotify notify,
2028 struct Neighbour *n;
2029 struct GNUNET_TRANSPORT_TransmitHandle *th;
2030 struct GNUNET_TIME_Relative delay;
2032 n = neighbour_find (handle, target);
2035 /* use GNUNET_TRANSPORT_try_connect first, only use this function
2036 * once a connection has been established */
2042 /* attempt to send two messages at the same time to the same peer */
2046 GNUNET_assert (NULL == n->hn);
2047 th = GNUNET_new (struct GNUNET_TRANSPORT_TransmitHandle);
2049 th->notify = notify;
2050 th->notify_cls = notify_cls;
2051 th->request_start = GNUNET_TIME_absolute_get ();
2052 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
2053 th->notify_size = size;
2055 /* calculate when our transmission should be ready */
2056 delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
2057 size + n->traffic_overhead);
2058 n->traffic_overhead = 0;
2059 if (delay.rel_value_us > timeout.rel_value_us)
2060 delay.rel_value_us = 0; /* notify immediately (with failure) */
2061 if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
2062 LOG (GNUNET_ERROR_TYPE_WARNING,
2063 "At bandwidth %u byte/s next transmission to %s in %s\n",
2064 (unsigned int) n->out_tracker.available_bytes_per_s__,
2065 GNUNET_i2s (target),
2066 GNUNET_STRINGS_relative_time_to_string (delay,
2069 LOG (GNUNET_ERROR_TYPE_DEBUG,
2070 "At bandwidth %u byte/s next transmission to %s in %s\n",
2071 (unsigned int) n->out_tracker.available_bytes_per_s__,
2072 GNUNET_i2s (target),
2073 GNUNET_STRINGS_relative_time_to_string (delay,
2075 n->hn = GNUNET_CONTAINER_heap_insert (handle->ready_heap,
2077 delay.rel_value_us);
2078 schedule_transmission (handle);
2084 * Cancel the specified transmission-ready notification.
2086 * @param th handle returned from #GNUNET_TRANSPORT_notify_transmit_ready()
2089 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct GNUNET_TRANSPORT_TransmitHandle *th)
2091 struct Neighbour *n;
2093 GNUNET_assert (NULL == th->next);
2094 GNUNET_assert (NULL == th->prev);
2096 GNUNET_assert (th == n->th);
2100 GNUNET_CONTAINER_heap_remove_node (n->hn);
2105 GNUNET_assert (NULL != th->timeout_task);
2106 GNUNET_SCHEDULER_cancel (th->timeout_task);
2107 th->timeout_task = NULL;
2113 /* end of transport_api.c */