2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/transport_api.c
23 * @brief library to access the low-level P2P IO service
24 * @author Christian Grothoff
27 * - adjust testcases to use new 'try connect' style (should be easy, breaks API compatibility!)
28 * - adjust core service to use new 'try connect' style (should be MUCH nicer there as well!)
32 #include "gnunet_constants.h"
33 #include "gnunet_bandwidth_lib.h"
34 #include "gnunet_client_lib.h"
35 #include "gnunet_constants.h"
36 #include "gnunet_container_lib.h"
37 #include "gnunet_arm_service.h"
38 #include "gnunet_hello_lib.h"
39 #include "gnunet_protocols.h"
40 #include "gnunet_server_lib.h"
41 #include "gnunet_time_lib.h"
42 #include "gnunet_transport_service.h"
43 #include "transport.h"
45 #define LOG(kind,...) GNUNET_log_from (kind, "transport-api",__VA_ARGS__)
48 * How large to start with for the hashmap of neighbours.
50 #define STARTING_NEIGHBOURS_SIZE 16
53 * Handle for a message that should be transmitted to the service.
54 * Used for both control messages and normal messages.
56 struct GNUNET_TRANSPORT_TransmitHandle
60 * We keep all requests in a DLL.
62 struct GNUNET_TRANSPORT_TransmitHandle *next;
65 * We keep all requests in a DLL.
67 struct GNUNET_TRANSPORT_TransmitHandle *prev;
70 * Neighbour for this handle, NULL for control messages.
72 struct Neighbour *neighbour;
75 * Function to call when notify_size bytes are available
78 GNUNET_CONNECTION_TransmitReadyNotify notify;
86 * Timeout for this request, 0 for control messages.
88 struct GNUNET_TIME_Absolute timeout;
91 * Task to trigger request timeout if the request is stalled due to
94 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
97 * How many bytes is our notify callback waiting for?
102 * How important is this message? Not used for control messages.
110 * Entry in hash table of all of our current neighbours.
115 * Overall transport handle.
117 struct GNUNET_TRANSPORT_Handle *h;
120 * Active transmit handle or NULL.
122 struct GNUNET_TRANSPORT_TransmitHandle *th;
125 * Identity of this neighbour.
127 struct GNUNET_PeerIdentity id;
130 * Outbound bandwidh tracker.
132 struct GNUNET_BANDWIDTH_Tracker out_tracker;
135 * Entry in our readyness heap (which is sorted by 'next_ready'
136 * value). NULL if there is no pending transmission request for
137 * this neighbour or if we're waiting for 'is_ready' to become
138 * true AFTER the 'out_tracker' suggested that this peer's quota
139 * has been satisfied (so once 'is_ready' goes to GNUNET_YES,
140 * we should immediately go back into the heap).
142 struct GNUNET_CONTAINER_HeapNode *hn;
145 * Is this peer currently ready to receive a message?
153 * Linked list of functions to call whenever our HELLO is updated.
155 struct GNUNET_TRANSPORT_GetHelloHandle
159 * This is a doubly linked list.
161 struct GNUNET_TRANSPORT_GetHelloHandle *next;
164 * This is a doubly linked list.
166 struct GNUNET_TRANSPORT_GetHelloHandle *prev;
171 struct GNUNET_TRANSPORT_Handle *handle;
174 * Callback to call once we got our HELLO.
176 GNUNET_TRANSPORT_HelloUpdateCallback rec;
187 * Handle for the transport service (includes all of the
188 * state for the transport service).
190 struct GNUNET_TRANSPORT_Handle
194 * Closure for the callbacks.
199 * Function to call for received data.
201 GNUNET_TRANSPORT_ReceiveCallback rec;
204 * function to call on connect events
206 GNUNET_TRANSPORT_NotifyConnect nc_cb;
209 * function to call on disconnect events
211 GNUNET_TRANSPORT_NotifyDisconnect nd_cb;
214 * Head of DLL of control messages.
216 struct GNUNET_TRANSPORT_TransmitHandle *control_head;
219 * Tail of DLL of control messages.
221 struct GNUNET_TRANSPORT_TransmitHandle *control_tail;
224 * The current HELLO message for this peer. Updated
225 * whenever transports change their addresses.
227 struct GNUNET_HELLO_Message *my_hello;
230 * My client connection to the transport service.
232 struct GNUNET_CLIENT_Connection *client;
235 * Handle to our registration with the client for notification.
237 struct GNUNET_CLIENT_TransmitHandle *cth;
240 * Linked list of pending requests for our HELLO.
242 struct GNUNET_TRANSPORT_GetHelloHandle *hwl_head;
245 * Linked list of pending requests for our HELLO.
247 struct GNUNET_TRANSPORT_GetHelloHandle *hwl_tail;
252 const struct GNUNET_CONFIGURATION_Handle *cfg;
255 * Hash map of the current connected neighbours of this peer.
256 * Maps peer identities to 'struct Neighbour' entries.
258 struct GNUNET_CONTAINER_MultiHashMap *neighbours;
261 * Heap sorting peers with pending messages by the timestamps that
262 * specify when we could next send a message to the respective peer.
263 * Excludes control messages (which can always go out immediately).
264 * Maps time stamps to 'struct Neighbour' entries.
266 struct GNUNET_CONTAINER_Heap *ready_heap;
269 * Peer identity as assumed by this process, or all zeros.
271 struct GNUNET_PeerIdentity self;
274 * ID of the task trying to reconnect to the service.
276 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
279 * ID of the task trying to trigger transmission for a peer while
280 * maintaining bandwidth quotas. In use if there are no control
281 * messages and the smallest entry in the 'ready_heap' has a time
282 * stamp in the future.
284 GNUNET_SCHEDULER_TaskIdentifier quota_task;
287 * Delay until we try to reconnect.
289 struct GNUNET_TIME_Relative reconnect_delay;
292 * Should we check that 'self' matches what the service thinks?
293 * (if GNUNET_NO, then 'self' is all zeros!).
300 * Schedule the task to send one message, either from the control
301 * list or the peer message queues to the service.
303 * @param h transport service to schedule a transmission for
306 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h);
310 * Function that will schedule the job that will try
311 * to connect us again to the client.
313 * @param h transport service to reconnect
316 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h);
320 * Get the neighbour list entry for the given peer
322 * @param h our context
323 * @param peer peer to look up
324 * @return NULL if no such peer entry exists
326 static struct Neighbour *
327 neighbour_find (struct GNUNET_TRANSPORT_Handle *h,
328 const struct GNUNET_PeerIdentity *peer)
330 return GNUNET_CONTAINER_multihashmap_get (h->neighbours, &peer->hashPubKey);
335 * Add neighbour to our list
337 * @return NULL if this API is currently disconnecting from the service
339 static struct Neighbour *
340 neighbour_add (struct GNUNET_TRANSPORT_Handle *h,
341 const struct GNUNET_PeerIdentity *pid)
345 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 struct 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 const struct QuotaSetMessage *qm;
406 const struct GNUNET_ATS_Information *ats;
407 struct GNUNET_TRANSPORT_GetHelloHandle *hwl;
408 struct GNUNET_TRANSPORT_GetHelloHandle *next_hwl;
410 struct GNUNET_PeerIdentity me;
414 GNUNET_assert (h->client != NULL);
417 LOG (GNUNET_ERROR_TYPE_DEBUG,
418 "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))
434 LOG (GNUNET_ERROR_TYPE_DEBUG,
435 "Receiving (my own) `%s' message, I am `%4s'.\n", "HELLO",
437 GNUNET_free_non_null (h->my_hello);
439 if (size < sizeof (struct GNUNET_MessageHeader))
444 h->my_hello = GNUNET_malloc (size);
445 memcpy (h->my_hello, msg, size);
449 next_hwl = hwl->next;
450 hwl->rec (hwl->rec_cls,
451 (const struct GNUNET_MessageHeader *) h->my_hello);
455 case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
456 if (size < sizeof (struct ConnectInfoMessage))
461 cim = (const struct ConnectInfoMessage *) msg;
462 ats_count = ntohl (cim->ats_count);
464 sizeof (struct ConnectInfoMessage) +
465 ats_count * sizeof (struct GNUNET_ATS_Information))
470 ats = (const struct GNUNET_ATS_Information *) &cim[1];
471 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message for `%4s'.\n",
472 "CONNECT", GNUNET_i2s (&cim->id));
473 n = neighbour_find (h, &cim->id);
479 n = neighbour_add (h, &cim->id);
480 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message for `%4s' with quota %u\n",
481 "CONNECT", GNUNET_i2s (&cim->id), ntohl (cim->quota_out.value__));
482 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker, cim->quota_out);
483 if (h->nc_cb != NULL)
484 h->nc_cb (h->cls, &n->id, 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 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message for `%4s'.\n",
495 "DISCONNECT", GNUNET_i2s (&dim->peer));
496 n = neighbour_find (h, &dim->peer);
502 neighbour_delete (h, &dim->peer.hashPubKey, n);
504 case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
505 if (size != sizeof (struct SendOkMessage))
510 okm = (const struct SendOkMessage *) msg;
511 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message, transmission %s.\n",
512 "SEND_OK", ntohl (okm->success) == GNUNET_OK ? "succeeded" : "failed");
513 n = neighbour_find (h, &okm->peer);
516 GNUNET_break (GNUNET_NO == n->is_ready);
517 n->is_ready = GNUNET_YES;
518 if ((n->th != NULL) && (n->hn == NULL))
520 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->th->timeout_task);
521 GNUNET_SCHEDULER_cancel (n->th->timeout_task);
522 n->th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
523 /* we've been waiting for this (congestion, not quota,
524 * caused delayed transmission) */
525 n->hn = GNUNET_CONTAINER_heap_insert (h->ready_heap, n, 0);
526 schedule_transmission (h);
529 case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
530 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message.\n", "RECV");
532 sizeof (struct InboundMessage) + sizeof (struct GNUNET_MessageHeader))
537 im = (const struct InboundMessage *) msg;
538 ats_count = ntohl (im->ats_count);
539 ats = (const struct GNUNET_ATS_Information *) &im[1];
540 imm = (const struct GNUNET_MessageHeader *) &ats[ats_count];
541 if (ntohs (imm->size) + sizeof (struct InboundMessage) +
542 ats_count * sizeof (struct GNUNET_ATS_Information) != size)
547 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %u from `%4s'.\n",
548 ntohs (imm->type), GNUNET_i2s (&im->peer));
549 n = neighbour_find (h, &im->peer);
556 h->rec (h->cls, &im->peer, imm, ats, ats_count);
558 case GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA:
559 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message.\n", "SET_QUOTA");
560 if (size != sizeof (struct QuotaSetMessage))
565 qm = (const struct QuotaSetMessage *) msg;
566 n = neighbour_find (h, &qm->peer);
569 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message for `%4s' with quota %u\n",
570 "SET_QUOTA", GNUNET_i2s (&qm->peer), ntohl (qm->quota.value__));
571 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker, qm->quota);
574 LOG (GNUNET_ERROR_TYPE_ERROR,
575 _("Received unexpected message of type %u in %s:%u\n"),
576 ntohs (msg->type), __FILE__, __LINE__);
584 * A transmission request could not be satisfied because of
585 * network congestion. Notify the initiator and clean up.
587 * @param cls the 'struct GNUNET_TRANSPORT_TransmitHandle'
588 * @param tc scheduler context
591 timeout_request_due_to_congestion (void *cls,
592 const struct GNUNET_SCHEDULER_TaskContext
595 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
596 struct Neighbour *n = th->neighbour;
598 n->th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
599 GNUNET_assert (th == n->th);
600 GNUNET_assert (NULL == n->hn);
602 th->notify (th->notify_cls, 0, NULL);
608 * Transmit message(s) to service.
610 * @param cls handle to transport
611 * @param size number of bytes available in buf
612 * @param buf where to copy the message
613 * @return number of bytes copied to buf
616 transport_notify_ready (void *cls, size_t size, void *buf)
618 struct GNUNET_TRANSPORT_Handle *h = cls;
619 struct GNUNET_TRANSPORT_TransmitHandle *th;
622 struct OutboundMessage obm;
627 GNUNET_assert (NULL != h->client);
631 /* transmission failed */
632 disconnect_and_schedule_reconnect (h);
638 /* first send control messages */
639 while ((NULL != (th = h->control_head)) && (th->notify_size <= size))
641 GNUNET_CONTAINER_DLL_remove (h->control_head, h->control_tail, th);
642 nret = th->notify (th->notify_cls, size, &cbuf[ret]);
643 LOG (GNUNET_ERROR_TYPE_DEBUG, "Added %u bytes of control message at %u\n",
650 /* then, if possible and no control messages pending, send data messages */
651 while ((NULL == h->control_head) &&
652 (NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap))))
654 if (GNUNET_YES != n->is_ready)
656 /* peer not ready, wait for notification! */
657 GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
659 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == n->th->timeout_task);
660 n->th->timeout_task =
661 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
663 &timeout_request_due_to_congestion,
668 if (th->notify_size + sizeof (struct OutboundMessage) > size)
669 break; /* does not fit */
670 if (GNUNET_BANDWIDTH_tracker_get_delay
671 (&n->out_tracker, th->notify_size).rel_value > 0)
672 break; /* too early */
673 GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
676 n->is_ready = GNUNET_NO;
677 GNUNET_assert (size >= sizeof (struct OutboundMessage));
679 th->notify (th->notify_cls, size - sizeof (struct OutboundMessage),
680 &cbuf[ret + sizeof (struct OutboundMessage)]);
681 GNUNET_assert (mret <= size - sizeof (struct OutboundMessage));
684 GNUNET_assert (mret + sizeof (struct OutboundMessage) <
685 GNUNET_SERVER_MAX_MESSAGE_SIZE);
686 obm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
687 obm.header.size = htons (mret + sizeof (struct OutboundMessage));
688 obm.priority = htonl (th->priority);
690 GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining
693 memcpy (&cbuf[ret], &obm, sizeof (struct OutboundMessage));
694 ret += (mret + sizeof (struct OutboundMessage));
695 size -= (mret + sizeof (struct OutboundMessage));
696 GNUNET_BANDWIDTH_tracker_consume (&n->out_tracker, mret);
700 /* if there are more pending messages, try to schedule those */
701 schedule_transmission (h);
702 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting %u bytes to transport service\n",
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));
736 LOG (GNUNET_ERROR_TYPE_DEBUG,
737 "Signalling timeout for transmission to peer %s due to congestion\n",
738 GNUNET_i2s (&n->id));
739 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
744 if (NULL != h->control_head)
746 size = h->control_head->notify_size;
750 n = GNUNET_CONTAINER_heap_peek (h->ready_heap);
752 return; /* no pending messages */
753 size = n->th->notify_size + sizeof (struct OutboundMessage);
755 LOG (GNUNET_ERROR_TYPE_DEBUG, "Calling notify_transmit_ready\n");
757 GNUNET_CLIENT_notify_transmit_ready (h->client, size,
758 GNUNET_TIME_UNIT_FOREVER_REL,
759 GNUNET_NO, &transport_notify_ready,
761 GNUNET_assert (NULL != h->cth);
766 * Schedule the task to send one message, either from the control
767 * list or the peer message queues to the service.
769 * @param h transport service to schedule a transmission for
772 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
774 struct GNUNET_TIME_Relative delay;
777 GNUNET_assert (NULL != h->client);
778 if (h->quota_task != GNUNET_SCHEDULER_NO_TASK)
780 GNUNET_SCHEDULER_cancel (h->quota_task);
781 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
783 if (NULL != h->control_head)
784 delay = GNUNET_TIME_UNIT_ZERO;
785 else if (NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap)))
787 GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
790 return; /* no work to be done */
791 LOG (GNUNET_ERROR_TYPE_DEBUG,
792 "Scheduling next transmission to service in %llu ms\n",
793 (unsigned long long) delay.rel_value);
795 GNUNET_SCHEDULER_add_delayed (delay, &schedule_transmission_task, h);
800 * Queue control request for transmission to the transport
803 * @param h handle to the transport service
804 * @param size number of bytes to be transmitted
805 * @param notify function to call to get the content
806 * @param notify_cls closure for notify
809 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h, size_t size,
810 GNUNET_CONNECTION_TransmitReadyNotify notify,
813 struct GNUNET_TRANSPORT_TransmitHandle *th;
815 LOG (GNUNET_ERROR_TYPE_DEBUG, "Control transmit of %u bytes requested\n",
817 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
819 th->notify_cls = notify_cls;
820 th->notify_size = size;
821 GNUNET_CONTAINER_DLL_insert_tail (h->control_head, h->control_tail, th);
822 schedule_transmission (h);
827 * Transmit START message to service.
830 * @param size number of bytes available in buf
831 * @param buf where to copy the message
832 * @return number of bytes copied to buf
835 send_start (void *cls, size_t size, void *buf)
837 struct GNUNET_TRANSPORT_Handle *h = cls;
838 struct StartMessage s;
843 /* Can only be shutdown, just give up */
844 LOG (GNUNET_ERROR_TYPE_DEBUG,
845 "Shutdown while trying to transmit `%s' request.\n", "START");
848 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "START");
849 GNUNET_assert (size >= sizeof (struct StartMessage));
850 s.header.size = htons (sizeof (struct StartMessage));
851 s.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
857 s.options = htonl (options);
859 memcpy (buf, &s, sizeof (struct StartMessage));
860 GNUNET_CLIENT_receive (h->client, &demultiplexer, h,
861 GNUNET_TIME_UNIT_FOREVER_REL);
862 return sizeof (struct StartMessage);
867 * Try again to connect to transport service.
869 * @param cls the handle to the transport service
870 * @param tc scheduler context
873 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
875 struct GNUNET_TRANSPORT_Handle *h = cls;
877 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
878 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
880 /* shutdown, just give up */
883 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n");
884 GNUNET_assert (h->client == NULL);
885 GNUNET_assert (h->control_head == NULL);
886 GNUNET_assert (h->control_tail == NULL);
887 h->client = GNUNET_CLIENT_connect ("transport", h->cfg);
888 GNUNET_assert (h->client != NULL);
889 schedule_control_transmit (h, sizeof (struct StartMessage), &send_start, h);
894 * Function that will schedule the job that will try
895 * to connect us again to the client.
897 * @param h transport service to reconnect
900 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
902 struct GNUNET_TRANSPORT_TransmitHandle *th;
904 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
907 GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
910 if (NULL != h->client)
912 GNUNET_CLIENT_disconnect (h->client);
915 /* Forget about all neighbours that we used to be connected to */
916 GNUNET_CONTAINER_multihashmap_iterate (h->neighbours, &neighbour_delete, h);
917 if (h->quota_task != GNUNET_SCHEDULER_NO_TASK)
919 GNUNET_SCHEDULER_cancel (h->quota_task);
920 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
922 while ((NULL != (th = h->control_head)))
924 GNUNET_CONTAINER_DLL_remove (h->control_head, h->control_tail, th);
925 th->notify (th->notify_cls, 0, NULL);
928 LOG (GNUNET_ERROR_TYPE_DEBUG,
929 "Scheduling task to reconnect to transport service in %llu ms.\n",
930 h->reconnect_delay.rel_value);
932 GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
933 if (h->reconnect_delay.rel_value == 0)
935 h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
939 h->reconnect_delay = GNUNET_TIME_relative_multiply (h->reconnect_delay, 2);
941 GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS, h->reconnect_delay);
947 * Send REQUEST_CONNECT message to the service.
949 * @param cls the 'struct GNUNET_PeerIdentity'
950 * @param size number of bytes available in buf
951 * @param buf where to copy the message
952 * @return number of bytes copied to buf
955 send_try_connect (void *cls, size_t size, void *buf)
957 struct GNUNET_PeerIdentity *pid = cls;
958 struct TransportRequestConnectMessage msg;
965 LOG (GNUNET_ERROR_TYPE_DEBUG,
966 "Transmitting `%s' request with respect to `%4s'.\n", "REQUEST_CONNECT",
968 GNUNET_assert (size >= sizeof (struct TransportRequestConnectMessage));
969 msg.header.size = htons (sizeof (struct TransportRequestConnectMessage));
970 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT);
971 msg.reserved = htonl (0);
973 memcpy (buf, &msg, sizeof (msg));
975 return sizeof (struct TransportRequestConnectMessage);
980 * Ask the transport service to establish a connection to
983 * @param handle connection to transport service
984 * @param target who we should try to connect to
987 GNUNET_TRANSPORT_try_connect (struct GNUNET_TRANSPORT_Handle *handle,
988 const struct GNUNET_PeerIdentity *target)
990 struct GNUNET_PeerIdentity *pid;
992 if (NULL == handle->client)
994 pid = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
996 schedule_control_transmit (handle,
997 sizeof (struct TransportRequestConnectMessage),
998 &send_try_connect, pid);
1002 struct SendHelloContext
1004 GNUNET_SCHEDULER_Task cont;
1008 struct GNUNET_MessageHeader *msg;
1012 * Send HELLO message to the service.
1014 * @param cls the HELLO message to send
1015 * @param size number of bytes available in buf
1016 * @param buf where to copy the message
1017 * @return number of bytes copied to buf
1020 send_hello (void *cls, size_t size, void *buf)
1022 struct SendHelloContext *shc = cls;
1023 struct GNUNET_MessageHeader *msg = shc->msg;
1025 struct GNUNET_SCHEDULER_TaskContext tc;
1026 tc.read_ready = NULL;
1027 tc.write_ready = NULL;
1028 tc.reason = GNUNET_SCHEDULER_REASON_TIMEOUT;
1032 LOG (GNUNET_ERROR_TYPE_DEBUG,
1033 "Timeout while trying to transmit `%s' request.\n", "HELLO");
1034 if (NULL != shc->cont)
1035 shc->cont (shc->cls, &tc);
1040 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "HELLO");
1041 ssize = ntohs (msg->size);
1042 GNUNET_assert (size >= ssize);
1043 memcpy (buf, msg, ssize);
1045 tc.reason = GNUNET_SCHEDULER_REASON_READ_READY;
1046 if (NULL != shc->cont)
1047 shc->cont (shc->cls, &tc);
1054 * Offer the transport service the HELLO of another peer. Note that
1055 * the transport service may just ignore this message if the HELLO is
1056 * malformed or useless due to our local configuration.
1058 * @param handle connection to transport service
1059 * @param hello the hello message
1060 * @param cont continuation to call when HELLO has been sent,
1061 * tc reason GNUNET_SCHEDULER_REASON_TIMEOUT for fail
1062 * tc reasong GNUNET_SCHEDULER_REASON_READY for success
1063 * @param cls closure for continuation
1067 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
1068 const struct GNUNET_MessageHeader *hello,
1069 GNUNET_SCHEDULER_Task cont, void *cls)
1072 struct GNUNET_PeerIdentity peer;
1073 struct GNUNET_MessageHeader *msg;
1074 struct SendHelloContext * shc;
1075 struct GNUNET_SCHEDULER_TaskContext tc;
1077 tc.read_ready = NULL;
1078 tc.write_ready = NULL;
1079 tc.reason = GNUNET_SCHEDULER_REASON_TIMEOUT;
1081 if (NULL == handle->client)
1087 GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
1088 size = ntohs (hello->size);
1089 GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
1091 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hello, &peer))
1099 msg = GNUNET_malloc (size);
1100 memcpy (msg, hello, size);
1101 LOG (GNUNET_ERROR_TYPE_DEBUG,
1102 "Offering `%s' message of `%4s' to transport for validation.\n", "HELLO",
1103 GNUNET_i2s (&peer));
1104 shc = GNUNET_malloc (sizeof (struct SendHelloContext));
1108 schedule_control_transmit (handle, size, &send_hello, shc);
1113 * Obtain the HELLO message for this peer.
1115 * @param handle connection to transport service
1116 * @param rec function to call with the HELLO, sender will be our peer
1117 * identity; message and sender will be NULL on timeout
1118 * (handshake with transport service pending/failed).
1119 * cost estimate will be 0.
1120 * @param rec_cls closure for rec
1121 * @return handle to cancel the operation
1123 struct GNUNET_TRANSPORT_GetHelloHandle *
1124 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
1125 GNUNET_TRANSPORT_HelloUpdateCallback rec,
1128 struct GNUNET_TRANSPORT_GetHelloHandle *hwl;
1130 hwl = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_GetHelloHandle));
1132 hwl->rec_cls = rec_cls;
1133 hwl->handle = handle;
1134 GNUNET_CONTAINER_DLL_insert (handle->hwl_head, handle->hwl_tail, hwl);
1135 if (handle->my_hello != NULL)
1136 rec (rec_cls, (const struct GNUNET_MessageHeader *) handle->my_hello);
1142 * Stop receiving updates about changes to our HELLO message.
1144 * @param ghh handle to cancel
1147 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_GetHelloHandle *ghh)
1149 struct GNUNET_TRANSPORT_Handle *handle = ghh->handle;
1151 GNUNET_CONTAINER_DLL_remove (handle->hwl_head, handle->hwl_tail, ghh);
1157 * Connect to the transport service. Note that the connection may
1158 * complete (or fail) asynchronously.
1160 * @param cfg configuration to use
1161 * @param self our own identity (API should check that it matches
1162 * the identity found by transport), or NULL (no check)
1163 * @param cls closure for the callbacks
1164 * @param rec receive function to call
1165 * @param nc function to call on connect events
1166 * @param nd function to call on disconnect events
1168 struct GNUNET_TRANSPORT_Handle *
1169 GNUNET_TRANSPORT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1170 const struct GNUNET_PeerIdentity *self, void *cls,
1171 GNUNET_TRANSPORT_ReceiveCallback rec,
1172 GNUNET_TRANSPORT_NotifyConnect nc,
1173 GNUNET_TRANSPORT_NotifyDisconnect nd)
1175 struct GNUNET_TRANSPORT_Handle *ret;
1177 ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Handle));
1181 ret->check_self = GNUNET_YES;
1188 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1190 GNUNET_CONTAINER_multihashmap_create (STARTING_NEIGHBOURS_SIZE);
1192 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1193 ret->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, ret);
1199 * Disconnect from the transport service.
1201 * @param handle handle to the service as returned from GNUNET_TRANSPORT_connect
1204 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1206 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transport disconnect called!\n");
1207 /* this disconnects all neighbours... */
1208 if (handle->reconnect_task == GNUNET_SCHEDULER_NO_TASK)
1209 disconnect_and_schedule_reconnect (handle);
1210 /* and now we stop trying to connect again... */
1211 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1213 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1214 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1216 GNUNET_CONTAINER_multihashmap_destroy (handle->neighbours);
1217 handle->neighbours = NULL;
1218 if (handle->quota_task != GNUNET_SCHEDULER_NO_TASK)
1220 GNUNET_SCHEDULER_cancel (handle->quota_task);
1221 handle->quota_task = GNUNET_SCHEDULER_NO_TASK;
1223 GNUNET_free_non_null (handle->my_hello);
1224 handle->my_hello = NULL;
1225 GNUNET_assert (handle->hwl_head == NULL);
1226 GNUNET_assert (handle->hwl_tail == NULL);
1227 GNUNET_CONTAINER_heap_destroy (handle->ready_heap);
1228 handle->ready_heap = NULL;
1229 GNUNET_free (handle);
1234 * Check if we could queue a message of the given size for
1235 * transmission. The transport service will take both its
1236 * internal buffers and bandwidth limits imposed by the
1237 * other peer into consideration when answering this query.
1239 * @param handle connection to transport service
1240 * @param target who should receive the message
1241 * @param size how big is the message we want to transmit?
1242 * @param priority how important is the message?
1243 * @param timeout after how long should we give up (and call
1244 * notify with buf NULL and size 0)?
1245 * @param notify function to call when we are ready to
1246 * send such a message
1247 * @param notify_cls closure for notify
1248 * @return NULL if someone else is already waiting to be notified
1249 * non-NULL if the notify callback was queued (can be used to cancel
1250 * using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1252 struct GNUNET_TRANSPORT_TransmitHandle *
1253 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle *handle,
1254 const struct GNUNET_PeerIdentity
1255 *target, size_t size, uint32_t priority,
1256 struct GNUNET_TIME_Relative timeout,
1257 GNUNET_CONNECTION_TransmitReadyNotify
1258 notify, void *notify_cls)
1260 struct Neighbour *n;
1261 struct GNUNET_TRANSPORT_TransmitHandle *th;
1262 struct GNUNET_TIME_Relative delay;
1264 n = neighbour_find (handle, target);
1267 /* use GNUNET_TRANSPORT_try_connect first, only use this function
1268 * once a connection has been established */
1274 /* attempt to send two messages at the same time to the same peer */
1278 GNUNET_assert (NULL == n->hn);
1279 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
1281 th->notify = notify;
1282 th->notify_cls = notify_cls;
1283 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1284 th->notify_size = size;
1285 th->priority = priority;
1287 /* calculate when our transmission should be ready */
1288 delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker, size);
1289 if (delay.rel_value > timeout.rel_value)
1290 delay.rel_value = 0; /* notify immediately (with failure) */
1291 LOG (GNUNET_ERROR_TYPE_DEBUG,
1292 "Bandwidth tracker allows next transmission to peer %s in %llu ms\n",
1293 GNUNET_i2s (target), (unsigned long long) delay.rel_value);
1294 n->hn = GNUNET_CONTAINER_heap_insert (handle->ready_heap, n, delay.rel_value);
1295 schedule_transmission (handle);
1301 * Cancel the specified transmission-ready notification.
1303 * @param th handle returned from GNUNET_TRANSPORT_notify_transmit_ready
1306 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
1307 GNUNET_TRANSPORT_TransmitHandle
1310 struct Neighbour *n;
1312 GNUNET_assert (NULL == th->next);
1313 GNUNET_assert (NULL == th->prev);
1315 GNUNET_assert (th == n->th);
1319 GNUNET_CONTAINER_heap_remove_node (n->hn);
1324 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != th->timeout_task);
1325 GNUNET_SCHEDULER_cancel (th->timeout_task);
1326 th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1332 /* end of transport_api.c */