2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/transport_api.c
23 * @brief library to access the low-level P2P IO service
24 * @author Christian Grothoff
27 * - adjust testcases to use new 'try connect' style (should be easy, breaks API compatibility!)
28 * - adjust core service to use new 'try connect' style (should be MUCH nicer there as well!)
32 #include "gnunet_constants.h"
33 #include "gnunet_bandwidth_lib.h"
34 #include "gnunet_client_lib.h"
35 #include "gnunet_constants.h"
36 #include "gnunet_container_lib.h"
37 #include "gnunet_arm_service.h"
38 #include "gnunet_hello_lib.h"
39 #include "gnunet_protocols.h"
40 #include "gnunet_server_lib.h"
41 #include "gnunet_time_lib.h"
42 #include "gnunet_transport_service.h"
43 #include "transport.h"
45 #define LOG(kind,...) GNUNET_log_from (kind, "transport-api",__VA_ARGS__)
48 * How large to start with for the hashmap of neighbours.
50 #define STARTING_NEIGHBOURS_SIZE 16
53 * Handle for a message that should be transmitted to the service.
54 * Used for both control messages and normal messages.
56 struct GNUNET_TRANSPORT_TransmitHandle
60 * We keep all requests in a DLL.
62 struct GNUNET_TRANSPORT_TransmitHandle *next;
65 * We keep all requests in a DLL.
67 struct GNUNET_TRANSPORT_TransmitHandle *prev;
70 * Neighbour for this handle, NULL for control messages.
72 struct Neighbour *neighbour;
75 * Function to call when notify_size bytes are available
78 GNUNET_CONNECTION_TransmitReadyNotify notify;
86 * Timeout for this request, 0 for control messages.
88 struct GNUNET_TIME_Absolute timeout;
91 * Task to trigger request timeout if the request is stalled due to
94 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
97 * How many bytes is our notify callback waiting for?
102 * How important is this message? Not used for control messages.
110 * Entry in hash table of all of our current neighbours.
115 * Overall transport handle.
117 struct GNUNET_TRANSPORT_Handle *h;
120 * Active transmit handle or NULL.
122 struct GNUNET_TRANSPORT_TransmitHandle *th;
125 * Identity of this neighbour.
127 struct GNUNET_PeerIdentity id;
130 * Outbound bandwidh tracker.
132 struct GNUNET_BANDWIDTH_Tracker out_tracker;
135 * Entry in our readyness heap (which is sorted by 'next_ready'
136 * value). NULL if there is no pending transmission request for
137 * this neighbour or if we're waiting for 'is_ready' to become
138 * true AFTER the 'out_tracker' suggested that this peer's quota
139 * has been satisfied (so once 'is_ready' goes to GNUNET_YES,
140 * we should immediately go back into the heap).
142 struct GNUNET_CONTAINER_HeapNode *hn;
145 * Is this peer currently ready to receive a message?
150 * Sending consumed more bytes on wire than payload was announced
151 * This overhead is added to the delay of next sending operation
153 size_t traffic_overhead;
158 * Linked list of functions to call whenever our HELLO is updated.
160 struct GNUNET_TRANSPORT_GetHelloHandle
164 * This is a doubly linked list.
166 struct GNUNET_TRANSPORT_GetHelloHandle *next;
169 * This is a doubly linked list.
171 struct GNUNET_TRANSPORT_GetHelloHandle *prev;
176 struct GNUNET_TRANSPORT_Handle *handle;
179 * Callback to call once we got our HELLO.
181 GNUNET_TRANSPORT_HelloUpdateCallback rec;
184 * Task for calling the HelloUpdateCallback when we already have a HELLO
186 GNUNET_SCHEDULER_TaskIdentifier notify_task;
196 * Linked list for all try-connect requests
198 struct GNUNET_TRANSPORT_TryConnectHandle
200 struct GNUNET_TRANSPORT_TryConnectHandle *prev;
201 struct GNUNET_TRANSPORT_TryConnectHandle *next;
203 struct GNUNET_PeerIdentity pid;
205 struct GNUNET_TRANSPORT_Handle *th;
206 struct GNUNET_TRANSPORT_TransmitHandle *tth;
207 GNUNET_TRANSPORT_TryConnectCallback cb;
213 * Linked list for all try-connect requests
215 struct GNUNET_TRANSPORT_OfferHelloHandle
217 struct GNUNET_TRANSPORT_OfferHelloHandle *prev;
218 struct GNUNET_TRANSPORT_OfferHelloHandle *next;
220 struct GNUNET_TRANSPORT_Handle *th;
222 struct GNUNET_TRANSPORT_TransmitHandle *tth;
223 GNUNET_SCHEDULER_Task cont;
227 struct GNUNET_MessageHeader *msg;
232 * Handle for the transport service (includes all of the
233 * state for the transport service).
235 struct GNUNET_TRANSPORT_Handle
239 * Closure for the callbacks.
244 * Function to call for received data.
246 GNUNET_TRANSPORT_ReceiveCallback rec;
249 * function to call on connect events
251 GNUNET_TRANSPORT_NotifyConnect nc_cb;
254 * function to call on disconnect events
256 GNUNET_TRANSPORT_NotifyDisconnect nd_cb;
259 * Head of DLL of control messages.
261 struct GNUNET_TRANSPORT_TransmitHandle *control_head;
264 * Tail of DLL of control messages.
266 struct GNUNET_TRANSPORT_TransmitHandle *control_tail;
269 * The current HELLO message for this peer. Updated
270 * whenever transports change their addresses.
272 struct GNUNET_HELLO_Message *my_hello;
275 * My client connection to the transport service.
277 struct GNUNET_CLIENT_Connection *client;
280 * Handle to our registration with the client for notification.
282 struct GNUNET_CLIENT_TransmitHandle *cth;
285 * Linked list of pending requests for our HELLO.
287 struct GNUNET_TRANSPORT_GetHelloHandle *hwl_head;
290 * Linked list of pending requests for our HELLO.
292 struct GNUNET_TRANSPORT_GetHelloHandle *hwl_tail;
295 * Linked list of pending try connect requests head
297 struct GNUNET_TRANSPORT_TryConnectHandle *tc_head;
300 * Linked list of pending try connect requests tail
302 struct GNUNET_TRANSPORT_TryConnectHandle *tc_tail;
305 * Linked list of pending offer HELLO requests head
307 struct GNUNET_TRANSPORT_OfferHelloHandle *oh_head;
310 * Linked list of pending offer HELLO requests tail
312 struct GNUNET_TRANSPORT_OfferHelloHandle *oh_tail;
317 const struct GNUNET_CONFIGURATION_Handle *cfg;
320 * Hash map of the current connected neighbours of this peer.
321 * Maps peer identities to 'struct Neighbour' entries.
323 struct GNUNET_CONTAINER_MultiHashMap *neighbours;
326 * Heap sorting peers with pending messages by the timestamps that
327 * specify when we could next send a message to the respective peer.
328 * Excludes control messages (which can always go out immediately).
329 * Maps time stamps to 'struct Neighbour' entries.
331 struct GNUNET_CONTAINER_Heap *ready_heap;
334 * Peer identity as assumed by this process, or all zeros.
336 struct GNUNET_PeerIdentity self;
339 * ID of the task trying to reconnect to the service.
341 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
344 * ID of the task trying to trigger transmission for a peer while
345 * maintaining bandwidth quotas. In use if there are no control
346 * messages and the smallest entry in the 'ready_heap' has a time
347 * stamp in the future.
349 GNUNET_SCHEDULER_TaskIdentifier quota_task;
352 * Delay until we try to reconnect.
354 struct GNUNET_TIME_Relative reconnect_delay;
357 * Should we check that 'self' matches what the service thinks?
358 * (if GNUNET_NO, then 'self' is all zeros!).
363 * Reconnect in progress
371 * Schedule the task to send one message, either from the control
372 * list or the peer message queues to the service.
374 * @param h transport service to schedule a transmission for
377 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h);
381 * Function that will schedule the job that will try
382 * to connect us again to the client.
384 * @param h transport service to reconnect
387 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h);
391 * Get the neighbour list entry for the given peer
393 * @param h our context
394 * @param peer peer to look up
395 * @return NULL if no such peer entry exists
397 static struct Neighbour *
398 neighbour_find (struct GNUNET_TRANSPORT_Handle *h,
399 const struct GNUNET_PeerIdentity *peer)
401 return GNUNET_CONTAINER_multihashmap_get (h->neighbours, &peer->hashPubKey);
406 * Add neighbour to our list
408 * @return NULL if this API is currently disconnecting from the service
410 static struct Neighbour *
411 neighbour_add (struct GNUNET_TRANSPORT_Handle *h,
412 const struct GNUNET_PeerIdentity *pid)
416 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating entry for neighbour `%4s'.\n",
418 n = GNUNET_malloc (sizeof (struct Neighbour));
421 n->is_ready = GNUNET_YES;
422 n->traffic_overhead = 0;
423 GNUNET_BANDWIDTH_tracker_init (&n->out_tracker,
424 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
425 MAX_BANDWIDTH_CARRY_S);
426 GNUNET_assert (GNUNET_OK ==
427 GNUNET_CONTAINER_multihashmap_put (h->neighbours,
428 &n->id.hashPubKey, n,
429 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
435 * Iterator over hash map entries, for deleting state of a neighbour.
437 * @param cls the 'struct GNUNET_TRANSPORT_Handle*'
438 * @param key peer identity
439 * @param value value in the hash map, the neighbour entry to delete
440 * @return GNUNET_YES if we should continue to
445 neighbour_delete (void *cls, const struct GNUNET_HashCode * key, void *value)
447 struct GNUNET_TRANSPORT_Handle *handle = cls;
448 struct Neighbour *n = value;
450 if (NULL != handle->nd_cb)
451 handle->nd_cb (handle->cls, &n->id);
452 GNUNET_assert (NULL == n->th);
453 GNUNET_assert (NULL == n->hn);
454 GNUNET_assert (GNUNET_YES ==
455 GNUNET_CONTAINER_multihashmap_remove (handle->neighbours, key,
463 * Function we use for handling incoming messages.
465 * @param cls closure (struct GNUNET_TRANSPORT_Handle *)
466 * @param msg message received, NULL on timeout or fatal error
469 demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
471 struct GNUNET_TRANSPORT_Handle *h = cls;
472 const struct DisconnectInfoMessage *dim;
473 const struct ConnectInfoMessage *cim;
474 const struct InboundMessage *im;
475 const struct GNUNET_MessageHeader *imm;
476 const struct SendOkMessage *okm;
477 const struct QuotaSetMessage *qm;
478 const struct GNUNET_ATS_Information *ats;
479 struct GNUNET_TRANSPORT_GetHelloHandle *hwl;
480 struct GNUNET_TRANSPORT_GetHelloHandle *next_hwl;
482 struct GNUNET_PeerIdentity me;
486 uint32_t bytes_physical;
488 GNUNET_assert (h->client != NULL);
491 LOG (GNUNET_ERROR_TYPE_DEBUG,
492 "Error receiving from transport service, disconnecting temporarily.\n");
493 disconnect_and_schedule_reconnect (h);
496 GNUNET_CLIENT_receive (h->client, &demultiplexer, h,
497 GNUNET_TIME_UNIT_FOREVER_REL);
498 size = ntohs (msg->size);
499 switch (ntohs (msg->type))
501 case GNUNET_MESSAGE_TYPE_HELLO:
503 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg, &me))
508 LOG (GNUNET_ERROR_TYPE_DEBUG,
509 "Receiving (my own) `%s' message, I am `%4s'.\n", "HELLO",
511 GNUNET_free_non_null (h->my_hello);
513 if (size < sizeof (struct GNUNET_MessageHeader))
518 h->my_hello = GNUNET_malloc (size);
519 memcpy (h->my_hello, msg, size);
523 next_hwl = hwl->next;
524 hwl->rec (hwl->rec_cls,
525 (const struct GNUNET_MessageHeader *) h->my_hello);
529 case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
530 if (size < sizeof (struct ConnectInfoMessage))
535 cim = (const struct ConnectInfoMessage *) msg;
536 ats_count = ntohl (cim->ats_count);
538 sizeof (struct ConnectInfoMessage) +
539 ats_count * sizeof (struct GNUNET_ATS_Information))
544 ats = (const struct GNUNET_ATS_Information *) &cim[1];
545 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message for `%4s'.\n",
546 "CONNECT", GNUNET_i2s (&cim->id));
547 n = neighbour_find (h, &cim->id);
553 n = neighbour_add (h, &cim->id);
554 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message for `%4s' with quota %u\n",
555 "CONNECT", GNUNET_i2s (&cim->id), ntohl (cim->quota_out.value__));
556 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker, cim->quota_out);
557 if (h->nc_cb != NULL)
558 h->nc_cb (h->cls, &n->id, ats, ats_count);
560 case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
561 if (size != sizeof (struct DisconnectInfoMessage))
566 dim = (const struct DisconnectInfoMessage *) msg;
567 GNUNET_break (ntohl (dim->reserved) == 0);
568 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message for `%4s'.\n",
569 "DISCONNECT", GNUNET_i2s (&dim->peer));
570 n = neighbour_find (h, &dim->peer);
576 neighbour_delete (h, &dim->peer.hashPubKey, n);
578 case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
579 if (size != sizeof (struct SendOkMessage))
584 okm = (const struct SendOkMessage *) msg;
585 bytes_msg = ntohl (okm->bytes_msg);
586 bytes_physical = ntohl (okm->bytes_physical);
587 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message, transmission %s.\n",
588 "SEND_OK", ntohl (okm->success) == GNUNET_OK ? "succeeded" : "failed");
590 n = neighbour_find (h, &okm->peer);
594 if (bytes_physical >= bytes_msg)
596 LOG (GNUNET_ERROR_TYPE_DEBUG, "Overhead for %u byte message: %u \n",
597 bytes_msg, bytes_physical - bytes_msg);
598 n->traffic_overhead += bytes_physical - bytes_msg;
600 GNUNET_break (GNUNET_NO == n->is_ready);
601 n->is_ready = GNUNET_YES;
602 if ((n->th != NULL) && (n->hn == NULL))
604 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->th->timeout_task);
605 GNUNET_SCHEDULER_cancel (n->th->timeout_task);
606 n->th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
607 /* we've been waiting for this (congestion, not quota,
608 * caused delayed transmission) */
609 n->hn = GNUNET_CONTAINER_heap_insert (h->ready_heap, n, 0);
610 schedule_transmission (h);
613 case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
614 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message.\n", "RECV");
616 sizeof (struct InboundMessage) + sizeof (struct GNUNET_MessageHeader))
621 im = (const struct InboundMessage *) msg;
622 imm = (const struct GNUNET_MessageHeader *) &im[1];
623 if (ntohs (imm->size) + sizeof (struct InboundMessage) != size)
628 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %u from `%4s'.\n",
629 ntohs (imm->type), GNUNET_i2s (&im->peer));
630 n = neighbour_find (h, &im->peer);
637 h->rec (h->cls, &im->peer, imm);
639 case GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA:
640 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message.\n", "SET_QUOTA");
641 if (size != sizeof (struct QuotaSetMessage))
646 qm = (const struct QuotaSetMessage *) msg;
647 n = neighbour_find (h, &qm->peer);
650 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message for `%4s' with quota %u\n",
651 "SET_QUOTA", GNUNET_i2s (&qm->peer), ntohl (qm->quota.value__));
652 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker, qm->quota);
655 LOG (GNUNET_ERROR_TYPE_ERROR,
656 _("Received unexpected message of type %u in %s:%u\n"),
657 ntohs (msg->type), __FILE__, __LINE__);
665 * A transmission request could not be satisfied because of
666 * network congestion. Notify the initiator and clean up.
668 * @param cls the 'struct GNUNET_TRANSPORT_TransmitHandle'
669 * @param tc scheduler context
672 timeout_request_due_to_congestion (void *cls,
673 const struct GNUNET_SCHEDULER_TaskContext
676 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
677 struct Neighbour *n = th->neighbour;
679 n->th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
680 GNUNET_assert (th == n->th);
681 GNUNET_assert (NULL == n->hn);
683 th->notify (th->notify_cls, 0, NULL);
689 * Transmit message(s) to service.
691 * @param cls handle to transport
692 * @param size number of bytes available in buf
693 * @param buf where to copy the message
694 * @return number of bytes copied to buf
697 transport_notify_ready (void *cls, size_t size, void *buf)
699 struct GNUNET_TRANSPORT_Handle *h = cls;
700 struct GNUNET_TRANSPORT_TransmitHandle *th;
703 struct OutboundMessage obm;
708 GNUNET_assert (NULL != h->client);
712 /* transmission failed */
713 disconnect_and_schedule_reconnect (h);
719 /* first send control messages */
720 while ((NULL != (th = h->control_head)) && (th->notify_size <= size))
722 GNUNET_CONTAINER_DLL_remove (h->control_head, h->control_tail, th);
723 nret = th->notify (th->notify_cls, size, &cbuf[ret]);
724 LOG (GNUNET_ERROR_TYPE_DEBUG, "Added %u bytes of control message at %u\n",
731 /* then, if possible and no control messages pending, send data messages */
732 while ((NULL == h->control_head) &&
733 (NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap))))
735 if (GNUNET_YES != n->is_ready)
737 /* peer not ready, wait for notification! */
738 GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
740 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == n->th->timeout_task);
741 n->th->timeout_task =
742 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
744 &timeout_request_due_to_congestion,
749 if (th->notify_size + sizeof (struct OutboundMessage) > size)
750 break; /* does not fit */
751 if (GNUNET_BANDWIDTH_tracker_get_delay
752 (&n->out_tracker, th->notify_size).rel_value > 0)
753 break; /* too early */
754 GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
757 n->is_ready = GNUNET_NO;
758 GNUNET_assert (size >= sizeof (struct OutboundMessage));
760 th->notify (th->notify_cls, size - sizeof (struct OutboundMessage),
761 &cbuf[ret + sizeof (struct OutboundMessage)]);
762 GNUNET_assert (mret <= size - sizeof (struct OutboundMessage));
765 GNUNET_assert (mret + sizeof (struct OutboundMessage) <
766 GNUNET_SERVER_MAX_MESSAGE_SIZE);
767 obm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
768 obm.header.size = htons (mret + sizeof (struct OutboundMessage));
769 obm.priority = htonl (th->priority);
771 GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining
774 memcpy (&cbuf[ret], &obm, sizeof (struct OutboundMessage));
775 ret += (mret + sizeof (struct OutboundMessage));
776 size -= (mret + sizeof (struct OutboundMessage));
777 GNUNET_BANDWIDTH_tracker_consume (&n->out_tracker, mret);
781 /* if there are more pending messages, try to schedule those */
782 schedule_transmission (h);
783 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting %u bytes to transport service\n",
790 * Schedule the task to send one message, either from the control
791 * list or the peer message queues to the service.
793 * @param cls transport service to schedule a transmission for
794 * @param tc scheduler context
797 schedule_transmission_task (void *cls,
798 const struct GNUNET_SCHEDULER_TaskContext *tc)
800 struct GNUNET_TRANSPORT_Handle *h = cls;
802 struct GNUNET_TRANSPORT_TransmitHandle *th;
805 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
806 GNUNET_assert (NULL != h->client);
807 /* destroy all requests that have timed out */
808 while ((NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap))) &&
809 (GNUNET_TIME_absolute_get_remaining (n->th->timeout).rel_value == 0))
811 /* notify client that the request could not be satisfied within
812 * the given time constraints */
815 GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
817 LOG (GNUNET_ERROR_TYPE_DEBUG,
818 "Signalling timeout for transmission to peer %s due to congestion\n",
819 GNUNET_i2s (&n->id));
820 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
825 if (NULL != h->control_head)
827 size = h->control_head->notify_size;
831 n = GNUNET_CONTAINER_heap_peek (h->ready_heap);
833 return; /* no pending messages */
834 size = n->th->notify_size + sizeof (struct OutboundMessage);
836 LOG (GNUNET_ERROR_TYPE_DEBUG, "Calling notify_transmit_ready\n");
838 GNUNET_CLIENT_notify_transmit_ready (h->client, size,
839 GNUNET_TIME_UNIT_FOREVER_REL,
840 GNUNET_NO, &transport_notify_ready,
842 GNUNET_assert (NULL != h->cth);
847 * Schedule the task to send one message, either from the control
848 * list or the peer message queues to the service.
850 * @param h transport service to schedule a transmission for
853 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
855 struct GNUNET_TIME_Relative delay;
858 GNUNET_assert (NULL != h->client);
859 if (h->quota_task != GNUNET_SCHEDULER_NO_TASK)
861 GNUNET_SCHEDULER_cancel (h->quota_task);
862 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
864 if (NULL != h->control_head)
865 delay = GNUNET_TIME_UNIT_ZERO;
866 else if (NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap)))
869 GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
870 n->th->notify_size + n->traffic_overhead);
871 n->traffic_overhead = 0;
874 return; /* no work to be done */
875 LOG (GNUNET_ERROR_TYPE_DEBUG,
876 "Scheduling next transmission to service in %llu ms\n",
877 (unsigned long long) delay.rel_value);
879 GNUNET_SCHEDULER_add_delayed (delay, &schedule_transmission_task, h);
884 * Queue control request for transmission to the transport
887 * @param h handle to the transport service
888 * @param size number of bytes to be transmitted
889 * @param notify function to call to get the content
890 * @param notify_cls closure for notify
891 * @return a GNUNET_TRANSPORT_TransmitHandle
893 static struct GNUNET_TRANSPORT_TransmitHandle *
894 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h, size_t size,
895 GNUNET_CONNECTION_TransmitReadyNotify notify,
898 struct GNUNET_TRANSPORT_TransmitHandle *th;
900 LOG (GNUNET_ERROR_TYPE_DEBUG, "Control transmit of %u bytes requested\n",
902 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
904 th->notify_cls = notify_cls;
905 th->notify_size = size;
906 GNUNET_CONTAINER_DLL_insert_tail (h->control_head, h->control_tail, th);
907 schedule_transmission (h);
913 * Transmit START message to service.
916 * @param size number of bytes available in buf
917 * @param buf where to copy the message
918 * @return number of bytes copied to buf
921 send_start (void *cls, size_t size, void *buf)
923 struct GNUNET_TRANSPORT_Handle *h = cls;
924 struct StartMessage s;
929 /* Can only be shutdown, just give up */
930 LOG (GNUNET_ERROR_TYPE_DEBUG,
931 "Shutdown while trying to transmit `%s' request.\n", "START");
934 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "START");
935 GNUNET_assert (size >= sizeof (struct StartMessage));
936 s.header.size = htons (sizeof (struct StartMessage));
937 s.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
943 s.options = htonl (options);
945 memcpy (buf, &s, sizeof (struct StartMessage));
946 GNUNET_CLIENT_receive (h->client, &demultiplexer, h,
947 GNUNET_TIME_UNIT_FOREVER_REL);
948 return sizeof (struct StartMessage);
953 * Try again to connect to transport service.
955 * @param cls the handle to the transport service
956 * @param tc scheduler context
959 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
961 struct GNUNET_TRANSPORT_Handle *h = cls;
963 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
964 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
966 /* shutdown, just give up */
969 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n");
970 GNUNET_assert (h->client == NULL);
971 GNUNET_assert (h->control_head == NULL);
972 GNUNET_assert (h->control_tail == NULL);
973 h->client = GNUNET_CLIENT_connect ("transport", h->cfg);
974 GNUNET_assert (h->client != NULL);
975 schedule_control_transmit (h, sizeof (struct StartMessage), &send_start, h);
980 * Function that will schedule the job that will try
981 * to connect us again to the client.
983 * @param h transport service to reconnect
986 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
988 struct GNUNET_TRANSPORT_TransmitHandle *th;
990 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
993 GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
996 if (NULL != h->client)
998 GNUNET_CLIENT_disconnect (h->client);
1001 /* Forget about all neighbours that we used to be connected to */
1002 GNUNET_CONTAINER_multihashmap_iterate (h->neighbours, &neighbour_delete, h);
1003 if (h->quota_task != GNUNET_SCHEDULER_NO_TASK)
1005 GNUNET_SCHEDULER_cancel (h->quota_task);
1006 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
1008 while ((NULL != (th = h->control_head)))
1010 GNUNET_CONTAINER_DLL_remove (h->control_head, h->control_tail, th);
1011 th->notify (th->notify_cls, 0, NULL);
1014 LOG (GNUNET_ERROR_TYPE_DEBUG,
1015 "Scheduling task to reconnect to transport service in %llu ms.\n",
1016 h->reconnect_delay.rel_value);
1018 GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
1019 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
1024 * Cancel control request for transmission to the transport service.
1026 * @param th handle to the transport service
1027 * @param tth transmit handle to cancel
1030 cancel_control_transmit (struct GNUNET_TRANSPORT_Handle *th, struct GNUNET_TRANSPORT_TransmitHandle *tth)
1032 GNUNET_assert (NULL != th);
1033 GNUNET_assert (NULL != tth);
1035 LOG (GNUNET_ERROR_TYPE_DEBUG, "Canceling transmit of contral transmission requested\n");
1037 GNUNET_CONTAINER_DLL_remove (th->control_head, th->control_tail, tth);
1044 * Send REQUEST_CONNECT message to the service.
1046 * @param cls the 'struct GNUNET_PeerIdentity'
1047 * @param size number of bytes available in buf
1048 * @param buf where to copy the message
1049 * @return number of bytes copied to buf
1052 send_try_connect (void *cls, size_t size, void *buf)
1054 struct GNUNET_TRANSPORT_TryConnectHandle *tch = cls;
1055 struct TransportRequestConnectMessage msg;
1059 if (NULL != tch->cb)
1060 tch->cb (tch->cb_cls, GNUNET_SYSERR);
1061 GNUNET_CONTAINER_DLL_remove (tch->th->tc_head, tch->th->tc_tail, tch);
1065 LOG (GNUNET_ERROR_TYPE_DEBUG,
1066 "Transmitting `%s' request with respect to `%4s'.\n", "REQUEST_CONNECT",
1067 GNUNET_i2s (&tch->pid));
1068 GNUNET_assert (size >= sizeof (struct TransportRequestConnectMessage));
1069 msg.header.size = htons (sizeof (struct TransportRequestConnectMessage));
1070 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT);
1071 msg.reserved = htonl (0);
1072 msg.peer = tch->pid;
1073 memcpy (buf, &msg, sizeof (msg));
1074 if (NULL != tch->cb)
1075 tch->cb (tch->cb_cls, GNUNET_OK);
1076 GNUNET_CONTAINER_DLL_remove (tch->th->tc_head, tch->th->tc_tail, tch);
1078 return sizeof (struct TransportRequestConnectMessage);
1082 * Ask the transport service to establish a connection to
1085 * @param handle connection to transport service
1086 * @param target who we should try to connect to
1087 * @param cb callback to be called when request was transmitted to transport
1089 * @param cb_cls closure for the callback
1090 * @return a GNUNET_TRANSPORT_TryConnectHandle handle or
1091 * NULL on failure (cb will not be called)
1093 struct GNUNET_TRANSPORT_TryConnectHandle *
1094 GNUNET_TRANSPORT_try_connect (struct GNUNET_TRANSPORT_Handle *handle,
1095 const struct GNUNET_PeerIdentity *target,
1096 GNUNET_TRANSPORT_TryConnectCallback cb,
1099 struct GNUNET_TRANSPORT_TryConnectHandle *tch = NULL;
1101 if (NULL == handle->client)
1104 tch = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TryConnectHandle));
1106 tch->pid = *(target);
1108 tch->cb_cls = cb_cls;
1109 tch->tth = schedule_control_transmit (handle,
1110 sizeof (struct TransportRequestConnectMessage),
1111 &send_try_connect, tch);
1112 GNUNET_CONTAINER_DLL_insert(handle->tc_head, handle->tc_tail, tch);
1118 * Cancel the request to transport to try a connect
1119 * Callback will not be called
1121 * @param tch GNUNET_TRANSPORT_TryConnectHandle handle to cancel
1124 GNUNET_TRANSPORT_try_connect_cancel (struct GNUNET_TRANSPORT_TryConnectHandle *tch)
1126 struct GNUNET_TRANSPORT_Handle *th;
1127 GNUNET_assert (NULL != tch);
1130 cancel_control_transmit (th, tch->tth);
1131 GNUNET_CONTAINER_DLL_remove (th->tc_head, th->tc_tail, tch);
1136 * Send HELLO message to the service.
1138 * @param cls the HELLO message to send
1139 * @param size number of bytes available in buf
1140 * @param buf where to copy the message
1141 * @return number of bytes copied to buf
1144 send_hello (void *cls, size_t size, void *buf)
1146 struct GNUNET_TRANSPORT_OfferHelloHandle *ohh = cls;
1147 struct GNUNET_MessageHeader *msg = ohh->msg;
1149 struct GNUNET_SCHEDULER_TaskContext tc;
1150 tc.read_ready = NULL;
1151 tc.write_ready = NULL;
1152 tc.reason = GNUNET_SCHEDULER_REASON_TIMEOUT;
1156 LOG (GNUNET_ERROR_TYPE_DEBUG,
1157 "Timeout while trying to transmit `%s' request.\n", "HELLO");
1158 if (NULL != ohh->cont)
1159 ohh->cont (ohh->cls, &tc);
1161 GNUNET_CONTAINER_DLL_remove (ohh->th->oh_head, ohh->th->oh_tail, ohh);
1165 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "HELLO");
1166 ssize = ntohs (msg->size);
1167 GNUNET_assert (size >= ssize);
1168 memcpy (buf, msg, ssize);
1170 tc.reason = GNUNET_SCHEDULER_REASON_READ_READY;
1171 if (NULL != ohh->cont)
1172 ohh->cont (ohh->cls, &tc);
1173 GNUNET_CONTAINER_DLL_remove (ohh->th->oh_head, ohh->th->oh_tail, ohh);
1180 * Send traffic metric message to the service.
1182 * @param cls the message to send
1183 * @param size number of bytes available in buf
1184 * @param buf where to copy the message
1185 * @return number of bytes copied to buf
1188 send_metric (void *cls, size_t size, void *buf)
1190 struct TrafficMetricMessage *msg = cls;
1194 LOG (GNUNET_ERROR_TYPE_DEBUG,
1195 "Timeout while trying to transmit `%s' request.\n", "TRAFFIC_METRIC");
1199 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "TRAFFIC_METRIC");
1200 ssize = ntohs (msg->header.size);
1201 GNUNET_assert (size >= ssize);
1202 memcpy (buf, msg, ssize);
1209 * Set transport metrics for a peer and a direction
1211 * @param handle transport handle
1212 * @param peer the peer to set the metric for
1213 * @param inbound set inbound direction (GNUNET_YES or GNUNET_NO)
1214 * @param outbound set outbound direction (GNUNET_YES or GNUNET_NO)
1215 * @param ats the metric as ATS information
1216 * @param ats_count the number of metrics
1218 * Supported ATS values:
1219 * GNUNET_ATS_QUALITY_NET_DELAY (value in ms)
1220 * GNUNET_ATS_QUALITY_NET_DISTANCE (value in count(hops))
1223 * To enforce a delay of 10 ms for peer p1 in sending direction use:
1225 * struct GNUNET_ATS_Information ats;
1226 * ats.type = ntohl (GNUNET_ATS_QUALITY_NET_DELAY);
1227 * ats.value = ntohl (10);
1228 * GNUNET_TRANSPORT_set_traffic_metric (th, p1, TM_SEND, &ats, 1);
1231 * Delay restrictions in receiving direction will be enforced with
1235 GNUNET_TRANSPORT_set_traffic_metric (struct GNUNET_TRANSPORT_Handle *handle,
1236 const struct GNUNET_PeerIdentity *peer,
1239 const struct GNUNET_ATS_Information *ats,
1242 struct TrafficMetricMessage *msg;
1244 GNUNET_assert (NULL != handle);
1245 GNUNET_assert (NULL != peer);
1246 GNUNET_assert ((outbound == GNUNET_YES) || (outbound == GNUNET_NO));
1247 GNUNET_assert ((inbound == GNUNET_YES) || (inbound == GNUNET_NO));
1249 if ((GNUNET_NO == inbound) && (GNUNET_NO == outbound))
1254 size_t len = sizeof (struct TrafficMetricMessage) +
1255 ats_count * sizeof (struct GNUNET_ATS_Information);
1257 msg = GNUNET_malloc (len);
1258 msg->header.size = htons (len);
1259 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TRAFFIC_METRIC);
1260 msg->direction = htons (0 + outbound + 2 * inbound);
1261 msg->ats_count = htons (ats_count);
1262 msg->peer = (*peer);
1263 memcpy (&msg[1], ats, ats_count * sizeof (struct GNUNET_ATS_Information));
1264 schedule_control_transmit (handle, len, &send_metric, msg);
1270 * Offer the transport service the HELLO of another peer. Note that
1271 * the transport service may just ignore this message if the HELLO is
1272 * malformed or useless due to our local configuration.
1274 * @param handle connection to transport service
1275 * @param hello the hello message
1276 * @param cont continuation to call when HELLO has been sent,
1277 * tc reason GNUNET_SCHEDULER_REASON_TIMEOUT for fail
1278 * tc reasong GNUNET_SCHEDULER_REASON_READ_READY for success
1279 * @param cls closure for continuation
1280 * @return a GNUNET_TRANSPORT_OfferHelloHandle handle or NULL on failure,
1281 * in case of failure cont will not be called
1284 struct GNUNET_TRANSPORT_OfferHelloHandle *
1285 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
1286 const struct GNUNET_MessageHeader *hello,
1287 GNUNET_SCHEDULER_Task cont, void *cls)
1289 struct GNUNET_TRANSPORT_OfferHelloHandle *ohh;
1290 struct GNUNET_MessageHeader *msg;
1291 struct GNUNET_PeerIdentity peer;
1294 GNUNET_assert (NULL != handle);
1295 GNUNET_assert (NULL != hello);
1297 if (NULL == handle->client)
1300 GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
1301 size = ntohs (hello->size);
1302 GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
1304 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hello, &peer))
1310 msg = GNUNET_malloc (size);
1311 memcpy (msg, hello, size);
1312 LOG (GNUNET_ERROR_TYPE_DEBUG,
1313 "Offering `%s' message of `%4s' to transport for validation.\n", "HELLO",
1314 GNUNET_i2s (&peer));
1316 ohh = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_OfferHelloHandle));
1321 ohh->tth = schedule_control_transmit (handle, size, &send_hello, ohh);
1322 GNUNET_CONTAINER_DLL_insert (handle->oh_head, handle->oh_tail, ohh);
1328 * Cancel the request to transport to offer the HELLO message
1330 * @param ohh the GNUNET_TRANSPORT_OfferHelloHandle to cancel
1333 GNUNET_TRANSPORT_offer_hello_cancel (struct GNUNET_TRANSPORT_OfferHelloHandle *ohh)
1335 struct GNUNET_TRANSPORT_Handle *th = ohh->th;
1336 GNUNET_assert (NULL != ohh);
1338 cancel_control_transmit (ohh->th, ohh->tth);
1339 GNUNET_CONTAINER_DLL_remove (th->oh_head, th->oh_tail, ohh);
1340 GNUNET_free (ohh->msg);
1345 GNUNET_TRANSPORT_check_neighbour_connected (struct GNUNET_TRANSPORT_Handle *handle,
1346 const struct GNUNET_PeerIdentity *peer)
1348 GNUNET_assert (NULL != handle);
1349 GNUNET_assert (NULL != peer);
1351 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(handle->neighbours, &peer->hashPubKey))
1359 * Task to call the HelloUpdateCallback of the GetHelloHandle
1361 * @param cls the GetHelloHandle
1362 * @param tc the scheduler task context
1365 call_hello_update_cb_async (void *cls,
1366 const struct GNUNET_SCHEDULER_TaskContext *tc)
1368 struct GNUNET_TRANSPORT_GetHelloHandle *ghh = cls;
1370 GNUNET_assert (NULL != ghh->handle->my_hello);
1371 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != ghh->notify_task);
1372 ghh->notify_task = GNUNET_SCHEDULER_NO_TASK;
1373 ghh->rec (ghh->rec_cls,
1374 (const struct GNUNET_MessageHeader *) ghh->handle->my_hello);
1379 * Obtain the HELLO message for this peer. The callback given in this function
1380 * is never called synchronously.
1382 * @param handle connection to transport service
1383 * @param rec function to call with the HELLO, sender will be our peer
1384 * identity; message and sender will be NULL on timeout
1385 * (handshake with transport service pending/failed).
1386 * cost estimate will be 0.
1387 * @param rec_cls closure for rec
1388 * @return handle to cancel the operation
1390 struct GNUNET_TRANSPORT_GetHelloHandle *
1391 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
1392 GNUNET_TRANSPORT_HelloUpdateCallback rec,
1395 struct GNUNET_TRANSPORT_GetHelloHandle *hwl;
1397 hwl = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_GetHelloHandle));
1399 hwl->rec_cls = rec_cls;
1400 hwl->handle = handle;
1401 GNUNET_CONTAINER_DLL_insert (handle->hwl_head, handle->hwl_tail, hwl);
1402 if (handle->my_hello != NULL)
1403 hwl->notify_task = GNUNET_SCHEDULER_add_now (&call_hello_update_cb_async,
1410 * Stop receiving updates about changes to our HELLO message.
1412 * @param ghh handle to cancel
1415 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_GetHelloHandle *ghh)
1417 struct GNUNET_TRANSPORT_Handle *handle = ghh->handle;
1419 if (GNUNET_SCHEDULER_NO_TASK != ghh->notify_task)
1420 GNUNET_SCHEDULER_cancel (ghh->notify_task);
1421 GNUNET_CONTAINER_DLL_remove (handle->hwl_head, handle->hwl_tail, ghh);
1427 * Connect to the transport service. Note that the connection may
1428 * complete (or fail) asynchronously.
1430 * @param cfg configuration to use
1431 * @param self our own identity (API should check that it matches
1432 * the identity found by transport), or NULL (no check)
1433 * @param cls closure for the callbacks
1434 * @param rec receive function to call
1435 * @param nc function to call on connect events
1436 * @param nd function to call on disconnect events
1438 struct GNUNET_TRANSPORT_Handle *
1439 GNUNET_TRANSPORT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1440 const struct GNUNET_PeerIdentity *self, void *cls,
1441 GNUNET_TRANSPORT_ReceiveCallback rec,
1442 GNUNET_TRANSPORT_NotifyConnect nc,
1443 GNUNET_TRANSPORT_NotifyDisconnect nd)
1445 struct GNUNET_TRANSPORT_Handle *ret;
1447 ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Handle));
1451 ret->check_self = GNUNET_YES;
1458 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1460 GNUNET_CONTAINER_multihashmap_create (STARTING_NEIGHBOURS_SIZE, GNUNET_YES);
1462 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1463 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n");
1464 ret->client = GNUNET_CLIENT_connect ("transport", cfg);
1465 if (ret->client == NULL)
1470 schedule_control_transmit (ret, sizeof (struct StartMessage), &send_start, ret);
1476 * Disconnect from the transport service.
1478 * @param handle handle to the service as returned from GNUNET_TRANSPORT_connect
1481 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1483 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transport disconnect called!\n");
1484 /* this disconnects all neighbours... */
1485 if (handle->reconnect_task == GNUNET_SCHEDULER_NO_TASK)
1486 disconnect_and_schedule_reconnect (handle);
1487 /* and now we stop trying to connect again... */
1488 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1490 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1491 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1493 GNUNET_CONTAINER_multihashmap_destroy (handle->neighbours);
1494 handle->neighbours = NULL;
1495 if (handle->quota_task != GNUNET_SCHEDULER_NO_TASK)
1497 GNUNET_SCHEDULER_cancel (handle->quota_task);
1498 handle->quota_task = GNUNET_SCHEDULER_NO_TASK;
1500 GNUNET_free_non_null (handle->my_hello);
1501 handle->my_hello = NULL;
1502 GNUNET_assert (handle->tc_head == NULL);
1503 GNUNET_assert (handle->tc_tail == NULL);
1504 GNUNET_assert (handle->hwl_head == NULL);
1505 GNUNET_assert (handle->hwl_tail == NULL);
1506 GNUNET_CONTAINER_heap_destroy (handle->ready_heap);
1507 handle->ready_heap = NULL;
1508 GNUNET_free (handle);
1513 * Check if we could queue a message of the given size for
1514 * transmission. The transport service will take both its
1515 * internal buffers and bandwidth limits imposed by the
1516 * other peer into consideration when answering this query.
1518 * @param handle connection to transport service
1519 * @param target who should receive the message
1520 * @param size how big is the message we want to transmit?
1521 * @param priority how important is the message?
1522 * @param timeout after how long should we give up (and call
1523 * notify with buf NULL and size 0)?
1524 * @param notify function to call when we are ready to
1525 * send such a message
1526 * @param notify_cls closure for notify
1527 * @return NULL if someone else is already waiting to be notified
1528 * non-NULL if the notify callback was queued (can be used to cancel
1529 * using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1531 struct GNUNET_TRANSPORT_TransmitHandle *
1532 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle *handle,
1533 const struct GNUNET_PeerIdentity
1534 *target, size_t size, uint32_t priority,
1535 struct GNUNET_TIME_Relative timeout,
1536 GNUNET_CONNECTION_TransmitReadyNotify
1537 notify, void *notify_cls)
1539 struct Neighbour *n;
1540 struct GNUNET_TRANSPORT_TransmitHandle *th;
1541 struct GNUNET_TIME_Relative delay;
1543 n = neighbour_find (handle, target);
1546 /* use GNUNET_TRANSPORT_try_connect first, only use this function
1547 * once a connection has been established */
1553 /* attempt to send two messages at the same time to the same peer */
1557 GNUNET_assert (NULL == n->hn);
1558 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
1560 th->notify = notify;
1561 th->notify_cls = notify_cls;
1562 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1563 th->notify_size = size;
1564 th->priority = priority;
1566 /* calculate when our transmission should be ready */
1567 delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker, size + n->traffic_overhead);
1568 n->traffic_overhead = 0;
1569 if (delay.rel_value > timeout.rel_value)
1570 delay.rel_value = 0; /* notify immediately (with failure) */
1571 LOG (GNUNET_ERROR_TYPE_DEBUG,
1572 "Bandwidth tracker allows next transmission to peer %s in %llu ms\n",
1573 GNUNET_i2s (target), (unsigned long long) delay.rel_value);
1574 n->hn = GNUNET_CONTAINER_heap_insert (handle->ready_heap, n, delay.rel_value);
1575 schedule_transmission (handle);
1581 * Cancel the specified transmission-ready notification.
1583 * @param th handle returned from GNUNET_TRANSPORT_notify_transmit_ready
1586 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
1587 GNUNET_TRANSPORT_TransmitHandle
1590 struct Neighbour *n;
1592 GNUNET_assert (NULL == th->next);
1593 GNUNET_assert (NULL == th->prev);
1595 GNUNET_assert (th == n->th);
1599 GNUNET_CONTAINER_heap_remove_node (n->hn);
1604 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != th->timeout_task);
1605 GNUNET_SCHEDULER_cancel (th->timeout_task);
1606 th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1612 /* end of transport_api.c */