cadet_tunnel_tree.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file cadet/cadet_tunnel_tree.h
  19. * @brief Tunnel tree handling functions
  20. * @author Bartlomiej Polot
  21. */
  22. #include "cadet.h"
  23. /******************************************************************************/
  24. /************************ DATA STRUCTURES ****************************/
  25. /******************************************************************************/
  26. /**
  27. * Information regarding a possible path to reach a single peer
  28. */
  29. struct CadetPeerPath
  30. {
  31. /**
  32. * Linked list
  33. */
  34. struct CadetPeerPath *next;
  35. struct CadetPeerPath *prev;
  36. /**
  37. * List of all the peers that form the path from origin to target.
  38. */
  39. GNUNET_PEER_Id *peers;
  40. /**
  41. * Number of peers (hops) in the path
  42. */
  43. unsigned int length;
  44. };
  45. /**
  46. * Node of path tree for a tunnel
  47. */
  48. struct CadetTunnelTreeNode;
  49. /**
  50. * Tree to reach all peers in the tunnel
  51. */
  52. struct CadetTunnelTree;
  53. /******************************************************************************/
  54. /************************* FUNCTIONS *****************************/
  55. /******************************************************************************/
  56. /**
  57. * Create a new path.
  58. *
  59. * @param length How many hops will the path have.
  60. *
  61. * @return A newly allocated path with a peer array of the specified length.
  62. */
  63. struct CadetPeerPath *
  64. path_new (unsigned int length);
  65. /**
  66. * Invert the path.
  67. *
  68. * @param path The path to invert.
  69. */
  70. void
  71. path_invert (struct CadetPeerPath *path);
  72. /**
  73. * Duplicate a path, incrementing short peer's rc.
  74. *
  75. * @param path The path to duplicate.
  76. */
  77. struct CadetPeerPath *
  78. path_duplicate (struct CadetPeerPath *path);
  79. /**
  80. * Get the length of a path.
  81. *
  82. * @param path The path to measure, with the local peer at any point of it.
  83. *
  84. * @return Number of hops to reach destination.
  85. * UINT_MAX in case the peer is not in the path.
  86. */
  87. unsigned int
  88. path_get_length (struct CadetPeerPath *path);
  89. /**
  90. * Destroy the path and free any allocated resources linked to it
  91. *
  92. * @param p the path to destroy
  93. *
  94. * @return GNUNET_OK on success
  95. */
  96. int
  97. path_destroy (struct CadetPeerPath *p);
  98. /******************************************************************************/
  99. /**
  100. * Iterator over all children of a node.
  101. *
  102. * @param cls Closure.
  103. * @param peer_id Short ID of the peer.
  104. */
  105. typedef void (*CadetTreeCallback) (void *cls, GNUNET_PEER_Id peer_id);
  106. /**
  107. * Iterator over all nodes in a tree.
  108. *
  109. * @param cls Closure.
  110. * @param peer_id Short ID of the peer.
  111. * @param peer_id Short ID of the parent of the peer.
  112. */
  113. typedef void (*CadetWholeTreeCallback) (void *cls,
  114. GNUNET_PEER_Id peer_id,
  115. GNUNET_PEER_Id parent_id);
  116. /**
  117. * Create a new tunnel tree associated to a tunnel
  118. *
  119. * @param peer A short peer id of the root of the tree
  120. *
  121. * @return A newly allocated and initialized tunnel tree
  122. */
  123. struct CadetTunnelTree *
  124. tree_new (GNUNET_PEER_Id peer);
  125. /**
  126. * Set the status of a node.
  127. *
  128. * @param tree Tree.
  129. * @param peer A short peer id of the node.
  130. * @param status New status to set.
  131. */
  132. void
  133. tree_set_status (struct CadetTunnelTree *tree, GNUNET_PEER_Id peer,
  134. enum CadetPeerState status);
  135. /**
  136. * Get the status of a node.
  137. *
  138. * @param tree Tree whose local id we want to now.
  139. * @param peer A short peer id of the node.
  140. *
  141. * @return Short peer id of local peer.
  142. */
  143. enum CadetPeerState
  144. tree_get_status (struct CadetTunnelTree *tree, GNUNET_PEER_Id peer);
  145. /**
  146. * Get the id of the predecessor of the local node.
  147. *
  148. * @param tree Tree whose local id we want to now.
  149. *
  150. * @return Short peer id of local peer.
  151. */
  152. GNUNET_PEER_Id
  153. tree_get_predecessor (struct CadetTunnelTree *tree);
  154. /**
  155. * Find the first peer whom to send a packet to go down this path
  156. *
  157. * @param t The tunnel tree to use
  158. * @param peer The peerinfo of the peer we are trying to reach
  159. *
  160. * @return peerinfo of the peer who is the first hop in the tunnel
  161. * NULL on error
  162. */
  163. struct GNUNET_PeerIdentity *
  164. tree_get_first_hop (struct CadetTunnelTree *t, GNUNET_PEER_Id peer);
  165. /**
  166. * Find the given peer in the tree.
  167. *
  168. * @param tree Tree where to look for the peer.
  169. * @param peer_id Peer to find.
  170. *
  171. * @return Pointer to the node of the peer. NULL if not found.
  172. */
  173. struct CadetTunnelTreeNode *
  174. tree_find_peer (struct CadetTunnelTree *tree, GNUNET_PEER_Id peer_id);
  175. /**
  176. * Iterate over all children of the local node.
  177. *
  178. * @param tree Tree to use. Must have "me" set.
  179. * @param cb Callback to call over each child.
  180. * @param cb_cls Closure for @c cb.
  181. */
  182. void
  183. tree_iterate_children (struct CadetTunnelTree *tree,
  184. CadetTreeCallback cb,
  185. void *cb_cls);
  186. /**
  187. * Iterate over all nodes in the tree.
  188. *
  189. * @param tree Tree to use..
  190. * @param cb Callback to call over each child.
  191. * @param cb_cls Closure for @c cb.
  192. *
  193. * TODO: recursive implementation? (s/heap/stack/g)
  194. */
  195. void
  196. tree_iterate_all (struct CadetTunnelTree *tree,
  197. CadetWholeTreeCallback cb,
  198. void *cb_cls);
  199. /**
  200. * Count how many children does the local node have in the tree.
  201. *
  202. * @param tree Tree to use. Must have "me" set.
  203. */
  204. unsigned int
  205. tree_count_children (struct CadetTunnelTree *tree);
  206. /**
  207. * Recusively update the info about what is the first hop to reach the node
  208. *
  209. * @param tree Tree this nodes belongs to.
  210. * @param parent_id Short ID from node form which to start updating.
  211. * @param hop If known, ID of the first hop.
  212. * If not known, NULL to find out and pass on children.
  213. */
  214. void
  215. tree_update_first_hops (struct CadetTunnelTree *tree, GNUNET_PEER_Id parent_id,
  216. struct GNUNET_PeerIdentity *hop);
  217. /**
  218. * Delete the current path to the peer, including all now unused relays.
  219. * The destination peer is NOT destroyed, it is returned in order to either set
  220. * a new path to it or destroy it explicitly, taking care of it's child nodes.
  221. *
  222. * @param t Tunnel tree where to delete the path from.
  223. * @param peer_id Short ID of the destination peer whose path we want to remove.
  224. * @param cb Callback to use to notify about which peers are going to be
  225. * disconnected.
  226. * @param cbcls Closure for cb.
  227. *
  228. * @return pointer to the pathless node.
  229. * NULL when not found
  230. */
  231. struct CadetTunnelTreeNode *
  232. tree_del_path (struct CadetTunnelTree *t, GNUNET_PEER_Id peer_id,
  233. CadetTreeCallback cb, void *cbcls);
  234. /**
  235. * Return a newly allocated individual path to reach a peer from the local peer,
  236. * according to the path tree of some tunnel.
  237. *
  238. * @param t Tunnel from which to read the path tree
  239. * @param peer Destination peer to whom we want a path
  240. *
  241. * @return A newly allocated individual path to reach the destination peer.
  242. * Path must be destroyed afterwards.
  243. */
  244. struct CadetPeerPath *
  245. tree_get_path_to_peer (struct CadetTunnelTree *t, GNUNET_PEER_Id peer);
  246. /**
  247. * Integrate a stand alone path into the tunnel tree.
  248. *
  249. * @param t Tunnel where to add the new path.
  250. * @param p Path to be integrated.
  251. * @param cb Callback to use to notify about peers temporarily disconnecting.
  252. * @param cbcls Closure for cb.
  253. *
  254. * @return GNUNET_OK in case of success.
  255. * GNUNET_SYSERR in case of error.
  256. */
  257. int
  258. tree_add_path (struct CadetTunnelTree *t, const struct CadetPeerPath *p,
  259. CadetTreeCallback cb, void *cbcls);
  260. /**
  261. * Notifies a tree that a connection it might be using is broken.
  262. * Marks all peers down the paths as disconnected and notifies the client.
  263. *
  264. * @param t Tree to use.
  265. * @param p1 Short id of one of the peers (order unimportant)
  266. * @param p2 Short id of one of the peers (order unimportant)
  267. * @param cb Function to call for every peer that is marked as disconnected.
  268. * @param cbcls Closure for cb.
  269. *
  270. * @return Short ID of the first disconnected peer in the tree.
  271. */
  272. GNUNET_PEER_Id
  273. tree_notify_connection_broken (struct CadetTunnelTree *t, GNUNET_PEER_Id p1,
  274. GNUNET_PEER_Id p2, CadetTreeCallback cb,
  275. void *cbcls);
  276. /**
  277. * Deletes a peer from a tunnel, liberating all unused resources on the path to
  278. * it. It shouldn't have children, if it has they will be destroyed as well.
  279. * If the tree is not local and no longer has any paths, the root node will be
  280. * destroyed and marked as NULL.
  281. *
  282. * FIXME: dont destroy the root
  283. *
  284. * @param t Tunnel tree to use.
  285. * @param peer Short ID of the peer to remove from the tunnel tree.
  286. * @param cb Callback to notify client of disconnected peers.
  287. * @param cbcls Closure for cb.
  288. *
  289. * @return GNUNET_YES if the tunnel still has nodes
  290. */
  291. int
  292. tree_del_peer (struct CadetTunnelTree *t, GNUNET_PEER_Id peer,
  293. CadetTreeCallback cb, void *cbcls);
  294. /**
  295. * Get the cost of the path relative to the already built tunnel tree
  296. *
  297. * @param t The tunnel tree to which compare
  298. * @param path The individual path to reach a peer
  299. *
  300. * @return Number of hops to reach destination, UINT_MAX in case the peer is not
  301. * in the path
  302. */
  303. unsigned int
  304. tree_get_path_cost (struct CadetTunnelTree *t, struct CadetPeerPath *path);
  305. /**
  306. * Print the tree on stderr
  307. *
  308. * @param t The tree
  309. */
  310. void
  311. tree_debug (struct CadetTunnelTree *t);
  312. /**
  313. * Destroy the whole tree and free all used memory and Peer_Ids
  314. *
  315. * @param t Tree to be destroyed
  316. */
  317. void
  318. tree_destroy (struct CadetTunnelTree *t);