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 if (n->is_connected != GNUNET_YES)
462 th = &n->transmit_handle;
463 GNUNET_break (n == th->neighbour);
464 /* check outgoing quota */
465 duration = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
466 th->notify_size - sizeof (struct OutboundMessage));
467 struct GNUNET_TIME_Absolute duration_abs = GNUNET_TIME_relative_to_absolute (duration);
468 if (th->timeout.abs_value < duration_abs.abs_value)
470 /* signal timeout! */
472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
473 "Would need %llu ms before bandwidth is available for delivery to `%4s', that is too long. Signaling timeout.\n",
475 GNUNET_i2s (&n->id));
477 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
479 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
480 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
482 n->transmit_stage = TS_NEW;
483 if (NULL != th->notify)
484 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
487 if (duration.rel_value > 0)
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
491 "Need more bandwidth (%u b/s allowed, %u b needed), delaying delivery to `%4s' by %llu ms\n",
492 (unsigned int) n->out_tracker.available_bytes_per_s__,
493 (unsigned int) th->notify_size - sizeof (struct OutboundMessage),
497 retry_time = GNUNET_TIME_relative_min (retry_time,
502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
503 "Have %u bytes of bandwidth available for transmission to `%4s' right now\n",
504 th->notify_size - sizeof (struct OutboundMessage),
505 GNUNET_i2s (&n->id));
508 if ( (ret == NULL) ||
509 (ret->priority < th->priority) )
513 h->quota_task = GNUNET_SCHEDULER_add_delayed (h->sched,
515 "a_transmit_ready,
522 * Transmit message(s) to service.
524 * @param cls handle to transport
525 * @param size number of bytes available in buf
526 * @param buf where to copy the message
527 * @return number of bytes copied to buf
530 transport_notify_ready (void *cls, size_t size, void *buf)
532 struct GNUNET_TRANSPORT_Handle *h = cls;
533 struct ControlMessage *cm;
534 struct GNUNET_TRANSPORT_TransmitHandle *th;
535 struct NeighbourList *n;
536 struct OutboundMessage obm;
542 h->network_handle = NULL;
545 schedule_transmission (h);
549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
550 "Ready to transmit %u bytes to transport service\n", size);
554 while ( (NULL != (cm = h->control_head)) &&
555 (cm->notify_size <= size) )
557 if (cm->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
559 GNUNET_SCHEDULER_cancel (h->sched, cm->notify_delay_task);
560 cm->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
562 GNUNET_CONTAINER_DLL_remove (h->control_head,
565 nret = cm->notify (cm->notify_cls, size, &cbuf[ret]);
567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
568 "Added %u bytes of control message at %u\n",
576 while ( (NULL != (th = schedule_peer_transmission (h))) &&
577 (th->notify_size <= size) )
579 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
581 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
582 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
585 switch (n->transmit_stage)
591 n->transmit_stage = TS_TRANSMITTED;
596 case TS_TRANSMITTED_QUEUED:
602 GNUNET_assert (size >= sizeof (struct OutboundMessage));
603 mret = th->notify (th->notify_cls,
604 size - sizeof (struct OutboundMessage),
605 &cbuf[ret + sizeof (struct OutboundMessage)]);
606 GNUNET_assert (mret <= size - sizeof (struct OutboundMessage));
608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
609 "Message of %u bytes with timeout %llums constructed for `%4s'\n",
611 (unsigned long long) GNUNET_TIME_absolute_get_remaining (th->timeout).abs_value,
612 GNUNET_i2s (&n->id));
616 GNUNET_assert (mret + sizeof (struct OutboundMessage) < GNUNET_SERVER_MAX_MESSAGE_SIZE);
617 obm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
618 obm.header.size = htons (mret + sizeof (struct OutboundMessage));
619 obm.priority = htonl (th->priority);
620 obm.timeout = GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining (th->timeout));
622 memcpy (&cbuf[ret], &obm, sizeof (struct OutboundMessage));
623 ret += (mret + sizeof (struct OutboundMessage));
624 size -= (mret + sizeof (struct OutboundMessage));
625 GNUNET_BANDWIDTH_tracker_consume (&n->out_tracker, mret);
629 switch (n->transmit_stage)
638 n->transmit_stage = TS_NEW;
640 case TS_TRANSMITTED_QUEUED:
641 n->transmit_stage = TS_QUEUED;
648 schedule_transmission (h);
650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
651 "Transmitting %u bytes to transport service\n", ret);
658 * Schedule the task to send one message, either from the control
659 * list or the peer message queues to the service.
662 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
665 struct GNUNET_TIME_Relative timeout;
666 struct GNUNET_TRANSPORT_TransmitHandle *th;
668 if (NULL != h->network_handle)
670 if (h->client == NULL)
672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
673 _("Could not yet schedule transmission: we are not yet connected to the transport service!\n"));
674 return; /* not yet connected */
676 if (NULL != h->control_head)
678 size = h->control_head->notify_size;
679 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
683 th = schedule_peer_transmission (h);
686 /* no transmission ready right now */
688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
689 "Could not yet schedule transmission: none ready\n");
693 size = th->notify_size;
694 timeout = GNUNET_TIME_absolute_get_remaining (th->timeout);
697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
698 "Calling notify_transmit_ready\n");
701 GNUNET_CLIENT_notify_transmit_ready (h->client,
705 &transport_notify_ready,
707 GNUNET_assert (NULL != h->network_handle);
712 * Called when our transmit request timed out before any transport
713 * reported success connecting to the desired peer or before the
714 * transport was ready to receive. Signal error and free
718 control_transmit_timeout (void *cls,
719 const struct GNUNET_SCHEDULER_TaskContext *tc)
721 struct ControlMessage *th = cls;
723 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
724 if (NULL != th->notify)
725 th->notify (th->notify_cls, 0, NULL);
726 GNUNET_CONTAINER_DLL_remove (th->h->control_head,
734 * Queue control request for transmission to the transport
737 * @param h handle to the transport service
738 * @param size number of bytes to be transmitted
739 * @param at_head request must be added to the head of the queue
740 * (otherwise request will be appended)
741 * @param timeout how long this transmission can wait (at most)
742 * @param notify function to call to get the content
743 * @param notify_cls closure for notify
746 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h,
749 struct GNUNET_TIME_Relative timeout,
750 GNUNET_CONNECTION_TransmitReadyNotify notify,
753 struct ControlMessage *th;
756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
757 "Control transmit of %u bytes within %llums requested\n",
758 size, (unsigned long long) timeout.abs_value);
760 th = GNUNET_malloc (sizeof (struct ControlMessage));
763 th->notify_cls = notify_cls;
764 th->notify_size = size;
765 th->notify_delay_task
766 = GNUNET_SCHEDULER_add_delayed (h->sched,
767 timeout, &control_transmit_timeout, th);
769 GNUNET_CONTAINER_DLL_insert (h->control_head,
773 GNUNET_CONTAINER_DLL_insert_after (h->control_head,
777 schedule_transmission (h);
781 struct SetQuotaContext
783 struct GNUNET_TRANSPORT_Handle *handle;
785 struct GNUNET_PeerIdentity target;
787 GNUNET_SCHEDULER_Task cont;
791 struct GNUNET_TIME_Absolute timeout;
793 struct GNUNET_BANDWIDTH_Value32NBO quota_in;
798 * Send SET_QUOTA message to the service.
800 * @param cls the 'struct SetQuotaContext'
801 * @param size number of bytes available in buf
802 * @param buf where to copy the message
803 * @return number of bytes copied to buf
806 send_set_quota (void *cls, size_t size, void *buf)
808 struct SetQuotaContext *sqc = cls;
809 struct QuotaSetMessage *msg;
813 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
816 GNUNET_SCHEDULER_REASON_TIMEOUT);
821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
822 "Transmitting `%s' request with respect to `%4s'.\n",
824 GNUNET_i2s (&sqc->target));
826 GNUNET_assert (size >= sizeof (struct QuotaSetMessage));
828 msg->header.size = htons (sizeof (struct QuotaSetMessage));
829 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
830 msg->quota = sqc->quota_in;
831 memcpy (&msg->peer, &sqc->target, sizeof (struct GNUNET_PeerIdentity));
832 if (sqc->cont != NULL)
833 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
836 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
838 return sizeof (struct QuotaSetMessage);
843 * Set the share of incoming bandwidth for the given
844 * peer to the specified amount.
846 * @param handle connection to transport service
847 * @param target who's bandwidth quota is being changed
848 * @param quota_in incoming bandwidth quota in bytes per ms
849 * @param quota_out outgoing bandwidth quota in bytes per ms
850 * @param timeout how long to wait until signaling failure if
851 * we can not communicate the quota change
852 * @param cont continuation to call when done, will be called
853 * either with reason "TIMEOUT" or with reason "PREREQ_DONE"
854 * @param cont_cls closure for continuation
857 GNUNET_TRANSPORT_set_quota (struct GNUNET_TRANSPORT_Handle *handle,
858 const struct GNUNET_PeerIdentity *target,
859 struct GNUNET_BANDWIDTH_Value32NBO quota_in,
860 struct GNUNET_BANDWIDTH_Value32NBO quota_out,
861 struct GNUNET_TIME_Relative timeout,
862 GNUNET_SCHEDULER_Task cont, void *cont_cls)
864 struct NeighbourList *n;
865 struct SetQuotaContext *sqc;
867 n = neighbour_find (handle, target);
871 if (ntohl (quota_out.rel_value__) != n->out_tracker.available_bytes_per_s__)
872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
873 "Quota changed from %u to %u for peer `%s'\n",
874 (unsigned int) n->out_tracker.available_bytes_per_s__,
875 (unsigned int) ntohl (quota_out.rel_value__),
876 GNUNET_i2s (target));
878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
879 "Quota remains at %u for peer `%s'\n",
880 (unsigned int) n->out_tracker.available_bytes_per_s__,
881 GNUNET_i2s (target));
883 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker,
889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
890 "Quota changed to %u for peer `%s', but I have no such neighbour!\n",
891 (unsigned int) ntohl (quota_out.rel_value__),
892 GNUNET_i2s (target));
895 sqc = GNUNET_malloc (sizeof (struct SetQuotaContext));
896 sqc->handle = handle;
897 sqc->target = *target;
899 sqc->cont_cls = cont_cls;
900 sqc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
901 sqc->quota_in = quota_in;
902 schedule_control_transmit (handle,
903 sizeof (struct QuotaSetMessage),
904 GNUNET_NO, timeout, &send_set_quota, sqc);
909 * Obtain the HELLO message for this peer.
911 * @param handle connection to transport service
912 * @param rec function to call with the HELLO, sender will be our peer
913 * identity; message and sender will be NULL on timeout
914 * (handshake with transport service pending/failed).
915 * cost estimate will be 0.
916 * @param rec_cls closure for rec
919 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
920 GNUNET_TRANSPORT_HelloUpdateCallback rec,
923 struct HelloWaitList *hwl;
925 hwl = GNUNET_malloc (sizeof (struct HelloWaitList));
926 hwl->next = handle->hwl_head;
927 handle->hwl_head = hwl;
928 hwl->handle = handle;
930 hwl->rec_cls = rec_cls;
931 if (handle->my_hello == NULL)
933 rec (rec_cls, (const struct GNUNET_MessageHeader *) handle->my_hello);
939 * Stop receiving updates about changes to our HELLO message.
941 * @param handle connection to transport service
942 * @param rec function previously registered to be called with the HELLOs
943 * @param rec_cls closure for rec
946 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_Handle *handle,
947 GNUNET_TRANSPORT_HelloUpdateCallback rec,
950 struct HelloWaitList *pos;
951 struct HelloWaitList *prev;
954 pos = handle->hwl_head;
957 if ( (pos->rec == rec) &&
958 (pos->rec_cls == rec_cls) )
963 GNUNET_break (pos != NULL);
967 handle->hwl_head = pos->next;
969 prev->next = pos->next;
975 * Send HELLO message to the service.
977 * @param cls the HELLO message to send
978 * @param size number of bytes available in buf
979 * @param buf where to copy the message
980 * @return number of bytes copied to buf
983 send_hello (void *cls, size_t size, void *buf)
985 struct GNUNET_MessageHeader *hello = cls;
990 #if DEBUG_TRANSPORT_TIMEOUT
991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
992 "Timeout while trying to transmit `%s' request.\n",
999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1000 "Transmitting `%s' request.\n", "HELLO");
1002 msize = ntohs (hello->size);
1003 GNUNET_assert (size >= msize);
1004 memcpy (buf, hello, msize);
1005 GNUNET_free (hello);
1011 * Offer the transport service the HELLO of another peer. Note that
1012 * the transport service may just ignore this message if the HELLO is
1013 * malformed or useless due to our local configuration.
1015 * @param handle connection to transport service
1016 * @param hello the hello message
1019 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
1020 const struct GNUNET_MessageHeader *hello)
1022 struct GNUNET_MessageHeader *hc;
1024 struct GNUNET_PeerIdentity peer;
1026 GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
1027 size = ntohs (hello->size);
1028 GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
1029 if (GNUNET_OK != GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message*) hello,
1036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1037 "Offering `%s' message of `%4s' to transport for validation.\n",
1039 GNUNET_i2s (&peer));
1041 hc = GNUNET_malloc (size);
1042 memcpy (hc, hello, size);
1043 schedule_control_transmit (handle,
1045 GNUNET_NO, OFFER_HELLO_TIMEOUT, &send_hello, hc);
1050 * Transmit START message to service.
1053 * @param size number of bytes available in buf
1054 * @param buf where to copy the message
1055 * @return number of bytes copied to buf
1058 send_start (void *cls, size_t size, void *buf)
1060 struct GNUNET_TRANSPORT_Handle *h = cls;
1061 struct StartMessage s;
1065 /* Can only be shutdown, just give up */
1067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1068 "Shutdown while trying to transmit `%s' request.\n",
1074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1075 "Transmitting `%s' request.\n", "START");
1077 GNUNET_assert (size >= sizeof (struct StartMessage));
1078 s.header.size = htons (sizeof (struct StartMessage));
1079 s.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
1080 s.do_check = htonl (h->check_self);
1082 memcpy (buf, &s, sizeof (struct StartMessage));
1083 return sizeof (struct StartMessage);
1090 * @param n the entry to free
1093 neighbour_free (struct NeighbourList *n)
1095 struct GNUNET_TRANSPORT_Handle *h;
1096 struct NeighbourList *prev;
1097 struct NeighbourList *pos;
1101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1102 "Removing neighbour `%s' from list of connected peers.\n",
1103 GNUNET_i2s (&n->id));
1105 GNUNET_break (n->is_connected == GNUNET_NO);
1106 GNUNET_break (n->transmit_stage == TS_NEW);
1109 pos = h->neighbours;
1116 h->neighbours = n->next;
1118 prev->next = n->next;
1124 * Mark neighbour as disconnected.
1126 * @param n the entry to mark as disconnected
1129 neighbour_disconnect (struct NeighbourList *n)
1131 struct GNUNET_TRANSPORT_Handle *h = n->h;
1133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1134 "Removing neighbour `%s' from list of connected peers.\n",
1135 GNUNET_i2s (&n->id));
1137 GNUNET_break (n->is_connected == GNUNET_YES);
1138 n->is_connected = GNUNET_NO;
1139 if (h->nd_cb != NULL)
1140 h->nd_cb (h->cls, &n->id);
1141 if (n->transmit_stage == TS_NEW)
1147 * Function we use for handling incoming messages.
1149 * @param cls closure (struct GNUNET_TRANSPORT_Handle *)
1150 * @param msg message received, NULL on timeout or fatal error
1152 static void demultiplexer (void *cls,
1153 const struct GNUNET_MessageHeader *msg);
1157 * Try again to connect to transport service.
1159 * @param cls the handle to the transport service
1160 * @param tc scheduler context
1163 reconnect (void *cls,
1164 const struct GNUNET_SCHEDULER_TaskContext *tc)
1166 struct GNUNET_TRANSPORT_Handle *h = cls;
1167 struct ControlMessage *pos;
1168 struct NeighbourList *n;
1169 struct NeighbourList *next;
1171 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1172 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1174 /* shutdown, just give up */
1177 /* Forget about all neighbours that we used to be connected to */
1181 #if DEBUG_TRANSPORT_DISCONNECT
1182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1183 "Disconnecting due to reconnect being called\n");
1186 if (n->is_connected)
1187 neighbour_disconnect (n);
1191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1192 "Connecting to transport service.\n");
1194 GNUNET_assert (h->client == NULL);
1195 h->client = GNUNET_CLIENT_connect (h->sched, "transport", h->cfg);
1196 GNUNET_assert (h->client != NULL);
1197 /* make sure we don't send "START" twice, remove existing entry from
1198 queue (if present) */
1199 pos = h->control_head;
1202 if (pos->notify == &send_start)
1204 GNUNET_CONTAINER_DLL_remove (h->control_head,
1207 if (GNUNET_SCHEDULER_NO_TASK != pos->notify_delay_task)
1209 GNUNET_SCHEDULER_cancel (h->sched, pos->notify_delay_task);
1210 pos->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1217 schedule_control_transmit (h,
1218 sizeof (struct StartMessage),
1220 GNUNET_TIME_UNIT_FOREVER_REL, &send_start, h);
1221 GNUNET_CLIENT_receive (h->client,
1222 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1227 * Function that will schedule the job that will try
1228 * to connect us again to the client.
1230 * @param h transport service to reconnect
1233 schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
1236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1237 "Scheduling task to reconnect to transport service in %llu ms.\n",
1238 h->reconnect_delay.abs_value);
1240 GNUNET_assert (h->client == NULL);
1241 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
1243 = GNUNET_SCHEDULER_add_delayed (h->sched,
1244 h->reconnect_delay, &reconnect, h);
1245 if (h->reconnect_delay.rel_value == 0)
1247 h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1251 h->reconnect_delay = GNUNET_TIME_relative_multiply (h->reconnect_delay, 2);
1252 h->reconnect_delay = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
1253 h->reconnect_delay);
1259 * Send request connect message to the service.
1261 * @param cls the TransportRequestConnectMessage
1262 * @param size number of bytes available in buf
1263 * @param buf where to copy the message
1264 * @return number of bytes copied to buf
1267 send_transport_request_connect (void *cls, size_t size, void *buf)
1269 struct TransportRequestConnectMessage *trcm = cls;
1274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1275 "Buffer null for %s\n",
1282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1283 "Transmitting `%s' request for `%4s'.\n",
1285 GNUNET_i2s (&trcm->peer));
1287 GNUNET_assert (size >= sizeof (struct TransportRequestConnectMessage));
1288 memcpy(buf, trcm, sizeof(struct TransportRequestConnectMessage));
1289 return sizeof(struct TransportRequestConnectMessage);
1293 * Create and send a request connect message to
1294 * the transport service for a particular peer.
1296 * @param h handle to the transport service
1297 * @param n the neighbor to send the request connect message about
1300 static void send_request_connect_message(struct GNUNET_TRANSPORT_Handle *h, struct NeighbourList *n)
1302 struct TransportRequestConnectMessage *trcm;
1304 trcm = GNUNET_malloc(sizeof(struct TransportRequestConnectMessage));
1305 trcm->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT);
1306 trcm->header.size = htons(sizeof(struct TransportRequestConnectMessage));
1307 memcpy(&trcm->peer, &n->id, sizeof(struct GNUNET_PeerIdentity));
1308 schedule_control_transmit (h,
1309 sizeof (struct TransportRequestConnectMessage),
1311 GNUNET_TIME_UNIT_FOREVER_REL, &send_transport_request_connect, trcm);
1315 * Add neighbour to our list
1317 * @return NULL if this API is currently disconnecting from the service
1319 static struct NeighbourList *
1320 neighbour_add (struct GNUNET_TRANSPORT_Handle *h,
1321 const struct GNUNET_PeerIdentity *pid)
1323 struct NeighbourList *n;
1325 if (GNUNET_YES == h->in_disconnect)
1327 /* check for duplicates */
1328 if (NULL != (n = neighbour_find (h, pid)))
1334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1335 "Creating entry for neighbour `%4s'.\n",
1338 n = GNUNET_malloc (sizeof (struct NeighbourList));
1340 GNUNET_BANDWIDTH_tracker_init (&n->out_tracker,
1341 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1342 MAX_BANDWIDTH_CARRY_S);
1343 n->next = h->neighbours;
1353 * Connect to the transport service. Note that the connection may
1354 * complete (or fail) asynchronously.
1356 * @param sched scheduler to use
1357 * @param cfg configuration to use
1358 * @param self our own identity (API should check that it matches
1359 * the identity found by transport), or NULL (no check)
1360 * @param cls closure for the callbacks
1361 * @param rec receive function to call
1362 * @param nc function to call on connect events
1363 * @param nd function to call on disconnect events
1365 struct GNUNET_TRANSPORT_Handle *
1366 GNUNET_TRANSPORT_connect (struct GNUNET_SCHEDULER_Handle *sched,
1367 const struct GNUNET_CONFIGURATION_Handle *cfg,
1368 const struct GNUNET_PeerIdentity *self,
1370 GNUNET_TRANSPORT_ReceiveCallback rec,
1371 GNUNET_TRANSPORT_NotifyConnect nc,
1372 GNUNET_TRANSPORT_NotifyDisconnect nd)
1374 struct GNUNET_TRANSPORT_Handle *ret;
1376 ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Handle));
1380 ret->check_self = GNUNET_YES;
1388 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1389 schedule_reconnect (ret);
1395 * Disconnect from the transport service.
1398 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1400 struct GNUNET_TRANSPORT_TransmitHandle *th;
1401 struct NeighbourList *n;
1402 struct HelloWaitList *hwl;
1403 struct GNUNET_CLIENT_Connection *client;
1404 struct ControlMessage *cm;
1407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transport disconnect called!\n");
1409 handle->in_disconnect = GNUNET_YES;
1410 while (NULL != (n = handle->neighbours))
1412 handle->neighbours = n->next;
1413 switch (n->transmit_stage)
1416 case TS_TRANSMITTED:
1420 case TS_TRANSMITTED_QUEUED:
1421 th = &n->transmit_handle;
1422 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1424 GNUNET_SCHEDULER_cancel (handle->sched,
1425 th->notify_delay_task);
1426 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1428 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
1435 while (NULL != (hwl = handle->hwl_head))
1437 handle->hwl_head = hwl->next;
1438 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1440 ("Disconnect while notification for `%s' still registered.\n"),
1442 if (hwl->rec != NULL)
1443 hwl->rec (hwl->rec_cls, NULL);
1447 /* Check for still scheduled control messages, cancel delay tasks if so */
1448 /* Added because somehow a notify_delay_task is remaining scheduled and is ever so annoying */
1449 while ( (NULL != (cm = handle->control_head)))
1452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1453 "Disconnect before control message sent!\n");
1455 if (cm->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1457 GNUNET_SCHEDULER_cancel (handle->sched, cm->notify_delay_task);
1458 cm->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1460 GNUNET_CONTAINER_DLL_remove (handle->control_head,
1461 handle->control_tail,
1467 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1469 GNUNET_SCHEDULER_cancel (handle->sched, handle->reconnect_task);
1470 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1472 if (handle->quota_task != GNUNET_SCHEDULER_NO_TASK)
1474 GNUNET_SCHEDULER_cancel (handle->sched, handle->quota_task);
1475 handle->quota_task = GNUNET_SCHEDULER_NO_TASK;
1477 GNUNET_free_non_null (handle->my_hello);
1478 handle->my_hello = NULL;
1480 if (NULL != handle->network_handle)
1482 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->network_handle);
1483 handle->network_handle = NULL;
1485 if (NULL != (client = handle->client))
1488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1489 "Disconnecting from transport service for good.\n");
1491 handle->client = NULL;
1492 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
1494 GNUNET_free (handle);
1499 * Function we use for handling incoming messages.
1501 * @param cls closure (struct GNUNET_TRANSPORT_Handle *)
1502 * @param msg message received, NULL on timeout or fatal error
1505 demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
1507 struct GNUNET_TRANSPORT_Handle *h = cls;
1508 const struct DisconnectInfoMessage *dim;
1509 const struct ConnectInfoMessage *cim;
1510 const struct InboundMessage *im;
1511 const struct GNUNET_MessageHeader *imm;
1512 const struct SendOkMessage *okm;
1513 struct HelloWaitList *hwl;
1514 struct HelloWaitList *next_hwl;
1515 struct NeighbourList *n;
1516 struct GNUNET_PeerIdentity me;
1519 if (h->client == NULL)
1521 /* shutdown initiated from 'GNUNET_TRANSPORT_disconnect',
1522 finish clean up work! */
1529 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1530 "Error receiving from transport service, disconnecting temporarily.\n");
1532 if (h->network_handle != NULL)
1534 GNUNET_CLIENT_notify_transmit_ready_cancel (h->network_handle);
1535 h->network_handle = NULL;
1537 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
1539 schedule_reconnect (h);
1542 GNUNET_CLIENT_receive (h->client,
1543 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1544 size = ntohs (msg->size);
1545 switch (ntohs (msg->type))
1547 case GNUNET_MESSAGE_TYPE_HELLO:
1549 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg,
1556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1557 "Receiving (my own) `%s' message, I am `%4s'.\n",
1558 "HELLO", GNUNET_i2s (&me));
1560 GNUNET_free_non_null (h->my_hello);
1562 if (size < sizeof (struct GNUNET_MessageHeader))
1567 h->my_hello = GNUNET_malloc (size);
1568 memcpy (h->my_hello, msg, size);
1572 next_hwl = hwl->next;
1573 hwl->rec (hwl->rec_cls,
1574 (const struct GNUNET_MessageHeader *) h->my_hello);
1578 case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
1579 if (size != sizeof (struct ConnectInfoMessage))
1584 cim = (const struct ConnectInfoMessage *) msg;
1586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1587 "Receiving `%s' message for `%4s'.\n",
1588 "CONNECT", GNUNET_i2s (&cim->id));
1590 n = neighbour_find (h, &cim->id);
1592 n = neighbour_add (h,
1596 GNUNET_break (n->is_connected == GNUNET_NO);
1597 n->is_connected = GNUNET_YES;
1598 if (h->nc_cb != NULL)
1599 h->nc_cb (h->cls, &n->id,
1600 GNUNET_TIME_relative_ntoh (cim->latency),
1601 ntohl (cim->distance));
1603 case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
1604 if (size != sizeof (struct DisconnectInfoMessage))
1609 dim = (const struct DisconnectInfoMessage *) msg;
1610 GNUNET_break (ntohl (dim->reserved) == 0);
1611 #if DEBUG_TRANSPORT_DISCONNECT
1612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1613 "Receiving `%s' message for `%4s'.\n",
1615 GNUNET_i2s (&dim->peer));
1617 n = neighbour_find (h, &dim->peer);
1618 GNUNET_break (n != NULL);
1620 neighbour_disconnect (n);
1622 case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
1623 if (size != sizeof (struct SendOkMessage))
1628 okm = (const struct SendOkMessage *) msg;
1630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1631 "Receiving `%s' message, transmission %s.\n", "SEND_OK",
1632 ntohl (okm->success) == GNUNET_OK ? "succeeded" : "failed");
1634 n = neighbour_find (h, &okm->peer);
1635 GNUNET_assert (n != NULL);
1636 switch (n->transmit_stage)
1644 case TS_TRANSMITTED:
1645 n->transmit_stage = TS_NEW;
1647 case TS_TRANSMITTED_QUEUED:
1648 n->transmit_stage = TS_QUEUED;
1649 schedule_transmission (h);
1655 case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
1657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1658 "Receiving `%s' message.\n", "RECV");
1661 sizeof (struct InboundMessage) +
1662 sizeof (struct GNUNET_MessageHeader))
1667 im = (const struct InboundMessage *) msg;
1668 GNUNET_break (0 == ntohl (im->reserved));
1669 imm = (const struct GNUNET_MessageHeader *) &im[1];
1670 if (ntohs (imm->size) + sizeof (struct InboundMessage) != size)
1676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1677 "Received message of type %u from `%4s'.\n",
1678 ntohs (imm->type), GNUNET_i2s (&im->peer));
1680 n = neighbour_find (h, &im->peer);
1686 if (n->is_connected != GNUNET_YES)
1692 h->rec (h->cls, &im->peer, imm,
1693 GNUNET_TIME_relative_ntoh (im->latency), ntohl(im->distance));
1696 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1698 ("Received unexpected message of type %u in %s:%u\n"),
1699 ntohs (msg->type), __FILE__, __LINE__);
1707 * Called when our transmit request timed out before any transport
1708 * reported success connecting to the desired peer or before the
1709 * transport was ready to receive. Signal error and free
1712 * @param cls the 'struct GNUNET_TRANSPORT_TransmitHandle*' that is timing out
1713 * @param tc scheduler context
1716 peer_transmit_timeout (void *cls,
1717 const struct GNUNET_SCHEDULER_TaskContext *tc)
1719 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
1720 struct NeighbourList *n;
1721 GNUNET_CONNECTION_TransmitReadyNotify notify;
1724 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1727 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1728 "Triggering timeout for request to transmit to `%4s' (%d)\n",
1729 GNUNET_i2s (&n->id),
1732 notify = th->notify;
1733 notify_cls = th->notify_cls;
1734 switch (n->transmit_stage)
1740 n->transmit_stage = TS_NEW;
1741 if (n->is_connected == GNUNET_NO)
1744 case TS_TRANSMITTED:
1747 case TS_TRANSMITTED_QUEUED:
1748 n->transmit_stage = TS_TRANSMITTED;
1754 notify (notify_cls, 0, NULL);
1759 * Check if we could queue a message of the given size for
1760 * transmission. The transport service will take both its
1761 * internal buffers and bandwidth limits imposed by the
1762 * other peer into consideration when answering this query.
1764 * @param handle connection to transport service
1765 * @param target who should receive the message
1766 * @param size how big is the message we want to transmit?
1767 * @param priority how important is the message?
1768 * @param timeout after how long should we give up (and call
1769 * notify with buf NULL and size 0)?
1770 * @param notify function to call when we are ready to
1771 * send such a message
1772 * @param notify_cls closure for notify
1773 * @return NULL if someone else is already waiting to be notified
1774 * non-NULL if the notify callback was queued (can be used to cancel
1775 * using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1777 struct GNUNET_TRANSPORT_TransmitHandle *
1778 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle
1780 const struct GNUNET_PeerIdentity
1781 *target, size_t size,
1782 unsigned int priority,
1783 struct GNUNET_TIME_Relative timeout,
1784 GNUNET_CONNECTION_TransmitReadyNotify
1785 notify, void *notify_cls)
1787 struct GNUNET_TRANSPORT_TransmitHandle *th;
1788 struct NeighbourList *n;
1790 if (size + sizeof (struct OutboundMessage) >=
1791 GNUNET_SERVER_MAX_MESSAGE_SIZE)
1794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1795 "Message size is %d, max allowed is %d.\n",
1796 size + sizeof (struct OutboundMessage), GNUNET_SERVER_MAX_MESSAGE_SIZE - 1);
1802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1803 "Asking transport service for transmission of %u bytes to peer `%4s' within %llu ms.\n",
1804 size, GNUNET_i2s (target),
1805 (unsigned long long) timeout.abs_value);
1807 n = neighbour_find (handle, target);
1810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1811 "Created neighbour entry for peer `%s'\n",
1812 GNUNET_i2s (target));
1813 n = neighbour_add (handle, target);
1818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1819 "Could not create neighbour entry for peer `%s'\n",
1820 GNUNET_i2s (target));
1825 * Send a request connect message if not connected,
1826 * otherwise we will never send anything to
1829 if (n->is_connected == GNUNET_NO)
1831 send_request_connect_message(handle, n);
1834 switch (n->transmit_stage)
1837 n->transmit_stage = TS_QUEUED;
1842 case TS_TRANSMITTED:
1843 n->transmit_stage = TS_TRANSMITTED_QUEUED;
1845 case TS_TRANSMITTED_QUEUED:
1852 th = &n->transmit_handle;
1854 th->notify = notify;
1855 th->notify_cls = notify_cls;
1856 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1857 th->notify_size = size + sizeof (struct OutboundMessage);
1858 th->priority = priority;
1859 th->notify_delay_task
1860 = GNUNET_SCHEDULER_add_delayed (handle->sched, timeout,
1861 &peer_transmit_timeout, th);
1862 schedule_transmission (handle);
1868 * Cancel the specified transmission-ready notification.
1871 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
1872 GNUNET_TRANSPORT_TransmitHandle
1875 struct NeighbourList *n;
1879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1880 "Transmission request of %u bytes to `%4s' was cancelled.\n",
1881 th->notify_size - sizeof (struct OutboundMessage),
1882 GNUNET_i2s (&n->id));
1884 switch (n->transmit_stage)
1890 n->transmit_stage = TS_NEW;
1891 if (n->is_connected == GNUNET_NO)
1894 case TS_TRANSMITTED:
1897 case TS_TRANSMITTED_QUEUED:
1898 n->transmit_stage = TS_TRANSMITTED;
1906 /* end of transport_api.c */