2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/transport_api.c
23 * @brief library to access the low-level P2P IO service
24 * @author Christian Grothoff
27 * - set_quota with low bandwidth should cause peer
28 * disconnects (currently never does that) (MINOR)
31 #include "gnunet_client_lib.h"
32 #include "gnunet_arm_service.h"
33 #include "gnunet_hello_lib.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_server_lib.h"
36 #include "gnunet_time_lib.h"
37 #include "gnunet_transport_service.h"
38 #include "transport.h"
41 * After how long do we give up on transmitting a HELLO
44 #define OFFER_HELLO_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
47 * After how long do we give automatically retry an unsuccessful
50 #define CONNECT_RETRY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 750)
53 * How long should ARM wait when starting up the
54 * transport service before reporting back?
56 #define START_SERVICE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
59 * How long should ARM wait when stopping the
60 * transport service before reporting back?
62 #define STOP_SERVICE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
65 * Entry in linked list of all of our current neighbours.
71 * This is a linked list.
73 struct NeighbourList *next;
76 * Active transmit handle, can be NULL. Used to move
77 * from ready to wait list on disconnect and to block
78 * two transmissions to the same peer from being scheduled
81 struct GNUNET_TRANSPORT_TransmitHandle *transmit_handle;
85 * Identity of this neighbour.
87 struct GNUNET_PeerIdentity id;
90 * At what time did we reset last_sent last?
92 struct GNUNET_TIME_Absolute last_quota_update;
95 * How many bytes have we sent since the "last_quota_update"
101 * Quota for outbound traffic to the neighbour in bytes/ms.
106 * Set to GNUNET_YES if we are currently allowed to
107 * transmit a message to the transport service for this
108 * peer, GNUNET_NO otherwise.
113 * Set to GNUNET_YES if we have received an ACK for the
114 * given peer. Peers that receive our HELLO always respond
115 * with an ACK to let us know that we are successfully
116 * communicating. Note that a PING can not be used for this
117 * since PINGs are only send if a HELLO address requires
118 * confirmation (and also, PINGs are not passed to the
119 * transport API itself).
127 * Linked list of requests from clients for our HELLO
128 * that were deferred.
134 * This is a linked list.
136 struct HelloWaitList *next;
139 * Reference back to our transport handle.
141 struct GNUNET_TRANSPORT_Handle *handle;
144 * Callback to call once we got our HELLO.
146 GNUNET_TRANSPORT_ReceiveCallback rec;
154 * When to time out (call rec with NULL).
156 struct GNUNET_TIME_Absolute timeout;
159 * Timeout task (used to trigger timeout,
160 * cancel if we get the HELLO in time).
162 GNUNET_SCHEDULER_TaskIdentifier task;
169 * Opaque handle for a transmission-ready request.
171 struct GNUNET_TRANSPORT_TransmitHandle
175 * We keep the transmit handles that are waiting for
176 * a transport-level connection in a doubly linked list.
178 struct GNUNET_TRANSPORT_TransmitHandle *next;
181 * We keep the transmit handles that are waiting for
182 * a transport-level connection in a doubly linked list.
184 struct GNUNET_TRANSPORT_TransmitHandle *prev;
187 * Handle of the main transport data structure.
189 struct GNUNET_TRANSPORT_Handle *handle;
192 * Neighbour for this handle, can be NULL if the service
193 * is not yet connected to the target.
195 struct NeighbourList *neighbour;
198 * Which peer is this transmission going to be for? All
199 * zeros if it is control-traffic to the service.
201 struct GNUNET_PeerIdentity target;
204 * Function to call when notify_size bytes are available
207 GNUNET_NETWORK_TransmitReadyNotify notify;
210 * Closure for notify.
215 * transmit_ready task Id. The task is used to introduce the
216 * artificial delay that may be required to maintain the bandwidth
217 * limits. Later, this will be the ID of the "transmit_timeout"
218 * task which is used to signal a timeout if the transmission could
219 * not be done in a timely fashion.
221 GNUNET_SCHEDULER_TaskIdentifier notify_delay_task;
224 * Timeout for this request.
226 struct GNUNET_TIME_Absolute timeout;
229 * How many bytes is our notify callback waiting for?
234 * How important is this message?
236 unsigned int priority;
242 * Handle for the transport service (includes all of the
243 * state for the transport service).
245 struct GNUNET_TRANSPORT_Handle
249 * Closure for the callbacks.
254 * Function to call for received data.
256 GNUNET_TRANSPORT_ReceiveCallback rec;
259 * function to call on connect events
261 GNUNET_TRANSPORT_NotifyConnect nc_cb;
264 * function to call on disconnect events
266 GNUNET_TRANSPORT_NotifyDisconnect nd_cb;
269 * The current HELLO message for this peer. Updated
270 * whenever transports change their addresses.
272 struct GNUNET_HELLO_Message *my_hello;
275 * My client connection to the transport service.
277 struct GNUNET_CLIENT_Connection *client;
280 * Handle to our registration with the client for notification.
282 struct GNUNET_NETWORK_TransmitHandle *network_handle;
285 * Linked list of transmit handles that are waiting for the
286 * transport to connect to the respective peer. When we
287 * receive notification that the transport connected to a
288 * peer, we go over this list and check if someone has already
289 * requested a transmission to the new peer; if so, we trigger
292 struct GNUNET_TRANSPORT_TransmitHandle *connect_wait_head;
295 * Linked list of transmit handles that are waiting for the
296 * transport to be ready for transmission to the respective
298 * receive notification that the transport disconnected from
299 * a peer, we go over this list and move the entry back to
300 * the connect_wait list.
302 struct GNUNET_TRANSPORT_TransmitHandle *connect_ready_head;
305 * Linked list of pending requests for our HELLO.
307 struct HelloWaitList *hwl_head;
312 struct GNUNET_SCHEDULER_Handle *sched;
317 struct GNUNET_CONFIGURATION_Handle *cfg;
320 * Linked list of the current neighbours of this peer.
322 struct NeighbourList *neighbours;
325 * ID of the task trying to reconnect to the
328 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
331 * Delay until we try to reconnect.
333 struct GNUNET_TIME_Relative reconnect_delay;
336 * Do we currently have a transmission pending?
337 * (schedule transmission was called but has not
340 int transmission_scheduled;
344 static struct NeighbourList *
345 find_neighbour (struct GNUNET_TRANSPORT_Handle *h,
346 const struct GNUNET_PeerIdentity *peer)
348 struct NeighbourList *pos;
351 while ((pos != NULL) &&
352 (0 != memcmp (peer, &pos->id, sizeof (struct GNUNET_PeerIdentity))))
359 * Schedule the task to send one message from the
360 * connect_ready list to the service.
362 static void schedule_transmission (struct GNUNET_TRANSPORT_Handle *h);
366 * Transmit message to client...
369 transport_notify_ready (void *cls, size_t size, void *buf)
371 struct GNUNET_TRANSPORT_Handle *h = cls;
372 struct GNUNET_TRANSPORT_TransmitHandle *th;
373 struct NeighbourList *n;
377 h->network_handle = NULL;
378 h->transmission_scheduled = GNUNET_NO;
382 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
383 "Could not transmit to transport service, cancelling pending requests\n");
385 th = h->connect_ready_head;
386 if (th->next != NULL)
387 th->next->prev = NULL;
388 h->connect_ready_head = th->next;
389 if (NULL != (n = th->neighbour))
391 GNUNET_assert (n->transmit_handle == th);
392 n->transmit_handle = NULL;
394 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
400 "Ready to transmit %u bytes to transport service\n", size);
404 h->network_handle = NULL;
405 h->transmission_scheduled = GNUNET_NO;
408 th = h->connect_ready_head;
409 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
411 /* remove existing time out task (only applies if
412 this is not the first iteration of the loop) */
413 GNUNET_SCHEDULER_cancel (h->sched,
414 th->notify_delay_task);
415 th->notify_delay_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
417 GNUNET_assert (th->notify_size <= size);
418 if (th->next != NULL)
419 th->next->prev = NULL;
420 h->connect_ready_head = th->next;
421 if (NULL != (n = th->neighbour))
423 GNUNET_assert (n->transmit_handle == th);
424 n->transmit_handle = NULL;
426 ret += th->notify (th->notify_cls, size, &cbuf[ret]);
432 while ((h->connect_ready_head != NULL) &&
433 (h->connect_ready_head->notify_size <= size));
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_PREREQUISITE_TASK)
465 /* remove existing time out task, will be integrated
466 with transmit_ready notification! */
467 GNUNET_SCHEDULER_cancel (h->sched,
468 th->notify_delay_task);
469 th->notify_delay_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
471 h->transmission_scheduled = GNUNET_YES;
472 h->network_handle = GNUNET_CLIENT_notify_transmit_ready (h->client,
474 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_PREREQUISITE_TASK)
535 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
536 th->notify_delay_task = GNUNET_SCHEDULER_NO_PREREQUISITE_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
563 try_connect (struct GNUNET_TRANSPORT_TransmitHandle *th);
567 * Called when our transmit request timed out before any transport
568 * reported success connecting to the desired peer or before the
569 * transport was ready to receive. Signal error and free
573 transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
575 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
577 th->notify_delay_task = GNUNET_SCHEDULER_NO_PREREQUISITE_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 th->notify (th->notify_cls, 0, NULL);
594 * Queue control request for transmission to the transport
597 * @param size number of bytes to be transmitted
598 * @param at_head request must be added to the head of the queue
599 * (otherwise request will be appended)
600 * @param timeout how long this transmission can wait (at most)
601 * @param notify function to call to get the content
602 * @param notify_cls closure for notify
605 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h,
608 struct GNUNET_TIME_Relative timeout,
609 GNUNET_NETWORK_TransmitReadyNotify notify,
612 struct GNUNET_TRANSPORT_TransmitHandle *th;
614 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
617 th->notify_cls = notify_cls;
618 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
619 th->notify_size = size;
620 th->notify_delay_task
621 = GNUNET_SCHEDULER_add_delayed (h->sched,
623 GNUNET_SCHEDULER_PRIORITY_KEEP,
624 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
626 &transmit_timeout, th);
629 th->next = h->connect_ready_head;
630 h->connect_ready_head = th;
631 if (th->next != NULL)
636 insert_transmit_handle (&h->connect_ready_head, th);
638 if (GNUNET_NO == h->transmission_scheduled)
639 schedule_transmission (h);
644 * Update the quota values for the given neighbour now.
647 update_quota (struct NeighbourList *n)
649 struct GNUNET_TIME_Relative delta;
653 delta = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
654 allowed = delta.value * n->quota_out;
655 if (n->last_sent < allowed)
657 remaining = allowed - n->last_sent;
658 if (n->quota_out > 0)
659 remaining /= n->quota_out;
662 if (remaining > MAX_BANDWIDTH_CARRY)
663 remaining = MAX_BANDWIDTH_CARRY;
665 n->last_quota_update = GNUNET_TIME_absolute_get ();
666 n->last_quota_update.value -= remaining;
670 n->last_sent -= allowed;
671 n->last_quota_update = GNUNET_TIME_absolute_get ();
676 struct SetQuotaContext
678 struct GNUNET_TRANSPORT_Handle *handle;
680 struct GNUNET_PeerIdentity target;
682 GNUNET_SCHEDULER_Task cont;
686 struct GNUNET_TIME_Absolute timeout;
693 send_set_quota (void *cls, size_t size, void *buf)
695 struct SetQuotaContext *sqc = cls;
696 struct QuotaSetMessage *msg;
700 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
704 GNUNET_SCHEDULER_REASON_TIMEOUT);
709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
710 "Transmitting `%s' request with respect to `%4s'.\n",
711 "SET_QUOTA", GNUNET_i2s (&sqc->target));
713 GNUNET_assert (size >= sizeof (struct QuotaSetMessage));
715 msg->header.size = htons (sizeof (struct QuotaSetMessage));
716 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
717 msg->quota_in = htonl (sqc->quota_in);
718 memcpy (&msg->peer, &sqc->target, sizeof (struct GNUNET_PeerIdentity));
719 if (sqc->cont != NULL)
720 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
724 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
726 return sizeof (struct QuotaSetMessage);
731 * Set the share of incoming bandwidth for the given
732 * peer to the specified amount.
734 * @param handle connection to transport service
735 * @param target who's bandwidth quota is being changed
736 * @param quota_in incoming bandwidth quota in bytes per ms; 0 can
737 * be used to force all traffic to be discarded
738 * @param quota_out outgoing bandwidth quota in bytes per ms; 0 can
739 * be used to force all traffic to be discarded
740 * @param timeout how long to wait until signaling failure if
741 * we can not communicate the quota change
742 * @param cont continuation to call when done, will be called
743 * either with reason "TIMEOUT" or with reason "PREREQ_DONE"
744 * @param cont_cls closure for continuation
747 GNUNET_TRANSPORT_set_quota (struct GNUNET_TRANSPORT_Handle *handle,
748 const struct GNUNET_PeerIdentity *target,
751 struct GNUNET_TIME_Relative timeout,
752 GNUNET_SCHEDULER_Task cont, void *cont_cls)
754 struct NeighbourList *n;
755 struct SetQuotaContext *sqc;
757 n = find_neighbour (handle, target);
761 if (n->quota_out < quota_out)
762 n->last_quota_update = GNUNET_TIME_absolute_get ();
763 n->quota_out = quota_out;
765 sqc = GNUNET_malloc (sizeof (struct SetQuotaContext));
766 sqc->handle = handle;
767 sqc->target = *target;
769 sqc->cont_cls = cont_cls;
770 sqc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
771 sqc->quota_in = quota_in;
772 schedule_control_transmit (handle,
773 sizeof (struct QuotaSetMessage),
774 GNUNET_NO, timeout, &send_set_quota, sqc);
779 * 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;
790 pos = hwl->handle->hwl_head;
793 GNUNET_assert (pos != NULL);
798 hwl->handle->hwl_head = hwl->next;
800 prev->next = hwl->next;
801 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
802 _("Timeout trying to obtain `%s' from transport service.\n"),
805 if (hwl->rec != NULL)
806 hwl->rec (hwl->rec_cls, GNUNET_TIME_UNIT_ZERO, NULL, NULL);
812 * Obtain the HELLO message for this peer.
814 * @param handle connection to transport service
815 * @param timeout how long to wait for the HELLO
816 * @param rec function to call with the HELLO, sender will be our peer
817 * identity; message and sender will be NULL on timeout
818 * (handshake with transport service pending/failed).
819 * cost estimate will be 0.
820 * @param rec_cls closure for rec
823 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
824 struct GNUNET_TIME_Relative timeout,
825 GNUNET_TRANSPORT_ReceiveCallback rec,
828 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
829 struct GNUNET_PeerIdentity me;
830 struct HelloWaitList *hwl;
832 if (handle->my_hello == NULL)
834 hwl = GNUNET_malloc (sizeof (struct HelloWaitList));
835 hwl->next = handle->hwl_head;
836 handle->hwl_head = hwl;
837 hwl->handle = handle;
839 hwl->rec_cls = rec_cls;
840 hwl->timeout = GNUNET_TIME_relative_to_absolute (timeout);
841 hwl->task = GNUNET_SCHEDULER_add_delayed (handle->sched,
843 GNUNET_SCHEDULER_PRIORITY_KEEP,
844 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
846 &hello_wait_timeout, hwl);
849 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_key (handle->my_hello, &pk));
850 GNUNET_CRYPTO_hash (&pk,
851 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
855 GNUNET_TIME_UNIT_ZERO,
856 &me, (const struct GNUNET_MessageHeader *) handle->my_hello);
861 send_hello (void *cls, size_t size, void *buf)
863 struct GNUNET_MessageHeader *hello = cls;
869 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
870 "Timeout while trying to transmit `%s' request.\n",
877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
878 "Transmitting `%s' request.\n", "HELLO");
880 msize = ntohs (hello->size);
881 GNUNET_assert (size >= msize);
882 memcpy (buf, hello, msize);
889 * Offer the transport service the HELLO of another peer. Note that
890 * the transport service may just ignore this message if the HELLO is
891 * malformed or useless due to our local configuration.
893 * @param handle connection to transport service
894 * @param hello the hello message
897 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
898 const struct GNUNET_MessageHeader *hello)
900 struct GNUNET_MessageHeader *hc;
903 if (handle->client == NULL)
906 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
907 "Not connected to transport service, dropping offered HELLO\n");
911 GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
912 size = ntohs (hello->size);
913 GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
914 hc = GNUNET_malloc (size);
915 memcpy (hc, hello, size);
916 schedule_control_transmit (handle,
918 GNUNET_NO, OFFER_HELLO_TIMEOUT, &send_hello, hc);
923 * Function we use for handling incoming messages.
925 static void demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg);
929 send_start (void *cls, size_t size, void *buf)
931 struct GNUNET_MessageHeader *s = buf;
936 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
937 "Timeout while trying to transmit `%s' request.\n",
943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
944 "Transmitting `%s' request.\n", "START");
946 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
947 s->size = htons (sizeof (struct GNUNET_MessageHeader));
948 s->type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
949 return sizeof (struct GNUNET_MessageHeader);
954 * We're ready to transmit the request that the transport service
955 * should connect to a new peer. In addition to sending the
956 * request, schedule the next phase for the transmission processing
957 * that caused the connect request in the first place.
960 request_connect (void *cls, size_t size, void *buf)
962 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
963 struct TryConnectMessage *tcm;
964 struct GNUNET_TRANSPORT_Handle *h;
966 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK);
971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
972 "Failed to transmit `%s' request for `%4s' to service.\n",
974 GNUNET_i2s(&th->target));
976 th->notify (th->notify_cls, 0, NULL);
981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
982 "Transmitting `%s' message for `%4s'.\n",
983 "TRY_CONNECT", GNUNET_i2s (&th->target));
985 GNUNET_assert (size >= sizeof (struct TryConnectMessage));
987 tcm->header.size = htons (sizeof (struct TryConnectMessage));
988 tcm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TRY_CONNECT);
989 tcm->reserved = htonl (0);
990 memcpy (&tcm->peer, &th->target, sizeof (struct GNUNET_PeerIdentity));
991 th->notify_delay_task
992 = GNUNET_SCHEDULER_add_delayed (h->sched,
994 GNUNET_SCHEDULER_PRIORITY_KEEP,
995 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
996 GNUNET_TIME_absolute_get_remaining
998 &transmit_timeout, th);
999 insert_transmit_handle (&h->connect_wait_head, th);
1000 return sizeof (struct TryConnectMessage);
1005 * Schedule a request to connect to the given
1006 * neighbour (and if successful, add the specified
1007 * handle to the wait list).
1009 * @param th handle for a request to transmit once we
1013 try_connect (struct GNUNET_TRANSPORT_TransmitHandle *th)
1015 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK);
1016 schedule_control_transmit (th->handle,
1017 sizeof (struct TryConnectMessage),
1019 GNUNET_TIME_absolute_get_remaining (th->timeout),
1020 &request_connect, th);
1025 * Task for delayed attempts to reconnect to a peer.
1027 * @param cls must be a transmit handle that determines the peer
1028 * to which we will try to connect
1029 * @param tc scheduler information about why we were triggered (not used)
1032 try_connect_task (void *cls,
1033 const struct GNUNET_SCHEDULER_TaskContext *tc)
1035 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
1037 th->notify_delay_task
1038 = GNUNET_SCHEDULER_add_delayed (th->handle->sched,
1040 GNUNET_SCHEDULER_PRIORITY_KEEP,
1041 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1042 GNUNET_TIME_absolute_get_remaining
1043 (th->timeout), &transmit_timeout, th);
1049 * Remove neighbour from our list. Will automatically
1050 * trigger a re-connect attempt if we have messages pending
1053 * @param h our state
1054 * @param peer the peer to remove
1057 remove_neighbour (struct GNUNET_TRANSPORT_Handle *h,
1058 const struct GNUNET_PeerIdentity *peer)
1060 struct NeighbourList *prev;
1061 struct NeighbourList *pos;
1062 struct GNUNET_TRANSPORT_TransmitHandle *th;
1065 pos = h->neighbours;
1066 while ((pos != NULL) &&
1069 sizeof (struct GNUNET_PeerIdentity))))
1080 h->neighbours = pos->next;
1082 prev->next = pos->next;
1083 if (NULL != (th = pos->transmit_handle))
1085 pos->transmit_handle = NULL;
1086 th->neighbour = NULL;
1087 remove_from_any_list (th);
1088 if (GNUNET_TIME_absolute_get_remaining (th->timeout).value > CONNECT_RETRY_TIMEOUT.value)
1091 GNUNET_SCHEDULER_cancel (h->sched,
1092 th->notify_delay_task);
1093 transmit_timeout (th, NULL);
1097 /* try again in a bit */
1098 GNUNET_SCHEDULER_cancel (h->sched,
1099 th->notify_delay_task);
1100 th->notify_delay_task
1101 = GNUNET_SCHEDULER_add_delayed (h->sched,
1103 GNUNET_SCHEDULER_PRIORITY_KEEP,
1104 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1105 CONNECT_RETRY_TIMEOUT,
1110 if (h->nc_cb != NULL)
1111 h->nd_cb (h->cls, peer);
1117 * Try again to connect to transport service.
1120 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1122 struct GNUNET_TRANSPORT_Handle *h = cls;
1123 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1124 struct NeighbourList *n;
1126 /* Forget about all neighbours that we used to be connected
1128 while (NULL != (n = h->neighbours))
1129 remove_neighbour (h, &n->id);
1131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n");
1133 GNUNET_assert (h->client == NULL);
1134 h->reconnect_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1135 h->client = GNUNET_CLIENT_connect (h->sched, "transport", h->cfg);
1136 GNUNET_assert (h->client != NULL);
1137 /* make sure we don't send "START" twice,
1138 remove existing entry from queue (if present) */
1139 pos = h->connect_ready_head;
1142 if (pos->notify == &send_start)
1144 if (pos->prev == NULL)
1145 h->connect_ready_head = pos->next;
1147 pos->prev->next = pos->next;
1148 if (pos->next != NULL)
1149 pos->next->prev = pos->prev;
1150 GNUNET_assert (pos->neighbour == NULL);
1156 schedule_control_transmit (h,
1157 sizeof (struct GNUNET_MessageHeader),
1159 GNUNET_TIME_UNIT_FOREVER_REL, &send_start, NULL);
1160 GNUNET_CLIENT_receive (h->client,
1161 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1166 * Function that will schedule the job that will try
1167 * to connect us again to the client.
1170 schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
1173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1174 "Scheduling task to reconnect to transport service in %llu ms.\n",
1175 h->reconnect_delay.value);
1177 GNUNET_assert (h->client == NULL);
1178 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK);
1180 = GNUNET_SCHEDULER_add_delayed (h->sched,
1182 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1183 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1184 h->reconnect_delay, &reconnect, h);
1185 h->reconnect_delay = GNUNET_TIME_UNIT_SECONDS;
1190 * We are connected to the respective peer, check the
1191 * bandwidth limits and schedule the transmission.
1193 static void schedule_request (struct GNUNET_TRANSPORT_TransmitHandle *th);
1197 * Function called by the scheduler when the timeout
1198 * for bandwidth availablility for the target
1199 * neighbour is reached.
1202 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1204 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
1206 th->notify_delay_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1207 schedule_request (th);
1212 * Remove the given transmit handle from the wait list. Does NOT free
1216 remove_from_wait_list (struct GNUNET_TRANSPORT_TransmitHandle *th)
1218 if (th->prev == NULL)
1219 th->handle->connect_wait_head = th->next;
1221 th->prev->next = th->next;
1222 if (th->next != NULL)
1223 th->next->prev = th->prev;
1228 * We are connected to the respective peer, check the
1229 * bandwidth limits and schedule the transmission.
1232 schedule_request (struct GNUNET_TRANSPORT_TransmitHandle *th)
1234 struct GNUNET_TRANSPORT_Handle *h;
1235 struct GNUNET_TIME_Relative duration;
1236 struct NeighbourList *n;
1241 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1243 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
1244 th->notify_delay_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1246 /* check outgoing quota */
1247 duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
1248 if (duration.value > MIN_QUOTA_REFRESH_TIME)
1251 duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
1253 available = duration.value * n->quota_out;
1254 if (available < n->last_sent + th->notify_size)
1256 /* calculate how much bandwidth we'd still need to
1257 accumulate and based on that how long we'll have
1259 available = n->last_sent + th->notify_size - available;
1260 duration = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1261 available / n->quota_out);
1262 if (th->timeout.value <
1263 GNUNET_TIME_relative_to_absolute (duration).value)
1265 /* signal timeout! */
1267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1268 "Would need %llu ms before bandwidth is available for delivery to `%4s', that is too long. Signaling timeout.\n",
1270 GNUNET_i2s(&th->target));
1272 remove_from_wait_list (th);
1273 th->notify (th->notify_cls, 0, NULL);
1278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1279 "Need more bandwidth, delaying delivery to `%4s' by %llu ms\n",
1280 GNUNET_i2s(&th->target),
1283 th->notify_delay_task
1284 = GNUNET_SCHEDULER_add_delayed (h->sched,
1286 GNUNET_SCHEDULER_PRIORITY_KEEP,
1287 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1288 duration, &transmit_ready, th);
1292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1293 "Bandwidth available for transmission to `%4s'\n",
1294 GNUNET_i2s (&n->id));
1296 if (GNUNET_NO == n->transmit_ok)
1298 /* we may be ready, but transport service is not;
1299 wait for SendOkMessage or timeout */
1301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1302 "Need to wait for transport service `%s' message\n",
1305 th->notify_delay_task
1306 = GNUNET_SCHEDULER_add_delayed (h->sched,
1308 GNUNET_SCHEDULER_PRIORITY_KEEP,
1309 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1310 GNUNET_TIME_absolute_get_remaining
1311 (th->timeout), &transmit_timeout, th);
1314 n->transmit_ok = GNUNET_NO;
1315 remove_from_wait_list (th);
1317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Moving message for `%4s' to ready list\n",
1318 GNUNET_i2s(&n->id));
1320 insert_transmit_handle (&h->connect_ready_head, th);
1321 if (GNUNET_NO == h->transmission_scheduled)
1322 schedule_transmission (h);
1327 * Add neighbour to our list
1330 add_neighbour (struct GNUNET_TRANSPORT_Handle *h,
1332 struct GNUNET_TIME_Relative latency,
1333 const struct GNUNET_PeerIdentity *pid)
1335 struct NeighbourList *n;
1336 struct GNUNET_TRANSPORT_TransmitHandle *prev;
1337 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1338 struct GNUNET_TRANSPORT_TransmitHandle *next;
1340 /* check for duplicates */
1341 if (NULL != find_neighbour (h, pid))
1347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1348 "Creating entry for new neighbour `%4s'.\n", GNUNET_i2s (pid));
1350 n = GNUNET_malloc (sizeof (struct NeighbourList));
1352 n->last_quota_update = GNUNET_TIME_absolute_get ();
1353 n->quota_out = quota_out;
1354 n->next = h->neighbours;
1355 n->transmit_ok = GNUNET_YES;
1357 if (h->nc_cb != NULL)
1358 h->nc_cb (h->cls, &n->id, latency);
1360 pos = h->connect_wait_head;
1364 if (0 == memcmp (pid,
1365 &pos->target, sizeof (struct GNUNET_PeerIdentity)))
1368 GNUNET_assert (NULL == n->transmit_handle);
1369 n->transmit_handle = pos;
1371 h->connect_wait_head = next;
1374 if (GNUNET_YES == n->received_ack)
1377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1378 "Found pending request for `%4s' will trigger it now.\n",
1379 GNUNET_i2s (&pos->target));
1381 if (pos->notify_delay_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1383 GNUNET_SCHEDULER_cancel (h->sched, pos->notify_delay_task);
1384 pos->notify_delay_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1386 schedule_request (pos);
1391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1392 "Found pending request for `%4s' but still need `%s' before proceeding.\n",
1393 GNUNET_i2s (&pos->target),
1406 * Connect to the transport service. Note that the connection may
1407 * complete (or fail) asynchronously.
1410 * @param sched scheduler to use
1411 * @param cfg configuration to use
1412 * @param cls closure for the callbacks
1413 * @param rec receive function to call
1414 * @param nc function to call on connect events
1415 * @param dc function to call on disconnect events
1417 struct GNUNET_TRANSPORT_Handle *
1418 GNUNET_TRANSPORT_connect (struct GNUNET_SCHEDULER_Handle *sched,
1419 struct GNUNET_CONFIGURATION_Handle *cfg,
1421 GNUNET_TRANSPORT_ReceiveCallback rec,
1422 GNUNET_TRANSPORT_NotifyConnect nc,
1423 GNUNET_TRANSPORT_NotifyDisconnect nd)
1425 struct GNUNET_TRANSPORT_Handle *ret;
1427 GNUNET_ARM_start_service ("peerinfo",
1428 cfg, sched, START_SERVICE_TIMEOUT, NULL, NULL);
1429 GNUNET_ARM_start_service ("transport",
1430 cfg, sched, START_SERVICE_TIMEOUT, NULL, NULL);
1431 ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Handle));
1438 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1439 schedule_reconnect (ret);
1445 * These stop activities must be run in a fresh
1446 * scheduler that is NOT in shutdown mode.
1449 stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1451 struct GNUNET_TRANSPORT_Handle *handle = cls;
1452 GNUNET_ARM_stop_service ("transport",
1454 tc->sched, STOP_SERVICE_TIMEOUT, NULL, NULL);
1455 GNUNET_ARM_stop_service ("peerinfo",
1457 tc->sched, STOP_SERVICE_TIMEOUT, NULL, NULL);
1462 * Disconnect from the transport service.
1465 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1467 struct GNUNET_TRANSPORT_TransmitHandle *th;
1468 struct NeighbourList *n;
1469 struct HelloWaitList *hwl;
1470 struct GNUNET_CLIENT_Connection *client;
1473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transport disconnect called!\n");
1475 while (NULL != (th = handle->connect_ready_head))
1477 handle->connect_ready_head = th->next;
1478 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_PREREQUISITE_TASK)
1486 GNUNET_SCHEDULER_cancel (handle->sched, th->notify_delay_task);
1487 th->notify_delay_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1489 th->notify (th->notify_cls, 0, NULL);
1492 while (NULL != (n = handle->neighbours))
1494 handle->neighbours = n->next;
1497 while (NULL != (hwl = handle->hwl_head))
1499 handle->hwl_head = hwl->next;
1500 GNUNET_SCHEDULER_cancel (handle->sched, hwl->task);
1501 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1502 _("Disconnect while trying to obtain `%s' from transport service.\n"),
1504 if (hwl->rec != NULL)
1505 hwl->rec (hwl->rec_cls, GNUNET_TIME_UNIT_ZERO, NULL, NULL);
1508 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1510 GNUNET_SCHEDULER_cancel (handle->sched, handle->reconnect_task);
1511 handle->reconnect_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1513 GNUNET_free_non_null (handle->my_hello);
1514 handle->my_hello = NULL;
1515 GNUNET_SCHEDULER_run (&stop_task, handle);
1516 if (NULL != (client = handle->client))
1519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1520 "Disconnecting from transport service for good.\n");
1522 handle->client = NULL;
1523 GNUNET_CLIENT_disconnect (client);
1526 GNUNET_free (handle);
1531 * Type of a function to call when we receive a message
1534 * @param cls closure
1535 * @param msg message received, NULL on timeout or fatal error
1538 demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
1540 struct GNUNET_TRANSPORT_Handle *h = cls;
1541 const struct DisconnectInfoMessage *dim;
1542 const struct ConnectInfoMessage *cim;
1543 const struct InboundMessage *im;
1544 const struct GNUNET_MessageHeader *imm;
1545 const struct SendOkMessage *okm;
1546 struct HelloWaitList *hwl;
1547 struct NeighbourList *n;
1548 struct GNUNET_PeerIdentity me;
1549 struct GNUNET_TRANSPORT_TransmitHandle *th;
1552 if ((msg == NULL) || (h->client == NULL))
1554 if (h->client != NULL)
1557 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1558 "Error receiving from transport service, disconnecting temporarily.\n");
1560 if (h->network_handle != NULL)
1562 GNUNET_NETWORK_notify_transmit_ready_cancel (h->network_handle);
1563 h->network_handle = NULL;
1564 h->transmission_scheduled = GNUNET_NO;
1565 th = h->connect_ready_head;
1566 /* add timeout again, we cancelled the transmit_ready task! */
1567 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK);
1568 th->notify_delay_task
1569 = GNUNET_SCHEDULER_add_delayed (h->sched,
1571 GNUNET_SCHEDULER_PRIORITY_KEEP,
1572 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1573 GNUNET_TIME_absolute_get_remaining(th->timeout),
1577 GNUNET_CLIENT_disconnect (h->client);
1579 schedule_reconnect (h);
1583 /* shutdown initiated from 'GNUNET_TRANSPORT_disconnect',
1584 finish clean up work! */
1589 GNUNET_CLIENT_receive (h->client,
1590 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1591 size = ntohs (msg->size);
1592 switch (ntohs (msg->type))
1594 case GNUNET_MESSAGE_TYPE_HELLO:
1596 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg,
1603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1604 "Receiving (my own) `%s' message, I am `%4s'.\n",
1605 "HELLO", GNUNET_i2s (&me));
1607 GNUNET_free_non_null (h->my_hello);
1609 if (size < sizeof (struct GNUNET_MessageHeader))
1614 h->my_hello = GNUNET_malloc (size);
1615 memcpy (h->my_hello, msg, size);
1616 while (NULL != (hwl = h->hwl_head))
1618 h->hwl_head = hwl->next;
1619 GNUNET_SCHEDULER_cancel (h->sched, hwl->task);
1620 GNUNET_TRANSPORT_get_hello (h,
1621 GNUNET_TIME_UNIT_ZERO,
1622 hwl->rec, hwl->rec_cls);
1626 case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
1627 if (size != sizeof (struct ConnectInfoMessage))
1632 cim = (const struct ConnectInfoMessage *) msg;
1634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1635 "Receiving `%s' message for `%4s'.\n",
1636 "CONNECT", GNUNET_i2s (&cim->id));
1639 ntohl (cim->quota_out),
1640 GNUNET_TIME_relative_ntoh (cim->latency), &cim->id);
1642 case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
1643 if (size != sizeof (struct DisconnectInfoMessage))
1648 dim = (const struct DisconnectInfoMessage *) msg;
1650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1651 "Receiving `%s' message for `%4s'.\n",
1652 "DISCONNECT", GNUNET_i2s (&dim->peer));
1654 remove_neighbour (h, &dim->peer);
1656 case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
1657 if (size != sizeof (struct SendOkMessage))
1662 okm = (const struct SendOkMessage *) msg;
1664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1665 "Receiving `%s' message, transmission %s.\n", "SEND_OK",
1666 ntohl(okm->success) == GNUNET_OK ? "succeeded" : "failed");
1668 /* FIXME: need to check status code and change action accordingly,
1669 especially if the error was for CONNECT */
1670 n = find_neighbour (h, &okm->peer);
1671 GNUNET_assert (n != NULL);
1672 n->transmit_ok = GNUNET_YES;
1673 if (n->transmit_handle != NULL)
1676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1677 "Processing pending message\n");
1679 GNUNET_SCHEDULER_cancel (h->sched,
1680 n->transmit_handle->notify_delay_task);
1681 n->transmit_handle->notify_delay_task =
1682 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1683 GNUNET_assert (GNUNET_YES == n->received_ack);
1684 schedule_request (n->transmit_handle);
1687 case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
1689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1690 "Receiving `%s' message.\n", "RECV");
1693 sizeof (struct InboundMessage) +
1694 sizeof (struct GNUNET_MessageHeader))
1699 im = (const struct InboundMessage *) msg;
1700 imm = (const struct GNUNET_MessageHeader *) &im[1];
1701 if (ntohs (imm->size) + sizeof (struct InboundMessage) != size)
1706 switch (ntohs (imm->type))
1708 case GNUNET_MESSAGE_TYPE_TRANSPORT_ACK:
1710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1711 "Receiving `%s' message from `%4s'.\n",
1712 "ACK", GNUNET_i2s (&im->peer));
1714 n = find_neighbour (h, &im->peer);
1720 if (n->received_ack == GNUNET_NO)
1722 n->received_ack = GNUNET_YES;
1723 if (NULL != n->transmit_handle)
1726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1727 "Peer connected, scheduling delayed message for deliverery now.\n");
1729 schedule_request (n->transmit_handle);
1735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1736 "Received message of type %u from `%4s'.\n",
1737 ntohs (imm->type), GNUNET_i2s (&im->peer));
1741 GNUNET_TIME_relative_ntoh (im->latency), &im->peer, imm);
1746 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1748 ("Received unexpected message of type %u in %s:%u\n"),
1749 ntohs (msg->type), __FILE__, __LINE__);
1756 struct ClientTransmitWrapper
1758 GNUNET_NETWORK_TransmitReadyNotify notify;
1760 struct GNUNET_TRANSPORT_TransmitHandle *th;
1765 * Transmit message of a client destined for another
1766 * peer to the service.
1769 client_notify_wrapper (void *cls, size_t size, void *buf)
1771 struct ClientTransmitWrapper *ctw = cls;
1772 struct OutboundMessage *obm;
1773 struct GNUNET_MessageHeader *hdr;
1779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1780 "Transmission request could not be satisfied.\n");
1782 ret = ctw->notify (ctw->notify_cls, 0, NULL);
1783 GNUNET_assert (ret == 0);
1787 GNUNET_assert (size >= sizeof (struct OutboundMessage));
1789 ret = ctw->notify (ctw->notify_cls,
1790 size - sizeof (struct OutboundMessage),
1794 /* Need to reset flag, no SEND means no SEND_OK! */
1795 ctw->th->neighbour->transmit_ok = GNUNET_YES;
1799 GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
1800 hdr = (struct GNUNET_MessageHeader *) &obm[1];
1801 GNUNET_assert (ntohs (hdr->size) == ret);
1802 GNUNET_assert (ret + sizeof (struct OutboundMessage) <
1803 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1806 "Transmitting `%s' message with data for `%4s'\n",
1807 "SEND", GNUNET_i2s (&ctw->th->target));
1809 ret += sizeof (struct OutboundMessage);
1810 obm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
1811 obm->header.size = htons (ret);
1812 obm->priority = htonl (ctw->th->priority);
1813 obm->peer = ctw->th->target;
1821 * Check if we could queue a message of the given size for
1822 * transmission. The transport service will take both its
1823 * internal buffers and bandwidth limits imposed by the
1824 * other peer into consideration when answering this query.
1826 * @param handle connection to transport service
1827 * @param target who should receive the message
1828 * @param size how big is the message we want to transmit?
1829 * @param priority how important is the message?
1830 * @param timeout after how long should we give up (and call
1831 * notify with buf NULL and size 0)?
1832 * @param notify function to call when we are ready to
1833 * send such a message
1834 * @param notify_cls closure for notify
1835 * @return NULL if someone else is already waiting to be notified
1836 * non-NULL if the notify callback was queued (can be used to cancel
1837 * using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1839 struct GNUNET_TRANSPORT_TransmitHandle *
1840 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle
1842 const struct GNUNET_PeerIdentity
1843 *target, size_t size,
1844 unsigned int priority,
1845 struct GNUNET_TIME_Relative timeout,
1846 GNUNET_NETWORK_TransmitReadyNotify
1847 notify, void *notify_cls)
1849 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1850 struct GNUNET_TRANSPORT_TransmitHandle *th;
1851 struct NeighbourList *n;
1852 struct ClientTransmitWrapper *ctw;
1854 if (size + sizeof (struct OutboundMessage) >=
1855 GNUNET_SERVER_MAX_MESSAGE_SIZE)
1861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1862 "Asking transport service for transmission of %u bytes to peer `%4s'.\n",
1863 size, GNUNET_i2s (target));
1865 n = find_neighbour (handle, target);
1867 (n->transmit_handle != NULL) )
1868 return NULL; /* already have a request pending for this peer! */
1869 ctw = GNUNET_malloc (sizeof (struct ClientTransmitWrapper));
1870 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
1871 ctw->notify = notify;
1872 ctw->notify_cls = notify_cls;
1874 th->handle = handle;
1876 th->target = *target;
1877 th->notify = &client_notify_wrapper;
1878 th->notify_cls = ctw;
1879 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1880 th->notify_size = size + sizeof (struct OutboundMessage);
1881 th->priority = priority;
1884 pos = handle->connect_wait_head;
1887 GNUNET_assert (0 != memcmp (target,
1889 sizeof (struct GNUNET_PeerIdentity)));
1893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1894 "Will now try to connect to `%4s'.\n", GNUNET_i2s (target));
1901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1902 "Transmission request queued for transmission to transport service.\n");
1904 GNUNET_assert (NULL == n->transmit_handle);
1905 n->transmit_handle = th;
1906 if (GNUNET_YES != n->received_ack)
1909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1910 "Connection to `%4s' is not yet confirmed connected, scheduling timeout (%llu ms) only.\n",
1911 GNUNET_i2s (target), timeout.value);
1913 th->notify_delay_task
1914 = GNUNET_SCHEDULER_add_delayed (handle->sched,
1916 GNUNET_SCHEDULER_PRIORITY_KEEP,
1917 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1918 timeout, &transmit_timeout, th);
1923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1924 "Peer `%4s' is ready to receive, scheduling message for delivery now.\n",
1925 GNUNET_i2s (target));
1927 schedule_request (th);
1933 * Cancel the specified transmission-ready
1937 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
1938 GNUNET_TRANSPORT_TransmitHandle
1941 struct GNUNET_TRANSPORT_Handle *h;
1944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1945 "Transmission request of %u bytes to `%4s' was cancelled.\n",
1946 th->notify_size - sizeof(struct OutboundMessage),
1947 GNUNET_i2s (&th->target));
1949 GNUNET_assert (th->notify == &client_notify_wrapper);
1950 remove_from_any_list (th);
1952 if ((h->connect_ready_head == NULL) && (h->network_handle != NULL))
1954 GNUNET_NETWORK_notify_transmit_ready_cancel (h->network_handle);
1955 h->network_handle = NULL;
1956 h->transmission_scheduled = GNUNET_NO;
1958 GNUNET_free (th->notify_cls);
1963 /* end of transport_api.c */