2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/transport_api.c
23 * @brief library to access the low-level P2P IO service
24 * @author Christian Grothoff
27 * - adjust testcases to use new 'try connect' style (should be easy, breaks API compatibility!)
28 * - adjust core service to use new 'try connect' style (should be MUCH nicer there as well!)
32 #include "gnunet_bandwidth_lib.h"
33 #include "gnunet_client_lib.h"
34 #include "gnunet_constants.h"
35 #include "gnunet_container_lib.h"
36 #include "gnunet_arm_service.h"
37 #include "gnunet_hello_lib.h"
38 #include "gnunet_protocols.h"
39 #include "gnunet_server_lib.h"
40 #include "gnunet_time_lib.h"
41 #include "gnunet_transport_service.h"
42 #include "transport.h"
45 * How large to start with for the hashmap of neighbours.
47 #define STARTING_NEIGHBOURS_SIZE 16
51 * Handle for a message that should be transmitted to the service.
52 * Used for both control messages and normal messages.
54 struct GNUNET_TRANSPORT_TransmitHandle
58 * We keep all requests in a DLL.
60 struct GNUNET_TRANSPORT_TransmitHandle *next;
63 * We keep all requests in a DLL.
65 struct GNUNET_TRANSPORT_TransmitHandle *prev;
68 * Neighbour for this handle, NULL for control messages.
70 struct Neighbour *neighbour;
73 * Function to call when notify_size bytes are available
76 GNUNET_CONNECTION_TransmitReadyNotify notify;
84 * Timeout for this request, 0 for control messages.
86 struct GNUNET_TIME_Absolute timeout;
89 * Task to trigger request timeout if the request is stalled due to
92 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
95 * How many bytes is our notify callback waiting for?
100 * How important is this message? Not used for control messages.
108 * Entry in hash table of all of our current neighbours.
113 * Overall transport handle.
115 struct GNUNET_TRANSPORT_Handle *h;
118 * Active transmit handle or NULL.
120 struct GNUNET_TRANSPORT_TransmitHandle *th;
123 * Identity of this neighbour.
125 struct GNUNET_PeerIdentity id;
128 * Outbound bandwidh tracker.
130 struct GNUNET_BANDWIDTH_Tracker out_tracker;
133 * Entry in our readyness heap (which is sorted by 'next_ready'
134 * value). NULL if there is no pending transmission request for
135 * this neighbour or if we're waiting for 'is_ready' to become
136 * true AFTER the 'out_tracker' suggested that this peer's quota
137 * has been satisfied (so once 'is_ready' goes to GNUNET_YES,
138 * we should immediately go back into the heap).
140 struct GNUNET_CONTAINER_HeapNode *hn;
143 * Is this peer currently ready to receive a message?
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;
185 * Handle for the transport service (includes all of the
186 * state for the transport service).
188 struct GNUNET_TRANSPORT_Handle
192 * Closure for the callbacks.
197 * Function to call for received data.
199 GNUNET_TRANSPORT_ReceiveCallback rec;
202 * function to call on connect events
204 GNUNET_TRANSPORT_NotifyConnect nc_cb;
207 * function to call on disconnect events
209 GNUNET_TRANSPORT_NotifyDisconnect nd_cb;
212 * Head of DLL of control messages.
214 struct GNUNET_TRANSPORT_TransmitHandle *control_head;
217 * Tail of DLL of control messages.
219 struct GNUNET_TRANSPORT_TransmitHandle *control_tail;
222 * The current HELLO message for this peer. Updated
223 * whenever transports change their addresses.
225 struct GNUNET_HELLO_Message *my_hello;
228 * My client connection to the transport service.
230 struct GNUNET_CLIENT_Connection *client;
233 * Handle to our registration with the client for notification.
235 struct GNUNET_CLIENT_TransmitHandle *cth;
238 * Linked list of pending requests for our HELLO.
240 struct GNUNET_TRANSPORT_GetHelloHandle *hwl_head;
243 * Linked list of pending requests for our HELLO.
245 struct GNUNET_TRANSPORT_GetHelloHandle *hwl_tail;
250 const struct GNUNET_CONFIGURATION_Handle *cfg;
253 * Hash map of the current connected neighbours of this peer.
254 * Maps peer identities to 'struct Neighbour' entries.
256 struct GNUNET_CONTAINER_MultiHashMap *neighbours;
259 * Heap sorting peers with pending messages by the timestamps that
260 * specify when we could next send a message to the respective peer.
261 * Excludes control messages (which can always go out immediately).
262 * Maps time stamps to 'struct Neighbour' entries.
264 struct GNUNET_CONTAINER_Heap *ready_heap;
267 * Peer identity as assumed by this process, or all zeros.
269 struct GNUNET_PeerIdentity self;
272 * ID of the task trying to reconnect to the service.
274 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
277 * ID of the task trying to trigger transmission for a peer while
278 * maintaining bandwidth quotas. In use if there are no control
279 * messages and the smallest entry in the 'ready_heap' has a time
280 * stamp in the future.
282 GNUNET_SCHEDULER_TaskIdentifier quota_task;
285 * Delay until we try to reconnect.
287 struct GNUNET_TIME_Relative reconnect_delay;
290 * Should we check that 'self' matches what the service thinks?
291 * (if GNUNET_NO, then 'self' is all zeros!).
298 * Schedule the task to send one message, either from the control
299 * list or the peer message queues to the service.
301 * @param h transport service to schedule a transmission for
304 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h);
308 * Function that will schedule the job that will try
309 * to connect us again to the client.
311 * @param h transport service to reconnect
314 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h);
318 * Get the neighbour list entry for the given peer
320 * @param h our context
321 * @param peer peer to look up
322 * @return NULL if no such peer entry exists
324 static struct Neighbour *
325 neighbour_find (struct GNUNET_TRANSPORT_Handle *h,
326 const struct GNUNET_PeerIdentity *peer)
328 return GNUNET_CONTAINER_multihashmap_get (h->neighbours, &peer->hashPubKey);
333 * Add neighbour to our list
335 * @return NULL if this API is currently disconnecting from the service
337 static struct Neighbour *
338 neighbour_add (struct GNUNET_TRANSPORT_Handle *h,
339 const struct GNUNET_PeerIdentity *pid)
344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating entry for neighbour `%4s'.\n",
347 n = GNUNET_malloc (sizeof (struct Neighbour));
350 n->is_ready = GNUNET_YES;
351 GNUNET_BANDWIDTH_tracker_init (&n->out_tracker,
352 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
353 MAX_BANDWIDTH_CARRY_S);
354 GNUNET_assert (GNUNET_OK ==
355 GNUNET_CONTAINER_multihashmap_put (h->neighbours,
357 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
363 * Iterator over hash map entries, for deleting state of a neighbour.
365 * @param cls the 'struct GNUNET_TRANSPORT_Handle*'
366 * @param key peer identity
367 * @param value value in the hash map, the neighbour entry to delete
368 * @return GNUNET_YES if we should continue to
373 neighbour_delete (void *cls, const GNUNET_HashCode * key, void *value)
375 struct GNUNET_TRANSPORT_Handle *handle = cls;
376 struct Neighbour *n = value;
378 if (NULL != handle->nd_cb)
379 handle->nd_cb (handle->cls, &n->id);
380 GNUNET_assert (NULL == n->th);
381 GNUNET_assert (NULL == n->hn);
382 GNUNET_assert (GNUNET_YES ==
383 GNUNET_CONTAINER_multihashmap_remove (handle->neighbours, key,
391 * Function we use for handling incoming messages.
393 * @param cls closure (struct GNUNET_TRANSPORT_Handle *)
394 * @param msg message received, NULL on timeout or fatal error
397 demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
399 struct GNUNET_TRANSPORT_Handle *h = cls;
400 const struct DisconnectInfoMessage *dim;
401 const struct ConnectInfoMessage *cim;
402 const struct InboundMessage *im;
403 const struct GNUNET_MessageHeader *imm;
404 const struct SendOkMessage *okm;
405 struct GNUNET_TRANSPORT_GetHelloHandle *hwl;
406 struct GNUNET_TRANSPORT_GetHelloHandle *next_hwl;
408 struct GNUNET_PeerIdentity me;
412 GNUNET_assert (h->client != NULL);
416 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
417 "Error receiving from transport service, disconnecting temporarily.\n");
419 disconnect_and_schedule_reconnect (h);
422 GNUNET_CLIENT_receive (h->client, &demultiplexer, h,
423 GNUNET_TIME_UNIT_FOREVER_REL);
424 size = ntohs (msg->size);
425 switch (ntohs (msg->type))
427 case GNUNET_MESSAGE_TYPE_HELLO:
429 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg, &me))
435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
436 "Receiving (my own) `%s' message, I am `%4s'.\n", "HELLO",
439 GNUNET_free_non_null (h->my_hello);
441 if (size < sizeof (struct GNUNET_MessageHeader))
446 h->my_hello = GNUNET_malloc (size);
447 memcpy (h->my_hello, msg, size);
451 next_hwl = hwl->next;
452 hwl->rec (hwl->rec_cls,
453 (const struct GNUNET_MessageHeader *) h->my_hello);
457 case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
458 if (size < sizeof (struct ConnectInfoMessage))
463 cim = (const struct ConnectInfoMessage *) msg;
464 ats_count = ntohl (cim->ats_count);
466 sizeof (struct ConnectInfoMessage) +
467 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information))
473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message for `%4s'.\n",
474 "CONNECT", GNUNET_i2s (&cim->id));
476 n = neighbour_find (h, &cim->id);
482 n = neighbour_add (h, &cim->id);
483 if (h->nc_cb != NULL)
484 h->nc_cb (h->cls, &n->id, &cim->ats, ats_count);
486 case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
487 if (size != sizeof (struct DisconnectInfoMessage))
492 dim = (const struct DisconnectInfoMessage *) msg;
493 GNUNET_break (ntohl (dim->reserved) == 0);
494 #if DEBUG_TRANSPORT_DISCONNECT
495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message for `%4s'.\n",
496 "DISCONNECT", GNUNET_i2s (&dim->peer));
498 n = neighbour_find (h, &dim->peer);
504 neighbour_delete (h, &dim->peer.hashPubKey, n);
506 case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
507 if (size != sizeof (struct SendOkMessage))
512 okm = (const struct SendOkMessage *) msg;
514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
515 "Receiving `%s' message, transmission %s.\n", "SEND_OK",
516 ntohl (okm->success) == GNUNET_OK ? "succeeded" : "failed");
518 n = neighbour_find (h, &okm->peer);
521 GNUNET_break (GNUNET_NO == n->is_ready);
522 n->is_ready = GNUNET_YES;
523 if ((n->th != NULL) && (n->hn == NULL))
525 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->th->timeout_task);
526 GNUNET_SCHEDULER_cancel (n->th->timeout_task);
527 n->th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
528 /* we've been waiting for this (congestion, not quota,
529 * caused delayed transmission) */
530 n->hn = GNUNET_CONTAINER_heap_insert (h->ready_heap, n, 0);
531 schedule_transmission (h);
534 case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message.\n", "RECV");
539 sizeof (struct InboundMessage) + sizeof (struct GNUNET_MessageHeader))
544 im = (const struct InboundMessage *) msg;
545 GNUNET_break (0 == ntohl (im->reserved));
546 ats_count = ntohl (im->ats_count);
547 imm = (const struct GNUNET_MessageHeader *) &((&(im->ats))[ats_count + 1]);
549 if (ntohs (imm->size) + sizeof (struct InboundMessage) +
550 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) != size)
556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
557 "Received message of type %u from `%4s'.\n", ntohs (imm->type),
558 GNUNET_i2s (&im->peer));
560 n = neighbour_find (h, &im->peer);
567 h->rec (h->cls, &im->peer, imm, &im->ats, ats_count);
570 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
571 _("Received unexpected message of type %u in %s:%u\n"),
572 ntohs (msg->type), __FILE__, __LINE__);
580 * A transmission request could not be satisfied because of
581 * network congestion. Notify the initiator and clean up.
583 * @param cls the 'struct GNUNET_TRANSPORT_TransmitHandle'
584 * @param tc scheduler context
587 timeout_request_due_to_congestion (void *cls,
588 const struct GNUNET_SCHEDULER_TaskContext
591 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
592 struct Neighbour *n = th->neighbour;
594 n->th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
595 GNUNET_assert (th == n->th);
596 GNUNET_assert (NULL == n->hn);
598 th->notify (th->notify_cls, 0, NULL);
604 * Transmit message(s) to service.
606 * @param cls handle to transport
607 * @param size number of bytes available in buf
608 * @param buf where to copy the message
609 * @return number of bytes copied to buf
612 transport_notify_ready (void *cls, size_t size, void *buf)
614 struct GNUNET_TRANSPORT_Handle *h = cls;
615 struct GNUNET_TRANSPORT_TransmitHandle *th;
618 struct OutboundMessage obm;
623 GNUNET_assert (NULL != h->client);
627 /* transmission failed */
628 disconnect_and_schedule_reconnect (h);
634 /* first send control messages */
635 while ((NULL != (th = h->control_head)) && (th->notify_size <= size))
637 GNUNET_CONTAINER_DLL_remove (h->control_head, h->control_tail, th);
638 nret = th->notify (th->notify_cls, size, &cbuf[ret]);
640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
641 "Added %u bytes of control message at %u\n", nret, ret);
648 /* then, if possible and no control messages pending, send data messages */
649 while ((NULL == h->control_head) &&
650 (NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap))))
652 if (GNUNET_YES != n->is_ready)
654 /* peer not ready, wait for notification! */
655 GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
657 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == n->th->timeout_task);
658 n->th->timeout_task =
659 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
661 &timeout_request_due_to_congestion,
666 if (th->notify_size + sizeof (struct OutboundMessage) > size)
667 break; /* does not fit */
668 if (GNUNET_BANDWIDTH_tracker_get_delay
669 (&n->out_tracker, th->notify_size).rel_value > 0)
670 break; /* too early */
671 GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
674 n->is_ready = GNUNET_NO;
675 GNUNET_assert (size >= sizeof (struct OutboundMessage));
677 th->notify (th->notify_cls, size - sizeof (struct OutboundMessage),
678 &cbuf[ret + sizeof (struct OutboundMessage)]);
679 GNUNET_assert (mret <= size - sizeof (struct OutboundMessage));
682 GNUNET_assert (mret + sizeof (struct OutboundMessage) <
683 GNUNET_SERVER_MAX_MESSAGE_SIZE);
684 obm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
685 obm.header.size = htons (mret + sizeof (struct OutboundMessage));
686 obm.priority = htonl (th->priority);
688 GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining
691 memcpy (&cbuf[ret], &obm, sizeof (struct OutboundMessage));
692 ret += (mret + sizeof (struct OutboundMessage));
693 size -= (mret + sizeof (struct OutboundMessage));
694 GNUNET_BANDWIDTH_tracker_consume (&n->out_tracker, mret);
698 /* if there are more pending messages, try to schedule those */
699 schedule_transmission (h);
701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
702 "Transmitting %u bytes to transport service\n", ret);
709 * Schedule the task to send one message, either from the control
710 * list or the peer message queues to the service.
712 * @param cls transport service to schedule a transmission for
713 * @param tc scheduler context
716 schedule_transmission_task (void *cls,
717 const struct GNUNET_SCHEDULER_TaskContext *tc)
719 struct GNUNET_TRANSPORT_Handle *h = cls;
721 struct GNUNET_TRANSPORT_TransmitHandle *th;
724 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
725 GNUNET_assert (NULL != h->client);
726 /* destroy all requests that have timed out */
727 while ((NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap))) &&
728 (GNUNET_TIME_absolute_get_remaining (n->th->timeout).rel_value == 0))
730 /* notify client that the request could not be satisfied within
731 * the given time constraints */
734 GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
738 "Signalling timeout for transmission to peer %s due to congestion\n",
739 GNUNET_i2s (&n->id));
741 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
746 if (NULL != h->control_head)
748 size = h->control_head->notify_size;
752 n = GNUNET_CONTAINER_heap_peek (h->ready_heap);
754 return; /* no pending messages */
755 size = n->th->notify_size + sizeof (struct OutboundMessage);
758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Calling notify_transmit_ready\n");
761 GNUNET_CLIENT_notify_transmit_ready (h->client, size,
762 GNUNET_TIME_UNIT_FOREVER_REL,
763 GNUNET_NO, &transport_notify_ready,
765 GNUNET_assert (NULL != h->cth);
770 * Schedule the task to send one message, either from the control
771 * list or the peer message queues to the service.
773 * @param h transport service to schedule a transmission for
776 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
778 struct GNUNET_TIME_Relative delay;
781 GNUNET_assert (NULL != h->client);
782 if (h->quota_task != GNUNET_SCHEDULER_NO_TASK)
784 GNUNET_SCHEDULER_cancel (h->quota_task);
785 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
787 if (NULL != h->control_head)
788 delay = GNUNET_TIME_UNIT_ZERO;
789 else if (NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap)))
791 GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
794 return; /* no work to be done */
795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
796 "Scheduling next transmission to service in %llu ms\n",
797 (unsigned long long) delay.rel_value);
799 GNUNET_SCHEDULER_add_delayed (delay, &schedule_transmission_task, h);
804 * Queue control request for transmission to the transport
807 * @param h handle to the transport service
808 * @param size number of bytes to be transmitted
809 * @param notify function to call to get the content
810 * @param notify_cls closure for notify
813 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h, size_t size,
814 GNUNET_CONNECTION_TransmitReadyNotify notify,
817 struct GNUNET_TRANSPORT_TransmitHandle *th;
820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
821 "Control transmit of %u bytes requested\n", size);
823 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
825 th->notify_cls = notify_cls;
826 th->notify_size = size;
827 GNUNET_CONTAINER_DLL_insert_tail (h->control_head, h->control_tail, th);
828 schedule_transmission (h);
833 * Transmit START message to service.
836 * @param size number of bytes available in buf
837 * @param buf where to copy the message
838 * @return number of bytes copied to buf
841 send_start (void *cls, size_t size, void *buf)
843 struct GNUNET_TRANSPORT_Handle *h = cls;
844 struct StartMessage s;
848 /* Can only be shutdown, just give up */
850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
851 "Shutdown while trying to transmit `%s' request.\n", "START");
856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "START");
858 GNUNET_assert (size >= sizeof (struct StartMessage));
859 s.header.size = htons (sizeof (struct StartMessage));
860 s.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
861 s.do_check = htonl (h->check_self);
863 memcpy (buf, &s, sizeof (struct StartMessage));
864 GNUNET_CLIENT_receive (h->client, &demultiplexer, h,
865 GNUNET_TIME_UNIT_FOREVER_REL);
866 return sizeof (struct StartMessage);
871 * Try again to connect to transport service.
873 * @param cls the handle to the transport service
874 * @param tc scheduler context
877 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
879 struct GNUNET_TRANSPORT_Handle *h = cls;
881 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
882 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
884 /* shutdown, just give up */
888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n");
890 GNUNET_assert (h->client == NULL);
891 GNUNET_assert (h->control_head == NULL);
892 GNUNET_assert (h->control_tail == NULL);
893 h->client = GNUNET_CLIENT_connect ("transport", h->cfg);
894 GNUNET_assert (h->client != NULL);
895 schedule_control_transmit (h, sizeof (struct StartMessage), &send_start, h);
900 * Function that will schedule the job that will try
901 * to connect us again to the client.
903 * @param h transport service to reconnect
906 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
908 struct GNUNET_TRANSPORT_TransmitHandle *th;
910 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
911 /* Forget about all neighbours that we used to be connected to */
912 GNUNET_CONTAINER_multihashmap_iterate (h->neighbours, &neighbour_delete, h);
915 GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
918 if (NULL != h->client)
920 GNUNET_CLIENT_disconnect (h->client, GNUNET_YES);
923 if (h->quota_task != GNUNET_SCHEDULER_NO_TASK)
925 GNUNET_SCHEDULER_cancel (h->quota_task);
926 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
928 while ((NULL != (th = h->control_head)))
930 GNUNET_CONTAINER_DLL_remove (h->control_head, h->control_tail, th);
931 th->notify (th->notify_cls, 0, NULL);
935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
936 "Scheduling task to reconnect to transport service in %llu ms.\n",
937 h->reconnect_delay.rel_value);
940 GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
941 if (h->reconnect_delay.rel_value == 0)
943 h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
947 h->reconnect_delay = GNUNET_TIME_relative_multiply (h->reconnect_delay, 2);
949 GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS, h->reconnect_delay);
955 * Closure for 'send_set_quota'.
957 struct SetQuotaContext
961 * Identity of the peer impacted by the quota change.
963 struct GNUNET_PeerIdentity target;
968 struct GNUNET_BANDWIDTH_Value32NBO quota_in;
973 * Send SET_QUOTA message to the service.
975 * @param cls the 'struct SetQuotaContext'
976 * @param size number of bytes available in buf
977 * @param buf where to copy the message
978 * @return number of bytes copied to buf
981 send_set_quota (void *cls, size_t size, void *buf)
983 struct SetQuotaContext *sqc = cls;
984 struct QuotaSetMessage msg;
992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
993 "Transmitting `%s' request with respect to `%4s'.\n", "SET_QUOTA",
994 GNUNET_i2s (&sqc->target));
996 GNUNET_assert (size >= sizeof (struct QuotaSetMessage));
997 msg.header.size = htons (sizeof (struct QuotaSetMessage));
998 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
999 msg.quota = sqc->quota_in;
1000 msg.peer = sqc->target;
1001 memcpy (buf, &msg, sizeof (msg));
1003 return sizeof (struct QuotaSetMessage);
1008 * Set the share of incoming bandwidth for the given
1009 * peer to the specified amount.
1011 * @param handle connection to transport service
1012 * @param target who's bandwidth quota is being changed
1013 * @param quota_in incoming bandwidth quota in bytes per ms
1014 * @param quota_out outgoing bandwidth quota in bytes per ms
1017 GNUNET_TRANSPORT_set_quota (struct GNUNET_TRANSPORT_Handle *handle,
1018 const struct GNUNET_PeerIdentity *target,
1019 struct GNUNET_BANDWIDTH_Value32NBO quota_in,
1020 struct GNUNET_BANDWIDTH_Value32NBO quota_out)
1022 struct Neighbour *n;
1023 struct SetQuotaContext *sqc;
1025 n = neighbour_find (handle, target);
1028 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1029 "Quota changed to %u for peer `%s', but I have no such neighbour!\n",
1030 (unsigned int) ntohl (quota_out.value__), GNUNET_i2s (target));
1033 GNUNET_assert (NULL != handle->client);
1035 if (ntohl (quota_out.value__) != n->out_tracker.available_bytes_per_s__)
1036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1037 "Quota changed from %u to %u for peer `%s'\n",
1038 (unsigned int) n->out_tracker.available_bytes_per_s__,
1039 (unsigned int) ntohl (quota_out.value__), GNUNET_i2s (target));
1041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Quota remains at %u for peer `%s'\n",
1042 (unsigned int) n->out_tracker.available_bytes_per_s__,
1043 GNUNET_i2s (target));
1045 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker, quota_out);
1046 sqc = GNUNET_malloc (sizeof (struct SetQuotaContext));
1047 sqc->target = *target;
1048 sqc->quota_in = quota_in;
1049 schedule_control_transmit (handle, sizeof (struct QuotaSetMessage),
1050 &send_set_quota, sqc);
1055 * Send REQUEST_CONNECT message to the service.
1057 * @param cls the 'struct GNUNET_PeerIdentity'
1058 * @param size number of bytes available in buf
1059 * @param buf where to copy the message
1060 * @return number of bytes copied to buf
1063 send_try_connect (void *cls, size_t size, void *buf)
1065 struct GNUNET_PeerIdentity *pid = cls;
1066 struct TransportRequestConnectMessage msg;
1074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1075 "Transmitting `%s' request with respect to `%4s'.\n",
1076 "REQUEST_CONNECT", GNUNET_i2s (pid));
1078 GNUNET_assert (size >= sizeof (struct TransportRequestConnectMessage));
1079 msg.header.size = htons (sizeof (struct TransportRequestConnectMessage));
1080 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT);
1081 msg.reserved = htonl (0);
1083 memcpy (buf, &msg, sizeof (msg));
1085 return sizeof (struct TransportRequestConnectMessage);
1090 * Ask the transport service to establish a connection to
1093 * @param handle connection to transport service
1094 * @param target who we should try to connect to
1097 GNUNET_TRANSPORT_try_connect (struct GNUNET_TRANSPORT_Handle *handle,
1098 const struct GNUNET_PeerIdentity *target)
1100 struct GNUNET_PeerIdentity *pid;
1102 if (NULL == handle->client)
1104 pid = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
1106 schedule_control_transmit (handle,
1107 sizeof (struct TransportRequestConnectMessage),
1108 &send_try_connect, pid);
1113 * Send HELLO message to the service.
1115 * @param cls the HELLO message to send
1116 * @param size number of bytes available in buf
1117 * @param buf where to copy the message
1118 * @return number of bytes copied to buf
1121 send_hello (void *cls, size_t size, void *buf)
1123 struct GNUNET_MessageHeader *msg = cls;
1128 #if DEBUG_TRANSPORT_TIMEOUT
1129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1130 "Timeout while trying to transmit `%s' request.\n", "HELLO");
1136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "HELLO");
1138 ssize = ntohs (msg->size);
1139 GNUNET_assert (size >= ssize);
1140 memcpy (buf, msg, ssize);
1147 * Offer the transport service the HELLO of another peer. Note that
1148 * the transport service may just ignore this message if the HELLO is
1149 * malformed or useless due to our local configuration.
1151 * @param handle connection to transport service
1152 * @param hello the hello message
1153 * @param cont continuation to call when HELLO has been sent
1154 * @param cls closure for continuation
1158 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
1159 const struct GNUNET_MessageHeader *hello,
1160 GNUNET_SCHEDULER_Task cont, void *cls)
1163 struct GNUNET_PeerIdentity peer;
1164 struct GNUNET_MessageHeader *msg;
1166 if (NULL == handle->client)
1168 GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
1169 size = ntohs (hello->size);
1170 GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
1172 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hello, &peer))
1177 msg = GNUNET_malloc (size);
1178 memcpy (msg, hello, size);
1180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1181 "Offering `%s' message of `%4s' to transport for validation.\n",
1182 "HELLO", GNUNET_i2s (&peer));
1184 schedule_control_transmit (handle, size, &send_hello, msg);
1189 * Obtain the HELLO message for this peer.
1191 * @param handle connection to transport service
1192 * @param rec function to call with the HELLO, sender will be our peer
1193 * identity; message and sender will be NULL on timeout
1194 * (handshake with transport service pending/failed).
1195 * cost estimate will be 0.
1196 * @param rec_cls closure for rec
1197 * @return handle to cancel the operation, NULL on error
1199 struct GNUNET_TRANSPORT_GetHelloHandle *
1200 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
1201 GNUNET_TRANSPORT_HelloUpdateCallback rec,
1204 struct GNUNET_TRANSPORT_GetHelloHandle *hwl;
1206 if (handle->my_hello == NULL)
1208 hwl = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_GetHelloHandle));
1210 hwl->rec_cls = rec_cls;
1211 hwl->handle = handle;
1212 GNUNET_CONTAINER_DLL_insert (handle->hwl_head, handle->hwl_tail, hwl);
1213 rec (rec_cls, (const struct GNUNET_MessageHeader *) handle->my_hello);
1219 * Stop receiving updates about changes to our HELLO message.
1221 * @param ghh handle to cancel
1224 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_GetHelloHandle *ghh)
1226 struct GNUNET_TRANSPORT_Handle *handle = ghh->handle;
1228 GNUNET_CONTAINER_DLL_remove (handle->hwl_head, handle->hwl_tail, ghh);
1234 * Connect to the transport service. Note that the connection may
1235 * complete (or fail) asynchronously.
1237 * @param cfg configuration to use
1238 * @param self our own identity (API should check that it matches
1239 * the identity found by transport), or NULL (no check)
1240 * @param cls closure for the callbacks
1241 * @param rec receive function to call
1242 * @param nc function to call on connect events
1243 * @param nd function to call on disconnect events
1245 struct GNUNET_TRANSPORT_Handle *
1246 GNUNET_TRANSPORT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1247 const struct GNUNET_PeerIdentity *self, void *cls,
1248 GNUNET_TRANSPORT_ReceiveCallback rec,
1249 GNUNET_TRANSPORT_NotifyConnect nc,
1250 GNUNET_TRANSPORT_NotifyDisconnect nd)
1252 struct GNUNET_TRANSPORT_Handle *ret;
1254 ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Handle));
1258 ret->check_self = GNUNET_YES;
1265 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1267 GNUNET_CONTAINER_multihashmap_create (STARTING_NEIGHBOURS_SIZE);
1269 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1270 ret->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, ret);
1276 * Disconnect from the transport service.
1278 * @param handle handle to the service as returned from GNUNET_TRANSPORT_connect
1281 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transport disconnect called!\n");
1286 /* this disconnects all neighbours... */
1287 if (handle->reconnect_task == GNUNET_SCHEDULER_NO_TASK)
1288 disconnect_and_schedule_reconnect (handle);
1289 /* and now we stop trying to connect again... */
1290 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1292 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1293 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1295 GNUNET_CONTAINER_multihashmap_destroy (handle->neighbours);
1296 handle->neighbours = NULL;
1297 if (handle->quota_task != GNUNET_SCHEDULER_NO_TASK)
1299 GNUNET_SCHEDULER_cancel (handle->quota_task);
1300 handle->quota_task = GNUNET_SCHEDULER_NO_TASK;
1302 GNUNET_free_non_null (handle->my_hello);
1303 handle->my_hello = NULL;
1304 GNUNET_assert (handle->hwl_head == NULL);
1305 GNUNET_assert (handle->hwl_tail == NULL);
1306 GNUNET_CONTAINER_heap_destroy (handle->ready_heap);
1307 handle->ready_heap = NULL;
1308 GNUNET_free (handle);
1313 * Check if we could queue a message of the given size for
1314 * transmission. The transport service will take both its
1315 * internal buffers and bandwidth limits imposed by the
1316 * other peer into consideration when answering this query.
1318 * @param handle connection to transport service
1319 * @param target who should receive the message
1320 * @param size how big is the message we want to transmit?
1321 * @param priority how important is the message?
1322 * @param timeout after how long should we give up (and call
1323 * notify with buf NULL and size 0)?
1324 * @param notify function to call when we are ready to
1325 * send such a message
1326 * @param notify_cls closure for notify
1327 * @return NULL if someone else is already waiting to be notified
1328 * non-NULL if the notify callback was queued (can be used to cancel
1329 * using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1331 struct GNUNET_TRANSPORT_TransmitHandle *
1332 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle *handle,
1333 const struct GNUNET_PeerIdentity
1334 *target, size_t size, uint32_t priority,
1335 struct GNUNET_TIME_Relative timeout,
1336 GNUNET_CONNECTION_TransmitReadyNotify
1337 notify, void *notify_cls)
1339 struct Neighbour *n;
1340 struct GNUNET_TRANSPORT_TransmitHandle *th;
1341 struct GNUNET_TIME_Relative delay;
1343 n = neighbour_find (handle, target);
1346 /* use GNUNET_TRANSPORT_try_connect first, only use this function
1347 * once a connection has been established */
1353 /* attempt to send two messages at the same time to the same peer */
1357 GNUNET_assert (NULL == n->hn);
1358 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
1360 th->notify = notify;
1361 th->notify_cls = notify_cls;
1362 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1363 th->notify_size = size;
1364 th->priority = priority;
1366 /* calculate when our transmission should be ready */
1367 delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker, size);
1368 if (delay.rel_value > timeout.rel_value)
1369 delay.rel_value = 0; /* notify immediately (with failure) */
1371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1372 "Bandwidth tracker allows next transmission to peer %s in %llu ms\n",
1373 GNUNET_i2s (target), (unsigned long long) delay.rel_value);
1375 n->hn = GNUNET_CONTAINER_heap_insert (handle->ready_heap, n, delay.rel_value);
1376 schedule_transmission (handle);
1382 * Cancel the specified transmission-ready notification.
1384 * @param th handle returned from GNUNET_TRANSPORT_notify_transmit_ready
1387 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
1388 GNUNET_TRANSPORT_TransmitHandle
1391 struct Neighbour *n;
1393 GNUNET_assert (NULL == th->next);
1394 GNUNET_assert (NULL == th->prev);
1396 GNUNET_assert (th == n->th);
1400 GNUNET_CONTAINER_heap_remove_node (n->hn);
1405 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != th->timeout_task);
1406 GNUNET_SCHEDULER_cancel (th->timeout_task);
1407 th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1413 /* end of transport_api.c */