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 #include "gnunet_client_lib.h"
28 #include "gnunet_arm_service.h"
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_server_lib.h"
32 #include "gnunet_time_lib.h"
33 #include "gnunet_transport_service.h"
34 #include "transport.h"
37 * After how long do we give up on transmitting a HELLO
40 #define OFFER_HELLO_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
43 * After how long do we automatically retry an unsuccessful
46 #define CONNECT_RETRY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 750)
49 * How long should ARM wait when starting up the
50 * transport service before reporting back?
52 #define START_SERVICE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
55 * How long should ARM wait when stopping the
56 * transport service before reporting back?
58 #define STOP_SERVICE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
61 * Entry in linked list of all of our current neighbours.
67 * This is a linked list.
69 struct NeighbourList *next;
72 * Active transmit handle, can be NULL. Used to move
73 * from ready to wait list on disconnect and to block
74 * two transmissions to the same peer from being scheduled
77 struct GNUNET_TRANSPORT_TransmitHandle *transmit_handle;
80 * Identity of this neighbour.
82 struct GNUNET_PeerIdentity id;
85 * At what time did we reset last_sent last?
87 struct GNUNET_TIME_Absolute last_quota_update;
90 * How many bytes have we sent since the "last_quota_update"
96 * Quota for outbound traffic to the neighbour in bytes/ms.
101 * Set to GNUNET_YES if we are currently allowed to
102 * transmit a message to the transport service for this
103 * peer, GNUNET_NO otherwise.
108 * Set to GNUNET_YES if we have received an ACK for the
109 * given peer. Peers that receive our HELLO always respond
110 * with an ACK to let us know that we are successfully
111 * communicating. Note that a PING can not be used for this
112 * since PINGs are only send if a HELLO address requires
113 * confirmation (and also, PINGs are not passed to the
114 * transport API itself).
122 * Linked list of requests from clients for our HELLO
123 * that were deferred.
129 * This is a linked list.
131 struct HelloWaitList *next;
134 * Reference back to our transport handle.
136 struct GNUNET_TRANSPORT_Handle *handle;
139 * Callback to call once we got our HELLO.
141 GNUNET_TRANSPORT_HelloUpdateCallback rec;
149 * When to time out (call rec with NULL).
151 struct GNUNET_TIME_Absolute timeout;
154 * Timeout task (used to trigger timeout,
155 * cancel if we get the HELLO in time).
157 GNUNET_SCHEDULER_TaskIdentifier task;
164 * Opaque handle for a transmission-ready request.
166 struct GNUNET_TRANSPORT_TransmitHandle
170 * We keep the transmit handles that are waiting for
171 * a transport-level connection in a doubly linked list.
173 struct GNUNET_TRANSPORT_TransmitHandle *next;
176 * We keep the transmit handles that are waiting for
177 * a transport-level connection in a doubly linked list.
179 struct GNUNET_TRANSPORT_TransmitHandle *prev;
182 * Handle of the main transport data structure.
184 struct GNUNET_TRANSPORT_Handle *handle;
187 * Neighbour for this handle, can be NULL if the service
188 * is not yet connected to the target.
190 struct NeighbourList *neighbour;
193 * Which peer is this transmission going to be for? All
194 * zeros if it is control-traffic to the service.
196 struct GNUNET_PeerIdentity target;
199 * Function to call when notify_size bytes are available
202 GNUNET_CONNECTION_TransmitReadyNotify notify;
205 * Closure for notify.
210 * transmit_ready task Id. The task is used to introduce the
211 * artificial delay that may be required to maintain the bandwidth
212 * limits. Later, this will be the ID of the "transmit_timeout"
213 * task which is used to signal a timeout if the transmission could
214 * not be done in a timely fashion.
216 GNUNET_SCHEDULER_TaskIdentifier notify_delay_task;
219 * Timeout for this request.
221 struct GNUNET_TIME_Absolute timeout;
224 * How many bytes is our notify callback waiting for?
229 * How important is this message?
231 unsigned int priority;
237 * Handle for the transport service (includes all of the
238 * state for the transport service).
240 struct GNUNET_TRANSPORT_Handle
244 * Closure for the callbacks.
249 * Function to call for received data.
251 GNUNET_TRANSPORT_ReceiveCallback rec;
254 * function to call on connect events
256 GNUNET_TRANSPORT_NotifyConnect nc_cb;
259 * function to call on disconnect events
261 GNUNET_TRANSPORT_NotifyDisconnect nd_cb;
264 * The current HELLO message for this peer. Updated
265 * whenever transports change their addresses.
267 struct GNUNET_HELLO_Message *my_hello;
270 * My client connection to the transport service.
272 struct GNUNET_CLIENT_Connection *client;
275 * Handle to our registration with the client for notification.
277 struct GNUNET_CLIENT_TransmitHandle *network_handle;
280 * Linked list of transmit handles that are waiting for the
281 * transport to connect to the respective peer. When we
282 * receive notification that the transport connected to a
283 * peer, we go over this list and check if someone has already
284 * requested a transmission to the new peer; if so, we trigger
287 struct GNUNET_TRANSPORT_TransmitHandle *connect_wait_head;
290 * Linked list of transmit handles that are waiting for the
291 * transport to be ready for transmission to the respective
293 * receive notification that the transport disconnected from
294 * a peer, we go over this list and move the entry back to
295 * the connect_wait list.
297 struct GNUNET_TRANSPORT_TransmitHandle *connect_ready_head;
300 * Linked list of pending requests for our HELLO.
302 struct HelloWaitList *hwl_head;
307 struct GNUNET_SCHEDULER_Handle *sched;
312 const struct GNUNET_CONFIGURATION_Handle *cfg;
315 * Linked list of the current neighbours of this peer.
317 struct NeighbourList *neighbours;
320 * ID of the task trying to reconnect to the
323 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
326 * Delay until we try to reconnect.
328 struct GNUNET_TIME_Relative reconnect_delay;
331 * Do we currently have a transmission pending?
332 * (schedule transmission was called but has not
335 int transmission_scheduled;
339 static struct NeighbourList *
340 find_neighbour (struct GNUNET_TRANSPORT_Handle *h,
341 const struct GNUNET_PeerIdentity *peer)
343 struct NeighbourList *pos;
346 while ((pos != NULL) &&
347 (0 != memcmp (peer, &pos->id, sizeof (struct GNUNET_PeerIdentity))))
354 * Schedule the task to send one message from the
355 * connect_ready list to the service.
357 static void schedule_transmission (struct GNUNET_TRANSPORT_Handle *h);
361 * Transmit message to client...
364 transport_notify_ready (void *cls, size_t size, void *buf)
366 struct GNUNET_TRANSPORT_Handle *h = cls;
367 struct GNUNET_TRANSPORT_TransmitHandle *th;
368 struct NeighbourList *n;
372 h->network_handle = NULL;
373 h->transmission_scheduled = GNUNET_NO;
377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
378 "Could not transmit to transport service, cancelling pending requests\n");
380 th = h->connect_ready_head;
381 if (th->next != NULL)
382 th->next->prev = NULL;
383 h->connect_ready_head = th->next;
384 if (NULL != (n = th->neighbour))
386 GNUNET_assert (n->transmit_handle == th);
387 n->transmit_handle = NULL;
389 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
391 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
392 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
394 if (NULL != th->notify)
395 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
397 if (h->connect_ready_head != NULL)
398 schedule_transmission (h); /* FIXME: is this ok? */
402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
403 "Ready to transmit %u bytes to transport service\n", size);
407 h->network_handle = NULL;
408 h->transmission_scheduled = GNUNET_NO;
409 while ((h->connect_ready_head != NULL) &&
410 (h->connect_ready_head->notify_size <= size))
412 th = h->connect_ready_head;
413 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
415 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
416 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
418 GNUNET_assert (th->notify_size <= size);
419 if (th->next != NULL)
420 th->next->prev = NULL;
421 h->connect_ready_head = th->next;
422 if (NULL != (n = th->neighbour))
424 GNUNET_assert (n->transmit_handle == th);
425 n->transmit_handle = NULL;
427 if (NULL != th->notify)
428 ret += th->notify (th->notify_cls, size, &cbuf[ret]);
434 if (h->connect_ready_head != NULL)
435 schedule_transmission (h);
437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
438 "Transmitting %u bytes to transport service\n", ret);
445 * Schedule the task to send one message from the
446 * connect_ready list to the service.
449 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
451 struct GNUNET_TRANSPORT_TransmitHandle *th;
453 GNUNET_assert (NULL == h->network_handle);
454 if (h->client == NULL)
456 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
457 "Could not yet schedule transmission: we are not yet connected to the transport service!\n");
458 return; /* not yet connected */
460 th = h->connect_ready_head;
462 return; /* no request pending */
463 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
465 /* remove existing time out task, will be integrated
466 with transmit_ready notification! */
467 GNUNET_SCHEDULER_cancel (h->sched, 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
476 &transport_notify_ready,
478 GNUNET_assert (NULL != h->network_handle);
483 * Insert the given transmit handle in the given sorted
484 * doubly linked list based on timeout.
486 * @param head pointer to the head of the linked list
487 * @param th element to insert into the list
490 insert_transmit_handle (struct GNUNET_TRANSPORT_TransmitHandle **head,
491 struct GNUNET_TRANSPORT_TransmitHandle *th)
493 struct GNUNET_TRANSPORT_TransmitHandle *pos;
494 struct GNUNET_TRANSPORT_TransmitHandle *prev;
498 while ((pos != NULL) && (pos->timeout.value < th->timeout.value))
506 if (th->next != NULL)
522 * Cancel a pending notify delay task (if pending) and also remove the
523 * given transmit handle from whatever list is on.
525 * @param th handle for the transmission request to manipulate
528 remove_from_any_list (struct GNUNET_TRANSPORT_TransmitHandle *th)
530 struct GNUNET_TRANSPORT_Handle *h;
533 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
535 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
536 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
538 if (th->prev == NULL)
540 if (th == h->connect_wait_head)
541 h->connect_wait_head = th->next;
543 h->connect_ready_head = th->next;
547 th->prev->next = th->next;
549 if (th->next != NULL)
550 th->next->prev = th->prev;
555 * Schedule a request to connect to the given
556 * neighbour (and if successful, add the specified
557 * handle to the wait list).
559 * @param th handle for a request to transmit once we
562 static void 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 peer_transmit_timeout (void *cls,
573 const struct GNUNET_SCHEDULER_TaskContext *tc)
575 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
577 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
578 if (th->neighbour != NULL)
579 th->neighbour->transmit_handle = NULL;
581 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
582 "Request for transmission to peer `%s' timed out.\n",
583 GNUNET_i2s (&th->target));
585 remove_from_any_list (th);
586 if (NULL != th->notify)
587 th->notify (th->notify_cls, 0, NULL);
595 * Queue control request for transmission to the transport
598 * @param h handle to the transport service
599 * @param size number of bytes to be transmitted
600 * @param at_head request must be added to the head of the queue
601 * (otherwise request will be appended)
602 * @param timeout how long this transmission can wait (at most)
603 * @param notify function to call to get the content
604 * @param notify_cls closure for notify
607 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h,
610 struct GNUNET_TIME_Relative timeout,
611 GNUNET_CONNECTION_TransmitReadyNotify notify,
614 struct GNUNET_TRANSPORT_TransmitHandle *th;
617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
618 "Control transmit of %u bytes within %llums requested\n",
619 size, (unsigned long long) timeout.value);
621 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
624 th->notify_cls = notify_cls;
625 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
626 th->notify_size = size;
627 th->notify_delay_task
628 = GNUNET_SCHEDULER_add_delayed (h->sched,
629 timeout, &peer_transmit_timeout, th);
632 th->next = h->connect_ready_head;
633 h->connect_ready_head = th;
634 if (th->next != NULL)
639 insert_transmit_handle (&h->connect_ready_head, th);
641 if (GNUNET_NO == h->transmission_scheduled)
642 schedule_transmission (h);
647 * Update the quota values for the given neighbour now.
650 update_quota (struct NeighbourList *n)
652 struct GNUNET_TIME_Relative delta;
656 delta = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
657 allowed = delta.value * n->quota_out;
658 if (n->last_sent < allowed)
660 remaining = allowed - n->last_sent;
661 if (n->quota_out > 0)
662 remaining /= n->quota_out;
665 if (remaining > MAX_BANDWIDTH_CARRY)
666 remaining = MAX_BANDWIDTH_CARRY;
668 n->last_quota_update = GNUNET_TIME_absolute_get ();
669 n->last_quota_update.value -= remaining;
673 n->last_sent -= allowed;
674 n->last_quota_update = GNUNET_TIME_absolute_get ();
679 struct SetQuotaContext
681 struct GNUNET_TRANSPORT_Handle *handle;
683 struct GNUNET_PeerIdentity target;
685 GNUNET_SCHEDULER_Task cont;
689 struct GNUNET_TIME_Absolute timeout;
696 send_set_quota (void *cls, size_t size, void *buf)
698 struct SetQuotaContext *sqc = cls;
699 struct QuotaSetMessage *msg;
703 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
706 GNUNET_SCHEDULER_REASON_TIMEOUT);
711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
712 "Transmitting `%s' request with respect to `%4s'.\n",
713 "SET_QUOTA", GNUNET_i2s (&sqc->target));
715 GNUNET_assert (size >= sizeof (struct QuotaSetMessage));
717 msg->header.size = htons (sizeof (struct QuotaSetMessage));
718 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
719 msg->quota_in = htonl (sqc->quota_in);
720 memcpy (&msg->peer, &sqc->target, sizeof (struct GNUNET_PeerIdentity));
721 if (sqc->cont != NULL)
722 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
725 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
727 return sizeof (struct QuotaSetMessage);
732 * Set the share of incoming bandwidth for the given
733 * peer to the specified amount.
735 * @param handle connection to transport service
736 * @param target who's bandwidth quota is being changed
737 * @param quota_in incoming bandwidth quota in bytes per ms
738 * @param quota_out outgoing bandwidth quota in bytes per ms
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
783 hello_wait_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
785 struct HelloWaitList *hwl = cls;
786 struct HelloWaitList *pos;
787 struct HelloWaitList *prev;
789 hwl->task = GNUNET_SCHEDULER_NO_TASK;
790 if (GNUNET_TIME_absolute_get_remaining (hwl->timeout).value > 0)
793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
795 ("First attempt to obtain `%s' from transport service failed, will try again for %llums.\n"),
797 GNUNET_TIME_absolute_get_remaining (hwl->timeout).value);
799 hwl->task = GNUNET_SCHEDULER_add_delayed (hwl->handle->sched,
800 GNUNET_TIME_absolute_get_remaining
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, 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 GNUNET_TRANSPORT_HelloUpdateCallback rec,
843 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
844 struct GNUNET_PeerIdentity me;
845 struct HelloWaitList *hwl;
847 if (handle->my_hello == NULL)
849 hwl = GNUNET_malloc (sizeof (struct HelloWaitList));
850 hwl->next = handle->hwl_head;
851 handle->hwl_head = hwl;
852 hwl->handle = handle;
854 hwl->rec_cls = rec_cls;
855 /* hwl->timeout = GNUNET_TIME_relative_to_absolute (timeout);
856 * Timeout not needed, because we should notify on change.
857 * FIXME: set up scheduler to notify on modification?
859 hwl->task = GNUNET_SCHEDULER_add_delayed (handle->sched,
861 &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),
870 rec (rec_cls, (const struct GNUNET_MessageHeader *) handle->my_hello);
875 send_hello (void *cls, size_t size, void *buf)
877 struct GNUNET_MessageHeader *hello = cls;
883 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
884 "Timeout while trying to transmit `%s' request.\n",
891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
892 "Transmitting `%s' request.\n", "HELLO");
894 msize = ntohs (hello->size);
895 GNUNET_assert (size >= msize);
896 memcpy (buf, hello, msize);
903 * Offer the transport service the HELLO of another peer. Note that
904 * the transport service may just ignore this message if the HELLO is
905 * malformed or useless due to our local configuration.
907 * @param handle connection to transport service
908 * @param hello the hello message
911 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
912 const struct GNUNET_MessageHeader *hello)
914 struct GNUNET_MessageHeader *hc;
917 if (handle->client == NULL)
920 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
921 "Not connected to transport service, dropping offered HELLO\n");
925 GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
926 size = ntohs (hello->size);
927 GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
928 hc = GNUNET_malloc (size);
929 memcpy (hc, hello, size);
930 schedule_control_transmit (handle,
932 GNUNET_NO, OFFER_HELLO_TIMEOUT, &send_hello, hc);
937 * Function we use for handling incoming messages.
939 static void demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg);
943 send_start (void *cls, size_t size, void *buf)
945 struct GNUNET_MessageHeader *s = buf;
950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
951 "Timeout while trying to transmit `%s' request.\n",
957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
958 "Transmitting `%s' request.\n", "START");
960 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
961 s->size = htons (sizeof (struct GNUNET_MessageHeader));
962 s->type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
963 return sizeof (struct GNUNET_MessageHeader);
968 * We're ready to transmit the request that the transport service
969 * should connect to a new peer. In addition to sending the
970 * request, schedule the next phase for the transmission processing
971 * that caused the connect request in the first place.
974 request_connect (void *cls, size_t size, void *buf)
976 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
977 struct TryConnectMessage *tcm;
978 struct GNUNET_TRANSPORT_Handle *h;
980 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
986 "Failed to transmit `%s' request for `%4s' to service.\n",
987 "TRY_CONNECT", GNUNET_i2s (&th->target));
989 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
991 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
992 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
994 if (NULL != th->notify)
995 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
1000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1001 "Transmitting `%s' message for `%4s' (need connection in %llu ms).\n",
1002 "TRY_CONNECT", GNUNET_i2s (&th->target),
1003 GNUNET_TIME_absolute_get_remaining (th->timeout).value);
1005 GNUNET_assert (size >= sizeof (struct TryConnectMessage));
1007 tcm->header.size = htons (sizeof (struct TryConnectMessage));
1008 tcm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TRY_CONNECT);
1009 tcm->reserved = htonl (0);
1010 memcpy (&tcm->peer, &th->target, sizeof (struct GNUNET_PeerIdentity));
1011 th->notify_delay_task
1012 = GNUNET_SCHEDULER_add_delayed (h->sched,
1013 GNUNET_TIME_absolute_get_remaining
1015 &peer_transmit_timeout, th);
1016 insert_transmit_handle (&h->connect_wait_head, th);
1017 return sizeof (struct TryConnectMessage);
1022 * Schedule a request to connect to the given
1023 * neighbour (and if successful, add the specified
1024 * handle to the wait list).
1026 * @param th handle for a request to transmit once we
1030 try_connect (struct GNUNET_TRANSPORT_TransmitHandle *th)
1032 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
1033 schedule_control_transmit (th->handle,
1034 sizeof (struct TryConnectMessage),
1036 GNUNET_TIME_absolute_get_remaining (th->timeout),
1037 &request_connect, th);
1042 * Task for delayed attempts to reconnect to a peer.
1044 * @param cls must be a transmit handle that determines the peer
1045 * to which we will try to connect
1046 * @param tc scheduler information about why we were triggered (not used)
1049 try_connect_task (void *cls, 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) &&
1082 (0 != memcmp (peer, &pos->id, sizeof (struct GNUNET_PeerIdentity))))
1093 h->neighbours = pos->next;
1095 prev->next = pos->next;
1096 if (NULL != (th = pos->transmit_handle))
1098 pos->transmit_handle = NULL;
1099 th->neighbour = NULL;
1100 remove_from_any_list (th);
1101 if (GNUNET_TIME_absolute_get_remaining (th->timeout).value <=
1102 CONNECT_RETRY_TIMEOUT.value)
1105 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1107 ("Connection with `%4s' failed and timeout was in the past, giving up on message delivery.\n"),
1109 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == th->notify_delay_task);
1110 peer_transmit_timeout (th, NULL);
1114 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1116 ("Connection with `%4s' failed, will keep trying for %llu ms to deliver message\n"),
1118 GNUNET_TIME_absolute_get_remaining (th->timeout).value);
1119 /* try again in a bit */
1120 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == th->notify_delay_task);
1121 th->notify_delay_task
1122 = GNUNET_SCHEDULER_add_delayed (h->sched,
1123 CONNECT_RETRY_TIMEOUT,
1124 &try_connect_task, th);
1127 if (h->nc_cb != NULL)
1128 h->nd_cb (h->cls, peer);
1134 * Try again to connect to transport service.
1137 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1139 struct GNUNET_TRANSPORT_Handle *h = cls;
1140 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1141 struct NeighbourList *n;
1143 /* Forget about all neighbours that we used to be connected
1145 while (NULL != (n = h->neighbours))
1146 remove_neighbour (h, &n->id);
1148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n");
1150 GNUNET_assert (h->client == NULL);
1151 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1152 h->client = GNUNET_CLIENT_connect (h->sched, "transport", h->cfg);
1153 GNUNET_assert (h->client != NULL);
1154 /* make sure we don't send "START" twice,
1155 remove existing entry from queue (if present) */
1156 pos = h->connect_ready_head;
1159 if (pos->notify == &send_start)
1161 if (pos->prev == NULL)
1162 h->connect_ready_head = pos->next;
1164 pos->prev->next = pos->next;
1165 if (pos->next != NULL)
1166 pos->next->prev = pos->prev;
1167 GNUNET_assert (pos->neighbour == NULL);
1168 if (GNUNET_SCHEDULER_NO_TASK != pos->notify_delay_task)
1170 GNUNET_SCHEDULER_cancel (h->sched, pos->notify_delay_task);
1171 pos->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1178 schedule_control_transmit (h,
1179 sizeof (struct GNUNET_MessageHeader),
1181 GNUNET_TIME_UNIT_FOREVER_REL, &send_start, NULL);
1182 GNUNET_CLIENT_receive (h->client,
1183 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1188 * Function that will schedule the job that will try
1189 * to connect us again to the client.
1192 schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
1195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1196 "Scheduling task to reconnect to transport service in %llu ms.\n",
1197 h->reconnect_delay.value);
1199 GNUNET_assert (h->client == NULL);
1200 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
1202 = GNUNET_SCHEDULER_add_delayed (h->sched,
1203 h->reconnect_delay, &reconnect, h);
1204 h->reconnect_delay = GNUNET_TIME_UNIT_SECONDS;
1209 * We are connected to the respective peer, check the
1210 * bandwidth limits and schedule the transmission.
1212 static void schedule_request (struct GNUNET_TRANSPORT_TransmitHandle *th);
1216 * Function called by the scheduler when the timeout
1217 * for bandwidth availablility for the target
1218 * neighbour is reached.
1221 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1223 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
1225 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1226 schedule_request (th);
1231 * Remove the given transmit handle from the wait list. Does NOT free
1235 remove_from_wait_list (struct GNUNET_TRANSPORT_TransmitHandle *th)
1237 if (th->prev == NULL)
1238 th->handle->connect_wait_head = th->next;
1240 th->prev->next = th->next;
1241 if (th->next != NULL)
1242 th->next->prev = th->prev;
1247 * We are connected to the respective peer, check the
1248 * bandwidth limits and schedule the transmission.
1251 schedule_request (struct GNUNET_TRANSPORT_TransmitHandle *th)
1253 struct GNUNET_TRANSPORT_Handle *h;
1254 struct GNUNET_TIME_Relative duration;
1255 struct NeighbourList *n;
1260 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1262 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
1263 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1265 /* check outgoing quota */
1266 duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
1267 if (duration.value > MIN_QUOTA_REFRESH_TIME)
1270 duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
1272 available = duration.value * n->quota_out;
1273 if (available < n->last_sent + th->notify_size)
1275 /* calculate how much bandwidth we'd still need to
1276 accumulate and based on that how long we'll have
1278 available = n->last_sent + th->notify_size - available;
1279 duration = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1280 available / n->quota_out);
1281 if (th->timeout.value <
1282 GNUNET_TIME_relative_to_absolute (duration).value)
1284 /* signal timeout! */
1286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1287 "Would need %llu ms before bandwidth is available for delivery to `%4s', that is too long. Signaling timeout.\n",
1288 duration.value, GNUNET_i2s (&th->target));
1290 remove_from_wait_list (th);
1291 if (NULL != th->notify)
1292 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
1297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1298 "Need more bandwidth, delaying delivery to `%4s' by %llu ms\n",
1299 GNUNET_i2s (&th->target), duration.value);
1301 th->notify_delay_task
1302 = GNUNET_SCHEDULER_add_delayed (h->sched,
1303 duration, &transmit_ready, th);
1307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1308 "Bandwidth available for transmission to `%4s'\n",
1309 GNUNET_i2s (&n->id));
1311 if (GNUNET_NO == n->transmit_ok)
1313 /* we may be ready, but transport service is not;
1314 wait for SendOkMessage or timeout */
1316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1317 "Need to wait for transport service `%s' message\n",
1320 th->notify_delay_task
1321 = GNUNET_SCHEDULER_add_delayed (h->sched,
1322 GNUNET_TIME_absolute_get_remaining
1323 (th->timeout), &peer_transmit_timeout,
1327 n->transmit_ok = GNUNET_NO;
1328 remove_from_wait_list (th);
1330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1331 "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,
1348 const struct GNUNET_PeerIdentity *pid)
1350 struct NeighbourList *n;
1351 struct GNUNET_TRANSPORT_TransmitHandle *prev;
1352 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1353 struct GNUNET_TRANSPORT_TransmitHandle *next;
1355 /* check for duplicates */
1356 if (NULL != find_neighbour (h, pid))
1362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1363 "Creating entry for new neighbour `%4s'.\n", GNUNET_i2s (pid));
1365 n = GNUNET_malloc (sizeof (struct NeighbourList));
1367 n->last_quota_update = GNUNET_TIME_absolute_get ();
1368 n->quota_out = quota_out;
1369 n->next = h->neighbours;
1370 n->transmit_ok = GNUNET_YES;
1372 if (h->nc_cb != NULL)
1373 h->nc_cb (h->cls, &n->id, latency, distance);
1375 pos = h->connect_wait_head;
1379 if (0 == memcmp (pid,
1380 &pos->target, sizeof (struct GNUNET_PeerIdentity)))
1383 GNUNET_assert (NULL == n->transmit_handle);
1384 n->transmit_handle = pos;
1386 h->connect_wait_head = next;
1389 if (GNUNET_YES == n->received_ack)
1392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1393 "Found pending request for `%4s' will trigger it now.\n",
1394 GNUNET_i2s (&pos->target));
1396 if (pos->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1398 GNUNET_SCHEDULER_cancel (h->sched, pos->notify_delay_task);
1399 pos->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1401 schedule_request (pos);
1406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1407 "Found pending request for `%4s' but still need `%s' before proceeding.\n",
1408 GNUNET_i2s (&pos->target), "ACK");
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 nd 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_services (cfg, sched, "peerinfo", "transport", NULL);
1442 ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Handle));
1449 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1450 schedule_reconnect (ret);
1456 * Disconnect from the transport service.
1459 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1461 struct GNUNET_TRANSPORT_TransmitHandle *th;
1462 struct NeighbourList *n;
1463 struct HelloWaitList *hwl;
1464 struct GNUNET_CLIENT_Connection *client;
1467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transport disconnect called!\n");
1469 while (NULL != (th = handle->connect_ready_head))
1471 handle->connect_ready_head = th->next;
1472 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1474 GNUNET_SCHEDULER_cancel (handle->sched, th->notify_delay_task);
1475 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1477 if (NULL != th->notify)
1478 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
1481 while (NULL != (th = handle->connect_wait_head))
1483 handle->connect_wait_head = th->next;
1484 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1486 GNUNET_SCHEDULER_cancel (handle->sched, th->notify_delay_task);
1487 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1489 if (NULL != th->notify)
1490 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
1493 while (NULL != (n = handle->neighbours))
1495 handle->neighbours = n->next;
1496 if (NULL != (th = n->transmit_handle))
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 if (NULL != th->notify)
1504 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
1509 while (NULL != (hwl = handle->hwl_head))
1511 handle->hwl_head = hwl->next;
1512 GNUNET_SCHEDULER_cancel (handle->sched, hwl->task);
1513 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1515 ("Disconnect while trying to obtain `%s' from transport service.\n"),
1517 if (hwl->rec != NULL)
1518 hwl->rec (hwl->rec_cls, NULL);
1521 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1523 GNUNET_SCHEDULER_cancel (handle->sched, handle->reconnect_task);
1524 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1526 GNUNET_free_non_null (handle->my_hello);
1527 handle->my_hello = NULL;
1528 GNUNET_ARM_stop_services (handle->cfg, handle->sched, "transport",
1530 if (NULL != handle->network_handle)
1532 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->network_handle);
1533 handle->network_handle = NULL;
1535 if (NULL != (client = handle->client))
1538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1539 "Disconnecting from transport service for good.\n");
1541 handle->client = NULL;
1542 GNUNET_CLIENT_disconnect (client);
1544 GNUNET_free (handle);
1549 * Type of a function to call when we receive a message
1552 * @param cls closure
1553 * @param msg message received, NULL on timeout or fatal error
1556 demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
1558 struct GNUNET_TRANSPORT_Handle *h = cls;
1559 const struct DisconnectInfoMessage *dim;
1560 const struct ConnectInfoMessage *cim;
1561 const struct InboundMessage *im;
1562 const struct GNUNET_MessageHeader *imm;
1563 const struct SendOkMessage *okm;
1564 struct HelloWaitList *hwl;
1565 struct NeighbourList *n;
1566 struct GNUNET_PeerIdentity me;
1567 struct GNUNET_TRANSPORT_TransmitHandle *th;
1570 if ((msg == NULL) || (h->client == NULL))
1572 if (h->client != NULL)
1575 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1576 "Error receiving from transport service, disconnecting temporarily.\n");
1578 if (h->network_handle != NULL)
1580 GNUNET_CLIENT_notify_transmit_ready_cancel (h->network_handle);
1581 h->network_handle = NULL;
1582 h->transmission_scheduled = GNUNET_NO;
1583 th = h->connect_ready_head;
1584 /* add timeout again, we cancelled the transmit_ready task! */
1585 GNUNET_assert (th->notify_delay_task ==
1586 GNUNET_SCHEDULER_NO_TASK);
1587 th->notify_delay_task =
1588 GNUNET_SCHEDULER_add_delayed (h->sched,
1589 GNUNET_TIME_absolute_get_remaining
1591 &peer_transmit_timeout, th);
1593 GNUNET_CLIENT_disconnect (h->client);
1595 schedule_reconnect (h);
1599 /* shutdown initiated from 'GNUNET_TRANSPORT_disconnect',
1600 finish clean up work! */
1605 GNUNET_CLIENT_receive (h->client,
1606 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1607 size = ntohs (msg->size);
1608 switch (ntohs (msg->type))
1610 case GNUNET_MESSAGE_TYPE_HELLO:
1612 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg,
1619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1620 "Receiving (my own) `%s' message, I am `%4s'.\n",
1621 "HELLO", GNUNET_i2s (&me));
1623 GNUNET_free_non_null (h->my_hello);
1625 if (size < sizeof (struct GNUNET_MessageHeader))
1630 h->my_hello = GNUNET_malloc (size);
1631 memcpy (h->my_hello, msg, size);
1632 while (NULL != (hwl = h->hwl_head))
1634 h->hwl_head = hwl->next;
1635 GNUNET_SCHEDULER_cancel (h->sched, hwl->task);
1636 GNUNET_TRANSPORT_get_hello (h,
1637 hwl->rec, hwl->rec_cls);
1641 case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
1642 if (size != sizeof (struct ConnectInfoMessage))
1647 cim = (const struct ConnectInfoMessage *) msg;
1649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1650 "Receiving `%s' message for `%4s'.\n",
1651 "CONNECT", GNUNET_i2s (&cim->id));
1654 ntohl (cim->quota_out),
1655 GNUNET_TIME_relative_ntoh (cim->latency), ntohs(cim->distance), &cim->id);
1657 case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
1658 if (size != sizeof (struct DisconnectInfoMessage))
1663 dim = (const struct DisconnectInfoMessage *) msg;
1665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1666 "Receiving `%s' message for `%4s'.\n",
1667 "DISCONNECT", GNUNET_i2s (&dim->peer));
1669 remove_neighbour (h, &dim->peer);
1671 case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
1672 if (size != sizeof (struct SendOkMessage))
1677 okm = (const struct SendOkMessage *) msg;
1679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1680 "Receiving `%s' message, transmission %s.\n", "SEND_OK",
1681 ntohl (okm->success) == GNUNET_OK ? "succeeded" : "failed");
1683 n = find_neighbour (h, &okm->peer);
1684 GNUNET_assert (n != NULL);
1685 n->transmit_ok = GNUNET_YES;
1686 if (n->transmit_handle != NULL)
1689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1690 "Processing pending message for `%4s'\n",
1691 GNUNET_i2s (&n->id));
1693 GNUNET_SCHEDULER_cancel (h->sched,
1694 n->transmit_handle->notify_delay_task);
1695 n->transmit_handle->notify_delay_task = 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));
1753 h->rec (h->cls, &im->peer, imm,
1754 GNUNET_TIME_relative_ntoh (im->latency), ntohs(im->distance));
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_CONNECTION_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 if (NULL != ctw->notify)
1796 GNUNET_assert (0 == ctw->notify (ctw->notify_cls, 0, NULL));
1800 GNUNET_assert (size >= sizeof (struct OutboundMessage));
1802 if (ctw->notify != NULL)
1803 ret = ctw->notify (ctw->notify_cls,
1804 size - sizeof (struct OutboundMessage),
1810 /* Need to reset flag, no SEND means no SEND_OK! */
1811 ctw->th->neighbour->transmit_ok = GNUNET_YES;
1815 GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
1816 hdr = (struct GNUNET_MessageHeader *) &obm[1];
1817 GNUNET_assert (ntohs (hdr->size) == ret);
1818 GNUNET_assert (ret + sizeof (struct OutboundMessage) <
1819 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1822 "Transmitting `%s' message with data for `%4s'\n",
1823 "SEND", GNUNET_i2s (&ctw->th->target));
1825 ret += sizeof (struct OutboundMessage);
1826 obm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
1827 obm->header.size = htons (ret);
1828 obm->priority = htonl (ctw->th->priority);
1829 obm->peer = ctw->th->target;
1837 * Check if we could queue a message of the given size for
1838 * transmission. The transport service will take both its
1839 * internal buffers and bandwidth limits imposed by the
1840 * other peer into consideration when answering this query.
1842 * @param handle connection to transport service
1843 * @param target who should receive the message
1844 * @param size how big is the message we want to transmit?
1845 * @param priority how important is the message?
1846 * @param timeout after how long should we give up (and call
1847 * notify with buf NULL and size 0)?
1848 * @param notify function to call when we are ready to
1849 * send such a message
1850 * @param notify_cls closure for notify
1851 * @return NULL if someone else is already waiting to be notified
1852 * non-NULL if the notify callback was queued (can be used to cancel
1853 * using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1855 struct GNUNET_TRANSPORT_TransmitHandle *
1856 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle
1858 const struct GNUNET_PeerIdentity
1859 *target, size_t size,
1860 unsigned int priority,
1861 struct GNUNET_TIME_Relative timeout,
1862 GNUNET_CONNECTION_TransmitReadyNotify
1863 notify, void *notify_cls)
1865 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1866 struct GNUNET_TRANSPORT_TransmitHandle *th;
1867 struct NeighbourList *n;
1868 struct ClientTransmitWrapper *ctw;
1870 if (size + sizeof (struct OutboundMessage) >=
1871 GNUNET_SERVER_MAX_MESSAGE_SIZE)
1877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1878 "Asking transport service for transmission of %u bytes to peer `%4s'.\n",
1879 size, GNUNET_i2s (target));
1881 n = find_neighbour (handle, target);
1882 if ((n != NULL) && (n->transmit_handle != NULL))
1883 return NULL; /* already have a request pending for this peer! */
1884 ctw = GNUNET_malloc (sizeof (struct ClientTransmitWrapper));
1885 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
1886 ctw->notify = notify;
1887 ctw->notify_cls = notify_cls;
1889 th->handle = handle;
1891 th->target = *target;
1892 th->notify = &client_notify_wrapper;
1893 th->notify_cls = ctw;
1894 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1895 th->notify_size = size + sizeof (struct OutboundMessage);
1896 th->priority = priority;
1899 pos = handle->connect_wait_head;
1902 GNUNET_assert (0 != memcmp (target,
1904 sizeof (struct GNUNET_PeerIdentity)));
1908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1909 "Will now try to connect to `%4s'.\n", GNUNET_i2s (target));
1916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1917 "Transmission request queued for transmission to transport service.\n");
1919 GNUNET_assert (NULL == n->transmit_handle);
1920 n->transmit_handle = th;
1921 if (GNUNET_YES != n->received_ack)
1924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1925 "Connection to `%4s' is not yet confirmed connected, scheduling timeout (%llu ms) only.\n",
1926 GNUNET_i2s (target), timeout.value);
1928 th->notify_delay_task
1929 = GNUNET_SCHEDULER_add_delayed (handle->sched,
1930 timeout, &peer_transmit_timeout, th);
1935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1936 "Peer `%4s' is ready to receive, scheduling message for delivery now.\n",
1937 GNUNET_i2s (target));
1939 th->notify_delay_task
1940 = GNUNET_SCHEDULER_add_now (handle->sched, &transmit_ready, th);
1946 * Cancel the specified transmission-ready notification.
1949 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
1950 GNUNET_TRANSPORT_TransmitHandle
1953 struct GNUNET_TRANSPORT_Handle *h;
1956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1957 "Transmission request of %u bytes to `%4s' was cancelled.\n",
1958 th->notify_size - sizeof (struct OutboundMessage),
1959 GNUNET_i2s (&th->target));
1961 GNUNET_assert (th->notify == &client_notify_wrapper);
1962 remove_from_any_list (th);
1964 if ((h->connect_ready_head == NULL) && (h->network_handle != NULL))
1966 GNUNET_CLIENT_notify_transmit_ready_cancel (h->network_handle);
1967 h->network_handle = NULL;
1968 h->transmission_scheduled = GNUNET_NO;
1970 GNUNET_free (th->notify_cls);
1971 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
1976 /* end of transport_api.c */