2 This file is part of GNUnet
3 Copyright (C) 2004-2013 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)
155 typedef void (*GNUNET_DHT_PutContinuation)(void *cls,
160 * Perform a PUT operation storing data in the DHT.
162 * @param handle handle to DHT service
163 * @param key the key to store under
164 * @param desired_replication_level estimate of how many
165 * nearest peers this request should reach
166 * @param options routing options for this message
167 * @param type type of the value
168 * @param size number of bytes in @a data; must be less than 64k
169 * @param data the data to store
170 * @param exp desired expiration time for the value
171 * @param timeout how long to wait for transmission of this request
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,
184 size_t size, const void *data,
185 struct GNUNET_TIME_Absolute exp,
186 struct GNUNET_TIME_Relative timeout,
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
223 typedef void (*GNUNET_DHT_GetIterator) (void *cls,
224 struct GNUNET_TIME_Absolute exp,
225 const struct GNUNET_HashCode *key,
226 const struct GNUNET_PeerIdentity *get_path,
227 unsigned int get_path_length,
228 const struct GNUNET_PeerIdentity *put_path,
229 unsigned int put_path_length,
230 enum GNUNET_BLOCK_Type type,
231 size_t size, const void *data);
235 * Perform an asynchronous GET operation on the DHT identified. See
236 * also #GNUNET_BLOCK_evaluate.
238 * @param handle handle to the DHT service
239 * @param type expected type of the response object
240 * @param key the key to look up
241 * @param desired_replication_level estimate of how many
242 nearest peers this request should reach
243 * @param options routing options for this message
244 * @param xquery extended query data (can be NULL, depending on type)
245 * @param xquery_size number of bytes in @a xquery
246 * @param iter function to call on each result
247 * @param iter_cls closure for @a iter
249 * @return handle to stop the async get
251 struct GNUNET_DHT_GetHandle *
252 GNUNET_DHT_get_start (struct GNUNET_DHT_Handle *handle,
253 enum GNUNET_BLOCK_Type type,
254 const struct GNUNET_HashCode *key,
255 uint32_t desired_replication_level,
256 enum GNUNET_DHT_RouteOption options,
257 const void *xquery, size_t xquery_size,
258 GNUNET_DHT_GetIterator iter, void *iter_cls);
262 * Tell the DHT not to return any of the following known results
265 * @param get_handle get operation for which results should be filtered
266 * @param num_results number of results to be blocked that are
267 * provided in this call (size of the @a results array)
268 * @param results array of hash codes over the 'data' of the results
272 GNUNET_DHT_get_filter_known_results (struct GNUNET_DHT_GetHandle *get_handle,
273 unsigned int num_results,
274 const struct GNUNET_HashCode *results);
277 * Stop async DHT-get. Frees associated resources.
279 * @param get_handle GET operation to stop.
281 * On return get_handle will no longer be valid, caller
282 * must not use again!!!
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.
307 typedef void (*GNUNET_DHT_MonitorGetCB) (void *cls,
308 enum GNUNET_DHT_RouteOption options,
309 enum GNUNET_BLOCK_Type type,
311 uint32_t desired_replication_level,
312 unsigned int path_length,
313 const struct GNUNET_PeerIdentity *path,
314 const struct GNUNET_HashCode * key);
317 * Callback called on each GET reply going through the DHT.
319 * @param cls Closure.
320 * @param type The type of data in the result.
321 * @param get_path Peers on GET path (or NULL if not recorded).
322 * @param get_path_length number of entries in @a get_path.
323 * @param put_path peers on the PUT path (or NULL if not recorded).
324 * @param put_path_length number of entries in @a get_path.
325 * @param exp Expiration time of the data.
326 * @param key Key of the data.
327 * @param data Pointer to the result data.
328 * @param size Number of bytes in @a data.
330 typedef void (*GNUNET_DHT_MonitorGetRespCB) (void *cls,
331 enum GNUNET_BLOCK_Type type,
332 const struct GNUNET_PeerIdentity *get_path,
333 unsigned int get_path_length,
334 const struct GNUNET_PeerIdentity *put_path,
335 unsigned int put_path_length,
336 struct GNUNET_TIME_Absolute exp,
337 const struct GNUNET_HashCode *key,
342 * Callback called on each PUT request going through the DHT.
344 * @param cls Closure.
345 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
346 * @param type The type of data in the request.
347 * @param hop_count Hop count so far.
348 * @param path_length number of entries in @a path (or 0 if not recorded).
349 * @param path peers on the PUT path (or NULL if not recorded).
350 * @param desired_replication_level Desired replication level.
351 * @param exp Expiration time of the data.
352 * @param key Key under which data is to be stored.
353 * @param data Pointer to the data carried.
354 * @param size Number of bytes in data.
356 typedef void (*GNUNET_DHT_MonitorPutCB) (void *cls,
357 enum GNUNET_DHT_RouteOption options,
358 enum GNUNET_BLOCK_Type type,
360 uint32_t desired_replication_level,
361 unsigned int path_length,
362 const struct GNUNET_PeerIdentity *path,
363 struct GNUNET_TIME_Absolute exp,
364 const struct GNUNET_HashCode *key,
369 * Start monitoring the local DHT service.
371 * @param handle Handle to the DHT service.
372 * @param type Type of blocks that are of interest.
373 * @param key Key of data of interest, NULL for all.
374 * @param get_cb Callback to process monitored get messages.
375 * @param get_resp_cb Callback to process monitored get response messages.
376 * @param put_cb Callback to process monitored put messages.
377 * @param cb_cls Closure for callbacks
378 * @return Handle to stop monitoring.
380 struct GNUNET_DHT_MonitorHandle *
381 GNUNET_DHT_monitor_start (struct GNUNET_DHT_Handle *handle,
382 enum GNUNET_BLOCK_Type type,
383 const struct GNUNET_HashCode *key,
384 GNUNET_DHT_MonitorGetCB get_cb,
385 GNUNET_DHT_MonitorGetRespCB get_resp_cb,
386 GNUNET_DHT_MonitorPutCB put_cb,
392 * On return handle will no longer be valid, caller must not use again!!!
394 * @param handle The handle to the monitor request returned by monitor_start.
397 GNUNET_DHT_monitor_stop (struct GNUNET_DHT_MonitorHandle *handle);
402 * Type of a Malicious continuation. You must not call
403 * #GNUNET_DHT_disconnect in this continuation.
406 * @param success #GNUNET_OK if the set malicious request was transmitted,
407 * #GNUNET_NO on timeout,
408 * #GNUNET_SYSERR on disconnect from service
409 * after the PUT message was transmitted
410 * (so we don't know if it was received or not)
412 typedef void (*GNUNET_DHT_ActMaliciousContinuation)(void *cls,
416 * Turn the DHT service to act malicious
418 * @param handle the DHT handle
419 * @param action 1 to make the service malicious; 0 to make it benign
420 FIXME: perhaps make this an enum of known malicious behaviors?
422 struct GNUNET_DHT_ActMaliciousHandle *
423 GNUNET_DHT_act_malicious (struct GNUNET_DHT_Handle *handle,
425 GNUNET_DHT_PutContinuation cont,
430 #if 0 /* keep Emacsens' auto-indent happy */
439 /** @} */ /* end of group dht */