2 This file is part of GNUnet
3 (C) 2004-2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file include/gnunet_dht_service.h
23 * @brief API to the DHT service
24 * @author Christian Grothoff
25 * @defgroup dht Distributed Hash Table
29 #ifndef GNUNET_DHT_SERVICE_H
30 #define GNUNET_DHT_SERVICE_H
32 #include "gnunet_util_lib.h"
33 #include "gnunet_block_lib.h"
34 #include "gnunet_hello_lib.h"
39 #if 0 /* keep Emacsens' auto-indent happy */
46 * Default republication frequency for stored data in the DHT.
48 #define GNUNET_DHT_DEFAULT_REPUBLISH_FREQUENCY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 60)
53 * Connection to the DHT service.
55 struct GNUNET_DHT_Handle;
58 * Handle to control a get operation.
60 struct GNUNET_DHT_GetHandle;
63 * Handle to control a find peer operation.
65 struct GNUNET_DHT_FindPeerHandle;
69 * Options for routing.
71 enum GNUNET_DHT_RouteOption
74 * Default. Do nothing special.
76 GNUNET_DHT_RO_NONE = 0,
79 * Each peer along the way should look at 'enc' (otherwise
80 * only the k-peers closest to the key should look at it).
82 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE = 1,
85 * We should keep track of the route that the message
86 * took in the P2P network.
88 GNUNET_DHT_RO_RECORD_ROUTE = 2,
91 * This is a 'FIND-PEER' request, so approximate results are fine.
93 GNUNET_DHT_RO_FIND_PEER = 4,
96 * Possible message option for query key randomization.
98 GNUNET_DHT_RO_BART = 8
103 * Initialize the connection with the DHT service.
105 * @param cfg configuration to use
106 * @param ht_len size of the internal hash table to use for
107 * processing multiple GET/FIND requests in parallel
108 * @return NULL on error
110 struct GNUNET_DHT_Handle *
111 GNUNET_DHT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
112 unsigned int ht_len);
116 * Shutdown connection with the DHT service.
118 * @param handle connection to shut down
121 GNUNET_DHT_disconnect (struct GNUNET_DHT_Handle *handle);
124 /* *************** Standard API: get and put ******************* */
128 * Opaque handle to cancel a PUT operation.
130 struct GNUNET_DHT_PutHandle;
134 * Type of a PUT continuation. You must not call
135 * #GNUNET_DHT_disconnect in this continuation.
138 * @param success #GNUNET_OK if the PUT was transmitted,
139 * #GNUNET_NO on timeout,
140 * #GNUNET_SYSERR on disconnect from service
141 * after the PUT message was transmitted
142 * (so we don't know if it was received or not)
144 typedef void (*GNUNET_DHT_PutContinuation)(void *cls,
149 * Perform a PUT operation storing data in the DHT.
151 * @param handle handle to DHT service
152 * @param key the key to store under
153 * @param desired_replication_level estimate of how many
154 * nearest peers this request should reach
155 * @param options routing options for this message
156 * @param type type of the value
157 * @param size number of bytes in @a data; must be less than 64k
158 * @param data the data to store
159 * @param exp desired expiration time for the value
160 * @param timeout how long to wait for transmission of this request
161 * @param cont continuation to call when done (transmitting request to service)
162 * You must not call #GNUNET_DHT_disconnect in this continuation
163 * @param cont_cls closure for @a cont
164 * @return handle to cancel the "PUT" operation, NULL on error
167 struct GNUNET_DHT_PutHandle *
168 GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle,
169 const struct GNUNET_HashCode *key,
170 uint32_t desired_replication_level,
171 enum GNUNET_DHT_RouteOption options,
172 enum GNUNET_BLOCK_Type type,
173 size_t size, const void *data,
174 struct GNUNET_TIME_Absolute exp,
175 struct GNUNET_TIME_Relative timeout,
176 GNUNET_DHT_PutContinuation cont,
181 * Cancels a DHT PUT operation. Note that the PUT request may still
182 * go out over the network (we can't stop that); However, if the PUT
183 * has not yet been sent to the service, cancelling the PUT will stop
184 * this from happening (but there is no way for the user of this API
185 * to tell if that is the case). The only use for this API is to
186 * prevent a later call to 'cont' from #GNUNET_DHT_put (i.e. because
187 * the system is shutting down).
189 * @param ph put operation to cancel ('cont' will no longer be called)
192 GNUNET_DHT_put_cancel (struct GNUNET_DHT_PutHandle *ph);
196 * Iterator called on each result obtained for a DHT
197 * operation that expects a reply
200 * @param exp when will this value expire
201 * @param key key of the result
202 * @param get_path peers on reply path (or NULL if not recorded)
203 * [0] = datastore's first neighbor, [length - 1] = local peer
204 * @param get_path_length number of entries in @a get_path
205 * @param put_path peers on the PUT path (or NULL if not recorded)
206 * [0] = origin, [length - 1] = datastore
207 * @param put_path_length number of entries in @a put_path
208 * @param type type of the result
209 * @param size number of bytes in @a data
210 * @param data pointer to the result data
212 typedef void (*GNUNET_DHT_GetIterator) (void *cls,
213 struct GNUNET_TIME_Absolute exp,
214 const struct GNUNET_HashCode *key,
215 const struct GNUNET_PeerIdentity *get_path,
216 unsigned int get_path_length,
217 const struct GNUNET_PeerIdentity *put_path,
218 unsigned int put_path_length,
219 enum GNUNET_BLOCK_Type type,
220 size_t size, const void *data);
224 * Perform an asynchronous GET operation on the DHT identified. See
225 * also #GNUNET_BLOCK_evaluate.
227 * @param handle handle to the DHT service
228 * @param type expected type of the response object
229 * @param key the key to look up
230 * @param desired_replication_level estimate of how many
231 nearest peers this request should reach
232 * @param options routing options for this message
233 * @param xquery extended query data (can be NULL, depending on type)
234 * @param xquery_size number of bytes in @a xquery
235 * @param iter function to call on each result
236 * @param iter_cls closure for @a iter
238 * @return handle to stop the async get
240 struct GNUNET_DHT_GetHandle *
241 GNUNET_DHT_get_start (struct GNUNET_DHT_Handle *handle,
242 enum GNUNET_BLOCK_Type type,
243 const struct GNUNET_HashCode *key,
244 uint32_t desired_replication_level,
245 enum GNUNET_DHT_RouteOption options,
246 const void *xquery, size_t xquery_size,
247 GNUNET_DHT_GetIterator iter, void *iter_cls);
251 * Tell the DHT not to return any of the following known results
254 * @param get_handle get operation for which results should be filtered
255 * @param num_results number of results to be blocked that are
256 * provided in this call (size of the @a results array)
257 * @param results array of hash codes over the 'data' of the results
261 GNUNET_DHT_get_filter_known_results (struct GNUNET_DHT_GetHandle *get_handle,
262 unsigned int num_results,
263 const struct GNUNET_HashCode *results);
266 * Stop async DHT-get. Frees associated resources.
268 * @param get_handle GET operation to stop.
270 * On return get_handle will no longer be valid, caller
271 * must not use again!!!
274 GNUNET_DHT_get_stop (struct GNUNET_DHT_GetHandle *get_handle);
277 /* *************** Extended API: monitor ******************* */
280 * Handle to monitor requests
282 struct GNUNET_DHT_MonitorHandle;
285 * Callback called on each GET request going through the DHT.
287 * @param cls Closure.
288 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
289 * @param type The type of data in the request.
290 * @param hop_count Hop count so far.
291 * @param path_length number of entries in @a path (or 0 if not recorded).
292 * @param path peers on the GET path (or NULL if not recorded).
293 * @param desired_replication_level Desired replication level.
294 * @param key Key of the requested data.
296 typedef void (*GNUNET_DHT_MonitorGetCB) (void *cls,
297 enum GNUNET_DHT_RouteOption options,
298 enum GNUNET_BLOCK_Type type,
300 uint32_t desired_replication_level,
301 unsigned int path_length,
302 const struct GNUNET_PeerIdentity *path,
303 const struct GNUNET_HashCode * key);
306 * Callback called on each GET reply going through the DHT.
308 * @param cls Closure.
309 * @param type The type of data in the result.
310 * @param get_path Peers on GET path (or NULL if not recorded).
311 * @param get_path_length number of entries in @a get_path.
312 * @param put_path peers on the PUT path (or NULL if not recorded).
313 * @param put_path_length number of entries in @a get_path.
314 * @param exp Expiration time of the data.
315 * @param key Key of the data.
316 * @param data Pointer to the result data.
317 * @param size Number of bytes in @a data.
319 typedef void (*GNUNET_DHT_MonitorGetRespCB) (void *cls,
320 enum GNUNET_BLOCK_Type type,
321 const struct GNUNET_PeerIdentity *get_path,
322 unsigned int get_path_length,
323 const struct GNUNET_PeerIdentity *put_path,
324 unsigned int put_path_length,
325 struct GNUNET_TIME_Absolute exp,
326 const struct GNUNET_HashCode *key,
331 * Callback called on each PUT request going through the DHT.
333 * @param cls Closure.
334 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
335 * @param type The type of data in the request.
336 * @param hop_count Hop count so far.
337 * @param path_length number of entries in @a path (or 0 if not recorded).
338 * @param path peers on the PUT path (or NULL if not recorded).
339 * @param desired_replication_level Desired replication level.
340 * @param exp Expiration time of the data.
341 * @param key Key under which data is to be stored.
342 * @param data Pointer to the data carried.
343 * @param size Number of bytes in data.
345 typedef void (*GNUNET_DHT_MonitorPutCB) (void *cls,
346 enum GNUNET_DHT_RouteOption options,
347 enum GNUNET_BLOCK_Type type,
349 uint32_t desired_replication_level,
350 unsigned int path_length,
351 const struct GNUNET_PeerIdentity *path,
352 struct GNUNET_TIME_Absolute exp,
353 const struct GNUNET_HashCode *key,
358 * Start monitoring the local DHT service.
360 * @param handle Handle to the DHT service.
361 * @param type Type of blocks that are of interest.
362 * @param key Key of data of interest, NULL for all.
363 * @param get_cb Callback to process monitored get messages.
364 * @param get_resp_cb Callback to process monitored get response messages.
365 * @param put_cb Callback to process monitored put messages.
366 * @param cb_cls Closure for callbacks
367 * @return Handle to stop monitoring.
369 struct GNUNET_DHT_MonitorHandle *
370 GNUNET_DHT_monitor_start (struct GNUNET_DHT_Handle *handle,
371 enum GNUNET_BLOCK_Type type,
372 const struct GNUNET_HashCode *key,
373 GNUNET_DHT_MonitorGetCB get_cb,
374 GNUNET_DHT_MonitorGetRespCB get_resp_cb,
375 GNUNET_DHT_MonitorPutCB put_cb,
381 * On return handle will no longer be valid, caller must not use again!!!
383 * @param handle The handle to the monitor request returned by monitor_start.
386 GNUNET_DHT_monitor_stop (struct GNUNET_DHT_MonitorHandle *handle);
389 #if 0 /* keep Emacsens' auto-indent happy */
396 /** @} */ /* end of group dht */
400 /* gnunet_dht_service.h */