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 * After how long do we automatically retry an unsuccessful
50 #define CONNECT_RETRY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 750)
53 * How long should ARM wait when starting up the
54 * transport service before reporting back?
56 #define START_SERVICE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
59 * How long should ARM wait when stopping the
60 * transport service before reporting back?
62 #define STOP_SERVICE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
65 * Entry in linked list of all of our current neighbours.
71 * This is a linked list.
73 struct NeighbourList *next;
76 * Active transmit handle, can be NULL. Used to move
77 * from ready to wait list on disconnect and to block
78 * two transmissions to the same peer from being scheduled
81 struct GNUNET_TRANSPORT_TransmitHandle *transmit_handle;
84 * Identity of this neighbour.
86 struct GNUNET_PeerIdentity id;
89 * At what time did we reset last_sent last?
91 struct GNUNET_TIME_Absolute last_quota_update;
94 * How many bytes have we sent since the "last_quota_update"
100 * Quota for outbound traffic to the neighbour in bytes/ms.
105 * Set to GNUNET_YES if we are currently allowed to
106 * transmit a message to the transport service for this
107 * peer, GNUNET_NO otherwise.
112 * Set to GNUNET_YES if we have received an ACK for the
113 * given peer. Peers that receive our HELLO always respond
114 * with an ACK to let us know that we are successfully
115 * communicating. Note that a PING can not be used for this
116 * since PINGs are only send if a HELLO address requires
117 * confirmation (and also, PINGs are not passed to the
118 * transport API itself).
126 * Linked list of requests from clients for our HELLO
127 * that were deferred.
133 * This is a linked list.
135 struct HelloWaitList *next;
138 * Reference back to our transport handle.
140 struct GNUNET_TRANSPORT_Handle *handle;
143 * Callback to call once we got our HELLO.
145 GNUNET_TRANSPORT_ReceiveCallback rec;
153 * When to time out (call rec with NULL).
155 struct GNUNET_TIME_Absolute timeout;
158 * Timeout task (used to trigger timeout,
159 * cancel if we get the HELLO in time).
161 GNUNET_SCHEDULER_TaskIdentifier task;
168 * Opaque handle for a transmission-ready request.
170 struct GNUNET_TRANSPORT_TransmitHandle
174 * We keep the transmit handles that are waiting for
175 * a transport-level connection in a doubly linked list.
177 struct GNUNET_TRANSPORT_TransmitHandle *next;
180 * We keep the transmit handles that are waiting for
181 * a transport-level connection in a doubly linked list.
183 struct GNUNET_TRANSPORT_TransmitHandle *prev;
186 * Handle of the main transport data structure.
188 struct GNUNET_TRANSPORT_Handle *handle;
191 * Neighbour for this handle, can be NULL if the service
192 * is not yet connected to the target.
194 struct NeighbourList *neighbour;
197 * Which peer is this transmission going to be for? All
198 * zeros if it is control-traffic to the service.
200 struct GNUNET_PeerIdentity target;
203 * Function to call when notify_size bytes are available
206 GNUNET_CONNECTION_TransmitReadyNotify notify;
209 * Closure for notify.
214 * transmit_ready task Id. The task is used to introduce the
215 * artificial delay that may be required to maintain the bandwidth
216 * limits. Later, this will be the ID of the "transmit_timeout"
217 * task which is used to signal a timeout if the transmission could
218 * not be done in a timely fashion.
220 GNUNET_SCHEDULER_TaskIdentifier notify_delay_task;
223 * Timeout for this request.
225 struct GNUNET_TIME_Absolute timeout;
228 * How many bytes is our notify callback waiting for?
233 * How important is this message?
235 unsigned int priority;
241 * Handle for the transport service (includes all of the
242 * state for the transport service).
244 struct GNUNET_TRANSPORT_Handle
248 * Closure for the callbacks.
253 * Function to call for received data.
255 GNUNET_TRANSPORT_ReceiveCallback rec;
258 * function to call on connect events
260 GNUNET_TRANSPORT_NotifyConnect nc_cb;
263 * function to call on disconnect events
265 GNUNET_TRANSPORT_NotifyDisconnect nd_cb;
268 * The current HELLO message for this peer. Updated
269 * whenever transports change their addresses.
271 struct GNUNET_HELLO_Message *my_hello;
274 * My client connection to the transport service.
276 struct GNUNET_CLIENT_Connection *client;
279 * Handle to our registration with the client for notification.
281 struct GNUNET_CLIENT_TransmitHandle *network_handle;
284 * Linked list of transmit handles that are waiting for the
285 * transport to connect to the respective peer. When we
286 * receive notification that the transport connected to a
287 * peer, we go over this list and check if someone has already
288 * requested a transmission to the new peer; if so, we trigger
291 struct GNUNET_TRANSPORT_TransmitHandle *connect_wait_head;
294 * Linked list of transmit handles that are waiting for the
295 * transport to be ready for transmission to the respective
297 * receive notification that the transport disconnected from
298 * a peer, we go over this list and move the entry back to
299 * the connect_wait list.
301 struct GNUNET_TRANSPORT_TransmitHandle *connect_ready_head;
304 * Linked list of pending requests for our HELLO.
306 struct HelloWaitList *hwl_head;
311 struct GNUNET_SCHEDULER_Handle *sched;
316 const struct GNUNET_CONFIGURATION_Handle *cfg;
319 * Linked list of the current neighbours of this peer.
321 struct NeighbourList *neighbours;
324 * ID of the task trying to reconnect to the
327 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
330 * Delay until we try to reconnect.
332 struct GNUNET_TIME_Relative reconnect_delay;
335 * Do we currently have a transmission pending?
336 * (schedule transmission was called but has not
339 int transmission_scheduled;
343 static struct NeighbourList *
344 find_neighbour (struct GNUNET_TRANSPORT_Handle *h,
345 const struct GNUNET_PeerIdentity *peer)
347 struct NeighbourList *pos;
350 while ((pos != NULL) &&
351 (0 != memcmp (peer, &pos->id, sizeof (struct GNUNET_PeerIdentity))))
358 * Schedule the task to send one message from the
359 * connect_ready list to the service.
361 static void schedule_transmission (struct GNUNET_TRANSPORT_Handle *h);
365 * Transmit message to client...
368 transport_notify_ready (void *cls, size_t size, void *buf)
370 struct GNUNET_TRANSPORT_Handle *h = cls;
371 struct GNUNET_TRANSPORT_TransmitHandle *th;
372 struct NeighbourList *n;
376 h->network_handle = NULL;
377 h->transmission_scheduled = GNUNET_NO;
381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
382 "Could not transmit to transport service, cancelling pending requests\n");
384 th = h->connect_ready_head;
385 if (th->next != NULL)
386 th->next->prev = NULL;
387 h->connect_ready_head = th->next;
388 if (NULL != (n = th->neighbour))
390 GNUNET_assert (n->transmit_handle == th);
391 n->transmit_handle = NULL;
393 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
395 GNUNET_SCHEDULER_cancel (h->sched,
396 th->notify_delay_task);
397 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
399 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
401 if (h->connect_ready_head != NULL)
402 schedule_transmission (h); /* FIXME: is this ok? */
406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
407 "Ready to transmit %u bytes to transport service\n", size);
411 h->network_handle = NULL;
412 h->transmission_scheduled = GNUNET_NO;
413 while ((h->connect_ready_head != NULL) &&
414 (h->connect_ready_head->notify_size <= size))
416 th = h->connect_ready_head;
417 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
419 GNUNET_SCHEDULER_cancel (h->sched,
420 th->notify_delay_task);
421 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
423 GNUNET_assert (th->notify_size <= size);
424 if (th->next != NULL)
425 th->next->prev = NULL;
426 h->connect_ready_head = th->next;
427 if (NULL != (n = th->neighbour))
429 GNUNET_assert (n->transmit_handle == th);
430 n->transmit_handle = NULL;
432 ret += th->notify (th->notify_cls, size, &cbuf[ret]);
438 if (h->connect_ready_head != NULL)
439 schedule_transmission (h);
441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
442 "Transmitting %u bytes to transport service\n", ret);
449 * Schedule the task to send one message from the
450 * connect_ready list to the service.
453 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
455 struct GNUNET_TRANSPORT_TransmitHandle *th;
457 GNUNET_assert (NULL == h->network_handle);
458 if (h->client == NULL)
460 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
461 "Could not yet schedule transmission: we are not yet connected to the transport service!\n");
462 return; /* not yet connected */
464 th = h->connect_ready_head;
466 return; /* no request pending */
467 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
469 /* remove existing time out task, will be integrated
470 with transmit_ready notification! */
471 GNUNET_SCHEDULER_cancel (h->sched,
472 th->notify_delay_task);
473 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
475 h->transmission_scheduled = GNUNET_YES;
476 h->network_handle = GNUNET_CLIENT_notify_transmit_ready (h->client,
478 GNUNET_TIME_absolute_get_remaining
481 &transport_notify_ready,
483 GNUNET_assert (NULL != h->network_handle);
488 * Insert the given transmit handle in the given sorted
489 * doubly linked list based on timeout.
491 * @param head pointer to the head of the linked list
492 * @param th element to insert into the list
495 insert_transmit_handle (struct GNUNET_TRANSPORT_TransmitHandle **head,
496 struct GNUNET_TRANSPORT_TransmitHandle *th)
498 struct GNUNET_TRANSPORT_TransmitHandle *pos;
499 struct GNUNET_TRANSPORT_TransmitHandle *prev;
503 while ((pos != NULL) && (pos->timeout.value < th->timeout.value))
511 if (th->next != NULL)
527 * Cancel a pending notify delay task (if pending) and also remove the
528 * given transmit handle from whatever list is on.
530 * @param th handle for the transmission request to manipulate
533 remove_from_any_list (struct GNUNET_TRANSPORT_TransmitHandle *th)
535 struct GNUNET_TRANSPORT_Handle *h;
538 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
540 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
541 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
543 if (th->prev == NULL)
545 if (th == h->connect_wait_head)
546 h->connect_wait_head = th->next;
548 h->connect_ready_head = th->next;
552 th->prev->next = th->next;
554 if (th->next != NULL)
555 th->next->prev = th->prev;
560 * Schedule a request to connect to the given
561 * neighbour (and if successful, add the specified
562 * handle to the wait list).
564 * @param th handle for a request to transmit once we
568 try_connect (struct GNUNET_TRANSPORT_TransmitHandle *th);
572 * Called when our transmit request timed out before any transport
573 * reported success connecting to the desired peer or before the
574 * transport was ready to receive. Signal error and free
578 peer_transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
580 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
582 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
583 if (th->neighbour != NULL)
584 th->neighbour->transmit_handle = NULL;
586 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
587 "Request for transmission to peer `%s' timed out.\n",
588 GNUNET_i2s(&th->target));
590 remove_from_any_list (th);
591 th->notify (th->notify_cls, 0, NULL);
599 * Queue control request for transmission to the transport
602 * @param h handle to the transport service
603 * @param size number of bytes to be transmitted
604 * @param at_head request must be added to the head of the queue
605 * (otherwise request will be appended)
606 * @param timeout how long this transmission can wait (at most)
607 * @param notify function to call to get the content
608 * @param notify_cls closure for notify
611 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h,
614 struct GNUNET_TIME_Relative timeout,
615 GNUNET_CONNECTION_TransmitReadyNotify notify,
618 struct GNUNET_TRANSPORT_TransmitHandle *th;
621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
622 "Control transmit of %u bytes within %llums requested\n",
624 (unsigned long long) timeout.value);
626 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
629 th->notify_cls = notify_cls;
630 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
631 th->notify_size = size;
632 th->notify_delay_task
633 = GNUNET_SCHEDULER_add_delayed (h->sched,
635 &peer_transmit_timeout, th);
638 th->next = h->connect_ready_head;
639 h->connect_ready_head = th;
640 if (th->next != NULL)
645 insert_transmit_handle (&h->connect_ready_head, th);
647 if (GNUNET_NO == h->transmission_scheduled)
648 schedule_transmission (h);
653 * Update the quota values for the given neighbour now.
656 update_quota (struct NeighbourList *n)
658 struct GNUNET_TIME_Relative delta;
662 delta = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
663 allowed = delta.value * n->quota_out;
664 if (n->last_sent < allowed)
666 remaining = allowed - n->last_sent;
667 if (n->quota_out > 0)
668 remaining /= n->quota_out;
671 if (remaining > MAX_BANDWIDTH_CARRY)
672 remaining = MAX_BANDWIDTH_CARRY;
674 n->last_quota_update = GNUNET_TIME_absolute_get ();
675 n->last_quota_update.value -= remaining;
679 n->last_sent -= allowed;
680 n->last_quota_update = GNUNET_TIME_absolute_get ();
685 struct SetQuotaContext
687 struct GNUNET_TRANSPORT_Handle *handle;
689 struct GNUNET_PeerIdentity target;
691 GNUNET_SCHEDULER_Task cont;
695 struct GNUNET_TIME_Absolute timeout;
702 send_set_quota (void *cls, size_t size, void *buf)
704 struct SetQuotaContext *sqc = cls;
705 struct QuotaSetMessage *msg;
709 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
712 GNUNET_SCHEDULER_REASON_TIMEOUT);
717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
718 "Transmitting `%s' request with respect to `%4s'.\n",
719 "SET_QUOTA", GNUNET_i2s (&sqc->target));
721 GNUNET_assert (size >= sizeof (struct QuotaSetMessage));
723 msg->header.size = htons (sizeof (struct QuotaSetMessage));
724 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
725 msg->quota_in = htonl (sqc->quota_in);
726 memcpy (&msg->peer, &sqc->target, sizeof (struct GNUNET_PeerIdentity));
727 if (sqc->cont != NULL)
728 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
731 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
733 return sizeof (struct QuotaSetMessage);
738 * Set the share of incoming bandwidth for the given
739 * peer to the specified amount.
741 * @param handle connection to transport service
742 * @param target who's bandwidth quota is being changed
743 * @param quota_in incoming bandwidth quota in bytes per ms; 0 can
744 * be used to force all traffic to be discarded
745 * @param quota_out outgoing bandwidth quota in bytes per ms; 0 can
746 * be used to force all traffic to be discarded
747 * @param timeout how long to wait until signaling failure if
748 * we can not communicate the quota change
749 * @param cont continuation to call when done, will be called
750 * either with reason "TIMEOUT" or with reason "PREREQ_DONE"
751 * @param cont_cls closure for continuation
754 GNUNET_TRANSPORT_set_quota (struct GNUNET_TRANSPORT_Handle *handle,
755 const struct GNUNET_PeerIdentity *target,
758 struct GNUNET_TIME_Relative timeout,
759 GNUNET_SCHEDULER_Task cont, void *cont_cls)
761 struct NeighbourList *n;
762 struct SetQuotaContext *sqc;
764 n = find_neighbour (handle, target);
768 if (n->quota_out < quota_out)
769 n->last_quota_update = GNUNET_TIME_absolute_get ();
770 n->quota_out = quota_out;
772 sqc = GNUNET_malloc (sizeof (struct SetQuotaContext));
773 sqc->handle = handle;
774 sqc->target = *target;
776 sqc->cont_cls = cont_cls;
777 sqc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
778 sqc->quota_in = quota_in;
779 schedule_control_transmit (handle,
780 sizeof (struct QuotaSetMessage),
781 GNUNET_NO, timeout, &send_set_quota, sqc);
786 * A "get_hello" request has timed out. Signal the client
790 hello_wait_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
792 struct HelloWaitList *hwl = cls;
793 struct HelloWaitList *pos;
794 struct HelloWaitList *prev;
796 hwl->task = GNUNET_SCHEDULER_NO_TASK;
797 if (GNUNET_TIME_absolute_get_remaining (hwl->timeout).value > 0)
800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
801 _("First attempt to obtain `%s' from transport service failed, will try again for %llums.\n"),
803 GNUNET_TIME_absolute_get_remaining (hwl->timeout).value);
805 hwl->task = GNUNET_SCHEDULER_add_delayed (hwl->handle->sched,
806 GNUNET_TIME_absolute_get_remaining (hwl->timeout),
807 &hello_wait_timeout, hwl);
811 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
812 _("Timeout trying to obtain `%s' from transport service.\n"),
815 pos = hwl->handle->hwl_head;
818 GNUNET_assert (pos != NULL);
823 hwl->handle->hwl_head = hwl->next;
825 prev->next = hwl->next;
826 if (hwl->rec != NULL)
827 hwl->rec (hwl->rec_cls, GNUNET_TIME_UNIT_ZERO, NULL, NULL);
833 * Obtain the HELLO message for this peer.
835 * @param handle connection to transport service
836 * @param timeout how long to wait for the HELLO
837 * @param rec function to call with the HELLO, sender will be our peer
838 * identity; message and sender will be NULL on timeout
839 * (handshake with transport service pending/failed).
840 * cost estimate will be 0.
841 * @param rec_cls closure for rec
844 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
845 struct GNUNET_TIME_Relative timeout,
846 GNUNET_TRANSPORT_ReceiveCallback rec,
849 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
850 struct GNUNET_PeerIdentity me;
851 struct HelloWaitList *hwl;
853 if (handle->my_hello == NULL)
855 hwl = GNUNET_malloc (sizeof (struct HelloWaitList));
856 hwl->next = handle->hwl_head;
857 handle->hwl_head = hwl;
858 hwl->handle = handle;
860 hwl->rec_cls = rec_cls;
861 hwl->timeout = GNUNET_TIME_relative_to_absolute (timeout);
862 hwl->task = GNUNET_SCHEDULER_add_delayed (handle->sched,
864 &hello_wait_timeout, hwl);
867 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_key (handle->my_hello, &pk));
868 GNUNET_CRYPTO_hash (&pk,
869 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
873 GNUNET_TIME_UNIT_ZERO,
874 &me, (const struct GNUNET_MessageHeader *) handle->my_hello);
879 send_hello (void *cls, size_t size, void *buf)
881 struct GNUNET_MessageHeader *hello = cls;
887 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
888 "Timeout while trying to transmit `%s' request.\n",
895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
896 "Transmitting `%s' request.\n", "HELLO");
898 msize = ntohs (hello->size);
899 GNUNET_assert (size >= msize);
900 memcpy (buf, hello, msize);
907 * Offer the transport service the HELLO of another peer. Note that
908 * the transport service may just ignore this message if the HELLO is
909 * malformed or useless due to our local configuration.
911 * @param handle connection to transport service
912 * @param hello the hello message
915 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
916 const struct GNUNET_MessageHeader *hello)
918 struct GNUNET_MessageHeader *hc;
921 if (handle->client == NULL)
924 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
925 "Not connected to transport service, dropping offered HELLO\n");
929 GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
930 size = ntohs (hello->size);
931 GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
932 hc = GNUNET_malloc (size);
933 memcpy (hc, hello, size);
934 schedule_control_transmit (handle,
936 GNUNET_NO, OFFER_HELLO_TIMEOUT, &send_hello, hc);
941 * Function we use for handling incoming messages.
943 static void demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg);
947 send_start (void *cls, size_t size, void *buf)
949 struct GNUNET_MessageHeader *s = buf;
954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
955 "Timeout while trying to transmit `%s' request.\n",
961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
962 "Transmitting `%s' request.\n", "START");
964 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
965 s->size = htons (sizeof (struct GNUNET_MessageHeader));
966 s->type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
967 return sizeof (struct GNUNET_MessageHeader);
972 * We're ready to transmit the request that the transport service
973 * should connect to a new peer. In addition to sending the
974 * request, schedule the next phase for the transmission processing
975 * that caused the connect request in the first place.
978 request_connect (void *cls, size_t size, void *buf)
980 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
981 struct TryConnectMessage *tcm;
982 struct GNUNET_TRANSPORT_Handle *h;
984 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
990 "Failed to transmit `%s' request for `%4s' to service.\n",
992 GNUNET_i2s(&th->target));
994 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
996 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
997 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
999 th->notify (th->notify_cls, 0, NULL);
1004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1005 "Transmitting `%s' message for `%4s' (need connection in %llu ms).\n",
1006 "TRY_CONNECT", GNUNET_i2s (&th->target),
1007 GNUNET_TIME_absolute_get_remaining (th->timeout).value);
1009 GNUNET_assert (size >= sizeof (struct TryConnectMessage));
1011 tcm->header.size = htons (sizeof (struct TryConnectMessage));
1012 tcm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TRY_CONNECT);
1013 tcm->reserved = htonl (0);
1014 memcpy (&tcm->peer, &th->target, sizeof (struct GNUNET_PeerIdentity));
1015 th->notify_delay_task
1016 = GNUNET_SCHEDULER_add_delayed (h->sched,
1017 GNUNET_TIME_absolute_get_remaining
1019 &peer_transmit_timeout, th);
1020 insert_transmit_handle (&h->connect_wait_head, th);
1021 return sizeof (struct TryConnectMessage);
1026 * Schedule a request to connect to the given
1027 * neighbour (and if successful, add the specified
1028 * handle to the wait list).
1030 * @param th handle for a request to transmit once we
1034 try_connect (struct GNUNET_TRANSPORT_TransmitHandle *th)
1036 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
1037 schedule_control_transmit (th->handle,
1038 sizeof (struct TryConnectMessage),
1040 GNUNET_TIME_absolute_get_remaining (th->timeout),
1041 &request_connect, th);
1046 * Task for delayed attempts to reconnect to a peer.
1048 * @param cls must be a transmit handle that determines the peer
1049 * to which we will try to connect
1050 * @param tc scheduler information about why we were triggered (not used)
1053 try_connect_task (void *cls,
1054 const struct GNUNET_SCHEDULER_TaskContext *tc)
1056 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
1058 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1064 * Remove neighbour from our list. Will automatically
1065 * trigger a re-connect attempt if we have messages pending
1068 * @param h our state
1069 * @param peer the peer to remove
1072 remove_neighbour (struct GNUNET_TRANSPORT_Handle *h,
1073 const struct GNUNET_PeerIdentity *peer)
1075 struct NeighbourList *prev;
1076 struct NeighbourList *pos;
1077 struct GNUNET_TRANSPORT_TransmitHandle *th;
1080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1081 "Removing neighbour `%s' from list of connected peers.\n",
1085 pos = h->neighbours;
1086 while ((pos != NULL) &&
1089 sizeof (struct GNUNET_PeerIdentity))))
1100 h->neighbours = pos->next;
1102 prev->next = pos->next;
1103 if (NULL != (th = pos->transmit_handle))
1105 pos->transmit_handle = NULL;
1106 th->neighbour = NULL;
1107 remove_from_any_list (th);
1108 if (GNUNET_TIME_absolute_get_remaining (th->timeout).value <= CONNECT_RETRY_TIMEOUT.value)
1111 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1112 _("Connection with `%4s' failed and timeout was in the past, giving up on message delivery.\n"),
1114 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == th->notify_delay_task);
1115 peer_transmit_timeout (th, NULL);
1119 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1120 _("Connection with `%4s' failed, will keep trying for %llu ms to deliver message\n"),
1122 GNUNET_TIME_absolute_get_remaining (th->timeout).value);
1123 /* try again in a bit */
1124 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == th->notify_delay_task);
1125 th->notify_delay_task
1126 = GNUNET_SCHEDULER_add_delayed (h->sched,
1127 CONNECT_RETRY_TIMEOUT,
1132 if (h->nc_cb != NULL)
1133 h->nd_cb (h->cls, peer);
1139 * Try again to connect to transport service.
1142 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1144 struct GNUNET_TRANSPORT_Handle *h = cls;
1145 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1146 struct NeighbourList *n;
1148 /* Forget about all neighbours that we used to be connected
1150 while (NULL != (n = h->neighbours))
1151 remove_neighbour (h, &n->id);
1153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n");
1155 GNUNET_assert (h->client == NULL);
1156 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1157 h->client = GNUNET_CLIENT_connect (h->sched, "transport", h->cfg);
1158 GNUNET_assert (h->client != NULL);
1159 /* make sure we don't send "START" twice,
1160 remove existing entry from queue (if present) */
1161 pos = h->connect_ready_head;
1164 if (pos->notify == &send_start)
1166 if (pos->prev == NULL)
1167 h->connect_ready_head = pos->next;
1169 pos->prev->next = pos->next;
1170 if (pos->next != NULL)
1171 pos->next->prev = pos->prev;
1172 GNUNET_assert (pos->neighbour == NULL);
1173 if (GNUNET_SCHEDULER_NO_TASK != pos->notify_delay_task)
1175 GNUNET_SCHEDULER_cancel (h->sched,
1176 pos->notify_delay_task);
1177 pos->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1184 schedule_control_transmit (h,
1185 sizeof (struct GNUNET_MessageHeader),
1187 GNUNET_TIME_UNIT_FOREVER_REL,
1189 GNUNET_CLIENT_receive (h->client,
1190 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1195 * Function that will schedule the job that will try
1196 * to connect us again to the client.
1199 schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
1202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1203 "Scheduling task to reconnect to transport service in %llu ms.\n",
1204 h->reconnect_delay.value);
1206 GNUNET_assert (h->client == NULL);
1207 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
1209 = GNUNET_SCHEDULER_add_delayed (h->sched,
1210 h->reconnect_delay, &reconnect, h);
1211 h->reconnect_delay = GNUNET_TIME_UNIT_SECONDS;
1216 * We are connected to the respective peer, check the
1217 * bandwidth limits and schedule the transmission.
1219 static void schedule_request (struct GNUNET_TRANSPORT_TransmitHandle *th);
1223 * Function called by the scheduler when the timeout
1224 * for bandwidth availablility for the target
1225 * neighbour is reached.
1228 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1230 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
1232 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1233 schedule_request (th);
1238 * Remove the given transmit handle from the wait list. Does NOT free
1242 remove_from_wait_list (struct GNUNET_TRANSPORT_TransmitHandle *th)
1244 if (th->prev == NULL)
1245 th->handle->connect_wait_head = th->next;
1247 th->prev->next = th->next;
1248 if (th->next != NULL)
1249 th->next->prev = th->prev;
1254 * We are connected to the respective peer, check the
1255 * bandwidth limits and schedule the transmission.
1258 schedule_request (struct GNUNET_TRANSPORT_TransmitHandle *th)
1260 struct GNUNET_TRANSPORT_Handle *h;
1261 struct GNUNET_TIME_Relative duration;
1262 struct NeighbourList *n;
1267 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1269 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
1270 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1272 /* check outgoing quota */
1273 duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
1274 if (duration.value > MIN_QUOTA_REFRESH_TIME)
1277 duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
1279 available = duration.value * n->quota_out;
1280 if (available < n->last_sent + th->notify_size)
1282 /* calculate how much bandwidth we'd still need to
1283 accumulate and based on that how long we'll have
1285 available = n->last_sent + th->notify_size - available;
1286 duration = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1287 available / n->quota_out);
1288 if (th->timeout.value <
1289 GNUNET_TIME_relative_to_absolute (duration).value)
1291 /* signal timeout! */
1293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1294 "Would need %llu ms before bandwidth is available for delivery to `%4s', that is too long. Signaling timeout.\n",
1296 GNUNET_i2s(&th->target));
1298 remove_from_wait_list (th);
1299 th->notify (th->notify_cls, 0, NULL);
1304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1305 "Need more bandwidth, delaying delivery to `%4s' by %llu ms\n",
1306 GNUNET_i2s(&th->target),
1309 th->notify_delay_task
1310 = GNUNET_SCHEDULER_add_delayed (h->sched,
1311 duration, &transmit_ready, th);
1315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1316 "Bandwidth available for transmission to `%4s'\n",
1317 GNUNET_i2s (&n->id));
1319 if (GNUNET_NO == n->transmit_ok)
1321 /* we may be ready, but transport service is not;
1322 wait for SendOkMessage or timeout */
1324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1325 "Need to wait for transport service `%s' message\n",
1328 th->notify_delay_task
1329 = GNUNET_SCHEDULER_add_delayed (h->sched,
1330 GNUNET_TIME_absolute_get_remaining
1331 (th->timeout), &peer_transmit_timeout, th);
1334 n->transmit_ok = GNUNET_NO;
1335 remove_from_wait_list (th);
1337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Moving message for `%4s' to ready list\n",
1338 GNUNET_i2s(&n->id));
1340 insert_transmit_handle (&h->connect_ready_head, th);
1341 if (GNUNET_NO == h->transmission_scheduled)
1342 schedule_transmission (h);
1347 * Add neighbour to our list
1350 add_neighbour (struct GNUNET_TRANSPORT_Handle *h,
1352 struct GNUNET_TIME_Relative latency,
1353 const struct GNUNET_PeerIdentity *pid)
1355 struct NeighbourList *n;
1356 struct GNUNET_TRANSPORT_TransmitHandle *prev;
1357 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1358 struct GNUNET_TRANSPORT_TransmitHandle *next;
1360 /* check for duplicates */
1361 if (NULL != find_neighbour (h, pid))
1367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1368 "Creating entry for new neighbour `%4s'.\n", GNUNET_i2s (pid));
1370 n = GNUNET_malloc (sizeof (struct NeighbourList));
1372 n->last_quota_update = GNUNET_TIME_absolute_get ();
1373 n->quota_out = quota_out;
1374 n->next = h->neighbours;
1375 n->transmit_ok = GNUNET_YES;
1377 if (h->nc_cb != NULL)
1378 h->nc_cb (h->cls, &n->id, latency);
1380 pos = h->connect_wait_head;
1384 if (0 == memcmp (pid,
1385 &pos->target, sizeof (struct GNUNET_PeerIdentity)))
1388 GNUNET_assert (NULL == n->transmit_handle);
1389 n->transmit_handle = pos;
1391 h->connect_wait_head = next;
1394 if (GNUNET_YES == n->received_ack)
1397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1398 "Found pending request for `%4s' will trigger it now.\n",
1399 GNUNET_i2s (&pos->target));
1401 if (pos->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1403 GNUNET_SCHEDULER_cancel (h->sched, pos->notify_delay_task);
1404 pos->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1406 schedule_request (pos);
1411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1412 "Found pending request for `%4s' but still need `%s' before proceeding.\n",
1413 GNUNET_i2s (&pos->target),
1426 * Connect to the transport service. Note that the connection may
1427 * complete (or fail) asynchronously.
1430 * @param sched scheduler to use
1431 * @param cfg configuration to use
1432 * @param cls closure for the callbacks
1433 * @param rec receive function to call
1434 * @param nc function to call on connect events
1435 * @param nd function to call on disconnect events
1437 struct GNUNET_TRANSPORT_Handle *
1438 GNUNET_TRANSPORT_connect (struct GNUNET_SCHEDULER_Handle *sched,
1439 const struct GNUNET_CONFIGURATION_Handle *cfg,
1441 GNUNET_TRANSPORT_ReceiveCallback rec,
1442 GNUNET_TRANSPORT_NotifyConnect nc,
1443 GNUNET_TRANSPORT_NotifyDisconnect nd)
1445 struct GNUNET_TRANSPORT_Handle *ret;
1447 GNUNET_ARM_start_services (cfg, sched, "peerinfo", "transport", NULL);
1448 ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Handle));
1455 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1456 schedule_reconnect (ret);
1462 * Disconnect from the transport service.
1465 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1467 struct GNUNET_TRANSPORT_TransmitHandle *th;
1468 struct NeighbourList *n;
1469 struct HelloWaitList *hwl;
1470 struct GNUNET_CLIENT_Connection *client;
1473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1474 "Transport disconnect called!\n");
1476 while (NULL != (th = handle->connect_ready_head))
1478 handle->connect_ready_head = th->next;
1479 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1481 GNUNET_SCHEDULER_cancel (handle->sched, th->notify_delay_task);
1482 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1484 th->notify (th->notify_cls, 0, NULL);
1487 while (NULL != (th = handle->connect_wait_head))
1489 handle->connect_wait_head = th->next;
1490 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1492 GNUNET_SCHEDULER_cancel (handle->sched, th->notify_delay_task);
1493 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1495 th->notify (th->notify_cls, 0, NULL);
1498 while (NULL != (n = handle->neighbours))
1500 handle->neighbours = n->next;
1501 if (NULL != (th = n->transmit_handle))
1503 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1505 GNUNET_SCHEDULER_cancel (handle->sched, th->notify_delay_task);
1506 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1508 th->notify (th->notify_cls, 0, NULL);
1513 while (NULL != (hwl = handle->hwl_head))
1515 handle->hwl_head = hwl->next;
1516 GNUNET_SCHEDULER_cancel (handle->sched, hwl->task);
1517 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1518 _("Disconnect while trying to obtain `%s' from transport service.\n"),
1520 if (hwl->rec != NULL)
1521 hwl->rec (hwl->rec_cls, GNUNET_TIME_UNIT_ZERO, NULL, NULL);
1524 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1526 GNUNET_SCHEDULER_cancel (handle->sched, handle->reconnect_task);
1527 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1529 GNUNET_free_non_null (handle->my_hello);
1530 handle->my_hello = NULL;
1531 GNUNET_ARM_stop_services (handle->cfg, handle->sched, "transport", "peerinfo", NULL);
1532 if (NULL != handle->network_handle)
1534 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->network_handle);
1535 handle->network_handle = NULL;
1537 if (NULL != (client = handle->client))
1540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1541 "Disconnecting from transport service for good.\n");
1543 handle->client = NULL;
1544 GNUNET_CLIENT_disconnect (client);
1546 GNUNET_free (handle);
1551 * Type of a function to call when we receive a message
1554 * @param cls closure
1555 * @param msg message received, NULL on timeout or fatal error
1558 demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
1560 struct GNUNET_TRANSPORT_Handle *h = cls;
1561 const struct DisconnectInfoMessage *dim;
1562 const struct ConnectInfoMessage *cim;
1563 const struct InboundMessage *im;
1564 const struct GNUNET_MessageHeader *imm;
1565 const struct SendOkMessage *okm;
1566 struct HelloWaitList *hwl;
1567 struct NeighbourList *n;
1568 struct GNUNET_PeerIdentity me;
1569 struct GNUNET_TRANSPORT_TransmitHandle *th;
1572 if ((msg == NULL) || (h->client == NULL))
1574 if (h->client != NULL)
1577 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1578 "Error receiving from transport service, disconnecting temporarily.\n");
1580 if (h->network_handle != NULL)
1582 GNUNET_CLIENT_notify_transmit_ready_cancel (h->network_handle);
1583 h->network_handle = NULL;
1584 h->transmission_scheduled = GNUNET_NO;
1585 th = h->connect_ready_head;
1586 /* add timeout again, we cancelled the transmit_ready task! */
1587 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
1588 th->notify_delay_task
1589 = GNUNET_SCHEDULER_add_delayed (h->sched,
1590 GNUNET_TIME_absolute_get_remaining(th->timeout),
1591 &peer_transmit_timeout,
1594 GNUNET_CLIENT_disconnect (h->client);
1596 schedule_reconnect (h);
1600 /* shutdown initiated from 'GNUNET_TRANSPORT_disconnect',
1601 finish clean up work! */
1606 GNUNET_CLIENT_receive (h->client,
1607 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1608 size = ntohs (msg->size);
1609 switch (ntohs (msg->type))
1611 case GNUNET_MESSAGE_TYPE_HELLO:
1613 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg,
1620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1621 "Receiving (my own) `%s' message, I am `%4s'.\n",
1622 "HELLO", GNUNET_i2s (&me));
1624 GNUNET_free_non_null (h->my_hello);
1626 if (size < sizeof (struct GNUNET_MessageHeader))
1631 h->my_hello = GNUNET_malloc (size);
1632 memcpy (h->my_hello, msg, size);
1633 while (NULL != (hwl = h->hwl_head))
1635 h->hwl_head = hwl->next;
1636 GNUNET_SCHEDULER_cancel (h->sched, hwl->task);
1637 GNUNET_TRANSPORT_get_hello (h,
1638 GNUNET_TIME_UNIT_ZERO,
1639 hwl->rec, hwl->rec_cls);
1643 case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
1644 if (size != sizeof (struct ConnectInfoMessage))
1649 cim = (const struct ConnectInfoMessage *) msg;
1651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1652 "Receiving `%s' message for `%4s'.\n",
1653 "CONNECT", GNUNET_i2s (&cim->id));
1656 ntohl (cim->quota_out),
1657 GNUNET_TIME_relative_ntoh (cim->latency), &cim->id);
1659 case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
1660 if (size != sizeof (struct DisconnectInfoMessage))
1665 dim = (const struct DisconnectInfoMessage *) msg;
1667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1668 "Receiving `%s' message for `%4s'.\n",
1669 "DISCONNECT", GNUNET_i2s (&dim->peer));
1671 remove_neighbour (h, &dim->peer);
1673 case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
1674 if (size != sizeof (struct SendOkMessage))
1679 okm = (const struct SendOkMessage *) msg;
1681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1682 "Receiving `%s' message, transmission %s.\n", "SEND_OK",
1683 ntohl(okm->success) == GNUNET_OK ? "succeeded" : "failed");
1685 n = find_neighbour (h, &okm->peer);
1686 GNUNET_assert (n != NULL);
1687 n->transmit_ok = GNUNET_YES;
1688 if (n->transmit_handle != NULL)
1691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1692 "Processing pending message for `%4s'\n",
1693 GNUNET_i2s(&n->id));
1695 GNUNET_SCHEDULER_cancel (h->sched,
1696 n->transmit_handle->notify_delay_task);
1697 n->transmit_handle->notify_delay_task =
1698 GNUNET_SCHEDULER_NO_TASK;
1699 GNUNET_assert (GNUNET_YES == n->received_ack);
1700 schedule_request (n->transmit_handle);
1703 case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
1705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1706 "Receiving `%s' message.\n", "RECV");
1709 sizeof (struct InboundMessage) +
1710 sizeof (struct GNUNET_MessageHeader))
1715 im = (const struct InboundMessage *) msg;
1716 imm = (const struct GNUNET_MessageHeader *) &im[1];
1717 if (ntohs (imm->size) + sizeof (struct InboundMessage) != size)
1722 switch (ntohs (imm->type))
1724 case GNUNET_MESSAGE_TYPE_TRANSPORT_ACK:
1726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1727 "Receiving `%s' message from `%4s'.\n",
1728 "ACK", GNUNET_i2s (&im->peer));
1730 n = find_neighbour (h, &im->peer);
1736 if (n->received_ack == GNUNET_NO)
1738 n->received_ack = GNUNET_YES;
1739 if (NULL != n->transmit_handle)
1742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1743 "Peer connected, scheduling delayed message for deliverery now.\n");
1745 schedule_request (n->transmit_handle);
1751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1752 "Received message of type %u from `%4s'.\n",
1753 ntohs (imm->type), GNUNET_i2s (&im->peer));
1757 GNUNET_TIME_relative_ntoh (im->latency), &im->peer, imm);
1762 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1764 ("Received unexpected message of type %u in %s:%u\n"),
1765 ntohs (msg->type), __FILE__, __LINE__);
1772 struct ClientTransmitWrapper
1774 GNUNET_CONNECTION_TransmitReadyNotify notify;
1776 struct GNUNET_TRANSPORT_TransmitHandle *th;
1781 * Transmit message of a client destined for another
1782 * peer to the service.
1785 client_notify_wrapper (void *cls, size_t size, void *buf)
1787 struct ClientTransmitWrapper *ctw = cls;
1788 struct OutboundMessage *obm;
1789 struct GNUNET_MessageHeader *hdr;
1795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1796 "Transmission request could not be satisfied.\n");
1798 ret = ctw->notify (ctw->notify_cls, 0, NULL);
1799 GNUNET_assert (ret == 0);
1803 GNUNET_assert (size >= sizeof (struct OutboundMessage));
1805 ret = ctw->notify (ctw->notify_cls,
1806 size - sizeof (struct OutboundMessage),
1810 /* Need to reset flag, no SEND means no SEND_OK! */
1811 ctw->th->neighbour->transmit_ok = GNUNET_YES;
1815 GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
1816 hdr = (struct GNUNET_MessageHeader *) &obm[1];
1817 GNUNET_assert (ntohs (hdr->size) == ret);
1818 GNUNET_assert (ret + sizeof (struct OutboundMessage) <
1819 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1822 "Transmitting `%s' message with data for `%4s'\n",
1823 "SEND", GNUNET_i2s (&ctw->th->target));
1825 ret += sizeof (struct OutboundMessage);
1826 obm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
1827 obm->header.size = htons (ret);
1828 obm->priority = htonl (ctw->th->priority);
1829 obm->peer = ctw->th->target;
1837 * Check if we could queue a message of the given size for
1838 * transmission. The transport service will take both its
1839 * internal buffers and bandwidth limits imposed by the
1840 * other peer into consideration when answering this query.
1842 * @param handle connection to transport service
1843 * @param target who should receive the message
1844 * @param size how big is the message we want to transmit?
1845 * @param priority how important is the message?
1846 * @param timeout after how long should we give up (and call
1847 * notify with buf NULL and size 0)?
1848 * @param notify function to call when we are ready to
1849 * send such a message
1850 * @param notify_cls closure for notify
1851 * @return NULL if someone else is already waiting to be notified
1852 * non-NULL if the notify callback was queued (can be used to cancel
1853 * using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1855 struct GNUNET_TRANSPORT_TransmitHandle *
1856 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle
1858 const struct GNUNET_PeerIdentity
1859 *target, size_t size,
1860 unsigned int priority,
1861 struct GNUNET_TIME_Relative timeout,
1862 GNUNET_CONNECTION_TransmitReadyNotify
1863 notify, void *notify_cls)
1865 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1866 struct GNUNET_TRANSPORT_TransmitHandle *th;
1867 struct NeighbourList *n;
1868 struct ClientTransmitWrapper *ctw;
1870 if (size + sizeof (struct OutboundMessage) >=
1871 GNUNET_SERVER_MAX_MESSAGE_SIZE)
1877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1878 "Asking transport service for transmission of %u bytes to peer `%4s'.\n",
1879 size, GNUNET_i2s (target));
1881 n = find_neighbour (handle, target);
1883 (n->transmit_handle != NULL) )
1884 return NULL; /* already have a request pending for this peer! */
1885 ctw = GNUNET_malloc (sizeof (struct ClientTransmitWrapper));
1886 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
1887 ctw->notify = notify;
1888 ctw->notify_cls = notify_cls;
1890 th->handle = handle;
1892 th->target = *target;
1893 th->notify = &client_notify_wrapper;
1894 th->notify_cls = ctw;
1895 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1896 th->notify_size = size + sizeof (struct OutboundMessage);
1897 th->priority = priority;
1900 pos = handle->connect_wait_head;
1903 GNUNET_assert (0 != memcmp (target,
1905 sizeof (struct GNUNET_PeerIdentity)));
1909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1910 "Will now try to connect to `%4s'.\n", GNUNET_i2s (target));
1917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1918 "Transmission request queued for transmission to transport service.\n");
1920 GNUNET_assert (NULL == n->transmit_handle);
1921 n->transmit_handle = th;
1922 if (GNUNET_YES != n->received_ack)
1925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1926 "Connection to `%4s' is not yet confirmed connected, scheduling timeout (%llu ms) only.\n",
1927 GNUNET_i2s (target), timeout.value);
1929 th->notify_delay_task
1930 = GNUNET_SCHEDULER_add_delayed (handle->sched,
1931 timeout, &peer_transmit_timeout, th);
1936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1937 "Peer `%4s' is ready to receive, scheduling message for delivery now.\n",
1938 GNUNET_i2s (target));
1940 th->notify_delay_task
1941 = GNUNET_SCHEDULER_add_now (handle->sched,
1942 &transmit_ready, th);
1948 * Cancel the specified transmission-ready notification.
1951 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
1952 GNUNET_TRANSPORT_TransmitHandle
1955 struct GNUNET_TRANSPORT_Handle *h;
1958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1959 "Transmission request of %u bytes to `%4s' was cancelled.\n",
1960 th->notify_size - sizeof(struct OutboundMessage),
1961 GNUNET_i2s (&th->target));
1963 GNUNET_assert (th->notify == &client_notify_wrapper);
1964 remove_from_any_list (th);
1966 if ((h->connect_ready_head == NULL) && (h->network_handle != NULL))
1968 GNUNET_CLIENT_notify_transmit_ready_cancel (h->network_handle);
1969 h->network_handle = NULL;
1970 h->transmission_scheduled = GNUNET_NO;
1972 GNUNET_free (th->notify_cls);
1973 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
1978 /* end of transport_api.c */