2 This file is part of GNUnet.
3 (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., 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
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 * How large to start with for the hashmap of neighbours.
43 #define STARTING_NEIGHBOURS_SIZE 16
46 * Handle for a message that should be transmitted to the service.
47 * Used for both control messages and normal messages.
49 struct GNUNET_TRANSPORT_TransmitHandle
53 * We keep all requests in a DLL.
55 struct GNUNET_TRANSPORT_TransmitHandle *next;
58 * We keep all requests in a DLL.
60 struct GNUNET_TRANSPORT_TransmitHandle *prev;
63 * Neighbour for this handle, NULL for control messages.
65 struct Neighbour *neighbour;
68 * Function to call when notify_size bytes are available
71 GNUNET_CONNECTION_TransmitReadyNotify notify;
79 * Timeout for this request, 0 for control messages.
81 struct GNUNET_TIME_Absolute timeout;
84 * Task to trigger request timeout if the request is stalled due to
87 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
90 * How many bytes is our notify callback waiting for?
95 * How important is this message? Not used for control messages.
103 * Entry in hash table of all of our current neighbours.
108 * Overall transport handle.
110 struct GNUNET_TRANSPORT_Handle *h;
113 * Active transmit handle or NULL.
115 struct GNUNET_TRANSPORT_TransmitHandle *th;
118 * Identity of this neighbour.
120 struct GNUNET_PeerIdentity id;
123 * Outbound bandwidh tracker.
125 struct GNUNET_BANDWIDTH_Tracker out_tracker;
128 * Entry in our readyness heap (which is sorted by 'next_ready'
129 * value). NULL if there is no pending transmission request for
130 * this neighbour or if we're waiting for 'is_ready' to become
131 * true AFTER the 'out_tracker' suggested that this peer's quota
132 * has been satisfied (so once 'is_ready' goes to GNUNET_YES,
133 * we should immediately go back into the heap).
135 struct GNUNET_CONTAINER_HeapNode *hn;
138 * Is this peer currently ready to receive a message?
143 * Sending consumed more bytes on wire than payload was announced
144 * This overhead is added to the delay of next sending operation
146 size_t traffic_overhead;
151 * Linked list of functions to call whenever our HELLO is updated.
153 struct GNUNET_TRANSPORT_GetHelloHandle
157 * This is a doubly linked list.
159 struct GNUNET_TRANSPORT_GetHelloHandle *next;
162 * This is a doubly linked list.
164 struct GNUNET_TRANSPORT_GetHelloHandle *prev;
169 struct GNUNET_TRANSPORT_Handle *handle;
172 * Callback to call once we got our HELLO.
174 GNUNET_TRANSPORT_HelloUpdateCallback rec;
177 * Task for calling the HelloUpdateCallback when we already have a HELLO
179 GNUNET_SCHEDULER_TaskIdentifier notify_task;
182 * Closure for @e rec.
189 * Linked list for all try-connect requests
191 struct GNUNET_TRANSPORT_TryConnectHandle
196 struct GNUNET_TRANSPORT_TryConnectHandle *prev;
201 struct GNUNET_TRANSPORT_TryConnectHandle *next;
203 struct GNUNET_PeerIdentity pid;
205 struct GNUNET_TRANSPORT_Handle *th;
207 struct GNUNET_TRANSPORT_TransmitHandle *tth;
209 GNUNET_TRANSPORT_TryConnectCallback cb;
219 * Linked list for all try-connect requests
221 struct GNUNET_TRANSPORT_OfferHelloHandle
226 struct GNUNET_TRANSPORT_OfferHelloHandle *prev;
231 struct GNUNET_TRANSPORT_OfferHelloHandle *next;
233 struct GNUNET_TRANSPORT_Handle *th;
235 struct GNUNET_TRANSPORT_TransmitHandle *tth;
237 GNUNET_SCHEDULER_Task cont;
240 * Closure for @e cont
244 struct GNUNET_MessageHeader *msg;
249 * Handle for the transport service (includes all of the
250 * state for the transport service).
252 struct GNUNET_TRANSPORT_Handle
256 * Closure for the callbacks.
261 * Function to call for received data.
263 GNUNET_TRANSPORT_ReceiveCallback rec;
266 * function to call on connect events
268 GNUNET_TRANSPORT_NotifyConnect nc_cb;
271 * function to call on disconnect events
273 GNUNET_TRANSPORT_NotifyDisconnect nd_cb;
276 * Head of DLL of control messages.
278 struct GNUNET_TRANSPORT_TransmitHandle *control_head;
281 * Tail of DLL of control messages.
283 struct GNUNET_TRANSPORT_TransmitHandle *control_tail;
286 * The current HELLO message for this peer. Updated
287 * whenever transports change their addresses.
289 struct GNUNET_HELLO_Message *my_hello;
292 * My client connection to the transport service.
294 struct GNUNET_CLIENT_Connection *client;
297 * Handle to our registration with the client for notification.
299 struct GNUNET_CLIENT_TransmitHandle *cth;
302 * Linked list of pending requests for our HELLO.
304 struct GNUNET_TRANSPORT_GetHelloHandle *hwl_head;
307 * Linked list of pending requests for our HELLO.
309 struct GNUNET_TRANSPORT_GetHelloHandle *hwl_tail;
312 * Linked list of pending try connect requests head
314 struct GNUNET_TRANSPORT_TryConnectHandle *tc_head;
317 * Linked list of pending try connect requests tail
319 struct GNUNET_TRANSPORT_TryConnectHandle *tc_tail;
322 * Linked list of pending offer HELLO requests head
324 struct GNUNET_TRANSPORT_OfferHelloHandle *oh_head;
327 * Linked list of pending offer HELLO requests tail
329 struct GNUNET_TRANSPORT_OfferHelloHandle *oh_tail;
334 const struct GNUNET_CONFIGURATION_Handle *cfg;
337 * Hash map of the current connected neighbours of this peer.
338 * Maps peer identities to 'struct Neighbour' entries.
340 struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
343 * Heap sorting peers with pending messages by the timestamps that
344 * specify when we could next send a message to the respective peer.
345 * Excludes control messages (which can always go out immediately).
346 * Maps time stamps to 'struct Neighbour' entries.
348 struct GNUNET_CONTAINER_Heap *ready_heap;
351 * Peer identity as assumed by this process, or all zeros.
353 struct GNUNET_PeerIdentity self;
356 * ID of the task trying to reconnect to the service.
358 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
361 * ID of the task trying to trigger transmission for a peer while
362 * maintaining bandwidth quotas. In use if there are no control
363 * messages and the smallest entry in the 'ready_heap' has a time
364 * stamp in the future.
366 GNUNET_SCHEDULER_TaskIdentifier quota_task;
369 * Delay until we try to reconnect.
371 struct GNUNET_TIME_Relative reconnect_delay;
374 * Should we check that @e self matches what the service thinks?
375 * (if #GNUNET_NO, then @e self is all zeros!).
380 * Reconnect in progress
388 * Schedule the task to send one message, either from the control
389 * list or the peer message queues to the service.
391 * @param h transport service to schedule a transmission for
394 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h);
398 * Function that will schedule the job that will try
399 * to connect us again to the client.
401 * @param h transport service to reconnect
404 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h);
408 * Get the neighbour list entry for the given peer
410 * @param h our context
411 * @param peer peer to look up
412 * @return NULL if no such peer entry exists
414 static struct Neighbour *
415 neighbour_find (struct GNUNET_TRANSPORT_Handle *h,
416 const struct GNUNET_PeerIdentity *peer)
418 return GNUNET_CONTAINER_multipeermap_get (h->neighbours, peer);
424 outbound_bw_tracker_update (void *cls)
426 struct Neighbour *n = cls;
427 struct GNUNET_TIME_Relative delay;
431 delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
432 n->th->notify_size + n->traffic_overhead);
433 LOG(GNUNET_ERROR_TYPE_DEBUG,
434 "New outbound delay %llu us\n",delay.rel_value_us);
435 GNUNET_CONTAINER_heap_update_cost (n->h->ready_heap,
436 n->hn, delay.rel_value_us);
437 schedule_transmission (n->h);
442 * Add neighbour to our list
444 * @return NULL if this API is currently disconnecting from the service
446 static struct Neighbour *
447 neighbour_add (struct GNUNET_TRANSPORT_Handle *h,
448 const struct GNUNET_PeerIdentity *pid)
452 LOG (GNUNET_ERROR_TYPE_DEBUG,
453 "Creating entry for neighbour `%4s'.\n",
455 n = GNUNET_new (struct Neighbour);
458 n->is_ready = GNUNET_YES;
459 n->traffic_overhead = 0;
460 GNUNET_BANDWIDTH_tracker_init (&n->out_tracker,
461 outbound_bw_tracker_update, n,
462 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
463 MAX_BANDWIDTH_CARRY_S);
464 GNUNET_assert (GNUNET_OK ==
465 GNUNET_CONTAINER_multipeermap_put (h->neighbours,
467 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
473 * Iterator over hash map entries, for deleting state of a neighbour.
475 * @param cls the `struct GNUNET_TRANSPORT_Handle *`
476 * @param key peer identity
477 * @param value value in the hash map, the neighbour entry to delete
478 * @return #GNUNET_YES if we should continue to
483 neighbour_delete (void *cls,
484 const struct GNUNET_PeerIdentity *key, void *value)
486 struct GNUNET_TRANSPORT_Handle *handle = cls;
487 struct Neighbour *n = value;
489 if (NULL != handle->nd_cb)
490 handle->nd_cb (handle->cls, &n->id);
491 GNUNET_assert (NULL == n->th);
492 GNUNET_assert (NULL == n->hn);
493 GNUNET_assert (GNUNET_YES ==
494 GNUNET_CONTAINER_multipeermap_remove (handle->neighbours, key,
500 static int reconnecting;
503 * Function we use for handling incoming messages.
505 * @param cls closure, a `struct GNUNET_TRANSPORT_Handle *`
506 * @param msg message received, NULL on timeout or fatal error
509 demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
511 struct GNUNET_TRANSPORT_Handle *h = cls;
512 const struct DisconnectInfoMessage *dim;
513 const struct ConnectInfoMessage *cim;
514 const struct InboundMessage *im;
515 const struct GNUNET_MessageHeader *imm;
516 const struct SendOkMessage *okm;
517 const struct QuotaSetMessage *qm;
518 struct GNUNET_TRANSPORT_GetHelloHandle *hwl;
519 struct GNUNET_TRANSPORT_GetHelloHandle *next_hwl;
521 struct GNUNET_PeerIdentity me;
524 uint32_t bytes_physical;
526 GNUNET_assert (NULL != h->client);
527 if (GNUNET_YES == reconnecting)
533 LOG (GNUNET_ERROR_TYPE_DEBUG,
534 "Error receiving from transport service, disconnecting temporarily.\n");
535 reconnecting = GNUNET_YES;
536 disconnect_and_schedule_reconnect (h);
539 GNUNET_CLIENT_receive (h->client, &demultiplexer, h,
540 GNUNET_TIME_UNIT_FOREVER_REL);
541 size = ntohs (msg->size);
542 switch (ntohs (msg->type))
544 case GNUNET_MESSAGE_TYPE_HELLO:
546 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg, &me))
551 LOG (GNUNET_ERROR_TYPE_DEBUG,
552 "Receiving (my own) `%s' message, I am `%4s'.\n", "HELLO",
554 GNUNET_free_non_null (h->my_hello);
556 if (size < sizeof (struct GNUNET_MessageHeader))
561 h->my_hello = GNUNET_malloc (size);
562 memcpy (h->my_hello, msg, size);
566 next_hwl = hwl->next;
567 hwl->rec (hwl->rec_cls,
568 (const struct GNUNET_MessageHeader *) h->my_hello);
572 case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
573 if (size < sizeof (struct ConnectInfoMessage))
578 cim = (const struct ConnectInfoMessage *) msg;
580 sizeof (struct ConnectInfoMessage))
585 LOG (GNUNET_ERROR_TYPE_DEBUG,
586 "Receiving `%s' message for `%4s'.\n",
587 "CONNECT", GNUNET_i2s (&cim->id));
588 n = neighbour_find (h, &cim->id);
594 n = neighbour_add (h, &cim->id);
595 LOG (GNUNET_ERROR_TYPE_DEBUG,
596 "Receiving `%s' message for `%4s' with quota %u\n",
598 GNUNET_i2s (&cim->id),
599 ntohl (cim->quota_out.value__));
600 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker, cim->quota_out);
601 if (h->nc_cb != NULL)
602 h->nc_cb (h->cls, &n->id);
604 case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
605 if (size != sizeof (struct DisconnectInfoMessage))
610 dim = (const struct DisconnectInfoMessage *) msg;
611 GNUNET_break (ntohl (dim->reserved) == 0);
612 LOG (GNUNET_ERROR_TYPE_DEBUG,
613 "Receiving `%s' message for `%4s'.\n",
614 "DISCONNECT", GNUNET_i2s (&dim->peer));
615 n = neighbour_find (h, &dim->peer);
621 neighbour_delete (h, &dim->peer, n);
623 case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
624 if (size != sizeof (struct SendOkMessage))
629 okm = (const struct SendOkMessage *) msg;
630 bytes_msg = ntohl (okm->bytes_msg);
631 bytes_physical = ntohl (okm->bytes_physical);
632 LOG (GNUNET_ERROR_TYPE_DEBUG,
633 "Receiving SEND_OK message, transmission %s.\n",
634 ntohl (okm->success) == GNUNET_OK ? "succeeded" : "failed");
636 n = neighbour_find (h, &okm->peer);
640 if (bytes_physical >= bytes_msg)
642 LOG (GNUNET_ERROR_TYPE_DEBUG,
643 "Overhead for %u byte message: %u\n",
644 bytes_msg, bytes_physical - bytes_msg);
645 n->traffic_overhead += bytes_physical - bytes_msg;
647 GNUNET_break (GNUNET_NO == n->is_ready);
648 n->is_ready = GNUNET_YES;
649 if ((NULL != n->th) && (NULL == n->hn))
651 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->th->timeout_task);
652 GNUNET_SCHEDULER_cancel (n->th->timeout_task);
653 n->th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
654 /* we've been waiting for this (congestion, not quota,
655 * caused delayed transmission) */
656 n->hn = GNUNET_CONTAINER_heap_insert (h->ready_heap, n, 0);
657 schedule_transmission (h);
660 case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
661 LOG (GNUNET_ERROR_TYPE_DEBUG,
662 "Receiving `%s' message.\n",
665 sizeof (struct InboundMessage) + sizeof (struct GNUNET_MessageHeader))
670 im = (const struct InboundMessage *) msg;
671 imm = (const struct GNUNET_MessageHeader *) &im[1];
672 if (ntohs (imm->size) + sizeof (struct InboundMessage) != size)
677 LOG (GNUNET_ERROR_TYPE_DEBUG,
678 "Received message of type %u from `%4s'.\n",
679 ntohs (imm->type), GNUNET_i2s (&im->peer));
680 n = neighbour_find (h, &im->peer);
687 h->rec (h->cls, &im->peer, imm);
689 case GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA:
690 LOG (GNUNET_ERROR_TYPE_DEBUG,
691 "Receiving `%s' message.\n", "SET_QUOTA");
692 if (size != sizeof (struct QuotaSetMessage))
697 qm = (const struct QuotaSetMessage *) msg;
698 n = neighbour_find (h, &qm->peer);
701 LOG (GNUNET_ERROR_TYPE_DEBUG,
702 "Receiving `%s' message for `%4s' with quota %u\n",
704 GNUNET_i2s (&qm->peer),
705 ntohl (qm->quota.value__));
706 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker, qm->quota);
709 LOG (GNUNET_ERROR_TYPE_ERROR,
710 _("Received unexpected message of type %u in %s:%u\n"),
711 ntohs (msg->type), __FILE__, __LINE__);
719 * A transmission request could not be satisfied because of
720 * network congestion. Notify the initiator and clean up.
722 * @param cls the `struct GNUNET_TRANSPORT_TransmitHandle`
723 * @param tc scheduler context
726 timeout_request_due_to_congestion (void *cls,
727 const struct GNUNET_SCHEDULER_TaskContext *tc)
729 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
730 struct Neighbour *n = th->neighbour;
732 n->th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
733 GNUNET_assert (th == n->th);
734 GNUNET_assert (NULL == n->hn);
736 th->notify (th->notify_cls, 0, NULL);
742 * Transmit message(s) to service.
744 * @param cls handle to transport
745 * @param size number of bytes available in @a buf
746 * @param buf where to copy the message
747 * @return number of bytes copied to @a buf
750 transport_notify_ready (void *cls, size_t size, void *buf)
752 struct GNUNET_TRANSPORT_Handle *h = cls;
753 struct GNUNET_TRANSPORT_TransmitHandle *th;
756 struct OutboundMessage obm;
761 GNUNET_assert (NULL != h->client);
765 /* transmission failed */
766 disconnect_and_schedule_reconnect (h);
772 /* first send control messages */
773 while ((NULL != (th = h->control_head)) && (th->notify_size <= size))
775 GNUNET_CONTAINER_DLL_remove (h->control_head, h->control_tail, th);
776 nret = th->notify (th->notify_cls, size, &cbuf[ret]);
777 LOG (GNUNET_ERROR_TYPE_DEBUG,
778 "Added %u bytes of control message at %u\n",
785 /* then, if possible and no control messages pending, send data messages */
786 while ((NULL == h->control_head) &&
787 (NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap))))
789 if (GNUNET_YES != n->is_ready)
791 /* peer not ready, wait for notification! */
792 GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
794 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == n->th->timeout_task);
795 n->th->timeout_task =
796 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
798 &timeout_request_due_to_congestion,
803 if (th->notify_size + sizeof (struct OutboundMessage) > size)
804 break; /* does not fit */
805 if (GNUNET_BANDWIDTH_tracker_get_delay
806 (&n->out_tracker, th->notify_size).rel_value_us > 0)
807 break; /* too early */
808 GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
811 n->is_ready = GNUNET_NO;
812 GNUNET_assert (size >= sizeof (struct OutboundMessage));
814 th->notify (th->notify_cls, size - sizeof (struct OutboundMessage),
815 &cbuf[ret + sizeof (struct OutboundMessage)]);
816 GNUNET_assert (mret <= size - sizeof (struct OutboundMessage));
819 GNUNET_assert (mret + sizeof (struct OutboundMessage) <
820 GNUNET_SERVER_MAX_MESSAGE_SIZE);
821 obm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
822 obm.header.size = htons (mret + sizeof (struct OutboundMessage));
823 obm.priority = htonl (th->priority);
825 GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining
828 memcpy (&cbuf[ret], &obm, sizeof (struct OutboundMessage));
829 ret += (mret + sizeof (struct OutboundMessage));
830 size -= (mret + sizeof (struct OutboundMessage));
831 GNUNET_BANDWIDTH_tracker_consume (&n->out_tracker, mret);
835 /* if there are more pending messages, try to schedule those */
836 schedule_transmission (h);
837 LOG (GNUNET_ERROR_TYPE_DEBUG,
838 "Transmitting %u bytes to transport service\n",
845 * Schedule the task to send one message, either from the control
846 * list or the peer message queues to the service.
848 * @param cls transport service to schedule a transmission for
849 * @param tc scheduler context
852 schedule_transmission_task (void *cls,
853 const struct GNUNET_SCHEDULER_TaskContext *tc)
855 struct GNUNET_TRANSPORT_Handle *h = cls;
857 struct GNUNET_TRANSPORT_TransmitHandle *th;
860 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
861 GNUNET_assert (NULL != h->client);
862 /* destroy all requests that have timed out */
863 while ((NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap))) &&
864 (0 == GNUNET_TIME_absolute_get_remaining (n->th->timeout).rel_value_us))
866 /* notify client that the request could not be satisfied within
867 * the given time constraints */
870 GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
872 LOG (GNUNET_ERROR_TYPE_DEBUG,
873 "Signalling timeout for transmission to peer %s due to congestion\n",
874 GNUNET_i2s (&n->id));
875 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
880 if (NULL != h->control_head)
882 size = h->control_head->notify_size;
886 n = GNUNET_CONTAINER_heap_peek (h->ready_heap);
888 return; /* no pending messages */
889 size = n->th->notify_size + sizeof (struct OutboundMessage);
891 LOG (GNUNET_ERROR_TYPE_DEBUG, "Calling notify_transmit_ready\n");
893 GNUNET_CLIENT_notify_transmit_ready (h->client, size,
894 GNUNET_TIME_UNIT_FOREVER_REL,
895 GNUNET_NO, &transport_notify_ready,
897 GNUNET_assert (NULL != h->cth);
902 * Schedule the task to send one message, either from the control
903 * list or the peer message queues to the service.
905 * @param h transport service to schedule a transmission for
908 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
910 struct GNUNET_TIME_Relative delay;
913 GNUNET_assert (NULL != h->client);
914 if (h->quota_task != GNUNET_SCHEDULER_NO_TASK)
916 GNUNET_SCHEDULER_cancel (h->quota_task);
917 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
919 if (NULL != h->control_head)
920 delay = GNUNET_TIME_UNIT_ZERO;
921 else if (NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap)))
924 GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
925 n->th->notify_size + n->traffic_overhead);
926 n->traffic_overhead = 0;
929 return; /* no work to be done */
930 LOG (GNUNET_ERROR_TYPE_DEBUG,
931 "Scheduling next transmission to service in %s\n",
932 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
934 GNUNET_SCHEDULER_add_delayed (delay, &schedule_transmission_task, h);
939 * Queue control request for transmission to the transport
942 * @param h handle to the transport service
943 * @param size number of bytes to be transmitted
944 * @param notify function to call to get the content
945 * @param notify_cls closure for @a notify
946 * @return a `struct GNUNET_TRANSPORT_TransmitHandle`
948 static struct GNUNET_TRANSPORT_TransmitHandle *
949 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h, size_t size,
950 GNUNET_CONNECTION_TransmitReadyNotify notify,
953 struct GNUNET_TRANSPORT_TransmitHandle *th;
955 LOG (GNUNET_ERROR_TYPE_DEBUG,
956 "Control transmit of %u bytes requested\n",
958 th = GNUNET_new (struct GNUNET_TRANSPORT_TransmitHandle);
960 th->notify_cls = notify_cls;
961 th->notify_size = size;
962 GNUNET_CONTAINER_DLL_insert_tail (h->control_head, h->control_tail, th);
963 schedule_transmission (h);
969 * Transmit START message to service.
972 * @param size number of bytes available in @a buf
973 * @param buf where to copy the message
974 * @return number of bytes copied to @a buf
977 send_start (void *cls, size_t size, void *buf)
979 struct GNUNET_TRANSPORT_Handle *h = cls;
980 struct StartMessage s;
985 /* Can only be shutdown, just give up */
986 LOG (GNUNET_ERROR_TYPE_DEBUG,
987 "Shutdown while trying to transmit `%s' request.\n",
991 LOG (GNUNET_ERROR_TYPE_DEBUG,
992 "Transmitting `%s' request.\n",
994 GNUNET_assert (size >= sizeof (struct StartMessage));
995 s.header.size = htons (sizeof (struct StartMessage));
996 s.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
1002 s.options = htonl (options);
1004 memcpy (buf, &s, sizeof (struct StartMessage));
1005 GNUNET_CLIENT_receive (h->client, &demultiplexer, h,
1006 GNUNET_TIME_UNIT_FOREVER_REL);
1007 return sizeof (struct StartMessage);
1012 * Try again to connect to transport service.
1014 * @param cls the handle to the transport service
1015 * @param tc scheduler context
1018 reconnect (void *cls,
1019 const struct GNUNET_SCHEDULER_TaskContext *tc)
1021 struct GNUNET_TRANSPORT_Handle *h = cls;
1023 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1024 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1026 /* shutdown, just give up */
1029 LOG (GNUNET_ERROR_TYPE_DEBUG,
1030 "Connecting to transport service.\n");
1031 GNUNET_assert (NULL == h->client);
1032 GNUNET_assert (NULL == h->control_head);
1033 GNUNET_assert (NULL == h->control_tail);
1034 reconnecting = GNUNET_NO;
1035 h->client = GNUNET_CLIENT_connect ("transport", h->cfg);
1037 GNUNET_assert (NULL != h->client);
1038 schedule_control_transmit (h, sizeof (struct StartMessage), &send_start, h);
1043 * Function that will schedule the job that will try
1044 * to connect us again to the client.
1046 * @param h transport service to reconnect
1049 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
1051 struct GNUNET_TRANSPORT_TransmitHandle *th;
1053 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
1056 GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
1059 if (NULL != h->client)
1061 GNUNET_CLIENT_disconnect (h->client);
1063 /* LOG (GNUNET_ERROR_TYPE_ERROR,
1064 "Client disconnect done \n");*/
1066 /* Forget about all neighbours that we used to be connected to */
1067 GNUNET_CONTAINER_multipeermap_iterate (h->neighbours,
1068 &neighbour_delete, h);
1069 if (h->quota_task != GNUNET_SCHEDULER_NO_TASK)
1071 GNUNET_SCHEDULER_cancel (h->quota_task);
1072 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
1074 while ((NULL != (th = h->control_head)))
1076 GNUNET_CONTAINER_DLL_remove (h->control_head, h->control_tail, th);
1077 th->notify (th->notify_cls, 0, NULL);
1080 LOG (GNUNET_ERROR_TYPE_DEBUG,
1081 "Scheduling task to reconnect to transport service in %s.\n",
1082 GNUNET_STRINGS_relative_time_to_string(h->reconnect_delay, GNUNET_YES));
1084 GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
1085 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
1090 * Cancel control request for transmission to the transport service.
1092 * @param th handle to the transport service
1093 * @param tth transmit handle to cancel
1096 cancel_control_transmit (struct GNUNET_TRANSPORT_Handle *th,
1097 struct GNUNET_TRANSPORT_TransmitHandle *tth)
1099 LOG (GNUNET_ERROR_TYPE_DEBUG,
1100 "Canceling transmit of contral transmission requested\n");
1101 GNUNET_CONTAINER_DLL_remove (th->control_head, th->control_tail, tth);
1108 * Send REQUEST_CONNECT message to the service.
1110 * @param cls the `struct GNUNET_PeerIdentity`
1111 * @param size number of bytes available in @a buf
1112 * @param buf where to copy the message
1113 * @return number of bytes copied to @a buf
1116 send_try_connect (void *cls, size_t size, void *buf)
1118 struct GNUNET_TRANSPORT_TryConnectHandle *tch = cls;
1119 struct TransportRequestConnectMessage msg;
1123 if (NULL != tch->cb)
1124 tch->cb (tch->cb_cls, GNUNET_SYSERR);
1125 GNUNET_CONTAINER_DLL_remove (tch->th->tc_head, tch->th->tc_tail, tch);
1126 LOG (GNUNET_ERROR_TYPE_DEBUG,
1127 "Discarding `%s' request to `%4s' due to error in transport service connection.\n",
1129 GNUNET_i2s (&tch->pid));
1133 LOG (GNUNET_ERROR_TYPE_DEBUG,
1134 "Transmitting `%s' request with respect to `%4s'.\n",
1136 GNUNET_i2s (&tch->pid));
1137 GNUNET_assert (size >= sizeof (struct TransportRequestConnectMessage));
1138 msg.header.size = htons (sizeof (struct TransportRequestConnectMessage));
1139 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT);
1140 msg.reserved = htonl (0);
1141 msg.peer = tch->pid;
1142 memcpy (buf, &msg, sizeof (msg));
1143 if (NULL != tch->cb)
1144 tch->cb (tch->cb_cls, GNUNET_OK);
1145 GNUNET_CONTAINER_DLL_remove (tch->th->tc_head, tch->th->tc_tail, tch);
1147 return sizeof (struct TransportRequestConnectMessage);
1151 * Ask the transport service to establish a connection to
1154 * @param handle connection to transport service
1155 * @param target who we should try to connect to
1156 * @param cb callback to be called when request was transmitted to transport
1158 * @param cb_cls closure for the callback
1159 * @return a `struct GNUNET_TRANSPORT_TryConnectHandle` handle or
1160 * NULL on failure (cb will not be called)
1162 struct GNUNET_TRANSPORT_TryConnectHandle *
1163 GNUNET_TRANSPORT_try_connect (struct GNUNET_TRANSPORT_Handle *handle,
1164 const struct GNUNET_PeerIdentity *target,
1165 GNUNET_TRANSPORT_TryConnectCallback cb,
1168 struct GNUNET_TRANSPORT_TryConnectHandle *tch = NULL;
1170 if (NULL == handle->client)
1172 tch = GNUNET_new (struct GNUNET_TRANSPORT_TryConnectHandle);
1174 tch->pid = *(target);
1176 tch->cb_cls = cb_cls;
1177 tch->tth = schedule_control_transmit (handle,
1178 sizeof (struct TransportRequestConnectMessage),
1179 &send_try_connect, tch);
1180 GNUNET_CONTAINER_DLL_insert(handle->tc_head, handle->tc_tail, tch);
1186 * Cancel the request to transport to try a connect
1187 * Callback will not be called
1189 * @param tch the handle to cancel
1192 GNUNET_TRANSPORT_try_connect_cancel (struct GNUNET_TRANSPORT_TryConnectHandle *tch)
1194 struct GNUNET_TRANSPORT_Handle *th;
1197 cancel_control_transmit (th, tch->tth);
1198 GNUNET_CONTAINER_DLL_remove (th->tc_head, th->tc_tail, tch);
1203 * Send HELLO message to the service.
1205 * @param cls the HELLO message to send
1206 * @param size number of bytes available in @a buf
1207 * @param buf where to copy the message
1208 * @return number of bytes copied to @a buf
1211 send_hello (void *cls, size_t size, void *buf)
1213 struct GNUNET_TRANSPORT_OfferHelloHandle *ohh = cls;
1214 struct GNUNET_MessageHeader *msg = ohh->msg;
1216 struct GNUNET_SCHEDULER_TaskContext tc;
1218 tc.read_ready = NULL;
1219 tc.write_ready = NULL;
1220 tc.reason = GNUNET_SCHEDULER_REASON_TIMEOUT;
1223 LOG (GNUNET_ERROR_TYPE_DEBUG,
1224 "Timeout while trying to transmit `%s' request.\n",
1226 if (NULL != ohh->cont)
1227 ohh->cont (ohh->cls, &tc);
1229 GNUNET_CONTAINER_DLL_remove (ohh->th->oh_head, ohh->th->oh_tail, ohh);
1233 LOG (GNUNET_ERROR_TYPE_DEBUG,
1234 "Transmitting `%s' request.\n",
1236 ssize = ntohs (msg->size);
1237 GNUNET_assert (size >= ssize);
1238 memcpy (buf, msg, ssize);
1240 tc.reason = GNUNET_SCHEDULER_REASON_READ_READY;
1241 if (NULL != ohh->cont)
1242 ohh->cont (ohh->cls, &tc);
1243 GNUNET_CONTAINER_DLL_remove (ohh->th->oh_head, ohh->th->oh_tail, ohh);
1250 * Send traffic metric message to the service.
1252 * @param cls the message to send
1253 * @param size number of bytes available in @a buf
1254 * @param buf where to copy the message
1255 * @return number of bytes copied to @a buf
1258 send_metric (void *cls, size_t size, void *buf)
1260 struct TrafficMetricMessage *msg = cls;
1265 LOG (GNUNET_ERROR_TYPE_DEBUG,
1266 "Timeout while trying to transmit `%s' request.\n",
1271 LOG (GNUNET_ERROR_TYPE_DEBUG,
1272 "Transmitting `%s' request.\n",
1274 ssize = ntohs (msg->header.size);
1275 GNUNET_assert (size >= ssize);
1276 memcpy (buf, msg, ssize);
1283 * Set transport metrics for a peer and a direction
1285 * @param handle transport handle
1286 * @param peer the peer to set the metric for
1287 * @param inbound set inbound direction (#GNUNET_YES or #GNUNET_NO)
1288 * @param outbound set outbound direction (#GNUNET_YES or #GNUNET_NO)
1289 * @param ats the metric as ATS information
1290 * @param ats_count the number of metrics
1292 * Supported ATS values:
1293 * #GNUNET_ATS_QUALITY_NET_DELAY (value in ms)
1294 * #GNUNET_ATS_QUALITY_NET_DISTANCE (value in count(hops))
1297 * To enforce a delay of 10 ms for peer p1 in sending direction use:
1299 * struct GNUNET_ATS_Information ats;
1300 * ats.type = ntohl (GNUNET_ATS_QUALITY_NET_DELAY);
1301 * ats.value = ntohl (10);
1302 * GNUNET_TRANSPORT_set_traffic_metric (th, p1, TM_SEND, &ats, 1);
1305 * Delay restrictions in receiving direction will be enforced with
1309 GNUNET_TRANSPORT_set_traffic_metric (struct GNUNET_TRANSPORT_Handle *handle,
1310 const struct GNUNET_PeerIdentity *peer,
1313 const struct GNUNET_ATS_Information *ats,
1316 struct TrafficMetricMessage *msg;
1318 GNUNET_assert ((outbound == GNUNET_YES) || (outbound == GNUNET_NO));
1319 GNUNET_assert ((inbound == GNUNET_YES) || (inbound == GNUNET_NO));
1320 if ((GNUNET_NO == inbound) && (GNUNET_NO == outbound))
1325 size_t len = sizeof (struct TrafficMetricMessage) +
1326 ats_count * sizeof (struct GNUNET_ATS_Information);
1328 msg = GNUNET_malloc (len);
1329 msg->header.size = htons (len);
1330 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TRAFFIC_METRIC);
1331 msg->direction = htons (0 + outbound + 2 * inbound);
1332 msg->ats_count = htons (ats_count);
1333 msg->peer = (*peer);
1334 memcpy (&msg[1], ats, ats_count * sizeof (struct GNUNET_ATS_Information));
1335 schedule_control_transmit (handle, len, &send_metric, msg);
1341 * Offer the transport service the HELLO of another peer. Note that
1342 * the transport service may just ignore this message if the HELLO is
1343 * malformed or useless due to our local configuration.
1345 * @param handle connection to transport service
1346 * @param hello the hello message
1347 * @param cont continuation to call when HELLO has been sent,
1348 * tc reason #GNUNET_SCHEDULER_REASON_TIMEOUT for fail
1349 * tc reasong #GNUNET_SCHEDULER_REASON_READ_READY for success
1350 * @param cls closure for continuation
1351 * @return a `struct GNUNET_TRANSPORT_OfferHelloHandle` handle or NULL on failure,
1352 * in case of failure cont will not be called
1355 struct GNUNET_TRANSPORT_OfferHelloHandle *
1356 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
1357 const struct GNUNET_MessageHeader *hello,
1358 GNUNET_SCHEDULER_Task cont, void *cls)
1360 struct GNUNET_TRANSPORT_OfferHelloHandle *ohh;
1361 struct GNUNET_MessageHeader *msg;
1362 struct GNUNET_PeerIdentity peer;
1365 if (NULL == handle->client)
1367 GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
1368 size = ntohs (hello->size);
1369 GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
1371 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hello, &peer))
1377 msg = GNUNET_malloc (size);
1378 memcpy (msg, hello, size);
1379 LOG (GNUNET_ERROR_TYPE_DEBUG,
1380 "Offering `%s' message of `%4s' to transport for validation.\n", "HELLO",
1381 GNUNET_i2s (&peer));
1383 ohh = GNUNET_new (struct GNUNET_TRANSPORT_OfferHelloHandle);
1388 ohh->tth = schedule_control_transmit (handle, size, &send_hello, ohh);
1389 GNUNET_CONTAINER_DLL_insert (handle->oh_head, handle->oh_tail, ohh);
1395 * Cancel the request to transport to offer the HELLO message
1397 * @param ohh the GNUNET_TRANSPORT_OfferHelloHandle to cancel
1400 GNUNET_TRANSPORT_offer_hello_cancel (struct GNUNET_TRANSPORT_OfferHelloHandle *ohh)
1402 struct GNUNET_TRANSPORT_Handle *th = ohh->th;
1404 cancel_control_transmit (ohh->th, ohh->tth);
1405 GNUNET_CONTAINER_DLL_remove (th->oh_head, th->oh_tail, ohh);
1406 GNUNET_free (ohh->msg);
1412 * Checks if a given peer is connected to us
1414 * @param handle connection to transport service
1415 * @param peer the peer to check
1416 * @return #GNUNET_YES (connected) or #GNUNET_NO (disconnected)
1419 GNUNET_TRANSPORT_check_peer_connected (struct GNUNET_TRANSPORT_Handle *handle,
1420 const struct GNUNET_PeerIdentity *peer)
1423 GNUNET_CONTAINER_multipeermap_contains (handle->neighbours,
1431 * Task to call the HelloUpdateCallback of the GetHelloHandle
1433 * @param cls the `struct GNUNET_TRANSPORT_GetHelloHandle`
1434 * @param tc the scheduler task context
1437 call_hello_update_cb_async (void *cls,
1438 const struct GNUNET_SCHEDULER_TaskContext *tc)
1440 struct GNUNET_TRANSPORT_GetHelloHandle *ghh = cls;
1442 GNUNET_assert (NULL != ghh->handle->my_hello);
1443 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != ghh->notify_task);
1444 ghh->notify_task = GNUNET_SCHEDULER_NO_TASK;
1445 ghh->rec (ghh->rec_cls,
1446 (const struct GNUNET_MessageHeader *) ghh->handle->my_hello);
1451 * Obtain the HELLO message for this peer. The callback given in this function
1452 * is never called synchronously.
1454 * @param handle connection to transport service
1455 * @param rec function to call with the HELLO, sender will be our peer
1456 * identity; message and sender will be NULL on timeout
1457 * (handshake with transport service pending/failed).
1458 * cost estimate will be 0.
1459 * @param rec_cls closure for @a rec
1460 * @return handle to cancel the operation
1462 struct GNUNET_TRANSPORT_GetHelloHandle *
1463 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
1464 GNUNET_TRANSPORT_HelloUpdateCallback rec,
1467 struct GNUNET_TRANSPORT_GetHelloHandle *hwl;
1469 hwl = GNUNET_new (struct GNUNET_TRANSPORT_GetHelloHandle);
1471 hwl->rec_cls = rec_cls;
1472 hwl->handle = handle;
1473 GNUNET_CONTAINER_DLL_insert (handle->hwl_head, handle->hwl_tail, hwl);
1474 if (handle->my_hello != NULL)
1475 hwl->notify_task = GNUNET_SCHEDULER_add_now (&call_hello_update_cb_async,
1482 * Stop receiving updates about changes to our HELLO message.
1484 * @param ghh handle to cancel
1487 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_GetHelloHandle *ghh)
1489 struct GNUNET_TRANSPORT_Handle *handle = ghh->handle;
1491 if (GNUNET_SCHEDULER_NO_TASK != ghh->notify_task)
1492 GNUNET_SCHEDULER_cancel (ghh->notify_task);
1493 GNUNET_CONTAINER_DLL_remove (handle->hwl_head, handle->hwl_tail, ghh);
1499 * Connect to the transport service. Note that the connection may
1500 * complete (or fail) asynchronously.
1502 * @param cfg configuration to use
1503 * @param self our own identity (API should check that it matches
1504 * the identity found by transport), or NULL (no check)
1505 * @param cls closure for the callbacks
1506 * @param rec receive function to call
1507 * @param nc function to call on connect events
1508 * @param nd function to call on disconnect events
1510 struct GNUNET_TRANSPORT_Handle *
1511 GNUNET_TRANSPORT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1512 const struct GNUNET_PeerIdentity *self, void *cls,
1513 GNUNET_TRANSPORT_ReceiveCallback rec,
1514 GNUNET_TRANSPORT_NotifyConnect nc,
1515 GNUNET_TRANSPORT_NotifyDisconnect nd)
1517 struct GNUNET_TRANSPORT_Handle *ret;
1519 ret = GNUNET_new (struct GNUNET_TRANSPORT_Handle);
1523 ret->check_self = GNUNET_YES;
1530 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1531 LOG (GNUNET_ERROR_TYPE_DEBUG,
1532 "Connecting to transport service.\n");
1533 ret->client = GNUNET_CLIENT_connect ("transport", cfg);
1534 if (NULL == ret->client)
1540 GNUNET_CONTAINER_multipeermap_create (STARTING_NEIGHBOURS_SIZE,
1543 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1544 schedule_control_transmit (ret, sizeof (struct StartMessage),
1551 * Disconnect from the transport service.
1553 * @param handle handle to the service as returned from #GNUNET_TRANSPORT_connect()
1556 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1558 LOG (GNUNET_ERROR_TYPE_DEBUG,
1559 "Transport disconnect called!\n");
1560 /* this disconnects all neighbours... */
1561 if (handle->reconnect_task == GNUNET_SCHEDULER_NO_TASK)
1562 disconnect_and_schedule_reconnect (handle);
1563 /* and now we stop trying to connect again... */
1564 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1566 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1567 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1569 GNUNET_CONTAINER_multipeermap_destroy (handle->neighbours);
1570 handle->neighbours = NULL;
1571 if (handle->quota_task != GNUNET_SCHEDULER_NO_TASK)
1573 GNUNET_SCHEDULER_cancel (handle->quota_task);
1574 handle->quota_task = GNUNET_SCHEDULER_NO_TASK;
1576 GNUNET_free_non_null (handle->my_hello);
1577 handle->my_hello = NULL;
1578 GNUNET_assert (handle->tc_head == NULL);
1579 GNUNET_assert (handle->tc_tail == NULL);
1580 GNUNET_assert (handle->hwl_head == NULL);
1581 GNUNET_assert (handle->hwl_tail == NULL);
1582 GNUNET_CONTAINER_heap_destroy (handle->ready_heap);
1583 handle->ready_heap = NULL;
1584 GNUNET_free (handle);
1589 * Check if we could queue a message of the given size for
1590 * transmission. The transport service will take both its
1591 * internal buffers and bandwidth limits imposed by the
1592 * other peer into consideration when answering this query.
1594 * @param handle connection to transport service
1595 * @param target who should receive the message
1596 * @param size how big is the message we want to transmit?
1597 * @param priority how important is the message?
1598 * @param timeout after how long should we give up (and call
1599 * notify with buf NULL and size 0)?
1600 * @param notify function to call when we are ready to
1601 * send such a message
1602 * @param notify_cls closure for @a notify
1603 * @return NULL if someone else is already waiting to be notified
1604 * non-NULL if the notify callback was queued (can be used to cancel
1605 * using #GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1607 struct GNUNET_TRANSPORT_TransmitHandle *
1608 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle *handle,
1609 const struct GNUNET_PeerIdentity *target,
1610 size_t size, uint32_t priority,
1611 struct GNUNET_TIME_Relative timeout,
1612 GNUNET_CONNECTION_TransmitReadyNotify notify,
1615 struct Neighbour *n;
1616 struct GNUNET_TRANSPORT_TransmitHandle *th;
1617 struct GNUNET_TIME_Relative delay;
1619 n = neighbour_find (handle, target);
1622 /* use GNUNET_TRANSPORT_try_connect first, only use this function
1623 * once a connection has been established */
1629 /* attempt to send two messages at the same time to the same peer */
1633 GNUNET_assert (NULL == n->hn);
1634 th = GNUNET_new (struct GNUNET_TRANSPORT_TransmitHandle);
1636 th->notify = notify;
1637 th->notify_cls = notify_cls;
1638 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1639 th->notify_size = size;
1640 th->priority = priority;
1642 /* calculate when our transmission should be ready */
1643 delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker, size + n->traffic_overhead);
1644 n->traffic_overhead = 0;
1645 if (delay.rel_value_us > timeout.rel_value_us)
1646 delay.rel_value_us = 0; /* notify immediately (with failure) */
1647 LOG (GNUNET_ERROR_TYPE_DEBUG,
1648 "Bandwidth tracker allows next transmission to peer %s in %s\n",
1649 GNUNET_i2s (target),
1650 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1651 n->hn = GNUNET_CONTAINER_heap_insert (handle->ready_heap, n, delay.rel_value_us);
1652 schedule_transmission (handle);
1658 * Cancel the specified transmission-ready notification.
1660 * @param th handle returned from #GNUNET_TRANSPORT_notify_transmit_ready()
1663 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct GNUNET_TRANSPORT_TransmitHandle *th)
1665 struct Neighbour *n;
1667 GNUNET_assert (NULL == th->next);
1668 GNUNET_assert (NULL == th->prev);
1670 GNUNET_assert (th == n->th);
1674 GNUNET_CONTAINER_heap_remove_node (n->hn);
1679 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != th->timeout_task);
1680 GNUNET_SCHEDULER_cancel (th->timeout_task);
1681 th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1687 /* end of transport_api.c */