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?
150 * Sending consumed more bytes on wire than payload was announced
151 * This overhead is added to the delay of next sending operation
153 size_t traffic_overhead;
158 * Linked list of functions to call whenever our HELLO is updated.
160 struct GNUNET_TRANSPORT_GetHelloHandle
164 * This is a doubly linked list.
166 struct GNUNET_TRANSPORT_GetHelloHandle *next;
169 * This is a doubly linked list.
171 struct GNUNET_TRANSPORT_GetHelloHandle *prev;
176 struct GNUNET_TRANSPORT_Handle *handle;
179 * Callback to call once we got our HELLO.
181 GNUNET_TRANSPORT_HelloUpdateCallback rec;
191 * Linked list for all try-connect requests
193 struct GNUNET_TRANSPORT_TryConnectHandle
195 struct GNUNET_TRANSPORT_TryConnectHandle *prev;
196 struct GNUNET_TRANSPORT_TryConnectHandle *next;
198 struct GNUNET_PeerIdentity pid;
200 struct GNUNET_TRANSPORT_Handle *th;
201 struct GNUNET_TRANSPORT_TransmitHandle *tth;
202 GNUNET_TRANSPORT_TryConnectCallback cb;
208 * Linked list for all try-connect requests
210 struct GNUNET_TRANSPORT_OfferHelloHandle
212 struct GNUNET_TRANSPORT_OfferHelloHandle *prev;
213 struct GNUNET_TRANSPORT_OfferHelloHandle *next;
215 struct GNUNET_TRANSPORT_Handle *th;
217 struct GNUNET_TRANSPORT_TransmitHandle *tth;
218 GNUNET_SCHEDULER_Task cont;
222 struct GNUNET_MessageHeader *msg;
227 * Handle for the transport service (includes all of the
228 * state for the transport service).
230 struct GNUNET_TRANSPORT_Handle
234 * Closure for the callbacks.
239 * Function to call for received data.
241 GNUNET_TRANSPORT_ReceiveCallback rec;
244 * function to call on connect events
246 GNUNET_TRANSPORT_NotifyConnect nc_cb;
249 * function to call on disconnect events
251 GNUNET_TRANSPORT_NotifyDisconnect nd_cb;
254 * Head of DLL of control messages.
256 struct GNUNET_TRANSPORT_TransmitHandle *control_head;
259 * Tail of DLL of control messages.
261 struct GNUNET_TRANSPORT_TransmitHandle *control_tail;
264 * The current HELLO message for this peer. Updated
265 * whenever transports change their addresses.
267 struct GNUNET_HELLO_Message *my_hello;
270 * My client connection to the transport service.
272 struct GNUNET_CLIENT_Connection *client;
275 * Handle to our registration with the client for notification.
277 struct GNUNET_CLIENT_TransmitHandle *cth;
280 * Linked list of pending requests for our HELLO.
282 struct GNUNET_TRANSPORT_GetHelloHandle *hwl_head;
285 * Linked list of pending requests for our HELLO.
287 struct GNUNET_TRANSPORT_GetHelloHandle *hwl_tail;
290 * Linked list of pending try connect requests head
292 struct GNUNET_TRANSPORT_TryConnectHandle *tc_head;
295 * Linked list of pending try connect requests tail
297 struct GNUNET_TRANSPORT_TryConnectHandle *tc_tail;
300 * Linked list of pending offer HELLO requests head
302 struct GNUNET_TRANSPORT_OfferHelloHandle *oh_head;
305 * Linked list of pending offer HELLO requests tail
307 struct GNUNET_TRANSPORT_OfferHelloHandle *oh_tail;
312 const struct GNUNET_CONFIGURATION_Handle *cfg;
315 * Hash map of the current connected neighbours of this peer.
316 * Maps peer identities to 'struct Neighbour' entries.
318 struct GNUNET_CONTAINER_MultiHashMap *neighbours;
321 * Heap sorting peers with pending messages by the timestamps that
322 * specify when we could next send a message to the respective peer.
323 * Excludes control messages (which can always go out immediately).
324 * Maps time stamps to 'struct Neighbour' entries.
326 struct GNUNET_CONTAINER_Heap *ready_heap;
329 * Peer identity as assumed by this process, or all zeros.
331 struct GNUNET_PeerIdentity self;
334 * ID of the task trying to reconnect to the service.
336 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
339 * ID of the task trying to trigger transmission for a peer while
340 * maintaining bandwidth quotas. In use if there are no control
341 * messages and the smallest entry in the 'ready_heap' has a time
342 * stamp in the future.
344 GNUNET_SCHEDULER_TaskIdentifier quota_task;
347 * Delay until we try to reconnect.
349 struct GNUNET_TIME_Relative reconnect_delay;
352 * Should we check that 'self' matches what the service thinks?
353 * (if GNUNET_NO, then 'self' is all zeros!).
358 * Reconnect in progress
366 * Schedule the task to send one message, either from the control
367 * list or the peer message queues to the service.
369 * @param h transport service to schedule a transmission for
372 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h);
376 * Function that will schedule the job that will try
377 * to connect us again to the client.
379 * @param h transport service to reconnect
382 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h);
386 * Get the neighbour list entry for the given peer
388 * @param h our context
389 * @param peer peer to look up
390 * @return NULL if no such peer entry exists
392 static struct Neighbour *
393 neighbour_find (struct GNUNET_TRANSPORT_Handle *h,
394 const struct GNUNET_PeerIdentity *peer)
396 return GNUNET_CONTAINER_multihashmap_get (h->neighbours, &peer->hashPubKey);
401 * Add neighbour to our list
403 * @return NULL if this API is currently disconnecting from the service
405 static struct Neighbour *
406 neighbour_add (struct GNUNET_TRANSPORT_Handle *h,
407 const struct GNUNET_PeerIdentity *pid)
411 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating entry for neighbour `%4s'.\n",
413 n = GNUNET_malloc (sizeof (struct Neighbour));
416 n->is_ready = GNUNET_YES;
417 n->traffic_overhead = 0;
418 GNUNET_BANDWIDTH_tracker_init (&n->out_tracker,
419 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
420 MAX_BANDWIDTH_CARRY_S);
421 GNUNET_assert (GNUNET_OK ==
422 GNUNET_CONTAINER_multihashmap_put (h->neighbours,
423 &n->id.hashPubKey, n,
424 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
430 * Iterator over hash map entries, for deleting state of a neighbour.
432 * @param cls the 'struct GNUNET_TRANSPORT_Handle*'
433 * @param key peer identity
434 * @param value value in the hash map, the neighbour entry to delete
435 * @return GNUNET_YES if we should continue to
440 neighbour_delete (void *cls, const struct GNUNET_HashCode * key, void *value)
442 struct GNUNET_TRANSPORT_Handle *handle = cls;
443 struct Neighbour *n = value;
445 if (NULL != handle->nd_cb)
446 handle->nd_cb (handle->cls, &n->id);
447 GNUNET_assert (NULL == n->th);
448 GNUNET_assert (NULL == n->hn);
449 GNUNET_assert (GNUNET_YES ==
450 GNUNET_CONTAINER_multihashmap_remove (handle->neighbours, key,
458 * Function we use for handling incoming messages.
460 * @param cls closure (struct GNUNET_TRANSPORT_Handle *)
461 * @param msg message received, NULL on timeout or fatal error
464 demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
466 struct GNUNET_TRANSPORT_Handle *h = cls;
467 const struct DisconnectInfoMessage *dim;
468 const struct ConnectInfoMessage *cim;
469 const struct InboundMessage *im;
470 const struct GNUNET_MessageHeader *imm;
471 const struct SendOkMessage *okm;
472 const struct QuotaSetMessage *qm;
473 const struct GNUNET_ATS_Information *ats;
474 struct GNUNET_TRANSPORT_GetHelloHandle *hwl;
475 struct GNUNET_TRANSPORT_GetHelloHandle *next_hwl;
477 struct GNUNET_PeerIdentity me;
481 uint32_t bytes_physical;
483 GNUNET_assert (h->client != NULL);
486 LOG (GNUNET_ERROR_TYPE_DEBUG,
487 "Error receiving from transport service, disconnecting temporarily.\n");
488 disconnect_and_schedule_reconnect (h);
491 GNUNET_CLIENT_receive (h->client, &demultiplexer, h,
492 GNUNET_TIME_UNIT_FOREVER_REL);
493 size = ntohs (msg->size);
494 switch (ntohs (msg->type))
496 case GNUNET_MESSAGE_TYPE_HELLO:
498 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg, &me))
503 LOG (GNUNET_ERROR_TYPE_DEBUG,
504 "Receiving (my own) `%s' message, I am `%4s'.\n", "HELLO",
506 GNUNET_free_non_null (h->my_hello);
508 if (size < sizeof (struct GNUNET_MessageHeader))
513 h->my_hello = GNUNET_malloc (size);
514 memcpy (h->my_hello, msg, size);
518 next_hwl = hwl->next;
519 hwl->rec (hwl->rec_cls,
520 (const struct GNUNET_MessageHeader *) h->my_hello);
524 case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
525 if (size < sizeof (struct ConnectInfoMessage))
530 cim = (const struct ConnectInfoMessage *) msg;
531 ats_count = ntohl (cim->ats_count);
533 sizeof (struct ConnectInfoMessage) +
534 ats_count * sizeof (struct GNUNET_ATS_Information))
539 ats = (const struct GNUNET_ATS_Information *) &cim[1];
540 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message for `%4s'.\n",
541 "CONNECT", GNUNET_i2s (&cim->id));
542 n = neighbour_find (h, &cim->id);
548 n = neighbour_add (h, &cim->id);
549 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message for `%4s' with quota %u\n",
550 "CONNECT", GNUNET_i2s (&cim->id), ntohl (cim->quota_out.value__));
551 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker, cim->quota_out);
552 if (h->nc_cb != NULL)
553 h->nc_cb (h->cls, &n->id, ats, ats_count);
555 case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
556 if (size != sizeof (struct DisconnectInfoMessage))
561 dim = (const struct DisconnectInfoMessage *) msg;
562 GNUNET_break (ntohl (dim->reserved) == 0);
563 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message for `%4s'.\n",
564 "DISCONNECT", GNUNET_i2s (&dim->peer));
565 n = neighbour_find (h, &dim->peer);
571 neighbour_delete (h, &dim->peer.hashPubKey, n);
573 case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
574 if (size != sizeof (struct SendOkMessage))
579 okm = (const struct SendOkMessage *) msg;
580 bytes_msg = ntohl (okm->bytes_msg);
581 bytes_physical = ntohl (okm->bytes_physical);
582 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message, transmission %s.\n",
583 "SEND_OK", ntohl (okm->success) == GNUNET_OK ? "succeeded" : "failed");
585 n = neighbour_find (h, &okm->peer);
589 if (bytes_physical >= bytes_msg)
591 LOG (GNUNET_ERROR_TYPE_DEBUG, "Overhead for %u byte message: %u \n",
592 bytes_msg, bytes_physical - bytes_msg);
593 n->traffic_overhead += bytes_physical - bytes_msg;
595 GNUNET_break (GNUNET_NO == n->is_ready);
596 n->is_ready = GNUNET_YES;
597 if ((n->th != NULL) && (n->hn == NULL))
599 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->th->timeout_task);
600 GNUNET_SCHEDULER_cancel (n->th->timeout_task);
601 n->th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
602 /* we've been waiting for this (congestion, not quota,
603 * caused delayed transmission) */
604 n->hn = GNUNET_CONTAINER_heap_insert (h->ready_heap, n, 0);
605 schedule_transmission (h);
608 case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
609 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message.\n", "RECV");
611 sizeof (struct InboundMessage) + sizeof (struct GNUNET_MessageHeader))
616 im = (const struct InboundMessage *) msg;
617 ats_count = ntohl (im->ats_count);
618 ats = (const struct GNUNET_ATS_Information *) &im[1];
619 imm = (const struct GNUNET_MessageHeader *) &ats[ats_count];
620 if (ntohs (imm->size) + sizeof (struct InboundMessage) +
621 ats_count * sizeof (struct GNUNET_ATS_Information) != size)
626 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %u from `%4s'.\n",
627 ntohs (imm->type), GNUNET_i2s (&im->peer));
628 n = neighbour_find (h, &im->peer);
635 h->rec (h->cls, &im->peer, imm, ats, ats_count);
637 case GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA:
638 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message.\n", "SET_QUOTA");
639 if (size != sizeof (struct QuotaSetMessage))
644 qm = (const struct QuotaSetMessage *) msg;
645 n = neighbour_find (h, &qm->peer);
648 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message for `%4s' with quota %u\n",
649 "SET_QUOTA", GNUNET_i2s (&qm->peer), ntohl (qm->quota.value__));
650 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker, qm->quota);
653 LOG (GNUNET_ERROR_TYPE_ERROR,
654 _("Received unexpected message of type %u in %s:%u\n"),
655 ntohs (msg->type), __FILE__, __LINE__);
663 * A transmission request could not be satisfied because of
664 * network congestion. Notify the initiator and clean up.
666 * @param cls the 'struct GNUNET_TRANSPORT_TransmitHandle'
667 * @param tc scheduler context
670 timeout_request_due_to_congestion (void *cls,
671 const struct GNUNET_SCHEDULER_TaskContext
674 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
675 struct Neighbour *n = th->neighbour;
677 n->th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
678 GNUNET_assert (th == n->th);
679 GNUNET_assert (NULL == n->hn);
681 th->notify (th->notify_cls, 0, NULL);
687 * Transmit message(s) to service.
689 * @param cls handle to transport
690 * @param size number of bytes available in buf
691 * @param buf where to copy the message
692 * @return number of bytes copied to buf
695 transport_notify_ready (void *cls, size_t size, void *buf)
697 struct GNUNET_TRANSPORT_Handle *h = cls;
698 struct GNUNET_TRANSPORT_TransmitHandle *th;
701 struct OutboundMessage obm;
706 GNUNET_assert (NULL != h->client);
710 /* transmission failed */
711 disconnect_and_schedule_reconnect (h);
717 /* first send control messages */
718 while ((NULL != (th = h->control_head)) && (th->notify_size <= size))
720 GNUNET_CONTAINER_DLL_remove (h->control_head, h->control_tail, th);
721 nret = th->notify (th->notify_cls, size, &cbuf[ret]);
722 LOG (GNUNET_ERROR_TYPE_DEBUG, "Added %u bytes of control message at %u\n",
729 /* then, if possible and no control messages pending, send data messages */
730 while ((NULL == h->control_head) &&
731 (NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap))))
733 if (GNUNET_YES != n->is_ready)
735 /* peer not ready, wait for notification! */
736 GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
738 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == n->th->timeout_task);
739 n->th->timeout_task =
740 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
742 &timeout_request_due_to_congestion,
747 if (th->notify_size + sizeof (struct OutboundMessage) > size)
748 break; /* does not fit */
749 if (GNUNET_BANDWIDTH_tracker_get_delay
750 (&n->out_tracker, th->notify_size).rel_value > 0)
751 break; /* too early */
752 GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
755 n->is_ready = GNUNET_NO;
756 GNUNET_assert (size >= sizeof (struct OutboundMessage));
758 th->notify (th->notify_cls, size - sizeof (struct OutboundMessage),
759 &cbuf[ret + sizeof (struct OutboundMessage)]);
760 GNUNET_assert (mret <= size - sizeof (struct OutboundMessage));
763 GNUNET_assert (mret + sizeof (struct OutboundMessage) <
764 GNUNET_SERVER_MAX_MESSAGE_SIZE);
765 obm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
766 obm.header.size = htons (mret + sizeof (struct OutboundMessage));
767 obm.priority = htonl (th->priority);
769 GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining
772 memcpy (&cbuf[ret], &obm, sizeof (struct OutboundMessage));
773 ret += (mret + sizeof (struct OutboundMessage));
774 size -= (mret + sizeof (struct OutboundMessage));
775 GNUNET_BANDWIDTH_tracker_consume (&n->out_tracker, mret);
779 /* if there are more pending messages, try to schedule those */
780 schedule_transmission (h);
781 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting %u bytes to transport service\n",
788 * Schedule the task to send one message, either from the control
789 * list or the peer message queues to the service.
791 * @param cls transport service to schedule a transmission for
792 * @param tc scheduler context
795 schedule_transmission_task (void *cls,
796 const struct GNUNET_SCHEDULER_TaskContext *tc)
798 struct GNUNET_TRANSPORT_Handle *h = cls;
800 struct GNUNET_TRANSPORT_TransmitHandle *th;
803 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
804 GNUNET_assert (NULL != h->client);
805 /* destroy all requests that have timed out */
806 while ((NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap))) &&
807 (GNUNET_TIME_absolute_get_remaining (n->th->timeout).rel_value == 0))
809 /* notify client that the request could not be satisfied within
810 * the given time constraints */
813 GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
815 LOG (GNUNET_ERROR_TYPE_DEBUG,
816 "Signalling timeout for transmission to peer %s due to congestion\n",
817 GNUNET_i2s (&n->id));
818 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
823 if (NULL != h->control_head)
825 size = h->control_head->notify_size;
829 n = GNUNET_CONTAINER_heap_peek (h->ready_heap);
831 return; /* no pending messages */
832 size = n->th->notify_size + sizeof (struct OutboundMessage);
834 LOG (GNUNET_ERROR_TYPE_DEBUG, "Calling notify_transmit_ready\n");
836 GNUNET_CLIENT_notify_transmit_ready (h->client, size,
837 GNUNET_TIME_UNIT_FOREVER_REL,
838 GNUNET_NO, &transport_notify_ready,
840 GNUNET_assert (NULL != h->cth);
845 * Schedule the task to send one message, either from the control
846 * list or the peer message queues to the service.
848 * @param h transport service to schedule a transmission for
851 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
853 struct GNUNET_TIME_Relative delay;
856 GNUNET_assert (NULL != h->client);
857 if (h->quota_task != GNUNET_SCHEDULER_NO_TASK)
859 GNUNET_SCHEDULER_cancel (h->quota_task);
860 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
862 if (NULL != h->control_head)
863 delay = GNUNET_TIME_UNIT_ZERO;
864 else if (NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap)))
867 GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
868 n->th->notify_size + n->traffic_overhead);
869 n->traffic_overhead = 0;
872 return; /* no work to be done */
873 LOG (GNUNET_ERROR_TYPE_DEBUG,
874 "Scheduling next transmission to service in %llu ms\n",
875 (unsigned long long) delay.rel_value);
877 GNUNET_SCHEDULER_add_delayed (delay, &schedule_transmission_task, h);
882 * Queue control request for transmission to the transport
885 * @param h handle to the transport service
886 * @param size number of bytes to be transmitted
887 * @param notify function to call to get the content
888 * @param notify_cls closure for notify
889 * @return a GNUNET_TRANSPORT_TransmitHandle
891 static struct GNUNET_TRANSPORT_TransmitHandle *
892 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h, size_t size,
893 GNUNET_CONNECTION_TransmitReadyNotify notify,
896 struct GNUNET_TRANSPORT_TransmitHandle *th;
898 LOG (GNUNET_ERROR_TYPE_DEBUG, "Control transmit of %u bytes requested\n",
900 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
902 th->notify_cls = notify_cls;
903 th->notify_size = size;
904 GNUNET_CONTAINER_DLL_insert_tail (h->control_head, h->control_tail, th);
905 schedule_transmission (h);
911 * Transmit START message to service.
914 * @param size number of bytes available in buf
915 * @param buf where to copy the message
916 * @return number of bytes copied to buf
919 send_start (void *cls, size_t size, void *buf)
921 struct GNUNET_TRANSPORT_Handle *h = cls;
922 struct StartMessage s;
927 /* Can only be shutdown, just give up */
928 LOG (GNUNET_ERROR_TYPE_DEBUG,
929 "Shutdown while trying to transmit `%s' request.\n", "START");
932 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "START");
933 GNUNET_assert (size >= sizeof (struct StartMessage));
934 s.header.size = htons (sizeof (struct StartMessage));
935 s.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
941 s.options = htonl (options);
943 memcpy (buf, &s, sizeof (struct StartMessage));
944 GNUNET_CLIENT_receive (h->client, &demultiplexer, h,
945 GNUNET_TIME_UNIT_FOREVER_REL);
946 return sizeof (struct StartMessage);
951 * Try again to connect to transport service.
953 * @param cls the handle to the transport service
954 * @param tc scheduler context
957 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
959 struct GNUNET_TRANSPORT_Handle *h = cls;
961 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
962 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
964 /* shutdown, just give up */
967 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n");
968 GNUNET_assert (h->client == NULL);
969 GNUNET_assert (h->control_head == NULL);
970 GNUNET_assert (h->control_tail == NULL);
971 h->client = GNUNET_CLIENT_connect ("transport", h->cfg);
972 GNUNET_assert (h->client != NULL);
973 schedule_control_transmit (h, sizeof (struct StartMessage), &send_start, h);
978 * Function that will schedule the job that will try
979 * to connect us again to the client.
981 * @param h transport service to reconnect
984 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
986 struct GNUNET_TRANSPORT_TransmitHandle *th;
988 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
991 GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
994 if (NULL != h->client)
996 GNUNET_CLIENT_disconnect (h->client);
999 /* Forget about all neighbours that we used to be connected to */
1000 GNUNET_CONTAINER_multihashmap_iterate (h->neighbours, &neighbour_delete, h);
1001 if (h->quota_task != GNUNET_SCHEDULER_NO_TASK)
1003 GNUNET_SCHEDULER_cancel (h->quota_task);
1004 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
1006 while ((NULL != (th = h->control_head)))
1008 GNUNET_CONTAINER_DLL_remove (h->control_head, h->control_tail, th);
1009 th->notify (th->notify_cls, 0, NULL);
1012 LOG (GNUNET_ERROR_TYPE_DEBUG,
1013 "Scheduling task to reconnect to transport service in %llu ms.\n",
1014 h->reconnect_delay.rel_value);
1016 GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
1017 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
1022 * Cancel control request for transmission to the transport service.
1024 * @param th handle to the transport service
1025 * @param tth transmit handle to cancel
1028 cancel_control_transmit (struct GNUNET_TRANSPORT_Handle *th, struct GNUNET_TRANSPORT_TransmitHandle *tth)
1030 GNUNET_assert (NULL != th);
1031 GNUNET_assert (NULL != tth);
1033 LOG (GNUNET_ERROR_TYPE_DEBUG, "Canceling transmit of contral transmission requested\n");
1035 GNUNET_CONTAINER_DLL_remove (th->control_head, th->control_tail, tth);
1042 * Send REQUEST_CONNECT message to the service.
1044 * @param cls the 'struct GNUNET_PeerIdentity'
1045 * @param size number of bytes available in buf
1046 * @param buf where to copy the message
1047 * @return number of bytes copied to buf
1050 send_try_connect (void *cls, size_t size, void *buf)
1052 struct GNUNET_TRANSPORT_TryConnectHandle *tch = cls;
1053 struct TransportRequestConnectMessage msg;
1057 if (NULL != tch->cb)
1058 tch->cb (tch->cb_cls, GNUNET_SYSERR);
1059 GNUNET_CONTAINER_DLL_remove (tch->th->tc_head, tch->th->tc_tail, tch);
1063 LOG (GNUNET_ERROR_TYPE_DEBUG,
1064 "Transmitting `%s' request with respect to `%4s'.\n", "REQUEST_CONNECT",
1065 GNUNET_i2s (&tch->pid));
1066 GNUNET_assert (size >= sizeof (struct TransportRequestConnectMessage));
1067 msg.header.size = htons (sizeof (struct TransportRequestConnectMessage));
1068 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT);
1069 msg.reserved = htonl (0);
1070 msg.peer = tch->pid;
1071 memcpy (buf, &msg, sizeof (msg));
1072 if (NULL != tch->cb)
1073 tch->cb (tch->cb_cls, GNUNET_OK);
1074 GNUNET_CONTAINER_DLL_remove (tch->th->tc_head, tch->th->tc_tail, tch);
1076 return sizeof (struct TransportRequestConnectMessage);
1080 * Ask the transport service to establish a connection to
1083 * @param handle connection to transport service
1084 * @param target who we should try to connect to
1085 * @param cb callback to be called when request was transmitted to transport
1087 * @param cb_cls closure for the callback
1088 * @return a GNUNET_TRANSPORT_TryConnectHandle handle or
1089 * NULL on failure (cb will not be called)
1091 struct GNUNET_TRANSPORT_TryConnectHandle *
1092 GNUNET_TRANSPORT_try_connect (struct GNUNET_TRANSPORT_Handle *handle,
1093 const struct GNUNET_PeerIdentity *target,
1094 GNUNET_TRANSPORT_TryConnectCallback cb,
1097 struct GNUNET_TRANSPORT_TryConnectHandle *tch = NULL;
1099 if (NULL == handle->client)
1102 tch = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TryConnectHandle));
1104 tch->pid = *(target);
1106 tch->cb_cls = cb_cls;
1107 tch->tth = schedule_control_transmit (handle,
1108 sizeof (struct TransportRequestConnectMessage),
1109 &send_try_connect, tch);
1110 GNUNET_CONTAINER_DLL_insert(handle->tc_head, handle->tc_tail, tch);
1116 * Cancel the request to transport to try a connect
1117 * Callback will not be called
1119 * @param tch GNUNET_TRANSPORT_TryConnectHandle handle to cancel
1122 GNUNET_TRANSPORT_try_connect_cancel (struct GNUNET_TRANSPORT_TryConnectHandle *tch)
1124 struct GNUNET_TRANSPORT_Handle *th;
1125 GNUNET_assert (NULL != tch);
1128 cancel_control_transmit (th, tch->tth);
1129 GNUNET_CONTAINER_DLL_remove (th->tc_head, th->tc_tail, tch);
1134 * Send HELLO message to the service.
1136 * @param cls the HELLO message to send
1137 * @param size number of bytes available in buf
1138 * @param buf where to copy the message
1139 * @return number of bytes copied to buf
1142 send_hello (void *cls, size_t size, void *buf)
1144 struct GNUNET_TRANSPORT_OfferHelloHandle *ohh = cls;
1145 struct GNUNET_MessageHeader *msg = ohh->msg;
1147 struct GNUNET_SCHEDULER_TaskContext tc;
1148 tc.read_ready = NULL;
1149 tc.write_ready = NULL;
1150 tc.reason = GNUNET_SCHEDULER_REASON_TIMEOUT;
1154 LOG (GNUNET_ERROR_TYPE_DEBUG,
1155 "Timeout while trying to transmit `%s' request.\n", "HELLO");
1156 if (NULL != ohh->cont)
1157 ohh->cont (ohh->cls, &tc);
1159 GNUNET_CONTAINER_DLL_remove (ohh->th->oh_head, ohh->th->oh_tail, ohh);
1163 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "HELLO");
1164 ssize = ntohs (msg->size);
1165 GNUNET_assert (size >= ssize);
1166 memcpy (buf, msg, ssize);
1168 tc.reason = GNUNET_SCHEDULER_REASON_READ_READY;
1169 if (NULL != ohh->cont)
1170 ohh->cont (ohh->cls, &tc);
1171 GNUNET_CONTAINER_DLL_remove (ohh->th->oh_head, ohh->th->oh_tail, ohh);
1178 * Offer the transport service the HELLO of another peer. Note that
1179 * the transport service may just ignore this message if the HELLO is
1180 * malformed or useless due to our local configuration.
1182 * @param handle connection to transport service
1183 * @param hello the hello message
1184 * @param cont continuation to call when HELLO has been sent,
1185 * tc reason GNUNET_SCHEDULER_REASON_TIMEOUT for fail
1186 * tc reasong GNUNET_SCHEDULER_REASON_READ_READY for success
1187 * @param cls closure for continuation
1188 * @return a GNUNET_TRANSPORT_OfferHelloHandle handle or NULL on failure,
1189 * in case of failure cont will not be called
1192 struct GNUNET_TRANSPORT_OfferHelloHandle *
1193 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
1194 const struct GNUNET_MessageHeader *hello,
1195 GNUNET_SCHEDULER_Task cont, void *cls)
1197 struct GNUNET_TRANSPORT_OfferHelloHandle *ohh;
1198 struct GNUNET_MessageHeader *msg;
1199 struct GNUNET_PeerIdentity peer;
1202 GNUNET_assert (NULL != handle);
1203 GNUNET_assert (NULL != hello);
1205 if (NULL == handle->client)
1208 GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
1209 size = ntohs (hello->size);
1210 GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
1212 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hello, &peer))
1218 msg = GNUNET_malloc (size);
1219 memcpy (msg, hello, size);
1220 LOG (GNUNET_ERROR_TYPE_DEBUG,
1221 "Offering `%s' message of `%4s' to transport for validation.\n", "HELLO",
1222 GNUNET_i2s (&peer));
1224 ohh = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_OfferHelloHandle));
1229 ohh->tth = schedule_control_transmit (handle, size, &send_hello, ohh);
1230 GNUNET_CONTAINER_DLL_insert (handle->oh_head, handle->oh_tail, ohh);
1236 * Cancel the request to transport to offer the HELLO message
1238 * @param ohh the GNUNET_TRANSPORT_OfferHelloHandle to cancel
1241 GNUNET_TRANSPORT_offer_hello_cancel (struct GNUNET_TRANSPORT_OfferHelloHandle *ohh)
1243 struct GNUNET_TRANSPORT_Handle *th = ohh->th;
1244 GNUNET_assert (NULL != ohh);
1246 cancel_control_transmit (ohh->th, ohh->tth);
1247 GNUNET_CONTAINER_DLL_remove (th->oh_head, th->oh_tail, ohh);
1248 GNUNET_free (ohh->msg);
1253 GNUNET_TRANSPORT_check_neighbour_connected (struct GNUNET_TRANSPORT_Handle *handle,
1254 const struct GNUNET_PeerIdentity *peer)
1256 GNUNET_assert (NULL != handle);
1257 GNUNET_assert (NULL != peer);
1259 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(handle->neighbours, &peer->hashPubKey))
1266 * Obtain the HELLO message for this peer.
1268 * @param handle connection to transport service
1269 * @param rec function to call with the HELLO, sender will be our peer
1270 * identity; message and sender will be NULL on timeout
1271 * (handshake with transport service pending/failed).
1272 * cost estimate will be 0.
1273 * @param rec_cls closure for rec
1274 * @return handle to cancel the operation
1276 struct GNUNET_TRANSPORT_GetHelloHandle *
1277 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
1278 GNUNET_TRANSPORT_HelloUpdateCallback rec,
1281 struct GNUNET_TRANSPORT_GetHelloHandle *hwl;
1283 hwl = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_GetHelloHandle));
1285 hwl->rec_cls = rec_cls;
1286 hwl->handle = handle;
1287 GNUNET_CONTAINER_DLL_insert (handle->hwl_head, handle->hwl_tail, hwl);
1288 if (handle->my_hello != NULL)
1289 rec (rec_cls, (const struct GNUNET_MessageHeader *) handle->my_hello);
1295 * Stop receiving updates about changes to our HELLO message.
1297 * @param ghh handle to cancel
1300 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_GetHelloHandle *ghh)
1302 struct GNUNET_TRANSPORT_Handle *handle = ghh->handle;
1304 GNUNET_CONTAINER_DLL_remove (handle->hwl_head, handle->hwl_tail, ghh);
1310 * Connect to the transport service. Note that the connection may
1311 * complete (or fail) asynchronously.
1313 * @param cfg configuration to use
1314 * @param self our own identity (API should check that it matches
1315 * the identity found by transport), or NULL (no check)
1316 * @param cls closure for the callbacks
1317 * @param rec receive function to call
1318 * @param nc function to call on connect events
1319 * @param nd function to call on disconnect events
1321 struct GNUNET_TRANSPORT_Handle *
1322 GNUNET_TRANSPORT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1323 const struct GNUNET_PeerIdentity *self, void *cls,
1324 GNUNET_TRANSPORT_ReceiveCallback rec,
1325 GNUNET_TRANSPORT_NotifyConnect nc,
1326 GNUNET_TRANSPORT_NotifyDisconnect nd)
1328 struct GNUNET_TRANSPORT_Handle *ret;
1330 ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Handle));
1334 ret->check_self = GNUNET_YES;
1341 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1343 GNUNET_CONTAINER_multihashmap_create (STARTING_NEIGHBOURS_SIZE, GNUNET_YES);
1345 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1346 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n");
1347 ret->client = GNUNET_CLIENT_connect ("transport", cfg);
1348 if (ret->client == NULL)
1353 schedule_control_transmit (ret, sizeof (struct StartMessage), &send_start, ret);
1359 * Disconnect from the transport service.
1361 * @param handle handle to the service as returned from GNUNET_TRANSPORT_connect
1364 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1366 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transport disconnect called!\n");
1367 /* this disconnects all neighbours... */
1368 if (handle->reconnect_task == GNUNET_SCHEDULER_NO_TASK)
1369 disconnect_and_schedule_reconnect (handle);
1370 /* and now we stop trying to connect again... */
1371 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1373 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1374 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1376 GNUNET_CONTAINER_multihashmap_destroy (handle->neighbours);
1377 handle->neighbours = NULL;
1378 if (handle->quota_task != GNUNET_SCHEDULER_NO_TASK)
1380 GNUNET_SCHEDULER_cancel (handle->quota_task);
1381 handle->quota_task = GNUNET_SCHEDULER_NO_TASK;
1383 GNUNET_free_non_null (handle->my_hello);
1384 handle->my_hello = NULL;
1385 GNUNET_assert (handle->tc_head == NULL);
1386 GNUNET_assert (handle->tc_tail == NULL);
1387 GNUNET_assert (handle->hwl_head == NULL);
1388 GNUNET_assert (handle->hwl_tail == NULL);
1389 GNUNET_CONTAINER_heap_destroy (handle->ready_heap);
1390 handle->ready_heap = NULL;
1391 GNUNET_free (handle);
1396 * Check if we could queue a message of the given size for
1397 * transmission. The transport service will take both its
1398 * internal buffers and bandwidth limits imposed by the
1399 * other peer into consideration when answering this query.
1401 * @param handle connection to transport service
1402 * @param target who should receive the message
1403 * @param size how big is the message we want to transmit?
1404 * @param priority how important is the message?
1405 * @param timeout after how long should we give up (and call
1406 * notify with buf NULL and size 0)?
1407 * @param notify function to call when we are ready to
1408 * send such a message
1409 * @param notify_cls closure for notify
1410 * @return NULL if someone else is already waiting to be notified
1411 * non-NULL if the notify callback was queued (can be used to cancel
1412 * using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1414 struct GNUNET_TRANSPORT_TransmitHandle *
1415 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle *handle,
1416 const struct GNUNET_PeerIdentity
1417 *target, size_t size, uint32_t priority,
1418 struct GNUNET_TIME_Relative timeout,
1419 GNUNET_CONNECTION_TransmitReadyNotify
1420 notify, void *notify_cls)
1422 struct Neighbour *n;
1423 struct GNUNET_TRANSPORT_TransmitHandle *th;
1424 struct GNUNET_TIME_Relative delay;
1426 n = neighbour_find (handle, target);
1429 /* use GNUNET_TRANSPORT_try_connect first, only use this function
1430 * once a connection has been established */
1436 /* attempt to send two messages at the same time to the same peer */
1440 GNUNET_assert (NULL == n->hn);
1441 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
1443 th->notify = notify;
1444 th->notify_cls = notify_cls;
1445 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1446 th->notify_size = size;
1447 th->priority = priority;
1449 /* calculate when our transmission should be ready */
1450 delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker, size + n->traffic_overhead);
1451 n->traffic_overhead = 0;
1452 if (delay.rel_value > timeout.rel_value)
1453 delay.rel_value = 0; /* notify immediately (with failure) */
1454 LOG (GNUNET_ERROR_TYPE_DEBUG,
1455 "Bandwidth tracker allows next transmission to peer %s in %llu ms\n",
1456 GNUNET_i2s (target), (unsigned long long) delay.rel_value);
1457 n->hn = GNUNET_CONTAINER_heap_insert (handle->ready_heap, n, delay.rel_value);
1458 schedule_transmission (handle);
1464 * Cancel the specified transmission-ready notification.
1466 * @param th handle returned from GNUNET_TRANSPORT_notify_transmit_ready
1469 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
1470 GNUNET_TRANSPORT_TransmitHandle
1473 struct Neighbour *n;
1475 GNUNET_assert (NULL == th->next);
1476 GNUNET_assert (NULL == th->prev);
1478 GNUNET_assert (th == n->th);
1482 GNUNET_CONTAINER_heap_remove_node (n->hn);
1487 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != th->timeout_task);
1488 GNUNET_SCHEDULER_cancel (th->timeout_task);
1489 th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1495 /* end of transport_api.c */