2 This file is part of GNUnet.
3 (C) 2009, 2010 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 2, 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 #include "gnunet_client_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_container_lib.h"
30 #include "gnunet_arm_service.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_server_lib.h"
34 #include "gnunet_time_lib.h"
35 #include "gnunet_transport_service.h"
36 #include "transport.h"
39 * After how long do we give up on transmitting a HELLO
42 #define OFFER_HELLO_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
45 * After how long do we automatically retry an unsuccessful
48 #define CONNECT_RETRY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 750)
51 * How long should ARM wait when starting up the
52 * transport service before reporting back?
54 #define START_SERVICE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
57 * How long should ARM wait when stopping the
58 * transport service before reporting back?
60 #define STOP_SERVICE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
64 * What stage are we in for transmission processing?
74 * Message in local queue, not given to service.
79 * Message given to service, not confirmed (no SEND_OK).
84 * One message was given to service and before it was confirmed,
85 * another one was already queued (waiting for SEND_OK to pass on
88 TS_TRANSMITTED_QUEUED = 3
93 * Handle for a transmission-ready request.
95 struct GNUNET_TRANSPORT_TransmitHandle
99 * Neighbour for this handle, NULL for control-traffic.
101 struct NeighbourList *neighbour;
104 * Function to call when notify_size bytes are available
107 GNUNET_CONNECTION_TransmitReadyNotify notify;
110 * Closure for notify.
115 * transmit_ready task Id. The task is used to introduce the
116 * artificial delay that may be required to maintain the bandwidth
117 * limits. Later, this will be the ID of the "transmit_timeout"
118 * task which is used to signal a timeout if the transmission could
119 * not be done in a timely fashion.
121 GNUNET_SCHEDULER_TaskIdentifier notify_delay_task;
124 * Timeout for this request.
126 struct GNUNET_TIME_Absolute timeout;
129 * How many bytes is our notify callback waiting for?
134 * How important is this message?
136 unsigned int priority;
142 * Handle for a control message queue entry.
144 struct ControlMessage
148 * This is a doubly-linked list.
150 struct ControlMessage *next;
153 * This is a doubly-linked list.
155 struct ControlMessage *prev;
158 * Overall transport handle.
160 struct GNUNET_TRANSPORT_Handle *h;
163 * Function to call when notify_size bytes are available
166 GNUNET_CONNECTION_TransmitReadyNotify notify;
169 * Closure for notify.
174 * transmit_ready task Id. The task is used to introduce the
175 * artificial delay that may be required to maintain the bandwidth
176 * limits. Later, this will be the ID of the "transmit_timeout"
177 * task which is used to signal a timeout if the transmission could
178 * not be done in a timely fashion.
180 GNUNET_SCHEDULER_TaskIdentifier notify_delay_task;
183 * How many bytes is our notify callback waiting for?
191 * Entry in linked list of all of our current neighbours.
197 * This is a linked list.
199 struct NeighbourList *next;
202 * Overall transport handle.
204 struct GNUNET_TRANSPORT_Handle *h;
207 * Active transmit handle; available if 'transmit_forbidden'
210 struct GNUNET_TRANSPORT_TransmitHandle transmit_handle;
213 * Identity of this neighbour.
215 struct GNUNET_PeerIdentity id;
218 * At what time did we reset last_sent last?
220 struct GNUNET_TIME_Absolute last_quota_update;
223 * How many bytes have we sent since the "last_quota_update"
229 * Quota for outbound traffic to the neighbour in bytes/ms.
234 * Set to GNUNET_NO if we are currently allowed to accept a
235 * message to the transport service for this peer, GNUNET_YES
236 * if we have one and are waiting for transmission, GNUNET_SYSERR
237 * if we are waiting for confirmation AND have already accepted
238 * yet another message.
240 enum TransmitStage transmit_stage;
243 * Have we received a notification that this peer is connected
252 * Linked list of requests from clients for our HELLO that were
259 * This is a linked list.
261 struct HelloWaitList *next;
264 * Reference back to our transport handle.
266 struct GNUNET_TRANSPORT_Handle *handle;
269 * Callback to call once we got our HELLO.
271 GNUNET_TRANSPORT_HelloUpdateCallback rec;
282 * Handle for the transport service (includes all of the
283 * state for the transport service).
285 struct GNUNET_TRANSPORT_Handle
289 * Closure for the callbacks.
294 * Function to call for received data.
296 GNUNET_TRANSPORT_ReceiveCallback rec;
299 * function to call on connect events
301 GNUNET_TRANSPORT_NotifyConnect nc_cb;
304 * function to call on disconnect events
306 GNUNET_TRANSPORT_NotifyDisconnect nd_cb;
309 * Head of DLL of control messages.
311 struct ControlMessage *control_head;
314 * Tail of DLL of control messages.
316 struct ControlMessage *control_tail;
319 * The current HELLO message for this peer. Updated
320 * whenever transports change their addresses.
322 struct GNUNET_HELLO_Message *my_hello;
325 * My client connection to the transport service.
327 struct GNUNET_CLIENT_Connection *client;
330 * Handle to our registration with the client for notification.
332 struct GNUNET_CLIENT_TransmitHandle *network_handle;
335 * Linked list of pending requests for our HELLO.
337 struct HelloWaitList *hwl_head;
342 struct GNUNET_SCHEDULER_Handle *sched;
347 const struct GNUNET_CONFIGURATION_Handle *cfg;
350 * Linked list of the current neighbours of this peer.
352 struct NeighbourList *neighbours;
355 * ID of the task trying to reconnect to the service.
357 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
360 * ID of the task trying to trigger transmission for a peer
361 * while maintaining bandwidth quotas.
363 GNUNET_SCHEDULER_TaskIdentifier quota_task;
366 * Delay until we try to reconnect.
368 struct GNUNET_TIME_Relative reconnect_delay;
373 // FIXME: replace with hash map!
375 * Get the neighbour list entry for the given peer
377 * @param h our context
378 * @param peer peer to look up
379 * @return NULL if no such peer entry exists
381 static struct NeighbourList *
382 neighbour_find (struct GNUNET_TRANSPORT_Handle *h,
383 const struct GNUNET_PeerIdentity *peer)
385 struct NeighbourList *pos;
388 while ((pos != NULL) &&
389 (0 != memcmp (peer, &pos->id, sizeof (struct GNUNET_PeerIdentity))))
396 * Schedule the task to send one message, either from the control
397 * list or the peer message queues to the service.
399 static void schedule_transmission (struct GNUNET_TRANSPORT_Handle *h);
403 * Function called by the scheduler when the timeout for bandwidth
404 * availablility for the target neighbour is reached.
406 * @param cls the 'struct GNUNET_TRANSPORT_Handle*'
407 * @param tc scheduler context
410 quota_transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
412 struct GNUNET_TRANSPORT_Handle *h = cls;
414 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
415 schedule_transmission (h);
420 * Update the quota values for the given neighbour now.
422 * @param n neighbour to update
425 update_quota (struct NeighbourList *n)
427 struct GNUNET_TIME_Relative delta;
431 delta = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
432 allowed = delta.value * n->quota_out;
433 if (n->last_sent < allowed)
435 remaining = allowed - n->last_sent;
436 if (n->quota_out > 0)
437 remaining /= n->quota_out;
440 if (remaining > MAX_BANDWIDTH_CARRY)
441 remaining = MAX_BANDWIDTH_CARRY;
443 n->last_quota_update = GNUNET_TIME_absolute_get ();
444 n->last_quota_update.value -= remaining;
448 n->last_sent -= allowed;
449 n->last_quota_update = GNUNET_TIME_absolute_get ();
455 * Figure out which transmission to a peer can be done right now.
456 * If none can, schedule a task to call 'schedule_transmission'
457 * whenever a peer transmission can be done in the future and
458 * return NULL. Otherwise return the next transmission to be
461 * @param h handle to transport
462 * @return NULL to wait longer before doing any peer transmissions
464 static struct GNUNET_TRANSPORT_TransmitHandle *
465 schedule_peer_transmission (struct GNUNET_TRANSPORT_Handle *h)
467 struct GNUNET_TRANSPORT_TransmitHandle *ret;
468 struct GNUNET_TRANSPORT_TransmitHandle *th;
469 struct NeighbourList *n;
470 struct NeighbourList *next;
471 struct GNUNET_TIME_Relative retry_time;
472 struct GNUNET_TIME_Relative duration;
475 if (h->quota_task != GNUNET_SCHEDULER_NO_TASK)
477 GNUNET_SCHEDULER_cancel (h->sched,
479 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
481 retry_time = GNUNET_TIME_UNIT_FOREVER_REL;
483 next = h->neighbours;
484 while (NULL != (n = next))
487 if (n->transmit_stage != TS_QUEUED)
488 continue; /* not eligible */
489 th = &n->transmit_handle;
490 /* check outgoing quota */
491 duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
492 if (duration.value > MIN_QUOTA_REFRESH_TIME)
495 duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
497 available = duration.value * n->quota_out;
498 if (available < n->last_sent + th->notify_size)
500 /* calculate how much bandwidth we'd still need to
501 accumulate and based on that how long we'll have
503 available = n->last_sent + th->notify_size - available;
504 duration = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
505 available / n->quota_out);
506 if (duration.value == 0)
507 duration = GNUNET_TIME_UNIT_MILLISECONDS;
508 if (th->timeout.value <
509 GNUNET_TIME_relative_to_absolute (duration).value)
511 /* signal timeout! */
513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
514 "Would need %llu ms before bandwidth is available for delivery to `%4s', that is too long. Signaling timeout.\n",
515 duration.value, GNUNET_i2s (&n->id));
517 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
519 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
520 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
522 n->transmit_stage = TS_NEW;
523 if (NULL != th->notify)
524 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
529 "Need more bandwidth, delaying delivery to `%4s' by %llu ms\n",
530 GNUNET_i2s (&n->id), duration.value);
532 retry_time = GNUNET_TIME_relative_min (retry_time,
537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
538 "Have %u bytes of bandwidth available for transmission to `%4s' right now\n",
540 GNUNET_i2s (&n->id));
542 if ( (ret == NULL) ||
543 (ret->priority < th->priority) )
547 h->quota_task = GNUNET_SCHEDULER_add_delayed (h->sched,
549 "a_transmit_ready,
556 * Transmit message(s) to service.
558 * @param cls handle to transport
559 * @param size number of bytes available in buf
560 * @param buf where to copy the message
561 * @return number of bytes copied to buf
564 transport_notify_ready (void *cls, size_t size, void *buf)
566 struct GNUNET_TRANSPORT_Handle *h = cls;
567 struct ControlMessage *cm;
568 struct GNUNET_TRANSPORT_TransmitHandle *th;
569 struct NeighbourList *n;
570 struct OutboundMessage obm;
575 h->network_handle = NULL;
578 schedule_transmission (h);
582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
583 "Ready to transmit %u bytes to transport service\n", size);
587 while ( (NULL != (cm = h->control_head)) &&
588 (cm->notify_size <= size) )
590 if (cm->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
592 GNUNET_SCHEDULER_cancel (h->sched, cm->notify_delay_task);
593 cm->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
595 GNUNET_CONTAINER_DLL_remove (h->control_head,
598 ret += cm->notify (cm->notify_cls, size, &cbuf[ret]);
602 while ( (NULL != (th = schedule_peer_transmission (h))) &&
603 (th->notify_size <= size) )
605 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
607 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
608 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
611 switch (n->transmit_stage)
617 n->transmit_stage = TS_TRANSMITTED;
622 case TS_TRANSMITTED_QUEUED:
628 GNUNET_assert (size >= sizeof (struct OutboundMessage));
629 mret = th->notify (th->notify_cls,
630 size - sizeof (struct OutboundMessage),
631 &cbuf[ret + sizeof (struct OutboundMessage)]);
632 GNUNET_assert (mret <= size - sizeof (struct OutboundMessage));
635 obm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
636 obm.header.size = htons (mret + sizeof (struct OutboundMessage));
637 obm.priority = htonl (th->priority);
638 obm.timeout = GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining (th->timeout));
640 memcpy (&cbuf[ret], &obm, sizeof (struct OutboundMessage));
641 ret += (mret + sizeof (struct OutboundMessage));
642 size -= (mret + sizeof (struct OutboundMessage));
646 switch (n->transmit_stage)
655 n->transmit_stage = TS_NEW;
657 case TS_TRANSMITTED_QUEUED:
665 schedule_transmission (h);
667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
668 "Transmitting %u bytes to transport service\n", ret);
675 * Schedule the task to send one message, either from the control
676 * list or the peer message queues to the service.
679 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
682 struct GNUNET_TIME_Relative timeout;
683 struct GNUNET_TRANSPORT_TransmitHandle *th;
685 if (NULL != h->network_handle)
687 if (h->client == NULL)
689 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
690 "Could not yet schedule transmission: we are not yet connected to the transport service!\n");
691 return; /* not yet connected */
693 if (NULL != h->control_head)
695 size = h->control_head->notify_size;
696 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
700 th = schedule_peer_transmission (h);
703 /* no transmission ready right now */
706 size = th->notify_size;
707 timeout = GNUNET_TIME_absolute_get_remaining (th->timeout);
710 GNUNET_CLIENT_notify_transmit_ready (h->client,
714 &transport_notify_ready,
716 GNUNET_assert (NULL != h->network_handle);
721 * Called when our transmit request timed out before any transport
722 * reported success connecting to the desired peer or before the
723 * transport was ready to receive. Signal error and free
727 control_transmit_timeout (void *cls,
728 const struct GNUNET_SCHEDULER_TaskContext *tc)
730 struct ControlMessage *th = cls;
732 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
733 if (NULL != th->notify)
734 th->notify (th->notify_cls, 0, NULL);
735 GNUNET_CONTAINER_DLL_remove (th->h->control_head,
743 * Queue control request for transmission to the transport
746 * @param h handle to the transport service
747 * @param size number of bytes to be transmitted
748 * @param at_head request must be added to the head of the queue
749 * (otherwise request will be appended)
750 * @param timeout how long this transmission can wait (at most)
751 * @param notify function to call to get the content
752 * @param notify_cls closure for notify
755 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h,
758 struct GNUNET_TIME_Relative timeout,
759 GNUNET_CONNECTION_TransmitReadyNotify notify,
762 struct ControlMessage *th;
765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
766 "Control transmit of %u bytes within %llums requested\n",
767 size, (unsigned long long) timeout.value);
769 th = GNUNET_malloc (sizeof (struct ControlMessage));
772 th->notify_cls = notify_cls;
773 th->notify_size = size;
774 th->notify_delay_task
775 = GNUNET_SCHEDULER_add_delayed (h->sched,
776 timeout, &control_transmit_timeout, th);
778 GNUNET_CONTAINER_DLL_insert (h->control_head,
782 GNUNET_CONTAINER_DLL_insert_after (h->control_head,
786 schedule_transmission (h);
790 struct SetQuotaContext
792 struct GNUNET_TRANSPORT_Handle *handle;
794 struct GNUNET_PeerIdentity target;
796 GNUNET_SCHEDULER_Task cont;
800 struct GNUNET_TIME_Absolute timeout;
807 * Send SET_QUOTA message to the service.
809 * @param cls the 'struct SetQuotaContext'
810 * @param size number of bytes available in buf
811 * @param buf where to copy the message
812 * @return number of bytes copied to buf
815 send_set_quota (void *cls, size_t size, void *buf)
817 struct SetQuotaContext *sqc = cls;
818 struct QuotaSetMessage *msg;
822 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
825 GNUNET_SCHEDULER_REASON_TIMEOUT);
830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
831 "Transmitting `%s' request with respect to `%4s'.\n",
833 GNUNET_i2s (&sqc->target));
835 GNUNET_assert (size >= sizeof (struct QuotaSetMessage));
837 msg->header.size = htons (sizeof (struct QuotaSetMessage));
838 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
839 msg->quota_in = htonl (sqc->quota_in);
840 memcpy (&msg->peer, &sqc->target, sizeof (struct GNUNET_PeerIdentity));
841 if (sqc->cont != NULL)
842 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
845 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
847 return sizeof (struct QuotaSetMessage);
852 * Set the share of incoming bandwidth for the given
853 * peer to the specified amount.
855 * @param handle connection to transport service
856 * @param target who's bandwidth quota is being changed
857 * @param quota_in incoming bandwidth quota in bytes per ms
858 * @param quota_out outgoing bandwidth quota in bytes per ms
859 * @param timeout how long to wait until signaling failure if
860 * we can not communicate the quota change
861 * @param cont continuation to call when done, will be called
862 * either with reason "TIMEOUT" or with reason "PREREQ_DONE"
863 * @param cont_cls closure for continuation
866 GNUNET_TRANSPORT_set_quota (struct GNUNET_TRANSPORT_Handle *handle,
867 const struct GNUNET_PeerIdentity *target,
870 struct GNUNET_TIME_Relative timeout,
871 GNUNET_SCHEDULER_Task cont, void *cont_cls)
873 struct NeighbourList *n;
874 struct SetQuotaContext *sqc;
876 n = neighbour_find (handle, target);
880 if (n->quota_out < quota_out)
881 n->last_quota_update = GNUNET_TIME_absolute_get ();
882 n->quota_out = quota_out;
884 sqc = GNUNET_malloc (sizeof (struct SetQuotaContext));
885 sqc->handle = handle;
886 sqc->target = *target;
888 sqc->cont_cls = cont_cls;
889 sqc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
890 sqc->quota_in = quota_in;
891 schedule_control_transmit (handle,
892 sizeof (struct QuotaSetMessage),
893 GNUNET_NO, timeout, &send_set_quota, sqc);
898 * Obtain the HELLO message for this peer.
900 * @param handle connection to transport service
901 * @param rec function to call with the HELLO, sender will be our peer
902 * identity; message and sender will be NULL on timeout
903 * (handshake with transport service pending/failed).
904 * cost estimate will be 0.
905 * @param rec_cls closure for rec
908 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
909 GNUNET_TRANSPORT_HelloUpdateCallback rec,
912 struct HelloWaitList *hwl;
914 hwl = GNUNET_malloc (sizeof (struct HelloWaitList));
915 hwl->next = handle->hwl_head;
916 handle->hwl_head = hwl;
917 hwl->handle = handle;
919 hwl->rec_cls = rec_cls;
920 if (handle->my_hello == NULL)
922 rec (rec_cls, (const struct GNUNET_MessageHeader *) handle->my_hello);
928 * Stop receiving updates about changes to our HELLO message.
930 * @param handle connection to transport service
931 * @param rec function previously registered to be called with the HELLOs
932 * @param rec_cls closure for rec
935 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_Handle *handle,
936 GNUNET_TRANSPORT_HelloUpdateCallback rec,
939 struct HelloWaitList *pos;
940 struct HelloWaitList *prev;
943 pos = handle->hwl_head;
946 if ( (pos->rec == rec) &&
947 (pos->rec_cls == rec_cls) )
952 GNUNET_break (pos != NULL);
956 handle->hwl_head = pos->next;
958 prev->next = pos->next;
964 * Send HELLO message to the service.
966 * @param cls the HELLO message to send
967 * @param size number of bytes available in buf
968 * @param buf where to copy the message
969 * @return number of bytes copied to buf
972 send_hello (void *cls, size_t size, void *buf)
974 struct GNUNET_MessageHeader *hello = cls;
979 #if DEBUG_TRANSPORT_TIMEOUT
980 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
981 "Timeout while trying to transmit `%s' request.\n",
988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
989 "Transmitting `%s' request.\n", "HELLO");
991 msize = ntohs (hello->size);
992 GNUNET_assert (size >= msize);
993 memcpy (buf, hello, msize);
1000 * Offer the transport service the HELLO of another peer. Note that
1001 * the transport service may just ignore this message if the HELLO is
1002 * malformed or useless due to our local configuration.
1004 * @param handle connection to transport service
1005 * @param hello the hello message
1008 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
1009 const struct GNUNET_MessageHeader *hello)
1011 struct GNUNET_MessageHeader *hc;
1014 GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
1015 size = ntohs (hello->size);
1016 GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
1017 hc = GNUNET_malloc (size);
1018 memcpy (hc, hello, size);
1019 schedule_control_transmit (handle,
1021 GNUNET_NO, OFFER_HELLO_TIMEOUT, &send_hello, hc);
1026 * Transmit START message to service.
1029 * @param size number of bytes available in buf
1030 * @param buf where to copy the message
1031 * @return number of bytes copied to buf
1034 send_start (void *cls, size_t size, void *buf)
1036 struct GNUNET_MessageHeader *s = buf;
1040 /* Can only be shutdown, just give up */
1042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1043 "Shutdown while trying to transmit `%s' request.\n",
1049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1050 "Transmitting `%s' request.\n", "START");
1052 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
1053 s->size = htons (sizeof (struct GNUNET_MessageHeader));
1054 s->type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
1055 return sizeof (struct GNUNET_MessageHeader);
1062 * @param n the entry to free
1065 neighbour_free (struct NeighbourList *n)
1067 struct GNUNET_TRANSPORT_Handle *h;
1068 struct NeighbourList *prev;
1069 struct NeighbourList *pos;
1073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1074 "Removing neighbour `%s' from list of connected peers.\n",
1075 GNUNET_i2s (&n->id));
1077 GNUNET_break (n->is_connected == GNUNET_NO);
1078 GNUNET_break (n->transmit_stage == TS_NEW);
1081 pos = h->neighbours;
1088 h->neighbours = n->next;
1090 prev->next = n->next;
1096 * Mark neighbour as disconnected.
1098 * @param n the entry to mark as disconnected
1101 neighbour_disconnect (struct NeighbourList *n)
1103 struct GNUNET_TRANSPORT_Handle *h = n->h;
1105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1106 "Removing neighbour `%s' from list of connected peers.\n",
1107 GNUNET_i2s (&n->id));
1109 GNUNET_break (n->is_connected == GNUNET_YES);
1110 n->is_connected = GNUNET_NO;
1111 if (h->nc_cb != NULL)
1112 h->nd_cb (h->cls, &n->id);
1113 if (n->transmit_stage == TS_NEW)
1119 * Function we use for handling incoming messages.
1121 * @param cls closure (struct GNUNET_TRANSPORT_Handle *)
1122 * @param msg message received, NULL on timeout or fatal error
1124 static void demultiplexer (void *cls,
1125 const struct GNUNET_MessageHeader *msg);
1129 * Try again to connect to transport service.
1131 * @param cls the handle to the transport service
1132 * @param tc scheduler context
1135 reconnect (void *cls,
1136 const struct GNUNET_SCHEDULER_TaskContext *tc)
1138 struct GNUNET_TRANSPORT_Handle *h = cls;
1139 struct ControlMessage *pos;
1140 struct NeighbourList *n;
1141 struct NeighbourList *next;
1143 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1144 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1146 /* shutdown, just give up */
1149 /* Forget about all neighbours that we used to be connected to */
1153 #if DEBUG_TRANSPORT_DISCONNECT
1154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting due to reconnect being called\n");
1157 if (n->is_connected)
1158 neighbour_disconnect (n);
1162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n");
1164 GNUNET_assert (h->client == NULL);
1165 h->client = GNUNET_CLIENT_connect (h->sched, "transport", h->cfg);
1166 GNUNET_assert (h->client != NULL);
1167 /* make sure we don't send "START" twice, remove existing entry from
1168 queue (if present) */
1169 pos = h->control_head;
1172 if (pos->notify == &send_start)
1174 GNUNET_CONTAINER_DLL_remove (h->control_head,
1177 if (GNUNET_SCHEDULER_NO_TASK != pos->notify_delay_task)
1179 GNUNET_SCHEDULER_cancel (h->sched, pos->notify_delay_task);
1180 pos->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1187 schedule_control_transmit (h,
1188 sizeof (struct GNUNET_MessageHeader),
1190 GNUNET_TIME_UNIT_FOREVER_REL, &send_start, NULL);
1191 GNUNET_CLIENT_receive (h->client,
1192 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1197 * Function that will schedule the job that will try
1198 * to connect us again to the client.
1200 * @param h transport service to reconnect
1203 schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
1206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1207 "Scheduling task to reconnect to transport service in %llu ms.\n",
1208 h->reconnect_delay.value);
1210 GNUNET_assert (h->client == NULL);
1211 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
1213 = GNUNET_SCHEDULER_add_delayed (h->sched,
1214 h->reconnect_delay, &reconnect, h);
1215 if (h->reconnect_delay.value == 0)
1217 h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1221 h->reconnect_delay = GNUNET_TIME_relative_multiply (h->reconnect_delay, 2);
1222 h->reconnect_delay = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
1223 h->reconnect_delay);
1229 * Add neighbour to our list
1231 static struct NeighbourList *
1232 neighbour_add (struct GNUNET_TRANSPORT_Handle *h,
1233 const struct GNUNET_PeerIdentity *pid)
1235 struct NeighbourList *n;
1237 /* check for duplicates */
1238 if (NULL != (n = neighbour_find (h, pid)))
1244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1245 "Creating entry for neighbour `%4s'.\n",
1248 n = GNUNET_malloc (sizeof (struct NeighbourList));
1250 n->last_quota_update = GNUNET_TIME_absolute_get ();
1251 n->next = h->neighbours;
1252 n->quota_out = GNUNET_CONSTANTS_DEFAULT_BPM_IN_OUT;
1260 * Connect to the transport service. Note that the connection may
1261 * complete (or fail) asynchronously.
1263 * @param sched scheduler to use
1264 * @param cfg configuration to use
1265 * @param cls closure for the callbacks
1266 * @param rec receive function to call
1267 * @param nc function to call on connect events
1268 * @param nd function to call on disconnect events
1270 struct GNUNET_TRANSPORT_Handle *
1271 GNUNET_TRANSPORT_connect (struct GNUNET_SCHEDULER_Handle *sched,
1272 const struct GNUNET_CONFIGURATION_Handle *cfg,
1274 GNUNET_TRANSPORT_ReceiveCallback rec,
1275 GNUNET_TRANSPORT_NotifyConnect nc,
1276 GNUNET_TRANSPORT_NotifyDisconnect nd)
1278 struct GNUNET_TRANSPORT_Handle *ret;
1280 GNUNET_ARM_start_services (cfg, sched, "peerinfo", "transport", NULL);
1281 ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Handle));
1288 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1289 schedule_reconnect (ret);
1295 * Disconnect from the transport service.
1298 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1300 struct GNUNET_TRANSPORT_TransmitHandle *th;
1301 struct NeighbourList *n;
1302 struct HelloWaitList *hwl;
1303 struct GNUNET_CLIENT_Connection *client;
1304 struct ControlMessage *cm;
1307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transport disconnect called!\n");
1309 while (NULL != (n = handle->neighbours))
1311 handle->neighbours = n->next;
1312 switch (n->transmit_stage)
1315 case TS_TRANSMITTED:
1319 case TS_TRANSMITTED_QUEUED:
1320 th = &n->transmit_handle;
1321 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1323 GNUNET_SCHEDULER_cancel (handle->sched,
1324 th->notify_delay_task);
1325 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1327 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
1334 while (NULL != (hwl = handle->hwl_head))
1336 handle->hwl_head = hwl->next;
1337 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1339 ("Disconnect while notification for `%s' still registered.\n"),
1341 if (hwl->rec != NULL)
1342 hwl->rec (hwl->rec_cls, NULL);
1346 /* Check for still scheduled control messages, cancel delay tasks if so */
1347 /* Added because somehow a notify_delay_task is remaining scheduled and is ever so annoying */
1348 while ( (NULL != (cm = handle->control_head)))
1350 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1351 _("Disconnect before control message sent!\n"));
1352 if (cm->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1354 GNUNET_SCHEDULER_cancel (handle->sched, cm->notify_delay_task);
1355 cm->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1357 GNUNET_CONTAINER_DLL_remove (handle->control_head,
1358 handle->control_tail,
1364 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1366 GNUNET_SCHEDULER_cancel (handle->sched, handle->reconnect_task);
1367 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1369 if (handle->quota_task != GNUNET_SCHEDULER_NO_TASK)
1371 GNUNET_SCHEDULER_cancel (handle->sched, handle->quota_task);
1372 handle->quota_task = GNUNET_SCHEDULER_NO_TASK;
1374 GNUNET_free_non_null (handle->my_hello);
1375 handle->my_hello = NULL;
1376 GNUNET_ARM_stop_services (handle->cfg, handle->sched, "transport",
1378 if (NULL != handle->network_handle)
1380 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->network_handle);
1381 handle->network_handle = NULL;
1383 if (NULL != (client = handle->client))
1386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1387 "Disconnecting from transport service for good.\n");
1389 handle->client = NULL;
1390 GNUNET_CLIENT_disconnect (client);
1392 GNUNET_free (handle);
1397 * Function we use for handling incoming messages.
1399 * @param cls closure (struct GNUNET_TRANSPORT_Handle *)
1400 * @param msg message received, NULL on timeout or fatal error
1403 demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
1405 struct GNUNET_TRANSPORT_Handle *h = cls;
1406 const struct DisconnectInfoMessage *dim;
1407 const struct ConnectInfoMessage *cim;
1408 const struct InboundMessage *im;
1409 const struct GNUNET_MessageHeader *imm;
1410 const struct SendOkMessage *okm;
1411 struct HelloWaitList *hwl;
1412 struct HelloWaitList *next_hwl;
1413 struct NeighbourList *n;
1414 struct GNUNET_PeerIdentity me;
1417 if (h->client == NULL)
1419 /* shutdown initiated from 'GNUNET_TRANSPORT_disconnect',
1420 finish clean up work! */
1427 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1428 "Error receiving from transport service, disconnecting temporarily.\n");
1430 if (h->network_handle != NULL)
1432 GNUNET_CLIENT_notify_transmit_ready_cancel (h->network_handle);
1433 h->network_handle = NULL;
1435 GNUNET_CLIENT_disconnect (h->client);
1437 schedule_reconnect (h);
1440 GNUNET_CLIENT_receive (h->client,
1441 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1442 size = ntohs (msg->size);
1443 switch (ntohs (msg->type))
1445 case GNUNET_MESSAGE_TYPE_HELLO:
1447 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg,
1454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1455 "Receiving (my own) `%s' message, I am `%4s'.\n",
1456 "HELLO", GNUNET_i2s (&me));
1458 GNUNET_free_non_null (h->my_hello);
1460 if (size < sizeof (struct GNUNET_MessageHeader))
1465 h->my_hello = GNUNET_malloc (size);
1466 memcpy (h->my_hello, msg, size);
1470 next_hwl = hwl->next;
1471 hwl->rec (hwl->rec_cls,
1472 (const struct GNUNET_MessageHeader *) h->my_hello);
1476 case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
1477 if (size != sizeof (struct ConnectInfoMessage))
1482 cim = (const struct ConnectInfoMessage *) msg;
1484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1485 "Receiving `%s' message for `%4s'.\n",
1486 "CONNECT", GNUNET_i2s (&cim->id));
1488 n = neighbour_find (h, &cim->id);
1490 n = neighbour_add (h,
1492 GNUNET_break (n->is_connected == GNUNET_NO);
1493 n->is_connected = GNUNET_YES;
1494 if (h->nc_cb != NULL)
1495 h->nc_cb (h->cls, &n->id,
1496 GNUNET_TIME_relative_ntoh (cim->latency),
1497 ntohs (cim->distance));
1499 case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
1500 if (size != sizeof (struct DisconnectInfoMessage))
1505 dim = (const struct DisconnectInfoMessage *) msg;
1506 #if DEBUG_TRANSPORT_DISCONNECT
1507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1508 "Receiving `%s' message for `%4s'.\n",
1510 GNUNET_i2s (&dim->peer));
1512 n = neighbour_find (h, &dim->peer);
1513 GNUNET_break (n != NULL);
1515 neighbour_disconnect (n);
1517 case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
1518 if (size != sizeof (struct SendOkMessage))
1523 okm = (const struct SendOkMessage *) msg;
1525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1526 "Receiving `%s' message, transmission %s.\n", "SEND_OK",
1527 ntohl (okm->success) == GNUNET_OK ? "succeeded" : "failed");
1529 n = neighbour_find (h, &okm->peer);
1530 GNUNET_assert (n != NULL);
1531 switch (n->transmit_stage)
1539 case TS_TRANSMITTED:
1540 n->transmit_stage = TS_NEW;
1542 case TS_TRANSMITTED_QUEUED:
1543 n->transmit_stage = TS_QUEUED;
1544 schedule_transmission (h);
1550 case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
1552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1553 "Receiving `%s' message.\n", "RECV");
1556 sizeof (struct InboundMessage) +
1557 sizeof (struct GNUNET_MessageHeader))
1562 im = (const struct InboundMessage *) msg;
1563 imm = (const struct GNUNET_MessageHeader *) &im[1];
1564 if (ntohs (imm->size) + sizeof (struct InboundMessage) != size)
1570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1571 "Received message of type %u from `%4s'.\n",
1572 ntohs (imm->type), GNUNET_i2s (&im->peer));
1574 n = neighbour_find (h, &im->peer);
1576 n = neighbour_add (h, &im->peer);
1580 h->rec (h->cls, &im->peer, imm,
1581 GNUNET_TIME_relative_ntoh (im->latency), ntohs(im->distance));
1584 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1586 ("Received unexpected message of type %u in %s:%u\n"),
1587 ntohs (msg->type), __FILE__, __LINE__);
1595 * Called when our transmit request timed out before any transport
1596 * reported success connecting to the desired peer or before the
1597 * transport was ready to receive. Signal error and free
1600 * @param cls the 'struct GNUNET_TRANSPORT_TransmitHandle*' that is timing out
1601 * @param tc scheduler context
1604 peer_transmit_timeout (void *cls,
1605 const struct GNUNET_SCHEDULER_TaskContext *tc)
1607 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
1608 struct NeighbourList *n;
1609 GNUNET_CONNECTION_TransmitReadyNotify notify;
1612 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1616 "Triggering timeout for request to transmit to `%4s' (%d)\n",
1617 GNUNET_i2s (&n->id),
1620 notify = th->notify;
1621 notify_cls = th->notify_cls;
1622 switch (n->transmit_stage)
1628 n->transmit_stage = TS_NEW;
1629 if (n->is_connected == GNUNET_NO)
1632 case TS_TRANSMITTED:
1635 case TS_TRANSMITTED_QUEUED:
1636 n->transmit_stage = TS_TRANSMITTED;
1642 notify (notify_cls, 0, NULL);
1647 * Check if we could queue a message of the given size for
1648 * transmission. The transport service will take both its
1649 * internal buffers and bandwidth limits imposed by the
1650 * other peer into consideration when answering this query.
1652 * @param handle connection to transport service
1653 * @param target who should receive the message
1654 * @param size how big is the message we want to transmit?
1655 * @param priority how important is the message?
1656 * @param timeout after how long should we give up (and call
1657 * notify with buf NULL and size 0)?
1658 * @param notify function to call when we are ready to
1659 * send such a message
1660 * @param notify_cls closure for notify
1661 * @return NULL if someone else is already waiting to be notified
1662 * non-NULL if the notify callback was queued (can be used to cancel
1663 * using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1665 struct GNUNET_TRANSPORT_TransmitHandle *
1666 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle
1668 const struct GNUNET_PeerIdentity
1669 *target, size_t size,
1670 unsigned int priority,
1671 struct GNUNET_TIME_Relative timeout,
1672 GNUNET_CONNECTION_TransmitReadyNotify
1673 notify, void *notify_cls)
1675 struct GNUNET_TRANSPORT_TransmitHandle *th;
1676 struct NeighbourList *n;
1678 if (size + sizeof (struct OutboundMessage) >=
1679 GNUNET_SERVER_MAX_MESSAGE_SIZE)
1682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1683 "Message size is %d, max allowed is %d.\n",
1684 size + sizeof (struct OutboundMessage), GNUNET_SERVER_MAX_MESSAGE_SIZE);
1690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1691 "Asking transport service for transmission of %u bytes to peer `%4s'.\n",
1692 size, GNUNET_i2s (target));
1694 n = neighbour_find (handle, target);
1696 n = neighbour_add (handle, target);
1699 switch (n->transmit_stage)
1702 n->transmit_stage = TS_QUEUED;
1707 case TS_TRANSMITTED:
1708 n->transmit_stage = TS_TRANSMITTED_QUEUED;
1710 case TS_TRANSMITTED_QUEUED:
1717 th = &n->transmit_handle;
1719 th->notify = notify;
1720 th->notify_cls = notify_cls;
1721 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1722 th->notify_size = size + sizeof (struct OutboundMessage);
1723 th->priority = priority;
1724 th->notify_delay_task
1725 = GNUNET_SCHEDULER_add_delayed (handle->sched, timeout,
1726 &peer_transmit_timeout, th);
1727 schedule_transmission (handle);
1733 * Cancel the specified transmission-ready notification.
1736 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
1737 GNUNET_TRANSPORT_TransmitHandle
1740 struct NeighbourList *n;
1744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1745 "Transmission request of %u bytes to `%4s' was cancelled.\n",
1746 th->notify_size - sizeof (struct OutboundMessage),
1747 GNUNET_i2s (&n->id));
1749 switch (n->transmit_stage)
1755 n->transmit_stage = TS_NEW;
1756 if (n->is_connected == GNUNET_NO)
1759 case TS_TRANSMITTED:
1762 case TS_TRANSMITTED_QUEUED:
1763 n->transmit_stage = TS_TRANSMITTED;
1771 /* end of transport_api.c */