aboutsummaryrefslogtreecommitdiff
path: root/src/cadet/cadet_path.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/cadet/cadet_path.c')
-rw-r--r--src/cadet/cadet_path.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/cadet/cadet_path.c b/src/cadet/cadet_path.c
index f378e124e..7e950a5ce 100644
--- a/src/cadet/cadet_path.c
+++ b/src/cadet/cadet_path.c
@@ -28,6 +28,9 @@
28#include "cadet_path.h" 28#include "cadet_path.h"
29#include "gnunet-service-cadet_peer.h" 29#include "gnunet-service-cadet_peer.h"
30 30
31#define LOG(level, ...) GNUNET_log_from (level,"cadet-pth",__VA_ARGS__)
32
33
31/** 34/**
32 * @brief Destroy a path after some time has past. 35 * @brief Destroy a path after some time has past.
33 * 36 *
@@ -147,6 +150,69 @@ path_invalidate (struct CadetPeerPath *p)
147 150
148 151
149/** 152/**
153 * Builds a path from a PeerIdentity array.
154 *
155 * @param peers PeerIdentity array.
156 * @param size Size of the @c peers array.
157 * @param myid ID of local peer, to find @c own_pos.
158 * @param own_pos Output parameter: own position in the path.
159 *
160 * @return Fixed and shortened path.
161 */
162struct CadetPeerPath *
163path_build_from_peer_ids (struct GNUNET_PeerIdentity *peers,
164 unsigned int size,
165 GNUNET_PEER_Id myid,
166 unsigned int *own_pos)
167{
168 struct CadetPeerPath *path;
169 GNUNET_PEER_Id shortid;
170 unsigned int i;
171 unsigned int j;
172 unsigned int offset;
173
174 /* Create path */
175 LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
176 path = path_new (size);
177 *own_pos = 0;
178 offset = 0;
179 for (i = 0; i < size; i++)
180 {
181 LOG (GNUNET_ERROR_TYPE_DEBUG, " - %u: taking %s\n",
182 i, GNUNET_i2s (&peers[i]));
183 shortid = GNUNET_PEER_intern (&peers[i]);
184
185 /* Check for loops / duplicates */
186 for (j = 0; j < i - offset; j++)
187 {
188 if (path->peers[j] == shortid)
189 {
190 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists at pos %u\n", j);
191 offset = i - j;
192 LOG (GNUNET_ERROR_TYPE_DEBUG, " offset now %u\n", offset);
193 GNUNET_PEER_change_rc (shortid, -1);
194 }
195 }
196 LOG (GNUNET_ERROR_TYPE_DEBUG, " storing at %u\n", i - offset);
197 path->peers[i - offset] = shortid;
198 if (path->peers[i - offset] == myid)
199 *own_pos = i - offset;
200 }
201 path->length -= offset;
202
203 if (path->peers[*own_pos] != myid)
204 {
205 /* create path: self not found in path through self */
206 GNUNET_break_op (0);
207 path_destroy (path);
208 return NULL;
209 }
210
211 return path;
212}
213
214
215/**
150 * Test if a path is valid (or at least not known to be invalid). 216 * Test if a path is valid (or at least not known to be invalid).
151 * 217 *
152 * @param path Path to test. 218 * @param path Path to test.