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.
109 * Set to GNUNET_YES if we have received an ACK for the
110 * given peer. Peers that receive our HELLO always respond
111 * with an ACK to let us know that we are successfully
112 * communicating. Note that a PING can not be used for this
113 * since PINGs are only send if a HELLO address requires
114 * confirmation (and also, PINGs are not passed to the
115 * transport API itself).
123 * Linked list of requests from clients for our HELLO
124 * that were deferred.
130 * This is a linked list.
132 struct HelloWaitList *next;
135 * Reference back to our transport handle.
137 struct GNUNET_TRANSPORT_Handle *handle;
140 * Callback to call once we got our HELLO.
142 GNUNET_TRANSPORT_HelloUpdateCallback rec;
153 * Opaque handle for a transmission-ready request.
155 struct GNUNET_TRANSPORT_TransmitHandle
159 * We keep the transmit handles that are waiting for
160 * a transport-level connection in a doubly linked list.
162 struct GNUNET_TRANSPORT_TransmitHandle *next;
165 * We keep the transmit handles that are waiting for
166 * a transport-level connection in a doubly linked list.
168 struct GNUNET_TRANSPORT_TransmitHandle *prev;
171 * Handle of the main transport data structure.
173 struct GNUNET_TRANSPORT_Handle *handle;
176 * Neighbour for this handle, can be NULL if the service
177 * is not yet connected to the target.
179 struct NeighbourList *neighbour;
182 * Which peer is this transmission going to be for? All
183 * zeros if it is control-traffic to the service.
185 struct GNUNET_PeerIdentity target;
188 * Function to call when notify_size bytes are available
191 GNUNET_CONNECTION_TransmitReadyNotify notify;
194 * Closure for notify.
199 * transmit_ready task Id. The task is used to introduce the
200 * artificial delay that may be required to maintain the bandwidth
201 * limits. Later, this will be the ID of the "transmit_timeout"
202 * task which is used to signal a timeout if the transmission could
203 * not be done in a timely fashion.
205 GNUNET_SCHEDULER_TaskIdentifier notify_delay_task;
208 * Timeout for this request.
210 struct GNUNET_TIME_Absolute timeout;
213 * How many bytes is our notify callback waiting for?
218 * How important is this message?
220 unsigned int priority;
226 * Handle for the transport service (includes all of the
227 * state for the transport service).
229 struct GNUNET_TRANSPORT_Handle
233 * Closure for the callbacks.
238 * Function to call for received data.
240 GNUNET_TRANSPORT_ReceiveCallback rec;
243 * function to call on connect events
245 GNUNET_TRANSPORT_NotifyConnect nc_cb;
248 * function to call on disconnect events
250 GNUNET_TRANSPORT_NotifyDisconnect nd_cb;
253 * The current HELLO message for this peer. Updated
254 * whenever transports change their addresses.
256 struct GNUNET_HELLO_Message *my_hello;
259 * My client connection to the transport service.
261 struct GNUNET_CLIENT_Connection *client;
264 * Handle to our registration with the client for notification.
266 struct GNUNET_CLIENT_TransmitHandle *network_handle;
269 * Linked list of transmit handles that are waiting for the
270 * transport to connect to the respective peer. When we
271 * receive notification that the transport connected to a
272 * peer, we go over this list and check if someone has already
273 * requested a transmission to the new peer; if so, we trigger
276 struct GNUNET_TRANSPORT_TransmitHandle *connect_wait_head;
279 * Linked list of transmit handles that are waiting for the
280 * transport to be ready for transmission to the respective
282 * receive notification that the transport disconnected from
283 * a peer, we go over this list and move the entry back to
284 * the connect_wait list.
286 struct GNUNET_TRANSPORT_TransmitHandle *connect_ready_head;
289 * Linked list of pending requests for our HELLO.
291 struct HelloWaitList *hwl_head;
296 struct GNUNET_SCHEDULER_Handle *sched;
301 const struct GNUNET_CONFIGURATION_Handle *cfg;
304 * Linked list of the current neighbours of this peer.
306 struct NeighbourList *neighbours;
309 * ID of the task trying to reconnect to the
312 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
315 * Delay until we try to reconnect.
317 struct GNUNET_TIME_Relative reconnect_delay;
320 * Do we currently have a transmission pending?
321 * (schedule transmission was called but has not
324 int transmission_scheduled;
328 static struct NeighbourList *
329 find_neighbour (struct GNUNET_TRANSPORT_Handle *h,
330 const struct GNUNET_PeerIdentity *peer)
332 struct NeighbourList *pos;
335 while ((pos != NULL) &&
336 (0 != memcmp (peer, &pos->id, sizeof (struct GNUNET_PeerIdentity))))
343 * Schedule the task to send one message from the
344 * connect_ready list to the service.
346 static void schedule_transmission (struct GNUNET_TRANSPORT_Handle *h);
350 * Transmit message to client...
353 transport_notify_ready (void *cls, size_t size, void *buf)
355 struct GNUNET_TRANSPORT_Handle *h = cls;
356 struct GNUNET_TRANSPORT_TransmitHandle *th;
357 struct NeighbourList *n;
361 h->network_handle = NULL;
362 h->transmission_scheduled = GNUNET_NO;
366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
367 "Could not transmit to transport service, cancelling pending requests\n");
369 th = h->connect_ready_head;
370 if (th->next != NULL)
371 th->next->prev = NULL;
372 h->connect_ready_head = th->next;
373 if (NULL != (n = th->neighbour))
375 GNUNET_assert (n->transmit_handle == th);
376 n->transmit_handle = NULL;
378 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
380 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
381 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
383 if (NULL != th->notify)
384 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
386 if (h->connect_ready_head != NULL)
387 schedule_transmission (h); /* FIXME: is this ok? */
391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
392 "Ready to transmit %u bytes to transport service\n", size);
396 h->network_handle = NULL;
397 h->transmission_scheduled = GNUNET_NO;
398 while ((h->connect_ready_head != NULL) &&
399 (h->connect_ready_head->notify_size <= size))
401 th = h->connect_ready_head;
402 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
404 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
405 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
407 GNUNET_assert (th->notify_size <= size);
408 if (th->next != NULL)
409 th->next->prev = NULL;
410 h->connect_ready_head = th->next;
411 if (NULL != (n = th->neighbour))
413 GNUNET_assert (n->transmit_handle == th);
414 n->transmit_handle = NULL;
416 if (NULL != th->notify)
417 ret += th->notify (th->notify_cls, size, &cbuf[ret]);
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_TASK)
454 /* remove existing time out task, will be integrated
455 with transmit_ready notification! */
456 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
457 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
459 h->transmission_scheduled = GNUNET_YES;
460 h->network_handle = GNUNET_CLIENT_notify_transmit_ready (h->client,
462 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_TASK)
524 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
525 th->notify_delay_task = GNUNET_SCHEDULER_NO_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 * Schedule a request to connect to the given
545 * neighbour (and if successful, add the specified
546 * handle to the wait list).
548 * @param th handle for a request to transmit once we
551 static void try_connect (struct GNUNET_TRANSPORT_TransmitHandle *th);
555 * Called when our transmit request timed out before any transport
556 * reported success connecting to the desired peer or before the
557 * transport was ready to receive. Signal error and free
561 peer_transmit_timeout (void *cls,
562 const struct GNUNET_SCHEDULER_TaskContext *tc)
564 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
566 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
567 if (th->neighbour != NULL)
568 th->neighbour->transmit_handle = NULL;
570 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
571 "Request for transmission to peer `%s' timed out.\n",
572 GNUNET_i2s (&th->target));
574 remove_from_any_list (th);
575 if (NULL != th->notify)
576 th->notify (th->notify_cls, 0, NULL);
584 * Queue control request for transmission to the transport
587 * @param h handle to the transport service
588 * @param size number of bytes to be transmitted
589 * @param at_head request must be added to the head of the queue
590 * (otherwise request will be appended)
591 * @param timeout how long this transmission can wait (at most)
592 * @param notify function to call to get the content
593 * @param notify_cls closure for notify
596 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h,
599 struct GNUNET_TIME_Relative timeout,
600 GNUNET_CONNECTION_TransmitReadyNotify notify,
603 struct GNUNET_TRANSPORT_TransmitHandle *th;
606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
607 "Control transmit of %u bytes within %llums requested\n",
608 size, (unsigned long long) timeout.value);
610 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
613 th->notify_cls = notify_cls;
614 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
615 th->notify_size = size;
616 th->notify_delay_task
617 = GNUNET_SCHEDULER_add_delayed (h->sched,
618 timeout, &peer_transmit_timeout, th);
621 th->next = h->connect_ready_head;
622 h->connect_ready_head = th;
623 if (th->next != NULL)
628 insert_transmit_handle (&h->connect_ready_head, th);
630 if (GNUNET_NO == h->transmission_scheduled)
631 schedule_transmission (h);
636 * Update the quota values for the given neighbour now.
639 update_quota (struct NeighbourList *n)
641 struct GNUNET_TIME_Relative delta;
645 delta = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
646 allowed = delta.value * n->quota_out;
647 if (n->last_sent < allowed)
649 remaining = allowed - n->last_sent;
650 if (n->quota_out > 0)
651 remaining /= n->quota_out;
654 if (remaining > MAX_BANDWIDTH_CARRY)
655 remaining = MAX_BANDWIDTH_CARRY;
657 n->last_quota_update = GNUNET_TIME_absolute_get ();
658 n->last_quota_update.value -= remaining;
662 n->last_sent -= allowed;
663 n->last_quota_update = GNUNET_TIME_absolute_get ();
668 struct SetQuotaContext
670 struct GNUNET_TRANSPORT_Handle *handle;
672 struct GNUNET_PeerIdentity target;
674 GNUNET_SCHEDULER_Task cont;
678 struct GNUNET_TIME_Absolute timeout;
685 send_set_quota (void *cls, size_t size, void *buf)
687 struct SetQuotaContext *sqc = cls;
688 struct QuotaSetMessage *msg;
692 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
695 GNUNET_SCHEDULER_REASON_TIMEOUT);
700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
701 "Transmitting `%s' request with respect to `%4s'.\n",
702 "SET_QUOTA", GNUNET_i2s (&sqc->target));
704 GNUNET_assert (size >= sizeof (struct QuotaSetMessage));
706 msg->header.size = htons (sizeof (struct QuotaSetMessage));
707 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
708 msg->quota_in = htonl (sqc->quota_in);
709 memcpy (&msg->peer, &sqc->target, sizeof (struct GNUNET_PeerIdentity));
710 if (sqc->cont != NULL)
711 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
714 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
716 return sizeof (struct QuotaSetMessage);
721 * Set the share of incoming bandwidth for the given
722 * peer to the specified amount.
724 * @param handle connection to transport service
725 * @param target who's bandwidth quota is being changed
726 * @param quota_in incoming bandwidth quota in bytes per ms
727 * @param quota_out outgoing bandwidth quota in bytes per ms
728 * @param timeout how long to wait until signaling failure if
729 * we can not communicate the quota change
730 * @param cont continuation to call when done, will be called
731 * either with reason "TIMEOUT" or with reason "PREREQ_DONE"
732 * @param cont_cls closure for continuation
735 GNUNET_TRANSPORT_set_quota (struct GNUNET_TRANSPORT_Handle *handle,
736 const struct GNUNET_PeerIdentity *target,
739 struct GNUNET_TIME_Relative timeout,
740 GNUNET_SCHEDULER_Task cont, void *cont_cls)
742 struct NeighbourList *n;
743 struct SetQuotaContext *sqc;
745 n = find_neighbour (handle, target);
749 if (n->quota_out < quota_out)
750 n->last_quota_update = GNUNET_TIME_absolute_get ();
751 n->quota_out = quota_out;
753 sqc = GNUNET_malloc (sizeof (struct SetQuotaContext));
754 sqc->handle = handle;
755 sqc->target = *target;
757 sqc->cont_cls = cont_cls;
758 sqc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
759 sqc->quota_in = quota_in;
760 schedule_control_transmit (handle,
761 sizeof (struct QuotaSetMessage),
762 GNUNET_NO, timeout, &send_set_quota, sqc);
767 * Obtain the HELLO message for this peer.
769 * @param handle connection to transport service
770 * @param timeout how long to wait for the HELLO
771 * @param rec function to call with the HELLO, sender will be our peer
772 * identity; message and sender will be NULL on timeout
773 * (handshake with transport service pending/failed).
774 * cost estimate will be 0.
775 * @param rec_cls closure for rec
778 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
779 GNUNET_TRANSPORT_HelloUpdateCallback rec,
782 struct HelloWaitList *hwl;
784 hwl = GNUNET_malloc (sizeof (struct HelloWaitList));
785 hwl->next = handle->hwl_head;
786 handle->hwl_head = hwl;
787 hwl->handle = handle;
789 hwl->rec_cls = rec_cls;
790 if (handle->my_hello == NULL)
792 rec (rec_cls, (const struct GNUNET_MessageHeader *) handle->my_hello);
798 * Stop receiving updates about changes to our HELLO message.
800 * @param handle connection to transport service
801 * @param rec function previously registered to be called with the HELLOs
802 * @param rec_cls closure for rec
805 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_Handle *handle,
806 GNUNET_TRANSPORT_HelloUpdateCallback rec,
809 struct HelloWaitList *pos;
810 struct HelloWaitList *prev;
813 pos = handle->hwl_head;
816 if ( (pos->rec == rec) &&
817 (pos->rec_cls == rec_cls) )
822 GNUNET_break (pos != NULL);
826 handle->hwl_head = pos->next;
828 prev->next = pos->next;
834 send_hello (void *cls, size_t size, void *buf)
836 struct GNUNET_MessageHeader *hello = cls;
842 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
843 "Timeout while trying to transmit `%s' request.\n",
850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
851 "Transmitting `%s' request.\n", "HELLO");
853 msize = ntohs (hello->size);
854 GNUNET_assert (size >= msize);
855 memcpy (buf, hello, msize);
862 * Offer the transport service the HELLO of another peer. Note that
863 * the transport service may just ignore this message if the HELLO is
864 * malformed or useless due to our local configuration.
866 * @param handle connection to transport service
867 * @param hello the hello message
870 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
871 const struct GNUNET_MessageHeader *hello)
873 struct GNUNET_MessageHeader *hc;
876 if (handle->client == NULL)
879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
880 "Not connected to transport service, dropping offered HELLO\n");
884 GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
885 size = ntohs (hello->size);
886 GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
887 hc = GNUNET_malloc (size);
888 memcpy (hc, hello, size);
889 schedule_control_transmit (handle,
891 GNUNET_NO, OFFER_HELLO_TIMEOUT, &send_hello, hc);
896 * Function we use for handling incoming messages.
898 static void demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg);
902 send_start (void *cls, size_t size, void *buf)
904 struct GNUNET_MessageHeader *s = buf;
909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
910 "Timeout while trying to transmit `%s' request.\n",
916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
917 "Transmitting `%s' request.\n", "START");
919 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
920 s->size = htons (sizeof (struct GNUNET_MessageHeader));
921 s->type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
922 return sizeof (struct GNUNET_MessageHeader);
927 * We're ready to transmit the request that the transport service
928 * should connect to a new peer. In addition to sending the
929 * request, schedule the next phase for the transmission processing
930 * that caused the connect request in the first place.
933 request_connect (void *cls, size_t size, void *buf)
935 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
936 struct TryConnectMessage *tcm;
937 struct GNUNET_TRANSPORT_Handle *h;
939 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
946 "Failed to transmit `%s' request for `%4s' to service.\n",
947 "TRY_CONNECT", GNUNET_i2s (&th->target));
949 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
951 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
952 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
954 if (NULL != th->notify)
955 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
961 "Transmitting `%s' message for `%4s' (need connection in %llu ms).\n",
962 "TRY_CONNECT", GNUNET_i2s (&th->target),
963 GNUNET_TIME_absolute_get_remaining (th->timeout).value);
965 GNUNET_assert (size >= sizeof (struct TryConnectMessage));
967 tcm->header.size = htons (sizeof (struct TryConnectMessage));
968 tcm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TRY_CONNECT);
969 tcm->reserved = htonl (0);
970 memcpy (&tcm->peer, &th->target, sizeof (struct GNUNET_PeerIdentity));
971 th->notify_delay_task
972 = GNUNET_SCHEDULER_add_delayed (h->sched,
973 GNUNET_TIME_absolute_get_remaining
975 &peer_transmit_timeout, th);
976 insert_transmit_handle (&h->connect_wait_head, th);
977 return sizeof (struct TryConnectMessage);
982 * Schedule a request to connect to the given
983 * neighbour (and if successful, add the specified
984 * handle to the wait list).
986 * @param th handle for a request to transmit once we
990 try_connect (struct GNUNET_TRANSPORT_TransmitHandle *th)
992 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
993 schedule_control_transmit (th->handle,
994 sizeof (struct TryConnectMessage),
996 GNUNET_TIME_absolute_get_remaining (th->timeout),
997 &request_connect, th);
1002 * Task for delayed attempts to reconnect to a peer.
1004 * @param cls must be a transmit handle that determines the peer
1005 * to which we will try to connect
1006 * @param tc scheduler information about why we were triggered (not used)
1009 try_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1011 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
1013 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1019 * Remove neighbour from our list. Will automatically
1020 * trigger a re-connect attempt if we have messages pending
1023 * @param h our state
1024 * @param peer the peer to remove
1027 remove_neighbour (struct GNUNET_TRANSPORT_Handle *h,
1028 const struct GNUNET_PeerIdentity *peer)
1030 struct NeighbourList *prev;
1031 struct NeighbourList *pos;
1032 struct GNUNET_TRANSPORT_TransmitHandle *th;
1035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1036 "Removing neighbour `%s' from list of connected peers.\n",
1040 pos = h->neighbours;
1041 while ((pos != NULL) &&
1042 (0 != memcmp (peer, &pos->id, sizeof (struct GNUNET_PeerIdentity))))
1053 h->neighbours = pos->next;
1055 prev->next = pos->next;
1056 if (NULL != (th = pos->transmit_handle))
1058 pos->transmit_handle = NULL;
1059 th->neighbour = NULL;
1060 remove_from_any_list (th);
1061 if (GNUNET_TIME_absolute_get_remaining (th->timeout).value <=
1062 CONNECT_RETRY_TIMEOUT.value)
1065 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1067 ("Connection with `%4s' failed and timeout was in the past, giving up on message delivery.\n"),
1069 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == th->notify_delay_task);
1070 peer_transmit_timeout (th, NULL);
1074 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1076 ("Connection with `%4s' failed, will keep trying for %llu ms to deliver message\n"),
1078 GNUNET_TIME_absolute_get_remaining (th->timeout).value);
1079 /* try again in a bit */
1080 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == th->notify_delay_task);
1081 th->notify_delay_task
1082 = GNUNET_SCHEDULER_add_delayed (h->sched,
1083 CONNECT_RETRY_TIMEOUT,
1084 &try_connect_task, th);
1087 if (h->nc_cb != NULL)
1088 h->nd_cb (h->cls, peer);
1094 * Try again to connect to transport service.
1097 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1099 struct GNUNET_TRANSPORT_Handle *h = cls;
1100 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1101 struct NeighbourList *n;
1103 /* Forget about all neighbours that we used to be connected
1105 while (NULL != (n = h->neighbours))
1106 remove_neighbour (h, &n->id);
1108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n");
1110 GNUNET_assert (h->client == NULL);
1111 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1112 h->client = GNUNET_CLIENT_connect (h->sched, "transport", h->cfg);
1113 GNUNET_assert (h->client != NULL);
1114 /* make sure we don't send "START" twice,
1115 remove existing entry from queue (if present) */
1116 pos = h->connect_ready_head;
1119 if (pos->notify == &send_start)
1121 if (pos->prev == NULL)
1122 h->connect_ready_head = pos->next;
1124 pos->prev->next = pos->next;
1125 if (pos->next != NULL)
1126 pos->next->prev = pos->prev;
1127 GNUNET_assert (pos->neighbour == NULL);
1128 if (GNUNET_SCHEDULER_NO_TASK != pos->notify_delay_task)
1130 GNUNET_SCHEDULER_cancel (h->sched, pos->notify_delay_task);
1131 pos->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1138 schedule_control_transmit (h,
1139 sizeof (struct GNUNET_MessageHeader),
1141 GNUNET_TIME_UNIT_FOREVER_REL, &send_start, NULL);
1142 GNUNET_CLIENT_receive (h->client,
1143 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1148 * Function that will schedule the job that will try
1149 * to connect us again to the client.
1152 schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
1155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1156 "Scheduling task to reconnect to transport service in %llu ms.\n",
1157 h->reconnect_delay.value);
1159 GNUNET_assert (h->client == NULL);
1160 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
1162 = GNUNET_SCHEDULER_add_delayed (h->sched,
1163 h->reconnect_delay, &reconnect, h);
1164 h->reconnect_delay = GNUNET_TIME_UNIT_SECONDS;
1169 * We are connected to the respective peer, check the
1170 * bandwidth limits and schedule the transmission.
1172 static void schedule_request (struct GNUNET_TRANSPORT_TransmitHandle *th);
1176 * Function called by the scheduler when the timeout
1177 * for bandwidth availablility for the target
1178 * neighbour is reached.
1181 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1183 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
1185 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1186 schedule_request (th);
1191 * Remove the given transmit handle from the wait list. Does NOT free
1195 remove_from_wait_list (struct GNUNET_TRANSPORT_TransmitHandle *th)
1197 if (th->prev == NULL)
1198 th->handle->connect_wait_head = th->next;
1200 th->prev->next = th->next;
1201 if (th->next != NULL)
1202 th->next->prev = th->prev;
1207 * We are connected to the respective peer, check the
1208 * bandwidth limits and schedule the transmission.
1211 schedule_request (struct GNUNET_TRANSPORT_TransmitHandle *th)
1213 struct GNUNET_TRANSPORT_Handle *h;
1214 struct GNUNET_TIME_Relative duration;
1215 struct NeighbourList *n;
1220 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1222 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
1223 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1225 /* check outgoing quota */
1226 duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
1227 if (duration.value > MIN_QUOTA_REFRESH_TIME)
1230 duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
1232 available = duration.value * n->quota_out;
1233 if (available < n->last_sent + th->notify_size)
1235 /* calculate how much bandwidth we'd still need to
1236 accumulate and based on that how long we'll have
1238 available = n->last_sent + th->notify_size - available;
1239 duration = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1240 available / n->quota_out);
1241 if (th->timeout.value <
1242 GNUNET_TIME_relative_to_absolute (duration).value)
1244 /* signal timeout! */
1246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1247 "Would need %llu ms before bandwidth is available for delivery to `%4s', that is too long. Signaling timeout.\n",
1248 duration.value, GNUNET_i2s (&th->target));
1250 remove_from_wait_list (th);
1251 if (NULL != th->notify)
1252 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
1257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1258 "Need more bandwidth, delaying delivery to `%4s' by %llu ms\n",
1259 GNUNET_i2s (&th->target), duration.value);
1261 th->notify_delay_task
1262 = GNUNET_SCHEDULER_add_delayed (h->sched,
1263 duration, &transmit_ready, th);
1267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1268 "Bandwidth available for transmission to `%4s'\n",
1269 GNUNET_i2s (&n->id));
1271 if (GNUNET_NO == n->transmit_ok)
1273 /* we may be ready, but transport service is not;
1274 wait for SendOkMessage or timeout */
1276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1277 "Need to wait for transport service `%s' message\n",
1280 th->notify_delay_task
1281 = GNUNET_SCHEDULER_add_delayed (h->sched,
1282 GNUNET_TIME_absolute_get_remaining
1283 (th->timeout), &peer_transmit_timeout,
1287 n->transmit_ok = GNUNET_NO;
1288 remove_from_wait_list (th);
1290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1291 "Moving message for `%4s' to ready list\n",
1292 GNUNET_i2s (&n->id));
1294 insert_transmit_handle (&h->connect_ready_head, th);
1295 if (GNUNET_NO == h->transmission_scheduled)
1296 schedule_transmission (h);
1301 * Add neighbour to our list
1304 add_neighbour (struct GNUNET_TRANSPORT_Handle *h,
1306 struct GNUNET_TIME_Relative latency,
1308 const struct GNUNET_PeerIdentity *pid)
1310 struct NeighbourList *n;
1311 struct GNUNET_TRANSPORT_TransmitHandle *prev;
1312 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1313 struct GNUNET_TRANSPORT_TransmitHandle *next;
1315 /* check for duplicates */
1316 if (NULL != find_neighbour (h, pid))
1322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1323 "Creating entry for new neighbour `%4s'.\n", GNUNET_i2s (pid));
1325 n = GNUNET_malloc (sizeof (struct NeighbourList));
1327 n->last_quota_update = GNUNET_TIME_absolute_get ();
1328 n->quota_out = quota_out;
1329 n->next = h->neighbours;
1330 n->transmit_ok = GNUNET_YES;
1332 if (h->nc_cb != NULL)
1333 h->nc_cb (h->cls, &n->id, latency, distance);
1335 pos = h->connect_wait_head;
1339 if (0 == memcmp (pid,
1340 &pos->target, sizeof (struct GNUNET_PeerIdentity)))
1343 GNUNET_assert (NULL == n->transmit_handle);
1344 n->transmit_handle = pos;
1346 h->connect_wait_head = next;
1350 if (GNUNET_YES == n->received_ack)
1354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1355 "Found pending request for `%4s' will trigger it now.\n",
1356 GNUNET_i2s (&pos->target));
1358 if (pos->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1360 GNUNET_SCHEDULER_cancel (h->sched, pos->notify_delay_task);
1361 pos->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1363 schedule_request (pos);
1377 * Connect to the transport service. Note that the connection may
1378 * complete (or fail) asynchronously.
1381 * @param sched scheduler to use
1382 * @param cfg configuration to use
1383 * @param cls closure for the callbacks
1384 * @param rec receive function to call
1385 * @param nc function to call on connect events
1386 * @param nd function to call on disconnect events
1388 struct GNUNET_TRANSPORT_Handle *
1389 GNUNET_TRANSPORT_connect (struct GNUNET_SCHEDULER_Handle *sched,
1390 const struct GNUNET_CONFIGURATION_Handle *cfg,
1392 GNUNET_TRANSPORT_ReceiveCallback rec,
1393 GNUNET_TRANSPORT_NotifyConnect nc,
1394 GNUNET_TRANSPORT_NotifyDisconnect nd)
1396 struct GNUNET_TRANSPORT_Handle *ret;
1398 GNUNET_ARM_start_services (cfg, sched, "peerinfo", "transport", NULL);
1399 ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Handle));
1406 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1407 schedule_reconnect (ret);
1413 * Disconnect from the transport service.
1416 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1418 struct GNUNET_TRANSPORT_TransmitHandle *th;
1419 struct NeighbourList *n;
1420 struct HelloWaitList *hwl;
1421 struct GNUNET_CLIENT_Connection *client;
1424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transport disconnect called!\n");
1426 while (NULL != (th = handle->connect_ready_head))
1428 handle->connect_ready_head = th->next;
1429 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1431 GNUNET_SCHEDULER_cancel (handle->sched, th->notify_delay_task);
1432 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1434 if (NULL != th->notify)
1435 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
1438 while (NULL != (th = handle->connect_wait_head))
1440 handle->connect_wait_head = th->next;
1441 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1443 GNUNET_SCHEDULER_cancel (handle->sched, th->notify_delay_task);
1444 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1446 if (NULL != th->notify)
1447 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
1450 while (NULL != (n = handle->neighbours))
1452 handle->neighbours = n->next;
1453 if (NULL != (th = n->transmit_handle))
1455 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1457 GNUNET_SCHEDULER_cancel (handle->sched, th->notify_delay_task);
1458 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1460 if (NULL != th->notify)
1461 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
1466 while (NULL != (hwl = handle->hwl_head))
1468 handle->hwl_head = hwl->next;
1469 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1471 ("Disconnect while notification for `%s' still registered.\n"),
1473 if (hwl->rec != NULL)
1474 hwl->rec (hwl->rec_cls, NULL);
1477 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1479 GNUNET_SCHEDULER_cancel (handle->sched, handle->reconnect_task);
1480 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1482 GNUNET_free_non_null (handle->my_hello);
1483 handle->my_hello = NULL;
1484 GNUNET_ARM_stop_services (handle->cfg, handle->sched, "transport",
1486 if (NULL != handle->network_handle)
1488 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->network_handle);
1489 handle->network_handle = NULL;
1491 if (NULL != (client = handle->client))
1494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1495 "Disconnecting from transport service for good.\n");
1497 handle->client = NULL;
1498 GNUNET_CLIENT_disconnect (client);
1500 GNUNET_free (handle);
1505 * Type of a function to call when we receive a message
1508 * @param cls closure
1509 * @param msg message received, NULL on timeout or fatal error
1512 demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
1514 struct GNUNET_TRANSPORT_Handle *h = cls;
1515 const struct DisconnectInfoMessage *dim;
1516 const struct ConnectInfoMessage *cim;
1517 const struct InboundMessage *im;
1518 const struct GNUNET_MessageHeader *imm;
1519 const struct SendOkMessage *okm;
1520 struct HelloWaitList *hwl;
1521 struct HelloWaitList *next_hwl;
1522 struct NeighbourList *n;
1523 struct GNUNET_PeerIdentity me;
1524 struct GNUNET_TRANSPORT_TransmitHandle *th;
1526 struct GNUNET_TRANSPORT_TransmitHandle *prev;
1527 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1528 struct GNUNET_TRANSPORT_TransmitHandle *next;
1531 if ((msg == NULL) || (h->client == NULL))
1533 if (h->client != NULL)
1536 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1537 "Error receiving from transport service, disconnecting temporarily.\n");
1539 if (h->network_handle != NULL)
1541 GNUNET_CLIENT_notify_transmit_ready_cancel (h->network_handle);
1542 h->network_handle = NULL;
1543 h->transmission_scheduled = GNUNET_NO;
1544 th = h->connect_ready_head;
1545 /* add timeout again, we canceled the transmit_ready task! */
1547 /*GNUNET_assert (th->notify_delay_task ==
1548 GNUNET_SCHEDULER_NO_TASK);*/
1550 /* START - somehow we are getting here when th->notify_delay_task is already
1551 * set. Not sure why, so just checking and canceling instead of asserting and
1552 * dying. Probably not a *fix*. */
1553 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1555 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
1556 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1559 GNUNET_assert (th->notify_delay_task ==
1560 GNUNET_SCHEDULER_NO_TASK);
1561 th->notify_delay_task =
1562 GNUNET_SCHEDULER_add_delayed (h->sched,
1563 GNUNET_TIME_absolute_get_remaining
1565 &peer_transmit_timeout, th);
1567 GNUNET_CLIENT_disconnect (h->client);
1569 schedule_reconnect (h);
1573 /* shutdown initiated from 'GNUNET_TRANSPORT_disconnect',
1574 finish clean up work! */
1579 GNUNET_CLIENT_receive (h->client,
1580 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1581 size = ntohs (msg->size);
1582 switch (ntohs (msg->type))
1584 case GNUNET_MESSAGE_TYPE_HELLO:
1586 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg,
1593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1594 "Receiving (my own) `%s' message, I am `%4s'.\n",
1595 "HELLO", GNUNET_i2s (&me));
1597 GNUNET_free_non_null (h->my_hello);
1599 if (size < sizeof (struct GNUNET_MessageHeader))
1604 h->my_hello = GNUNET_malloc (size);
1605 memcpy (h->my_hello, msg, size);
1609 next_hwl = hwl->next;
1610 hwl->rec (hwl->rec_cls,
1611 (const struct GNUNET_MessageHeader *) h->my_hello);
1615 case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
1616 if (size != sizeof (struct ConnectInfoMessage))
1621 cim = (const struct ConnectInfoMessage *) msg;
1623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1624 "Receiving `%s' message for `%4s'.\n",
1625 "CONNECT", GNUNET_i2s (&cim->id));
1627 if (NULL == (n = find_neighbour(h, &cim->id)))
1630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1631 "Don't know neighbor, adding!\n");
1634 ntohl (cim->quota_out),
1635 GNUNET_TIME_relative_ntoh (cim->latency), ntohs(cim->distance), &cim->id);
1640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1641 "Do know neighbor, scheduling transmission!\n");
1644 n->received_ack = GNUNET_YES;
1646 if (NULL != n->transmit_handle)
1649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1650 "Peer connected, scheduling delayed message for delivery now.\n");
1652 schedule_request (n->transmit_handle);
1657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1658 "Transmit handle is null... Checking for pending stuff(?)\n");
1661 pos = h->connect_wait_head;
1665 if (0 == memcmp (&cim->id,
1666 &pos->target, sizeof (struct GNUNET_PeerIdentity)))
1669 GNUNET_assert (NULL == n->transmit_handle);
1670 n->transmit_handle = pos;
1672 h->connect_wait_head = next;
1676 if (GNUNET_YES == n->received_ack)
1680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1681 "Found pending request for `%4s' will trigger it now.\n",
1682 GNUNET_i2s (&pos->target));
1684 if (pos->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1686 GNUNET_SCHEDULER_cancel (h->sched, pos->notify_delay_task);
1687 pos->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1689 schedule_request (pos);
1703 case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
1704 if (size != sizeof (struct DisconnectInfoMessage))
1709 dim = (const struct DisconnectInfoMessage *) msg;
1711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1712 "Receiving `%s' message for `%4s'.\n",
1713 "DISCONNECT", GNUNET_i2s (&dim->peer));
1715 remove_neighbour (h, &dim->peer);
1717 case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
1718 if (size != sizeof (struct SendOkMessage))
1723 okm = (const struct SendOkMessage *) msg;
1725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1726 "Receiving `%s' message, transmission %s.\n", "SEND_OK",
1727 ntohl (okm->success) == GNUNET_OK ? "succeeded" : "failed");
1729 n = find_neighbour (h, &okm->peer);
1730 GNUNET_assert (n != NULL);
1731 n->transmit_ok = GNUNET_YES;
1732 if (n->transmit_handle != NULL)
1735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1736 "Processing pending message for `%4s'\n",
1737 GNUNET_i2s (&n->id));
1739 GNUNET_SCHEDULER_cancel (h->sched,
1740 n->transmit_handle->notify_delay_task);
1741 n->transmit_handle->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1742 schedule_request (n->transmit_handle);
1745 case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
1747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1748 "Receiving `%s' message.\n", "RECV");
1751 sizeof (struct InboundMessage) +
1752 sizeof (struct GNUNET_MessageHeader))
1757 im = (const struct InboundMessage *) msg;
1758 imm = (const struct GNUNET_MessageHeader *) &im[1];
1759 if (ntohs (imm->size) + sizeof (struct InboundMessage) != size)
1764 switch (ntohs (imm->type))
1766 case GNUNET_MESSAGE_TYPE_TRANSPORT_ACK:
1768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1769 "Receiving `%s' message from `%4s'.\n",
1770 "ACK", GNUNET_i2s (&im->peer));
1775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1776 "Received message of type %u from `%4s'.\n",
1777 ntohs (imm->type), GNUNET_i2s (&im->peer));
1780 n = find_neighbour (h, &im->peer);
1787 if (NULL != n->transmit_handle)
1790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1791 "Peer connected, scheduling delayed message for delivery now.\n");
1793 schedule_request (n->transmit_handle);
1796 h->rec (h->cls, &im->peer, imm,
1797 GNUNET_TIME_relative_ntoh (im->latency), ntohs(im->distance));
1802 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1804 ("Received unexpected message of type %u in %s:%u\n"),
1805 ntohs (msg->type), __FILE__, __LINE__);
1812 struct ClientTransmitWrapper
1814 GNUNET_CONNECTION_TransmitReadyNotify notify;
1816 struct GNUNET_TRANSPORT_TransmitHandle *th;
1821 * Transmit message of a client destined for another
1822 * peer to the service.
1825 client_notify_wrapper (void *cls, size_t size, void *buf)
1827 struct ClientTransmitWrapper *ctw = cls;
1828 struct OutboundMessage *obm;
1829 struct GNUNET_MessageHeader *hdr;
1835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1836 "Transmission request could not be satisfied.\n");
1838 if (NULL != ctw->notify)
1839 GNUNET_assert (0 == ctw->notify (ctw->notify_cls, 0, NULL));
1843 GNUNET_assert (size >= sizeof (struct OutboundMessage));
1845 if (ctw->notify != NULL)
1846 ret = ctw->notify (ctw->notify_cls,
1847 size - sizeof (struct OutboundMessage),
1853 /* Need to reset flag, no SEND means no SEND_OK! */
1854 ctw->th->neighbour->transmit_ok = GNUNET_YES;
1858 GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
1859 hdr = (struct GNUNET_MessageHeader *) &obm[1];
1860 GNUNET_assert (ntohs (hdr->size) == ret);
1861 GNUNET_assert (ret + sizeof (struct OutboundMessage) <
1862 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1865 "Transmitting `%s' message with data for `%4s'\n",
1866 "SEND", GNUNET_i2s (&ctw->th->target));
1868 ret += sizeof (struct OutboundMessage);
1869 obm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
1870 obm->header.size = htons (ret);
1871 obm->priority = htonl (ctw->th->priority);
1872 obm->peer = ctw->th->target;
1880 * Check if we could queue a message of the given size for
1881 * transmission. The transport service will take both its
1882 * internal buffers and bandwidth limits imposed by the
1883 * other peer into consideration when answering this query.
1885 * @param handle connection to transport service
1886 * @param target who should receive the message
1887 * @param size how big is the message we want to transmit?
1888 * @param priority how important is the message?
1889 * @param timeout after how long should we give up (and call
1890 * notify with buf NULL and size 0)?
1891 * @param notify function to call when we are ready to
1892 * send such a message
1893 * @param notify_cls closure for notify
1894 * @return NULL if someone else is already waiting to be notified
1895 * non-NULL if the notify callback was queued (can be used to cancel
1896 * using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1898 struct GNUNET_TRANSPORT_TransmitHandle *
1899 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle
1901 const struct GNUNET_PeerIdentity
1902 *target, size_t size,
1903 unsigned int priority,
1904 struct GNUNET_TIME_Relative timeout,
1905 GNUNET_CONNECTION_TransmitReadyNotify
1906 notify, void *notify_cls)
1908 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1909 struct GNUNET_TRANSPORT_TransmitHandle *th;
1910 struct NeighbourList *n;
1911 struct ClientTransmitWrapper *ctw;
1913 if (size + sizeof (struct OutboundMessage) >=
1914 GNUNET_SERVER_MAX_MESSAGE_SIZE)
1917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1918 "Message size is %d, max allowed is %d.\n",
1919 size + sizeof (struct OutboundMessage), GNUNET_SERVER_MAX_MESSAGE_SIZE);
1925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1926 "Asking transport service for transmission of %u bytes to peer `%4s'.\n",
1927 size, GNUNET_i2s (target));
1929 n = find_neighbour (handle, target);
1930 if ((n != NULL) && (n->transmit_handle != NULL))
1931 return NULL; /* already have a request pending for this peer! */
1932 ctw = GNUNET_malloc (sizeof (struct ClientTransmitWrapper));
1933 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
1934 ctw->notify = notify;
1935 ctw->notify_cls = notify_cls;
1937 th->handle = handle;
1939 th->target = *target;
1940 th->notify = &client_notify_wrapper;
1941 th->notify_cls = ctw;
1942 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1943 th->notify_size = size + sizeof (struct OutboundMessage);
1944 th->priority = priority;
1947 pos = handle->connect_wait_head;
1950 GNUNET_assert (0 != memcmp (target,
1952 sizeof (struct GNUNET_PeerIdentity)));
1956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1957 "Will now try to connect to `%4s'.\n", GNUNET_i2s (target));
1964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1965 "Transmission request queued for transmission to transport service.\n");
1967 GNUNET_assert (NULL == n->transmit_handle);
1968 n->transmit_handle = th;
1969 if (GNUNET_YES != n->transmit_ok)
1972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1973 "Connection to `%4s' is not yet confirmed connected, scheduling timeout (%llu ms) only.\n",
1974 GNUNET_i2s (target), timeout.value);
1976 th->notify_delay_task
1977 = GNUNET_SCHEDULER_add_delayed (handle->sched,
1978 timeout, &peer_transmit_timeout, th);
1983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1984 "Peer `%4s' is ready to receive, scheduling message for delivery now.\n",
1985 GNUNET_i2s (target));
1987 th->notify_delay_task
1988 = GNUNET_SCHEDULER_add_now (handle->sched, &transmit_ready, th);
1994 * Cancel the specified transmission-ready notification.
1997 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
1998 GNUNET_TRANSPORT_TransmitHandle
2001 struct GNUNET_TRANSPORT_Handle *h;
2004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2005 "Transmission request of %u bytes to `%4s' was cancelled.\n",
2006 th->notify_size - sizeof (struct OutboundMessage),
2007 GNUNET_i2s (&th->target));
2009 GNUNET_assert (th->notify == &client_notify_wrapper);
2010 remove_from_any_list (th);
2012 if ((h->connect_ready_head == NULL) && (h->network_handle != NULL))
2014 GNUNET_CLIENT_notify_transmit_ready_cancel (h->network_handle);
2015 h->network_handle = NULL;
2016 h->transmission_scheduled = GNUNET_NO;
2018 GNUNET_free (th->notify_cls);
2019 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
2024 /* end of transport_api.c */