2 This file is part of GNUnet
3 (C) 2004, 2005, 2006, 2008, 2009, 2011 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
27 #ifndef GNUNET_DHT_SERVICE_H
28 #define GNUNET_DHT_SERVICE_H
30 #include "gnunet_util_lib.h"
31 #include "gnunet_block_lib.h"
32 #include "gnunet_hello_lib.h"
37 #if 0 /* keep Emacsens' auto-indent happy */
44 * Default republication frequency for stored data in the DHT.
46 #define GNUNET_DHT_DEFAULT_REPUBLISH_FREQUENCY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 60)
51 * Connection to the DHT service.
53 struct GNUNET_DHT_Handle;
56 * Handle to control a get operation.
58 struct GNUNET_DHT_GetHandle;
61 * Handle to control a find peer operation.
63 struct GNUNET_DHT_FindPeerHandle;
67 * Options for routing.
69 enum GNUNET_DHT_RouteOption
72 * Default. Do nothing special.
74 GNUNET_DHT_RO_NONE = 0,
77 * Each peer along the way should look at 'enc' (otherwise
78 * only the k-peers closest to the key should look at it).
80 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE = 1,
83 * We should keep track of the route that the message
84 * took in the P2P network.
86 GNUNET_DHT_RO_RECORD_ROUTE = 2,
89 * This is a 'FIND-PEER' request, so approximate results are fine.
91 GNUNET_DHT_RO_FIND_PEER = 4,
94 * Possible message option for query key randomization.
96 GNUNET_DHT_RO_BART = 8
101 * Initialize the connection with the DHT service.
103 * @param cfg configuration to use
104 * @param ht_len size of the internal hash table to use for
105 * processing multiple GET/FIND requests in parallel
106 * @return NULL on error
108 struct GNUNET_DHT_Handle *
109 GNUNET_DHT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
110 unsigned int ht_len);
114 * Shutdown connection with the DHT service.
116 * @param handle connection to shut down
119 GNUNET_DHT_disconnect (struct GNUNET_DHT_Handle *handle);
122 /* *************** Standard API: get and put ******************* */
126 * Opaque handle to cancel a PUT operation.
128 struct GNUNET_DHT_PutHandle;
132 * Type of a PUT continuation. You must not call
133 * "GNUNET_DHT_disconnect" in this continuation.
136 * @param success GNUNET_OK if the PUT was transmitted,
137 * GNUNET_NO on timeout,
138 * GNUNET_SYSERR on disconnect from service
139 * after the PUT message was transmitted
140 * (so we don't know if it was received or not)
142 typedef void (*GNUNET_DHT_PutContinuation)(void *cls,
147 * Perform a PUT operation storing data in the DHT.
149 * @param handle handle to DHT service
150 * @param key the key to store under
151 * @param desired_replication_level estimate of how many
152 * nearest peers this request should reach
153 * @param options routing options for this message
154 * @param type type of the value
155 * @param size number of bytes in data; must be less than 64k
156 * @param data the data to store
157 * @param exp desired expiration time for the value
158 * @param timeout how long to wait for transmission of this request
159 * @param cont continuation to call when done (transmitting request to service)
160 * You must not call "GNUNET_DHT_disconnect" in this continuation
161 * @param cont_cls closure for cont
162 * @return handle to cancel the "PUT" operation, NULL on error
165 struct GNUNET_DHT_PutHandle *
166 GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle, const GNUNET_HashCode * key,
167 uint32_t desired_replication_level,
168 enum GNUNET_DHT_RouteOption options,
169 enum GNUNET_BLOCK_Type type, size_t size, const char *data,
170 struct GNUNET_TIME_Absolute exp,
171 struct GNUNET_TIME_Relative timeout,
172 GNUNET_DHT_PutContinuation cont,
177 * Cancels a DHT PUT operation. Note that the PUT request may still
178 * go out over the network (we can't stop that); However, if the PUT
179 * has not yet been sent to the service, cancelling the PUT will stop
180 * this from happening (but there is no way for the user of this API
181 * to tell if that is the case). The only use for this API is to
182 * prevent a later call to 'cont' from "GNUNET_DHT_put" (i.e. because
183 * the system is shutting down).
185 * @param ph put operation to cancel ('cont' will no longer be called)
188 GNUNET_DHT_put_cancel (struct GNUNET_DHT_PutHandle *ph);
192 * Iterator called on each result obtained for a DHT
193 * operation that expects a reply
196 * @param exp when will this value expire
197 * @param key key of the result
198 * @param get_path peers on reply path (or NULL if not recorded)
199 * @param get_path_length number of entries in get_path
200 * @param put_path peers on the PUT path (or NULL if not recorded)
201 * @param put_path_length number of entries in get_path
202 * @param type type of the result
203 * @param size number of bytes in data
204 * @param data pointer to the result data
206 typedef void (*GNUNET_DHT_GetIterator) (void *cls,
207 struct GNUNET_TIME_Absolute exp,
208 const GNUNET_HashCode * key,
209 const struct GNUNET_PeerIdentity *
210 get_path, unsigned int get_path_length,
211 const struct GNUNET_PeerIdentity *
212 put_path, unsigned int put_path_length,
213 enum GNUNET_BLOCK_Type type,
214 size_t size, const void *data);
219 * Perform an asynchronous GET operation on the DHT identified. See
220 * also "GNUNET_BLOCK_evaluate".
222 * @param handle handle to the DHT service
223 * @param timeout how long to wait for transmission of this request to the service
224 * FIXME: this argument should probably be removed in the medium term...
225 * @param type expected type of the response object
226 * @param key the key to look up
227 * @param desired_replication_level estimate of how many
228 nearest peers this request should reach
229 * @param options routing options for this message
230 * @param xquery extended query data (can be NULL, depending on type)
231 * @param xquery_size number of bytes in xquery
232 * @param iter function to call on each result
233 * @param iter_cls closure for iter
235 * @return handle to stop the async get
237 struct GNUNET_DHT_GetHandle *
238 GNUNET_DHT_get_start (struct GNUNET_DHT_Handle *handle,
239 struct GNUNET_TIME_Relative timeout,
240 enum GNUNET_BLOCK_Type type, const GNUNET_HashCode * key,
241 uint32_t desired_replication_level,
242 enum GNUNET_DHT_RouteOption options, const void *xquery,
243 size_t xquery_size, GNUNET_DHT_GetIterator iter,
248 * Stop async DHT-get. Frees associated resources.
250 * @param get_handle GET operation to stop.
252 * On return get_handle will no longer be valid, caller
253 * must not use again!!!
256 GNUNET_DHT_get_stop (struct GNUNET_DHT_GetHandle *get_handle);
259 /* *************** Extended API: monitor ******************* */
262 * Handle to monitor requests
264 struct GNUNET_DHT_MonitorHandle;
267 * Callback called on each GET request going through the DHT.
269 * @param cls Closure.
270 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
271 * @param type The type of data in the request.
272 * @param hop_count Hop count so far.
273 * @param path_length number of entries in path (or 0 if not recorded).
274 * @param path peers on the GET path (or NULL if not recorded).
275 * @param desired_replication_level Desired replication level.
276 * @param key Key of the requested data.
278 typedef void (*GNUNET_DHT_MonitorGetCB) (void *cls,
279 enum GNUNET_DHT_RouteOption options,
280 enum GNUNET_BLOCK_Type type,
282 uint32_t desired_replication_level,
283 unsigned int path_length,
284 const struct GNUNET_PeerIdentity *path,
285 const GNUNET_HashCode * key);
288 * Callback called on each GET reply going through the DHT.
290 * @param cls Closure.
291 * @param type The type of data in the result.
292 * @param get_path Peers on GET path (or NULL if not recorded).
293 * @param get_path_length number of entries in get_path.
294 * @param put_path peers on the PUT path (or NULL if not recorded).
295 * @param put_path_length number of entries in get_path.
296 * @param exp Expiration time of the data.
297 * @param key Key of the data.
298 * @param data Pointer to the result data.
299 * @param size Number of bytes in data.
301 typedef void (*GNUNET_DHT_MonitorGetRespCB) (void *cls,
302 enum GNUNET_BLOCK_Type type,
303 const struct GNUNET_PeerIdentity
305 unsigned int get_path_length,
306 const struct GNUNET_PeerIdentity
308 unsigned int put_path_length,
309 struct GNUNET_TIME_Absolute exp,
310 const GNUNET_HashCode * key,
315 * Callback called on each PUT request going through the DHT.
317 * @param cls Closure.
318 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
319 * @param type The type of data in the request.
320 * @param hop_count Hop count so far.
321 * @param path_length number of entries in path (or 0 if not recorded).
322 * @param path peers on the PUT path (or NULL if not recorded).
323 * @param desired_replication_level Desired replication level.
324 * @param exp Expiration time of the data.
325 * @param key Key under which data is to be stored.
326 * @param data Pointer to the data carried.
327 * @param size Number of bytes in data.
329 typedef void (*GNUNET_DHT_MonitorPutCB) (void *cls,
330 enum GNUNET_DHT_RouteOption options,
331 enum GNUNET_BLOCK_Type type,
333 uint32_t desired_replication_level,
334 unsigned int path_length,
335 const struct GNUNET_PeerIdentity *path,
336 struct GNUNET_TIME_Absolute exp,
337 const GNUNET_HashCode * key,
342 * Start monitoring the local DHT service.
344 * @param handle Handle to the DHT service.
345 * @param type Type of blocks that are of interest.
346 * @param key Key of data of interest, NULL for all.
347 * @param get_cb Callback to process monitored get messages.
348 * @param get_resp_cb Callback to process monitored get response messages.
349 * @param put_cb Callback to process monitored put messages.
350 * @param cb_cls Closure for cb.
352 * @return Handle to stop monitoring.
354 struct GNUNET_DHT_MonitorHandle *
355 GNUNET_DHT_monitor_start (struct GNUNET_DHT_Handle *handle,
356 enum GNUNET_BLOCK_Type type,
357 const GNUNET_HashCode *key,
358 GNUNET_DHT_MonitorGetCB get_cb,
359 GNUNET_DHT_MonitorGetRespCB get_resp_cb,
360 GNUNET_DHT_MonitorPutCB put_cb,
367 * @param handle The handle to the monitor request returned by monitor_start.
369 * On return handle will no longer be valid, caller must not use again!!!
372 GNUNET_DHT_monitor_stop (struct GNUNET_DHT_MonitorHandle *handle);
375 #if 0 /* keep Emacsens' auto-indent happy */
384 /* gnunet_dht_service.h */