2 This file is part of GNUnet.
3 (C) 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.
23 #include "gnunet_util_lib.h"
25 #include "gnunet_dht_service.h"
26 #include "gnunet_statistics_service.h"
28 #include "block_mesh.h"
29 #include "mesh_path.h"
30 #include "gnunet-service-mesh_dht.h"
31 #include "gnunet-service-mesh_peer.h"
33 #define LOG(level, ...) GNUNET_log_from (level,"mesh-dht",__VA_ARGS__)
36 /******************************************************************************/
37 /******************************** STRUCTS **********************************/
38 /******************************************************************************/
41 * Handle for DHT searches.
43 struct GMD_search_handle
45 /** DHT_GET handle. */
46 struct GNUNET_DHT_GetHandle *dhtget;
48 /** Provided callback to call when a path is found. */
49 GMD_search_callback callback;
51 /** Provided closure. */
56 /******************************************************************************/
57 /******************************* GLOBALS ***********************************/
58 /******************************************************************************/
61 * Global handle to the statistics service.
63 extern struct GNUNET_STATISTICS_Handle *stats;
66 * Own ID (short value).
68 extern GNUNET_PEER_Id myid;
71 * Own ID (full value).
73 extern struct GNUNET_PeerIdentity my_full_id;
78 static struct GNUNET_DHT_Handle *dht_handle;
81 * How often to PUT own ID in the DHT.
83 static struct GNUNET_TIME_Relative id_announce_time;
86 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
88 static unsigned long long dht_replication_level;
91 * Task to periodically announce itself in the network.
93 static GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
96 /******************************************************************************/
97 /******************************** STATIC ***********************************/
98 /******************************************************************************/
102 * Build a PeerPath from the paths returned from the DHT, reversing the paths
103 * to obtain a local peer -> destination path and interning the peer ids.
105 * @return Newly allocated and created path
107 static struct MeshPeerPath *
108 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
109 unsigned int get_path_length,
110 const struct GNUNET_PeerIdentity *put_path,
111 unsigned int put_path_length)
113 struct MeshPeerPath *p;
119 GNUNET_PEER_change_rc (myid, 1);
121 LOG (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
122 for (i--; i >= 0; i--)
124 id = GNUNET_PEER_intern (&get_path[i]);
125 if (p->length > 0 && id == p->peers[p->length - 1])
127 LOG (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
128 GNUNET_PEER_change_rc (id, -1);
132 LOG (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
133 GNUNET_i2s (&get_path[i]));
135 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
136 p->peers[p->length - 1] = id;
140 LOG (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
141 for (i--; i >= 0; i--)
143 id = GNUNET_PEER_intern (&put_path[i]);
146 /* PUT path went through us, so discard the path up until now and start
147 * from here to get a much shorter (and loop-free) path.
152 if (p->length > 0 && id == p->peers[p->length - 1])
154 LOG (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
155 GNUNET_PEER_change_rc (id, -1);
159 LOG (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
160 GNUNET_i2s (&put_path[i]));
162 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
163 p->peers[p->length - 1] = id;
167 if (get_path_length > 0)
168 LOG (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
169 GNUNET_i2s (&get_path[0]));
170 if (put_path_length > 0)
171 LOG (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
172 GNUNET_i2s (&put_path[0]));
173 LOG (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
175 for (i = 0; i < p->length; i++)
177 struct GNUNET_PeerIdentity peer_id;
179 GNUNET_PEER_resolve (p->peers[i], &peer_id);
180 LOG (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
181 GNUNET_i2s (&peer_id));
189 * Function to process paths received for a new peer addition. The recorded
190 * paths form the initial tunnel, which can be optimized later.
191 * Called on each result obtained for the DHT search.
194 * @param exp when will this value expire
195 * @param key key of the result
196 * @param get_path path of the get request
197 * @param get_path_length lenght of get_path
198 * @param put_path path of the put request
199 * @param put_path_length length of the put_path
200 * @param type type of the result
201 * @param size number of bytes in data
202 * @param data pointer to the result data
205 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
206 const struct GNUNET_HashCode * key,
207 const struct GNUNET_PeerIdentity *get_path,
208 unsigned int get_path_length,
209 const struct GNUNET_PeerIdentity *put_path,
210 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
211 size_t size, const void *data)
213 struct GMD_search_handle *h = cls;
214 struct MeshPeerPath *p;
216 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got results!\n");
217 p = path_build_from_dht (get_path, get_path_length,
218 put_path, put_path_length);
219 h->callback (h->cls, p);
226 * Periodically announce self id in the DHT
229 * @param tc task context
232 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
235 struct GNUNET_HashCode phash;
237 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
239 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
244 * - Set data expiration in function of X
247 block.id = my_full_id;
248 GNUNET_CRYPTO_hash (&my_full_id, sizeof (struct GNUNET_PeerIdentity), &phash);
249 GNUNET_DHT_put (dht_handle, /* DHT handle */
250 &phash, /* Key to use */
251 dht_replication_level, /* Replication level */
252 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
253 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
254 sizeof (block), /* Size of the data */
255 (const char *) &block, /* Data itself */
256 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
257 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
258 NULL, /* Continuation */
259 NULL); /* Continuation closure */
261 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
265 /******************************************************************************/
266 /******************************** API ***********************************/
267 /******************************************************************************/
270 * Initialize the DHT subsystem.
272 * @param c Configuration.
273 * @param peer_id Local peer ID (must remain valid during all execution time).
276 GMD_init (const struct GNUNET_CONFIGURATION_Handle *c)
278 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
280 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
281 &dht_replication_level))
283 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
284 "MESH", "DHT_REPLICATION_LEVEL", "USING DEFAULT");
285 dht_replication_level = 3;
289 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
292 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
293 "MESH", "ID_ANNOUNCE_TIME", "MISSING");
294 GNUNET_SCHEDULER_shutdown ();
298 dht_handle = GNUNET_DHT_connect (c, 64);
299 if (NULL == dht_handle)
304 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, NULL);
309 * Shut down the DHT subsystem.
314 if (dht_handle != NULL)
316 GNUNET_DHT_disconnect (dht_handle);
319 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
321 GNUNET_SCHEDULER_cancel (announce_id_task);
322 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
326 struct GMD_search_handle *
327 GMD_search (const struct GNUNET_PeerIdentity *peer_id,
328 GMD_search_callback callback, void *cls)
330 struct GNUNET_HashCode phash;
331 struct GMD_search_handle *h;
333 LOG (GNUNET_ERROR_TYPE_DEBUG,
334 " Starting DHT GET for peer %s\n", GNUNET_i2s (peer_id));
335 GNUNET_CRYPTO_hash (peer_id, sizeof (struct GNUNET_PeerIdentity), &phash);
336 h = GNUNET_new (struct GMD_search_handle);
338 h->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
339 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
340 &phash, /* key to search */
341 dht_replication_level, /* replication level */
342 GNUNET_DHT_RO_RECORD_ROUTE |
343 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
346 &dht_get_id_handler, h);
351 GMD_search_stop (struct GMD_search_handle *h)
353 GNUNET_DHT_get_stop (h->dhtget);