2 This file is part of GNUnet.
3 Copyright (C) 2013, 2017 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file cadet/gnunet-service-cadet-new_dht.c
22 * @brief Information we track per peer.
23 * @author Bartlomiej Polot
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_dht_service.h"
30 #include "gnunet_statistics_service.h"
31 #include "gnunet-service-cadet-new.h"
32 #include "gnunet-service-cadet-new_dht.h"
33 #include "gnunet-service-cadet-new_hello.h"
34 #include "gnunet-service-cadet-new_peer.h"
35 #include "gnunet-service-cadet-new_paths.h"
37 #define LOG(level, ...) GNUNET_log_from (level,"cadet-dht",__VA_ARGS__)
41 * Handle for DHT searches.
43 struct GCD_search_handle
48 struct GNUNET_DHT_GetHandle *dhtget;
51 * Provided callback to call when a path is found.
53 GCD_search_callback callback;
66 static struct GNUNET_DHT_Handle *dht_handle;
69 * How often to PUT own ID in the DHT.
71 static struct GNUNET_TIME_Relative id_announce_time;
74 * DHT replication level, see DHT API: #GNUNET_DHT_get_start(), #GNUNET_DHT_put().
76 static unsigned long long dht_replication_level;
79 * Task to periodically announce itself in the network.
81 static struct GNUNET_SCHEDULER_Task *announce_id_task;
84 * Delay for the next ID announce.
86 static struct GNUNET_TIME_Relative announce_delay;
91 * Function to process paths received for a new peer addition. The recorded
92 * paths form the initial tunnel, which can be optimized later.
93 * Called on each result obtained for the DHT search.
96 * @param exp when will this value expire
97 * @param key key of the result
98 * @param get_path path of the get request
99 * @param get_path_length lenght of @a get_path
100 * @param put_path path of the put request
101 * @param put_path_length length of the @a put_path
102 * @param type type of the result
103 * @param size number of bytes in data
104 * @param data pointer to the result data
107 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
108 const struct GNUNET_HashCode *key,
109 const struct GNUNET_PeerIdentity *get_path,
110 unsigned int get_path_length,
111 const struct GNUNET_PeerIdentity *put_path,
112 unsigned int put_path_length,
113 enum GNUNET_BLOCK_Type type,
117 struct GCD_search_handle *h = cls;
118 const struct GNUNET_HELLO_Message *hello = data;
119 struct CadetPeerPath *p;
120 struct CadetPeer *peer;
122 p = GCPP_path_from_dht (get_path,
126 h->callback (h->cls, p);
127 GCPP_path_destroy (p);
129 if ( (size >= sizeof (struct GNUNET_HELLO_Message)) &&
130 (ntohs (hello->header.size) == size) &&
131 (size == GNUNET_HELLO_size (hello)) )
133 peer = GCP_get (&put_path[0],
135 LOG (GNUNET_ERROR_TYPE_DEBUG,
136 "Got HELLO for %s\n",
145 * Periodically announce self id in the DHT
150 announce_id (void *cls)
152 struct GNUNET_HashCode phash;
153 const struct GNUNET_HELLO_Message *hello;
155 struct GNUNET_TIME_Absolute expiration;
156 struct GNUNET_TIME_Relative next_put;
158 hello = GCH_get_mine ();
159 size = (NULL != hello) ? GNUNET_HELLO_size (hello) : 0;
162 expiration = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
164 announce_delay = GNUNET_TIME_STD_BACKOFF (announce_delay);
168 expiration = GNUNET_HELLO_get_last_expiration (hello);
169 announce_delay = GNUNET_TIME_UNIT_SECONDS;
172 /* Call again in id_announce_time, unless HELLO expires first,
173 * but wait at least 1s. */
175 = GNUNET_TIME_absolute_get_remaining (expiration);
177 = GNUNET_TIME_relative_min (next_put,
180 = GNUNET_TIME_relative_max (next_put,
181 GNUNET_TIME_UNIT_SECONDS);
183 = GNUNET_SCHEDULER_add_delayed (next_put,
186 GNUNET_STATISTICS_update (stats,
193 GNUNET_memcpy (&phash,
195 sizeof (my_full_id));
196 LOG (GNUNET_ERROR_TYPE_DEBUG,
197 "Announcing my HELLO (%u bytes) in the DHT\n",
199 GNUNET_DHT_put (dht_handle, /* DHT handle */
200 &phash, /* Key to use */
201 dht_replication_level, /* Replication level */
202 GNUNET_DHT_RO_RECORD_ROUTE
203 | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
204 GNUNET_BLOCK_TYPE_DHT_HELLO, /* Block type */
205 size, /* Size of the data */
206 (const char *) hello, /* Data itself */
207 expiration, /* Data expiration */
208 NULL, /* Continuation */
209 NULL); /* Continuation closure */
214 * Initialize the DHT subsystem.
216 * @param c Configuration.
219 GCD_init (const struct GNUNET_CONFIGURATION_Handle *c)
221 LOG (GNUNET_ERROR_TYPE_DEBUG,
224 GNUNET_CONFIGURATION_get_value_number (c,
226 "DHT_REPLICATION_LEVEL",
227 &dht_replication_level))
229 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
231 "DHT_REPLICATION_LEVEL",
233 dht_replication_level = 3;
237 GNUNET_CONFIGURATION_get_value_time (c,
242 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
246 GNUNET_SCHEDULER_shutdown ();
250 dht_handle = GNUNET_DHT_connect (c,
252 GNUNET_break (NULL != dht_handle);
253 announce_delay = GNUNET_TIME_UNIT_SECONDS;
254 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id,
260 * Shut down the DHT subsystem.
265 if (NULL != dht_handle)
267 GNUNET_DHT_disconnect (dht_handle);
270 if (NULL != announce_id_task)
272 GNUNET_SCHEDULER_cancel (announce_id_task);
273 announce_id_task = NULL;
279 * Search DHT for paths to @a peeR_id
281 * @param peer_id peer to search for
282 * @param callback function to call with results
283 * @param callback_cls closure for @a callback
284 * @return handle to abort search
286 struct GCD_search_handle *
287 GCD_search (const struct GNUNET_PeerIdentity *peer_id,
288 GCD_search_callback callback,
291 struct GNUNET_HashCode phash;
292 struct GCD_search_handle *h;
294 LOG (GNUNET_ERROR_TYPE_DEBUG,
295 "Starting DHT GET for peer %s\n",
296 GNUNET_i2s (peer_id));
297 GNUNET_STATISTICS_update (stats,
304 GNUNET_memcpy (&phash,
308 h = GNUNET_new (struct GCD_search_handle);
309 h->callback = callback;
310 h->cls = callback_cls;
311 h->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
312 GNUNET_BLOCK_TYPE_DHT_HELLO, /* type */
313 &phash, /* key to search */
314 dht_replication_level, /* replication level */
315 GNUNET_DHT_RO_RECORD_ROUTE |
316 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
326 * Stop DHT search started with #GCD_search().
328 * @param h handle to search to stop
331 GCD_search_stop (struct GCD_search_handle *h)
333 GNUNET_DHT_get_stop (h->dhtget);
337 /* end of gnunet-service-cadet_dht.c */