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
994 th->notify_size).rel_value_us > 0)
995 break; /* too early */
996 GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
999 GNUNET_assert (size >= sizeof (struct OutboundMessage));
1000 mret = th->notify (th->notify_cls,
1001 size - sizeof (struct OutboundMessage),
1002 &cbuf[ret + sizeof (struct OutboundMessage)]);
1003 GNUNET_assert (mret <= size - sizeof (struct OutboundMessage));
1009 if (NULL != n->unready_warn_task)
1010 n->unready_warn_task
1011 = GNUNET_SCHEDULER_add_delayed (UNREADY_WARN_TIME,
1014 n->last_payload = GNUNET_TIME_absolute_get ();
1015 n->is_ready = GNUNET_NO;
1016 GNUNET_assert (mret + sizeof (struct OutboundMessage) <
1017 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1018 obm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
1019 obm.header.size = htons (mret + sizeof (struct OutboundMessage));
1020 obm.reserved = htonl (0);
1022 GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining
1027 sizeof (struct OutboundMessage));
1028 ret += (mret + sizeof (struct OutboundMessage));
1029 size -= (mret + sizeof (struct OutboundMessage));
1030 GNUNET_BANDWIDTH_tracker_consume (&n->out_tracker,
1032 delay = GNUNET_TIME_absolute_get_duration (th->request_start);
1033 if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
1034 LOG (GNUNET_ERROR_TYPE_WARNING,
1035 "Added %u bytes of payload message for %s after %s delay at %u b/s\n",
1037 GNUNET_i2s (&n->id),
1038 GNUNET_STRINGS_relative_time_to_string (delay,
1040 (unsigned int) n->out_tracker.available_bytes_per_s__);
1042 LOG (GNUNET_ERROR_TYPE_DEBUG,
1043 "Added %u bytes of payload message for %s after %s delay at %u b/s\n",
1045 GNUNET_i2s (&n->id),
1046 GNUNET_STRINGS_relative_time_to_string (delay,
1048 (unsigned int) n->out_tracker.available_bytes_per_s__);
1052 /* if there are more pending messages, try to schedule those */
1053 schedule_transmission (h);
1054 LOG (GNUNET_ERROR_TYPE_DEBUG,
1055 "Transmitting %u bytes to transport service\n",
1062 * Schedule the task to send one message, either from the control
1063 * list or the peer message queues to the service.
1065 * @param cls transport service to schedule a transmission for
1066 * @param tc scheduler context
1069 schedule_transmission_task (void *cls,
1070 const struct GNUNET_SCHEDULER_TaskContext *tc)
1072 struct GNUNET_TRANSPORT_Handle *h = cls;
1074 struct GNUNET_TRANSPORT_TransmitHandle *th;
1075 struct Neighbour *n;
1077 h->quota_task = NULL;
1078 GNUNET_assert (NULL != h->client);
1079 /* destroy all requests that have timed out */
1080 while ( (NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap))) &&
1081 (0 == GNUNET_TIME_absolute_get_remaining (n->th->timeout).rel_value_us) )
1083 /* notify client that the request could not be satisfied within
1084 * the given time constraints */
1087 GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
1089 LOG (GNUNET_ERROR_TYPE_DEBUG,
1090 "Signalling timeout for transmission to peer %s due to congestion\n",
1091 GNUNET_i2s (&n->id));
1092 GNUNET_assert (0 == th->notify (th->notify_cls,
1099 if (NULL != h->control_head)
1101 size = h->control_head->notify_size;
1105 n = GNUNET_CONTAINER_heap_peek (h->ready_heap);
1107 return; /* no pending messages */
1108 size = n->th->notify_size + sizeof (struct OutboundMessage);
1110 LOG (GNUNET_ERROR_TYPE_DEBUG,
1111 "Calling notify_transmit_ready\n");
1113 = GNUNET_CLIENT_notify_transmit_ready (h->client,
1115 GNUNET_TIME_UNIT_FOREVER_REL,
1117 &transport_notify_ready,
1119 GNUNET_assert (NULL != h->cth);
1124 * Schedule the task to send one message, either from the control
1125 * list or the peer message queues to the service.
1127 * @param h transport service to schedule a transmission for
1130 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
1132 struct GNUNET_TIME_Relative delay;
1133 struct Neighbour *n;
1135 GNUNET_assert (NULL != h->client);
1136 if (NULL != h->quota_task)
1138 GNUNET_SCHEDULER_cancel (h->quota_task);
1139 h->quota_task = NULL;
1141 if (NULL != h->control_head)
1142 delay = GNUNET_TIME_UNIT_ZERO;
1143 else if (NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap)))
1146 GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
1147 n->th->notify_size + n->traffic_overhead);
1148 n->traffic_overhead = 0;
1151 return; /* no work to be done */
1152 LOG (GNUNET_ERROR_TYPE_DEBUG,
1153 "Scheduling next transmission to service in %s\n",
1154 GNUNET_STRINGS_relative_time_to_string (delay,
1157 GNUNET_SCHEDULER_add_delayed (delay,
1158 &schedule_transmission_task,
1164 * Queue control request for transmission to the transport
1167 * @param h handle to the transport service
1168 * @param size number of bytes to be transmitted
1169 * @param notify function to call to get the content
1170 * @param notify_cls closure for @a notify
1171 * @return a `struct GNUNET_TRANSPORT_TransmitHandle`
1173 static struct GNUNET_TRANSPORT_TransmitHandle *
1174 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h,
1176 GNUNET_TRANSPORT_TransmitReadyNotify notify,
1179 struct GNUNET_TRANSPORT_TransmitHandle *th;
1181 LOG (GNUNET_ERROR_TYPE_DEBUG,
1182 "Control transmit of %u bytes requested\n",
1184 th = GNUNET_new (struct GNUNET_TRANSPORT_TransmitHandle);
1185 th->notify = notify;
1186 th->notify_cls = notify_cls;
1187 th->notify_size = size;
1188 th->request_start = GNUNET_TIME_absolute_get ();
1189 GNUNET_CONTAINER_DLL_insert_tail (h->control_head,
1192 schedule_transmission (h);
1198 * Transmit START message to service.
1201 * @param size number of bytes available in @a buf
1202 * @param buf where to copy the message
1203 * @return number of bytes copied to @a buf
1206 send_start (void *cls,
1210 struct GNUNET_TRANSPORT_Handle *h = cls;
1211 struct StartMessage s;
1216 /* Can only be shutdown, just give up */
1217 LOG (GNUNET_ERROR_TYPE_DEBUG,
1218 "Shutdown while trying to transmit START request.\n");
1221 LOG (GNUNET_ERROR_TYPE_DEBUG,
1222 "Transmitting START request.\n");
1223 GNUNET_assert (size >= sizeof (struct StartMessage));
1224 s.header.size = htons (sizeof (struct StartMessage));
1225 s.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
1231 s.options = htonl (options);
1233 memcpy (buf, &s, sizeof (struct StartMessage));
1234 GNUNET_CLIENT_receive (h->client, &demultiplexer, h,
1235 GNUNET_TIME_UNIT_FOREVER_REL);
1236 return sizeof (struct StartMessage);
1241 * Try again to connect to transport service.
1243 * @param cls the handle to the transport service
1244 * @param tc scheduler context
1247 reconnect (void *cls,
1248 const struct GNUNET_SCHEDULER_TaskContext *tc)
1250 struct GNUNET_TRANSPORT_Handle *h = cls;
1252 h->reconnect_task = NULL;
1253 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1255 /* shutdown, just give up */
1258 LOG (GNUNET_ERROR_TYPE_DEBUG,
1259 "Connecting to transport service.\n");
1260 GNUNET_assert (NULL == h->client);
1261 GNUNET_assert (NULL == h->control_head);
1262 GNUNET_assert (NULL == h->control_tail);
1263 h->reconnecting = GNUNET_NO;
1264 h->client = GNUNET_CLIENT_connect ("transport", h->cfg);
1266 GNUNET_assert (NULL != h->client);
1267 schedule_control_transmit (h, sizeof (struct StartMessage),
1273 * Function that will schedule the job that will try
1274 * to connect us again to the client.
1276 * @param h transport service to reconnect
1279 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
1281 struct GNUNET_TRANSPORT_TransmitHandle *th;
1283 GNUNET_assert (NULL == h->reconnect_task);
1286 GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
1289 if (NULL != h->client)
1291 GNUNET_CLIENT_disconnect (h->client);
1293 /* LOG (GNUNET_ERROR_TYPE_ERROR,
1294 "Client disconnect done \n");*/
1296 /* Forget about all neighbours that we used to be connected to */
1297 GNUNET_CONTAINER_multipeermap_iterate (h->neighbours,
1300 if (NULL != h->quota_task)
1302 GNUNET_SCHEDULER_cancel (h->quota_task);
1303 h->quota_task = NULL;
1305 while ((NULL != (th = h->control_head)))
1307 GNUNET_CONTAINER_DLL_remove (h->control_head,
1310 th->notify (th->notify_cls,
1315 LOG (GNUNET_ERROR_TYPE_DEBUG,
1316 "Scheduling task to reconnect to transport service in %s.\n",
1317 GNUNET_STRINGS_relative_time_to_string (h->reconnect_delay,
1320 GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
1323 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
1328 * Cancel control request for transmission to the transport service.
1330 * @param th handle to the transport service
1331 * @param tth transmit handle to cancel
1334 cancel_control_transmit (struct GNUNET_TRANSPORT_Handle *th,
1335 struct GNUNET_TRANSPORT_TransmitHandle *tth)
1337 LOG (GNUNET_ERROR_TYPE_DEBUG,
1338 "Canceling transmit of contral transmission requested\n");
1339 GNUNET_CONTAINER_DLL_remove (th->control_head,
1347 * Send #GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT message to the
1350 * @param cls the `struct GNUNET_TRANSPORT_TryConnectHandle`
1351 * @param size number of bytes available in @a buf
1352 * @param buf where to copy the message
1353 * @return number of bytes copied to @a buf
1356 send_try_connect (void *cls,
1360 struct GNUNET_TRANSPORT_TryConnectHandle *tch = cls;
1361 struct TransportRequestConnectMessage msg;
1366 LOG (GNUNET_ERROR_TYPE_DEBUG,
1367 "Discarding `%s' request to `%s' due to error in transport service connection.\n",
1369 GNUNET_i2s (&tch->pid));
1370 if (NULL != tch->cb)
1371 tch->cb (tch->cb_cls,
1373 GNUNET_TRANSPORT_try_connect_cancel (tch);
1376 LOG (GNUNET_ERROR_TYPE_DEBUG,
1377 "Transmitting `%s' request with respect to `%s'.\n",
1379 GNUNET_i2s (&tch->pid));
1380 GNUNET_assert (size >= sizeof (struct TransportRequestConnectMessage));
1381 msg.header.size = htons (sizeof (struct TransportRequestConnectMessage));
1382 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT);
1383 msg.reserved = htonl (0);
1384 msg.peer = tch->pid;
1385 memcpy (buf, &msg, sizeof (msg));
1386 if (NULL != tch->cb)
1387 tch->cb (tch->cb_cls, GNUNET_OK);
1388 GNUNET_TRANSPORT_try_connect_cancel (tch);
1389 return sizeof (struct TransportRequestConnectMessage);
1394 * Ask the transport service to establish a connection to
1397 * @param handle connection to transport service
1398 * @param target who we should try to connect to
1399 * @param cb callback to be called when request was transmitted to transport
1401 * @param cb_cls closure for the callback
1402 * @return a `struct GNUNET_TRANSPORT_TryConnectHandle` handle or
1403 * NULL on failure (cb will not be called)
1405 struct GNUNET_TRANSPORT_TryConnectHandle *
1406 GNUNET_TRANSPORT_try_connect (struct GNUNET_TRANSPORT_Handle *handle,
1407 const struct GNUNET_PeerIdentity *target,
1408 GNUNET_TRANSPORT_TryConnectCallback cb,
1411 struct GNUNET_TRANSPORT_TryConnectHandle *tch;
1413 if (NULL == handle->client)
1415 tch = GNUNET_new (struct GNUNET_TRANSPORT_TryConnectHandle);
1419 tch->cb_cls = cb_cls;
1420 tch->tth = schedule_control_transmit (handle,
1421 sizeof (struct TransportRequestConnectMessage),
1422 &send_try_connect, tch);
1423 GNUNET_CONTAINER_DLL_insert (handle->tc_head,
1431 * Cancel the request to transport to try a connect
1432 * Callback will not be called
1434 * @param tch the handle to cancel
1437 GNUNET_TRANSPORT_try_connect_cancel (struct GNUNET_TRANSPORT_TryConnectHandle *tch)
1439 struct GNUNET_TRANSPORT_Handle *th;
1442 if (NULL != tch->tth)
1443 cancel_control_transmit (th, tch->tth);
1444 GNUNET_CONTAINER_DLL_remove (th->tc_head,
1452 * Send HELLO message to the service.
1454 * @param cls the HELLO message to send
1455 * @param size number of bytes available in @a buf
1456 * @param buf where to copy the message
1457 * @return number of bytes copied to @a buf
1460 send_hello (void *cls,
1464 struct GNUNET_TRANSPORT_OfferHelloHandle *ohh = cls;
1465 struct GNUNET_MessageHeader *msg = ohh->msg;
1467 struct GNUNET_SCHEDULER_TaskContext tc;
1469 tc.read_ready = NULL;
1470 tc.write_ready = NULL;
1471 tc.reason = GNUNET_SCHEDULER_REASON_TIMEOUT;
1474 LOG (GNUNET_ERROR_TYPE_DEBUG,
1475 "Timeout while trying to transmit `%s' request.\n",
1477 if (NULL != ohh->cont)
1478 ohh->cont (ohh->cls,
1481 GNUNET_CONTAINER_DLL_remove (ohh->th->oh_head,
1487 LOG (GNUNET_ERROR_TYPE_DEBUG,
1488 "Transmitting `%s' request.\n",
1490 ssize = ntohs (msg->size);
1491 GNUNET_assert (size >= ssize);
1496 tc.reason = GNUNET_SCHEDULER_REASON_READ_READY;
1497 if (NULL != ohh->cont)
1498 ohh->cont (ohh->cls,
1500 GNUNET_CONTAINER_DLL_remove (ohh->th->oh_head,
1509 * Send traffic metric message to the service.
1511 * @param cls the message to send
1512 * @param size number of bytes available in @a buf
1513 * @param buf where to copy the message
1514 * @return number of bytes copied to @a buf
1517 send_metric (void *cls,
1521 struct TrafficMetricMessage *msg = cls;
1526 LOG (GNUNET_ERROR_TYPE_DEBUG,
1527 "Timeout while trying to transmit TRAFFIC_METRIC request.\n");
1531 LOG (GNUNET_ERROR_TYPE_DEBUG,
1532 "Transmitting TRAFFIC_METRIC request.\n");
1533 ssize = ntohs (msg->header.size);
1534 GNUNET_assert (size >= ssize);
1535 memcpy (buf, msg, ssize);
1542 * Set transport metrics for a peer and a direction
1544 * @param handle transport handle
1545 * @param peer the peer to set the metric for
1546 * @param prop the performance metrics to set
1547 * @param delay_in inbound delay to introduce
1548 * @param delay_out outbound delay to introduce
1550 * Note: Delay restrictions in receiving direction will be enforced
1551 * with one message delay.
1554 GNUNET_TRANSPORT_set_traffic_metric (struct GNUNET_TRANSPORT_Handle *handle,
1555 const struct GNUNET_PeerIdentity *peer,
1556 const struct GNUNET_ATS_Properties *prop,
1557 struct GNUNET_TIME_Relative delay_in,
1558 struct GNUNET_TIME_Relative delay_out)
1560 struct TrafficMetricMessage *msg;
1562 msg = GNUNET_new (struct TrafficMetricMessage);
1563 msg->header.size = htons (sizeof (struct TrafficMetricMessage));
1564 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TRAFFIC_METRIC);
1565 msg->reserved = htonl (0);
1567 GNUNET_ATS_properties_hton (&msg->properties,
1569 msg->delay_in = GNUNET_TIME_relative_hton (delay_in);
1570 msg->delay_out = GNUNET_TIME_relative_hton (delay_out);
1571 schedule_control_transmit (handle,
1572 sizeof (struct TrafficMetricMessage),
1579 * Offer the transport service the HELLO of another peer. Note that
1580 * the transport service may just ignore this message if the HELLO is
1581 * malformed or useless due to our local configuration.
1583 * @param handle connection to transport service
1584 * @param hello the hello message
1585 * @param cont continuation to call when HELLO has been sent,
1586 * tc reason #GNUNET_SCHEDULER_REASON_TIMEOUT for fail
1587 * tc reasong #GNUNET_SCHEDULER_REASON_READ_READY for success
1588 * @param cont_cls closure for @a cont
1589 * @return a `struct GNUNET_TRANSPORT_OfferHelloHandle` handle or NULL on failure,
1590 * in case of failure @a cont will not be called
1593 struct GNUNET_TRANSPORT_OfferHelloHandle *
1594 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
1595 const struct GNUNET_MessageHeader *hello,
1596 GNUNET_SCHEDULER_TaskCallback cont,
1599 struct GNUNET_TRANSPORT_OfferHelloHandle *ohh;
1600 struct GNUNET_MessageHeader *msg;
1601 struct GNUNET_PeerIdentity peer;
1604 if (NULL == handle->client)
1606 GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
1607 size = ntohs (hello->size);
1608 GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
1610 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hello,
1617 msg = GNUNET_malloc (size);
1618 memcpy (msg, hello, size);
1619 LOG (GNUNET_ERROR_TYPE_DEBUG,
1620 "Offering HELLO message of `%s' to transport for validation.\n",
1621 GNUNET_i2s (&peer));
1623 ohh = GNUNET_new (struct GNUNET_TRANSPORT_OfferHelloHandle);
1626 ohh->cls = cont_cls;
1628 ohh->tth = schedule_control_transmit (handle,
1632 GNUNET_CONTAINER_DLL_insert (handle->oh_head,
1640 * Cancel the request to transport to offer the HELLO message
1642 * @param ohh the handle for the operation to cancel
1645 GNUNET_TRANSPORT_offer_hello_cancel (struct GNUNET_TRANSPORT_OfferHelloHandle *ohh)
1647 struct GNUNET_TRANSPORT_Handle *th = ohh->th;
1649 cancel_control_transmit (ohh->th, ohh->tth);
1650 GNUNET_CONTAINER_DLL_remove (th->oh_head,
1653 GNUNET_free (ohh->msg);
1659 * Checks if a given peer is connected to us
1661 * @param handle connection to transport service
1662 * @param peer the peer to check
1663 * @return #GNUNET_YES (connected) or #GNUNET_NO (disconnected)
1666 GNUNET_TRANSPORT_check_peer_connected (struct GNUNET_TRANSPORT_Handle *handle,
1667 const struct GNUNET_PeerIdentity *peer)
1670 GNUNET_CONTAINER_multipeermap_contains (handle->neighbours,
1678 * Task to call the HelloUpdateCallback of the GetHelloHandle
1680 * @param cls the `struct GNUNET_TRANSPORT_GetHelloHandle`
1681 * @param tc the scheduler task context
1684 call_hello_update_cb_async (void *cls,
1685 const struct GNUNET_SCHEDULER_TaskContext *tc)
1687 struct GNUNET_TRANSPORT_GetHelloHandle *ghh = cls;
1689 GNUNET_assert (NULL != ghh->handle->my_hello);
1690 GNUNET_assert (NULL != ghh->notify_task);
1691 ghh->notify_task = NULL;
1692 ghh->rec (ghh->rec_cls,
1693 ghh->handle->my_hello);
1698 * Obtain the HELLO message for this peer. The callback given in this function
1699 * is never called synchronously.
1701 * @param handle connection to transport service
1702 * @param rec function to call with the HELLO, sender will be our peer
1703 * identity; message and sender will be NULL on timeout
1704 * (handshake with transport service pending/failed).
1705 * cost estimate will be 0.
1706 * @param rec_cls closure for @a rec
1707 * @return handle to cancel the operation
1709 struct GNUNET_TRANSPORT_GetHelloHandle *
1710 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
1711 GNUNET_TRANSPORT_HelloUpdateCallback rec,
1714 struct GNUNET_TRANSPORT_GetHelloHandle *hwl;
1716 hwl = GNUNET_new (struct GNUNET_TRANSPORT_GetHelloHandle);
1718 hwl->rec_cls = rec_cls;
1719 hwl->handle = handle;
1720 GNUNET_CONTAINER_DLL_insert (handle->hwl_head,
1723 if (NULL != handle->my_hello)
1724 hwl->notify_task = GNUNET_SCHEDULER_add_now (&call_hello_update_cb_async,
1731 * Stop receiving updates about changes to our HELLO message.
1733 * @param ghh handle to cancel
1736 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_GetHelloHandle *ghh)
1738 struct GNUNET_TRANSPORT_Handle *handle = ghh->handle;
1740 if (NULL != ghh->notify_task)
1741 GNUNET_SCHEDULER_cancel (ghh->notify_task);
1742 GNUNET_CONTAINER_DLL_remove (handle->hwl_head, handle->hwl_tail, ghh);
1748 * Connect to the transport service. Note that the connection may
1749 * complete (or fail) asynchronously.
1751 * @param cfg configuration to use
1752 * @param self our own identity (API should check that it matches
1753 * the identity found by transport), or NULL (no check)
1754 * @param cls closure for the callbacks
1755 * @param rec receive function to call
1756 * @param nc function to call on connect events
1757 * @param nd function to call on disconnect events
1758 * @return NULL on error
1760 struct GNUNET_TRANSPORT_Handle *
1761 GNUNET_TRANSPORT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1762 const struct GNUNET_PeerIdentity *self,
1764 GNUNET_TRANSPORT_ReceiveCallback rec,
1765 GNUNET_TRANSPORT_NotifyConnect nc,
1766 GNUNET_TRANSPORT_NotifyDisconnect nd)
1768 return GNUNET_TRANSPORT_connect2 (cfg,
1779 * Connect to the transport service. Note that the connection may
1780 * complete (or fail) asynchronously.
1782 * @param cfg configuration to use
1783 * @param self our own identity (API should check that it matches
1784 * the identity found by transport), or NULL (no check)
1785 * @param cls closure for the callbacks
1786 * @param rec receive function to call
1787 * @param nc function to call on connect events
1788 * @param nd function to call on disconnect events
1789 * @param neb function to call if we have excess bandwidth to a peer
1790 * @return NULL on error
1792 struct GNUNET_TRANSPORT_Handle *
1793 GNUNET_TRANSPORT_connect2 (const struct GNUNET_CONFIGURATION_Handle *cfg,
1794 const struct GNUNET_PeerIdentity *self,
1796 GNUNET_TRANSPORT_ReceiveCallback rec,
1797 GNUNET_TRANSPORT_NotifyConnect nc,
1798 GNUNET_TRANSPORT_NotifyDisconnect nd,
1799 GNUNET_TRANSPORT_NotifyExcessBandwidth neb)
1801 struct GNUNET_TRANSPORT_Handle *ret;
1803 ret = GNUNET_new (struct GNUNET_TRANSPORT_Handle);
1807 ret->check_self = GNUNET_YES;
1815 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1816 LOG (GNUNET_ERROR_TYPE_DEBUG,
1817 "Connecting to transport service.\n");
1818 ret->client = GNUNET_CLIENT_connect ("transport",
1820 if (NULL == ret->client)
1826 GNUNET_CONTAINER_multipeermap_create (STARTING_NEIGHBOURS_SIZE,
1829 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1830 schedule_control_transmit (ret,
1831 sizeof (struct StartMessage),
1839 * Disconnect from the transport service.
1841 * @param handle handle to the service as returned from #GNUNET_TRANSPORT_connect()
1844 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1846 LOG (GNUNET_ERROR_TYPE_DEBUG,
1847 "Transport disconnect called!\n");
1848 /* this disconnects all neighbours... */
1849 if (NULL == handle->reconnect_task)
1850 disconnect_and_schedule_reconnect (handle);
1851 /* and now we stop trying to connect again... */
1852 if (NULL != handle->reconnect_task)
1854 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1855 handle->reconnect_task = NULL;
1857 GNUNET_CONTAINER_multipeermap_destroy (handle->neighbours);
1858 handle->neighbours = NULL;
1859 if (NULL != handle->quota_task)
1861 GNUNET_SCHEDULER_cancel (handle->quota_task);
1862 handle->quota_task = NULL;
1864 GNUNET_free_non_null (handle->my_hello);
1865 handle->my_hello = NULL;
1866 GNUNET_assert (NULL == handle->tc_head);
1867 GNUNET_assert (NULL == handle->tc_tail);
1868 GNUNET_assert (NULL == handle->hwl_head);
1869 GNUNET_assert (NULL == handle->hwl_tail);
1870 GNUNET_CONTAINER_heap_destroy (handle->ready_heap);
1871 handle->ready_heap = NULL;
1872 GNUNET_free (handle);
1877 * Check if we could queue a message of the given size for
1878 * transmission. The transport service will take both its
1879 * internal buffers and bandwidth limits imposed by the
1880 * other peer into consideration when answering this query.
1882 * @param handle connection to transport service
1883 * @param target who should receive the message
1884 * @param size how big is the message we want to transmit?
1885 * @param timeout after how long should we give up (and call
1886 * notify with buf NULL and size 0)?
1887 * @param notify function to call when we are ready to
1888 * send such a message
1889 * @param notify_cls closure for @a notify
1890 * @return NULL if someone else is already waiting to be notified
1891 * non-NULL if the notify callback was queued (can be used to cancel
1892 * using #GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1894 struct GNUNET_TRANSPORT_TransmitHandle *
1895 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle *handle,
1896 const struct GNUNET_PeerIdentity *target,
1898 struct GNUNET_TIME_Relative timeout,
1899 GNUNET_TRANSPORT_TransmitReadyNotify notify,
1902 struct Neighbour *n;
1903 struct GNUNET_TRANSPORT_TransmitHandle *th;
1904 struct GNUNET_TIME_Relative delay;
1906 n = neighbour_find (handle, target);
1909 /* use GNUNET_TRANSPORT_try_connect first, only use this function
1910 * once a connection has been established */
1916 /* attempt to send two messages at the same time to the same peer */
1920 GNUNET_assert (NULL == n->hn);
1921 th = GNUNET_new (struct GNUNET_TRANSPORT_TransmitHandle);
1923 th->notify = notify;
1924 th->notify_cls = notify_cls;
1925 th->request_start = GNUNET_TIME_absolute_get ();
1926 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1927 th->notify_size = size;
1929 /* calculate when our transmission should be ready */
1930 delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
1931 size + n->traffic_overhead);
1932 n->traffic_overhead = 0;
1933 if (delay.rel_value_us > timeout.rel_value_us)
1934 delay.rel_value_us = 0; /* notify immediately (with failure) */
1935 if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
1936 LOG (GNUNET_ERROR_TYPE_WARNING,
1937 "At bandwidth %u byte/s next transmission to %s in %s\n",
1938 (unsigned int) n->out_tracker.available_bytes_per_s__,
1939 GNUNET_i2s (target),
1940 GNUNET_STRINGS_relative_time_to_string (delay,
1943 LOG (GNUNET_ERROR_TYPE_DEBUG,
1944 "At bandwidth %u byte/s next transmission to %s in %s\n",
1945 (unsigned int) n->out_tracker.available_bytes_per_s__,
1946 GNUNET_i2s (target),
1947 GNUNET_STRINGS_relative_time_to_string (delay,
1949 n->hn = GNUNET_CONTAINER_heap_insert (handle->ready_heap,
1951 delay.rel_value_us);
1952 schedule_transmission (handle);
1958 * Cancel the specified transmission-ready notification.
1960 * @param th handle returned from #GNUNET_TRANSPORT_notify_transmit_ready()
1963 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct GNUNET_TRANSPORT_TransmitHandle *th)
1965 struct Neighbour *n;
1967 GNUNET_assert (NULL == th->next);
1968 GNUNET_assert (NULL == th->prev);
1970 GNUNET_assert (th == n->th);
1974 GNUNET_CONTAINER_heap_remove_node (n->hn);
1979 GNUNET_assert (NULL != th->timeout_task);
1980 GNUNET_SCHEDULER_cancel (th->timeout_task);
1981 th->timeout_task = NULL;
1987 /* end of transport_api.c */