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;
361 * Set once we are in the process of disconnecting from the
369 // FIXME: replace with hash map!
371 * Get the neighbour list entry for the given peer
373 * @param h our context
374 * @param peer peer to look up
375 * @return NULL if no such peer entry exists
377 static struct NeighbourList *
378 neighbour_find (struct GNUNET_TRANSPORT_Handle *h,
379 const struct GNUNET_PeerIdentity *peer)
381 struct NeighbourList *pos;
384 while ((pos != NULL) &&
385 (0 != memcmp (peer, &pos->id, sizeof (struct GNUNET_PeerIdentity))))
392 * Schedule the task to send one message, either from the control
393 * list or the peer message queues to the service.
395 static void schedule_transmission (struct GNUNET_TRANSPORT_Handle *h);
399 * Function called by the scheduler when the timeout for bandwidth
400 * availablility for the target neighbour is reached.
402 * @param cls the 'struct GNUNET_TRANSPORT_Handle*'
403 * @param tc scheduler context
406 quota_transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
408 struct GNUNET_TRANSPORT_Handle *h = cls;
410 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
411 schedule_transmission (h);
416 * Figure out which transmission to a peer can be done right now.
417 * If none can, schedule a task to call 'schedule_transmission'
418 * whenever a peer transmission can be done in the future and
419 * return NULL. Otherwise return the next transmission to be
422 * @param h handle to transport
423 * @return NULL to wait longer before doing any peer transmissions
425 static struct GNUNET_TRANSPORT_TransmitHandle *
426 schedule_peer_transmission (struct GNUNET_TRANSPORT_Handle *h)
428 struct GNUNET_TRANSPORT_TransmitHandle *ret;
429 struct GNUNET_TRANSPORT_TransmitHandle *th;
430 struct NeighbourList *n;
431 struct NeighbourList *next;
432 struct GNUNET_TIME_Relative retry_time;
433 struct GNUNET_TIME_Relative duration;
435 if (h->quota_task != GNUNET_SCHEDULER_NO_TASK)
437 GNUNET_SCHEDULER_cancel (h->sched,
439 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
441 retry_time = GNUNET_TIME_UNIT_FOREVER_REL;
443 next = h->neighbours;
444 while (NULL != (n = next))
447 if (n->transmit_stage != TS_QUEUED)
448 continue; /* not eligible */
449 th = &n->transmit_handle;
450 GNUNET_break (n == th->neighbour);
451 /* check outgoing quota */
452 duration = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
453 th->notify_size - sizeof (struct OutboundMessage));
454 if (th->timeout.value < duration.value)
456 /* signal timeout! */
458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
459 "Would need %llu ms before bandwidth is available for delivery to `%4s', that is too long. Signaling timeout.\n",
461 GNUNET_i2s (&n->id));
463 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
465 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
466 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
468 n->transmit_stage = TS_NEW;
469 if (NULL != th->notify)
470 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
473 if (duration.value > 0)
476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
477 "Need more bandwidth (%u b/s allowed, %u b needed), delaying delivery to `%4s' by %llu ms\n",
478 (unsigned int) n->out_tracker.available_bytes_per_s__,
479 (unsigned int) th->notify_size - sizeof (struct OutboundMessage),
483 retry_time = GNUNET_TIME_relative_min (retry_time,
488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
489 "Have %u bytes of bandwidth available for transmission to `%4s' right now\n",
490 th->notify_size - sizeof (struct OutboundMessage),
491 GNUNET_i2s (&n->id));
493 if ( (ret == NULL) ||
494 (ret->priority < th->priority) )
498 h->quota_task = GNUNET_SCHEDULER_add_delayed (h->sched,
500 "a_transmit_ready,
507 * Transmit message(s) to service.
509 * @param cls handle to transport
510 * @param size number of bytes available in buf
511 * @param buf where to copy the message
512 * @return number of bytes copied to buf
515 transport_notify_ready (void *cls, size_t size, void *buf)
517 struct GNUNET_TRANSPORT_Handle *h = cls;
518 struct ControlMessage *cm;
519 struct GNUNET_TRANSPORT_TransmitHandle *th;
520 struct NeighbourList *n;
521 struct OutboundMessage obm;
527 h->network_handle = NULL;
530 schedule_transmission (h);
534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
535 "Ready to transmit %u bytes to transport service\n", size);
539 while ( (NULL != (cm = h->control_head)) &&
540 (cm->notify_size <= size) )
542 if (cm->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
544 GNUNET_SCHEDULER_cancel (h->sched, cm->notify_delay_task);
545 cm->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
547 GNUNET_CONTAINER_DLL_remove (h->control_head,
550 nret = cm->notify (cm->notify_cls, size, &cbuf[ret]);
555 while ( (NULL != (th = schedule_peer_transmission (h))) &&
556 (th->notify_size <= size) )
558 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
560 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
561 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
564 switch (n->transmit_stage)
570 n->transmit_stage = TS_TRANSMITTED;
575 case TS_TRANSMITTED_QUEUED:
581 GNUNET_assert (size >= sizeof (struct OutboundMessage));
582 mret = th->notify (th->notify_cls,
583 size - sizeof (struct OutboundMessage),
584 &cbuf[ret + sizeof (struct OutboundMessage)]);
585 GNUNET_assert (mret <= size - sizeof (struct OutboundMessage));
587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
588 "Message of %u bytes with timeout %llums constructed for `%4s'\n",
590 (unsigned long long) GNUNET_TIME_absolute_get_remaining (th->timeout).value,
591 GNUNET_i2s (&n->id));
595 obm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
596 obm.header.size = htons (mret + sizeof (struct OutboundMessage));
597 obm.priority = htonl (th->priority);
598 obm.timeout = GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining (th->timeout));
600 memcpy (&cbuf[ret], &obm, sizeof (struct OutboundMessage));
601 ret += (mret + sizeof (struct OutboundMessage));
602 size -= (mret + sizeof (struct OutboundMessage));
603 GNUNET_BANDWIDTH_tracker_consume (&n->out_tracker, mret);
607 switch (n->transmit_stage)
616 n->transmit_stage = TS_NEW;
618 case TS_TRANSMITTED_QUEUED:
619 n->transmit_stage = TS_QUEUED;
626 schedule_transmission (h);
628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
629 "Transmitting %u bytes to transport service\n", ret);
636 * Schedule the task to send one message, either from the control
637 * list or the peer message queues to the service.
640 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
643 struct GNUNET_TIME_Relative timeout;
644 struct GNUNET_TRANSPORT_TransmitHandle *th;
646 if (NULL != h->network_handle)
648 if (h->client == NULL)
650 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
651 _("Could not yet schedule transmission: we are not yet connected to the transport service!\n"));
652 return; /* not yet connected */
654 if (NULL != h->control_head)
656 size = h->control_head->notify_size;
657 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
661 th = schedule_peer_transmission (h);
664 /* no transmission ready right now */
666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
667 "Could not yet schedule transmission: none ready\n");
671 size = th->notify_size;
672 timeout = GNUNET_TIME_absolute_get_remaining (th->timeout);
675 GNUNET_CLIENT_notify_transmit_ready (h->client,
679 &transport_notify_ready,
681 GNUNET_assert (NULL != h->network_handle);
686 * Called when our transmit request timed out before any transport
687 * reported success connecting to the desired peer or before the
688 * transport was ready to receive. Signal error and free
692 control_transmit_timeout (void *cls,
693 const struct GNUNET_SCHEDULER_TaskContext *tc)
695 struct ControlMessage *th = cls;
697 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
698 if (NULL != th->notify)
699 th->notify (th->notify_cls, 0, NULL);
700 GNUNET_CONTAINER_DLL_remove (th->h->control_head,
708 * Queue control request for transmission to the transport
711 * @param h handle to the transport service
712 * @param size number of bytes to be transmitted
713 * @param at_head request must be added to the head of the queue
714 * (otherwise request will be appended)
715 * @param timeout how long this transmission can wait (at most)
716 * @param notify function to call to get the content
717 * @param notify_cls closure for notify
720 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h,
723 struct GNUNET_TIME_Relative timeout,
724 GNUNET_CONNECTION_TransmitReadyNotify notify,
727 struct ControlMessage *th;
730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
731 "Control transmit of %u bytes within %llums requested\n",
732 size, (unsigned long long) timeout.value);
734 th = GNUNET_malloc (sizeof (struct ControlMessage));
737 th->notify_cls = notify_cls;
738 th->notify_size = size;
739 th->notify_delay_task
740 = GNUNET_SCHEDULER_add_delayed (h->sched,
741 timeout, &control_transmit_timeout, th);
743 GNUNET_CONTAINER_DLL_insert (h->control_head,
747 GNUNET_CONTAINER_DLL_insert_after (h->control_head,
751 schedule_transmission (h);
755 struct SetQuotaContext
757 struct GNUNET_TRANSPORT_Handle *handle;
759 struct GNUNET_PeerIdentity target;
761 GNUNET_SCHEDULER_Task cont;
765 struct GNUNET_TIME_Absolute timeout;
767 struct GNUNET_BANDWIDTH_Value32NBO quota_in;
772 * Send SET_QUOTA message to the service.
774 * @param cls the 'struct SetQuotaContext'
775 * @param size number of bytes available in buf
776 * @param buf where to copy the message
777 * @return number of bytes copied to buf
780 send_set_quota (void *cls, size_t size, void *buf)
782 struct SetQuotaContext *sqc = cls;
783 struct QuotaSetMessage *msg;
787 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
790 GNUNET_SCHEDULER_REASON_TIMEOUT);
795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
796 "Transmitting `%s' request with respect to `%4s'.\n",
798 GNUNET_i2s (&sqc->target));
800 GNUNET_assert (size >= sizeof (struct QuotaSetMessage));
802 msg->header.size = htons (sizeof (struct QuotaSetMessage));
803 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
804 msg->quota = sqc->quota_in;
805 memcpy (&msg->peer, &sqc->target, sizeof (struct GNUNET_PeerIdentity));
806 if (sqc->cont != NULL)
807 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
810 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
812 return sizeof (struct QuotaSetMessage);
817 * Set the share of incoming bandwidth for the given
818 * peer to the specified amount.
820 * @param handle connection to transport service
821 * @param target who's bandwidth quota is being changed
822 * @param quota_in incoming bandwidth quota in bytes per ms
823 * @param quota_out outgoing bandwidth quota in bytes per ms
824 * @param timeout how long to wait until signaling failure if
825 * we can not communicate the quota change
826 * @param cont continuation to call when done, will be called
827 * either with reason "TIMEOUT" or with reason "PREREQ_DONE"
828 * @param cont_cls closure for continuation
831 GNUNET_TRANSPORT_set_quota (struct GNUNET_TRANSPORT_Handle *handle,
832 const struct GNUNET_PeerIdentity *target,
833 struct GNUNET_BANDWIDTH_Value32NBO quota_in,
834 struct GNUNET_BANDWIDTH_Value32NBO quota_out,
835 struct GNUNET_TIME_Relative timeout,
836 GNUNET_SCHEDULER_Task cont, void *cont_cls)
838 struct NeighbourList *n;
839 struct SetQuotaContext *sqc;
841 n = neighbour_find (handle, target);
845 if (ntohl (quota_out.value__) != n->out_tracker.available_bytes_per_s__)
846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
847 "Quota changed from %u to %u for peer `%s'\n",
848 (unsigned int) n->out_tracker.available_bytes_per_s__,
849 (unsigned int) ntohl (quota_out.value__),
850 GNUNET_i2s (target));
852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
853 "Quota remains at %u for peer `%s'\n",
854 (unsigned int) n->out_tracker.available_bytes_per_s__,
855 GNUNET_i2s (target));
857 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker,
863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
864 "Quota changed to %u for peer `%s', but I have no such neighbour!\n",
865 (unsigned int) ntohl (quota_out.value__),
866 GNUNET_i2s (target));
869 sqc = GNUNET_malloc (sizeof (struct SetQuotaContext));
870 sqc->handle = handle;
871 sqc->target = *target;
873 sqc->cont_cls = cont_cls;
874 sqc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
875 sqc->quota_in = quota_in;
876 schedule_control_transmit (handle,
877 sizeof (struct QuotaSetMessage),
878 GNUNET_NO, timeout, &send_set_quota, sqc);
883 * Obtain the HELLO message for this peer.
885 * @param handle connection to transport service
886 * @param rec function to call with the HELLO, sender will be our peer
887 * identity; message and sender will be NULL on timeout
888 * (handshake with transport service pending/failed).
889 * cost estimate will be 0.
890 * @param rec_cls closure for rec
893 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
894 GNUNET_TRANSPORT_HelloUpdateCallback rec,
897 struct HelloWaitList *hwl;
899 hwl = GNUNET_malloc (sizeof (struct HelloWaitList));
900 hwl->next = handle->hwl_head;
901 handle->hwl_head = hwl;
902 hwl->handle = handle;
904 hwl->rec_cls = rec_cls;
905 if (handle->my_hello == NULL)
907 rec (rec_cls, (const struct GNUNET_MessageHeader *) handle->my_hello);
913 * Stop receiving updates about changes to our HELLO message.
915 * @param handle connection to transport service
916 * @param rec function previously registered to be called with the HELLOs
917 * @param rec_cls closure for rec
920 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_Handle *handle,
921 GNUNET_TRANSPORT_HelloUpdateCallback rec,
924 struct HelloWaitList *pos;
925 struct HelloWaitList *prev;
928 pos = handle->hwl_head;
931 if ( (pos->rec == rec) &&
932 (pos->rec_cls == rec_cls) )
937 GNUNET_break (pos != NULL);
941 handle->hwl_head = pos->next;
943 prev->next = pos->next;
949 * Send HELLO message to the service.
951 * @param cls the HELLO message to send
952 * @param size number of bytes available in buf
953 * @param buf where to copy the message
954 * @return number of bytes copied to buf
957 send_hello (void *cls, size_t size, void *buf)
959 struct GNUNET_MessageHeader *hello = cls;
964 #if DEBUG_TRANSPORT_TIMEOUT
965 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
966 "Timeout while trying to transmit `%s' request.\n",
973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
974 "Transmitting `%s' request.\n", "HELLO");
976 msize = ntohs (hello->size);
977 GNUNET_assert (size >= msize);
978 memcpy (buf, hello, msize);
985 * Offer the transport service the HELLO of another peer. Note that
986 * the transport service may just ignore this message if the HELLO is
987 * malformed or useless due to our local configuration.
989 * @param handle connection to transport service
990 * @param hello the hello message
993 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
994 const struct GNUNET_MessageHeader *hello)
996 struct GNUNET_MessageHeader *hc;
998 struct GNUNET_PeerIdentity peer;
1000 GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
1001 size = ntohs (hello->size);
1002 GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
1003 if (GNUNET_OK != GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message*) hello,
1010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1011 "Offering `%s' message of `%4s' to transport for validation.\n",
1013 GNUNET_i2s (&peer));
1015 hc = GNUNET_malloc (size);
1016 memcpy (hc, hello, size);
1017 schedule_control_transmit (handle,
1019 GNUNET_NO, OFFER_HELLO_TIMEOUT, &send_hello, hc);
1024 * Transmit START message to service.
1027 * @param size number of bytes available in buf
1028 * @param buf where to copy the message
1029 * @return number of bytes copied to buf
1032 send_start (void *cls, size_t size, void *buf)
1034 struct GNUNET_MessageHeader *s = buf;
1038 /* Can only be shutdown, just give up */
1040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1041 "Shutdown while trying to transmit `%s' request.\n",
1047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1048 "Transmitting `%s' request.\n", "START");
1050 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
1051 s->size = htons (sizeof (struct GNUNET_MessageHeader));
1052 s->type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
1053 return sizeof (struct GNUNET_MessageHeader);
1060 * @param n the entry to free
1063 neighbour_free (struct NeighbourList *n)
1065 struct GNUNET_TRANSPORT_Handle *h;
1066 struct NeighbourList *prev;
1067 struct NeighbourList *pos;
1071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1072 "Removing neighbour `%s' from list of connected peers.\n",
1073 GNUNET_i2s (&n->id));
1075 GNUNET_break (n->is_connected == GNUNET_NO);
1076 GNUNET_break (n->transmit_stage == TS_NEW);
1079 pos = h->neighbours;
1086 h->neighbours = n->next;
1088 prev->next = n->next;
1094 * Mark neighbour as disconnected.
1096 * @param n the entry to mark as disconnected
1099 neighbour_disconnect (struct NeighbourList *n)
1101 struct GNUNET_TRANSPORT_Handle *h = n->h;
1103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1104 "Removing neighbour `%s' from list of connected peers.\n",
1105 GNUNET_i2s (&n->id));
1107 GNUNET_break (n->is_connected == GNUNET_YES);
1108 n->is_connected = GNUNET_NO;
1109 if (h->nc_cb != NULL)
1110 h->nd_cb (h->cls, &n->id);
1111 if (n->transmit_stage == TS_NEW)
1117 * Function we use for handling incoming messages.
1119 * @param cls closure (struct GNUNET_TRANSPORT_Handle *)
1120 * @param msg message received, NULL on timeout or fatal error
1122 static void demultiplexer (void *cls,
1123 const struct GNUNET_MessageHeader *msg);
1127 * Try again to connect to transport service.
1129 * @param cls the handle to the transport service
1130 * @param tc scheduler context
1133 reconnect (void *cls,
1134 const struct GNUNET_SCHEDULER_TaskContext *tc)
1136 struct GNUNET_TRANSPORT_Handle *h = cls;
1137 struct ControlMessage *pos;
1138 struct NeighbourList *n;
1139 struct NeighbourList *next;
1141 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1142 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1144 /* shutdown, just give up */
1147 /* Forget about all neighbours that we used to be connected to */
1151 #if DEBUG_TRANSPORT_DISCONNECT
1152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1153 "Disconnecting due to reconnect being called\n");
1156 if (n->is_connected)
1157 neighbour_disconnect (n);
1161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1162 "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 * @return NULL if this API is currently disconnecting from the service
1233 static struct NeighbourList *
1234 neighbour_add (struct GNUNET_TRANSPORT_Handle *h,
1235 const struct GNUNET_PeerIdentity *pid)
1237 struct NeighbourList *n;
1239 if (GNUNET_YES == h->in_disconnect)
1241 /* check for duplicates */
1242 if (NULL != (n = neighbour_find (h, pid)))
1248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1249 "Creating entry for neighbour `%4s'.\n",
1252 n = GNUNET_malloc (sizeof (struct NeighbourList));
1254 GNUNET_BANDWIDTH_tracker_init (&n->out_tracker,
1255 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1256 MAX_BANDWIDTH_CARRY_S);
1257 n->next = h->neighbours;
1265 * Connect to the transport service. Note that the connection may
1266 * complete (or fail) asynchronously.
1268 * @param sched scheduler to use
1269 * @param cfg configuration to use
1270 * @param cls closure for the callbacks
1271 * @param rec receive function to call
1272 * @param nc function to call on connect events
1273 * @param nd function to call on disconnect events
1275 struct GNUNET_TRANSPORT_Handle *
1276 GNUNET_TRANSPORT_connect (struct GNUNET_SCHEDULER_Handle *sched,
1277 const struct GNUNET_CONFIGURATION_Handle *cfg,
1279 GNUNET_TRANSPORT_ReceiveCallback rec,
1280 GNUNET_TRANSPORT_NotifyConnect nc,
1281 GNUNET_TRANSPORT_NotifyDisconnect nd)
1283 struct GNUNET_TRANSPORT_Handle *ret;
1285 ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Handle));
1292 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1293 schedule_reconnect (ret);
1299 * Disconnect from the transport service.
1302 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1304 struct GNUNET_TRANSPORT_TransmitHandle *th;
1305 struct NeighbourList *n;
1306 struct HelloWaitList *hwl;
1307 struct GNUNET_CLIENT_Connection *client;
1308 struct ControlMessage *cm;
1311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transport disconnect called!\n");
1313 handle->in_disconnect = GNUNET_YES;
1314 while (NULL != (n = handle->neighbours))
1316 handle->neighbours = n->next;
1317 switch (n->transmit_stage)
1320 case TS_TRANSMITTED:
1324 case TS_TRANSMITTED_QUEUED:
1325 th = &n->transmit_handle;
1326 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1328 GNUNET_SCHEDULER_cancel (handle->sched,
1329 th->notify_delay_task);
1330 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1332 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
1339 while (NULL != (hwl = handle->hwl_head))
1341 handle->hwl_head = hwl->next;
1342 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1344 ("Disconnect while notification for `%s' still registered.\n"),
1346 if (hwl->rec != NULL)
1347 hwl->rec (hwl->rec_cls, NULL);
1351 /* Check for still scheduled control messages, cancel delay tasks if so */
1352 /* Added because somehow a notify_delay_task is remaining scheduled and is ever so annoying */
1353 while ( (NULL != (cm = handle->control_head)))
1355 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1356 _("Disconnect before control message sent!\n"));
1357 if (cm->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1359 GNUNET_SCHEDULER_cancel (handle->sched, cm->notify_delay_task);
1360 cm->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1362 GNUNET_CONTAINER_DLL_remove (handle->control_head,
1363 handle->control_tail,
1369 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1371 GNUNET_SCHEDULER_cancel (handle->sched, handle->reconnect_task);
1372 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1374 if (handle->quota_task != GNUNET_SCHEDULER_NO_TASK)
1376 GNUNET_SCHEDULER_cancel (handle->sched, handle->quota_task);
1377 handle->quota_task = GNUNET_SCHEDULER_NO_TASK;
1379 GNUNET_free_non_null (handle->my_hello);
1380 handle->my_hello = NULL;
1382 if (NULL != handle->network_handle)
1384 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->network_handle);
1385 handle->network_handle = NULL;
1387 if (NULL != (client = handle->client))
1390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1391 "Disconnecting from transport service for good.\n");
1393 handle->client = NULL;
1394 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
1396 GNUNET_free (handle);
1401 * Function we use for handling incoming messages.
1403 * @param cls closure (struct GNUNET_TRANSPORT_Handle *)
1404 * @param msg message received, NULL on timeout or fatal error
1407 demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
1409 struct GNUNET_TRANSPORT_Handle *h = cls;
1410 const struct DisconnectInfoMessage *dim;
1411 const struct ConnectInfoMessage *cim;
1412 const struct InboundMessage *im;
1413 const struct GNUNET_MessageHeader *imm;
1414 const struct SendOkMessage *okm;
1415 struct HelloWaitList *hwl;
1416 struct HelloWaitList *next_hwl;
1417 struct NeighbourList *n;
1418 struct GNUNET_PeerIdentity me;
1421 if (h->client == NULL)
1423 /* shutdown initiated from 'GNUNET_TRANSPORT_disconnect',
1424 finish clean up work! */
1431 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1432 "Error receiving from transport service, disconnecting temporarily.\n");
1434 if (h->network_handle != NULL)
1436 GNUNET_CLIENT_notify_transmit_ready_cancel (h->network_handle);
1437 h->network_handle = NULL;
1439 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
1441 schedule_reconnect (h);
1444 GNUNET_CLIENT_receive (h->client,
1445 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1446 size = ntohs (msg->size);
1447 switch (ntohs (msg->type))
1449 case GNUNET_MESSAGE_TYPE_HELLO:
1451 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg,
1458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1459 "Receiving (my own) `%s' message, I am `%4s'.\n",
1460 "HELLO", GNUNET_i2s (&me));
1462 GNUNET_free_non_null (h->my_hello);
1464 if (size < sizeof (struct GNUNET_MessageHeader))
1469 h->my_hello = GNUNET_malloc (size);
1470 memcpy (h->my_hello, msg, size);
1474 next_hwl = hwl->next;
1475 hwl->rec (hwl->rec_cls,
1476 (const struct GNUNET_MessageHeader *) h->my_hello);
1480 case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
1481 if (size != sizeof (struct ConnectInfoMessage))
1486 cim = (const struct ConnectInfoMessage *) msg;
1488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1489 "Receiving `%s' message for `%4s'.\n",
1490 "CONNECT", GNUNET_i2s (&cim->id));
1492 n = neighbour_find (h, &cim->id);
1494 n = neighbour_add (h,
1498 GNUNET_break (n->is_connected == GNUNET_NO);
1499 n->is_connected = GNUNET_YES;
1500 if (h->nc_cb != NULL)
1501 h->nc_cb (h->cls, &n->id,
1502 GNUNET_TIME_relative_ntoh (cim->latency),
1503 ntohl (cim->distance));
1505 case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
1506 if (size != sizeof (struct DisconnectInfoMessage))
1511 dim = (const struct DisconnectInfoMessage *) msg;
1512 #if DEBUG_TRANSPORT_DISCONNECT
1513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1514 "Receiving `%s' message for `%4s'.\n",
1516 GNUNET_i2s (&dim->peer));
1518 n = neighbour_find (h, &dim->peer);
1519 GNUNET_break (n != NULL);
1521 neighbour_disconnect (n);
1523 case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
1524 if (size != sizeof (struct SendOkMessage))
1529 okm = (const struct SendOkMessage *) msg;
1531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1532 "Receiving `%s' message, transmission %s.\n", "SEND_OK",
1533 ntohl (okm->success) == GNUNET_OK ? "succeeded" : "failed");
1535 n = neighbour_find (h, &okm->peer);
1536 GNUNET_assert (n != NULL);
1537 switch (n->transmit_stage)
1545 case TS_TRANSMITTED:
1546 n->transmit_stage = TS_NEW;
1548 case TS_TRANSMITTED_QUEUED:
1549 n->transmit_stage = TS_QUEUED;
1550 schedule_transmission (h);
1556 case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
1558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1559 "Receiving `%s' message.\n", "RECV");
1562 sizeof (struct InboundMessage) +
1563 sizeof (struct GNUNET_MessageHeader))
1568 im = (const struct InboundMessage *) msg;
1569 imm = (const struct GNUNET_MessageHeader *) &im[1];
1570 if (ntohs (imm->size) + sizeof (struct InboundMessage) != size)
1576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1577 "Received message of type %u from `%4s'.\n",
1578 ntohs (imm->type), GNUNET_i2s (&im->peer));
1580 n = neighbour_find (h, &im->peer);
1586 if (n->is_connected != GNUNET_YES)
1592 h->rec (h->cls, &im->peer, imm,
1593 GNUNET_TIME_relative_ntoh (im->latency), ntohl(im->distance));
1596 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1598 ("Received unexpected message of type %u in %s:%u\n"),
1599 ntohs (msg->type), __FILE__, __LINE__);
1607 * Called when our transmit request timed out before any transport
1608 * reported success connecting to the desired peer or before the
1609 * transport was ready to receive. Signal error and free
1612 * @param cls the 'struct GNUNET_TRANSPORT_TransmitHandle*' that is timing out
1613 * @param tc scheduler context
1616 peer_transmit_timeout (void *cls,
1617 const struct GNUNET_SCHEDULER_TaskContext *tc)
1619 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
1620 struct NeighbourList *n;
1621 GNUNET_CONNECTION_TransmitReadyNotify notify;
1624 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1628 "Triggering timeout for request to transmit to `%4s' (%d)\n",
1629 GNUNET_i2s (&n->id),
1632 notify = th->notify;
1633 notify_cls = th->notify_cls;
1634 switch (n->transmit_stage)
1640 n->transmit_stage = TS_NEW;
1641 if (n->is_connected == GNUNET_NO)
1644 case TS_TRANSMITTED:
1647 case TS_TRANSMITTED_QUEUED:
1648 n->transmit_stage = TS_TRANSMITTED;
1654 notify (notify_cls, 0, NULL);
1659 * Check if we could queue a message of the given size for
1660 * transmission. The transport service will take both its
1661 * internal buffers and bandwidth limits imposed by the
1662 * other peer into consideration when answering this query.
1664 * @param handle connection to transport service
1665 * @param target who should receive the message
1666 * @param size how big is the message we want to transmit?
1667 * @param priority how important is the message?
1668 * @param timeout after how long should we give up (and call
1669 * notify with buf NULL and size 0)?
1670 * @param notify function to call when we are ready to
1671 * send such a message
1672 * @param notify_cls closure for notify
1673 * @return NULL if someone else is already waiting to be notified
1674 * non-NULL if the notify callback was queued (can be used to cancel
1675 * using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1677 struct GNUNET_TRANSPORT_TransmitHandle *
1678 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle
1680 const struct GNUNET_PeerIdentity
1681 *target, size_t size,
1682 unsigned int priority,
1683 struct GNUNET_TIME_Relative timeout,
1684 GNUNET_CONNECTION_TransmitReadyNotify
1685 notify, void *notify_cls)
1687 struct GNUNET_TRANSPORT_TransmitHandle *th;
1688 struct NeighbourList *n;
1690 if (size + sizeof (struct OutboundMessage) >=
1691 GNUNET_SERVER_MAX_MESSAGE_SIZE)
1694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1695 "Message size is %d, max allowed is %d.\n",
1696 size + sizeof (struct OutboundMessage), GNUNET_SERVER_MAX_MESSAGE_SIZE);
1702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1703 "Asking transport service for transmission of %u bytes to peer `%4s' within %llu ms.\n",
1704 size, GNUNET_i2s (target),
1705 (unsigned long long) timeout.value);
1707 n = neighbour_find (handle, target);
1709 n = neighbour_add (handle, target);
1712 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1713 "Could not create neighbour entry for peer `%s'\n",
1714 GNUNET_i2s (target));
1717 switch (n->transmit_stage)
1720 n->transmit_stage = TS_QUEUED;
1725 case TS_TRANSMITTED:
1726 n->transmit_stage = TS_TRANSMITTED_QUEUED;
1728 case TS_TRANSMITTED_QUEUED:
1735 th = &n->transmit_handle;
1737 th->notify = notify;
1738 th->notify_cls = notify_cls;
1739 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1740 th->notify_size = size + sizeof (struct OutboundMessage);
1741 th->priority = priority;
1742 th->notify_delay_task
1743 = GNUNET_SCHEDULER_add_delayed (handle->sched, timeout,
1744 &peer_transmit_timeout, th);
1745 schedule_transmission (handle);
1751 * Cancel the specified transmission-ready notification.
1754 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
1755 GNUNET_TRANSPORT_TransmitHandle
1758 struct NeighbourList *n;
1762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1763 "Transmission request of %u bytes to `%4s' was cancelled.\n",
1764 th->notify_size - sizeof (struct OutboundMessage),
1765 GNUNET_i2s (&n->id));
1767 switch (n->transmit_stage)
1773 n->transmit_stage = TS_NEW;
1774 if (n->is_connected == GNUNET_NO)
1777 case TS_TRANSMITTED:
1780 case TS_TRANSMITTED_QUEUED:
1781 n->transmit_stage = TS_TRANSMITTED;
1789 /* end of transport_api.c */