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 give 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_INFO,
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) schedule_transmission (h); /* FIXME: is this ok? */
405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
406 "Ready to transmit %u bytes to transport service\n", size);
410 h->network_handle = NULL;
411 h->transmission_scheduled = GNUNET_NO;
414 th = h->connect_ready_head;
415 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
417 GNUNET_SCHEDULER_cancel (h->sched,
418 th->notify_delay_task);
419 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
421 GNUNET_assert (th->notify_size <= size);
422 if (th->next != NULL)
423 th->next->prev = NULL;
424 h->connect_ready_head = th->next;
425 if (NULL != (n = th->neighbour))
427 GNUNET_assert (n->transmit_handle == th);
428 n->transmit_handle = NULL;
430 ret += th->notify (th->notify_cls, size, &cbuf[ret]);
436 while ((h->connect_ready_head != NULL) &&
437 (h->connect_ready_head->notify_size <= size));
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;
620 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
623 th->notify_cls = notify_cls;
624 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
625 th->notify_size = size;
626 th->notify_delay_task
627 = GNUNET_SCHEDULER_add_delayed (h->sched,
629 GNUNET_SCHEDULER_PRIORITY_KEEP,
630 GNUNET_SCHEDULER_NO_TASK,
632 &peer_transmit_timeout, th);
635 th->next = h->connect_ready_head;
636 h->connect_ready_head = th;
637 if (th->next != NULL)
642 insert_transmit_handle (&h->connect_ready_head, th);
644 if (GNUNET_NO == h->transmission_scheduled)
645 schedule_transmission (h);
650 * Update the quota values for the given neighbour now.
653 update_quota (struct NeighbourList *n)
655 struct GNUNET_TIME_Relative delta;
659 delta = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
660 allowed = delta.value * n->quota_out;
661 if (n->last_sent < allowed)
663 remaining = allowed - n->last_sent;
664 if (n->quota_out > 0)
665 remaining /= n->quota_out;
668 if (remaining > MAX_BANDWIDTH_CARRY)
669 remaining = MAX_BANDWIDTH_CARRY;
671 n->last_quota_update = GNUNET_TIME_absolute_get ();
672 n->last_quota_update.value -= remaining;
676 n->last_sent -= allowed;
677 n->last_quota_update = GNUNET_TIME_absolute_get ();
682 struct SetQuotaContext
684 struct GNUNET_TRANSPORT_Handle *handle;
686 struct GNUNET_PeerIdentity target;
688 GNUNET_SCHEDULER_Task cont;
692 struct GNUNET_TIME_Absolute timeout;
699 send_set_quota (void *cls, size_t size, void *buf)
701 struct SetQuotaContext *sqc = cls;
702 struct QuotaSetMessage *msg;
706 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
710 GNUNET_SCHEDULER_REASON_TIMEOUT);
715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
716 "Transmitting `%s' request with respect to `%4s'.\n",
717 "SET_QUOTA", GNUNET_i2s (&sqc->target));
719 GNUNET_assert (size >= sizeof (struct QuotaSetMessage));
721 msg->header.size = htons (sizeof (struct QuotaSetMessage));
722 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
723 msg->quota_in = htonl (sqc->quota_in);
724 memcpy (&msg->peer, &sqc->target, sizeof (struct GNUNET_PeerIdentity));
725 if (sqc->cont != NULL)
726 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
730 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
732 return sizeof (struct QuotaSetMessage);
737 * Set the share of incoming bandwidth for the given
738 * peer to the specified amount.
740 * @param handle connection to transport service
741 * @param target who's bandwidth quota is being changed
742 * @param quota_in incoming bandwidth quota in bytes per ms; 0 can
743 * be used to force all traffic to be discarded
744 * @param quota_out outgoing bandwidth quota in bytes per ms; 0 can
745 * be used to force all traffic to be discarded
746 * @param timeout how long to wait until signaling failure if
747 * we can not communicate the quota change
748 * @param cont continuation to call when done, will be called
749 * either with reason "TIMEOUT" or with reason "PREREQ_DONE"
750 * @param cont_cls closure for continuation
753 GNUNET_TRANSPORT_set_quota (struct GNUNET_TRANSPORT_Handle *handle,
754 const struct GNUNET_PeerIdentity *target,
757 struct GNUNET_TIME_Relative timeout,
758 GNUNET_SCHEDULER_Task cont, void *cont_cls)
760 struct NeighbourList *n;
761 struct SetQuotaContext *sqc;
763 n = find_neighbour (handle, target);
767 if (n->quota_out < quota_out)
768 n->last_quota_update = GNUNET_TIME_absolute_get ();
769 n->quota_out = quota_out;
771 sqc = GNUNET_malloc (sizeof (struct SetQuotaContext));
772 sqc->handle = handle;
773 sqc->target = *target;
775 sqc->cont_cls = cont_cls;
776 sqc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
777 sqc->quota_in = quota_in;
778 schedule_control_transmit (handle,
779 sizeof (struct QuotaSetMessage),
780 GNUNET_NO, timeout, &send_set_quota, sqc);
785 * A "get_hello" request has timed out. Signal the client
789 hello_wait_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
791 struct HelloWaitList *hwl = cls;
792 struct HelloWaitList *pos;
793 struct HelloWaitList *prev;
795 hwl->task = GNUNET_SCHEDULER_NO_TASK;
796 if (GNUNET_TIME_absolute_get_remaining (hwl->timeout).value > 0)
799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
800 _("First attempt to obtain `%s' from transport service failed, will try again for %llums.\n"),
802 GNUNET_TIME_absolute_get_remaining (hwl->timeout).value);
804 hwl->task = GNUNET_SCHEDULER_add_delayed (hwl->handle->sched,
806 GNUNET_SCHEDULER_PRIORITY_KEEP,
807 GNUNET_SCHEDULER_NO_TASK,
808 GNUNET_TIME_absolute_get_remaining (hwl->timeout),
809 &hello_wait_timeout, hwl);
813 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
814 _("Timeout trying to obtain `%s' from transport service.\n"),
817 pos = hwl->handle->hwl_head;
820 GNUNET_assert (pos != NULL);
825 hwl->handle->hwl_head = hwl->next;
827 prev->next = hwl->next;
828 if (hwl->rec != NULL)
829 hwl->rec (hwl->rec_cls, GNUNET_TIME_UNIT_ZERO, NULL, NULL);
835 * Obtain the HELLO message for this peer.
837 * @param handle connection to transport service
838 * @param timeout how long to wait for the HELLO
839 * @param rec function to call with the HELLO, sender will be our peer
840 * identity; message and sender will be NULL on timeout
841 * (handshake with transport service pending/failed).
842 * cost estimate will be 0.
843 * @param rec_cls closure for rec
846 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
847 struct GNUNET_TIME_Relative timeout,
848 GNUNET_TRANSPORT_ReceiveCallback rec,
851 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
852 struct GNUNET_PeerIdentity me;
853 struct HelloWaitList *hwl;
855 if (handle->my_hello == NULL)
857 hwl = GNUNET_malloc (sizeof (struct HelloWaitList));
858 hwl->next = handle->hwl_head;
859 handle->hwl_head = hwl;
860 hwl->handle = handle;
862 hwl->rec_cls = rec_cls;
863 hwl->timeout = GNUNET_TIME_relative_to_absolute (timeout);
864 hwl->task = GNUNET_SCHEDULER_add_delayed (handle->sched,
866 GNUNET_SCHEDULER_PRIORITY_KEEP,
867 GNUNET_SCHEDULER_NO_TASK,
869 &hello_wait_timeout, hwl);
872 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_key (handle->my_hello, &pk));
873 GNUNET_CRYPTO_hash (&pk,
874 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
878 GNUNET_TIME_UNIT_ZERO,
879 &me, (const struct GNUNET_MessageHeader *) handle->my_hello);
884 send_hello (void *cls, size_t size, void *buf)
886 struct GNUNET_MessageHeader *hello = cls;
892 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
893 "Timeout while trying to transmit `%s' request.\n",
900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
901 "Transmitting `%s' request.\n", "HELLO");
903 msize = ntohs (hello->size);
904 GNUNET_assert (size >= msize);
905 memcpy (buf, hello, msize);
912 * Offer the transport service the HELLO of another peer. Note that
913 * the transport service may just ignore this message if the HELLO is
914 * malformed or useless due to our local configuration.
916 * @param handle connection to transport service
917 * @param hello the hello message
920 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
921 const struct GNUNET_MessageHeader *hello)
923 struct GNUNET_MessageHeader *hc;
926 if (handle->client == NULL)
929 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
930 "Not connected to transport service, dropping offered HELLO\n");
934 GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
935 size = ntohs (hello->size);
936 GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
937 hc = GNUNET_malloc (size);
938 memcpy (hc, hello, size);
939 schedule_control_transmit (handle,
941 GNUNET_NO, OFFER_HELLO_TIMEOUT, &send_hello, hc);
946 * Function we use for handling incoming messages.
948 static void demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg);
952 send_start (void *cls, size_t size, void *buf)
954 struct GNUNET_MessageHeader *s = buf;
959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
960 "Timeout while trying to transmit `%s' request.\n",
966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
967 "Transmitting `%s' request.\n", "START");
969 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
970 s->size = htons (sizeof (struct GNUNET_MessageHeader));
971 s->type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
972 return sizeof (struct GNUNET_MessageHeader);
977 * We're ready to transmit the request that the transport service
978 * should connect to a new peer. In addition to sending the
979 * request, schedule the next phase for the transmission processing
980 * that caused the connect request in the first place.
983 request_connect (void *cls, size_t size, void *buf)
985 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
986 struct TryConnectMessage *tcm;
987 struct GNUNET_TRANSPORT_Handle *h;
989 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
995 "Failed to transmit `%s' request for `%4s' to service.\n",
997 GNUNET_i2s(&th->target));
999 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1001 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
1002 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1004 th->notify (th->notify_cls, 0, NULL);
1009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1010 "Transmitting `%s' message for `%4s'.\n",
1011 "TRY_CONNECT", GNUNET_i2s (&th->target));
1013 GNUNET_assert (size >= sizeof (struct TryConnectMessage));
1015 tcm->header.size = htons (sizeof (struct TryConnectMessage));
1016 tcm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TRY_CONNECT);
1017 tcm->reserved = htonl (0);
1018 memcpy (&tcm->peer, &th->target, sizeof (struct GNUNET_PeerIdentity));
1019 th->notify_delay_task
1020 = GNUNET_SCHEDULER_add_delayed (h->sched,
1022 GNUNET_SCHEDULER_PRIORITY_KEEP,
1023 GNUNET_SCHEDULER_NO_TASK,
1024 GNUNET_TIME_absolute_get_remaining
1026 &peer_transmit_timeout, th);
1027 insert_transmit_handle (&h->connect_wait_head, th);
1028 return sizeof (struct TryConnectMessage);
1033 * Schedule a request to connect to the given
1034 * neighbour (and if successful, add the specified
1035 * handle to the wait list).
1037 * @param th handle for a request to transmit once we
1041 try_connect (struct GNUNET_TRANSPORT_TransmitHandle *th)
1043 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
1044 schedule_control_transmit (th->handle,
1045 sizeof (struct TryConnectMessage),
1047 GNUNET_TIME_absolute_get_remaining (th->timeout),
1048 &request_connect, th);
1053 * Task for delayed attempts to reconnect to a peer.
1055 * @param cls must be a transmit handle that determines the peer
1056 * to which we will try to connect
1057 * @param tc scheduler information about why we were triggered (not used)
1060 try_connect_task (void *cls,
1061 const struct GNUNET_SCHEDULER_TaskContext *tc)
1063 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
1065 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1071 * Remove neighbour from our list. Will automatically
1072 * trigger a re-connect attempt if we have messages pending
1075 * @param h our state
1076 * @param peer the peer to remove
1079 remove_neighbour (struct GNUNET_TRANSPORT_Handle *h,
1080 const struct GNUNET_PeerIdentity *peer)
1082 struct NeighbourList *prev;
1083 struct NeighbourList *pos;
1084 struct GNUNET_TRANSPORT_TransmitHandle *th;
1087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1088 "Removing neighbour `%s' from list of connected peers.\n",
1092 pos = h->neighbours;
1093 while ((pos != NULL) &&
1096 sizeof (struct GNUNET_PeerIdentity))))
1107 h->neighbours = pos->next;
1109 prev->next = pos->next;
1110 if (NULL != (th = pos->transmit_handle))
1112 pos->transmit_handle = NULL;
1113 th->neighbour = NULL;
1114 remove_from_any_list (th);
1115 if (GNUNET_TIME_absolute_get_remaining (th->timeout).value <= CONNECT_RETRY_TIMEOUT.value)
1118 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == th->notify_delay_task);
1119 peer_transmit_timeout (th, NULL);
1123 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1124 _("Connection with `%4s' failed, will keep trying for %llu ms to deliver message\n"),
1126 GNUNET_TIME_absolute_get_remaining (th->timeout).value);
1127 /* try again in a bit */
1128 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == th->notify_delay_task);
1129 th->notify_delay_task
1130 = GNUNET_SCHEDULER_add_delayed (h->sched,
1132 GNUNET_SCHEDULER_PRIORITY_KEEP,
1133 GNUNET_SCHEDULER_NO_TASK,
1134 CONNECT_RETRY_TIMEOUT,
1139 if (h->nc_cb != NULL)
1140 h->nd_cb (h->cls, peer);
1146 * Try again to connect to transport service.
1149 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1151 struct GNUNET_TRANSPORT_Handle *h = cls;
1152 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1153 struct NeighbourList *n;
1155 /* Forget about all neighbours that we used to be connected
1157 while (NULL != (n = h->neighbours))
1158 remove_neighbour (h, &n->id);
1160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n");
1162 GNUNET_assert (h->client == NULL);
1163 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1164 h->client = GNUNET_CLIENT_connect (h->sched, "transport", h->cfg);
1165 GNUNET_assert (h->client != NULL);
1166 /* make sure we don't send "START" twice,
1167 remove existing entry from queue (if present) */
1168 pos = h->connect_ready_head;
1171 if (pos->notify == &send_start)
1173 if (pos->prev == NULL)
1174 h->connect_ready_head = pos->next;
1176 pos->prev->next = pos->next;
1177 if (pos->next != NULL)
1178 pos->next->prev = pos->prev;
1179 GNUNET_assert (pos->neighbour == NULL);
1180 if (GNUNET_SCHEDULER_NO_TASK != pos->notify_delay_task)
1182 GNUNET_SCHEDULER_cancel (h->sched,
1183 pos->notify_delay_task);
1184 pos->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1191 schedule_control_transmit (h,
1192 sizeof (struct GNUNET_MessageHeader),
1194 GNUNET_TIME_UNIT_FOREVER_REL,
1196 GNUNET_CLIENT_receive (h->client,
1197 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1202 * Function that will schedule the job that will try
1203 * to connect us again to the client.
1206 schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
1209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1210 "Scheduling task to reconnect to transport service in %llu ms.\n",
1211 h->reconnect_delay.value);
1213 GNUNET_assert (h->client == NULL);
1214 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
1216 = GNUNET_SCHEDULER_add_delayed (h->sched,
1218 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1219 GNUNET_SCHEDULER_NO_TASK,
1220 h->reconnect_delay, &reconnect, h);
1221 h->reconnect_delay = GNUNET_TIME_UNIT_SECONDS;
1226 * We are connected to the respective peer, check the
1227 * bandwidth limits and schedule the transmission.
1229 static void schedule_request (struct GNUNET_TRANSPORT_TransmitHandle *th);
1233 * Function called by the scheduler when the timeout
1234 * for bandwidth availablility for the target
1235 * neighbour is reached.
1238 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1240 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
1242 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1243 schedule_request (th);
1248 * Remove the given transmit handle from the wait list. Does NOT free
1252 remove_from_wait_list (struct GNUNET_TRANSPORT_TransmitHandle *th)
1254 if (th->prev == NULL)
1255 th->handle->connect_wait_head = th->next;
1257 th->prev->next = th->next;
1258 if (th->next != NULL)
1259 th->next->prev = th->prev;
1264 * We are connected to the respective peer, check the
1265 * bandwidth limits and schedule the transmission.
1268 schedule_request (struct GNUNET_TRANSPORT_TransmitHandle *th)
1270 struct GNUNET_TRANSPORT_Handle *h;
1271 struct GNUNET_TIME_Relative duration;
1272 struct NeighbourList *n;
1277 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1279 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
1280 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1282 /* check outgoing quota */
1283 duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
1284 if (duration.value > MIN_QUOTA_REFRESH_TIME)
1287 duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
1289 available = duration.value * n->quota_out;
1290 if (available < n->last_sent + th->notify_size)
1292 /* calculate how much bandwidth we'd still need to
1293 accumulate and based on that how long we'll have
1295 available = n->last_sent + th->notify_size - available;
1296 duration = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1297 available / n->quota_out);
1298 if (th->timeout.value <
1299 GNUNET_TIME_relative_to_absolute (duration).value)
1301 /* signal timeout! */
1303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1304 "Would need %llu ms before bandwidth is available for delivery to `%4s', that is too long. Signaling timeout.\n",
1306 GNUNET_i2s(&th->target));
1308 remove_from_wait_list (th);
1309 th->notify (th->notify_cls, 0, NULL);
1314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1315 "Need more bandwidth, delaying delivery to `%4s' by %llu ms\n",
1316 GNUNET_i2s(&th->target),
1319 th->notify_delay_task
1320 = GNUNET_SCHEDULER_add_delayed (h->sched,
1322 GNUNET_SCHEDULER_PRIORITY_KEEP,
1323 GNUNET_SCHEDULER_NO_TASK,
1324 duration, &transmit_ready, th);
1328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1329 "Bandwidth available for transmission to `%4s'\n",
1330 GNUNET_i2s (&n->id));
1332 if (GNUNET_NO == n->transmit_ok)
1334 /* we may be ready, but transport service is not;
1335 wait for SendOkMessage or timeout */
1337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1338 "Need to wait for transport service `%s' message\n",
1341 th->notify_delay_task
1342 = GNUNET_SCHEDULER_add_delayed (h->sched,
1344 GNUNET_SCHEDULER_PRIORITY_KEEP,
1345 GNUNET_SCHEDULER_NO_TASK,
1346 GNUNET_TIME_absolute_get_remaining
1347 (th->timeout), &peer_transmit_timeout, th);
1350 n->transmit_ok = GNUNET_NO;
1351 remove_from_wait_list (th);
1353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Moving message for `%4s' to ready list\n",
1354 GNUNET_i2s(&n->id));
1356 insert_transmit_handle (&h->connect_ready_head, th);
1357 if (GNUNET_NO == h->transmission_scheduled)
1358 schedule_transmission (h);
1363 * Add neighbour to our list
1366 add_neighbour (struct GNUNET_TRANSPORT_Handle *h,
1368 struct GNUNET_TIME_Relative latency,
1369 const struct GNUNET_PeerIdentity *pid)
1371 struct NeighbourList *n;
1372 struct GNUNET_TRANSPORT_TransmitHandle *prev;
1373 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1374 struct GNUNET_TRANSPORT_TransmitHandle *next;
1376 /* check for duplicates */
1377 if (NULL != find_neighbour (h, pid))
1383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1384 "Creating entry for new neighbour `%4s'.\n", GNUNET_i2s (pid));
1386 n = GNUNET_malloc (sizeof (struct NeighbourList));
1388 n->last_quota_update = GNUNET_TIME_absolute_get ();
1389 n->quota_out = quota_out;
1390 n->next = h->neighbours;
1391 n->transmit_ok = GNUNET_YES;
1393 if (h->nc_cb != NULL)
1394 h->nc_cb (h->cls, &n->id, latency);
1396 pos = h->connect_wait_head;
1400 if (0 == memcmp (pid,
1401 &pos->target, sizeof (struct GNUNET_PeerIdentity)))
1404 GNUNET_assert (NULL == n->transmit_handle);
1405 n->transmit_handle = pos;
1407 h->connect_wait_head = next;
1410 if (GNUNET_YES == n->received_ack)
1413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1414 "Found pending request for `%4s' will trigger it now.\n",
1415 GNUNET_i2s (&pos->target));
1417 if (pos->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1419 GNUNET_SCHEDULER_cancel (h->sched, pos->notify_delay_task);
1420 pos->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1422 schedule_request (pos);
1427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1428 "Found pending request for `%4s' but still need `%s' before proceeding.\n",
1429 GNUNET_i2s (&pos->target),
1442 * Connect to the transport service. Note that the connection may
1443 * complete (or fail) asynchronously.
1446 * @param sched scheduler to use
1447 * @param cfg configuration to use
1448 * @param cls closure for the callbacks
1449 * @param rec receive function to call
1450 * @param nc function to call on connect events
1451 * @param nd function to call on disconnect events
1453 struct GNUNET_TRANSPORT_Handle *
1454 GNUNET_TRANSPORT_connect (struct GNUNET_SCHEDULER_Handle *sched,
1455 const struct GNUNET_CONFIGURATION_Handle *cfg,
1457 GNUNET_TRANSPORT_ReceiveCallback rec,
1458 GNUNET_TRANSPORT_NotifyConnect nc,
1459 GNUNET_TRANSPORT_NotifyDisconnect nd)
1461 struct GNUNET_TRANSPORT_Handle *ret;
1463 GNUNET_ARM_start_services (cfg, sched, "peerinfo", "transport", NULL);
1464 ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Handle));
1471 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1472 schedule_reconnect (ret);
1478 * Disconnect from the transport service.
1481 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1483 struct GNUNET_TRANSPORT_TransmitHandle *th;
1484 struct NeighbourList *n;
1485 struct HelloWaitList *hwl;
1486 struct GNUNET_CLIENT_Connection *client;
1489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1490 "Transport disconnect called!\n");
1492 while (NULL != (th = handle->connect_ready_head))
1494 handle->connect_ready_head = th->next;
1495 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1497 GNUNET_SCHEDULER_cancel (handle->sched, th->notify_delay_task);
1498 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1500 th->notify (th->notify_cls, 0, NULL);
1503 while (NULL != (th = handle->connect_wait_head))
1505 handle->connect_wait_head = th->next;
1506 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1508 GNUNET_SCHEDULER_cancel (handle->sched, th->notify_delay_task);
1509 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1511 th->notify (th->notify_cls, 0, NULL);
1514 while (NULL != (n = handle->neighbours))
1516 handle->neighbours = n->next;
1519 while (NULL != (hwl = handle->hwl_head))
1521 handle->hwl_head = hwl->next;
1522 GNUNET_SCHEDULER_cancel (handle->sched, hwl->task);
1523 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1524 _("Disconnect while trying to obtain `%s' from transport service.\n"),
1526 if (hwl->rec != NULL)
1527 hwl->rec (hwl->rec_cls, GNUNET_TIME_UNIT_ZERO, NULL, NULL);
1530 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1532 GNUNET_SCHEDULER_cancel (handle->sched, handle->reconnect_task);
1533 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1535 GNUNET_free_non_null (handle->my_hello);
1536 handle->my_hello = NULL;
1537 GNUNET_ARM_stop_services (handle->cfg, handle->sched, "transport", "peerinfo", NULL);
1538 if (NULL != (client = handle->client))
1541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1542 "Disconnecting from transport service for good.\n");
1544 handle->client = NULL;
1545 GNUNET_CLIENT_disconnect (client);
1547 GNUNET_free (handle);
1552 * Type of a function to call when we receive a message
1555 * @param cls closure
1556 * @param msg message received, NULL on timeout or fatal error
1559 demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
1561 struct GNUNET_TRANSPORT_Handle *h = cls;
1562 const struct DisconnectInfoMessage *dim;
1563 const struct ConnectInfoMessage *cim;
1564 const struct InboundMessage *im;
1565 const struct GNUNET_MessageHeader *imm;
1566 const struct SendOkMessage *okm;
1567 struct HelloWaitList *hwl;
1568 struct NeighbourList *n;
1569 struct GNUNET_PeerIdentity me;
1570 struct GNUNET_TRANSPORT_TransmitHandle *th;
1573 if ((msg == NULL) || (h->client == NULL))
1575 if (h->client != NULL)
1578 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1579 "Error receiving from transport service, disconnecting temporarily.\n");
1581 if (h->network_handle != NULL)
1583 GNUNET_CLIENT_notify_transmit_ready_cancel (h->network_handle);
1584 h->network_handle = NULL;
1585 h->transmission_scheduled = GNUNET_NO;
1586 th = h->connect_ready_head;
1587 /* add timeout again, we cancelled the transmit_ready task! */
1588 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
1589 th->notify_delay_task
1590 = GNUNET_SCHEDULER_add_delayed (h->sched,
1592 GNUNET_SCHEDULER_PRIORITY_KEEP,
1593 GNUNET_SCHEDULER_NO_TASK,
1594 GNUNET_TIME_absolute_get_remaining(th->timeout),
1595 &peer_transmit_timeout,
1598 GNUNET_CLIENT_disconnect (h->client);
1600 schedule_reconnect (h);
1604 /* shutdown initiated from 'GNUNET_TRANSPORT_disconnect',
1605 finish clean up work! */
1610 GNUNET_CLIENT_receive (h->client,
1611 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1612 size = ntohs (msg->size);
1613 switch (ntohs (msg->type))
1615 case GNUNET_MESSAGE_TYPE_HELLO:
1617 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg,
1624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1625 "Receiving (my own) `%s' message, I am `%4s'.\n",
1626 "HELLO", GNUNET_i2s (&me));
1628 GNUNET_free_non_null (h->my_hello);
1630 if (size < sizeof (struct GNUNET_MessageHeader))
1635 h->my_hello = GNUNET_malloc (size);
1636 memcpy (h->my_hello, msg, size);
1637 while (NULL != (hwl = h->hwl_head))
1639 h->hwl_head = hwl->next;
1640 GNUNET_SCHEDULER_cancel (h->sched, hwl->task);
1641 GNUNET_TRANSPORT_get_hello (h,
1642 GNUNET_TIME_UNIT_ZERO,
1643 hwl->rec, hwl->rec_cls);
1647 case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
1648 if (size != sizeof (struct ConnectInfoMessage))
1653 cim = (const struct ConnectInfoMessage *) msg;
1655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1656 "Receiving `%s' message for `%4s'.\n",
1657 "CONNECT", GNUNET_i2s (&cim->id));
1660 ntohl (cim->quota_out),
1661 GNUNET_TIME_relative_ntoh (cim->latency), &cim->id);
1663 case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
1664 if (size != sizeof (struct DisconnectInfoMessage))
1669 dim = (const struct DisconnectInfoMessage *) msg;
1671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1672 "Receiving `%s' message for `%4s'.\n",
1673 "DISCONNECT", GNUNET_i2s (&dim->peer));
1675 remove_neighbour (h, &dim->peer);
1677 case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
1678 if (size != sizeof (struct SendOkMessage))
1683 okm = (const struct SendOkMessage *) msg;
1685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1686 "Receiving `%s' message, transmission %s.\n", "SEND_OK",
1687 ntohl(okm->success) == GNUNET_OK ? "succeeded" : "failed");
1689 n = find_neighbour (h, &okm->peer);
1690 GNUNET_assert (n != NULL);
1691 n->transmit_ok = GNUNET_YES;
1692 if (n->transmit_handle != NULL)
1695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1696 "Processing pending message for `%4s'\n",
1697 GNUNET_i2s(&n->id));
1699 GNUNET_SCHEDULER_cancel (h->sched,
1700 n->transmit_handle->notify_delay_task);
1701 n->transmit_handle->notify_delay_task =
1702 GNUNET_SCHEDULER_NO_TASK;
1703 GNUNET_assert (GNUNET_YES == n->received_ack);
1704 schedule_request (n->transmit_handle);
1707 case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
1709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1710 "Receiving `%s' message.\n", "RECV");
1713 sizeof (struct InboundMessage) +
1714 sizeof (struct GNUNET_MessageHeader))
1719 im = (const struct InboundMessage *) msg;
1720 imm = (const struct GNUNET_MessageHeader *) &im[1];
1721 if (ntohs (imm->size) + sizeof (struct InboundMessage) != size)
1726 switch (ntohs (imm->type))
1728 case GNUNET_MESSAGE_TYPE_TRANSPORT_ACK:
1730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1731 "Receiving `%s' message from `%4s'.\n",
1732 "ACK", GNUNET_i2s (&im->peer));
1734 n = find_neighbour (h, &im->peer);
1740 if (n->received_ack == GNUNET_NO)
1742 n->received_ack = GNUNET_YES;
1743 if (NULL != n->transmit_handle)
1746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1747 "Peer connected, scheduling delayed message for deliverery now.\n");
1749 schedule_request (n->transmit_handle);
1755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1756 "Received message of type %u from `%4s'.\n",
1757 ntohs (imm->type), GNUNET_i2s (&im->peer));
1761 GNUNET_TIME_relative_ntoh (im->latency), &im->peer, imm);
1766 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1768 ("Received unexpected message of type %u in %s:%u\n"),
1769 ntohs (msg->type), __FILE__, __LINE__);
1776 struct ClientTransmitWrapper
1778 GNUNET_CONNECTION_TransmitReadyNotify notify;
1780 struct GNUNET_TRANSPORT_TransmitHandle *th;
1785 * Transmit message of a client destined for another
1786 * peer to the service.
1789 client_notify_wrapper (void *cls, size_t size, void *buf)
1791 struct ClientTransmitWrapper *ctw = cls;
1792 struct OutboundMessage *obm;
1793 struct GNUNET_MessageHeader *hdr;
1799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1800 "Transmission request could not be satisfied.\n");
1802 ret = ctw->notify (ctw->notify_cls, 0, NULL);
1803 GNUNET_assert (ret == 0);
1807 GNUNET_assert (size >= sizeof (struct OutboundMessage));
1809 ret = ctw->notify (ctw->notify_cls,
1810 size - sizeof (struct OutboundMessage),
1814 /* Need to reset flag, no SEND means no SEND_OK! */
1815 ctw->th->neighbour->transmit_ok = GNUNET_YES;
1819 GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
1820 hdr = (struct GNUNET_MessageHeader *) &obm[1];
1821 GNUNET_assert (ntohs (hdr->size) == ret);
1822 GNUNET_assert (ret + sizeof (struct OutboundMessage) <
1823 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1826 "Transmitting `%s' message with data for `%4s'\n",
1827 "SEND", GNUNET_i2s (&ctw->th->target));
1829 ret += sizeof (struct OutboundMessage);
1830 obm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
1831 obm->header.size = htons (ret);
1832 obm->priority = htonl (ctw->th->priority);
1833 obm->peer = ctw->th->target;
1841 * Check if we could queue a message of the given size for
1842 * transmission. The transport service will take both its
1843 * internal buffers and bandwidth limits imposed by the
1844 * other peer into consideration when answering this query.
1846 * @param handle connection to transport service
1847 * @param target who should receive the message
1848 * @param size how big is the message we want to transmit?
1849 * @param priority how important is the message?
1850 * @param timeout after how long should we give up (and call
1851 * notify with buf NULL and size 0)?
1852 * @param notify function to call when we are ready to
1853 * send such a message
1854 * @param notify_cls closure for notify
1855 * @return NULL if someone else is already waiting to be notified
1856 * non-NULL if the notify callback was queued (can be used to cancel
1857 * using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1859 struct GNUNET_TRANSPORT_TransmitHandle *
1860 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle
1862 const struct GNUNET_PeerIdentity
1863 *target, size_t size,
1864 unsigned int priority,
1865 struct GNUNET_TIME_Relative timeout,
1866 GNUNET_CONNECTION_TransmitReadyNotify
1867 notify, void *notify_cls)
1869 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1870 struct GNUNET_TRANSPORT_TransmitHandle *th;
1871 struct NeighbourList *n;
1872 struct ClientTransmitWrapper *ctw;
1874 if (size + sizeof (struct OutboundMessage) >=
1875 GNUNET_SERVER_MAX_MESSAGE_SIZE)
1881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1882 "Asking transport service for transmission of %u bytes to peer `%4s'.\n",
1883 size, GNUNET_i2s (target));
1885 n = find_neighbour (handle, target);
1887 (n->transmit_handle != NULL) )
1888 return NULL; /* already have a request pending for this peer! */
1889 ctw = GNUNET_malloc (sizeof (struct ClientTransmitWrapper));
1890 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
1891 ctw->notify = notify;
1892 ctw->notify_cls = notify_cls;
1894 th->handle = handle;
1896 th->target = *target;
1897 th->notify = &client_notify_wrapper;
1898 th->notify_cls = ctw;
1899 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1900 th->notify_size = size + sizeof (struct OutboundMessage);
1901 th->priority = priority;
1904 pos = handle->connect_wait_head;
1907 GNUNET_assert (0 != memcmp (target,
1909 sizeof (struct GNUNET_PeerIdentity)));
1913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1914 "Will now try to connect to `%4s'.\n", GNUNET_i2s (target));
1921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1922 "Transmission request queued for transmission to transport service.\n");
1924 GNUNET_assert (NULL == n->transmit_handle);
1925 n->transmit_handle = th;
1926 if (GNUNET_YES != n->received_ack)
1929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1930 "Connection to `%4s' is not yet confirmed connected, scheduling timeout (%llu ms) only.\n",
1931 GNUNET_i2s (target), timeout.value);
1933 th->notify_delay_task
1934 = GNUNET_SCHEDULER_add_delayed (handle->sched,
1936 GNUNET_SCHEDULER_PRIORITY_KEEP,
1937 GNUNET_SCHEDULER_NO_TASK,
1938 timeout, &peer_transmit_timeout, th);
1943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1944 "Peer `%4s' is ready to receive, scheduling message for delivery now.\n",
1945 GNUNET_i2s (target));
1947 schedule_request (th);
1953 * Cancel the specified transmission-ready notification.
1956 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
1957 GNUNET_TRANSPORT_TransmitHandle
1960 struct GNUNET_TRANSPORT_Handle *h;
1963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1964 "Transmission request of %u bytes to `%4s' was cancelled.\n",
1965 th->notify_size - sizeof(struct OutboundMessage),
1966 GNUNET_i2s (&th->target));
1968 GNUNET_assert (th->notify == &client_notify_wrapper);
1969 remove_from_any_list (th);
1971 if ((h->connect_ready_head == NULL) && (h->network_handle != NULL))
1973 GNUNET_CLIENT_notify_transmit_ready_cancel (h->network_handle);
1974 h->network_handle = NULL;
1975 h->transmission_scheduled = GNUNET_NO;
1977 GNUNET_free (th->notify_cls);
1978 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
1983 /* end of transport_api.c */