2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 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 * - adjust testcases to use new 'try connect' style (should be easy, breaks API compatibility!)
28 * - adjust core service to use new 'try connect' style (should be MUCH nicer there as well!)
32 #include "gnunet_constants.h"
33 #include "gnunet_bandwidth_lib.h"
34 #include "gnunet_client_lib.h"
35 #include "gnunet_constants.h"
36 #include "gnunet_container_lib.h"
37 #include "gnunet_arm_service.h"
38 #include "gnunet_hello_lib.h"
39 #include "gnunet_protocols.h"
40 #include "gnunet_server_lib.h"
41 #include "gnunet_time_lib.h"
42 #include "gnunet_transport_service.h"
43 #include "transport.h"
45 #define LOG(kind,...) GNUNET_log_from (kind, "transport-api",__VA_ARGS__)
48 * How large to start with for the hashmap of neighbours.
50 #define STARTING_NEIGHBOURS_SIZE 16
53 * Handle for a message that should be transmitted to the service.
54 * Used for both control messages and normal messages.
56 struct GNUNET_TRANSPORT_TransmitHandle
60 * We keep all requests in a DLL.
62 struct GNUNET_TRANSPORT_TransmitHandle *next;
65 * We keep all requests in a DLL.
67 struct GNUNET_TRANSPORT_TransmitHandle *prev;
70 * Neighbour for this handle, NULL for control messages.
72 struct Neighbour *neighbour;
75 * Function to call when notify_size bytes are available
78 GNUNET_CONNECTION_TransmitReadyNotify notify;
86 * Timeout for this request, 0 for control messages.
88 struct GNUNET_TIME_Absolute timeout;
91 * Task to trigger request timeout if the request is stalled due to
94 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
97 * How many bytes is our notify callback waiting for?
102 * How important is this message? Not used for control messages.
110 * Entry in hash table of all of our current neighbours.
115 * Overall transport handle.
117 struct GNUNET_TRANSPORT_Handle *h;
120 * Active transmit handle or NULL.
122 struct GNUNET_TRANSPORT_TransmitHandle *th;
125 * Identity of this neighbour.
127 struct GNUNET_PeerIdentity id;
130 * Outbound bandwidh tracker.
132 struct GNUNET_BANDWIDTH_Tracker out_tracker;
135 * Entry in our readyness heap (which is sorted by 'next_ready'
136 * value). NULL if there is no pending transmission request for
137 * this neighbour or if we're waiting for 'is_ready' to become
138 * true AFTER the 'out_tracker' suggested that this peer's quota
139 * has been satisfied (so once 'is_ready' goes to GNUNET_YES,
140 * we should immediately go back into the heap).
142 struct GNUNET_CONTAINER_HeapNode *hn;
145 * Is this peer currently ready to receive a message?
150 * Sending consumed more bytes on wire than payload was announced
151 * This overhead is added to the delay of next sending operation
153 size_t traffic_overhead;
158 * Linked list of functions to call whenever our HELLO is updated.
160 struct GNUNET_TRANSPORT_GetHelloHandle
164 * This is a doubly linked list.
166 struct GNUNET_TRANSPORT_GetHelloHandle *next;
169 * This is a doubly linked list.
171 struct GNUNET_TRANSPORT_GetHelloHandle *prev;
176 struct GNUNET_TRANSPORT_Handle *handle;
179 * Callback to call once we got our HELLO.
181 GNUNET_TRANSPORT_HelloUpdateCallback rec;
192 * Handle for the transport service (includes all of the
193 * state for the transport service).
195 struct GNUNET_TRANSPORT_Handle
199 * Closure for the callbacks.
204 * Function to call for received data.
206 GNUNET_TRANSPORT_ReceiveCallback rec;
209 * function to call on connect events
211 GNUNET_TRANSPORT_NotifyConnect nc_cb;
214 * function to call on disconnect events
216 GNUNET_TRANSPORT_NotifyDisconnect nd_cb;
219 * Head of DLL of control messages.
221 struct GNUNET_TRANSPORT_TransmitHandle *control_head;
224 * Tail of DLL of control messages.
226 struct GNUNET_TRANSPORT_TransmitHandle *control_tail;
229 * The current HELLO message for this peer. Updated
230 * whenever transports change their addresses.
232 struct GNUNET_HELLO_Message *my_hello;
235 * My client connection to the transport service.
237 struct GNUNET_CLIENT_Connection *client;
240 * Handle to our registration with the client for notification.
242 struct GNUNET_CLIENT_TransmitHandle *cth;
245 * Linked list of pending requests for our HELLO.
247 struct GNUNET_TRANSPORT_GetHelloHandle *hwl_head;
250 * Linked list of pending requests for our HELLO.
252 struct GNUNET_TRANSPORT_GetHelloHandle *hwl_tail;
257 const struct GNUNET_CONFIGURATION_Handle *cfg;
260 * Hash map of the current connected neighbours of this peer.
261 * Maps peer identities to 'struct Neighbour' entries.
263 struct GNUNET_CONTAINER_MultiHashMap *neighbours;
266 * Heap sorting peers with pending messages by the timestamps that
267 * specify when we could next send a message to the respective peer.
268 * Excludes control messages (which can always go out immediately).
269 * Maps time stamps to 'struct Neighbour' entries.
271 struct GNUNET_CONTAINER_Heap *ready_heap;
274 * Peer identity as assumed by this process, or all zeros.
276 struct GNUNET_PeerIdentity self;
279 * ID of the task trying to reconnect to the service.
281 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
284 * ID of the task trying to trigger transmission for a peer while
285 * maintaining bandwidth quotas. In use if there are no control
286 * messages and the smallest entry in the 'ready_heap' has a time
287 * stamp in the future.
289 GNUNET_SCHEDULER_TaskIdentifier quota_task;
292 * Delay until we try to reconnect.
294 struct GNUNET_TIME_Relative reconnect_delay;
297 * Should we check that 'self' matches what the service thinks?
298 * (if GNUNET_NO, then 'self' is all zeros!).
305 * Schedule the task to send one message, either from the control
306 * list or the peer message queues to the service.
308 * @param h transport service to schedule a transmission for
311 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h);
315 * Function that will schedule the job that will try
316 * to connect us again to the client.
318 * @param h transport service to reconnect
321 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h);
325 * Get the neighbour list entry for the given peer
327 * @param h our context
328 * @param peer peer to look up
329 * @return NULL if no such peer entry exists
331 static struct Neighbour *
332 neighbour_find (struct GNUNET_TRANSPORT_Handle *h,
333 const struct GNUNET_PeerIdentity *peer)
335 return GNUNET_CONTAINER_multihashmap_get (h->neighbours, &peer->hashPubKey);
340 * Add neighbour to our list
342 * @return NULL if this API is currently disconnecting from the service
344 static struct Neighbour *
345 neighbour_add (struct GNUNET_TRANSPORT_Handle *h,
346 const struct GNUNET_PeerIdentity *pid)
350 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating entry for neighbour `%4s'.\n",
352 n = GNUNET_malloc (sizeof (struct Neighbour));
355 n->is_ready = GNUNET_YES;
356 n->traffic_overhead = 0;
357 GNUNET_BANDWIDTH_tracker_init (&n->out_tracker,
358 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
359 MAX_BANDWIDTH_CARRY_S);
360 GNUNET_assert (GNUNET_OK ==
361 GNUNET_CONTAINER_multihashmap_put (h->neighbours,
362 &n->id.hashPubKey, n,
363 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
369 * Iterator over hash map entries, for deleting state of a neighbour.
371 * @param cls the 'struct GNUNET_TRANSPORT_Handle*'
372 * @param key peer identity
373 * @param value value in the hash map, the neighbour entry to delete
374 * @return GNUNET_YES if we should continue to
379 neighbour_delete (void *cls, const struct GNUNET_HashCode * key, void *value)
381 struct GNUNET_TRANSPORT_Handle *handle = cls;
382 struct Neighbour *n = value;
384 if (NULL != handle->nd_cb)
385 handle->nd_cb (handle->cls, &n->id);
386 GNUNET_assert (NULL == n->th);
387 GNUNET_assert (NULL == n->hn);
388 GNUNET_assert (GNUNET_YES ==
389 GNUNET_CONTAINER_multihashmap_remove (handle->neighbours, key,
397 * Function we use for handling incoming messages.
399 * @param cls closure (struct GNUNET_TRANSPORT_Handle *)
400 * @param msg message received, NULL on timeout or fatal error
403 demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
405 struct GNUNET_TRANSPORT_Handle *h = cls;
406 const struct DisconnectInfoMessage *dim;
407 const struct ConnectInfoMessage *cim;
408 const struct InboundMessage *im;
409 const struct GNUNET_MessageHeader *imm;
410 const struct SendOkMessage *okm;
411 const struct QuotaSetMessage *qm;
412 const struct GNUNET_ATS_Information *ats;
413 struct GNUNET_TRANSPORT_GetHelloHandle *hwl;
414 struct GNUNET_TRANSPORT_GetHelloHandle *next_hwl;
416 struct GNUNET_PeerIdentity me;
420 uint32_t bytes_physical;
422 GNUNET_assert (h->client != NULL);
425 LOG (GNUNET_ERROR_TYPE_DEBUG,
426 "Error receiving from transport service, disconnecting temporarily.\n");
427 disconnect_and_schedule_reconnect (h);
430 GNUNET_CLIENT_receive (h->client, &demultiplexer, h,
431 GNUNET_TIME_UNIT_FOREVER_REL);
432 size = ntohs (msg->size);
433 switch (ntohs (msg->type))
435 case GNUNET_MESSAGE_TYPE_HELLO:
437 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg, &me))
442 LOG (GNUNET_ERROR_TYPE_DEBUG,
443 "Receiving (my own) `%s' message, I am `%4s'.\n", "HELLO",
445 GNUNET_free_non_null (h->my_hello);
447 if (size < sizeof (struct GNUNET_MessageHeader))
452 h->my_hello = GNUNET_malloc (size);
453 memcpy (h->my_hello, msg, size);
457 next_hwl = hwl->next;
458 hwl->rec (hwl->rec_cls,
459 (const struct GNUNET_MessageHeader *) h->my_hello);
463 case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
464 if (size < sizeof (struct ConnectInfoMessage))
469 cim = (const struct ConnectInfoMessage *) msg;
470 ats_count = ntohl (cim->ats_count);
472 sizeof (struct ConnectInfoMessage) +
473 ats_count * sizeof (struct GNUNET_ATS_Information))
478 ats = (const struct GNUNET_ATS_Information *) &cim[1];
479 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message for `%4s'.\n",
480 "CONNECT", GNUNET_i2s (&cim->id));
481 n = neighbour_find (h, &cim->id);
487 n = neighbour_add (h, &cim->id);
488 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message for `%4s' with quota %u\n",
489 "CONNECT", GNUNET_i2s (&cim->id), ntohl (cim->quota_out.value__));
490 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker, cim->quota_out);
491 if (h->nc_cb != NULL)
492 h->nc_cb (h->cls, &n->id, ats, ats_count);
494 case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
495 if (size != sizeof (struct DisconnectInfoMessage))
500 dim = (const struct DisconnectInfoMessage *) msg;
501 GNUNET_break (ntohl (dim->reserved) == 0);
502 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message for `%4s'.\n",
503 "DISCONNECT", GNUNET_i2s (&dim->peer));
504 n = neighbour_find (h, &dim->peer);
510 neighbour_delete (h, &dim->peer.hashPubKey, n);
512 case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
513 if (size != sizeof (struct SendOkMessage))
518 okm = (const struct SendOkMessage *) msg;
519 bytes_msg = ntohl (okm->bytes_msg);
520 bytes_physical = ntohl (okm->bytes_physical);
521 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message, transmission %s.\n",
522 "SEND_OK", ntohl (okm->success) == GNUNET_OK ? "succeeded" : "failed");
524 n = neighbour_find (h, &okm->peer);
528 if (bytes_physical >= bytes_msg)
530 LOG (GNUNET_ERROR_TYPE_DEBUG, "Overhead for %u byte message: %u \n",
531 bytes_msg, bytes_physical - bytes_msg);
532 n->traffic_overhead += bytes_physical - bytes_msg;
534 GNUNET_break (GNUNET_NO == n->is_ready);
535 n->is_ready = GNUNET_YES;
536 if ((n->th != NULL) && (n->hn == NULL))
538 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->th->timeout_task);
539 GNUNET_SCHEDULER_cancel (n->th->timeout_task);
540 n->th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
541 /* we've been waiting for this (congestion, not quota,
542 * caused delayed transmission) */
543 n->hn = GNUNET_CONTAINER_heap_insert (h->ready_heap, n, 0);
544 schedule_transmission (h);
547 case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
548 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message.\n", "RECV");
550 sizeof (struct InboundMessage) + sizeof (struct GNUNET_MessageHeader))
555 im = (const struct InboundMessage *) msg;
556 ats_count = ntohl (im->ats_count);
557 ats = (const struct GNUNET_ATS_Information *) &im[1];
558 imm = (const struct GNUNET_MessageHeader *) &ats[ats_count];
559 if (ntohs (imm->size) + sizeof (struct InboundMessage) +
560 ats_count * sizeof (struct GNUNET_ATS_Information) != size)
565 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %u from `%4s'.\n",
566 ntohs (imm->type), GNUNET_i2s (&im->peer));
567 n = neighbour_find (h, &im->peer);
574 h->rec (h->cls, &im->peer, imm, ats, ats_count);
576 case GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA:
577 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message.\n", "SET_QUOTA");
578 if (size != sizeof (struct QuotaSetMessage))
583 qm = (const struct QuotaSetMessage *) msg;
584 n = neighbour_find (h, &qm->peer);
587 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message for `%4s' with quota %u\n",
588 "SET_QUOTA", GNUNET_i2s (&qm->peer), ntohl (qm->quota.value__));
589 GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker, qm->quota);
592 LOG (GNUNET_ERROR_TYPE_ERROR,
593 _("Received unexpected message of type %u in %s:%u\n"),
594 ntohs (msg->type), __FILE__, __LINE__);
602 * A transmission request could not be satisfied because of
603 * network congestion. Notify the initiator and clean up.
605 * @param cls the 'struct GNUNET_TRANSPORT_TransmitHandle'
606 * @param tc scheduler context
609 timeout_request_due_to_congestion (void *cls,
610 const struct GNUNET_SCHEDULER_TaskContext
613 struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
614 struct Neighbour *n = th->neighbour;
616 n->th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
617 GNUNET_assert (th == n->th);
618 GNUNET_assert (NULL == n->hn);
620 th->notify (th->notify_cls, 0, NULL);
626 * Transmit message(s) to service.
628 * @param cls handle to transport
629 * @param size number of bytes available in buf
630 * @param buf where to copy the message
631 * @return number of bytes copied to buf
634 transport_notify_ready (void *cls, size_t size, void *buf)
636 struct GNUNET_TRANSPORT_Handle *h = cls;
637 struct GNUNET_TRANSPORT_TransmitHandle *th;
640 struct OutboundMessage obm;
645 GNUNET_assert (NULL != h->client);
649 /* transmission failed */
650 disconnect_and_schedule_reconnect (h);
656 /* first send control messages */
657 while ((NULL != (th = h->control_head)) && (th->notify_size <= size))
659 GNUNET_CONTAINER_DLL_remove (h->control_head, h->control_tail, th);
660 nret = th->notify (th->notify_cls, size, &cbuf[ret]);
661 LOG (GNUNET_ERROR_TYPE_DEBUG, "Added %u bytes of control message at %u\n",
668 /* then, if possible and no control messages pending, send data messages */
669 while ((NULL == h->control_head) &&
670 (NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap))))
672 if (GNUNET_YES != n->is_ready)
674 /* peer not ready, wait for notification! */
675 GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
677 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == n->th->timeout_task);
678 n->th->timeout_task =
679 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
681 &timeout_request_due_to_congestion,
686 if (th->notify_size + sizeof (struct OutboundMessage) > size)
687 break; /* does not fit */
688 if (GNUNET_BANDWIDTH_tracker_get_delay
689 (&n->out_tracker, th->notify_size).rel_value > 0)
690 break; /* too early */
691 GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
694 n->is_ready = GNUNET_NO;
695 GNUNET_assert (size >= sizeof (struct OutboundMessage));
697 th->notify (th->notify_cls, size - sizeof (struct OutboundMessage),
698 &cbuf[ret + sizeof (struct OutboundMessage)]);
699 GNUNET_assert (mret <= size - sizeof (struct OutboundMessage));
702 GNUNET_assert (mret + sizeof (struct OutboundMessage) <
703 GNUNET_SERVER_MAX_MESSAGE_SIZE);
704 obm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
705 obm.header.size = htons (mret + sizeof (struct OutboundMessage));
706 obm.priority = htonl (th->priority);
708 GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining
711 memcpy (&cbuf[ret], &obm, sizeof (struct OutboundMessage));
712 ret += (mret + sizeof (struct OutboundMessage));
713 size -= (mret + sizeof (struct OutboundMessage));
714 GNUNET_BANDWIDTH_tracker_consume (&n->out_tracker, mret);
718 /* if there are more pending messages, try to schedule those */
719 schedule_transmission (h);
720 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting %u bytes to transport service\n",
727 * Schedule the task to send one message, either from the control
728 * list or the peer message queues to the service.
730 * @param cls transport service to schedule a transmission for
731 * @param tc scheduler context
734 schedule_transmission_task (void *cls,
735 const struct GNUNET_SCHEDULER_TaskContext *tc)
737 struct GNUNET_TRANSPORT_Handle *h = cls;
739 struct GNUNET_TRANSPORT_TransmitHandle *th;
742 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
743 GNUNET_assert (NULL != h->client);
744 /* destroy all requests that have timed out */
745 while ((NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap))) &&
746 (GNUNET_TIME_absolute_get_remaining (n->th->timeout).rel_value == 0))
748 /* notify client that the request could not be satisfied within
749 * the given time constraints */
752 GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
754 LOG (GNUNET_ERROR_TYPE_DEBUG,
755 "Signalling timeout for transmission to peer %s due to congestion\n",
756 GNUNET_i2s (&n->id));
757 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
762 if (NULL != h->control_head)
764 size = h->control_head->notify_size;
768 n = GNUNET_CONTAINER_heap_peek (h->ready_heap);
770 return; /* no pending messages */
771 size = n->th->notify_size + sizeof (struct OutboundMessage);
773 LOG (GNUNET_ERROR_TYPE_DEBUG, "Calling notify_transmit_ready\n");
775 GNUNET_CLIENT_notify_transmit_ready (h->client, size,
776 GNUNET_TIME_UNIT_FOREVER_REL,
777 GNUNET_NO, &transport_notify_ready,
779 GNUNET_assert (NULL != h->cth);
784 * Schedule the task to send one message, either from the control
785 * list or the peer message queues to the service.
787 * @param h transport service to schedule a transmission for
790 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
792 struct GNUNET_TIME_Relative delay;
795 GNUNET_assert (NULL != h->client);
796 if (h->quota_task != GNUNET_SCHEDULER_NO_TASK)
798 GNUNET_SCHEDULER_cancel (h->quota_task);
799 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
801 if (NULL != h->control_head)
802 delay = GNUNET_TIME_UNIT_ZERO;
803 else if (NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap)))
806 GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
807 n->th->notify_size + n->traffic_overhead);
808 n->traffic_overhead = 0;
811 return; /* no work to be done */
812 LOG (GNUNET_ERROR_TYPE_DEBUG,
813 "Scheduling next transmission to service in %llu ms\n",
814 (unsigned long long) delay.rel_value);
816 GNUNET_SCHEDULER_add_delayed (delay, &schedule_transmission_task, h);
821 * Queue control request for transmission to the transport
824 * @param h handle to the transport service
825 * @param size number of bytes to be transmitted
826 * @param notify function to call to get the content
827 * @param notify_cls closure for notify
830 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h, size_t size,
831 GNUNET_CONNECTION_TransmitReadyNotify notify,
834 struct GNUNET_TRANSPORT_TransmitHandle *th;
836 LOG (GNUNET_ERROR_TYPE_DEBUG, "Control transmit of %u bytes requested\n",
838 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
840 th->notify_cls = notify_cls;
841 th->notify_size = size;
842 GNUNET_CONTAINER_DLL_insert_tail (h->control_head, h->control_tail, th);
843 schedule_transmission (h);
848 * Transmit START message to service.
851 * @param size number of bytes available in buf
852 * @param buf where to copy the message
853 * @return number of bytes copied to buf
856 send_start (void *cls, size_t size, void *buf)
858 struct GNUNET_TRANSPORT_Handle *h = cls;
859 struct StartMessage s;
864 /* Can only be shutdown, just give up */
865 LOG (GNUNET_ERROR_TYPE_DEBUG,
866 "Shutdown while trying to transmit `%s' request.\n", "START");
869 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "START");
870 GNUNET_assert (size >= sizeof (struct StartMessage));
871 s.header.size = htons (sizeof (struct StartMessage));
872 s.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
878 s.options = htonl (options);
880 memcpy (buf, &s, sizeof (struct StartMessage));
881 GNUNET_CLIENT_receive (h->client, &demultiplexer, h,
882 GNUNET_TIME_UNIT_FOREVER_REL);
883 return sizeof (struct StartMessage);
888 * Try again to connect to transport service.
890 * @param cls the handle to the transport service
891 * @param tc scheduler context
894 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
896 struct GNUNET_TRANSPORT_Handle *h = cls;
898 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
899 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
901 /* shutdown, just give up */
904 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n");
905 GNUNET_assert (h->client == NULL);
906 GNUNET_assert (h->control_head == NULL);
907 GNUNET_assert (h->control_tail == NULL);
908 h->client = GNUNET_CLIENT_connect ("transport", h->cfg);
909 GNUNET_assert (h->client != NULL);
910 schedule_control_transmit (h, sizeof (struct StartMessage), &send_start, h);
915 * Function that will schedule the job that will try
916 * to connect us again to the client.
918 * @param h transport service to reconnect
921 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
923 struct GNUNET_TRANSPORT_TransmitHandle *th;
925 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
928 GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
931 if (NULL != h->client)
933 GNUNET_CLIENT_disconnect (h->client);
936 /* Forget about all neighbours that we used to be connected to */
937 GNUNET_CONTAINER_multihashmap_iterate (h->neighbours, &neighbour_delete, h);
938 if (h->quota_task != GNUNET_SCHEDULER_NO_TASK)
940 GNUNET_SCHEDULER_cancel (h->quota_task);
941 h->quota_task = GNUNET_SCHEDULER_NO_TASK;
943 while ((NULL != (th = h->control_head)))
945 GNUNET_CONTAINER_DLL_remove (h->control_head, h->control_tail, th);
946 th->notify (th->notify_cls, 0, NULL);
949 LOG (GNUNET_ERROR_TYPE_DEBUG,
950 "Scheduling task to reconnect to transport service in %llu ms.\n",
951 h->reconnect_delay.rel_value);
953 GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
954 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
959 * Send REQUEST_CONNECT message to the service.
961 * @param cls the 'struct GNUNET_PeerIdentity'
962 * @param size number of bytes available in buf
963 * @param buf where to copy the message
964 * @return number of bytes copied to buf
967 send_try_connect (void *cls, size_t size, void *buf)
969 struct GNUNET_PeerIdentity *pid = cls;
970 struct TransportRequestConnectMessage msg;
977 LOG (GNUNET_ERROR_TYPE_DEBUG,
978 "Transmitting `%s' request with respect to `%4s'.\n", "REQUEST_CONNECT",
980 GNUNET_assert (size >= sizeof (struct TransportRequestConnectMessage));
981 msg.header.size = htons (sizeof (struct TransportRequestConnectMessage));
982 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT);
983 msg.reserved = htonl (0);
985 memcpy (buf, &msg, sizeof (msg));
987 return sizeof (struct TransportRequestConnectMessage);
992 * Ask the transport service to establish a connection to
995 * @param handle connection to transport service
996 * @param target who we should try to connect to
999 GNUNET_TRANSPORT_try_connect (struct GNUNET_TRANSPORT_Handle *handle,
1000 const struct GNUNET_PeerIdentity *target)
1002 struct GNUNET_PeerIdentity *pid;
1003 if (NULL == handle->client)
1005 /* FIXME: handle->client can be NULL when transport api is reconnecting */
1010 pid = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
1012 schedule_control_transmit (handle,
1013 sizeof (struct TransportRequestConnectMessage),
1014 &send_try_connect, pid);
1018 struct SendHelloContext
1020 GNUNET_SCHEDULER_Task cont;
1024 struct GNUNET_MessageHeader *msg;
1028 * Send HELLO message to the service.
1030 * @param cls the HELLO message to send
1031 * @param size number of bytes available in buf
1032 * @param buf where to copy the message
1033 * @return number of bytes copied to buf
1036 send_hello (void *cls, size_t size, void *buf)
1038 struct SendHelloContext *shc = cls;
1039 struct GNUNET_MessageHeader *msg = shc->msg;
1041 struct GNUNET_SCHEDULER_TaskContext tc;
1042 tc.read_ready = NULL;
1043 tc.write_ready = NULL;
1044 tc.reason = GNUNET_SCHEDULER_REASON_TIMEOUT;
1048 LOG (GNUNET_ERROR_TYPE_DEBUG,
1049 "Timeout while trying to transmit `%s' request.\n", "HELLO");
1050 if (NULL != shc->cont)
1051 shc->cont (shc->cls, &tc);
1056 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "HELLO");
1057 ssize = ntohs (msg->size);
1058 GNUNET_assert (size >= ssize);
1059 memcpy (buf, msg, ssize);
1061 tc.reason = GNUNET_SCHEDULER_REASON_READ_READY;
1062 if (NULL != shc->cont)
1063 shc->cont (shc->cls, &tc);
1070 * Offer the transport service the HELLO of another peer. Note that
1071 * the transport service may just ignore this message if the HELLO is
1072 * malformed or useless due to our local configuration.
1074 * @param handle connection to transport service
1075 * @param hello the hello message
1076 * @param cont continuation to call when HELLO has been sent,
1077 * tc reason GNUNET_SCHEDULER_REASON_TIMEOUT for fail
1078 * tc reasong GNUNET_SCHEDULER_REASON_READY for success
1079 * @param cls closure for continuation
1083 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
1084 const struct GNUNET_MessageHeader *hello,
1085 GNUNET_SCHEDULER_Task cont, void *cls)
1088 struct GNUNET_PeerIdentity peer;
1089 struct GNUNET_MessageHeader *msg;
1090 struct SendHelloContext * shc;
1091 struct GNUNET_SCHEDULER_TaskContext tc;
1093 tc.read_ready = NULL;
1094 tc.write_ready = NULL;
1095 tc.reason = GNUNET_SCHEDULER_REASON_TIMEOUT;
1097 if (NULL == handle->client)
1103 GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
1104 size = ntohs (hello->size);
1105 GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
1107 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hello, &peer))
1115 msg = GNUNET_malloc (size);
1116 memcpy (msg, hello, size);
1117 LOG (GNUNET_ERROR_TYPE_DEBUG,
1118 "Offering `%s' message of `%4s' to transport for validation.\n", "HELLO",
1119 GNUNET_i2s (&peer));
1120 shc = GNUNET_malloc (sizeof (struct SendHelloContext));
1124 schedule_control_transmit (handle, size, &send_hello, shc);
1129 * Obtain the HELLO message for this peer.
1131 * @param handle connection to transport service
1132 * @param rec function to call with the HELLO, sender will be our peer
1133 * identity; message and sender will be NULL on timeout
1134 * (handshake with transport service pending/failed).
1135 * cost estimate will be 0.
1136 * @param rec_cls closure for rec
1137 * @return handle to cancel the operation
1139 struct GNUNET_TRANSPORT_GetHelloHandle *
1140 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
1141 GNUNET_TRANSPORT_HelloUpdateCallback rec,
1144 struct GNUNET_TRANSPORT_GetHelloHandle *hwl;
1146 hwl = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_GetHelloHandle));
1148 hwl->rec_cls = rec_cls;
1149 hwl->handle = handle;
1150 GNUNET_CONTAINER_DLL_insert (handle->hwl_head, handle->hwl_tail, hwl);
1151 if (handle->my_hello != NULL)
1152 rec (rec_cls, (const struct GNUNET_MessageHeader *) handle->my_hello);
1158 * Stop receiving updates about changes to our HELLO message.
1160 * @param ghh handle to cancel
1163 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_GetHelloHandle *ghh)
1165 struct GNUNET_TRANSPORT_Handle *handle = ghh->handle;
1167 GNUNET_CONTAINER_DLL_remove (handle->hwl_head, handle->hwl_tail, ghh);
1173 * Connect to the transport service. Note that the connection may
1174 * complete (or fail) asynchronously.
1176 * @param cfg configuration to use
1177 * @param self our own identity (API should check that it matches
1178 * the identity found by transport), or NULL (no check)
1179 * @param cls closure for the callbacks
1180 * @param rec receive function to call
1181 * @param nc function to call on connect events
1182 * @param nd function to call on disconnect events
1184 struct GNUNET_TRANSPORT_Handle *
1185 GNUNET_TRANSPORT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1186 const struct GNUNET_PeerIdentity *self, void *cls,
1187 GNUNET_TRANSPORT_ReceiveCallback rec,
1188 GNUNET_TRANSPORT_NotifyConnect nc,
1189 GNUNET_TRANSPORT_NotifyDisconnect nd)
1191 struct GNUNET_TRANSPORT_Handle *ret;
1193 ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Handle));
1197 ret->check_self = GNUNET_YES;
1204 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1206 GNUNET_CONTAINER_multihashmap_create (STARTING_NEIGHBOURS_SIZE, GNUNET_YES);
1208 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1209 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n");
1210 ret->client = GNUNET_CLIENT_connect ("transport", cfg);
1211 if (ret->client == NULL)
1216 schedule_control_transmit (ret, sizeof (struct StartMessage), &send_start, ret);
1222 * Disconnect from the transport service.
1224 * @param handle handle to the service as returned from GNUNET_TRANSPORT_connect
1227 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1229 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transport disconnect called!\n");
1230 /* this disconnects all neighbours... */
1231 if (handle->reconnect_task == GNUNET_SCHEDULER_NO_TASK)
1232 disconnect_and_schedule_reconnect (handle);
1233 /* and now we stop trying to connect again... */
1234 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1236 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1237 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1239 GNUNET_CONTAINER_multihashmap_destroy (handle->neighbours);
1240 handle->neighbours = NULL;
1241 if (handle->quota_task != GNUNET_SCHEDULER_NO_TASK)
1243 GNUNET_SCHEDULER_cancel (handle->quota_task);
1244 handle->quota_task = GNUNET_SCHEDULER_NO_TASK;
1246 GNUNET_free_non_null (handle->my_hello);
1247 handle->my_hello = NULL;
1248 GNUNET_assert (handle->hwl_head == NULL);
1249 GNUNET_assert (handle->hwl_tail == NULL);
1250 GNUNET_CONTAINER_heap_destroy (handle->ready_heap);
1251 handle->ready_heap = NULL;
1252 GNUNET_free (handle);
1257 * Check if we could queue a message of the given size for
1258 * transmission. The transport service will take both its
1259 * internal buffers and bandwidth limits imposed by the
1260 * other peer into consideration when answering this query.
1262 * @param handle connection to transport service
1263 * @param target who should receive the message
1264 * @param size how big is the message we want to transmit?
1265 * @param priority how important is the message?
1266 * @param timeout after how long should we give up (and call
1267 * notify with buf NULL and size 0)?
1268 * @param notify function to call when we are ready to
1269 * send such a message
1270 * @param notify_cls closure for notify
1271 * @return NULL if someone else is already waiting to be notified
1272 * non-NULL if the notify callback was queued (can be used to cancel
1273 * using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1275 struct GNUNET_TRANSPORT_TransmitHandle *
1276 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle *handle,
1277 const struct GNUNET_PeerIdentity
1278 *target, size_t size, uint32_t priority,
1279 struct GNUNET_TIME_Relative timeout,
1280 GNUNET_CONNECTION_TransmitReadyNotify
1281 notify, void *notify_cls)
1283 struct Neighbour *n;
1284 struct GNUNET_TRANSPORT_TransmitHandle *th;
1285 struct GNUNET_TIME_Relative delay;
1287 n = neighbour_find (handle, target);
1290 /* use GNUNET_TRANSPORT_try_connect first, only use this function
1291 * once a connection has been established */
1297 /* attempt to send two messages at the same time to the same peer */
1301 GNUNET_assert (NULL == n->hn);
1302 th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
1304 th->notify = notify;
1305 th->notify_cls = notify_cls;
1306 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1307 th->notify_size = size;
1308 th->priority = priority;
1310 /* calculate when our transmission should be ready */
1311 delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker, size + n->traffic_overhead);
1312 n->traffic_overhead = 0;
1313 if (delay.rel_value > timeout.rel_value)
1314 delay.rel_value = 0; /* notify immediately (with failure) */
1315 LOG (GNUNET_ERROR_TYPE_DEBUG,
1316 "Bandwidth tracker allows next transmission to peer %s in %llu ms\n",
1317 GNUNET_i2s (target), (unsigned long long) delay.rel_value);
1318 n->hn = GNUNET_CONTAINER_heap_insert (handle->ready_heap, n, delay.rel_value);
1319 schedule_transmission (handle);
1325 * Cancel the specified transmission-ready notification.
1327 * @param th handle returned from GNUNET_TRANSPORT_notify_transmit_ready
1330 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
1331 GNUNET_TRANSPORT_TransmitHandle
1334 struct Neighbour *n;
1336 GNUNET_assert (NULL == th->next);
1337 GNUNET_assert (NULL == th->prev);
1339 GNUNET_assert (th == n->th);
1343 GNUNET_CONTAINER_heap_remove_node (n->hn);
1348 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != th->timeout_task);
1349 GNUNET_SCHEDULER_cancel (th->timeout_task);
1350 th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1356 /* end of transport_api.c */