aboutsummaryrefslogtreecommitdiff
path: root/src/cadet/gnunet-service-cadet_dht.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/cadet/gnunet-service-cadet_dht.c')
-rw-r--r--src/cadet/gnunet-service-cadet_dht.c354
1 files changed, 0 insertions, 354 deletions
diff --git a/src/cadet/gnunet-service-cadet_dht.c b/src/cadet/gnunet-service-cadet_dht.c
deleted file mode 100644
index d44a50f50..000000000
--- a/src/cadet/gnunet-service-cadet_dht.c
+++ /dev/null
@@ -1,354 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2013, 2017 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
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 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file cadet/gnunet-service-cadet_dht.c
22 * @brief Information we track per peer.
23 * @author Bartlomiej Polot
24 * @author Christian Grothoff
25 */
26
27#include "platform.h"
28#include "gnunet_util_lib.h"
29#include "gnunet_dht_service.h"
30#include "gnunet_statistics_service.h"
31#include "gnunet-service-cadet.h"
32#include "gnunet-service-cadet_dht.h"
33#include "gnunet-service-cadet_hello.h"
34#include "gnunet-service-cadet_peer.h"
35#include "gnunet-service-cadet_paths.h"
36
37/**
38 * How long do we wait before first announcing our presence to the DHT.
39 * Used to wait for our HELLO to be available. Note that we also get
40 * notifications when our HELLO is ready, so this is just the maximum
41 * we wait for the first notification.
42 */
43#define STARTUP_DELAY GNUNET_TIME_relative_multiply ( \
44 GNUNET_TIME_UNIT_MILLISECONDS, 500)
45
46/**
47 * How long do we wait after we get an updated HELLO before publishing?
48 * Allows for the HELLO to be updated again quickly, for example in
49 * case multiple addresses changed and we got a partial update.
50 */
51#define CHANGE_DELAY GNUNET_TIME_relative_multiply ( \
52 GNUNET_TIME_UNIT_MILLISECONDS, 100)
53
54
55#define LOG(level, ...) GNUNET_log_from (level, "cadet-dht", __VA_ARGS__)
56
57
58/**
59 * Handle for DHT searches.
60 */
61struct GCD_search_handle
62{
63 /**
64 * DHT_GET handle.
65 */
66 struct GNUNET_DHT_GetHandle *dhtget;
67};
68
69
70/**
71 * Handle to use DHT.
72 */
73static struct GNUNET_DHT_Handle *dht_handle;
74
75/**
76 * How often to PUT own ID in the DHT.
77 */
78static struct GNUNET_TIME_Relative id_announce_time;
79
80/**
81 * DHT replication level, see DHT API: #GNUNET_DHT_get_start(), #GNUNET_DHT_put().
82 */
83static unsigned long long dht_replication_level;
84
85/**
86 * Task to periodically announce itself in the network.
87 */
88static struct GNUNET_SCHEDULER_Task *announce_id_task;
89
90/**
91 * Delay for the next ID announce.
92 */
93static struct GNUNET_TIME_Relative announce_delay;
94
95
96/**
97 * Function to process paths received for a new peer addition. The recorded
98 * paths form the initial tunnel, which can be optimized later.
99 * Called on each result obtained for the DHT search.
100 *
101 * @param cls closure
102 * @param exp when will this value expire
103 * @param key key of the result
104 * @param get_path path of the get request
105 * @param get_path_length length of @a get_path
106 * @param put_path path of the put request
107 * @param put_path_length length of the @a put_path
108 * @param type type of the result
109 * @param size number of bytes in data
110 * @param data pointer to the result data
111 */
112static void
113dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
114 const struct GNUNET_HashCode *key,
115 const struct GNUNET_DHT_PathElement *get_path,
116 unsigned int get_path_length,
117 const struct GNUNET_DHT_PathElement *put_path,
118 unsigned int put_path_length,
119 enum GNUNET_BLOCK_Type type,
120 size_t size,
121 const void *data)
122{
123 const struct GNUNET_HELLO_Message *hello = data;
124
125 GCPP_try_path_from_dht (get_path,
126 get_path_length,
127 put_path,
128 put_path_length);
129 if ((size >= sizeof(struct GNUNET_HELLO_Message)) &&
130 (ntohs (hello->header.size) == size) &&
131 (size == GNUNET_HELLO_size (hello)))
132 {
133 struct CadetPeer *peer;
134
135 peer = GCP_get (&put_path[0].pred,
136 GNUNET_YES);
137 LOG (GNUNET_ERROR_TYPE_DEBUG,
138 "Got HELLO for %s\n",
139 GCP_2s (peer));
140 GCP_set_hello (peer,
141 hello);
142 }
143}
144
145
146/**
147 * Periodically announce self id in the DHT
148 *
149 * @param cls closure
150 */
151static void
152announce_id (void *cls)
153{
154 struct GNUNET_HashCode phash;
155 const struct GNUNET_HELLO_Message *hello;
156 size_t size;
157 struct GNUNET_TIME_Absolute expiration;
158 struct GNUNET_TIME_Relative next_put;
159
160 hello = GCH_get_mine ();
161 size = (NULL != hello) ? GNUNET_HELLO_size (hello) : 0;
162 if (0 == size)
163 {
164 expiration = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
165 announce_delay);
166 announce_delay = GNUNET_TIME_STD_BACKOFF (announce_delay);
167 }
168 else
169 {
170 expiration = GNUNET_HELLO_get_last_expiration (hello);
171 announce_delay = GNUNET_TIME_UNIT_SECONDS;
172 }
173
174 /* Call again in id_announce_time, unless HELLO expires first,
175 * but wait at least 1s. */
176 next_put
177 = GNUNET_TIME_absolute_get_remaining (expiration);
178 next_put
179 = GNUNET_TIME_relative_min (next_put,
180 id_announce_time);
181 next_put
182 = GNUNET_TIME_relative_max (next_put,
183 GNUNET_TIME_UNIT_SECONDS);
184 announce_id_task
185 = GNUNET_SCHEDULER_add_delayed (next_put,
186 &announce_id,
187 cls);
188 GNUNET_STATISTICS_update (stats,
189 "# DHT announce",
190 1,
191 GNUNET_NO);
192 memset (&phash,
193 0,
194 sizeof(phash));
195 GNUNET_memcpy (&phash,
196 &my_full_id,
197 sizeof(my_full_id));
198 LOG (GNUNET_ERROR_TYPE_DEBUG,
199 "Announcing my HELLO (%lu bytes) in the DHT\n",
200 (unsigned long) size);
201 GNUNET_DHT_put (dht_handle, /* DHT handle */
202 &phash, /* Key to use */
203 dht_replication_level, /* Replication level */
204 GNUNET_DHT_RO_RECORD_ROUTE
205 | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
206 GNUNET_BLOCK_TYPE_DHT_HELLO, /* Block type */
207 size, /* Size of the data */
208 (const char *) hello, /* Data itself */
209 expiration, /* Data expiration */
210 NULL, /* Continuation */
211 NULL); /* Continuation closure */
212}
213
214
215/**
216 * Function called by the HELLO subsystem whenever OUR hello
217 * changes. Re-triggers the DHT PUT immediately.
218 */
219void
220GCD_hello_update ()
221{
222 if (NULL == announce_id_task)
223 return; /* too early */
224 GNUNET_SCHEDULER_cancel (announce_id_task);
225 announce_id_task
226 = GNUNET_SCHEDULER_add_delayed (CHANGE_DELAY,
227 &announce_id,
228 NULL);
229}
230
231
232/**
233 * Initialize the DHT subsystem.
234 *
235 * @param c Configuration.
236 */
237void
238GCD_init (const struct GNUNET_CONFIGURATION_Handle *c)
239{
240 if (GNUNET_OK !=
241 GNUNET_CONFIGURATION_get_value_number (c,
242 "CADET",
243 "DHT_REPLICATION_LEVEL",
244 &dht_replication_level))
245 {
246 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
247 "CADET",
248 "DHT_REPLICATION_LEVEL",
249 "USING DEFAULT");
250 dht_replication_level = 3;
251 }
252
253 if (GNUNET_OK !=
254 GNUNET_CONFIGURATION_get_value_time (c,
255 "CADET",
256 "ID_ANNOUNCE_TIME",
257 &id_announce_time))
258 {
259 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
260 "CADET",
261 "ID_ANNOUNCE_TIME",
262 "MISSING");
263 GNUNET_SCHEDULER_shutdown ();
264 return;
265 }
266
267 dht_handle = GNUNET_DHT_connect (c,
268 64);
269 GNUNET_break (NULL != dht_handle);
270 announce_delay = GNUNET_TIME_UNIT_SECONDS;
271 announce_id_task = GNUNET_SCHEDULER_add_delayed (STARTUP_DELAY,
272 &announce_id,
273 NULL);
274}
275
276
277/**
278 * Shut down the DHT subsystem.
279 */
280void
281GCD_shutdown (void)
282{
283 if (NULL != dht_handle)
284 {
285 GNUNET_DHT_disconnect (dht_handle);
286 dht_handle = NULL;
287 }
288 if (NULL != announce_id_task)
289 {
290 GNUNET_SCHEDULER_cancel (announce_id_task);
291 announce_id_task = NULL;
292 }
293}
294
295
296/**
297 * Search DHT for paths to @a peeR_id
298 *
299 * @param peer_id peer to search for
300 * @return handle to abort search
301 */
302struct GCD_search_handle *
303GCD_search (const struct GNUNET_PeerIdentity *peer_id)
304{
305 struct GNUNET_HashCode phash;
306 struct GCD_search_handle *h;
307
308 GNUNET_STATISTICS_update (stats,
309 "# DHT search",
310 1,
311 GNUNET_NO);
312 memset (&phash,
313 0,
314 sizeof(phash));
315 GNUNET_memcpy (&phash,
316 peer_id,
317 sizeof(*peer_id));
318
319 h = GNUNET_new (struct GCD_search_handle);
320 h->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
321 GNUNET_BLOCK_TYPE_DHT_HELLO, /* type */
322 &phash, /* key to search */
323 dht_replication_level, /* replication level */
324 GNUNET_DHT_RO_RECORD_ROUTE
325 | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
326 NULL, /* xquery */
327 0, /* xquery bits */
328 &dht_get_id_handler,
329 h);
330 LOG (GNUNET_ERROR_TYPE_DEBUG,
331 "Starting DHT GET for peer %s (%p)\n",
332 GNUNET_i2s (peer_id),
333 h);
334 return h;
335}
336
337
338/**
339 * Stop DHT search started with #GCD_search().
340 *
341 * @param h handle to search to stop
342 */
343void
344GCD_search_stop (struct GCD_search_handle *h)
345{
346 LOG (GNUNET_ERROR_TYPE_DEBUG,
347 "Stopping DHT GET %p\n",
348 h);
349 GNUNET_DHT_get_stop (h->dhtget);
350 GNUNET_free (h);
351}
352
353
354/* end of gnunet-service-cadet_dht.c */