2 This file is part of GNUnet.
3 (C) 2009, 2010 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 3, 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_bandwidth_lib.h"
28 #include "gnunet_client_lib.h"
29 #include "gnunet_constants.h"
30 #include "gnunet_container_lib.h"
31 #include "gnunet_arm_service.h"
32 #include "gnunet_hello_lib.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_server_lib.h"
35 #include "gnunet_time_lib.h"
36 #include "gnunet_transport_service.h"
37 #include "transport.h"
40 * After how long do we give up on transmitting a HELLO
43 #define OFFER_HELLO_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
46 * After how long do we automatically retry an unsuccessful
49 #define CONNECT_RETRY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 750)
52 * How long should ARM wait when starting up the
53 * transport service before reporting back?
55 #define START_SERVICE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
58 * How long should ARM wait when stopping the
59 * transport service before reporting back?
61 #define STOP_SERVICE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
64 * How large to start with for the hashmap of neighbours.
66 #define STARTING_NEIGHBOURS_SIZE 10
70 * What stage are we in for transmission processing?
80 * Message in local queue, not given to service.
85 * Message given to service, not confirmed (no SEND_OK).
90 * One message was given to service and before it was confirmed,
91 * another one was already queued (waiting for SEND_OK to pass on
94 TS_TRANSMITTED_QUEUED = 3
99 * Handle for a transmission-ready request.
101 struct GNUNET_TRANSPORT_TransmitHandle
105 * Neighbour for this handle, NULL for control-traffic.
107 struct NeighbourList *neighbour;
110 * Function to call when notify_size bytes are available
113 GNUNET_CONNECTION_TransmitReadyNotify notify;
116 * Closure for notify.
121 * transmit_ready task Id. The task is used to introduce the
122 * artificial delay that may be required to maintain the bandwidth
123 * limits. Later, this will be the ID of the "transmit_timeout"
124 * task which is used to signal a timeout if the transmission could
125 * not be done in a timely fashion.
127 GNUNET_SCHEDULER_TaskIdentifier notify_delay_task;
130 * Timeout for this request.
132 struct GNUNET_TIME_Absolute timeout;
135 * How many bytes is our notify callback waiting for?
140 * How important is this message?
142 unsigned int priority;
148 * Handle for a control message queue entry.
150 struct ControlMessage
154 * This is a doubly-linked list.
156 struct ControlMessage *next;
159 * This is a doubly-linked list.
161 struct ControlMessage *prev;
164 * Overall transport handle.
166 struct GNUNET_TRANSPORT_Handle *h;
169 * Function to call when notify_size bytes are available
172 GNUNET_CONNECTION_TransmitReadyNotify notify;
175 * Closure for notify.
180 * transmit_ready task Id. The task is used to introduce the
181 * artificial delay that may be required to maintain the bandwidth
182 * limits. Later, this will be the ID of the "transmit_timeout"
183 * task which is used to signal a timeout if the transmission could
184 * not be done in a timely fashion.
186 GNUNET_SCHEDULER_TaskIdentifier notify_delay_task;
189 * How many bytes is our notify callback waiting for?
196 * Context for storing information about attempted next transmission.
198 struct TryTransmitContext
202 * Main transport handle.
204 struct GNUNET_TRANSPORT_Handle *h;
207 * Returned transmission handle.
209 struct GNUNET_TRANSPORT_TransmitHandle *ret;
212 * Time to retry the send task.
214 struct GNUNET_TIME_Relative retry_time;
218 * Entry in hash table of all of our current neighbours.
223 * Overall transport handle.
225 struct GNUNET_TRANSPORT_Handle *h;
228 * Active transmit handle; available if 'transmit_forbidden'
231 struct GNUNET_TRANSPORT_TransmitHandle transmit_handle;
234 * Identity of this neighbour.
236 struct GNUNET_PeerIdentity id;
239 * Outbound bandwidh tracker.
241 struct GNUNET_BANDWIDTH_Tracker out_tracker;
244 * Set to GNUNET_NO if we are currently allowed to accept a
245 * message to the transport service for this peer, GNUNET_YES
246 * if we have one and are waiting for transmission, GNUNET_SYSERR
247 * if we are waiting for confirmation AND have already accepted
248 * yet another message.
250 enum TransmitStage transmit_stage;
253 * Have we received a notification that this peer is connected
259 * Are we in the middle of disconnecting the peer already?
261 unsigned int in_disconnect;
267 * Linked list of requests from clients for our HELLO that were
274 * This is a linked list.
276 struct HelloWaitList *next;
279 * Reference back to our transport handle.
281 struct GNUNET_TRANSPORT_Handle *handle;
284 * Callback to call once we got our HELLO.
286 GNUNET_TRANSPORT_HelloUpdateCallback rec;
297 * Handle for the transport service (includes all of the
298 * state for the transport service).
300 struct GNUNET_TRANSPORT_Handle
304 * Closure for the callbacks.
309 * Function to call for received data.
311 GNUNET_TRANSPORT_ReceiveCallback rec;
314 * function to call on connect events
316 GNUNET_TRANSPORT_NotifyConnect nc_cb;
319 * function to call on disconnect events
321 GNUNET_TRANSPORT_NotifyDisconnect nd_cb;
324 * Head of DLL of control messages.
326 struct ControlMessage *control_head;
329 * Tail of DLL of control messages.
331 struct ControlMessage *control_tail;
334 * The current HELLO message for this peer. Updated
335 * whenever transports change their addresses.
337 struct GNUNET_HELLO_Message *my_hello;
340 * My client connection to the transport service.
342 struct GNUNET_CLIENT_Connection *client;
345 * Handle to our registration with the client for notification.
347 struct GNUNET_CLIENT_TransmitHandle *network_handle;
350 * Linked list of pending requests for our HELLO.
352 struct HelloWaitList *hwl_head;
357 const struct GNUNET_CONFIGURATION_Handle *cfg;
360 * Linked list of the current neighbours of this peer.
362 struct GNUNET_CONTAINER_MultiHashMap *neighbours;
365 * Peer identity as assumed by this process, or all zeros.
367 struct GNUNET_PeerIdentity self;
370 * ID of the task trying to reconnect to the service.
372 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
375 * ID of the task trying to trigger transmission for a peer
376 * while maintaining bandwidth quotas.
378 GNUNET_SCHEDULER_TaskIdentifier quota_task;
381 * Delay until we try to reconnect.
383 struct GNUNET_TIME_Relative reconnect_delay;
386 * Set once we are in the process of disconnecting from the
392 * Should we check that 'self' matches what the service thinks?
393 * (if GNUNET_NO, then 'self' is all zeros!).
400 * Get the neighbour list entry for the given peer
402 * @param h our context
403 * @param peer peer to look up
404 * @return NULL if no such peer entry exists
406 static struct NeighbourList *
407 neighbour_find (struct GNUNET_TRANSPORT_Handle *h,
408 const struct GNUNET_PeerIdentity *peer)
410 return GNUNET_CONTAINER_multihashmap_get(h->neighbours, &peer->hashPubKey);
415 * Schedule the task to send one message, either from the control
416 * list or the peer message queues to the service.
418 static void schedule_transmission (struct GNUNET_TRANSPORT_Handle *h);
422 * Function called by the scheduler when the timeout for bandwidth
423 * availablility for the target neighbour is reached.
425 * @param cls the 'struct GNUNET_TRANSPORT_Handle*'
426 * @param tc scheduler context
429 quota_transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
431 struct GNUNET_TRANSPORT_Handle *h = cls;
433 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
434 schedule_transmission (h);
439 * Iterator over hash map entries, attempt to schedule
440 * a transmission to entries in the neighbour hashmap.
442 * @param cls closure a TryTransmitContext
443 * @param key current key code
444 * @param value value in the hash map, the neighbour entry to consider
445 * @return GNUNET_YES if we should continue to
450 try_schedule_transmission (void *cls,
451 const GNUNET_HashCode * key,
454 struct NeighbourList *n = value;
455 struct TryTransmitContext *try_transmit_ctx = cls;
456 struct GNUNET_TIME_Relative duration;
457 GNUNET_CONNECTION_TransmitReadyNotify notify;
458 struct GNUNET_TRANSPORT_TransmitHandle *th;
459 struct GNUNET_TIME_Absolute duration_abs;
461 if (n->transmit_stage != TS_QUEUED)
462 return GNUNET_YES; /* not eligible, keep iterating */
463 if (n->is_connected != GNUNET_YES)
464 return GNUNET_YES; /* keep iterating */
466 th = &n->transmit_handle;
467 GNUNET_break (n == th->neighbour);
468 /* check outgoing quota */
469 duration = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
470 th->notify_size - sizeof (struct OutboundMessage));
471 duration_abs = GNUNET_TIME_relative_to_absolute (duration);
472 if (th->timeout.abs_value < duration_abs.abs_value)
474 /* signal timeout! */
476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
477 "Would need %llu ms before bandwidth is available for delivery to `%4s', that is too long. Signaling timeout.\n",
479 GNUNET_i2s (&n->id));
481 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
483 GNUNET_SCHEDULER_cancel (th->notify_delay_task);
484 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
486 n->transmit_stage = TS_NEW;
487 if (NULL != (notify = th->notify))
490 GNUNET_assert (0 == notify (th->notify_cls, 0, NULL));
492 return GNUNET_YES; /* keep iterating */
494 if (duration.rel_value > 0)
497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
498 "Need more bandwidth (%u b/s allowed, %u b needed), delaying delivery to `%4s' by %llu ms\n",
499 (unsigned int) n->out_tracker.available_bytes_per_s__,
500 (unsigned int) th->notify_size - sizeof (struct OutboundMessage),
502 (unsigned long long) duration.rel_value);
504 try_transmit_ctx->retry_time = GNUNET_TIME_relative_min (try_transmit_ctx->retry_time,
506 return GNUNET_YES; /* keep iterating */
509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
510 "Have %u bytes of bandwidth available for transmission to `%4s' right now\n",
511 th->notify_size - sizeof (struct OutboundMessage),
512 GNUNET_i2s (&n->id));
515 if ( (try_transmit_ctx->ret == NULL) ||
516 (try_transmit_ctx->ret->priority < th->priority) )
517 try_transmit_ctx->ret = th;
523 * Figure out which transmission to a peer can be done right now.
524 * If none can, schedule a task to call 'schedule_transmission'
525 * whenever a peer transmission can be done in the future and
526 * return NULL. Otherwise return the next transmission to be
529 * @param h handle to transport
530 * @return NULL to wait longer before doing any peer transmissions
532 static struct GNUNET_TRANSPORT_TransmitHandle *
533 schedule_peer_transmission (struct GNUNET_TRANSPORT_Handle *h)
535 struct TryTransmitContext try_transmit_ctx;
537 if (h->quota_task != GNUNET_SCHEDULER_NO_TASK)
539 GNUNET_SCHEDULER_cancel (h->quota_task);
540 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
542 try_transmit_ctx.h = h;
543 try_transmit_ctx.ret = NULL;
544 try_transmit_ctx.retry_time = GNUNET_TIME_UNIT_FOREVER_REL;
545 GNUNET_CONTAINER_multihashmap_iterate(h->neighbours,
546 &try_schedule_transmission,
548 if (try_transmit_ctx.ret == NULL)
549 h->quota_task = GNUNET_SCHEDULER_add_delayed (try_transmit_ctx.retry_time,
550 "a_transmit_ready,
552 return try_transmit_ctx.ret;
557 * Transmit message(s) to service.
559 * @param cls handle to transport
560 * @param size number of bytes available in buf
561 * @param buf where to copy the message
562 * @return number of bytes copied to buf
565 transport_notify_ready (void *cls, size_t size, void *buf)
567 struct GNUNET_TRANSPORT_Handle *h = cls;
568 struct ControlMessage *cm;
569 struct GNUNET_TRANSPORT_TransmitHandle *th;
570 struct NeighbourList *n;
571 struct OutboundMessage obm;
572 GNUNET_CONNECTION_TransmitReadyNotify notify;
578 h->network_handle = NULL;
581 schedule_transmission (h);
585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
586 "Ready to transmit %u bytes to transport service\n", size);
590 while ( (NULL != (cm = h->control_head)) &&
591 (cm->notify_size <= size) )
593 if (cm->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
595 GNUNET_SCHEDULER_cancel (cm->notify_delay_task);
596 cm->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
598 GNUNET_CONTAINER_DLL_remove (h->control_head,
601 nret = cm->notify (cm->notify_cls, size, &cbuf[ret]);
603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
604 "Added %u bytes of control message at %u\n",
612 while ( (NULL != (th = schedule_peer_transmission (h))) &&
613 (th->notify_size <= size) )
615 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
617 GNUNET_SCHEDULER_cancel (th->notify_delay_task);
618 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
621 switch (n->transmit_stage)
627 n->transmit_stage = TS_TRANSMITTED;
632 case TS_TRANSMITTED_QUEUED:
638 GNUNET_assert (size >= sizeof (struct OutboundMessage));
641 mret = notify (th->notify_cls,
642 size - sizeof (struct OutboundMessage),
643 &cbuf[ret + sizeof (struct OutboundMessage)]);
644 GNUNET_assert (mret <= size - sizeof (struct OutboundMessage));
646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
647 "Message of %u bytes with timeout %llums constructed for `%4s'\n",
649 (unsigned long long) GNUNET_TIME_absolute_get_remaining (th->timeout).rel_value,
650 GNUNET_i2s (&n->id));
654 GNUNET_assert (mret + sizeof (struct OutboundMessage) < GNUNET_SERVER_MAX_MESSAGE_SIZE);
655 obm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
656 obm.header.size = htons (mret + sizeof (struct OutboundMessage));
657 obm.priority = htonl (th->priority);
658 obm.timeout = GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining (th->timeout));
660 memcpy (&cbuf[ret], &obm, sizeof (struct OutboundMessage));
661 ret += (mret + sizeof (struct OutboundMessage));
662 size -= (mret + sizeof (struct OutboundMessage));
663 GNUNET_BANDWIDTH_tracker_consume (&n->out_tracker, mret);
667 switch (n->transmit_stage)
676 n->transmit_stage = TS_NEW;
678 case TS_TRANSMITTED_QUEUED:
679 n->transmit_stage = TS_QUEUED;
686 schedule_transmission (h);
688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
689 "Transmitting %u bytes to transport service\n", ret);
696 * Schedule the task to send one message, either from the control
697 * list or the peer message queues to the service.
700 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
703 struct GNUNET_TIME_Relative timeout;
704 struct GNUNET_TRANSPORT_TransmitHandle *th;
706 if (NULL != h->network_handle)
708 if (h->client == NULL)
710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
711 _("Could not yet schedule transmission: we are not yet connected to the transport service!\n"));
712 return; /* not yet connected */
714 if (NULL != h->control_head)
716 size = h->control_head->notify_size;
717 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
721 th = schedule_peer_transmission (h);
724 /* no transmission ready right now */
726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
727 "Could not yet schedule transmission: none ready\n");
731 size = th->notify_size;
732 timeout = GNUNET_TIME_absolute_get_remaining (th->timeout);
735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
736 "Calling notify_transmit_ready\n");
739 GNUNET_CLIENT_notify_transmit_ready (h->client,
743 &transport_notify_ready,
745 GNUNET_assert (NULL != h->network_handle);
750 * Called when our transmit request timed out before any transport
751 * reported success connecting to the desired peer or before the
752 * transport was ready to receive. Signal error and free
756 control_transmit_timeout (void *cls,
757 const struct GNUNET_SCHEDULER_TaskContext *tc)
759 struct ControlMessage *th = cls;
761 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
762 if (NULL != th->notify)
763 th->notify (th->notify_cls, 0, NULL);
764 GNUNET_CONTAINER_DLL_remove (th->h->control_head,
772 * Queue control request for transmission to the transport
775 * @param h handle to the transport service
776 * @param size number of bytes to be transmitted
777 * @param at_head request must be added to the head of the queue
778 * (otherwise request will be appended)
779 * @param timeout how long this transmission can wait (at most)
780 * @param notify function to call to get the content
781 * @param notify_cls closure for notify
784 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h,
787 struct GNUNET_TIME_Relative timeout,
788 GNUNET_CONNECTION_TransmitReadyNotify notify,
791 struct ControlMessage *cm;
794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
795 "Control transmit of %u bytes within %llums requested\n",
796 size, (unsigned long long) timeout.rel_value);
798 cm = GNUNET_malloc (sizeof (struct ControlMessage));
801 cm->notify_cls = notify_cls;
802 cm->notify_size = size;
803 cm->notify_delay_task
804 = GNUNET_SCHEDULER_add_delayed (timeout, &control_transmit_timeout, cm);
806 GNUNET_CONTAINER_DLL_insert (h->control_head,
810 GNUNET_CONTAINER_DLL_insert_after (h->control_head,
814 schedule_transmission (h);
818 struct SetQuotaContext
820 struct GNUNET_TRANSPORT_Handle *handle;
822 struct GNUNET_PeerIdentity target;
824 GNUNET_SCHEDULER_Task cont;
828 struct GNUNET_TIME_Absolute timeout;
830 struct GNUNET_BANDWIDTH_Value32NBO quota_in;
835 * Send SET_QUOTA message to the service.
837 * @param cls the 'struct SetQuotaContext'
838 * @param size number of bytes available in buf
839 * @param buf where to copy the message
840 * @return number of bytes copied to buf
843 send_set_quota (void *cls, size_t size, void *buf)
845 struct SetQuotaContext *sqc = cls;
846 struct QuotaSetMessage *msg;
850 if (sqc->cont != NULL)
851 GNUNET_SCHEDULER_add_continuation (sqc->cont,
853 GNUNET_SCHEDULER_REASON_TIMEOUT);
858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
859 "Transmitting `%s' request with respect to `%4s'.\n",
861 GNUNET_i2s (&sqc->target));
863 GNUNET_assert (size >= sizeof (struct QuotaSetMessage));
865 msg->header.size = htons (sizeof (struct QuotaSetMessage));
866 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
867 msg->quota = sqc->quota_in;
868 memcpy (&msg->peer, &sqc->target, sizeof (struct GNUNET_PeerIdentity));
869 if (sqc->cont != NULL)
870 GNUNET_SCHEDULER_add_continuation (sqc->cont,
872 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
874 return sizeof (struct QuotaSetMessage);
879 * Set the share of incoming bandwidth for the given
880 * peer to the specified amount.
882 * @param handle connection to transport service
883 * @param target who's bandwidth quota is being changed
884 * @param quota_in incoming bandwidth quota in bytes per ms
885 * @param quota_out outgoing bandwidth quota in bytes per ms
886 * @param timeout how long to wait until signaling failure if
887 * we can not communicate the quota change
888 * @param cont continuation to call when done, will be called
889 * either with reason "TIMEOUT" or with reason "PREREQ_DONE"
890 * @param cont_cls closure for continuation
893 GNUNET_TRANSPORT_set_quota (struct GNUNET_TRANSPORT_Handle *handle,
894 const struct GNUNET_PeerIdentity *target,
895 struct GNUNET_BANDWIDTH_Value32NBO quota_in,
896 struct GNUNET_BANDWIDTH_Value32NBO quota_out,
897 struct GNUNET_TIME_Relative timeout,
898 GNUNET_SCHEDULER_Task cont, void *cont_cls)
900 struct NeighbourList *n;
901 struct SetQuotaContext *sqc;
903 n = neighbour_find (handle, target);
907 if (ntohl (quota_out.value__) != n->out_tracker.available_bytes_per_s__)
908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
909 "Quota changed from %u to %u for peer `%s'\n",
910 (unsigned int) n->out_tracker.available_bytes_per_s__,
911 (unsigned int) ntohl (quota_out.value__),
912 GNUNET_i2s (target));
914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
915 "Quota remains at %u for peer `%s'\n",
916 (unsigned int) n->out_tracker.available_bytes_per_s__,
917 GNUNET_i2s (target));
919 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker,
925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
926 "Quota changed to %u for peer `%s', but I have no such neighbour!\n",
927 (unsigned int) ntohl (quota_out.value__),
928 GNUNET_i2s (target));
931 sqc = GNUNET_malloc (sizeof (struct SetQuotaContext));
932 sqc->handle = handle;
933 sqc->target = *target;
935 sqc->cont_cls = cont_cls;
936 sqc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
937 sqc->quota_in = quota_in;
938 schedule_control_transmit (handle,
939 sizeof (struct QuotaSetMessage),
940 GNUNET_NO, timeout, &send_set_quota, sqc);
945 * Obtain the HELLO message for this peer.
947 * @param handle connection to transport service
948 * @param rec function to call with the HELLO, sender will be our peer
949 * identity; message and sender will be NULL on timeout
950 * (handshake with transport service pending/failed).
951 * cost estimate will be 0.
952 * @param rec_cls closure for rec
955 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
956 GNUNET_TRANSPORT_HelloUpdateCallback rec,
959 struct HelloWaitList *hwl;
961 hwl = GNUNET_malloc (sizeof (struct HelloWaitList));
962 hwl->next = handle->hwl_head;
963 handle->hwl_head = hwl;
964 hwl->handle = handle;
966 hwl->rec_cls = rec_cls;
967 if (handle->my_hello == NULL)
969 rec (rec_cls, (const struct GNUNET_MessageHeader *) handle->my_hello);
975 * Stop receiving updates about changes to our HELLO message.
977 * @param handle connection to transport service
978 * @param rec function previously registered to be called with the HELLOs
979 * @param rec_cls closure for rec
982 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_Handle *handle,
983 GNUNET_TRANSPORT_HelloUpdateCallback rec,
986 struct HelloWaitList *pos;
987 struct HelloWaitList *prev;
990 pos = handle->hwl_head;
993 if ( (pos->rec == rec) &&
994 (pos->rec_cls == rec_cls) )
999 GNUNET_break (pos != NULL);
1003 handle->hwl_head = pos->next;
1005 prev->next = pos->next;
1011 * Send HELLO message to the service.
1013 * @param cls the HELLO message to send
1014 * @param size number of bytes available in buf
1015 * @param buf where to copy the message
1016 * @return number of bytes copied to buf
1019 send_hello (void *cls, size_t size, void *buf)
1021 struct GNUNET_MessageHeader *hello = cls;
1026 #if DEBUG_TRANSPORT_TIMEOUT
1027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1028 "Timeout while trying to transmit `%s' request.\n",
1031 GNUNET_free (hello);
1035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1036 "Transmitting `%s' request.\n", "HELLO");
1038 msize = ntohs (hello->size);
1039 GNUNET_assert (size >= msize);
1040 memcpy (buf, hello, msize);
1041 GNUNET_free (hello);
1047 * Offer the transport service the HELLO of another peer. Note that
1048 * the transport service may just ignore this message if the HELLO is
1049 * malformed or useless due to our local configuration.
1051 * @param handle connection to transport service
1052 * @param hello the hello message
1055 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
1056 const struct GNUNET_MessageHeader *hello)
1058 struct GNUNET_MessageHeader *hc;
1060 struct GNUNET_PeerIdentity peer;
1062 GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
1063 size = ntohs (hello->size);
1064 GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
1065 if (GNUNET_OK != GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message*) hello,
1072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1073 "Offering `%s' message of `%4s' to transport for validation.\n",
1075 GNUNET_i2s (&peer));
1077 hc = GNUNET_malloc (size);
1078 memcpy (hc, hello, size);
1079 schedule_control_transmit (handle,
1081 GNUNET_NO, OFFER_HELLO_TIMEOUT, &send_hello, hc);
1086 * Transmit START message to service.
1089 * @param size number of bytes available in buf
1090 * @param buf where to copy the message
1091 * @return number of bytes copied to buf
1094 send_start (void *cls, size_t size, void *buf)
1096 struct GNUNET_TRANSPORT_Handle *h = cls;
1097 struct StartMessage s;
1101 /* Can only be shutdown, just give up */
1103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1104 "Shutdown while trying to transmit `%s' request.\n",
1110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1111 "Transmitting `%s' request.\n", "START");
1113 GNUNET_assert (size >= sizeof (struct StartMessage));
1114 s.header.size = htons (sizeof (struct StartMessage));
1115 s.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
1116 s.do_check = htonl (h->check_self);
1118 memcpy (buf, &s, sizeof (struct StartMessage));
1119 return sizeof (struct StartMessage);
1126 * @param n the entry to free
1129 neighbour_free (struct NeighbourList *n)
1131 struct GNUNET_TRANSPORT_Handle *h;
1133 /* Added so task gets canceled when a disconnect is received! */
1135 if (n->transmit_handle.notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1137 GNUNET_SCHEDULER_cancel(n->transmit_handle.notify_delay_task);
1138 n->transmit_handle.notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1139 n->transmit_handle.notify = NULL;
1143 GNUNET_assert (n->transmit_handle.notify == NULL);
1146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1147 "Removing neighbour `%s' from list of connected peers.\n",
1148 GNUNET_i2s (&n->id));
1150 GNUNET_break (n->is_connected == GNUNET_NO);
1151 GNUNET_break (n->transmit_stage == TS_NEW);
1153 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(h->neighbours, &n->id.hashPubKey, n));
1160 * Mark neighbour as disconnected.
1162 * @param n the entry to mark as disconnected
1165 neighbour_disconnect (struct NeighbourList *n)
1167 struct GNUNET_TRANSPORT_Handle *h = n->h;
1169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1170 "Removing neighbour `%s' from list of connected peers.\n",
1171 GNUNET_i2s (&n->id));
1173 GNUNET_break (n->is_connected == GNUNET_YES);
1174 n->is_connected = GNUNET_NO;
1175 n->in_disconnect = GNUNET_YES;
1176 if (h->nd_cb != NULL)
1177 h->nd_cb (h->cls, &n->id);
1178 if (n->transmit_stage == TS_NEW)
1185 * Function we use for handling incoming messages.
1187 * @param cls closure (struct GNUNET_TRANSPORT_Handle *)
1188 * @param msg message received, NULL on timeout or fatal error
1190 static void demultiplexer (void *cls,
1191 const struct GNUNET_MessageHeader *msg);
1195 * Iterator over hash map entries, for getting rid of a neighbor
1196 * upon a reconnect call.
1198 * @param cls closure (NULL)
1199 * @param key current key code
1200 * @param value value in the hash map, the neighbour entry to forget
1201 * @return GNUNET_YES if we should continue to
1206 forget_neighbours (void *cls,
1207 const GNUNET_HashCode * key,
1210 struct NeighbourList *n = value;
1211 #if DEBUG_TRANSPORT_DISCONNECT
1212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1213 "Disconnecting due to reconnect being called\n");
1215 if (n->is_connected)
1216 neighbour_disconnect (n);
1222 * Try again to connect to transport service.
1224 * @param cls the handle to the transport service
1225 * @param tc scheduler context
1228 reconnect (void *cls,
1229 const struct GNUNET_SCHEDULER_TaskContext *tc)
1231 struct GNUNET_TRANSPORT_Handle *h = cls;
1232 struct ControlMessage *pos;
1234 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1235 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1237 /* shutdown, just give up */
1240 /* Forget about all neighbours that we used to be connected to */
1241 GNUNET_CONTAINER_multihashmap_iterate(h->neighbours, &forget_neighbours, NULL);
1244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1245 "Connecting to transport service.\n");
1247 GNUNET_assert (h->client == NULL);
1248 h->client = GNUNET_CLIENT_connect ("transport", h->cfg);
1249 GNUNET_assert (h->client != NULL);
1250 /* make sure we don't send "START" twice, remove existing entry from
1251 queue (if present) */
1252 pos = h->control_head;
1255 if (pos->notify == &send_start)
1257 GNUNET_CONTAINER_DLL_remove (h->control_head,
1260 if (GNUNET_SCHEDULER_NO_TASK != pos->notify_delay_task)
1262 GNUNET_SCHEDULER_cancel (pos->notify_delay_task);
1263 pos->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1270 schedule_control_transmit (h,
1271 sizeof (struct StartMessage),
1273 GNUNET_TIME_UNIT_FOREVER_REL, &send_start, h);
1274 GNUNET_CLIENT_receive (h->client,
1275 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1280 * Function that will schedule the job that will try
1281 * to connect us again to the client.
1283 * @param h transport service to reconnect
1286 schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
1289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1290 "Scheduling task to reconnect to transport service in %llu ms.\n",
1291 h->reconnect_delay.rel_value);
1293 GNUNET_assert (h->client == NULL);
1294 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
1296 = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
1297 if (h->reconnect_delay.rel_value == 0)
1299 h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1303 h->reconnect_delay = GNUNET_TIME_relative_multiply (h->reconnect_delay, 2);
1304 h->reconnect_delay = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
1305 h->reconnect_delay);
1311 * Send request connect message to the service.
1313 * @param cls the TransportRequestConnectMessage
1314 * @param size number of bytes available in buf
1315 * @param buf where to copy the message
1316 * @return number of bytes copied to buf
1319 send_transport_request_connect (void *cls, size_t size, void *buf)
1321 struct TransportRequestConnectMessage *trcm = cls;
1326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1327 "Buffer null for %s\n",
1334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1335 "Transmitting `%s' request for `%4s'.\n",
1337 GNUNET_i2s (&trcm->peer));
1339 GNUNET_assert (size >= sizeof (struct TransportRequestConnectMessage));
1340 memcpy(buf, trcm, sizeof(struct TransportRequestConnectMessage));
1341 return sizeof(struct TransportRequestConnectMessage);
1345 * Create and send a request connect message to
1346 * the transport service for a particular peer.
1348 * @param h handle to the transport service
1349 * @param n the neighbor to send the request connect message about
1353 send_request_connect_message(struct GNUNET_TRANSPORT_Handle *h, struct NeighbourList *n)
1355 struct TransportRequestConnectMessage *trcm;
1357 trcm = GNUNET_malloc(sizeof(struct TransportRequestConnectMessage));
1358 trcm->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT);
1359 trcm->header.size = htons(sizeof(struct TransportRequestConnectMessage));
1360 memcpy(&trcm->peer, &n->id, sizeof(struct GNUNET_PeerIdentity));
1361 schedule_control_transmit (h,
1362 sizeof (struct TransportRequestConnectMessage),
1364 GNUNET_TIME_UNIT_FOREVER_REL, &send_transport_request_connect, trcm);
1368 * Add neighbour to our list
1370 * @return NULL if this API is currently disconnecting from the service
1372 static struct NeighbourList *
1373 neighbour_add (struct GNUNET_TRANSPORT_Handle *h,
1374 const struct GNUNET_PeerIdentity *pid)
1376 struct NeighbourList *n;
1378 if (GNUNET_YES == h->in_disconnect)
1380 /* check for duplicates */
1381 if (NULL != (n = neighbour_find (h, pid)))
1387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1388 "Creating entry for neighbour `%4s'.\n",
1391 n = GNUNET_malloc (sizeof (struct NeighbourList));
1394 GNUNET_BANDWIDTH_tracker_init (&n->out_tracker,
1395 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1396 MAX_BANDWIDTH_CARRY_S);
1397 GNUNET_CONTAINER_multihashmap_put (h->neighbours,
1400 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1406 * Iterator over hash map entries, for deleting state of a neighbor.
1408 * @param cls closure (NULL)
1409 * @param key current key code
1410 * @param value value in the hash map, the neighbour entry to delete
1411 * @return GNUNET_YES if we should continue to
1416 delete_neighbours (void *cls,
1417 const GNUNET_HashCode * key,
1420 struct NeighbourList *n = value;
1421 struct GNUNET_TRANSPORT_TransmitHandle *th;
1423 switch (n->transmit_stage)
1426 case TS_TRANSMITTED:
1430 case TS_TRANSMITTED_QUEUED:
1431 th = &n->transmit_handle;
1432 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1434 GNUNET_SCHEDULER_cancel (th->notify_delay_task);
1435 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1437 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
1448 * Connect to the transport service. Note that the connection may
1449 * complete (or fail) asynchronously.
1451 * @param cfg configuration to use
1452 * @param self our own identity (API should check that it matches
1453 * the identity found by transport), or NULL (no check)
1454 * @param cls closure for the callbacks
1455 * @param rec receive function to call
1456 * @param nc function to call on connect events
1457 * @param nd function to call on disconnect events
1459 struct GNUNET_TRANSPORT_Handle *
1460 GNUNET_TRANSPORT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1461 const struct GNUNET_PeerIdentity *self,
1463 GNUNET_TRANSPORT_ReceiveCallback rec,
1464 GNUNET_TRANSPORT_NotifyConnect nc,
1465 GNUNET_TRANSPORT_NotifyDisconnect nd)
1467 struct GNUNET_TRANSPORT_Handle *ret;
1469 ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Handle));
1473 ret->check_self = GNUNET_YES;
1480 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1481 ret->neighbours = GNUNET_CONTAINER_multihashmap_create(STARTING_NEIGHBOURS_SIZE);
1482 schedule_reconnect (ret);
1488 * Disconnect from the transport service.
1491 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1493 struct HelloWaitList *hwl;
1494 struct GNUNET_CLIENT_Connection *client;
1495 struct ControlMessage *cm;
1498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transport disconnect called!\n");
1500 handle->in_disconnect = GNUNET_YES;
1502 GNUNET_assert(GNUNET_SYSERR !=
1503 GNUNET_CONTAINER_multihashmap_iterate(handle->neighbours,
1506 GNUNET_CONTAINER_multihashmap_destroy(handle->neighbours);
1508 while (NULL != (hwl = handle->hwl_head))
1510 handle->hwl_head = hwl->next;
1511 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1513 ("Disconnect while notification for `%s' still registered.\n"),
1515 if (hwl->rec != NULL)
1516 hwl->rec (hwl->rec_cls, NULL);
1520 /* Check for still scheduled control messages, cancel delay tasks if so */
1521 /* Added because somehow a notify_delay_task is remaining scheduled and is ever so annoying */
1522 while ( (NULL != (cm = handle->control_head)))
1525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1526 "Disconnect before control message sent!\n");
1528 if (cm->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1530 GNUNET_SCHEDULER_cancel (cm->notify_delay_task);
1531 cm->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1533 GNUNET_CONTAINER_DLL_remove (handle->control_head,
1534 handle->control_tail,
1540 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1542 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1543 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1545 if (handle->quota_task != GNUNET_SCHEDULER_NO_TASK)
1547 GNUNET_SCHEDULER_cancel (handle->quota_task);
1548 handle->quota_task = GNUNET_SCHEDULER_NO_TASK;
1550 GNUNET_free_non_null (handle->my_hello);
1551 handle->my_hello = NULL;
1553 if (NULL != handle->network_handle)
1555 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->network_handle);
1556 handle->network_handle = NULL;
1558 if (NULL != (client = handle->client))
1561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1562 "Disconnecting from transport service for good.\n");
1564 handle->client = NULL;
1565 GNUNET_CLIENT_disconnect (client, GNUNET_NO);
1567 GNUNET_free (handle);
1572 * Function we use for handling incoming messages.
1574 * @param cls closure (struct GNUNET_TRANSPORT_Handle *)
1575 * @param msg message received, NULL on timeout or fatal error
1578 demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
1580 struct GNUNET_TRANSPORT_Handle *h = cls;
1581 const struct DisconnectInfoMessage *dim;
1582 const struct ConnectInfoMessage *cim;
1583 const struct InboundMessage *im;
1584 const struct GNUNET_MessageHeader *imm;
1585 const struct SendOkMessage *okm;
1586 struct HelloWaitList *hwl;
1587 struct HelloWaitList *next_hwl;
1588 struct NeighbourList *n;
1589 struct GNUNET_PeerIdentity me;
1592 if (h->client == NULL)
1594 /* shutdown initiated from 'GNUNET_TRANSPORT_disconnect',
1595 finish clean up work! */
1602 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1603 "Error receiving from transport service, disconnecting temporarily.\n");
1605 if (h->network_handle != NULL)
1607 GNUNET_CLIENT_notify_transmit_ready_cancel (h->network_handle);
1608 h->network_handle = NULL;
1610 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
1612 schedule_reconnect (h);
1615 GNUNET_CLIENT_receive (h->client,
1616 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1617 size = ntohs (msg->size);
1618 switch (ntohs (msg->type))
1620 case GNUNET_MESSAGE_TYPE_HELLO:
1622 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg,
1629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1630 "Receiving (my own) `%s' message, I am `%4s'.\n",
1631 "HELLO", GNUNET_i2s (&me));
1633 GNUNET_free_non_null (h->my_hello);
1635 if (size < sizeof (struct GNUNET_MessageHeader))
1640 h->my_hello = GNUNET_malloc (size);
1641 memcpy (h->my_hello, msg, size);
1645 next_hwl = hwl->next;
1646 hwl->rec (hwl->rec_cls,
1647 (const struct GNUNET_MessageHeader *) h->my_hello);
1651 case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
1652 if (size != sizeof (struct ConnectInfoMessage))
1657 cim = (const struct ConnectInfoMessage *) msg;
1659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1660 "Receiving `%s' message for `%4s'.\n",
1661 "CONNECT", GNUNET_i2s (&cim->id));
1663 n = neighbour_find (h, &cim->id);
1665 n = neighbour_add (h,
1672 GNUNET_break (n->is_connected == GNUNET_NO);
1673 if (ntohl ((&cim->ats)[ntohl (cim->ats_count)].type) != GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR)
1678 fprintf(stderr,"transport_api GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT ats_count %u\n",ntohl (cim->ats_count));
1679 n->is_connected = GNUNET_YES;
1680 if (h->nc_cb != NULL)
1681 h->nc_cb (h->cls, &n->id,
1684 /* if (h->nc_cb != NULL)
1685 h->nc_cb (h->cls, &n->id,
1687 ntohl (cim->ats_count));*/
1689 case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
1690 if (size != sizeof (struct DisconnectInfoMessage))
1695 dim = (const struct DisconnectInfoMessage *) msg;
1696 GNUNET_break (ntohl (dim->reserved) == 0);
1697 #if DEBUG_TRANSPORT_DISCONNECT
1698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1699 "Receiving `%s' message for `%4s'.\n",
1701 GNUNET_i2s (&dim->peer));
1703 n = neighbour_find (h, &dim->peer);
1704 GNUNET_break (n != NULL);
1706 neighbour_disconnect (n);
1708 case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
1709 if (size != sizeof (struct SendOkMessage))
1714 okm = (const struct SendOkMessage *) msg;
1716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1717 "Receiving `%s' message, transmission %s.\n", "SEND_OK",
1718 ntohl (okm->success) == GNUNET_OK ? "succeeded" : "failed");
1720 n = neighbour_find (h, &okm->peer);
1721 GNUNET_assert (n != NULL);
1722 switch (n->transmit_stage)
1730 case TS_TRANSMITTED:
1731 n->transmit_stage = TS_NEW;
1733 case TS_TRANSMITTED_QUEUED:
1734 n->transmit_stage = TS_QUEUED;
1735 schedule_transmission (h);
1741 case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
1743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1744 "Receiving `%s' message.\n", "RECV");
1747 sizeof (struct InboundMessage) +
1748 sizeof (struct GNUNET_MessageHeader))
1753 im = (const struct InboundMessage *) msg;
1754 GNUNET_break (0 == ntohl (im->reserved));
1755 GNUNET_assert(sizeof (struct InboundMessage) + ntohl(im->ats_count) * sizeof(struct GNUNET_TRANSPORT_ATS_Information) + sizeof (struct GNUNET_MessageHeader) <= size);
1756 imm = (const struct GNUNET_MessageHeader *) &((&im->ats)[ntohl(im->ats_count)+1]);
1757 if (ntohs (imm->size) + sizeof (struct InboundMessage) + ntohl(im->ats_count) * sizeof(struct GNUNET_TRANSPORT_ATS_Information) != size)
1763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1764 "Received message of type %u from `%4s'.\n",
1765 ntohs (imm->type), GNUNET_i2s (&im->peer));
1767 n = neighbour_find (h, &im->peer);
1773 if (n->is_connected != GNUNET_YES)
1778 if (ntohl ((&im->ats)[ntohl(im->ats_count)].type) != GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR)
1783 fprintf(stderr,"transport_api GNUNET_MESSAGE_TYPE_TRANSPORT_RECV ats_count %u\n",ntohl (im->ats_count));
1785 h->rec (h->cls, &im->peer,
1790 /*h->rec (h->cls, &im->peer,
1793 ntohl (im->ats_count));*/
1796 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1798 ("Received unexpected message of type %u in %s:%u\n"),
1799 ntohs (msg->type), __FILE__, __LINE__);
1807 * Called when our transmit request timed out before any transport
1808 * reported success connecting to the desired peer or before the
1809 * transport was ready to receive. Signal error and free
1812 * @param cls the 'struct GNUNET_TRANSPORT_TransmitHandle*' that is timing out
1813 * @param tc scheduler context
1816 peer_transmit_timeout (void *cls,
1817 const struct GNUNET_SCHEDULER_TaskContext *tc)
1819 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
1820 struct NeighbourList *n;
1821 GNUNET_CONNECTION_TransmitReadyNotify notify;
1824 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1827 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1828 "Triggering timeout for request to transmit to `%4s' (%d)\n",
1829 GNUNET_i2s (&n->id),
1832 notify = th->notify;
1834 notify_cls = th->notify_cls;
1835 switch (n->transmit_stage)
1841 n->transmit_stage = TS_NEW;
1842 if (n->is_connected == GNUNET_NO)
1845 case TS_TRANSMITTED:
1848 case TS_TRANSMITTED_QUEUED:
1849 n->transmit_stage = TS_TRANSMITTED;
1855 notify (notify_cls, 0, NULL);
1860 * Check if we could queue a message of the given size for
1861 * transmission. The transport service will take both its
1862 * internal buffers and bandwidth limits imposed by the
1863 * other peer into consideration when answering this query.
1865 * @param handle connection to transport service
1866 * @param target who should receive the message
1867 * @param size how big is the message we want to transmit?
1868 * @param priority how important is the message?
1869 * @param timeout after how long should we give up (and call
1870 * notify with buf NULL and size 0)?
1871 * @param notify function to call when we are ready to
1872 * send such a message
1873 * @param notify_cls closure for notify
1874 * @return NULL if someone else is already waiting to be notified
1875 * non-NULL if the notify callback was queued (can be used to cancel
1876 * using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1878 struct GNUNET_TRANSPORT_TransmitHandle *
1879 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle
1881 const struct GNUNET_PeerIdentity
1882 *target, size_t size,
1883 unsigned int priority,
1884 struct GNUNET_TIME_Relative timeout,
1885 GNUNET_CONNECTION_TransmitReadyNotify
1886 notify, void *notify_cls)
1888 struct GNUNET_TRANSPORT_TransmitHandle *th;
1889 struct NeighbourList *n;
1891 if (size + sizeof (struct OutboundMessage) >=
1892 GNUNET_SERVER_MAX_MESSAGE_SIZE)
1895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1896 "Message size is %d, max allowed is %d.\n",
1897 size + sizeof (struct OutboundMessage), GNUNET_SERVER_MAX_MESSAGE_SIZE - 1);
1903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1904 "Asking transport service for transmission of %u bytes to peer `%4s' within %llu ms.\n",
1905 size, GNUNET_i2s (target),
1906 (unsigned long long) timeout.rel_value);
1908 n = neighbour_find (handle, target);
1911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1912 "Created neighbour entry for peer `%s'\n",
1913 GNUNET_i2s (target));
1914 n = neighbour_add (handle, target);
1919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1920 "Could not create neighbour entry for peer `%s'\n",
1921 GNUNET_i2s (target));
1926 * Send a request connect message if not connected,
1927 * otherwise we will never send anything to
1930 if (n->is_connected == GNUNET_NO)
1932 send_request_connect_message(handle, n);
1935 switch (n->transmit_stage)
1938 n->transmit_stage = TS_QUEUED;
1943 case TS_TRANSMITTED:
1944 n->transmit_stage = TS_TRANSMITTED_QUEUED;
1946 case TS_TRANSMITTED_QUEUED:
1953 th = &n->transmit_handle;
1955 th->notify = notify;
1956 th->notify_cls = notify_cls;
1957 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1958 th->notify_size = size + sizeof (struct OutboundMessage);
1959 th->priority = priority;
1960 th->notify_delay_task
1961 = GNUNET_SCHEDULER_add_delayed (timeout,
1962 &peer_transmit_timeout, th);
1963 schedule_transmission (handle);
1969 * Cancel the specified transmission-ready notification.
1972 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
1973 GNUNET_TRANSPORT_TransmitHandle
1976 struct NeighbourList *n;
1981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1982 "Transmission request of %u bytes to `%4s' was canceled.\n",
1983 th->notify_size - sizeof (struct OutboundMessage),
1984 GNUNET_i2s (&n->id));
1986 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1988 GNUNET_SCHEDULER_cancel (th->notify_delay_task);
1989 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1991 switch (n->transmit_stage)
1997 n->transmit_stage = TS_NEW;
1998 if (n->in_disconnect == GNUNET_NO)
2001 case TS_TRANSMITTED:
2004 case TS_TRANSMITTED_QUEUED:
2005 n->transmit_stage = TS_TRANSMITTED;
2013 /* end of transport_api.c */