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 "mesh_path.h"
29 #include "gnunet-service-mesh_dht.h"
30 #include "gnunet-service-mesh_peer.h"
32 #define LOG(level, ...) GNUNET_log_from (level,"mesh-dht",__VA_ARGS__)
35 /******************************************************************************/
36 /******************************** STRUCTS **********************************/
37 /******************************************************************************/
40 * Handle for DHT searches.
42 struct GMD_search_handle
44 /** DHT_GET handle. */
45 struct GNUNET_DHT_GetHandle *dhtget;
47 /** Provided callback to call when a path is found. */
48 GMD_search_callback callback;
50 /** Provided closure. */
55 /******************************************************************************/
56 /******************************* GLOBALS ***********************************/
57 /******************************************************************************/
60 * Global handle to the statistics service.
62 extern struct GNUNET_STATISTICS_Handle *stats;
65 * Own ID (short value).
67 extern GNUNET_PEER_Id myid;
70 * Own ID (full value).
72 extern struct GNUNET_PeerIdentity my_full_id;
77 static struct GNUNET_DHT_Handle *dht_handle;
80 * How often to PUT own ID in the DHT.
82 static struct GNUNET_TIME_Relative id_announce_time;
85 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
87 static unsigned long long dht_replication_level;
90 * Task to periodically announce itself in the network.
92 static GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
95 /******************************************************************************/
96 /******************************** STATIC ***********************************/
97 /******************************************************************************/
101 * Build a PeerPath from the paths returned from the DHT, reversing the paths
102 * to obtain a local peer -> destination path and interning the peer ids.
104 * @return Newly allocated and created path
106 static struct MeshPeerPath *
107 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
108 unsigned int get_path_length,
109 const struct GNUNET_PeerIdentity *put_path,
110 unsigned int put_path_length)
112 struct MeshPeerPath *p;
118 GNUNET_PEER_change_rc (myid, 1);
120 LOG (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
121 for (i--; i >= 0; i--)
123 id = GNUNET_PEER_intern (&get_path[i]);
124 if (p->length > 0 && id == p->peers[p->length - 1])
126 LOG (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
127 GNUNET_PEER_change_rc (id, -1);
131 LOG (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
132 GNUNET_i2s (&get_path[i]));
134 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
135 p->peers[p->length - 1] = id;
139 LOG (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
140 for (i--; i >= 0; i--)
142 id = GNUNET_PEER_intern (&put_path[i]);
145 /* PUT path went through us, so discard the path up until now and start
146 * from here to get a much shorter (and loop-free) path.
151 if (p->length > 0 && id == p->peers[p->length - 1])
153 LOG (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
154 GNUNET_PEER_change_rc (id, -1);
158 LOG (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
159 GNUNET_i2s (&put_path[i]));
161 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
162 p->peers[p->length - 1] = id;
166 if (get_path_length > 0)
167 LOG (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
168 GNUNET_i2s (&get_path[0]));
169 if (put_path_length > 0)
170 LOG (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
171 GNUNET_i2s (&put_path[0]));
172 LOG (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
174 for (i = 0; i < p->length; i++)
176 struct GNUNET_PeerIdentity peer_id;
178 GNUNET_PEER_resolve (p->peers[i], &peer_id);
179 LOG (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
180 GNUNET_i2s (&peer_id));
188 * Function to process paths received for a new peer addition. The recorded
189 * paths form the initial tunnel, which can be optimized later.
190 * Called on each result obtained for the DHT search.
193 * @param exp when will this value expire
194 * @param key key of the result
195 * @param get_path path of the get request
196 * @param get_path_length lenght of get_path
197 * @param put_path path of the put request
198 * @param put_path_length length of the put_path
199 * @param type type of the result
200 * @param size number of bytes in data
201 * @param data pointer to the result data
204 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
205 const struct GNUNET_HashCode * key,
206 const struct GNUNET_PeerIdentity *get_path,
207 unsigned int get_path_length,
208 const struct GNUNET_PeerIdentity *put_path,
209 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
210 size_t size, const void *data)
212 struct GMD_search_handle *h = cls;
213 struct MeshPeerPath *p;
215 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got results!\n");
216 p = path_build_from_dht (get_path, get_path_length,
217 put_path, put_path_length);
218 h->callback (h->cls, p);
225 * Periodically announce self id in the DHT
228 * @param tc task context
231 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
234 struct GNUNET_HashCode phash;
236 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
238 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
243 * - Set data expiration in function of X
246 block.id = my_full_id;
247 GNUNET_CRYPTO_hash (&my_full_id, sizeof (struct GNUNET_PeerIdentity), &phash);
248 GNUNET_DHT_put (dht_handle, /* DHT handle */
249 &phash, /* Key to use */
250 dht_replication_level, /* Replication level */
251 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
252 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
253 sizeof (block), /* Size of the data */
254 (const char *) &block, /* Data itself */
255 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
256 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
257 NULL, /* Continuation */
258 NULL); /* Continuation closure */
260 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
264 /******************************************************************************/
265 /******************************** API ***********************************/
266 /******************************************************************************/
269 * Initialize the DHT subsystem.
271 * @param c Configuration.
272 * @param peer_id Local peer ID (must remain valid during all execution time).
275 GMD_init (const struct GNUNET_CONFIGURATION_Handle *c)
279 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
280 &dht_replication_level))
282 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
283 "MESH", "DHT_REPLICATION_LEVEL", "USING DEFAULT");
284 dht_replication_level = 3;
288 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
291 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
292 "MESH", "ID_ANNOUNCE_TIME", "MISSING");
293 GNUNET_SCHEDULER_shutdown ();
297 dht_handle = GNUNET_DHT_connect (c, 64);
298 if (NULL == dht_handle)
303 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, NULL);
308 * Shut down the DHT subsystem.
313 if (dht_handle != NULL)
315 GNUNET_DHT_disconnect (dht_handle);
318 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
320 GNUNET_SCHEDULER_cancel (announce_id_task);
321 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
325 struct GMD_search_handle *
326 GMD_search (const struct GNUNET_PeerIdentity *peer_id,
327 GMD_search_callback callback, void *cls)
329 struct GNUNET_HashCode phash;
330 struct GMD_search_handle *h;
332 LOG (GNUNET_ERROR_TYPE_DEBUG,
333 " Starting DHT GET for peer %s\n", GNUNET_i2s (peer_id));
334 GNUNET_CRYPTO_hash (peer_id, sizeof (struct GNUNET_PeerIdentity), &phash);
335 h = GNUNET_new (struct GMD_search_handle);
337 h->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
338 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
339 &phash, /* key to search */
340 dht_replication_level, /* replication level */
341 GNUNET_DHT_RO_RECORD_ROUTE |
342 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
345 &dht_get_id_handler, h);
350 GMD_search_stop (struct GMD_search_handle *h)
352 GNUNET_DHT_get_stop (h->dhtget);