2 This file is part of GNUnet
3 (C) 2004, 2005, 2006, 2008, 2009 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)
49 * K-value that must be used for the bloom filters in 'GET'
52 #define GNUNET_DHT_GET_BLOOMFILTER_K GNUNET_CONSTANTS_BLOOMFILTER_K
55 * Non-intelligent default DHT GET replication.
56 * Should be chosen by application if anything about
57 * the network is known.
59 #define DEFAULT_GET_REPLICATION 5
62 * Non-intelligent default DHT PUT replication.
63 * Should be chosen by application if anything about
64 * the network is known.
66 #define DEFAULT_PUT_REPLICATION 8
69 * Connection to the DHT service.
71 struct GNUNET_DHT_Handle;
74 * Handle to control a route operation.
76 struct GNUNET_DHT_RouteHandle;
79 * Handle to control a get operation.
81 struct GNUNET_DHT_GetHandle;
84 * Handle to control a find peer operation.
86 struct GNUNET_DHT_FindPeerHandle;
90 * Options for routing.
92 enum GNUNET_DHT_RouteOption
95 * Default. Do nothing special.
97 GNUNET_DHT_RO_NONE = 0,
100 * Each peer along the way should look at 'enc' (otherwise
101 * only the k-peers closest to the key should look at it).
103 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE = 1,
106 * We should keep track of the route that the message
107 * took in the P2P network.
109 GNUNET_DHT_RO_RECORD_ROUTE = 2,
112 * Possible message option for query key randomization.
114 GNUNET_DHT_RO_BART = 4,
117 * This is a 'FIND-PEER' request, so approximate results are fine.
119 GNUNET_DHT_RO_FIND_PEER = 8
124 * Initialize the connection with the DHT service.
126 * @param cfg configuration to use
127 * @param ht_len size of the internal hash table to use for
128 * processing multiple GET/FIND requests in parallel
129 * @return NULL on error
131 struct GNUNET_DHT_Handle *
132 GNUNET_DHT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
133 unsigned int ht_len);
137 * Shutdown connection with the DHT service.
139 * @param handle connection to shut down
142 GNUNET_DHT_disconnect (struct GNUNET_DHT_Handle *handle);
145 /* *************** Standard API: get and put ******************* */
148 * Perform a PUT operation storing data in the DHT.
150 * @param handle handle to DHT service
151 * @param key the key to store under
152 * @param desired_replication_level estimate of how many
153 * nearest peers this request should reach
154 * @param options routing options for this message
155 * @param type type of the value
156 * @param size number of bytes in data; must be less than 64k
157 * @param data the data to store
158 * @param exp desired expiration time for the value
159 * @param timeout how long to wait for transmission of this request
160 * @param cont continuation to call when done (transmitting request to service)
161 * @param cont_cls closure for cont
164 GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle, const GNUNET_HashCode * key,
165 uint32_t desired_replication_level,
166 enum GNUNET_DHT_RouteOption options,
167 enum GNUNET_BLOCK_Type type, size_t size, const char *data,
168 struct GNUNET_TIME_Absolute exp,
169 struct GNUNET_TIME_Relative timeout, GNUNET_SCHEDULER_Task cont,
174 * Iterator called on each result obtained for a DHT
175 * operation that expects a reply
178 * @param exp when will this value expire
179 * @param key key of the result
180 * @param get_path NULL-terminated array of pointers
181 * to the peers on reverse GET path (or NULL if not recorded)
182 * @param put_path NULL-terminated array of pointers
183 * to the peers on the PUT path (or NULL if not recorded)
184 * @param type type of the result
185 * @param size number of bytes in data
186 * @param data pointer to the result data
188 typedef void (*GNUNET_DHT_GetIterator) (void *cls,
189 struct GNUNET_TIME_Absolute exp,
190 const GNUNET_HashCode * key,
191 const struct GNUNET_PeerIdentity *
193 const struct GNUNET_PeerIdentity *
195 enum GNUNET_BLOCK_Type type,
196 size_t size, const void *data);
201 * Perform an asynchronous GET operation on the DHT identified. See
202 * also "GNUNET_BLOCK_evaluate".
204 * @param handle handle to the DHT service
205 * @param timeout how long to wait for transmission of this request to the service
206 * @param type expected type of the response object
207 * @param key the key to look up
208 * @param desired_replication_level estimate of how many
209 nearest peers this request should reach
210 * @param options routing options for this message
211 * @param bf bloom filter associated with query (can be NULL)
212 * @param bf_mutator mutation value for bf
213 * @param xquery extended query data (can be NULL, depending on type)
214 * @param xquery_size number of bytes in xquery
215 * @param iter function to call on each result
216 * @param iter_cls closure for iter
218 * @return handle to stop the async get
220 struct GNUNET_DHT_GetHandle *
221 GNUNET_DHT_get_start (struct GNUNET_DHT_Handle *handle,
222 struct GNUNET_TIME_Relative timeout,
223 enum GNUNET_BLOCK_Type type, const GNUNET_HashCode * key,
224 uint32_t desired_replication_level,
225 enum GNUNET_DHT_RouteOption options,
226 const struct GNUNET_CONTAINER_BloomFilter *bf,
227 int32_t bf_mutator, const void *xquery,
228 size_t xquery_size, GNUNET_DHT_GetIterator iter,
233 * Stop async DHT-get. Frees associated resources.
235 * @param get_handle GET operation to stop.
237 * On return get_handle will no longer be valid, caller
238 * must not use again!!!
241 GNUNET_DHT_get_stop (struct GNUNET_DHT_GetHandle *get_handle);
244 /* ******** Special high-level API for finding peers *********** */
247 * Iterator called on each result obtained from a find peer
251 * @param peer hello of a target (peer near key)
253 typedef void (*GNUNET_DHT_FindPeerProcessor) (void *cls,
254 const struct GNUNET_HELLO_Message
259 * Perform an asynchronous FIND PEER operation on the DHT.
261 * @param handle handle to the DHT service
262 * @param timeout timeout for this request to be sent to the
264 * @param key the key to look up
265 * @param options routing options for this message
266 * @param proc function to call on each result
267 * @param proc_cls closure for proc
268 * @return handle to stop the async get, NULL on error
270 struct GNUNET_DHT_FindPeerHandle *
271 GNUNET_DHT_find_peer_start (struct GNUNET_DHT_Handle *handle,
272 struct GNUNET_TIME_Relative timeout,
273 const GNUNET_HashCode * key,
274 enum GNUNET_DHT_RouteOption options,
275 GNUNET_DHT_FindPeerProcessor proc, void *proc_cls);
279 * Stop async find peer. Frees associated resources.
281 * @param find_peer_handle GET operation to stop.
284 GNUNET_DHT_find_peer_stop (struct GNUNET_DHT_FindPeerHandle *find_peer_handle);
288 /* ***** Special low-level API providing generic routeing abstraction ***** */
291 * Iterator called on each result obtained from a generic route
295 * @param key key that was used
296 * @param outgoing_path NULL-terminated array of pointers
297 * to the peers on reverse outgoing
298 * path (or NULL if not recorded)
299 * to the peers on the PUT path (or NULL if not recorded)
300 * @param reply response
302 typedef void (*GNUNET_DHT_ReplyProcessor) (void *cls,
303 const GNUNET_HashCode * key,
304 const struct GNUNET_PeerIdentity *
305 const *outgoing_path,
306 const struct GNUNET_MessageHeader *
311 * Perform an asynchronous ROUTE_START operation on the DHT.
313 * @param handle handle to the DHT service
314 * @param key the key to look up
315 * @param desired_replication_level how many peers should ultimately receive
316 * this message (advisory only, target may be too high for the
317 * given DHT or not hit exactly).
318 * @param options options for routing
319 * @param enc send the encapsulated message to a peer close to the key
320 * @param timeout when to abort with an error if we fail to get
321 * a confirmation for the request (when necessary) or how long
322 * to wait for transmission to the service; only applies
324 * @param iter function to call on each result, NULL if no replies are expected
325 * @param iter_cls closure for iter
326 * @param cont continuation to call when the request has been transmitted
327 * the first time to the service
328 * @param cont_cls closure for cont
329 * @return handle to stop the request, NULL if the request is "fire and forget"
331 struct GNUNET_DHT_RouteHandle *
332 GNUNET_DHT_route_start (struct GNUNET_DHT_Handle *handle,
333 const GNUNET_HashCode * key,
334 uint32_t desired_replication_level,
335 enum GNUNET_DHT_RouteOption options,
336 const struct GNUNET_MessageHeader *enc,
337 struct GNUNET_TIME_Relative timeout,
338 GNUNET_DHT_ReplyProcessor iter, void *iter_cls,
339 GNUNET_SCHEDULER_Task cont, void *cont_cls);
344 * Stop async route operation. Frees associated resources.
346 * @param route_handle operation to stop.
349 GNUNET_DHT_route_stop (struct GNUNET_DHT_RouteHandle *route_handle);
352 /* ***** Special API for controlling DHT routing maintenance ******* */
356 * Send a message to the DHT telling it to issue a single find
357 * peer request using the peers unique identifier as key. This
358 * is used to fill the routing table, and is normally controlled
359 * by the DHT itself. However, for testing and perhaps more
360 * close control over the DHT, this can be explicitly managed.
362 * @param cont continuation to call when done (transmitting request to service)
363 * @param cont_cls closure for cont
364 * @param handle handle to the DHT service
367 GNUNET_DHT_find_peers (struct GNUNET_DHT_Handle *handle,
368 GNUNET_SCHEDULER_Task cont, void *cont_cls);
370 /* ***** Special API for testing robustness with malicious peers ******* */
373 /* Note that these functions are NOT considered to be part of the
374 "official" API and hence are NOT subjected to library versioning;
375 only developers testing GNUnet's robustness should have any use for
376 them, applications should never use them. Applications must NOT
377 define "HAVE_MALICIOUS" before including this header. */
380 * Send a message to the DHT telling it to start dropping
381 * all requests received.
383 * @param handle handle to the DHT service
384 * @param cont continuation to call when done (transmitting request to service)
385 * @param cont_cls closure for cont
389 GNUNET_DHT_set_malicious_dropper (struct GNUNET_DHT_Handle *handle,
390 GNUNET_SCHEDULER_Task cont, void *cont_cls);
394 * Send a message to the DHT telling it to start issuing random PUT
395 * requests every 'frequency' milliseconds.
397 * @param handle handle to the DHT service
398 * @param frequency delay between sending malicious messages
399 * @param cont continuation to call when done (transmitting request to service)
400 * @param cont_cls closure for cont
403 GNUNET_DHT_set_malicious_putter (struct GNUNET_DHT_Handle *handle,
404 struct GNUNET_TIME_Relative frequency,
405 GNUNET_SCHEDULER_Task cont, void *cont_cls);
409 * Send a message to the DHT telling it to start issuing random GET
410 * requests every 'frequency' milliseconds.
412 * @param handle handle to the DHT service
413 * @param frequency delay between sending malicious messages
414 * @param cont continuation to call when done (transmitting request to service)
415 * @param cont_cls closure for cont
418 GNUNET_DHT_set_malicious_getter (struct GNUNET_DHT_Handle *handle,
419 struct GNUNET_TIME_Relative frequency,
420 GNUNET_SCHEDULER_Task cont, void *cont_cls);
425 #if 0 /* keep Emacsens' auto-indent happy */
434 /* gnunet_dht_service.h */