2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/transport_api.c
23 * @brief library to access the low-level P2P IO service
24 * @author Christian Grothoff
27 * - set_quota with low bandwidth should cause peer
28 * disconnects (currently never does that) (MINOR)
31 #include "gnunet_client_lib.h"
32 #include "gnunet_arm_service.h"
33 #include "gnunet_hello_lib.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_server_lib.h"
36 #include "gnunet_time_lib.h"
37 #include "gnunet_transport_service.h"
38 #include "transport.h"
41 * After how long do we give up on transmitting a HELLO
44 #define OFFER_HELLO_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
47 * How long should ARM wait when starting up the
48 * transport service before reporting back?
50 #define START_SERVICE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
53 * How long should ARM wait when stopping the
54 * transport service before reporting back?
56 #define STOP_SERVICE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
59 * Entry in linked list of all of our current neighbours.
65 * This is a linked list.
67 struct NeighbourList *next;
70 * Active transmit handle, can be NULL. Used to move
71 * from ready to wait list on disconnect and to block
72 * two transmissions to the same peer from being scheduled
75 struct GNUNET_TRANSPORT_TransmitHandle *transmit_handle;
79 * Identity of this neighbour.
81 struct GNUNET_PeerIdentity id;
84 * At what time did we reset last_sent last?
86 struct GNUNET_TIME_Absolute last_quota_update;
89 * How many bytes have we sent since the "last_quota_update"
95 * Quota for outbound traffic to the neighbour in bytes/ms.
100 * Set to GNUNET_YES if we are currently allowed to
101 * transmit a message to the transport service for this
102 * peer, GNUNET_NO otherwise.
107 * Set to GNUNET_YES if we have received an ACK for the
108 * given peer. Peers that receive our HELLO always respond
109 * with an ACK to let us know that we are successfully
110 * communicating. Note that a PING can not be used for this
111 * since PINGs are only send if a HELLO address requires
112 * confirmation (and also, PINGs are not passed to the
113 * transport API itself).
121 * Linked list of requests from clients for our HELLO
122 * that were deferred.
128 * This is a linked list.
130 struct HelloWaitList *next;
133 * Reference back to our transport handle.
135 struct GNUNET_TRANSPORT_Handle *handle;
138 * Callback to call once we got our HELLO.
140 GNUNET_TRANSPORT_ReceiveCallback rec;
148 * When to time out (call rec with NULL).
150 struct GNUNET_TIME_Absolute timeout;
153 * Timeout task (used to trigger timeout,
154 * cancel if we get the HELLO in time).
156 GNUNET_SCHEDULER_TaskIdentifier task;
163 * Opaque handle for a transmission-ready request.
165 struct GNUNET_TRANSPORT_TransmitHandle
169 * We keep the transmit handles that are waiting for
170 * a transport-level connection in a doubly linked list.
172 struct GNUNET_TRANSPORT_TransmitHandle *next;
175 * We keep the transmit handles that are waiting for
176 * a transport-level connection in a doubly linked list.
178 struct GNUNET_TRANSPORT_TransmitHandle *prev;
181 * Handle of the main transport data structure.
183 struct GNUNET_TRANSPORT_Handle *handle;
186 * Neighbour for this handle, can be NULL if the service
187 * is not yet connected to the target.
189 struct NeighbourList *neighbour;
192 * Which peer is this transmission going to be for? All
193 * zeros if it is control-traffic to the service.
195 struct GNUNET_PeerIdentity target;
198 * Function to call when notify_size bytes are available
201 GNUNET_NETWORK_TransmitReadyNotify notify;
204 * Closure for notify.
209 * transmit_ready task Id. The task is used to introduce the
210 * artificial delay that may be required to maintain the bandwidth
211 * limits. Later, this will be the ID of the "transmit_timeout"
212 * task which is used to signal a timeout if the transmission could
213 * not be done in a timely fashion.
215 GNUNET_SCHEDULER_TaskIdentifier notify_delay_task;
218 * Timeout for this request.
220 struct GNUNET_TIME_Absolute timeout;
223 * How many bytes is our notify callback waiting for?
228 * How important is this message?
230 unsigned int priority;
236 * Handle for the transport service (includes all of the
237 * state for the transport service).
239 struct GNUNET_TRANSPORT_Handle
243 * Closure for the callbacks.
248 * Function to call for received data.
250 GNUNET_TRANSPORT_ReceiveCallback rec;
253 * function to call on connect events
255 GNUNET_TRANSPORT_NotifyConnect nc_cb;
258 * function to call on disconnect events
260 GNUNET_TRANSPORT_NotifyDisconnect nd_cb;
263 * The current HELLO message for this peer. Updated
264 * whenever transports change their addresses.
266 struct GNUNET_HELLO_Message *my_hello;
269 * My client connection to the transport service.
271 struct GNUNET_CLIENT_Connection *client;
274 * Handle to our registration with the client for notification.
276 struct GNUNET_NETWORK_TransmitHandle *network_handle;
279 * Linked list of transmit handles that are waiting for the
280 * transport to connect to the respective peer. When we
281 * receive notification that the transport connected to a
282 * peer, we go over this list and check if someone has already
283 * requested a transmission to the new peer; if so, we trigger
286 struct GNUNET_TRANSPORT_TransmitHandle *connect_wait_head;
289 * Linked list of transmit handles that are waiting for the
290 * transport to be ready for transmission to the respective
292 * receive notification that the transport disconnected from
293 * a peer, we go over this list and move the entry back to
294 * the connect_wait list.
296 struct GNUNET_TRANSPORT_TransmitHandle *connect_ready_head;
299 * Linked list of pending requests for our HELLO.
301 struct HelloWaitList *hwl_head;
306 struct GNUNET_SCHEDULER_Handle *sched;
311 struct GNUNET_CONFIGURATION_Handle *cfg;
314 * Linked list of the current neighbours of this peer.
316 struct NeighbourList *neighbours;
319 * ID of the task trying to reconnect to the
322 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
325 * Delay until we try to reconnect.
327 struct GNUNET_TIME_Relative reconnect_delay;
330 * Do we currently have a transmission pending?
331 * (schedule transmission was called but has not
334 int transmission_scheduled;
338 static struct NeighbourList *
339 find_neighbour (struct GNUNET_TRANSPORT_Handle *h,
340 const struct GNUNET_PeerIdentity *peer)
342 struct NeighbourList *pos;
345 while ((pos != NULL) &&
346 (0 != memcmp (peer, &pos->id, sizeof (struct GNUNET_PeerIdentity))))
353 * Schedule the task to send one message from the
354 * connect_ready list to the service.
356 static void schedule_transmission (struct GNUNET_TRANSPORT_Handle *h);
360 * Transmit message to client...
363 transport_notify_ready (void *cls, size_t size, void *buf)
365 struct GNUNET_TRANSPORT_Handle *h = cls;
366 struct GNUNET_TRANSPORT_TransmitHandle *th;
367 struct NeighbourList *n;
371 h->network_handle = NULL;
372 h->transmission_scheduled = GNUNET_NO;
376 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
377 "Could not transmit to transport service, cancelling pending requests\n");
379 th = h->connect_ready_head;
380 if (th->next != NULL)
381 th->next->prev = NULL;
382 h->connect_ready_head = th->next;
383 if (NULL != (n = th->neighbour))
385 GNUNET_assert (n->transmit_handle == th);
386 n->transmit_handle = NULL;
388 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
394 "Ready to transmit %u bytes to transport service\n", size);
398 h->network_handle = NULL;
399 h->transmission_scheduled = GNUNET_NO;
402 th = h->connect_ready_head;
403 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
405 /* remove existing time out task (only applies if
406 this is not the first iteration of the loop) */
407 GNUNET_SCHEDULER_cancel (h->sched,
408 th->notify_delay_task);
409 th->notify_delay_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
411 GNUNET_assert (th->notify_size <= size);
412 if (th->next != NULL)
413 th->next->prev = NULL;
414 h->connect_ready_head = th->next;
415 if (NULL != (n = th->neighbour))
417 GNUNET_assert (n->transmit_handle == th);
418 n->transmit_handle = NULL;
420 ret += th->notify (th->notify_cls, size, &cbuf[ret]);
426 while ((h->connect_ready_head != NULL) &&
427 (h->connect_ready_head->notify_size <= size));
428 if (h->connect_ready_head != NULL)
429 schedule_transmission (h);
431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
432 "Transmitting %u bytes to transport service\n", ret);
439 * Schedule the task to send one message from the
440 * connect_ready list to the service.
443 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
445 struct GNUNET_TRANSPORT_TransmitHandle *th;
447 GNUNET_assert (NULL == h->network_handle);
448 if (h->client == NULL)
450 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
451 "Could not yet schedule transmission: we are not yet connected to the transport service!\n");
452 return; /* not yet connected */
454 th = h->connect_ready_head;
456 return; /* no request pending */
457 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
459 /* remove existing time out task, will be integrated
460 with transmit_ready notification! */
461 GNUNET_SCHEDULER_cancel (h->sched,
462 th->notify_delay_task);
463 th->notify_delay_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
465 h->transmission_scheduled = GNUNET_YES;
466 h->network_handle = GNUNET_CLIENT_notify_transmit_ready (h->client,
468 GNUNET_TIME_absolute_get_remaining
470 &transport_notify_ready,
472 GNUNET_assert (NULL != h->network_handle);
477 * Insert the given transmit handle in the given sorted
478 * doubly linked list based on timeout.
480 * @param head pointer to the head of the linked list
481 * @param th element to insert into the list
484 insert_transmit_handle (struct GNUNET_TRANSPORT_TransmitHandle **head,
485 struct GNUNET_TRANSPORT_TransmitHandle *th)
487 struct GNUNET_TRANSPORT_TransmitHandle *pos;
488 struct GNUNET_TRANSPORT_TransmitHandle *prev;
492 while ((pos != NULL) && (pos->timeout.value < th->timeout.value))
500 if (th->next != NULL)
516 * Cancel a pending notify delay task (if pending) and also remove the
517 * given transmit handle from whatever list is on.
519 * @param th handle for the transmission request to manipulate
522 remove_from_any_list (struct GNUNET_TRANSPORT_TransmitHandle *th)
524 struct GNUNET_TRANSPORT_Handle *h;
527 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
529 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
530 th->notify_delay_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
532 if (th->prev == NULL)
534 if (th == h->connect_wait_head)
535 h->connect_wait_head = th->next;
537 h->connect_ready_head = th->next;
541 th->prev->next = th->next;
543 if (th->next != NULL)
544 th->next->prev = th->prev;
549 * Called when our transmit request timed out before any transport
550 * reported success connecting to the desired peer or before the
551 * transport was ready to receive. Signal error and free
555 transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
557 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
559 th->notify_delay_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
560 if (th->neighbour != NULL)
561 th->neighbour->transmit_handle = NULL;
563 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
564 "Request for transmission to peer `%s' timed out.\n",
565 GNUNET_i2s(&th->target));
567 remove_from_any_list (th);
568 th->notify (th->notify_cls, 0, NULL);
576 * Queue control request for transmission to the transport
579 * @param size number of bytes to be transmitted
580 * @param at_head request must be added to the head of the queue
581 * (otherwise request will be appended)
582 * @param timeout how long this transmission can wait (at most)
583 * @param notify function to call to get the content
584 * @param notify_cls closure for notify
587 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h,
590 struct GNUNET_TIME_Relative timeout,
591 GNUNET_NETWORK_TransmitReadyNotify notify,
594 struct GNUNET_TRANSPORT_TransmitHandle *th;
596 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
599 th->notify_cls = notify_cls;
600 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
601 th->notify_size = size;
602 th->notify_delay_task
603 = GNUNET_SCHEDULER_add_delayed (h->sched,
605 GNUNET_SCHEDULER_PRIORITY_KEEP,
606 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
608 &transmit_timeout, th);
611 th->next = h->connect_ready_head;
612 h->connect_ready_head = th;
613 if (th->next != NULL)
618 insert_transmit_handle (&h->connect_ready_head, th);
620 if (GNUNET_NO == h->transmission_scheduled)
621 schedule_transmission (h);
626 * Update the quota values for the given neighbour now.
629 update_quota (struct NeighbourList *n)
631 struct GNUNET_TIME_Relative delta;
635 delta = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
636 allowed = delta.value * n->quota_out;
637 if (n->last_sent < allowed)
639 remaining = allowed - n->last_sent;
640 if (n->quota_out > 0)
641 remaining /= n->quota_out;
644 if (remaining > MAX_BANDWIDTH_CARRY)
645 remaining = MAX_BANDWIDTH_CARRY;
647 n->last_quota_update = GNUNET_TIME_absolute_get ();
648 n->last_quota_update.value -= remaining;
652 n->last_sent -= allowed;
653 n->last_quota_update = GNUNET_TIME_absolute_get ();
658 struct SetQuotaContext
660 struct GNUNET_TRANSPORT_Handle *handle;
662 struct GNUNET_PeerIdentity target;
664 GNUNET_SCHEDULER_Task cont;
668 struct GNUNET_TIME_Absolute timeout;
675 send_set_quota (void *cls, size_t size, void *buf)
677 struct SetQuotaContext *sqc = cls;
678 struct QuotaSetMessage *msg;
682 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
686 GNUNET_SCHEDULER_REASON_TIMEOUT);
691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
692 "Transmitting `%s' request with respect to `%4s'.\n",
693 "SET_QUOTA", GNUNET_i2s (&sqc->target));
695 GNUNET_assert (size >= sizeof (struct QuotaSetMessage));
697 msg->header.size = htons (sizeof (struct QuotaSetMessage));
698 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
699 msg->quota_in = htonl (sqc->quota_in);
700 memcpy (&msg->peer, &sqc->target, sizeof (struct GNUNET_PeerIdentity));
701 if (sqc->cont != NULL)
702 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
706 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
708 return sizeof (struct QuotaSetMessage);
713 * Set the share of incoming bandwidth for the given
714 * peer to the specified amount.
716 * @param handle connection to transport service
717 * @param target who's bandwidth quota is being changed
718 * @param quota_in incoming bandwidth quota in bytes per ms; 0 can
719 * be used to force all traffic to be discarded
720 * @param quota_out outgoing bandwidth quota in bytes per ms; 0 can
721 * be used to force all traffic to be discarded
722 * @param timeout how long to wait until signaling failure if
723 * we can not communicate the quota change
724 * @param cont continuation to call when done, will be called
725 * either with reason "TIMEOUT" or with reason "PREREQ_DONE"
726 * @param cont_cls closure for continuation
729 GNUNET_TRANSPORT_set_quota (struct GNUNET_TRANSPORT_Handle *handle,
730 const struct GNUNET_PeerIdentity *target,
733 struct GNUNET_TIME_Relative timeout,
734 GNUNET_SCHEDULER_Task cont, void *cont_cls)
736 struct NeighbourList *n;
737 struct SetQuotaContext *sqc;
739 n = find_neighbour (handle, target);
743 if (n->quota_out < quota_out)
744 n->last_quota_update = GNUNET_TIME_absolute_get ();
745 n->quota_out = quota_out;
747 sqc = GNUNET_malloc (sizeof (struct SetQuotaContext));
748 sqc->handle = handle;
749 sqc->target = *target;
751 sqc->cont_cls = cont_cls;
752 sqc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
753 sqc->quota_in = quota_in;
754 schedule_control_transmit (handle,
755 sizeof (struct QuotaSetMessage),
756 GNUNET_NO, timeout, &send_set_quota, sqc);
761 * A "get_hello" request has timed out. Signal the client
765 hello_wait_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
767 struct HelloWaitList *hwl = cls;
768 struct HelloWaitList *pos;
769 struct HelloWaitList *prev;
772 pos = hwl->handle->hwl_head;
775 GNUNET_assert (pos != NULL);
780 hwl->handle->hwl_head = hwl->next;
782 prev->next = hwl->next;
783 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
784 _("Timeout trying to obtain `%s' from transport service.\n"),
787 if (hwl->rec != NULL)
788 hwl->rec (hwl->rec_cls, GNUNET_TIME_UNIT_ZERO, NULL, NULL);
794 * Obtain the HELLO message for this peer.
796 * @param handle connection to transport service
797 * @param timeout how long to wait for the HELLO
798 * @param rec function to call with the HELLO, sender will be our peer
799 * identity; message and sender will be NULL on timeout
800 * (handshake with transport service pending/failed).
801 * cost estimate will be 0.
802 * @param rec_cls closure for rec
805 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
806 struct GNUNET_TIME_Relative timeout,
807 GNUNET_TRANSPORT_ReceiveCallback rec,
810 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
811 struct GNUNET_PeerIdentity me;
812 struct HelloWaitList *hwl;
814 if (handle->my_hello == NULL)
816 hwl = GNUNET_malloc (sizeof (struct HelloWaitList));
817 hwl->next = handle->hwl_head;
818 handle->hwl_head = hwl;
819 hwl->handle = handle;
821 hwl->rec_cls = rec_cls;
822 hwl->timeout = GNUNET_TIME_relative_to_absolute (timeout);
823 hwl->task = GNUNET_SCHEDULER_add_delayed (handle->sched,
825 GNUNET_SCHEDULER_PRIORITY_KEEP,
826 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
828 &hello_wait_timeout, hwl);
831 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_key (handle->my_hello, &pk));
832 GNUNET_CRYPTO_hash (&pk,
833 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
837 GNUNET_TIME_UNIT_ZERO,
838 &me, (const struct GNUNET_MessageHeader *) handle->my_hello);
843 send_hello (void *cls, size_t size, void *buf)
845 struct GNUNET_MessageHeader *hello = cls;
851 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
852 "Timeout while trying to transmit `%s' request.\n",
859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
860 "Transmitting `%s' request.\n", "HELLO");
862 msize = ntohs (hello->size);
863 GNUNET_assert (size >= msize);
864 memcpy (buf, hello, msize);
871 * Offer the transport service the HELLO of another peer. Note that
872 * the transport service may just ignore this message if the HELLO is
873 * malformed or useless due to our local configuration.
875 * @param handle connection to transport service
876 * @param hello the hello message
879 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
880 const struct GNUNET_MessageHeader *hello)
882 struct GNUNET_MessageHeader *hc;
885 if (handle->client == NULL)
888 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
889 "Not connected to transport service, dropping offered HELLO\n");
893 GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
894 size = ntohs (hello->size);
895 GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
896 hc = GNUNET_malloc (size);
897 memcpy (hc, hello, size);
898 schedule_control_transmit (handle,
900 GNUNET_NO, OFFER_HELLO_TIMEOUT, &send_hello, hc);
905 * Function we use for handling incoming messages.
907 static void demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg);
911 send_start (void *cls, size_t size, void *buf)
913 struct GNUNET_MessageHeader *s = buf;
918 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
919 "Timeout while trying to transmit `%s' request.\n",
925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
926 "Transmitting `%s' request.\n", "START");
928 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
929 s->size = htons (sizeof (struct GNUNET_MessageHeader));
930 s->type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
931 return sizeof (struct GNUNET_MessageHeader);
936 * We're ready to transmit the request that the transport service
937 * should connect to a new peer. In addition to sending the
938 * request, schedule the next phase for the transmission processing
939 * that caused the connect request in the first place.
942 request_connect (void *cls, size_t size, void *buf)
944 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
945 struct TryConnectMessage *tcm;
946 struct GNUNET_TRANSPORT_Handle *h;
948 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK);
953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
954 "Failed to transmit `%s' request for `%4s' to service.\n",
956 GNUNET_i2s(&th->target));
958 th->notify (th->notify_cls, 0, NULL);
963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
964 "Transmitting `%s' message for `%4s'.\n",
965 "TRY_CONNECT", GNUNET_i2s (&th->target));
967 GNUNET_assert (size >= sizeof (struct TryConnectMessage));
969 tcm->header.size = htons (sizeof (struct TryConnectMessage));
970 tcm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TRY_CONNECT);
971 tcm->reserved = htonl (0);
972 memcpy (&tcm->peer, &th->target, sizeof (struct GNUNET_PeerIdentity));
973 th->notify_delay_task
974 = GNUNET_SCHEDULER_add_delayed (h->sched,
976 GNUNET_SCHEDULER_PRIORITY_KEEP,
977 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
978 GNUNET_TIME_absolute_get_remaining
979 (th->timeout), &transmit_timeout, th);
980 insert_transmit_handle (&h->connect_wait_head, th);
981 return sizeof (struct TryConnectMessage);
986 * Schedule a request to connect to the given
987 * neighbour (and if successful, add the specified
988 * handle to the wait list).
990 * @param th handle for a request to transmit once we
994 try_connect (struct GNUNET_TRANSPORT_TransmitHandle *th)
996 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK);
997 schedule_control_transmit (th->handle,
998 sizeof (struct TryConnectMessage),
1000 GNUNET_TIME_absolute_get_remaining (th->timeout),
1001 &request_connect, th);
1006 * Remove neighbour from our list
1009 remove_neighbour (struct GNUNET_TRANSPORT_Handle *h,
1010 const struct GNUNET_PeerIdentity *peer)
1012 struct NeighbourList *prev;
1013 struct NeighbourList *pos;
1014 struct GNUNET_TRANSPORT_TransmitHandle *th;
1017 pos = h->neighbours;
1018 while ((pos != NULL) &&
1019 (0 != memcmp (peer, &pos->id, sizeof (struct GNUNET_PeerIdentity))))
1030 h->neighbours = pos->next;
1032 prev->next = pos->next;
1033 if (NULL != (th = pos->transmit_handle))
1035 pos->transmit_handle = NULL;
1036 th->neighbour = NULL;
1037 remove_from_any_list (th);
1040 if (h->nc_cb != NULL)
1041 h->nd_cb (h->cls, peer);
1047 * Try again to connect to transport service.
1050 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1052 struct GNUNET_TRANSPORT_Handle *h = cls;
1053 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1054 struct NeighbourList *n;
1056 /* Forget about all neighbours that we used to be connected
1058 while (NULL != (n = h->neighbours))
1059 remove_neighbour (h, &n->id);
1061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n");
1063 GNUNET_assert (h->client == NULL);
1064 h->reconnect_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1065 h->client = GNUNET_CLIENT_connect (h->sched, "transport", h->cfg);
1066 GNUNET_assert (h->client != NULL);
1067 /* make sure we don't send "START" twice,
1068 remove existing entry from queue (if present) */
1069 pos = h->connect_ready_head;
1072 if (pos->notify == &send_start)
1074 if (pos->prev == NULL)
1075 h->connect_ready_head = pos->next;
1077 pos->prev->next = pos->next;
1078 if (pos->next != NULL)
1079 pos->next->prev = pos->prev;
1080 GNUNET_assert (pos->neighbour == NULL);
1086 schedule_control_transmit (h,
1087 sizeof (struct GNUNET_MessageHeader),
1089 GNUNET_TIME_UNIT_FOREVER_REL, &send_start, NULL);
1090 GNUNET_CLIENT_receive (h->client,
1091 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1096 * Function that will schedule the job that will try
1097 * to connect us again to the client.
1100 schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
1103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1104 "Scheduling task to reconnect to transport service in %llu ms.\n",
1105 h->reconnect_delay.value);
1107 GNUNET_assert (h->client == NULL);
1108 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK);
1110 = GNUNET_SCHEDULER_add_delayed (h->sched,
1112 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1113 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1114 h->reconnect_delay, &reconnect, h);
1115 h->reconnect_delay = GNUNET_TIME_UNIT_SECONDS;
1120 * We are connected to the respective peer, check the
1121 * bandwidth limits and schedule the transmission.
1123 static void schedule_request (struct GNUNET_TRANSPORT_TransmitHandle *th);
1127 * Function called by the scheduler when the timeout
1128 * for bandwidth availablility for the target
1129 * neighbour is reached.
1132 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1134 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
1136 th->notify_delay_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1137 schedule_request (th);
1142 * Remove the given transmit handle from the wait list. Does NOT free
1146 remove_from_wait_list (struct GNUNET_TRANSPORT_TransmitHandle *th)
1148 if (th->prev == NULL)
1149 th->handle->connect_wait_head = th->next;
1151 th->prev->next = th->next;
1152 if (th->next != NULL)
1153 th->next->prev = th->prev;
1158 * We are connected to the respective peer, check the
1159 * bandwidth limits and schedule the transmission.
1162 schedule_request (struct GNUNET_TRANSPORT_TransmitHandle *th)
1164 struct GNUNET_TRANSPORT_Handle *h;
1165 struct GNUNET_TIME_Relative duration;
1166 struct NeighbourList *n;
1171 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1173 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
1174 th->notify_delay_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1176 /* check outgoing quota */
1177 duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
1178 if (duration.value > MIN_QUOTA_REFRESH_TIME)
1181 duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
1183 available = duration.value * n->quota_out;
1184 if (available < n->last_sent + th->notify_size)
1186 /* calculate how much bandwidth we'd still need to
1187 accumulate and based on that how long we'll have
1189 available = n->last_sent + th->notify_size - available;
1190 duration = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1191 available / n->quota_out);
1192 if (th->timeout.value <
1193 GNUNET_TIME_relative_to_absolute (duration).value)
1195 /* signal timeout! */
1197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1198 "Would need %llu ms before bandwidth is available for delivery to `%4s', that is too long. Signaling timeout.\n",
1200 GNUNET_i2s(&th->target));
1202 remove_from_wait_list (th);
1203 th->notify (th->notify_cls, 0, NULL);
1208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1209 "Need more bandwidth, delaying delivery to `%4s' by %llu ms\n",
1210 GNUNET_i2s(&th->target),
1213 th->notify_delay_task
1214 = GNUNET_SCHEDULER_add_delayed (h->sched,
1216 GNUNET_SCHEDULER_PRIORITY_KEEP,
1217 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1218 duration, &transmit_ready, th);
1222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1223 "Bandwidth available for transmission to `%4s'\n",
1224 GNUNET_i2s (&n->id));
1226 if (GNUNET_NO == n->transmit_ok)
1228 /* we may be ready, but transport service is not;
1229 wait for SendOkMessage or timeout */
1231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1232 "Need to wait for transport service `%s' message\n",
1235 th->notify_delay_task
1236 = GNUNET_SCHEDULER_add_delayed (h->sched,
1238 GNUNET_SCHEDULER_PRIORITY_KEEP,
1239 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1240 GNUNET_TIME_absolute_get_remaining
1241 (th->timeout), &transmit_timeout, th);
1244 n->transmit_ok = GNUNET_NO;
1245 remove_from_wait_list (th);
1247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Moving message for `%4s' to ready list\n",
1248 GNUNET_i2s(&n->id));
1250 insert_transmit_handle (&h->connect_ready_head, th);
1251 if (GNUNET_NO == h->transmission_scheduled)
1252 schedule_transmission (h);
1257 * Add neighbour to our list
1260 add_neighbour (struct GNUNET_TRANSPORT_Handle *h,
1262 struct GNUNET_TIME_Relative latency,
1263 const struct GNUNET_PeerIdentity *pid)
1265 struct NeighbourList *n;
1266 struct GNUNET_TRANSPORT_TransmitHandle *prev;
1267 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1268 struct GNUNET_TRANSPORT_TransmitHandle *next;
1270 /* check for duplicates */
1271 if (NULL != find_neighbour (h, pid))
1277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1278 "Creating entry for new neighbour `%4s'.\n", GNUNET_i2s (pid));
1280 n = GNUNET_malloc (sizeof (struct NeighbourList));
1282 n->last_quota_update = GNUNET_TIME_absolute_get ();
1283 n->quota_out = quota_out;
1284 n->next = h->neighbours;
1285 n->transmit_ok = GNUNET_YES;
1287 if (h->nc_cb != NULL)
1288 h->nc_cb (h->cls, &n->id, latency);
1290 pos = h->connect_wait_head;
1294 if (0 == memcmp (pid,
1295 &pos->target, sizeof (struct GNUNET_PeerIdentity)))
1298 GNUNET_assert (NULL == n->transmit_handle);
1299 n->transmit_handle = pos;
1301 h->connect_wait_head = next;
1304 if (GNUNET_YES == n->received_ack)
1307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1308 "Found pending request for `%4s' will trigger it now.\n",
1309 GNUNET_i2s (&pos->target));
1311 if (pos->notify_delay_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1313 GNUNET_SCHEDULER_cancel (h->sched, pos->notify_delay_task);
1314 pos->notify_delay_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1316 schedule_request (pos);
1321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1322 "Found pending request for `%4s' but still need `%s' before proceeding.\n",
1323 GNUNET_i2s (&pos->target),
1336 * Connect to the transport service. Note that the connection may
1337 * complete (or fail) asynchronously.
1340 * @param sched scheduler to use
1341 * @param cfg configuration to use
1342 * @param cls closure for the callbacks
1343 * @param rec receive function to call
1344 * @param nc function to call on connect events
1345 * @param dc function to call on disconnect events
1347 struct GNUNET_TRANSPORT_Handle *
1348 GNUNET_TRANSPORT_connect (struct GNUNET_SCHEDULER_Handle *sched,
1349 struct GNUNET_CONFIGURATION_Handle *cfg,
1351 GNUNET_TRANSPORT_ReceiveCallback rec,
1352 GNUNET_TRANSPORT_NotifyConnect nc,
1353 GNUNET_TRANSPORT_NotifyDisconnect nd)
1355 struct GNUNET_TRANSPORT_Handle *ret;
1357 GNUNET_ARM_start_service ("peerinfo",
1358 cfg, sched, START_SERVICE_TIMEOUT, NULL, NULL);
1359 GNUNET_ARM_start_service ("transport",
1360 cfg, sched, START_SERVICE_TIMEOUT, NULL, NULL);
1361 ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Handle));
1368 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1369 schedule_reconnect (ret);
1375 * These stop activities must be run in a fresh
1376 * scheduler that is NOT in shutdown mode.
1379 stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1381 struct GNUNET_TRANSPORT_Handle *handle = cls;
1382 GNUNET_ARM_stop_service ("transport",
1384 tc->sched, STOP_SERVICE_TIMEOUT, NULL, NULL);
1385 GNUNET_ARM_stop_service ("peerinfo",
1387 tc->sched, STOP_SERVICE_TIMEOUT, NULL, NULL);
1392 * Disconnect from the transport service.
1395 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1397 struct GNUNET_TRANSPORT_TransmitHandle *th;
1398 struct NeighbourList *n;
1399 struct HelloWaitList *hwl;
1400 struct GNUNET_CLIENT_Connection *client;
1403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transport disconnect called!\n");
1405 while (NULL != (th = handle->connect_ready_head))
1407 handle->connect_ready_head = th->next;
1408 th->notify (th->notify_cls, 0, NULL);
1411 while (NULL != (th = handle->connect_wait_head))
1413 handle->connect_wait_head = th->next;
1414 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1416 GNUNET_SCHEDULER_cancel (handle->sched, th->notify_delay_task);
1417 th->notify_delay_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1419 th->notify (th->notify_cls, 0, NULL);
1422 while (NULL != (n = handle->neighbours))
1424 handle->neighbours = n->next;
1427 while (NULL != (hwl = handle->hwl_head))
1429 handle->hwl_head = hwl->next;
1430 GNUNET_SCHEDULER_cancel (handle->sched, hwl->task);
1431 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1432 _("Disconnect while trying to obtain `%s' from transport service.\n"),
1434 if (hwl->rec != NULL)
1435 hwl->rec (hwl->rec_cls, GNUNET_TIME_UNIT_ZERO, NULL, NULL);
1438 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1440 GNUNET_SCHEDULER_cancel (handle->sched, handle->reconnect_task);
1441 handle->reconnect_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1443 GNUNET_free_non_null (handle->my_hello);
1444 handle->my_hello = NULL;
1445 GNUNET_SCHEDULER_run (&stop_task, handle);
1446 if (NULL != (client = handle->client))
1449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1450 "Disconnecting from transport service for good.\n");
1452 handle->client = NULL;
1453 GNUNET_CLIENT_disconnect (client);
1456 GNUNET_free (handle);
1461 * Type of a function to call when we receive a message
1464 * @param cls closure
1465 * @param msg message received, NULL on timeout or fatal error
1468 demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
1470 struct GNUNET_TRANSPORT_Handle *h = cls;
1471 const struct DisconnectInfoMessage *dim;
1472 const struct ConnectInfoMessage *cim;
1473 const struct InboundMessage *im;
1474 const struct GNUNET_MessageHeader *imm;
1475 const struct SendOkMessage *okm;
1476 struct HelloWaitList *hwl;
1477 struct NeighbourList *n;
1478 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
1479 struct GNUNET_PeerIdentity me;
1480 struct GNUNET_TRANSPORT_TransmitHandle *th;
1483 if ((msg == NULL) || (h->client == NULL))
1485 if (h->client != NULL)
1488 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1489 "Error receiving from transport service, disconnecting temporarily.\n");
1491 if (h->network_handle != NULL)
1493 GNUNET_NETWORK_notify_transmit_ready_cancel (h->network_handle);
1494 h->network_handle = NULL;
1495 h->transmission_scheduled = GNUNET_NO;
1496 th = h->connect_ready_head;
1497 /* add timeout again, we cancelled the transmit_ready task! */
1498 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK);
1499 th->notify_delay_task
1500 = GNUNET_SCHEDULER_add_delayed (h->sched,
1502 GNUNET_SCHEDULER_PRIORITY_KEEP,
1503 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1504 GNUNET_TIME_absolute_get_remaining(th->timeout),
1508 GNUNET_CLIENT_disconnect (h->client);
1510 schedule_reconnect (h);
1514 /* shutdown initiated from 'GNUNET_TRANSPORT_disconnect',
1515 finish clean up work! */
1520 GNUNET_CLIENT_receive (h->client,
1521 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1522 size = ntohs (msg->size);
1523 switch (ntohs (msg->type))
1525 case GNUNET_MESSAGE_TYPE_HELLO:
1527 GNUNET_HELLO_get_key ((const struct GNUNET_HELLO_Message *) msg,
1533 GNUNET_CRYPTO_hash (&pkey,
1535 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1539 "Receiving (my own) `%s' message, I am `%4s'.\n",
1540 "HELLO", GNUNET_i2s (&me));
1542 GNUNET_free_non_null (h->my_hello);
1544 if (size < sizeof (struct GNUNET_MessageHeader))
1549 h->my_hello = GNUNET_malloc (size);
1550 memcpy (h->my_hello, msg, size);
1551 while (NULL != (hwl = h->hwl_head))
1553 h->hwl_head = hwl->next;
1554 GNUNET_SCHEDULER_cancel (h->sched, hwl->task);
1555 GNUNET_TRANSPORT_get_hello (h,
1556 GNUNET_TIME_UNIT_ZERO,
1557 hwl->rec, hwl->rec_cls);
1561 case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
1562 if (size != sizeof (struct ConnectInfoMessage))
1567 cim = (const struct ConnectInfoMessage *) msg;
1569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1570 "Receiving `%s' message for `%4s'.\n",
1571 "CONNECT", GNUNET_i2s (&cim->id));
1574 ntohl (cim->quota_out),
1575 GNUNET_TIME_relative_ntoh (cim->latency), &cim->id);
1577 case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
1578 if (size != sizeof (struct DisconnectInfoMessage))
1583 dim = (const struct DisconnectInfoMessage *) msg;
1585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1586 "Receiving `%s' message for `%4s'.\n",
1587 "DISCONNECT", GNUNET_i2s (&dim->peer));
1589 remove_neighbour (h, &dim->peer);
1591 case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
1592 if (size != sizeof (struct SendOkMessage))
1597 okm = (const struct SendOkMessage *) msg;
1599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1600 "Receiving `%s' message, transmission %s.\n", "SEND_OK",
1601 ntohl(okm->success) == GNUNET_OK ? "succeeded" : "failed");
1603 n = find_neighbour (h, &okm->peer);
1604 GNUNET_assert (n != NULL);
1605 n->transmit_ok = GNUNET_YES;
1606 if (n->transmit_handle != NULL)
1609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1610 "Processing pending message\n");
1612 GNUNET_SCHEDULER_cancel (h->sched,
1613 n->transmit_handle->notify_delay_task);
1614 n->transmit_handle->notify_delay_task =
1615 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1616 GNUNET_assert (GNUNET_YES == n->received_ack);
1617 schedule_request (n->transmit_handle);
1620 case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
1622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1623 "Receiving `%s' message.\n", "RECV");
1626 sizeof (struct InboundMessage) +
1627 sizeof (struct GNUNET_MessageHeader))
1632 im = (const struct InboundMessage *) msg;
1633 imm = (const struct GNUNET_MessageHeader *) &im[1];
1634 if (ntohs (imm->size) + sizeof (struct InboundMessage) != size)
1639 switch (ntohs (imm->type))
1641 case GNUNET_MESSAGE_TYPE_TRANSPORT_ACK:
1643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1644 "Receiving `%s' message from `%4s'.\n",
1645 "ACK", GNUNET_i2s (&im->peer));
1647 n = find_neighbour (h, &im->peer);
1653 if (n->received_ack == GNUNET_NO)
1655 n->received_ack = GNUNET_YES;
1656 if (NULL != n->transmit_handle)
1659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1660 "Peer connected, scheduling delayed message for deliverery now.\n");
1662 schedule_request (n->transmit_handle);
1668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1669 "Received message of type %u from `%4s'.\n",
1670 ntohs (imm->type), GNUNET_i2s (&im->peer));
1674 GNUNET_TIME_relative_ntoh (im->latency), &im->peer, imm);
1679 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1681 ("Received unexpected message of type %u in %s:%u\n"),
1682 ntohs (msg->type), __FILE__, __LINE__);
1689 struct ClientTransmitWrapper
1691 GNUNET_NETWORK_TransmitReadyNotify notify;
1693 struct GNUNET_TRANSPORT_TransmitHandle *th;
1698 * Transmit message of a client destined for another
1699 * peer to the service.
1702 client_notify_wrapper (void *cls, size_t size, void *buf)
1704 struct ClientTransmitWrapper *ctw = cls;
1705 struct OutboundMessage *obm;
1706 struct GNUNET_MessageHeader *hdr;
1712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1713 "Transmission request could not be satisfied.\n");
1715 ret = ctw->notify (ctw->notify_cls, 0, NULL);
1716 GNUNET_assert (ret == 0);
1720 GNUNET_assert (size >= sizeof (struct OutboundMessage));
1722 ret = ctw->notify (ctw->notify_cls,
1723 size - sizeof (struct OutboundMessage),
1727 /* Need to reset flag, no SEND means no SEND_OK! */
1728 ctw->th->neighbour->transmit_ok = GNUNET_YES;
1732 GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
1733 hdr = (struct GNUNET_MessageHeader *) &obm[1];
1734 GNUNET_assert (ntohs (hdr->size) == ret);
1735 GNUNET_assert (ret + sizeof (struct OutboundMessage) <
1736 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1739 "Transmitting `%s' message with data for `%4s'\n",
1740 "SEND", GNUNET_i2s (&ctw->th->target));
1742 ret += sizeof (struct OutboundMessage);
1743 obm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
1744 obm->header.size = htons (ret);
1745 obm->priority = htonl (ctw->th->priority);
1746 obm->peer = ctw->th->target;
1754 * Check if we could queue a message of the given size for
1755 * transmission. The transport service will take both its
1756 * internal buffers and bandwidth limits imposed by the
1757 * other peer into consideration when answering this query.
1759 * @param handle connection to transport service
1760 * @param target who should receive the message
1761 * @param size how big is the message we want to transmit?
1762 * @param priority how important is the message?
1763 * @param timeout after how long should we give up (and call
1764 * notify with buf NULL and size 0)?
1765 * @param notify function to call when we are ready to
1766 * send such a message
1767 * @param notify_cls closure for notify
1768 * @return NULL if someone else is already waiting to be notified
1769 * non-NULL if the notify callback was queued (can be used to cancel
1770 * using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1772 struct GNUNET_TRANSPORT_TransmitHandle *
1773 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle
1775 const struct GNUNET_PeerIdentity
1776 *target, size_t size,
1777 unsigned int priority,
1778 struct GNUNET_TIME_Relative timeout,
1779 GNUNET_NETWORK_TransmitReadyNotify
1780 notify, void *notify_cls)
1782 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1783 struct GNUNET_TRANSPORT_TransmitHandle *th;
1784 struct NeighbourList *n;
1785 struct ClientTransmitWrapper *ctw;
1787 if (size + sizeof (struct OutboundMessage) >=
1788 GNUNET_SERVER_MAX_MESSAGE_SIZE)
1794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1795 "Asking transport service for transmission of %u bytes to peer `%4s'.\n",
1796 size, GNUNET_i2s (target));
1798 n = find_neighbour (handle, target);
1800 (n->transmit_handle != NULL) )
1801 return NULL; /* already have a request pending for this peer! */
1802 ctw = GNUNET_malloc (sizeof (struct ClientTransmitWrapper));
1803 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
1804 ctw->notify = notify;
1805 ctw->notify_cls = notify_cls;
1807 th->handle = handle;
1809 th->target = *target;
1810 th->notify = &client_notify_wrapper;
1811 th->notify_cls = ctw;
1812 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1813 th->notify_size = size + sizeof (struct OutboundMessage);
1814 th->priority = priority;
1817 pos = handle->connect_wait_head;
1820 GNUNET_assert (0 != memcmp (target,
1822 sizeof (struct GNUNET_PeerIdentity)));
1826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1827 "Will now try to connect to `%4s'.\n", GNUNET_i2s (target));
1834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1835 "Transmission request queued for transmission to transport service.\n");
1837 GNUNET_assert (NULL == n->transmit_handle);
1838 n->transmit_handle = th;
1839 if (GNUNET_YES != n->received_ack)
1842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1843 "Connection to `%4s' is not yet confirmed connected, scheduling timeout (%llu ms) only.\n",
1844 GNUNET_i2s (target), timeout.value);
1846 th->notify_delay_task
1847 = GNUNET_SCHEDULER_add_delayed (handle->sched,
1849 GNUNET_SCHEDULER_PRIORITY_KEEP,
1850 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1851 timeout, &transmit_timeout, th);
1856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1857 "Peer `%4s' is ready to receive, scheduling message for delivery now.\n",
1858 GNUNET_i2s (target));
1860 schedule_request (th);
1866 * Cancel the specified transmission-ready
1870 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
1871 GNUNET_TRANSPORT_TransmitHandle
1874 struct GNUNET_TRANSPORT_Handle *h;
1877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1878 "Transmission request of %u bytes to `%4s' was cancelled.\n",
1879 th->notify_size - sizeof(struct OutboundMessage),
1880 GNUNET_i2s (&th->target));
1882 GNUNET_assert (th->notify == &client_notify_wrapper);
1883 remove_from_any_list (th);
1885 if ((h->connect_ready_head == NULL) && (h->network_handle != NULL))
1887 GNUNET_NETWORK_notify_transmit_ready_cancel (h->network_handle);
1888 h->network_handle = NULL;
1889 h->transmission_scheduled = GNUNET_NO;
1891 GNUNET_free (th->notify_cls);
1896 /* end of transport_api.c */