2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/transport_api.c
23 * @brief library to access the low-level P2P IO service
24 * @author Christian Grothoff
27 #include "gnunet_client_lib.h"
28 #include "gnunet_arm_service.h"
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_server_lib.h"
32 #include "gnunet_time_lib.h"
33 #include "gnunet_transport_service.h"
34 #include "transport.h"
37 * After how long do we give up on transmitting a HELLO
40 #define OFFER_HELLO_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
43 * After how long do we automatically retry an unsuccessful
46 #define CONNECT_RETRY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 750)
49 * How long should ARM wait when starting up the
50 * transport service before reporting back?
52 #define START_SERVICE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
55 * How long should ARM wait when stopping the
56 * transport service before reporting back?
58 #define STOP_SERVICE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
61 * Entry in linked list of all of our current neighbours.
67 * This is a linked list.
69 struct NeighbourList *next;
72 * Active transmit handle, can be NULL. Used to move
73 * from ready to wait list on disconnect and to block
74 * two transmissions to the same peer from being scheduled
77 struct GNUNET_TRANSPORT_TransmitHandle *transmit_handle;
80 * Identity of this neighbour.
82 struct GNUNET_PeerIdentity id;
85 * At what time did we reset last_sent last?
87 struct GNUNET_TIME_Absolute last_quota_update;
90 * How many bytes have we sent since the "last_quota_update"
96 * Quota for outbound traffic to the neighbour in bytes/ms.
101 * Set to GNUNET_YES if we are currently allowed to
102 * transmit a message to the transport service for this
103 * peer, GNUNET_NO otherwise.
108 * Set to GNUNET_YES if we have received an ACK for the
109 * given peer. Peers that receive our HELLO always respond
110 * with an ACK to let us know that we are successfully
111 * communicating. Note that a PING can not be used for this
112 * since PINGs are only send if a HELLO address requires
113 * confirmation (and also, PINGs are not passed to the
114 * transport API itself).
122 * Linked list of requests from clients for our HELLO
123 * that were deferred.
129 * This is a linked list.
131 struct HelloWaitList *next;
134 * Reference back to our transport handle.
136 struct GNUNET_TRANSPORT_Handle *handle;
139 * Callback to call once we got our HELLO.
141 GNUNET_TRANSPORT_HelloUpdateCallback rec;
149 * When to time out (call rec with NULL).
151 struct GNUNET_TIME_Absolute timeout;
154 * Timeout task (used to trigger timeout,
155 * cancel if we get the HELLO in time).
157 GNUNET_SCHEDULER_TaskIdentifier task;
164 * Opaque handle for a transmission-ready request.
166 struct GNUNET_TRANSPORT_TransmitHandle
170 * We keep the transmit handles that are waiting for
171 * a transport-level connection in a doubly linked list.
173 struct GNUNET_TRANSPORT_TransmitHandle *next;
176 * We keep the transmit handles that are waiting for
177 * a transport-level connection in a doubly linked list.
179 struct GNUNET_TRANSPORT_TransmitHandle *prev;
182 * Handle of the main transport data structure.
184 struct GNUNET_TRANSPORT_Handle *handle;
187 * Neighbour for this handle, can be NULL if the service
188 * is not yet connected to the target.
190 struct NeighbourList *neighbour;
193 * Which peer is this transmission going to be for? All
194 * zeros if it is control-traffic to the service.
196 struct GNUNET_PeerIdentity target;
199 * Function to call when notify_size bytes are available
202 GNUNET_CONNECTION_TransmitReadyNotify notify;
205 * Closure for notify.
210 * transmit_ready task Id. The task is used to introduce the
211 * artificial delay that may be required to maintain the bandwidth
212 * limits. Later, this will be the ID of the "transmit_timeout"
213 * task which is used to signal a timeout if the transmission could
214 * not be done in a timely fashion.
216 GNUNET_SCHEDULER_TaskIdentifier notify_delay_task;
219 * Timeout for this request.
221 struct GNUNET_TIME_Absolute timeout;
224 * How many bytes is our notify callback waiting for?
229 * How important is this message?
231 unsigned int priority;
237 * Handle for the transport service (includes all of the
238 * state for the transport service).
240 struct GNUNET_TRANSPORT_Handle
244 * Closure for the callbacks.
249 * Function to call for received data.
251 GNUNET_TRANSPORT_ReceiveCallback rec;
254 * function to call on connect events
256 GNUNET_TRANSPORT_NotifyConnect nc_cb;
259 * function to call on disconnect events
261 GNUNET_TRANSPORT_NotifyDisconnect nd_cb;
264 * The current HELLO message for this peer. Updated
265 * whenever transports change their addresses.
267 struct GNUNET_HELLO_Message *my_hello;
270 * My client connection to the transport service.
272 struct GNUNET_CLIENT_Connection *client;
275 * Handle to our registration with the client for notification.
277 struct GNUNET_CLIENT_TransmitHandle *network_handle;
280 * Linked list of transmit handles that are waiting for the
281 * transport to connect to the respective peer. When we
282 * receive notification that the transport connected to a
283 * peer, we go over this list and check if someone has already
284 * requested a transmission to the new peer; if so, we trigger
287 struct GNUNET_TRANSPORT_TransmitHandle *connect_wait_head;
290 * Linked list of transmit handles that are waiting for the
291 * transport to be ready for transmission to the respective
293 * receive notification that the transport disconnected from
294 * a peer, we go over this list and move the entry back to
295 * the connect_wait list.
297 struct GNUNET_TRANSPORT_TransmitHandle *connect_ready_head;
300 * Linked list of pending requests for our HELLO.
302 struct HelloWaitList *hwl_head;
307 struct GNUNET_SCHEDULER_Handle *sched;
312 const struct GNUNET_CONFIGURATION_Handle *cfg;
315 * Linked list of the current neighbours of this peer.
317 struct NeighbourList *neighbours;
320 * ID of the task trying to reconnect to the
323 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
326 * Delay until we try to reconnect.
328 struct GNUNET_TIME_Relative reconnect_delay;
331 * Do we currently have a transmission pending?
332 * (schedule transmission was called but has not
335 int transmission_scheduled;
339 static struct NeighbourList *
340 find_neighbour (struct GNUNET_TRANSPORT_Handle *h,
341 const struct GNUNET_PeerIdentity *peer)
343 struct NeighbourList *pos;
346 while ((pos != NULL) &&
347 (0 != memcmp (peer, &pos->id, sizeof (struct GNUNET_PeerIdentity))))
354 * Schedule the task to send one message from the
355 * connect_ready list to the service.
357 static void schedule_transmission (struct GNUNET_TRANSPORT_Handle *h);
361 * Transmit message to client...
364 transport_notify_ready (void *cls, size_t size, void *buf)
366 struct GNUNET_TRANSPORT_Handle *h = cls;
367 struct GNUNET_TRANSPORT_TransmitHandle *th;
368 struct NeighbourList *n;
372 h->network_handle = NULL;
373 h->transmission_scheduled = GNUNET_NO;
377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
378 "Could not transmit to transport service, cancelling pending requests\n");
380 th = h->connect_ready_head;
381 if (th->next != NULL)
382 th->next->prev = NULL;
383 h->connect_ready_head = th->next;
384 if (NULL != (n = th->neighbour))
386 GNUNET_assert (n->transmit_handle == th);
387 n->transmit_handle = NULL;
389 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
391 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
392 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
394 if (NULL != th->notify)
395 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
397 if (h->connect_ready_head != NULL)
398 schedule_transmission (h); /* FIXME: is this ok? */
402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
403 "Ready to transmit %u bytes to transport service\n", size);
407 h->network_handle = NULL;
408 h->transmission_scheduled = GNUNET_NO;
409 while ((h->connect_ready_head != NULL) &&
410 (h->connect_ready_head->notify_size <= size))
412 th = h->connect_ready_head;
413 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
415 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
416 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
418 GNUNET_assert (th->notify_size <= size);
419 if (th->next != NULL)
420 th->next->prev = NULL;
421 h->connect_ready_head = th->next;
422 if (NULL != (n = th->neighbour))
424 GNUNET_assert (n->transmit_handle == th);
425 n->transmit_handle = NULL;
427 if (NULL != th->notify)
428 ret += th->notify (th->notify_cls, size, &cbuf[ret]);
434 if (h->connect_ready_head != NULL)
435 schedule_transmission (h);
437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
438 "Transmitting %u bytes to transport service\n", ret);
445 * Schedule the task to send one message from the
446 * connect_ready list to the service.
449 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
451 struct GNUNET_TRANSPORT_TransmitHandle *th;
453 GNUNET_assert (NULL == h->network_handle);
454 if (h->client == NULL)
456 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
457 "Could not yet schedule transmission: we are not yet connected to the transport service!\n");
458 return; /* not yet connected */
460 th = h->connect_ready_head;
462 return; /* no request pending */
463 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
465 /* remove existing time out task, will be integrated
466 with transmit_ready notification! */
467 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
468 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
470 h->transmission_scheduled = GNUNET_YES;
471 h->network_handle = GNUNET_CLIENT_notify_transmit_ready (h->client,
473 GNUNET_TIME_absolute_get_remaining
476 &transport_notify_ready,
478 GNUNET_assert (NULL != h->network_handle);
483 * Insert the given transmit handle in the given sorted
484 * doubly linked list based on timeout.
486 * @param head pointer to the head of the linked list
487 * @param th element to insert into the list
490 insert_transmit_handle (struct GNUNET_TRANSPORT_TransmitHandle **head,
491 struct GNUNET_TRANSPORT_TransmitHandle *th)
493 struct GNUNET_TRANSPORT_TransmitHandle *pos;
494 struct GNUNET_TRANSPORT_TransmitHandle *prev;
498 while ((pos != NULL) && (pos->timeout.value < th->timeout.value))
506 if (th->next != NULL)
522 * Cancel a pending notify delay task (if pending) and also remove the
523 * given transmit handle from whatever list is on.
525 * @param th handle for the transmission request to manipulate
528 remove_from_any_list (struct GNUNET_TRANSPORT_TransmitHandle *th)
530 struct GNUNET_TRANSPORT_Handle *h;
533 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
535 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
536 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
538 if (th->prev == NULL)
540 if (th == h->connect_wait_head)
541 h->connect_wait_head = th->next;
543 h->connect_ready_head = th->next;
547 th->prev->next = th->next;
549 if (th->next != NULL)
550 th->next->prev = th->prev;
555 * Schedule a request to connect to the given
556 * neighbour (and if successful, add the specified
557 * handle to the wait list).
559 * @param th handle for a request to transmit once we
562 static void try_connect (struct GNUNET_TRANSPORT_TransmitHandle *th);
566 * Called when our transmit request timed out before any transport
567 * reported success connecting to the desired peer or before the
568 * transport was ready to receive. Signal error and free
572 peer_transmit_timeout (void *cls,
573 const struct GNUNET_SCHEDULER_TaskContext *tc)
575 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
577 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
578 if (th->neighbour != NULL)
579 th->neighbour->transmit_handle = NULL;
581 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
582 "Request for transmission to peer `%s' timed out.\n",
583 GNUNET_i2s (&th->target));
585 remove_from_any_list (th);
586 if (NULL != th->notify)
587 th->notify (th->notify_cls, 0, NULL);
595 * Queue control request for transmission to the transport
598 * @param h handle to the transport service
599 * @param size number of bytes to be transmitted
600 * @param at_head request must be added to the head of the queue
601 * (otherwise request will be appended)
602 * @param timeout how long this transmission can wait (at most)
603 * @param notify function to call to get the content
604 * @param notify_cls closure for notify
607 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h,
610 struct GNUNET_TIME_Relative timeout,
611 GNUNET_CONNECTION_TransmitReadyNotify notify,
614 struct GNUNET_TRANSPORT_TransmitHandle *th;
617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
618 "Control transmit of %u bytes within %llums requested\n",
619 size, (unsigned long long) timeout.value);
621 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
624 th->notify_cls = notify_cls;
625 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
626 th->notify_size = size;
627 th->notify_delay_task
628 = GNUNET_SCHEDULER_add_delayed (h->sched,
629 timeout, &peer_transmit_timeout, th);
632 th->next = h->connect_ready_head;
633 h->connect_ready_head = th;
634 if (th->next != NULL)
639 insert_transmit_handle (&h->connect_ready_head, th);
641 if (GNUNET_NO == h->transmission_scheduled)
642 schedule_transmission (h);
647 * Update the quota values for the given neighbour now.
650 update_quota (struct NeighbourList *n)
652 struct GNUNET_TIME_Relative delta;
656 delta = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
657 allowed = delta.value * n->quota_out;
658 if (n->last_sent < allowed)
660 remaining = allowed - n->last_sent;
661 if (n->quota_out > 0)
662 remaining /= n->quota_out;
665 if (remaining > MAX_BANDWIDTH_CARRY)
666 remaining = MAX_BANDWIDTH_CARRY;
668 n->last_quota_update = GNUNET_TIME_absolute_get ();
669 n->last_quota_update.value -= remaining;
673 n->last_sent -= allowed;
674 n->last_quota_update = GNUNET_TIME_absolute_get ();
679 struct SetQuotaContext
681 struct GNUNET_TRANSPORT_Handle *handle;
683 struct GNUNET_PeerIdentity target;
685 GNUNET_SCHEDULER_Task cont;
689 struct GNUNET_TIME_Absolute timeout;
696 send_set_quota (void *cls, size_t size, void *buf)
698 struct SetQuotaContext *sqc = cls;
699 struct QuotaSetMessage *msg;
703 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
706 GNUNET_SCHEDULER_REASON_TIMEOUT);
711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
712 "Transmitting `%s' request with respect to `%4s'.\n",
713 "SET_QUOTA", GNUNET_i2s (&sqc->target));
715 GNUNET_assert (size >= sizeof (struct QuotaSetMessage));
717 msg->header.size = htons (sizeof (struct QuotaSetMessage));
718 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
719 msg->quota_in = htonl (sqc->quota_in);
720 memcpy (&msg->peer, &sqc->target, sizeof (struct GNUNET_PeerIdentity));
721 if (sqc->cont != NULL)
722 GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
725 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
727 return sizeof (struct QuotaSetMessage);
732 * Set the share of incoming bandwidth for the given
733 * peer to the specified amount.
735 * @param handle connection to transport service
736 * @param target who's bandwidth quota is being changed
737 * @param quota_in incoming bandwidth quota in bytes per ms
738 * @param quota_out outgoing bandwidth quota in bytes per ms
739 * @param timeout how long to wait until signaling failure if
740 * we can not communicate the quota change
741 * @param cont continuation to call when done, will be called
742 * either with reason "TIMEOUT" or with reason "PREREQ_DONE"
743 * @param cont_cls closure for continuation
746 GNUNET_TRANSPORT_set_quota (struct GNUNET_TRANSPORT_Handle *handle,
747 const struct GNUNET_PeerIdentity *target,
750 struct GNUNET_TIME_Relative timeout,
751 GNUNET_SCHEDULER_Task cont, void *cont_cls)
753 struct NeighbourList *n;
754 struct SetQuotaContext *sqc;
756 n = find_neighbour (handle, target);
760 if (n->quota_out < quota_out)
761 n->last_quota_update = GNUNET_TIME_absolute_get ();
762 n->quota_out = quota_out;
764 sqc = GNUNET_malloc (sizeof (struct SetQuotaContext));
765 sqc->handle = handle;
766 sqc->target = *target;
768 sqc->cont_cls = cont_cls;
769 sqc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
770 sqc->quota_in = quota_in;
771 schedule_control_transmit (handle,
772 sizeof (struct QuotaSetMessage),
773 GNUNET_NO, timeout, &send_set_quota, sqc);
778 * Obtain the HELLO message for this peer.
780 * @param handle connection to transport service
781 * @param timeout how long to wait for the HELLO
782 * @param rec function to call with the HELLO, sender will be our peer
783 * identity; message and sender will be NULL on timeout
784 * (handshake with transport service pending/failed).
785 * cost estimate will be 0.
786 * @param rec_cls closure for rec
789 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
790 GNUNET_TRANSPORT_HelloUpdateCallback rec,
793 struct HelloWaitList *hwl;
795 hwl = GNUNET_malloc (sizeof (struct HelloWaitList));
796 hwl->next = handle->hwl_head;
797 handle->hwl_head = hwl;
798 hwl->handle = handle;
800 hwl->rec_cls = rec_cls;
801 if (handle->my_hello == NULL)
803 rec (rec_cls, (const struct GNUNET_MessageHeader *) handle->my_hello);
809 * Stop receiving updates about changes to our HELLO message.
811 * @param handle connection to transport service
812 * @param rec function previously registered to be called with the HELLOs
813 * @param rec_cls closure for rec
816 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_Handle *handle,
817 GNUNET_TRANSPORT_HelloUpdateCallback rec,
820 struct HelloWaitList *pos;
821 struct HelloWaitList *prev;
824 pos = handle->hwl_head;
827 if ( (pos->rec == rec) &&
828 (pos->rec_cls == rec_cls) )
833 GNUNET_break (pos != NULL);
837 handle->hwl_head = pos->next;
839 prev->next = pos->next;
845 send_hello (void *cls, size_t size, void *buf)
847 struct GNUNET_MessageHeader *hello = cls;
853 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
854 "Timeout while trying to transmit `%s' request.\n",
861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
862 "Transmitting `%s' request.\n", "HELLO");
864 msize = ntohs (hello->size);
865 GNUNET_assert (size >= msize);
866 memcpy (buf, hello, msize);
873 * Offer the transport service the HELLO of another peer. Note that
874 * the transport service may just ignore this message if the HELLO is
875 * malformed or useless due to our local configuration.
877 * @param handle connection to transport service
878 * @param hello the hello message
881 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
882 const struct GNUNET_MessageHeader *hello)
884 struct GNUNET_MessageHeader *hc;
887 if (handle->client == NULL)
890 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
891 "Not connected to transport service, dropping offered HELLO\n");
892 fprintf(stderr, "Not connected to transport service, dropping offered HELLO\n");
896 GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
897 size = ntohs (hello->size);
898 GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
899 hc = GNUNET_malloc (size);
900 memcpy (hc, hello, size);
901 schedule_control_transmit (handle,
903 GNUNET_NO, OFFER_HELLO_TIMEOUT, &send_hello, hc);
908 * Function we use for handling incoming messages.
910 static void demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg);
914 send_start (void *cls, size_t size, void *buf)
916 struct GNUNET_MessageHeader *s = buf;
921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
922 "Timeout while trying to transmit `%s' request.\n",
928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
929 "Transmitting `%s' request.\n", "START");
931 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
932 s->size = htons (sizeof (struct GNUNET_MessageHeader));
933 s->type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
934 return sizeof (struct GNUNET_MessageHeader);
939 * We're ready to transmit the request that the transport service
940 * should connect to a new peer. In addition to sending the
941 * request, schedule the next phase for the transmission processing
942 * that caused the connect request in the first place.
945 request_connect (void *cls, size_t size, void *buf)
947 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
948 struct TryConnectMessage *tcm;
949 struct GNUNET_TRANSPORT_Handle *h;
951 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
957 "Failed to transmit `%s' request for `%4s' to service.\n",
958 "TRY_CONNECT", GNUNET_i2s (&th->target));
960 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
962 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
963 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
965 if (NULL != th->notify)
966 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
972 "Transmitting `%s' message for `%4s' (need connection in %llu ms).\n",
973 "TRY_CONNECT", GNUNET_i2s (&th->target),
974 GNUNET_TIME_absolute_get_remaining (th->timeout).value);
976 GNUNET_assert (size >= sizeof (struct TryConnectMessage));
978 tcm->header.size = htons (sizeof (struct TryConnectMessage));
979 tcm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TRY_CONNECT);
980 tcm->reserved = htonl (0);
981 memcpy (&tcm->peer, &th->target, sizeof (struct GNUNET_PeerIdentity));
982 th->notify_delay_task
983 = GNUNET_SCHEDULER_add_delayed (h->sched,
984 GNUNET_TIME_absolute_get_remaining
986 &peer_transmit_timeout, th);
987 insert_transmit_handle (&h->connect_wait_head, th);
988 return sizeof (struct TryConnectMessage);
993 * Schedule a request to connect to the given
994 * neighbour (and if successful, add the specified
995 * handle to the wait list).
997 * @param th handle for a request to transmit once we
1001 try_connect (struct GNUNET_TRANSPORT_TransmitHandle *th)
1003 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
1004 schedule_control_transmit (th->handle,
1005 sizeof (struct TryConnectMessage),
1007 GNUNET_TIME_absolute_get_remaining (th->timeout),
1008 &request_connect, th);
1013 * Task for delayed attempts to reconnect to a peer.
1015 * @param cls must be a transmit handle that determines the peer
1016 * to which we will try to connect
1017 * @param tc scheduler information about why we were triggered (not used)
1020 try_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1022 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
1024 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1030 * Remove neighbour from our list. Will automatically
1031 * trigger a re-connect attempt if we have messages pending
1034 * @param h our state
1035 * @param peer the peer to remove
1038 remove_neighbour (struct GNUNET_TRANSPORT_Handle *h,
1039 const struct GNUNET_PeerIdentity *peer)
1041 struct NeighbourList *prev;
1042 struct NeighbourList *pos;
1043 struct GNUNET_TRANSPORT_TransmitHandle *th;
1046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1047 "Removing neighbour `%s' from list of connected peers.\n",
1051 pos = h->neighbours;
1052 while ((pos != NULL) &&
1053 (0 != memcmp (peer, &pos->id, sizeof (struct GNUNET_PeerIdentity))))
1064 h->neighbours = pos->next;
1066 prev->next = pos->next;
1067 if (NULL != (th = pos->transmit_handle))
1069 pos->transmit_handle = NULL;
1070 th->neighbour = NULL;
1071 remove_from_any_list (th);
1072 if (GNUNET_TIME_absolute_get_remaining (th->timeout).value <=
1073 CONNECT_RETRY_TIMEOUT.value)
1076 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1078 ("Connection with `%4s' failed and timeout was in the past, giving up on message delivery.\n"),
1080 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == th->notify_delay_task);
1081 peer_transmit_timeout (th, NULL);
1085 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1087 ("Connection with `%4s' failed, will keep trying for %llu ms to deliver message\n"),
1089 GNUNET_TIME_absolute_get_remaining (th->timeout).value);
1090 /* try again in a bit */
1091 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == th->notify_delay_task);
1092 th->notify_delay_task
1093 = GNUNET_SCHEDULER_add_delayed (h->sched,
1094 CONNECT_RETRY_TIMEOUT,
1095 &try_connect_task, th);
1098 if (h->nc_cb != NULL)
1099 h->nd_cb (h->cls, peer);
1105 * Try again to connect to transport service.
1108 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1110 struct GNUNET_TRANSPORT_Handle *h = cls;
1111 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1112 struct NeighbourList *n;
1114 /* Forget about all neighbours that we used to be connected
1116 while (NULL != (n = h->neighbours))
1117 remove_neighbour (h, &n->id);
1119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n");
1121 GNUNET_assert (h->client == NULL);
1122 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1123 h->client = GNUNET_CLIENT_connect (h->sched, "transport", h->cfg);
1124 GNUNET_assert (h->client != NULL);
1125 /* make sure we don't send "START" twice,
1126 remove existing entry from queue (if present) */
1127 pos = h->connect_ready_head;
1130 if (pos->notify == &send_start)
1132 if (pos->prev == NULL)
1133 h->connect_ready_head = pos->next;
1135 pos->prev->next = pos->next;
1136 if (pos->next != NULL)
1137 pos->next->prev = pos->prev;
1138 GNUNET_assert (pos->neighbour == NULL);
1139 if (GNUNET_SCHEDULER_NO_TASK != pos->notify_delay_task)
1141 GNUNET_SCHEDULER_cancel (h->sched, pos->notify_delay_task);
1142 pos->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1149 schedule_control_transmit (h,
1150 sizeof (struct GNUNET_MessageHeader),
1152 GNUNET_TIME_UNIT_FOREVER_REL, &send_start, NULL);
1153 GNUNET_CLIENT_receive (h->client,
1154 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1159 * Function that will schedule the job that will try
1160 * to connect us again to the client.
1163 schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
1166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1167 "Scheduling task to reconnect to transport service in %llu ms.\n",
1168 h->reconnect_delay.value);
1170 GNUNET_assert (h->client == NULL);
1171 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
1173 = GNUNET_SCHEDULER_add_delayed (h->sched,
1174 h->reconnect_delay, &reconnect, h);
1175 h->reconnect_delay = GNUNET_TIME_UNIT_SECONDS;
1180 * We are connected to the respective peer, check the
1181 * bandwidth limits and schedule the transmission.
1183 static void schedule_request (struct GNUNET_TRANSPORT_TransmitHandle *th);
1187 * Function called by the scheduler when the timeout
1188 * for bandwidth availablility for the target
1189 * neighbour is reached.
1192 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1194 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
1196 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1197 schedule_request (th);
1202 * Remove the given transmit handle from the wait list. Does NOT free
1206 remove_from_wait_list (struct GNUNET_TRANSPORT_TransmitHandle *th)
1208 if (th->prev == NULL)
1209 th->handle->connect_wait_head = th->next;
1211 th->prev->next = th->next;
1212 if (th->next != NULL)
1213 th->next->prev = th->prev;
1218 * We are connected to the respective peer, check the
1219 * bandwidth limits and schedule the transmission.
1222 schedule_request (struct GNUNET_TRANSPORT_TransmitHandle *th)
1224 struct GNUNET_TRANSPORT_Handle *h;
1225 struct GNUNET_TIME_Relative duration;
1226 struct NeighbourList *n;
1231 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1233 GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
1234 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1236 /* check outgoing quota */
1237 duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
1238 if (duration.value > MIN_QUOTA_REFRESH_TIME)
1241 duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
1243 available = duration.value * n->quota_out;
1244 if (available < n->last_sent + th->notify_size)
1246 /* calculate how much bandwidth we'd still need to
1247 accumulate and based on that how long we'll have
1249 available = n->last_sent + th->notify_size - available;
1250 duration = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1251 available / n->quota_out);
1252 if (th->timeout.value <
1253 GNUNET_TIME_relative_to_absolute (duration).value)
1255 /* signal timeout! */
1257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1258 "Would need %llu ms before bandwidth is available for delivery to `%4s', that is too long. Signaling timeout.\n",
1259 duration.value, GNUNET_i2s (&th->target));
1261 remove_from_wait_list (th);
1262 if (NULL != th->notify)
1263 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
1268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1269 "Need more bandwidth, delaying delivery to `%4s' by %llu ms\n",
1270 GNUNET_i2s (&th->target), duration.value);
1272 th->notify_delay_task
1273 = GNUNET_SCHEDULER_add_delayed (h->sched,
1274 duration, &transmit_ready, th);
1278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1279 "Bandwidth available for transmission to `%4s'\n",
1280 GNUNET_i2s (&n->id));
1282 if (GNUNET_NO == n->transmit_ok)
1284 /* we may be ready, but transport service is not;
1285 wait for SendOkMessage or timeout */
1287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1288 "Need to wait for transport service `%s' message\n",
1291 th->notify_delay_task
1292 = GNUNET_SCHEDULER_add_delayed (h->sched,
1293 GNUNET_TIME_absolute_get_remaining
1294 (th->timeout), &peer_transmit_timeout,
1298 n->transmit_ok = GNUNET_NO;
1299 remove_from_wait_list (th);
1301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1302 "Moving message for `%4s' to ready list\n",
1303 GNUNET_i2s (&n->id));
1305 insert_transmit_handle (&h->connect_ready_head, th);
1306 if (GNUNET_NO == h->transmission_scheduled)
1307 schedule_transmission (h);
1312 * Add neighbour to our list
1315 add_neighbour (struct GNUNET_TRANSPORT_Handle *h,
1317 struct GNUNET_TIME_Relative latency,
1319 const struct GNUNET_PeerIdentity *pid)
1321 struct NeighbourList *n;
1322 struct GNUNET_TRANSPORT_TransmitHandle *prev;
1323 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1324 struct GNUNET_TRANSPORT_TransmitHandle *next;
1326 /* check for duplicates */
1327 if (NULL != find_neighbour (h, pid))
1333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1334 "Creating entry for new neighbour `%4s'.\n", GNUNET_i2s (pid));
1336 n = GNUNET_malloc (sizeof (struct NeighbourList));
1338 n->last_quota_update = GNUNET_TIME_absolute_get ();
1339 n->quota_out = quota_out;
1340 n->next = h->neighbours;
1341 n->transmit_ok = GNUNET_YES;
1343 if (h->nc_cb != NULL)
1344 h->nc_cb (h->cls, &n->id, latency, distance);
1346 pos = h->connect_wait_head;
1350 if (0 == memcmp (pid,
1351 &pos->target, sizeof (struct GNUNET_PeerIdentity)))
1354 GNUNET_assert (NULL == n->transmit_handle);
1355 n->transmit_handle = pos;
1357 h->connect_wait_head = next;
1360 // if (GNUNET_YES == n->received_ack)
1363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1364 "Found pending request for `%4s' will trigger it now.\n",
1365 GNUNET_i2s (&pos->target));
1367 if (pos->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1369 GNUNET_SCHEDULER_cancel (h->sched, pos->notify_delay_task);
1370 pos->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1372 schedule_request (pos);
1384 * Connect to the transport service. Note that the connection may
1385 * complete (or fail) asynchronously.
1388 * @param sched scheduler to use
1389 * @param cfg configuration to use
1390 * @param cls closure for the callbacks
1391 * @param rec receive function to call
1392 * @param nc function to call on connect events
1393 * @param nd function to call on disconnect events
1395 struct GNUNET_TRANSPORT_Handle *
1396 GNUNET_TRANSPORT_connect (struct GNUNET_SCHEDULER_Handle *sched,
1397 const struct GNUNET_CONFIGURATION_Handle *cfg,
1399 GNUNET_TRANSPORT_ReceiveCallback rec,
1400 GNUNET_TRANSPORT_NotifyConnect nc,
1401 GNUNET_TRANSPORT_NotifyDisconnect nd)
1403 struct GNUNET_TRANSPORT_Handle *ret;
1405 GNUNET_ARM_start_services (cfg, sched, "peerinfo", "transport", NULL);
1406 ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Handle));
1413 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1414 schedule_reconnect (ret);
1420 * Disconnect from the transport service.
1423 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1425 struct GNUNET_TRANSPORT_TransmitHandle *th;
1426 struct NeighbourList *n;
1427 struct HelloWaitList *hwl;
1428 struct GNUNET_CLIENT_Connection *client;
1431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transport disconnect called!\n");
1433 while (NULL != (th = handle->connect_ready_head))
1435 handle->connect_ready_head = th->next;
1436 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1438 GNUNET_SCHEDULER_cancel (handle->sched, th->notify_delay_task);
1439 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1441 if (NULL != th->notify)
1442 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
1445 while (NULL != (th = handle->connect_wait_head))
1447 handle->connect_wait_head = th->next;
1448 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1450 GNUNET_SCHEDULER_cancel (handle->sched, th->notify_delay_task);
1451 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1453 if (NULL != th->notify)
1454 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
1457 while (NULL != (n = handle->neighbours))
1459 handle->neighbours = n->next;
1460 if (NULL != (th = n->transmit_handle))
1462 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1464 GNUNET_SCHEDULER_cancel (handle->sched, th->notify_delay_task);
1465 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1467 if (NULL != th->notify)
1468 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
1473 while (NULL != (hwl = handle->hwl_head))
1475 handle->hwl_head = hwl->next;
1476 GNUNET_SCHEDULER_cancel (handle->sched, hwl->task);
1477 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1479 ("Disconnect while notification for `%s' still registered.\n"),
1481 if (hwl->rec != NULL)
1482 hwl->rec (hwl->rec_cls, NULL);
1485 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1487 GNUNET_SCHEDULER_cancel (handle->sched, handle->reconnect_task);
1488 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1490 GNUNET_free_non_null (handle->my_hello);
1491 handle->my_hello = NULL;
1492 GNUNET_ARM_stop_services (handle->cfg, handle->sched, "transport",
1494 if (NULL != handle->network_handle)
1496 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->network_handle);
1497 handle->network_handle = NULL;
1499 if (NULL != (client = handle->client))
1502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1503 "Disconnecting from transport service for good.\n");
1505 handle->client = NULL;
1506 GNUNET_CLIENT_disconnect (client);
1508 GNUNET_free (handle);
1513 * Type of a function to call when we receive a message
1516 * @param cls closure
1517 * @param msg message received, NULL on timeout or fatal error
1520 demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
1522 struct GNUNET_TRANSPORT_Handle *h = cls;
1523 const struct DisconnectInfoMessage *dim;
1524 const struct ConnectInfoMessage *cim;
1525 const struct InboundMessage *im;
1526 const struct GNUNET_MessageHeader *imm;
1527 const struct SendOkMessage *okm;
1528 struct HelloWaitList *hwl;
1529 struct HelloWaitList *next_hwl;
1530 struct NeighbourList *n;
1531 struct GNUNET_PeerIdentity me;
1532 struct GNUNET_TRANSPORT_TransmitHandle *th;
1535 if ((msg == NULL) || (h->client == NULL))
1537 if (h->client != NULL)
1540 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1541 "Error receiving from transport service, disconnecting temporarily.\n");
1543 if (h->network_handle != NULL)
1545 GNUNET_CLIENT_notify_transmit_ready_cancel (h->network_handle);
1546 h->network_handle = NULL;
1547 h->transmission_scheduled = GNUNET_NO;
1548 th = h->connect_ready_head;
1549 /* add timeout again, we cancelled the transmit_ready task! */
1550 GNUNET_assert (th->notify_delay_task ==
1551 GNUNET_SCHEDULER_NO_TASK);
1552 th->notify_delay_task =
1553 GNUNET_SCHEDULER_add_delayed (h->sched,
1554 GNUNET_TIME_absolute_get_remaining
1556 &peer_transmit_timeout, th);
1558 GNUNET_CLIENT_disconnect (h->client);
1560 schedule_reconnect (h);
1564 /* shutdown initiated from 'GNUNET_TRANSPORT_disconnect',
1565 finish clean up work! */
1570 GNUNET_CLIENT_receive (h->client,
1571 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1572 size = ntohs (msg->size);
1573 switch (ntohs (msg->type))
1575 case GNUNET_MESSAGE_TYPE_HELLO:
1577 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg,
1584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1585 "Receiving (my own) `%s' message, I am `%4s'.\n",
1586 "HELLO", GNUNET_i2s (&me));
1588 GNUNET_free_non_null (h->my_hello);
1590 if (size < sizeof (struct GNUNET_MessageHeader))
1595 h->my_hello = GNUNET_malloc (size);
1596 memcpy (h->my_hello, msg, size);
1600 next_hwl = hwl->next;
1601 hwl->rec (hwl->rec_cls,
1602 (const struct GNUNET_MessageHeader *) h->my_hello);
1606 case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
1607 if (size != sizeof (struct ConnectInfoMessage))
1612 cim = (const struct ConnectInfoMessage *) msg;
1614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1615 "Receiving `%s' message for `%4s'.\n",
1616 "CONNECT", GNUNET_i2s (&cim->id));
1619 ntohl (cim->quota_out),
1620 GNUNET_TIME_relative_ntoh (cim->latency), ntohs(cim->distance), &cim->id);
1622 case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
1623 if (size != sizeof (struct DisconnectInfoMessage))
1628 dim = (const struct DisconnectInfoMessage *) msg;
1630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1631 "Receiving `%s' message for `%4s'.\n",
1632 "DISCONNECT", GNUNET_i2s (&dim->peer));
1634 remove_neighbour (h, &dim->peer);
1636 case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
1637 if (size != sizeof (struct SendOkMessage))
1642 okm = (const struct SendOkMessage *) msg;
1644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1645 "Receiving `%s' message, transmission %s.\n", "SEND_OK",
1646 ntohl (okm->success) == GNUNET_OK ? "succeeded" : "failed");
1648 n = find_neighbour (h, &okm->peer);
1649 GNUNET_assert (n != NULL);
1650 n->transmit_ok = GNUNET_YES;
1651 if (n->transmit_handle != NULL)
1654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1655 "Processing pending message for `%4s'\n",
1656 GNUNET_i2s (&n->id));
1658 GNUNET_SCHEDULER_cancel (h->sched,
1659 n->transmit_handle->notify_delay_task);
1660 n->transmit_handle->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1661 GNUNET_assert (GNUNET_YES == n->received_ack);
1662 schedule_request (n->transmit_handle);
1665 case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
1667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1668 "Receiving `%s' message.\n", "RECV");
1671 sizeof (struct InboundMessage) +
1672 sizeof (struct GNUNET_MessageHeader))
1677 im = (const struct InboundMessage *) msg;
1678 imm = (const struct GNUNET_MessageHeader *) &im[1];
1679 if (ntohs (imm->size) + sizeof (struct InboundMessage) != size)
1684 switch (ntohs (imm->type))
1686 case GNUNET_MESSAGE_TYPE_TRANSPORT_ACK:
1688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1689 "Receiving `%s' message from `%4s'.\n",
1690 "ACK", GNUNET_i2s (&im->peer));
1692 n = find_neighbour (h, &im->peer);
1698 if (n->received_ack == GNUNET_NO)
1700 n->received_ack = GNUNET_YES;
1701 if (NULL != n->transmit_handle)
1704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1705 "Peer connected, scheduling delayed message for delivery now.\n");
1707 schedule_request (n->transmit_handle);
1713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1714 "Received message of type %u from `%4s'.\n",
1715 ntohs (imm->type), GNUNET_i2s (&im->peer));
1718 h->rec (h->cls, &im->peer, imm,
1719 GNUNET_TIME_relative_ntoh (im->latency), ntohs(im->distance));
1724 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1726 ("Received unexpected message of type %u in %s:%u\n"),
1727 ntohs (msg->type), __FILE__, __LINE__);
1734 struct ClientTransmitWrapper
1736 GNUNET_CONNECTION_TransmitReadyNotify notify;
1738 struct GNUNET_TRANSPORT_TransmitHandle *th;
1743 * Transmit message of a client destined for another
1744 * peer to the service.
1747 client_notify_wrapper (void *cls, size_t size, void *buf)
1749 struct ClientTransmitWrapper *ctw = cls;
1750 struct OutboundMessage *obm;
1751 struct GNUNET_MessageHeader *hdr;
1757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1758 "Transmission request could not be satisfied.\n");
1760 if (NULL != ctw->notify)
1761 GNUNET_assert (0 == ctw->notify (ctw->notify_cls, 0, NULL));
1765 GNUNET_assert (size >= sizeof (struct OutboundMessage));
1767 if (ctw->notify != NULL)
1768 ret = ctw->notify (ctw->notify_cls,
1769 size - sizeof (struct OutboundMessage),
1775 /* Need to reset flag, no SEND means no SEND_OK! */
1776 ctw->th->neighbour->transmit_ok = GNUNET_YES;
1780 GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
1781 hdr = (struct GNUNET_MessageHeader *) &obm[1];
1782 GNUNET_assert (ntohs (hdr->size) == ret);
1783 GNUNET_assert (ret + sizeof (struct OutboundMessage) <
1784 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1787 "Transmitting `%s' message with data for `%4s'\n",
1788 "SEND", GNUNET_i2s (&ctw->th->target));
1790 ret += sizeof (struct OutboundMessage);
1791 obm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
1792 obm->header.size = htons (ret);
1793 obm->priority = htonl (ctw->th->priority);
1794 obm->peer = ctw->th->target;
1802 * Check if we could queue a message of the given size for
1803 * transmission. The transport service will take both its
1804 * internal buffers and bandwidth limits imposed by the
1805 * other peer into consideration when answering this query.
1807 * @param handle connection to transport service
1808 * @param target who should receive the message
1809 * @param size how big is the message we want to transmit?
1810 * @param priority how important is the message?
1811 * @param timeout after how long should we give up (and call
1812 * notify with buf NULL and size 0)?
1813 * @param notify function to call when we are ready to
1814 * send such a message
1815 * @param notify_cls closure for notify
1816 * @return NULL if someone else is already waiting to be notified
1817 * non-NULL if the notify callback was queued (can be used to cancel
1818 * using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1820 struct GNUNET_TRANSPORT_TransmitHandle *
1821 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle
1823 const struct GNUNET_PeerIdentity
1824 *target, size_t size,
1825 unsigned int priority,
1826 struct GNUNET_TIME_Relative timeout,
1827 GNUNET_CONNECTION_TransmitReadyNotify
1828 notify, void *notify_cls)
1830 struct GNUNET_TRANSPORT_TransmitHandle *pos;
1831 struct GNUNET_TRANSPORT_TransmitHandle *th;
1832 struct NeighbourList *n;
1833 struct ClientTransmitWrapper *ctw;
1835 if (size + sizeof (struct OutboundMessage) >=
1836 GNUNET_SERVER_MAX_MESSAGE_SIZE)
1842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1843 "Asking transport service for transmission of %u bytes to peer `%4s'.\n",
1844 size, GNUNET_i2s (target));
1846 n = find_neighbour (handle, target);
1847 if ((n != NULL) && (n->transmit_handle != NULL))
1848 return NULL; /* already have a request pending for this peer! */
1849 ctw = GNUNET_malloc (sizeof (struct ClientTransmitWrapper));
1850 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
1851 ctw->notify = notify;
1852 ctw->notify_cls = notify_cls;
1854 th->handle = handle;
1856 th->target = *target;
1857 th->notify = &client_notify_wrapper;
1858 th->notify_cls = ctw;
1859 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1860 th->notify_size = size + sizeof (struct OutboundMessage);
1861 th->priority = priority;
1864 pos = handle->connect_wait_head;
1867 GNUNET_assert (0 != memcmp (target,
1869 sizeof (struct GNUNET_PeerIdentity)));
1873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1874 "Will now try to connect to `%4s'.\n", GNUNET_i2s (target));
1881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1882 "Transmission request queued for transmission to transport service.\n");
1884 GNUNET_assert (NULL == n->transmit_handle);
1885 n->transmit_handle = th;
1886 if (GNUNET_YES != n->received_ack)
1889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1890 "Connection to `%4s' is not yet confirmed connected, scheduling timeout (%llu ms) only.\n",
1891 GNUNET_i2s (target), timeout.value);
1893 th->notify_delay_task
1894 = GNUNET_SCHEDULER_add_delayed (handle->sched,
1895 timeout, &peer_transmit_timeout, th);
1900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1901 "Peer `%4s' is ready to receive, scheduling message for delivery now.\n",
1902 GNUNET_i2s (target));
1904 th->notify_delay_task
1905 = GNUNET_SCHEDULER_add_now (handle->sched, &transmit_ready, th);
1911 * Cancel the specified transmission-ready notification.
1914 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
1915 GNUNET_TRANSPORT_TransmitHandle
1918 struct GNUNET_TRANSPORT_Handle *h;
1921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1922 "Transmission request of %u bytes to `%4s' was cancelled.\n",
1923 th->notify_size - sizeof (struct OutboundMessage),
1924 GNUNET_i2s (&th->target));
1926 GNUNET_assert (th->notify == &client_notify_wrapper);
1927 remove_from_any_list (th);
1929 if ((h->connect_ready_head == NULL) && (h->network_handle != NULL))
1931 GNUNET_CLIENT_notify_transmit_ready_cancel (h->network_handle);
1932 h->network_handle = NULL;
1933 h->transmission_scheduled = GNUNET_NO;
1935 GNUNET_free (th->notify_cls);
1936 GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
1941 /* end of transport_api.c */