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 it
6 under the terms of the GNU 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.
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.
17 * @author Christian Grothoff
20 * API to the DHT service
22 * @defgroup dht DHT service
23 * Distributed Hash Table
25 * @see [Documentation](https://gnunet.org/developer-handbook-dht)
30 #ifndef GNUNET_DHT_SERVICE_H
31 #define GNUNET_DHT_SERVICE_H
33 #include "gnunet_util_lib.h"
34 #include "gnunet_block_lib.h"
35 #include "gnunet_hello_lib.h"
40 #if 0 /* keep Emacsens' auto-indent happy */
47 * Default republication frequency for stored data in the DHT.
49 #define GNUNET_DHT_DEFAULT_REPUBLISH_FREQUENCY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 60)
54 * Connection to the DHT service.
56 struct GNUNET_DHT_Handle;
59 * Handle to control a get operation.
61 struct GNUNET_DHT_GetHandle;
64 * Handle to control a find peer operation.
66 struct GNUNET_DHT_FindPeerHandle;
70 * Options for routing.
72 enum GNUNET_DHT_RouteOption
75 * Default. Do nothing special.
77 GNUNET_DHT_RO_NONE = 0,
80 * Each peer along the way should look at 'enc' (otherwise
81 * only the k-peers closest to the key should look at it).
83 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE = 1,
86 * We should keep track of the route that the message
87 * took in the P2P network.
89 GNUNET_DHT_RO_RECORD_ROUTE = 2,
92 * This is a 'FIND-PEER' request, so approximate results are fine.
94 GNUNET_DHT_RO_FIND_PEER = 4,
97 * Possible message option for query key randomization.
99 GNUNET_DHT_RO_BART = 8,
102 * Flag given to monitors if this was the last hop for a GET/PUT.
104 GNUNET_DHT_RO_LAST_HOP = 16
109 * Initialize the connection with the DHT service.
111 * @param cfg configuration to use
112 * @param ht_len size of the internal hash table to use for
113 * processing multiple GET/FIND requests in parallel
114 * @return NULL on error
116 struct GNUNET_DHT_Handle *
117 GNUNET_DHT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
118 unsigned int ht_len);
122 * Shutdown connection with the DHT service.
124 * @param handle connection to shut down
127 GNUNET_DHT_disconnect (struct GNUNET_DHT_Handle *handle);
130 /* *************** Standard API: get and put ******************* */
134 * Opaque handle to cancel a PUT operation.
136 struct GNUNET_DHT_PutHandle;
140 * Perform a PUT operation storing data in the DHT.
142 * @param handle handle to DHT service
143 * @param key the key to store under
144 * @param desired_replication_level estimate of how many
145 * nearest peers this request should reach
146 * @param options routing options for this message
147 * @param type type of the value
148 * @param size number of bytes in @a data; must be less than 64k
149 * @param data the data to store
150 * @param exp desired expiration time for the value
151 * @param cont continuation to call when done (transmitting request to service)
152 * You must not call #GNUNET_DHT_disconnect in this continuation
153 * @param cont_cls closure for @a cont
154 * @return handle to cancel the "PUT" operation, NULL on error
157 struct GNUNET_DHT_PutHandle *
158 GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle,
159 const struct GNUNET_HashCode *key,
160 uint32_t desired_replication_level,
161 enum GNUNET_DHT_RouteOption options,
162 enum GNUNET_BLOCK_Type type,
165 struct GNUNET_TIME_Absolute exp,
166 GNUNET_SCHEDULER_TaskCallback cont,
171 * Cancels a DHT PUT operation. Note that the PUT request may still
172 * go out over the network (we can't stop that); However, if the PUT
173 * has not yet been sent to the service, cancelling the PUT will stop
174 * this from happening (but there is no way for the user of this API
175 * to tell if that is the case). The only use for this API is to
176 * prevent a later call to 'cont' from #GNUNET_DHT_put (i.e. because
177 * the system is shutting down).
179 * @param ph put operation to cancel ('cont' will no longer be called)
182 GNUNET_DHT_put_cancel (struct GNUNET_DHT_PutHandle *ph);
186 * Iterator called on each result obtained for a DHT
187 * operation that expects a reply
190 * @param exp when will this value expire
191 * @param key key of the result
192 * @param get_path peers on reply path (or NULL if not recorded)
193 * [0] = datastore's first neighbor, [length - 1] = local peer
194 * @param get_path_length number of entries in @a get_path
195 * @param put_path peers on the PUT path (or NULL if not recorded)
196 * [0] = origin, [length - 1] = datastore
197 * @param put_path_length number of entries in @a put_path
198 * @param type type of the result
199 * @param size number of bytes in @a data
200 * @param data pointer to the result data
203 (*GNUNET_DHT_GetIterator) (void *cls,
204 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,
210 enum GNUNET_BLOCK_Type type,
216 * Perform an asynchronous GET operation on the DHT identified. See
217 * also #GNUNET_BLOCK_evaluate.
219 * @param handle handle to the DHT service
220 * @param type expected type of the response object
221 * @param key the key to look up
222 * @param desired_replication_level estimate of how many
223 nearest peers this request should reach
224 * @param options routing options for this message
225 * @param xquery extended query data (can be NULL, depending on type)
226 * @param xquery_size number of bytes in @a xquery
227 * @param iter function to call on each result
228 * @param iter_cls closure for @a iter
229 * @return handle to stop the async get
231 struct GNUNET_DHT_GetHandle *
232 GNUNET_DHT_get_start (struct GNUNET_DHT_Handle *handle,
233 enum GNUNET_BLOCK_Type type,
234 const struct GNUNET_HashCode *key,
235 uint32_t desired_replication_level,
236 enum GNUNET_DHT_RouteOption options,
239 GNUNET_DHT_GetIterator iter,
244 * Tell the DHT not to return any of the following known results
247 * @param get_handle get operation for which results should be filtered
248 * @param num_results number of results to be blocked that are
249 * provided in this call (size of the @a results array)
250 * @param results array of hash codes over the `data` of the results
254 GNUNET_DHT_get_filter_known_results (struct GNUNET_DHT_GetHandle *get_handle,
255 unsigned int num_results,
256 const struct GNUNET_HashCode *results);
259 * Stop async DHT-get. Frees associated resources.
261 * @param get_handle GET operation to stop.
264 GNUNET_DHT_get_stop (struct GNUNET_DHT_GetHandle *get_handle);
267 /* *************** Extended API: monitor ******************* */
270 * Handle to monitor requests
272 struct GNUNET_DHT_MonitorHandle;
275 * Callback called on each GET request going through the DHT.
277 * @param cls Closure.
278 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
279 * @param type The type of data in the request.
280 * @param hop_count Hop count so far.
281 * @param path_length number of entries in @a path (or 0 if not recorded).
282 * @param path peers on the GET path (or NULL if not recorded).
283 * @param desired_replication_level Desired replication level.
284 * @param key Key of the requested data.
287 (*GNUNET_DHT_MonitorGetCB) (void *cls,
288 enum GNUNET_DHT_RouteOption options,
289 enum GNUNET_BLOCK_Type type,
291 uint32_t desired_replication_level,
292 unsigned int path_length,
293 const struct GNUNET_PeerIdentity *path,
294 const struct GNUNET_HashCode *key);
298 * Callback called on each GET reply going through the DHT.
300 * @param cls Closure.
301 * @param type The type of data in the result.
302 * @param get_path Peers on GET path (or NULL if not recorded).
303 * @param get_path_length number of entries in @a get_path.
304 * @param put_path peers on the PUT path (or NULL if not recorded).
305 * @param put_path_length number of entries in @a get_path.
306 * @param exp Expiration time of the data.
307 * @param key Key of the data.
308 * @param data Pointer to the result data.
309 * @param size Number of bytes in @a data.
312 (*GNUNET_DHT_MonitorGetRespCB) (void *cls,
313 enum GNUNET_BLOCK_Type type,
314 const struct GNUNET_PeerIdentity *get_path,
315 unsigned int get_path_length,
316 const struct GNUNET_PeerIdentity *put_path,
317 unsigned int put_path_length,
318 struct GNUNET_TIME_Absolute exp,
319 const struct GNUNET_HashCode *key,
325 * Callback called on each PUT request going through the DHT.
327 * @param cls Closure.
328 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
329 * @param type The type of data in the request.
330 * @param hop_count Hop count so far.
331 * @param path_length number of entries in @a path (or 0 if not recorded).
332 * @param path peers on the PUT path (or NULL if not recorded).
333 * @param desired_replication_level Desired replication level.
334 * @param exp Expiration time of the data.
335 * @param key Key under which data is to be stored.
336 * @param data Pointer to the data carried.
337 * @param size Number of bytes in @a data.
340 (*GNUNET_DHT_MonitorPutCB) (void *cls,
341 enum GNUNET_DHT_RouteOption options,
342 enum GNUNET_BLOCK_Type type,
344 uint32_t desired_replication_level,
345 unsigned int path_length,
346 const struct GNUNET_PeerIdentity *path,
347 struct GNUNET_TIME_Absolute exp,
348 const struct GNUNET_HashCode *key,
355 * Start monitoring the local DHT service.
357 * @param handle Handle to the DHT service.
358 * @param type Type of blocks that are of interest.
359 * @param key Key of data of interest, NULL for all.
360 * @param get_cb Callback to process monitored get messages.
361 * @param get_resp_cb Callback to process monitored get response messages.
362 * @param put_cb Callback to process monitored put messages.
363 * @param cb_cls Closure for callbacks
364 * @return Handle to stop monitoring.
366 struct GNUNET_DHT_MonitorHandle *
367 GNUNET_DHT_monitor_start (struct GNUNET_DHT_Handle *handle,
368 enum GNUNET_BLOCK_Type type,
369 const struct GNUNET_HashCode *key,
370 GNUNET_DHT_MonitorGetCB get_cb,
371 GNUNET_DHT_MonitorGetRespCB get_resp_cb,
372 GNUNET_DHT_MonitorPutCB put_cb,
378 * On return handle will no longer be valid, caller must not use it anymore.
380 * @param handle The handle to the monitor request returned by
381 * #GNUNET_DHT_monitor_start().
384 GNUNET_DHT_monitor_stop (struct GNUNET_DHT_MonitorHandle *handle);
387 #if 0 /* keep Emacsens' auto-indent happy */
396 /** @} */ /* end of group dht */