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_NETWORK_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_NETWORK_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 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
399 "Ready to transmit %u bytes to transport service\n", size);
403 h->network_handle = NULL;
404 h->transmission_scheduled = GNUNET_NO;
407 th = h->connect_ready_head;
408 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
410 /* remove existing time out task (only applies if
411 this is not the first iteration of the loop) */
412 GNUNET_SCHEDULER_cancel (h->sched,
413 th->notify_delay_task);
414 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
416 GNUNET_assert (th->notify_size <= size);
417 if (th->next != NULL)
418 th->next->prev = NULL;
419 h->connect_ready_head = th->next;
420 if (NULL != (n = th->neighbour))
422 GNUNET_assert (n->transmit_handle == th);
423 n->transmit_handle = NULL;
425 ret += th->notify (th->notify_cls, size, &cbuf[ret]);
431 while ((h->connect_ready_head != NULL) &&
432 (h->connect_ready_head->notify_size <= size));
433 if (h->connect_ready_head != NULL)
434 schedule_transmission (h);
436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
437 "Transmitting %u bytes to transport service\n", ret);
444 * Schedule the task to send one message from the
445 * connect_ready list to the service.
448 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
450 struct GNUNET_TRANSPORT_TransmitHandle *th;
452 GNUNET_assert (NULL == h->network_handle);
453 if (h->client == NULL)
455 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
456 "Could not yet schedule transmission: we are not yet connected to the transport service!\n");
457 return; /* not yet connected */
459 th = h->connect_ready_head;
461 return; /* no request pending */
462 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
464 /* remove existing time out task, will be integrated
465 with transmit_ready notification! */
466 GNUNET_SCHEDULER_cancel (h->sched,
467 th->notify_delay_task);
468 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
470 h->transmission_scheduled = GNUNET_YES;
471 h->network_handle = GNUNET_CLIENT_notify_transmit_ready (h->client,
473 GNUNET_TIME_absolute_get_remaining
475 &transport_notify_ready,
477 GNUNET_assert (NULL != h->network_handle);
482 * Insert the given transmit handle in the given sorted
483 * doubly linked list based on timeout.
485 * @param head pointer to the head of the linked list
486 * @param th element to insert into the list
489 insert_transmit_handle (struct GNUNET_TRANSPORT_TransmitHandle **head,
490 struct GNUNET_TRANSPORT_TransmitHandle *th)
492 struct GNUNET_TRANSPORT_TransmitHandle *pos;
493 struct GNUNET_TRANSPORT_TransmitHandle *prev;
497 while ((pos != NULL) && (pos->timeout.value < th->timeout.value))
505 if (th->next != NULL)
521 * Cancel a pending notify delay task (if pending) and also remove the
522 * given transmit handle from whatever list is on.
524 * @param th handle for the transmission request to manipulate
527 remove_from_any_list (struct GNUNET_TRANSPORT_TransmitHandle *th)
529 struct GNUNET_TRANSPORT_Handle *h;
532 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
534 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
535 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
537 if (th->prev == NULL)
539 if (th == h->connect_wait_head)
540 h->connect_wait_head = th->next;
542 h->connect_ready_head = th->next;
546 th->prev->next = th->next;
548 if (th->next != NULL)
549 th->next->prev = th->prev;
554 * Schedule a request to connect to the given
555 * neighbour (and if successful, add the specified
556 * handle to the wait list).
558 * @param th handle for a request to transmit once we
562 try_connect (struct GNUNET_TRANSPORT_TransmitHandle *th);
566 * Called when our transmit request timed out before any transport
567 * reported success connecting to the desired peer or before the
568 * transport was ready to receive. Signal error and free
572 transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
574 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
576 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
577 if (th->neighbour != NULL)
578 th->neighbour->transmit_handle = NULL;
580 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
581 "Request for transmission to peer `%s' timed out.\n",
582 GNUNET_i2s(&th->target));
584 remove_from_any_list (th);
585 th->notify (th->notify_cls, 0, NULL);
593 * Queue control request for transmission to the transport
596 * @param size number of bytes to be transmitted
597 * @param at_head request must be added to the head of the queue
598 * (otherwise request will be appended)
599 * @param timeout how long this transmission can wait (at most)
600 * @param notify function to call to get the content
601 * @param notify_cls closure for notify
604 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h,
607 struct GNUNET_TIME_Relative timeout,
608 GNUNET_NETWORK_TransmitReadyNotify notify,
611 struct GNUNET_TRANSPORT_TransmitHandle *th;
613 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
616 th->notify_cls = notify_cls;
617 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
618 th->notify_size = size;
619 th->notify_delay_task
620 = GNUNET_SCHEDULER_add_delayed (h->sched,
622 GNUNET_SCHEDULER_PRIORITY_KEEP,
623 GNUNET_SCHEDULER_NO_TASK,
625 &transmit_timeout, th);
628 th->next = h->connect_ready_head;
629 h->connect_ready_head = th;
630 if (th->next != NULL)
635 insert_transmit_handle (&h->connect_ready_head, th);
637 if (GNUNET_NO == h->transmission_scheduled)
638 schedule_transmission (h);
643 * Update the quota values for the given neighbour now.
646 update_quota (struct NeighbourList *n)
648 struct GNUNET_TIME_Relative delta;
652 delta = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
653 allowed = delta.value * n->quota_out;
654 if (n->last_sent < allowed)
656 remaining = allowed - n->last_sent;
657 if (n->quota_out > 0)
658 remaining /= n->quota_out;
661 if (remaining > MAX_BANDWIDTH_CARRY)
662 remaining = MAX_BANDWIDTH_CARRY;
664 n->last_quota_update = GNUNET_TIME_absolute_get ();
665 n->last_quota_update.value -= remaining;
669 n->last_sent -= allowed;
670 n->last_quota_update = GNUNET_TIME_absolute_get ();
675 struct SetQuotaContext
677 struct GNUNET_TRANSPORT_Handle *handle;
679 struct GNUNET_PeerIdentity target;
681 GNUNET_SCHEDULER_Task cont;
685 struct GNUNET_TIME_Absolute timeout;
692 send_set_quota (void *cls, size_t size, void *buf)
694 struct SetQuotaContext *sqc = cls;
695 struct QuotaSetMessage *msg;
699 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
703 GNUNET_SCHEDULER_REASON_TIMEOUT);
708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
709 "Transmitting `%s' request with respect to `%4s'.\n",
710 "SET_QUOTA", GNUNET_i2s (&sqc->target));
712 GNUNET_assert (size >= sizeof (struct QuotaSetMessage));
714 msg->header.size = htons (sizeof (struct QuotaSetMessage));
715 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
716 msg->quota_in = htonl (sqc->quota_in);
717 memcpy (&msg->peer, &sqc->target, sizeof (struct GNUNET_PeerIdentity));
718 if (sqc->cont != NULL)
719 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
723 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
725 return sizeof (struct QuotaSetMessage);
730 * Set the share of incoming bandwidth for the given
731 * peer to the specified amount.
733 * @param handle connection to transport service
734 * @param target who's bandwidth quota is being changed
735 * @param quota_in incoming bandwidth quota in bytes per ms; 0 can
736 * be used to force all traffic to be discarded
737 * @param quota_out outgoing bandwidth quota in bytes per ms; 0 can
738 * be used to force all traffic to be discarded
739 * @param timeout how long to wait until signaling failure if
740 * we can not communicate the quota change
741 * @param cont continuation to call when done, will be called
742 * either with reason "TIMEOUT" or with reason "PREREQ_DONE"
743 * @param cont_cls closure for continuation
746 GNUNET_TRANSPORT_set_quota (struct GNUNET_TRANSPORT_Handle *handle,
747 const struct GNUNET_PeerIdentity *target,
750 struct GNUNET_TIME_Relative timeout,
751 GNUNET_SCHEDULER_Task cont, void *cont_cls)
753 struct NeighbourList *n;
754 struct SetQuotaContext *sqc;
756 n = find_neighbour (handle, target);
760 if (n->quota_out < quota_out)
761 n->last_quota_update = GNUNET_TIME_absolute_get ();
762 n->quota_out = quota_out;
764 sqc = GNUNET_malloc (sizeof (struct SetQuotaContext));
765 sqc->handle = handle;
766 sqc->target = *target;
768 sqc->cont_cls = cont_cls;
769 sqc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
770 sqc->quota_in = quota_in;
771 schedule_control_transmit (handle,
772 sizeof (struct QuotaSetMessage),
773 GNUNET_NO, timeout, &send_set_quota, sqc);
778 * A "get_hello" request has timed out. Signal the client
782 hello_wait_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
784 struct HelloWaitList *hwl = cls;
785 struct HelloWaitList *pos;
786 struct HelloWaitList *prev;
788 hwl->task = GNUNET_SCHEDULER_NO_TASK;
789 if (GNUNET_TIME_absolute_get_remaining (hwl->timeout).value > 0)
792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
793 _("First attempt to obtain `%s' from transport service failed, will try again for %llums.\n"),
795 GNUNET_TIME_absolute_get_remaining (hwl->timeout).value);
797 hwl->task = GNUNET_SCHEDULER_add_delayed (hwl->handle->sched,
799 GNUNET_SCHEDULER_PRIORITY_KEEP,
800 GNUNET_SCHEDULER_NO_TASK,
801 GNUNET_TIME_absolute_get_remaining (hwl->timeout),
802 &hello_wait_timeout, hwl);
806 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
807 _("Timeout trying to obtain `%s' from transport service.\n"),
810 pos = hwl->handle->hwl_head;
813 GNUNET_assert (pos != NULL);
818 hwl->handle->hwl_head = hwl->next;
820 prev->next = hwl->next;
821 if (hwl->rec != NULL)
822 hwl->rec (hwl->rec_cls, GNUNET_TIME_UNIT_ZERO, NULL, NULL);
828 * Obtain the HELLO message for this peer.
830 * @param handle connection to transport service
831 * @param timeout how long to wait for the HELLO
832 * @param rec function to call with the HELLO, sender will be our peer
833 * identity; message and sender will be NULL on timeout
834 * (handshake with transport service pending/failed).
835 * cost estimate will be 0.
836 * @param rec_cls closure for rec
839 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
840 struct GNUNET_TIME_Relative timeout,
841 GNUNET_TRANSPORT_ReceiveCallback rec,
844 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
845 struct GNUNET_PeerIdentity me;
846 struct HelloWaitList *hwl;
848 if (handle->my_hello == NULL)
850 hwl = GNUNET_malloc (sizeof (struct HelloWaitList));
851 hwl->next = handle->hwl_head;
852 handle->hwl_head = hwl;
853 hwl->handle = handle;
855 hwl->rec_cls = rec_cls;
856 hwl->timeout = GNUNET_TIME_relative_to_absolute (timeout);
857 hwl->task = GNUNET_SCHEDULER_add_delayed (handle->sched,
859 GNUNET_SCHEDULER_PRIORITY_KEEP,
860 GNUNET_SCHEDULER_NO_TASK,
862 &hello_wait_timeout, hwl);
865 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_key (handle->my_hello, &pk));
866 GNUNET_CRYPTO_hash (&pk,
867 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
871 GNUNET_TIME_UNIT_ZERO,
872 &me, (const struct GNUNET_MessageHeader *) handle->my_hello);
877 send_hello (void *cls, size_t size, void *buf)
879 struct GNUNET_MessageHeader *hello = cls;
885 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
886 "Timeout while trying to transmit `%s' request.\n",
893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
894 "Transmitting `%s' request.\n", "HELLO");
896 msize = ntohs (hello->size);
897 GNUNET_assert (size >= msize);
898 memcpy (buf, hello, msize);
905 * Offer the transport service the HELLO of another peer. Note that
906 * the transport service may just ignore this message if the HELLO is
907 * malformed or useless due to our local configuration.
909 * @param handle connection to transport service
910 * @param hello the hello message
913 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
914 const struct GNUNET_MessageHeader *hello)
916 struct GNUNET_MessageHeader *hc;
919 if (handle->client == NULL)
922 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
923 "Not connected to transport service, dropping offered HELLO\n");
927 GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
928 size = ntohs (hello->size);
929 GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
930 hc = GNUNET_malloc (size);
931 memcpy (hc, hello, size);
932 schedule_control_transmit (handle,
934 GNUNET_NO, OFFER_HELLO_TIMEOUT, &send_hello, hc);
939 * Function we use for handling incoming messages.
941 static void demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg);
945 send_start (void *cls, size_t size, void *buf)
947 struct GNUNET_MessageHeader *s = buf;
952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
953 "Timeout while trying to transmit `%s' request.\n",
959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
960 "Transmitting `%s' request.\n", "START");
962 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
963 s->size = htons (sizeof (struct GNUNET_MessageHeader));
964 s->type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
965 return sizeof (struct GNUNET_MessageHeader);
970 * We're ready to transmit the request that the transport service
971 * should connect to a new peer. In addition to sending the
972 * request, schedule the next phase for the transmission processing
973 * that caused the connect request in the first place.
976 request_connect (void *cls, size_t size, void *buf)
978 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
979 struct TryConnectMessage *tcm;
980 struct GNUNET_TRANSPORT_Handle *h;
982 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
988 "Failed to transmit `%s' request for `%4s' to service.\n",
990 GNUNET_i2s(&th->target));
992 th->notify (th->notify_cls, 0, NULL);
997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
998 "Transmitting `%s' message for `%4s'.\n",
999 "TRY_CONNECT", GNUNET_i2s (&th->target));
1001 GNUNET_assert (size >= sizeof (struct TryConnectMessage));
1003 tcm->header.size = htons (sizeof (struct TryConnectMessage));
1004 tcm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TRY_CONNECT);
1005 tcm->reserved = htonl (0);
1006 memcpy (&tcm->peer, &th->target, sizeof (struct GNUNET_PeerIdentity));
1007 th->notify_delay_task
1008 = GNUNET_SCHEDULER_add_delayed (h->sched,
1010 GNUNET_SCHEDULER_PRIORITY_KEEP,
1011 GNUNET_SCHEDULER_NO_TASK,
1012 GNUNET_TIME_absolute_get_remaining
1014 &transmit_timeout, th);
1015 insert_transmit_handle (&h->connect_wait_head, th);
1016 return sizeof (struct TryConnectMessage);
1021 * Schedule a request to connect to the given
1022 * neighbour (and if successful, add the specified
1023 * handle to the wait list).
1025 * @param th handle for a request to transmit once we
1029 try_connect (struct GNUNET_TRANSPORT_TransmitHandle *th)
1031 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
1032 schedule_control_transmit (th->handle,
1033 sizeof (struct TryConnectMessage),
1035 GNUNET_TIME_absolute_get_remaining (th->timeout),
1036 &request_connect, th);
1041 * Task for delayed attempts to reconnect to a peer.
1043 * @param cls must be a transmit handle that determines the peer
1044 * to which we will try to connect
1045 * @param tc scheduler information about why we were triggered (not used)
1048 try_connect_task (void *cls,
1049 const struct GNUNET_SCHEDULER_TaskContext *tc)
1051 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
1053 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1059 * Remove neighbour from our list. Will automatically
1060 * trigger a re-connect attempt if we have messages pending
1063 * @param h our state
1064 * @param peer the peer to remove
1067 remove_neighbour (struct GNUNET_TRANSPORT_Handle *h,
1068 const struct GNUNET_PeerIdentity *peer)
1070 struct NeighbourList *prev;
1071 struct NeighbourList *pos;
1072 struct GNUNET_TRANSPORT_TransmitHandle *th;
1075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1076 "Removing neighbour `%s' from list of connected peers.\n",
1080 pos = h->neighbours;
1081 while ((pos != NULL) &&
1084 sizeof (struct GNUNET_PeerIdentity))))
1095 h->neighbours = pos->next;
1097 prev->next = pos->next;
1098 if (NULL != (th = pos->transmit_handle))
1100 pos->transmit_handle = NULL;
1101 th->neighbour = NULL;
1102 remove_from_any_list (th);
1103 if (GNUNET_TIME_absolute_get_remaining (th->timeout).value <= CONNECT_RETRY_TIMEOUT.value)
1106 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == th->notify_delay_task);
1107 transmit_timeout (th, NULL);
1111 /* try again in a bit */
1112 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == th->notify_delay_task);
1113 th->notify_delay_task
1114 = GNUNET_SCHEDULER_add_delayed (h->sched,
1116 GNUNET_SCHEDULER_PRIORITY_KEEP,
1117 GNUNET_SCHEDULER_NO_TASK,
1118 CONNECT_RETRY_TIMEOUT,
1123 if (h->nc_cb != NULL)
1124 h->nd_cb (h->cls, peer);
1130 * Try again to connect to transport service.
1133 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1135 struct GNUNET_TRANSPORT_Handle *h = cls;
1136 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1137 struct NeighbourList *n;
1139 /* Forget about all neighbours that we used to be connected
1141 while (NULL != (n = h->neighbours))
1142 remove_neighbour (h, &n->id);
1144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n");
1146 GNUNET_assert (h->client == NULL);
1147 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1148 h->client = GNUNET_CLIENT_connect (h->sched, "transport", h->cfg);
1149 GNUNET_assert (h->client != NULL);
1150 /* make sure we don't send "START" twice,
1151 remove existing entry from queue (if present) */
1152 pos = h->connect_ready_head;
1155 if (pos->notify == &send_start)
1157 if (pos->prev == NULL)
1158 h->connect_ready_head = pos->next;
1160 pos->prev->next = pos->next;
1161 if (pos->next != NULL)
1162 pos->next->prev = pos->prev;
1163 GNUNET_assert (pos->neighbour == NULL);
1169 schedule_control_transmit (h,
1170 sizeof (struct GNUNET_MessageHeader),
1172 GNUNET_TIME_UNIT_FOREVER_REL,
1174 GNUNET_CLIENT_receive (h->client,
1175 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1180 * Function that will schedule the job that will try
1181 * to connect us again to the client.
1184 schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
1187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1188 "Scheduling task to reconnect to transport service in %llu ms.\n",
1189 h->reconnect_delay.value);
1191 GNUNET_assert (h->client == NULL);
1192 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
1194 = GNUNET_SCHEDULER_add_delayed (h->sched,
1196 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1197 GNUNET_SCHEDULER_NO_TASK,
1198 h->reconnect_delay, &reconnect, h);
1199 h->reconnect_delay = GNUNET_TIME_UNIT_SECONDS;
1204 * We are connected to the respective peer, check the
1205 * bandwidth limits and schedule the transmission.
1207 static void schedule_request (struct GNUNET_TRANSPORT_TransmitHandle *th);
1211 * Function called by the scheduler when the timeout
1212 * for bandwidth availablility for the target
1213 * neighbour is reached.
1216 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1218 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
1220 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1221 schedule_request (th);
1226 * Remove the given transmit handle from the wait list. Does NOT free
1230 remove_from_wait_list (struct GNUNET_TRANSPORT_TransmitHandle *th)
1232 if (th->prev == NULL)
1233 th->handle->connect_wait_head = th->next;
1235 th->prev->next = th->next;
1236 if (th->next != NULL)
1237 th->next->prev = th->prev;
1242 * We are connected to the respective peer, check the
1243 * bandwidth limits and schedule the transmission.
1246 schedule_request (struct GNUNET_TRANSPORT_TransmitHandle *th)
1248 struct GNUNET_TRANSPORT_Handle *h;
1249 struct GNUNET_TIME_Relative duration;
1250 struct NeighbourList *n;
1255 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1257 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
1258 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1260 /* check outgoing quota */
1261 duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
1262 if (duration.value > MIN_QUOTA_REFRESH_TIME)
1265 duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
1267 available = duration.value * n->quota_out;
1268 if (available < n->last_sent + th->notify_size)
1270 /* calculate how much bandwidth we'd still need to
1271 accumulate and based on that how long we'll have
1273 available = n->last_sent + th->notify_size - available;
1274 duration = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1275 available / n->quota_out);
1276 if (th->timeout.value <
1277 GNUNET_TIME_relative_to_absolute (duration).value)
1279 /* signal timeout! */
1281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1282 "Would need %llu ms before bandwidth is available for delivery to `%4s', that is too long. Signaling timeout.\n",
1284 GNUNET_i2s(&th->target));
1286 remove_from_wait_list (th);
1287 th->notify (th->notify_cls, 0, NULL);
1292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1293 "Need more bandwidth, delaying delivery to `%4s' by %llu ms\n",
1294 GNUNET_i2s(&th->target),
1297 th->notify_delay_task
1298 = GNUNET_SCHEDULER_add_delayed (h->sched,
1300 GNUNET_SCHEDULER_PRIORITY_KEEP,
1301 GNUNET_SCHEDULER_NO_TASK,
1302 duration, &transmit_ready, th);
1306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1307 "Bandwidth available for transmission to `%4s'\n",
1308 GNUNET_i2s (&n->id));
1310 if (GNUNET_NO == n->transmit_ok)
1312 /* we may be ready, but transport service is not;
1313 wait for SendOkMessage or timeout */
1315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1316 "Need to wait for transport service `%s' message\n",
1319 th->notify_delay_task
1320 = GNUNET_SCHEDULER_add_delayed (h->sched,
1322 GNUNET_SCHEDULER_PRIORITY_KEEP,
1323 GNUNET_SCHEDULER_NO_TASK,
1324 GNUNET_TIME_absolute_get_remaining
1325 (th->timeout), &transmit_timeout, th);
1328 n->transmit_ok = GNUNET_NO;
1329 remove_from_wait_list (th);
1331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Moving message for `%4s' to ready list\n",
1332 GNUNET_i2s(&n->id));
1334 insert_transmit_handle (&h->connect_ready_head, th);
1335 if (GNUNET_NO == h->transmission_scheduled)
1336 schedule_transmission (h);
1341 * Add neighbour to our list
1344 add_neighbour (struct GNUNET_TRANSPORT_Handle *h,
1346 struct GNUNET_TIME_Relative latency,
1347 const struct GNUNET_PeerIdentity *pid)
1349 struct NeighbourList *n;
1350 struct GNUNET_TRANSPORT_TransmitHandle *prev;
1351 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1352 struct GNUNET_TRANSPORT_TransmitHandle *next;
1354 /* check for duplicates */
1355 if (NULL != find_neighbour (h, pid))
1361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1362 "Creating entry for new neighbour `%4s'.\n", GNUNET_i2s (pid));
1364 n = GNUNET_malloc (sizeof (struct NeighbourList));
1366 n->last_quota_update = GNUNET_TIME_absolute_get ();
1367 n->quota_out = quota_out;
1368 n->next = h->neighbours;
1369 n->transmit_ok = GNUNET_YES;
1371 if (h->nc_cb != NULL)
1372 h->nc_cb (h->cls, &n->id, latency);
1374 pos = h->connect_wait_head;
1378 if (0 == memcmp (pid,
1379 &pos->target, sizeof (struct GNUNET_PeerIdentity)))
1382 GNUNET_assert (NULL == n->transmit_handle);
1383 n->transmit_handle = pos;
1385 h->connect_wait_head = next;
1388 if (GNUNET_YES == n->received_ack)
1391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1392 "Found pending request for `%4s' will trigger it now.\n",
1393 GNUNET_i2s (&pos->target));
1395 if (pos->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1397 GNUNET_SCHEDULER_cancel (h->sched, pos->notify_delay_task);
1398 pos->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1400 schedule_request (pos);
1405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1406 "Found pending request for `%4s' but still need `%s' before proceeding.\n",
1407 GNUNET_i2s (&pos->target),
1420 * Connect to the transport service. Note that the connection may
1421 * complete (or fail) asynchronously.
1424 * @param sched scheduler to use
1425 * @param cfg configuration to use
1426 * @param cls closure for the callbacks
1427 * @param rec receive function to call
1428 * @param nc function to call on connect events
1429 * @param dc function to call on disconnect events
1431 struct GNUNET_TRANSPORT_Handle *
1432 GNUNET_TRANSPORT_connect (struct GNUNET_SCHEDULER_Handle *sched,
1433 const struct GNUNET_CONFIGURATION_Handle *cfg,
1435 GNUNET_TRANSPORT_ReceiveCallback rec,
1436 GNUNET_TRANSPORT_NotifyConnect nc,
1437 GNUNET_TRANSPORT_NotifyDisconnect nd)
1439 struct GNUNET_TRANSPORT_Handle *ret;
1441 GNUNET_ARM_start_service ("peerinfo",
1442 cfg, sched, START_SERVICE_TIMEOUT, NULL, NULL);
1443 GNUNET_ARM_start_service ("transport",
1444 cfg, sched, START_SERVICE_TIMEOUT, NULL, NULL);
1445 ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Handle));
1452 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1453 schedule_reconnect (ret);
1459 * These stop activities must be run in a fresh
1460 * scheduler that is NOT in shutdown mode.
1463 stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1465 struct GNUNET_TRANSPORT_Handle *handle = cls;
1466 GNUNET_ARM_stop_service ("transport",
1468 tc->sched, STOP_SERVICE_TIMEOUT, NULL, NULL);
1469 GNUNET_ARM_stop_service ("peerinfo",
1471 tc->sched, STOP_SERVICE_TIMEOUT, NULL, NULL);
1476 * Disconnect from the transport service.
1479 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1481 struct GNUNET_TRANSPORT_TransmitHandle *th;
1482 struct NeighbourList *n;
1483 struct HelloWaitList *hwl;
1484 struct GNUNET_CLIENT_Connection *client;
1487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transport disconnect called!\n");
1489 while (NULL != (th = handle->connect_ready_head))
1491 handle->connect_ready_head = th->next;
1492 th->notify (th->notify_cls, 0, NULL);
1495 while (NULL != (th = handle->connect_wait_head))
1497 handle->connect_wait_head = th->next;
1498 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1500 GNUNET_SCHEDULER_cancel (handle->sched, th->notify_delay_task);
1501 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1503 th->notify (th->notify_cls, 0, NULL);
1506 while (NULL != (n = handle->neighbours))
1508 handle->neighbours = n->next;
1511 while (NULL != (hwl = handle->hwl_head))
1513 handle->hwl_head = hwl->next;
1514 GNUNET_SCHEDULER_cancel (handle->sched, hwl->task);
1515 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1516 _("Disconnect while trying to obtain `%s' from transport service.\n"),
1518 if (hwl->rec != NULL)
1519 hwl->rec (hwl->rec_cls, GNUNET_TIME_UNIT_ZERO, NULL, NULL);
1522 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1524 GNUNET_SCHEDULER_cancel (handle->sched, handle->reconnect_task);
1525 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1527 GNUNET_free_non_null (handle->my_hello);
1528 handle->my_hello = NULL;
1529 GNUNET_SCHEDULER_run (&stop_task, handle);
1530 if (NULL != (client = handle->client))
1533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1534 "Disconnecting from transport service for good.\n");
1536 handle->client = NULL;
1537 GNUNET_CLIENT_disconnect (client);
1540 GNUNET_free (handle);
1545 * Type of a function to call when we receive a message
1548 * @param cls closure
1549 * @param msg message received, NULL on timeout or fatal error
1552 demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
1554 struct GNUNET_TRANSPORT_Handle *h = cls;
1555 const struct DisconnectInfoMessage *dim;
1556 const struct ConnectInfoMessage *cim;
1557 const struct InboundMessage *im;
1558 const struct GNUNET_MessageHeader *imm;
1559 const struct SendOkMessage *okm;
1560 struct HelloWaitList *hwl;
1561 struct NeighbourList *n;
1562 struct GNUNET_PeerIdentity me;
1563 struct GNUNET_TRANSPORT_TransmitHandle *th;
1566 if ((msg == NULL) || (h->client == NULL))
1568 if (h->client != NULL)
1571 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1572 "Error receiving from transport service, disconnecting temporarily.\n");
1574 if (h->network_handle != NULL)
1576 GNUNET_NETWORK_notify_transmit_ready_cancel (h->network_handle);
1577 h->network_handle = NULL;
1578 h->transmission_scheduled = GNUNET_NO;
1579 th = h->connect_ready_head;
1580 /* add timeout again, we cancelled the transmit_ready task! */
1581 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
1582 th->notify_delay_task
1583 = GNUNET_SCHEDULER_add_delayed (h->sched,
1585 GNUNET_SCHEDULER_PRIORITY_KEEP,
1586 GNUNET_SCHEDULER_NO_TASK,
1587 GNUNET_TIME_absolute_get_remaining(th->timeout),
1591 GNUNET_CLIENT_disconnect (h->client);
1593 schedule_reconnect (h);
1597 /* shutdown initiated from 'GNUNET_TRANSPORT_disconnect',
1598 finish clean up work! */
1603 GNUNET_CLIENT_receive (h->client,
1604 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1605 size = ntohs (msg->size);
1606 switch (ntohs (msg->type))
1608 case GNUNET_MESSAGE_TYPE_HELLO:
1610 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg,
1617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1618 "Receiving (my own) `%s' message, I am `%4s'.\n",
1619 "HELLO", GNUNET_i2s (&me));
1621 GNUNET_free_non_null (h->my_hello);
1623 if (size < sizeof (struct GNUNET_MessageHeader))
1628 h->my_hello = GNUNET_malloc (size);
1629 memcpy (h->my_hello, msg, size);
1630 while (NULL != (hwl = h->hwl_head))
1632 h->hwl_head = hwl->next;
1633 GNUNET_SCHEDULER_cancel (h->sched, hwl->task);
1634 GNUNET_TRANSPORT_get_hello (h,
1635 GNUNET_TIME_UNIT_ZERO,
1636 hwl->rec, hwl->rec_cls);
1640 case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
1641 if (size != sizeof (struct ConnectInfoMessage))
1646 cim = (const struct ConnectInfoMessage *) msg;
1648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1649 "Receiving `%s' message for `%4s'.\n",
1650 "CONNECT", GNUNET_i2s (&cim->id));
1653 ntohl (cim->quota_out),
1654 GNUNET_TIME_relative_ntoh (cim->latency), &cim->id);
1656 case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
1657 if (size != sizeof (struct DisconnectInfoMessage))
1662 dim = (const struct DisconnectInfoMessage *) msg;
1664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1665 "Receiving `%s' message for `%4s'.\n",
1666 "DISCONNECT", GNUNET_i2s (&dim->peer));
1668 remove_neighbour (h, &dim->peer);
1670 case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
1671 if (size != sizeof (struct SendOkMessage))
1676 okm = (const struct SendOkMessage *) msg;
1678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1679 "Receiving `%s' message, transmission %s.\n", "SEND_OK",
1680 ntohl(okm->success) == GNUNET_OK ? "succeeded" : "failed");
1682 n = find_neighbour (h, &okm->peer);
1683 GNUNET_assert (n != NULL);
1684 n->transmit_ok = GNUNET_YES;
1685 if (n->transmit_handle != NULL)
1688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1689 "Processing pending message for `%4s'\n",
1690 GNUNET_i2s(&n->id));
1692 GNUNET_SCHEDULER_cancel (h->sched,
1693 n->transmit_handle->notify_delay_task);
1694 n->transmit_handle->notify_delay_task =
1695 GNUNET_SCHEDULER_NO_TASK;
1696 GNUNET_assert (GNUNET_YES == n->received_ack);
1697 schedule_request (n->transmit_handle);
1700 case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
1702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1703 "Receiving `%s' message.\n", "RECV");
1706 sizeof (struct InboundMessage) +
1707 sizeof (struct GNUNET_MessageHeader))
1712 im = (const struct InboundMessage *) msg;
1713 imm = (const struct GNUNET_MessageHeader *) &im[1];
1714 if (ntohs (imm->size) + sizeof (struct InboundMessage) != size)
1719 switch (ntohs (imm->type))
1721 case GNUNET_MESSAGE_TYPE_TRANSPORT_ACK:
1723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1724 "Receiving `%s' message from `%4s'.\n",
1725 "ACK", GNUNET_i2s (&im->peer));
1727 n = find_neighbour (h, &im->peer);
1733 if (n->received_ack == GNUNET_NO)
1735 n->received_ack = GNUNET_YES;
1736 if (NULL != n->transmit_handle)
1739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1740 "Peer connected, scheduling delayed message for deliverery now.\n");
1742 schedule_request (n->transmit_handle);
1748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1749 "Received message of type %u from `%4s'.\n",
1750 ntohs (imm->type), GNUNET_i2s (&im->peer));
1754 GNUNET_TIME_relative_ntoh (im->latency), &im->peer, imm);
1759 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1761 ("Received unexpected message of type %u in %s:%u\n"),
1762 ntohs (msg->type), __FILE__, __LINE__);
1769 struct ClientTransmitWrapper
1771 GNUNET_NETWORK_TransmitReadyNotify notify;
1773 struct GNUNET_TRANSPORT_TransmitHandle *th;
1778 * Transmit message of a client destined for another
1779 * peer to the service.
1782 client_notify_wrapper (void *cls, size_t size, void *buf)
1784 struct ClientTransmitWrapper *ctw = cls;
1785 struct OutboundMessage *obm;
1786 struct GNUNET_MessageHeader *hdr;
1792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1793 "Transmission request could not be satisfied.\n");
1795 ret = ctw->notify (ctw->notify_cls, 0, NULL);
1796 GNUNET_assert (ret == 0);
1800 GNUNET_assert (size >= sizeof (struct OutboundMessage));
1802 ret = ctw->notify (ctw->notify_cls,
1803 size - sizeof (struct OutboundMessage),
1807 /* Need to reset flag, no SEND means no SEND_OK! */
1808 ctw->th->neighbour->transmit_ok = GNUNET_YES;
1812 GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
1813 hdr = (struct GNUNET_MessageHeader *) &obm[1];
1814 GNUNET_assert (ntohs (hdr->size) == ret);
1815 GNUNET_assert (ret + sizeof (struct OutboundMessage) <
1816 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1819 "Transmitting `%s' message with data for `%4s'\n",
1820 "SEND", GNUNET_i2s (&ctw->th->target));
1822 ret += sizeof (struct OutboundMessage);
1823 obm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
1824 obm->header.size = htons (ret);
1825 obm->priority = htonl (ctw->th->priority);
1826 obm->peer = ctw->th->target;
1834 * Check if we could queue a message of the given size for
1835 * transmission. The transport service will take both its
1836 * internal buffers and bandwidth limits imposed by the
1837 * other peer into consideration when answering this query.
1839 * @param handle connection to transport service
1840 * @param target who should receive the message
1841 * @param size how big is the message we want to transmit?
1842 * @param priority how important is the message?
1843 * @param timeout after how long should we give up (and call
1844 * notify with buf NULL and size 0)?
1845 * @param notify function to call when we are ready to
1846 * send such a message
1847 * @param notify_cls closure for notify
1848 * @return NULL if someone else is already waiting to be notified
1849 * non-NULL if the notify callback was queued (can be used to cancel
1850 * using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1852 struct GNUNET_TRANSPORT_TransmitHandle *
1853 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle
1855 const struct GNUNET_PeerIdentity
1856 *target, size_t size,
1857 unsigned int priority,
1858 struct GNUNET_TIME_Relative timeout,
1859 GNUNET_NETWORK_TransmitReadyNotify
1860 notify, void *notify_cls)
1862 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1863 struct GNUNET_TRANSPORT_TransmitHandle *th;
1864 struct NeighbourList *n;
1865 struct ClientTransmitWrapper *ctw;
1867 if (size + sizeof (struct OutboundMessage) >=
1868 GNUNET_SERVER_MAX_MESSAGE_SIZE)
1874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1875 "Asking transport service for transmission of %u bytes to peer `%4s'.\n",
1876 size, GNUNET_i2s (target));
1878 n = find_neighbour (handle, target);
1880 (n->transmit_handle != NULL) )
1881 return NULL; /* already have a request pending for this peer! */
1882 ctw = GNUNET_malloc (sizeof (struct ClientTransmitWrapper));
1883 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
1884 ctw->notify = notify;
1885 ctw->notify_cls = notify_cls;
1887 th->handle = handle;
1889 th->target = *target;
1890 th->notify = &client_notify_wrapper;
1891 th->notify_cls = ctw;
1892 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1893 th->notify_size = size + sizeof (struct OutboundMessage);
1894 th->priority = priority;
1897 pos = handle->connect_wait_head;
1900 GNUNET_assert (0 != memcmp (target,
1902 sizeof (struct GNUNET_PeerIdentity)));
1906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1907 "Will now try to connect to `%4s'.\n", GNUNET_i2s (target));
1914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1915 "Transmission request queued for transmission to transport service.\n");
1917 GNUNET_assert (NULL == n->transmit_handle);
1918 n->transmit_handle = th;
1919 if (GNUNET_YES != n->received_ack)
1922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1923 "Connection to `%4s' is not yet confirmed connected, scheduling timeout (%llu ms) only.\n",
1924 GNUNET_i2s (target), timeout.value);
1926 th->notify_delay_task
1927 = GNUNET_SCHEDULER_add_delayed (handle->sched,
1929 GNUNET_SCHEDULER_PRIORITY_KEEP,
1930 GNUNET_SCHEDULER_NO_TASK,
1931 timeout, &transmit_timeout, th);
1936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1937 "Peer `%4s' is ready to receive, scheduling message for delivery now.\n",
1938 GNUNET_i2s (target));
1940 schedule_request (th);
1946 * Cancel the specified transmission-ready
1950 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
1951 GNUNET_TRANSPORT_TransmitHandle
1954 struct GNUNET_TRANSPORT_Handle *h;
1957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1958 "Transmission request of %u bytes to `%4s' was cancelled.\n",
1959 th->notify_size - sizeof(struct OutboundMessage),
1960 GNUNET_i2s (&th->target));
1962 GNUNET_assert (th->notify == &client_notify_wrapper);
1963 remove_from_any_list (th);
1965 if ((h->connect_ready_head == NULL) && (h->network_handle != NULL))
1967 GNUNET_NETWORK_notify_transmit_ready_cancel (h->network_handle);
1968 h->network_handle = NULL;
1969 h->transmission_scheduled = GNUNET_NO;
1971 GNUNET_free (th->notify_cls);
1976 /* end of transport_api.c */