2 This file is part of GNUnet
3 Copyright (C) 2004-2013, 2016 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.
22 * @author Christian Grothoff
25 * API to the DHT service
27 * @defgroup dht DHT service
28 * Distributed Hash Table
30 * @see [Documentation](https://gnunet.org/developer-handbook-dht)
35 #ifndef GNUNET_DHT_SERVICE_H
36 #define GNUNET_DHT_SERVICE_H
38 #include "gnunet_util_lib.h"
39 #include "gnunet_block_lib.h"
40 #include "gnunet_hello_lib.h"
45 #if 0 /* keep Emacsens' auto-indent happy */
52 * Default republication frequency for stored data in the DHT.
54 #define GNUNET_DHT_DEFAULT_REPUBLISH_FREQUENCY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 60)
59 * Connection to the DHT service.
61 struct GNUNET_DHT_Handle;
64 * Handle to control a get operation.
66 struct GNUNET_DHT_GetHandle;
69 * Handle to control a find peer operation.
71 struct GNUNET_DHT_FindPeerHandle;
75 * Options for routing.
77 enum GNUNET_DHT_RouteOption
80 * Default. Do nothing special.
82 GNUNET_DHT_RO_NONE = 0,
85 * Each peer along the way should look at 'enc' (otherwise
86 * only the k-peers closest to the key should look at it).
88 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE = 1,
91 * We should keep track of the route that the message
92 * took in the P2P network.
94 GNUNET_DHT_RO_RECORD_ROUTE = 2,
97 * This is a 'FIND-PEER' request, so approximate results are fine.
99 GNUNET_DHT_RO_FIND_PEER = 4,
102 * Possible message option for query key randomization.
104 GNUNET_DHT_RO_BART = 8,
107 * Flag given to monitors if this was the last hop for a GET/PUT.
109 GNUNET_DHT_RO_LAST_HOP = 16
114 * Initialize the connection with the DHT service.
116 * @param cfg configuration to use
117 * @param ht_len size of the internal hash table to use for
118 * processing multiple GET/FIND requests in parallel
119 * @return NULL on error
121 struct GNUNET_DHT_Handle *
122 GNUNET_DHT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
123 unsigned int ht_len);
127 * Shutdown connection with the DHT service.
129 * @param handle connection to shut down
132 GNUNET_DHT_disconnect (struct GNUNET_DHT_Handle *handle);
135 /* *************** Standard API: get and put ******************* */
139 * Opaque handle to cancel a PUT operation.
141 struct GNUNET_DHT_PutHandle;
145 * Type of a PUT continuation. You must not call
146 * #GNUNET_DHT_disconnect in this continuation.
149 * @param success #GNUNET_OK if the PUT was transmitted,
150 * #GNUNET_NO on timeout,
151 * #GNUNET_SYSERR on disconnect from service
152 * after the PUT message was transmitted
153 * (so we don't know if it was received or not)
156 (*GNUNET_DHT_PutContinuation)(void *cls,
161 * Perform a PUT operation storing data in the DHT.
163 * @param handle handle to DHT service
164 * @param key the key to store under
165 * @param desired_replication_level estimate of how many
166 * nearest peers this request should reach
167 * @param options routing options for this message
168 * @param type type of the value
169 * @param size number of bytes in @a data; must be less than 64k
170 * @param data the data to store
171 * @param exp desired expiration time for the value
172 * @param cont continuation to call when done (transmitting request to service)
173 * You must not call #GNUNET_DHT_disconnect in this continuation
174 * @param cont_cls closure for @a cont
175 * @return handle to cancel the "PUT" operation, NULL on error
178 struct GNUNET_DHT_PutHandle *
179 GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle,
180 const struct GNUNET_HashCode *key,
181 uint32_t desired_replication_level,
182 enum GNUNET_DHT_RouteOption options,
183 enum GNUNET_BLOCK_Type type,
186 struct GNUNET_TIME_Absolute exp,
187 GNUNET_DHT_PutContinuation cont,
192 * Cancels a DHT PUT operation. Note that the PUT request may still
193 * go out over the network (we can't stop that); However, if the PUT
194 * has not yet been sent to the service, cancelling the PUT will stop
195 * this from happening (but there is no way for the user of this API
196 * to tell if that is the case). The only use for this API is to
197 * prevent a later call to 'cont' from #GNUNET_DHT_put (i.e. because
198 * the system is shutting down).
200 * @param ph put operation to cancel ('cont' will no longer be called)
203 GNUNET_DHT_put_cancel (struct GNUNET_DHT_PutHandle *ph);
207 * Iterator called on each result obtained for a DHT
208 * operation that expects a reply
211 * @param exp when will this value expire
212 * @param key key of the result
213 * @param get_path peers on reply path (or NULL if not recorded)
214 * [0] = datastore's first neighbor, [length - 1] = local peer
215 * @param get_path_length number of entries in @a get_path
216 * @param put_path peers on the PUT path (or NULL if not recorded)
217 * [0] = origin, [length - 1] = datastore
218 * @param put_path_length number of entries in @a put_path
219 * @param type type of the result
220 * @param size number of bytes in @a data
221 * @param data pointer to the result data
224 (*GNUNET_DHT_GetIterator) (void *cls,
225 struct GNUNET_TIME_Absolute exp,
226 const struct GNUNET_HashCode *key,
227 const struct GNUNET_PeerIdentity *get_path,
228 unsigned int get_path_length,
229 const struct GNUNET_PeerIdentity *put_path,
230 unsigned int put_path_length,
231 enum GNUNET_BLOCK_Type type,
237 * Perform an asynchronous GET operation on the DHT identified. See
238 * also #GNUNET_BLOCK_evaluate.
240 * @param handle handle to the DHT service
241 * @param type expected type of the response object
242 * @param key the key to look up
243 * @param desired_replication_level estimate of how many
244 nearest peers this request should reach
245 * @param options routing options for this message
246 * @param xquery extended query data (can be NULL, depending on type)
247 * @param xquery_size number of bytes in @a xquery
248 * @param iter function to call on each result
249 * @param iter_cls closure for @a iter
250 * @return handle to stop the async get
252 struct GNUNET_DHT_GetHandle *
253 GNUNET_DHT_get_start (struct GNUNET_DHT_Handle *handle,
254 enum GNUNET_BLOCK_Type type,
255 const struct GNUNET_HashCode *key,
256 uint32_t desired_replication_level,
257 enum GNUNET_DHT_RouteOption options,
260 GNUNET_DHT_GetIterator iter,
265 * Tell the DHT not to return any of the following known results
268 * @param get_handle get operation for which results should be filtered
269 * @param num_results number of results to be blocked that are
270 * provided in this call (size of the @a results array)
271 * @param results array of hash codes over the `data` of the results
275 GNUNET_DHT_get_filter_known_results (struct GNUNET_DHT_GetHandle *get_handle,
276 unsigned int num_results,
277 const struct GNUNET_HashCode *results);
280 * Stop async DHT-get. Frees associated resources.
282 * @param get_handle GET operation to stop.
285 GNUNET_DHT_get_stop (struct GNUNET_DHT_GetHandle *get_handle);
288 /* *************** Extended API: monitor ******************* */
291 * Handle to monitor requests
293 struct GNUNET_DHT_MonitorHandle;
296 * Callback called on each GET request going through the DHT.
298 * @param cls Closure.
299 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
300 * @param type The type of data in the request.
301 * @param hop_count Hop count so far.
302 * @param path_length number of entries in @a path (or 0 if not recorded).
303 * @param path peers on the GET path (or NULL if not recorded).
304 * @param desired_replication_level Desired replication level.
305 * @param key Key of the requested data.
308 (*GNUNET_DHT_MonitorGetCB) (void *cls,
309 enum GNUNET_DHT_RouteOption options,
310 enum GNUNET_BLOCK_Type type,
312 uint32_t desired_replication_level,
313 unsigned int path_length,
314 const struct GNUNET_PeerIdentity *path,
315 const struct GNUNET_HashCode *key);
319 * Callback called on each GET reply going through the DHT.
321 * @param cls Closure.
322 * @param type The type of data in the result.
323 * @param get_path Peers on GET path (or NULL if not recorded).
324 * @param get_path_length number of entries in @a get_path.
325 * @param put_path peers on the PUT path (or NULL if not recorded).
326 * @param put_path_length number of entries in @a get_path.
327 * @param exp Expiration time of the data.
328 * @param key Key of the data.
329 * @param data Pointer to the result data.
330 * @param size Number of bytes in @a data.
333 (*GNUNET_DHT_MonitorGetRespCB) (void *cls,
334 enum GNUNET_BLOCK_Type type,
335 const struct GNUNET_PeerIdentity *get_path,
336 unsigned int get_path_length,
337 const struct GNUNET_PeerIdentity *put_path,
338 unsigned int put_path_length,
339 struct GNUNET_TIME_Absolute exp,
340 const struct GNUNET_HashCode *key,
346 * Callback called on each PUT request going through the DHT.
348 * @param cls Closure.
349 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
350 * @param type The type of data in the request.
351 * @param hop_count Hop count so far.
352 * @param path_length number of entries in @a path (or 0 if not recorded).
353 * @param path peers on the PUT path (or NULL if not recorded).
354 * @param desired_replication_level Desired replication level.
355 * @param exp Expiration time of the data.
356 * @param key Key under which data is to be stored.
357 * @param data Pointer to the data carried.
358 * @param size Number of bytes in @a data.
361 (*GNUNET_DHT_MonitorPutCB) (void *cls,
362 enum GNUNET_DHT_RouteOption options,
363 enum GNUNET_BLOCK_Type type,
365 uint32_t desired_replication_level,
366 unsigned int path_length,
367 const struct GNUNET_PeerIdentity *path,
368 struct GNUNET_TIME_Absolute exp,
369 const struct GNUNET_HashCode *key,
376 * Start monitoring the local DHT service.
378 * @param handle Handle to the DHT service.
379 * @param type Type of blocks that are of interest.
380 * @param key Key of data of interest, NULL for all.
381 * @param get_cb Callback to process monitored get messages.
382 * @param get_resp_cb Callback to process monitored get response messages.
383 * @param put_cb Callback to process monitored put messages.
384 * @param cb_cls Closure for callbacks
385 * @return Handle to stop monitoring.
387 struct GNUNET_DHT_MonitorHandle *
388 GNUNET_DHT_monitor_start (struct GNUNET_DHT_Handle *handle,
389 enum GNUNET_BLOCK_Type type,
390 const struct GNUNET_HashCode *key,
391 GNUNET_DHT_MonitorGetCB get_cb,
392 GNUNET_DHT_MonitorGetRespCB get_resp_cb,
393 GNUNET_DHT_MonitorPutCB put_cb,
399 * On return handle will no longer be valid, caller must not use it anymore.
401 * @param handle The handle to the monitor request returned by
402 * #GNUNET_DHT_monitor_start().
405 GNUNET_DHT_monitor_stop (struct GNUNET_DHT_MonitorHandle *handle);
408 #if 0 /* keep Emacsens' auto-indent happy */
417 /** @} */ /* end of group dht */