2 This file is part of GNUnet.
3 Copyright (C) 2001-2017 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file cadet/gnunet-service-cadet_peer.c
23 * @brief Information we track per peer.
24 * @author Bartlomiej Polot
25 * @author Christian Grothoff
28 * - optimize stopping/restarting DHT search to situations
29 * where we actually need it (i.e. not if we have a direct connection,
30 * or if we already have plenty of good short ones, or maybe even
31 * to take a break if we have some connections and have searched a lot (?))
34 #include "gnunet_util_lib.h"
35 #include "gnunet_hello_lib.h"
36 #include "gnunet_signatures.h"
37 #include "gnunet_transport_service.h"
38 #include "gnunet_ats_service.h"
39 #include "gnunet_core_service.h"
40 #include "gnunet_statistics_service.h"
41 #include "cadet_protocol.h"
42 #include "gnunet-service-cadet_connection.h"
43 #include "gnunet-service-cadet_dht.h"
44 #include "gnunet-service-cadet_peer.h"
45 #include "gnunet-service-cadet_paths.h"
46 #include "gnunet-service-cadet_tunnels.h"
49 #define LOG(level, ...) GNUNET_log_from(level, "cadet-per", __VA_ARGS__)
53 * How long do we wait until tearing down an idle peer?
55 #define IDLE_PEER_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
58 * How long do we keep paths around if we no longer care about the peer?
60 #define IDLE_PATH_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
63 * Queue size when we start dropping OOO messages.
65 #define MAX_OOO_QUEUE_SIZE 100
69 * Data structure used to track whom we have to notify about changes
70 * to our message queue.
72 struct GCP_MessageQueueManager {
76 struct GCP_MessageQueueManager *next;
81 struct GCP_MessageQueueManager *prev;
84 * Function to call with updated message queue object.
86 GCP_MessageQueueNotificationCallback cb;
94 * The peer this is for.
99 * Envelope this manager would like to transmit once it is its turn.
101 struct GNUNET_MQ_Envelope *env;
106 * Struct containing all information regarding a given peer
112 struct GNUNET_PeerIdentity pid;
115 * Last time we heard from this peer (currently not used!)
117 struct GNUNET_TIME_Absolute last_contactXXX;
120 * Array of DLLs of paths traversing the peer, organized by the
121 * offset of the peer on the larger path.
123 struct CadetPeerPathEntry **path_heads;
126 * Array of DLL of paths traversing the peer, organized by the
127 * offset of the peer on the larger path.
129 struct CadetPeerPathEntry **path_tails;
132 * Notifications to call when @e core_mq changes.
134 struct GCP_MessageQueueManager *mqm_head;
137 * Notifications to call when @e core_mq changes.
139 struct GCP_MessageQueueManager *mqm_tail;
142 * Pointer to first "ready" entry in @e mqm_head.
144 struct GCP_MessageQueueManager *mqm_ready_ptr;
147 * MIN-heap of paths owned by this peer (they also end at this
148 * peer). Ordered by desirability.
150 struct GNUNET_CONTAINER_Heap *path_heap;
153 * Handle to stop the DHT search for paths to this peer
155 struct GCD_search_handle *search_h;
158 * Task to clean up @e path_heap asynchronously.
160 struct GNUNET_SCHEDULER_Task *heap_cleanup_task;
163 * Task to destroy this entry.
165 struct GNUNET_SCHEDULER_Task *destroy_task;
168 * Tunnel to this peer, if any.
170 struct CadetTunnel *t;
173 * Connections that go through this peer; indexed by tid.
175 struct GNUNET_CONTAINER_MultiShortmap *connections;
178 * Handle for core transmissions.
180 struct GNUNET_MQ_Handle *core_mq;
183 * Hello message of the peer.
185 struct GNUNET_HELLO_Message *hello;
188 * Handle to us offering the HELLO to the transport.
190 struct GNUNET_TRANSPORT_OfferHelloHandle *hello_offer;
193 * Handle to our ATS request asking ATS to suggest an address
194 * to TRANSPORT for this peer (to establish a direct link).
196 struct GNUNET_ATS_ConnectivitySuggestHandle *connectivity_suggestion;
199 * How many messages are in the queue to this peer.
201 unsigned int queue_n;
204 * How many paths do we have to this peer (in all @e path_heads DLLs combined).
206 unsigned int num_paths;
209 * Sum over all of the offsets of all of the paths in the @a path_heads DLLs.
210 * Used to speed-up @GCP_get_desirability_of_path() calculation.
212 unsigned int off_sum;
215 * Number of message queue managers of this peer that have a message in waiting.
217 * Used to quickly see if we need to bother scanning the @e msm_head DLL.
218 * TODO: could be replaced by another DLL that would then allow us to avoid
219 * the O(n)-scan of the DLL for ready entries!
221 unsigned int mqm_ready_counter;
224 * Current length of the @e path_heads and @path_tails arrays.
225 * The arrays should be grown as needed.
227 unsigned int path_dll_length;
232 * Get the static string for a peer ID.
235 * @return Static string for it's ID.
238 GCP_2s(const struct CadetPeer *cp)
244 (0 == GNUNET_is_zero(&cp->pid.public_key)))
247 ret = GNUNET_CRYPTO_eddsa_public_key_to_string(&cp->pid.public_key);
260 * Calculate how desirable a path is for @a cp if @a cp
261 * is at offset @a off.
263 * The 'desirability_table.c' program can be used to compute a list of
264 * sample outputs for different scenarios. Basically, we score paths
265 * lower if there are many alternatives, and higher if they are
266 * shorter than average, and very high if they are much shorter than
267 * average and without many alternatives.
269 * @param cp a peer reachable via a path
270 * @param off offset of @a cp in the path
271 * @return score how useful a path is to reach @a cp,
272 * positive scores mean path is more desirable
275 GCP_get_desirability_of_path(struct CadetPeer *cp,
278 unsigned int num_alts = cp->num_paths;
279 unsigned int off_sum;
284 GNUNET_assert(num_alts >= 1); /* 'path' should be in there! */
285 GNUNET_assert(0 != cp->path_dll_length);
287 /* We maintain 'off_sum' in 'peer' and thereby
288 avoid the SLOW recalculation each time. Kept here
289 just to document what is going on. */
292 for (unsigned int j = 0; j < cp->path_dll_length; j++)
293 for (struct CadetPeerPathEntry *pe = cp->path_heads[j];
297 GNUNET_assert(off_sum == cp->off_sum);
299 off_sum = cp->off_sum;
301 avg_sum = off_sum * 1.0 / cp->path_dll_length;
302 path_delta = off - avg_sum;
303 /* path_delta positiv: path off of peer above average (bad path for peer),
304 path_delta negativ: path off of peer below average (good path for peer) */
305 if (path_delta <= -1.0)
306 weight_alts = -num_alts / path_delta; /* discount alternative paths */
307 else if (path_delta >= 1.0)
308 weight_alts = num_alts * path_delta; /* overcount alternative paths */
310 weight_alts = num_alts; /* count alternative paths normally */
313 /* off+1: long paths are generally harder to find and thus count
314 a bit more as they get longer. However, above-average paths
315 still need to count less, hence the squaring of that factor. */
316 return (off + 1.0) / (weight_alts * weight_alts);
321 * This peer is no longer be needed, clean it up now.
323 * @param cls peer to clean up
326 destroy_peer(void *cls)
328 struct CadetPeer *cp = cls;
330 LOG(GNUNET_ERROR_TYPE_DEBUG,
331 "Destroying state about peer %s\n",
333 cp->destroy_task = NULL;
334 GNUNET_assert(NULL == cp->t);
335 GNUNET_assert(NULL == cp->core_mq);
336 GNUNET_assert(0 == cp->num_paths);
337 for (unsigned int i = 0; i < cp->path_dll_length; i++)
338 GNUNET_assert(NULL == cp->path_heads[i]);
339 GNUNET_assert(0 == GNUNET_CONTAINER_multishortmap_size(cp->connections));
340 GNUNET_assert(GNUNET_YES ==
341 GNUNET_CONTAINER_multipeermap_remove(peers,
344 GNUNET_free_non_null(cp->path_heads);
345 GNUNET_free_non_null(cp->path_tails);
346 cp->path_dll_length = 0;
347 if (NULL != cp->search_h)
349 GCD_search_stop(cp->search_h);
352 /* FIXME: clean up search_delayedXXX! */
354 if (NULL != cp->hello_offer)
356 GNUNET_TRANSPORT_offer_hello_cancel(cp->hello_offer);
357 cp->hello_offer = NULL;
359 if (NULL != cp->connectivity_suggestion)
361 GNUNET_ATS_connectivity_suggest_cancel(cp->connectivity_suggestion);
362 cp->connectivity_suggestion = NULL;
364 GNUNET_CONTAINER_multishortmap_destroy(cp->connections);
365 if (NULL != cp->path_heap)
367 GNUNET_CONTAINER_heap_destroy(cp->path_heap);
368 cp->path_heap = NULL;
370 if (NULL != cp->heap_cleanup_task)
372 GNUNET_SCHEDULER_cancel(cp->heap_cleanup_task);
373 cp->heap_cleanup_task = NULL;
375 GNUNET_free_non_null(cp->hello);
376 /* Peer should not be freed if paths exist; if there are no paths,
377 there ought to be no connections, and without connections, no
378 notifications. Thus we can assert that mqm_head is empty at this
380 GNUNET_assert(NULL == cp->mqm_head);
381 GNUNET_assert(NULL == cp->mqm_ready_ptr);
387 * This peer is now on more "active" duty, activate processes related to it.
389 * @param cp the more-active peer
392 consider_peer_activate(struct CadetPeer *cp)
396 LOG(GNUNET_ERROR_TYPE_DEBUG,
397 "Updating peer %s activation state (%u connections)%s%s\n",
399 GNUNET_CONTAINER_multishortmap_size(cp->connections),
400 (NULL == cp->t) ? "" : " with tunnel",
401 (NULL == cp->core_mq) ? "" : " with CORE link");
402 if (NULL != cp->destroy_task)
404 /* It's active, do not destory! */
405 GNUNET_SCHEDULER_cancel(cp->destroy_task);
406 cp->destroy_task = NULL;
408 if ((0 == GNUNET_CONTAINER_multishortmap_size(cp->connections)) &&
411 /* We're just on a path or directly connected; don't bother too much */
412 if (NULL != cp->connectivity_suggestion)
414 GNUNET_ATS_connectivity_suggest_cancel(cp->connectivity_suggestion);
415 cp->connectivity_suggestion = NULL;
417 if (NULL != cp->search_h)
419 GCD_search_stop(cp->search_h);
424 if (NULL == cp->core_mq)
426 /* Lacks direct connection, try to create one by querying the DHT */
427 if ((NULL == cp->search_h) &&
428 (DESIRED_CONNECTIONS_PER_TUNNEL > cp->num_paths))
430 = GCD_search(&cp->pid);
434 /* Have direct connection, stop DHT search if active */
435 if (NULL != cp->search_h)
437 GCD_search_stop(cp->search_h);
442 /* If we have a tunnel, our urge for connections is much bigger */
443 strength = (NULL != cp->t) ? 32 : 1;
444 if (NULL != cp->connectivity_suggestion)
445 GNUNET_ATS_connectivity_suggest_cancel(cp->connectivity_suggestion);
446 cp->connectivity_suggestion
447 = GNUNET_ATS_connectivity_suggest(ats_ch,
454 * This peer may no longer be needed, consider cleaning it up.
456 * @param cp peer to clean up
459 consider_peer_destroy(struct CadetPeer *cp);
463 * We really no longere care about a peer, stop hogging memory with paths to it.
464 * Afterwards, see if there is more to be cleaned up about this peer.
466 * @param cls a `struct CadetPeer`.
469 drop_paths(void *cls)
471 struct CadetPeer *cp = cls;
472 struct CadetPeerPath *path;
474 cp->destroy_task = NULL;
475 while (NULL != (path = GNUNET_CONTAINER_heap_remove_root(cp->path_heap)))
477 consider_peer_destroy(cp);
482 * This peer may no longer be needed, consider cleaning it up.
484 * @param cp peer to clean up
487 consider_peer_destroy(struct CadetPeer *cp)
489 struct GNUNET_TIME_Relative exp;
491 if (NULL != cp->destroy_task)
493 GNUNET_SCHEDULER_cancel(cp->destroy_task);
494 cp->destroy_task = NULL;
497 return; /* still relevant! */
498 if (NULL != cp->core_mq)
499 return; /* still relevant! */
500 if (0 != GNUNET_CONTAINER_multishortmap_size(cp->connections))
501 return; /* still relevant! */
502 if ((NULL != cp->path_heap) &&
503 (0 < GNUNET_CONTAINER_heap_get_size(cp->path_heap)))
505 cp->destroy_task = GNUNET_SCHEDULER_add_delayed(IDLE_PATH_TIMEOUT,
510 if (0 != cp->num_paths)
511 return; /* still relevant! */
512 if (NULL != cp->hello)
514 /* relevant only until HELLO expires */
515 exp = GNUNET_TIME_absolute_get_remaining(GNUNET_HELLO_get_last_expiration(cp->hello));
516 cp->destroy_task = GNUNET_SCHEDULER_add_delayed(exp,
521 cp->destroy_task = GNUNET_SCHEDULER_add_delayed(IDLE_PEER_TIMEOUT,
528 * Set the message queue to @a mq for peer @a cp and notify watchers.
530 * @param cp peer to modify
531 * @param mq message queue to set (can be NULL)
534 GCP_set_mq(struct CadetPeer *cp,
535 struct GNUNET_MQ_Handle *mq)
537 LOG(GNUNET_ERROR_TYPE_DEBUG,
538 "Message queue for peer %s is now %p\n",
542 for (struct GCP_MessageQueueManager *mqm = cp->mqm_head, *next;
546 /* Save next pointer in case mqm gets freed by the callback */
550 if (NULL != mqm->env)
552 GNUNET_MQ_discard(mqm->env);
565 GNUNET_assert(NULL == mqm->env);
572 consider_peer_activate(cp);
574 consider_peer_destroy(cp);
579 /* have a new, direct path to the target, notify tunnel */
580 struct CadetPeerPath *path;
582 path = GCPP_get_path_from_route(1,
584 GCT_consider_path(cp->t,
592 * Debug function should NEVER return true in production code, useful to
593 * simulate losses for testcases.
595 * @return #GNUNET_YES or #GNUNET_NO with the decision to drop.
600 if (0 == drop_percent)
602 if (GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK,
610 * Function called when CORE took one of the messages from
611 * a message queue manager and transmitted it.
613 * @param cls the `struct CadetPeeer` where we made progress
616 mqm_send_done(void *cls);
620 * Transmit current envelope from this @a mqm.
622 * @param mqm mqm to transmit message for now
625 mqm_execute(struct GCP_MessageQueueManager *mqm)
627 struct CadetPeer *cp = mqm->cp;
629 /* Move ready pointer to the next entry that might be ready. */
630 if ((mqm == cp->mqm_ready_ptr) &&
632 cp->mqm_ready_ptr = mqm->next;
633 /* Move entry to the end of the DLL, to be fair. */
634 if (mqm != cp->mqm_tail)
636 GNUNET_CONTAINER_DLL_remove(cp->mqm_head,
639 GNUNET_CONTAINER_DLL_insert_tail(cp->mqm_head,
643 cp->mqm_ready_counter--;
644 if (GNUNET_YES == should_I_drop())
646 LOG(GNUNET_ERROR_TYPE_DEBUG,
647 "DROPPING message to peer %s from MQM %p\n",
650 GNUNET_MQ_discard(mqm->env);
657 const struct GNUNET_MessageHeader *mh;
659 mh = GNUNET_MQ_env_get_msg(mqm->env);
660 switch (ntohs(mh->type))
662 case GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX:
664 const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg
665 = (const struct GNUNET_CADET_TunnelKeyExchangeMessage *)mh;
666 LOG(GNUNET_ERROR_TYPE_DEBUG,
667 "P2P forwarding KX with ephemeral %s to %s on CID %s\n",
668 GNUNET_e2s(&msg->ephemeral_key),
670 GNUNET_sh2s(&msg->cid.connection_of_tunnel));
678 LOG(GNUNET_ERROR_TYPE_DEBUG,
679 "Sending to peer %s from MQM %p\n",
682 GNUNET_MQ_send(cp->core_mq,
692 * Find the next ready message in the queue (starting
693 * the search from the `cp->mqm_ready_ptr`) and if possible
694 * execute the transmission.
696 * @param cp peer to try to send the next ready message to
699 send_next_ready(struct CadetPeer *cp)
701 struct GCP_MessageQueueManager *mqm;
703 if (0 == cp->mqm_ready_counter)
705 while ((NULL != (mqm = cp->mqm_ready_ptr)) &&
707 cp->mqm_ready_ptr = mqm->next;
709 return; /* nothing to do */
715 * Function called when CORE took one of the messages from
716 * a message queue manager and transmitted it.
718 * @param cls the `struct CadetPeeer` where we made progress
721 mqm_send_done(void *cls)
723 struct CadetPeer *cp = cls;
725 LOG(GNUNET_ERROR_TYPE_DEBUG,
726 "Sending to peer %s completed\n",
733 * Send the message in @a env to @a cp.
735 * @param mqm the message queue manager to use for transmission
736 * @param env envelope with the message to send; must NOT
737 * yet have a #GNUNET_MQ_notify_sent() callback attached to it
740 GCP_send(struct GCP_MessageQueueManager *mqm,
741 struct GNUNET_MQ_Envelope *env)
743 struct CadetPeer *cp = mqm->cp;
745 GNUNET_assert(NULL != env);
746 LOG(GNUNET_ERROR_TYPE_DEBUG,
747 "Queueing message to peer %s in MQM %p\n",
750 GNUNET_assert(NULL != cp->core_mq);
751 GNUNET_assert(NULL == mqm->env);
752 GNUNET_MQ_notify_sent(env,
756 cp->mqm_ready_counter++;
757 if (mqm != cp->mqm_ready_ptr)
758 cp->mqm_ready_ptr = cp->mqm_head;
759 if (1 == cp->mqm_ready_counter)
760 cp->mqm_ready_ptr = mqm;
761 if (0 != GNUNET_MQ_get_length(cp->core_mq))
768 * Function called to destroy a peer now.
771 * @param pid identity of the peer (unused)
772 * @param value the `struct CadetPeer` to clean up
773 * @return #GNUNET_OK (continue to iterate)
776 destroy_iterator_cb(void *cls,
777 const struct GNUNET_PeerIdentity *pid,
780 struct CadetPeer *cp = value;
782 if (NULL != cp->destroy_task)
784 GNUNET_SCHEDULER_cancel(cp->destroy_task);
785 cp->destroy_task = NULL;
793 * Clean up all entries about all peers.
794 * Must only be called after all tunnels, CORE-connections and
795 * connections are down.
798 GCP_destroy_all_peers()
800 LOG(GNUNET_ERROR_TYPE_DEBUG,
801 "Destroying all peers now\n");
802 GNUNET_CONTAINER_multipeermap_iterate(peers,
803 &destroy_iterator_cb,
809 * Drop all paths owned by this peer, and do not
810 * allow new ones to be added: We are shutting down.
812 * @param cp peer to drop paths to
815 GCP_drop_owned_paths(struct CadetPeer *cp)
817 struct CadetPeerPath *path;
819 LOG(GNUNET_ERROR_TYPE_DEBUG,
820 "Destroying all paths to %s\n",
822 while (NULL != (path =
823 GNUNET_CONTAINER_heap_remove_root(cp->path_heap)))
825 GNUNET_CONTAINER_heap_destroy(cp->path_heap);
826 cp->path_heap = NULL;
831 * Add an entry to the DLL of all of the paths that this peer is on.
833 * @param cp peer to modify
834 * @param entry an entry on a path
835 * @param off offset of this peer on the path
838 GCP_path_entry_add(struct CadetPeer *cp,
839 struct CadetPeerPathEntry *entry,
842 GNUNET_assert(cp == GCPP_get_peer_at_offset(entry->path,
844 LOG(GNUNET_ERROR_TYPE_DEBUG,
845 "Discovered that peer %s is on path %s at offset %u\n",
847 GCPP_2s(entry->path),
849 if (off >= cp->path_dll_length)
851 unsigned int len = cp->path_dll_length;
853 GNUNET_array_grow(cp->path_heads,
856 GNUNET_array_grow(cp->path_tails,
860 GNUNET_CONTAINER_DLL_insert(cp->path_heads[off],
866 /* If we have a tunnel to this peer, tell the tunnel that there is a
867 new path available. */
869 GCT_consider_path(cp->t,
873 if ((NULL != cp->search_h) &&
874 (DESIRED_CONNECTIONS_PER_TUNNEL <= cp->num_paths))
876 /* Now I have enough paths, stop search */
877 GCD_search_stop(cp->search_h);
880 if (NULL != cp->destroy_task)
882 /* paths changed, this resets the destroy timeout counter
883 and aborts a destroy task that may no longer be valid
884 to have (as we now have more paths via this peer). */
885 consider_peer_destroy(cp);
891 * Remove an entry from the DLL of all of the paths that this peer is on.
893 * @param cp peer to modify
894 * @param entry an entry on a path
895 * @param off offset of this peer on the path
898 GCP_path_entry_remove(struct CadetPeer *cp,
899 struct CadetPeerPathEntry *entry,
902 LOG(GNUNET_ERROR_TYPE_DEBUG,
903 "Removing knowledge about peer %s beging on path %s at offset %u\n",
905 GCPP_2s(entry->path),
907 GNUNET_CONTAINER_DLL_remove(cp->path_heads[off],
910 GNUNET_assert(0 < cp->num_paths);
913 if ((NULL == cp->core_mq) &&
915 (NULL == cp->search_h) &&
916 (DESIRED_CONNECTIONS_PER_TUNNEL > cp->num_paths))
918 = GCD_search(&cp->pid);
919 if (NULL == cp->destroy_task)
921 /* paths changed, we might now be ready for destruction, check again */
922 consider_peer_destroy(cp);
928 * Prune down the number of paths to this peer, we seem to
931 * @param cls the `struct CadetPeer` to maintain the path heap for
934 path_heap_cleanup(void *cls)
936 struct CadetPeer *cp = cls;
937 struct CadetPeerPath *root;
939 cp->heap_cleanup_task = NULL;
940 while (GNUNET_CONTAINER_heap_get_size(cp->path_heap) >=
941 2 * DESIRED_CONNECTIONS_PER_TUNNEL)
943 /* Now we have way too many, drop least desirable UNLESS it is in use!
944 (Note that this intentionally keeps highly desireable, but currently
945 unused paths around in the hope that we might be able to switch, even
946 if the number of paths exceeds the threshold.) */
947 root = GNUNET_CONTAINER_heap_peek(cp->path_heap);
948 GNUNET_assert(NULL != root);
950 GCPP_get_connection(root,
952 GCPP_get_length(root) - 1))
953 break; /* can't fix */
954 /* Got plenty of paths to this destination, and this is a low-quality
955 one that we don't care about. Allow it to die. */
956 GNUNET_assert(root ==
957 GNUNET_CONTAINER_heap_remove_root(cp->path_heap));
964 * Try adding a @a path to this @a peer. If the peer already
965 * has plenty of paths, return NULL.
967 * @param cp peer to which the @a path leads to
968 * @param path a path looking for an owner; may not be fully initialized yet!
969 * @param off offset of @a cp in @a path
970 * @param force force attaching the path
971 * @return NULL if this peer does not care to become a new owner,
972 * otherwise the node in the peer's path heap for the @a path.
974 struct GNUNET_CONTAINER_HeapNode *
975 GCP_attach_path(struct CadetPeer *cp,
976 struct CadetPeerPath *path,
980 GNUNET_CONTAINER_HeapCostType desirability;
981 struct CadetPeerPath *root;
982 GNUNET_CONTAINER_HeapCostType root_desirability;
983 struct GNUNET_CONTAINER_HeapNode *hn;
985 GNUNET_assert(off == GCPP_get_length(path) - 1);
986 GNUNET_assert(cp == GCPP_get_peer_at_offset(path,
988 if (NULL == cp->path_heap)
990 /* #GCP_drop_owned_paths() was already called, we cannot take new ones! */
991 GNUNET_assert(GNUNET_NO == force);
994 desirability = GCPP_get_desirability(path);
995 if (GNUNET_NO == force)
997 /* FIXME: desirability is not yet initialized; tricky! */
999 GNUNET_CONTAINER_heap_peek2(cp->path_heap,
1001 &root_desirability))
1004 root_desirability = 0;
1007 if ((DESIRED_CONNECTIONS_PER_TUNNEL > cp->num_paths) &&
1008 (desirability < root_desirability))
1010 LOG(GNUNET_ERROR_TYPE_DEBUG,
1011 "Decided to not attach path %s to peer %s due to undesirability\n",
1018 LOG(GNUNET_ERROR_TYPE_DEBUG,
1019 "Attaching path %s to peer %s (%s)\n",
1022 (GNUNET_NO == force) ? "desirable" : "forced");
1024 /* Yes, we'd like to add this path, add to our heap */
1025 hn = GNUNET_CONTAINER_heap_insert(cp->path_heap,
1029 /* Consider maybe dropping other paths because of the new one */
1030 if ((GNUNET_CONTAINER_heap_get_size(cp->path_heap) >=
1031 2 * DESIRED_CONNECTIONS_PER_TUNNEL) &&
1032 (NULL != cp->heap_cleanup_task))
1033 cp->heap_cleanup_task = GNUNET_SCHEDULER_add_now(&path_heap_cleanup,
1040 * This peer can no longer own @a path as the path
1041 * has been extended and a peer further down the line
1042 * is now the new owner.
1044 * @param cp old owner of the @a path
1045 * @param path path where the ownership is lost
1046 * @param hn note in @a cp's path heap that must be deleted
1049 GCP_detach_path(struct CadetPeer *cp,
1050 struct CadetPeerPath *path,
1051 struct GNUNET_CONTAINER_HeapNode *hn)
1053 LOG(GNUNET_ERROR_TYPE_DEBUG,
1054 "Detatching path %s from peer %s\n",
1057 GNUNET_assert(path ==
1058 GNUNET_CONTAINER_heap_remove_node(hn));
1063 * Add a @a connection to this @a cp.
1065 * @param cp peer via which the @a connection goes
1066 * @param cc the connection to add
1069 GCP_add_connection(struct CadetPeer *cp,
1070 struct CadetConnection *cc)
1072 LOG(GNUNET_ERROR_TYPE_DEBUG,
1073 "Adding %s to peer %s\n",
1076 GNUNET_assert(GNUNET_OK ==
1077 GNUNET_CONTAINER_multishortmap_put(cp->connections,
1078 &GCC_get_id(cc)->connection_of_tunnel,
1080 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1081 if (NULL != cp->destroy_task)
1083 GNUNET_SCHEDULER_cancel(cp->destroy_task);
1084 cp->destroy_task = NULL;
1090 * Remove a @a connection that went via this @a cp.
1092 * @param cp peer via which the @a connection went
1093 * @param cc the connection to remove
1096 GCP_remove_connection(struct CadetPeer *cp,
1097 struct CadetConnection *cc)
1099 LOG(GNUNET_ERROR_TYPE_DEBUG,
1100 "Removing connection %s from peer %s\n",
1103 GNUNET_assert(GNUNET_YES ==
1104 GNUNET_CONTAINER_multishortmap_remove(cp->connections,
1105 &GCC_get_id(cc)->connection_of_tunnel,
1107 consider_peer_destroy(cp);
1112 * Retrieve the CadetPeer stucture associated with the
1113 * peer. Optionally create one and insert it in the appropriate
1114 * structures if the peer is not known yet.
1116 * @param peer_id Full identity of the peer.
1117 * @param create #GNUNET_YES if a new peer should be created if unknown.
1118 * #GNUNET_NO to return NULL if peer is unknown.
1119 * @return Existing or newly created peer structure.
1120 * NULL if unknown and not requested @a create
1123 GCP_get(const struct GNUNET_PeerIdentity *peer_id,
1126 struct CadetPeer *cp;
1128 cp = GNUNET_CONTAINER_multipeermap_get(peers,
1132 if (GNUNET_NO == create)
1134 cp = GNUNET_new(struct CadetPeer);
1136 cp->connections = GNUNET_CONTAINER_multishortmap_create(32,
1138 cp->path_heap = GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN);
1139 GNUNET_assert(GNUNET_YES ==
1140 GNUNET_CONTAINER_multipeermap_put(peers,
1143 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1144 LOG(GNUNET_ERROR_TYPE_DEBUG,
1145 "Creating peer %s\n",
1152 * Obtain the peer identity for a `struct CadetPeer`.
1154 * @param cp our peer handle
1155 * @return the peer identity
1157 const struct GNUNET_PeerIdentity *
1158 GCP_get_id(struct CadetPeer *cp)
1165 * Iterate over all known peers.
1167 * @param iter Iterator.
1168 * @param cls Closure for @c iter.
1171 GCP_iterate_all(GNUNET_CONTAINER_PeerMapIterator iter,
1174 GNUNET_CONTAINER_multipeermap_iterate(peers,
1181 * Count the number of known paths toward the peer.
1183 * @param cp Peer to get path info.
1184 * @return Number of known paths.
1187 GCP_count_paths(const struct CadetPeer *cp)
1189 return cp->num_paths;
1194 * Iterate over the paths to a peer.
1196 * @param cp Peer to get path info.
1197 * @param callback Function to call for every path.
1198 * @param callback_cls Closure for @a callback.
1199 * @return Number of iterated paths.
1202 GCP_iterate_paths(struct CadetPeer *cp,
1203 GCP_PathIterator callback,
1206 unsigned int ret = 0;
1208 LOG(GNUNET_ERROR_TYPE_DEBUG,
1209 "Iterating over paths to peer %s%s\n",
1211 (NULL == cp->core_mq) ? "" : " including direct link");
1212 if (NULL != cp->core_mq)
1214 /* FIXME: this branch seems to duplicate the
1215 i=0 case below (direct link). Leave out!??? -CG */
1216 struct CadetPeerPath *path;
1218 path = GCPP_get_path_from_route(1,
1222 callback(callback_cls,
1227 for (unsigned int i = 0; i < cp->path_dll_length; i++)
1229 for (struct CadetPeerPathEntry *pe = cp->path_heads[i];
1235 callback(callback_cls,
1245 * Iterate over the paths to a peer without direct link.
1247 * @param cp Peer to get path info.
1248 * @param callback Function to call for every path.
1249 * @param callback_cls Closure for @a callback.
1250 * @return Number of iterated paths.
1253 GCP_iterate_indirect_paths(struct CadetPeer *cp,
1254 GCP_PathIterator callback,
1257 unsigned int ret = 0;
1259 LOG(GNUNET_ERROR_TYPE_DEBUG,
1260 "Iterating over paths to peer %s without direct link\n",
1262 for (unsigned int i = 1; i < cp->path_dll_length; i++)
1264 for (struct CadetPeerPathEntry *pe = cp->path_heads[i];
1270 callback(callback_cls,
1281 * Iterate over the paths to @a cp where
1282 * @a cp is at distance @a dist from us.
1284 * @param cp Peer to get path info.
1285 * @param dist desired distance of @a cp to us on the path
1286 * @param callback Function to call for every path.
1287 * @param callback_cls Closure for @a callback.
1288 * @return Number of iterated paths.
1291 GCP_iterate_paths_at(struct CadetPeer *cp,
1293 GCP_PathIterator callback,
1296 unsigned int ret = 0;
1298 if (dist >= cp->path_dll_length)
1300 LOG(GNUNET_ERROR_TYPE_DEBUG,
1301 "Asked to look for paths at distance %u, but maximum for me is < %u\n",
1303 cp->path_dll_length);
1306 for (struct CadetPeerPathEntry *pe = cp->path_heads[dist];
1311 callback(callback_cls,
1322 * Get the tunnel towards a peer.
1324 * @param cp Peer to get from.
1325 * @param create #GNUNET_YES to create a tunnel if we do not have one
1326 * @return Tunnel towards peer.
1328 struct CadetTunnel *
1329 GCP_get_tunnel(struct CadetPeer *cp,
1334 if ((NULL != cp->t) ||
1335 (GNUNET_NO == create))
1337 cp->t = GCT_create_tunnel(cp);
1338 consider_peer_activate(cp);
1344 * Hello offer was passed to the transport service. Mark it
1347 * @param cls the `struct CadetPeer` where the offer completed
1350 hello_offer_done(void *cls)
1352 struct CadetPeer *cp = cls;
1354 cp->hello_offer = NULL;
1359 * We got a HELLO for a @a peer, remember it, and possibly
1360 * trigger adequate actions (like trying to connect).
1362 * @param cp the peer we got a HELLO for
1363 * @param hello the HELLO to remember
1366 GCP_set_hello(struct CadetPeer *cp,
1367 const struct GNUNET_HELLO_Message *hello)
1369 struct GNUNET_HELLO_Message *mrg;
1371 LOG(GNUNET_ERROR_TYPE_DEBUG,
1372 "Got %u byte HELLO for peer %s\n",
1373 (unsigned int)GNUNET_HELLO_size(hello),
1375 if (NULL != cp->hello_offer)
1377 GNUNET_TRANSPORT_offer_hello_cancel(cp->hello_offer);
1378 cp->hello_offer = NULL;
1380 if (NULL != cp->hello)
1382 mrg = GNUNET_HELLO_merge(hello,
1384 GNUNET_free(cp->hello);
1389 cp->hello = GNUNET_memdup(hello,
1390 GNUNET_HELLO_size(hello));
1393 = GNUNET_TRANSPORT_offer_hello(cfg,
1394 GNUNET_HELLO_get_header(cp->hello),
1397 /* New HELLO means cp's destruction time may change... */
1398 consider_peer_destroy(cp);
1403 * The tunnel to the given peer no longer exists, remove it from our
1404 * data structures, and possibly clean up the peer itself.
1406 * @param cp the peer affected
1407 * @param t the dead tunnel
1410 GCP_drop_tunnel(struct CadetPeer *cp,
1411 struct CadetTunnel *t)
1413 LOG(GNUNET_ERROR_TYPE_DEBUG,
1414 "Dropping tunnel %s to peer %s\n",
1417 GNUNET_assert(cp->t == t);
1419 consider_peer_destroy(cp);
1424 * Test if @a cp has a core-level connection
1426 * @param cp peer to test
1427 * @return #GNUNET_YES if @a cp has a core-level connection
1430 GCP_has_core_connection(struct CadetPeer *cp)
1432 return (NULL != cp->core_mq) ? GNUNET_YES : GNUNET_NO;
1437 * Start message queue change notifications.
1439 * @param cp peer to notify for
1440 * @param cb function to call if mq becomes available or unavailable
1441 * @param cb_cls closure for @a cb
1442 * @return handle to cancel request
1444 struct GCP_MessageQueueManager *
1445 GCP_request_mq(struct CadetPeer *cp,
1446 GCP_MessageQueueNotificationCallback cb,
1449 struct GCP_MessageQueueManager *mqm;
1451 mqm = GNUNET_new(struct GCP_MessageQueueManager);
1453 mqm->cb_cls = cb_cls;
1455 GNUNET_CONTAINER_DLL_insert(cp->mqm_head,
1458 LOG(GNUNET_ERROR_TYPE_DEBUG,
1459 "Creating MQM %p for peer %s\n",
1462 if (NULL != cp->core_mq)
1470 * Stops message queue change notifications.
1472 * @param mqm handle matching request to cancel
1473 * @param last_env final message to transmit, or NULL
1476 GCP_request_mq_cancel(struct GCP_MessageQueueManager *mqm,
1477 struct GNUNET_MQ_Envelope *last_env)
1479 struct CadetPeer *cp = mqm->cp;
1481 LOG(GNUNET_ERROR_TYPE_DEBUG,
1482 "Destroying MQM %p for peer %s%s\n",
1485 (NULL == last_env) ? "" : " with last ditch transmission");
1486 if (NULL != mqm->env)
1487 GNUNET_MQ_discard(mqm->env);
1488 if (NULL != last_env)
1490 if (NULL != cp->core_mq)
1492 GNUNET_MQ_notify_sent(last_env,
1495 GNUNET_MQ_send(cp->core_mq,
1500 GNUNET_MQ_discard(last_env);
1503 if (cp->mqm_ready_ptr == mqm)
1504 cp->mqm_ready_ptr = mqm->next;
1505 GNUNET_CONTAINER_DLL_remove(cp->mqm_head,
1513 * Send the message in @a env to @a cp, overriding queueing logic.
1514 * This function should only be used to send error messages outside
1515 * of flow and congestion control, similar to ICMP. Note that
1516 * the envelope may be silently discarded as well.
1518 * @param cp peer to send the message to
1519 * @param env envelope with the message to send
1522 GCP_send_ooo(struct CadetPeer *cp,
1523 struct GNUNET_MQ_Envelope *env)
1525 LOG(GNUNET_ERROR_TYPE_DEBUG,
1526 "Sending message to %s out of management\n",
1528 if (NULL == cp->core_mq)
1530 GNUNET_MQ_discard(env);
1533 if (GNUNET_MQ_get_length(cp->core_mq) > MAX_OOO_QUEUE_SIZE)
1535 GNUNET_MQ_discard(env);
1538 GNUNET_MQ_notify_sent(env,
1541 GNUNET_MQ_send(cp->core_mq,
1548 /* end of gnunet-service-cadet-new_peer.c */