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?
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!).
402 * Size of the HELLO copied to end of struct.
407 * Continuation to call once HELLO sent.
409 GNUNET_SCHEDULER_Task cont;
412 * Closure to call with the continuation.
421 * Get the neighbour list entry for the given peer
423 * @param h our context
424 * @param peer peer to look up
425 * @return NULL if no such peer entry exists
427 static struct NeighbourList *
428 neighbour_find (struct GNUNET_TRANSPORT_Handle *h,
429 const struct GNUNET_PeerIdentity *peer)
431 return GNUNET_CONTAINER_multihashmap_get(h->neighbours, &peer->hashPubKey);
436 * Schedule the task to send one message, either from the control
437 * list or the peer message queues to the service.
439 static void schedule_transmission (struct GNUNET_TRANSPORT_Handle *h);
443 * Function called by the scheduler when the timeout for bandwidth
444 * availablility for the target neighbour is reached.
446 * @param cls the 'struct GNUNET_TRANSPORT_Handle*'
447 * @param tc scheduler context
450 quota_transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
452 struct GNUNET_TRANSPORT_Handle *h = cls;
454 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
455 schedule_transmission (h);
460 * Iterator over hash map entries, attempt to schedule
461 * a transmission to entries in the neighbour hashmap.
463 * @param cls closure a TryTransmitContext
464 * @param key current key code
465 * @param value value in the hash map, the neighbour entry to consider
466 * @return GNUNET_YES if we should continue to
471 try_schedule_transmission (void *cls,
472 const GNUNET_HashCode * key,
475 struct NeighbourList *n = value;
476 struct TryTransmitContext *try_transmit_ctx = cls;
477 struct GNUNET_TIME_Relative duration;
478 GNUNET_CONNECTION_TransmitReadyNotify notify;
479 struct GNUNET_TRANSPORT_TransmitHandle *th;
480 struct GNUNET_TIME_Absolute duration_abs;
482 if (n->transmit_stage != TS_QUEUED)
483 return GNUNET_YES; /* not eligible, keep iterating */
484 if (n->is_connected != GNUNET_YES)
485 return GNUNET_YES; /* keep iterating */
487 th = &n->transmit_handle;
488 GNUNET_break (n == th->neighbour);
489 /* check outgoing quota */
490 duration = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
491 th->notify_size - sizeof (struct OutboundMessage));
492 duration_abs = GNUNET_TIME_relative_to_absolute (duration);
493 if (th->timeout.abs_value < duration_abs.abs_value)
495 /* signal timeout! */
497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
498 "Would need %llu ms before bandwidth is available for delivery to `%4s', that is too long. Signaling timeout.\n",
500 GNUNET_i2s (&n->id));
502 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
504 GNUNET_SCHEDULER_cancel (th->notify_delay_task);
505 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
507 n->transmit_stage = TS_NEW;
508 if (NULL != (notify = th->notify))
511 GNUNET_assert (0 == notify (th->notify_cls, 0, NULL));
513 return GNUNET_YES; /* keep iterating */
515 if (duration.rel_value > 0)
518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
519 "Need more bandwidth (%u b/s allowed, %u b needed), delaying delivery to `%4s' by %llu ms\n",
520 (unsigned int) n->out_tracker.available_bytes_per_s__,
521 (unsigned int) th->notify_size - sizeof (struct OutboundMessage),
523 (unsigned long long) duration.rel_value);
525 try_transmit_ctx->retry_time = GNUNET_TIME_relative_min (try_transmit_ctx->retry_time,
527 return GNUNET_YES; /* keep iterating */
530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
531 "Have %u bytes of bandwidth available for transmission to `%4s' right now\n",
532 th->notify_size - sizeof (struct OutboundMessage),
533 GNUNET_i2s (&n->id));
536 if ( (try_transmit_ctx->ret == NULL) ||
537 (try_transmit_ctx->ret->priority < th->priority) )
538 try_transmit_ctx->ret = th;
544 * Figure out which transmission to a peer can be done right now.
545 * If none can, schedule a task to call 'schedule_transmission'
546 * whenever a peer transmission can be done in the future and
547 * return NULL. Otherwise return the next transmission to be
550 * @param h handle to transport
551 * @return NULL to wait longer before doing any peer transmissions
553 static struct GNUNET_TRANSPORT_TransmitHandle *
554 schedule_peer_transmission (struct GNUNET_TRANSPORT_Handle *h)
556 struct TryTransmitContext try_transmit_ctx;
558 if (h->quota_task != GNUNET_SCHEDULER_NO_TASK)
560 GNUNET_SCHEDULER_cancel (h->quota_task);
561 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
563 try_transmit_ctx.h = h;
564 try_transmit_ctx.ret = NULL;
565 try_transmit_ctx.retry_time = GNUNET_TIME_UNIT_FOREVER_REL;
566 GNUNET_CONTAINER_multihashmap_iterate(h->neighbours,
567 &try_schedule_transmission,
569 if (try_transmit_ctx.ret == NULL)
570 h->quota_task = GNUNET_SCHEDULER_add_delayed (try_transmit_ctx.retry_time,
571 "a_transmit_ready,
573 return try_transmit_ctx.ret;
578 * Transmit message(s) to service.
580 * @param cls handle to transport
581 * @param size number of bytes available in buf
582 * @param buf where to copy the message
583 * @return number of bytes copied to buf
586 transport_notify_ready (void *cls, size_t size, void *buf)
588 struct GNUNET_TRANSPORT_Handle *h = cls;
589 struct ControlMessage *cm;
590 struct GNUNET_TRANSPORT_TransmitHandle *th;
591 struct NeighbourList *n;
592 struct OutboundMessage obm;
593 GNUNET_CONNECTION_TransmitReadyNotify notify;
599 h->network_handle = NULL;
602 schedule_transmission (h);
606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
607 "Ready to transmit %u bytes to transport service\n", size);
611 while ( (NULL != (cm = h->control_head)) &&
612 (cm->notify_size <= size) )
614 if (cm->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
616 GNUNET_SCHEDULER_cancel (cm->notify_delay_task);
617 cm->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
619 GNUNET_CONTAINER_DLL_remove (h->control_head,
622 nret = cm->notify (cm->notify_cls, size, &cbuf[ret]);
624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
625 "Added %u bytes of control message at %u\n",
633 while ( (NULL != (th = schedule_peer_transmission (h))) &&
634 (th->notify_size <= size) )
636 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
638 GNUNET_SCHEDULER_cancel (th->notify_delay_task);
639 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
642 switch (n->transmit_stage)
648 n->transmit_stage = TS_TRANSMITTED;
653 case TS_TRANSMITTED_QUEUED:
659 GNUNET_assert (size >= sizeof (struct OutboundMessage));
662 mret = notify (th->notify_cls,
663 size - sizeof (struct OutboundMessage),
664 &cbuf[ret + sizeof (struct OutboundMessage)]);
665 GNUNET_assert (mret <= size - sizeof (struct OutboundMessage));
667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
668 "Message of %u bytes with timeout %llums constructed for `%4s'\n",
670 (unsigned long long) GNUNET_TIME_absolute_get_remaining (th->timeout).rel_value,
671 GNUNET_i2s (&n->id));
675 GNUNET_assert (mret + sizeof (struct OutboundMessage) < GNUNET_SERVER_MAX_MESSAGE_SIZE);
676 obm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
677 obm.header.size = htons (mret + sizeof (struct OutboundMessage));
678 obm.priority = htonl (th->priority);
679 obm.timeout = GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining (th->timeout));
681 memcpy (&cbuf[ret], &obm, sizeof (struct OutboundMessage));
682 ret += (mret + sizeof (struct OutboundMessage));
683 size -= (mret + sizeof (struct OutboundMessage));
684 GNUNET_BANDWIDTH_tracker_consume (&n->out_tracker, mret);
688 switch (n->transmit_stage)
697 n->transmit_stage = TS_NEW;
699 case TS_TRANSMITTED_QUEUED:
700 n->transmit_stage = TS_QUEUED;
707 schedule_transmission (h);
709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
710 "Transmitting %u bytes to transport service\n", ret);
717 * Schedule the task to send one message, either from the control
718 * list or the peer message queues to the service.
721 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
724 struct GNUNET_TIME_Relative timeout;
725 struct GNUNET_TRANSPORT_TransmitHandle *th;
727 if (NULL != h->network_handle)
729 if (h->client == NULL)
731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
732 _("Could not yet schedule transmission: we are not yet connected to the transport service!\n"));
733 return; /* not yet connected */
735 if (NULL != h->control_head)
737 size = h->control_head->notify_size;
738 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
742 th = schedule_peer_transmission (h);
745 /* no transmission ready right now */
747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
748 "Could not yet schedule transmission: none ready\n");
752 size = th->notify_size;
753 timeout = GNUNET_TIME_absolute_get_remaining (th->timeout);
756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
757 "Calling notify_transmit_ready\n");
760 GNUNET_CLIENT_notify_transmit_ready (h->client,
764 &transport_notify_ready,
766 GNUNET_assert (NULL != h->network_handle);
771 * Called when our transmit request timed out before any transport
772 * reported success connecting to the desired peer or before the
773 * transport was ready to receive. Signal error and free
777 control_transmit_timeout (void *cls,
778 const struct GNUNET_SCHEDULER_TaskContext *tc)
780 struct ControlMessage *th = cls;
782 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
783 if (NULL != th->notify)
784 th->notify (th->notify_cls, 0, NULL);
785 GNUNET_CONTAINER_DLL_remove (th->h->control_head,
793 * Queue control request for transmission to the transport
796 * @param h handle to the transport service
797 * @param size number of bytes to be transmitted
798 * @param at_head request must be added to the head of the queue
799 * (otherwise request will be appended)
800 * @param timeout how long this transmission can wait (at most)
801 * @param notify function to call to get the content
802 * @param notify_cls closure for notify
805 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h,
808 struct GNUNET_TIME_Relative timeout,
809 GNUNET_CONNECTION_TransmitReadyNotify notify,
812 struct ControlMessage *cm;
815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
816 "Control transmit of %u bytes within %llums requested\n",
817 size, (unsigned long long) timeout.rel_value);
819 cm = GNUNET_malloc (sizeof (struct ControlMessage));
822 cm->notify_cls = notify_cls;
823 cm->notify_size = size;
824 cm->notify_delay_task
825 = GNUNET_SCHEDULER_add_delayed (timeout, &control_transmit_timeout, cm);
827 GNUNET_CONTAINER_DLL_insert (h->control_head,
831 GNUNET_CONTAINER_DLL_insert_after (h->control_head,
835 schedule_transmission (h);
842 struct SetQuotaContext
847 struct GNUNET_TRANSPORT_Handle *handle;
852 struct GNUNET_PeerIdentity target;
857 GNUNET_SCHEDULER_Task cont;
860 * Closure for 'cont'.
867 struct GNUNET_TIME_Absolute timeout;
872 struct GNUNET_BANDWIDTH_Value32NBO quota_in;
877 * Send SET_QUOTA message to the service.
879 * @param cls the 'struct SetQuotaContext'
880 * @param size number of bytes available in buf
881 * @param buf where to copy the message
882 * @return number of bytes copied to buf
885 send_set_quota (void *cls, size_t size, void *buf)
887 struct SetQuotaContext *sqc = cls;
888 struct QuotaSetMessage *msg;
892 if (sqc->cont != NULL)
893 GNUNET_SCHEDULER_add_continuation (sqc->cont,
895 GNUNET_SCHEDULER_REASON_TIMEOUT);
900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
901 "Transmitting `%s' request with respect to `%4s'.\n",
903 GNUNET_i2s (&sqc->target));
905 GNUNET_assert (size >= sizeof (struct QuotaSetMessage));
907 msg->header.size = htons (sizeof (struct QuotaSetMessage));
908 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
909 msg->quota = sqc->quota_in;
910 memcpy (&msg->peer, &sqc->target, sizeof (struct GNUNET_PeerIdentity));
911 if (sqc->cont != NULL)
912 GNUNET_SCHEDULER_add_continuation (sqc->cont,
914 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
916 return sizeof (struct QuotaSetMessage);
921 * Set the share of incoming bandwidth for the given
922 * peer to the specified amount.
924 * @param handle connection to transport service
925 * @param target who's bandwidth quota is being changed
926 * @param quota_in incoming bandwidth quota in bytes per ms
927 * @param quota_out outgoing bandwidth quota in bytes per ms
930 GNUNET_TRANSPORT_set_quota (struct GNUNET_TRANSPORT_Handle *handle,
931 const struct GNUNET_PeerIdentity *target,
932 struct GNUNET_BANDWIDTH_Value32NBO quota_in,
933 struct GNUNET_BANDWIDTH_Value32NBO quota_out)
935 struct NeighbourList *n;
936 struct SetQuotaContext *sqc;
938 n = neighbour_find (handle, target);
942 if (ntohl (quota_out.value__) != n->out_tracker.available_bytes_per_s__)
943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
944 "Quota changed from %u to %u for peer `%s'\n",
945 (unsigned int) n->out_tracker.available_bytes_per_s__,
946 (unsigned int) ntohl (quota_out.value__),
947 GNUNET_i2s (target));
949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
950 "Quota remains at %u for peer `%s'\n",
951 (unsigned int) n->out_tracker.available_bytes_per_s__,
952 GNUNET_i2s (target));
954 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker,
960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
961 "Quota changed to %u for peer `%s', but I have no such neighbour!\n",
962 (unsigned int) ntohl (quota_out.value__),
963 GNUNET_i2s (target));
966 sqc = GNUNET_malloc (sizeof (struct SetQuotaContext));
967 sqc->handle = handle;
968 sqc->target = *target;
970 sqc->cont_cls = NULL;
971 sqc->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_FOREVER_REL);
972 sqc->quota_in = quota_in;
973 schedule_control_transmit (handle,
974 sizeof (struct QuotaSetMessage),
976 GNUNET_TIME_UNIT_FOREVER_REL, &send_set_quota, sqc);
981 * Ask the transport service to establish a connection to
984 * @param handle connection to transport service
985 * @param target who we should try to connect to
988 GNUNET_TRANSPORT_try_connect (struct GNUNET_TRANSPORT_Handle *handle,
989 const struct GNUNET_PeerIdentity *target)
991 /* old API does nothing */
998 * Obtain the HELLO message for this peer.
1000 * @param handle connection to transport service
1001 * @param rec function to call with the HELLO, sender will be our peer
1002 * identity; message and sender will be NULL on timeout
1003 * (handshake with transport service pending/failed).
1004 * cost estimate will be 0.
1005 * @param rec_cls closure for rec
1008 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
1009 GNUNET_TRANSPORT_HelloUpdateCallback rec,
1012 struct HelloWaitList *hwl;
1014 hwl = GNUNET_malloc (sizeof (struct HelloWaitList));
1015 hwl->next = handle->hwl_head;
1016 handle->hwl_head = hwl;
1017 hwl->handle = handle;
1019 hwl->rec_cls = rec_cls;
1020 if (handle->my_hello == NULL)
1022 #if DEBUG_TRANSPORT_HELLO
1023 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1024 "No HELLO yet, waiting to receive it from transport service\n");
1028 rec (rec_cls, (const struct GNUNET_MessageHeader *) handle->my_hello);
1034 * Stop receiving updates about changes to our HELLO message.
1036 * @param handle connection to transport service
1037 * @param rec function previously registered to be called with the HELLOs
1038 * @param rec_cls closure for rec
1041 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_Handle *handle,
1042 GNUNET_TRANSPORT_HelloUpdateCallback rec,
1045 struct HelloWaitList *pos;
1046 struct HelloWaitList *prev;
1049 pos = handle->hwl_head;
1052 if ( (pos->rec == rec) &&
1053 (pos->rec_cls == rec_cls) )
1058 GNUNET_break (pos != NULL);
1062 handle->hwl_head = pos->next;
1064 prev->next = pos->next;
1070 * Send HELLO message to the service.
1072 * @param cls the HELLO message to send
1073 * @param size number of bytes available in buf
1074 * @param buf where to copy the message
1075 * @return number of bytes copied to buf
1078 send_hello (void *cls, size_t size, void *buf)
1080 struct HelloContext *hc = cls;
1084 #if DEBUG_TRANSPORT_TIMEOUT
1085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1086 "Timeout while trying to transmit `%s' request.\n",
1089 if (NULL != hc->cont)
1090 GNUNET_SCHEDULER_add_now(hc->cont, hc->cont_cls);
1095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1096 "Transmitting `%s' request.\n", "HELLO");
1098 GNUNET_assert (size >= hc->size);
1099 memcpy (buf, &hc[1], hc->size);
1101 if (hc->cont != NULL)
1103 GNUNET_SCHEDULER_add_continuation(hc->cont, hc->cont_cls, GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1112 * Offer the transport service the HELLO of another peer. Note that
1113 * the transport service may just ignore this message if the HELLO is
1114 * malformed or useless due to our local configuration.
1116 * @param handle connection to transport service
1117 * @param hello the hello message
1118 * @param cont continuation to call when HELLO has been sent
1119 * @param cls closure for continuation
1123 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
1124 const struct GNUNET_MessageHeader *hello,
1125 GNUNET_SCHEDULER_Task cont,
1129 struct GNUNET_PeerIdentity peer;
1130 struct HelloContext *hc;
1132 GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
1133 size = ntohs (hello->size);
1134 GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
1135 if (GNUNET_OK != GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message*) hello,
1141 hc = GNUNET_malloc(sizeof(struct HelloContext) + size);
1145 memcpy (&hc[1], hello, size);
1148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1149 "Offering `%s' message of `%4s' to transport for validation.\n",
1151 GNUNET_i2s (&peer));
1154 schedule_control_transmit (handle,
1156 GNUNET_NO, OFFER_HELLO_TIMEOUT, &send_hello, hc);
1161 * Transmit START message to service.
1164 * @param size number of bytes available in buf
1165 * @param buf where to copy the message
1166 * @return number of bytes copied to buf
1169 send_start (void *cls, size_t size, void *buf)
1171 struct GNUNET_TRANSPORT_Handle *h = cls;
1172 struct StartMessage s;
1176 /* Can only be shutdown, just give up */
1178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1179 "Shutdown while trying to transmit `%s' request.\n",
1185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1186 "Transmitting `%s' request.\n", "START");
1188 GNUNET_assert (size >= sizeof (struct StartMessage));
1189 s.header.size = htons (sizeof (struct StartMessage));
1190 s.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
1191 s.do_check = htonl (h->check_self);
1193 memcpy (buf, &s, sizeof (struct StartMessage));
1194 return sizeof (struct StartMessage);
1201 * @param n the entry to free
1204 neighbour_free (struct NeighbourList *n)
1206 struct GNUNET_TRANSPORT_Handle *h;
1208 /* Added so task gets canceled when a disconnect is received! */
1210 if (n->transmit_handle.notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1212 GNUNET_SCHEDULER_cancel(n->transmit_handle.notify_delay_task);
1213 n->transmit_handle.notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1214 n->transmit_handle.notify = NULL;
1217 /* NATE: if the above is not needed, then clearly this assertion
1218 should hold (I've checked the code and I'm pretty sure this is
1220 FIXME: remove above comments once we've seen tests pass with the assert... */
1221 GNUNET_assert (n->transmit_handle.notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
1222 GNUNET_assert (n->transmit_handle.notify == NULL);
1225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1226 "Removing neighbour `%s' from list of connected peers.\n",
1227 GNUNET_i2s (&n->id));
1229 GNUNET_break (n->is_connected == GNUNET_NO);
1230 GNUNET_break (n->transmit_stage == TS_NEW);
1232 GNUNET_assert(GNUNET_YES ==
1233 GNUNET_CONTAINER_multihashmap_remove(h->neighbours,
1241 * Mark neighbour as disconnected.
1243 * @param n the entry to mark as disconnected
1246 neighbour_disconnect (struct NeighbourList *n)
1248 struct GNUNET_TRANSPORT_Handle *h = n->h;
1250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1251 "Removing neighbour `%s' from list of connected peers.\n",
1252 GNUNET_i2s (&n->id));
1254 GNUNET_break (n->is_connected == GNUNET_YES);
1255 n->is_connected = GNUNET_NO;
1256 /* FIXME: this 'in_disconnect' flag is dubious; we should define
1257 clearly what disconnect means for pending 'notify_transmit_ready'
1258 requests; maybe a good approach is to REQUIRE clients to
1259 call 'notify_transmit_ready_cancel' on pending requests on disconnect
1260 and otherwise FAIL HARD with an assertion failure before
1261 'neighbour_free' right here (transmit_stage would be forced
1263 n->in_disconnect = GNUNET_YES;
1264 if (h->nd_cb != NULL)
1265 h->nd_cb (h->cls, &n->id);
1266 if (n->transmit_stage == TS_NEW)
1272 * Function we use for handling incoming messages.
1274 * @param cls closure (struct GNUNET_TRANSPORT_Handle *)
1275 * @param msg message received, NULL on timeout or fatal error
1277 static void demultiplexer (void *cls,
1278 const struct GNUNET_MessageHeader *msg);
1282 * Iterator over hash map entries, for getting rid of a neighbor
1283 * upon a reconnect call.
1285 * @param cls closure (NULL)
1286 * @param key current key code
1287 * @param value value in the hash map, the neighbour entry to forget
1288 * @return GNUNET_YES if we should continue to
1293 forget_neighbours (void *cls,
1294 const GNUNET_HashCode * key,
1297 struct NeighbourList *n = value;
1299 #if DEBUG_TRANSPORT_DISCONNECT
1300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1301 "Disconnecting due to reconnect being called\n");
1303 if (n->is_connected)
1304 neighbour_disconnect (n);
1311 * Try again to connect to transport service.
1313 * @param cls the handle to the transport service
1314 * @param tc scheduler context
1317 reconnect (void *cls,
1318 const struct GNUNET_SCHEDULER_TaskContext *tc)
1320 struct GNUNET_TRANSPORT_Handle *h = cls;
1321 struct ControlMessage *pos;
1323 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1324 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1326 /* shutdown, just give up */
1329 /* Forget about all neighbours that we used to be connected to */
1330 GNUNET_CONTAINER_multihashmap_iterate(h->neighbours,
1335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1336 "Connecting to transport service.\n");
1338 GNUNET_assert (h->client == NULL);
1339 h->client = GNUNET_CLIENT_connect ("transport", h->cfg);
1340 GNUNET_assert (h->client != NULL);
1341 /* make sure we don't send "START" twice, remove existing entry from
1342 queue (if present) */
1343 pos = h->control_head;
1346 if (pos->notify == &send_start)
1348 GNUNET_CONTAINER_DLL_remove (h->control_head,
1351 if (GNUNET_SCHEDULER_NO_TASK != pos->notify_delay_task)
1353 GNUNET_SCHEDULER_cancel (pos->notify_delay_task);
1354 pos->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1361 schedule_control_transmit (h,
1362 sizeof (struct StartMessage),
1364 GNUNET_TIME_UNIT_FOREVER_REL, &send_start, h);
1365 GNUNET_CLIENT_receive (h->client,
1366 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1371 * Function that will schedule the job that will try
1372 * to connect us again to the client.
1374 * @param h transport service to reconnect
1377 schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
1380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1381 "Scheduling task to reconnect to transport service in %llu ms.\n",
1382 h->reconnect_delay.rel_value);
1384 GNUNET_assert (h->client == NULL);
1385 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
1387 = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
1388 if (h->reconnect_delay.rel_value == 0)
1390 h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1394 h->reconnect_delay = GNUNET_TIME_relative_multiply (h->reconnect_delay, 2);
1395 h->reconnect_delay = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
1396 h->reconnect_delay);
1402 * Send request connect message to the service.
1404 * @param cls the TransportRequestConnectMessage
1405 * @param size number of bytes available in buf
1406 * @param buf where to copy the message
1407 * @return number of bytes copied to buf
1410 send_transport_request_connect (void *cls, size_t size, void *buf)
1412 struct TransportRequestConnectMessage *trcm = cls;
1417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1418 "Buffer null for %s\n",
1425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1426 "Transmitting `%s' request for `%4s'.\n",
1428 GNUNET_i2s (&trcm->peer));
1430 GNUNET_assert (size >= sizeof (struct TransportRequestConnectMessage));
1431 memcpy(buf, trcm, sizeof(struct TransportRequestConnectMessage));
1433 return sizeof(struct TransportRequestConnectMessage);
1437 * Create and send a request connect message to
1438 * the transport service for a particular peer.
1440 * @param h handle to the transport service
1441 * @param n the neighbor to send the request connect message about
1445 send_request_connect_message(struct GNUNET_TRANSPORT_Handle *h, struct NeighbourList *n)
1447 struct TransportRequestConnectMessage *trcm;
1449 trcm = GNUNET_malloc(sizeof(struct TransportRequestConnectMessage));
1450 trcm->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT);
1451 trcm->header.size = htons(sizeof(struct TransportRequestConnectMessage));
1452 memcpy(&trcm->peer, &n->id, sizeof(struct GNUNET_PeerIdentity));
1453 schedule_control_transmit (h,
1454 sizeof (struct TransportRequestConnectMessage),
1456 GNUNET_TIME_UNIT_FOREVER_REL, &send_transport_request_connect, trcm);
1461 * Add neighbour to our list
1463 * @return NULL if this API is currently disconnecting from the service
1465 static struct NeighbourList *
1466 neighbour_add (struct GNUNET_TRANSPORT_Handle *h,
1467 const struct GNUNET_PeerIdentity *pid)
1469 struct NeighbourList *n;
1471 if (GNUNET_YES == h->in_disconnect)
1473 /* check for duplicates */
1474 if (NULL != (n = neighbour_find (h, pid)))
1480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1481 "Creating entry for neighbour `%4s'.\n",
1484 n = GNUNET_malloc (sizeof (struct NeighbourList));
1487 GNUNET_BANDWIDTH_tracker_init (&n->out_tracker,
1488 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1489 MAX_BANDWIDTH_CARRY_S);
1490 GNUNET_CONTAINER_multihashmap_put (h->neighbours,
1493 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1500 * Iterator over hash map entries, for deleting state of a neighbor.
1502 * @param cls closure (NULL)
1503 * @param key current key code
1504 * @param value value in the hash map, the neighbour entry to delete
1505 * @return GNUNET_YES if we should continue to
1510 delete_neighbours (void *cls,
1511 const GNUNET_HashCode * key,
1514 struct NeighbourList *n = value;
1515 struct GNUNET_TRANSPORT_TransmitHandle *th;
1517 switch (n->transmit_stage)
1520 case TS_TRANSMITTED:
1524 case TS_TRANSMITTED_QUEUED:
1525 th = &n->transmit_handle;
1526 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1528 GNUNET_SCHEDULER_cancel (th->notify_delay_task);
1529 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1531 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
1542 * Connect to the transport service. Note that the connection may
1543 * complete (or fail) asynchronously.
1545 * @param cfg configuration to use
1546 * @param self our own identity (API should check that it matches
1547 * the identity found by transport), or NULL (no check)
1548 * @param cls closure for the callbacks
1549 * @param rec receive function to call
1550 * @param nc function to call on connect events
1551 * @param nd function to call on disconnect events
1553 struct GNUNET_TRANSPORT_Handle *
1554 GNUNET_TRANSPORT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1555 const struct GNUNET_PeerIdentity *self,
1557 GNUNET_TRANSPORT_ReceiveCallback rec,
1558 GNUNET_TRANSPORT_NotifyConnect nc,
1559 GNUNET_TRANSPORT_NotifyDisconnect nd)
1561 struct GNUNET_TRANSPORT_Handle *ret;
1563 ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Handle));
1567 ret->check_self = GNUNET_YES;
1574 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1575 ret->neighbours = GNUNET_CONTAINER_multihashmap_create(STARTING_NEIGHBOURS_SIZE);
1576 schedule_reconnect (ret);
1582 * Disconnect from the transport service.
1585 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1587 struct HelloWaitList *hwl;
1588 struct GNUNET_CLIENT_Connection *client;
1589 struct ControlMessage *cm;
1592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transport disconnect called!\n");
1594 /* FIXME: this flag is dubious, we should be able to do this
1595 more cleanly; also, we should probably do 'disconnect'
1596 callbacks for every connected peer here, i.e. by calling
1597 the iterator with 'forget_neighbours' instead of 'delete_neighbours'.
1600 handle->in_disconnect = GNUNET_YES;
1602 GNUNET_assert (GNUNET_SYSERR !=
1603 GNUNET_CONTAINER_multihashmap_iterate(handle->neighbours,
1606 GNUNET_CONTAINER_multihashmap_destroy (handle->neighbours);
1608 while (NULL != (hwl = handle->hwl_head))
1610 handle->hwl_head = hwl->next;
1611 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1613 ("Disconnect while notification for `%s' still registered.\n"),
1615 if (hwl->rec != NULL)
1616 hwl->rec (hwl->rec_cls, NULL);
1620 /* Check for still scheduled control messages, cancel delay tasks if so */
1621 /* Added because somehow a notify_delay_task is remaining scheduled and is ever so annoying */
1622 while ( (NULL != (cm = handle->control_head)))
1625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1626 "Disconnect before control message sent!\n");
1628 if (cm->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1630 GNUNET_SCHEDULER_cancel (cm->notify_delay_task);
1631 cm->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1633 GNUNET_CONTAINER_DLL_remove (handle->control_head,
1634 handle->control_tail,
1640 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1642 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1643 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1645 if (handle->quota_task != GNUNET_SCHEDULER_NO_TASK)
1647 GNUNET_SCHEDULER_cancel (handle->quota_task);
1648 handle->quota_task = GNUNET_SCHEDULER_NO_TASK;
1650 GNUNET_free_non_null (handle->my_hello);
1651 handle->my_hello = NULL;
1653 if (NULL != handle->network_handle)
1655 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->network_handle);
1656 handle->network_handle = NULL;
1658 if (NULL != (client = handle->client))
1661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1662 "Disconnecting from transport service for good.\n");
1664 handle->client = NULL;
1665 GNUNET_CLIENT_disconnect (client, GNUNET_YES);
1667 GNUNET_free (handle);
1672 * Function we use for handling incoming messages.
1674 * @param cls closure (struct GNUNET_TRANSPORT_Handle *)
1675 * @param msg message received, NULL on timeout or fatal error
1678 demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
1680 struct GNUNET_TRANSPORT_Handle *h = cls;
1681 const struct DisconnectInfoMessage *dim;
1682 const struct ConnectInfoMessage *cim;
1683 const struct InboundMessage *im;
1684 const struct GNUNET_MessageHeader *imm;
1685 const struct SendOkMessage *okm;
1686 struct HelloWaitList *hwl;
1687 struct HelloWaitList *next_hwl;
1688 struct NeighbourList *n;
1689 struct GNUNET_PeerIdentity me;
1693 if (h->client == NULL)
1695 /* shutdown initiated from 'GNUNET_TRANSPORT_disconnect',
1696 finish clean up work! */
1703 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1704 "Error receiving from transport service, disconnecting temporarily.\n");
1706 if (h->network_handle != NULL)
1708 GNUNET_CLIENT_notify_transmit_ready_cancel (h->network_handle);
1709 h->network_handle = NULL;
1711 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
1713 schedule_reconnect (h);
1716 GNUNET_CLIENT_receive (h->client,
1717 &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1718 size = ntohs (msg->size);
1719 switch (ntohs (msg->type))
1721 case GNUNET_MESSAGE_TYPE_HELLO:
1723 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg,
1730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1731 "Receiving (my own) `%s' message, I am `%4s'.\n",
1732 "HELLO", GNUNET_i2s (&me));
1734 GNUNET_free_non_null (h->my_hello);
1736 if (size < sizeof (struct GNUNET_MessageHeader))
1741 h->my_hello = GNUNET_malloc (size);
1742 memcpy (h->my_hello, msg, size);
1746 next_hwl = hwl->next;
1747 hwl->rec (hwl->rec_cls,
1748 (const struct GNUNET_MessageHeader *) h->my_hello);
1752 case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
1753 if (size < sizeof (struct ConnectInfoMessage))
1758 cim = (const struct ConnectInfoMessage *) msg;
1759 ats_count = ntohl (cim->ats_count);
1760 if (size != sizeof (struct ConnectInfoMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information))
1765 if (0 == memcmp (&cim->id,
1767 sizeof (struct GNUNET_PeerIdentity)))
1769 /* connect to self!? */
1774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1775 "Receiving `%s' message for `%4s'.\n",
1776 "CONNECT", GNUNET_i2s (&cim->id));
1778 n = neighbour_find (h, &cim->id);
1780 n = neighbour_add (h, &cim->id);
1783 GNUNET_break (n->is_connected == GNUNET_NO);
1784 n->is_connected = GNUNET_YES;
1786 if (h->nc_cb != NULL)
1787 h->nc_cb (h->cls, &n->id,
1788 &cim->ats,ats_count);
1791 case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
1792 if (size != sizeof (struct DisconnectInfoMessage))
1797 dim = (const struct DisconnectInfoMessage *) msg;
1798 GNUNET_break (ntohl (dim->reserved) == 0);
1799 if (0 == memcmp (&dim->peer,
1801 sizeof (struct GNUNET_PeerIdentity)))
1803 /* discconnect from self!? */
1807 #if DEBUG_TRANSPORT_DISCONNECT
1808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1809 "Receiving `%s' message for `%4s'.\n",
1811 GNUNET_i2s (&dim->peer));
1813 n = neighbour_find (h, &dim->peer);
1814 GNUNET_break (n != NULL);
1816 neighbour_disconnect (n);
1818 case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
1819 if (size != sizeof (struct SendOkMessage))
1824 okm = (const struct SendOkMessage *) msg;
1826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1827 "Receiving `%s' message, transmission %s.\n", "SEND_OK",
1828 ntohl (okm->success) == GNUNET_OK ? "succeeded" : "failed");
1830 n = neighbour_find (h, &okm->peer);
1831 GNUNET_assert (n != NULL);
1832 switch (n->transmit_stage)
1840 case TS_TRANSMITTED:
1841 n->transmit_stage = TS_NEW;
1843 case TS_TRANSMITTED_QUEUED:
1844 n->transmit_stage = TS_QUEUED;
1845 schedule_transmission (h);
1851 case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
1853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1854 "Receiving `%s' message.\n", "RECV");
1857 sizeof (struct InboundMessage) +
1858 sizeof (struct GNUNET_MessageHeader))
1863 im = (const struct InboundMessage *) msg;
1864 GNUNET_break (0 == ntohl (im->reserved));
1865 ats_count = ntohl(im->ats_count);
1866 //imm = (const struct GNUNET_MessageHeader *) &im[1];
1867 imm = (const struct GNUNET_MessageHeader *) &((&(im->ats))[ats_count+1]);
1869 if (ntohs (imm->size) + sizeof (struct InboundMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) != size)
1875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1876 "Received message of type %u from `%4s'.\n",
1877 ntohs (imm->type), GNUNET_i2s (&im->peer));
1879 n = neighbour_find (h, &im->peer);
1885 if (n->is_connected != GNUNET_YES)
1892 h->rec (h->cls, &im->peer, imm,
1893 &im->ats, ats_count);
1897 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1899 ("Received unexpected message of type %u in %s:%u\n"),
1900 ntohs (msg->type), __FILE__, __LINE__);
1908 * Called when our transmit request timed out before any transport
1909 * reported success connecting to the desired peer or before the
1910 * transport was ready to receive. Signal error and free
1913 * @param cls the 'struct GNUNET_TRANSPORT_TransmitHandle*' that is timing out
1914 * @param tc scheduler context
1917 peer_transmit_timeout (void *cls,
1918 const struct GNUNET_SCHEDULER_TaskContext *tc)
1920 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
1921 struct NeighbourList *n;
1922 GNUNET_CONNECTION_TransmitReadyNotify notify;
1925 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1928 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1929 "Triggering timeout for request to transmit to `%4s' (%d)\n",
1930 GNUNET_i2s (&n->id),
1933 notify = th->notify;
1935 notify_cls = th->notify_cls;
1936 switch (n->transmit_stage)
1942 n->transmit_stage = TS_NEW;
1943 if (n->is_connected == GNUNET_NO)
1946 case TS_TRANSMITTED:
1949 case TS_TRANSMITTED_QUEUED:
1950 n->transmit_stage = TS_TRANSMITTED;
1956 notify (notify_cls, 0, NULL);
1961 * Check if we could queue a message of the given size for
1962 * transmission. The transport service will take both its
1963 * internal buffers and bandwidth limits imposed by the
1964 * other peer into consideration when answering this query.
1966 * @param handle connection to transport service
1967 * @param target who should receive the message
1968 * @param size how big is the message we want to transmit?
1969 * @param priority how important is the message?
1970 * @param timeout after how long should we give up (and call
1971 * notify with buf NULL and size 0)?
1972 * @param notify function to call when we are ready to
1973 * send such a message
1974 * @param notify_cls closure for notify
1975 * @return NULL if someone else is already waiting to be notified
1976 * non-NULL if the notify callback was queued (can be used to cancel
1977 * using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1979 struct GNUNET_TRANSPORT_TransmitHandle *
1980 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle
1982 const struct GNUNET_PeerIdentity
1983 *target, size_t size,
1985 struct GNUNET_TIME_Relative timeout,
1986 GNUNET_CONNECTION_TransmitReadyNotify
1987 notify, void *notify_cls)
1989 struct GNUNET_TRANSPORT_TransmitHandle *th;
1990 struct NeighbourList *n;
1992 if (size + sizeof (struct OutboundMessage) >=
1993 GNUNET_SERVER_MAX_MESSAGE_SIZE)
1996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1997 "Message size is %d, max allowed is %d.\n",
1998 size + sizeof (struct OutboundMessage), GNUNET_SERVER_MAX_MESSAGE_SIZE - 1);
2004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2005 "Asking transport service for transmission of %u bytes to peer `%4s' within %llu ms.\n",
2006 size, GNUNET_i2s (target),
2007 (unsigned long long) timeout.rel_value);
2009 n = neighbour_find (handle, target);
2012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2013 "Created neighbour entry for peer `%s'\n",
2014 GNUNET_i2s (target));
2015 n = neighbour_add (handle, target);
2019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2020 "Could not create neighbour entry for peer `%s'\n",
2021 GNUNET_i2s (target));
2026 * Send a request connect message if not connected,
2027 * otherwise we will never send anything to
2030 if (n->is_connected == GNUNET_NO)
2032 send_request_connect_message(handle, n);
2035 switch (n->transmit_stage)
2038 n->transmit_stage = TS_QUEUED;
2043 case TS_TRANSMITTED:
2044 n->transmit_stage = TS_TRANSMITTED_QUEUED;
2046 case TS_TRANSMITTED_QUEUED:
2053 th = &n->transmit_handle;
2055 th->notify = notify;
2056 th->notify_cls = notify_cls;
2057 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
2058 th->notify_size = size + sizeof (struct OutboundMessage);
2059 th->priority = priority;
2060 th->notify_delay_task
2061 = GNUNET_SCHEDULER_add_delayed (timeout,
2062 &peer_transmit_timeout, th);
2063 schedule_transmission (handle);
2069 * Cancel the specified transmission-ready notification.
2072 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
2073 GNUNET_TRANSPORT_TransmitHandle
2076 struct NeighbourList *n;
2081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2082 "Transmission request of %u bytes to `%4s' was canceled.\n",
2083 th->notify_size - sizeof (struct OutboundMessage),
2084 GNUNET_i2s (&n->id));
2086 if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
2088 GNUNET_SCHEDULER_cancel (th->notify_delay_task);
2089 th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
2091 switch (n->transmit_stage)
2097 n->transmit_stage = TS_NEW;
2098 if ( (n->in_disconnect == GNUNET_NO) &&
2099 (n->is_connected == GNUNET_NO) )
2102 case TS_TRANSMITTED:
2105 case TS_TRANSMITTED_QUEUED:
2106 n->transmit_stage = TS_TRANSMITTED;
2114 /* end of transport_api.c */