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_bandwidth_lib.h"
28 #include "gnunet_client_lib.h"
29 #include "gnunet_constants.h"
30 #include "gnunet_container_lib.h"
31 #include "gnunet_arm_service.h"
32 #include "gnunet_hello_lib.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_server_lib.h"
35 #include "gnunet_time_lib.h"
36 #include "gnunet_transport_service.h"
37 #include "transport.h"
40 * After how long do we give up on transmitting a HELLO
43 #define OFFER_HELLO_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
46 * After how long do we automatically retry an unsuccessful
49 #define CONNECT_RETRY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 750)
52 * How long should ARM wait when starting up the
53 * transport service before reporting back?
55 #define START_SERVICE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
58 * How long should ARM wait when stopping the
59 * transport service before reporting back?
61 #define STOP_SERVICE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
65 * What stage are we in for transmission processing?
75 * Message in local queue, not given to service.
80 * Message given to service, not confirmed (no SEND_OK).
85 * One message was given to service and before it was confirmed,
86 * another one was already queued (waiting for SEND_OK to pass on
89 TS_TRANSMITTED_QUEUED = 3
94 * Handle for a transmission-ready request.
96 struct GNUNET_TRANSPORT_TransmitHandle
100 * Neighbour for this handle, NULL for control-traffic.
102 struct NeighbourList *neighbour;
105 * Function to call when notify_size bytes are available
108 GNUNET_CONNECTION_TransmitReadyNotify notify;
111 * Closure for notify.
116 * transmit_ready task Id. The task is used to introduce the
117 * artificial delay that may be required to maintain the bandwidth
118 * limits. Later, this will be the ID of the "transmit_timeout"
119 * task which is used to signal a timeout if the transmission could
120 * not be done in a timely fashion.
122 GNUNET_SCHEDULER_TaskIdentifier notify_delay_task;
125 * Timeout for this request.
127 struct GNUNET_TIME_Absolute timeout;
130 * How many bytes is our notify callback waiting for?
135 * How important is this message?
137 unsigned int priority;
143 * Handle for a control message queue entry.
145 struct ControlMessage
149 * This is a doubly-linked list.
151 struct ControlMessage *next;
154 * This is a doubly-linked list.
156 struct ControlMessage *prev;
159 * Overall transport handle.
161 struct GNUNET_TRANSPORT_Handle *h;
164 * Function to call when notify_size bytes are available
167 GNUNET_CONNECTION_TransmitReadyNotify notify;
170 * Closure for notify.
175 * transmit_ready task Id. The task is used to introduce the
176 * artificial delay that may be required to maintain the bandwidth
177 * limits. Later, this will be the ID of the "transmit_timeout"
178 * task which is used to signal a timeout if the transmission could
179 * not be done in a timely fashion.
181 GNUNET_SCHEDULER_TaskIdentifier notify_delay_task;
184 * How many bytes is our notify callback waiting for?
192 * Entry in linked list of all of our current neighbours.
198 * This is a linked list.
200 struct NeighbourList *next;
203 * Overall transport handle.
205 struct GNUNET_TRANSPORT_Handle *h;
208 * Active transmit handle; available if 'transmit_forbidden'
211 struct GNUNET_TRANSPORT_TransmitHandle transmit_handle;
214 * Identity of this neighbour.
216 struct GNUNET_PeerIdentity id;
219 * Outbound bandwidh tracker.
221 struct GNUNET_BANDWIDTH_Tracker out_tracker;
224 * Set to GNUNET_NO if we are currently allowed to accept a
225 * message to the transport service for this peer, GNUNET_YES
226 * if we have one and are waiting for transmission, GNUNET_SYSERR
227 * if we are waiting for confirmation AND have already accepted
228 * yet another message.
230 enum TransmitStage transmit_stage;
233 * Have we received a notification that this peer is connected
242 * Linked list of requests from clients for our HELLO that were
249 * This is a linked list.
251 struct HelloWaitList *next;
254 * Reference back to our transport handle.
256 struct GNUNET_TRANSPORT_Handle *handle;
259 * Callback to call once we got our HELLO.
261 GNUNET_TRANSPORT_HelloUpdateCallback rec;
272 * Handle for the transport service (includes all of the
273 * state for the transport service).
275 struct GNUNET_TRANSPORT_Handle
279 * Closure for the callbacks.
284 * Function to call for received data.
286 GNUNET_TRANSPORT_ReceiveCallback rec;
289 * function to call on connect events
291 GNUNET_TRANSPORT_NotifyConnect nc_cb;
294 * function to call on disconnect events
296 GNUNET_TRANSPORT_NotifyDisconnect nd_cb;
299 * Head of DLL of control messages.
301 struct ControlMessage *control_head;
304 * Tail of DLL of control messages.
306 struct ControlMessage *control_tail;
309 * The current HELLO message for this peer. Updated
310 * whenever transports change their addresses.
312 struct GNUNET_HELLO_Message *my_hello;
315 * My client connection to the transport service.
317 struct GNUNET_CLIENT_Connection *client;
320 * Handle to our registration with the client for notification.
322 struct GNUNET_CLIENT_TransmitHandle *network_handle;
325 * Linked list of pending requests for our HELLO.
327 struct HelloWaitList *hwl_head;
332 struct GNUNET_SCHEDULER_Handle *sched;
337 const struct GNUNET_CONFIGURATION_Handle *cfg;
340 * Linked list of the current neighbours of this peer.
342 struct NeighbourList *neighbours;
345 * ID of the task trying to reconnect to the service.
347 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
350 * ID of the task trying to trigger transmission for a peer
351 * while maintaining bandwidth quotas.
353 GNUNET_SCHEDULER_TaskIdentifier quota_task;
356 * Delay until we try to reconnect.
358 struct GNUNET_TIME_Relative reconnect_delay;
363 // FIXME: replace with hash map!
365 * Get the neighbour list entry for the given peer
367 * @param h our context
368 * @param peer peer to look up
369 * @return NULL if no such peer entry exists
371 static struct NeighbourList *
372 neighbour_find (struct GNUNET_TRANSPORT_Handle *h,
373 const struct GNUNET_PeerIdentity *peer)
375 struct NeighbourList *pos;
378 while ((pos != NULL) &&
379 (0 != memcmp (peer, &pos->id, sizeof (struct GNUNET_PeerIdentity))))
386 * Schedule the task to send one message, either from the control
387 * list or the peer message queues to the service.
389 static void schedule_transmission (struct GNUNET_TRANSPORT_Handle *h);
393 * Function called by the scheduler when the timeout for bandwidth
394 * availablility for the target neighbour is reached.
396 * @param cls the 'struct GNUNET_TRANSPORT_Handle*'
397 * @param tc scheduler context
400 quota_transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
402 struct GNUNET_TRANSPORT_Handle *h = cls;
404 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
405 schedule_transmission (h);
410 * Figure out which transmission to a peer can be done right now.
411 * If none can, schedule a task to call 'schedule_transmission'
412 * whenever a peer transmission can be done in the future and
413 * return NULL. Otherwise return the next transmission to be
416 * @param h handle to transport
417 * @return NULL to wait longer before doing any peer transmissions
419 static struct GNUNET_TRANSPORT_TransmitHandle *
420 schedule_peer_transmission (struct GNUNET_TRANSPORT_Handle *h)
422 struct GNUNET_TRANSPORT_TransmitHandle *ret;
423 struct GNUNET_TRANSPORT_TransmitHandle *th;
424 struct NeighbourList *n;
425 struct NeighbourList *next;
426 struct GNUNET_TIME_Relative retry_time;
427 struct GNUNET_TIME_Relative duration;
429 if (h->quota_task != GNUNET_SCHEDULER_NO_TASK)
431 GNUNET_SCHEDULER_cancel (h->sched,
433 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
435 retry_time = GNUNET_TIME_UNIT_FOREVER_REL;
437 next = h->neighbours;
438 while (NULL != (n = next))
441 if (n->transmit_stage != TS_QUEUED)
442 continue; /* not eligible */
443 th = &n->transmit_handle;
444 GNUNET_break (n == th->neighbour);
445 /* check outgoing quota */
446 duration = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
447 th->notify_size - sizeof (struct OutboundMessage));
448 if (th->timeout.value < duration.value)
450 /* signal timeout! */
452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
453 "Would need %llu ms before bandwidth is available for delivery to `%4s', that is too long. Signaling timeout.\n",
455 GNUNET_i2s (&n->id));
457 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
459 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
460 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
462 n->transmit_stage = TS_NEW;
463 if (NULL != th->notify)
464 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
467 if (duration.value > 0)
470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
471 "Need more bandwidth (%u b/s allowed, %u b needed), delaying delivery to `%4s' by %llu ms\n",
472 (unsigned int) n->out_tracker.available_bytes_per_s__,
473 (unsigned int) th->notify_size - sizeof (struct OutboundMessage),
477 retry_time = GNUNET_TIME_relative_min (retry_time,
482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
483 "Have %u bytes of bandwidth available for transmission to `%4s' right now\n",
484 th->notify_size - sizeof (struct OutboundMessage),
485 GNUNET_i2s (&n->id));
487 if ( (ret == NULL) ||
488 (ret->priority < th->priority) )
492 h->quota_task = GNUNET_SCHEDULER_add_delayed (h->sched,
494 "a_transmit_ready,
501 * Transmit message(s) to service.
503 * @param cls handle to transport
504 * @param size number of bytes available in buf
505 * @param buf where to copy the message
506 * @return number of bytes copied to buf
509 transport_notify_ready (void *cls, size_t size, void *buf)
511 struct GNUNET_TRANSPORT_Handle *h = cls;
512 struct ControlMessage *cm;
513 struct GNUNET_TRANSPORT_TransmitHandle *th;
514 struct NeighbourList *n;
515 struct OutboundMessage obm;
520 h->network_handle = NULL;
523 schedule_transmission (h);
527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
528 "Ready to transmit %u bytes to transport service\n", size);
532 while ( (NULL != (cm = h->control_head)) &&
533 (cm->notify_size <= size) )
535 if (cm->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
537 GNUNET_SCHEDULER_cancel (h->sched, cm->notify_delay_task);
538 cm->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
540 GNUNET_CONTAINER_DLL_remove (h->control_head,
543 ret += cm->notify (cm->notify_cls, size, &cbuf[ret]);
547 while ( (NULL != (th = schedule_peer_transmission (h))) &&
548 (th->notify_size <= size) )
550 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
552 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
553 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
556 switch (n->transmit_stage)
562 n->transmit_stage = TS_TRANSMITTED;
567 case TS_TRANSMITTED_QUEUED:
573 GNUNET_assert (size >= sizeof (struct OutboundMessage));
574 mret = th->notify (th->notify_cls,
575 size - sizeof (struct OutboundMessage),
576 &cbuf[ret + sizeof (struct OutboundMessage)]);
577 GNUNET_assert (mret <= size - sizeof (struct OutboundMessage));
579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
580 "Message of %u bytes with timeout %llums constructed for `%4s'\n",
582 (unsigned long long) GNUNET_TIME_absolute_get_remaining (th->timeout).value,
583 GNUNET_i2s (&n->id));
587 obm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
588 obm.header.size = htons (mret + sizeof (struct OutboundMessage));
589 obm.priority = htonl (th->priority);
590 obm.timeout = GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining (th->timeout));
592 memcpy (&cbuf[ret], &obm, sizeof (struct OutboundMessage));
593 ret += (mret + sizeof (struct OutboundMessage));
594 size -= (mret + sizeof (struct OutboundMessage));
595 GNUNET_BANDWIDTH_tracker_consume (&n->out_tracker, mret);
599 switch (n->transmit_stage)
608 n->transmit_stage = TS_NEW;
610 case TS_TRANSMITTED_QUEUED:
611 n->transmit_stage = TS_QUEUED;
618 schedule_transmission (h);
620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
621 "Transmitting %u bytes to transport service\n", ret);
628 * Schedule the task to send one message, either from the control
629 * list or the peer message queues to the service.
632 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
635 struct GNUNET_TIME_Relative timeout;
636 struct GNUNET_TRANSPORT_TransmitHandle *th;
638 if (NULL != h->network_handle)
640 if (h->client == NULL)
642 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
643 "Could not yet schedule transmission: we are not yet connected to the transport service!\n");
644 return; /* not yet connected */
646 if (NULL != h->control_head)
648 size = h->control_head->notify_size;
649 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
653 th = schedule_peer_transmission (h);
656 /* no transmission ready right now */
659 size = th->notify_size;
660 timeout = GNUNET_TIME_absolute_get_remaining (th->timeout);
663 GNUNET_CLIENT_notify_transmit_ready (h->client,
667 &transport_notify_ready,
669 GNUNET_assert (NULL != h->network_handle);
674 * Called when our transmit request timed out before any transport
675 * reported success connecting to the desired peer or before the
676 * transport was ready to receive. Signal error and free
680 control_transmit_timeout (void *cls,
681 const struct GNUNET_SCHEDULER_TaskContext *tc)
683 struct ControlMessage *th = cls;
685 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
686 if (NULL != th->notify)
687 th->notify (th->notify_cls, 0, NULL);
688 GNUNET_CONTAINER_DLL_remove (th->h->control_head,
696 * Queue control request for transmission to the transport
699 * @param h handle to the transport service
700 * @param size number of bytes to be transmitted
701 * @param at_head request must be added to the head of the queue
702 * (otherwise request will be appended)
703 * @param timeout how long this transmission can wait (at most)
704 * @param notify function to call to get the content
705 * @param notify_cls closure for notify
708 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h,
711 struct GNUNET_TIME_Relative timeout,
712 GNUNET_CONNECTION_TransmitReadyNotify notify,
715 struct ControlMessage *th;
718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
719 "Control transmit of %u bytes within %llums requested\n",
720 size, (unsigned long long) timeout.value);
722 th = GNUNET_malloc (sizeof (struct ControlMessage));
725 th->notify_cls = notify_cls;
726 th->notify_size = size;
727 th->notify_delay_task
728 = GNUNET_SCHEDULER_add_delayed (h->sched,
729 timeout, &control_transmit_timeout, th);
731 GNUNET_CONTAINER_DLL_insert (h->control_head,
735 GNUNET_CONTAINER_DLL_insert_after (h->control_head,
739 schedule_transmission (h);
743 struct SetQuotaContext
745 struct GNUNET_TRANSPORT_Handle *handle;
747 struct GNUNET_PeerIdentity target;
749 GNUNET_SCHEDULER_Task cont;
753 struct GNUNET_TIME_Absolute timeout;
755 struct GNUNET_BANDWIDTH_Value32NBO quota_in;
760 * Send SET_QUOTA message to the service.
762 * @param cls the 'struct SetQuotaContext'
763 * @param size number of bytes available in buf
764 * @param buf where to copy the message
765 * @return number of bytes copied to buf
768 send_set_quota (void *cls, size_t size, void *buf)
770 struct SetQuotaContext *sqc = cls;
771 struct QuotaSetMessage *msg;
775 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
778 GNUNET_SCHEDULER_REASON_TIMEOUT);
783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
784 "Transmitting `%s' request with respect to `%4s'.\n",
786 GNUNET_i2s (&sqc->target));
788 GNUNET_assert (size >= sizeof (struct QuotaSetMessage));
790 msg->header.size = htons (sizeof (struct QuotaSetMessage));
791 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
792 msg->quota = sqc->quota_in;
793 memcpy (&msg->peer, &sqc->target, sizeof (struct GNUNET_PeerIdentity));
794 if (sqc->cont != NULL)
795 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
798 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
800 return sizeof (struct QuotaSetMessage);
805 * Set the share of incoming bandwidth for the given
806 * peer to the specified amount.
808 * @param handle connection to transport service
809 * @param target who's bandwidth quota is being changed
810 * @param quota_in incoming bandwidth quota in bytes per ms
811 * @param quota_out outgoing bandwidth quota in bytes per ms
812 * @param timeout how long to wait until signaling failure if
813 * we can not communicate the quota change
814 * @param cont continuation to call when done, will be called
815 * either with reason "TIMEOUT" or with reason "PREREQ_DONE"
816 * @param cont_cls closure for continuation
819 GNUNET_TRANSPORT_set_quota (struct GNUNET_TRANSPORT_Handle *handle,
820 const struct GNUNET_PeerIdentity *target,
821 struct GNUNET_BANDWIDTH_Value32NBO quota_in,
822 struct GNUNET_BANDWIDTH_Value32NBO quota_out,
823 struct GNUNET_TIME_Relative timeout,
824 GNUNET_SCHEDULER_Task cont, void *cont_cls)
826 struct NeighbourList *n;
827 struct SetQuotaContext *sqc;
829 n = neighbour_find (handle, target);
833 if (ntohl (quota_out.value__) != n->out_tracker.available_bytes_per_s__)
834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
835 "Quota changed from %u to %u for peer `%s'\n",
836 (unsigned int) n->out_tracker.available_bytes_per_s__,
837 (unsigned int) ntohl (quota_out.value__),
838 GNUNET_i2s (target));
840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
841 "Quota remains at %u for peer `%s'\n",
842 (unsigned int) n->out_tracker.available_bytes_per_s__,
843 GNUNET_i2s (target));
845 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker,
851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
852 "Quota changed to %u for peer `%s', but I have no such neighbour!\n",
853 (unsigned int) ntohl (quota_out.value__),
854 GNUNET_i2s (target));
857 sqc = GNUNET_malloc (sizeof (struct SetQuotaContext));
858 sqc->handle = handle;
859 sqc->target = *target;
861 sqc->cont_cls = cont_cls;
862 sqc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
863 sqc->quota_in = quota_in;
864 schedule_control_transmit (handle,
865 sizeof (struct QuotaSetMessage),
866 GNUNET_NO, timeout, &send_set_quota, sqc);
871 * Obtain the HELLO message for this peer.
873 * @param handle connection to transport service
874 * @param rec function to call with the HELLO, sender will be our peer
875 * identity; message and sender will be NULL on timeout
876 * (handshake with transport service pending/failed).
877 * cost estimate will be 0.
878 * @param rec_cls closure for rec
881 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
882 GNUNET_TRANSPORT_HelloUpdateCallback rec,
885 struct HelloWaitList *hwl;
887 hwl = GNUNET_malloc (sizeof (struct HelloWaitList));
888 hwl->next = handle->hwl_head;
889 handle->hwl_head = hwl;
890 hwl->handle = handle;
892 hwl->rec_cls = rec_cls;
893 if (handle->my_hello == NULL)
895 rec (rec_cls, (const struct GNUNET_MessageHeader *) handle->my_hello);
901 * Stop receiving updates about changes to our HELLO message.
903 * @param handle connection to transport service
904 * @param rec function previously registered to be called with the HELLOs
905 * @param rec_cls closure for rec
908 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_Handle *handle,
909 GNUNET_TRANSPORT_HelloUpdateCallback rec,
912 struct HelloWaitList *pos;
913 struct HelloWaitList *prev;
916 pos = handle->hwl_head;
919 if ( (pos->rec == rec) &&
920 (pos->rec_cls == rec_cls) )
925 GNUNET_break (pos != NULL);
929 handle->hwl_head = pos->next;
931 prev->next = pos->next;
937 * Send HELLO message to the service.
939 * @param cls the HELLO message to send
940 * @param size number of bytes available in buf
941 * @param buf where to copy the message
942 * @return number of bytes copied to buf
945 send_hello (void *cls, size_t size, void *buf)
947 struct GNUNET_MessageHeader *hello = cls;
952 #if DEBUG_TRANSPORT_TIMEOUT
953 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
954 "Timeout while trying to transmit `%s' request.\n",
961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
962 "Transmitting `%s' request.\n", "HELLO");
964 msize = ntohs (hello->size);
965 GNUNET_assert (size >= msize);
966 memcpy (buf, hello, msize);
973 * Offer the transport service the HELLO of another peer. Note that
974 * the transport service may just ignore this message if the HELLO is
975 * malformed or useless due to our local configuration.
977 * @param handle connection to transport service
978 * @param hello the hello message
981 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
982 const struct GNUNET_MessageHeader *hello)
984 struct GNUNET_MessageHeader *hc;
986 struct GNUNET_PeerIdentity peer;
988 GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
989 size = ntohs (hello->size);
990 GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
991 if (GNUNET_OK != GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message*) hello,
998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
999 "Offering `%s' message of `%4s' to transport for validation.\n",
1001 GNUNET_i2s (&peer));
1003 hc = GNUNET_malloc (size);
1004 memcpy (hc, hello, size);
1005 schedule_control_transmit (handle,
1007 GNUNET_NO, OFFER_HELLO_TIMEOUT, &send_hello, hc);
1012 * Transmit START message to service.
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_start (void *cls, size_t size, void *buf)
1022 struct GNUNET_MessageHeader *s = buf;
1026 /* Can only be shutdown, just give up */
1028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1029 "Shutdown while trying to transmit `%s' request.\n",
1035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1036 "Transmitting `%s' request.\n", "START");
1038 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
1039 s->size = htons (sizeof (struct GNUNET_MessageHeader));
1040 s->type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
1041 return sizeof (struct GNUNET_MessageHeader);
1048 * @param n the entry to free
1051 neighbour_free (struct NeighbourList *n)
1053 struct GNUNET_TRANSPORT_Handle *h;
1054 struct NeighbourList *prev;
1055 struct NeighbourList *pos;
1059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1060 "Removing neighbour `%s' from list of connected peers.\n",
1061 GNUNET_i2s (&n->id));
1063 GNUNET_break (n->is_connected == GNUNET_NO);
1064 GNUNET_break (n->transmit_stage == TS_NEW);
1067 pos = h->neighbours;
1074 h->neighbours = n->next;
1076 prev->next = n->next;
1082 * Mark neighbour as disconnected.
1084 * @param n the entry to mark as disconnected
1087 neighbour_disconnect (struct NeighbourList *n)
1089 struct GNUNET_TRANSPORT_Handle *h = n->h;
1091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1092 "Removing neighbour `%s' from list of connected peers.\n",
1093 GNUNET_i2s (&n->id));
1095 GNUNET_break (n->is_connected == GNUNET_YES);
1096 n->is_connected = GNUNET_NO;
1097 if (h->nc_cb != NULL)
1098 h->nd_cb (h->cls, &n->id);
1099 if (n->transmit_stage == TS_NEW)
1105 * Function we use for handling incoming messages.
1107 * @param cls closure (struct GNUNET_TRANSPORT_Handle *)
1108 * @param msg message received, NULL on timeout or fatal error
1110 static void demultiplexer (void *cls,
1111 const struct GNUNET_MessageHeader *msg);
1115 * Try again to connect to transport service.
1117 * @param cls the handle to the transport service
1118 * @param tc scheduler context
1121 reconnect (void *cls,
1122 const struct GNUNET_SCHEDULER_TaskContext *tc)
1124 struct GNUNET_TRANSPORT_Handle *h = cls;
1125 struct ControlMessage *pos;
1126 struct NeighbourList *n;
1127 struct NeighbourList *next;
1129 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1130 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1132 /* shutdown, just give up */
1135 /* Forget about all neighbours that we used to be connected to */
1139 #if DEBUG_TRANSPORT_DISCONNECT
1140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1141 "Disconnecting due to reconnect being called\n");
1144 if (n->is_connected)
1145 neighbour_disconnect (n);
1149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1150 "Connecting to transport service.\n");
1152 GNUNET_assert (h->client == NULL);
1153 h->client = GNUNET_CLIENT_connect (h->sched, "transport", h->cfg);
1154 GNUNET_assert (h->client != NULL);
1155 /* make sure we don't send "START" twice, remove existing entry from
1156 queue (if present) */
1157 pos = h->control_head;
1160 if (pos->notify == &send_start)
1162 GNUNET_CONTAINER_DLL_remove (h->control_head,
1165 if (GNUNET_SCHEDULER_NO_TASK != pos->notify_delay_task)
1167 GNUNET_SCHEDULER_cancel (h->sched, pos->notify_delay_task);
1168 pos->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1175 schedule_control_transmit (h,
1176 sizeof (struct GNUNET_MessageHeader),
1178 GNUNET_TIME_UNIT_FOREVER_REL, &send_start, NULL);
1179 GNUNET_CLIENT_receive (h->client,
1180 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1185 * Function that will schedule the job that will try
1186 * to connect us again to the client.
1188 * @param h transport service to reconnect
1191 schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
1194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1195 "Scheduling task to reconnect to transport service in %llu ms.\n",
1196 h->reconnect_delay.value);
1198 GNUNET_assert (h->client == NULL);
1199 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
1201 = GNUNET_SCHEDULER_add_delayed (h->sched,
1202 h->reconnect_delay, &reconnect, h);
1203 if (h->reconnect_delay.value == 0)
1205 h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1209 h->reconnect_delay = GNUNET_TIME_relative_multiply (h->reconnect_delay, 2);
1210 h->reconnect_delay = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
1211 h->reconnect_delay);
1217 * Add neighbour to our list
1219 static struct NeighbourList *
1220 neighbour_add (struct GNUNET_TRANSPORT_Handle *h,
1221 const struct GNUNET_PeerIdentity *pid)
1223 struct NeighbourList *n;
1225 /* check for duplicates */
1226 if (NULL != (n = neighbour_find (h, pid)))
1232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1233 "Creating entry for neighbour `%4s'.\n",
1236 n = GNUNET_malloc (sizeof (struct NeighbourList));
1238 GNUNET_BANDWIDTH_tracker_init (&n->out_tracker,
1239 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1240 MAX_BANDWIDTH_CARRY_S);
1241 n->next = h->neighbours;
1249 * Connect to the transport service. Note that the connection may
1250 * complete (or fail) asynchronously.
1252 * @param sched scheduler to use
1253 * @param cfg configuration to use
1254 * @param cls closure for the callbacks
1255 * @param rec receive function to call
1256 * @param nc function to call on connect events
1257 * @param nd function to call on disconnect events
1259 struct GNUNET_TRANSPORT_Handle *
1260 GNUNET_TRANSPORT_connect (struct GNUNET_SCHEDULER_Handle *sched,
1261 const struct GNUNET_CONFIGURATION_Handle *cfg,
1263 GNUNET_TRANSPORT_ReceiveCallback rec,
1264 GNUNET_TRANSPORT_NotifyConnect nc,
1265 GNUNET_TRANSPORT_NotifyDisconnect nd)
1267 struct GNUNET_TRANSPORT_Handle *ret;
1269 GNUNET_ARM_start_services (cfg, sched, "peerinfo", "transport", NULL);
1270 ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Handle));
1277 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1278 schedule_reconnect (ret);
1284 * Disconnect from the transport service.
1287 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1289 struct GNUNET_TRANSPORT_TransmitHandle *th;
1290 struct NeighbourList *n;
1291 struct HelloWaitList *hwl;
1292 struct GNUNET_CLIENT_Connection *client;
1293 struct ControlMessage *cm;
1296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transport disconnect called!\n");
1298 while (NULL != (n = handle->neighbours))
1300 handle->neighbours = n->next;
1301 switch (n->transmit_stage)
1304 case TS_TRANSMITTED:
1308 case TS_TRANSMITTED_QUEUED:
1309 th = &n->transmit_handle;
1310 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1312 GNUNET_SCHEDULER_cancel (handle->sched,
1313 th->notify_delay_task);
1314 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1316 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
1323 while (NULL != (hwl = handle->hwl_head))
1325 handle->hwl_head = hwl->next;
1326 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1328 ("Disconnect while notification for `%s' still registered.\n"),
1330 if (hwl->rec != NULL)
1331 hwl->rec (hwl->rec_cls, NULL);
1335 /* Check for still scheduled control messages, cancel delay tasks if so */
1336 /* Added because somehow a notify_delay_task is remaining scheduled and is ever so annoying */
1337 while ( (NULL != (cm = handle->control_head)))
1339 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1340 _("Disconnect before control message sent!\n"));
1341 if (cm->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1343 GNUNET_SCHEDULER_cancel (handle->sched, cm->notify_delay_task);
1344 cm->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1346 GNUNET_CONTAINER_DLL_remove (handle->control_head,
1347 handle->control_tail,
1353 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1355 GNUNET_SCHEDULER_cancel (handle->sched, handle->reconnect_task);
1356 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1358 if (handle->quota_task != GNUNET_SCHEDULER_NO_TASK)
1360 GNUNET_SCHEDULER_cancel (handle->sched, handle->quota_task);
1361 handle->quota_task = GNUNET_SCHEDULER_NO_TASK;
1363 GNUNET_free_non_null (handle->my_hello);
1364 handle->my_hello = NULL;
1365 GNUNET_ARM_stop_services (handle->cfg, handle->sched, "transport",
1367 if (NULL != handle->network_handle)
1369 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->network_handle);
1370 handle->network_handle = NULL;
1372 if (NULL != (client = handle->client))
1375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1376 "Disconnecting from transport service for good.\n");
1378 handle->client = NULL;
1379 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
1381 GNUNET_free (handle);
1386 * Function we use for handling incoming messages.
1388 * @param cls closure (struct GNUNET_TRANSPORT_Handle *)
1389 * @param msg message received, NULL on timeout or fatal error
1392 demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
1394 struct GNUNET_TRANSPORT_Handle *h = cls;
1395 const struct DisconnectInfoMessage *dim;
1396 const struct ConnectInfoMessage *cim;
1397 const struct InboundMessage *im;
1398 const struct GNUNET_MessageHeader *imm;
1399 const struct SendOkMessage *okm;
1400 struct HelloWaitList *hwl;
1401 struct HelloWaitList *next_hwl;
1402 struct NeighbourList *n;
1403 struct GNUNET_PeerIdentity me;
1406 if (h->client == NULL)
1408 /* shutdown initiated from 'GNUNET_TRANSPORT_disconnect',
1409 finish clean up work! */
1416 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1417 "Error receiving from transport service, disconnecting temporarily.\n");
1419 if (h->network_handle != NULL)
1421 GNUNET_CLIENT_notify_transmit_ready_cancel (h->network_handle);
1422 h->network_handle = NULL;
1424 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
1426 schedule_reconnect (h);
1429 GNUNET_CLIENT_receive (h->client,
1430 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1431 size = ntohs (msg->size);
1432 switch (ntohs (msg->type))
1434 case GNUNET_MESSAGE_TYPE_HELLO:
1436 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg,
1443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1444 "Receiving (my own) `%s' message, I am `%4s'.\n",
1445 "HELLO", GNUNET_i2s (&me));
1447 GNUNET_free_non_null (h->my_hello);
1449 if (size < sizeof (struct GNUNET_MessageHeader))
1454 h->my_hello = GNUNET_malloc (size);
1455 memcpy (h->my_hello, msg, size);
1459 next_hwl = hwl->next;
1460 hwl->rec (hwl->rec_cls,
1461 (const struct GNUNET_MessageHeader *) h->my_hello);
1465 case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
1466 if (size != sizeof (struct ConnectInfoMessage))
1471 cim = (const struct ConnectInfoMessage *) msg;
1473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1474 "Receiving `%s' message for `%4s'.\n",
1475 "CONNECT", GNUNET_i2s (&cim->id));
1477 n = neighbour_find (h, &cim->id);
1479 n = neighbour_add (h,
1481 GNUNET_break (n->is_connected == GNUNET_NO);
1482 n->is_connected = GNUNET_YES;
1483 if (h->nc_cb != NULL)
1484 h->nc_cb (h->cls, &n->id,
1485 GNUNET_TIME_relative_ntoh (cim->latency),
1486 ntohs (cim->distance));
1488 case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
1489 if (size != sizeof (struct DisconnectInfoMessage))
1494 dim = (const struct DisconnectInfoMessage *) msg;
1495 #if DEBUG_TRANSPORT_DISCONNECT
1496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1497 "Receiving `%s' message for `%4s'.\n",
1499 GNUNET_i2s (&dim->peer));
1501 n = neighbour_find (h, &dim->peer);
1502 GNUNET_break (n != NULL);
1504 neighbour_disconnect (n);
1506 case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
1507 if (size != sizeof (struct SendOkMessage))
1512 okm = (const struct SendOkMessage *) msg;
1514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1515 "Receiving `%s' message, transmission %s.\n", "SEND_OK",
1516 ntohl (okm->success) == GNUNET_OK ? "succeeded" : "failed");
1518 n = neighbour_find (h, &okm->peer);
1519 GNUNET_assert (n != NULL);
1520 switch (n->transmit_stage)
1528 case TS_TRANSMITTED:
1529 n->transmit_stage = TS_NEW;
1531 case TS_TRANSMITTED_QUEUED:
1532 n->transmit_stage = TS_QUEUED;
1533 schedule_transmission (h);
1539 case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
1541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1542 "Receiving `%s' message.\n", "RECV");
1545 sizeof (struct InboundMessage) +
1546 sizeof (struct GNUNET_MessageHeader))
1551 im = (const struct InboundMessage *) msg;
1552 imm = (const struct GNUNET_MessageHeader *) &im[1];
1553 if (ntohs (imm->size) + sizeof (struct InboundMessage) != size)
1559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1560 "Received message of type %u from `%4s'.\n",
1561 ntohs (imm->type), GNUNET_i2s (&im->peer));
1563 n = neighbour_find (h, &im->peer);
1565 n = neighbour_add (h, &im->peer);
1569 h->rec (h->cls, &im->peer, imm,
1570 GNUNET_TIME_relative_ntoh (im->latency), ntohs(im->distance));
1573 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1575 ("Received unexpected message of type %u in %s:%u\n"),
1576 ntohs (msg->type), __FILE__, __LINE__);
1584 * Called when our transmit request timed out before any transport
1585 * reported success connecting to the desired peer or before the
1586 * transport was ready to receive. Signal error and free
1589 * @param cls the 'struct GNUNET_TRANSPORT_TransmitHandle*' that is timing out
1590 * @param tc scheduler context
1593 peer_transmit_timeout (void *cls,
1594 const struct GNUNET_SCHEDULER_TaskContext *tc)
1596 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
1597 struct NeighbourList *n;
1598 GNUNET_CONNECTION_TransmitReadyNotify notify;
1601 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1605 "Triggering timeout for request to transmit to `%4s' (%d)\n",
1606 GNUNET_i2s (&n->id),
1609 notify = th->notify;
1610 notify_cls = th->notify_cls;
1611 switch (n->transmit_stage)
1617 n->transmit_stage = TS_NEW;
1618 if (n->is_connected == GNUNET_NO)
1621 case TS_TRANSMITTED:
1624 case TS_TRANSMITTED_QUEUED:
1625 n->transmit_stage = TS_TRANSMITTED;
1631 notify (notify_cls, 0, NULL);
1636 * Check if we could queue a message of the given size for
1637 * transmission. The transport service will take both its
1638 * internal buffers and bandwidth limits imposed by the
1639 * other peer into consideration when answering this query.
1641 * @param handle connection to transport service
1642 * @param target who should receive the message
1643 * @param size how big is the message we want to transmit?
1644 * @param priority how important is the message?
1645 * @param timeout after how long should we give up (and call
1646 * notify with buf NULL and size 0)?
1647 * @param notify function to call when we are ready to
1648 * send such a message
1649 * @param notify_cls closure for notify
1650 * @return NULL if someone else is already waiting to be notified
1651 * non-NULL if the notify callback was queued (can be used to cancel
1652 * using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1654 struct GNUNET_TRANSPORT_TransmitHandle *
1655 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle
1657 const struct GNUNET_PeerIdentity
1658 *target, size_t size,
1659 unsigned int priority,
1660 struct GNUNET_TIME_Relative timeout,
1661 GNUNET_CONNECTION_TransmitReadyNotify
1662 notify, void *notify_cls)
1664 struct GNUNET_TRANSPORT_TransmitHandle *th;
1665 struct NeighbourList *n;
1667 if (size + sizeof (struct OutboundMessage) >=
1668 GNUNET_SERVER_MAX_MESSAGE_SIZE)
1671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1672 "Message size is %d, max allowed is %d.\n",
1673 size + sizeof (struct OutboundMessage), GNUNET_SERVER_MAX_MESSAGE_SIZE);
1679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1680 "Asking transport service for transmission of %u bytes to peer `%4s' within %llu ms.\n",
1681 size, GNUNET_i2s (target),
1682 (unsigned long long) timeout.value);
1684 n = neighbour_find (handle, target);
1686 n = neighbour_add (handle, target);
1689 switch (n->transmit_stage)
1692 n->transmit_stage = TS_QUEUED;
1697 case TS_TRANSMITTED:
1698 n->transmit_stage = TS_TRANSMITTED_QUEUED;
1700 case TS_TRANSMITTED_QUEUED:
1707 th = &n->transmit_handle;
1709 th->notify = notify;
1710 th->notify_cls = notify_cls;
1711 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1712 th->notify_size = size + sizeof (struct OutboundMessage);
1713 th->priority = priority;
1714 th->notify_delay_task
1715 = GNUNET_SCHEDULER_add_delayed (handle->sched, timeout,
1716 &peer_transmit_timeout, th);
1717 schedule_transmission (handle);
1723 * Cancel the specified transmission-ready notification.
1726 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
1727 GNUNET_TRANSPORT_TransmitHandle
1730 struct NeighbourList *n;
1734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1735 "Transmission request of %u bytes to `%4s' was cancelled.\n",
1736 th->notify_size - sizeof (struct OutboundMessage),
1737 GNUNET_i2s (&n->id));
1739 switch (n->transmit_stage)
1745 n->transmit_stage = TS_NEW;
1746 if (n->is_connected == GNUNET_NO)
1749 case TS_TRANSMITTED:
1752 case TS_TRANSMITTED_QUEUED:
1753 n->transmit_stage = TS_TRANSMITTED;
1761 /* end of transport_api.c */