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_CONNECTION_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->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
385 th = h->connect_ready_head;
386 if (th->next != NULL)
387 th->next->prev = NULL;
388 h->connect_ready_head = th->next;
389 if (NULL != (n = th->neighbour))
391 GNUNET_assert (n->transmit_handle == th);
392 n->transmit_handle = NULL;
394 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != th->notify_delay_task);
395 GNUNET_SCHEDULER_cancel (h->sched,
396 th->notify_delay_task);
397 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
399 if (h->connect_ready_head != NULL) schedule_transmission (h); /* FIXME: is this ok? */
403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
404 "Ready to transmit %u bytes to transport service\n", size);
408 h->network_handle = NULL;
409 h->transmission_scheduled = GNUNET_NO;
412 th = h->connect_ready_head;
413 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
415 /* remove existing time out task (only applies if
416 this is not the first iteration of the loop) */
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
480 &transport_notify_ready,
482 GNUNET_assert (NULL != h->network_handle);
487 * Insert the given transmit handle in the given sorted
488 * doubly linked list based on timeout.
490 * @param head pointer to the head of the linked list
491 * @param th element to insert into the list
494 insert_transmit_handle (struct GNUNET_TRANSPORT_TransmitHandle **head,
495 struct GNUNET_TRANSPORT_TransmitHandle *th)
497 struct GNUNET_TRANSPORT_TransmitHandle *pos;
498 struct GNUNET_TRANSPORT_TransmitHandle *prev;
502 while ((pos != NULL) && (pos->timeout.value < th->timeout.value))
510 if (th->next != NULL)
526 * Cancel a pending notify delay task (if pending) and also remove the
527 * given transmit handle from whatever list is on.
529 * @param th handle for the transmission request to manipulate
532 remove_from_any_list (struct GNUNET_TRANSPORT_TransmitHandle *th)
534 struct GNUNET_TRANSPORT_Handle *h;
537 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
539 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
540 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
542 if (th->prev == NULL)
544 if (th == h->connect_wait_head)
545 h->connect_wait_head = th->next;
547 h->connect_ready_head = th->next;
551 th->prev->next = th->next;
553 if (th->next != NULL)
554 th->next->prev = th->prev;
559 * Schedule a request to connect to the given
560 * neighbour (and if successful, add the specified
561 * handle to the wait list).
563 * @param th handle for a request to transmit once we
567 try_connect (struct GNUNET_TRANSPORT_TransmitHandle *th);
571 * Called when our transmit request timed out before any transport
572 * reported success connecting to the desired peer or before the
573 * transport was ready to receive. Signal error and free
577 transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
579 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
581 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
582 if (th->neighbour != NULL)
583 th->neighbour->transmit_handle = NULL;
585 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
586 "Request for transmission to peer `%s' timed out.\n",
587 GNUNET_i2s(&th->target));
589 remove_from_any_list (th);
590 th->notify (th->notify_cls, 0, NULL);
598 * Queue control request for transmission to the transport
601 * @param size number of bytes to be transmitted
602 * @param at_head request must be added to the head of the queue
603 * (otherwise request will be appended)
604 * @param timeout how long this transmission can wait (at most)
605 * @param notify function to call to get the content
606 * @param notify_cls closure for notify
609 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h,
612 struct GNUNET_TIME_Relative timeout,
613 GNUNET_CONNECTION_TransmitReadyNotify notify,
616 struct GNUNET_TRANSPORT_TransmitHandle *th;
618 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
621 th->notify_cls = notify_cls;
622 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
623 th->notify_size = size;
624 th->notify_delay_task
625 = GNUNET_SCHEDULER_add_delayed (h->sched,
627 GNUNET_SCHEDULER_PRIORITY_KEEP,
628 GNUNET_SCHEDULER_NO_TASK,
630 &transmit_timeout, th);
633 th->next = h->connect_ready_head;
634 h->connect_ready_head = th;
635 if (th->next != NULL)
640 insert_transmit_handle (&h->connect_ready_head, th);
642 if (GNUNET_NO == h->transmission_scheduled)
643 schedule_transmission (h);
648 * Update the quota values for the given neighbour now.
651 update_quota (struct NeighbourList *n)
653 struct GNUNET_TIME_Relative delta;
657 delta = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
658 allowed = delta.value * n->quota_out;
659 if (n->last_sent < allowed)
661 remaining = allowed - n->last_sent;
662 if (n->quota_out > 0)
663 remaining /= n->quota_out;
666 if (remaining > MAX_BANDWIDTH_CARRY)
667 remaining = MAX_BANDWIDTH_CARRY;
669 n->last_quota_update = GNUNET_TIME_absolute_get ();
670 n->last_quota_update.value -= remaining;
674 n->last_sent -= allowed;
675 n->last_quota_update = GNUNET_TIME_absolute_get ();
680 struct SetQuotaContext
682 struct GNUNET_TRANSPORT_Handle *handle;
684 struct GNUNET_PeerIdentity target;
686 GNUNET_SCHEDULER_Task cont;
690 struct GNUNET_TIME_Absolute timeout;
697 send_set_quota (void *cls, size_t size, void *buf)
699 struct SetQuotaContext *sqc = cls;
700 struct QuotaSetMessage *msg;
704 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
708 GNUNET_SCHEDULER_REASON_TIMEOUT);
713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
714 "Transmitting `%s' request with respect to `%4s'.\n",
715 "SET_QUOTA", GNUNET_i2s (&sqc->target));
717 GNUNET_assert (size >= sizeof (struct QuotaSetMessage));
719 msg->header.size = htons (sizeof (struct QuotaSetMessage));
720 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
721 msg->quota_in = htonl (sqc->quota_in);
722 memcpy (&msg->peer, &sqc->target, sizeof (struct GNUNET_PeerIdentity));
723 if (sqc->cont != NULL)
724 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
728 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
730 return sizeof (struct QuotaSetMessage);
735 * Set the share of incoming bandwidth for the given
736 * peer to the specified amount.
738 * @param handle connection to transport service
739 * @param target who's bandwidth quota is being changed
740 * @param quota_in incoming bandwidth quota in bytes per ms; 0 can
741 * be used to force all traffic to be discarded
742 * @param quota_out outgoing bandwidth quota in bytes per ms; 0 can
743 * be used to force all traffic to be discarded
744 * @param timeout how long to wait until signaling failure if
745 * we can not communicate the quota change
746 * @param cont continuation to call when done, will be called
747 * either with reason "TIMEOUT" or with reason "PREREQ_DONE"
748 * @param cont_cls closure for continuation
751 GNUNET_TRANSPORT_set_quota (struct GNUNET_TRANSPORT_Handle *handle,
752 const struct GNUNET_PeerIdentity *target,
755 struct GNUNET_TIME_Relative timeout,
756 GNUNET_SCHEDULER_Task cont, void *cont_cls)
758 struct NeighbourList *n;
759 struct SetQuotaContext *sqc;
761 n = find_neighbour (handle, target);
765 if (n->quota_out < quota_out)
766 n->last_quota_update = GNUNET_TIME_absolute_get ();
767 n->quota_out = quota_out;
769 sqc = GNUNET_malloc (sizeof (struct SetQuotaContext));
770 sqc->handle = handle;
771 sqc->target = *target;
773 sqc->cont_cls = cont_cls;
774 sqc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
775 sqc->quota_in = quota_in;
776 schedule_control_transmit (handle,
777 sizeof (struct QuotaSetMessage),
778 GNUNET_NO, timeout, &send_set_quota, sqc);
783 * A "get_hello" request has timed out. Signal the client
787 hello_wait_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
789 struct HelloWaitList *hwl = cls;
790 struct HelloWaitList *pos;
791 struct HelloWaitList *prev;
793 hwl->task = GNUNET_SCHEDULER_NO_TASK;
794 if (GNUNET_TIME_absolute_get_remaining (hwl->timeout).value > 0)
797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
798 _("First attempt to obtain `%s' from transport service failed, will try again for %llums.\n"),
800 GNUNET_TIME_absolute_get_remaining (hwl->timeout).value);
802 hwl->task = GNUNET_SCHEDULER_add_delayed (hwl->handle->sched,
804 GNUNET_SCHEDULER_PRIORITY_KEEP,
805 GNUNET_SCHEDULER_NO_TASK,
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 GNUNET_SCHEDULER_PRIORITY_KEEP,
865 GNUNET_SCHEDULER_NO_TASK,
867 &hello_wait_timeout, hwl);
870 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_key (handle->my_hello, &pk));
871 GNUNET_CRYPTO_hash (&pk,
872 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
876 GNUNET_TIME_UNIT_ZERO,
877 &me, (const struct GNUNET_MessageHeader *) handle->my_hello);
882 send_hello (void *cls, size_t size, void *buf)
884 struct GNUNET_MessageHeader *hello = cls;
890 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
891 "Timeout while trying to transmit `%s' request.\n",
898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
899 "Transmitting `%s' request.\n", "HELLO");
901 msize = ntohs (hello->size);
902 GNUNET_assert (size >= msize);
903 memcpy (buf, hello, msize);
910 * Offer the transport service the HELLO of another peer. Note that
911 * the transport service may just ignore this message if the HELLO is
912 * malformed or useless due to our local configuration.
914 * @param handle connection to transport service
915 * @param hello the hello message
918 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
919 const struct GNUNET_MessageHeader *hello)
921 struct GNUNET_MessageHeader *hc;
924 if (handle->client == NULL)
927 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
928 "Not connected to transport service, dropping offered HELLO\n");
932 GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
933 size = ntohs (hello->size);
934 GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
935 hc = GNUNET_malloc (size);
936 memcpy (hc, hello, size);
937 schedule_control_transmit (handle,
939 GNUNET_NO, OFFER_HELLO_TIMEOUT, &send_hello, hc);
944 * Function we use for handling incoming messages.
946 static void demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg);
950 send_start (void *cls, size_t size, void *buf)
952 struct GNUNET_MessageHeader *s = buf;
957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
958 "Timeout while trying to transmit `%s' request.\n",
964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
965 "Transmitting `%s' request.\n", "START");
967 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
968 s->size = htons (sizeof (struct GNUNET_MessageHeader));
969 s->type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
970 return sizeof (struct GNUNET_MessageHeader);
975 * We're ready to transmit the request that the transport service
976 * should connect to a new peer. In addition to sending the
977 * request, schedule the next phase for the transmission processing
978 * that caused the connect request in the first place.
981 request_connect (void *cls, size_t size, void *buf)
983 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
984 struct TryConnectMessage *tcm;
985 struct GNUNET_TRANSPORT_Handle *h;
987 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
993 "Failed to transmit `%s' request for `%4s' to service.\n",
995 GNUNET_i2s(&th->target));
997 th->notify (th->notify_cls, 0, NULL);
998 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1000 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
1001 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1008 "Transmitting `%s' message for `%4s'.\n",
1009 "TRY_CONNECT", GNUNET_i2s (&th->target));
1011 GNUNET_assert (size >= sizeof (struct TryConnectMessage));
1013 tcm->header.size = htons (sizeof (struct TryConnectMessage));
1014 tcm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TRY_CONNECT);
1015 tcm->reserved = htonl (0);
1016 memcpy (&tcm->peer, &th->target, sizeof (struct GNUNET_PeerIdentity));
1017 th->notify_delay_task
1018 = GNUNET_SCHEDULER_add_delayed (h->sched,
1020 GNUNET_SCHEDULER_PRIORITY_KEEP,
1021 GNUNET_SCHEDULER_NO_TASK,
1022 GNUNET_TIME_absolute_get_remaining
1024 &transmit_timeout, th);
1025 insert_transmit_handle (&h->connect_wait_head, th);
1026 return sizeof (struct TryConnectMessage);
1031 * Schedule a request to connect to the given
1032 * neighbour (and if successful, add the specified
1033 * handle to the wait list).
1035 * @param th handle for a request to transmit once we
1039 try_connect (struct GNUNET_TRANSPORT_TransmitHandle *th)
1041 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
1042 schedule_control_transmit (th->handle,
1043 sizeof (struct TryConnectMessage),
1045 GNUNET_TIME_absolute_get_remaining (th->timeout),
1046 &request_connect, th);
1051 * Task for delayed attempts to reconnect to a peer.
1053 * @param cls must be a transmit handle that determines the peer
1054 * to which we will try to connect
1055 * @param tc scheduler information about why we were triggered (not used)
1058 try_connect_task (void *cls,
1059 const struct GNUNET_SCHEDULER_TaskContext *tc)
1061 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
1063 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1069 * Remove neighbour from our list. Will automatically
1070 * trigger a re-connect attempt if we have messages pending
1073 * @param h our state
1074 * @param peer the peer to remove
1077 remove_neighbour (struct GNUNET_TRANSPORT_Handle *h,
1078 const struct GNUNET_PeerIdentity *peer)
1080 struct NeighbourList *prev;
1081 struct NeighbourList *pos;
1082 struct GNUNET_TRANSPORT_TransmitHandle *th;
1085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1086 "Removing neighbour `%s' from list of connected peers.\n",
1090 pos = h->neighbours;
1091 while ((pos != NULL) &&
1094 sizeof (struct GNUNET_PeerIdentity))))
1105 h->neighbours = pos->next;
1107 prev->next = pos->next;
1108 if (NULL != (th = pos->transmit_handle))
1110 pos->transmit_handle = NULL;
1111 th->neighbour = NULL;
1112 remove_from_any_list (th);
1113 if (GNUNET_TIME_absolute_get_remaining (th->timeout).value <= CONNECT_RETRY_TIMEOUT.value)
1116 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == th->notify_delay_task);
1117 transmit_timeout (th, NULL);
1121 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1122 _("Connection with `%4s' failed, will keep trying for %llu ms to deliver message\n"),
1124 GNUNET_TIME_absolute_get_remaining (th->timeout).value);
1125 /* try again in a bit */
1126 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == th->notify_delay_task);
1127 th->notify_delay_task
1128 = GNUNET_SCHEDULER_add_delayed (h->sched,
1130 GNUNET_SCHEDULER_PRIORITY_KEEP,
1131 GNUNET_SCHEDULER_NO_TASK,
1132 CONNECT_RETRY_TIMEOUT,
1137 if (h->nc_cb != NULL)
1138 h->nd_cb (h->cls, peer);
1144 * Try again to connect to transport service.
1147 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1149 struct GNUNET_TRANSPORT_Handle *h = cls;
1150 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1151 struct NeighbourList *n;
1153 /* Forget about all neighbours that we used to be connected
1155 while (NULL != (n = h->neighbours))
1156 remove_neighbour (h, &n->id);
1158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n");
1160 GNUNET_assert (h->client == NULL);
1161 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1162 h->client = GNUNET_CLIENT_connect (h->sched, "transport", h->cfg);
1163 GNUNET_assert (h->client != NULL);
1164 /* make sure we don't send "START" twice,
1165 remove existing entry from queue (if present) */
1166 pos = h->connect_ready_head;
1169 if (pos->notify == &send_start)
1171 if (pos->prev == NULL)
1172 h->connect_ready_head = pos->next;
1174 pos->prev->next = pos->next;
1175 if (pos->next != NULL)
1176 pos->next->prev = pos->prev;
1177 GNUNET_assert (pos->neighbour == NULL);
1183 schedule_control_transmit (h,
1184 sizeof (struct GNUNET_MessageHeader),
1186 GNUNET_TIME_UNIT_FOREVER_REL,
1188 GNUNET_CLIENT_receive (h->client,
1189 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1194 * Function that will schedule the job that will try
1195 * to connect us again to the client.
1198 schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
1201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1202 "Scheduling task to reconnect to transport service in %llu ms.\n",
1203 h->reconnect_delay.value);
1205 GNUNET_assert (h->client == NULL);
1206 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
1208 = GNUNET_SCHEDULER_add_delayed (h->sched,
1210 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1211 GNUNET_SCHEDULER_NO_TASK,
1212 h->reconnect_delay, &reconnect, h);
1213 h->reconnect_delay = GNUNET_TIME_UNIT_SECONDS;
1218 * We are connected to the respective peer, check the
1219 * bandwidth limits and schedule the transmission.
1221 static void schedule_request (struct GNUNET_TRANSPORT_TransmitHandle *th);
1225 * Function called by the scheduler when the timeout
1226 * for bandwidth availablility for the target
1227 * neighbour is reached.
1230 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1232 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
1234 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1235 schedule_request (th);
1240 * Remove the given transmit handle from the wait list. Does NOT free
1244 remove_from_wait_list (struct GNUNET_TRANSPORT_TransmitHandle *th)
1246 if (th->prev == NULL)
1247 th->handle->connect_wait_head = th->next;
1249 th->prev->next = th->next;
1250 if (th->next != NULL)
1251 th->next->prev = th->prev;
1256 * We are connected to the respective peer, check the
1257 * bandwidth limits and schedule the transmission.
1260 schedule_request (struct GNUNET_TRANSPORT_TransmitHandle *th)
1262 struct GNUNET_TRANSPORT_Handle *h;
1263 struct GNUNET_TIME_Relative duration;
1264 struct NeighbourList *n;
1269 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1271 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
1272 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1274 /* check outgoing quota */
1275 duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
1276 if (duration.value > MIN_QUOTA_REFRESH_TIME)
1279 duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
1281 available = duration.value * n->quota_out;
1282 if (available < n->last_sent + th->notify_size)
1284 /* calculate how much bandwidth we'd still need to
1285 accumulate and based on that how long we'll have
1287 available = n->last_sent + th->notify_size - available;
1288 duration = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1289 available / n->quota_out);
1290 if (th->timeout.value <
1291 GNUNET_TIME_relative_to_absolute (duration).value)
1293 /* signal timeout! */
1295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1296 "Would need %llu ms before bandwidth is available for delivery to `%4s', that is too long. Signaling timeout.\n",
1298 GNUNET_i2s(&th->target));
1300 remove_from_wait_list (th);
1301 th->notify (th->notify_cls, 0, NULL);
1306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1307 "Need more bandwidth, delaying delivery to `%4s' by %llu ms\n",
1308 GNUNET_i2s(&th->target),
1311 th->notify_delay_task
1312 = GNUNET_SCHEDULER_add_delayed (h->sched,
1314 GNUNET_SCHEDULER_PRIORITY_KEEP,
1315 GNUNET_SCHEDULER_NO_TASK,
1316 duration, &transmit_ready, th);
1320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1321 "Bandwidth available for transmission to `%4s'\n",
1322 GNUNET_i2s (&n->id));
1324 if (GNUNET_NO == n->transmit_ok)
1326 /* we may be ready, but transport service is not;
1327 wait for SendOkMessage or timeout */
1329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1330 "Need to wait for transport service `%s' message\n",
1333 th->notify_delay_task
1334 = GNUNET_SCHEDULER_add_delayed (h->sched,
1336 GNUNET_SCHEDULER_PRIORITY_KEEP,
1337 GNUNET_SCHEDULER_NO_TASK,
1338 GNUNET_TIME_absolute_get_remaining
1339 (th->timeout), &transmit_timeout, th);
1342 n->transmit_ok = GNUNET_NO;
1343 remove_from_wait_list (th);
1345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Moving message for `%4s' to ready list\n",
1346 GNUNET_i2s(&n->id));
1348 insert_transmit_handle (&h->connect_ready_head, th);
1349 if (GNUNET_NO == h->transmission_scheduled)
1350 schedule_transmission (h);
1355 * Add neighbour to our list
1358 add_neighbour (struct GNUNET_TRANSPORT_Handle *h,
1360 struct GNUNET_TIME_Relative latency,
1361 const struct GNUNET_PeerIdentity *pid)
1363 struct NeighbourList *n;
1364 struct GNUNET_TRANSPORT_TransmitHandle *prev;
1365 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1366 struct GNUNET_TRANSPORT_TransmitHandle *next;
1368 /* check for duplicates */
1369 if (NULL != find_neighbour (h, pid))
1375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1376 "Creating entry for new neighbour `%4s'.\n", GNUNET_i2s (pid));
1378 n = GNUNET_malloc (sizeof (struct NeighbourList));
1380 n->last_quota_update = GNUNET_TIME_absolute_get ();
1381 n->quota_out = quota_out;
1382 n->next = h->neighbours;
1383 n->transmit_ok = GNUNET_YES;
1385 if (h->nc_cb != NULL)
1386 h->nc_cb (h->cls, &n->id, latency);
1388 pos = h->connect_wait_head;
1392 if (0 == memcmp (pid,
1393 &pos->target, sizeof (struct GNUNET_PeerIdentity)))
1396 GNUNET_assert (NULL == n->transmit_handle);
1397 n->transmit_handle = pos;
1399 h->connect_wait_head = next;
1402 if (GNUNET_YES == n->received_ack)
1405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1406 "Found pending request for `%4s' will trigger it now.\n",
1407 GNUNET_i2s (&pos->target));
1409 if (pos->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1411 GNUNET_SCHEDULER_cancel (h->sched, pos->notify_delay_task);
1412 pos->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1414 schedule_request (pos);
1419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1420 "Found pending request for `%4s' but still need `%s' before proceeding.\n",
1421 GNUNET_i2s (&pos->target),
1434 * Connect to the transport service. Note that the connection may
1435 * complete (or fail) asynchronously.
1438 * @param sched scheduler to use
1439 * @param cfg configuration to use
1440 * @param cls closure for the callbacks
1441 * @param rec receive function to call
1442 * @param nc function to call on connect events
1443 * @param dc function to call on disconnect events
1445 struct GNUNET_TRANSPORT_Handle *
1446 GNUNET_TRANSPORT_connect (struct GNUNET_SCHEDULER_Handle *sched,
1447 const struct GNUNET_CONFIGURATION_Handle *cfg,
1449 GNUNET_TRANSPORT_ReceiveCallback rec,
1450 GNUNET_TRANSPORT_NotifyConnect nc,
1451 GNUNET_TRANSPORT_NotifyDisconnect nd)
1453 struct GNUNET_TRANSPORT_Handle *ret;
1455 GNUNET_ARM_start_service ("peerinfo",
1456 cfg, sched, START_SERVICE_TIMEOUT, NULL, NULL);
1457 GNUNET_ARM_start_service ("transport",
1458 cfg, sched, START_SERVICE_TIMEOUT, NULL, NULL);
1459 ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Handle));
1466 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1467 schedule_reconnect (ret);
1473 * These stop activities must be run in a fresh
1474 * scheduler that is NOT in shutdown mode.
1477 stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1479 struct GNUNET_TRANSPORT_Handle *handle = cls;
1480 GNUNET_ARM_stop_service ("transport",
1482 tc->sched, STOP_SERVICE_TIMEOUT, NULL, NULL);
1483 GNUNET_ARM_stop_service ("peerinfo",
1485 tc->sched, STOP_SERVICE_TIMEOUT, NULL, NULL);
1490 * Disconnect from the transport service.
1493 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1495 struct GNUNET_TRANSPORT_TransmitHandle *th;
1496 struct NeighbourList *n;
1497 struct HelloWaitList *hwl;
1498 struct GNUNET_CLIENT_Connection *client;
1501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transport disconnect called!\n");
1503 while (NULL != (th = handle->connect_ready_head))
1505 handle->connect_ready_head = th->next;
1506 th->notify (th->notify_cls, 0, NULL);
1509 while (NULL != (th = handle->connect_wait_head))
1511 handle->connect_wait_head = th->next;
1512 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1514 GNUNET_SCHEDULER_cancel (handle->sched, th->notify_delay_task);
1515 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1517 th->notify (th->notify_cls, 0, NULL);
1520 while (NULL != (n = handle->neighbours))
1522 handle->neighbours = n->next;
1525 while (NULL != (hwl = handle->hwl_head))
1527 handle->hwl_head = hwl->next;
1528 GNUNET_SCHEDULER_cancel (handle->sched, hwl->task);
1529 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1530 _("Disconnect while trying to obtain `%s' from transport service.\n"),
1532 if (hwl->rec != NULL)
1533 hwl->rec (hwl->rec_cls, GNUNET_TIME_UNIT_ZERO, NULL, NULL);
1536 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1538 GNUNET_SCHEDULER_cancel (handle->sched, handle->reconnect_task);
1539 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1541 GNUNET_free_non_null (handle->my_hello);
1542 handle->my_hello = NULL;
1543 GNUNET_SCHEDULER_run (&stop_task, handle);
1544 if (NULL != (client = handle->client))
1547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1548 "Disconnecting from transport service for good.\n");
1550 handle->client = NULL;
1551 GNUNET_CLIENT_disconnect (client);
1554 GNUNET_free (handle);
1559 * Type of a function to call when we receive a message
1562 * @param cls closure
1563 * @param msg message received, NULL on timeout or fatal error
1566 demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
1568 struct GNUNET_TRANSPORT_Handle *h = cls;
1569 const struct DisconnectInfoMessage *dim;
1570 const struct ConnectInfoMessage *cim;
1571 const struct InboundMessage *im;
1572 const struct GNUNET_MessageHeader *imm;
1573 const struct SendOkMessage *okm;
1574 struct HelloWaitList *hwl;
1575 struct NeighbourList *n;
1576 struct GNUNET_PeerIdentity me;
1577 struct GNUNET_TRANSPORT_TransmitHandle *th;
1580 if ((msg == NULL) || (h->client == NULL))
1582 if (h->client != NULL)
1585 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1586 "Error receiving from transport service, disconnecting temporarily.\n");
1588 if (h->network_handle != NULL)
1590 GNUNET_CONNECTION_notify_transmit_ready_cancel (h->network_handle);
1591 h->network_handle = NULL;
1592 h->transmission_scheduled = GNUNET_NO;
1593 th = h->connect_ready_head;
1594 /* add timeout again, we cancelled the transmit_ready task! */
1595 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
1596 th->notify_delay_task
1597 = GNUNET_SCHEDULER_add_delayed (h->sched,
1599 GNUNET_SCHEDULER_PRIORITY_KEEP,
1600 GNUNET_SCHEDULER_NO_TASK,
1601 GNUNET_TIME_absolute_get_remaining(th->timeout),
1605 GNUNET_CLIENT_disconnect (h->client);
1607 schedule_reconnect (h);
1611 /* shutdown initiated from 'GNUNET_TRANSPORT_disconnect',
1612 finish clean up work! */
1617 GNUNET_CLIENT_receive (h->client,
1618 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1619 size = ntohs (msg->size);
1620 switch (ntohs (msg->type))
1622 case GNUNET_MESSAGE_TYPE_HELLO:
1624 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg,
1631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1632 "Receiving (my own) `%s' message, I am `%4s'.\n",
1633 "HELLO", GNUNET_i2s (&me));
1635 GNUNET_free_non_null (h->my_hello);
1637 if (size < sizeof (struct GNUNET_MessageHeader))
1642 h->my_hello = GNUNET_malloc (size);
1643 memcpy (h->my_hello, msg, size);
1644 while (NULL != (hwl = h->hwl_head))
1646 h->hwl_head = hwl->next;
1647 GNUNET_SCHEDULER_cancel (h->sched, hwl->task);
1648 GNUNET_TRANSPORT_get_hello (h,
1649 GNUNET_TIME_UNIT_ZERO,
1650 hwl->rec, hwl->rec_cls);
1654 case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
1655 if (size != sizeof (struct ConnectInfoMessage))
1660 cim = (const struct ConnectInfoMessage *) msg;
1662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1663 "Receiving `%s' message for `%4s'.\n",
1664 "CONNECT", GNUNET_i2s (&cim->id));
1667 ntohl (cim->quota_out),
1668 GNUNET_TIME_relative_ntoh (cim->latency), &cim->id);
1670 case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
1671 if (size != sizeof (struct DisconnectInfoMessage))
1676 dim = (const struct DisconnectInfoMessage *) msg;
1678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1679 "Receiving `%s' message for `%4s'.\n",
1680 "DISCONNECT", GNUNET_i2s (&dim->peer));
1682 remove_neighbour (h, &dim->peer);
1684 case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
1685 if (size != sizeof (struct SendOkMessage))
1690 okm = (const struct SendOkMessage *) msg;
1692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1693 "Receiving `%s' message, transmission %s.\n", "SEND_OK",
1694 ntohl(okm->success) == GNUNET_OK ? "succeeded" : "failed");
1696 n = find_neighbour (h, &okm->peer);
1697 GNUNET_assert (n != NULL);
1698 n->transmit_ok = GNUNET_YES;
1699 if (n->transmit_handle != NULL)
1702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1703 "Processing pending message for `%4s'\n",
1704 GNUNET_i2s(&n->id));
1706 GNUNET_SCHEDULER_cancel (h->sched,
1707 n->transmit_handle->notify_delay_task);
1708 n->transmit_handle->notify_delay_task =
1709 GNUNET_SCHEDULER_NO_TASK;
1710 GNUNET_assert (GNUNET_YES == n->received_ack);
1711 schedule_request (n->transmit_handle);
1714 case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
1716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1717 "Receiving `%s' message.\n", "RECV");
1720 sizeof (struct InboundMessage) +
1721 sizeof (struct GNUNET_MessageHeader))
1726 im = (const struct InboundMessage *) msg;
1727 imm = (const struct GNUNET_MessageHeader *) &im[1];
1728 if (ntohs (imm->size) + sizeof (struct InboundMessage) != size)
1733 switch (ntohs (imm->type))
1735 case GNUNET_MESSAGE_TYPE_TRANSPORT_ACK:
1737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1738 "Receiving `%s' message from `%4s'.\n",
1739 "ACK", GNUNET_i2s (&im->peer));
1741 n = find_neighbour (h, &im->peer);
1747 if (n->received_ack == GNUNET_NO)
1749 n->received_ack = GNUNET_YES;
1750 if (NULL != n->transmit_handle)
1753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1754 "Peer connected, scheduling delayed message for deliverery now.\n");
1756 schedule_request (n->transmit_handle);
1762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1763 "Received message of type %u from `%4s'.\n",
1764 ntohs (imm->type), GNUNET_i2s (&im->peer));
1768 GNUNET_TIME_relative_ntoh (im->latency), &im->peer, imm);
1773 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1775 ("Received unexpected message of type %u in %s:%u\n"),
1776 ntohs (msg->type), __FILE__, __LINE__);
1783 struct ClientTransmitWrapper
1785 GNUNET_CONNECTION_TransmitReadyNotify notify;
1787 struct GNUNET_TRANSPORT_TransmitHandle *th;
1792 * Transmit message of a client destined for another
1793 * peer to the service.
1796 client_notify_wrapper (void *cls, size_t size, void *buf)
1798 struct ClientTransmitWrapper *ctw = cls;
1799 struct OutboundMessage *obm;
1800 struct GNUNET_MessageHeader *hdr;
1806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1807 "Transmission request could not be satisfied.\n");
1809 ret = ctw->notify (ctw->notify_cls, 0, NULL);
1810 GNUNET_assert (ret == 0);
1814 GNUNET_assert (size >= sizeof (struct OutboundMessage));
1816 ret = ctw->notify (ctw->notify_cls,
1817 size - sizeof (struct OutboundMessage),
1821 /* Need to reset flag, no SEND means no SEND_OK! */
1822 ctw->th->neighbour->transmit_ok = GNUNET_YES;
1826 GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
1827 hdr = (struct GNUNET_MessageHeader *) &obm[1];
1828 GNUNET_assert (ntohs (hdr->size) == ret);
1829 GNUNET_assert (ret + sizeof (struct OutboundMessage) <
1830 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1833 "Transmitting `%s' message with data for `%4s'\n",
1834 "SEND", GNUNET_i2s (&ctw->th->target));
1836 ret += sizeof (struct OutboundMessage);
1837 obm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
1838 obm->header.size = htons (ret);
1839 obm->priority = htonl (ctw->th->priority);
1840 obm->peer = ctw->th->target;
1848 * Check if we could queue a message of the given size for
1849 * transmission. The transport service will take both its
1850 * internal buffers and bandwidth limits imposed by the
1851 * other peer into consideration when answering this query.
1853 * @param handle connection to transport service
1854 * @param target who should receive the message
1855 * @param size how big is the message we want to transmit?
1856 * @param priority how important is the message?
1857 * @param timeout after how long should we give up (and call
1858 * notify with buf NULL and size 0)?
1859 * @param notify function to call when we are ready to
1860 * send such a message
1861 * @param notify_cls closure for notify
1862 * @return NULL if someone else is already waiting to be notified
1863 * non-NULL if the notify callback was queued (can be used to cancel
1864 * using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1866 struct GNUNET_TRANSPORT_TransmitHandle *
1867 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle
1869 const struct GNUNET_PeerIdentity
1870 *target, size_t size,
1871 unsigned int priority,
1872 struct GNUNET_TIME_Relative timeout,
1873 GNUNET_CONNECTION_TransmitReadyNotify
1874 notify, void *notify_cls)
1876 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1877 struct GNUNET_TRANSPORT_TransmitHandle *th;
1878 struct NeighbourList *n;
1879 struct ClientTransmitWrapper *ctw;
1881 if (size + sizeof (struct OutboundMessage) >=
1882 GNUNET_SERVER_MAX_MESSAGE_SIZE)
1888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1889 "Asking transport service for transmission of %u bytes to peer `%4s'.\n",
1890 size, GNUNET_i2s (target));
1892 n = find_neighbour (handle, target);
1894 (n->transmit_handle != NULL) )
1895 return NULL; /* already have a request pending for this peer! */
1896 ctw = GNUNET_malloc (sizeof (struct ClientTransmitWrapper));
1897 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
1898 ctw->notify = notify;
1899 ctw->notify_cls = notify_cls;
1901 th->handle = handle;
1903 th->target = *target;
1904 th->notify = &client_notify_wrapper;
1905 th->notify_cls = ctw;
1906 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1907 th->notify_size = size + sizeof (struct OutboundMessage);
1908 th->priority = priority;
1911 pos = handle->connect_wait_head;
1914 GNUNET_assert (0 != memcmp (target,
1916 sizeof (struct GNUNET_PeerIdentity)));
1920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1921 "Will now try to connect to `%4s'.\n", GNUNET_i2s (target));
1928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1929 "Transmission request queued for transmission to transport service.\n");
1931 GNUNET_assert (NULL == n->transmit_handle);
1932 n->transmit_handle = th;
1933 if (GNUNET_YES != n->received_ack)
1936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1937 "Connection to `%4s' is not yet confirmed connected, scheduling timeout (%llu ms) only.\n",
1938 GNUNET_i2s (target), timeout.value);
1940 th->notify_delay_task
1941 = GNUNET_SCHEDULER_add_delayed (handle->sched,
1943 GNUNET_SCHEDULER_PRIORITY_KEEP,
1944 GNUNET_SCHEDULER_NO_TASK,
1945 timeout, &transmit_timeout, th);
1950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1951 "Peer `%4s' is ready to receive, scheduling message for delivery now.\n",
1952 GNUNET_i2s (target));
1954 schedule_request (th);
1960 * Cancel the specified transmission-ready
1964 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
1965 GNUNET_TRANSPORT_TransmitHandle
1968 struct GNUNET_TRANSPORT_Handle *h;
1971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1972 "Transmission request of %u bytes to `%4s' was cancelled.\n",
1973 th->notify_size - sizeof(struct OutboundMessage),
1974 GNUNET_i2s (&th->target));
1976 GNUNET_assert (th->notify == &client_notify_wrapper);
1977 remove_from_any_list (th);
1979 if ((h->connect_ready_head == NULL) && (h->network_handle != NULL))
1981 GNUNET_CONNECTION_notify_transmit_ready_cancel (h->network_handle);
1982 h->network_handle = NULL;
1983 h->transmission_scheduled = GNUNET_NO;
1985 GNUNET_free (th->notify_cls);
1990 /* end of transport_api.c */