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
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.
39 struct CadetPeerPath {
41 * Array of all the peers on the path. If @e hn is non-NULL, the
42 * last one is our owner.
44 struct CadetPeerPathEntry **entries;
47 * Node of this path in the owner's heap. Used to update our position
48 * in the heap whenever our @e desirability changes.
50 struct GNUNET_CONTAINER_HeapNode *hn;
53 * Desirability of the path. How unique is it for the various peers
56 GNUNET_CONTAINER_HeapCostType desirability;
59 * Length of the @e entries array.
61 unsigned int entries_length;
66 * Calculate the path's desirability score.
68 * @param path path to calculate the score for
71 recalculate_path_desirability(struct CadetPeerPath *path)
75 for (unsigned int i = 0; i < path->entries_length; i++)
77 struct CadetPeer *cp = path->entries[i]->peer;
79 result += GCP_get_desirability_of_path(cp,
82 path->desirability = (GNUNET_CONTAINER_HeapCostType)result;
87 * Return how much we like keeping the path. This is an aggregate
88 * score based on various factors, including the age of the path
89 * (older == better), and the value of this path to all of its ajacent
90 * peers. For example, long paths that end at a peer that we have no
91 * shorter way to reach are very desirable, while long paths that end
92 * at a peer for which we have a shorter way as well are much less
93 * desirable. Higher values indicate more valuable paths. The
94 * returned value should be used to decide which paths to remember.
96 * @param path path to return the length for
97 * @return desirability of the path, larger is more desirable
99 GNUNET_CONTAINER_HeapCostType
100 GCPP_get_desirability(const struct CadetPeerPath *path)
102 return path->desirability;
107 * Return connection to @a destination using @a path, or return
108 * NULL if no such connection exists.
110 * @param path path to traverse
111 * @param destination destination node to get to, must be on path
112 * @param off offset of @a destination on @a path
113 * @return NULL if we have no existing connection
114 * otherwise connection from us to @a destination via @a path
116 struct CadetConnection *
117 GCPP_get_connection(struct CadetPeerPath *path,
118 struct CadetPeer *destination,
121 struct CadetPeerPathEntry *entry;
123 GNUNET_assert(off < path->entries_length);
124 entry = path->entries[off];
125 GNUNET_assert(entry->peer == destination);
131 * Notify @a path that it is used for connection @a cc
132 * which ends at the path's offset @a off.
134 * @param path the path to remember the @a cc
135 * @param off the offset where the @a cc ends
136 * @param cc the connection to remember
139 GCPP_add_connection(struct CadetPeerPath *path,
141 struct CadetConnection *cc)
143 struct CadetPeerPathEntry *entry;
145 LOG(GNUNET_ERROR_TYPE_DEBUG,
146 "Adding %s to path %s at offset %u\n",
150 GNUNET_assert(off < path->entries_length);
151 entry = path->entries[off];
152 GNUNET_assert(NULL == entry->cc);
153 GNUNET_assert(NULL != cc);
160 * Notify @a path that it is no longer used for connection @a cc which
161 * ended at the path's offset @a off.
163 * @param path the path to forget the @a cc
164 * @param off the offset where the @a cc ended
165 * @param cc the connection to forget
168 GCPP_del_connection(struct CadetPeerPath *path,
170 struct CadetConnection *cc)
172 struct CadetPeerPathEntry *entry;
174 LOG(GNUNET_ERROR_TYPE_DEBUG,
175 "Removing connection %s to path %s at offset %u\n",
179 GNUNET_assert(off < path->entries_length);
180 entry = path->entries[off];
181 GNUNET_assert(cc == entry->cc);
187 * Tries to attach @a path to a peer, working backwards from the end
188 * and stopping at @a stop_at. If path->hn is NULL on return then the
189 * path was not attached and you can assume that path->entries_length
190 * is equal to @a stop_at.
192 * @param path the path to attach
193 * @param stop_at the path length at which to stop trying
196 attach_path(struct CadetPeerPath *path, unsigned int stop_at)
198 GNUNET_assert(NULL == path->hn);
200 /* Try to attach this path to a peer, working backwards from the end. */
201 while (path->entries_length > stop_at)
203 unsigned int end = path->entries_length - 1;
204 struct CadetPeerPathEntry *entry = path->entries[end];
205 int force = GNUNET_NO;
207 recalculate_path_desirability(path);
208 /* If the entry already has a connection using it, force attach. */
209 if (NULL != entry->cc)
211 path->hn = GCP_attach_path(entry->peer,
215 if (NULL != path->hn)
218 /* Attach failed, trim this entry from the path. */
219 GNUNET_assert(NULL == entry->cc);
220 GCP_path_entry_remove(entry->peer,
224 path->entries[end] = NULL;
225 path->entries_length--;
228 /* Shrink array to actual path length. */
229 GNUNET_array_grow(path->entries,
230 path->entries_length,
231 path->entries_length);
236 * The owning peer of this path is no longer interested in maintaining
237 * it, so the path should be discarded or shortened (in case a
238 * previous peer on the path finds the path desirable).
240 * @param path the path that is being released
243 GCPP_release(struct CadetPeerPath *path)
245 struct CadetPeerPathEntry *entry;
247 LOG(GNUNET_ERROR_TYPE_DEBUG,
248 "Owner releases path %s\n",
251 entry = path->entries[path->entries_length - 1];
252 GNUNET_assert(path == entry->path);
253 GNUNET_assert(NULL == entry->cc);
254 /* cut 'off' end of path */
255 GCP_path_entry_remove(entry->peer,
257 path->entries_length - 1);
259 path->entries[path->entries_length - 1] = NULL;
260 path->entries_length--;
261 /* see if new peer at the end likes this path any better */
262 attach_path(path, 0);
263 if (NULL == path->hn)
265 /* nobody wants us, discard the path */
266 GNUNET_assert(0 == path->entries_length);
267 GNUNET_assert(NULL == path->entries);
274 * Updates the score for an entry on the path based
275 * on our experiences with using @a path.
277 * @param path the path to update
278 * @param off offset of the entry to update
279 * @param delta change in the score to apply
282 GCPP_update_score(struct CadetPeerPath *path,
286 struct CadetPeerPathEntry *entry;
288 GNUNET_assert(off < path->entries_length);
289 entry = path->entries[off];
291 /* Add delta, with checks for overflows */
294 if (delta + entry->score < entry->score)
295 entry->score = INT_MAX;
297 entry->score += delta;
301 if (delta + entry->score > entry->score)
302 entry->score = INT_MIN;
304 entry->score += delta;
306 recalculate_path_desirability(path);
311 * Closure for #find_peer_at() and #check_match().
313 struct CheckMatchContext {
315 * Set to a matching path, if any.
317 struct CadetPeerPath *match;
320 * Array the combined paths.
322 struct CadetPeer **cpath;
325 * How long is the @e cpath array?
327 unsigned int cpath_length;
332 * Check if the given path is identical on all of the
333 * hops until @a off, and not longer than @a off. If the
334 * @a path matches, store it in `match`.
336 * @param cls the `struct CheckMatchContext` to check against
337 * @param path the path to check
338 * @param off offset to check at
339 * @return #GNUNET_YES (continue to iterate), or if found #GNUNET_NO
342 check_match(void *cls,
343 struct CadetPeerPath *path,
346 struct CheckMatchContext *cm_ctx = cls;
348 GNUNET_assert(path->entries_length > off);
349 if ((path->entries_length != off + 1) &&
350 (off + 1 != cm_ctx->cpath_length))
352 LOG(GNUNET_ERROR_TYPE_DEBUG,
353 "check_match mismatch because path %s is too long (%u vs. %u vs. %u)\n",
355 path->entries_length,
357 cm_ctx->cpath_length);
358 return GNUNET_YES; /* too long, goes somewhere else already, thus cannot be useful */
360 for (unsigned int i = 0; i < off; i++)
361 if (cm_ctx->cpath[i] !=
362 GCPP_get_peer_at_offset(path,
365 LOG(GNUNET_ERROR_TYPE_DEBUG,
366 "check_match path %s mismatches at offset %u\n",
369 return GNUNET_YES; /* mismatch, ignore */
371 LOG(GNUNET_ERROR_TYPE_DEBUG,
372 "check_match found match with path %s\n",
374 cm_ctx->match = path;
375 return GNUNET_NO; /* match, we are done! */
380 * Extend path @a path by the @a num_peers from the @a peers
381 * array, assuming the owners past the current owner want it.
383 * @param path path to extend
384 * @param peers list of peers beyond the end of @a path
385 * @param num_peers length of the @a peers array
386 * @param force force attachment, even if we have other
390 extend_path(struct CadetPeerPath *path,
391 struct CadetPeer **peers,
392 unsigned int num_peers,
395 unsigned int old_len = path->entries_length;
399 GNUNET_array_grow(path->entries,
400 path->entries_length,
401 old_len + num_peers);
402 for (i = num_peers - 1; i >= 0; i--)
404 struct CadetPeerPathEntry *entry = GNUNET_new(struct CadetPeerPathEntry);
406 path->entries[old_len + i] = entry;
407 entry->peer = peers[i];
410 for (i = num_peers - 1; i >= 0; i--)
412 struct CadetPeerPathEntry *entry = path->entries[old_len + i];
414 GCP_path_entry_add(entry->peer,
419 /* If we extend an existing path, detach it from the
420 old owner and re-attach to the new one */
421 GCP_detach_path(path->entries[old_len - 1]->peer,
425 path->entries_length = old_len + num_peers;
426 if (GNUNET_YES == force)
428 int end = path->entries_length - 1;
430 path->hn = GCP_attach_path(path->entries[end]->peer,
437 attach_path(path, old_len);
439 if (NULL == path->hn)
441 /* none of the peers is interested in this path;
443 GNUNET_assert(old_len == path->entries_length);
444 path->hn = GCP_attach_path(path->entries[old_len - 1]->peer,
448 GNUNET_assert(NULL != path->hn);
451 LOG(GNUNET_ERROR_TYPE_DEBUG,
452 "Extended path %s\n",
458 * Create a peer path based on the result of a DHT lookup. If we
459 * already know this path, or one that is longer, simply return NULL.
460 * Otherwise, we try to extend an existing path, or create a new one
463 * @param get_path path of the get request
464 * @param get_path_length lenght of @a get_path
465 * @param put_path path of the put request
466 * @param put_path_length length of the @a put_path
467 * @return a path through the network
470 GCPP_try_path_from_dht(const struct GNUNET_PeerIdentity *get_path,
471 unsigned int get_path_length,
472 const struct GNUNET_PeerIdentity *put_path,
473 unsigned int put_path_length)
475 struct CadetPeer *cpath[get_path_length + put_path_length];
476 struct CheckMatchContext cm_ctx;
477 struct CadetPeerPath *path;
479 unsigned int total_len;
481 /* precompute 'cpath' so we can avoid doing the lookups lots of times */
485 sizeof(cpath)); /* Just to trigger harder errors later. */
486 total_len = get_path_length + put_path_length;
487 for (unsigned int off = 0; off < total_len; off++)
489 const struct GNUNET_PeerIdentity *pid;
491 pid = (off < get_path_length)
492 ? &get_path[get_path_length - off - 1]
493 : &put_path[get_path_length + put_path_length - off - 1];
494 /* Check that I am not in the path */
495 if (0 == GNUNET_memcmp(&my_full_id,
501 cpath[off - skip] = GCP_get(pid,
503 /* Check that no peer is twice on the path */
504 for (unsigned int i = 0; i < off - skip; i++)
506 if (cpath[i] == cpath[off - skip])
513 if (skip >= total_len)
515 LOG(GNUNET_ERROR_TYPE_DEBUG,
516 "Path discovered from DHT is one big cycle?\n");
521 /* First figure out if this path is a subset of an existing path, an
522 extension of an existing path, or a new path. */
523 cm_ctx.cpath_length = total_len;
524 cm_ctx.cpath = cpath;
526 for (int i = total_len - 1; i >= 0; i--)
528 GCP_iterate_paths_at(cpath[i],
532 if (NULL != cm_ctx.match)
534 if (i == total_len - 1)
536 /* Existing path includes this one, nothing to do! */
537 LOG(GNUNET_ERROR_TYPE_DEBUG,
538 "Path discovered from DHT is already known\n");
541 if (cm_ctx.match->entries_length == i + 1)
543 /* Existing path ends in the middle of new path, extend it! */
544 LOG(GNUNET_ERROR_TYPE_DEBUG,
545 "Trying to extend existing path %s by additional links discovered from DHT\n",
546 GCPP_2s(cm_ctx.match));
547 extend_path(cm_ctx.match,
556 /* No match at all, create completely new path */
557 path = GNUNET_new(struct CadetPeerPath);
558 path->entries_length = total_len;
559 path->entries = GNUNET_new_array(path->entries_length,
560 struct CadetPeerPathEntry *);
561 for (int i = path->entries_length - 1; i >= 0; i--)
563 struct CadetPeerPathEntry *entry = GNUNET_new(struct CadetPeerPathEntry);
565 path->entries[i] = entry;
566 entry->peer = cpath[i];
569 for (int i = path->entries_length - 1; i >= 0; i--)
571 struct CadetPeerPathEntry *entry = path->entries[i];
573 GCP_path_entry_add(entry->peer,
578 /* Finally, try to attach it */
579 attach_path(path, 0);
580 if (NULL == path->hn)
582 /* None of the peers on the path care about it. */
583 LOG(GNUNET_ERROR_TYPE_DEBUG,
584 "Path discovered from DHT is not interesting to us\n");
585 GNUNET_assert(0 == path->entries_length);
586 GNUNET_assert(NULL == path->entries);
590 LOG(GNUNET_ERROR_TYPE_DEBUG,
591 "Created new path %s based on information from DHT\n",
597 * We got an incoming connection, obtain the corresponding path.
599 * @param path_length number of segments on the @a path
600 * @param pids path through the network, in reverse order (we are at the end at index @a path_length)
601 * @return corresponding path object
603 struct CadetPeerPath *
604 GCPP_get_path_from_route(unsigned int path_length,
605 const struct GNUNET_PeerIdentity *pids)
607 struct CheckMatchContext cm_ctx;
608 struct CadetPeer *cpath[path_length];
609 struct CadetPeerPath *path;
611 /* precompute inverted 'cpath' so we can avoid doing the lookups and
612 have the correct order */
613 for (unsigned int off = 0; off < path_length; off++)
614 cpath[off] = GCP_get(&pids[path_length - 1 - off],
617 /* First figure out if this path is a subset of an existing path, an
618 extension of an existing path, or a new path. */
619 cm_ctx.cpath = cpath;
620 cm_ctx.cpath_length = path_length;
622 for (int i = path_length - 1; i >= 0; i--)
624 GCP_iterate_paths_at(cpath[i],
628 if (NULL != cm_ctx.match)
630 if (i == path_length - 1)
632 /* Existing path includes this one, return the match! */
633 LOG(GNUNET_ERROR_TYPE_DEBUG,
634 "Returning existing path %s as inverse for incoming connection\n",
635 GCPP_2s(cm_ctx.match));
638 if (cm_ctx.match->entries_length == i + 1)
640 /* Existing path ends in the middle of new path, extend it! */
641 LOG(GNUNET_ERROR_TYPE_DEBUG,
642 "Extending existing path %s to create inverse for incoming connection\n",
643 GCPP_2s(cm_ctx.match));
644 extend_path(cm_ctx.match,
648 /* Check that extension was successful */
649 GNUNET_assert(cm_ctx.match->entries_length == path_length);
652 /* Eh, we found a match but couldn't use it? Something is wrong. */
657 /* No match at all, create completely new path */
658 path = GNUNET_new(struct CadetPeerPath);
659 path->entries_length = path_length;
660 path->entries = GNUNET_new_array(path->entries_length,
661 struct CadetPeerPathEntry *);
662 for (int i = path_length - 1; i >= 0; i--)
664 struct CadetPeerPathEntry *entry = GNUNET_new(struct CadetPeerPathEntry);
666 path->entries[i] = entry;
667 entry->peer = cpath[i];
670 for (int i = path_length - 1; i >= 0; i--)
672 struct CadetPeerPathEntry *entry = path->entries[i];
674 GCP_path_entry_add(entry->peer,
678 recalculate_path_desirability(path);
679 LOG(GNUNET_ERROR_TYPE_DEBUG,
680 "Created new path %s to create inverse for incoming connection\n",
682 path->hn = GCP_attach_path(cpath[path_length - 1],
691 * Return the length of the path. Excludes one end of the
692 * path, so the loopback path has length 0.
694 * @param path path to return the length for
695 * @return number of peers on the path
698 GCPP_get_length(struct CadetPeerPath *path)
700 return path->entries_length;
705 * Find peer's offset on path.
707 * @param path path to search
708 * @param cp peer to look for
709 * @return offset of @a cp on @a path, or UINT_MAX if not found
712 GCPP_find_peer(struct CadetPeerPath *path,
713 struct CadetPeer *cp)
715 for (unsigned int off = 0;
716 off < path->entries_length;
718 if (cp == GCPP_get_peer_at_offset(path,
726 * Obtain the peer at offset @a off in @a path.
728 * @param path peer path to inspect
729 * @param off offset to return, must be smaller than path length
730 * @return the peer at offset @a off
733 GCPP_get_peer_at_offset(struct CadetPeerPath *path,
736 GNUNET_assert(off < path->entries_length);
737 return path->entries[off]->peer;
742 * Convert a path to a human-readable string.
744 * @param path path to convert
745 * @return string, to be freed by caller (unlike other *_2s APIs!)
748 GCPP_2s(struct CadetPeerPath *path)
750 static char buf[2048];
752 const unsigned int max_plen = (sizeof(buf) - 16) / 5 - 2; /* 5 characters per entry */
755 for (unsigned int i = 0;
756 i < path->entries_length;
759 if ((path->entries_length > max_plen) &&
761 off += GNUNET_snprintf(&buf[off],
764 if ((path->entries_length > max_plen) &&
765 (i > max_plen / 2) &&
766 (i < path->entries_length - max_plen / 2))
768 off += GNUNET_snprintf(&buf[off],
771 GNUNET_i2s(GCP_get_id(GCPP_get_peer_at_offset(path,
773 (i == path->entries_length - 1) ? "" : "-");
775 GNUNET_snprintf(&buf[off],
783 /* end of gnunet-service-cadet-new_paths.c */