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 */
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 filter 'GET'
52 #define GNUNET_DHT_GET_BLOOMFILTER_K 16
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
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 ******************* */
138 * Perform a PUT operation storing data in the DHT.
140 * @param handle handle to DHT service
141 * @param key the key to store under
142 * @param desired_replication_level estimate of how many
143 * nearest peers this request should reach
144 * @param options routing options for this message
145 * @param type type of the value
146 * @param size number of bytes in data; must be less than 64k
147 * @param data the data to store
148 * @param exp desired expiration time for the value
149 * @param timeout how long to wait for transmission of this request
150 * @param cont continuation to call when done (transmitting request to service)
151 * @param cont_cls closure for cont
152 * @return GNUNET_YES if put message is queued for transmission
155 GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle,
156 const GNUNET_HashCode * key,
157 uint32_t desired_replication_level,
158 enum GNUNET_DHT_RouteOption options,
159 enum GNUNET_BLOCK_Type type,
162 struct GNUNET_TIME_Absolute exp,
163 struct GNUNET_TIME_Relative timeout,
164 GNUNET_SCHEDULER_Task cont,
169 * Iterator called on each result obtained for a DHT
170 * operation that expects a reply
173 * @param exp when will this value expire
174 * @param key key of the result
175 * @param get_path NULL-terminated array of pointers
176 * to the peers on reverse GET path (or NULL if not recorded)
177 * @param put_path NULL-terminated array of pointers
178 * to the peers on the PUT path (or NULL if not recorded)
179 * @param type type of the result
180 * @param size number of bytes in data
181 * @param data pointer to the result data
183 typedef void (*GNUNET_DHT_GetIterator)(void *cls,
184 struct GNUNET_TIME_Absolute exp,
185 const GNUNET_HashCode * key,
186 const struct GNUNET_PeerIdentity * const *get_path,
187 const struct GNUNET_PeerIdentity * const *put_path,
188 enum GNUNET_BLOCK_Type type,
195 * Perform an asynchronous GET operation on the DHT identified. See
196 * also "GNUNET_BLOCK_evaluate".
198 * @param handle handle to the DHT service
199 * @param timeout how long to wait for transmission of this request to the service
200 * @param type expected type of the response object
201 * @param key the key to look up
202 * @param desired_replication_level estimate of how many
203 nearest peers this request should reach
204 * @param options routing options for this message
205 * @param bf bloom filter associated with query (can be NULL)
206 * @param bf_mutator mutation value for bf
207 * @param xquery extended query data (can be NULL, depending on type)
208 * @param xquery_size number of bytes in xquery
209 * @param iter function to call on each result
210 * @param iter_cls closure for iter
212 * @return handle to stop the async get
214 struct GNUNET_DHT_GetHandle *
215 GNUNET_DHT_get_start (struct GNUNET_DHT_Handle *handle,
216 struct GNUNET_TIME_Relative timeout,
217 enum GNUNET_BLOCK_Type type,
218 const GNUNET_HashCode * key,
219 uint32_t desired_replication_level,
220 enum GNUNET_DHT_RouteOption options,
221 const struct GNUNET_CONTAINER_BloomFilter *bf,
225 GNUNET_DHT_GetIterator iter,
230 * Stop async DHT-get. Frees associated resources.
232 * @param get_handle GET operation to stop.
234 * On return get_handle will no longer be valid, caller
235 * must not use again!!!
238 GNUNET_DHT_get_stop (struct GNUNET_DHT_GetHandle *get_handle);
241 /* ******** Special high-level API for finding peers *********** */
244 * Iterator called on each result obtained from a find peer
248 * @param peer hello of a target (peer near key)
250 typedef void (*GNUNET_DHT_FindPeerProcessor)(void *cls,
251 const struct GNUNET_HELLO_Message *peer);
255 * Perform an asynchronous FIND PEER operation on the DHT.
257 * @param handle handle to the DHT service
258 * @param timeout timeout for this request to be sent to the
260 * @param key the key to look up
261 * @param options routing options for this message
262 * @param proc function to call on each result
263 * @param proc_cls closure for proc
264 * @return handle to stop the async get, NULL on error
266 struct GNUNET_DHT_FindPeerHandle *
267 GNUNET_DHT_find_peer_start (struct GNUNET_DHT_Handle *handle,
268 struct GNUNET_TIME_Relative timeout,
269 const GNUNET_HashCode *key,
270 enum GNUNET_DHT_RouteOption options,
271 GNUNET_DHT_FindPeerProcessor proc,
276 * Stop async find peer. Frees associated resources.
278 * @param find_peer_handle GET operation to stop.
281 GNUNET_DHT_find_peer_stop (struct GNUNET_DHT_FindPeerHandle *find_peer_handle);
285 /* ***** Special low-level API providing generic routeing abstraction ***** */
288 * Iterator called on each result obtained from a generic route
292 * @param key key that was used
293 * @param get_path NULL-terminated array of pointers
294 * to the peers on reverse GET path (or NULL if not recorded)
295 * @param put_path NULL-terminated array of pointers
296 * to the peers on the PUT path (or NULL if not recorded)
297 * @param reply response
299 typedef void (*GNUNET_DHT_ReplyProcessor)(void *cls,
300 const GNUNET_HashCode *key,
301 const struct GNUNET_PeerIdentity * const *get_path,
302 const struct GNUNET_PeerIdentity * const *put_path,
303 const struct GNUNET_MessageHeader *reply);
307 * Perform an asynchronous ROUTE_START operation on the DHT.
309 * @param handle handle to the DHT service
310 * @param key the key to look up
311 * @param desired_replication_level how many peers should ultimately receive
312 * this message (advisory only, target may be too high for the
313 * given DHT or not hit exactly).
314 * @param options options for routing
315 * @param enc send the encapsulated message to a peer close to the key
316 * @param timeout when to abort with an error if we fail to get
317 * a confirmation for the request (when necessary) or how long
318 * to wait for transmission to the service; only applies
320 * @param iter function to call on each result, NULL if no replies are expected
321 * @param iter_cls closure for iter
322 * @param cont continuation to call when the request has been transmitted
323 * the first time to the service
324 * @param cont_cls closure for cont
325 * @return handle to stop the request, NULL if the request is "fire and forget"
327 struct GNUNET_DHT_RouteHandle *
328 GNUNET_DHT_route_start (struct GNUNET_DHT_Handle *handle,
329 const GNUNET_HashCode *key,
330 uint32_t desired_replication_level,
331 enum GNUNET_DHT_RouteOption options,
332 const struct GNUNET_MessageHeader *enc,
333 struct GNUNET_TIME_Relative timeout,
334 GNUNET_DHT_ReplyProcessor iter,
336 GNUNET_SCHEDULER_Task cont,
342 * Stop async route operation. Frees associated resources.
344 * @param route_handle operation to stop.
347 GNUNET_DHT_route_stop (struct GNUNET_DHT_RouteHandle *route_handle);
350 /* ***** Special API for controlling DHT routing maintenance ******* */
354 * Send a message to the DHT telling it to issue a single find
355 * peer request using the peers unique identifier as key. This
356 * is used to fill the routing table, and is normally controlled
357 * by the DHT itself. However, for testing and perhaps more
358 * close control over the DHT, this can be explicitly managed.
360 * @param cont continuation to call when done (transmitting request to service)
361 * @param cont_cls closure for cont
362 * @param handle handle to the DHT service
365 GNUNET_DHT_find_peers (struct GNUNET_DHT_Handle *handle,
366 GNUNET_SCHEDULER_Task cont,
369 /* ***** Special API for testing robustness with malicious peers ******* */
372 /* Note that these functions are NOT considered to be part of the
373 "official" API and hence are NOT subjected to library versioning;
374 only developers testing GNUnet's robustness should have any use for
375 them, applications should never use them. Applications must NOT
376 define "HAVE_MALICIOUS" before including this header. */
379 * Send a message to the DHT telling it to start dropping
380 * all requests received.
382 * @param handle handle to the DHT service
385 GNUNET_DHT_set_malicious_dropper (struct GNUNET_DHT_Handle *handle);
389 * Send a message to the DHT telling it to start issuing random PUT
390 * requests every 'frequency' milliseconds.
392 * @param handle handle to the DHT service
393 * @param frequency delay (in milliseconds) between sending malicious messages
396 GNUNET_DHT_set_malicious_putter (struct GNUNET_DHT_Handle *handle,
397 struct GNUNET_TIME_Relative frequency);
401 * Send a message to the DHT telling it to start issuing random GET
402 * requests every 'frequency' milliseconds.
404 * @param handle handle to the DHT service
405 * @param frequency delay between sending malicious messages
408 GNUNET_DHT_set_malicious_getter (struct GNUNET_DHT_Handle *handle,
409 struct GNUNET_TIME_Relative frequency);
414 #if 0 /* keep Emacsens' auto-indent happy */
423 /* gnunet_dht_service.h */