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 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/transport_api.c
23 * @brief library to access the low-level P2P IO service
24 * @author Christian Grothoff
27 #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 * Peer identity as assumed by this process, or all zeros.
347 struct GNUNET_PeerIdentity self;
350 * ID of the task trying to reconnect to the service.
352 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
355 * ID of the task trying to trigger transmission for a peer
356 * while maintaining bandwidth quotas.
358 GNUNET_SCHEDULER_TaskIdentifier quota_task;
361 * Delay until we try to reconnect.
363 struct GNUNET_TIME_Relative reconnect_delay;
366 * Set once we are in the process of disconnecting from the
372 * Should we check that 'self' matches what the service thinks?
373 * (if GNUNET_NO, then 'self' is all zeros!).
379 // FIXME: replace with hash map!
381 * Get the neighbour list entry for the given peer
383 * @param h our context
384 * @param peer peer to look up
385 * @return NULL if no such peer entry exists
387 static struct NeighbourList *
388 neighbour_find (struct GNUNET_TRANSPORT_Handle *h,
389 const struct GNUNET_PeerIdentity *peer)
391 struct NeighbourList *pos;
394 while ((pos != NULL) &&
395 (0 != memcmp (peer, &pos->id, sizeof (struct GNUNET_PeerIdentity))))
402 * Schedule the task to send one message, either from the control
403 * list or the peer message queues to the service.
405 static void schedule_transmission (struct GNUNET_TRANSPORT_Handle *h);
409 * Function called by the scheduler when the timeout for bandwidth
410 * availablility for the target neighbour is reached.
412 * @param cls the 'struct GNUNET_TRANSPORT_Handle*'
413 * @param tc scheduler context
416 quota_transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
418 struct GNUNET_TRANSPORT_Handle *h = cls;
420 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
421 schedule_transmission (h);
426 * Figure out which transmission to a peer can be done right now.
427 * If none can, schedule a task to call 'schedule_transmission'
428 * whenever a peer transmission can be done in the future and
429 * return NULL. Otherwise return the next transmission to be
432 * @param h handle to transport
433 * @return NULL to wait longer before doing any peer transmissions
435 static struct GNUNET_TRANSPORT_TransmitHandle *
436 schedule_peer_transmission (struct GNUNET_TRANSPORT_Handle *h)
438 struct GNUNET_TRANSPORT_TransmitHandle *ret;
439 struct GNUNET_TRANSPORT_TransmitHandle *th;
440 struct NeighbourList *n;
441 struct NeighbourList *next;
442 struct GNUNET_TIME_Relative retry_time;
443 struct GNUNET_TIME_Relative duration;
445 if (h->quota_task != GNUNET_SCHEDULER_NO_TASK)
447 GNUNET_SCHEDULER_cancel (h->sched,
449 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
451 retry_time = GNUNET_TIME_UNIT_FOREVER_REL;
453 next = h->neighbours;
454 while (NULL != (n = next))
457 if (n->transmit_stage != TS_QUEUED)
458 continue; /* not eligible */
459 th = &n->transmit_handle;
460 GNUNET_break (n == th->neighbour);
461 /* check outgoing quota */
462 duration = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
463 th->notify_size - sizeof (struct OutboundMessage));
464 if (th->timeout.value < duration.value)
466 /* signal timeout! */
468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
469 "Would need %llu ms before bandwidth is available for delivery to `%4s', that is too long. Signaling timeout.\n",
471 GNUNET_i2s (&n->id));
473 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
475 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
476 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
478 n->transmit_stage = TS_NEW;
479 if (NULL != th->notify)
480 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
483 if (duration.value > 0)
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
487 "Need more bandwidth (%u b/s allowed, %u b needed), delaying delivery to `%4s' by %llu ms\n",
488 (unsigned int) n->out_tracker.available_bytes_per_s__,
489 (unsigned int) th->notify_size - sizeof (struct OutboundMessage),
493 retry_time = GNUNET_TIME_relative_min (retry_time,
498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
499 "Have %u bytes of bandwidth available for transmission to `%4s' right now\n",
500 th->notify_size - sizeof (struct OutboundMessage),
501 GNUNET_i2s (&n->id));
503 if ( (ret == NULL) ||
504 (ret->priority < th->priority) )
508 h->quota_task = GNUNET_SCHEDULER_add_delayed (h->sched,
510 "a_transmit_ready,
517 * Transmit message(s) to service.
519 * @param cls handle to transport
520 * @param size number of bytes available in buf
521 * @param buf where to copy the message
522 * @return number of bytes copied to buf
525 transport_notify_ready (void *cls, size_t size, void *buf)
527 struct GNUNET_TRANSPORT_Handle *h = cls;
528 struct ControlMessage *cm;
529 struct GNUNET_TRANSPORT_TransmitHandle *th;
530 struct NeighbourList *n;
531 struct OutboundMessage obm;
537 h->network_handle = NULL;
540 schedule_transmission (h);
544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
545 "Ready to transmit %u bytes to transport service\n", size);
549 while ( (NULL != (cm = h->control_head)) &&
550 (cm->notify_size <= size) )
552 if (cm->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
554 GNUNET_SCHEDULER_cancel (h->sched, cm->notify_delay_task);
555 cm->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
557 GNUNET_CONTAINER_DLL_remove (h->control_head,
560 nret = cm->notify (cm->notify_cls, size, &cbuf[ret]);
562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
563 "Added %u bytes of control message at %u\n",
571 while ( (NULL != (th = schedule_peer_transmission (h))) &&
572 (th->notify_size <= size) )
574 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
576 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
577 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
580 switch (n->transmit_stage)
586 n->transmit_stage = TS_TRANSMITTED;
591 case TS_TRANSMITTED_QUEUED:
597 GNUNET_assert (size >= sizeof (struct OutboundMessage));
598 mret = th->notify (th->notify_cls,
599 size - sizeof (struct OutboundMessage),
600 &cbuf[ret + sizeof (struct OutboundMessage)]);
601 GNUNET_assert (mret <= size - sizeof (struct OutboundMessage));
603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
604 "Message of %u bytes with timeout %llums constructed for `%4s'\n",
606 (unsigned long long) GNUNET_TIME_absolute_get_remaining (th->timeout).value,
607 GNUNET_i2s (&n->id));
611 GNUNET_assert (mret + sizeof (struct OutboundMessage) < GNUNET_SERVER_MAX_MESSAGE_SIZE);
612 obm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
613 obm.header.size = htons (mret + sizeof (struct OutboundMessage));
614 obm.priority = htonl (th->priority);
615 obm.timeout = GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining (th->timeout));
617 memcpy (&cbuf[ret], &obm, sizeof (struct OutboundMessage));
618 ret += (mret + sizeof (struct OutboundMessage));
619 size -= (mret + sizeof (struct OutboundMessage));
620 GNUNET_BANDWIDTH_tracker_consume (&n->out_tracker, mret);
624 switch (n->transmit_stage)
633 n->transmit_stage = TS_NEW;
635 case TS_TRANSMITTED_QUEUED:
636 n->transmit_stage = TS_QUEUED;
643 schedule_transmission (h);
645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
646 "Transmitting %u bytes to transport service\n", ret);
653 * Schedule the task to send one message, either from the control
654 * list or the peer message queues to the service.
657 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
660 struct GNUNET_TIME_Relative timeout;
661 struct GNUNET_TRANSPORT_TransmitHandle *th;
663 if (NULL != h->network_handle)
665 if (h->client == NULL)
667 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
668 _("Could not yet schedule transmission: we are not yet connected to the transport service!\n"));
669 return; /* not yet connected */
671 if (NULL != h->control_head)
673 size = h->control_head->notify_size;
674 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
678 th = schedule_peer_transmission (h);
681 /* no transmission ready right now */
683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
684 "Could not yet schedule transmission: none ready\n");
688 size = th->notify_size;
689 timeout = GNUNET_TIME_absolute_get_remaining (th->timeout);
692 GNUNET_CLIENT_notify_transmit_ready (h->client,
696 &transport_notify_ready,
698 GNUNET_assert (NULL != h->network_handle);
703 * Called when our transmit request timed out before any transport
704 * reported success connecting to the desired peer or before the
705 * transport was ready to receive. Signal error and free
709 control_transmit_timeout (void *cls,
710 const struct GNUNET_SCHEDULER_TaskContext *tc)
712 struct ControlMessage *th = cls;
714 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
715 if (NULL != th->notify)
716 th->notify (th->notify_cls, 0, NULL);
717 GNUNET_CONTAINER_DLL_remove (th->h->control_head,
725 * Queue control request for transmission to the transport
728 * @param h handle to the transport service
729 * @param size number of bytes to be transmitted
730 * @param at_head request must be added to the head of the queue
731 * (otherwise request will be appended)
732 * @param timeout how long this transmission can wait (at most)
733 * @param notify function to call to get the content
734 * @param notify_cls closure for notify
737 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h,
740 struct GNUNET_TIME_Relative timeout,
741 GNUNET_CONNECTION_TransmitReadyNotify notify,
744 struct ControlMessage *th;
747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
748 "Control transmit of %u bytes within %llums requested\n",
749 size, (unsigned long long) timeout.value);
751 th = GNUNET_malloc (sizeof (struct ControlMessage));
754 th->notify_cls = notify_cls;
755 th->notify_size = size;
756 th->notify_delay_task
757 = GNUNET_SCHEDULER_add_delayed (h->sched,
758 timeout, &control_transmit_timeout, th);
760 GNUNET_CONTAINER_DLL_insert (h->control_head,
764 GNUNET_CONTAINER_DLL_insert_after (h->control_head,
768 schedule_transmission (h);
772 struct SetQuotaContext
774 struct GNUNET_TRANSPORT_Handle *handle;
776 struct GNUNET_PeerIdentity target;
778 GNUNET_SCHEDULER_Task cont;
782 struct GNUNET_TIME_Absolute timeout;
784 struct GNUNET_BANDWIDTH_Value32NBO quota_in;
789 * Send SET_QUOTA message to the service.
791 * @param cls the 'struct SetQuotaContext'
792 * @param size number of bytes available in buf
793 * @param buf where to copy the message
794 * @return number of bytes copied to buf
797 send_set_quota (void *cls, size_t size, void *buf)
799 struct SetQuotaContext *sqc = cls;
800 struct QuotaSetMessage *msg;
804 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
807 GNUNET_SCHEDULER_REASON_TIMEOUT);
812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
813 "Transmitting `%s' request with respect to `%4s'.\n",
815 GNUNET_i2s (&sqc->target));
817 GNUNET_assert (size >= sizeof (struct QuotaSetMessage));
819 msg->header.size = htons (sizeof (struct QuotaSetMessage));
820 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
821 msg->quota = sqc->quota_in;
822 memcpy (&msg->peer, &sqc->target, sizeof (struct GNUNET_PeerIdentity));
823 if (sqc->cont != NULL)
824 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
827 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
829 return sizeof (struct QuotaSetMessage);
834 * Set the share of incoming bandwidth for the given
835 * peer to the specified amount.
837 * @param handle connection to transport service
838 * @param target who's bandwidth quota is being changed
839 * @param quota_in incoming bandwidth quota in bytes per ms
840 * @param quota_out outgoing bandwidth quota in bytes per ms
841 * @param timeout how long to wait until signaling failure if
842 * we can not communicate the quota change
843 * @param cont continuation to call when done, will be called
844 * either with reason "TIMEOUT" or with reason "PREREQ_DONE"
845 * @param cont_cls closure for continuation
848 GNUNET_TRANSPORT_set_quota (struct GNUNET_TRANSPORT_Handle *handle,
849 const struct GNUNET_PeerIdentity *target,
850 struct GNUNET_BANDWIDTH_Value32NBO quota_in,
851 struct GNUNET_BANDWIDTH_Value32NBO quota_out,
852 struct GNUNET_TIME_Relative timeout,
853 GNUNET_SCHEDULER_Task cont, void *cont_cls)
855 struct NeighbourList *n;
856 struct SetQuotaContext *sqc;
858 n = neighbour_find (handle, target);
862 if (ntohl (quota_out.value__) != n->out_tracker.available_bytes_per_s__)
863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
864 "Quota changed from %u to %u for peer `%s'\n",
865 (unsigned int) n->out_tracker.available_bytes_per_s__,
866 (unsigned int) ntohl (quota_out.value__),
867 GNUNET_i2s (target));
869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
870 "Quota remains at %u for peer `%s'\n",
871 (unsigned int) n->out_tracker.available_bytes_per_s__,
872 GNUNET_i2s (target));
874 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker,
880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
881 "Quota changed to %u for peer `%s', but I have no such neighbour!\n",
882 (unsigned int) ntohl (quota_out.value__),
883 GNUNET_i2s (target));
886 sqc = GNUNET_malloc (sizeof (struct SetQuotaContext));
887 sqc->handle = handle;
888 sqc->target = *target;
890 sqc->cont_cls = cont_cls;
891 sqc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
892 sqc->quota_in = quota_in;
893 schedule_control_transmit (handle,
894 sizeof (struct QuotaSetMessage),
895 GNUNET_NO, timeout, &send_set_quota, sqc);
900 * Obtain the HELLO message for this peer.
902 * @param handle connection to transport service
903 * @param rec function to call with the HELLO, sender will be our peer
904 * identity; message and sender will be NULL on timeout
905 * (handshake with transport service pending/failed).
906 * cost estimate will be 0.
907 * @param rec_cls closure for rec
910 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
911 GNUNET_TRANSPORT_HelloUpdateCallback rec,
914 struct HelloWaitList *hwl;
916 hwl = GNUNET_malloc (sizeof (struct HelloWaitList));
917 hwl->next = handle->hwl_head;
918 handle->hwl_head = hwl;
919 hwl->handle = handle;
921 hwl->rec_cls = rec_cls;
922 if (handle->my_hello == NULL)
924 rec (rec_cls, (const struct GNUNET_MessageHeader *) handle->my_hello);
930 * Stop receiving updates about changes to our HELLO message.
932 * @param handle connection to transport service
933 * @param rec function previously registered to be called with the HELLOs
934 * @param rec_cls closure for rec
937 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_Handle *handle,
938 GNUNET_TRANSPORT_HelloUpdateCallback rec,
941 struct HelloWaitList *pos;
942 struct HelloWaitList *prev;
945 pos = handle->hwl_head;
948 if ( (pos->rec == rec) &&
949 (pos->rec_cls == rec_cls) )
954 GNUNET_break (pos != NULL);
958 handle->hwl_head = pos->next;
960 prev->next = pos->next;
966 * Send HELLO message to the service.
968 * @param cls the HELLO message to send
969 * @param size number of bytes available in buf
970 * @param buf where to copy the message
971 * @return number of bytes copied to buf
974 send_hello (void *cls, size_t size, void *buf)
976 struct GNUNET_MessageHeader *hello = cls;
981 #if DEBUG_TRANSPORT_TIMEOUT
982 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
983 "Timeout while trying to transmit `%s' request.\n",
990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
991 "Transmitting `%s' request.\n", "HELLO");
993 msize = ntohs (hello->size);
994 GNUNET_assert (size >= msize);
995 memcpy (buf, hello, msize);
1002 * Offer the transport service the HELLO of another peer. Note that
1003 * the transport service may just ignore this message if the HELLO is
1004 * malformed or useless due to our local configuration.
1006 * @param handle connection to transport service
1007 * @param hello the hello message
1010 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
1011 const struct GNUNET_MessageHeader *hello)
1013 struct GNUNET_MessageHeader *hc;
1015 struct GNUNET_PeerIdentity peer;
1017 GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
1018 size = ntohs (hello->size);
1019 GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
1020 if (GNUNET_OK != GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message*) hello,
1027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1028 "Offering `%s' message of `%4s' to transport for validation.\n",
1030 GNUNET_i2s (&peer));
1032 hc = GNUNET_malloc (size);
1033 memcpy (hc, hello, size);
1034 schedule_control_transmit (handle,
1036 GNUNET_NO, OFFER_HELLO_TIMEOUT, &send_hello, hc);
1041 * Transmit START message to service.
1044 * @param size number of bytes available in buf
1045 * @param buf where to copy the message
1046 * @return number of bytes copied to buf
1049 send_start (void *cls, size_t size, void *buf)
1051 struct GNUNET_TRANSPORT_Handle *h = cls;
1052 struct StartMessage s;
1056 /* Can only be shutdown, just give up */
1058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1059 "Shutdown while trying to transmit `%s' request.\n",
1065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1066 "Transmitting `%s' request.\n", "START");
1068 GNUNET_assert (size >= sizeof (struct StartMessage));
1069 s.header.size = htons (sizeof (struct StartMessage));
1070 s.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
1071 s.do_check = htonl (h->check_self);
1073 memcpy (buf, &s, sizeof (struct StartMessage));
1074 return sizeof (struct StartMessage);
1081 * @param n the entry to free
1084 neighbour_free (struct NeighbourList *n)
1086 struct GNUNET_TRANSPORT_Handle *h;
1087 struct NeighbourList *prev;
1088 struct NeighbourList *pos;
1092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1093 "Removing neighbour `%s' from list of connected peers.\n",
1094 GNUNET_i2s (&n->id));
1096 GNUNET_break (n->is_connected == GNUNET_NO);
1097 GNUNET_break (n->transmit_stage == TS_NEW);
1100 pos = h->neighbours;
1107 h->neighbours = n->next;
1109 prev->next = n->next;
1115 * Mark neighbour as disconnected.
1117 * @param n the entry to mark as disconnected
1120 neighbour_disconnect (struct NeighbourList *n)
1122 struct GNUNET_TRANSPORT_Handle *h = n->h;
1124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1125 "Removing neighbour `%s' from list of connected peers.\n",
1126 GNUNET_i2s (&n->id));
1128 GNUNET_break (n->is_connected == GNUNET_YES);
1129 n->is_connected = GNUNET_NO;
1130 if (h->nc_cb != NULL)
1131 h->nd_cb (h->cls, &n->id);
1132 if (n->transmit_stage == TS_NEW)
1138 * Function we use for handling incoming messages.
1140 * @param cls closure (struct GNUNET_TRANSPORT_Handle *)
1141 * @param msg message received, NULL on timeout or fatal error
1143 static void demultiplexer (void *cls,
1144 const struct GNUNET_MessageHeader *msg);
1148 * Try again to connect to transport service.
1150 * @param cls the handle to the transport service
1151 * @param tc scheduler context
1154 reconnect (void *cls,
1155 const struct GNUNET_SCHEDULER_TaskContext *tc)
1157 struct GNUNET_TRANSPORT_Handle *h = cls;
1158 struct ControlMessage *pos;
1159 struct NeighbourList *n;
1160 struct NeighbourList *next;
1162 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1163 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1165 /* shutdown, just give up */
1168 /* Forget about all neighbours that we used to be connected to */
1172 #if DEBUG_TRANSPORT_DISCONNECT
1173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1174 "Disconnecting due to reconnect being called\n");
1177 if (n->is_connected)
1178 neighbour_disconnect (n);
1182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1183 "Connecting to transport service.\n");
1185 GNUNET_assert (h->client == NULL);
1186 h->client = GNUNET_CLIENT_connect (h->sched, "transport", h->cfg);
1187 GNUNET_assert (h->client != NULL);
1188 /* make sure we don't send "START" twice, remove existing entry from
1189 queue (if present) */
1190 pos = h->control_head;
1193 if (pos->notify == &send_start)
1195 GNUNET_CONTAINER_DLL_remove (h->control_head,
1198 if (GNUNET_SCHEDULER_NO_TASK != pos->notify_delay_task)
1200 GNUNET_SCHEDULER_cancel (h->sched, pos->notify_delay_task);
1201 pos->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1208 schedule_control_transmit (h,
1209 sizeof (struct StartMessage),
1211 GNUNET_TIME_UNIT_FOREVER_REL, &send_start, h);
1212 GNUNET_CLIENT_receive (h->client,
1213 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1218 * Function that will schedule the job that will try
1219 * to connect us again to the client.
1221 * @param h transport service to reconnect
1224 schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
1227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1228 "Scheduling task to reconnect to transport service in %llu ms.\n",
1229 h->reconnect_delay.value);
1231 GNUNET_assert (h->client == NULL);
1232 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
1234 = GNUNET_SCHEDULER_add_delayed (h->sched,
1235 h->reconnect_delay, &reconnect, h);
1236 if (h->reconnect_delay.value == 0)
1238 h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1242 h->reconnect_delay = GNUNET_TIME_relative_multiply (h->reconnect_delay, 2);
1243 h->reconnect_delay = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
1244 h->reconnect_delay);
1250 * Add neighbour to our list
1252 * @return NULL if this API is currently disconnecting from the service
1254 static struct NeighbourList *
1255 neighbour_add (struct GNUNET_TRANSPORT_Handle *h,
1256 const struct GNUNET_PeerIdentity *pid)
1258 struct NeighbourList *n;
1260 if (GNUNET_YES == h->in_disconnect)
1262 /* check for duplicates */
1263 if (NULL != (n = neighbour_find (h, pid)))
1269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1270 "Creating entry for neighbour `%4s'.\n",
1273 n = GNUNET_malloc (sizeof (struct NeighbourList));
1275 GNUNET_BANDWIDTH_tracker_init (&n->out_tracker,
1276 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1277 MAX_BANDWIDTH_CARRY_S);
1278 n->next = h->neighbours;
1286 * Connect to the transport service. Note that the connection may
1287 * complete (or fail) asynchronously.
1289 * @param sched scheduler to use
1290 * @param cfg configuration to use
1291 * @param self our own identity (API should check that it matches
1292 * the identity found by transport), or NULL (no check)
1293 * @param cls closure for the callbacks
1294 * @param rec receive function to call
1295 * @param nc function to call on connect events
1296 * @param nd function to call on disconnect events
1298 struct GNUNET_TRANSPORT_Handle *
1299 GNUNET_TRANSPORT_connect (struct GNUNET_SCHEDULER_Handle *sched,
1300 const struct GNUNET_CONFIGURATION_Handle *cfg,
1301 const struct GNUNET_PeerIdentity *self,
1303 GNUNET_TRANSPORT_ReceiveCallback rec,
1304 GNUNET_TRANSPORT_NotifyConnect nc,
1305 GNUNET_TRANSPORT_NotifyDisconnect nd)
1307 struct GNUNET_TRANSPORT_Handle *ret;
1309 ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Handle));
1313 ret->check_self = GNUNET_YES;
1321 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1322 schedule_reconnect (ret);
1328 * Disconnect from the transport service.
1331 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1333 struct GNUNET_TRANSPORT_TransmitHandle *th;
1334 struct NeighbourList *n;
1335 struct HelloWaitList *hwl;
1336 struct GNUNET_CLIENT_Connection *client;
1337 struct ControlMessage *cm;
1340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transport disconnect called!\n");
1342 handle->in_disconnect = GNUNET_YES;
1343 while (NULL != (n = handle->neighbours))
1345 handle->neighbours = n->next;
1346 switch (n->transmit_stage)
1349 case TS_TRANSMITTED:
1353 case TS_TRANSMITTED_QUEUED:
1354 th = &n->transmit_handle;
1355 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1357 GNUNET_SCHEDULER_cancel (handle->sched,
1358 th->notify_delay_task);
1359 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1361 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
1368 while (NULL != (hwl = handle->hwl_head))
1370 handle->hwl_head = hwl->next;
1371 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1373 ("Disconnect while notification for `%s' still registered.\n"),
1375 if (hwl->rec != NULL)
1376 hwl->rec (hwl->rec_cls, NULL);
1380 /* Check for still scheduled control messages, cancel delay tasks if so */
1381 /* Added because somehow a notify_delay_task is remaining scheduled and is ever so annoying */
1382 while ( (NULL != (cm = handle->control_head)))
1385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1386 "Disconnect before control message sent!\n");
1388 if (cm->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1390 GNUNET_SCHEDULER_cancel (handle->sched, cm->notify_delay_task);
1391 cm->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1393 GNUNET_CONTAINER_DLL_remove (handle->control_head,
1394 handle->control_tail,
1400 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1402 GNUNET_SCHEDULER_cancel (handle->sched, handle->reconnect_task);
1403 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1405 if (handle->quota_task != GNUNET_SCHEDULER_NO_TASK)
1407 GNUNET_SCHEDULER_cancel (handle->sched, handle->quota_task);
1408 handle->quota_task = GNUNET_SCHEDULER_NO_TASK;
1410 GNUNET_free_non_null (handle->my_hello);
1411 handle->my_hello = NULL;
1413 if (NULL != handle->network_handle)
1415 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->network_handle);
1416 handle->network_handle = NULL;
1418 if (NULL != (client = handle->client))
1421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1422 "Disconnecting from transport service for good.\n");
1424 handle->client = NULL;
1425 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
1427 GNUNET_free (handle);
1432 * Function we use for handling incoming messages.
1434 * @param cls closure (struct GNUNET_TRANSPORT_Handle *)
1435 * @param msg message received, NULL on timeout or fatal error
1438 demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
1440 struct GNUNET_TRANSPORT_Handle *h = cls;
1441 const struct DisconnectInfoMessage *dim;
1442 const struct ConnectInfoMessage *cim;
1443 const struct InboundMessage *im;
1444 const struct GNUNET_MessageHeader *imm;
1445 const struct SendOkMessage *okm;
1446 struct HelloWaitList *hwl;
1447 struct HelloWaitList *next_hwl;
1448 struct NeighbourList *n;
1449 struct GNUNET_PeerIdentity me;
1452 if (h->client == NULL)
1454 /* shutdown initiated from 'GNUNET_TRANSPORT_disconnect',
1455 finish clean up work! */
1462 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1463 "Error receiving from transport service, disconnecting temporarily.\n");
1465 if (h->network_handle != NULL)
1467 GNUNET_CLIENT_notify_transmit_ready_cancel (h->network_handle);
1468 h->network_handle = NULL;
1470 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
1472 schedule_reconnect (h);
1475 GNUNET_CLIENT_receive (h->client,
1476 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1477 size = ntohs (msg->size);
1478 switch (ntohs (msg->type))
1480 case GNUNET_MESSAGE_TYPE_HELLO:
1482 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg,
1489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1490 "Receiving (my own) `%s' message, I am `%4s'.\n",
1491 "HELLO", GNUNET_i2s (&me));
1493 GNUNET_free_non_null (h->my_hello);
1495 if (size < sizeof (struct GNUNET_MessageHeader))
1500 h->my_hello = GNUNET_malloc (size);
1501 memcpy (h->my_hello, msg, size);
1505 next_hwl = hwl->next;
1506 hwl->rec (hwl->rec_cls,
1507 (const struct GNUNET_MessageHeader *) h->my_hello);
1511 case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
1512 if (size != sizeof (struct ConnectInfoMessage))
1517 cim = (const struct ConnectInfoMessage *) msg;
1519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1520 "Receiving `%s' message for `%4s'.\n",
1521 "CONNECT", GNUNET_i2s (&cim->id));
1523 n = neighbour_find (h, &cim->id);
1525 n = neighbour_add (h,
1529 GNUNET_break (n->is_connected == GNUNET_NO);
1530 n->is_connected = GNUNET_YES;
1531 if (h->nc_cb != NULL)
1532 h->nc_cb (h->cls, &n->id,
1533 GNUNET_TIME_relative_ntoh (cim->latency),
1534 ntohl (cim->distance));
1536 case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
1537 if (size != sizeof (struct DisconnectInfoMessage))
1542 dim = (const struct DisconnectInfoMessage *) msg;
1543 #if DEBUG_TRANSPORT_DISCONNECT
1544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1545 "Receiving `%s' message for `%4s'.\n",
1547 GNUNET_i2s (&dim->peer));
1549 n = neighbour_find (h, &dim->peer);
1550 GNUNET_break (n != NULL);
1552 neighbour_disconnect (n);
1554 case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
1555 if (size != sizeof (struct SendOkMessage))
1560 okm = (const struct SendOkMessage *) msg;
1562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1563 "Receiving `%s' message, transmission %s.\n", "SEND_OK",
1564 ntohl (okm->success) == GNUNET_OK ? "succeeded" : "failed");
1566 n = neighbour_find (h, &okm->peer);
1567 GNUNET_assert (n != NULL);
1568 switch (n->transmit_stage)
1576 case TS_TRANSMITTED:
1577 n->transmit_stage = TS_NEW;
1579 case TS_TRANSMITTED_QUEUED:
1580 n->transmit_stage = TS_QUEUED;
1581 schedule_transmission (h);
1587 case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
1589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1590 "Receiving `%s' message.\n", "RECV");
1593 sizeof (struct InboundMessage) +
1594 sizeof (struct GNUNET_MessageHeader))
1599 im = (const struct InboundMessage *) msg;
1600 imm = (const struct GNUNET_MessageHeader *) &im[1];
1601 if (ntohs (imm->size) + sizeof (struct InboundMessage) != size)
1607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1608 "Received message of type %u from `%4s'.\n",
1609 ntohs (imm->type), GNUNET_i2s (&im->peer));
1611 n = neighbour_find (h, &im->peer);
1617 if (n->is_connected != GNUNET_YES)
1623 h->rec (h->cls, &im->peer, imm,
1624 GNUNET_TIME_relative_ntoh (im->latency), ntohl(im->distance));
1627 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1629 ("Received unexpected message of type %u in %s:%u\n"),
1630 ntohs (msg->type), __FILE__, __LINE__);
1638 * Called when our transmit request timed out before any transport
1639 * reported success connecting to the desired peer or before the
1640 * transport was ready to receive. Signal error and free
1643 * @param cls the 'struct GNUNET_TRANSPORT_TransmitHandle*' that is timing out
1644 * @param tc scheduler context
1647 peer_transmit_timeout (void *cls,
1648 const struct GNUNET_SCHEDULER_TaskContext *tc)
1650 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
1651 struct NeighbourList *n;
1652 GNUNET_CONNECTION_TransmitReadyNotify notify;
1655 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1659 "Triggering timeout for request to transmit to `%4s' (%d)\n",
1660 GNUNET_i2s (&n->id),
1663 notify = th->notify;
1664 notify_cls = th->notify_cls;
1665 switch (n->transmit_stage)
1671 n->transmit_stage = TS_NEW;
1672 if (n->is_connected == GNUNET_NO)
1675 case TS_TRANSMITTED:
1678 case TS_TRANSMITTED_QUEUED:
1679 n->transmit_stage = TS_TRANSMITTED;
1685 notify (notify_cls, 0, NULL);
1690 * Check if we could queue a message of the given size for
1691 * transmission. The transport service will take both its
1692 * internal buffers and bandwidth limits imposed by the
1693 * other peer into consideration when answering this query.
1695 * @param handle connection to transport service
1696 * @param target who should receive the message
1697 * @param size how big is the message we want to transmit?
1698 * @param priority how important is the message?
1699 * @param timeout after how long should we give up (and call
1700 * notify with buf NULL and size 0)?
1701 * @param notify function to call when we are ready to
1702 * send such a message
1703 * @param notify_cls closure for notify
1704 * @return NULL if someone else is already waiting to be notified
1705 * non-NULL if the notify callback was queued (can be used to cancel
1706 * using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1708 struct GNUNET_TRANSPORT_TransmitHandle *
1709 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle
1711 const struct GNUNET_PeerIdentity
1712 *target, size_t size,
1713 unsigned int priority,
1714 struct GNUNET_TIME_Relative timeout,
1715 GNUNET_CONNECTION_TransmitReadyNotify
1716 notify, void *notify_cls)
1718 struct GNUNET_TRANSPORT_TransmitHandle *th;
1719 struct NeighbourList *n;
1721 if (size + sizeof (struct OutboundMessage) >=
1722 GNUNET_SERVER_MAX_MESSAGE_SIZE)
1725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1726 "Message size is %d, max allowed is %d.\n",
1727 size + sizeof (struct OutboundMessage), GNUNET_SERVER_MAX_MESSAGE_SIZE - 1);
1733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1734 "Asking transport service for transmission of %u bytes to peer `%4s' within %llu ms.\n",
1735 size, GNUNET_i2s (target),
1736 (unsigned long long) timeout.value);
1738 n = neighbour_find (handle, target);
1740 n = neighbour_add (handle, target);
1743 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1744 "Could not create neighbour entry for peer `%s'\n",
1745 GNUNET_i2s (target));
1748 switch (n->transmit_stage)
1751 n->transmit_stage = TS_QUEUED;
1756 case TS_TRANSMITTED:
1757 n->transmit_stage = TS_TRANSMITTED_QUEUED;
1759 case TS_TRANSMITTED_QUEUED:
1766 th = &n->transmit_handle;
1768 th->notify = notify;
1769 th->notify_cls = notify_cls;
1770 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1771 th->notify_size = size + sizeof (struct OutboundMessage);
1772 th->priority = priority;
1773 th->notify_delay_task
1774 = GNUNET_SCHEDULER_add_delayed (handle->sched, timeout,
1775 &peer_transmit_timeout, th);
1776 schedule_transmission (handle);
1782 * Cancel the specified transmission-ready notification.
1785 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
1786 GNUNET_TRANSPORT_TransmitHandle
1789 struct NeighbourList *n;
1793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1794 "Transmission request of %u bytes to `%4s' was cancelled.\n",
1795 th->notify_size - sizeof (struct OutboundMessage),
1796 GNUNET_i2s (&n->id));
1798 switch (n->transmit_stage)
1804 n->transmit_stage = TS_NEW;
1805 if (n->is_connected == GNUNET_NO)
1808 case TS_TRANSMITTED:
1811 case TS_TRANSMITTED_QUEUED:
1812 n->transmit_stage = TS_TRANSMITTED;
1820 /* end of transport_api.c */