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.
21 * @file cadet/gnunet-service-cadet_paths.c
22 * @brief Information we track per path.
23 * @author Bartlomiej Polot
24 * @author Christian Grothoff
27 #include "gnunet-service-cadet_connection.h"
28 #include "gnunet-service-cadet_tunnels.h"
29 #include "gnunet-service-cadet_peer.h"
30 #include "gnunet-service-cadet_paths.h"
33 #define LOG(level, ...) GNUNET_log_from(level,"cadet-pat",__VA_ARGS__)
37 * Information regarding a possible path to reach a peer.
43 * Array of all the peers on the path. If @e hn is non-NULL, the
44 * last one is our owner.
46 struct CadetPeerPathEntry **entries;
49 * Node of this path in the owner's heap. Used to update our position
50 * in the heap whenever our @e desirability changes.
52 struct GNUNET_CONTAINER_HeapNode *hn;
55 * Desirability of the path. How unique is it for the various peers
58 GNUNET_CONTAINER_HeapCostType desirability;
61 * Length of the @e entries array.
63 unsigned int entries_length;
69 * Calculate the path's desirability score.
71 * @param path path to calculate the score for
74 recalculate_path_desirability (struct CadetPeerPath *path)
78 for (unsigned int i=0;i<path->entries_length;i++)
80 struct CadetPeer *cp = path->entries[i]->peer;
82 result += GCP_get_desirability_of_path (cp,
85 path->desirability = (GNUNET_CONTAINER_HeapCostType) result;
90 * Return how much we like keeping the path. This is an aggregate
91 * score based on various factors, including the age of the path
92 * (older == better), and the value of this path to all of its ajacent
93 * peers. For example, long paths that end at a peer that we have no
94 * shorter way to reach are very desirable, while long paths that end
95 * at a peer for which we have a shorter way as well are much less
96 * desirable. Higher values indicate more valuable paths. The
97 * returned value should be used to decide which paths to remember.
99 * @param path path to return the length for
100 * @return desirability of the path, larger is more desirable
102 GNUNET_CONTAINER_HeapCostType
103 GCPP_get_desirability (const struct CadetPeerPath *path)
105 return path->desirability;
110 * Return connection to @a destination using @a path, or return
111 * NULL if no such connection exists.
113 * @param path path to traverse
114 * @param destination destination node to get to, must be on path
115 * @param off offset of @a destination on @a path
116 * @return NULL if we have no existing connection
117 * otherwise connection from us to @a destination via @a path
119 struct CadetConnection *
120 GCPP_get_connection (struct CadetPeerPath *path,
121 struct CadetPeer *destination,
124 struct CadetPeerPathEntry *entry;
126 GNUNET_assert (off < path->entries_length);
127 entry = path->entries[off];
128 GNUNET_assert (entry->peer == destination);
134 * Notify @a path that it is used for connection @a cc
135 * which ends at the path's offset @a off.
137 * @param path the path to remember the @a cc
138 * @param off the offset where the @a cc ends
139 * @param cc the connection to remember
142 GCPP_add_connection (struct CadetPeerPath *path,
144 struct CadetConnection *cc)
146 struct CadetPeerPathEntry *entry;
148 LOG (GNUNET_ERROR_TYPE_DEBUG,
149 "Adding connection %s to path %s at offset %u\n",
153 GNUNET_assert (off < path->entries_length);
154 entry = path->entries[off];
155 GNUNET_assert (NULL == entry->cc);
156 GNUNET_assert (NULL != cc);
163 * Notify @a path that it is no longer used for connection @a cc which
164 * ended at the path's offset @a off.
166 * @param path the path to forget the @a cc
167 * @param off the offset where the @a cc ended
168 * @param cc the connection to forget
171 GCPP_del_connection (struct CadetPeerPath *path,
173 struct CadetConnection *cc)
175 struct CadetPeerPathEntry *entry;
177 LOG (GNUNET_ERROR_TYPE_DEBUG,
178 "Removing connection %s to path %s at offset %u\n",
182 GNUNET_assert (off < path->entries_length);
183 entry = path->entries[off];
184 GNUNET_assert (cc == entry->cc);
190 * Tries to attach @a path to a peer, working backwards from the end
191 * and stopping at @a stop_at. If path->hn is NULL on return then the
192 * path was not attached and you can assume that path->entries_length
193 * is equal to @a stop_at.
195 * @param path the path to attach
196 * @param stop_at the path length at which to stop trying
199 attach_path (struct CadetPeerPath *path, unsigned int stop_at)
201 GNUNET_assert (NULL == path->hn);
203 /* Try to attach this path to a peer, working backwards from the end. */
204 while (path->entries_length > stop_at)
206 unsigned int end = path->entries_length - 1;
207 struct CadetPeerPathEntry *entry = path->entries[end];
208 int force = GNUNET_NO;
210 recalculate_path_desirability (path);
211 /* If the entry already has a connection using it, force attach. */
212 if (NULL != entry->cc)
214 path->hn = GCP_attach_path (entry->peer,
218 if (NULL != path->hn)
221 /* Attach failed, trim this entry from the path. */
222 GNUNET_assert (NULL == entry->cc);
223 GCP_path_entry_remove (entry->peer,
227 path->entries[end] = NULL;
228 path->entries_length--;
231 /* Shrink array to actual path length. */
232 GNUNET_array_grow (path->entries,
233 path->entries_length,
234 path->entries_length);
239 * The owning peer of this path is no longer interested in maintaining
240 * it, so the path should be discarded or shortened (in case a
241 * previous peer on the path finds the path desirable).
243 * @param path the path that is being released
246 GCPP_release (struct CadetPeerPath *path)
248 struct CadetPeerPathEntry *entry;
250 LOG (GNUNET_ERROR_TYPE_DEBUG,
251 "Owner releases path %s\n",
254 entry = path->entries[path->entries_length - 1];
255 GNUNET_assert (path == entry->path);
256 GNUNET_assert (NULL == entry->cc);
257 /* cut 'off' end of path */
258 GCP_path_entry_remove (entry->peer,
260 path->entries_length - 1);
262 path->entries[path->entries_length - 1] = NULL;
263 path->entries_length--;
264 /* see if new peer at the end likes this path any better */
265 attach_path (path, 0);
266 if (NULL == path->hn)
268 /* nobody wants us, discard the path */
269 GNUNET_assert (0 == path->entries_length);
270 GNUNET_assert (NULL == path->entries);
277 * Updates the score for an entry on the path based
278 * on our experiences with using @a path.
280 * @param path the path to update
281 * @param off offset of the entry to update
282 * @param delta change in the score to apply
285 GCPP_update_score (struct CadetPeerPath *path,
289 struct CadetPeerPathEntry *entry;
291 GNUNET_assert (off < path->entries_length);
292 entry = path->entries[off];
294 /* Add delta, with checks for overflows */
297 if (delta + entry->score < entry->score)
298 entry->score = INT_MAX;
300 entry->score += delta;
304 if (delta + entry->score > entry->score)
305 entry->score = INT_MIN;
307 entry->score += delta;
309 recalculate_path_desirability (path);
314 * Closure for #find_peer_at() and #check_match().
316 struct CheckMatchContext
320 * Set to a matching path, if any.
322 struct CadetPeerPath *match;
325 * Array the combined paths.
327 struct CadetPeer **cpath;
330 * How long is the @e cpath array?
332 unsigned int cpath_length;
338 * Check if the given path is identical on all of the
339 * hops until @a off, and not longer than @a off. If the
340 * @a path matches, store it in `match`.
342 * @param cls the `struct CheckMatchContext` to check against
343 * @param path the path to check
344 * @param off offset to check at
345 * @return #GNUNET_YES (continue to iterate), or if found #GNUNET_NO
348 check_match (void *cls,
349 struct CadetPeerPath *path,
352 struct CheckMatchContext *cm_ctx = cls;
354 GNUNET_assert (path->entries_length > off);
355 if ( (path->entries_length != off + 1) &&
356 (off + 1 != cm_ctx->cpath_length) )
358 LOG (GNUNET_ERROR_TYPE_DEBUG,
359 "check_match missmatch because path %s is too long (%u vs. %u vs. %u)\n",
361 path->entries_length,
363 cm_ctx->cpath_length);
364 return GNUNET_YES; /* too long, goes somewhere else already, thus cannot be useful */
366 for (unsigned int i=0;i<off;i++)
367 if (cm_ctx->cpath[i] !=
368 GCPP_get_peer_at_offset (path,
371 LOG (GNUNET_ERROR_TYPE_DEBUG,
372 "check_match path %s missmatches at offset %u\n",
375 return GNUNET_YES; /* missmatch, ignore */
377 LOG (GNUNET_ERROR_TYPE_DEBUG,
378 "check_match found match with path %s\n",
380 cm_ctx->match = path;
381 return GNUNET_NO; /* match, we are done! */
386 * Extend path @a path by the @a num_peers from the @a peers
387 * array, assuming the owners past the current owner want it.
389 * @param path path to extend
390 * @param peers list of peers beyond the end of @a path
391 * @param num_peers length of the @a peers array
392 * @param force force attachment, even if we have other
396 extend_path (struct CadetPeerPath *path,
397 struct CadetPeer **peers,
398 unsigned int num_peers,
401 unsigned int old_len = path->entries_length;
405 GNUNET_array_grow (path->entries,
406 path->entries_length,
407 old_len + num_peers);
408 for (i=num_peers-1;i >= 0;i--)
410 struct CadetPeerPathEntry *entry = GNUNET_new (struct CadetPeerPathEntry);
412 path->entries[old_len + i] = entry;
413 entry->peer = peers[i];
416 for (i=num_peers-1;i >= 0;i--)
418 struct CadetPeerPathEntry *entry = path->entries[old_len + i];
420 GCP_path_entry_add (entry->peer,
425 /* If we extend an existing path, detach it from the
426 old owner and re-attach to the new one */
427 GCP_detach_path (path->entries[old_len-1]->peer,
431 path->entries_length = old_len + num_peers;
432 if (GNUNET_YES == force)
434 int end = path->entries_length - 1;
436 path->hn = GCP_attach_path (path->entries[end]->peer,
441 attach_path (path, old_len);
443 if (NULL == path->hn)
445 /* none of the peers is interested in this path;
447 GNUNET_assert (old_len == path->entries_length);
448 path->hn = GCP_attach_path (path->entries[old_len - 1]->peer,
452 GNUNET_assert (NULL != path->hn);
455 LOG (GNUNET_ERROR_TYPE_DEBUG,
456 "Extended path %s\n",
462 * Create a peer path based on the result of a DHT lookup. If we
463 * already know this path, or one that is longer, simply return NULL.
464 * Otherwise, we try to extend an existing path, or create a new one
467 * @param get_path path of the get request
468 * @param get_path_length lenght of @a get_path
469 * @param put_path path of the put request
470 * @param put_path_length length of the @a put_path
471 * @return a path through the network
474 GCPP_try_path_from_dht (const struct GNUNET_PeerIdentity *get_path,
475 unsigned int get_path_length,
476 const struct GNUNET_PeerIdentity *put_path,
477 unsigned int put_path_length)
479 struct CadetPeer *cpath[get_path_length + put_path_length];
480 struct CheckMatchContext cm_ctx;
481 struct CadetPeerPath *path;
484 unsigned int total_len;
486 /* precompute 'cpath' so we can avoid doing the lookups lots of times */
490 sizeof (cpath)); /* Just to trigger harder errors later. */
491 total_len = get_path_length + put_path_length;
492 for (unsigned int off=0;off<total_len;off++)
494 const struct GNUNET_PeerIdentity *pid;
496 pid = (off < get_path_length)
497 ? &get_path[get_path_length - off - 1]
498 : &put_path[get_path_length + put_path_length - off - 1];
499 /* Check that I am not in the path */
500 if (0 == memcmp (&my_full_id,
502 sizeof (struct GNUNET_PeerIdentity)))
507 cpath[off - skip] = GCP_get (pid,
509 /* Check that no peer is twice on the path */
510 for (unsigned int i=0;i<off - skip;i++)
512 if (cpath[i] == cpath[off - skip])
519 if (skip >= total_len)
521 LOG (GNUNET_ERROR_TYPE_DEBUG,
522 "Path discovered from DHT is one big cycle?\n");
527 /* First figure out if this path is a subset of an existing path, an
528 extension of an existing path, or a new path. */
529 cm_ctx.cpath_length = total_len;
530 cm_ctx.cpath = cpath;
532 for (i=total_len-1;i>=0;i--)
534 GCP_iterate_paths_at (cpath[i],
538 if (NULL != cm_ctx.match)
540 if (i == total_len - 1)
542 /* Existing path includes this one, nothing to do! */
543 LOG (GNUNET_ERROR_TYPE_DEBUG,
544 "Path discovered from DHT is already known\n");
547 if (cm_ctx.match->entries_length == i + 1)
549 /* Existing path ends in the middle of new path, extend it! */
550 LOG (GNUNET_ERROR_TYPE_DEBUG,
551 "Trying to extend existing path %s by additional links discovered from DHT\n",
552 GCPP_2s (cm_ctx.match));
553 extend_path (cm_ctx.match,
562 /* No match at all, create completely new path */
563 path = GNUNET_new (struct CadetPeerPath);
564 path->entries_length = total_len;
565 path->entries = GNUNET_new_array (path->entries_length,
566 struct CadetPeerPathEntry *);
567 for (i=path->entries_length-1;i>=0;i--)
569 struct CadetPeerPathEntry *entry = GNUNET_new (struct CadetPeerPathEntry);
571 path->entries[i] = entry;
572 entry->peer = cpath[i];
575 for (i=path->entries_length-1;i>=0;i--)
577 struct CadetPeerPathEntry *entry = path->entries[i];
579 GCP_path_entry_add (entry->peer,
584 /* Finally, try to attach it */
585 attach_path (path, 0);
586 if (NULL == path->hn)
588 /* None of the peers on the path care about it. */
589 LOG (GNUNET_ERROR_TYPE_DEBUG,
590 "Path discovered from DHT is not interesting to us\n");
591 GNUNET_assert (0 == path->entries_length);
592 GNUNET_assert (NULL == path->entries);
596 LOG (GNUNET_ERROR_TYPE_DEBUG,
597 "Created new path %s based on information from DHT\n",
603 * We got an incoming connection, obtain the corresponding path.
605 * @param path_length number of segments on the @a path
606 * @param pids path through the network, in reverse order (we are at the end at index @a path_length)
607 * @return corresponding path object
609 struct CadetPeerPath *
610 GCPP_get_path_from_route (unsigned int path_length,
611 const struct GNUNET_PeerIdentity *pids)
613 struct CheckMatchContext cm_ctx;
614 struct CadetPeer *cpath[path_length];
615 struct CadetPeerPath *path;
617 /* precompute inverted 'cpath' so we can avoid doing the lookups and
618 have the correct order */
619 for (unsigned int off=0;off<path_length;off++)
620 cpath[off] = GCP_get (&pids[path_length - 1 - off],
623 /* First figure out if this path is a subset of an existing path, an
624 extension of an existing path, or a new path. */
625 cm_ctx.cpath = cpath;
626 cm_ctx.cpath_length = path_length;
628 for (int i=path_length-1;i>=0;i--)
630 GCP_iterate_paths_at (cpath[i],
634 if (NULL != cm_ctx.match)
636 if (i == path_length - 1)
638 /* Existing path includes this one, return the match! */
639 LOG (GNUNET_ERROR_TYPE_DEBUG,
640 "Returning existing path %s as inverse for incoming connection\n",
641 GCPP_2s (cm_ctx.match));
644 if (cm_ctx.match->entries_length == i + 1)
646 /* Existing path ends in the middle of new path, extend it! */
647 LOG (GNUNET_ERROR_TYPE_DEBUG,
648 "Extending existing path %s to create inverse for incoming connection\n",
649 GCPP_2s (cm_ctx.match));
650 extend_path (cm_ctx.match,
654 /* Check that extension was successful */
655 GNUNET_assert (cm_ctx.match->entries_length == path_length);
658 /* Eh, we found a match but couldn't use it? Something is wrong. */
663 /* No match at all, create completely new path */
664 path = GNUNET_new (struct CadetPeerPath);
665 path->entries_length = path_length;
666 path->entries = GNUNET_new_array (path->entries_length,
667 struct CadetPeerPathEntry *);
668 for (int i=path_length-1;i>=0;i--)
670 struct CadetPeerPathEntry *entry = GNUNET_new (struct CadetPeerPathEntry);
672 path->entries[i] = entry;
673 entry->peer = cpath[i];
676 for (int i=path_length-1;i>=0;i--)
678 struct CadetPeerPathEntry *entry = path->entries[i];
680 GCP_path_entry_add (entry->peer,
684 recalculate_path_desirability (path);
685 LOG (GNUNET_ERROR_TYPE_DEBUG,
686 "Created new path %s to create inverse for incoming connection\n",
688 path->hn = GCP_attach_path (cpath[path_length - 1],
697 * Return the length of the path. Excludes one end of the
698 * path, so the loopback path has length 0.
700 * @param path path to return the length for
701 * @return number of peers on the path
704 GCPP_get_length (struct CadetPeerPath *path)
706 return path->entries_length;
711 * Find peer's offset on path.
713 * @param path path to search
714 * @param cp peer to look for
715 * @return offset of @a cp on @a path, or UINT_MAX if not found
718 GCPP_find_peer (struct CadetPeerPath *path,
719 struct CadetPeer *cp)
721 for (unsigned int off = 0;
722 off < path->entries_length;
724 if (cp == GCPP_get_peer_at_offset (path,
732 * Obtain the peer at offset @a off in @a path.
734 * @param path peer path to inspect
735 * @param off offset to return, must be smaller than path length
736 * @return the peer at offset @a off
739 GCPP_get_peer_at_offset (struct CadetPeerPath *path,
742 GNUNET_assert (off < path->entries_length);
743 return path->entries[off]->peer;
748 * Convert a path to a human-readable string.
750 * @param path path to convert
751 * @return string, to be freed by caller (unlike other *_2s APIs!)
754 GCPP_2s (struct CadetPeerPath *path)
756 static char buf[2048];
758 const unsigned int max_plen = (sizeof(buf) - 16) / 5 - 2; /* 5 characters per entry */
761 for (unsigned int i = 0;
762 i < path->entries_length;
765 if ( (path->entries_length > max_plen) &&
766 (i == max_plen / 2) )
767 off += GNUNET_snprintf (&buf[off],
770 if ( (path->entries_length > max_plen) &&
771 (i > max_plen / 2) &&
772 (i < path->entries_length - max_plen / 2) )
774 off += GNUNET_snprintf (&buf[off],
777 GNUNET_i2s (GCP_get_id (GCPP_get_peer_at_offset (path,
779 (i == path->entries_length -1) ? "" : "-");
781 GNUNET_snprintf (&buf[off],
789 /* end of gnunet-service-cadet-new_paths.c */