2 This file is part of GNUnet.
3 (C) 2001 - 2011 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 mesh/mesh_tunnel_tree.c
23 * @brief Tunnel tree handling functions
24 * @author Bartlomiej Polot
28 #include "mesh_tunnel_tree.h"
32 debug_node(struct MeshTunnelTreeNode *n, uint16_t level)
36 for (i = 0; i < level; i++)
38 fprintf(stderr, "%u\n", n->peer);
39 for (i = 0; i < n->nchildren; i++)
40 debug_node(&n->children[i], level + 1);
46 tree_debug(struct MeshTunnelTree *t)
48 debug_node(t->root, 0);
56 * @param p the path to invert
59 path_invert (struct MeshPeerPath *path)
64 for (i = 0; i < path->length / 2; i++)
67 path->peers[i] = path->peers[path->length - i - 1];
68 path->peers[path->length - i - 1] = aux;
74 * Destroy the path and free any allocated resources linked to it
76 * @param p the path to destroy
78 * @return GNUNET_OK on success
81 path_destroy (struct MeshPeerPath *p)
83 GNUNET_PEER_decrement_rcs (p->peers, p->length);
84 GNUNET_free (p->peers);
91 * Find the first peer whom to send a packet to go down this path
93 * @param t The tunnel tree to use
94 * @param peer The peerinfo of the peer we are trying to reach
96 * @return peerinfo of the peer who is the first hop in the tunnel
99 struct GNUNET_PeerIdentity *
100 path_get_first_hop (struct MeshTunnelTree *t, GNUNET_PEER_Id peer)
102 struct GNUNET_PeerIdentity id;
104 GNUNET_PEER_resolve (peer, &id);
105 return GNUNET_CONTAINER_multihashmap_get (t->first_hops,
111 * Get the length of a path
113 * @param path The path to measure, with the local peer at any point of it
115 * @return Number of hops to reach destination
116 * UINT_MAX in case the peer is not in the path
119 path_get_length (struct MeshPeerPath *path)
128 * Get the cost of the path relative to the already built tunnel tree
130 * @param t The tunnel tree to which compare
131 * @param path The individual path to reach a peer
133 * @return Number of hops to reach destination, UINT_MAX in case the peer is not
136 * TODO: remove dummy implementation, look into the tunnel tree
139 path_get_cost (struct MeshTunnelTree *t, struct MeshPeerPath *path)
141 return path_get_length (path);
146 * Recursively find the given peer in the tree.
148 * @param t Tunnel where to look for the peer.
149 * @param peer Peer to find
151 * @return Pointer to the node of the peer. NULL if not found.
153 struct MeshTunnelTreeNode *
154 tree_find_peer (struct MeshTunnelTreeNode *root, GNUNET_PEER_Id peer_id)
156 struct MeshTunnelTreeNode *n;
159 if (root->peer == peer_id)
161 for (i = 0; i < root->nchildren; i++)
163 n = tree_find_peer (&root->children[i], peer_id);
172 * Recusively mark peer and children as disconnected, notify client
174 * @param tree Tree this node belongs to
175 * @param parent Node to be clean, potentially with children
176 * @param cb Callback to use to notify about disconnected peers.
179 tree_mark_peers_disconnected (struct MeshTunnelTree *tree,
180 struct MeshTunnelTreeNode *parent,
181 MeshNodeDisconnectCB cb)
183 struct GNUNET_PeerIdentity *pi;
184 struct GNUNET_PeerIdentity id;
187 for (i = 0; i < parent->nchildren; i++)
189 tree_mark_peers_disconnected (tree, &parent->children[i], cb);
191 if (MESH_PEER_READY == parent->status)
195 parent->status = MESH_PEER_RECONNECTING;
197 /* Remove and free info about first hop */
198 GNUNET_PEER_resolve(parent->peer, &id);
199 pi = GNUNET_CONTAINER_multihashmap_get(tree->first_hops, &id.hashPubKey);
200 GNUNET_CONTAINER_multihashmap_remove_all(tree->first_hops, &id.hashPubKey);
203 // FIXME: add to service code on callback
204 // struct GNUNET_MESH_PeerControl msg;
205 // if (NULL == parent->t->client)
207 // msg.header.size = htons (sizeof (msg));
208 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
209 // msg.tunnel_id = htonl (parent->t->local_tid);
210 // GNUNET_PEER_resolve (parent->peer, &msg.peer);
213 // GNUNET_SERVER_notification_context_unicast (nc, parent->t->client->handle,
214 // &msg.header, GNUNET_NO);
219 * Recusively update the info about what is the first hop to reach the node
221 * @param tree Tree this nodes belongs to
222 * @param parent Node to be start updating
223 * @param hop If known, ID of the first hop.
224 * If not known, NULL to find out and pass on children.
227 tree_update_first_hops (struct MeshTunnelTree *tree,
228 struct MeshTunnelTreeNode *parent,
229 struct GNUNET_PeerIdentity *hop)
231 struct GNUNET_PeerIdentity pi;
232 struct GNUNET_PeerIdentity *copy;
233 struct GNUNET_PeerIdentity id;
236 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
237 "tree: Finding first hop for %u.\n",
241 struct MeshTunnelTreeNode *aux;
242 struct MeshTunnelTreeNode *old;
246 while (aux != tree->me)
248 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
249 "tree: ... its not %u.\n",
253 GNUNET_assert(NULL != aux);
255 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
259 GNUNET_PEER_resolve(old->peer, hop);
261 copy = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
263 GNUNET_PEER_resolve(parent->peer, &id);
264 GNUNET_CONTAINER_multihashmap_put(tree->first_hops, &id.hashPubKey, copy,
265 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
267 for (i = 0; i < parent->nchildren; i++)
269 tree_update_first_hops (tree, &parent->children[i], hop);
275 * Delete the current path to the peer, including all now unused relays.
276 * The destination peer is NOT destroyed, it is returned in order to either set
277 * a new path to it or destroy it explicitly, taking care of it's child nodes.
279 * @param t Tunnel tree where to delete the path from.
280 * @param peer Destination peer whose path we want to remove.
281 * @param cb Callback to use to notify about disconnected peers.
283 * @return pointer to the pathless node, NULL on error
285 struct MeshTunnelTreeNode *
286 tree_del_path (struct MeshTunnelTree *t, GNUNET_PEER_Id peer_id,
287 MeshNodeDisconnectCB cb)
289 struct MeshTunnelTreeNode *parent;
290 struct MeshTunnelTreeNode *node;
291 struct MeshTunnelTreeNode *n;
293 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tree: Deleting path to %u.\n", peer_id);
294 if (peer_id == t->root->peer)
296 n = tree_find_peer (t->me, peer_id);
299 node = GNUNET_malloc(sizeof(struct MeshTunnelTreeNode));
304 *n = parent->children[parent->nchildren];
305 parent->children = GNUNET_realloc(parent->children,
307 * sizeof(struct MeshTunnelTreeNode));
308 while (t->root != parent && MESH_PEER_RELAY == parent->status &&
309 0 == parent->nchildren)
311 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tree: Deleting node %u.\n", parent->peer);
313 tree_node_destroy(parent);
317 tree_mark_peers_disconnected (t, node, cb);
324 * Return a newly allocated individual path to reach a peer from the local peer,
325 * according to the path tree of some tunnel.
327 * @param t Tunnel from which to read the path tree
328 * @param peer_info Destination peer to whom we want a path
330 * @return A newly allocated individual path to reach the destination peer.
331 * Path must be destroyed afterwards.
333 struct MeshPeerPath *
334 tree_get_path_to_peer(struct MeshTunnelTree *t, GNUNET_PEER_Id peer)
336 struct MeshTunnelTreeNode *n;
337 struct MeshPeerPath *p;
338 GNUNET_PEER_Id myid = t->me->peer;
340 n = tree_find_peer(t->me, peer);
341 p = GNUNET_malloc(sizeof(struct MeshPeerPath));
343 /* Building the path (inverted!) */
344 while (n->peer != myid)
346 GNUNET_array_append(p->peers, p->length, n->peer);
347 GNUNET_PEER_change_rc(n->peer, 1);
349 GNUNET_assert(NULL != n);
351 GNUNET_array_append(p->peers, p->length, myid);
352 GNUNET_PEER_change_rc(myid, 1);
361 * Integrate a stand alone path into the tunnel tree.
363 * @param t Tunnel where to add the new path.
364 * @param p Path to be integrated.
365 * @param cb Callback to use to notify about peers temporarily disconnecting
367 * @return GNUNET_OK in case of success.
368 * GNUNET_SYSERR in case of error.
371 * - go backwards on path looking for each peer in the present tree
372 * - do not disconnect peers until new path is created & connected
375 tree_add_path (struct MeshTunnelTree *t, const struct MeshPeerPath *p,
376 MeshNodeDisconnectCB cb)
378 struct MeshTunnelTreeNode *parent;
379 struct MeshTunnelTreeNode *oldnode;
380 struct MeshTunnelTreeNode *n;
381 struct GNUNET_PeerIdentity id;
382 struct GNUNET_PeerIdentity *hop;
383 GNUNET_PEER_Id myid = t->me->peer;
388 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
389 "tree: Adding path [%u] towards peer %u to peer %u.\n",
391 p->peers[p->length - 1],
393 GNUNET_assert(0 != p->length);
394 parent = n = t->root;
395 if (n->peer != p->peers[0])
398 return GNUNET_SYSERR;
402 oldnode = tree_del_path (t, p->peers[p->length - 1], cb);
403 /* Look for the first node that is not already present in the tree
405 * Assuming that the tree is somewhat balanced, O(log n * log N).
406 * - Length of the path is expected to be log N (size of whole network).
407 * - Each level of the tree is expected to have log n children (size of tree).
409 me = t->root->peer == myid ? 0 : -1;
410 for (i = 1; i < p->length; i++)
412 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
413 "tree: Looking for peer %u.\n",
416 if (p->peers[i] == myid)
418 for (j = 0; j < n->nchildren; j++)
420 if (n->children[j].peer == p->peers[i])
426 /* If we couldn't find a child equal to path[i], we have reached the end
427 * of the common path. */
431 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
432 "tree: All childen visited.\n");
435 /* New path deviates from tree before reaching us. What happened? */
437 return GNUNET_SYSERR;
439 /* Add the rest of the path as a branch from parent. */
440 while (i < p->length)
442 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
443 "tree: Adding peer %u, to %u.\n",
447 parent->children = GNUNET_realloc (parent->children,
449 sizeof(struct MeshTunnelTreeNode));
450 n = &parent->children[parent->nchildren - 1];
452 if (i == p->length - 1 && NULL != oldnode)
454 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tree: Putting old node into place.\n");
455 /* Assignation and free can be misleading, using explicit mempcy */
456 memcpy (n, oldnode, sizeof (struct MeshTunnelTreeNode));
458 GNUNET_free (oldnode);
459 for (j = 0; j < n->nchildren; j++)
461 n->children[j].parent = n;
462 tree_update_first_hops (t, &n->children[j], NULL);
467 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tree: Creating new node.\n");
469 n->status = MESH_PEER_RELAY;
470 n->peer = p->peers[i];
477 n->status = MESH_PEER_SEARCHING;
479 /* Add info about first hop into hashmap. */
480 if (me < p->length - 1)
482 GNUNET_PEER_resolve (p->peers[p->length - 1], &id);
483 hop = GNUNET_CONTAINER_multihashmap_get(t->first_hops, &id.hashPubKey);
486 hop = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
487 GNUNET_PEER_resolve (p->peers[me + 1], hop);
488 GNUNET_CONTAINER_multihashmap_put (t->first_hops, &id.hashPubKey,
490 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
492 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tree: New node added.\n");
498 * Destroy the node and all children
500 * @param n Parent node to be destroyed
503 tree_node_destroy (struct MeshTunnelTreeNode *n)
505 struct MeshTunnelTreeNode *parent;
508 if (n->nchildren != 0)
510 for (i = 0; i < n->nchildren; i++)
512 tree_node_destroy(&n->children[i]);
514 if (n->children != NULL)
515 GNUNET_free(n->children);
517 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tree: Destroying node %u.\n", n->peer);
518 if (NULL == (parent = n->parent))
520 i = (n - parent->children) / sizeof(struct MeshTunnelTreeNode);
521 parent->children[i] = parent->children[parent->nchildren - 1];
523 parent->children = realloc(parent->children,
525 * sizeof(struct MeshTunnelTreeNode));
527 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tree: Destroyed.\n");
532 * Iterator over hash map peer entries and frees all data in it.
533 * Used prior to destroying a hashmap. Makes you miss anonymous functions in C.
536 * @param key current key code (will no longer contain valid data!!)
537 * @param value value in the hash map (treated as void *)
538 * @return GNUNET_YES if we should continue to iterate, GNUNET_NO if not.
541 iterate_free (void *cls, const GNUNET_HashCode * key, void *value)
549 * Destroy the whole tree and free all used memory and Peer_Ids
551 * @param t Tree to be destroyed
554 tree_destroy (struct MeshTunnelTree *t)
556 tree_node_destroy(t->root);
557 GNUNET_free(t->root);
558 GNUNET_CONTAINER_multihashmap_iterate(t->first_hops, &iterate_free, NULL);
559 GNUNET_CONTAINER_multihashmap_destroy(t->first_hops);