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_TRANSPORT_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?
98 * Entry in hash table of all of our current neighbours.
103 * Overall transport handle.
105 struct GNUNET_TRANSPORT_Handle *h;
108 * Active transmit handle or NULL.
110 struct GNUNET_TRANSPORT_TransmitHandle *th;
113 * Identity of this neighbour.
115 struct GNUNET_PeerIdentity id;
118 * Outbound bandwidh tracker.
120 struct GNUNET_BANDWIDTH_Tracker out_tracker;
123 * Entry in our readyness heap (which is sorted by 'next_ready'
124 * value). NULL if there is no pending transmission request for
125 * this neighbour or if we're waiting for 'is_ready' to become
126 * true AFTER the 'out_tracker' suggested that this peer's quota
127 * has been satisfied (so once 'is_ready' goes to GNUNET_YES,
128 * we should immediately go back into the heap).
130 struct GNUNET_CONTAINER_HeapNode *hn;
133 * Is this peer currently ready to receive a message?
138 * Sending consumed more bytes on wire than payload was announced
139 * This overhead is added to the delay of next sending operation
141 size_t traffic_overhead;
146 * Linked list of functions to call whenever our HELLO is updated.
148 struct GNUNET_TRANSPORT_GetHelloHandle
152 * This is a doubly linked list.
154 struct GNUNET_TRANSPORT_GetHelloHandle *next;
157 * This is a doubly linked list.
159 struct GNUNET_TRANSPORT_GetHelloHandle *prev;
164 struct GNUNET_TRANSPORT_Handle *handle;
167 * Callback to call once we got our HELLO.
169 GNUNET_TRANSPORT_HelloUpdateCallback rec;
172 * Task for calling the HelloUpdateCallback when we already have a HELLO
174 GNUNET_SCHEDULER_TaskIdentifier notify_task;
177 * Closure for @e rec.
184 * Linked list for all try-connect requests
186 struct GNUNET_TRANSPORT_TryConnectHandle
191 struct GNUNET_TRANSPORT_TryConnectHandle *prev;
196 struct GNUNET_TRANSPORT_TryConnectHandle *next;
198 struct GNUNET_PeerIdentity pid;
200 struct GNUNET_TRANSPORT_Handle *th;
202 struct GNUNET_TRANSPORT_TransmitHandle *tth;
204 GNUNET_TRANSPORT_TryConnectCallback cb;
214 * Linked list for all try-connect requests
216 struct GNUNET_TRANSPORT_OfferHelloHandle
221 struct GNUNET_TRANSPORT_OfferHelloHandle *prev;
226 struct GNUNET_TRANSPORT_OfferHelloHandle *next;
228 struct GNUNET_TRANSPORT_Handle *th;
230 struct GNUNET_TRANSPORT_TransmitHandle *tth;
232 GNUNET_SCHEDULER_Task cont;
235 * Closure for @e cont
239 struct GNUNET_MessageHeader *msg;
244 * Handle for the transport service (includes all of the
245 * state for the transport service).
247 struct GNUNET_TRANSPORT_Handle
251 * Closure for the callbacks.
256 * Function to call for received data.
258 GNUNET_TRANSPORT_ReceiveCallback rec;
261 * function to call on connect events
263 GNUNET_TRANSPORT_NotifyConnect nc_cb;
266 * function to call on disconnect events
268 GNUNET_TRANSPORT_NotifyDisconnect nd_cb;
271 * function to call on excess bandwidth events
273 GNUNET_TRANSPORT_NotifyExcessBandwidth neb_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);
423 * The outbound quota has changed in a way that may require
424 * us to reset the timeout. Update the timeout.
426 * @param cls the `struct Neighbour` for which the timeout changed
429 outbound_bw_tracker_update (void *cls)
431 struct Neighbour *n = cls;
432 struct GNUNET_TIME_Relative delay;
436 delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
437 n->th->notify_size + n->traffic_overhead);
438 LOG (GNUNET_ERROR_TYPE_DEBUG,
439 "New outbound delay %llu us\n",
440 GNUNET_STRINGS_relative_time_to_string (delay,
442 GNUNET_CONTAINER_heap_update_cost (n->h->ready_heap,
443 n->hn, delay.rel_value_us);
444 schedule_transmission (n->h);
449 * Function called by the bandwidth tracker if we have excess
452 * @param cls the `struct Neighbour` that has excess bandwidth
455 notify_excess_cb (void *cls)
457 struct Neighbour *n = cls;
458 struct GNUNET_TRANSPORT_Handle *h = n->h;
460 if (NULL != h->neb_cb)
467 * Add neighbour to our list
469 * @return NULL if this API is currently disconnecting from the service
471 static struct Neighbour *
472 neighbour_add (struct GNUNET_TRANSPORT_Handle *h,
473 const struct GNUNET_PeerIdentity *pid)
477 LOG (GNUNET_ERROR_TYPE_DEBUG,
478 "Creating entry for neighbour `%4s'.\n",
480 n = GNUNET_new (struct Neighbour);
483 n->is_ready = GNUNET_YES;
484 n->traffic_overhead = 0;
485 GNUNET_BANDWIDTH_tracker_init2 (&n->out_tracker,
486 &outbound_bw_tracker_update, n,
487 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
488 MAX_BANDWIDTH_CARRY_S,
491 GNUNET_assert (GNUNET_OK ==
492 GNUNET_CONTAINER_multipeermap_put (h->neighbours,
494 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
500 * Iterator over hash map entries, for deleting state of a neighbour.
502 * @param cls the `struct GNUNET_TRANSPORT_Handle *`
503 * @param key peer identity
504 * @param value value in the hash map, the neighbour entry to delete
505 * @return #GNUNET_YES if we should continue to
510 neighbour_delete (void *cls,
511 const struct GNUNET_PeerIdentity *key, void *value)
513 struct GNUNET_TRANSPORT_Handle *handle = cls;
514 struct Neighbour *n = value;
516 if (NULL != handle->nd_cb)
517 handle->nd_cb (handle->cls, &n->id);
518 GNUNET_assert (NULL == n->th);
519 GNUNET_assert (NULL == n->hn);
520 GNUNET_assert (GNUNET_YES ==
521 GNUNET_CONTAINER_multipeermap_remove (handle->neighbours, key,
529 * Function we use for handling incoming messages.
531 * @param cls closure, a `struct GNUNET_TRANSPORT_Handle *`
532 * @param msg message received, NULL on timeout or fatal error
535 demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
537 struct GNUNET_TRANSPORT_Handle *h = cls;
538 const struct DisconnectInfoMessage *dim;
539 const struct ConnectInfoMessage *cim;
540 const struct InboundMessage *im;
541 const struct GNUNET_MessageHeader *imm;
542 const struct SendOkMessage *okm;
543 const struct QuotaSetMessage *qm;
544 struct GNUNET_TRANSPORT_GetHelloHandle *hwl;
545 struct GNUNET_TRANSPORT_GetHelloHandle *next_hwl;
547 struct GNUNET_PeerIdentity me;
550 uint32_t bytes_physical;
552 GNUNET_assert (NULL != h->client);
553 if (GNUNET_YES == h->reconnecting)
559 LOG (GNUNET_ERROR_TYPE_DEBUG,
560 "Error receiving from transport service, disconnecting temporarily.\n");
561 h->reconnecting = GNUNET_YES;
562 disconnect_and_schedule_reconnect (h);
565 GNUNET_CLIENT_receive (h->client, &demultiplexer, h,
566 GNUNET_TIME_UNIT_FOREVER_REL);
567 size = ntohs (msg->size);
568 switch (ntohs (msg->type))
570 case GNUNET_MESSAGE_TYPE_HELLO:
572 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg, &me))
577 LOG (GNUNET_ERROR_TYPE_DEBUG,
578 "Receiving (my own) `%s' message, I am `%4s'.\n", "HELLO",
580 GNUNET_free_non_null (h->my_hello);
582 if (size < sizeof (struct GNUNET_MessageHeader))
587 h->my_hello = GNUNET_malloc (size);
588 memcpy (h->my_hello, msg, size);
592 next_hwl = hwl->next;
593 hwl->rec (hwl->rec_cls,
594 (const struct GNUNET_MessageHeader *) h->my_hello);
598 case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
599 if (size < sizeof (struct ConnectInfoMessage))
604 cim = (const struct ConnectInfoMessage *) msg;
606 sizeof (struct ConnectInfoMessage))
611 LOG (GNUNET_ERROR_TYPE_DEBUG,
612 "Receiving `%s' message for `%4s'.\n",
613 "CONNECT", GNUNET_i2s (&cim->id));
614 n = neighbour_find (h, &cim->id);
620 n = neighbour_add (h, &cim->id);
621 LOG (GNUNET_ERROR_TYPE_DEBUG,
622 "Receiving `%s' message for `%4s' with quota %u\n",
624 GNUNET_i2s (&cim->id),
625 ntohl (cim->quota_out.value__));
626 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker, cim->quota_out);
627 if (h->nc_cb != NULL)
628 h->nc_cb (h->cls, &n->id);
630 case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
631 if (size != sizeof (struct DisconnectInfoMessage))
636 dim = (const struct DisconnectInfoMessage *) msg;
637 GNUNET_break (ntohl (dim->reserved) == 0);
638 LOG (GNUNET_ERROR_TYPE_DEBUG,
639 "Receiving `%s' message for `%4s'.\n",
640 "DISCONNECT", GNUNET_i2s (&dim->peer));
641 n = neighbour_find (h, &dim->peer);
647 neighbour_delete (h, &dim->peer, n);
649 case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
650 if (size != sizeof (struct SendOkMessage))
655 okm = (const struct SendOkMessage *) msg;
656 bytes_msg = ntohl (okm->bytes_msg);
657 bytes_physical = ntohl (okm->bytes_physical);
658 LOG (GNUNET_ERROR_TYPE_DEBUG,
659 "Receiving SEND_OK message, transmission %s.\n",
660 ntohl (okm->success) == GNUNET_OK ? "succeeded" : "failed");
662 n = neighbour_find (h, &okm->peer);
666 if (bytes_physical >= bytes_msg)
668 LOG (GNUNET_ERROR_TYPE_DEBUG,
669 "Overhead for %u byte message: %u\n",
670 bytes_msg, bytes_physical - bytes_msg);
671 n->traffic_overhead += bytes_physical - bytes_msg;
673 GNUNET_break (GNUNET_NO == n->is_ready);
674 n->is_ready = GNUNET_YES;
675 if ((NULL != n->th) && (NULL == n->hn))
677 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->th->timeout_task);
678 GNUNET_SCHEDULER_cancel (n->th->timeout_task);
679 n->th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
680 /* we've been waiting for this (congestion, not quota,
681 * caused delayed transmission) */
682 n->hn = GNUNET_CONTAINER_heap_insert (h->ready_heap, n, 0);
683 schedule_transmission (h);
686 case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
687 LOG (GNUNET_ERROR_TYPE_DEBUG,
688 "Receiving `%s' message.\n",
691 sizeof (struct InboundMessage) + sizeof (struct GNUNET_MessageHeader))
696 im = (const struct InboundMessage *) msg;
697 imm = (const struct GNUNET_MessageHeader *) &im[1];
698 if (ntohs (imm->size) + sizeof (struct InboundMessage) != size)
703 LOG (GNUNET_ERROR_TYPE_DEBUG,
704 "Received message of type %u from `%4s'.\n",
705 ntohs (imm->type), GNUNET_i2s (&im->peer));
706 n = neighbour_find (h, &im->peer);
713 h->rec (h->cls, &im->peer, imm);
715 case GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA:
716 LOG (GNUNET_ERROR_TYPE_DEBUG,
717 "Receiving `%s' message.\n", "SET_QUOTA");
718 if (size != sizeof (struct QuotaSetMessage))
723 qm = (const struct QuotaSetMessage *) msg;
724 n = neighbour_find (h, &qm->peer);
727 LOG (GNUNET_ERROR_TYPE_DEBUG,
728 "Receiving `%s' message for `%4s' with quota %u\n",
730 GNUNET_i2s (&qm->peer),
731 ntohl (qm->quota.value__));
732 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker, qm->quota);
735 LOG (GNUNET_ERROR_TYPE_ERROR,
736 _("Received unexpected message of type %u in %s:%u\n"),
737 ntohs (msg->type), __FILE__, __LINE__);
745 * A transmission request could not be satisfied because of
746 * network congestion. Notify the initiator and clean up.
748 * @param cls the `struct GNUNET_TRANSPORT_TransmitHandle`
749 * @param tc scheduler context
752 timeout_request_due_to_congestion (void *cls,
753 const struct GNUNET_SCHEDULER_TaskContext *tc)
755 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
756 struct Neighbour *n = th->neighbour;
758 n->th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
759 GNUNET_assert (th == n->th);
760 GNUNET_assert (NULL == n->hn);
762 th->notify (th->notify_cls, 0, NULL);
768 * Transmit message(s) to service.
770 * @param cls handle to transport
771 * @param size number of bytes available in @a buf
772 * @param buf where to copy the message
773 * @return number of bytes copied to @a buf
776 transport_notify_ready (void *cls, size_t size, void *buf)
778 struct GNUNET_TRANSPORT_Handle *h = cls;
779 struct GNUNET_TRANSPORT_TransmitHandle *th;
782 struct OutboundMessage obm;
787 GNUNET_assert (NULL != h->client);
791 /* transmission failed */
792 disconnect_and_schedule_reconnect (h);
798 /* first send control messages */
799 while ((NULL != (th = h->control_head)) && (th->notify_size <= size))
801 GNUNET_CONTAINER_DLL_remove (h->control_head, h->control_tail, th);
802 nret = th->notify (th->notify_cls, size, &cbuf[ret]);
803 LOG (GNUNET_ERROR_TYPE_DEBUG,
804 "Added %u bytes of control message at %u\n",
811 /* then, if possible and no control messages pending, send data messages */
812 while ((NULL == h->control_head) &&
813 (NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap))))
815 if (GNUNET_YES != n->is_ready)
817 /* peer not ready, wait for notification! */
818 GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
820 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == n->th->timeout_task);
821 n->th->timeout_task =
822 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
824 &timeout_request_due_to_congestion,
829 if (th->notify_size + sizeof (struct OutboundMessage) > size)
830 break; /* does not fit */
831 if (GNUNET_BANDWIDTH_tracker_get_delay
832 (&n->out_tracker, th->notify_size).rel_value_us > 0)
833 break; /* too early */
834 GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
837 n->is_ready = GNUNET_NO;
838 GNUNET_assert (size >= sizeof (struct OutboundMessage));
840 th->notify (th->notify_cls, size - sizeof (struct OutboundMessage),
841 &cbuf[ret + sizeof (struct OutboundMessage)]);
842 GNUNET_assert (mret <= size - sizeof (struct OutboundMessage));
845 GNUNET_assert (mret + sizeof (struct OutboundMessage) <
846 GNUNET_SERVER_MAX_MESSAGE_SIZE);
847 obm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
848 obm.header.size = htons (mret + sizeof (struct OutboundMessage));
849 obm.reserved = htonl (0);
851 GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining
854 memcpy (&cbuf[ret], &obm, sizeof (struct OutboundMessage));
855 ret += (mret + sizeof (struct OutboundMessage));
856 size -= (mret + sizeof (struct OutboundMessage));
857 GNUNET_BANDWIDTH_tracker_consume (&n->out_tracker, mret);
861 /* if there are more pending messages, try to schedule those */
862 schedule_transmission (h);
863 LOG (GNUNET_ERROR_TYPE_DEBUG,
864 "Transmitting %u bytes to transport service\n",
871 * Schedule the task to send one message, either from the control
872 * list or the peer message queues to the service.
874 * @param cls transport service to schedule a transmission for
875 * @param tc scheduler context
878 schedule_transmission_task (void *cls,
879 const struct GNUNET_SCHEDULER_TaskContext *tc)
881 struct GNUNET_TRANSPORT_Handle *h = cls;
883 struct GNUNET_TRANSPORT_TransmitHandle *th;
886 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
887 GNUNET_assert (NULL != h->client);
888 /* destroy all requests that have timed out */
889 while ((NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap))) &&
890 (0 == GNUNET_TIME_absolute_get_remaining (n->th->timeout).rel_value_us))
892 /* notify client that the request could not be satisfied within
893 * the given time constraints */
896 GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
898 LOG (GNUNET_ERROR_TYPE_DEBUG,
899 "Signalling timeout for transmission to peer %s due to congestion\n",
900 GNUNET_i2s (&n->id));
901 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
906 if (NULL != h->control_head)
908 size = h->control_head->notify_size;
912 n = GNUNET_CONTAINER_heap_peek (h->ready_heap);
914 return; /* no pending messages */
915 size = n->th->notify_size + sizeof (struct OutboundMessage);
917 LOG (GNUNET_ERROR_TYPE_DEBUG,
918 "Calling notify_transmit_ready\n");
920 GNUNET_CLIENT_notify_transmit_ready (h->client, size,
921 GNUNET_TIME_UNIT_FOREVER_REL,
922 GNUNET_NO, &transport_notify_ready,
924 GNUNET_assert (NULL != h->cth);
929 * Schedule the task to send one message, either from the control
930 * list or the peer message queues to the service.
932 * @param h transport service to schedule a transmission for
935 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
937 struct GNUNET_TIME_Relative delay;
940 GNUNET_assert (NULL != h->client);
941 if (h->quota_task != GNUNET_SCHEDULER_NO_TASK)
943 GNUNET_SCHEDULER_cancel (h->quota_task);
944 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
946 if (NULL != h->control_head)
947 delay = GNUNET_TIME_UNIT_ZERO;
948 else if (NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap)))
951 GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
952 n->th->notify_size + n->traffic_overhead);
953 n->traffic_overhead = 0;
956 return; /* no work to be done */
957 LOG (GNUNET_ERROR_TYPE_DEBUG,
958 "Scheduling next transmission to service in %s\n",
959 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
961 GNUNET_SCHEDULER_add_delayed (delay, &schedule_transmission_task, h);
966 * Queue control request for transmission to the transport
969 * @param h handle to the transport service
970 * @param size number of bytes to be transmitted
971 * @param notify function to call to get the content
972 * @param notify_cls closure for @a notify
973 * @return a `struct GNUNET_TRANSPORT_TransmitHandle`
975 static struct GNUNET_TRANSPORT_TransmitHandle *
976 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h, size_t size,
977 GNUNET_TRANSPORT_TransmitReadyNotify notify,
980 struct GNUNET_TRANSPORT_TransmitHandle *th;
982 LOG (GNUNET_ERROR_TYPE_DEBUG,
983 "Control transmit of %u bytes requested\n",
985 th = GNUNET_new (struct GNUNET_TRANSPORT_TransmitHandle);
987 th->notify_cls = notify_cls;
988 th->notify_size = size;
989 GNUNET_CONTAINER_DLL_insert_tail (h->control_head, h->control_tail, th);
990 schedule_transmission (h);
996 * Transmit START message to service.
999 * @param size number of bytes available in @a buf
1000 * @param buf where to copy the message
1001 * @return number of bytes copied to @a buf
1004 send_start (void *cls, size_t size, void *buf)
1006 struct GNUNET_TRANSPORT_Handle *h = cls;
1007 struct StartMessage s;
1012 /* Can only be shutdown, just give up */
1013 LOG (GNUNET_ERROR_TYPE_DEBUG,
1014 "Shutdown while trying to transmit `%s' request.\n",
1018 LOG (GNUNET_ERROR_TYPE_DEBUG,
1019 "Transmitting `%s' request.\n",
1021 GNUNET_assert (size >= sizeof (struct StartMessage));
1022 s.header.size = htons (sizeof (struct StartMessage));
1023 s.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
1029 s.options = htonl (options);
1031 memcpy (buf, &s, sizeof (struct StartMessage));
1032 GNUNET_CLIENT_receive (h->client, &demultiplexer, h,
1033 GNUNET_TIME_UNIT_FOREVER_REL);
1034 return sizeof (struct StartMessage);
1039 * Try again to connect to transport service.
1041 * @param cls the handle to the transport service
1042 * @param tc scheduler context
1045 reconnect (void *cls,
1046 const struct GNUNET_SCHEDULER_TaskContext *tc)
1048 struct GNUNET_TRANSPORT_Handle *h = cls;
1050 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1051 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1053 /* shutdown, just give up */
1056 LOG (GNUNET_ERROR_TYPE_DEBUG,
1057 "Connecting to transport service.\n");
1058 GNUNET_assert (NULL == h->client);
1059 GNUNET_assert (NULL == h->control_head);
1060 GNUNET_assert (NULL == h->control_tail);
1061 h->reconnecting = GNUNET_NO;
1062 h->client = GNUNET_CLIENT_connect ("transport", h->cfg);
1064 GNUNET_assert (NULL != h->client);
1065 schedule_control_transmit (h, sizeof (struct StartMessage),
1071 * Function that will schedule the job that will try
1072 * to connect us again to the client.
1074 * @param h transport service to reconnect
1077 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
1079 struct GNUNET_TRANSPORT_TransmitHandle *th;
1081 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
1084 GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
1087 if (NULL != h->client)
1089 GNUNET_CLIENT_disconnect (h->client);
1091 /* LOG (GNUNET_ERROR_TYPE_ERROR,
1092 "Client disconnect done \n");*/
1094 /* Forget about all neighbours that we used to be connected to */
1095 GNUNET_CONTAINER_multipeermap_iterate (h->neighbours,
1096 &neighbour_delete, h);
1097 if (h->quota_task != GNUNET_SCHEDULER_NO_TASK)
1099 GNUNET_SCHEDULER_cancel (h->quota_task);
1100 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
1102 while ((NULL != (th = h->control_head)))
1104 GNUNET_CONTAINER_DLL_remove (h->control_head, h->control_tail, th);
1105 th->notify (th->notify_cls, 0, NULL);
1108 LOG (GNUNET_ERROR_TYPE_DEBUG,
1109 "Scheduling task to reconnect to transport service in %s.\n",
1110 GNUNET_STRINGS_relative_time_to_string(h->reconnect_delay, GNUNET_YES));
1112 GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
1113 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
1118 * Cancel control request for transmission to the transport service.
1120 * @param th handle to the transport service
1121 * @param tth transmit handle to cancel
1124 cancel_control_transmit (struct GNUNET_TRANSPORT_Handle *th,
1125 struct GNUNET_TRANSPORT_TransmitHandle *tth)
1127 LOG (GNUNET_ERROR_TYPE_DEBUG,
1128 "Canceling transmit of contral transmission requested\n");
1129 GNUNET_CONTAINER_DLL_remove (th->control_head, th->control_tail, tth);
1136 * Send REQUEST_CONNECT message to the service.
1138 * @param cls the `struct GNUNET_PeerIdentity`
1139 * @param size number of bytes available in @a buf
1140 * @param buf where to copy the message
1141 * @return number of bytes copied to @a buf
1144 send_try_connect (void *cls, size_t size, void *buf)
1146 struct GNUNET_TRANSPORT_TryConnectHandle *tch = cls;
1147 struct TransportRequestConnectMessage msg;
1151 if (NULL != tch->cb)
1152 tch->cb (tch->cb_cls, GNUNET_SYSERR);
1153 GNUNET_CONTAINER_DLL_remove (tch->th->tc_head, tch->th->tc_tail, tch);
1154 LOG (GNUNET_ERROR_TYPE_DEBUG,
1155 "Discarding `%s' request to `%4s' due to error in transport service connection.\n",
1157 GNUNET_i2s (&tch->pid));
1161 LOG (GNUNET_ERROR_TYPE_DEBUG,
1162 "Transmitting `%s' request with respect to `%4s'.\n",
1164 GNUNET_i2s (&tch->pid));
1165 GNUNET_assert (size >= sizeof (struct TransportRequestConnectMessage));
1166 msg.header.size = htons (sizeof (struct TransportRequestConnectMessage));
1167 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT);
1168 msg.reserved = htonl (0);
1169 msg.peer = tch->pid;
1170 memcpy (buf, &msg, sizeof (msg));
1171 if (NULL != tch->cb)
1172 tch->cb (tch->cb_cls, GNUNET_OK);
1173 GNUNET_CONTAINER_DLL_remove (tch->th->tc_head, tch->th->tc_tail, tch);
1175 return sizeof (struct TransportRequestConnectMessage);
1179 * Ask the transport service to establish a connection to
1182 * @param handle connection to transport service
1183 * @param target who we should try to connect to
1184 * @param cb callback to be called when request was transmitted to transport
1186 * @param cb_cls closure for the callback
1187 * @return a `struct GNUNET_TRANSPORT_TryConnectHandle` handle or
1188 * NULL on failure (cb will not be called)
1190 struct GNUNET_TRANSPORT_TryConnectHandle *
1191 GNUNET_TRANSPORT_try_connect (struct GNUNET_TRANSPORT_Handle *handle,
1192 const struct GNUNET_PeerIdentity *target,
1193 GNUNET_TRANSPORT_TryConnectCallback cb,
1196 struct GNUNET_TRANSPORT_TryConnectHandle *tch = NULL;
1198 if (NULL == handle->client)
1200 tch = GNUNET_new (struct GNUNET_TRANSPORT_TryConnectHandle);
1202 tch->pid = *(target);
1204 tch->cb_cls = cb_cls;
1205 tch->tth = schedule_control_transmit (handle,
1206 sizeof (struct TransportRequestConnectMessage),
1207 &send_try_connect, tch);
1208 GNUNET_CONTAINER_DLL_insert(handle->tc_head, handle->tc_tail, tch);
1214 * Cancel the request to transport to try a connect
1215 * Callback will not be called
1217 * @param tch the handle to cancel
1220 GNUNET_TRANSPORT_try_connect_cancel (struct GNUNET_TRANSPORT_TryConnectHandle *tch)
1222 struct GNUNET_TRANSPORT_Handle *th;
1225 cancel_control_transmit (th, tch->tth);
1226 GNUNET_CONTAINER_DLL_remove (th->tc_head, th->tc_tail, tch);
1232 * Send HELLO message to the service.
1234 * @param cls the HELLO message to send
1235 * @param size number of bytes available in @a buf
1236 * @param buf where to copy the message
1237 * @return number of bytes copied to @a buf
1240 send_hello (void *cls, size_t size, void *buf)
1242 struct GNUNET_TRANSPORT_OfferHelloHandle *ohh = cls;
1243 struct GNUNET_MessageHeader *msg = ohh->msg;
1245 struct GNUNET_SCHEDULER_TaskContext tc;
1247 tc.read_ready = NULL;
1248 tc.write_ready = NULL;
1249 tc.reason = GNUNET_SCHEDULER_REASON_TIMEOUT;
1252 LOG (GNUNET_ERROR_TYPE_DEBUG,
1253 "Timeout while trying to transmit `%s' request.\n",
1255 if (NULL != ohh->cont)
1256 ohh->cont (ohh->cls, &tc);
1258 GNUNET_CONTAINER_DLL_remove (ohh->th->oh_head, ohh->th->oh_tail, ohh);
1262 LOG (GNUNET_ERROR_TYPE_DEBUG,
1263 "Transmitting `%s' request.\n",
1265 ssize = ntohs (msg->size);
1266 GNUNET_assert (size >= ssize);
1267 memcpy (buf, msg, ssize);
1269 tc.reason = GNUNET_SCHEDULER_REASON_READ_READY;
1270 if (NULL != ohh->cont)
1271 ohh->cont (ohh->cls, &tc);
1272 GNUNET_CONTAINER_DLL_remove (ohh->th->oh_head, ohh->th->oh_tail, ohh);
1279 * Send traffic metric message to the service.
1281 * @param cls the message to send
1282 * @param size number of bytes available in @a buf
1283 * @param buf where to copy the message
1284 * @return number of bytes copied to @a buf
1287 send_metric (void *cls, size_t size, void *buf)
1289 struct TrafficMetricMessage *msg = cls;
1294 LOG (GNUNET_ERROR_TYPE_DEBUG,
1295 "Timeout while trying to transmit `%s' request.\n",
1300 LOG (GNUNET_ERROR_TYPE_DEBUG,
1301 "Transmitting `%s' request.\n",
1303 ssize = ntohs (msg->header.size);
1304 GNUNET_assert (size >= ssize);
1305 memcpy (buf, msg, ssize);
1312 * Set transport metrics for a peer and a direction
1314 * @param handle transport handle
1315 * @param peer the peer to set the metric for
1316 * @param inbound set inbound direction (#GNUNET_YES or #GNUNET_NO)
1317 * @param outbound set outbound direction (#GNUNET_YES or #GNUNET_NO)
1318 * @param ats the metric as ATS information
1319 * @param ats_count the number of metrics
1321 * Supported ATS values:
1322 * #GNUNET_ATS_QUALITY_NET_DELAY (value in ms)
1323 * #GNUNET_ATS_QUALITY_NET_DISTANCE (value in count(hops))
1326 * To enforce a delay of 10 ms for peer p1 in sending direction use:
1328 * struct GNUNET_ATS_Information ats;
1329 * ats.type = ntohl (GNUNET_ATS_QUALITY_NET_DELAY);
1330 * ats.value = ntohl (10);
1331 * GNUNET_TRANSPORT_set_traffic_metric (th, p1, TM_SEND, &ats, 1);
1334 * Delay restrictions in receiving direction will be enforced with
1338 GNUNET_TRANSPORT_set_traffic_metric (struct GNUNET_TRANSPORT_Handle *handle,
1339 const struct GNUNET_PeerIdentity *peer,
1342 const struct GNUNET_ATS_Information *ats,
1345 struct TrafficMetricMessage *msg;
1347 GNUNET_assert ((outbound == GNUNET_YES) || (outbound == GNUNET_NO));
1348 GNUNET_assert ((inbound == GNUNET_YES) || (inbound == GNUNET_NO));
1349 if ((GNUNET_NO == inbound) && (GNUNET_NO == outbound))
1354 size_t len = sizeof (struct TrafficMetricMessage) +
1355 ats_count * sizeof (struct GNUNET_ATS_Information);
1357 msg = GNUNET_malloc (len);
1358 msg->header.size = htons (len);
1359 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TRAFFIC_METRIC);
1360 msg->direction = htons (0 + outbound + 2 * inbound);
1361 msg->ats_count = htons (ats_count);
1362 msg->peer = (*peer);
1363 memcpy (&msg[1], ats, ats_count * sizeof (struct GNUNET_ATS_Information));
1364 schedule_control_transmit (handle, len,
1370 * Offer the transport service the HELLO of another peer. Note that
1371 * the transport service may just ignore this message if the HELLO is
1372 * malformed or useless due to our local configuration.
1374 * @param handle connection to transport service
1375 * @param hello the hello message
1376 * @param cont continuation to call when HELLO has been sent,
1377 * tc reason #GNUNET_SCHEDULER_REASON_TIMEOUT for fail
1378 * tc reasong #GNUNET_SCHEDULER_REASON_READ_READY for success
1379 * @param cls closure for continuation
1380 * @return a `struct GNUNET_TRANSPORT_OfferHelloHandle` handle or NULL on failure,
1381 * in case of failure cont will not be called
1384 struct GNUNET_TRANSPORT_OfferHelloHandle *
1385 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
1386 const struct GNUNET_MessageHeader *hello,
1387 GNUNET_SCHEDULER_Task cont, void *cls)
1389 struct GNUNET_TRANSPORT_OfferHelloHandle *ohh;
1390 struct GNUNET_MessageHeader *msg;
1391 struct GNUNET_PeerIdentity peer;
1394 if (NULL == handle->client)
1396 GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
1397 size = ntohs (hello->size);
1398 GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
1400 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hello, &peer))
1406 msg = GNUNET_malloc (size);
1407 memcpy (msg, hello, size);
1408 LOG (GNUNET_ERROR_TYPE_DEBUG,
1409 "Offering `%s' message of `%4s' to transport for validation.\n", "HELLO",
1410 GNUNET_i2s (&peer));
1412 ohh = GNUNET_new (struct GNUNET_TRANSPORT_OfferHelloHandle);
1417 ohh->tth = schedule_control_transmit (handle, size,
1419 GNUNET_CONTAINER_DLL_insert (handle->oh_head, handle->oh_tail, ohh);
1425 * Cancel the request to transport to offer the HELLO message
1427 * @param ohh the GNUNET_TRANSPORT_OfferHelloHandle to cancel
1430 GNUNET_TRANSPORT_offer_hello_cancel (struct GNUNET_TRANSPORT_OfferHelloHandle *ohh)
1432 struct GNUNET_TRANSPORT_Handle *th = ohh->th;
1434 cancel_control_transmit (ohh->th, ohh->tth);
1435 GNUNET_CONTAINER_DLL_remove (th->oh_head, th->oh_tail, ohh);
1436 GNUNET_free (ohh->msg);
1442 * Checks if a given peer is connected to us
1444 * @param handle connection to transport service
1445 * @param peer the peer to check
1446 * @return #GNUNET_YES (connected) or #GNUNET_NO (disconnected)
1449 GNUNET_TRANSPORT_check_peer_connected (struct GNUNET_TRANSPORT_Handle *handle,
1450 const struct GNUNET_PeerIdentity *peer)
1453 GNUNET_CONTAINER_multipeermap_contains (handle->neighbours,
1461 * Task to call the HelloUpdateCallback of the GetHelloHandle
1463 * @param cls the `struct GNUNET_TRANSPORT_GetHelloHandle`
1464 * @param tc the scheduler task context
1467 call_hello_update_cb_async (void *cls,
1468 const struct GNUNET_SCHEDULER_TaskContext *tc)
1470 struct GNUNET_TRANSPORT_GetHelloHandle *ghh = cls;
1472 GNUNET_assert (NULL != ghh->handle->my_hello);
1473 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != ghh->notify_task);
1474 ghh->notify_task = GNUNET_SCHEDULER_NO_TASK;
1475 ghh->rec (ghh->rec_cls,
1476 (const struct GNUNET_MessageHeader *) ghh->handle->my_hello);
1481 * Obtain the HELLO message for this peer. The callback given in this function
1482 * is never called synchronously.
1484 * @param handle connection to transport service
1485 * @param rec function to call with the HELLO, sender will be our peer
1486 * identity; message and sender will be NULL on timeout
1487 * (handshake with transport service pending/failed).
1488 * cost estimate will be 0.
1489 * @param rec_cls closure for @a rec
1490 * @return handle to cancel the operation
1492 struct GNUNET_TRANSPORT_GetHelloHandle *
1493 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
1494 GNUNET_TRANSPORT_HelloUpdateCallback rec,
1497 struct GNUNET_TRANSPORT_GetHelloHandle *hwl;
1499 hwl = GNUNET_new (struct GNUNET_TRANSPORT_GetHelloHandle);
1501 hwl->rec_cls = rec_cls;
1502 hwl->handle = handle;
1503 GNUNET_CONTAINER_DLL_insert (handle->hwl_head, handle->hwl_tail, hwl);
1504 if (handle->my_hello != NULL)
1505 hwl->notify_task = GNUNET_SCHEDULER_add_now (&call_hello_update_cb_async,
1512 * Stop receiving updates about changes to our HELLO message.
1514 * @param ghh handle to cancel
1517 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_GetHelloHandle *ghh)
1519 struct GNUNET_TRANSPORT_Handle *handle = ghh->handle;
1521 if (GNUNET_SCHEDULER_NO_TASK != ghh->notify_task)
1522 GNUNET_SCHEDULER_cancel (ghh->notify_task);
1523 GNUNET_CONTAINER_DLL_remove (handle->hwl_head, handle->hwl_tail, ghh);
1529 * Connect to the transport service. Note that the connection may
1530 * complete (or fail) asynchronously.
1532 * @param cfg configuration to use
1533 * @param self our own identity (API should check that it matches
1534 * the identity found by transport), or NULL (no check)
1535 * @param cls closure for the callbacks
1536 * @param rec receive function to call
1537 * @param nc function to call on connect events
1538 * @param nd function to call on disconnect events
1539 * @return NULL on error
1541 struct GNUNET_TRANSPORT_Handle *
1542 GNUNET_TRANSPORT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1543 const struct GNUNET_PeerIdentity *self, void *cls,
1544 GNUNET_TRANSPORT_ReceiveCallback rec,
1545 GNUNET_TRANSPORT_NotifyConnect nc,
1546 GNUNET_TRANSPORT_NotifyDisconnect nd)
1548 return GNUNET_TRANSPORT_connect2 (cfg, self, cls,
1554 * Connect to the transport service. Note that the connection may
1555 * complete (or fail) asynchronously.
1557 * @param cfg configuration to use
1558 * @param self our own identity (API should check that it matches
1559 * the identity found by transport), or NULL (no check)
1560 * @param cls closure for the callbacks
1561 * @param rec receive function to call
1562 * @param nc function to call on connect events
1563 * @param nd function to call on disconnect events
1564 * @param neb function to call if we have excess bandwidth to a peer
1565 * @return NULL on error
1567 struct GNUNET_TRANSPORT_Handle *
1568 GNUNET_TRANSPORT_connect2 (const struct GNUNET_CONFIGURATION_Handle *cfg,
1569 const struct GNUNET_PeerIdentity *self, void *cls,
1570 GNUNET_TRANSPORT_ReceiveCallback rec,
1571 GNUNET_TRANSPORT_NotifyConnect nc,
1572 GNUNET_TRANSPORT_NotifyDisconnect nd,
1573 GNUNET_TRANSPORT_NotifyExcessBandwidth neb)
1575 struct GNUNET_TRANSPORT_Handle *ret;
1577 ret = GNUNET_new (struct GNUNET_TRANSPORT_Handle);
1581 ret->check_self = GNUNET_YES;
1589 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1590 LOG (GNUNET_ERROR_TYPE_DEBUG,
1591 "Connecting to transport service.\n");
1592 ret->client = GNUNET_CLIENT_connect ("transport", cfg);
1593 if (NULL == ret->client)
1599 GNUNET_CONTAINER_multipeermap_create (STARTING_NEIGHBOURS_SIZE,
1602 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1603 schedule_control_transmit (ret, sizeof (struct StartMessage),
1610 * Disconnect from the transport service.
1612 * @param handle handle to the service as returned from #GNUNET_TRANSPORT_connect()
1615 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1617 LOG (GNUNET_ERROR_TYPE_DEBUG,
1618 "Transport disconnect called!\n");
1619 /* this disconnects all neighbours... */
1620 if (handle->reconnect_task == GNUNET_SCHEDULER_NO_TASK)
1621 disconnect_and_schedule_reconnect (handle);
1622 /* and now we stop trying to connect again... */
1623 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1625 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1626 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1628 GNUNET_CONTAINER_multipeermap_destroy (handle->neighbours);
1629 handle->neighbours = NULL;
1630 if (handle->quota_task != GNUNET_SCHEDULER_NO_TASK)
1632 GNUNET_SCHEDULER_cancel (handle->quota_task);
1633 handle->quota_task = GNUNET_SCHEDULER_NO_TASK;
1635 GNUNET_free_non_null (handle->my_hello);
1636 handle->my_hello = NULL;
1637 GNUNET_assert (NULL == handle->tc_head);
1638 GNUNET_assert (NULL == handle->tc_tail);
1639 GNUNET_assert (NULL == handle->hwl_head);
1640 GNUNET_assert (NULL == handle->hwl_tail);
1641 GNUNET_CONTAINER_heap_destroy (handle->ready_heap);
1642 handle->ready_heap = NULL;
1643 GNUNET_free (handle);
1648 * Check if we could queue a message of the given size for
1649 * transmission. The transport service will take both its
1650 * internal buffers and bandwidth limits imposed by the
1651 * other peer into consideration when answering this query.
1653 * @param handle connection to transport service
1654 * @param target who should receive the message
1655 * @param size how big is the message we want to transmit?
1656 * @param timeout after how long should we give up (and call
1657 * notify with buf NULL and size 0)?
1658 * @param notify function to call when we are ready to
1659 * send such a message
1660 * @param notify_cls closure for @a notify
1661 * @return NULL if someone else is already waiting to be notified
1662 * non-NULL if the notify callback was queued (can be used to cancel
1663 * using #GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1665 struct GNUNET_TRANSPORT_TransmitHandle *
1666 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle *handle,
1667 const struct GNUNET_PeerIdentity *target,
1669 struct GNUNET_TIME_Relative timeout,
1670 GNUNET_TRANSPORT_TransmitReadyNotify notify,
1673 struct Neighbour *n;
1674 struct GNUNET_TRANSPORT_TransmitHandle *th;
1675 struct GNUNET_TIME_Relative delay;
1677 n = neighbour_find (handle, target);
1680 /* use GNUNET_TRANSPORT_try_connect first, only use this function
1681 * once a connection has been established */
1687 /* attempt to send two messages at the same time to the same peer */
1691 GNUNET_assert (NULL == n->hn);
1692 th = GNUNET_new (struct GNUNET_TRANSPORT_TransmitHandle);
1694 th->notify = notify;
1695 th->notify_cls = notify_cls;
1696 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1697 th->notify_size = size;
1699 /* calculate when our transmission should be ready */
1700 delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker, size + n->traffic_overhead);
1701 n->traffic_overhead = 0;
1702 if (delay.rel_value_us > timeout.rel_value_us)
1703 delay.rel_value_us = 0; /* notify immediately (with failure) */
1704 LOG (GNUNET_ERROR_TYPE_DEBUG,
1705 "Bandwidth tracker allows next transmission to peer %s in %s\n",
1706 GNUNET_i2s (target),
1707 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1708 n->hn = GNUNET_CONTAINER_heap_insert (handle->ready_heap, n, delay.rel_value_us);
1709 schedule_transmission (handle);
1715 * Cancel the specified transmission-ready notification.
1717 * @param th handle returned from #GNUNET_TRANSPORT_notify_transmit_ready()
1720 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct GNUNET_TRANSPORT_TransmitHandle *th)
1722 struct Neighbour *n;
1724 GNUNET_assert (NULL == th->next);
1725 GNUNET_assert (NULL == th->prev);
1727 GNUNET_assert (th == n->th);
1731 GNUNET_CONTAINER_heap_remove_node (n->hn);
1736 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != th->timeout_task);
1737 GNUNET_SCHEDULER_cancel (th->timeout_task);
1738 th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1744 /* end of transport_api.c */