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-new_paths.c
22 * @brief Information we track per path.
23 * @author Bartlomiej Polot
24 * @author Christian Grothoff
27 * - path desirability score calculations are not done
28 * (and will be tricky to have during path changes)
31 #include "gnunet-service-cadet-new_connection.h"
32 #include "gnunet-service-cadet-new_peer.h"
33 #include "gnunet-service-cadet-new_paths.h"
36 #define LOG(level, ...) GNUNET_log_from(level,"cadet-pat",__VA_ARGS__)
40 * Information regarding a possible path to reach a peer.
46 * Array of all the peers on the path. If @e hn is non-NULL, the
47 * last one is our owner.
49 struct CadetPeerPathEntry **entries;
52 * Node of this path in the owner's heap. Used to update our position
53 * in the heap whenever our @e desirability changes.
55 struct GNUNET_CONTAINER_HeapNode *hn;
58 * Desirability of the path. How unique is it for the various peers
61 GNUNET_CONTAINER_HeapCostType desirability;
64 * Length of the @e entries array.
66 unsigned int entries_length;
72 * Return how much we like keeping the path. This is an aggregate
73 * score based on various factors, including the age of the path
74 * (older == better), and the value of this path to all of its ajacent
75 * peers. For example, long paths that end at a peer that we have no
76 * shorter way to reach are very desirable, while long paths that end
77 * at a peer for which we have a shorter way as well are much less
78 * desirable. Higher values indicate more valuable paths. The
79 * returned value should be used to decide which paths to remember.
81 * @param path path to return the length for
82 * @return desirability of the path, larger is more desirable
84 GNUNET_CONTAINER_HeapCostType
85 GCPP_get_desirability (const struct CadetPeerPath *path)
87 return path->desirability;
92 * Return connection to @a destination using @a path, or return
93 * NULL if no such connection exists.
95 * @param path path to traverse
96 * @param destination destination node to get to, must be on path
97 * @param off offset of @a destination on @a path
98 * @return NULL if we have no existing connection
99 * otherwise connection from us to @a destination via @a path
101 struct CadetConnection *
102 GCPP_get_connection (struct CadetPeerPath *path,
103 struct CadetPeer *destination,
106 struct CadetPeerPathEntry *entry;
108 GNUNET_assert (off < path->entries_length);
109 entry = path->entries[off];
110 GNUNET_assert (entry->peer == destination);
116 * Notify @a path that it is used for connection @a cc
117 * which ends at the path's offset @a off.
119 * @param path the path to remember the @a cc
120 * @param off the offset where the @a cc ends
121 * @param cc the connection to remember
124 GCPP_add_connection (struct CadetPeerPath *path,
126 struct CadetConnection *cc)
128 struct CadetPeerPathEntry *entry;
130 LOG (GNUNET_ERROR_TYPE_DEBUG,
131 "Adding connection %s to path %s at offset %u\n",
135 GNUNET_assert (off < path->entries_length);
136 entry = path->entries[off];
137 GNUNET_assert (NULL == entry->cc);
144 * Notify @a path that it is no longer used for connection @a cc which
145 * ended at the path's offset @a off.
147 * @param path the path to forget the @a cc
148 * @param off the offset where the @a cc ended
149 * @param cc the connection to forget
152 GCPP_del_connection (struct CadetPeerPath *path,
154 struct CadetConnection *cc)
156 struct CadetPeerPathEntry *entry;
158 LOG (GNUNET_ERROR_TYPE_DEBUG,
159 "Removing connection %s to path %s at offset %u\n",
163 GNUNET_assert (off < path->entries_length);
164 entry = path->entries[off];
165 GNUNET_assert (cc == entry->cc);
171 * This path is no longer needed, free resources.
173 * @param path path resources to free
176 path_destroy (struct CadetPeerPath *path)
178 LOG (GNUNET_ERROR_TYPE_DEBUG,
179 "Destroying path %s\n",
181 for (unsigned int i=0;i<path->entries_length;i++)
182 GNUNET_free (path->entries[i]);
183 GNUNET_free (path->entries);
189 * The owning peer of this path is no longer interested in maintaining
190 * it, so the path should be discarded or shortened (in case a
191 * previous peer on the path finds the path desirable).
193 * @param path the path that is being released
196 GCPP_release (struct CadetPeerPath *path)
198 struct CadetPeerPathEntry *entry;
200 LOG (GNUNET_ERROR_TYPE_DEBUG,
201 "Owner releases path %s\n",
204 entry = path->entries[path->entries_length - 1];
207 /* cut 'off' end of path */
208 GCP_path_entry_remove (entry->peer,
210 path->entries_length - 1);
211 path->entries_length--; /* We don't bother shrinking the 'entries' array,
212 as it's probably not worth it. */
214 if (0 == path->entries_length)
217 /* see if new peer at the end likes this path any better */
218 entry = path->entries[path->entries_length - 1];
219 path->hn = GCP_attach_path (entry->peer,
221 path->entries_length,
223 if (NULL != path->hn)
224 return; /* yep, got attached, we are done. */
227 /* nobody wants us, discard the path */
233 * Updates the score for an entry on the path based
234 * on our experiences with using @a path.
236 * @param path the path to update
237 * @param off offset of the entry to update
238 * @param delta change in the score to apply
241 GCPP_update_score (struct CadetPeerPath *path,
245 struct CadetPeerPathEntry *entry;
247 GNUNET_assert (off < path->entries_length);
248 entry = path->entries[off];
250 /* Add delta, with checks for overflows */
253 if (delta + entry->score < entry->score)
254 entry->score = INT_MAX;
256 entry->score += delta;
260 if (delta + entry->score > entry->score)
261 entry->score = INT_MIN;
263 entry->score += delta;
266 /* FIXME: update path desirability! */
271 * Closure for #find_peer_at() and #check_match().
273 struct CheckMatchContext
277 * Set to a matching path, if any.
279 struct CadetPeerPath *match;
282 * Array the combined paths.
284 struct CadetPeer **cpath;
287 * How long is the @e cpath array?
289 unsigned int cpath_length;
295 * Check if the given path is identical on all of the
296 * hops until @a off, and not longer than @a off. If the
297 * @a path matches, store it in `match`.
299 * @param cls the `struct CheckMatchContext` to check against
300 * @param path the path to check
301 * @param off offset to check at
302 * @return #GNUNET_YES (continue to iterate), or if found #GNUNET_NO
305 check_match (void *cls,
306 struct CadetPeerPath *path,
309 struct CheckMatchContext *cm_ctx = cls;
311 GNUNET_assert (path->entries_length > off);
312 if ( (path->entries_length != off + 1) &&
313 (off + 1 != cm_ctx->cpath_length) )
315 LOG (GNUNET_ERROR_TYPE_DEBUG,
316 "check_match missmatch because path %s is too long (%u vs. %u vs. %u)\n",
318 path->entries_length,
320 cm_ctx->cpath_length);
321 return GNUNET_YES; /* too long, goes somewhere else already, thus cannot be useful */
323 for (unsigned int i=0;i<off;i++)
324 if (cm_ctx->cpath[i] !=
325 GCPP_get_peer_at_offset (path,
328 LOG (GNUNET_ERROR_TYPE_DEBUG,
329 "check_match path %s missmatches at offset %u\n",
332 return GNUNET_YES; /* missmatch, ignore */
334 LOG (GNUNET_ERROR_TYPE_DEBUG,
335 "check_match found match with path %s\n",
337 cm_ctx->match = path;
338 return GNUNET_NO; /* match, we are done! */
343 * Extend path @a path by the @a num_peers from the @a peers
344 * array, assuming the owners past the current owner want it.
346 * @param path path to extend
347 * @param peers list of peers beyond the end of @a path
348 * @param num_peers length of the @a peers array
349 * @param force force attachment, even if we have other
353 extend_path (struct CadetPeerPath *path,
354 struct CadetPeer **peers,
355 unsigned int num_peers,
358 unsigned int old_len = path->entries_length;
359 struct GNUNET_CONTAINER_HeapNode *hn;
362 /* If we extend an existing path, detach it from the
363 old owner and re-attach to the new one */
365 for (i=num_peers-1;i>=0;i--)
367 /* FIXME: note that path->desirability is used, but not yet updated here! */
368 hn = GCP_attach_path (peers[i],
370 old_len + (unsigned int) i,
376 return; /* none of the peers is interested in this path */
377 GCP_detach_path (path->entries[old_len-1]->peer,
381 GNUNET_array_grow (path->entries,
382 path->entries_length,
386 struct CadetPeerPathEntry *entry = GNUNET_new (struct CadetPeerPathEntry);
388 path->entries[old_len + i] = entry;
389 entry->peer = peers[i];
391 GCP_path_entry_add (entry->peer,
395 LOG (GNUNET_ERROR_TYPE_DEBUG,
396 "Extended path %s\n",
402 * Create a peer path based on the result of a DHT lookup. If we
403 * already know this path, or one that is longer, simply return NULL.
404 * Otherwise, we try to extend an existing path, or create a new one
407 * @param get_path path of the get request
408 * @param get_path_length lenght of @a get_path
409 * @param put_path path of the put request
410 * @param put_path_length length of the @a put_path
411 * @return a path through the network
414 GCPP_try_path_from_dht (const struct GNUNET_PeerIdentity *get_path,
415 unsigned int get_path_length,
416 const struct GNUNET_PeerIdentity *put_path,
417 unsigned int put_path_length)
419 struct CadetPeer *cpath[get_path_length + put_path_length];
420 struct CheckMatchContext cm_ctx;
421 struct CadetPeerPath *path;
422 struct GNUNET_CONTAINER_HeapNode *hn;
425 /* precompute 'cpath' so we can avoid doing the lookups lots of times */
426 for (unsigned int off=0;off<get_path_length + put_path_length;off++)
428 const struct GNUNET_PeerIdentity *pid;
430 pid = (off < get_path_length)
431 ? &get_path[get_path_length - off]
432 : &put_path[get_path_length + put_path_length - off];
433 cpath[off] = GCP_get (pid,
437 /* First figure out if this path is a subset of an existing path, an
438 extension of an existing path, or a new path. */
439 cm_ctx.cpath_length = get_path_length + put_path_length;
440 cm_ctx.cpath = cpath;
442 for (i=get_path_length + put_path_length-1;i>=0;i--)
444 GCP_iterate_paths_at (cpath[i],
448 if (NULL != cm_ctx.match)
450 if (i == get_path_length + put_path_length - 1)
452 /* Existing path includes this one, nothing to do! */
453 LOG (GNUNET_ERROR_TYPE_DEBUG,
454 "Path discovered from DHT is already known\n");
457 if (cm_ctx.match->entries_length == i + 1)
459 /* Existing path ends in the middle of new path, extend it! */
460 LOG (GNUNET_ERROR_TYPE_DEBUG,
461 "Trying to extend existing path %s by additional links discovered from DHT\n",
462 GCPP_2s (cm_ctx.match));
463 extend_path (cm_ctx.match,
465 get_path_length + put_path_length - i,
472 /* No match at all, create completely new path */
473 path = GNUNET_new (struct CadetPeerPath);
475 /* First, try to attach it */
477 for (i=get_path_length + put_path_length-1;i>=0;i--)
479 path->entries_length = i + 1;
480 /* FIXME: note that path->desirability is used, but not yet initialized here! */
481 hn = GCP_attach_path (cpath[i],
490 /* None of the peers on the path care about it. */
491 LOG (GNUNET_ERROR_TYPE_DEBUG,
492 "Path discovered from DHT is not interesting to us\n");
497 path->entries_length = i + 1;
498 path->entries = GNUNET_new_array (path->entries_length,
499 struct CadetPeerPathEntry *);
502 struct CadetPeerPathEntry *entry = GNUNET_new (struct CadetPeerPathEntry);
504 path->entries[i] = entry;
505 entry->peer = cpath[i];
507 GCP_path_entry_add (entry->peer,
511 LOG (GNUNET_ERROR_TYPE_DEBUG,
512 "Created new path %s based on information from DHT\n",
518 * We got an incoming connection, obtain the corresponding path.
520 * @param path_length number of segments on the @a path
521 * @param pids path through the network, in reverse order (we are at the end at index @a path_length)
522 * @return corresponding path object
524 struct CadetPeerPath *
525 GCPP_get_path_from_route (unsigned int path_length,
526 const struct GNUNET_PeerIdentity *pids)
528 struct CheckMatchContext cm_ctx;
529 struct CadetPeer *cpath[path_length];
530 struct CadetPeerPath *path;
532 /* precompute inverted 'cpath' so we can avoid doing the lookups and
533 have the correct order */
534 for (unsigned int off=0;off<path_length;off++)
535 cpath[off] = GCP_get (&pids[path_length - 1 - off],
538 /* First figure out if this path is a subset of an existing path, an
539 extension of an existing path, or a new path. */
540 cm_ctx.cpath = cpath;
541 cm_ctx.cpath_length = path_length;
543 for (int i=path_length-1;i>=0;i--)
545 GCP_iterate_paths_at (cpath[i],
549 if (NULL != cm_ctx.match)
551 if (i == path_length - 1)
553 /* Existing path includes this one, return the match! */
554 LOG (GNUNET_ERROR_TYPE_DEBUG,
555 "Returning existing path %s as inverse for incoming connection\n",
556 GCPP_2s (cm_ctx.match));
559 if (cm_ctx.match->entries_length == i + 1)
561 /* Existing path ends in the middle of new path, extend it! */
562 LOG (GNUNET_ERROR_TYPE_DEBUG,
563 "Extending existing path %s to create inverse for incoming connection\n",
564 GCPP_2s (cm_ctx.match));
565 extend_path (cm_ctx.match,
569 /* Check that extension was successful */
570 GNUNET_assert (cm_ctx.match->entries_length == path_length);
573 /* Eh, we found a match but couldn't use it? Something is wrong. */
578 /* No match at all, create completely new path */
579 path = GNUNET_new (struct CadetPeerPath);
580 path->entries_length = path_length;
581 path->entries = GNUNET_new_array (path->entries_length,
582 struct CadetPeerPathEntry *);
583 for (int i=path_length-1;i>=0;i--)
585 struct CadetPeerPathEntry *entry = GNUNET_new (struct CadetPeerPathEntry);
587 path->entries[i] = entry;
588 entry->peer = cpath[i];
590 GCP_path_entry_add (entry->peer,
594 LOG (GNUNET_ERROR_TYPE_DEBUG,
595 "Created new path %s to create inverse for incoming connection\n",
597 path->hn = GCP_attach_path (cpath[path_length - 1],
606 * Return the length of the path. Excludes one end of the
607 * path, so the loopback path has length 0.
609 * @param path path to return the length for
610 * @return number of peers on the path
613 GCPP_get_length (struct CadetPeerPath *path)
615 return path->entries_length;
620 * Find peer's offset on path.
622 * @param path path to search
623 * @param cp peer to look for
624 * @return offset of @a cp on @a path, or UINT_MAX if not found
627 GCPP_find_peer (struct CadetPeerPath *path,
628 struct CadetPeer *cp)
630 for (unsigned int off = 0;
631 off < path->entries_length;
633 if (cp == GCPP_get_peer_at_offset (path,
641 * Obtain the peer at offset @a off in @a path.
643 * @param path peer path to inspect
644 * @param off offset to return, must be smaller than path length
645 * @return the peer at offset @a off
648 GCPP_get_peer_at_offset (struct CadetPeerPath *path,
651 GNUNET_assert (off < path->entries_length);
652 return path->entries[off]->peer;
657 * Convert a path to a human-readable string.
659 * @param path path to convert
660 * @return string, to be freed by caller (unlike other *_2s APIs!)
663 GCPP_2s (struct CadetPeerPath *path)
665 static char buf[2048];
667 const unsigned int max_plen = (sizeof(buf) - 16) / 5 - 2; /* 5 characters per entry */
670 for (unsigned int i = 0;
671 i < path->entries_length;
674 if ( (path->entries_length > max_plen) &&
675 (i == max_plen / 2) )
676 off += GNUNET_snprintf (&buf[off],
679 if ( (path->entries_length > max_plen) &&
680 (i > max_plen / 2) &&
681 (i < path->entries_length - max_plen / 2) )
683 off += GNUNET_snprintf (&buf[off],
686 GNUNET_i2s (GCP_get_id (GCPP_get_peer_at_offset (path,
688 (i == path->entries_length -1) ? "" : "-");
690 GNUNET_snprintf (&buf[off],
698 /* end of gnunet-service-cadet-new_paths.c */