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-new_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-new.h"
43 #include "gnunet-service-cadet-new_connection.h"
44 #include "gnunet-service-cadet-new_dht.h"
45 #include "gnunet-service-cadet-new_peer.h"
46 #include "gnunet-service-cadet-new_paths.h"
47 #include "gnunet-service-cadet-new_tunnels.h"
50 #define LOG(level, ...) GNUNET_log_from(level,"cadet-per",__VA_ARGS__)
54 * How long do we wait until tearing down an idle peer?
56 #define IDLE_PEER_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
59 * How long do we keep paths around if we no longer care about the peer?
61 #define IDLE_PATH_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
67 * Data structure used to track whom we have to notify about changes
68 * to our message queue.
70 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;
107 * Struct containing all information regarding a given peer
114 struct GNUNET_PeerIdentity pid;
117 * Last time we heard from this peer (currently not used!)
119 struct GNUNET_TIME_Absolute last_contactXXX;
122 * Array of DLLs of paths traversing the peer, organized by the
123 * offset of the peer on the larger path.
125 struct CadetPeerPathEntry **path_heads;
128 * Array of DLL of paths traversing the peer, organized by the
129 * offset of the peer on the larger path.
131 struct CadetPeerPathEntry **path_tails;
134 * Notifications to call when @e core_mq changes.
136 struct GCP_MessageQueueManager *mqm_head;
139 * Notifications to call when @e core_mq changes.
141 struct GCP_MessageQueueManager *mqm_tail;
144 * Pointer to first "ready" entry in @e mqm_head.
146 struct GCP_MessageQueueManager *mqm_ready_ptr;
149 * MIN-heap of paths owned by this peer (they also end at this
150 * peer). Ordered by desirability.
152 struct GNUNET_CONTAINER_Heap *path_heap;
155 * Handle to stop the DHT search for paths to this peer
157 struct GCD_search_handle *search_h;
160 * Task to stop the DHT search for paths to this peer
162 struct GNUNET_SCHEDULER_Task *search_delayedXXX;
165 * Task to destroy this entry.
167 struct GNUNET_SCHEDULER_Task *destroy_task;
170 * Tunnel to this peer, if any.
172 struct CadetTunnel *t;
175 * Connections that go through this peer; indexed by tid.
177 struct GNUNET_CONTAINER_MultiShortmap *connections;
180 * Handle for core transmissions.
182 struct GNUNET_MQ_Handle *core_mq;
185 * Hello message of the peer.
187 struct GNUNET_HELLO_Message *hello;
190 * Handle to us offering the HELLO to the transport.
192 struct GNUNET_TRANSPORT_OfferHelloHandle *hello_offer;
195 * Handle to our ATS request asking ATS to suggest an address
196 * to TRANSPORT for this peer (to establish a direct link).
198 struct GNUNET_ATS_ConnectivitySuggestHandle *connectivity_suggestion;
201 * How many messages are in the queue to this peer.
203 unsigned int queue_n;
206 * How many paths do we have to this peer (in all @e path_heads DLLs combined).
208 unsigned int num_paths;
211 * Sum over all of the offsets of all of the paths in the @a path_heads DLLs.
212 * Used to speed-up @GCP_get_desirability_of_path() calculation.
214 unsigned int off_sum;
217 * Number of message queue managers of this peer that have a message in waiting.
219 * Used to quickly see if we need to bother scanning the @e msm_head DLL.
220 * TODO: could be replaced by another DLL that would then allow us to avoid
221 * the O(n)-scan of the DLL for ready entries!
223 unsigned int mqm_ready_counter;
226 * Current length of the @e path_heads and @path_tails arrays.
227 * The arrays should be grown as needed.
229 unsigned int path_dll_length;
235 * Get the static string for a peer ID.
238 * @return Static string for it's ID.
241 GCP_2s (const struct CadetPeer *cp)
245 GNUNET_snprintf (buf,
248 GNUNET_i2s (&cp->pid));
254 * Calculate how desirable a path is for @a cp if @a cp
255 * is at offset @a off.
257 * The 'desirability_table.c' program can be used to compute a list of
258 * sample outputs for different scenarios. Basically, we score paths
259 * lower if there are many alternatives, and higher if they are
260 * shorter than average, and very high if they are much shorter than
261 * average and without many alternatives.
263 * @param cp a peer reachable via a path
264 * @param off offset of @a cp in the path
265 * @return score how useful a path is to reach @a cp,
266 * positive scores mean path is more desirable
269 GCP_get_desirability_of_path (struct CadetPeer *cp,
272 unsigned int num_alts = cp->num_paths;
273 unsigned int off_sum;
278 GNUNET_assert (num_alts >= 1); /* 'path' should be in there! */
279 GNUNET_assert (0 != cp->path_dll_length);
281 /* We maintain 'off_sum' in 'peer' and thereby
282 avoid the SLOW recalculation each time. Kept here
283 just to document what is going on. */
286 for (unsigned int j=0;j<cp->path_dll_length;j++)
287 for (struct CadetPeerPathEntry *pe = cp->path_heads[j];
291 GNUNET_assert (off_sum == cp->off_sum);
293 off_sum = cp->off_sum;
295 avg_sum = off_sum * 1.0 / cp->path_dll_length;
296 path_delta = off - avg_sum;
297 /* path_delta positiv: path off of peer above average (bad path for peer),
298 path_delta negativ: path off of peer below average (good path for peer) */
299 if (path_delta <= - 1.0)
300 weight_alts = - num_alts / path_delta; /* discount alternative paths */
301 else if (path_delta >= 1.0)
302 weight_alts = num_alts * path_delta; /* overcount alternative paths */
304 weight_alts = num_alts; /* count alternative paths normally */
307 /* off+1: long paths are generally harder to find and thus count
308 a bit more as they get longer. However, above-average paths
309 still need to count less, hence the squaring of that factor. */
310 return (off + 1.0) / (weight_alts * weight_alts);
315 * This peer is no longer be needed, clean it up now.
317 * @param cls peer to clean up
320 destroy_peer (void *cls)
322 struct CadetPeer *cp = cls;
324 LOG (GNUNET_ERROR_TYPE_DEBUG,
325 "Destroying state about peer %s\n",
327 cp->destroy_task = NULL;
328 GNUNET_assert (NULL == cp->t);
329 GNUNET_assert (NULL == cp->core_mq);
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 GNUNET_free_non_null (cp->hello);
364 /* Peer should not be freed if paths exist; if there are no paths,
365 there ought to be no connections, and without connections, no
366 notifications. Thus we can assert that mqm_head is empty at this
368 GNUNET_assert (NULL == cp->mqm_head);
369 GNUNET_assert (NULL == cp->mqm_ready_ptr);
375 * This peer is now on more "active" duty, activate processes related to it.
377 * @param cp the more-active peer
380 consider_peer_activate (struct CadetPeer *cp)
384 LOG (GNUNET_ERROR_TYPE_DEBUG,
385 "Updating peer %s activation state (%u connections)%s%s\n",
387 GNUNET_CONTAINER_multishortmap_size (cp->connections),
388 (NULL == cp->t) ? "" : " with tunnel",
389 (NULL == cp->core_mq) ? "" : " with CORE link");
390 if (NULL != cp->destroy_task)
392 /* It's active, do not destory! */
393 GNUNET_SCHEDULER_cancel (cp->destroy_task);
394 cp->destroy_task = NULL;
396 if ( (0 == GNUNET_CONTAINER_multishortmap_size (cp->connections)) &&
399 /* We're just on a path or directly connected; don't bother too much */
400 if (NULL != cp->connectivity_suggestion)
402 GNUNET_ATS_connectivity_suggest_cancel (cp->connectivity_suggestion);
403 cp->connectivity_suggestion = NULL;
405 if (NULL != cp->search_h)
407 GCD_search_stop (cp->search_h);
412 if (NULL == cp->core_mq)
414 /* Lacks direct connection, try to create one by querying the DHT */
415 if ( (NULL == cp->search_h) &&
416 (DESIRED_CONNECTIONS_PER_TUNNEL > cp->num_paths) )
418 = GCD_search (&cp->pid);
422 /* Have direct connection, stop DHT search if active */
423 if (NULL != cp->search_h)
425 GCD_search_stop (cp->search_h);
430 /* If we have a tunnel, our urge for connections is much bigger */
431 strength = (NULL != cp->t) ? 32 : 1;
432 if (NULL != cp->connectivity_suggestion)
433 GNUNET_ATS_connectivity_suggest_cancel (cp->connectivity_suggestion);
434 cp->connectivity_suggestion
435 = GNUNET_ATS_connectivity_suggest (ats_ch,
442 * This peer may no longer be needed, consider cleaning it up.
444 * @param cp peer to clean up
447 consider_peer_destroy (struct CadetPeer *cp);
451 * We really no longere care about a peer, stop hogging memory with paths to it.
452 * Afterwards, see if there is more to be cleaned up about this peer.
454 * @param cls a `struct CadetPeer`.
457 drop_paths (void *cls)
459 struct CadetPeer *cp = cls;
460 struct CadetPeerPath *path;
462 cp->destroy_task = NULL;
463 while (NULL != (path = GNUNET_CONTAINER_heap_remove_root (cp->path_heap)))
465 consider_peer_destroy (cp);
470 * This peer may no longer be needed, consider cleaning it up.
472 * @param cp peer to clean up
475 consider_peer_destroy (struct CadetPeer *cp)
477 struct GNUNET_TIME_Relative exp;
479 if (NULL != cp->destroy_task)
481 GNUNET_SCHEDULER_cancel (cp->destroy_task);
482 cp->destroy_task = NULL;
485 return; /* still relevant! */
486 if (NULL != cp->core_mq)
487 return; /* still relevant! */
488 if (0 != GNUNET_CONTAINER_multishortmap_size (cp->connections))
489 return; /* still relevant! */
490 if (0 < GNUNET_CONTAINER_heap_get_size (cp->path_heap))
492 cp->destroy_task = GNUNET_SCHEDULER_add_delayed (IDLE_PATH_TIMEOUT,
497 for (unsigned int i=0;i<cp->path_dll_length;i++)
498 if (NULL != cp->path_heads[i])
499 return; /* still relevant! */
500 if (NULL != cp->hello)
502 /* relevant only until HELLO expires */
503 exp = GNUNET_TIME_absolute_get_remaining (GNUNET_HELLO_get_last_expiration (cp->hello));
504 cp->destroy_task = GNUNET_SCHEDULER_add_delayed (exp,
509 cp->destroy_task = GNUNET_SCHEDULER_add_delayed (IDLE_PEER_TIMEOUT,
516 * Set the message queue to @a mq for peer @a cp and notify watchers.
518 * @param cp peer to modify
519 * @param mq message queue to set (can be NULL)
522 GCP_set_mq (struct CadetPeer *cp,
523 struct GNUNET_MQ_Handle *mq)
525 LOG (GNUNET_ERROR_TYPE_DEBUG,
526 "Message queue for peer %s is now %p\n",
530 for (struct GCP_MessageQueueManager *mqm = cp->mqm_head;
536 if (NULL != mqm->env)
538 GNUNET_MQ_discard (mqm->env);
540 mqm->cb (mqm->cb_cls,
545 mqm->cb (mqm->cb_cls,
551 GNUNET_assert (NULL == mqm->env);
552 mqm->cb (mqm->cb_cls,
558 consider_peer_activate (cp);
560 consider_peer_destroy (cp);
565 /* have a new, direct path to the target, notify tunnel */
566 struct CadetPeerPath *path;
568 path = GCPP_get_path_from_route (1,
570 GCT_consider_path (cp->t,
578 * Debug function should NEVER return true in production code, useful to
579 * simulate losses for testcases.
581 * @return #GNUNET_YES or #GNUNET_NO with the decision to drop.
586 if (0 == drop_percent)
588 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
596 * Function called when CORE took one of the messages from
597 * a message queue manager and transmitted it.
599 * @param cls the `struct CadetPeeer` where we made progress
602 mqm_send_done (void *cls);
606 * Transmit current envelope from this @a mqm.
608 * @param mqm mqm to transmit message for now
611 mqm_execute (struct GCP_MessageQueueManager *mqm)
613 struct CadetPeer *cp = mqm->cp;
615 /* Move ready pointer to the next entry that might be ready. */
616 if ( (mqm == cp->mqm_ready_ptr) &&
617 (NULL != mqm->next) )
618 cp->mqm_ready_ptr = mqm->next;
619 /* Move entry to the end of the DLL, to be fair. */
620 if (mqm != cp->mqm_tail)
622 GNUNET_CONTAINER_DLL_remove (cp->mqm_head,
625 GNUNET_CONTAINER_DLL_insert_tail (cp->mqm_head,
629 cp->mqm_ready_counter--;
630 if (GNUNET_YES == should_I_drop ())
632 LOG (GNUNET_ERROR_TYPE_DEBUG,
633 "DROPPING message to peer %s from MQM %p\n",
636 GNUNET_MQ_discard (mqm->env);
642 LOG (GNUNET_ERROR_TYPE_DEBUG,
643 "Sending to peer %s from MQM %p\n",
646 GNUNET_MQ_send (cp->core_mq,
650 mqm->cb (mqm->cb_cls,
656 * Find the next ready message in the queue (starting
657 * the search from the `cp->mqm_ready_ptr`) and if possible
658 * execute the transmission.
660 * @param cp peer to try to send the next ready message to
663 send_next_ready (struct CadetPeer *cp)
665 struct GCP_MessageQueueManager *mqm;
667 if (0 == cp->mqm_ready_counter)
669 while ( (NULL != (mqm = cp->mqm_ready_ptr)) &&
671 cp->mqm_ready_ptr = mqm->next;
673 return; /* nothing to do */
679 * Function called when CORE took one of the messages from
680 * a message queue manager and transmitted it.
682 * @param cls the `struct CadetPeeer` where we made progress
685 mqm_send_done (void *cls)
687 struct CadetPeer *cp = cls;
689 LOG (GNUNET_ERROR_TYPE_DEBUG,
690 "Sending to peer %s completed\n",
692 send_next_ready (cp);
697 * Send the message in @a env to @a cp.
699 * @param mqm the message queue manager to use for transmission
700 * @param env envelope with the message to send; must NOT
701 * yet have a #GNUNET_MQ_notify_sent() callback attached to it
704 GCP_send (struct GCP_MessageQueueManager *mqm,
705 struct GNUNET_MQ_Envelope *env)
707 struct CadetPeer *cp = mqm->cp;
709 GNUNET_assert (NULL != env);
710 LOG (GNUNET_ERROR_TYPE_DEBUG,
711 "Queueing message to peer %s in MQM %p\n",
714 GNUNET_assert (NULL != cp->core_mq);
715 GNUNET_assert (NULL == mqm->env);
716 GNUNET_MQ_notify_sent (env,
720 cp->mqm_ready_counter++;
721 if (mqm != cp->mqm_ready_ptr)
722 cp->mqm_ready_ptr = cp->mqm_head;
723 if (1 == cp->mqm_ready_counter)
724 cp->mqm_ready_ptr = mqm;
725 if (0 != GNUNET_MQ_get_length (cp->core_mq))
727 send_next_ready (cp);
732 * Function called to destroy a peer now.
735 * @param pid identity of the peer (unused)
736 * @param value the `struct CadetPeer` to clean up
737 * @return #GNUNET_OK (continue to iterate)
740 destroy_iterator_cb (void *cls,
741 const struct GNUNET_PeerIdentity *pid,
744 struct CadetPeer *cp = value;
746 if (NULL != cp->destroy_task)
748 GNUNET_SCHEDULER_cancel (cp->destroy_task);
749 cp->destroy_task = NULL;
757 * Clean up all entries about all peers.
758 * Must only be called after all tunnels, CORE-connections and
759 * connections are down.
762 GCP_destroy_all_peers ()
764 LOG (GNUNET_ERROR_TYPE_DEBUG,
765 "Destroying all peers now\n");
766 GNUNET_CONTAINER_multipeermap_iterate (peers,
767 &destroy_iterator_cb,
773 * Drop all paths owned by this peer, and do not
774 * allow new ones to be added: We are shutting down.
776 * @param cp peer to drop paths to
779 GCP_drop_owned_paths (struct CadetPeer *cp)
781 struct CadetPeerPath *path;
783 LOG (GNUNET_ERROR_TYPE_DEBUG,
784 "Destroying all paths to %s\n",
786 while (NULL != (path =
787 GNUNET_CONTAINER_heap_remove_root (cp->path_heap)))
789 GNUNET_CONTAINER_heap_destroy (cp->path_heap);
790 cp->path_heap = NULL;
795 * Add an entry to the DLL of all of the paths that this peer is on.
797 * @param cp peer to modify
798 * @param entry an entry on a path
799 * @param off offset of this peer on the path
802 GCP_path_entry_add (struct CadetPeer *cp,
803 struct CadetPeerPathEntry *entry,
806 GNUNET_assert (cp == GCPP_get_peer_at_offset (entry->path,
808 LOG (GNUNET_ERROR_TYPE_DEBUG,
809 "Discovered that peer %s is on path %s at offset %u\n",
811 GCPP_2s (entry->path),
813 if (off >= cp->path_dll_length)
815 unsigned int len = cp->path_dll_length;
817 GNUNET_array_grow (cp->path_heads,
820 GNUNET_array_grow (cp->path_tails,
824 GNUNET_CONTAINER_DLL_insert (cp->path_heads[off],
830 /* If we have a tunnel to this peer, tell the tunnel that there is a
831 new path available. */
833 GCT_consider_path (cp->t,
837 if ( (NULL != cp->search_h) &&
838 (DESIRED_CONNECTIONS_PER_TUNNEL <= cp->num_paths) )
840 /* Now I have enough paths, stop search */
841 GCD_search_stop (cp->search_h);
848 * Remove an entry from the DLL of all of the paths that this peer is on.
850 * @param cp peer to modify
851 * @param entry an entry on a path
852 * @param off offset of this peer on the path
855 GCP_path_entry_remove (struct CadetPeer *cp,
856 struct CadetPeerPathEntry *entry,
859 LOG (GNUNET_ERROR_TYPE_DEBUG,
860 "Removing knowledge about peer %s beging on path %s at offset %u\n",
862 GCPP_2s (entry->path),
864 GNUNET_CONTAINER_DLL_remove (cp->path_heads[off],
867 GNUNET_assert (0 < cp->num_paths);
870 if ( (NULL == cp->core_mq) &&
872 (NULL == cp->search_h) &&
873 (DESIRED_CONNECTIONS_PER_TUNNEL > cp->num_paths) )
875 = GCD_search (&cp->pid);
880 * Try adding a @a path to this @a peer. If the peer already
881 * has plenty of paths, return NULL.
883 * @param cp peer to which the @a path leads to
884 * @param path a path looking for an owner; may not be fully initialized yet!
885 * @param off offset of @a cp in @a path
886 * @param force force attaching the path
887 * @return NULL if this peer does not care to become a new owner,
888 * otherwise the node in the peer's path heap for the @a path.
890 struct GNUNET_CONTAINER_HeapNode *
891 GCP_attach_path (struct CadetPeer *cp,
892 struct CadetPeerPath *path,
896 GNUNET_CONTAINER_HeapCostType desirability;
897 struct CadetPeerPath *root;
898 GNUNET_CONTAINER_HeapCostType root_desirability;
899 struct GNUNET_CONTAINER_HeapNode *hn;
901 GNUNET_assert (cp == GCPP_get_peer_at_offset (path,
903 if (NULL == cp->path_heap)
905 /* #GCP_drop_owned_paths() was already called, we cannot take new ones! */
906 GNUNET_assert (GNUNET_NO == force);
909 desirability = GCPP_get_desirability (path);
910 if (GNUNET_NO == force)
912 /* FIXME: desirability is not yet initialized; tricky! */
914 GNUNET_CONTAINER_heap_peek2 (cp->path_heap,
919 root_desirability = 0;
922 if ( (DESIRED_CONNECTIONS_PER_TUNNEL > cp->num_paths) &&
923 (desirability < root_desirability) )
925 LOG (GNUNET_ERROR_TYPE_DEBUG,
926 "Decided to not attach path %p to peer %s due to undesirability\n",
933 LOG (GNUNET_ERROR_TYPE_DEBUG,
934 "Attaching path %s to peer %s (%s)\n",
937 (GNUNET_NO == force) ? "desirable" : "forced");
939 /* Yes, we'd like to add this path, add to our heap */
940 hn = GNUNET_CONTAINER_heap_insert (cp->path_heap,
944 /* Consider maybe dropping other paths because of the new one */
945 if (GNUNET_CONTAINER_heap_get_size (cp->path_heap) >=
946 2 * DESIRED_CONNECTIONS_PER_TUNNEL)
948 /* Now we have way too many, drop least desirable UNLESS it is in use!
949 (Note that this intentionally keeps highly desireable, but currently
950 unused paths around in the hope that we might be able to switch, even
951 if the number of paths exceeds the threshold.) */
952 root = GNUNET_CONTAINER_heap_peek (cp->path_heap);
953 if ( (path != root) &&
955 GCPP_get_connection (root,
957 GCPP_get_length (root) - 1)) )
959 /* Got plenty of paths to this destination, and this is a low-quality
960 one that we don't care, allow it to die. */
961 GNUNET_assert (root ==
962 GNUNET_CONTAINER_heap_remove_root (cp->path_heap));
971 * This peer can no longer own @a path as the path
972 * has been extended and a peer further down the line
973 * is now the new owner.
975 * @param cp old owner of the @a path
976 * @param path path where the ownership is lost
977 * @param hn note in @a cp's path heap that must be deleted
980 GCP_detach_path (struct CadetPeer *cp,
981 struct CadetPeerPath *path,
982 struct GNUNET_CONTAINER_HeapNode *hn)
984 LOG (GNUNET_ERROR_TYPE_DEBUG,
985 "Detatching path %s from peer %s\n",
988 GNUNET_assert (path ==
989 GNUNET_CONTAINER_heap_remove_node (hn));
994 * Add a @a connection to this @a cp.
996 * @param cp peer via which the @a connection goes
997 * @param cc the connection to add
1000 GCP_add_connection (struct CadetPeer *cp,
1001 struct CadetConnection *cc)
1003 LOG (GNUNET_ERROR_TYPE_DEBUG,
1004 "Adding connection %s to peer %s\n",
1007 GNUNET_assert (GNUNET_OK ==
1008 GNUNET_CONTAINER_multishortmap_put (cp->connections,
1009 &GCC_get_id (cc)->connection_of_tunnel,
1011 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1016 * Remove a @a connection that went via this @a cp.
1018 * @param cp peer via which the @a connection went
1019 * @param cc the connection to remove
1022 GCP_remove_connection (struct CadetPeer *cp,
1023 struct CadetConnection *cc)
1025 LOG (GNUNET_ERROR_TYPE_DEBUG,
1026 "Removing connection %s from peer %s\n",
1029 GNUNET_assert (GNUNET_YES ==
1030 GNUNET_CONTAINER_multishortmap_remove (cp->connections,
1031 &GCC_get_id (cc)->connection_of_tunnel,
1037 * Retrieve the CadetPeer stucture associated with the
1038 * peer. Optionally create one and insert it in the appropriate
1039 * structures if the peer is not known yet.
1041 * @param peer_id Full identity of the peer.
1042 * @param create #GNUNET_YES if a new peer should be created if unknown.
1043 * #GNUNET_NO to return NULL if peer is unknown.
1044 * @return Existing or newly created peer structure.
1045 * NULL if unknown and not requested @a create
1048 GCP_get (const struct GNUNET_PeerIdentity *peer_id,
1051 struct CadetPeer *cp;
1053 cp = GNUNET_CONTAINER_multipeermap_get (peers,
1057 if (GNUNET_NO == create)
1059 cp = GNUNET_new (struct CadetPeer);
1061 cp->connections = GNUNET_CONTAINER_multishortmap_create (32,
1063 cp->path_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1064 GNUNET_assert (GNUNET_YES ==
1065 GNUNET_CONTAINER_multipeermap_put (peers,
1068 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1069 LOG (GNUNET_ERROR_TYPE_DEBUG,
1070 "Creating peer %s\n",
1077 * Obtain the peer identity for a `struct CadetPeer`.
1079 * @param cp our peer handle
1080 * @return the peer identity
1082 const struct GNUNET_PeerIdentity *
1083 GCP_get_id (struct CadetPeer *cp)
1090 * Iterate over all known peers.
1092 * @param iter Iterator.
1093 * @param cls Closure for @c iter.
1096 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter,
1099 GNUNET_CONTAINER_multipeermap_iterate (peers,
1106 * Count the number of known paths toward the peer.
1108 * @param cp Peer to get path info.
1109 * @return Number of known paths.
1112 GCP_count_paths (const struct CadetPeer *cp)
1114 return cp->num_paths;
1119 * Iterate over the paths to a peer.
1121 * @param cp Peer to get path info.
1122 * @param callback Function to call for every path.
1123 * @param callback_cls Closure for @a callback.
1124 * @return Number of iterated paths.
1127 GCP_iterate_paths (struct CadetPeer *cp,
1128 GCP_PathIterator callback,
1131 unsigned int ret = 0;
1133 LOG (GNUNET_ERROR_TYPE_DEBUG,
1134 "Iterating over paths to peer %s%s\n",
1136 (NULL == cp->core_mq) ? "" : " including direct link");
1137 if (NULL != cp->core_mq)
1139 struct CadetPeerPath *path;
1141 path = GCPP_get_path_from_route (1,
1145 callback (callback_cls,
1150 for (unsigned int i=0;i<cp->path_dll_length;i++)
1152 for (struct CadetPeerPathEntry *pe = cp->path_heads[i];
1158 callback (callback_cls,
1169 * Iterate over the paths to @a cp where
1170 * @a cp is at distance @a dist from us.
1172 * @param cp Peer to get path info.
1173 * @param dist desired distance of @a cp to us on the path
1174 * @param callback Function to call for every path.
1175 * @param callback_cls Closure for @a callback.
1176 * @return Number of iterated paths.
1179 GCP_iterate_paths_at (struct CadetPeer *cp,
1181 GCP_PathIterator callback,
1184 unsigned int ret = 0;
1186 if (dist >= cp->path_dll_length)
1188 LOG (GNUNET_ERROR_TYPE_DEBUG,
1189 "Asked to look for paths at distance %u, but maximum for me is < %u\n",
1191 cp->path_dll_length);
1194 for (struct CadetPeerPathEntry *pe = cp->path_heads[dist];
1199 callback (callback_cls,
1210 * Get the tunnel towards a peer.
1212 * @param cp Peer to get from.
1213 * @param create #GNUNET_YES to create a tunnel if we do not have one
1214 * @return Tunnel towards peer.
1216 struct CadetTunnel *
1217 GCP_get_tunnel (struct CadetPeer *cp,
1222 if ( (NULL != cp->t) ||
1223 (GNUNET_NO == create) )
1225 cp->t = GCT_create_tunnel (cp);
1226 consider_peer_activate (cp);
1232 * Hello offer was passed to the transport service. Mark it
1235 * @param cls the `struct CadetPeer` where the offer completed
1238 hello_offer_done (void *cls)
1240 struct CadetPeer *cp = cls;
1242 cp->hello_offer = NULL;
1247 * We got a HELLO for a @a peer, remember it, and possibly
1248 * trigger adequate actions (like trying to connect).
1250 * @param cp the peer we got a HELLO for
1251 * @param hello the HELLO to remember
1254 GCP_set_hello (struct CadetPeer *cp,
1255 const struct GNUNET_HELLO_Message *hello)
1257 struct GNUNET_HELLO_Message *mrg;
1259 LOG (GNUNET_ERROR_TYPE_DEBUG,
1260 "Got %u byte HELLO for peer %s\n",
1261 (unsigned int) GNUNET_HELLO_size (hello),
1263 if (NULL != cp->hello_offer)
1265 GNUNET_TRANSPORT_offer_hello_cancel (cp->hello_offer);
1266 cp->hello_offer = NULL;
1268 if (NULL != cp->hello)
1270 mrg = GNUNET_HELLO_merge (hello,
1272 GNUNET_free (cp->hello);
1277 cp->hello = GNUNET_memdup (hello,
1278 GNUNET_HELLO_size (hello));
1281 = GNUNET_TRANSPORT_offer_hello (cfg,
1282 GNUNET_HELLO_get_header (cp->hello) ,
1285 /* New HELLO means cp's destruction time may change... */
1286 consider_peer_destroy (cp);
1291 * The tunnel to the given peer no longer exists, remove it from our
1292 * data structures, and possibly clean up the peer itself.
1294 * @param cp the peer affected
1295 * @param t the dead tunnel
1298 GCP_drop_tunnel (struct CadetPeer *cp,
1299 struct CadetTunnel *t)
1301 LOG (GNUNET_ERROR_TYPE_DEBUG,
1302 "Dropping tunnel %s to peer %s\n",
1305 GNUNET_assert (cp->t == t);
1307 consider_peer_destroy (cp);
1312 * Test if @a cp has a core-level connection
1314 * @param cp peer to test
1315 * @return #GNUNET_YES if @a cp has a core-level connection
1318 GCP_has_core_connection (struct CadetPeer *cp)
1320 return (NULL != cp->core_mq) ? GNUNET_YES : GNUNET_NO;
1325 * Start message queue change notifications.
1327 * @param cp peer to notify for
1328 * @param cb function to call if mq becomes available or unavailable
1329 * @param cb_cls closure for @a cb
1330 * @return handle to cancel request
1332 struct GCP_MessageQueueManager *
1333 GCP_request_mq (struct CadetPeer *cp,
1334 GCP_MessageQueueNotificationCallback cb,
1337 struct GCP_MessageQueueManager *mqm;
1339 mqm = GNUNET_new (struct GCP_MessageQueueManager);
1341 mqm->cb_cls = cb_cls;
1343 GNUNET_CONTAINER_DLL_insert (cp->mqm_head,
1346 LOG (GNUNET_ERROR_TYPE_DEBUG,
1347 "Creating MQM %p for peer %s\n",
1350 if (NULL != cp->core_mq)
1358 * Stops message queue change notifications.
1360 * @param mqm handle matching request to cancel
1361 * @param last_env final message to transmit, or NULL
1364 GCP_request_mq_cancel (struct GCP_MessageQueueManager *mqm,
1365 struct GNUNET_MQ_Envelope *last_env)
1367 struct CadetPeer *cp = mqm->cp;
1369 LOG (GNUNET_ERROR_TYPE_DEBUG,
1370 "Destroying MQM %p for peer %s%s\n",
1373 (NULL == last_env) ? "" : " with last ditch transmission");
1374 if (NULL != mqm->env)
1375 GNUNET_MQ_discard (mqm->env);
1376 if (NULL != last_env)
1378 if (NULL != cp->core_mq)
1379 GNUNET_MQ_send (cp->core_mq,
1382 GNUNET_MQ_discard (last_env);
1384 if (cp->mqm_ready_ptr == mqm)
1385 cp->mqm_ready_ptr = mqm->next;
1386 GNUNET_CONTAINER_DLL_remove (cp->mqm_head,
1394 * Send the message in @a env to @a cp, overriding queueing logic.
1395 * This function should only be used to send error messages outside
1396 * of flow and congestion control, similar to ICMP. Note that
1397 * the envelope may be silently discarded as well.
1399 * @param cp peer to send the message to
1400 * @param env envelope with the message to send
1403 GCP_send_ooo (struct CadetPeer *cp,
1404 struct GNUNET_MQ_Envelope *env)
1406 LOG (GNUNET_ERROR_TYPE_DEBUG,
1407 "Sending message to %s out of management\n",
1409 if (NULL == cp->core_mq)
1411 GNUNET_MQ_discard (env);
1414 GNUNET_MQ_send (cp->core_mq,
1421 /* end of gnunet-service-cadet-new_peer.c */