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 @e notify_size bytes are available
71 GNUNET_TRANSPORT_TransmitReadyNotify notify;
74 * Closure for @e 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 (connected) 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 @e next_ready
124 * value). NULL if there is no pending transmission request for
125 * this neighbour or if we're waiting for @e is_ready to become
126 * true AFTER the @e out_tracker suggested that this peer's quota
127 * has been satisfied (so once @e 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.
185 * Entry in linked list for a try-connect request.
187 struct GNUNET_TRANSPORT_TryConnectHandle
192 struct GNUNET_TRANSPORT_TryConnectHandle *prev;
197 struct GNUNET_TRANSPORT_TryConnectHandle *next;
200 * Peer we should try to connect to.
202 struct GNUNET_PeerIdentity pid;
205 * Transport service handle this request is part of.
207 struct GNUNET_TRANSPORT_Handle *th;
210 * Message transmission request to communicate to service.
212 struct GNUNET_TRANSPORT_TransmitHandle *tth;
215 * Function to call upon completion (of request transmission).
217 GNUNET_TRANSPORT_TryConnectCallback cb;
228 * Entry in linked list for all try-disconnect requests
230 struct GNUNET_TRANSPORT_TryDisconnectHandle
235 struct GNUNET_TRANSPORT_TryDisconnectHandle *prev;
240 struct GNUNET_TRANSPORT_TryDisconnectHandle *next;
243 * Peer we should try to connect to.
245 struct GNUNET_PeerIdentity pid;
248 * Transport service handle this request is part of.
250 struct GNUNET_TRANSPORT_Handle *th;
253 * Message transmission request to communicate to service.
255 struct GNUNET_TRANSPORT_TransmitHandle *tth;
258 * Function to call upon completion (of request transmission).
260 GNUNET_TRANSPORT_TryDisconnectCallback cb;
271 * Entry in linked list for all offer-HELLO requests.
273 struct GNUNET_TRANSPORT_OfferHelloHandle
278 struct GNUNET_TRANSPORT_OfferHelloHandle *prev;
283 struct GNUNET_TRANSPORT_OfferHelloHandle *next;
286 * Transport service handle we use for transmission.
288 struct GNUNET_TRANSPORT_Handle *th;
291 * Transmission handle for this request.
293 struct GNUNET_TRANSPORT_TransmitHandle *tth;
296 * Function to call once we are done.
298 GNUNET_SCHEDULER_Task cont;
301 * Closure for @e cont
306 * The HELLO message to be transmitted.
308 struct GNUNET_MessageHeader *msg;
313 * Handle for the transport service (includes all of the
314 * state for the transport service).
316 struct GNUNET_TRANSPORT_Handle
320 * Closure for the callbacks.
325 * Function to call for received data.
327 GNUNET_TRANSPORT_ReceiveCallback rec;
330 * function to call on connect events
332 GNUNET_TRANSPORT_NotifyConnect nc_cb;
335 * function to call on disconnect events
337 GNUNET_TRANSPORT_NotifyDisconnect nd_cb;
340 * function to call on excess bandwidth events
342 GNUNET_TRANSPORT_NotifyExcessBandwidth neb_cb;
345 * Head of DLL of control messages.
347 struct GNUNET_TRANSPORT_TransmitHandle *control_head;
350 * Tail of DLL of control messages.
352 struct GNUNET_TRANSPORT_TransmitHandle *control_tail;
355 * The current HELLO message for this peer. Updated
356 * whenever transports change their addresses.
358 struct GNUNET_HELLO_Message *my_hello;
361 * My client connection to the transport service.
363 struct GNUNET_CLIENT_Connection *client;
366 * Handle to our registration with the client for notification.
368 struct GNUNET_CLIENT_TransmitHandle *cth;
371 * Linked list of pending requests for our HELLO.
373 struct GNUNET_TRANSPORT_GetHelloHandle *hwl_head;
376 * Linked list of pending requests for our HELLO.
378 struct GNUNET_TRANSPORT_GetHelloHandle *hwl_tail;
381 * Linked list of pending try connect requests head
383 struct GNUNET_TRANSPORT_TryConnectHandle *tc_head;
386 * Linked list of pending try connect requests tail
388 struct GNUNET_TRANSPORT_TryConnectHandle *tc_tail;
391 * Linked list of pending try disconnect requests head
393 struct GNUNET_TRANSPORT_TryDisconnectHandle *td_head;
396 * Linked list of pending try connect requests tail
398 struct GNUNET_TRANSPORT_TryDisconnectHandle *td_tail;
401 * Linked list of pending offer HELLO requests head
403 struct GNUNET_TRANSPORT_OfferHelloHandle *oh_head;
406 * Linked list of pending offer HELLO requests tail
408 struct GNUNET_TRANSPORT_OfferHelloHandle *oh_tail;
413 const struct GNUNET_CONFIGURATION_Handle *cfg;
416 * Hash map of the current connected neighbours of this peer.
417 * Maps peer identities to 'struct Neighbour' entries.
419 struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
422 * Heap sorting peers with pending messages by the timestamps that
423 * specify when we could next send a message to the respective peer.
424 * Excludes control messages (which can always go out immediately).
425 * Maps time stamps to 'struct Neighbour' entries.
427 struct GNUNET_CONTAINER_Heap *ready_heap;
430 * Peer identity as assumed by this process, or all zeros.
432 struct GNUNET_PeerIdentity self;
435 * ID of the task trying to reconnect to the service.
437 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
440 * ID of the task trying to trigger transmission for a peer while
441 * maintaining bandwidth quotas. In use if there are no control
442 * messages and the smallest entry in the 'ready_heap' has a time
443 * stamp in the future.
445 GNUNET_SCHEDULER_TaskIdentifier quota_task;
448 * Delay until we try to reconnect.
450 struct GNUNET_TIME_Relative reconnect_delay;
453 * Should we check that @e self matches what the service thinks?
454 * (if #GNUNET_NO, then @e self is all zeros!).
459 * Reconnect in progress
466 * Schedule the task to send one message, either from the control
467 * list or the peer message queues to the service.
469 * @param h transport service to schedule a transmission for
472 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h);
476 * Function that will schedule the job that will try
477 * to connect us again to the client.
479 * @param h transport service to reconnect
482 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h);
486 * Get the neighbour list entry for the given peer
488 * @param h our context
489 * @param peer peer to look up
490 * @return NULL if no such peer entry exists
492 static struct Neighbour *
493 neighbour_find (struct GNUNET_TRANSPORT_Handle *h,
494 const struct GNUNET_PeerIdentity *peer)
496 return GNUNET_CONTAINER_multipeermap_get (h->neighbours, peer);
501 * The outbound quota has changed in a way that may require
502 * us to reset the timeout. Update the timeout.
504 * @param cls the `struct Neighbour` for which the timeout changed
507 outbound_bw_tracker_update (void *cls)
509 struct Neighbour *n = cls;
510 struct GNUNET_TIME_Relative delay;
514 delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
515 n->th->notify_size + n->traffic_overhead);
516 LOG (GNUNET_ERROR_TYPE_DEBUG,
517 "New outbound delay %llu us\n",
518 GNUNET_STRINGS_relative_time_to_string (delay,
520 GNUNET_CONTAINER_heap_update_cost (n->h->ready_heap,
521 n->hn, delay.rel_value_us);
522 schedule_transmission (n->h);
527 * Function called by the bandwidth tracker if we have excess
530 * @param cls the `struct Neighbour` that has excess bandwidth
533 notify_excess_cb (void *cls)
535 struct Neighbour *n = cls;
536 struct GNUNET_TRANSPORT_Handle *h = n->h;
538 if (NULL != h->neb_cb)
545 * Add neighbour to our list
547 * @return NULL if this API is currently disconnecting from the service
549 static struct Neighbour *
550 neighbour_add (struct GNUNET_TRANSPORT_Handle *h,
551 const struct GNUNET_PeerIdentity *pid)
555 LOG (GNUNET_ERROR_TYPE_DEBUG,
556 "Creating entry for neighbour `%4s'.\n",
558 n = GNUNET_new (struct Neighbour);
561 n->is_ready = GNUNET_YES;
562 n->traffic_overhead = 0;
563 GNUNET_BANDWIDTH_tracker_init2 (&n->out_tracker,
564 &outbound_bw_tracker_update, n,
565 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
566 MAX_BANDWIDTH_CARRY_S,
569 GNUNET_assert (GNUNET_OK ==
570 GNUNET_CONTAINER_multipeermap_put (h->neighbours,
572 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
578 * Iterator over hash map entries, for deleting state of a neighbour.
580 * @param cls the `struct GNUNET_TRANSPORT_Handle *`
581 * @param key peer identity
582 * @param value value in the hash map, the neighbour entry to delete
583 * @return #GNUNET_YES if we should continue to
588 neighbour_delete (void *cls,
589 const struct GNUNET_PeerIdentity *key, void *value)
591 struct GNUNET_TRANSPORT_Handle *handle = cls;
592 struct Neighbour *n = value;
594 if (NULL != handle->nd_cb)
595 handle->nd_cb (handle->cls, &n->id);
596 GNUNET_assert (NULL == n->th);
597 GNUNET_assert (NULL == n->hn);
598 GNUNET_assert (GNUNET_YES ==
599 GNUNET_CONTAINER_multipeermap_remove (handle->neighbours, key,
601 GNUNET_BANDWIDTH_tracker_notification_stop (&n->out_tracker);
608 * Function we use for handling incoming messages.
610 * @param cls closure, a `struct GNUNET_TRANSPORT_Handle *`
611 * @param msg message received, NULL on timeout or fatal error
614 demultiplexer (void *cls,
615 const struct GNUNET_MessageHeader *msg)
617 struct GNUNET_TRANSPORT_Handle *h = cls;
618 const struct DisconnectInfoMessage *dim;
619 const struct ConnectInfoMessage *cim;
620 const struct InboundMessage *im;
621 const struct GNUNET_MessageHeader *imm;
622 const struct SendOkMessage *okm;
623 const struct QuotaSetMessage *qm;
624 struct GNUNET_TRANSPORT_GetHelloHandle *hwl;
625 struct GNUNET_TRANSPORT_GetHelloHandle *next_hwl;
627 struct GNUNET_PeerIdentity me;
630 uint32_t bytes_physical;
632 GNUNET_assert (NULL != h->client);
633 if (GNUNET_YES == h->reconnecting)
639 LOG (GNUNET_ERROR_TYPE_DEBUG,
640 "Error receiving from transport service, disconnecting temporarily.\n");
641 h->reconnecting = GNUNET_YES;
642 disconnect_and_schedule_reconnect (h);
645 GNUNET_CLIENT_receive (h->client, &demultiplexer, h,
646 GNUNET_TIME_UNIT_FOREVER_REL);
647 size = ntohs (msg->size);
648 switch (ntohs (msg->type))
650 case GNUNET_MESSAGE_TYPE_HELLO:
652 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg, &me))
657 LOG (GNUNET_ERROR_TYPE_DEBUG,
658 "Receiving (my own) `%s' message, I am `%4s'.\n", "HELLO",
660 GNUNET_free_non_null (h->my_hello);
662 if (size < sizeof (struct GNUNET_MessageHeader))
667 h->my_hello = GNUNET_malloc (size);
668 memcpy (h->my_hello, msg, size);
672 next_hwl = hwl->next;
673 hwl->rec (hwl->rec_cls,
674 (const struct GNUNET_MessageHeader *) h->my_hello);
678 case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
679 if (size < sizeof (struct ConnectInfoMessage))
684 cim = (const struct ConnectInfoMessage *) msg;
686 sizeof (struct ConnectInfoMessage))
691 LOG (GNUNET_ERROR_TYPE_DEBUG,
692 "Receiving `%s' message for `%4s'.\n",
693 "CONNECT", GNUNET_i2s (&cim->id));
694 n = neighbour_find (h, &cim->id);
700 n = neighbour_add (h, &cim->id);
701 LOG (GNUNET_ERROR_TYPE_DEBUG,
702 "Receiving `%s' message for `%4s' with quota %u\n",
704 GNUNET_i2s (&cim->id),
705 ntohl (cim->quota_out.value__));
706 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker, cim->quota_out);
707 if (h->nc_cb != NULL)
708 h->nc_cb (h->cls, &n->id);
710 case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
711 if (size != sizeof (struct DisconnectInfoMessage))
716 dim = (const struct DisconnectInfoMessage *) msg;
717 GNUNET_break (ntohl (dim->reserved) == 0);
718 LOG (GNUNET_ERROR_TYPE_DEBUG,
719 "Receiving `%s' message for `%4s'.\n",
720 "DISCONNECT", GNUNET_i2s (&dim->peer));
721 n = neighbour_find (h, &dim->peer);
727 neighbour_delete (h, &dim->peer, n);
729 case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
730 if (size != sizeof (struct SendOkMessage))
735 okm = (const struct SendOkMessage *) msg;
736 bytes_msg = ntohl (okm->bytes_msg);
737 bytes_physical = ntohl (okm->bytes_physical);
738 LOG (GNUNET_ERROR_TYPE_DEBUG,
739 "Receiving SEND_OK message, transmission %s.\n",
740 ntohl (okm->success) == GNUNET_OK ? "succeeded" : "failed");
742 n = neighbour_find (h, &okm->peer);
746 if (bytes_physical >= bytes_msg)
748 LOG (GNUNET_ERROR_TYPE_DEBUG,
749 "Overhead for %u byte message: %u\n",
750 bytes_msg, bytes_physical - bytes_msg);
751 n->traffic_overhead += bytes_physical - bytes_msg;
753 GNUNET_break (GNUNET_NO == n->is_ready);
754 n->is_ready = GNUNET_YES;
755 if ((NULL != n->th) && (NULL == n->hn))
757 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->th->timeout_task);
758 GNUNET_SCHEDULER_cancel (n->th->timeout_task);
759 n->th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
760 /* we've been waiting for this (congestion, not quota,
761 * caused delayed transmission) */
762 n->hn = GNUNET_CONTAINER_heap_insert (h->ready_heap, n, 0);
763 schedule_transmission (h);
766 case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
767 LOG (GNUNET_ERROR_TYPE_DEBUG,
768 "Receiving `%s' message.\n",
771 sizeof (struct InboundMessage) + sizeof (struct GNUNET_MessageHeader))
776 im = (const struct InboundMessage *) msg;
777 imm = (const struct GNUNET_MessageHeader *) &im[1];
778 if (ntohs (imm->size) + sizeof (struct InboundMessage) != size)
783 LOG (GNUNET_ERROR_TYPE_DEBUG,
784 "Received message of type %u from `%4s'.\n",
785 ntohs (imm->type), GNUNET_i2s (&im->peer));
786 n = neighbour_find (h, &im->peer);
793 h->rec (h->cls, &im->peer, imm);
795 case GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA:
796 LOG (GNUNET_ERROR_TYPE_DEBUG,
797 "Receiving `%s' message.\n", "SET_QUOTA");
798 if (size != sizeof (struct QuotaSetMessage))
803 qm = (const struct QuotaSetMessage *) msg;
804 n = neighbour_find (h, &qm->peer);
807 LOG (GNUNET_ERROR_TYPE_DEBUG,
808 "Receiving `%s' message for `%4s' with quota %u\n",
810 GNUNET_i2s (&qm->peer),
811 ntohl (qm->quota.value__));
812 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker, qm->quota);
815 LOG (GNUNET_ERROR_TYPE_ERROR,
816 _("Received unexpected message of type %u in %s:%u\n"),
817 ntohs (msg->type), __FILE__, __LINE__);
825 * A transmission request could not be satisfied because of
826 * network congestion. Notify the initiator and clean up.
828 * @param cls the `struct GNUNET_TRANSPORT_TransmitHandle`
829 * @param tc scheduler context
832 timeout_request_due_to_congestion (void *cls,
833 const struct GNUNET_SCHEDULER_TaskContext *tc)
835 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
836 struct Neighbour *n = th->neighbour;
838 n->th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
839 GNUNET_assert (th == n->th);
840 GNUNET_assert (NULL == n->hn);
842 th->notify (th->notify_cls, 0, NULL);
848 * Transmit message(s) to service.
850 * @param cls handle to transport
851 * @param size number of bytes available in @a buf
852 * @param buf where to copy the message
853 * @return number of bytes copied to @a buf
856 transport_notify_ready (void *cls, size_t size, void *buf)
858 struct GNUNET_TRANSPORT_Handle *h = cls;
859 struct GNUNET_TRANSPORT_TransmitHandle *th;
862 struct OutboundMessage obm;
867 GNUNET_assert (NULL != h->client);
871 /* transmission failed */
872 disconnect_and_schedule_reconnect (h);
878 /* first send control messages */
879 while ((NULL != (th = h->control_head)) && (th->notify_size <= size))
881 GNUNET_CONTAINER_DLL_remove (h->control_head, h->control_tail, th);
882 nret = th->notify (th->notify_cls, size, &cbuf[ret]);
883 LOG (GNUNET_ERROR_TYPE_DEBUG,
884 "Added %u bytes of control message at %u\n",
891 /* then, if possible and no control messages pending, send data messages */
892 while ((NULL == h->control_head) &&
893 (NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap))))
895 if (GNUNET_YES != n->is_ready)
897 /* peer not ready, wait for notification! */
898 GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
900 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == n->th->timeout_task);
901 n->th->timeout_task =
902 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
904 &timeout_request_due_to_congestion,
909 if (th->notify_size + sizeof (struct OutboundMessage) > size)
910 break; /* does not fit */
911 if (GNUNET_BANDWIDTH_tracker_get_delay
912 (&n->out_tracker, th->notify_size).rel_value_us > 0)
913 break; /* too early */
914 GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
917 n->is_ready = GNUNET_NO;
918 GNUNET_assert (size >= sizeof (struct OutboundMessage));
920 th->notify (th->notify_cls, size - sizeof (struct OutboundMessage),
921 &cbuf[ret + sizeof (struct OutboundMessage)]);
922 GNUNET_assert (mret <= size - sizeof (struct OutboundMessage));
925 GNUNET_assert (mret + sizeof (struct OutboundMessage) <
926 GNUNET_SERVER_MAX_MESSAGE_SIZE);
927 obm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
928 obm.header.size = htons (mret + sizeof (struct OutboundMessage));
929 obm.reserved = htonl (0);
931 GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining
934 memcpy (&cbuf[ret], &obm, sizeof (struct OutboundMessage));
935 ret += (mret + sizeof (struct OutboundMessage));
936 size -= (mret + sizeof (struct OutboundMessage));
937 GNUNET_BANDWIDTH_tracker_consume (&n->out_tracker, mret);
941 /* if there are more pending messages, try to schedule those */
942 schedule_transmission (h);
943 LOG (GNUNET_ERROR_TYPE_DEBUG,
944 "Transmitting %u bytes to transport service\n",
951 * Schedule the task to send one message, either from the control
952 * list or the peer message queues to the service.
954 * @param cls transport service to schedule a transmission for
955 * @param tc scheduler context
958 schedule_transmission_task (void *cls,
959 const struct GNUNET_SCHEDULER_TaskContext *tc)
961 struct GNUNET_TRANSPORT_Handle *h = cls;
963 struct GNUNET_TRANSPORT_TransmitHandle *th;
966 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
967 GNUNET_assert (NULL != h->client);
968 /* destroy all requests that have timed out */
969 while ((NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap))) &&
970 (0 == GNUNET_TIME_absolute_get_remaining (n->th->timeout).rel_value_us))
972 /* notify client that the request could not be satisfied within
973 * the given time constraints */
976 GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
978 LOG (GNUNET_ERROR_TYPE_DEBUG,
979 "Signalling timeout for transmission to peer %s due to congestion\n",
980 GNUNET_i2s (&n->id));
981 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
986 if (NULL != h->control_head)
988 size = h->control_head->notify_size;
992 n = GNUNET_CONTAINER_heap_peek (h->ready_heap);
994 return; /* no pending messages */
995 size = n->th->notify_size + sizeof (struct OutboundMessage);
997 LOG (GNUNET_ERROR_TYPE_DEBUG,
998 "Calling notify_transmit_ready\n");
1000 GNUNET_CLIENT_notify_transmit_ready (h->client, size,
1001 GNUNET_TIME_UNIT_FOREVER_REL,
1002 GNUNET_NO, &transport_notify_ready,
1004 GNUNET_assert (NULL != h->cth);
1009 * Schedule the task to send one message, either from the control
1010 * list or the peer message queues to the service.
1012 * @param h transport service to schedule a transmission for
1015 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
1017 struct GNUNET_TIME_Relative delay;
1018 struct Neighbour *n;
1020 GNUNET_assert (NULL != h->client);
1021 if (h->quota_task != GNUNET_SCHEDULER_NO_TASK)
1023 GNUNET_SCHEDULER_cancel (h->quota_task);
1024 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
1026 if (NULL != h->control_head)
1027 delay = GNUNET_TIME_UNIT_ZERO;
1028 else if (NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap)))
1031 GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
1032 n->th->notify_size + n->traffic_overhead);
1033 n->traffic_overhead = 0;
1036 return; /* no work to be done */
1037 LOG (GNUNET_ERROR_TYPE_DEBUG,
1038 "Scheduling next transmission to service in %s\n",
1039 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1041 GNUNET_SCHEDULER_add_delayed (delay, &schedule_transmission_task, h);
1046 * Queue control request for transmission to the transport
1049 * @param h handle to the transport service
1050 * @param size number of bytes to be transmitted
1051 * @param notify function to call to get the content
1052 * @param notify_cls closure for @a notify
1053 * @return a `struct GNUNET_TRANSPORT_TransmitHandle`
1055 static struct GNUNET_TRANSPORT_TransmitHandle *
1056 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h, size_t size,
1057 GNUNET_TRANSPORT_TransmitReadyNotify notify,
1060 struct GNUNET_TRANSPORT_TransmitHandle *th;
1062 LOG (GNUNET_ERROR_TYPE_DEBUG,
1063 "Control transmit of %u bytes requested\n",
1065 th = GNUNET_new (struct GNUNET_TRANSPORT_TransmitHandle);
1066 th->notify = notify;
1067 th->notify_cls = notify_cls;
1068 th->notify_size = size;
1069 GNUNET_CONTAINER_DLL_insert_tail (h->control_head, h->control_tail, th);
1070 schedule_transmission (h);
1076 * Transmit START message to service.
1079 * @param size number of bytes available in @a buf
1080 * @param buf where to copy the message
1081 * @return number of bytes copied to @a buf
1084 send_start (void *cls, size_t size, void *buf)
1086 struct GNUNET_TRANSPORT_Handle *h = cls;
1087 struct StartMessage s;
1092 /* Can only be shutdown, just give up */
1093 LOG (GNUNET_ERROR_TYPE_DEBUG,
1094 "Shutdown while trying to transmit `%s' request.\n",
1098 LOG (GNUNET_ERROR_TYPE_DEBUG,
1099 "Transmitting `%s' request.\n",
1101 GNUNET_assert (size >= sizeof (struct StartMessage));
1102 s.header.size = htons (sizeof (struct StartMessage));
1103 s.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
1109 s.options = htonl (options);
1111 memcpy (buf, &s, sizeof (struct StartMessage));
1112 GNUNET_CLIENT_receive (h->client, &demultiplexer, h,
1113 GNUNET_TIME_UNIT_FOREVER_REL);
1114 return sizeof (struct StartMessage);
1119 * Try again to connect to transport service.
1121 * @param cls the handle to the transport service
1122 * @param tc scheduler context
1125 reconnect (void *cls,
1126 const struct GNUNET_SCHEDULER_TaskContext *tc)
1128 struct GNUNET_TRANSPORT_Handle *h = cls;
1130 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1131 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1133 /* shutdown, just give up */
1136 LOG (GNUNET_ERROR_TYPE_DEBUG,
1137 "Connecting to transport service.\n");
1138 GNUNET_assert (NULL == h->client);
1139 GNUNET_assert (NULL == h->control_head);
1140 GNUNET_assert (NULL == h->control_tail);
1141 h->reconnecting = GNUNET_NO;
1142 h->client = GNUNET_CLIENT_connect ("transport", h->cfg);
1144 GNUNET_assert (NULL != h->client);
1145 schedule_control_transmit (h, sizeof (struct StartMessage),
1151 * Function that will schedule the job that will try
1152 * to connect us again to the client.
1154 * @param h transport service to reconnect
1157 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
1159 struct GNUNET_TRANSPORT_TransmitHandle *th;
1161 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
1164 GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
1167 if (NULL != h->client)
1169 GNUNET_CLIENT_disconnect (h->client);
1171 /* LOG (GNUNET_ERROR_TYPE_ERROR,
1172 "Client disconnect done \n");*/
1174 /* Forget about all neighbours that we used to be connected to */
1175 GNUNET_CONTAINER_multipeermap_iterate (h->neighbours,
1176 &neighbour_delete, h);
1177 if (h->quota_task != GNUNET_SCHEDULER_NO_TASK)
1179 GNUNET_SCHEDULER_cancel (h->quota_task);
1180 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
1182 while ((NULL != (th = h->control_head)))
1184 GNUNET_CONTAINER_DLL_remove (h->control_head, h->control_tail, th);
1185 th->notify (th->notify_cls, 0, NULL);
1188 LOG (GNUNET_ERROR_TYPE_DEBUG,
1189 "Scheduling task to reconnect to transport service in %s.\n",
1190 GNUNET_STRINGS_relative_time_to_string(h->reconnect_delay, GNUNET_YES));
1192 GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
1193 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
1198 * Cancel control request for transmission to the transport service.
1200 * @param th handle to the transport service
1201 * @param tth transmit handle to cancel
1204 cancel_control_transmit (struct GNUNET_TRANSPORT_Handle *th,
1205 struct GNUNET_TRANSPORT_TransmitHandle *tth)
1207 LOG (GNUNET_ERROR_TYPE_DEBUG,
1208 "Canceling transmit of contral transmission requested\n");
1209 GNUNET_CONTAINER_DLL_remove (th->control_head, th->control_tail, tth);
1215 * Send #GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT message to the
1218 * @param cls the `struct GNUNET_TRANSPORT_TryConnectHandle`
1219 * @param size number of bytes available in @a buf
1220 * @param buf where to copy the message
1221 * @return number of bytes copied to @a buf
1224 send_try_connect (void *cls,
1228 struct GNUNET_TRANSPORT_TryConnectHandle *tch = cls;
1229 struct TransportRequestConnectMessage msg;
1234 LOG (GNUNET_ERROR_TYPE_DEBUG,
1235 "Discarding `%s' request to `%4s' due to error in transport service connection.\n",
1237 GNUNET_i2s (&tch->pid));
1238 if (NULL != tch->cb)
1239 tch->cb (tch->cb_cls,
1241 GNUNET_TRANSPORT_try_connect_cancel (tch);
1244 LOG (GNUNET_ERROR_TYPE_DEBUG,
1245 "Transmitting `%s' request with respect to `%4s'.\n",
1247 GNUNET_i2s (&tch->pid));
1248 GNUNET_assert (size >= sizeof (struct TransportRequestConnectMessage));
1249 msg.header.size = htons (sizeof (struct TransportRequestConnectMessage));
1250 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT);
1251 msg.reserved = htonl (0);
1252 msg.peer = tch->pid;
1253 memcpy (buf, &msg, sizeof (msg));
1254 if (NULL != tch->cb)
1255 tch->cb (tch->cb_cls, GNUNET_OK);
1256 GNUNET_TRANSPORT_try_connect_cancel (tch);
1257 return sizeof (struct TransportRequestConnectMessage);
1262 * Ask the transport service to establish a connection to
1265 * @param handle connection to transport service
1266 * @param target who we should try to connect to
1267 * @param cb callback to be called when request was transmitted to transport
1269 * @param cb_cls closure for the callback
1270 * @return a `struct GNUNET_TRANSPORT_TryConnectHandle` handle or
1271 * NULL on failure (cb will not be called)
1273 struct GNUNET_TRANSPORT_TryConnectHandle *
1274 GNUNET_TRANSPORT_try_connect (struct GNUNET_TRANSPORT_Handle *handle,
1275 const struct GNUNET_PeerIdentity *target,
1276 GNUNET_TRANSPORT_TryConnectCallback cb,
1279 struct GNUNET_TRANSPORT_TryConnectHandle *tch;
1281 if (NULL == handle->client)
1283 tch = GNUNET_new (struct GNUNET_TRANSPORT_TryConnectHandle);
1285 tch->pid = *(target);
1287 tch->cb_cls = cb_cls;
1288 tch->tth = schedule_control_transmit (handle,
1289 sizeof (struct TransportRequestConnectMessage),
1290 &send_try_connect, tch);
1291 GNUNET_CONTAINER_DLL_insert (handle->tc_head,
1299 * Cancel the request to transport to try a connect
1300 * Callback will not be called
1302 * @param tch the handle to cancel
1305 GNUNET_TRANSPORT_try_connect_cancel (struct GNUNET_TRANSPORT_TryConnectHandle *tch)
1307 struct GNUNET_TRANSPORT_Handle *th;
1310 if (NULL != tch->tth)
1311 cancel_control_transmit (th, tch->tth);
1312 GNUNET_CONTAINER_DLL_remove (th->tc_head,
1320 * Send #GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_DISCONNECT message to the
1323 * @param cls the `struct GNUNET_TRANSPORT_TryDisconnectHandle`
1324 * @param size number of bytes available in @a buf
1325 * @param buf where to copy the message
1326 * @return number of bytes copied to @a buf
1329 send_try_disconnect (void *cls,
1333 struct GNUNET_TRANSPORT_TryDisconnectHandle *tdh = cls;
1334 struct TransportRequestConnectMessage msg;
1339 LOG (GNUNET_ERROR_TYPE_DEBUG,
1340 "Discarding `%s' request to `%4s' due to error in transport service connection.\n",
1341 "REQUEST_DISCONNECT",
1342 GNUNET_i2s (&tdh->pid));
1343 if (NULL != tdh->cb)
1344 tdh->cb (tdh->cb_cls,
1346 GNUNET_TRANSPORT_try_disconnect_cancel (tdh);
1349 LOG (GNUNET_ERROR_TYPE_DEBUG,
1350 "Transmitting `%s' request with respect to `%4s'.\n",
1351 "REQUEST_DISCONNECT",
1352 GNUNET_i2s (&tdh->pid));
1353 GNUNET_assert (size >= sizeof (struct TransportRequestDisconnectMessage));
1354 msg.header.size = htons (sizeof (struct TransportRequestDisconnectMessage));
1355 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_DISCONNECT);
1356 msg.reserved = htonl (0);
1357 msg.peer = tdh->pid;
1358 memcpy (buf, &msg, sizeof (msg));
1359 if (NULL != tdh->cb)
1360 tdh->cb (tdh->cb_cls, GNUNET_OK);
1361 GNUNET_TRANSPORT_try_disconnect_cancel (tdh);
1362 return sizeof (struct TransportRequestDisconnectMessage);
1367 * Ask the transport service to shutdown a connection to
1370 * @param handle connection to transport service
1371 * @param target who we should try to connect to
1372 * @param cb callback to be called when request was transmitted to transport
1374 * @param cb_cls closure for the callback @a cb
1375 * @return a `struct GNUNET_TRANSPORT_TryDisconnectHandle` handle or
1376 * NULL on failure (cb will not be called)
1378 struct GNUNET_TRANSPORT_TryDisconnectHandle *
1379 GNUNET_TRANSPORT_try_disconnect (struct GNUNET_TRANSPORT_Handle *handle,
1380 const struct GNUNET_PeerIdentity *target,
1381 GNUNET_TRANSPORT_TryDisconnectCallback cb,
1384 struct GNUNET_TRANSPORT_TryDisconnectHandle *tdh;
1386 if (NULL == handle->client)
1388 tdh = GNUNET_new (struct GNUNET_TRANSPORT_TryDisconnectHandle);
1390 tdh->pid = *(target);
1392 tdh->cb_cls = cb_cls;
1393 tdh->tth = schedule_control_transmit (handle,
1394 sizeof (struct TransportRequestDisconnectMessage),
1395 &send_try_disconnect, tdh);
1396 GNUNET_CONTAINER_DLL_insert (handle->td_head,
1404 * Cancel the request to transport to try a disconnect
1405 * Callback will not be called
1407 * @param tdh the handle to cancel
1410 GNUNET_TRANSPORT_try_disconnect_cancel (struct GNUNET_TRANSPORT_TryDisconnectHandle *tdh)
1412 struct GNUNET_TRANSPORT_Handle *th;
1415 if (NULL != tdh->tth)
1416 cancel_control_transmit (th, tdh->tth);
1417 GNUNET_CONTAINER_DLL_remove (th->td_head,
1425 * Send HELLO message to the service.
1427 * @param cls the HELLO message to send
1428 * @param size number of bytes available in @a buf
1429 * @param buf where to copy the message
1430 * @return number of bytes copied to @a buf
1433 send_hello (void *cls, size_t size, void *buf)
1435 struct GNUNET_TRANSPORT_OfferHelloHandle *ohh = cls;
1436 struct GNUNET_MessageHeader *msg = ohh->msg;
1438 struct GNUNET_SCHEDULER_TaskContext tc;
1440 tc.read_ready = NULL;
1441 tc.write_ready = NULL;
1442 tc.reason = GNUNET_SCHEDULER_REASON_TIMEOUT;
1445 LOG (GNUNET_ERROR_TYPE_DEBUG,
1446 "Timeout while trying to transmit `%s' request.\n",
1448 if (NULL != ohh->cont)
1449 ohh->cont (ohh->cls, &tc);
1451 GNUNET_CONTAINER_DLL_remove (ohh->th->oh_head, ohh->th->oh_tail, ohh);
1455 LOG (GNUNET_ERROR_TYPE_DEBUG,
1456 "Transmitting `%s' request.\n",
1458 ssize = ntohs (msg->size);
1459 GNUNET_assert (size >= ssize);
1460 memcpy (buf, msg, ssize);
1462 tc.reason = GNUNET_SCHEDULER_REASON_READ_READY;
1463 if (NULL != ohh->cont)
1464 ohh->cont (ohh->cls, &tc);
1465 GNUNET_CONTAINER_DLL_remove (ohh->th->oh_head, ohh->th->oh_tail, ohh);
1472 * Send traffic metric message to the service.
1474 * @param cls the message to send
1475 * @param size number of bytes available in @a buf
1476 * @param buf where to copy the message
1477 * @return number of bytes copied to @a buf
1480 send_metric (void *cls, size_t size, void *buf)
1482 struct TrafficMetricMessage *msg = cls;
1487 LOG (GNUNET_ERROR_TYPE_DEBUG,
1488 "Timeout while trying to transmit `%s' request.\n",
1493 LOG (GNUNET_ERROR_TYPE_DEBUG,
1494 "Transmitting `%s' request.\n",
1496 ssize = ntohs (msg->header.size);
1497 GNUNET_assert (size >= ssize);
1498 memcpy (buf, msg, ssize);
1505 * Set transport metrics for a peer and a direction
1507 * @param handle transport handle
1508 * @param peer the peer to set the metric for
1509 * @param inbound set inbound direction (#GNUNET_YES or #GNUNET_NO)
1510 * @param outbound set outbound direction (#GNUNET_YES or #GNUNET_NO)
1511 * @param ats the metric as ATS information
1512 * @param ats_count the number of metrics
1514 * Supported ATS values:
1515 * #GNUNET_ATS_QUALITY_NET_DELAY (value in ms)
1516 * #GNUNET_ATS_QUALITY_NET_DISTANCE (value in count(hops))
1519 * To enforce a delay of 10 ms for peer p1 in sending direction use:
1521 * struct GNUNET_ATS_Information ats;
1522 * ats.type = ntohl (GNUNET_ATS_QUALITY_NET_DELAY);
1523 * ats.value = ntohl (10);
1524 * GNUNET_TRANSPORT_set_traffic_metric (th, p1, TM_SEND, &ats, 1);
1527 * Delay restrictions in receiving direction will be enforced with
1531 GNUNET_TRANSPORT_set_traffic_metric (struct GNUNET_TRANSPORT_Handle *handle,
1532 const struct GNUNET_PeerIdentity *peer,
1535 const struct GNUNET_ATS_Information *ats,
1538 struct TrafficMetricMessage *msg;
1540 GNUNET_assert ((outbound == GNUNET_YES) || (outbound == GNUNET_NO));
1541 GNUNET_assert ((inbound == GNUNET_YES) || (inbound == GNUNET_NO));
1542 if ((GNUNET_NO == inbound) && (GNUNET_NO == outbound))
1547 size_t len = sizeof (struct TrafficMetricMessage) +
1548 ats_count * sizeof (struct GNUNET_ATS_Information);
1550 msg = GNUNET_malloc (len);
1551 msg->header.size = htons (len);
1552 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TRAFFIC_METRIC);
1553 msg->direction = htons (0 + outbound + 2 * inbound);
1554 msg->ats_count = htons (ats_count);
1555 msg->peer = (*peer);
1556 memcpy (&msg[1], ats, ats_count * sizeof (struct GNUNET_ATS_Information));
1557 schedule_control_transmit (handle, len,
1563 * Offer the transport service the HELLO of another peer. Note that
1564 * the transport service may just ignore this message if the HELLO is
1565 * malformed or useless due to our local configuration.
1567 * @param handle connection to transport service
1568 * @param hello the hello message
1569 * @param cont continuation to call when HELLO has been sent,
1570 * tc reason #GNUNET_SCHEDULER_REASON_TIMEOUT for fail
1571 * tc reasong #GNUNET_SCHEDULER_REASON_READ_READY for success
1572 * @param cls closure for continuation
1573 * @return a `struct GNUNET_TRANSPORT_OfferHelloHandle` handle or NULL on failure,
1574 * in case of failure cont will not be called
1577 struct GNUNET_TRANSPORT_OfferHelloHandle *
1578 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
1579 const struct GNUNET_MessageHeader *hello,
1580 GNUNET_SCHEDULER_Task cont, void *cls)
1582 struct GNUNET_TRANSPORT_OfferHelloHandle *ohh;
1583 struct GNUNET_MessageHeader *msg;
1584 struct GNUNET_PeerIdentity peer;
1587 if (NULL == handle->client)
1589 GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
1590 size = ntohs (hello->size);
1591 GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
1593 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hello, &peer))
1599 msg = GNUNET_malloc (size);
1600 memcpy (msg, hello, size);
1601 LOG (GNUNET_ERROR_TYPE_DEBUG,
1602 "Offering `%s' message of `%4s' to transport for validation.\n", "HELLO",
1603 GNUNET_i2s (&peer));
1605 ohh = GNUNET_new (struct GNUNET_TRANSPORT_OfferHelloHandle);
1610 ohh->tth = schedule_control_transmit (handle, size,
1612 GNUNET_CONTAINER_DLL_insert (handle->oh_head, handle->oh_tail, ohh);
1618 * Cancel the request to transport to offer the HELLO message
1620 * @param ohh the GNUNET_TRANSPORT_OfferHelloHandle to cancel
1623 GNUNET_TRANSPORT_offer_hello_cancel (struct GNUNET_TRANSPORT_OfferHelloHandle *ohh)
1625 struct GNUNET_TRANSPORT_Handle *th = ohh->th;
1627 cancel_control_transmit (ohh->th, ohh->tth);
1628 GNUNET_CONTAINER_DLL_remove (th->oh_head, th->oh_tail, ohh);
1629 GNUNET_free (ohh->msg);
1635 * Checks if a given peer is connected to us
1637 * @param handle connection to transport service
1638 * @param peer the peer to check
1639 * @return #GNUNET_YES (connected) or #GNUNET_NO (disconnected)
1642 GNUNET_TRANSPORT_check_peer_connected (struct GNUNET_TRANSPORT_Handle *handle,
1643 const struct GNUNET_PeerIdentity *peer)
1646 GNUNET_CONTAINER_multipeermap_contains (handle->neighbours,
1654 * Task to call the HelloUpdateCallback of the GetHelloHandle
1656 * @param cls the `struct GNUNET_TRANSPORT_GetHelloHandle`
1657 * @param tc the scheduler task context
1660 call_hello_update_cb_async (void *cls,
1661 const struct GNUNET_SCHEDULER_TaskContext *tc)
1663 struct GNUNET_TRANSPORT_GetHelloHandle *ghh = cls;
1665 GNUNET_assert (NULL != ghh->handle->my_hello);
1666 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != ghh->notify_task);
1667 ghh->notify_task = GNUNET_SCHEDULER_NO_TASK;
1668 ghh->rec (ghh->rec_cls,
1669 (const struct GNUNET_MessageHeader *) ghh->handle->my_hello);
1674 * Obtain the HELLO message for this peer. The callback given in this function
1675 * is never called synchronously.
1677 * @param handle connection to transport service
1678 * @param rec function to call with the HELLO, sender will be our peer
1679 * identity; message and sender will be NULL on timeout
1680 * (handshake with transport service pending/failed).
1681 * cost estimate will be 0.
1682 * @param rec_cls closure for @a rec
1683 * @return handle to cancel the operation
1685 struct GNUNET_TRANSPORT_GetHelloHandle *
1686 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
1687 GNUNET_TRANSPORT_HelloUpdateCallback rec,
1690 struct GNUNET_TRANSPORT_GetHelloHandle *hwl;
1692 hwl = GNUNET_new (struct GNUNET_TRANSPORT_GetHelloHandle);
1694 hwl->rec_cls = rec_cls;
1695 hwl->handle = handle;
1696 GNUNET_CONTAINER_DLL_insert (handle->hwl_head, handle->hwl_tail, hwl);
1697 if (handle->my_hello != NULL)
1698 hwl->notify_task = GNUNET_SCHEDULER_add_now (&call_hello_update_cb_async,
1705 * Stop receiving updates about changes to our HELLO message.
1707 * @param ghh handle to cancel
1710 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_GetHelloHandle *ghh)
1712 struct GNUNET_TRANSPORT_Handle *handle = ghh->handle;
1714 if (GNUNET_SCHEDULER_NO_TASK != ghh->notify_task)
1715 GNUNET_SCHEDULER_cancel (ghh->notify_task);
1716 GNUNET_CONTAINER_DLL_remove (handle->hwl_head, handle->hwl_tail, ghh);
1722 * Connect to the transport service. Note that the connection may
1723 * complete (or fail) asynchronously.
1725 * @param cfg configuration to use
1726 * @param self our own identity (API should check that it matches
1727 * the identity found by transport), or NULL (no check)
1728 * @param cls closure for the callbacks
1729 * @param rec receive function to call
1730 * @param nc function to call on connect events
1731 * @param nd function to call on disconnect events
1732 * @return NULL on error
1734 struct GNUNET_TRANSPORT_Handle *
1735 GNUNET_TRANSPORT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1736 const struct GNUNET_PeerIdentity *self, void *cls,
1737 GNUNET_TRANSPORT_ReceiveCallback rec,
1738 GNUNET_TRANSPORT_NotifyConnect nc,
1739 GNUNET_TRANSPORT_NotifyDisconnect nd)
1741 return GNUNET_TRANSPORT_connect2 (cfg, self, cls,
1747 * Connect to the transport service. Note that the connection may
1748 * complete (or fail) asynchronously.
1750 * @param cfg configuration to use
1751 * @param self our own identity (API should check that it matches
1752 * the identity found by transport), or NULL (no check)
1753 * @param cls closure for the callbacks
1754 * @param rec receive function to call
1755 * @param nc function to call on connect events
1756 * @param nd function to call on disconnect events
1757 * @param neb function to call if we have excess bandwidth to a peer
1758 * @return NULL on error
1760 struct GNUNET_TRANSPORT_Handle *
1761 GNUNET_TRANSPORT_connect2 (const struct GNUNET_CONFIGURATION_Handle *cfg,
1762 const struct GNUNET_PeerIdentity *self, void *cls,
1763 GNUNET_TRANSPORT_ReceiveCallback rec,
1764 GNUNET_TRANSPORT_NotifyConnect nc,
1765 GNUNET_TRANSPORT_NotifyDisconnect nd,
1766 GNUNET_TRANSPORT_NotifyExcessBandwidth neb)
1768 struct GNUNET_TRANSPORT_Handle *ret;
1770 ret = GNUNET_new (struct GNUNET_TRANSPORT_Handle);
1774 ret->check_self = GNUNET_YES;
1782 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1783 LOG (GNUNET_ERROR_TYPE_DEBUG,
1784 "Connecting to transport service.\n");
1785 ret->client = GNUNET_CLIENT_connect ("transport", cfg);
1786 if (NULL == ret->client)
1792 GNUNET_CONTAINER_multipeermap_create (STARTING_NEIGHBOURS_SIZE,
1795 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1796 schedule_control_transmit (ret, sizeof (struct StartMessage),
1803 * Disconnect from the transport service.
1805 * @param handle handle to the service as returned from #GNUNET_TRANSPORT_connect()
1808 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1810 LOG (GNUNET_ERROR_TYPE_DEBUG,
1811 "Transport disconnect called!\n");
1812 /* this disconnects all neighbours... */
1813 if (handle->reconnect_task == GNUNET_SCHEDULER_NO_TASK)
1814 disconnect_and_schedule_reconnect (handle);
1815 /* and now we stop trying to connect again... */
1816 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1818 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1819 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1821 GNUNET_CONTAINER_multipeermap_destroy (handle->neighbours);
1822 handle->neighbours = NULL;
1823 if (handle->quota_task != GNUNET_SCHEDULER_NO_TASK)
1825 GNUNET_SCHEDULER_cancel (handle->quota_task);
1826 handle->quota_task = GNUNET_SCHEDULER_NO_TASK;
1828 GNUNET_free_non_null (handle->my_hello);
1829 handle->my_hello = NULL;
1830 GNUNET_assert (NULL == handle->tc_head);
1831 GNUNET_assert (NULL == handle->tc_tail);
1832 GNUNET_assert (NULL == handle->hwl_head);
1833 GNUNET_assert (NULL == handle->hwl_tail);
1834 GNUNET_CONTAINER_heap_destroy (handle->ready_heap);
1835 handle->ready_heap = NULL;
1836 GNUNET_free (handle);
1841 * Check if we could queue a message of the given size for
1842 * transmission. The transport service will take both its
1843 * internal buffers and bandwidth limits imposed by the
1844 * other peer into consideration when answering this query.
1846 * @param handle connection to transport service
1847 * @param target who should receive the message
1848 * @param size how big is the message we want to transmit?
1849 * @param timeout after how long should we give up (and call
1850 * notify with buf NULL and size 0)?
1851 * @param notify function to call when we are ready to
1852 * send such a message
1853 * @param notify_cls closure for @a notify
1854 * @return NULL if someone else is already waiting to be notified
1855 * non-NULL if the notify callback was queued (can be used to cancel
1856 * using #GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1858 struct GNUNET_TRANSPORT_TransmitHandle *
1859 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle *handle,
1860 const struct GNUNET_PeerIdentity *target,
1862 struct GNUNET_TIME_Relative timeout,
1863 GNUNET_TRANSPORT_TransmitReadyNotify notify,
1866 struct Neighbour *n;
1867 struct GNUNET_TRANSPORT_TransmitHandle *th;
1868 struct GNUNET_TIME_Relative delay;
1870 n = neighbour_find (handle, target);
1873 /* use GNUNET_TRANSPORT_try_connect first, only use this function
1874 * once a connection has been established */
1880 /* attempt to send two messages at the same time to the same peer */
1884 GNUNET_assert (NULL == n->hn);
1885 th = GNUNET_new (struct GNUNET_TRANSPORT_TransmitHandle);
1887 th->notify = notify;
1888 th->notify_cls = notify_cls;
1889 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1890 th->notify_size = size;
1892 /* calculate when our transmission should be ready */
1893 delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker, size + n->traffic_overhead);
1894 n->traffic_overhead = 0;
1895 if (delay.rel_value_us > timeout.rel_value_us)
1896 delay.rel_value_us = 0; /* notify immediately (with failure) */
1897 LOG (GNUNET_ERROR_TYPE_DEBUG,
1898 "Bandwidth tracker allows next transmission to peer %s in %s\n",
1899 GNUNET_i2s (target),
1900 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1901 n->hn = GNUNET_CONTAINER_heap_insert (handle->ready_heap, n, delay.rel_value_us);
1902 schedule_transmission (handle);
1908 * Cancel the specified transmission-ready notification.
1910 * @param th handle returned from #GNUNET_TRANSPORT_notify_transmit_ready()
1913 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct GNUNET_TRANSPORT_TransmitHandle *th)
1915 struct Neighbour *n;
1917 GNUNET_assert (NULL == th->next);
1918 GNUNET_assert (NULL == th->prev);
1920 GNUNET_assert (th == n->th);
1924 GNUNET_CONTAINER_heap_remove_node (n->hn);
1929 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != th->timeout_task);
1930 GNUNET_SCHEDULER_cancel (th->timeout_task);
1931 th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1937 /* end of transport_api.c */