2 This file is part of GNUnet.
3 Copyright (C) 2013, 2015 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file cadet/gnunet-service-cadet_peer.c
22 * @brief GNUnet CADET service connection handling
23 * @author Bartlomiej Polot
26 #include "gnunet_util_lib.h"
27 #include "gnunet_signatures.h"
28 #include "gnunet_transport_service.h"
29 #include "gnunet_ats_service.h"
30 #include "gnunet_core_service.h"
31 #include "gnunet_statistics_service.h"
32 #include "cadet_protocol.h"
33 #include "gnunet-service-cadet_peer.h"
34 #include "gnunet-service-cadet_dht.h"
35 #include "gnunet-service-cadet_connection.h"
36 #include "gnunet-service-cadet_tunnel.h"
37 #include "cadet_path.h"
39 #define LOG(level, ...) GNUNET_log_from (level,"cadet-p2p",__VA_ARGS__)
40 #define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-p2p",__VA_ARGS__)
43 /******************************************************************************/
44 /******************************** STRUCTS **********************************/
45 /******************************************************************************/
48 * Struct containing info about a queued transmission to this peer
55 struct CadetPeerQueue *next;
60 struct CadetPeerQueue *prev;
63 * Peer this transmission is directed to.
65 struct CadetPeer *peer;
68 * Connection this message belongs to.
70 struct CadetConnection *c;
78 * Pointer to info stucture used as cls.
90 uint16_t payload_type;
103 * Set when this message starts waiting for CORE.
105 struct GNUNET_TIME_Absolute start_waiting;
108 * Function to call on sending.
113 * Closure for callback.
120 * Struct containing all information regarding a given peer
130 * Last time we heard from this peer
132 struct GNUNET_TIME_Absolute last_contact;
135 * Paths to reach the peer, ordered by ascending hop count
137 struct CadetPeerPath *path_head;
140 * Paths to reach the peer, ordered by ascending hop count
142 struct CadetPeerPath *path_tail;
145 * Handle to stop the DHT search for paths to this peer
147 struct GCD_search_handle *search_h;
150 * Handle to stop the DHT search for paths to this peer
152 struct GNUNET_SCHEDULER_Task *search_delayed;
155 * Tunnel to this peer, if any.
157 struct CadetTunnel *tunnel;
160 * Connections that go through this peer; indexed by tid.
162 struct GNUNET_CONTAINER_MultiHashMap *connections;
165 * Handle for queued transmissions
167 struct GNUNET_CORE_TransmitHandle *core_transmit;
172 struct GNUNET_TIME_Absolute tmt_time;
175 * Transmission queue to core DLL head
177 struct CadetPeerQueue *queue_head;
180 * Transmission queue to core DLL tail
182 struct CadetPeerQueue *queue_tail;
185 * How many messages are in the queue to this peer.
187 unsigned int queue_n;
192 struct GNUNET_HELLO_Message* hello;
195 * Handle to us offering the HELLO to the transport.
197 struct GNUNET_TRANSPORT_OfferHelloHandle *hello_offer;
200 * Handle to our ATS request asking ATS to suggest an address
201 * to TRANSPORT for this peer (to establish a direct link).
203 struct GNUNET_ATS_ConnectivitySuggestHandle *connectivity_suggestion;
208 /******************************************************************************/
209 /******************************* GLOBALS ***********************************/
210 /******************************************************************************/
213 * Global handle to the statistics service.
215 extern struct GNUNET_STATISTICS_Handle *stats;
218 * Local peer own ID (full value).
220 extern struct GNUNET_PeerIdentity my_full_id;
223 * Local peer own ID (short)
225 extern GNUNET_PEER_Id myid;
228 * Peers known, indexed by PeerIdentity, values of type `struct CadetPeer`.
230 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
233 * How many peers do we want to remember?
235 static unsigned long long max_peers;
238 * Percentage of messages that will be dropped (for test purposes only).
240 static unsigned long long drop_percent;
243 * Handle to communicate with CORE.
245 static struct GNUNET_CORE_Handle *core_handle;
250 static const struct GNUNET_CONFIGURATION_Handle *cfg;
253 * Handle to communicate with ATS.
255 static struct GNUNET_ATS_ConnectivityHandle *ats_ch;
260 static int in_shutdown;
263 /******************************************************************************/
264 /***************************** DEBUG *********************************/
265 /******************************************************************************/
268 * Log all kinds of info about the queueing status of a peer.
270 * @param p Peer whose queue to show.
271 * @param level Error level to use for logging.
274 queue_debug (const struct CadetPeer *p, enum GNUNET_ErrorType level)
276 struct GNUNET_TIME_Relative core_wait_time;
277 struct CadetPeerQueue *q;
280 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
282 __FILE__, __FUNCTION__, __LINE__);
286 LOG2 (level, "QQQ Message queue towards %s\n", GCP_2s (p));
287 LOG2 (level, "QQQ queue length: %u\n", p->queue_n);
288 LOG2 (level, "QQQ core tmt rdy: %p\n", p->core_transmit);
289 if (NULL != p->core_transmit)
291 core_wait_time = GNUNET_TIME_absolute_get_duration (p->tmt_time);
292 LOG2 (level, "QQQ core called %s ago\n",
293 GNUNET_STRINGS_relative_time_to_string (core_wait_time, GNUNET_NO));
295 for (q = p->queue_head; NULL != q; q = q->next)
297 LOG2 (level, "QQQ - %s %s on %s\n",
298 GC_m2s (q->type), GC_f2s (q->fwd), GCC_2s (q->c));
299 LOG2 (level, "QQQ payload %s, %u\n",
300 GC_m2s (q->payload_type), q->payload_id);
301 LOG2 (level, "QQQ size: %u bytes\n", q->size);
304 LOG2 (level, "QQQ End queue towards %s\n", GCP_2s (p));
309 * Log all kinds of info about a peer.
314 GCP_debug (const struct CadetPeer *p, enum GNUNET_ErrorType level)
316 struct CadetPeerPath *path;
320 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
322 __FILE__, __FUNCTION__, __LINE__);
328 LOG2 (level, "PPP DEBUG PEER NULL\n");
332 LOG2 (level, "PPP DEBUG PEER %s\n", GCP_2s (p));
333 LOG2 (level, "PPP last contact %s\n",
334 GNUNET_STRINGS_absolute_time_to_string (p->last_contact));
335 for (path = p->path_head; NULL != path; path = path->next)
340 LOG2 (level, "PPP path: %s\n", s);
344 LOG2 (level, "PPP core transmit handle %p\n", p->core_transmit);
345 LOG2 (level, "PPP DHT GET handle %p\n", p->search_h);
347 if (NULL != p->connections)
348 conns += GNUNET_CONTAINER_multihashmap_size (p->connections);
349 LOG2 (level, "PPP # connections over link to peer: %u\n", conns);
350 queue_debug (p, level);
351 LOG2 (level, "PPP DEBUG END\n");
355 /******************************************************************************/
356 /***************************** CORE HELPERS *********************************/
357 /******************************************************************************/
361 * Iterator to notify all connections of a broken link. Mark connections
362 * to destroy after all traffic has been sent.
364 * @param cls Closure (disconnected peer).
365 * @param key Current key code (peer id).
366 * @param value Value in the hash map (connection).
368 * @return #GNUNET_YES to continue to iterate.
371 notify_broken (void *cls,
372 const struct GNUNET_HashCode *key,
375 struct CadetPeer *peer = cls;
376 struct CadetConnection *c = value;
378 LOG (GNUNET_ERROR_TYPE_DEBUG,
379 "Notifying %s due to %s disconnect\n",
380 GCC_2s (c), GCP_2s (peer));
381 GCC_neighbor_disconnected (c, peer);
387 * Remove the direct path to the peer.
389 * @param peer Peer to remove the direct path from.
391 static struct CadetPeerPath *
392 pop_direct_path (struct CadetPeer *peer)
394 struct CadetPeerPath *iter;
396 for (iter = peer->path_head; NULL != iter; iter = iter->next)
398 if (2 >= iter->length)
400 GNUNET_CONTAINER_DLL_remove (peer->path_head,
410 /******************************************************************************/
411 /***************************** CORE CALLBACKS *********************************/
412 /******************************************************************************/
416 * Method called whenever a given peer connects.
419 * @param peer peer identity this notification is about
422 core_connect (void *cls,
423 const struct GNUNET_PeerIdentity *peer)
425 struct CadetPeer *neighbor;
426 struct CadetPeerPath *path;
429 GCC_check_connections ();
430 GNUNET_snprintf (own_id,
433 GNUNET_i2s (&my_full_id));
434 neighbor = GCP_get (peer, GNUNET_YES);
435 if (myid == neighbor->id)
437 LOG (GNUNET_ERROR_TYPE_INFO,
438 "CONNECTED %s (self)\n",
444 LOG (GNUNET_ERROR_TYPE_INFO,
445 "CONNECTED %s <= %s\n",
449 path->peers[1] = neighbor->id;
450 GNUNET_PEER_change_rc (neighbor->id, 1);
452 path->peers[0] = myid;
453 GNUNET_PEER_change_rc (myid, 1);
454 GCP_add_path (neighbor, path, GNUNET_YES);
456 GNUNET_assert (NULL == neighbor->connections);
457 neighbor->connections = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
458 GNUNET_STATISTICS_update (stats,
463 if ( (NULL != GCP_get_tunnel (neighbor)) &&
464 (0 > GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, peer)) )
465 GCP_connect (neighbor);
466 GCC_check_connections ();
471 * Method called whenever a peer disconnects.
474 * @param peer peer identity this notification is about
477 core_disconnect (void *cls,
478 const struct GNUNET_PeerIdentity *peer)
481 struct CadetPeerPath *direct_path;
484 GCC_check_connections ();
485 strncpy (own_id, GNUNET_i2s (&my_full_id), 16);
487 p = GNUNET_CONTAINER_multipeermap_get (peers, peer);
490 GNUNET_break (GNUNET_YES == in_shutdown);
494 LOG (GNUNET_ERROR_TYPE_INFO,
495 "DISCONNECTED %s (self)\n",
498 LOG (GNUNET_ERROR_TYPE_INFO,
499 "DISCONNECTED %s <= %s\n",
500 own_id, GNUNET_i2s (peer));
501 direct_path = pop_direct_path (p);
502 if (NULL != p->connections)
504 GNUNET_CONTAINER_multihashmap_iterate (p->connections,
507 GNUNET_CONTAINER_multihashmap_destroy (p->connections);
508 p->connections = NULL;
510 if (NULL != p->core_transmit)
512 GNUNET_CORE_notify_transmit_ready_cancel (p->core_transmit);
513 p->core_transmit = NULL;
514 p->tmt_time.abs_value_us = 0;
516 GNUNET_STATISTICS_update (stats,
520 path_destroy (direct_path);
521 GCC_check_connections ();
526 * Functions to handle messages from core
528 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
529 {&GCC_handle_create, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0},
530 {&GCC_handle_confirm, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK,
531 sizeof (struct GNUNET_CADET_ConnectionACK)},
532 {&GCC_handle_broken, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
533 sizeof (struct GNUNET_CADET_ConnectionBroken)},
534 {&GCC_handle_destroy, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
535 sizeof (struct GNUNET_CADET_ConnectionDestroy)},
536 {&GCC_handle_ack, GNUNET_MESSAGE_TYPE_CADET_ACK,
537 sizeof (struct GNUNET_CADET_ACK)},
538 {&GCC_handle_poll, GNUNET_MESSAGE_TYPE_CADET_POLL,
539 sizeof (struct GNUNET_CADET_Poll)},
540 {&GCC_handle_kx, GNUNET_MESSAGE_TYPE_CADET_KX, 0},
541 {&GCC_handle_encrypted, GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED, 0},
542 {&GCC_handle_encrypted, GNUNET_MESSAGE_TYPE_CADET_AX, 0},
548 * To be called on core init/fail.
550 * @param cls Closure (config)
551 * @param identity the public identity of this peer
554 core_init (void *cls,
555 const struct GNUNET_PeerIdentity *identity)
557 const struct GNUNET_CONFIGURATION_Handle *c = cls;
560 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
561 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
563 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
564 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (identity));
565 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
566 GNUNET_CORE_disconnect (core_handle);
567 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
568 NULL, /* Closure passed to CADET functions */
569 &core_init, /* Call core_init once connected */
570 &core_connect, /* Handle connects */
571 &core_disconnect, /* remove peers on disconnects */
572 NULL, /* Don't notify about all incoming messages */
573 GNUNET_NO, /* For header only in notification */
574 NULL, /* Don't notify about all outbound messages */
575 GNUNET_NO, /* For header-only out notification */
576 core_handlers); /* Register these handlers */
585 * Core callback to write a pre-constructed data packet to core buffer
587 * @param cls Closure (CadetTransmissionDescriptor with data in "data" member).
588 * @param size Number of bytes available in buf.
589 * @param buf Where the to write the message.
591 * @return number of bytes written to buf
594 send_core_data_raw (void *cls, size_t size, void *buf)
596 struct GNUNET_MessageHeader *msg = cls;
599 GNUNET_assert (NULL != msg);
600 total_size = ntohs (msg->size);
602 if (total_size > size)
607 memcpy (buf, msg, total_size);
614 * Function to send a create connection message to a peer.
616 * @param c Connection to create.
617 * @param size number of bytes available in buf
618 * @param buf where the callee should write the message
619 * @return number of bytes written to buf
622 send_core_connection_create (struct CadetConnection *c, size_t size, void *buf)
624 struct GNUNET_CADET_ConnectionCreate *msg;
625 struct GNUNET_PeerIdentity *peer_ptr;
626 const struct CadetPeerPath *p = GCC_get_path (c);
633 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
635 sizeof (struct GNUNET_CADET_ConnectionCreate) +
636 p->length * sizeof (struct GNUNET_PeerIdentity);
638 if (size < size_needed || NULL == buf)
643 msg = (struct GNUNET_CADET_ConnectionCreate *) buf;
644 msg->header.size = htons (size_needed);
645 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
646 msg->cid = *GCC_get_id (c);
648 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
649 for (i = 0; i < p->length; i++)
651 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
654 LOG (GNUNET_ERROR_TYPE_DEBUG,
655 "CONNECTION CREATE (%u bytes long) sent!\n",
662 * Creates a path ack message in buf and frees all unused resources.
664 * @param c Connection to send an ACK on.
665 * @param size number of bytes available in buf
666 * @param buf where the callee should write the message
668 * @return number of bytes written to buf
671 send_core_connection_ack (struct CadetConnection *c, size_t size, void *buf)
673 struct GNUNET_CADET_ConnectionACK *msg = buf;
675 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
676 if (sizeof (struct GNUNET_CADET_ConnectionACK) > size)
681 msg->header.size = htons (sizeof (struct GNUNET_CADET_ConnectionACK));
682 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK);
683 msg->cid = *GCC_get_id (c);
685 LOG (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
686 return sizeof (struct GNUNET_CADET_ConnectionACK);
690 /******************************************************************************/
691 /******************************** STATIC ***********************************/
692 /******************************************************************************/
696 * Get priority for a queued message.
698 * @param q Queued message
700 * @return CORE priority to use.
702 static enum GNUNET_CORE_Priority
703 get_priority (struct CadetPeerQueue *q)
705 enum GNUNET_CORE_Priority low;
706 enum GNUNET_CORE_Priority high;
711 return GNUNET_CORE_PRIO_BACKGROUND;
714 /* Relayed traffic has lower priority, our own traffic has higher */
715 if (NULL == q->c || GNUNET_NO == GCC_is_origin (q->c, q->fwd))
717 low = GNUNET_CORE_PRIO_BEST_EFFORT;
718 high = GNUNET_CORE_PRIO_URGENT;
722 low = GNUNET_CORE_PRIO_URGENT;
723 high = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
726 /* Bulky payload has lower priority, control traffic has higher. */
727 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == q->type
728 || GNUNET_MESSAGE_TYPE_CADET_AX == q->type)
736 * Destroy the peer_info and free any allocated resources linked to it
738 * @param peer The peer_info to destroy.
739 * @return #GNUNET_OK on success
742 peer_destroy (struct CadetPeer *peer)
744 struct GNUNET_PeerIdentity id;
745 struct CadetPeerPath *p;
746 struct CadetPeerPath *nextp;
748 GNUNET_PEER_resolve (peer->id, &id);
749 GNUNET_PEER_change_rc (peer->id, -1);
751 LOG (GNUNET_ERROR_TYPE_INFO,
752 "destroying peer %s\n",
756 GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
759 LOG (GNUNET_ERROR_TYPE_WARNING, " peer not in peermap!!\n");
761 GCP_stop_search (peer);
766 GNUNET_CONTAINER_DLL_remove (peer->path_head,
772 if (NULL != peer->tunnel)
773 GCT_destroy_empty (peer->tunnel);
774 if (NULL != peer->connections)
776 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (peer->connections));
777 GNUNET_CONTAINER_multihashmap_destroy (peer->connections);
778 peer->connections = NULL;
780 if (NULL != peer->hello_offer)
782 GNUNET_TRANSPORT_offer_hello_cancel (peer->hello_offer);
783 peer->hello_offer = NULL;
785 if (NULL != peer->connectivity_suggestion)
787 GNUNET_ATS_connectivity_suggest_cancel (peer->connectivity_suggestion);
788 peer->connectivity_suggestion = NULL;
790 while (NULL != peer->queue_head)
792 GCP_queue_destroy (peer->queue_head, GNUNET_YES, GNUNET_NO, 0);
794 if (NULL != peer->core_transmit)
796 GNUNET_break (0); /* GCP_queue_destroy should've cancelled it! */
797 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
798 peer->core_transmit = NULL;
801 GNUNET_free_non_null (peer->hello);
808 * Iterator over peer hash map entries to destroy the peer during in_shutdown.
811 * @param key current key code
812 * @param value value in the hash map
813 * @return #GNUNET_YES if we should continue to iterate,
817 shutdown_peer (void *cls,
818 const struct GNUNET_PeerIdentity *key,
821 struct CadetPeer *p = value;
822 struct CadetTunnel *t = p->tunnel;
824 LOG (GNUNET_ERROR_TYPE_DEBUG, " shutting down %s\n", GCP_2s (p));
835 * Check if peer is searching for a path (either active or delayed search).
837 * @param peer Peer to check
838 * @return #GNUNET_YES if there is a search active.
839 * #GNUNET_NO otherwise.
842 is_searching (const struct CadetPeer *peer)
844 return ( (NULL == peer->search_h) &&
845 (NULL == peer->search_delayed) ) ?
846 GNUNET_NO : GNUNET_YES;
851 * @brief Start a search for a peer.
853 * @param cls Closure (Peer to search for).
856 delayed_search (void *cls)
858 struct CadetPeer *peer = cls;
860 peer->search_delayed = NULL;
861 GCC_check_connections ();
862 GCP_start_search (peer);
863 GCC_check_connections ();
868 * Returns if peer is used (has a tunnel or is neighbor).
870 * @param peer Peer to check.
871 * @return #GNUNET_YES if peer is in use.
874 peer_is_used (struct CadetPeer *peer)
876 struct CadetPeerPath *p;
878 if (NULL != peer->tunnel)
881 for (p = peer->path_head; NULL != p; p = p->next)
891 * Iterator over all the peers to get the oldest timestamp.
893 * @param cls Closure (unsued).
894 * @param key ID of the peer.
895 * @param value Peer_Info of the peer.
898 peer_get_oldest (void *cls,
899 const struct GNUNET_PeerIdentity *key,
902 struct CadetPeer *p = value;
903 struct GNUNET_TIME_Absolute *abs = cls;
905 /* Don't count active peers */
906 if (GNUNET_YES == peer_is_used (p))
909 if (abs->abs_value_us < p->last_contact.abs_value_us)
910 abs->abs_value_us = p->last_contact.abs_value_us;
917 * Iterator over all the peers to remove the oldest entry.
919 * @param cls Closure (unsued).
920 * @param key ID of the peer.
921 * @param value Peer_Info of the peer.
924 peer_timeout (void *cls,
925 const struct GNUNET_PeerIdentity *key,
928 struct CadetPeer *p = value;
929 struct GNUNET_TIME_Absolute *abs = cls;
931 LOG (GNUNET_ERROR_TYPE_WARNING,
932 "peer %s timeout\n", GNUNET_i2s (key));
934 if (p->last_contact.abs_value_us == abs->abs_value_us &&
935 GNUNET_NO == peer_is_used (p))
945 * Delete oldest unused peer.
948 peer_delete_oldest (void)
950 struct GNUNET_TIME_Absolute abs;
952 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
954 GNUNET_CONTAINER_multipeermap_iterate (peers,
957 GNUNET_CONTAINER_multipeermap_iterate (peers,
964 * Choose the best (yet unused) path towards a peer,
965 * considering the tunnel properties.
967 * @param peer The destination peer.
968 * @return Best current known path towards the peer, if any.
970 static struct CadetPeerPath *
971 peer_get_best_path (const struct CadetPeer *peer)
973 struct CadetPeerPath *best_p;
974 struct CadetPeerPath *p;
975 unsigned int best_cost;
978 best_cost = UINT_MAX;
980 for (p = peer->path_head; NULL != p; p = p->next)
982 if (GNUNET_NO == path_is_valid (p))
983 continue; /* Don't use invalid paths. */
984 if (GNUNET_YES == GCT_is_path_used (peer->tunnel, p))
985 continue; /* If path is already in use, skip it. */
987 if ((cost = GCT_get_path_cost (peer->tunnel, p)) < best_cost)
998 * Is this queue element sendable?
1000 * - All management traffic is always sendable.
1001 * - For payload traffic, check the connection flow control.
1003 * @param q Queue element to inspect.
1004 * @return #GNUNET_YES if it is sendable, #GNUNET_NO otherwise.
1007 queue_is_sendable (struct CadetPeerQueue *q)
1009 /* Is PID-independent? */
1012 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1013 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1014 case GNUNET_MESSAGE_TYPE_CADET_KX:
1015 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1016 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1017 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1018 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1021 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1022 case GNUNET_MESSAGE_TYPE_CADET_AX:
1029 return GCC_is_sendable (q->c, q->fwd);
1034 * Get first sendable message.
1036 * @param peer The destination peer.
1038 * @return First transmittable message, if any. Otherwise, NULL.
1040 static struct CadetPeerQueue *
1041 peer_get_first_message (const struct CadetPeer *peer)
1043 struct CadetPeerQueue *q;
1045 for (q = peer->queue_head; NULL != q; q = q->next)
1047 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking q:%p on c:%s\n", q, GCC_2s (q->c));
1048 if (queue_is_sendable (q))
1057 * Function to process paths received for a new peer addition. The recorded
1058 * paths form the initial tunnel, which can be optimized later.
1059 * Called on each result obtained for the DHT search.
1061 * @param cls Closure (peer towards a path has been found).
1062 * @param path Path created from the DHT query. Will be freed afterwards.
1065 search_handler (void *cls, const struct CadetPeerPath *path)
1067 struct CadetPeer *peer = cls;
1068 unsigned int connection_count;
1070 GCC_check_connections ();
1071 GCP_add_path_to_all (path, GNUNET_NO);
1073 /* Count connections */
1074 connection_count = GCT_count_connections (peer->tunnel);
1076 /* If we already have our minimum (or more) connections, it's enough */
1077 if (CONNECTIONS_PER_TUNNEL <= connection_count)
1079 GCC_check_connections ();
1083 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (peer->tunnel))
1085 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
1088 GCC_check_connections ();
1093 * Adjust core requested size to accomodate an ACK.
1095 * @param message_size Requested size.
1097 * @return Size enough to fit @c message_size and an ACK.
1100 get_core_size (size_t message_size)
1102 return message_size + sizeof (struct GNUNET_CADET_ACK);
1106 * Test if a message type is connection management traffic
1107 * or regular payload traffic.
1109 * @param type Message type.
1111 * @return #GNUNET_YES if connection management, #GNUNET_NO otherwise.
1114 is_connection_management (uint16_t type)
1116 return type == GNUNET_MESSAGE_TYPE_CADET_ACK ||
1117 type == GNUNET_MESSAGE_TYPE_CADET_POLL;
1122 * Fill a core buffer with the appropriate data for the queued message.
1124 * @param queue Queue element for the message.
1125 * @param buf Core buffer to fill.
1126 * @param size Size remaining in @c buf.
1127 * @param[out] pid In case its an encrypted payload, set payload.
1129 * @return Bytes written to @c buf.
1132 fill_buf (struct CadetPeerQueue *queue, void *buf, size_t size, uint32_t *pid)
1134 struct CadetConnection *c = queue->c;
1137 switch (queue->type)
1139 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1140 *pid = GCC_get_pid (queue->c, queue->fwd);
1141 LOG (GNUNET_ERROR_TYPE_DEBUG, " otr payload ID %u\n", *pid);
1142 msg_size = send_core_data_raw (queue->cls, size, buf);
1143 ((struct GNUNET_CADET_Encrypted *) buf)->pid = htonl (*pid);
1145 case GNUNET_MESSAGE_TYPE_CADET_AX:
1146 *pid = GCC_get_pid (queue->c, queue->fwd);
1147 LOG (GNUNET_ERROR_TYPE_DEBUG, " ax payload ID %u\n", *pid);
1148 msg_size = send_core_data_raw (queue->cls, size, buf);
1149 ((struct GNUNET_CADET_AX *) buf)->pid = htonl (*pid);
1151 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1152 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1153 case GNUNET_MESSAGE_TYPE_CADET_KX:
1154 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1155 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1156 LOG (GNUNET_ERROR_TYPE_DEBUG, " raw %s\n", GC_m2s (queue->type));
1157 msg_size = send_core_data_raw (queue->cls, size, buf);
1159 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1160 LOG (GNUNET_ERROR_TYPE_DEBUG, " path create\n");
1161 if (GCC_is_origin (c, GNUNET_YES))
1162 msg_size = send_core_connection_create (c, size, buf);
1164 msg_size = send_core_data_raw (queue->cls, size, buf);
1166 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1167 LOG (GNUNET_ERROR_TYPE_DEBUG, " path ack\n");
1168 if (GCC_is_origin (c, GNUNET_NO) ||
1169 GCC_is_origin (c, GNUNET_YES))
1171 msg_size = send_core_connection_ack (c, size, buf);
1175 msg_size = send_core_data_raw (queue->cls, size, buf);
1178 case GNUNET_MESSAGE_TYPE_CADET_DATA:
1179 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1180 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1181 /* This should be encapsulted */
1187 LOG (GNUNET_ERROR_TYPE_WARNING, " type unknown: %u\n", queue->type);
1191 GNUNET_assert (size >= msg_size);
1198 * Debug function should NEVER return true in production code, useful to
1199 * simulate losses for testcases.
1201 * @param q Queue handle with info about the message.
1203 * @return #GNUNET_YES or #GNUNET_NO with the decision to drop.
1206 should_I_drop (struct CadetPeerQueue *q)
1208 if (0 == drop_percent)
1211 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
1219 * Core callback to write a queued packet to core buffer
1221 * @param cls Closure (peer info).
1222 * @param size Number of bytes available in buf.
1223 * @param buf Where the to write the message.
1225 * @return number of bytes written to buf
1228 queue_send (void *cls, size_t size, void *buf)
1230 struct CadetPeer *peer = cls;
1231 struct CadetConnection *c;
1232 struct CadetPeerQueue *queue;
1233 struct GNUNET_TIME_Relative core_wait_time;
1235 const struct GNUNET_PeerIdentity *dst_id;
1242 GCC_check_connections ();
1243 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1244 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1245 LOG (GNUNET_ERROR_TYPE_DEBUG, "Queue send towards %s (max %u)\n",
1246 GCP_2s (peer), size);
1248 /* Sanity checking */
1249 if (NULL == buf || 0 == size)
1251 LOG (GNUNET_ERROR_TYPE_DEBUG, " not allowed/\n");
1252 if (GNUNET_NO == in_shutdown)
1254 queue = peer_get_first_message (peer);
1257 peer->core_transmit = NULL;
1258 peer->tmt_time.abs_value_us = 0;
1259 GCC_check_connections ();
1262 dst_id = GNUNET_PEER_resolve2 (peer->id);
1263 peer->core_transmit =
1264 GNUNET_CORE_notify_transmit_ready (core_handle,
1265 GNUNET_NO, get_priority (queue),
1266 GNUNET_TIME_UNIT_FOREVER_REL,
1268 get_core_size (queue->size),
1271 peer->tmt_time = GNUNET_TIME_absolute_get ();
1275 peer->core_transmit = NULL;
1276 peer->tmt_time.abs_value_us = 0;
1278 GCC_check_connections ();
1287 peer->core_transmit = NULL;
1288 queue = peer_get_first_message (peer);
1291 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
1292 peer->tmt_time.abs_value_us = 0;
1295 core_wait_time = GNUNET_TIME_absolute_get_duration (peer->tmt_time);
1296 wait_s = GNUNET_STRINGS_relative_time_to_string (core_wait_time, GNUNET_YES);
1297 if (core_wait_time.rel_value_us >= 1000000)
1299 LOG (GNUNET_ERROR_TYPE_WARNING,
1300 " %s: core wait time %s (> 1 second) for %u bytes\n",
1301 GCP_2s (peer), wait_s, queue->size);
1303 peer->tmt_time.abs_value_us = 0;
1305 /* Copy all possible messages to the core buffer */
1306 while (NULL != queue && rest >= queue->size)
1310 LOG (GNUNET_ERROR_TYPE_DEBUG, " on conn %s %s\n",
1311 GCC_2s (c), GC_f2s(queue->fwd));
1312 LOG (GNUNET_ERROR_TYPE_DEBUG, " size %u ok (%u/%u)\n",
1313 queue->size, total_size, size);
1315 msg_size = fill_buf (queue, (void *) dst, size, &pid);
1317 if (should_I_drop (queue))
1319 LOG (GNUNET_ERROR_TYPE_WARNING, "DD %s (%s %u) on conn %s %s\n",
1320 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1321 queue->payload_id, GCC_2s (c), GC_f2s (queue->fwd));
1326 LOG (GNUNET_ERROR_TYPE_INFO,
1327 ">>> %s (%s %4u) on conn %s (%p) %s [%5u], after %s\n",
1328 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1329 queue->payload_id, GCC_2s (c), c,
1330 GC_f2s (queue->fwd), msg_size, wait_s);
1332 total_size += msg_size;
1334 dst = &dst[msg_size];
1337 /* Free queue, but cls was freed by send_core_* in fill_buf. */
1338 (void) GCP_queue_destroy (queue, GNUNET_NO, GNUNET_YES, pid);
1341 queue = peer_get_first_message (peer);
1344 /* If more data in queue, send next */
1347 LOG (GNUNET_ERROR_TYPE_DEBUG, " more data! (%u)\n", queue->size);
1348 if (NULL == peer->core_transmit)
1350 dst_id = GNUNET_PEER_resolve2 (peer->id);
1351 peer->core_transmit =
1352 GNUNET_CORE_notify_transmit_ready (core_handle,
1353 GNUNET_NO, get_priority (queue),
1354 GNUNET_TIME_UNIT_FOREVER_REL,
1356 get_core_size (queue->size),
1359 peer->tmt_time = GNUNET_TIME_absolute_get ();
1360 queue->start_waiting = GNUNET_TIME_absolute_get ();
1364 LOG (GNUNET_ERROR_TYPE_DEBUG, "* tmt rdy called somewhere else\n");
1366 // GCC_start_poll (); FIXME needed?
1370 // GCC_stop_poll(); FIXME needed?
1373 LOG (GNUNET_ERROR_TYPE_DEBUG, " return %d\n", total_size);
1374 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1375 GCC_check_connections ();
1380 /******************************************************************************/
1381 /******************************** API ***********************************/
1382 /******************************************************************************/
1386 * Free a transmission that was already queued with all resources
1387 * associated to the request.
1389 * If connection was marked to be destroyed, and this was the last queued
1390 * message on it, the connection will be free'd as a result.
1392 * @param queue Queue handler to cancel.
1393 * @param clear_cls Is it necessary to free associated cls?
1394 * @param sent Was it really sent? (Could have been canceled)
1395 * @param pid PID, if relevant (was sent and was a payload message).
1397 * @return #GNUNET_YES if connection was destroyed as a result,
1398 * #GNUNET_NO otherwise.
1401 GCP_queue_destroy (struct CadetPeerQueue *queue,
1406 struct CadetPeer *peer;
1407 int connection_destroyed;
1409 GCC_check_connections ();
1411 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue destroy %s\n", GC_m2s (queue->type));
1412 if (GNUNET_YES == clear_cls)
1414 LOG (GNUNET_ERROR_TYPE_DEBUG, " free cls\n");
1415 switch (queue->type)
1417 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1418 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
1420 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1421 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1422 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1423 case GNUNET_MESSAGE_TYPE_CADET_KX:
1424 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1425 case GNUNET_MESSAGE_TYPE_CADET_AX:
1426 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1427 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1428 GNUNET_free_non_null (queue->cls);
1433 LOG (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
1434 GC_m2s (queue->type));
1437 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
1439 if (!is_connection_management (queue->type))
1444 if (NULL != queue->cont)
1446 struct GNUNET_TIME_Relative wait_time;
1448 wait_time = GNUNET_TIME_absolute_get_duration (queue->start_waiting);
1449 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling callback, time elapsed %s\n",
1450 GNUNET_STRINGS_relative_time_to_string (wait_time, GNUNET_NO));
1451 connection_destroyed = queue->cont (queue->cont_cls,
1452 queue->c, sent, queue->type, pid,
1453 queue->fwd, queue->size, wait_time);
1457 connection_destroyed = GNUNET_NO;
1460 if (NULL == peer_get_first_message (peer) && NULL != peer->core_transmit)
1462 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1463 peer->core_transmit = NULL;
1464 peer->tmt_time.abs_value_us = 0;
1467 GNUNET_free (queue);
1468 GCC_check_connections ();
1469 return connection_destroyed;
1474 * @brief Queue and pass message to core when possible.
1476 * @param peer Peer towards which to queue the message.
1477 * @param cls Closure (@c type dependant). It will be used by queue_send to
1478 * build the message to be sent if not already prebuilt.
1479 * @param type Type of the message.
1480 * @param payload_type Type of the message's payload
1481 * 0 if the message is a retransmission (unknown payload).
1482 * UINT16_MAX if the message does not have payload.
1483 * @param payload_id ID of the payload (MID, ACK #, etc)
1484 * @param size Size of the message.
1485 * @param c Connection this message belongs to (can be NULL).
1486 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1487 * @param cont Continuation to be called once CORE has taken the message.
1488 * @param cont_cls Closure for @c cont.
1490 * @return Handle to cancel the message before it is sent. Once cont is called
1491 * message has been sent and therefore the handle is no longer valid.
1493 struct CadetPeerQueue *
1494 GCP_queue_add (struct CadetPeer *peer,
1497 uint16_t payload_type,
1498 uint32_t payload_id,
1500 struct CadetConnection *c,
1505 struct CadetPeerQueue *q;
1509 GCC_check_connections ();
1510 LOG (GNUNET_ERROR_TYPE_DEBUG,
1511 "que %s (%s %4u) on conn %s (%p) %s towards %s (size %u)\n",
1512 GC_m2s (type), GC_m2s (payload_type), payload_id,
1513 GCC_2s (c), c, GC_f2s (fwd), GCP_2s (peer), size);
1515 if (NULL == peer->connections)
1517 /* We are not connected to this peer, ignore request. */
1518 LOG (GNUNET_ERROR_TYPE_INFO, "%s not a neighbor\n", GCP_2s (peer));
1519 GNUNET_STATISTICS_update (stats, "# messages dropped due to wrong hop", 1,
1525 if (is_connection_management (type))
1529 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1531 call_core = (NULL == c || GNUNET_MESSAGE_TYPE_CADET_KX == type) ?
1532 GNUNET_YES : GCC_is_sendable (c, fwd);
1533 q = GNUNET_new (struct CadetPeerQueue);
1536 q->payload_type = payload_type;
1537 q->payload_id = payload_id;
1543 q->cont_cls = cont_cls;
1546 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, q);
1551 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, q);
1552 call_core = GNUNET_YES;
1555 q->start_waiting = GNUNET_TIME_absolute_get ();
1556 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1558 LOG (GNUNET_ERROR_TYPE_DEBUG,
1559 "calling core tmt rdy towards %s for %u bytes\n",
1560 GCP_2s (peer), size);
1561 peer->core_transmit =
1562 GNUNET_CORE_notify_transmit_ready (core_handle,
1563 GNUNET_NO, get_priority (q),
1564 GNUNET_TIME_UNIT_FOREVER_REL,
1565 GNUNET_PEER_resolve2 (peer->id),
1566 get_core_size (size),
1568 peer->tmt_time = GNUNET_TIME_absolute_get ();
1570 else if (GNUNET_NO == call_core)
1572 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s not needed\n",
1578 struct GNUNET_TIME_Relative elapsed;
1579 elapsed = GNUNET_TIME_absolute_get_duration (peer->tmt_time);
1580 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s already called %s\n",
1582 GNUNET_STRINGS_relative_time_to_string (elapsed, GNUNET_NO));
1585 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1586 GCC_check_connections ();
1592 * Cancel all queued messages to a peer that belong to a certain connection.
1594 * @param peer Peer towards whom to cancel.
1595 * @param c Connection whose queued messages to cancel. Might be destroyed by
1596 * the sent continuation call.
1599 GCP_queue_cancel (struct CadetPeer *peer,
1600 struct CadetConnection *c)
1602 struct CadetPeerQueue *q;
1603 struct CadetPeerQueue *next;
1604 struct CadetPeerQueue *prev;
1605 int connection_destroyed;
1607 GCC_check_connections ();
1608 connection_destroyed = GNUNET_NO;
1609 for (q = peer->queue_head; NULL != q; q = next)
1614 LOG (GNUNET_ERROR_TYPE_DEBUG,
1615 "GMP queue cancel %s\n",
1617 GNUNET_assert (GNUNET_NO == connection_destroyed);
1618 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY == q->type)
1624 connection_destroyed = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1627 /* Get next from prev, q->next might be already freed:
1628 * queue destroy -> callback -> GCC_destroy -> cancel_queues -> here
1631 next = peer->queue_head;
1641 if ( (NULL == peer->queue_head) &&
1642 (NULL != peer->core_transmit) )
1644 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1645 peer->core_transmit = NULL;
1646 peer->tmt_time.abs_value_us = 0;
1648 GCC_check_connections ();
1653 * Get the first transmittable message for a connection.
1655 * @param peer Neighboring peer.
1656 * @param c Connection.
1658 * @return First transmittable message.
1660 static struct CadetPeerQueue *
1661 connection_get_first_message (struct CadetPeer *peer, struct CadetConnection *c)
1663 struct CadetPeerQueue *q;
1665 for (q = peer->queue_head; NULL != q; q = q->next)
1669 if (queue_is_sendable (q))
1671 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable!!\n");
1674 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
1682 * Get the first message for a connection and unqueue it.
1684 * Only tunnel (or higher) level messages are unqueued. Connection specific
1685 * messages are silently destroyed upon encounter.
1687 * @param peer Neighboring peer.
1688 * @param c Connection.
1689 * @param destroyed[in/out] Was the connection destroyed (prev/as a result)?.
1692 * @return First message for this connection.
1694 struct GNUNET_MessageHeader *
1695 GCP_connection_pop (struct CadetPeer *peer,
1696 struct CadetConnection *c,
1699 struct CadetPeerQueue *q;
1700 struct CadetPeerQueue *next;
1701 struct GNUNET_MessageHeader *msg;
1704 GCC_check_connections ();
1705 GNUNET_assert (NULL != destroyed);
1706 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection_pop on conn %p\n", c);
1707 for (q = peer->queue_head; NULL != q; q = next)
1712 LOG (GNUNET_ERROR_TYPE_DEBUG, " - queued: %s (%s %u), cont: %p\n",
1713 GC_m2s (q->type), GC_m2s (q->payload_type), q->payload_id,
1717 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1718 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1719 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1720 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1721 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1722 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1723 dest = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1724 if (GNUNET_YES == dest)
1726 GNUNET_break (GNUNET_NO == *destroyed);
1727 *destroyed = GNUNET_YES;
1731 case GNUNET_MESSAGE_TYPE_CADET_KX:
1732 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1733 case GNUNET_MESSAGE_TYPE_CADET_AX:
1734 case GNUNET_MESSAGE_TYPE_CADET_AX_KX:
1735 msg = (struct GNUNET_MessageHeader *) q->cls;
1736 dest = GCP_queue_destroy (q, GNUNET_NO, GNUNET_NO, 0);
1737 if (GNUNET_YES == dest)
1739 GNUNET_break (GNUNET_NO == *destroyed);
1740 *destroyed = GNUNET_YES;
1746 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unknown message %s\n", GC_m2s (q->type));
1749 GCC_check_connections ();
1755 * Unlock a possibly locked queue for a connection.
1757 * If there is a message that can be sent on this connection, call core for it.
1758 * Otherwise (if core transmit is already called or there is no sendable
1759 * message) do nothing.
1761 * @param peer Peer who keeps the queue.
1762 * @param c Connection whose messages to unlock.
1765 GCP_queue_unlock (struct CadetPeer *peer, struct CadetConnection *c)
1767 struct CadetPeerQueue *q;
1770 GCC_check_connections ();
1771 if (NULL != peer->core_transmit)
1773 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
1774 return; /* Already unlocked */
1777 q = connection_get_first_message (peer, c);
1780 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
1781 return; /* Nothing to transmit */
1785 peer->core_transmit =
1786 GNUNET_CORE_notify_transmit_ready (core_handle,
1787 GNUNET_NO, get_priority (q),
1788 GNUNET_TIME_UNIT_FOREVER_REL,
1789 GNUNET_PEER_resolve2 (peer->id),
1790 get_core_size (size),
1793 peer->tmt_time = GNUNET_TIME_absolute_get ();
1794 GCC_check_connections ();
1799 * Initialize the peer subsystem.
1801 * @param c Configuration.
1804 GCP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1807 LOG (GNUNET_ERROR_TYPE_DEBUG,
1809 in_shutdown = GNUNET_NO;
1810 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1812 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_PEERS",
1815 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1816 "CADET", "MAX_PEERS", "USING DEFAULT");
1821 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "DROP_PERCENT",
1828 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1829 LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1830 LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1831 LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1832 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1834 ats_ch = GNUNET_ATS_connectivity_init (c);
1835 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1836 NULL, /* Closure passed to CADET functions */
1837 &core_init, /* Call core_init once connected */
1838 &core_connect, /* Handle connects */
1839 &core_disconnect, /* remove peers on disconnects */
1840 NULL, /* Don't notify about all incoming messages */
1841 GNUNET_NO, /* For header only in notification */
1842 NULL, /* Don't notify about all outbound messages */
1843 GNUNET_NO, /* For header-only out notification */
1844 core_handlers); /* Register these handlers */
1845 if (NULL == core_handle)
1848 GNUNET_SCHEDULER_shutdown ();
1856 * Shut down the peer subsystem.
1861 LOG (GNUNET_ERROR_TYPE_DEBUG,
1862 "Shutting down peer subsystem\n");
1863 in_shutdown = GNUNET_YES;
1864 GNUNET_CONTAINER_multipeermap_iterate (peers,
1867 if (NULL != core_handle)
1869 GNUNET_CORE_disconnect (core_handle);
1874 GNUNET_ATS_connectivity_done (ats_ch);
1877 GNUNET_PEER_change_rc (myid, -1);
1878 GNUNET_CONTAINER_multipeermap_destroy (peers);
1884 * Retrieve the CadetPeer stucture associated with the peer. Optionally create
1885 * one and insert it in the appropriate structures if the peer is not known yet.
1887 * @param peer_id Full identity of the peer.
1888 * @param create #GNUNET_YES if a new peer should be created if unknown.
1889 * #GNUNET_NO otherwise.
1891 * @return Existing or newly created peer structure.
1892 * NULL if unknown and not requested @a create
1895 GCP_get (const struct GNUNET_PeerIdentity *peer_id, int create)
1897 struct CadetPeer *peer;
1899 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1902 peer = GNUNET_new (struct CadetPeer);
1903 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1905 peer_delete_oldest ();
1907 GNUNET_assert (GNUNET_OK ==
1908 GNUNET_CONTAINER_multipeermap_put (peers,
1911 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1912 peer->id = GNUNET_PEER_intern (peer_id);
1914 peer->last_contact = GNUNET_TIME_absolute_get ();
1921 * Retrieve the CadetPeer stucture associated with the
1922 * peer. Optionally create one and insert it in the appropriate
1923 * structures if the peer is not known yet.
1925 * @param peer Short identity of the peer.
1926 * @param create #GNUNET_YES if a new peer should be created if unknown.
1927 * #GNUNET_NO otherwise.
1929 * @return Existing or newly created peer structure.
1930 * NULL if unknown and not requested @a create
1933 GCP_get_short (const GNUNET_PEER_Id peer, int create)
1935 return GCP_get (GNUNET_PEER_resolve2 (peer), create);
1940 * Function called once #GNUNET_TRANSPORT_offer_hello() is done.
1941 * Marks the operation as finished.
1943 * @param cls Closure (our `struct CadetPeer`).
1946 hello_offer_done (void *cls)
1948 struct CadetPeer *peer = cls;
1950 peer->hello_offer = NULL;
1955 * Try to establish a new connection to this peer (in its tunnel).
1956 * If the peer doesn't have any path to it yet, try to get one.
1957 * If the peer already has some path, send a CREATE CONNECTION towards it.
1959 * @param peer Peer to connect to.
1962 GCP_connect (struct CadetPeer *peer)
1964 struct CadetTunnel *t;
1965 struct CadetPeerPath *path;
1966 struct CadetConnection *c;
1969 GCC_check_connections ();
1970 LOG (GNUNET_ERROR_TYPE_DEBUG,
1971 "peer_connect towards %s\n",
1973 /* If we have a current hello, try to connect using it. */
1974 GCP_try_connect (peer);
1978 rerun_search = GNUNET_NO;
1980 if (NULL != peer->path_head)
1982 LOG (GNUNET_ERROR_TYPE_DEBUG, " some path exists\n");
1983 path = peer_get_best_path (peer);
1989 LOG (GNUNET_ERROR_TYPE_DEBUG, " path to use: %s\n", s);
1992 c = GCT_use_path (t, path);
1995 /* This case can happen when the path includes a first hop that is
1996 * not yet known to be connected.
1998 * This happens quite often during testing when running cadet
1999 * under valgrind: core connect notifications come very late
2000 * and the DHT result has already come and created a valid
2001 * path. In this case, the peer->connections
2002 * hashmaps will be NULL and tunnel_use_path will not be able
2003 * to create a connection from that path.
2005 * Re-running the DHT GET should give core time to callback.
2007 * GCT_use_path -> GCC_new -> register_neighbors takes care of
2008 * updating statistics about this issue.
2010 rerun_search = GNUNET_YES;
2014 GCC_send_create (c);
2020 LOG (GNUNET_ERROR_TYPE_DEBUG, " but is NULL, all paths are in use\n");
2024 if (GNUNET_YES == rerun_search)
2026 struct GNUNET_TIME_Relative delay;
2028 GCP_stop_search (peer);
2029 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
2030 peer->search_delayed = GNUNET_SCHEDULER_add_delayed (delay,
2033 GCC_check_connections ();
2037 if (GNUNET_NO == is_searching (peer))
2038 GCP_start_search (peer);
2039 GCC_check_connections ();
2044 * Chech whether there is a direct (core level) connection to peer.
2046 * @param peer Peer to check.
2048 * @return #GNUNET_YES if there is a direct connection.
2051 GCP_is_neighbor (const struct CadetPeer *peer)
2053 struct CadetPeerPath *path;
2055 if (NULL == peer->connections)
2058 for (path = peer->path_head; NULL != path; path = path->next)
2060 if (3 > path->length)
2064 /* Is not a neighbor but connections is not NULL, probably disconnecting */
2071 * Create and initialize a new tunnel towards a peer, in case it has none.
2072 * In case the peer already has a tunnel, nothing is done.
2074 * Does not generate any traffic, just creates the local data structures.
2076 * @param peer Peer towards which to create the tunnel.
2079 GCP_add_tunnel (struct CadetPeer *peer)
2081 GCC_check_connections ();
2082 if (NULL != peer->tunnel)
2084 peer->tunnel = GCT_new (peer);
2085 GCC_check_connections ();
2090 * Add a connection to a neighboring peer.
2092 * Store that the peer is the first hop of the connection in one
2093 * direction and that on peer disconnect the connection must be
2094 * notified and destroyed, for it will no longer be valid.
2096 * @param peer Peer to add connection to.
2097 * @param c Connection to add.
2098 * @param pred #GNUNET_YES if we are predecessor, #GNUNET_NO if we are successor
2101 GCP_add_connection (struct CadetPeer *peer,
2102 struct CadetConnection *c,
2105 LOG (GNUNET_ERROR_TYPE_DEBUG,
2106 "adding connection %s\n",
2108 LOG (GNUNET_ERROR_TYPE_DEBUG,
2111 GNUNET_assert (NULL != peer->connections);
2112 GNUNET_assert (GNUNET_OK ==
2113 GNUNET_CONTAINER_multihashmap_put (peer->connections,
2116 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2117 LOG (GNUNET_ERROR_TYPE_DEBUG,
2118 "Peer %s has now %u connections.\n",
2120 GNUNET_CONTAINER_multihashmap_size (peer->connections));
2125 * Add the path to the peer and update the path used to reach it in case this
2128 * @param peer Destination peer to add the path to.
2129 * @param path New path to add. Last peer must be @c peer.
2130 * Path will be either used of freed if already known.
2131 * @param trusted Do we trust that this path is real?
2133 * @return path if path was taken, pointer to existing duplicate if exists
2136 struct CadetPeerPath *
2137 GCP_add_path (struct CadetPeer *peer,
2138 struct CadetPeerPath *path,
2141 struct CadetPeerPath *aux;
2145 GCC_check_connections ();
2146 LOG (GNUNET_ERROR_TYPE_DEBUG,
2147 "adding path [%u] to peer %s\n",
2148 path->length, GCP_2s (peer));
2150 if (NULL == peer || NULL == path
2151 || path->peers[path->length - 1] != peer->id)
2154 path_destroy (path);
2158 for (l = 1; l < path->length; l++)
2160 if (path->peers[l] == myid)
2162 LOG (GNUNET_ERROR_TYPE_DEBUG, " shortening path by %u\n", l);
2163 for (l2 = 0; l2 < path->length - l; l2++)
2165 path->peers[l2] = path->peers[l + l2];
2169 path->peers = GNUNET_realloc (path->peers,
2170 path->length * sizeof (GNUNET_PEER_Id));
2174 LOG (GNUNET_ERROR_TYPE_DEBUG, " final length: %u\n", path->length);
2176 if (2 >= path->length && GNUNET_NO == trusted)
2178 /* Only allow CORE to tell us about direct paths */
2179 path_destroy (path);
2183 l = path_get_length (path);
2186 path_destroy (path);
2190 GNUNET_assert (peer->id == path->peers[path->length - 1]);
2191 for (aux = peer->path_head; aux != NULL; aux = aux->next)
2193 l2 = path_get_length (aux);
2196 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
2197 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
2198 peer->path_tail, aux, path);
2203 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2205 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
2206 path_destroy (path);
2211 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head,
2214 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
2217 if (NULL != peer->tunnel
2218 && CONNECTIONS_PER_TUNNEL > GCT_count_connections (peer->tunnel)
2219 && 2 < path->length) /* Direct paths are handled by core_connect */
2223 GCC_check_connections ();
2229 * Add the path to the origin peer and update the path used to reach it in case
2230 * this is the shortest.
2231 * The path is given in peer_info -> destination, therefore we turn the path
2232 * upside down first.
2234 * @param peer Peer to add the path to, being the origin of the path.
2235 * @param path New path to add after being inversed.
2236 * Path will be either used or freed.
2237 * @param trusted Do we trust that this path is real?
2239 * @return path if path was taken, pointer to existing duplicate if exists
2242 struct CadetPeerPath *
2243 GCP_add_path_to_origin (struct CadetPeer *peer,
2244 struct CadetPeerPath *path,
2250 return GCP_add_path (peer, path, trusted);
2255 * Adds a path to the info of all the peers in the path
2257 * @param p Path to process.
2258 * @param confirmed Whether we know if the path works or not.
2261 GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed)
2265 /* TODO: invert and add */
2266 GCC_check_connections ();
2267 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2268 for (i++; i < p->length; i++)
2270 struct CadetPeer *peer;
2271 struct CadetPeerPath *copy;
2273 peer = GCP_get_short (p->peers[i], GNUNET_YES);
2274 copy = path_duplicate (p);
2275 copy->length = i + 1;
2276 GCP_add_path (peer, copy, 3 > p->length ? GNUNET_NO : confirmed);
2278 GCC_check_connections ();
2283 * Remove any path to the peer that has the exact same peers as the one given.
2285 * @param peer Peer to remove the path from.
2286 * @param path Path to remove. Is always destroyed .
2289 GCP_remove_path (struct CadetPeer *peer,
2290 struct CadetPeerPath *path)
2292 struct CadetPeerPath *iter;
2293 struct CadetPeerPath *next;
2295 GCC_check_connections ();
2296 GNUNET_assert (myid == path->peers[0]);
2297 GNUNET_assert (peer->id == path->peers[path->length - 1]);
2299 LOG (GNUNET_ERROR_TYPE_INFO,
2300 "Removing path %p (%u) from %s\n",
2301 path, path->length, GCP_2s (peer));
2303 for (iter = peer->path_head; NULL != iter; iter = next)
2306 if (0 == path_cmp (path, iter))
2308 GNUNET_CONTAINER_DLL_remove (peer->path_head,
2312 path_destroy (iter);
2315 path_destroy (path);
2316 GCC_check_connections ();
2321 * Check that we are aware of a connection from a neighboring peer.
2323 * @param peer Peer to the connection is with
2324 * @param c Connection that should be in the map with this peer.
2327 GCP_check_connection (const struct CadetPeer *peer,
2328 const struct CadetConnection *c)
2330 GNUNET_assert (NULL != peer);
2331 GNUNET_assert (NULL != peer->connections);
2333 GNUNET_assert (GNUNET_YES ==
2334 GNUNET_CONTAINER_multihashmap_contains_value (peer->connections,
2341 * Remove a connection from a neighboring peer.
2343 * @param peer Peer to remove connection from.
2344 * @param c Connection to remove.
2347 GCP_remove_connection (struct CadetPeer *peer,
2348 const struct CadetConnection *c)
2350 LOG (GNUNET_ERROR_TYPE_DEBUG,
2351 "Removing connection %s\n",
2353 LOG (GNUNET_ERROR_TYPE_DEBUG,
2356 if ( (NULL == peer) ||
2357 (NULL == peer->connections) )
2359 GNUNET_assert (GNUNET_YES ==
2360 GNUNET_CONTAINER_multihashmap_remove (peer->connections,
2363 LOG (GNUNET_ERROR_TYPE_DEBUG,
2364 "Peer %s remains with %u connections.\n",
2366 GNUNET_CONTAINER_multihashmap_size (peer->connections));
2371 * Start the DHT search for new paths towards the peer: we don't have
2372 * enough good connections.
2374 * @param peer Destination peer.
2377 GCP_start_search (struct CadetPeer *peer)
2379 const struct GNUNET_PeerIdentity *id;
2380 struct CadetTunnel *t = peer->tunnel;
2382 GCC_check_connections ();
2383 if (NULL != peer->search_h)
2389 if (NULL != peer->search_delayed)
2390 GCP_stop_search (peer);
2392 id = GNUNET_PEER_resolve2 (peer->id);
2393 peer->search_h = GCD_search (id, &search_handler, peer);
2397 /* Why would we search for a peer with no tunnel towards it? */
2402 if (CADET_TUNNEL_NEW == GCT_get_cstate (t)
2403 || 0 == GCT_count_any_connections (t))
2405 GCT_change_cstate (t, CADET_TUNNEL_SEARCHING);
2407 GCC_check_connections ();
2412 * Stop the DHT search for new paths towards the peer: we already have
2413 * enough good connections.
2415 * @param peer Destination peer.
2418 GCP_stop_search (struct CadetPeer *peer)
2420 GCC_check_connections ();
2421 if (NULL != peer->search_h)
2423 GCD_search_stop (peer->search_h);
2424 peer->search_h = NULL;
2426 if (NULL != peer->search_delayed)
2428 GNUNET_SCHEDULER_cancel (peer->search_delayed);
2429 peer->search_delayed = NULL;
2431 GCC_check_connections ();
2436 * Get the Full ID of a peer.
2438 * @param peer Peer to get from.
2440 * @return Full ID of peer.
2442 const struct GNUNET_PeerIdentity *
2443 GCP_get_id (const struct CadetPeer *peer)
2445 return GNUNET_PEER_resolve2 (peer->id);
2450 * Get the Short ID of a peer.
2452 * @param peer Peer to get from.
2454 * @return Short ID of peer.
2457 GCP_get_short_id (const struct CadetPeer *peer)
2466 * If tunnel is NULL and there was a search active, stop it, as it's useless.
2472 GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t)
2475 if (NULL == t && GNUNET_YES == is_searching (peer))
2477 GCP_stop_search (peer);
2483 * Get the tunnel towards a peer.
2485 * @param peer Peer to get from.
2487 * @return Tunnel towards peer.
2489 struct CadetTunnel *
2490 GCP_get_tunnel (const struct CadetPeer *peer)
2494 return peer->tunnel;
2499 * Set the hello message.
2501 * @param peer Peer whose message to set.
2502 * @param hello Hello message.
2505 GCP_set_hello (struct CadetPeer *peer,
2506 const struct GNUNET_HELLO_Message *hello)
2508 struct GNUNET_HELLO_Message *old;
2511 GCC_check_connections ();
2512 LOG (GNUNET_ERROR_TYPE_DEBUG, "set hello for %s\n", GCP_2s (peer));
2516 old = GCP_get_hello (peer);
2519 size = GNUNET_HELLO_size (hello);
2520 peer->hello = GNUNET_malloc (size);
2521 memcpy (peer->hello, hello, size);
2525 peer->hello = GNUNET_HELLO_merge (old, hello);
2528 GCC_check_connections ();
2533 * Get the hello message.
2535 * @param peer Peer whose message to get.
2537 * @return Hello message.
2539 struct GNUNET_HELLO_Message *
2540 GCP_get_hello (struct CadetPeer *peer)
2542 struct GNUNET_TIME_Absolute expiration;
2543 struct GNUNET_TIME_Relative remaining;
2545 if (NULL == peer->hello)
2548 expiration = GNUNET_HELLO_get_last_expiration (peer->hello);
2549 remaining = GNUNET_TIME_absolute_get_remaining (expiration);
2550 if (0 == remaining.rel_value_us)
2552 LOG (GNUNET_ERROR_TYPE_DEBUG, " get - hello expired on %s\n",
2553 GNUNET_STRINGS_absolute_time_to_string (expiration));
2554 GNUNET_free (peer->hello);
2562 * Try to connect to a peer on TRANSPORT level.
2564 * @param peer Peer to whom to connect.
2567 GCP_try_connect (struct CadetPeer *peer)
2569 struct GNUNET_HELLO_Message *hello;
2570 struct GNUNET_MessageHeader *mh;
2573 GNUNET_CONFIGURATION_get_value_yesno (cfg,
2575 "DISABLE_TRY_CONNECT"))
2577 GCC_check_connections ();
2578 if (GNUNET_YES == GCP_is_neighbor (peer))
2580 hello = GCP_get_hello (peer);
2584 mh = GNUNET_HELLO_get_header (hello);
2585 if (NULL != peer->hello_offer)
2587 GNUNET_TRANSPORT_offer_hello_cancel (peer->hello_offer);
2588 peer->hello_offer = NULL;
2590 peer->hello_offer = GNUNET_TRANSPORT_offer_hello (cfg,
2594 if (NULL == peer->connectivity_suggestion)
2595 peer->connectivity_suggestion
2596 = GNUNET_ATS_connectivity_suggest (ats_ch,
2597 GNUNET_PEER_resolve2 (peer->id),
2599 GCC_check_connections ();
2604 * Notify a peer that a link between two other peers is broken. If any path
2605 * used that link, eliminate it.
2607 * @param peer Peer affected by the change.
2608 * @param peer1 Peer whose link is broken.
2609 * @param peer2 Peer whose link is broken.
2612 GCP_notify_broken_link (struct CadetPeer *peer,
2613 const struct GNUNET_PeerIdentity *peer1,
2614 const struct GNUNET_PeerIdentity *peer2)
2616 struct CadetPeerPath *iter;
2617 struct CadetPeerPath *next;
2622 GCC_check_connections ();
2623 p1 = GNUNET_PEER_search (peer1);
2624 p2 = GNUNET_PEER_search (peer2);
2626 LOG (GNUNET_ERROR_TYPE_DEBUG, "Link %u-%u broken\n", p1, p2);
2627 if (0 == p1 || 0 == p2)
2629 /* We don't even know them */
2633 for (iter = peer->path_head; NULL != iter; iter = next)
2636 for (i = 0; i < iter->length - 1; i++)
2638 if ((iter->peers[i] == p1 && iter->peers[i + 1] == p2)
2639 || (iter->peers[i] == p2 && iter->peers[i + 1] == p1))
2644 LOG (GNUNET_ERROR_TYPE_DEBUG, " - invalidating %s\n", s);
2647 path_invalidate (iter);
2651 GCC_check_connections ();
2656 * Count the number of known paths toward the peer.
2658 * @param peer Peer to get path info.
2660 * @return Number of known paths.
2663 GCP_count_paths (const struct CadetPeer *peer)
2665 struct CadetPeerPath *iter;
2668 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2676 * Iterate over the paths to a peer.
2678 * @param peer Peer to get path info.
2679 * @param callback Function to call for every path.
2680 * @param cls Closure for @a callback.
2682 * @return Number of iterated paths.
2685 GCP_iterate_paths (struct CadetPeer *peer,
2686 GCP_path_iterator callback,
2689 struct CadetPeerPath *iter;
2692 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2695 if (GNUNET_YES != callback (cls, peer, iter))
2704 * Iterate all known peers.
2706 * @param iter Iterator.
2707 * @param cls Closure for @c iter.
2710 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter,
2713 GCC_check_connections ();
2714 GNUNET_CONTAINER_multipeermap_iterate (peers,
2717 GCC_check_connections ();
2722 * Get the static string for a peer ID.
2726 * @return Static string for it's ID.
2729 GCP_2s (const struct CadetPeer *peer)
2733 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));
2737 /* end of gnunet-service-cadet_peer.c */