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 * Global 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?
231 * Handle for the transport service (includes all of the
232 * state for the transport service).
234 struct GNUNET_TRANSPORT_Handle
238 * Closure for the callbacks.
243 * Function to call for received data.
245 GNUNET_TRANSPORT_ReceiveCallback rec;
248 * function to call on connect events
250 GNUNET_TRANSPORT_NotifyConnect nc_cb;
253 * function to call on disconnect events
255 GNUNET_TRANSPORT_NotifyDisconnect nd_cb;
258 * The current HELLO message for this peer. Updated
259 * whenever transports change their addresses.
261 struct GNUNET_HELLO_Message *my_hello;
264 * My client connection to the transport service.
266 struct GNUNET_CLIENT_Connection *client;
269 * Handle to our registration with the client for notification.
271 struct GNUNET_NETWORK_TransmitHandle *network_handle;
274 * Linked list of transmit handles that are waiting for the
275 * transport to connect to the respective peer. When we
276 * receive notification that the transport connected to a
277 * peer, we go over this list and check if someone has already
278 * requested a transmission to the new peer; if so, we trigger
281 struct GNUNET_TRANSPORT_TransmitHandle *connect_wait_head;
284 * Linked list of transmit handles that are waiting for the
285 * transport to be ready for transmission to the respective
287 * receive notification that the transport disconnected from
288 * a peer, we go over this list and move the entry back to
289 * the connect_wait list.
291 struct GNUNET_TRANSPORT_TransmitHandle *connect_ready_head;
294 * Linked list of pending requests for our HELLO.
296 struct HelloWaitList *hwl_head;
301 struct GNUNET_SCHEDULER_Handle *sched;
306 struct GNUNET_CONFIGURATION_Handle *cfg;
309 * Linked list of the current neighbours of this peer.
311 struct NeighbourList *neighbours;
314 * ID of the task trying to reconnect to the
317 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
320 * Delay until we try to reconnect.
322 struct GNUNET_TIME_Relative reconnect_delay;
325 * Do we currently have a transmission pending?
326 * (schedule transmission was called but has not
329 int transmission_scheduled;
333 static struct NeighbourList *
334 find_neighbour (struct GNUNET_TRANSPORT_Handle *h,
335 const struct GNUNET_PeerIdentity *peer)
337 struct NeighbourList *pos;
340 while ((pos != NULL) &&
341 (0 != memcmp (peer, &pos->id, sizeof (struct GNUNET_PeerIdentity))))
348 * Schedule the task to send one message from the
349 * connect_ready list to the service.
351 static void schedule_transmission (struct GNUNET_TRANSPORT_Handle *h);
355 * Transmit message to client...
358 transport_notify_ready (void *cls, size_t size, void *buf)
360 struct GNUNET_TRANSPORT_Handle *h = cls;
361 struct GNUNET_TRANSPORT_TransmitHandle *th;
362 struct NeighbourList *n;
366 h->network_handle = NULL;
367 h->transmission_scheduled = GNUNET_NO;
371 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
372 "Could not transmit to transport service, cancelling pending requests\n");
374 th = h->connect_ready_head;
375 if (th->next != NULL)
376 th->next->prev = NULL;
377 h->connect_ready_head = th->next;
378 if (NULL != (n = th->neighbour))
380 GNUNET_assert (n->transmit_handle == th);
381 n->transmit_handle = NULL;
383 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
389 "Ready to transmit %u bytes to transport service\n", size);
393 h->network_handle = NULL;
394 h->transmission_scheduled = GNUNET_NO;
397 th = h->connect_ready_head;
398 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
400 /* remove existing time out task (only applies if
401 this is not the first iteration of the loop) */
402 GNUNET_SCHEDULER_cancel (h->sched,
403 th->notify_delay_task);
404 th->notify_delay_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
406 GNUNET_assert (th->notify_size <= size);
407 if (th->next != NULL)
408 th->next->prev = NULL;
409 h->connect_ready_head = th->next;
410 if (NULL != (n = th->neighbour))
412 GNUNET_assert (n->transmit_handle == th);
413 n->transmit_handle = NULL;
415 ret += th->notify (th->notify_cls, size, &cbuf[ret]);
421 while ((h->connect_ready_head != NULL) &&
422 (h->connect_ready_head->notify_size <= size));
423 if (h->connect_ready_head != NULL)
424 schedule_transmission (h);
426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
427 "Transmitting %u bytes to transport service\n", ret);
434 * Schedule the task to send one message from the
435 * connect_ready list to the service.
438 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
440 struct GNUNET_TRANSPORT_TransmitHandle *th;
442 GNUNET_assert (NULL == h->network_handle);
443 if (h->client == NULL)
445 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
446 "Could not yet schedule transmission: we are not yet connected to the transport service!\n");
447 return; /* not yet connected */
449 th = h->connect_ready_head;
451 return; /* no request pending */
452 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
454 /* remove existing time out task, will be integrated
455 with transmit_ready notification! */
456 GNUNET_SCHEDULER_cancel (h->sched,
457 th->notify_delay_task);
458 th->notify_delay_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
460 h->transmission_scheduled = GNUNET_YES;
461 h->network_handle = GNUNET_CLIENT_notify_transmit_ready (h->client,
463 GNUNET_TIME_absolute_get_remaining
465 &transport_notify_ready,
467 GNUNET_assert (NULL != h->network_handle);
472 * Insert the given transmit handle in the given sorted
473 * doubly linked list based on timeout.
475 * @param head pointer to the head of the linked list
476 * @param th element to insert into the list
479 insert_transmit_handle (struct GNUNET_TRANSPORT_TransmitHandle **head,
480 struct GNUNET_TRANSPORT_TransmitHandle *th)
482 struct GNUNET_TRANSPORT_TransmitHandle *pos;
483 struct GNUNET_TRANSPORT_TransmitHandle *prev;
487 while ((pos != NULL) && (pos->timeout.value < th->timeout.value))
495 if (th->next != NULL)
511 * Cancel a pending notify delay task (if pending) and also remove the
512 * given transmit handle from whatever list is on.
514 * @param th handle for the transmission request to manipulate
517 remove_from_any_list (struct GNUNET_TRANSPORT_TransmitHandle *th)
519 struct GNUNET_TRANSPORT_Handle *h;
522 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
524 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
525 th->notify_delay_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
527 if (th->prev == NULL)
529 if (th == h->connect_wait_head)
530 h->connect_wait_head = th->next;
532 h->connect_ready_head = th->next;
536 th->prev->next = th->next;
538 if (th->next != NULL)
539 th->next->prev = th->prev;
544 * Called when our transmit request timed out before any transport
545 * reported success connecting to the desired peer or before the
546 * transport was ready to receive. Signal error and free
550 transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
552 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
554 th->notify_delay_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
555 if (th->neighbour != NULL)
556 th->neighbour->transmit_handle = NULL;
558 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
559 "Request for transmission to peer `%s' timed out.\n",
560 GNUNET_i2s(&th->target));
562 remove_from_any_list (th);
563 th->notify (th->notify_cls, 0, NULL);
571 * Queue control request for transmission to the transport
574 * @param size number of bytes to be transmitted
575 * @param at_head request must be added to the head of the queue
576 * (otherwise request will be appended)
577 * @param timeout how long this transmission can wait (at most)
578 * @param notify function to call to get the content
579 * @param notify_cls closure for notify
582 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h,
585 struct GNUNET_TIME_Relative timeout,
586 GNUNET_NETWORK_TransmitReadyNotify notify,
589 struct GNUNET_TRANSPORT_TransmitHandle *th;
591 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
594 th->notify_cls = notify_cls;
595 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
596 th->notify_size = size;
597 th->notify_delay_task
598 = GNUNET_SCHEDULER_add_delayed (h->sched,
600 GNUNET_SCHEDULER_PRIORITY_KEEP,
601 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
603 &transmit_timeout, th);
606 th->next = h->connect_ready_head;
607 h->connect_ready_head = th;
608 if (th->next != NULL)
613 insert_transmit_handle (&h->connect_ready_head, th);
615 if (GNUNET_NO == h->transmission_scheduled)
616 schedule_transmission (h);
621 * Update the quota values for the given neighbour now.
624 update_quota (struct NeighbourList *n)
626 struct GNUNET_TIME_Relative delta;
630 delta = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
631 allowed = delta.value * n->quota_out;
632 if (n->last_sent < allowed)
634 remaining = allowed - n->last_sent;
635 if (n->quota_out > 0)
636 remaining /= n->quota_out;
639 if (remaining > MAX_BANDWIDTH_CARRY)
640 remaining = MAX_BANDWIDTH_CARRY;
642 n->last_quota_update = GNUNET_TIME_absolute_get ();
643 n->last_quota_update.value -= remaining;
647 n->last_sent -= allowed;
648 n->last_quota_update = GNUNET_TIME_absolute_get ();
653 struct SetQuotaContext
655 struct GNUNET_TRANSPORT_Handle *handle;
657 struct GNUNET_PeerIdentity target;
659 GNUNET_SCHEDULER_Task cont;
663 struct GNUNET_TIME_Absolute timeout;
670 send_set_quota (void *cls, size_t size, void *buf)
672 struct SetQuotaContext *sqc = cls;
673 struct QuotaSetMessage *msg;
677 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
681 GNUNET_SCHEDULER_REASON_TIMEOUT);
686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
687 "Transmitting `%s' request with respect to `%4s'.\n",
688 "SET_QUOTA", GNUNET_i2s (&sqc->target));
690 GNUNET_assert (size >= sizeof (struct QuotaSetMessage));
692 msg->header.size = htons (sizeof (struct QuotaSetMessage));
693 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
694 msg->quota_in = htonl (sqc->quota_in);
695 memcpy (&msg->peer, &sqc->target, sizeof (struct GNUNET_PeerIdentity));
696 if (sqc->cont != NULL)
697 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
701 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
703 return sizeof (struct QuotaSetMessage);
708 * Set the share of incoming bandwidth for the given
709 * peer to the specified amount.
711 * @param handle connection to transport service
712 * @param target who's bandwidth quota is being changed
713 * @param quota_in incoming bandwidth quota in bytes per ms; 0 can
714 * be used to force all traffic to be discarded
715 * @param quota_out outgoing bandwidth quota in bytes per ms; 0 can
716 * be used to force all traffic to be discarded
717 * @param timeout how long to wait until signaling failure if
718 * we can not communicate the quota change
719 * @param cont continuation to call when done, will be called
720 * either with reason "TIMEOUT" or with reason "PREREQ_DONE"
721 * @param cont_cls closure for continuation
724 GNUNET_TRANSPORT_set_quota (struct GNUNET_TRANSPORT_Handle *handle,
725 const struct GNUNET_PeerIdentity *target,
728 struct GNUNET_TIME_Relative timeout,
729 GNUNET_SCHEDULER_Task cont, void *cont_cls)
731 struct NeighbourList *n;
732 struct SetQuotaContext *sqc;
734 n = find_neighbour (handle, target);
738 if (n->quota_out < quota_out)
739 n->last_quota_update = GNUNET_TIME_absolute_get ();
740 n->quota_out = quota_out;
742 sqc = GNUNET_malloc (sizeof (struct SetQuotaContext));
743 sqc->handle = handle;
744 sqc->target = *target;
746 sqc->cont_cls = cont_cls;
747 sqc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
748 sqc->quota_in = quota_in;
749 schedule_control_transmit (handle,
750 sizeof (struct QuotaSetMessage),
751 GNUNET_NO, timeout, &send_set_quota, sqc);
756 * A "get_hello" request has timed out. Signal the client
760 hello_wait_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
762 struct HelloWaitList *hwl = cls;
763 struct HelloWaitList *pos;
764 struct HelloWaitList *prev;
767 pos = hwl->handle->hwl_head;
770 GNUNET_assert (pos != NULL);
775 hwl->handle->hwl_head = hwl->next;
777 prev->next = hwl->next;
778 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
779 _("Timeout trying to obtain `%s' from transport service.\n"),
782 if (hwl->rec != NULL)
783 hwl->rec (hwl->rec_cls, GNUNET_TIME_UNIT_ZERO, NULL, NULL);
789 * Obtain the HELLO message for this peer.
791 * @param handle connection to transport service
792 * @param timeout how long to wait for the HELLO
793 * @param rec function to call with the HELLO, sender will be our peer
794 * identity; message and sender will be NULL on timeout
795 * (handshake with transport service pending/failed).
796 * cost estimate will be 0.
797 * @param rec_cls closure for rec
800 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
801 struct GNUNET_TIME_Relative timeout,
802 GNUNET_TRANSPORT_ReceiveCallback rec,
805 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
806 struct GNUNET_PeerIdentity me;
807 struct HelloWaitList *hwl;
809 if (handle->my_hello == NULL)
811 hwl = GNUNET_malloc (sizeof (struct HelloWaitList));
812 hwl->next = handle->hwl_head;
813 handle->hwl_head = hwl;
814 hwl->handle = handle;
816 hwl->rec_cls = rec_cls;
817 hwl->timeout = GNUNET_TIME_relative_to_absolute (timeout);
818 hwl->task = GNUNET_SCHEDULER_add_delayed (handle->sched,
820 GNUNET_SCHEDULER_PRIORITY_KEEP,
821 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
823 &hello_wait_timeout, hwl);
826 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_key (handle->my_hello, &pk));
827 GNUNET_CRYPTO_hash (&pk,
828 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
832 GNUNET_TIME_UNIT_ZERO,
833 &me, (const struct GNUNET_MessageHeader *) handle->my_hello);
838 send_hello (void *cls, size_t size, void *buf)
840 struct GNUNET_MessageHeader *hello = cls;
846 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
847 "Timeout while trying to transmit `%s' request.\n",
854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
855 "Transmitting `%s' request.\n", "HELLO");
857 msize = ntohs (hello->size);
858 GNUNET_assert (size >= msize);
859 memcpy (buf, hello, msize);
866 * Offer the transport service the HELLO of another peer. Note that
867 * the transport service may just ignore this message if the HELLO is
868 * malformed or useless due to our local configuration.
870 * @param handle connection to transport service
871 * @param hello the hello message
874 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
875 const struct GNUNET_MessageHeader *hello)
877 struct GNUNET_MessageHeader *hc;
880 if (handle->client == NULL)
883 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
884 "Not connected to transport service, dropping offered HELLO\n");
888 GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
889 size = ntohs (hello->size);
890 GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
891 hc = GNUNET_malloc (size);
892 memcpy (hc, hello, size);
893 schedule_control_transmit (handle,
895 GNUNET_NO, OFFER_HELLO_TIMEOUT, &send_hello, hc);
900 * Function we use for handling incoming messages.
902 static void demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg);
906 send_start (void *cls, size_t size, void *buf)
908 struct GNUNET_MessageHeader *s = buf;
913 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
914 "Timeout while trying to transmit `%s' request.\n",
920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
921 "Transmitting `%s' request.\n", "START");
923 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
924 s->size = htons (sizeof (struct GNUNET_MessageHeader));
925 s->type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
926 return sizeof (struct GNUNET_MessageHeader);
931 * We're ready to transmit the request that the transport service
932 * should connect to a new peer. In addition to sending the
933 * request, schedule the next phase for the transmission processing
934 * that caused the connect request in the first place.
937 request_connect (void *cls, size_t size, void *buf)
939 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
940 struct TryConnectMessage *tcm;
941 struct GNUNET_TRANSPORT_Handle *h;
943 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK);
948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
949 "Failed to transmit `%s' request for `%4s' to service.\n",
951 GNUNET_i2s(&th->target));
953 th->notify (th->notify_cls, 0, NULL);
958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
959 "Transmitting `%s' message for `%4s'.\n",
960 "TRY_CONNECT", GNUNET_i2s (&th->target));
962 GNUNET_assert (size >= sizeof (struct TryConnectMessage));
964 tcm->header.size = htons (sizeof (struct TryConnectMessage));
965 tcm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TRY_CONNECT);
966 tcm->reserved = htonl (0);
967 memcpy (&tcm->peer, &th->target, sizeof (struct GNUNET_PeerIdentity));
968 th->notify_delay_task
969 = GNUNET_SCHEDULER_add_delayed (h->sched,
971 GNUNET_SCHEDULER_PRIORITY_KEEP,
972 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
973 GNUNET_TIME_absolute_get_remaining
974 (th->timeout), &transmit_timeout, th);
975 insert_transmit_handle (&h->connect_wait_head, th);
976 return sizeof (struct TryConnectMessage);
981 * Schedule a request to connect to the given
982 * neighbour (and if successful, add the specified
983 * handle to the wait list).
985 * @param th handle for a request to transmit once we
989 try_connect (struct GNUNET_TRANSPORT_TransmitHandle *th)
991 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK);
992 schedule_control_transmit (th->handle,
993 sizeof (struct TryConnectMessage),
995 GNUNET_TIME_absolute_get_remaining (th->timeout),
996 &request_connect, th);
1001 * Remove neighbour from our list
1004 remove_neighbour (struct GNUNET_TRANSPORT_Handle *h,
1005 const struct GNUNET_PeerIdentity *peer)
1007 struct NeighbourList *prev;
1008 struct NeighbourList *pos;
1009 struct GNUNET_TRANSPORT_TransmitHandle *th;
1012 pos = h->neighbours;
1013 while ((pos != NULL) &&
1014 (0 != memcmp (peer, &pos->id, sizeof (struct GNUNET_PeerIdentity))))
1025 h->neighbours = pos->next;
1027 prev->next = pos->next;
1028 if (NULL != (th = pos->transmit_handle))
1030 pos->transmit_handle = NULL;
1031 th->neighbour = NULL;
1032 remove_from_any_list (th);
1035 if (h->nc_cb != NULL)
1036 h->nd_cb (h->cls, peer);
1042 * Try again to connect to transport service.
1045 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1047 struct GNUNET_TRANSPORT_Handle *h = cls;
1048 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1049 struct NeighbourList *n;
1051 /* Forget about all neighbours that we used to be connected
1053 while (NULL != (n = h->neighbours))
1054 remove_neighbour (h, &n->id);
1056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n");
1058 GNUNET_assert (h->client == NULL);
1059 h->reconnect_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1060 h->client = GNUNET_CLIENT_connect (h->sched, "transport", h->cfg);
1061 GNUNET_assert (h->client != NULL);
1062 /* make sure we don't send "START" twice,
1063 remove existing entry from queue (if present) */
1064 pos = h->connect_ready_head;
1067 if (pos->notify == &send_start)
1069 if (pos->prev == NULL)
1070 h->connect_ready_head = pos->next;
1072 pos->prev->next = pos->next;
1073 if (pos->next != NULL)
1074 pos->next->prev = pos->prev;
1075 GNUNET_assert (pos->neighbour == NULL);
1081 schedule_control_transmit (h,
1082 sizeof (struct GNUNET_MessageHeader),
1084 GNUNET_TIME_UNIT_FOREVER_REL, &send_start, NULL);
1085 GNUNET_CLIENT_receive (h->client,
1086 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1091 * Function that will schedule the job that will try
1092 * to connect us again to the client.
1095 schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
1098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1099 "Scheduling task to reconnect to transport service in %llu ms.\n",
1100 h->reconnect_delay.value);
1102 GNUNET_assert (h->client == NULL);
1103 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK);
1105 = GNUNET_SCHEDULER_add_delayed (h->sched,
1107 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1108 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1109 h->reconnect_delay, &reconnect, h);
1110 h->reconnect_delay = GNUNET_TIME_UNIT_SECONDS;
1115 * We are connected to the respective peer, check the
1116 * bandwidth limits and schedule the transmission.
1118 static void schedule_request (struct GNUNET_TRANSPORT_TransmitHandle *th);
1122 * Function called by the scheduler when the timeout
1123 * for bandwidth availablility for the target
1124 * neighbour is reached.
1127 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1129 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
1131 th->notify_delay_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1132 schedule_request (th);
1137 * Remove the given transmit handle from the wait list. Does NOT free
1141 remove_from_wait_list (struct GNUNET_TRANSPORT_TransmitHandle *th)
1143 if (th->prev == NULL)
1144 th->handle->connect_wait_head = th->next;
1146 th->prev->next = th->next;
1147 if (th->next != NULL)
1148 th->next->prev = th->prev;
1153 * We are connected to the respective peer, check the
1154 * bandwidth limits and schedule the transmission.
1157 schedule_request (struct GNUNET_TRANSPORT_TransmitHandle *th)
1159 struct GNUNET_TRANSPORT_Handle *h;
1160 struct GNUNET_TIME_Relative duration;
1161 struct NeighbourList *n;
1166 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1168 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
1169 th->notify_delay_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1171 /* check outgoing quota */
1172 duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
1173 if (duration.value > MIN_QUOTA_REFRESH_TIME)
1176 duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
1178 available = duration.value * n->quota_out;
1179 if (available < n->last_sent + th->notify_size)
1181 /* calculate how much bandwidth we'd still need to
1182 accumulate and based on that how long we'll have
1184 available = n->last_sent + th->notify_size - available;
1185 duration = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1186 available / n->quota_out);
1187 if (th->timeout.value <
1188 GNUNET_TIME_relative_to_absolute (duration).value)
1190 /* signal timeout! */
1192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1193 "Would need %llu ms before bandwidth is available for delivery to `%4s', that is too long. Signaling timeout.\n",
1195 GNUNET_i2s(&th->target));
1197 remove_from_wait_list (th);
1198 th->notify (th->notify_cls, 0, NULL);
1203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1204 "Need more bandwidth, delaying delivery to `%4s' by %llu ms\n",
1205 GNUNET_i2s(&th->target),
1208 th->notify_delay_task
1209 = GNUNET_SCHEDULER_add_delayed (h->sched,
1211 GNUNET_SCHEDULER_PRIORITY_KEEP,
1212 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1213 duration, &transmit_ready, th);
1217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1218 "Bandwidth available for transmission to `%4s'\n",
1219 GNUNET_i2s (&n->id));
1221 if (GNUNET_NO == n->transmit_ok)
1223 /* we may be ready, but transport service is not;
1224 wait for SendOkMessage or timeout */
1226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1227 "Need to wait for transport service `%s' message\n",
1230 th->notify_delay_task
1231 = GNUNET_SCHEDULER_add_delayed (h->sched,
1233 GNUNET_SCHEDULER_PRIORITY_KEEP,
1234 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1235 GNUNET_TIME_absolute_get_remaining
1236 (th->timeout), &transmit_timeout, th);
1239 n->transmit_ok = GNUNET_NO;
1240 remove_from_wait_list (th);
1242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Moving message for `%4s' to ready list\n",
1243 GNUNET_i2s(&n->id));
1245 insert_transmit_handle (&h->connect_ready_head, th);
1246 if (GNUNET_NO == h->transmission_scheduled)
1247 schedule_transmission (h);
1252 * Add neighbour to our list
1255 add_neighbour (struct GNUNET_TRANSPORT_Handle *h,
1257 struct GNUNET_TIME_Relative latency,
1258 const struct GNUNET_PeerIdentity *pid)
1260 struct NeighbourList *n;
1261 struct GNUNET_TRANSPORT_TransmitHandle *prev;
1262 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1263 struct GNUNET_TRANSPORT_TransmitHandle *next;
1265 /* check for duplicates */
1266 if (NULL != find_neighbour (h, pid))
1272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1273 "Creating entry for new neighbour `%4s'.\n", GNUNET_i2s (pid));
1275 n = GNUNET_malloc (sizeof (struct NeighbourList));
1277 n->last_quota_update = GNUNET_TIME_absolute_get ();
1278 n->quota_out = quota_out;
1279 n->next = h->neighbours;
1280 n->transmit_ok = GNUNET_YES;
1282 if (h->nc_cb != NULL)
1283 h->nc_cb (h->cls, &n->id, latency);
1285 pos = h->connect_wait_head;
1289 if (0 == memcmp (pid,
1290 &pos->target, sizeof (struct GNUNET_PeerIdentity)))
1293 GNUNET_assert (NULL == n->transmit_handle);
1294 n->transmit_handle = pos;
1296 h->connect_wait_head = next;
1299 if (GNUNET_YES == n->received_ack)
1302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1303 "Found pending request for `%4s' will trigger it now.\n",
1304 GNUNET_i2s (&pos->target));
1306 if (pos->notify_delay_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1308 GNUNET_SCHEDULER_cancel (h->sched, pos->notify_delay_task);
1309 pos->notify_delay_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1311 schedule_request (pos);
1316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1317 "Found pending request for `%4s' but still need `%s' before proceeding.\n",
1318 GNUNET_i2s (&pos->target),
1331 * Connect to the transport service. Note that the connection may
1332 * complete (or fail) asynchronously.
1335 * @param sched scheduler to use
1336 * @param cfg configuration to use
1337 * @param cls closure for the callbacks
1338 * @param rec receive function to call
1339 * @param nc function to call on connect events
1340 * @param dc function to call on disconnect events
1342 struct GNUNET_TRANSPORT_Handle *
1343 GNUNET_TRANSPORT_connect (struct GNUNET_SCHEDULER_Handle *sched,
1344 struct GNUNET_CONFIGURATION_Handle *cfg,
1346 GNUNET_TRANSPORT_ReceiveCallback rec,
1347 GNUNET_TRANSPORT_NotifyConnect nc,
1348 GNUNET_TRANSPORT_NotifyDisconnect nd)
1350 struct GNUNET_TRANSPORT_Handle *ret;
1352 GNUNET_ARM_start_service ("peerinfo",
1353 cfg, sched, START_SERVICE_TIMEOUT, NULL, NULL);
1354 GNUNET_ARM_start_service ("transport",
1355 cfg, sched, START_SERVICE_TIMEOUT, NULL, NULL);
1356 ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Handle));
1363 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1364 schedule_reconnect (ret);
1370 * These stop activities must be run in a fresh
1371 * scheduler that is NOT in shutdown mode.
1374 stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1376 struct GNUNET_TRANSPORT_Handle *handle = cls;
1377 GNUNET_ARM_stop_service ("transport",
1379 tc->sched, STOP_SERVICE_TIMEOUT, NULL, NULL);
1380 GNUNET_ARM_stop_service ("peerinfo",
1382 tc->sched, STOP_SERVICE_TIMEOUT, NULL, NULL);
1387 * Disconnect from the transport service.
1390 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1392 struct GNUNET_TRANSPORT_TransmitHandle *th;
1393 struct NeighbourList *n;
1394 struct HelloWaitList *hwl;
1395 struct GNUNET_CLIENT_Connection *client;
1398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transport disconnect called!\n");
1400 while (NULL != (th = handle->connect_ready_head))
1402 handle->connect_ready_head = th->next;
1403 th->notify (th->notify_cls, 0, NULL);
1407 while (NULL != (th = handle->connect_wait_head))
1409 handle->connect_wait_head = th->next;
1410 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1412 GNUNET_SCHEDULER_cancel (handle->sched, th->notify_delay_task);
1413 th->notify_delay_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1415 th->notify (th->notify_cls, 0, NULL);
1418 while (NULL != (n = handle->neighbours))
1420 handle->neighbours = n->next;
1423 while (NULL != (hwl = handle->hwl_head))
1425 handle->hwl_head = hwl->next;
1426 GNUNET_SCHEDULER_cancel (handle->sched, hwl->task);
1427 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1428 _("Disconnect while trying to obtain `%s' from transport service.\n"),
1430 if (hwl->rec != NULL)
1431 hwl->rec (hwl->rec_cls, GNUNET_TIME_UNIT_ZERO, NULL, NULL);
1434 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1436 GNUNET_SCHEDULER_cancel (handle->sched, handle->reconnect_task);
1437 handle->reconnect_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1439 GNUNET_free_non_null (handle->my_hello);
1440 handle->my_hello = NULL;
1441 GNUNET_SCHEDULER_run (&stop_task, handle);
1442 if (NULL != (client = handle->client))
1445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1446 "Disconnecting from transport service for good.\n");
1448 handle->client = NULL;
1449 GNUNET_CLIENT_disconnect (client);
1452 GNUNET_free (handle);
1457 * Type of a function to call when we receive a message
1460 * @param cls closure
1461 * @param msg message received, NULL on timeout or fatal error
1464 demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
1466 struct GNUNET_TRANSPORT_Handle *h = cls;
1467 const struct DisconnectInfoMessage *dim;
1468 const struct ConnectInfoMessage *cim;
1469 const struct InboundMessage *im;
1470 const struct GNUNET_MessageHeader *imm;
1471 const struct SendOkMessage *okm;
1472 struct HelloWaitList *hwl;
1473 struct NeighbourList *n;
1474 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
1475 struct GNUNET_PeerIdentity me;
1476 struct GNUNET_TRANSPORT_TransmitHandle *th;
1479 if ((msg == NULL) || (h->client == NULL))
1481 if (h->client != NULL)
1484 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1485 "Error receiving from transport service, disconnecting temporarily.\n");
1487 if (h->network_handle != NULL)
1489 GNUNET_NETWORK_notify_transmit_ready_cancel (h->network_handle);
1490 h->network_handle = NULL;
1491 h->transmission_scheduled = GNUNET_NO;
1492 th = h->connect_ready_head;
1493 /* add timeout again, we cancelled the transmit_ready task! */
1494 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK);
1495 th->notify_delay_task
1496 = GNUNET_SCHEDULER_add_delayed (h->sched,
1498 GNUNET_SCHEDULER_PRIORITY_KEEP,
1499 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1500 GNUNET_TIME_absolute_get_remaining(th->timeout),
1504 GNUNET_CLIENT_disconnect (h->client);
1506 schedule_reconnect (h);
1510 /* shutdown initiated from 'GNUNET_TRANSPORT_disconnect',
1511 finish clean up work! */
1516 GNUNET_CLIENT_receive (h->client,
1517 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1518 size = ntohs (msg->size);
1519 switch (ntohs (msg->type))
1521 case GNUNET_MESSAGE_TYPE_HELLO:
1523 GNUNET_HELLO_get_key ((const struct GNUNET_HELLO_Message *) msg,
1529 GNUNET_CRYPTO_hash (&pkey,
1531 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1535 "Receiving (my own) `%s' message, I am `%4s'.\n",
1536 "HELLO", GNUNET_i2s (&me));
1538 GNUNET_free_non_null (h->my_hello);
1540 if (size < sizeof (struct GNUNET_MessageHeader))
1545 h->my_hello = GNUNET_malloc (size);
1546 memcpy (h->my_hello, msg, size);
1547 while (NULL != (hwl = h->hwl_head))
1549 h->hwl_head = hwl->next;
1550 GNUNET_SCHEDULER_cancel (h->sched, hwl->task);
1551 GNUNET_TRANSPORT_get_hello (h,
1552 GNUNET_TIME_UNIT_ZERO,
1553 hwl->rec, hwl->rec_cls);
1557 case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
1558 if (size != sizeof (struct ConnectInfoMessage))
1563 cim = (const struct ConnectInfoMessage *) msg;
1565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1566 "Receiving `%s' message for `%4s'.\n",
1567 "CONNECT", GNUNET_i2s (&cim->id));
1570 ntohl (cim->quota_out),
1571 GNUNET_TIME_relative_ntoh (cim->latency), &cim->id);
1573 case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
1574 if (size != sizeof (struct DisconnectInfoMessage))
1579 dim = (const struct DisconnectInfoMessage *) msg;
1581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1582 "Receiving `%s' message for `%4s'.\n",
1583 "DISCONNECT", GNUNET_i2s (&dim->peer));
1585 remove_neighbour (h, &dim->peer);
1587 case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
1588 if (size != sizeof (struct SendOkMessage))
1593 okm = (const struct SendOkMessage *) msg;
1595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1596 "Receiving `%s' message, transmission %s.\n", "SEND_OK",
1597 ntohl(okm->success) == GNUNET_OK ? "succeeded" : "failed");
1599 n = find_neighbour (h, &okm->peer);
1600 GNUNET_assert (n != NULL);
1601 n->transmit_ok = GNUNET_YES;
1602 if (n->transmit_handle != NULL)
1605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1606 "Processing pending message\n");
1608 GNUNET_SCHEDULER_cancel (h->sched,
1609 n->transmit_handle->notify_delay_task);
1610 n->transmit_handle->notify_delay_task =
1611 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1612 GNUNET_assert (GNUNET_YES == n->received_ack);
1613 schedule_request (n->transmit_handle);
1616 case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
1618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1619 "Receiving `%s' message.\n", "RECV");
1622 sizeof (struct InboundMessage) +
1623 sizeof (struct GNUNET_MessageHeader))
1628 im = (const struct InboundMessage *) msg;
1629 imm = (const struct GNUNET_MessageHeader *) &im[1];
1630 if (ntohs (imm->size) + sizeof (struct InboundMessage) != size)
1635 switch (ntohs (imm->type))
1637 case GNUNET_MESSAGE_TYPE_TRANSPORT_ACK:
1639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1640 "Receiving `%s' message from `%4s'.\n",
1641 "ACK", GNUNET_i2s (&im->peer));
1643 n = find_neighbour (h, &im->peer);
1649 if (n->received_ack == GNUNET_NO)
1651 n->received_ack = GNUNET_YES;
1652 if (NULL != n->transmit_handle)
1655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1656 "Peer connected, scheduling delayed message for deliverery now.\n");
1658 schedule_request (n->transmit_handle);
1664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1665 "Received message of type %u from `%4s'.\n",
1666 ntohs (imm->type), GNUNET_i2s (&im->peer));
1670 GNUNET_TIME_relative_ntoh (im->latency), &im->peer, imm);
1675 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1677 ("Received unexpected message of type %u in %s:%u\n"),
1678 ntohs (msg->type), __FILE__, __LINE__);
1685 struct ClientTransmitWrapper
1687 GNUNET_NETWORK_TransmitReadyNotify notify;
1689 struct GNUNET_TRANSPORT_TransmitHandle *th;
1694 * Transmit message of a client destined for another
1695 * peer to the service.
1698 client_notify_wrapper (void *cls, size_t size, void *buf)
1700 struct ClientTransmitWrapper *ctw = cls;
1701 struct OutboundMessage *obm;
1702 struct GNUNET_MessageHeader *hdr;
1708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1709 "Transmission request could not be satisfied.\n");
1711 ret = ctw->notify (ctw->notify_cls, 0, NULL);
1712 GNUNET_assert (ret == 0);
1716 GNUNET_assert (size >= sizeof (struct OutboundMessage));
1718 ret = ctw->notify (ctw->notify_cls,
1719 size - sizeof (struct OutboundMessage),
1723 /* Need to reset flag, no SEND means no SEND_OK! */
1724 ctw->th->neighbour->transmit_ok = GNUNET_YES;
1728 GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
1729 hdr = (struct GNUNET_MessageHeader *) &obm[1];
1730 GNUNET_assert (ntohs (hdr->size) == ret);
1731 GNUNET_assert (ret + sizeof (struct OutboundMessage) <
1732 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1735 "Transmitting `%s' message with data for `%4s'\n",
1736 "SEND", GNUNET_i2s (&ctw->th->target));
1738 ret += sizeof (struct OutboundMessage);
1739 obm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
1740 obm->header.size = htons (ret);
1741 obm->reserved = htonl (0);
1742 obm->peer = ctw->th->target;
1750 * Check if we could queue a message of the given size for
1751 * transmission. The transport service will take both its
1752 * internal buffers and bandwidth limits imposed by the
1753 * other peer into consideration when answering this query.
1755 * @param handle connection to transport service
1756 * @param target who should receive the message
1757 * @param size how big is the message we want to transmit?
1758 * @param timeout after how long should we give up (and call
1759 * notify with buf NULL and size 0)?
1760 * @param notify function to call when we are ready to
1761 * send such a message
1762 * @param notify_cls closure for notify
1763 * @return NULL if someone else is already waiting to be notified
1764 * non-NULL if the notify callback was queued (can be used to cancel
1765 * using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1767 struct GNUNET_TRANSPORT_TransmitHandle *
1768 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle
1770 const struct GNUNET_PeerIdentity
1771 *target, size_t size,
1772 struct GNUNET_TIME_Relative timeout,
1773 GNUNET_NETWORK_TransmitReadyNotify
1774 notify, void *notify_cls)
1776 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1777 struct GNUNET_TRANSPORT_TransmitHandle *th;
1778 struct NeighbourList *n;
1779 struct ClientTransmitWrapper *ctw;
1781 if (size + sizeof (struct OutboundMessage) >=
1782 GNUNET_SERVER_MAX_MESSAGE_SIZE)
1788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1789 "Asking transport service for transmission of %u bytes to peer `%4s'.\n",
1790 size, GNUNET_i2s (target));
1792 n = find_neighbour (handle, target);
1793 ctw = GNUNET_malloc (sizeof (struct ClientTransmitWrapper));
1794 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
1795 ctw->notify = notify;
1796 ctw->notify_cls = notify_cls;
1798 th->handle = handle;
1799 th->target = *target;
1800 th->notify = &client_notify_wrapper;
1801 th->notify_cls = ctw;
1802 th->notify_size = size + sizeof (struct OutboundMessage);
1803 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1807 pos = handle->connect_wait_head;
1810 GNUNET_assert (0 != memcmp (target,
1812 sizeof (struct GNUNET_PeerIdentity)));
1816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1817 "Will now try to connect to `%4s'.\n", GNUNET_i2s (target));
1824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1825 "Transmission request queued for transmission to transport service.\n");
1827 GNUNET_assert (NULL == n->transmit_handle);
1828 n->transmit_handle = th;
1829 if (GNUNET_YES != n->received_ack)
1832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1833 "Connection to `%4s' is not yet confirmed connected, scheduling timeout (%llu ms) only.\n",
1834 GNUNET_i2s (target), timeout.value);
1836 th->notify_delay_task
1837 = GNUNET_SCHEDULER_add_delayed (handle->sched,
1839 GNUNET_SCHEDULER_PRIORITY_KEEP,
1840 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1841 timeout, &transmit_timeout, th);
1846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1847 "Peer `%4s' is ready to receive, scheduling message for delivery now.\n",
1848 GNUNET_i2s (target));
1850 schedule_request (th);
1856 * Cancel the specified transmission-ready
1860 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
1861 GNUNET_TRANSPORT_TransmitHandle
1864 struct GNUNET_TRANSPORT_Handle *h;
1867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1868 "Transmission request of %u bytes to `%4s' was cancelled.\n",
1869 th->notify_size - sizeof(struct OutboundMessage),
1870 GNUNET_i2s (&th->target));
1872 GNUNET_assert (th->notify == &client_notify_wrapper);
1873 remove_from_any_list (th);
1875 if ((h->connect_ready_head == NULL) && (h->network_handle != NULL))
1877 GNUNET_NETWORK_notify_transmit_ready_cancel (h->network_handle);
1878 h->network_handle = NULL;
1879 h->transmission_scheduled = GNUNET_NO;
1881 GNUNET_free (th->notify_cls);
1886 /* end of transport_api.c */