2 This file is part of GNUnet.
3 (C) 2009 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 * - set_quota with low bandwidth should cause peer
28 * disconnects (currently never does that) (MINOR)
31 #include "gnunet_client_lib.h"
32 #include "gnunet_arm_service.h"
33 #include "gnunet_hello_lib.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_server_lib.h"
36 #include "gnunet_time_lib.h"
37 #include "gnunet_transport_service.h"
38 #include "transport.h"
41 * After how long do we give up on transmitting a HELLO
44 #define OFFER_HELLO_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
47 * How long should ARM wait when starting up the
48 * transport service before reporting back?
50 #define START_SERVICE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
53 * How long should ARM wait when stopping the
54 * transport service before reporting back?
56 #define STOP_SERVICE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
59 * Entry in linked list of all of our current neighbours.
65 * This is a linked list.
67 struct NeighbourList *next;
70 * Active transmit handle, can be NULL. Used to move
71 * from ready to wait list on disconnect and to block
72 * two transmissions to the same peer from being scheduled
75 struct GNUNET_TRANSPORT_TransmitHandle *transmit_handle;
79 * Identity of this neighbour.
81 struct GNUNET_PeerIdentity id;
84 * At what time did we reset last_sent last?
86 struct GNUNET_TIME_Absolute last_quota_update;
89 * How many bytes have we sent since the "last_quota_update"
95 * Global quota for outbound traffic to the neighbour in bytes/ms.
100 * Set to GNUNET_YES if we are currently allowed to
101 * transmit a message to the transport service for this
102 * peer, GNUNET_NO otherwise.
107 * Set to GNUNET_YES if we have received an ACK for the
108 * given peer. Peers that receive our HELLO always respond
109 * with an ACK to let us know that we are successfully
110 * communicating. Note that a PING can not be used for this
111 * since PINGs are only send if a HELLO address requires
112 * confirmation (and also, PINGs are not passed to the
113 * transport API itself).
121 * Linked list of requests from clients for our HELLO
122 * that were deferred.
128 * This is a linked list.
130 struct HelloWaitList *next;
133 * Reference back to our transport handle.
135 struct GNUNET_TRANSPORT_Handle *handle;
138 * Callback to call once we got our HELLO.
140 GNUNET_TRANSPORT_ReceiveCallback rec;
148 * When to time out (call rec with NULL).
150 struct GNUNET_TIME_Absolute timeout;
153 * Timeout task (used to trigger timeout,
154 * cancel if we get the HELLO in time).
156 GNUNET_SCHEDULER_TaskIdentifier task;
163 * Opaque handle for a transmission-ready request.
165 struct GNUNET_TRANSPORT_TransmitHandle
169 * We keep the transmit handles that are waiting for
170 * a transport-level connection in a doubly linked list.
172 struct GNUNET_TRANSPORT_TransmitHandle *next;
175 * We keep the transmit handles that are waiting for
176 * a transport-level connection in a doubly linked list.
178 struct GNUNET_TRANSPORT_TransmitHandle *prev;
181 * Handle of the main transport data structure.
183 struct GNUNET_TRANSPORT_Handle *handle;
186 * Neighbour for this handle, can be NULL if the service
187 * is not yet connected to the target.
189 struct NeighbourList *neighbour;
192 * Which peer is this transmission going to be for? All
193 * zeros if it is control-traffic to the service.
195 struct GNUNET_PeerIdentity target;
198 * Function to call when notify_size bytes are available
201 GNUNET_NETWORK_TransmitReadyNotify notify;
204 * Closure for notify.
209 * transmit_ready task Id. The task is used to introduce
210 * the artificial delay that may be required to maintain
211 * the bandwidth limits.
213 GNUNET_SCHEDULER_TaskIdentifier notify_delay_task;
216 * Timeout for this request.
218 struct GNUNET_TIME_Absolute timeout;
221 * How many bytes is our notify callback waiting for?
229 * Handle for the transport service (includes all of the
230 * state for the transport service).
232 struct GNUNET_TRANSPORT_Handle
236 * Closure for the callbacks.
241 * Function to call for received data.
243 GNUNET_TRANSPORT_ReceiveCallback rec;
246 * function to call on connect events
248 GNUNET_TRANSPORT_NotifyConnect nc_cb;
251 * function to call on disconnect events
253 GNUNET_TRANSPORT_NotifyDisconnect nd_cb;
256 * The current HELLO message for this peer. Updated
257 * whenever transports change their addresses.
259 struct GNUNET_HELLO_Message *my_hello;
262 * My client connection to the transport service.
264 struct GNUNET_CLIENT_Connection *client;
267 * Handle to our registration with the client for notification.
269 struct GNUNET_NETWORK_TransmitHandle *network_handle;
272 * Linked list of transmit handles that are waiting for the
273 * transport to connect to the respective peer. When we
274 * receive notification that the transport connected to a
275 * peer, we go over this list and check if someone has already
276 * requested a transmission to the new peer; if so, we trigger
279 struct GNUNET_TRANSPORT_TransmitHandle *connect_wait_head;
282 * Linked list of transmit handles that are waiting for the
283 * transport to be ready for transmission to the respective
285 * receive notification that the transport disconnected from
286 * a peer, we go over this list and move the entry back to
287 * the connect_wait list.
289 struct GNUNET_TRANSPORT_TransmitHandle *connect_ready_head;
292 * Linked list of pending requests for our HELLO.
294 struct HelloWaitList *hwl_head;
299 struct GNUNET_SCHEDULER_Handle *sched;
304 struct GNUNET_CONFIGURATION_Handle *cfg;
307 * Linked list of the current neighbours of this peer.
309 struct NeighbourList *neighbours;
312 * ID of the task trying to reconnect to the
315 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
318 * Delay until we try to reconnect.
320 struct GNUNET_TIME_Relative reconnect_delay;
323 * Do we currently have a transmission pending?
324 * (schedule transmission was called but has not
327 int transmission_scheduled;
331 static struct NeighbourList *
332 find_neighbour (struct GNUNET_TRANSPORT_Handle *h,
333 const struct GNUNET_PeerIdentity *peer)
335 struct NeighbourList *pos;
338 while ((pos != NULL) &&
339 (0 != memcmp (peer, &pos->id, sizeof (struct GNUNET_PeerIdentity))))
346 * Schedule the task to send one message from the
347 * connect_ready list to the service.
349 static void schedule_transmission (struct GNUNET_TRANSPORT_Handle *h);
353 * Transmit message to client...
356 transport_notify_ready (void *cls, size_t size, void *buf)
358 struct GNUNET_TRANSPORT_Handle *h = cls;
359 struct GNUNET_TRANSPORT_TransmitHandle *th;
360 struct NeighbourList *n;
365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
366 "Ready to transmit %u bytes to transport service\n", size);
368 h->network_handle = NULL;
369 h->transmission_scheduled = GNUNET_NO;
372 th = h->connect_ready_head;
373 if (th->next != NULL)
374 th->next->prev = NULL;
375 h->connect_ready_head = th->next;
376 if (NULL != (n = th->neighbour))
378 GNUNET_assert (n->transmit_handle == th);
379 n->transmit_handle = NULL;
381 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
387 h->network_handle = NULL;
388 h->transmission_scheduled = GNUNET_NO;
391 th = h->connect_ready_head;
392 GNUNET_assert (th->notify_size <= size);
393 if (th->next != NULL)
394 th->next->prev = NULL;
395 h->connect_ready_head = th->next;
396 if (NULL != (n = th->neighbour))
398 GNUNET_assert (n->transmit_handle == th);
399 n->transmit_handle = NULL;
401 ret += th->notify (th->notify_cls, size, &cbuf[ret]);
407 while ((h->connect_ready_head != NULL) &&
408 (h->connect_ready_head->notify_size <= size));
409 if (h->connect_ready_head != NULL)
410 schedule_transmission (h);
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
413 "Transmitting %u bytes to transport service\n", ret);
420 * Schedule the task to send one message from the
421 * connect_ready list to the service.
424 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
426 struct GNUNET_TRANSPORT_TransmitHandle *th;
428 GNUNET_assert (NULL == h->network_handle);
429 if (h->client == NULL)
432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
433 "Not yet connected to transport service, need to wait.\n");
437 th = h->connect_ready_head;
441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
442 "Schedule transmission called, but no request is pending.\n");
446 h->transmission_scheduled = GNUNET_YES;
448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
449 "Asking client API for transmission of %u bytes\n",
452 h->network_handle = GNUNET_CLIENT_notify_transmit_ready (h->client,
454 GNUNET_TIME_absolute_get_remaining
456 &transport_notify_ready,
458 GNUNET_assert (NULL != h->network_handle);
463 * Insert the given transmit handle in the given sorted
464 * doubly linked list based on timeout.
466 * @param head pointer to the head of the linked list
467 * @param th element to insert into the list
470 insert_transmit_handle (struct GNUNET_TRANSPORT_TransmitHandle **head,
471 struct GNUNET_TRANSPORT_TransmitHandle *th)
473 struct GNUNET_TRANSPORT_TransmitHandle *pos;
474 struct GNUNET_TRANSPORT_TransmitHandle *prev;
478 while ((pos != NULL) && (pos->timeout.value < th->timeout.value))
486 if (th->next != NULL)
502 * Queue control request for transmission to the transport
505 * @param size number of bytes to be transmitted
506 * @param at_head request must be added to the head of the queue
507 * (otherwise request will be appended)
508 * @param timeout how long this transmission can wait (at most)
509 * @param notify function to call to get the content
510 * @param notify_cls closure for notify
513 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h,
516 struct GNUNET_TIME_Relative timeout,
517 GNUNET_NETWORK_TransmitReadyNotify notify,
520 struct GNUNET_TRANSPORT_TransmitHandle *th;
522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
523 "Queueing %u bytes control transmission request.\n", size);
525 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
528 th->notify_cls = notify_cls;
529 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
530 th->notify_size = size;
533 th->next = h->connect_ready_head;
534 h->connect_ready_head = th;
535 if (th->next != NULL)
540 insert_transmit_handle (&h->connect_ready_head, th);
542 if (GNUNET_NO == h->transmission_scheduled)
543 schedule_transmission (h);
548 * Update the quota values for the given neighbour now.
551 update_quota (struct NeighbourList *n)
553 struct GNUNET_TIME_Relative delta;
557 delta = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
558 allowed = delta.value * n->quota_out;
559 if (n->last_sent < allowed)
561 remaining = allowed - n->last_sent;
562 if (n->quota_out > 0)
563 remaining /= n->quota_out;
566 if (remaining > MAX_BANDWIDTH_CARRY)
567 remaining = MAX_BANDWIDTH_CARRY;
569 n->last_quota_update = GNUNET_TIME_absolute_get ();
570 n->last_quota_update.value -= remaining;
574 n->last_sent -= allowed;
575 n->last_quota_update = GNUNET_TIME_absolute_get ();
580 struct SetQuotaContext
582 struct GNUNET_TRANSPORT_Handle *handle;
584 struct GNUNET_PeerIdentity target;
586 GNUNET_SCHEDULER_Task cont;
590 struct GNUNET_TIME_Absolute timeout;
597 send_set_quota (void *cls, size_t size, void *buf)
599 struct SetQuotaContext *sqc = cls;
600 struct QuotaSetMessage *msg;
604 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
608 GNUNET_SCHEDULER_REASON_TIMEOUT);
613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
614 "Transmitting `%s' request with respect to `%4s'.\n",
615 "SET_QUOTA", GNUNET_i2s (&sqc->target));
617 GNUNET_assert (size >= sizeof (struct QuotaSetMessage));
619 msg->header.size = htons (sizeof (struct QuotaSetMessage));
620 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
621 msg->quota_in = htonl (sqc->quota_in);
622 memcpy (&msg->peer, &sqc->target, sizeof (struct GNUNET_PeerIdentity));
623 if (sqc->cont != NULL)
624 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
628 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
630 return sizeof (struct QuotaSetMessage);
635 * Set the share of incoming bandwidth for the given
636 * peer to the specified amount.
638 * @param handle connection to transport service
639 * @param target who's bandwidth quota is being changed
640 * @param quota_in incoming bandwidth quota in bytes per ms; 0 can
641 * be used to force all traffic to be discarded
642 * @param quota_out outgoing bandwidth quota in bytes per ms; 0 can
643 * be used to force all traffic to be discarded
644 * @param timeout how long to wait until signaling failure if
645 * we can not communicate the quota change
646 * @param cont continuation to call when done, will be called
647 * either with reason "TIMEOUT" or with reason "PREREQ_DONE"
648 * @param cont_cls closure for continuation
651 GNUNET_TRANSPORT_set_quota (struct GNUNET_TRANSPORT_Handle *handle,
652 const struct GNUNET_PeerIdentity *target,
655 struct GNUNET_TIME_Relative timeout,
656 GNUNET_SCHEDULER_Task cont, void *cont_cls)
658 struct NeighbourList *n;
659 struct SetQuotaContext *sqc;
661 n = find_neighbour (handle, target);
665 if (n->quota_out < quota_out)
666 n->last_quota_update = GNUNET_TIME_absolute_get ();
667 n->quota_out = quota_out;
669 sqc = GNUNET_malloc (sizeof (struct SetQuotaContext));
670 sqc->handle = handle;
671 sqc->target = *target;
673 sqc->cont_cls = cont_cls;
674 sqc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
675 sqc->quota_in = quota_in;
676 schedule_control_transmit (handle,
677 sizeof (struct QuotaSetMessage),
678 GNUNET_NO, timeout, &send_set_quota, sqc);
683 * A "get_hello" request has timed out. Signal the client
687 hello_wait_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
689 struct HelloWaitList *hwl = cls;
690 struct HelloWaitList *pos;
691 struct HelloWaitList *prev;
694 pos = hwl->handle->hwl_head;
697 GNUNET_assert (pos != NULL);
702 hwl->handle->hwl_head = hwl->next;
704 prev->next = hwl->next;
705 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
706 _("Timeout trying to obtain `%s' from transport service.\n"),
709 if (hwl->rec != NULL)
710 hwl->rec (hwl->rec_cls, GNUNET_TIME_UNIT_ZERO, NULL, NULL);
716 * Obtain the HELLO message for this peer.
718 * @param handle connection to transport service
719 * @param timeout how long to wait for the HELLO
720 * @param rec function to call with the HELLO, sender will be our peer
721 * identity; message and sender will be NULL on timeout
722 * (handshake with transport service pending/failed).
723 * cost estimate will be 0.
724 * @param rec_cls closure for rec
727 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
728 struct GNUNET_TIME_Relative timeout,
729 GNUNET_TRANSPORT_ReceiveCallback rec,
732 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
733 struct GNUNET_PeerIdentity me;
734 struct HelloWaitList *hwl;
736 if (handle->my_hello == NULL)
738 hwl = GNUNET_malloc (sizeof (struct HelloWaitList));
739 hwl->next = handle->hwl_head;
740 handle->hwl_head = hwl;
741 hwl->handle = handle;
743 hwl->rec_cls = rec_cls;
744 hwl->timeout = GNUNET_TIME_relative_to_absolute (timeout);
745 hwl->task = GNUNET_SCHEDULER_add_delayed (handle->sched,
747 GNUNET_SCHEDULER_PRIORITY_KEEP,
748 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
750 &hello_wait_timeout, hwl);
753 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_key (handle->my_hello, &pk));
754 GNUNET_CRYPTO_hash (&pk,
755 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
759 GNUNET_TIME_UNIT_ZERO,
760 &me, (const struct GNUNET_MessageHeader *) handle->my_hello);
765 send_hello (void *cls, size_t size, void *buf)
767 struct GNUNET_MessageHeader *hello = cls;
773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
774 "Timeout while trying to transmit `%s' request.\n",
781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
782 "Transmitting `%s' request.\n", "HELLO");
784 msize = ntohs (hello->size);
785 GNUNET_assert (size >= msize);
786 memcpy (buf, hello, msize);
793 * Offer the transport service the HELLO of another peer. Note that
794 * the transport service may just ignore this message if the HELLO is
795 * malformed or useless due to our local configuration.
797 * @param handle connection to transport service
798 * @param hello the hello message
801 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
802 const struct GNUNET_MessageHeader *hello)
804 struct GNUNET_MessageHeader *hc;
807 if (handle->client == NULL)
810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
811 "Not connected to transport service, dropping offered HELLO\n");
815 GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
816 size = ntohs (hello->size);
817 GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
818 hc = GNUNET_malloc (size);
819 memcpy (hc, hello, size);
820 schedule_control_transmit (handle,
822 GNUNET_NO, OFFER_HELLO_TIMEOUT, &send_hello, hc);
827 * Function we use for handling incoming messages.
829 static void demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg);
833 send_start (void *cls, size_t size, void *buf)
835 struct GNUNET_MessageHeader *s = buf;
840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
841 "Transmitting `%s' request.\n", "START");
843 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
844 s->size = htons (sizeof (struct GNUNET_MessageHeader));
845 s->type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
846 return sizeof (struct GNUNET_MessageHeader);
851 * Try again to connect to transport service.
854 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
856 struct GNUNET_TRANSPORT_Handle *h = cls;
857 struct GNUNET_TRANSPORT_TransmitHandle *pos;
858 struct NeighbourList *n;
860 while (NULL != (n = h->neighbours))
862 h->neighbours = n->next;
863 pos = n->transmit_handle;
866 pos->neighbour = NULL;
867 pos->next = h->connect_wait_head;
868 h->connect_wait_head = pos;
869 if (pos->next != NULL)
870 pos->next->prev = pos;
875 h->connect_ready_head = NULL;
877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n");
879 GNUNET_assert (h->client == NULL);
880 h->reconnect_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
881 h->client = GNUNET_CLIENT_connect (h->sched, "transport", h->cfg);
882 GNUNET_assert (h->client != NULL);
883 /* make sure we don't send "START" twice,
884 remove existing entry from queue (if present) */
885 pos = h->connect_ready_head;
888 if (pos->notify == &send_start)
890 if (pos->prev == NULL)
891 h->connect_ready_head = pos->next;
893 pos->prev->next = pos->next;
894 if (pos->next != NULL)
895 pos->next->prev = pos->prev;
896 GNUNET_assert (pos->neighbour == NULL);
902 schedule_control_transmit (h,
903 sizeof (struct GNUNET_MessageHeader),
905 GNUNET_TIME_UNIT_FOREVER_REL, &send_start, NULL);
906 GNUNET_CLIENT_receive (h->client,
907 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
912 * Function that will schedule the job that will try
913 * to connect us again to the client.
916 schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
920 "Scheduling task to reconnect to transport service in %llu ms.\n",
921 h->reconnect_delay.value);
923 GNUNET_assert (h->client == NULL);
924 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK);
926 = GNUNET_SCHEDULER_add_delayed (h->sched,
928 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
929 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
930 h->reconnect_delay, &reconnect, h);
931 h->reconnect_delay = GNUNET_TIME_UNIT_SECONDS;
936 * Remove the given transmit handle from the wait list. Does NOT free
940 remove_from_wait_list (struct GNUNET_TRANSPORT_TransmitHandle *th)
942 if (th->prev == NULL)
943 th->handle->connect_wait_head = th->next;
945 th->prev->next = th->next;
946 if (th->next != NULL)
947 th->next->prev = th->prev;
952 * We are connected to the respective peer, check the
953 * bandwidth limits and schedule the transmission.
955 static void schedule_request (struct GNUNET_TRANSPORT_TransmitHandle *th);
959 * Function called by the scheduler when the timeout
960 * for bandwidth availablility for the target
961 * neighbour is reached.
964 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
966 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
968 th->notify_delay_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
969 schedule_request (th);
974 * Called when our transmit request timed out before any transport
975 * reported success connecting to the desired peer or before the
976 * transport was ready to receive. Signal error and free
980 transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
982 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
984 if (th->neighbour != NULL)
985 th->neighbour->transmit_handle = NULL;
987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmission request timed out.\n");
989 th->notify_delay_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
990 remove_from_wait_list (th);
991 th->notify (th->notify_cls, 0, NULL);
997 * We are connected to the respective peer, check the
998 * bandwidth limits and schedule the transmission.
1001 schedule_request (struct GNUNET_TRANSPORT_TransmitHandle *th)
1003 struct GNUNET_TRANSPORT_Handle *h;
1004 struct GNUNET_TIME_Relative duration;
1005 struct NeighbourList *n;
1010 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1012 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
1013 th->notify_delay_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1015 /* check outgoing quota */
1016 duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
1017 if (duration.value > MIN_QUOTA_REFRESH_TIME)
1020 duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
1022 available = duration.value * n->quota_out;
1023 if (available < n->last_sent + th->notify_size)
1025 /* calculate how much bandwidth we'd still need to
1026 accumulate and based on that how long we'll have
1028 available = n->last_sent + th->notify_size - available;
1029 duration = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1030 available / n->quota_out);
1031 if (th->timeout.value <
1032 GNUNET_TIME_relative_to_absolute (duration).value)
1034 /* signal timeout! */
1036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1037 "Would need %llu ms before bandwidth is available for delivery, that is too long. Signaling timeout.\n",
1040 remove_from_wait_list (th);
1041 th->notify (th->notify_cls, 0, NULL);
1046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1047 "Need more bandwidth, delaying delivery by %llu ms\n",
1050 th->notify_delay_task
1051 = GNUNET_SCHEDULER_add_delayed (h->sched,
1053 GNUNET_SCHEDULER_PRIORITY_KEEP,
1054 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1055 duration, &transmit_ready, th);
1059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1060 "Bandwidth available for transmission to `%4s'\n",
1061 GNUNET_i2s (&n->id));
1063 if (GNUNET_NO == n->transmit_ok)
1065 /* we may be ready, but transport service is not;
1066 wait for SendOkMessage or timeout */
1068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1069 "Need to wait for transport service `%s' message\n",
1072 th->notify_delay_task
1073 = GNUNET_SCHEDULER_add_delayed (h->sched,
1075 GNUNET_SCHEDULER_PRIORITY_KEEP,
1076 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1077 GNUNET_TIME_absolute_get_remaining
1078 (th->timeout), &transmit_timeout, th);
1081 n->transmit_ok = GNUNET_NO;
1082 remove_from_wait_list (th);
1084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Moving message to ready list\n");
1086 insert_transmit_handle (&h->connect_ready_head, th);
1087 if (GNUNET_NO == h->transmission_scheduled)
1088 schedule_transmission (h);
1093 * Add neighbour to our list
1096 add_neighbour (struct GNUNET_TRANSPORT_Handle *h,
1098 struct GNUNET_TIME_Relative latency,
1099 const struct GNUNET_PeerIdentity *pid)
1101 struct NeighbourList *n;
1102 struct GNUNET_TRANSPORT_TransmitHandle *prev;
1103 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1104 struct GNUNET_TRANSPORT_TransmitHandle *next;
1106 /* check for duplicates */
1107 if (NULL != find_neighbour (h, pid))
1113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1114 "Creating entry for new neighbour `%4s'.\n", GNUNET_i2s (pid));
1116 n = GNUNET_malloc (sizeof (struct NeighbourList));
1118 n->last_quota_update = GNUNET_TIME_absolute_get ();
1119 n->quota_out = quota_out;
1120 n->next = h->neighbours;
1121 n->transmit_ok = GNUNET_YES;
1123 if (h->nc_cb != NULL)
1124 h->nc_cb (h->cls, &n->id, latency);
1126 pos = h->connect_wait_head;
1131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1132 "Found entry in connect_wait_head for `%4s'.\n",
1133 GNUNET_i2s (&pos->target));
1135 if (0 == memcmp (pid,
1136 &pos->target, sizeof (struct GNUNET_PeerIdentity)))
1139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1140 "Found pending request for new connection, will trigger now.\n");
1143 if (pos->notify_delay_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1145 GNUNET_SCHEDULER_cancel (h->sched, pos->notify_delay_task);
1146 pos->notify_delay_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1148 GNUNET_assert (NULL == n->transmit_handle);
1149 n->transmit_handle = pos;
1150 if (GNUNET_YES == n->received_ack)
1153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1154 "`%s' already received, scheduling request\n",
1157 schedule_request (pos);
1162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1163 "Still need to wait to receive `%s' message\n",
1166 pos->notify_delay_task
1167 = GNUNET_SCHEDULER_add_delayed (h->sched,
1169 GNUNET_SCHEDULER_PRIORITY_KEEP,
1170 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1171 GNUNET_TIME_absolute_get_remaining
1173 &transmit_timeout, pos);
1176 h->connect_wait_head = next;
1188 * Connect to the transport service. Note that the connection may
1189 * complete (or fail) asynchronously.
1192 * @param sched scheduler to use
1193 * @param cfg configuration to use
1194 * @param cls closure for the callbacks
1195 * @param rec receive function to call
1196 * @param nc function to call on connect events
1197 * @param dc function to call on disconnect events
1199 struct GNUNET_TRANSPORT_Handle *
1200 GNUNET_TRANSPORT_connect (struct GNUNET_SCHEDULER_Handle *sched,
1201 struct GNUNET_CONFIGURATION_Handle *cfg,
1203 GNUNET_TRANSPORT_ReceiveCallback rec,
1204 GNUNET_TRANSPORT_NotifyConnect nc,
1205 GNUNET_TRANSPORT_NotifyDisconnect nd)
1207 struct GNUNET_TRANSPORT_Handle *ret;
1209 GNUNET_ARM_start_service ("peerinfo",
1210 cfg, sched, START_SERVICE_TIMEOUT, NULL, NULL);
1211 GNUNET_ARM_start_service ("transport",
1212 cfg, sched, START_SERVICE_TIMEOUT, NULL, NULL);
1213 ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Handle));
1220 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1221 schedule_reconnect (ret);
1227 * These stop activities must be run in a fresh
1228 * scheduler that is NOT in shutdown mode.
1231 stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1233 struct GNUNET_TRANSPORT_Handle *handle = cls;
1234 GNUNET_ARM_stop_service ("transport",
1236 tc->sched, STOP_SERVICE_TIMEOUT, NULL, NULL);
1237 GNUNET_ARM_stop_service ("peerinfo",
1239 tc->sched, STOP_SERVICE_TIMEOUT, NULL, NULL);
1244 * Disconnect from the transport service.
1247 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1249 struct GNUNET_TRANSPORT_TransmitHandle *th;
1250 struct NeighbourList *n;
1251 struct HelloWaitList *hwl;
1252 struct GNUNET_CLIENT_Connection *client;
1255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transport disconnect called!\n");
1257 while (NULL != (th = handle->connect_ready_head))
1259 handle->connect_ready_head = th->next;
1260 th->notify (th->notify_cls, 0, NULL);
1264 while (NULL != (th = handle->connect_wait_head))
1266 handle->connect_wait_head = th->next;
1267 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1269 GNUNET_SCHEDULER_cancel (handle->sched, th->notify_delay_task);
1270 th->notify_delay_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1272 th->notify (th->notify_cls, 0, NULL);
1275 while (NULL != (n = handle->neighbours))
1277 handle->neighbours = n->next;
1280 while (NULL != (hwl = handle->hwl_head))
1282 handle->hwl_head = hwl->next;
1283 GNUNET_SCHEDULER_cancel (handle->sched, hwl->task);
1284 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1286 ("Disconnect while trying to obtain HELLO from transport service.\n"));
1287 if (hwl->rec != NULL)
1288 hwl->rec (hwl->rec_cls, GNUNET_TIME_UNIT_ZERO, NULL, NULL);
1291 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1293 GNUNET_SCHEDULER_cancel (handle->sched, handle->reconnect_task);
1294 handle->reconnect_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1296 GNUNET_free_non_null (handle->my_hello);
1297 handle->my_hello = NULL;
1298 GNUNET_SCHEDULER_run (&stop_task, handle);
1299 if (NULL != (client = handle->client))
1302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1303 "Disconnecting from transport service for good.\n");
1305 handle->client = NULL;
1306 GNUNET_CLIENT_disconnect (client);
1309 GNUNET_free (handle);
1314 * We're ready to transmit the request that the transport service
1315 * should connect to a new peer. In addition to sending the
1316 * request, schedule the next phase for the transmission processing
1317 * that caused the connect request in the first place.
1320 request_connect (void *cls, size_t size, void *buf)
1322 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
1323 struct TryConnectMessage *tcm;
1324 struct GNUNET_TRANSPORT_Handle *h;
1330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1331 "Failed to transmit connect request to service.\n");
1333 th->notify (th->notify_cls, 0, NULL);
1338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1339 "Transmitting `%s' message for `%4s'.\n",
1340 "TRY_CONNECT", GNUNET_i2s (&th->target));
1342 GNUNET_assert (size >= sizeof (struct TryConnectMessage));
1344 tcm->header.size = htons (sizeof (struct TryConnectMessage));
1345 tcm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TRY_CONNECT);
1346 tcm->reserved = htonl (0);
1347 memcpy (&tcm->peer, &th->target, sizeof (struct GNUNET_PeerIdentity));
1348 th->notify_delay_task
1349 = GNUNET_SCHEDULER_add_delayed (h->sched,
1351 GNUNET_SCHEDULER_PRIORITY_KEEP,
1352 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1353 GNUNET_TIME_absolute_get_remaining (th->
1355 &transmit_timeout, th);
1356 insert_transmit_handle (&h->connect_wait_head, th);
1357 return sizeof (struct TryConnectMessage);
1362 * Schedule a request to connect to the given
1363 * neighbour (and if successful, add the specified
1364 * handle to the wait list).
1367 try_connect (struct GNUNET_TRANSPORT_TransmitHandle *th)
1369 schedule_control_transmit (th->handle,
1370 sizeof (struct TryConnectMessage),
1372 GNUNET_TIME_absolute_get_remaining (th->timeout),
1373 &request_connect, th);
1378 * Cancel a pending notify transmit task
1379 * and also remove the given transmit handle
1380 * from whatever list is on.
1383 remove_from_any_list (struct GNUNET_TRANSPORT_TransmitHandle *th)
1385 struct GNUNET_TRANSPORT_Handle *h;
1388 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1390 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
1391 th->notify_delay_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1393 if (th->prev == NULL)
1395 if (th == h->connect_wait_head)
1396 h->connect_wait_head = th->next;
1398 h->connect_ready_head = th->next;
1401 th->prev->next = th->next;
1402 if (th->next != NULL)
1403 th->next->prev = th->prev;
1408 * Remove neighbour from our list
1411 remove_neighbour (struct GNUNET_TRANSPORT_Handle *h,
1412 const struct GNUNET_PeerIdentity *peer)
1414 struct NeighbourList *prev;
1415 struct NeighbourList *pos;
1416 struct GNUNET_TRANSPORT_TransmitHandle *th;
1419 pos = h->neighbours;
1420 while ((pos != NULL) &&
1421 (0 != memcmp (peer, &pos->id, sizeof (struct GNUNET_PeerIdentity))))
1432 h->neighbours = pos->next;
1434 prev->next = pos->next;
1435 if (NULL != (th = pos->transmit_handle))
1437 pos->transmit_handle = NULL;
1438 th->neighbour = NULL;
1439 remove_from_any_list (th);
1442 if (h->nc_cb != NULL)
1443 h->nd_cb (h->cls, peer);
1449 * Type of a function to call when we receive a message
1452 * @param cls closure
1453 * @param msg message received, NULL on timeout or fatal error
1456 demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
1458 struct GNUNET_TRANSPORT_Handle *h = cls;
1459 const struct DisconnectInfoMessage *dim;
1460 const struct ConnectInfoMessage *cim;
1461 const struct InboundMessage *im;
1462 const struct GNUNET_MessageHeader *imm;
1463 const struct SendOkMessage *okm;
1464 struct HelloWaitList *hwl;
1465 struct NeighbourList *n;
1466 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
1467 struct GNUNET_PeerIdentity me;
1470 if ((msg == NULL) || (h->client == NULL))
1472 if (h->client != NULL)
1475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1476 "Error receiving from transport service, disconnecting temporarily.\n");
1478 if (h->network_handle != NULL)
1480 GNUNET_NETWORK_notify_transmit_ready_cancel (h->network_handle);
1481 h->network_handle = NULL;
1482 h->transmission_scheduled = GNUNET_NO;
1484 GNUNET_CLIENT_disconnect (h->client);
1486 schedule_reconnect (h);
1490 /* shutdown initiated from 'GNUNET_TRANSPORT_disconnect',
1491 finish clean up work! */
1496 GNUNET_CLIENT_receive (h->client,
1497 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1498 size = ntohs (msg->size);
1499 switch (ntohs (msg->type))
1501 case GNUNET_MESSAGE_TYPE_HELLO:
1503 GNUNET_HELLO_get_key ((const struct GNUNET_HELLO_Message *) msg,
1509 GNUNET_CRYPTO_hash (&pkey,
1511 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1515 "Receiving (my own) `%s' message, I am `%4s'.\n",
1516 "HELLO", GNUNET_i2s (&me));
1518 GNUNET_free_non_null (h->my_hello);
1520 if (size < sizeof (struct GNUNET_MessageHeader))
1525 h->my_hello = GNUNET_malloc (size);
1526 memcpy (h->my_hello, msg, size);
1527 while (NULL != (hwl = h->hwl_head))
1529 h->hwl_head = hwl->next;
1530 GNUNET_SCHEDULER_cancel (h->sched, hwl->task);
1531 GNUNET_TRANSPORT_get_hello (h,
1532 GNUNET_TIME_UNIT_ZERO,
1533 hwl->rec, hwl->rec_cls);
1537 case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
1538 if (size != sizeof (struct ConnectInfoMessage))
1543 cim = (const struct ConnectInfoMessage *) msg;
1545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1546 "Receiving `%s' message for `%4s'.\n",
1547 "CONNECT", GNUNET_i2s (&cim->id));
1550 ntohl (cim->quota_out),
1551 GNUNET_TIME_relative_ntoh (cim->latency), &cim->id);
1553 case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
1554 if (size != sizeof (struct DisconnectInfoMessage))
1559 dim = (const struct DisconnectInfoMessage *) msg;
1561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1562 "Receiving `%s' message for `%4s'.\n",
1563 "DISCONNECT", GNUNET_i2s (&dim->peer));
1565 remove_neighbour (h, &dim->peer);
1567 case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
1569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1570 "Receiving `%s' message.\n", "SEND_OK");
1572 if (size != sizeof (struct SendOkMessage))
1577 okm = (const struct SendOkMessage *) msg;
1578 n = find_neighbour (h, &okm->peer);
1579 GNUNET_assert (n != NULL);
1580 n->transmit_ok = GNUNET_YES;
1581 if (n->transmit_handle != NULL)
1584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1585 "Processing pending message\n");
1587 GNUNET_SCHEDULER_cancel (h->sched,
1588 n->transmit_handle->notify_delay_task);
1589 n->transmit_handle->notify_delay_task =
1590 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1591 GNUNET_assert (GNUNET_YES == n->received_ack);
1592 schedule_request (n->transmit_handle);
1595 case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
1597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1598 "Receiving `%s' message.\n", "RECV");
1601 sizeof (struct InboundMessage) +
1602 sizeof (struct GNUNET_MessageHeader))
1607 im = (const struct InboundMessage *) msg;
1608 imm = (const struct GNUNET_MessageHeader *) &im[1];
1609 if (ntohs (imm->size) + sizeof (struct InboundMessage) != size)
1614 switch (ntohs (imm->type))
1616 case GNUNET_MESSAGE_TYPE_TRANSPORT_ACK:
1618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1619 "Receiving `%s' message from `%4s'.\n",
1620 "ACK", GNUNET_i2s (&im->peer));
1622 n = find_neighbour (h, &im->peer);
1628 if (n->received_ack == GNUNET_NO)
1630 n->received_ack = GNUNET_YES;
1631 if (NULL != n->transmit_handle)
1634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1635 "Peer connected, scheduling delayed message for deliverery now.\n");
1637 schedule_request (n->transmit_handle);
1643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1644 "Received message of type %u from `%4s'.\n",
1645 ntohs (imm->type), GNUNET_i2s (&im->peer));
1649 GNUNET_TIME_relative_ntoh (im->latency), &im->peer, imm);
1654 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1656 ("Received unexpected message of type %u from `%4s' in %s:%u\n"),
1657 ntohs (msg->type), GNUNET_i2s (&im->peer), __FILE__,
1665 struct ClientTransmitWrapper
1667 GNUNET_NETWORK_TransmitReadyNotify notify;
1669 struct GNUNET_TRANSPORT_TransmitHandle *th;
1674 * Transmit message of a client destined for another
1675 * peer to the service.
1678 client_notify_wrapper (void *cls, size_t size, void *buf)
1680 struct ClientTransmitWrapper *ctw = cls;
1681 struct OutboundMessage *obm;
1682 struct GNUNET_MessageHeader *hdr;
1688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1689 "Transmission request could not be satisfied.\n");
1691 ret = ctw->notify (ctw->notify_cls, 0, NULL);
1692 GNUNET_assert (ret == 0);
1696 GNUNET_assert (size >= sizeof (struct OutboundMessage));
1698 ret = ctw->notify (ctw->notify_cls,
1699 size - sizeof (struct OutboundMessage),
1703 /* Need to reset flag, no SEND means no SEND_OK! */
1704 ctw->th->neighbour->transmit_ok = GNUNET_YES;
1708 GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
1709 hdr = (struct GNUNET_MessageHeader *) &obm[1];
1710 GNUNET_assert (ntohs (hdr->size) == ret);
1711 GNUNET_assert (ret + sizeof (struct OutboundMessage) <
1712 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1715 "Transmitting `%s' message with data for `%4s'\n",
1716 "SEND", GNUNET_i2s (&ctw->th->target));
1718 ret += sizeof (struct OutboundMessage);
1719 obm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
1720 obm->header.size = htons (ret);
1721 obm->reserved = htonl (0);
1722 obm->peer = ctw->th->target;
1730 * Check if we could queue a message of the given size for
1731 * transmission. The transport service will take both its
1732 * internal buffers and bandwidth limits imposed by the
1733 * other peer into consideration when answering this query.
1735 * @param handle connection to transport service
1736 * @param target who should receive the message
1737 * @param size how big is the message we want to transmit?
1738 * @param timeout after how long should we give up (and call
1739 * notify with buf NULL and size 0)?
1740 * @param notify function to call when we are ready to
1741 * send such a message
1742 * @param notify_cls closure for notify
1743 * @return NULL if someone else is already waiting to be notified
1744 * non-NULL if the notify callback was queued (can be used to cancel
1745 * using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1747 struct GNUNET_TRANSPORT_TransmitHandle *
1748 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle
1750 const struct GNUNET_PeerIdentity
1751 *target, size_t size,
1752 struct GNUNET_TIME_Relative timeout,
1753 GNUNET_NETWORK_TransmitReadyNotify
1754 notify, void *notify_cls)
1756 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1757 struct GNUNET_TRANSPORT_TransmitHandle *th;
1758 struct NeighbourList *n;
1759 struct ClientTransmitWrapper *ctw;
1761 if (size + sizeof (struct OutboundMessage) >=
1762 GNUNET_SERVER_MAX_MESSAGE_SIZE)
1765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1766 "Asking transport service for transmission of %u bytes to peer `%4s'.\n",
1767 size, GNUNET_i2s (target));
1769 n = find_neighbour (handle, target);
1770 ctw = GNUNET_malloc (sizeof (struct ClientTransmitWrapper));
1771 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
1772 ctw->notify = notify;
1773 ctw->notify_cls = notify_cls;
1775 th->handle = handle;
1776 th->target = *target;
1777 th->notify = &client_notify_wrapper;
1778 th->notify_cls = ctw;
1779 th->notify_size = size + sizeof (struct OutboundMessage);
1780 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1786 "Transmission request could not be satisfied (not yet connected), adding it to pending request list.\n");
1788 pos = handle->connect_wait_head;
1791 GNUNET_assert (0 != memcmp (target,
1793 sizeof (struct GNUNET_PeerIdentity)));
1797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1798 "Will now try to connect to `%4s'.\n", GNUNET_i2s (target));
1805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1806 "Transmission request queued for transmission to transport service.\n");
1808 GNUNET_assert (NULL == n->transmit_handle);
1809 n->transmit_handle = th;
1810 if (GNUNET_YES == n->received_ack)
1813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1814 "Peer `%4s' is connected, scheduling for delivery now.\n",
1815 GNUNET_i2s (target));
1817 schedule_request (th);
1822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1823 "Connection to `%4s' is not yet confirmed connected, scheduling timeout (%llums) only.\n",
1824 GNUNET_i2s (target), timeout.value);
1826 th->notify_delay_task
1827 = GNUNET_SCHEDULER_add_delayed (handle->sched,
1829 GNUNET_SCHEDULER_PRIORITY_KEEP,
1830 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1831 timeout, &transmit_timeout, th);
1839 * Cancel the specified transmission-ready
1843 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
1844 GNUNET_TRANSPORT_TransmitHandle
1847 struct GNUNET_TRANSPORT_Handle *h;
1849 GNUNET_assert (th->notify == &client_notify_wrapper);
1850 remove_from_any_list (th);
1852 if ((h->connect_ready_head == NULL) && (h->network_handle != NULL))
1854 GNUNET_NETWORK_notify_transmit_ready_cancel (h->network_handle);
1855 h->network_handle = NULL;
1856 h->transmission_scheduled = GNUNET_NO;
1858 GNUNET_free (th->notify_cls);
1863 /* end of transport_api.c */