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
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.
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)
66 * Data structure used to track whom we have to notify about changes
67 * to our message queue.
69 struct GCP_MessageQueueManager
75 struct GCP_MessageQueueManager *next;
80 struct GCP_MessageQueueManager *prev;
83 * Function to call with updated message queue object.
85 GCP_MessageQueueNotificationCallback cb;
93 * The peer this is for.
98 * Envelope this manager would like to transmit once it is its turn.
100 struct GNUNET_MQ_Envelope *env;
106 * Struct containing all information regarding a given peer
113 struct GNUNET_PeerIdentity pid;
116 * Last time we heard from this peer (currently not used!)
118 struct GNUNET_TIME_Absolute last_contactXXX;
121 * Array of DLLs of paths traversing the peer, organized by the
122 * offset of the peer on the larger path.
124 struct CadetPeerPathEntry **path_heads;
127 * Array of DLL of paths traversing the peer, organized by the
128 * offset of the peer on the larger path.
130 struct CadetPeerPathEntry **path_tails;
133 * Notifications to call when @e core_mq changes.
135 struct GCP_MessageQueueManager *mqm_head;
138 * Notifications to call when @e core_mq changes.
140 struct GCP_MessageQueueManager *mqm_tail;
143 * Pointer to first "ready" entry in @e mqm_head.
145 struct GCP_MessageQueueManager *mqm_ready_ptr;
148 * MIN-heap of paths owned by this peer (they also end at this
149 * peer). Ordered by desirability.
151 struct GNUNET_CONTAINER_Heap *path_heap;
154 * Handle to stop the DHT search for paths to this peer
156 struct GCD_search_handle *search_h;
159 * Task to clean up @e path_heap asynchronously.
161 struct GNUNET_SCHEDULER_Task *heap_cleanup_task;
164 * Task to destroy this entry.
166 struct GNUNET_SCHEDULER_Task *destroy_task;
169 * Tunnel to this peer, if any.
171 struct CadetTunnel *t;
174 * Connections that go through this peer; indexed by tid.
176 struct GNUNET_CONTAINER_MultiShortmap *connections;
179 * Handle for core transmissions.
181 struct GNUNET_MQ_Handle *core_mq;
184 * Hello message of the peer.
186 struct GNUNET_HELLO_Message *hello;
189 * Handle to us offering the HELLO to the transport.
191 struct GNUNET_TRANSPORT_OfferHelloHandle *hello_offer;
194 * Handle to our ATS request asking ATS to suggest an address
195 * to TRANSPORT for this peer (to establish a direct link).
197 struct GNUNET_ATS_ConnectivitySuggestHandle *connectivity_suggestion;
200 * How many messages are in the queue to this peer.
202 unsigned int queue_n;
205 * How many paths do we have to this peer (in all @e path_heads DLLs combined).
207 unsigned int num_paths;
210 * Sum over all of the offsets of all of the paths in the @a path_heads DLLs.
211 * Used to speed-up @GCP_get_desirability_of_path() calculation.
213 unsigned int off_sum;
216 * Number of message queue managers of this peer that have a message in waiting.
218 * Used to quickly see if we need to bother scanning the @e msm_head DLL.
219 * TODO: could be replaced by another DLL that would then allow us to avoid
220 * the O(n)-scan of the DLL for ready entries!
222 unsigned int mqm_ready_counter;
225 * Current length of the @e path_heads and @path_tails arrays.
226 * The arrays should be grown as needed.
228 unsigned int path_dll_length;
234 * Get the static string for a peer ID.
237 * @return Static string for it's ID.
240 GCP_2s (const struct CadetPeer *cp)
244 GNUNET_snprintf (buf,
247 GNUNET_i2s (&cp->pid));
253 * Calculate how desirable a path is for @a cp if @a cp
254 * is at offset @a off.
256 * The 'desirability_table.c' program can be used to compute a list of
257 * sample outputs for different scenarios. Basically, we score paths
258 * lower if there are many alternatives, and higher if they are
259 * shorter than average, and very high if they are much shorter than
260 * average and without many alternatives.
262 * @param cp a peer reachable via a path
263 * @param off offset of @a cp in the path
264 * @return score how useful a path is to reach @a cp,
265 * positive scores mean path is more desirable
268 GCP_get_desirability_of_path (struct CadetPeer *cp,
271 unsigned int num_alts = cp->num_paths;
272 unsigned int off_sum;
277 GNUNET_assert (num_alts >= 1); /* 'path' should be in there! */
278 GNUNET_assert (0 != cp->path_dll_length);
280 /* We maintain 'off_sum' in 'peer' and thereby
281 avoid the SLOW recalculation each time. Kept here
282 just to document what is going on. */
285 for (unsigned int j=0;j<cp->path_dll_length;j++)
286 for (struct CadetPeerPathEntry *pe = cp->path_heads[j];
290 GNUNET_assert (off_sum == cp->off_sum);
292 off_sum = cp->off_sum;
294 avg_sum = off_sum * 1.0 / cp->path_dll_length;
295 path_delta = off - avg_sum;
296 /* path_delta positiv: path off of peer above average (bad path for peer),
297 path_delta negativ: path off of peer below average (good path for peer) */
298 if (path_delta <= - 1.0)
299 weight_alts = - num_alts / path_delta; /* discount alternative paths */
300 else if (path_delta >= 1.0)
301 weight_alts = num_alts * path_delta; /* overcount alternative paths */
303 weight_alts = num_alts; /* count alternative paths normally */
306 /* off+1: long paths are generally harder to find and thus count
307 a bit more as they get longer. However, above-average paths
308 still need to count less, hence the squaring of that factor. */
309 return (off + 1.0) / (weight_alts * weight_alts);
314 * This peer is no longer be needed, clean it up now.
316 * @param cls peer to clean up
319 destroy_peer (void *cls)
321 struct CadetPeer *cp = cls;
323 LOG (GNUNET_ERROR_TYPE_DEBUG,
324 "Destroying state about peer %s\n",
326 cp->destroy_task = NULL;
327 GNUNET_assert (NULL == cp->t);
328 GNUNET_assert (NULL == cp->core_mq);
329 GNUNET_assert (0 == cp->num_paths);
330 for (unsigned int i=0;i<cp->path_dll_length;i++)
331 GNUNET_assert (NULL == cp->path_heads[i]);
332 GNUNET_assert (0 == GNUNET_CONTAINER_multishortmap_size (cp->connections));
333 GNUNET_assert (GNUNET_YES ==
334 GNUNET_CONTAINER_multipeermap_remove (peers,
337 GNUNET_free_non_null (cp->path_heads);
338 GNUNET_free_non_null (cp->path_tails);
339 cp->path_dll_length = 0;
340 if (NULL != cp->search_h)
342 GCD_search_stop (cp->search_h);
345 /* FIXME: clean up search_delayedXXX! */
347 if (NULL != cp->hello_offer)
349 GNUNET_TRANSPORT_offer_hello_cancel (cp->hello_offer);
350 cp->hello_offer = NULL;
352 if (NULL != cp->connectivity_suggestion)
354 GNUNET_ATS_connectivity_suggest_cancel (cp->connectivity_suggestion);
355 cp->connectivity_suggestion = NULL;
357 GNUNET_CONTAINER_multishortmap_destroy (cp->connections);
358 if (NULL != cp->path_heap)
360 GNUNET_CONTAINER_heap_destroy (cp->path_heap);
361 cp->path_heap = NULL;
363 if (NULL != cp->heap_cleanup_task)
365 GNUNET_SCHEDULER_cancel (cp->heap_cleanup_task);
366 cp->heap_cleanup_task = NULL;
368 GNUNET_free_non_null (cp->hello);
369 /* Peer should not be freed if paths exist; if there are no paths,
370 there ought to be no connections, and without connections, no
371 notifications. Thus we can assert that mqm_head is empty at this
373 GNUNET_assert (NULL == cp->mqm_head);
374 GNUNET_assert (NULL == cp->mqm_ready_ptr);
380 * This peer is now on more "active" duty, activate processes related to it.
382 * @param cp the more-active peer
385 consider_peer_activate (struct CadetPeer *cp)
389 LOG (GNUNET_ERROR_TYPE_DEBUG,
390 "Updating peer %s activation state (%u connections)%s%s\n",
392 GNUNET_CONTAINER_multishortmap_size (cp->connections),
393 (NULL == cp->t) ? "" : " with tunnel",
394 (NULL == cp->core_mq) ? "" : " with CORE link");
395 if (NULL != cp->destroy_task)
397 /* It's active, do not destory! */
398 GNUNET_SCHEDULER_cancel (cp->destroy_task);
399 cp->destroy_task = NULL;
401 if ( (0 == GNUNET_CONTAINER_multishortmap_size (cp->connections)) &&
404 /* We're just on a path or directly connected; don't bother too much */
405 if (NULL != cp->connectivity_suggestion)
407 GNUNET_ATS_connectivity_suggest_cancel (cp->connectivity_suggestion);
408 cp->connectivity_suggestion = NULL;
410 if (NULL != cp->search_h)
412 GCD_search_stop (cp->search_h);
417 if (NULL == cp->core_mq)
419 /* Lacks direct connection, try to create one by querying the DHT */
420 if ( (NULL == cp->search_h) &&
421 (DESIRED_CONNECTIONS_PER_TUNNEL > cp->num_paths) )
423 = GCD_search (&cp->pid);
427 /* Have direct connection, stop DHT search if active */
428 if (NULL != cp->search_h)
430 GCD_search_stop (cp->search_h);
435 /* If we have a tunnel, our urge for connections is much bigger */
436 strength = (NULL != cp->t) ? 32 : 1;
437 if (NULL != cp->connectivity_suggestion)
438 GNUNET_ATS_connectivity_suggest_cancel (cp->connectivity_suggestion);
439 cp->connectivity_suggestion
440 = GNUNET_ATS_connectivity_suggest (ats_ch,
447 * This peer may no longer be needed, consider cleaning it up.
449 * @param cp peer to clean up
452 consider_peer_destroy (struct CadetPeer *cp);
456 * We really no longere care about a peer, stop hogging memory with paths to it.
457 * Afterwards, see if there is more to be cleaned up about this peer.
459 * @param cls a `struct CadetPeer`.
462 drop_paths (void *cls)
464 struct CadetPeer *cp = cls;
465 struct CadetPeerPath *path;
467 cp->destroy_task = NULL;
468 while (NULL != (path = GNUNET_CONTAINER_heap_remove_root (cp->path_heap)))
470 consider_peer_destroy (cp);
475 * This peer may no longer be needed, consider cleaning it up.
477 * @param cp peer to clean up
480 consider_peer_destroy (struct CadetPeer *cp)
482 struct GNUNET_TIME_Relative exp;
484 if (NULL != cp->destroy_task)
486 GNUNET_SCHEDULER_cancel (cp->destroy_task);
487 cp->destroy_task = NULL;
490 return; /* still relevant! */
491 if (NULL != cp->core_mq)
492 return; /* still relevant! */
493 if (0 != GNUNET_CONTAINER_multishortmap_size (cp->connections))
494 return; /* still relevant! */
495 if ( (NULL != cp->path_heap) &&
496 (0 < GNUNET_CONTAINER_heap_get_size (cp->path_heap)) )
498 cp->destroy_task = GNUNET_SCHEDULER_add_delayed (IDLE_PATH_TIMEOUT,
503 if (0 != cp->num_paths)
504 return; /* still relevant! */
505 if (NULL != cp->hello)
507 /* relevant only until HELLO expires */
508 exp = GNUNET_TIME_absolute_get_remaining (GNUNET_HELLO_get_last_expiration (cp->hello));
509 cp->destroy_task = GNUNET_SCHEDULER_add_delayed (exp,
514 cp->destroy_task = GNUNET_SCHEDULER_add_delayed (IDLE_PEER_TIMEOUT,
521 * Set the message queue to @a mq for peer @a cp and notify watchers.
523 * @param cp peer to modify
524 * @param mq message queue to set (can be NULL)
527 GCP_set_mq (struct CadetPeer *cp,
528 struct GNUNET_MQ_Handle *mq)
530 LOG (GNUNET_ERROR_TYPE_DEBUG,
531 "Message queue for peer %s is now %p\n",
535 for (struct GCP_MessageQueueManager *mqm = cp->mqm_head, *next;
539 /* Save next pointer in case mqm gets freed by the callback */
543 if (NULL != mqm->env)
545 GNUNET_MQ_discard (mqm->env);
547 mqm->cb (mqm->cb_cls,
552 mqm->cb (mqm->cb_cls,
558 GNUNET_assert (NULL == mqm->env);
559 mqm->cb (mqm->cb_cls,
565 consider_peer_activate (cp);
567 consider_peer_destroy (cp);
572 /* have a new, direct path to the target, notify tunnel */
573 struct CadetPeerPath *path;
575 path = GCPP_get_path_from_route (1,
577 GCT_consider_path (cp->t,
585 * Debug function should NEVER return true in production code, useful to
586 * simulate losses for testcases.
588 * @return #GNUNET_YES or #GNUNET_NO with the decision to drop.
593 if (0 == drop_percent)
595 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
603 * Function called when CORE took one of the messages from
604 * a message queue manager and transmitted it.
606 * @param cls the `struct CadetPeeer` where we made progress
609 mqm_send_done (void *cls);
613 * Transmit current envelope from this @a mqm.
615 * @param mqm mqm to transmit message for now
618 mqm_execute (struct GCP_MessageQueueManager *mqm)
620 struct CadetPeer *cp = mqm->cp;
622 /* Move ready pointer to the next entry that might be ready. */
623 if ( (mqm == cp->mqm_ready_ptr) &&
624 (NULL != mqm->next) )
625 cp->mqm_ready_ptr = mqm->next;
626 /* Move entry to the end of the DLL, to be fair. */
627 if (mqm != cp->mqm_tail)
629 GNUNET_CONTAINER_DLL_remove (cp->mqm_head,
632 GNUNET_CONTAINER_DLL_insert_tail (cp->mqm_head,
636 cp->mqm_ready_counter--;
637 if (GNUNET_YES == should_I_drop ())
639 LOG (GNUNET_ERROR_TYPE_DEBUG,
640 "DROPPING message to peer %s from MQM %p\n",
643 GNUNET_MQ_discard (mqm->env);
649 LOG (GNUNET_ERROR_TYPE_DEBUG,
650 "Sending to peer %s from MQM %p\n",
653 GNUNET_MQ_send (cp->core_mq,
657 mqm->cb (mqm->cb_cls,
663 * Find the next ready message in the queue (starting
664 * the search from the `cp->mqm_ready_ptr`) and if possible
665 * execute the transmission.
667 * @param cp peer to try to send the next ready message to
670 send_next_ready (struct CadetPeer *cp)
672 struct GCP_MessageQueueManager *mqm;
674 if (0 == cp->mqm_ready_counter)
676 while ( (NULL != (mqm = cp->mqm_ready_ptr)) &&
678 cp->mqm_ready_ptr = mqm->next;
680 return; /* nothing to do */
686 * Function called when CORE took one of the messages from
687 * a message queue manager and transmitted it.
689 * @param cls the `struct CadetPeeer` where we made progress
692 mqm_send_done (void *cls)
694 struct CadetPeer *cp = cls;
696 LOG (GNUNET_ERROR_TYPE_DEBUG,
697 "Sending to peer %s completed\n",
699 send_next_ready (cp);
704 * Send the message in @a env to @a cp.
706 * @param mqm the message queue manager to use for transmission
707 * @param env envelope with the message to send; must NOT
708 * yet have a #GNUNET_MQ_notify_sent() callback attached to it
711 GCP_send (struct GCP_MessageQueueManager *mqm,
712 struct GNUNET_MQ_Envelope *env)
714 struct CadetPeer *cp = mqm->cp;
716 GNUNET_assert (NULL != env);
717 LOG (GNUNET_ERROR_TYPE_DEBUG,
718 "Queueing message to peer %s in MQM %p\n",
721 GNUNET_assert (NULL != cp->core_mq);
722 GNUNET_assert (NULL == mqm->env);
723 GNUNET_MQ_notify_sent (env,
727 cp->mqm_ready_counter++;
728 if (mqm != cp->mqm_ready_ptr)
729 cp->mqm_ready_ptr = cp->mqm_head;
730 if (1 == cp->mqm_ready_counter)
731 cp->mqm_ready_ptr = mqm;
732 if (0 != GNUNET_MQ_get_length (cp->core_mq))
734 send_next_ready (cp);
739 * Function called to destroy a peer now.
742 * @param pid identity of the peer (unused)
743 * @param value the `struct CadetPeer` to clean up
744 * @return #GNUNET_OK (continue to iterate)
747 destroy_iterator_cb (void *cls,
748 const struct GNUNET_PeerIdentity *pid,
751 struct CadetPeer *cp = value;
753 if (NULL != cp->destroy_task)
755 GNUNET_SCHEDULER_cancel (cp->destroy_task);
756 cp->destroy_task = NULL;
764 * Clean up all entries about all peers.
765 * Must only be called after all tunnels, CORE-connections and
766 * connections are down.
769 GCP_destroy_all_peers ()
771 LOG (GNUNET_ERROR_TYPE_DEBUG,
772 "Destroying all peers now\n");
773 GNUNET_CONTAINER_multipeermap_iterate (peers,
774 &destroy_iterator_cb,
780 * Drop all paths owned by this peer, and do not
781 * allow new ones to be added: We are shutting down.
783 * @param cp peer to drop paths to
786 GCP_drop_owned_paths (struct CadetPeer *cp)
788 struct CadetPeerPath *path;
790 LOG (GNUNET_ERROR_TYPE_DEBUG,
791 "Destroying all paths to %s\n",
793 while (NULL != (path =
794 GNUNET_CONTAINER_heap_remove_root (cp->path_heap)))
796 GNUNET_CONTAINER_heap_destroy (cp->path_heap);
797 cp->path_heap = NULL;
802 * Add an entry to the DLL of all of the paths that this peer is on.
804 * @param cp peer to modify
805 * @param entry an entry on a path
806 * @param off offset of this peer on the path
809 GCP_path_entry_add (struct CadetPeer *cp,
810 struct CadetPeerPathEntry *entry,
813 GNUNET_assert (cp == GCPP_get_peer_at_offset (entry->path,
815 LOG (GNUNET_ERROR_TYPE_DEBUG,
816 "Discovered that peer %s is on path %s at offset %u\n",
818 GCPP_2s (entry->path),
820 if (off >= cp->path_dll_length)
822 unsigned int len = cp->path_dll_length;
824 GNUNET_array_grow (cp->path_heads,
827 GNUNET_array_grow (cp->path_tails,
831 GNUNET_CONTAINER_DLL_insert (cp->path_heads[off],
837 /* If we have a tunnel to this peer, tell the tunnel that there is a
838 new path available. */
840 GCT_consider_path (cp->t,
844 if ( (NULL != cp->search_h) &&
845 (DESIRED_CONNECTIONS_PER_TUNNEL <= cp->num_paths) )
847 /* Now I have enough paths, stop search */
848 GCD_search_stop (cp->search_h);
851 if (NULL != cp->destroy_task)
853 /* paths changed, this resets the destroy timeout counter
854 and aborts a destroy task that may no longer be valid
855 to have (as we now have more paths via this peer). */
856 consider_peer_destroy (cp);
862 * Remove an entry from the DLL of all of the paths that this peer is on.
864 * @param cp peer to modify
865 * @param entry an entry on a path
866 * @param off offset of this peer on the path
869 GCP_path_entry_remove (struct CadetPeer *cp,
870 struct CadetPeerPathEntry *entry,
873 LOG (GNUNET_ERROR_TYPE_DEBUG,
874 "Removing knowledge about peer %s beging on path %s at offset %u\n",
876 GCPP_2s (entry->path),
878 GNUNET_CONTAINER_DLL_remove (cp->path_heads[off],
881 GNUNET_assert (0 < cp->num_paths);
884 if ( (NULL == cp->core_mq) &&
886 (NULL == cp->search_h) &&
887 (DESIRED_CONNECTIONS_PER_TUNNEL > cp->num_paths) )
889 = GCD_search (&cp->pid);
890 if (NULL == cp->destroy_task)
892 /* paths changed, we might now be ready for destruction, check again */
893 consider_peer_destroy (cp);
899 * Prune down the number of paths to this peer, we seem to
902 * @param cls the `struct CadetPeer` to maintain the path heap for
905 path_heap_cleanup (void *cls)
907 struct CadetPeer *cp = cls;
908 struct CadetPeerPath *root;
910 cp->heap_cleanup_task = NULL;
911 while (GNUNET_CONTAINER_heap_get_size (cp->path_heap) >=
912 2 * DESIRED_CONNECTIONS_PER_TUNNEL)
914 /* Now we have way too many, drop least desirable UNLESS it is in use!
915 (Note that this intentionally keeps highly desireable, but currently
916 unused paths around in the hope that we might be able to switch, even
917 if the number of paths exceeds the threshold.) */
918 root = GNUNET_CONTAINER_heap_peek (cp->path_heap);
919 GNUNET_assert (NULL != root);
921 GCPP_get_connection (root,
923 GCPP_get_length (root) - 1))
924 break; /* can't fix */
925 /* Got plenty of paths to this destination, and this is a low-quality
926 one that we don't care about. Allow it to die. */
927 GNUNET_assert (root ==
928 GNUNET_CONTAINER_heap_remove_root (cp->path_heap));
935 * Try adding a @a path to this @a peer. If the peer already
936 * has plenty of paths, return NULL.
938 * @param cp peer to which the @a path leads to
939 * @param path a path looking for an owner; may not be fully initialized yet!
940 * @param off offset of @a cp in @a path
941 * @param force force attaching the path
942 * @return NULL if this peer does not care to become a new owner,
943 * otherwise the node in the peer's path heap for the @a path.
945 struct GNUNET_CONTAINER_HeapNode *
946 GCP_attach_path (struct CadetPeer *cp,
947 struct CadetPeerPath *path,
951 GNUNET_CONTAINER_HeapCostType desirability;
952 struct CadetPeerPath *root;
953 GNUNET_CONTAINER_HeapCostType root_desirability;
954 struct GNUNET_CONTAINER_HeapNode *hn;
956 GNUNET_assert (off == GCPP_get_length (path) - 1);
957 GNUNET_assert (cp == GCPP_get_peer_at_offset (path,
959 if (NULL == cp->path_heap)
961 /* #GCP_drop_owned_paths() was already called, we cannot take new ones! */
962 GNUNET_assert (GNUNET_NO == force);
965 desirability = GCPP_get_desirability (path);
966 if (GNUNET_NO == force)
968 /* FIXME: desirability is not yet initialized; tricky! */
970 GNUNET_CONTAINER_heap_peek2 (cp->path_heap,
975 root_desirability = 0;
978 if ( (DESIRED_CONNECTIONS_PER_TUNNEL > cp->num_paths) &&
979 (desirability < root_desirability) )
981 LOG (GNUNET_ERROR_TYPE_DEBUG,
982 "Decided to not attach path %s to peer %s due to undesirability\n",
989 LOG (GNUNET_ERROR_TYPE_DEBUG,
990 "Attaching path %s to peer %s (%s)\n",
993 (GNUNET_NO == force) ? "desirable" : "forced");
995 /* Yes, we'd like to add this path, add to our heap */
996 hn = GNUNET_CONTAINER_heap_insert (cp->path_heap,
1000 /* Consider maybe dropping other paths because of the new one */
1001 if ( (GNUNET_CONTAINER_heap_get_size (cp->path_heap) >=
1002 2 * DESIRED_CONNECTIONS_PER_TUNNEL) &&
1003 (NULL != cp->heap_cleanup_task) )
1004 cp->heap_cleanup_task = GNUNET_SCHEDULER_add_now (&path_heap_cleanup,
1011 * This peer can no longer own @a path as the path
1012 * has been extended and a peer further down the line
1013 * is now the new owner.
1015 * @param cp old owner of the @a path
1016 * @param path path where the ownership is lost
1017 * @param hn note in @a cp's path heap that must be deleted
1020 GCP_detach_path (struct CadetPeer *cp,
1021 struct CadetPeerPath *path,
1022 struct GNUNET_CONTAINER_HeapNode *hn)
1024 LOG (GNUNET_ERROR_TYPE_DEBUG,
1025 "Detatching path %s from peer %s\n",
1028 GNUNET_assert (path ==
1029 GNUNET_CONTAINER_heap_remove_node (hn));
1034 * Add a @a connection to this @a cp.
1036 * @param cp peer via which the @a connection goes
1037 * @param cc the connection to add
1040 GCP_add_connection (struct CadetPeer *cp,
1041 struct CadetConnection *cc)
1043 LOG (GNUNET_ERROR_TYPE_DEBUG,
1044 "Adding connection %s to peer %s\n",
1047 GNUNET_assert (GNUNET_OK ==
1048 GNUNET_CONTAINER_multishortmap_put (cp->connections,
1049 &GCC_get_id (cc)->connection_of_tunnel,
1051 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1052 if (NULL != cp->destroy_task)
1054 GNUNET_SCHEDULER_cancel (cp->destroy_task);
1055 cp->destroy_task = NULL;
1061 * Remove a @a connection that went via this @a cp.
1063 * @param cp peer via which the @a connection went
1064 * @param cc the connection to remove
1067 GCP_remove_connection (struct CadetPeer *cp,
1068 struct CadetConnection *cc)
1070 LOG (GNUNET_ERROR_TYPE_DEBUG,
1071 "Removing connection %s from peer %s\n",
1074 GNUNET_assert (GNUNET_YES ==
1075 GNUNET_CONTAINER_multishortmap_remove (cp->connections,
1076 &GCC_get_id (cc)->connection_of_tunnel,
1078 consider_peer_destroy (cp);
1083 * Retrieve the CadetPeer stucture associated with the
1084 * peer. Optionally create one and insert it in the appropriate
1085 * structures if the peer is not known yet.
1087 * @param peer_id Full identity of the peer.
1088 * @param create #GNUNET_YES if a new peer should be created if unknown.
1089 * #GNUNET_NO to return NULL if peer is unknown.
1090 * @return Existing or newly created peer structure.
1091 * NULL if unknown and not requested @a create
1094 GCP_get (const struct GNUNET_PeerIdentity *peer_id,
1097 struct CadetPeer *cp;
1099 cp = GNUNET_CONTAINER_multipeermap_get (peers,
1103 if (GNUNET_NO == create)
1105 cp = GNUNET_new (struct CadetPeer);
1107 cp->connections = GNUNET_CONTAINER_multishortmap_create (32,
1109 cp->path_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1110 GNUNET_assert (GNUNET_YES ==
1111 GNUNET_CONTAINER_multipeermap_put (peers,
1114 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1115 LOG (GNUNET_ERROR_TYPE_DEBUG,
1116 "Creating peer %s\n",
1123 * Obtain the peer identity for a `struct CadetPeer`.
1125 * @param cp our peer handle
1126 * @return the peer identity
1128 const struct GNUNET_PeerIdentity *
1129 GCP_get_id (struct CadetPeer *cp)
1136 * Iterate over all known peers.
1138 * @param iter Iterator.
1139 * @param cls Closure for @c iter.
1142 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter,
1145 GNUNET_CONTAINER_multipeermap_iterate (peers,
1152 * Count the number of known paths toward the peer.
1154 * @param cp Peer to get path info.
1155 * @return Number of known paths.
1158 GCP_count_paths (const struct CadetPeer *cp)
1160 return cp->num_paths;
1165 * Iterate over the paths to a peer.
1167 * @param cp Peer to get path info.
1168 * @param callback Function to call for every path.
1169 * @param callback_cls Closure for @a callback.
1170 * @return Number of iterated paths.
1173 GCP_iterate_paths (struct CadetPeer *cp,
1174 GCP_PathIterator callback,
1177 unsigned int ret = 0;
1179 LOG (GNUNET_ERROR_TYPE_DEBUG,
1180 "Iterating over paths to peer %s%s\n",
1182 (NULL == cp->core_mq) ? "" : " including direct link");
1183 if (NULL != cp->core_mq)
1185 struct CadetPeerPath *path;
1187 path = GCPP_get_path_from_route (1,
1191 callback (callback_cls,
1196 for (unsigned int i=0;i<cp->path_dll_length;i++)
1198 for (struct CadetPeerPathEntry *pe = cp->path_heads[i];
1204 callback (callback_cls,
1215 * Iterate over the paths to @a cp where
1216 * @a cp is at distance @a dist from us.
1218 * @param cp Peer to get path info.
1219 * @param dist desired distance of @a cp to us on the path
1220 * @param callback Function to call for every path.
1221 * @param callback_cls Closure for @a callback.
1222 * @return Number of iterated paths.
1225 GCP_iterate_paths_at (struct CadetPeer *cp,
1227 GCP_PathIterator callback,
1230 unsigned int ret = 0;
1232 if (dist >= cp->path_dll_length)
1234 LOG (GNUNET_ERROR_TYPE_DEBUG,
1235 "Asked to look for paths at distance %u, but maximum for me is < %u\n",
1237 cp->path_dll_length);
1240 for (struct CadetPeerPathEntry *pe = cp->path_heads[dist];
1245 callback (callback_cls,
1256 * Get the tunnel towards a peer.
1258 * @param cp Peer to get from.
1259 * @param create #GNUNET_YES to create a tunnel if we do not have one
1260 * @return Tunnel towards peer.
1262 struct CadetTunnel *
1263 GCP_get_tunnel (struct CadetPeer *cp,
1268 if ( (NULL != cp->t) ||
1269 (GNUNET_NO == create) )
1271 cp->t = GCT_create_tunnel (cp);
1272 consider_peer_activate (cp);
1278 * Hello offer was passed to the transport service. Mark it
1281 * @param cls the `struct CadetPeer` where the offer completed
1284 hello_offer_done (void *cls)
1286 struct CadetPeer *cp = cls;
1288 cp->hello_offer = NULL;
1293 * We got a HELLO for a @a peer, remember it, and possibly
1294 * trigger adequate actions (like trying to connect).
1296 * @param cp the peer we got a HELLO for
1297 * @param hello the HELLO to remember
1300 GCP_set_hello (struct CadetPeer *cp,
1301 const struct GNUNET_HELLO_Message *hello)
1303 struct GNUNET_HELLO_Message *mrg;
1305 LOG (GNUNET_ERROR_TYPE_DEBUG,
1306 "Got %u byte HELLO for peer %s\n",
1307 (unsigned int) GNUNET_HELLO_size (hello),
1309 if (NULL != cp->hello_offer)
1311 GNUNET_TRANSPORT_offer_hello_cancel (cp->hello_offer);
1312 cp->hello_offer = NULL;
1314 if (NULL != cp->hello)
1316 mrg = GNUNET_HELLO_merge (hello,
1318 GNUNET_free (cp->hello);
1323 cp->hello = GNUNET_memdup (hello,
1324 GNUNET_HELLO_size (hello));
1327 = GNUNET_TRANSPORT_offer_hello (cfg,
1328 GNUNET_HELLO_get_header (cp->hello) ,
1331 /* New HELLO means cp's destruction time may change... */
1332 consider_peer_destroy (cp);
1337 * The tunnel to the given peer no longer exists, remove it from our
1338 * data structures, and possibly clean up the peer itself.
1340 * @param cp the peer affected
1341 * @param t the dead tunnel
1344 GCP_drop_tunnel (struct CadetPeer *cp,
1345 struct CadetTunnel *t)
1347 LOG (GNUNET_ERROR_TYPE_DEBUG,
1348 "Dropping tunnel %s to peer %s\n",
1351 GNUNET_assert (cp->t == t);
1353 consider_peer_destroy (cp);
1358 * Test if @a cp has a core-level connection
1360 * @param cp peer to test
1361 * @return #GNUNET_YES if @a cp has a core-level connection
1364 GCP_has_core_connection (struct CadetPeer *cp)
1366 return (NULL != cp->core_mq) ? GNUNET_YES : GNUNET_NO;
1371 * Start message queue change notifications.
1373 * @param cp peer to notify for
1374 * @param cb function to call if mq becomes available or unavailable
1375 * @param cb_cls closure for @a cb
1376 * @return handle to cancel request
1378 struct GCP_MessageQueueManager *
1379 GCP_request_mq (struct CadetPeer *cp,
1380 GCP_MessageQueueNotificationCallback cb,
1383 struct GCP_MessageQueueManager *mqm;
1385 mqm = GNUNET_new (struct GCP_MessageQueueManager);
1387 mqm->cb_cls = cb_cls;
1389 GNUNET_CONTAINER_DLL_insert (cp->mqm_head,
1392 LOG (GNUNET_ERROR_TYPE_DEBUG,
1393 "Creating MQM %p for peer %s\n",
1396 if (NULL != cp->core_mq)
1404 * Stops message queue change notifications.
1406 * @param mqm handle matching request to cancel
1407 * @param last_env final message to transmit, or NULL
1410 GCP_request_mq_cancel (struct GCP_MessageQueueManager *mqm,
1411 struct GNUNET_MQ_Envelope *last_env)
1413 struct CadetPeer *cp = mqm->cp;
1415 LOG (GNUNET_ERROR_TYPE_DEBUG,
1416 "Destroying MQM %p for peer %s%s\n",
1419 (NULL == last_env) ? "" : " with last ditch transmission");
1420 if (NULL != mqm->env)
1421 GNUNET_MQ_discard (mqm->env);
1422 if (NULL != last_env)
1424 if (NULL != cp->core_mq)
1426 GNUNET_MQ_notify_sent (last_env,
1429 GNUNET_MQ_send (cp->core_mq,
1434 GNUNET_MQ_discard (last_env);
1437 if (cp->mqm_ready_ptr == mqm)
1438 cp->mqm_ready_ptr = mqm->next;
1439 GNUNET_CONTAINER_DLL_remove (cp->mqm_head,
1447 * Send the message in @a env to @a cp, overriding queueing logic.
1448 * This function should only be used to send error messages outside
1449 * of flow and congestion control, similar to ICMP. Note that
1450 * the envelope may be silently discarded as well.
1452 * @param cp peer to send the message to
1453 * @param env envelope with the message to send
1456 GCP_send_ooo (struct CadetPeer *cp,
1457 struct GNUNET_MQ_Envelope *env)
1459 LOG (GNUNET_ERROR_TYPE_DEBUG,
1460 "Sending message to %s out of management\n",
1462 if (NULL == cp->core_mq)
1464 GNUNET_MQ_discard (env);
1467 GNUNET_MQ_notify_sent (env,
1470 GNUNET_MQ_send (cp->core_mq,
1477 /* end of gnunet-service-cadet-new_peer.c */