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 offer-HELLO requests.
251 struct GNUNET_TRANSPORT_OfferHelloHandle
256 struct GNUNET_TRANSPORT_OfferHelloHandle *prev;
261 struct GNUNET_TRANSPORT_OfferHelloHandle *next;
264 * Transport service handle we use for transmission.
266 struct GNUNET_TRANSPORT_Handle *th;
269 * Transmission handle for this request.
271 struct GNUNET_TRANSPORT_TransmitHandle *tth;
274 * Function to call once we are done.
276 GNUNET_SCHEDULER_TaskCallback cont;
279 * Closure for @e cont
284 * The HELLO message to be transmitted.
286 struct GNUNET_MessageHeader *msg;
291 * Handle for the transport service (includes all of the
292 * state for the transport service).
294 struct GNUNET_TRANSPORT_Handle
298 * Closure for the callbacks.
303 * Function to call for received data.
305 GNUNET_TRANSPORT_ReceiveCallback rec;
308 * function to call on connect events
310 GNUNET_TRANSPORT_NotifyConnect nc_cb;
313 * function to call on disconnect events
315 GNUNET_TRANSPORT_NotifyDisconnect nd_cb;
318 * function to call on excess bandwidth events
320 GNUNET_TRANSPORT_NotifyExcessBandwidth neb_cb;
323 * Head of DLL of control messages.
325 struct GNUNET_TRANSPORT_TransmitHandle *control_head;
328 * Tail of DLL of control messages.
330 struct GNUNET_TRANSPORT_TransmitHandle *control_tail;
333 * The current HELLO message for this peer. Updated
334 * whenever transports change their addresses.
336 struct GNUNET_MessageHeader *my_hello;
339 * My client connection to the transport service.
341 struct GNUNET_CLIENT_Connection *client;
344 * Handle to our registration with the client for notification.
346 struct GNUNET_CLIENT_TransmitHandle *cth;
349 * Linked list of pending requests for our HELLO.
351 struct GNUNET_TRANSPORT_GetHelloHandle *hwl_head;
354 * Linked list of pending requests for our HELLO.
356 struct GNUNET_TRANSPORT_GetHelloHandle *hwl_tail;
359 * Linked list of pending try connect requests head
361 struct GNUNET_TRANSPORT_TryConnectHandle *tc_head;
364 * Linked list of pending try connect requests tail
366 struct GNUNET_TRANSPORT_TryConnectHandle *tc_tail;
369 * Linked list of pending offer HELLO requests head
371 struct GNUNET_TRANSPORT_OfferHelloHandle *oh_head;
374 * Linked list of pending offer HELLO requests tail
376 struct GNUNET_TRANSPORT_OfferHelloHandle *oh_tail;
381 const struct GNUNET_CONFIGURATION_Handle *cfg;
384 * Hash map of the current connected neighbours of this peer.
385 * Maps peer identities to `struct Neighbour` entries.
387 struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
390 * Heap sorting peers with pending messages by the timestamps that
391 * specify when we could next send a message to the respective peer.
392 * Excludes control messages (which can always go out immediately).
393 * Maps time stamps to `struct Neighbour` entries.
395 struct GNUNET_CONTAINER_Heap *ready_heap;
398 * Peer identity as assumed by this process, or all zeros.
400 struct GNUNET_PeerIdentity self;
403 * ID of the task trying to reconnect to the service.
405 struct GNUNET_SCHEDULER_Task *reconnect_task;
408 * ID of the task trying to trigger transmission for a peer while
409 * maintaining bandwidth quotas. In use if there are no control
410 * messages and the smallest entry in the @e ready_heap has a time
411 * stamp in the future.
413 struct GNUNET_SCHEDULER_Task *quota_task;
416 * Delay until we try to reconnect.
418 struct GNUNET_TIME_Relative reconnect_delay;
421 * Should we check that @e self matches what the service thinks?
422 * (if #GNUNET_NO, then @e self is all zeros!).
427 * Reconnect in progress
434 * Schedule the task to send one message, either from the control
435 * list or the peer message queues to the service.
437 * @param h transport service to schedule a transmission for
440 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h);
444 * Function that will schedule the job that will try
445 * to connect us again to the client.
447 * @param h transport service to reconnect
450 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h);
454 * A neighbour has not gotten a SEND_OK in a while. Print a warning.
456 * @param cls the `struct Neighbour`
457 * @param tc scheduler context
460 do_warn_unready (void *cls,
461 const struct GNUNET_SCHEDULER_TaskContext *tc)
463 struct Neighbour *n = cls;
464 struct GNUNET_TIME_Relative delay;
466 delay = GNUNET_TIME_absolute_get_duration (n->last_payload);
467 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
468 "Lacking SEND_OK, no payload could be send to %s for %s\n",
470 GNUNET_STRINGS_relative_time_to_string (delay,
473 = GNUNET_SCHEDULER_add_delayed (UNREADY_WARN_TIME,
480 * Get the neighbour list entry for the given peer
482 * @param h our context
483 * @param peer peer to look up
484 * @return NULL if no such peer entry exists
486 static struct Neighbour *
487 neighbour_find (struct GNUNET_TRANSPORT_Handle *h,
488 const struct GNUNET_PeerIdentity *peer)
490 return GNUNET_CONTAINER_multipeermap_get (h->neighbours,
496 * The outbound quota has changed in a way that may require
497 * us to reset the timeout. Update the timeout.
499 * @param cls the `struct Neighbour` for which the timeout changed
502 outbound_bw_tracker_update (void *cls)
504 struct Neighbour *n = cls;
505 struct GNUNET_TIME_Relative delay;
509 delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
510 n->th->notify_size + n->traffic_overhead);
511 LOG (GNUNET_ERROR_TYPE_DEBUG,
512 "New outbound delay %s us\n",
513 GNUNET_STRINGS_relative_time_to_string (delay,
515 GNUNET_CONTAINER_heap_update_cost (n->h->ready_heap,
516 n->hn, delay.rel_value_us);
517 schedule_transmission (n->h);
522 * Function called by the bandwidth tracker if we have excess
525 * @param cls the `struct Neighbour` that has excess bandwidth
528 notify_excess_cb (void *cls)
530 struct Neighbour *n = cls;
531 struct GNUNET_TRANSPORT_Handle *h = n->h;
533 if (NULL != h->neb_cb)
540 * Add neighbour to our list
542 * @return NULL if this API is currently disconnecting from the service
544 static struct Neighbour *
545 neighbour_add (struct GNUNET_TRANSPORT_Handle *h,
546 const struct GNUNET_PeerIdentity *pid)
550 LOG (GNUNET_ERROR_TYPE_DEBUG,
551 "Creating entry for neighbour `%s'.\n",
553 n = GNUNET_new (struct Neighbour);
556 n->is_ready = GNUNET_YES;
557 n->traffic_overhead = 0;
558 GNUNET_BANDWIDTH_tracker_init2 (&n->out_tracker,
559 &outbound_bw_tracker_update,
561 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
562 MAX_BANDWIDTH_CARRY_S,
565 GNUNET_assert (GNUNET_OK ==
566 GNUNET_CONTAINER_multipeermap_put (h->neighbours,
569 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
575 * Iterator over hash map entries, for deleting state of a neighbour.
577 * @param cls the `struct GNUNET_TRANSPORT_Handle *`
578 * @param key peer identity
579 * @param value value in the hash map, the neighbour entry to delete
580 * @return #GNUNET_YES if we should continue to
585 neighbour_delete (void *cls,
586 const struct GNUNET_PeerIdentity *key,
589 struct GNUNET_TRANSPORT_Handle *handle = cls;
590 struct Neighbour *n = value;
592 LOG (GNUNET_ERROR_TYPE_DEBUG,
593 "Dropping entry for neighbour `%s'.\n",
595 GNUNET_BANDWIDTH_tracker_notification_stop (&n->out_tracker);
596 if (NULL != handle->nd_cb)
597 handle->nd_cb (handle->cls,
599 if (NULL != n->unready_warn_task)
601 GNUNET_SCHEDULER_cancel (n->unready_warn_task);
602 n->unready_warn_task = NULL;
604 GNUNET_assert (NULL == n->th);
605 GNUNET_assert (NULL == n->hn);
606 GNUNET_assert (GNUNET_YES ==
607 GNUNET_CONTAINER_multipeermap_remove (handle->neighbours,
616 * Function we use for handling incoming messages.
618 * @param cls closure, a `struct GNUNET_TRANSPORT_Handle *`
619 * @param msg message received, NULL on timeout or fatal error
622 demultiplexer (void *cls,
623 const struct GNUNET_MessageHeader *msg)
625 struct GNUNET_TRANSPORT_Handle *h = cls;
626 const struct DisconnectInfoMessage *dim;
627 const struct ConnectInfoMessage *cim;
628 const struct InboundMessage *im;
629 const struct GNUNET_MessageHeader *imm;
630 const struct SendOkMessage *okm;
631 const struct QuotaSetMessage *qm;
632 struct GNUNET_TRANSPORT_GetHelloHandle *hwl;
633 struct GNUNET_TRANSPORT_GetHelloHandle *next_hwl;
635 struct GNUNET_PeerIdentity me;
638 uint32_t bytes_physical;
640 GNUNET_assert (NULL != h->client);
641 if (GNUNET_YES == h->reconnecting)
647 LOG (GNUNET_ERROR_TYPE_DEBUG,
648 "Error receiving from transport service, disconnecting temporarily.\n");
649 h->reconnecting = GNUNET_YES;
650 disconnect_and_schedule_reconnect (h);
653 GNUNET_CLIENT_receive (h->client,
656 GNUNET_TIME_UNIT_FOREVER_REL);
657 size = ntohs (msg->size);
658 switch (ntohs (msg->type))
660 case GNUNET_MESSAGE_TYPE_HELLO:
662 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg,
668 LOG (GNUNET_ERROR_TYPE_DEBUG,
669 "Receiving (my own) HELLO message (%u bytes), I am `%s'.\n",
672 GNUNET_free_non_null (h->my_hello);
674 if (size < sizeof (struct GNUNET_MessageHeader))
679 h->my_hello = GNUNET_copy_message (msg);
683 next_hwl = hwl->next;
684 hwl->rec (hwl->rec_cls,
689 case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
690 if (size < sizeof (struct ConnectInfoMessage))
693 h->reconnecting = GNUNET_YES;
694 disconnect_and_schedule_reconnect (h);
697 cim = (const struct ConnectInfoMessage *) msg;
699 sizeof (struct ConnectInfoMessage))
702 h->reconnecting = GNUNET_YES;
703 disconnect_and_schedule_reconnect (h);
706 LOG (GNUNET_ERROR_TYPE_DEBUG,
707 "Receiving CONNECT message for `%s'.\n",
708 GNUNET_i2s (&cim->id));
709 n = neighbour_find (h, &cim->id);
713 h->reconnecting = GNUNET_YES;
714 disconnect_and_schedule_reconnect (h);
717 n = neighbour_add (h,
719 LOG (GNUNET_ERROR_TYPE_DEBUG,
720 "Receiving CONNECT message for `%s' with quota %u\n",
721 GNUNET_i2s (&cim->id),
722 ntohl (cim->quota_out.value__));
723 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker,
725 if (NULL != h->nc_cb)
729 case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
730 if (size != sizeof (struct DisconnectInfoMessage))
733 h->reconnecting = GNUNET_YES;
734 disconnect_and_schedule_reconnect (h);
737 dim = (const struct DisconnectInfoMessage *) msg;
738 GNUNET_break (ntohl (dim->reserved) == 0);
739 LOG (GNUNET_ERROR_TYPE_DEBUG,
740 "Receiving DISCONNECT message for `%s'.\n",
741 GNUNET_i2s (&dim->peer));
742 n = neighbour_find (h, &dim->peer);
746 h->reconnecting = GNUNET_YES;
747 disconnect_and_schedule_reconnect (h);
754 case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
755 if (size != sizeof (struct SendOkMessage))
758 h->reconnecting = GNUNET_YES;
759 disconnect_and_schedule_reconnect (h);
762 okm = (const struct SendOkMessage *) msg;
763 bytes_msg = ntohl (okm->bytes_msg);
764 bytes_physical = ntohl (okm->bytes_physical);
765 LOG (GNUNET_ERROR_TYPE_DEBUG,
766 "Receiving SEND_OK message, transmission %s.\n",
767 ntohl (okm->success) == GNUNET_OK ? "succeeded" : "failed");
769 n = neighbour_find (h,
773 /* We should never get a 'SEND_OK' for a peer that we are not
776 h->reconnecting = GNUNET_YES;
777 disconnect_and_schedule_reconnect (h);
780 if (bytes_physical >= bytes_msg)
782 LOG (GNUNET_ERROR_TYPE_DEBUG,
783 "Overhead for %u byte message: %u\n",
785 bytes_physical - bytes_msg);
786 n->traffic_overhead += bytes_physical - bytes_msg;
788 GNUNET_break (GNUNET_NO == n->is_ready);
789 n->is_ready = GNUNET_YES;
790 if (NULL != n->unready_warn_task)
792 GNUNET_SCHEDULER_cancel (n->unready_warn_task);
793 n->unready_warn_task = NULL;
795 if ((NULL != n->th) && (NULL == n->hn))
797 GNUNET_assert (NULL != n->th->timeout_task);
798 GNUNET_SCHEDULER_cancel (n->th->timeout_task);
799 n->th->timeout_task = NULL;
800 /* we've been waiting for this (congestion, not quota,
801 * caused delayed transmission) */
802 n->hn = GNUNET_CONTAINER_heap_insert (h->ready_heap, n, 0);
803 schedule_transmission (h);
806 case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
808 sizeof (struct InboundMessage) + sizeof (struct GNUNET_MessageHeader))
811 h->reconnecting = GNUNET_YES;
812 disconnect_and_schedule_reconnect (h);
815 im = (const struct InboundMessage *) msg;
816 imm = (const struct GNUNET_MessageHeader *) &im[1];
817 if (ntohs (imm->size) + sizeof (struct InboundMessage) != size)
820 h->reconnecting = GNUNET_YES;
821 disconnect_and_schedule_reconnect (h);
824 LOG (GNUNET_ERROR_TYPE_DEBUG,
825 "Received message of type %u from `%s'.\n",
826 ntohs (imm->type), GNUNET_i2s (&im->peer));
827 n = neighbour_find (h, &im->peer);
831 h->reconnecting = GNUNET_YES;
832 disconnect_and_schedule_reconnect (h);
840 case GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA:
841 if (size != sizeof (struct QuotaSetMessage))
844 h->reconnecting = GNUNET_YES;
845 disconnect_and_schedule_reconnect (h);
848 qm = (const struct QuotaSetMessage *) msg;
849 n = neighbour_find (h, &qm->peer);
853 h->reconnecting = GNUNET_YES;
854 disconnect_and_schedule_reconnect (h);
857 LOG (GNUNET_ERROR_TYPE_DEBUG,
858 "Receiving SET_QUOTA message for `%s' with quota %u\n",
859 GNUNET_i2s (&qm->peer),
860 ntohl (qm->quota.value__));
861 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker,
865 LOG (GNUNET_ERROR_TYPE_ERROR,
866 _("Received unexpected message of type %u in %s:%u\n"),
877 * A transmission request could not be satisfied because of
878 * network congestion. Notify the initiator and clean up.
880 * @param cls the `struct GNUNET_TRANSPORT_TransmitHandle`
881 * @param tc scheduler context
884 timeout_request_due_to_congestion (void *cls,
885 const struct GNUNET_SCHEDULER_TaskContext *tc)
887 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
888 struct Neighbour *n = th->neighbour;
889 struct GNUNET_TIME_Relative delay;
891 n->th->timeout_task = NULL;
892 delay = GNUNET_TIME_absolute_get_duration (th->request_start);
893 LOG (GNUNET_ERROR_TYPE_WARNING,
894 "Discarding %u bytes of payload message after %s delay due to congestion\n",
896 GNUNET_STRINGS_relative_time_to_string (delay,
898 GNUNET_assert (th == n->th);
899 GNUNET_assert (NULL == n->hn);
901 th->notify (th->notify_cls,
909 * Transmit message(s) to service.
911 * @param cls handle to transport
912 * @param size number of bytes available in @a buf
913 * @param buf where to copy the message
914 * @return number of bytes copied to @a buf
917 transport_notify_ready (void *cls,
921 struct GNUNET_TRANSPORT_Handle *h = cls;
922 struct GNUNET_TRANSPORT_TransmitHandle *th;
923 struct GNUNET_TIME_Relative delay;
926 struct OutboundMessage obm;
931 GNUNET_assert (NULL != h->client);
935 /* transmission failed */
936 disconnect_and_schedule_reconnect (h);
942 /* first send control messages */
943 while ( (NULL != (th = h->control_head)) &&
944 (th->notify_size <= size) )
946 GNUNET_CONTAINER_DLL_remove (h->control_head,
949 nret = th->notify (th->notify_cls,
952 delay = GNUNET_TIME_absolute_get_duration (th->request_start);
953 if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
954 LOG (GNUNET_ERROR_TYPE_WARNING,
955 "Added %u bytes of control message at %u after %s delay\n",
958 GNUNET_STRINGS_relative_time_to_string (delay,
961 LOG (GNUNET_ERROR_TYPE_DEBUG,
962 "Added %u bytes of control message at %u after %s delay\n",
965 GNUNET_STRINGS_relative_time_to_string (delay,
972 /* then, if possible and no control messages pending, send data messages */
973 while ( (NULL == h->control_head) &&
974 (NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap))) )
976 if (GNUNET_YES != n->is_ready)
978 /* peer not ready, wait for notification! */
979 GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
981 GNUNET_assert (NULL == n->th->timeout_task);
983 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
985 &timeout_request_due_to_congestion,
990 if (th->notify_size + sizeof (struct OutboundMessage) > size)
991 break; /* does not fit */
992 if (GNUNET_BANDWIDTH_tracker_get_delay
993 (&n->out_tracker, th->notify_size).rel_value_us > 0)
994 break; /* too early */
995 GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
998 GNUNET_assert (size >= sizeof (struct OutboundMessage));
999 mret = th->notify (th->notify_cls,
1000 size - sizeof (struct OutboundMessage),
1001 &cbuf[ret + sizeof (struct OutboundMessage)]);
1002 GNUNET_assert (mret <= size - sizeof (struct OutboundMessage));
1008 if (NULL != n->unready_warn_task)
1009 n->unready_warn_task
1010 = GNUNET_SCHEDULER_add_delayed (UNREADY_WARN_TIME,
1013 n->last_payload = GNUNET_TIME_absolute_get ();
1014 n->is_ready = GNUNET_NO;
1015 GNUNET_assert (mret + sizeof (struct OutboundMessage) <
1016 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1017 obm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
1018 obm.header.size = htons (mret + sizeof (struct OutboundMessage));
1019 obm.reserved = htonl (0);
1021 GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining
1026 sizeof (struct OutboundMessage));
1027 ret += (mret + sizeof (struct OutboundMessage));
1028 size -= (mret + sizeof (struct OutboundMessage));
1029 GNUNET_BANDWIDTH_tracker_consume (&n->out_tracker,
1031 delay = GNUNET_TIME_absolute_get_duration (th->request_start);
1032 if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
1033 LOG (GNUNET_ERROR_TYPE_WARNING,
1034 "Added %u bytes of payload message for %s after %s delay at %u b/s\n",
1036 GNUNET_i2s (&n->id),
1037 GNUNET_STRINGS_relative_time_to_string (delay,
1039 (unsigned int) n->out_tracker.available_bytes_per_s__);
1041 LOG (GNUNET_ERROR_TYPE_DEBUG,
1042 "Added %u bytes of payload message for %s after %s delay at %u b/s\n",
1044 GNUNET_i2s (&n->id),
1045 GNUNET_STRINGS_relative_time_to_string (delay,
1047 (unsigned int) n->out_tracker.available_bytes_per_s__);
1051 /* if there are more pending messages, try to schedule those */
1052 schedule_transmission (h);
1053 LOG (GNUNET_ERROR_TYPE_DEBUG,
1054 "Transmitting %u bytes to transport service\n",
1061 * Schedule the task to send one message, either from the control
1062 * list or the peer message queues to the service.
1064 * @param cls transport service to schedule a transmission for
1065 * @param tc scheduler context
1068 schedule_transmission_task (void *cls,
1069 const struct GNUNET_SCHEDULER_TaskContext *tc)
1071 struct GNUNET_TRANSPORT_Handle *h = cls;
1073 struct GNUNET_TRANSPORT_TransmitHandle *th;
1074 struct Neighbour *n;
1076 h->quota_task = NULL;
1077 GNUNET_assert (NULL != h->client);
1078 /* destroy all requests that have timed out */
1079 while ( (NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap))) &&
1080 (0 == GNUNET_TIME_absolute_get_remaining (n->th->timeout).rel_value_us) )
1082 /* notify client that the request could not be satisfied within
1083 * the given time constraints */
1086 GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
1088 LOG (GNUNET_ERROR_TYPE_DEBUG,
1089 "Signalling timeout for transmission to peer %s due to congestion\n",
1090 GNUNET_i2s (&n->id));
1091 GNUNET_assert (0 == th->notify (th->notify_cls,
1098 if (NULL != h->control_head)
1100 size = h->control_head->notify_size;
1104 n = GNUNET_CONTAINER_heap_peek (h->ready_heap);
1106 return; /* no pending messages */
1107 size = n->th->notify_size + sizeof (struct OutboundMessage);
1109 LOG (GNUNET_ERROR_TYPE_DEBUG,
1110 "Calling notify_transmit_ready\n");
1112 = GNUNET_CLIENT_notify_transmit_ready (h->client,
1114 GNUNET_TIME_UNIT_FOREVER_REL,
1116 &transport_notify_ready,
1118 GNUNET_assert (NULL != h->cth);
1123 * Schedule the task to send one message, either from the control
1124 * list or the peer message queues to the service.
1126 * @param h transport service to schedule a transmission for
1129 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
1131 struct GNUNET_TIME_Relative delay;
1132 struct Neighbour *n;
1134 GNUNET_assert (NULL != h->client);
1135 if (NULL != h->quota_task)
1137 GNUNET_SCHEDULER_cancel (h->quota_task);
1138 h->quota_task = NULL;
1140 if (NULL != h->control_head)
1141 delay = GNUNET_TIME_UNIT_ZERO;
1142 else if (NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap)))
1145 GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
1146 n->th->notify_size + n->traffic_overhead);
1147 n->traffic_overhead = 0;
1150 return; /* no work to be done */
1151 LOG (GNUNET_ERROR_TYPE_DEBUG,
1152 "Scheduling next transmission to service in %s\n",
1153 GNUNET_STRINGS_relative_time_to_string (delay,
1156 GNUNET_SCHEDULER_add_delayed (delay,
1157 &schedule_transmission_task,
1163 * Queue control request for transmission to the transport
1166 * @param h handle to the transport service
1167 * @param size number of bytes to be transmitted
1168 * @param notify function to call to get the content
1169 * @param notify_cls closure for @a notify
1170 * @return a `struct GNUNET_TRANSPORT_TransmitHandle`
1172 static struct GNUNET_TRANSPORT_TransmitHandle *
1173 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h,
1175 GNUNET_TRANSPORT_TransmitReadyNotify notify,
1178 struct GNUNET_TRANSPORT_TransmitHandle *th;
1180 LOG (GNUNET_ERROR_TYPE_DEBUG,
1181 "Control transmit of %u bytes requested\n",
1183 th = GNUNET_new (struct GNUNET_TRANSPORT_TransmitHandle);
1184 th->notify = notify;
1185 th->notify_cls = notify_cls;
1186 th->notify_size = size;
1187 th->request_start = GNUNET_TIME_absolute_get ();
1188 GNUNET_CONTAINER_DLL_insert_tail (h->control_head,
1191 schedule_transmission (h);
1197 * Transmit START message to service.
1200 * @param size number of bytes available in @a buf
1201 * @param buf where to copy the message
1202 * @return number of bytes copied to @a buf
1205 send_start (void *cls,
1209 struct GNUNET_TRANSPORT_Handle *h = cls;
1210 struct StartMessage s;
1215 /* Can only be shutdown, just give up */
1216 LOG (GNUNET_ERROR_TYPE_DEBUG,
1217 "Shutdown while trying to transmit START request.\n");
1220 LOG (GNUNET_ERROR_TYPE_DEBUG,
1221 "Transmitting START request.\n");
1222 GNUNET_assert (size >= sizeof (struct StartMessage));
1223 s.header.size = htons (sizeof (struct StartMessage));
1224 s.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
1230 s.options = htonl (options);
1232 memcpy (buf, &s, sizeof (struct StartMessage));
1233 GNUNET_CLIENT_receive (h->client, &demultiplexer, h,
1234 GNUNET_TIME_UNIT_FOREVER_REL);
1235 return sizeof (struct StartMessage);
1240 * Try again to connect to transport service.
1242 * @param cls the handle to the transport service
1243 * @param tc scheduler context
1246 reconnect (void *cls,
1247 const struct GNUNET_SCHEDULER_TaskContext *tc)
1249 struct GNUNET_TRANSPORT_Handle *h = cls;
1251 h->reconnect_task = NULL;
1252 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1254 /* shutdown, just give up */
1257 LOG (GNUNET_ERROR_TYPE_DEBUG,
1258 "Connecting to transport service.\n");
1259 GNUNET_assert (NULL == h->client);
1260 GNUNET_assert (NULL == h->control_head);
1261 GNUNET_assert (NULL == h->control_tail);
1262 h->reconnecting = GNUNET_NO;
1263 h->client = GNUNET_CLIENT_connect ("transport", h->cfg);
1265 GNUNET_assert (NULL != h->client);
1266 schedule_control_transmit (h, sizeof (struct StartMessage),
1272 * Function that will schedule the job that will try
1273 * to connect us again to the client.
1275 * @param h transport service to reconnect
1278 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
1280 struct GNUNET_TRANSPORT_TransmitHandle *th;
1282 GNUNET_assert (NULL == h->reconnect_task);
1285 GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
1288 if (NULL != h->client)
1290 GNUNET_CLIENT_disconnect (h->client);
1292 /* LOG (GNUNET_ERROR_TYPE_ERROR,
1293 "Client disconnect done \n");*/
1295 /* Forget about all neighbours that we used to be connected to */
1296 GNUNET_CONTAINER_multipeermap_iterate (h->neighbours,
1299 if (NULL != h->quota_task)
1301 GNUNET_SCHEDULER_cancel (h->quota_task);
1302 h->quota_task = NULL;
1304 while ((NULL != (th = h->control_head)))
1306 GNUNET_CONTAINER_DLL_remove (h->control_head,
1309 th->notify (th->notify_cls,
1314 LOG (GNUNET_ERROR_TYPE_DEBUG,
1315 "Scheduling task to reconnect to transport service in %s.\n",
1316 GNUNET_STRINGS_relative_time_to_string (h->reconnect_delay,
1319 GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
1322 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
1327 * Cancel control request for transmission to the transport service.
1329 * @param th handle to the transport service
1330 * @param tth transmit handle to cancel
1333 cancel_control_transmit (struct GNUNET_TRANSPORT_Handle *th,
1334 struct GNUNET_TRANSPORT_TransmitHandle *tth)
1336 LOG (GNUNET_ERROR_TYPE_DEBUG,
1337 "Canceling transmit of contral transmission requested\n");
1338 GNUNET_CONTAINER_DLL_remove (th->control_head,
1346 * Send #GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT message to the
1349 * @param cls the `struct GNUNET_TRANSPORT_TryConnectHandle`
1350 * @param size number of bytes available in @a buf
1351 * @param buf where to copy the message
1352 * @return number of bytes copied to @a buf
1355 send_try_connect (void *cls,
1359 struct GNUNET_TRANSPORT_TryConnectHandle *tch = cls;
1360 struct TransportRequestConnectMessage msg;
1365 LOG (GNUNET_ERROR_TYPE_DEBUG,
1366 "Discarding `%s' request to `%s' due to error in transport service connection.\n",
1368 GNUNET_i2s (&tch->pid));
1369 if (NULL != tch->cb)
1370 tch->cb (tch->cb_cls,
1372 GNUNET_TRANSPORT_try_connect_cancel (tch);
1375 LOG (GNUNET_ERROR_TYPE_DEBUG,
1376 "Transmitting `%s' request with respect to `%s'.\n",
1378 GNUNET_i2s (&tch->pid));
1379 GNUNET_assert (size >= sizeof (struct TransportRequestConnectMessage));
1380 msg.header.size = htons (sizeof (struct TransportRequestConnectMessage));
1381 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT);
1382 msg.reserved = htonl (0);
1383 msg.peer = tch->pid;
1384 memcpy (buf, &msg, sizeof (msg));
1385 if (NULL != tch->cb)
1386 tch->cb (tch->cb_cls, GNUNET_OK);
1387 GNUNET_TRANSPORT_try_connect_cancel (tch);
1388 return sizeof (struct TransportRequestConnectMessage);
1393 * Ask the transport service to establish a connection to
1396 * @param handle connection to transport service
1397 * @param target who we should try to connect to
1398 * @param cb callback to be called when request was transmitted to transport
1400 * @param cb_cls closure for the callback
1401 * @return a `struct GNUNET_TRANSPORT_TryConnectHandle` handle or
1402 * NULL on failure (cb will not be called)
1404 struct GNUNET_TRANSPORT_TryConnectHandle *
1405 GNUNET_TRANSPORT_try_connect (struct GNUNET_TRANSPORT_Handle *handle,
1406 const struct GNUNET_PeerIdentity *target,
1407 GNUNET_TRANSPORT_TryConnectCallback cb,
1410 struct GNUNET_TRANSPORT_TryConnectHandle *tch;
1412 if (NULL == handle->client)
1414 tch = GNUNET_new (struct GNUNET_TRANSPORT_TryConnectHandle);
1418 tch->cb_cls = cb_cls;
1419 tch->tth = schedule_control_transmit (handle,
1420 sizeof (struct TransportRequestConnectMessage),
1421 &send_try_connect, tch);
1422 GNUNET_CONTAINER_DLL_insert (handle->tc_head,
1430 * Cancel the request to transport to try a connect
1431 * Callback will not be called
1433 * @param tch the handle to cancel
1436 GNUNET_TRANSPORT_try_connect_cancel (struct GNUNET_TRANSPORT_TryConnectHandle *tch)
1438 struct GNUNET_TRANSPORT_Handle *th;
1441 if (NULL != tch->tth)
1442 cancel_control_transmit (th, tch->tth);
1443 GNUNET_CONTAINER_DLL_remove (th->tc_head,
1451 * Send HELLO message to the service.
1453 * @param cls the HELLO message to send
1454 * @param size number of bytes available in @a buf
1455 * @param buf where to copy the message
1456 * @return number of bytes copied to @a buf
1459 send_hello (void *cls,
1463 struct GNUNET_TRANSPORT_OfferHelloHandle *ohh = cls;
1464 struct GNUNET_MessageHeader *msg = ohh->msg;
1466 struct GNUNET_SCHEDULER_TaskContext tc;
1468 tc.read_ready = NULL;
1469 tc.write_ready = NULL;
1470 tc.reason = GNUNET_SCHEDULER_REASON_TIMEOUT;
1473 LOG (GNUNET_ERROR_TYPE_DEBUG,
1474 "Timeout while trying to transmit `%s' request.\n",
1476 if (NULL != ohh->cont)
1477 ohh->cont (ohh->cls,
1480 GNUNET_CONTAINER_DLL_remove (ohh->th->oh_head,
1486 LOG (GNUNET_ERROR_TYPE_DEBUG,
1487 "Transmitting `%s' request.\n",
1489 ssize = ntohs (msg->size);
1490 GNUNET_assert (size >= ssize);
1495 tc.reason = GNUNET_SCHEDULER_REASON_READ_READY;
1496 if (NULL != ohh->cont)
1497 ohh->cont (ohh->cls,
1499 GNUNET_CONTAINER_DLL_remove (ohh->th->oh_head,
1508 * Send traffic metric message to the service.
1510 * @param cls the message to send
1511 * @param size number of bytes available in @a buf
1512 * @param buf where to copy the message
1513 * @return number of bytes copied to @a buf
1516 send_metric (void *cls,
1520 struct TrafficMetricMessage *msg = cls;
1525 LOG (GNUNET_ERROR_TYPE_DEBUG,
1526 "Timeout while trying to transmit TRAFFIC_METRIC request.\n");
1530 LOG (GNUNET_ERROR_TYPE_DEBUG,
1531 "Transmitting TRAFFIC_METRIC request.\n");
1532 ssize = ntohs (msg->header.size);
1533 GNUNET_assert (size >= ssize);
1534 memcpy (buf, msg, ssize);
1541 * Set transport metrics for a peer and a direction
1543 * @param handle transport handle
1544 * @param peer the peer to set the metric for
1545 * @param prop the performance metrics to set
1546 * @param delay_in inbound delay to introduce
1547 * @param delay_out outbound delay to introduce
1549 * Note: Delay restrictions in receiving direction will be enforced
1550 * with one message delay.
1553 GNUNET_TRANSPORT_set_traffic_metric (struct GNUNET_TRANSPORT_Handle *handle,
1554 const struct GNUNET_PeerIdentity *peer,
1555 const struct GNUNET_ATS_Properties *prop,
1556 struct GNUNET_TIME_Relative delay_in,
1557 struct GNUNET_TIME_Relative delay_out)
1559 struct TrafficMetricMessage *msg;
1561 msg = GNUNET_new (struct TrafficMetricMessage);
1562 msg->header.size = htons (sizeof (struct TrafficMetricMessage));
1563 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TRAFFIC_METRIC);
1564 msg->reserved = htonl (0);
1566 GNUNET_ATS_properties_hton (&msg->properties,
1568 msg->delay_in = GNUNET_TIME_relative_hton (delay_in);
1569 msg->delay_out = GNUNET_TIME_relative_hton (delay_out);
1570 schedule_control_transmit (handle,
1571 sizeof (struct TrafficMetricMessage),
1578 * Offer the transport service the HELLO of another peer. Note that
1579 * the transport service may just ignore this message if the HELLO is
1580 * malformed or useless due to our local configuration.
1582 * @param handle connection to transport service
1583 * @param hello the hello message
1584 * @param cont continuation to call when HELLO has been sent,
1585 * tc reason #GNUNET_SCHEDULER_REASON_TIMEOUT for fail
1586 * tc reasong #GNUNET_SCHEDULER_REASON_READ_READY for success
1587 * @param cont_cls closure for @a cont
1588 * @return a `struct GNUNET_TRANSPORT_OfferHelloHandle` handle or NULL on failure,
1589 * in case of failure @a cont will not be called
1592 struct GNUNET_TRANSPORT_OfferHelloHandle *
1593 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
1594 const struct GNUNET_MessageHeader *hello,
1595 GNUNET_SCHEDULER_TaskCallback cont,
1598 struct GNUNET_TRANSPORT_OfferHelloHandle *ohh;
1599 struct GNUNET_MessageHeader *msg;
1600 struct GNUNET_PeerIdentity peer;
1603 if (NULL == handle->client)
1605 GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
1606 size = ntohs (hello->size);
1607 GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
1609 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hello,
1616 msg = GNUNET_malloc (size);
1617 memcpy (msg, hello, size);
1618 LOG (GNUNET_ERROR_TYPE_DEBUG,
1619 "Offering HELLO message of `%s' to transport for validation.\n",
1620 GNUNET_i2s (&peer));
1622 ohh = GNUNET_new (struct GNUNET_TRANSPORT_OfferHelloHandle);
1625 ohh->cls = cont_cls;
1627 ohh->tth = schedule_control_transmit (handle,
1631 GNUNET_CONTAINER_DLL_insert (handle->oh_head,
1639 * Cancel the request to transport to offer the HELLO message
1641 * @param ohh the handle for the operation to cancel
1644 GNUNET_TRANSPORT_offer_hello_cancel (struct GNUNET_TRANSPORT_OfferHelloHandle *ohh)
1646 struct GNUNET_TRANSPORT_Handle *th = ohh->th;
1648 cancel_control_transmit (ohh->th, ohh->tth);
1649 GNUNET_CONTAINER_DLL_remove (th->oh_head,
1652 GNUNET_free (ohh->msg);
1658 * Checks if a given peer is connected to us
1660 * @param handle connection to transport service
1661 * @param peer the peer to check
1662 * @return #GNUNET_YES (connected) or #GNUNET_NO (disconnected)
1665 GNUNET_TRANSPORT_check_peer_connected (struct GNUNET_TRANSPORT_Handle *handle,
1666 const struct GNUNET_PeerIdentity *peer)
1669 GNUNET_CONTAINER_multipeermap_contains (handle->neighbours,
1677 * Task to call the HelloUpdateCallback of the GetHelloHandle
1679 * @param cls the `struct GNUNET_TRANSPORT_GetHelloHandle`
1680 * @param tc the scheduler task context
1683 call_hello_update_cb_async (void *cls,
1684 const struct GNUNET_SCHEDULER_TaskContext *tc)
1686 struct GNUNET_TRANSPORT_GetHelloHandle *ghh = cls;
1688 GNUNET_assert (NULL != ghh->handle->my_hello);
1689 GNUNET_assert (NULL != ghh->notify_task);
1690 ghh->notify_task = NULL;
1691 ghh->rec (ghh->rec_cls,
1692 ghh->handle->my_hello);
1697 * Obtain the HELLO message for this peer. The callback given in this function
1698 * is never called synchronously.
1700 * @param handle connection to transport service
1701 * @param rec function to call with the HELLO, sender will be our peer
1702 * identity; message and sender will be NULL on timeout
1703 * (handshake with transport service pending/failed).
1704 * cost estimate will be 0.
1705 * @param rec_cls closure for @a rec
1706 * @return handle to cancel the operation
1708 struct GNUNET_TRANSPORT_GetHelloHandle *
1709 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
1710 GNUNET_TRANSPORT_HelloUpdateCallback rec,
1713 struct GNUNET_TRANSPORT_GetHelloHandle *hwl;
1715 hwl = GNUNET_new (struct GNUNET_TRANSPORT_GetHelloHandle);
1717 hwl->rec_cls = rec_cls;
1718 hwl->handle = handle;
1719 GNUNET_CONTAINER_DLL_insert (handle->hwl_head,
1722 if (NULL != handle->my_hello)
1723 hwl->notify_task = GNUNET_SCHEDULER_add_now (&call_hello_update_cb_async,
1730 * Stop receiving updates about changes to our HELLO message.
1732 * @param ghh handle to cancel
1735 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_GetHelloHandle *ghh)
1737 struct GNUNET_TRANSPORT_Handle *handle = ghh->handle;
1739 if (NULL != ghh->notify_task)
1740 GNUNET_SCHEDULER_cancel (ghh->notify_task);
1741 GNUNET_CONTAINER_DLL_remove (handle->hwl_head, handle->hwl_tail, ghh);
1747 * Connect to the transport service. Note that the connection may
1748 * complete (or fail) asynchronously.
1750 * @param cfg configuration to use
1751 * @param self our own identity (API should check that it matches
1752 * the identity found by transport), or NULL (no check)
1753 * @param cls closure for the callbacks
1754 * @param rec receive function to call
1755 * @param nc function to call on connect events
1756 * @param nd function to call on disconnect events
1757 * @return NULL on error
1759 struct GNUNET_TRANSPORT_Handle *
1760 GNUNET_TRANSPORT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1761 const struct GNUNET_PeerIdentity *self,
1763 GNUNET_TRANSPORT_ReceiveCallback rec,
1764 GNUNET_TRANSPORT_NotifyConnect nc,
1765 GNUNET_TRANSPORT_NotifyDisconnect nd)
1767 return GNUNET_TRANSPORT_connect2 (cfg,
1778 * Connect to the transport service. Note that the connection may
1779 * complete (or fail) asynchronously.
1781 * @param cfg configuration to use
1782 * @param self our own identity (API should check that it matches
1783 * the identity found by transport), or NULL (no check)
1784 * @param cls closure for the callbacks
1785 * @param rec receive function to call
1786 * @param nc function to call on connect events
1787 * @param nd function to call on disconnect events
1788 * @param neb function to call if we have excess bandwidth to a peer
1789 * @return NULL on error
1791 struct GNUNET_TRANSPORT_Handle *
1792 GNUNET_TRANSPORT_connect2 (const struct GNUNET_CONFIGURATION_Handle *cfg,
1793 const struct GNUNET_PeerIdentity *self,
1795 GNUNET_TRANSPORT_ReceiveCallback rec,
1796 GNUNET_TRANSPORT_NotifyConnect nc,
1797 GNUNET_TRANSPORT_NotifyDisconnect nd,
1798 GNUNET_TRANSPORT_NotifyExcessBandwidth neb)
1800 struct GNUNET_TRANSPORT_Handle *ret;
1802 ret = GNUNET_new (struct GNUNET_TRANSPORT_Handle);
1806 ret->check_self = GNUNET_YES;
1814 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1815 LOG (GNUNET_ERROR_TYPE_DEBUG,
1816 "Connecting to transport service.\n");
1817 ret->client = GNUNET_CLIENT_connect ("transport",
1819 if (NULL == ret->client)
1825 GNUNET_CONTAINER_multipeermap_create (STARTING_NEIGHBOURS_SIZE,
1828 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1829 schedule_control_transmit (ret,
1830 sizeof (struct StartMessage),
1838 * Disconnect from the transport service.
1840 * @param handle handle to the service as returned from #GNUNET_TRANSPORT_connect()
1843 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1845 LOG (GNUNET_ERROR_TYPE_DEBUG,
1846 "Transport disconnect called!\n");
1847 /* this disconnects all neighbours... */
1848 if (NULL == handle->reconnect_task)
1849 disconnect_and_schedule_reconnect (handle);
1850 /* and now we stop trying to connect again... */
1851 if (NULL != handle->reconnect_task)
1853 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1854 handle->reconnect_task = NULL;
1856 GNUNET_CONTAINER_multipeermap_destroy (handle->neighbours);
1857 handle->neighbours = NULL;
1858 if (NULL != handle->quota_task)
1860 GNUNET_SCHEDULER_cancel (handle->quota_task);
1861 handle->quota_task = NULL;
1863 GNUNET_free_non_null (handle->my_hello);
1864 handle->my_hello = NULL;
1865 GNUNET_assert (NULL == handle->tc_head);
1866 GNUNET_assert (NULL == handle->tc_tail);
1867 GNUNET_assert (NULL == handle->hwl_head);
1868 GNUNET_assert (NULL == handle->hwl_tail);
1869 GNUNET_CONTAINER_heap_destroy (handle->ready_heap);
1870 handle->ready_heap = NULL;
1871 GNUNET_free (handle);
1876 * Check if we could queue a message of the given size for
1877 * transmission. The transport service will take both its
1878 * internal buffers and bandwidth limits imposed by the
1879 * other peer into consideration when answering this query.
1881 * @param handle connection to transport service
1882 * @param target who should receive the message
1883 * @param size how big is the message we want to transmit?
1884 * @param timeout after how long should we give up (and call
1885 * notify with buf NULL and size 0)?
1886 * @param notify function to call when we are ready to
1887 * send such a message
1888 * @param notify_cls closure for @a notify
1889 * @return NULL if someone else is already waiting to be notified
1890 * non-NULL if the notify callback was queued (can be used to cancel
1891 * using #GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1893 struct GNUNET_TRANSPORT_TransmitHandle *
1894 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle *handle,
1895 const struct GNUNET_PeerIdentity *target,
1897 struct GNUNET_TIME_Relative timeout,
1898 GNUNET_TRANSPORT_TransmitReadyNotify notify,
1901 struct Neighbour *n;
1902 struct GNUNET_TRANSPORT_TransmitHandle *th;
1903 struct GNUNET_TIME_Relative delay;
1905 n = neighbour_find (handle, target);
1908 /* use GNUNET_TRANSPORT_try_connect first, only use this function
1909 * once a connection has been established */
1915 /* attempt to send two messages at the same time to the same peer */
1919 GNUNET_assert (NULL == n->hn);
1920 th = GNUNET_new (struct GNUNET_TRANSPORT_TransmitHandle);
1922 th->notify = notify;
1923 th->notify_cls = notify_cls;
1924 th->request_start = GNUNET_TIME_absolute_get ();
1925 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1926 th->notify_size = size;
1928 /* calculate when our transmission should be ready */
1929 delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
1930 size + n->traffic_overhead);
1931 n->traffic_overhead = 0;
1932 if (delay.rel_value_us > timeout.rel_value_us)
1933 delay.rel_value_us = 0; /* notify immediately (with failure) */
1934 if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
1935 LOG (GNUNET_ERROR_TYPE_WARNING,
1936 "At bandwidth %u byte/s next transmission to %s in %s\n",
1937 (unsigned int) n->out_tracker.available_bytes_per_s__,
1938 GNUNET_i2s (target),
1939 GNUNET_STRINGS_relative_time_to_string (delay,
1942 LOG (GNUNET_ERROR_TYPE_DEBUG,
1943 "At bandwidth %u byte/s next transmission to %s in %s\n",
1944 (unsigned int) n->out_tracker.available_bytes_per_s__,
1945 GNUNET_i2s (target),
1946 GNUNET_STRINGS_relative_time_to_string (delay,
1948 n->hn = GNUNET_CONTAINER_heap_insert (handle->ready_heap,
1950 delay.rel_value_us);
1951 schedule_transmission (handle);
1957 * Cancel the specified transmission-ready notification.
1959 * @param th handle returned from #GNUNET_TRANSPORT_notify_transmit_ready()
1962 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct GNUNET_TRANSPORT_TransmitHandle *th)
1964 struct Neighbour *n;
1966 GNUNET_assert (NULL == th->next);
1967 GNUNET_assert (NULL == th->prev);
1969 GNUNET_assert (th == n->th);
1973 GNUNET_CONTAINER_heap_remove_node (n->hn);
1978 GNUNET_assert (NULL != th->timeout_task);
1979 GNUNET_SCHEDULER_cancel (th->timeout_task);
1980 th->timeout_task = NULL;
1986 /* end of transport_api.c */