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 mismatch 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 mismatches at offset %u\n",
373 return GNUNET_YES; /* mismatch, 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;
481 unsigned int total_len;
483 /* precompute 'cpath' so we can avoid doing the lookups lots of times */
487 sizeof (cpath)); /* Just to trigger harder errors later. */
488 total_len = get_path_length + put_path_length;
489 for (unsigned int off=0;off<total_len;off++)
491 const struct GNUNET_PeerIdentity *pid;
493 pid = (off < get_path_length)
494 ? &get_path[get_path_length - off - 1]
495 : &put_path[get_path_length + put_path_length - off - 1];
496 /* Check that I am not in the path */
497 if (0 == memcmp (&my_full_id,
499 sizeof (struct GNUNET_PeerIdentity)))
504 cpath[off - skip] = GCP_get (pid,
506 /* Check that no peer is twice on the path */
507 for (unsigned int i=0;i<off - skip;i++)
509 if (cpath[i] == cpath[off - skip])
516 if (skip >= total_len)
518 LOG (GNUNET_ERROR_TYPE_DEBUG,
519 "Path discovered from DHT is one big cycle?\n");
524 /* First figure out if this path is a subset of an existing path, an
525 extension of an existing path, or a new path. */
526 cm_ctx.cpath_length = total_len;
527 cm_ctx.cpath = cpath;
529 for (int i=total_len-1;i>=0;i--)
531 GCP_iterate_paths_at (cpath[i],
535 if (NULL != cm_ctx.match)
537 if (i == total_len - 1)
539 /* Existing path includes this one, nothing to do! */
540 LOG (GNUNET_ERROR_TYPE_DEBUG,
541 "Path discovered from DHT is already known\n");
544 if (cm_ctx.match->entries_length == i + 1)
546 /* Existing path ends in the middle of new path, extend it! */
547 LOG (GNUNET_ERROR_TYPE_DEBUG,
548 "Trying to extend existing path %s by additional links discovered from DHT\n",
549 GCPP_2s (cm_ctx.match));
550 extend_path (cm_ctx.match,
559 /* No match at all, create completely new path */
560 path = GNUNET_new (struct CadetPeerPath);
561 path->entries_length = total_len;
562 path->entries = GNUNET_new_array (path->entries_length,
563 struct CadetPeerPathEntry *);
564 for (int i=path->entries_length-1;i>=0;i--)
566 struct CadetPeerPathEntry *entry = GNUNET_new (struct CadetPeerPathEntry);
568 path->entries[i] = entry;
569 entry->peer = cpath[i];
572 for (int i=path->entries_length-1;i>=0;i--)
574 struct CadetPeerPathEntry *entry = path->entries[i];
576 GCP_path_entry_add (entry->peer,
581 /* Finally, try to attach it */
582 attach_path (path, 0);
583 if (NULL == path->hn)
585 /* None of the peers on the path care about it. */
586 LOG (GNUNET_ERROR_TYPE_DEBUG,
587 "Path discovered from DHT is not interesting to us\n");
588 GNUNET_assert (0 == path->entries_length);
589 GNUNET_assert (NULL == path->entries);
593 LOG (GNUNET_ERROR_TYPE_DEBUG,
594 "Created new path %s based on information from DHT\n",
600 * We got an incoming connection, obtain the corresponding path.
602 * @param path_length number of segments on the @a path
603 * @param pids path through the network, in reverse order (we are at the end at index @a path_length)
604 * @return corresponding path object
606 struct CadetPeerPath *
607 GCPP_get_path_from_route (unsigned int path_length,
608 const struct GNUNET_PeerIdentity *pids)
610 struct CheckMatchContext cm_ctx;
611 struct CadetPeer *cpath[path_length];
612 struct CadetPeerPath *path;
614 /* precompute inverted 'cpath' so we can avoid doing the lookups and
615 have the correct order */
616 for (unsigned int off=0;off<path_length;off++)
617 cpath[off] = GCP_get (&pids[path_length - 1 - off],
620 /* First figure out if this path is a subset of an existing path, an
621 extension of an existing path, or a new path. */
622 cm_ctx.cpath = cpath;
623 cm_ctx.cpath_length = path_length;
625 for (int i=path_length-1;i>=0;i--)
627 GCP_iterate_paths_at (cpath[i],
631 if (NULL != cm_ctx.match)
633 if (i == path_length - 1)
635 /* Existing path includes this one, return the match! */
636 LOG (GNUNET_ERROR_TYPE_DEBUG,
637 "Returning existing path %s as inverse for incoming connection\n",
638 GCPP_2s (cm_ctx.match));
641 if (cm_ctx.match->entries_length == i + 1)
643 /* Existing path ends in the middle of new path, extend it! */
644 LOG (GNUNET_ERROR_TYPE_DEBUG,
645 "Extending existing path %s to create inverse for incoming connection\n",
646 GCPP_2s (cm_ctx.match));
647 extend_path (cm_ctx.match,
651 /* Check that extension was successful */
652 GNUNET_assert (cm_ctx.match->entries_length == path_length);
655 /* Eh, we found a match but couldn't use it? Something is wrong. */
660 /* No match at all, create completely new path */
661 path = GNUNET_new (struct CadetPeerPath);
662 path->entries_length = path_length;
663 path->entries = GNUNET_new_array (path->entries_length,
664 struct CadetPeerPathEntry *);
665 for (int i=path_length-1;i>=0;i--)
667 struct CadetPeerPathEntry *entry = GNUNET_new (struct CadetPeerPathEntry);
669 path->entries[i] = entry;
670 entry->peer = cpath[i];
673 for (int i=path_length-1;i>=0;i--)
675 struct CadetPeerPathEntry *entry = path->entries[i];
677 GCP_path_entry_add (entry->peer,
681 recalculate_path_desirability (path);
682 LOG (GNUNET_ERROR_TYPE_DEBUG,
683 "Created new path %s to create inverse for incoming connection\n",
685 path->hn = GCP_attach_path (cpath[path_length - 1],
694 * Return the length of the path. Excludes one end of the
695 * path, so the loopback path has length 0.
697 * @param path path to return the length for
698 * @return number of peers on the path
701 GCPP_get_length (struct CadetPeerPath *path)
703 return path->entries_length;
708 * Find peer's offset on path.
710 * @param path path to search
711 * @param cp peer to look for
712 * @return offset of @a cp on @a path, or UINT_MAX if not found
715 GCPP_find_peer (struct CadetPeerPath *path,
716 struct CadetPeer *cp)
718 for (unsigned int off = 0;
719 off < path->entries_length;
721 if (cp == GCPP_get_peer_at_offset (path,
729 * Obtain the peer at offset @a off in @a path.
731 * @param path peer path to inspect
732 * @param off offset to return, must be smaller than path length
733 * @return the peer at offset @a off
736 GCPP_get_peer_at_offset (struct CadetPeerPath *path,
739 GNUNET_assert (off < path->entries_length);
740 return path->entries[off]->peer;
745 * Convert a path to a human-readable string.
747 * @param path path to convert
748 * @return string, to be freed by caller (unlike other *_2s APIs!)
751 GCPP_2s (struct CadetPeerPath *path)
753 static char buf[2048];
755 const unsigned int max_plen = (sizeof(buf) - 16) / 5 - 2; /* 5 characters per entry */
758 for (unsigned int i = 0;
759 i < path->entries_length;
762 if ( (path->entries_length > max_plen) &&
763 (i == max_plen / 2) )
764 off += GNUNET_snprintf (&buf[off],
767 if ( (path->entries_length > max_plen) &&
768 (i > max_plen / 2) &&
769 (i < path->entries_length - max_plen / 2) )
771 off += GNUNET_snprintf (&buf[off],
774 GNUNET_i2s (GCP_get_id (GCPP_get_peer_at_offset (path,
776 (i == path->entries_length -1) ? "" : "-");
778 GNUNET_snprintf (&buf[off],
786 /* end of gnunet-service-cadet-new_paths.c */