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 * Connections using this path, by destination peer
59 * (each hop of the path could correspond to an
62 struct GNUNET_CONTAINER_MultiPeerMap *connections;
65 * Desirability of the path. How unique is it for the various peers
68 GNUNET_CONTAINER_HeapCostType desirability;
71 * Length of the @e entries array.
73 unsigned int entries_length;
79 * Return how much we like keeping the path. This is an aggregate
80 * score based on various factors, including the age of the path
81 * (older == better), and the value of this path to all of its ajacent
82 * peers. For example, long paths that end at a peer that we have no
83 * shorter way to reach are very desirable, while long paths that end
84 * at a peer for which we have a shorter way as well are much less
85 * desirable. Higher values indicate more valuable paths. The
86 * returned value should be used to decide which paths to remember.
88 * @param path path to return the length for
89 * @return desirability of the path, larger is more desirable
91 GNUNET_CONTAINER_HeapCostType
92 GCPP_get_desirability (const struct CadetPeerPath *path)
94 return path->desirability;
99 * Return connection to @a destination using @a path, or return
100 * NULL if no such connection exists.
102 * @param path path to traverse
103 * @param destination destination node to get to, must be on path
104 * @param off offset of @a destination on @a path
105 * @return NULL if we have no existing connection
106 * otherwise connection from us to @a destination via @a path
108 struct CadetConnection *
109 GCPP_get_connection (struct CadetPeerPath *path,
110 struct CadetPeer *destination,
113 struct CadetPeerPathEntry *entry;
115 GNUNET_assert (off < path->entries_length);
116 entry = &path->entries[off];
117 GNUNET_assert (entry->peer == destination);
123 * Notify @a path that it is used for connection @a cc
124 * which ends at the path's offset @a off.
126 * @param path the path to remember the @a cc
127 * @param off the offset where the @a cc ends
128 * @param cc the connection to remember
131 GCPP_add_connection (struct CadetPeerPath *path,
133 struct CadetConnection *cc)
135 struct CadetPeerPathEntry *entry;
137 LOG (GNUNET_ERROR_TYPE_DEBUG,
138 "Adding connection %s to path %s at offset %u\n",
142 GNUNET_assert (off < path->entries_length);
143 entry = &path->entries[off];
144 GNUNET_assert (NULL == entry->cc);
151 * Notify @a path that it is no longer used for connection @a cc which
152 * ended at the path's offset @a off.
154 * @param path the path to forget the @a cc
155 * @param off the offset where the @a cc ended
156 * @param cc the connection to forget
159 GCPP_del_connection (struct CadetPeerPath *path,
161 struct CadetConnection *cc)
163 struct CadetPeerPathEntry *entry;
165 LOG (GNUNET_ERROR_TYPE_DEBUG,
166 "Removing connection %s to path %s at offset %u\n",
170 GNUNET_assert (off < path->entries_length);
171 entry = &path->entries[off];
172 GNUNET_assert (cc == entry->cc);
178 * This path is no longer needed, free resources.
180 * @param path path resources to free
183 path_destroy (struct CadetPeerPath *path)
185 LOG (GNUNET_ERROR_TYPE_DEBUG,
186 "Destroying path %s\n",
189 GNUNET_CONTAINER_multipeermap_size (path->connections));
190 GNUNET_CONTAINER_multipeermap_destroy (path->connections);
191 GNUNET_free (path->entries);
197 * The owning peer of this path is no longer interested in maintaining
198 * it, so the path should be discarded or shortened (in case a
199 * previous peer on the path finds the path desirable).
201 * @param path the path that is being released
204 GCPP_release (struct CadetPeerPath *path)
206 struct CadetPeerPathEntry *entry;
208 LOG (GNUNET_ERROR_TYPE_DEBUG,
209 "Owner releases path %s\n",
212 entry = &path->entries[path->entries_length - 1];
215 /* cut 'off' end of path, verifying it is not in use */
216 GNUNET_assert (NULL ==
217 GNUNET_CONTAINER_multipeermap_get (path->connections,
218 GCP_get_id (entry->peer)));
219 GCP_path_entry_remove (entry->peer,
221 path->entries_length - 1);
222 path->entries_length--; /* We don't bother shrinking the 'entries' array,
223 as it's probably not worth it. */
224 if (0 == path->entries_length)
227 /* see if new peer at the end likes this path any better */
228 entry = &path->entries[path->entries_length - 1];
229 path->hn = GCP_attach_path (entry->peer,
231 path->entries_length,
233 if (NULL != path->hn)
234 return; /* yep, got attached, we are done. */
237 /* nobody wants us, discard the path */
243 * Updates the score for an entry on the path based
244 * on our experiences with using @a path.
246 * @param path the path to update
247 * @param off offset of the entry to update
248 * @param delta change in the score to apply
251 GCPP_update_score (struct CadetPeerPath *path,
255 struct CadetPeerPathEntry *entry;
257 GNUNET_assert (off < path->entries_length);
258 entry = &path->entries[off];
260 /* Add delta, with checks for overflows */
263 if (delta + entry->score < entry->score)
264 entry->score = INT_MAX;
266 entry->score += delta;
270 if (delta + entry->score > entry->score)
271 entry->score = INT_MIN;
273 entry->score += delta;
276 /* FIXME: update path desirability! */
281 * Closure for #find_peer_at() and #check_match().
283 struct CheckMatchContext
287 * Set to a matching path, if any.
289 struct CadetPeerPath *match;
292 * Array the combined paths.
294 struct CadetPeer **cpath;
300 * Check if the given path is identical on all of the
301 * hops until @a off, and not longer than @a off. If the
302 * @a path matches, store it in `match`.
304 * @param cls the `struct CheckMatchContext` to check against
305 * @param path the path to check
306 * @param off offset to check at
307 * @return #GNUNET_YES (continue to iterate), or if found #GNUNET_NO
310 check_match (void *cls,
311 struct CadetPeerPath *path,
314 struct CheckMatchContext *cm_ctx = cls;
316 if (path->entries_length > off)
317 return GNUNET_YES; /* too long, cannot be useful */
318 for (unsigned int i=0;i<off;i++)
319 if (cm_ctx->cpath[i] !=
320 GCPP_get_peer_at_offset (path,
322 return GNUNET_YES; /* missmatch, ignore */
323 cm_ctx->match = path;
324 return GNUNET_NO; /* match, we are done! */
329 * Extend path @a path by the @a num_peers from the @a peers
330 * array, assuming the owners past the current owner want it.
332 * @param path path to extend
333 * @param peers list of peers beyond the end of @a path
334 * @param num_peers length of the @a peers array
335 * @param force force attachment, even if we have other
339 extend_path (struct CadetPeerPath *path,
340 struct CadetPeer **peers,
341 unsigned int num_peers,
344 unsigned int old_len = path->entries_length;
345 struct GNUNET_CONTAINER_HeapNode *hn;
348 /* If we extend an existing path, detach it from the
349 old owner and re-attach to the new one */
351 for (i=num_peers-1;i>=0;i--)
353 /* FIXME: note that path->desirability is used, but not yet updated here! */
354 hn = GCP_attach_path (peers[i],
356 old_len + (unsigned int) i,
362 return; /* none of the peers is interested in this path */
363 GCP_detach_path (path->entries[old_len-1].peer,
367 GNUNET_array_grow (path->entries,
368 path->entries_length,
372 struct CadetPeerPathEntry *entry = &path->entries[old_len + i];
374 entry->peer = peers[i];
376 GCP_path_entry_add (entry->peer,
380 LOG (GNUNET_ERROR_TYPE_DEBUG,
381 "Extended path %s\n",
387 * Create a peer path based on the result of a DHT lookup. If we
388 * already know this path, or one that is longer, simply return NULL.
389 * Otherwise, we try to extend an existing path, or create a new one
392 * @param get_path path of the get request
393 * @param get_path_length lenght of @a get_path
394 * @param put_path path of the put request
395 * @param put_path_length length of the @a put_path
396 * @return a path through the network
399 GCPP_try_path_from_dht (const struct GNUNET_PeerIdentity *get_path,
400 unsigned int get_path_length,
401 const struct GNUNET_PeerIdentity *put_path,
402 unsigned int put_path_length)
404 struct CadetPeer *cpath[get_path_length + put_path_length];
405 struct CheckMatchContext cm_ctx;
406 struct CadetPeerPath *path;
407 struct GNUNET_CONTAINER_HeapNode *hn;
410 /* precompute 'cpath' so we can avoid doing the lookups lots of times */
411 for (unsigned int off=0;off<get_path_length + put_path_length;off++)
413 const struct GNUNET_PeerIdentity *pid;
415 pid = (off < get_path_length)
416 ? &get_path[get_path_length - off]
417 : &put_path[get_path_length + put_path_length - off];
418 cpath[off] = GCP_get (pid,
422 /* First figure out if this path is a subset of an existing path, an
423 extension of an existing path, or a new path. */
424 cm_ctx.cpath = cpath;
426 for (i=get_path_length + put_path_length-1;i>=0;i--)
428 GCP_iterate_paths_at (cpath[i],
432 if (NULL != cm_ctx.match)
434 if (i == get_path_length + put_path_length - 1)
436 /* Existing path includes this one, nothing to do! */
437 LOG (GNUNET_ERROR_TYPE_DEBUG,
438 "Path discovered from DHT is already known\n");
441 if (cm_ctx.match->entries_length == i + 1)
443 /* Existing path ends in the middle of new path, extend it! */
444 LOG (GNUNET_ERROR_TYPE_DEBUG,
445 "Trying to extend existing path %s by additional links discovered from DHT\n",
446 GCPP_2s (cm_ctx.match));
447 extend_path (cm_ctx.match,
449 get_path_length + put_path_length - i,
456 /* No match at all, create completely new path */
457 path = GNUNET_new (struct CadetPeerPath);
459 /* First, try to attach it */
461 for (i=get_path_length + put_path_length-1;i>=0;i--)
463 path->entries_length = i;
464 /* FIXME: note that path->desirability is used, but not yet initialized here! */
465 hn = GCP_attach_path (cpath[i],
474 /* None of the peers on the path care about it. */
475 LOG (GNUNET_ERROR_TYPE_DEBUG,
476 "Path discovered from DHT is not interesting to us\n");
481 path->entries_length = i;
482 path->entries = GNUNET_new_array (path->entries_length,
483 struct CadetPeerPathEntry);
486 struct CadetPeerPathEntry *entry = &path->entries[i];
488 entry->peer = cpath[i];
490 GCP_path_entry_add (entry->peer,
494 LOG (GNUNET_ERROR_TYPE_DEBUG,
495 "Created new path %s based on information from DHT\n",
501 * We got an incoming connection, obtain the corresponding path.
503 * @param path_length number of segments on the @a path
504 * @param pids path through the network, in reverse order (we are at the end at index @a path_length)
505 * @return corresponding path object
507 struct CadetPeerPath *
508 GCPP_get_path_from_route (unsigned int path_length,
509 const struct GNUNET_PeerIdentity *pids)
511 struct CheckMatchContext cm_ctx;
512 struct CadetPeer *cpath[path_length];
513 struct CadetPeerPath *path;
515 /* precompute inverted 'cpath' so we can avoid doing the lookups and
516 have the correct order */
517 for (unsigned int off=0;off<path_length;off++)
518 cpath[off] = GCP_get (&pids[path_length - 1 - off],
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 = cpath;
525 for (int i=path_length-1;i>=0;i--)
527 GCP_iterate_paths_at (cpath[i],
531 if (NULL != cm_ctx.match)
533 if (i == path_length - 1)
535 /* Existing path includes this one, return the match! */
536 LOG (GNUNET_ERROR_TYPE_DEBUG,
537 "Returning existing path %s as inverse for incoming connection\n",
538 GCPP_2s (cm_ctx.match));
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 "Extending existing path %s to create inverse for incoming connection\n",
546 GCPP_2s (cm_ctx.match));
547 extend_path (cm_ctx.match,
551 /* Check that extension was successful */
552 GNUNET_assert (cm_ctx.match->entries_length == path_length);
558 /* No match at all, create completely new path */
559 path = GNUNET_new (struct CadetPeerPath);
560 path->entries_length = path_length;
561 path->entries = GNUNET_new_array (path->entries_length,
562 struct CadetPeerPathEntry);
563 for (int i=path_length-1;i>=0;i--)
565 struct CadetPeerPathEntry *entry = &path->entries[i];
567 entry->peer = cpath[i];
569 GCP_path_entry_add (entry->peer,
573 path->hn = GCP_attach_path (cpath[path_length - 1],
577 LOG (GNUNET_ERROR_TYPE_DEBUG,
578 "Created new path %s to create inverse for incoming connection\n",
585 * Return the length of the path. Excludes one end of the
586 * path, so the loopback path has length 0.
588 * @param path path to return the length for
589 * @return number of peers on the path
592 GCPP_get_length (struct CadetPeerPath *path)
594 return path->entries_length;
599 * Find peer's offset on path.
601 * @param path path to search
602 * @param cp peer to look for
603 * @return offset of @a cp on @a path, or UINT_MAX if not found
606 GCPP_find_peer (struct CadetPeerPath *path,
607 struct CadetPeer *cp)
609 for (unsigned int off = 0;
610 off < path->entries_length;
612 if (cp == GCPP_get_peer_at_offset (path,
620 * Obtain the peer at offset @a off in @a path.
622 * @param path peer path to inspect
623 * @param off offset to return, must be smaller than path length
624 * @return the peer at offset @a off
627 GCPP_get_peer_at_offset (struct CadetPeerPath *path,
630 GNUNET_assert (off < path->entries_length);
631 return path->entries[off].peer;
636 * Convert a path to a human-readable string.
638 * @param path path to convert
639 * @return string, to be freed by caller (unlike other *_2s APIs!)
642 GCPP_2s (struct CadetPeerPath *path)
644 static char buf[2048];
646 const unsigned int max_plen = sizeof(buf) / 5 - 2; /* 5 characters per entry */
649 for (unsigned int i = 0;
650 i < path->entries_length;
653 if ( (path->entries_length > max_plen) &&
654 (i == max_plen / 2) )
655 off += GNUNET_snprintf (&buf[off],
658 if ( (path->entries_length > max_plen) &&
659 (i > max_plen / 2) &&
660 (i < path->entries_length - max_plen / 2) )
662 off += GNUNET_snprintf (&buf[off],
665 GNUNET_i2s (GCP_get_id (GCPP_get_peer_at_offset (path,
672 /* end of gnunet-service-cadet-new_paths.c */