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/>.
19 * @file cadet/gnunet-service-cadet_paths.c
20 * @brief Information we track per path.
21 * @author Bartlomiej Polot
22 * @author Christian Grothoff
25 #include "gnunet-service-cadet_connection.h"
26 #include "gnunet-service-cadet_tunnels.h"
27 #include "gnunet-service-cadet_peer.h"
28 #include "gnunet-service-cadet_paths.h"
31 #define LOG(level, ...) GNUNET_log_from(level,"cadet-pat",__VA_ARGS__)
35 * Information regarding a possible path to reach a peer.
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;
67 * Calculate the path's desirability score.
69 * @param path path to calculate the score for
72 recalculate_path_desirability (struct CadetPeerPath *path)
76 for (unsigned int i=0;i<path->entries_length;i++)
78 struct CadetPeer *cp = path->entries[i]->peer;
80 result += GCP_get_desirability_of_path (cp,
83 path->desirability = (GNUNET_CONTAINER_HeapCostType) result;
88 * Return how much we like keeping the path. This is an aggregate
89 * score based on various factors, including the age of the path
90 * (older == better), and the value of this path to all of its ajacent
91 * peers. For example, long paths that end at a peer that we have no
92 * shorter way to reach are very desirable, while long paths that end
93 * at a peer for which we have a shorter way as well are much less
94 * desirable. Higher values indicate more valuable paths. The
95 * returned value should be used to decide which paths to remember.
97 * @param path path to return the length for
98 * @return desirability of the path, larger is more desirable
100 GNUNET_CONTAINER_HeapCostType
101 GCPP_get_desirability (const struct CadetPeerPath *path)
103 return path->desirability;
108 * Return connection to @a destination using @a path, or return
109 * NULL if no such connection exists.
111 * @param path path to traverse
112 * @param destination destination node to get to, must be on path
113 * @param off offset of @a destination on @a path
114 * @return NULL if we have no existing connection
115 * otherwise connection from us to @a destination via @a path
117 struct CadetConnection *
118 GCPP_get_connection (struct CadetPeerPath *path,
119 struct CadetPeer *destination,
122 struct CadetPeerPathEntry *entry;
124 GNUNET_assert (off < path->entries_length);
125 entry = path->entries[off];
126 GNUNET_assert (entry->peer == destination);
132 * Notify @a path that it is used for connection @a cc
133 * which ends at the path's offset @a off.
135 * @param path the path to remember the @a cc
136 * @param off the offset where the @a cc ends
137 * @param cc the connection to remember
140 GCPP_add_connection (struct CadetPeerPath *path,
142 struct CadetConnection *cc)
144 struct CadetPeerPathEntry *entry;
146 LOG (GNUNET_ERROR_TYPE_DEBUG,
147 "Adding %s to path %s at offset %u\n",
151 GNUNET_assert (off < path->entries_length);
152 entry = path->entries[off];
153 GNUNET_assert (NULL == entry->cc);
154 GNUNET_assert (NULL != cc);
161 * Notify @a path that it is no longer used for connection @a cc which
162 * ended at the path's offset @a off.
164 * @param path the path to forget the @a cc
165 * @param off the offset where the @a cc ended
166 * @param cc the connection to forget
169 GCPP_del_connection (struct CadetPeerPath *path,
171 struct CadetConnection *cc)
173 struct CadetPeerPathEntry *entry;
175 LOG (GNUNET_ERROR_TYPE_DEBUG,
176 "Removing connection %s to path %s at offset %u\n",
180 GNUNET_assert (off < path->entries_length);
181 entry = path->entries[off];
182 GNUNET_assert (cc == entry->cc);
188 * Tries to attach @a path to a peer, working backwards from the end
189 * and stopping at @a stop_at. If path->hn is NULL on return then the
190 * path was not attached and you can assume that path->entries_length
191 * is equal to @a stop_at.
193 * @param path the path to attach
194 * @param stop_at the path length at which to stop trying
197 attach_path (struct CadetPeerPath *path, unsigned int stop_at)
199 GNUNET_assert (NULL == path->hn);
201 /* Try to attach this path to a peer, working backwards from the end. */
202 while (path->entries_length > stop_at)
204 unsigned int end = path->entries_length - 1;
205 struct CadetPeerPathEntry *entry = path->entries[end];
206 int force = GNUNET_NO;
208 recalculate_path_desirability (path);
209 /* If the entry already has a connection using it, force attach. */
210 if (NULL != entry->cc)
212 path->hn = GCP_attach_path (entry->peer,
216 if (NULL != path->hn)
219 /* Attach failed, trim this entry from the path. */
220 GNUNET_assert (NULL == entry->cc);
221 GCP_path_entry_remove (entry->peer,
225 path->entries[end] = NULL;
226 path->entries_length--;
229 /* Shrink array to actual path length. */
230 GNUNET_array_grow (path->entries,
231 path->entries_length,
232 path->entries_length);
237 * The owning peer of this path is no longer interested in maintaining
238 * it, so the path should be discarded or shortened (in case a
239 * previous peer on the path finds the path desirable).
241 * @param path the path that is being released
244 GCPP_release (struct CadetPeerPath *path)
246 struct CadetPeerPathEntry *entry;
248 LOG (GNUNET_ERROR_TYPE_DEBUG,
249 "Owner releases path %s\n",
252 entry = path->entries[path->entries_length - 1];
253 GNUNET_assert (path == entry->path);
254 GNUNET_assert (NULL == entry->cc);
255 /* cut 'off' end of path */
256 GCP_path_entry_remove (entry->peer,
258 path->entries_length - 1);
260 path->entries[path->entries_length - 1] = NULL;
261 path->entries_length--;
262 /* see if new peer at the end likes this path any better */
263 attach_path (path, 0);
264 if (NULL == path->hn)
266 /* nobody wants us, discard the path */
267 GNUNET_assert (0 == path->entries_length);
268 GNUNET_assert (NULL == path->entries);
275 * Updates the score for an entry on the path based
276 * on our experiences with using @a path.
278 * @param path the path to update
279 * @param off offset of the entry to update
280 * @param delta change in the score to apply
283 GCPP_update_score (struct CadetPeerPath *path,
287 struct CadetPeerPathEntry *entry;
289 GNUNET_assert (off < path->entries_length);
290 entry = path->entries[off];
292 /* Add delta, with checks for overflows */
295 if (delta + entry->score < entry->score)
296 entry->score = INT_MAX;
298 entry->score += delta;
302 if (delta + entry->score > entry->score)
303 entry->score = INT_MIN;
305 entry->score += delta;
307 recalculate_path_desirability (path);
312 * Closure for #find_peer_at() and #check_match().
314 struct CheckMatchContext
318 * Set to a matching path, if any.
320 struct CadetPeerPath *match;
323 * Array the combined paths.
325 struct CadetPeer **cpath;
328 * How long is the @e cpath array?
330 unsigned int cpath_length;
336 * Check if the given path is identical on all of the
337 * hops until @a off, and not longer than @a off. If the
338 * @a path matches, store it in `match`.
340 * @param cls the `struct CheckMatchContext` to check against
341 * @param path the path to check
342 * @param off offset to check at
343 * @return #GNUNET_YES (continue to iterate), or if found #GNUNET_NO
346 check_match (void *cls,
347 struct CadetPeerPath *path,
350 struct CheckMatchContext *cm_ctx = cls;
352 GNUNET_assert (path->entries_length > off);
353 if ( (path->entries_length != off + 1) &&
354 (off + 1 != cm_ctx->cpath_length) )
356 LOG (GNUNET_ERROR_TYPE_DEBUG,
357 "check_match missmatch because path %s is too long (%u vs. %u vs. %u)\n",
359 path->entries_length,
361 cm_ctx->cpath_length);
362 return GNUNET_YES; /* too long, goes somewhere else already, thus cannot be useful */
364 for (unsigned int i=0;i<off;i++)
365 if (cm_ctx->cpath[i] !=
366 GCPP_get_peer_at_offset (path,
369 LOG (GNUNET_ERROR_TYPE_DEBUG,
370 "check_match path %s missmatches at offset %u\n",
373 return GNUNET_YES; /* missmatch, ignore */
375 LOG (GNUNET_ERROR_TYPE_DEBUG,
376 "check_match found match with path %s\n",
378 cm_ctx->match = path;
379 return GNUNET_NO; /* match, we are done! */
384 * Extend path @a path by the @a num_peers from the @a peers
385 * array, assuming the owners past the current owner want it.
387 * @param path path to extend
388 * @param peers list of peers beyond the end of @a path
389 * @param num_peers length of the @a peers array
390 * @param force force attachment, even if we have other
394 extend_path (struct CadetPeerPath *path,
395 struct CadetPeer **peers,
396 unsigned int num_peers,
399 unsigned int old_len = path->entries_length;
403 GNUNET_array_grow (path->entries,
404 path->entries_length,
405 old_len + num_peers);
406 for (i=num_peers-1;i >= 0;i--)
408 struct CadetPeerPathEntry *entry = GNUNET_new (struct CadetPeerPathEntry);
410 path->entries[old_len + i] = entry;
411 entry->peer = peers[i];
414 for (i=num_peers-1;i >= 0;i--)
416 struct CadetPeerPathEntry *entry = path->entries[old_len + i];
418 GCP_path_entry_add (entry->peer,
423 /* If we extend an existing path, detach it from the
424 old owner and re-attach to the new one */
425 GCP_detach_path (path->entries[old_len-1]->peer,
429 path->entries_length = old_len + num_peers;
430 if (GNUNET_YES == force)
432 int end = path->entries_length - 1;
434 path->hn = GCP_attach_path (path->entries[end]->peer,
439 attach_path (path, old_len);
441 if (NULL == path->hn)
443 /* none of the peers is interested in this path;
445 GNUNET_assert (old_len == path->entries_length);
446 path->hn = GCP_attach_path (path->entries[old_len - 1]->peer,
450 GNUNET_assert (NULL != path->hn);
453 LOG (GNUNET_ERROR_TYPE_DEBUG,
454 "Extended path %s\n",
460 * Create a peer path based on the result of a DHT lookup. If we
461 * already know this path, or one that is longer, simply return NULL.
462 * Otherwise, we try to extend an existing path, or create a new one
465 * @param get_path path of the get request
466 * @param get_path_length lenght of @a get_path
467 * @param put_path path of the put request
468 * @param put_path_length length of the @a put_path
469 * @return a path through the network
472 GCPP_try_path_from_dht (const struct GNUNET_PeerIdentity *get_path,
473 unsigned int get_path_length,
474 const struct GNUNET_PeerIdentity *put_path,
475 unsigned int put_path_length)
477 struct CadetPeer *cpath[get_path_length + put_path_length];
478 struct CheckMatchContext cm_ctx;
479 struct CadetPeerPath *path;
482 unsigned int total_len;
484 /* precompute 'cpath' so we can avoid doing the lookups lots of times */
488 sizeof (cpath)); /* Just to trigger harder errors later. */
489 total_len = get_path_length + put_path_length;
490 for (unsigned int off=0;off<total_len;off++)
492 const struct GNUNET_PeerIdentity *pid;
494 pid = (off < get_path_length)
495 ? &get_path[get_path_length - off - 1]
496 : &put_path[get_path_length + put_path_length - off - 1];
497 /* Check that I am not in the path */
498 if (0 == memcmp (&my_full_id,
500 sizeof (struct GNUNET_PeerIdentity)))
505 cpath[off - skip] = GCP_get (pid,
507 /* Check that no peer is twice on the path */
508 for (unsigned int i=0;i<off - skip;i++)
510 if (cpath[i] == cpath[off - skip])
517 if (skip >= total_len)
519 LOG (GNUNET_ERROR_TYPE_DEBUG,
520 "Path discovered from DHT is one big cycle?\n");
525 /* First figure out if this path is a subset of an existing path, an
526 extension of an existing path, or a new path. */
527 cm_ctx.cpath_length = total_len;
528 cm_ctx.cpath = cpath;
530 for (i=total_len-1;i>=0;i--)
532 GCP_iterate_paths_at (cpath[i],
536 if (NULL != cm_ctx.match)
538 if (i == total_len - 1)
540 /* Existing path includes this one, nothing to do! */
541 LOG (GNUNET_ERROR_TYPE_DEBUG,
542 "Path discovered from DHT is already known\n");
545 if (cm_ctx.match->entries_length == i + 1)
547 /* Existing path ends in the middle of new path, extend it! */
548 LOG (GNUNET_ERROR_TYPE_DEBUG,
549 "Trying to extend existing path %s by additional links discovered from DHT\n",
550 GCPP_2s (cm_ctx.match));
551 extend_path (cm_ctx.match,
560 /* No match at all, create completely new path */
561 path = GNUNET_new (struct CadetPeerPath);
562 path->entries_length = total_len;
563 path->entries = GNUNET_new_array (path->entries_length,
564 struct CadetPeerPathEntry *);
565 for (i=path->entries_length-1;i>=0;i--)
567 struct CadetPeerPathEntry *entry = GNUNET_new (struct CadetPeerPathEntry);
569 path->entries[i] = entry;
570 entry->peer = cpath[i];
573 for (i=path->entries_length-1;i>=0;i--)
575 struct CadetPeerPathEntry *entry = path->entries[i];
577 GCP_path_entry_add (entry->peer,
582 /* Finally, try to attach it */
583 attach_path (path, 0);
584 if (NULL == path->hn)
586 /* None of the peers on the path care about it. */
587 LOG (GNUNET_ERROR_TYPE_DEBUG,
588 "Path discovered from DHT is not interesting to us\n");
589 GNUNET_assert (0 == path->entries_length);
590 GNUNET_assert (NULL == path->entries);
594 LOG (GNUNET_ERROR_TYPE_DEBUG,
595 "Created new path %s based on information from DHT\n",
601 * We got an incoming connection, obtain the corresponding path.
603 * @param path_length number of segments on the @a path
604 * @param pids path through the network, in reverse order (we are at the end at index @a path_length)
605 * @return corresponding path object
607 struct CadetPeerPath *
608 GCPP_get_path_from_route (unsigned int path_length,
609 const struct GNUNET_PeerIdentity *pids)
611 struct CheckMatchContext cm_ctx;
612 struct CadetPeer *cpath[path_length];
613 struct CadetPeerPath *path;
615 /* precompute inverted 'cpath' so we can avoid doing the lookups and
616 have the correct order */
617 for (unsigned int off=0;off<path_length;off++)
618 cpath[off] = GCP_get (&pids[path_length - 1 - off],
621 /* First figure out if this path is a subset of an existing path, an
622 extension of an existing path, or a new path. */
623 cm_ctx.cpath = cpath;
624 cm_ctx.cpath_length = path_length;
626 for (int i=path_length-1;i>=0;i--)
628 GCP_iterate_paths_at (cpath[i],
632 if (NULL != cm_ctx.match)
634 if (i == path_length - 1)
636 /* Existing path includes this one, return the match! */
637 LOG (GNUNET_ERROR_TYPE_DEBUG,
638 "Returning existing path %s as inverse for incoming connection\n",
639 GCPP_2s (cm_ctx.match));
642 if (cm_ctx.match->entries_length == i + 1)
644 /* Existing path ends in the middle of new path, extend it! */
645 LOG (GNUNET_ERROR_TYPE_DEBUG,
646 "Extending existing path %s to create inverse for incoming connection\n",
647 GCPP_2s (cm_ctx.match));
648 extend_path (cm_ctx.match,
652 /* Check that extension was successful */
653 GNUNET_assert (cm_ctx.match->entries_length == path_length);
656 /* Eh, we found a match but couldn't use it? Something is wrong. */
661 /* No match at all, create completely new path */
662 path = GNUNET_new (struct CadetPeerPath);
663 path->entries_length = path_length;
664 path->entries = GNUNET_new_array (path->entries_length,
665 struct CadetPeerPathEntry *);
666 for (int i=path_length-1;i>=0;i--)
668 struct CadetPeerPathEntry *entry = GNUNET_new (struct CadetPeerPathEntry);
670 path->entries[i] = entry;
671 entry->peer = cpath[i];
674 for (int i=path_length-1;i>=0;i--)
676 struct CadetPeerPathEntry *entry = path->entries[i];
678 GCP_path_entry_add (entry->peer,
682 recalculate_path_desirability (path);
683 LOG (GNUNET_ERROR_TYPE_DEBUG,
684 "Created new path %s to create inverse for incoming connection\n",
686 path->hn = GCP_attach_path (cpath[path_length - 1],
695 * Return the length of the path. Excludes one end of the
696 * path, so the loopback path has length 0.
698 * @param path path to return the length for
699 * @return number of peers on the path
702 GCPP_get_length (struct CadetPeerPath *path)
704 return path->entries_length;
709 * Find peer's offset on path.
711 * @param path path to search
712 * @param cp peer to look for
713 * @return offset of @a cp on @a path, or UINT_MAX if not found
716 GCPP_find_peer (struct CadetPeerPath *path,
717 struct CadetPeer *cp)
719 for (unsigned int off = 0;
720 off < path->entries_length;
722 if (cp == GCPP_get_peer_at_offset (path,
730 * Obtain the peer at offset @a off in @a path.
732 * @param path peer path to inspect
733 * @param off offset to return, must be smaller than path length
734 * @return the peer at offset @a off
737 GCPP_get_peer_at_offset (struct CadetPeerPath *path,
740 GNUNET_assert (off < path->entries_length);
741 return path->entries[off]->peer;
746 * Convert a path to a human-readable string.
748 * @param path path to convert
749 * @return string, to be freed by caller (unlike other *_2s APIs!)
752 GCPP_2s (struct CadetPeerPath *path)
754 static char buf[2048];
756 const unsigned int max_plen = (sizeof(buf) - 16) / 5 - 2; /* 5 characters per entry */
759 for (unsigned int i = 0;
760 i < path->entries_length;
763 if ( (path->entries_length > max_plen) &&
764 (i == max_plen / 2) )
765 off += GNUNET_snprintf (&buf[off],
768 if ( (path->entries_length > max_plen) &&
769 (i > max_plen / 2) &&
770 (i < path->entries_length - max_plen / 2) )
772 off += GNUNET_snprintf (&buf[off],
775 GNUNET_i2s (GCP_get_id (GCPP_get_peer_at_offset (path,
777 (i == path->entries_length -1) ? "" : "-");
779 GNUNET_snprintf (&buf[off],
787 /* end of gnunet-service-cadet-new_paths.c */