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"
31 #include "gnunet-service-mesh_hello.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. */
54 /** Peer ID searched for */
55 GNUNET_PEER_Id peer_id;
59 /******************************************************************************/
60 /******************************* GLOBALS ***********************************/
61 /******************************************************************************/
64 * Global handle to the statistics service.
66 extern struct GNUNET_STATISTICS_Handle *stats;
69 * Own ID (short value).
71 extern GNUNET_PEER_Id myid;
74 * Own ID (full value).
76 extern struct GNUNET_PeerIdentity my_full_id;
81 static struct GNUNET_DHT_Handle *dht_handle;
84 * How often to PUT own ID in the DHT.
86 static struct GNUNET_TIME_Relative id_announce_time;
89 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
91 static unsigned long long dht_replication_level;
94 * Task to periodically announce itself in the network.
96 static GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
99 * GET requests to stop on shutdown.
101 static struct GNUNET_CONTAINER_MultiHashMap32 *get_requests;
103 /******************************************************************************/
104 /******************************** STATIC ***********************************/
105 /******************************************************************************/
109 * Build a PeerPath from the paths returned from the DHT, reversing the paths
110 * to obtain a local peer -> destination path and interning the peer ids.
112 * @return Newly allocated and created path
114 * FIXME refactor and use build_path_from_peer_ids
116 static struct MeshPeerPath *
117 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
118 unsigned int get_path_length,
119 const struct GNUNET_PeerIdentity *put_path,
120 unsigned int put_path_length)
122 struct MeshPeerPath *p;
128 GNUNET_PEER_change_rc (myid, 1);
130 LOG (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
131 for (i--; i >= 0; i--)
133 id = GNUNET_PEER_intern (&get_path[i]);
134 if (p->length > 0 && id == p->peers[p->length - 1])
136 LOG (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
137 GNUNET_PEER_change_rc (id, -1);
141 LOG (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
142 GNUNET_i2s (&get_path[i]));
144 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
145 p->peers[p->length - 1] = id;
149 LOG (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
150 for (i--; i >= 0; i--)
152 id = GNUNET_PEER_intern (&put_path[i]);
155 /* PUT path went through us, so discard the path up until now and start
156 * from here to get a much shorter (and loop-free) path.
161 if (p->length > 0 && id == p->peers[p->length - 1])
163 LOG (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
164 GNUNET_PEER_change_rc (id, -1);
168 LOG (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
169 GNUNET_i2s (&put_path[i]));
171 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
172 p->peers[p->length - 1] = id;
176 if (get_path_length > 0)
177 LOG (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
178 GNUNET_i2s (&get_path[0]));
179 if (put_path_length > 0)
180 LOG (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
181 GNUNET_i2s (&put_path[0]));
182 LOG (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
184 for (i = 0; i < p->length; i++)
186 struct GNUNET_PeerIdentity peer_id;
188 GNUNET_PEER_resolve (p->peers[i], &peer_id);
189 LOG (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
190 GNUNET_i2s (&peer_id));
198 * Function to process paths received for a new peer addition. The recorded
199 * paths form the initial tunnel, which can be optimized later.
200 * Called on each result obtained for the DHT search.
203 * @param exp when will this value expire
204 * @param key key of the result
205 * @param get_path path of the get request
206 * @param get_path_length lenght of get_path
207 * @param put_path path of the put request
208 * @param put_path_length length of the put_path
209 * @param type type of the result
210 * @param size number of bytes in data
211 * @param data pointer to the result data
214 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
215 const struct GNUNET_HashCode * key,
216 const struct GNUNET_PeerIdentity *get_path,
217 unsigned int get_path_length,
218 const struct GNUNET_PeerIdentity *put_path,
219 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
220 size_t size, const void *data)
222 struct GMD_search_handle *h = cls;
223 struct GNUNET_HELLO_Message *hello;
224 struct MeshPeerPath *p;
226 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got results!\n");
227 p = path_build_from_dht (get_path, get_path_length,
228 put_path, put_path_length);
229 h->callback (h->cls, p);
231 LOG (GNUNET_ERROR_TYPE_ERROR, "Got type %u!\n", type);
232 LOG (GNUNET_ERROR_TYPE_ERROR, "Got size %u!\n", size);
233 hello = (struct GNUNET_HELLO_Message *) data;
234 LOG (GNUNET_ERROR_TYPE_ERROR, "HELLO size %u!\n", GNUNET_HELLO_size (hello));
240 * Periodically announce self id in the DHT
243 * @param tc task context
246 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
248 struct GNUNET_HashCode phash;
249 const struct GNUNET_HELLO_Message *hello;
252 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
254 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
257 LOG (GNUNET_ERROR_TYPE_ERROR, "Announce ID\n");
260 * - Set data expiration in function of X
263 hello = GMH_get_mine ();
264 if (NULL == hello || (size = GNUNET_HELLO_size (hello)) == 0)
266 /* Peerinfo gave us no hello yet, try again in a second. */
267 announce_id_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
269 LOG (GNUNET_ERROR_TYPE_ERROR, " no hello, waiting!\n");
273 LOG (GNUNET_ERROR_TYPE_ERROR, "Hello %p size: %u\n", hello, size);
274 memset (&phash, 0, sizeof (phash));
275 memcpy (&phash, &my_full_id, sizeof (my_full_id));
276 GNUNET_DHT_put (dht_handle, /* DHT handle */
277 &phash, /* Key to use */
278 dht_replication_level, /* Replication level */
279 GNUNET_DHT_RO_RECORD_ROUTE
280 | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
281 GNUNET_BLOCK_TYPE_DHT_HELLO, /* Block type */
282 size, /* Size of the data */
283 (const char *) hello, /* Data itself */
284 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
285 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
286 NULL, /* Continuation */
287 NULL); /* Continuation closure */
289 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
293 * Iterator over hash map entries and stop GET requests before disconnecting
296 * @param cls Closure (unused)
297 * @param key Current peer ID.
298 * @param value Value in the hash map (GMD_search_handle).
300 * @return #GNUNET_YES, we should continue to iterate,
307 struct GMD_search_handle *h = value;
314 /******************************************************************************/
315 /******************************** API ***********************************/
316 /******************************************************************************/
319 * Initialize the DHT subsystem.
321 * @param c Configuration.
324 GMD_init (const struct GNUNET_CONFIGURATION_Handle *c)
326 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
328 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
329 &dht_replication_level))
331 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
332 "MESH", "DHT_REPLICATION_LEVEL", "USING DEFAULT");
333 dht_replication_level = 3;
337 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
340 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
341 "MESH", "ID_ANNOUNCE_TIME", "MISSING");
342 GNUNET_SCHEDULER_shutdown ();
346 dht_handle = GNUNET_DHT_connect (c, 64);
347 if (NULL == dht_handle)
352 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, NULL);
353 get_requests = GNUNET_CONTAINER_multihashmap32_create (32);
358 * Shut down the DHT subsystem.
363 GNUNET_CONTAINER_multihashmap32_iterate (get_requests, &stop_get, NULL);
364 GNUNET_CONTAINER_multihashmap32_destroy (get_requests);
365 if (dht_handle != NULL)
367 GNUNET_DHT_disconnect (dht_handle);
370 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
372 GNUNET_SCHEDULER_cancel (announce_id_task);
373 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
377 struct GMD_search_handle *
378 GMD_search (const struct GNUNET_PeerIdentity *peer_id,
379 GMD_search_callback callback, void *cls)
381 struct GNUNET_HashCode phash;
382 struct GMD_search_handle *h;
384 LOG (GNUNET_ERROR_TYPE_DEBUG,
385 " Starting DHT GET for peer %s\n", GNUNET_i2s (peer_id));
386 memset (&phash, 0, sizeof (phash));
387 memcpy (&phash, &my_full_id, sizeof (my_full_id));
388 h = GNUNET_new (struct GMD_search_handle);
389 h->peer_id = GNUNET_PEER_intern (peer_id);
390 h->callback = callback;
392 h->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
393 GNUNET_BLOCK_TYPE_DHT_HELLO, /* type */
394 &phash, /* key to search */
395 dht_replication_level, /* replication level */
396 GNUNET_DHT_RO_RECORD_ROUTE |
397 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
400 &dht_get_id_handler, h);
401 GNUNET_CONTAINER_multihashmap32_put (get_requests, h->peer_id, h,
402 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
407 GMD_search_stop (struct GMD_search_handle *h)
409 GNUNET_break (GNUNET_OK ==
410 GNUNET_CONTAINER_multihashmap32_remove (get_requests,
412 GNUNET_DHT_get_stop (h->dhtget);