2 This file is part of GNUnet.
3 Copyright (C) 2001 - 2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file cadet/cadet_path.c
23 * @brief Path handling functions
24 * @author Bartlomiej Polot
28 #include "cadet_path.h"
29 #include "gnunet-service-cadet_peer.h"
31 #define LOG(level, ...) GNUNET_log_from (level,"cadet-pth",__VA_ARGS__)
35 * @brief Destroy a path after some time has past.
37 * If the path is returned from DHT again after a while, try again.
39 * Removes the path from the peer (except for direct paths).
41 * @param cls Closure (path to destroy).
42 * @param tc Task context.
45 path_destroy_delayed (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
47 struct CadetPeerPath *path = cls;
48 struct CadetPeer *peer;
50 LOG (GNUNET_ERROR_TYPE_INFO, "Destroy delayed %p (%u)\n", path, path->length);
51 path->path_delete = NULL;
53 if (2 < path->length && (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) == 0)
55 /* During shutdown, the peers peermap might not exist anymore. */
56 peer = GCP_get_short (path->peers[path->length - 1]);
57 GCP_remove_path (peer, path);
67 * @param length How many hops will the path have.
69 * @return A newly allocated path with a peer array of the specified length.
71 struct CadetPeerPath *
72 path_new (unsigned int length)
74 struct CadetPeerPath *p;
76 p = GNUNET_new (struct CadetPeerPath);
80 p->peers = GNUNET_malloc (length * sizeof (GNUNET_PEER_Id));
82 LOG (GNUNET_ERROR_TYPE_INFO, "New path %p (%u)\n", p, p->length);
90 * @param path the path to invert
93 path_invert (struct CadetPeerPath *path)
98 for (i = 0; i < path->length / 2; i++)
100 aux = path->peers[i];
101 path->peers[i] = path->peers[path->length - i - 1];
102 path->peers[path->length - i - 1] = aux;
108 * Duplicate a path, incrementing short peer's rc.
110 * @param path The path to duplicate.
112 struct CadetPeerPath *
113 path_duplicate (const struct CadetPeerPath *path)
115 struct CadetPeerPath *aux;
118 aux = path_new (path->length);
119 memcpy (aux->peers, path->peers, path->length * sizeof (GNUNET_PEER_Id));
120 for (i = 0; i < aux->length; i++)
121 GNUNET_PEER_change_rc (aux->peers[i], 1);
127 * Get the length of a path.
129 * @param path The path to measure, with the local peer at any point of it.
131 * @return Number of hops to reach destination.
132 * UINT_MAX in case the peer is not in the path.
135 path_get_length (struct CadetPeerPath *path)
145 * Mark path as invalid: keep it aroud for a while to avoid trying it in a loop.
147 * Never invalidates a two-hop (direct) path, only a core handler can do that.
149 * Rationale: DHT_get sometimes returns bad cached results, for instance,
150 * on a locally cached result where the PUT followed a path that is no longer
151 * current. The path must remain "known and marked as invalid" for a while.
153 * @param p Path to invalidate.
156 path_invalidate (struct CadetPeerPath *p)
158 if (NULL != p->path_delete)
161 LOG (GNUNET_ERROR_TYPE_INFO, "Invalidating path %p (%u)\n", p, p->length);
162 p->path_delete = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
163 &path_destroy_delayed, p);
168 * Builds a path from a PeerIdentity array.
170 * @param peers PeerIdentity array.
171 * @param size Size of the @c peers array.
172 * @param myid ID of local peer, to find @c own_pos.
173 * @param own_pos Output parameter: own position in the path.
175 * @return Fixed and shortened path.
177 struct CadetPeerPath *
178 path_build_from_peer_ids (struct GNUNET_PeerIdentity *peers,
181 unsigned int *own_pos)
183 struct CadetPeerPath *path;
184 GNUNET_PEER_Id shortid;
190 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
191 path = path_new (size);
194 for (i = 0; i < size; i++)
196 LOG (GNUNET_ERROR_TYPE_DEBUG, " - %u: taking %s\n",
197 i, GNUNET_i2s (&peers[i]));
198 shortid = GNUNET_PEER_intern (&peers[i]);
200 /* Check for loops / duplicates */
201 for (j = 0; j < i - offset; j++)
203 if (path->peers[j] == shortid)
205 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists at pos %u\n", j);
207 LOG (GNUNET_ERROR_TYPE_DEBUG, " offset now %u\n", offset);
208 GNUNET_PEER_change_rc (shortid, -1);
211 LOG (GNUNET_ERROR_TYPE_DEBUG, " storing at %u\n", i - offset);
212 path->peers[i - offset] = shortid;
213 if (path->peers[i - offset] == myid)
214 *own_pos = i - offset;
216 path->length -= offset;
218 if (path->peers[*own_pos] != myid)
220 /* create path: self not found in path through self */
231 * Test if two paths are equivalent (equal or revese of each other).
233 * @param p1 First path
234 * @param p2 Second path
236 * @return GNUNET_YES if both paths are equivalent
237 * GNUNET_NO otherwise
240 path_equivalent (const struct CadetPeerPath *p1,
241 const struct CadetPeerPath *p2)
247 if (p1->length != p2->length)
251 if (0 == memcmp (p1->peers, p2->peers, sizeof (p1->peers[0]) * l))
256 for (i = 0; i <= half; i++)
257 if (p1->peers[i] != p2->peers[l - i])
265 * Test if a path is valid (or at least not known to be invalid).
267 * @param path Path to test.
269 * @return #GNUNET_YES If the path is valid or unknown,
270 * #GNUNET_NO If the path is known to be invalid.
273 path_is_valid (const struct CadetPeerPath *path)
275 return (NULL == path->path_delete);
280 * Destroy the path and free any allocated resources linked to it
282 * @param p the path to destroy
284 * @return GNUNET_OK on success
287 path_destroy (struct CadetPeerPath *p)
292 LOG (GNUNET_ERROR_TYPE_INFO, "destroying path %p (%u)\n", p, p->length);
293 GNUNET_PEER_decrement_rcs (p->peers, p->length);
294 GNUNET_free_non_null (p->peers);
295 if (NULL != p->path_delete)
296 GNUNET_SCHEDULER_cancel (p->path_delete);
305 * @param p1 First path.
306 * @param p2 Second path.
308 * @return > 0 if p1 is longer, or the first differing PEER_Id is higher on p1.
309 * < 0 if p2 is longer, or the first differing PEER_Id is higher on p2.
310 * 0 if they are identical.
313 path_cmp (const struct CadetPeerPath *p1, const struct CadetPeerPath *p2)
315 if (p1->length > p2->length)
318 if (p1->length < p2->length)
321 return memcmp (p1->peers, p2->peers, sizeof (GNUNET_PEER_Id) * p1->length);
326 path_2s (struct CadetPeerPath *p)
332 old = GNUNET_strdup ("");
333 for (i = 0; i < p->length; i++)
335 GNUNET_asprintf (&s, "%s %s",
336 old, GNUNET_i2s (GNUNET_PEER_resolve2 (p->peers[i])));
337 GNUNET_free_non_null (old);
345 path_debug (struct CadetPeerPath *p)
349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH:\n");
350 for (i = 0; i < p->length; i++)
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n",
352 GNUNET_i2s (GNUNET_PEER_resolve2 (p->peers[i])));
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "END\n");