e57dd718c607bcb769ac8d7ecf3fb14d787c1928
[oweals/gnunet.git] / src / include / gnunet_dht_service.h
1 /*
2       This file is part of GNUnet
3       (C) 2004, 2005, 2006, 2008, 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19  */
20
21 /**
22  * @file include/gnunet_dht_service.h
23  * @brief API to the DHT service
24  * @author Christian Grothoff
25  */
26
27 #ifndef GNUNET_DHT_SERVICE_H
28 #define GNUNET_DHT_SERVICE_H
29
30 #include "gnunet_util_lib.h"
31 #include "gnunet_block_lib.h"
32 #include "gnunet_hello_lib.h"
33
34 #ifdef __cplusplus
35 extern "C"
36 {
37 #if 0                           /* keep Emacsens' auto-indent happy */
38 }
39 #endif
40 #endif
41
42
43 /**
44  * FIXME: document.
45  */
46 #define GNUNET_DHT_DEFAULT_REPUBLISH_FREQUENCY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 60)
47
48 /**
49  * K-value that must be used for the bloom filter 'GET'
50  * queries.
51  */
52 #define GNUNET_DHT_GET_BLOOMFILTER_K 16
53
54 /**
55  * Non-intelligent default DHT GET replication.
56  * Should be chosen by application if anything about
57  * the network is known.
58  */
59 #define DEFAULT_GET_REPLICATION 5
60
61 /**
62  * Non-intelligent default DHT PUT replication.
63  * Should be chosen by application if anything about
64  * the network is known.
65  */
66 #define DEFAULT_PUT_REPLICATION 8
67
68 /**
69  * Connection to the DHT service.
70  */
71 struct GNUNET_DHT_Handle;
72
73 /**
74  * Handle to control a route operation.
75  */
76 struct GNUNET_DHT_RouteHandle;
77
78 /**
79  * Handle to control a get operation.
80  */
81 struct GNUNET_DHT_GetHandle;
82
83 /**
84  * Handle to control a find peer operation.
85  */
86 struct GNUNET_DHT_FindPeerHandle;
87
88
89 /**
90  * Options for routing.
91  */
92 enum GNUNET_DHT_RouteOption
93   {
94     /**
95      * Default.  Do nothing special.
96      */
97     GNUNET_DHT_RO_NONE = 0,
98
99     /**
100      * Each peer along the way should look at 'enc' (otherwise
101      * only the k-peers closest to the key should look at it).
102      */
103     GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE = 1,
104
105     /**
106      * We should keep track of the route that the message
107      * took in the P2P network.
108      */
109     GNUNET_DHT_RO_RECORD_ROUTE = 2,
110
111     /**
112      * Possible message option for query key randomization.
113      */
114     GNUNET_DHT_RO_BART = 4
115   };
116
117
118 /**
119  * Initialize the connection with the DHT service.
120  *
121  * @param cfg configuration to use
122  * @param ht_len size of the internal hash table to use for
123  *               processing multiple GET/FIND requests in parallel
124  * @return NULL on error
125  */
126 struct GNUNET_DHT_Handle *
127 GNUNET_DHT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
128                     unsigned int ht_len);
129
130
131 /**
132  * Shutdown connection with the DHT service.
133  *
134  * @param handle connection to shut down
135  */
136 void
137 GNUNET_DHT_disconnect (struct GNUNET_DHT_Handle *handle);
138
139
140 /* *************** Standard API: get and put ******************* */
141
142 /**
143  * Perform a PUT operation storing data in the DHT.
144  *
145  * @param handle handle to DHT service
146  * @param key the key to store under
147  * @param desired_replication_level estimate of how many
148  *                nearest peers this request should reach
149  * @param options routing options for this message
150  * @param type type of the value
151  * @param size number of bytes in data; must be less than 64k
152  * @param data the data to store
153  * @param exp desired expiration time for the value
154  * @param timeout how long to wait for transmission of this request
155  * @param cont continuation to call when done (transmitting request to service)
156  * @param cont_cls closure for cont
157  * @return GNUNET_YES if put message is queued for transmission
158  */
159 void
160 GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle,
161                 const GNUNET_HashCode * key,
162                 uint32_t desired_replication_level,
163                 enum GNUNET_DHT_RouteOption options,
164                 enum GNUNET_BLOCK_Type type,
165                 size_t size,
166                 const char *data,
167                 struct GNUNET_TIME_Absolute exp,
168                 struct GNUNET_TIME_Relative timeout,
169                 GNUNET_SCHEDULER_Task cont,
170                 void *cont_cls);
171
172
173 /**
174  * Iterator called on each result obtained for a DHT
175  * operation that expects a reply
176  *
177  * @param cls closure
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
187  */
188 typedef void (*GNUNET_DHT_GetIterator)(void *cls,
189                                        struct GNUNET_TIME_Absolute exp,
190                                        const GNUNET_HashCode * key,
191                                        const struct GNUNET_PeerIdentity * const *get_path,
192                                        const struct GNUNET_PeerIdentity * const *put_path,
193                                        enum GNUNET_BLOCK_Type type,
194                                        size_t size,
195                                        const void *data);
196
197
198
199 /**
200  * Perform an asynchronous GET operation on the DHT identified. See
201  * also "GNUNET_BLOCK_evaluate".
202  *
203  * @param handle handle to the DHT service
204  * @param timeout how long to wait for transmission of this request to the service
205  * @param type expected type of the response object
206  * @param key the key to look up
207  * @param desired_replication_level estimate of how many
208                   nearest peers this request should reach
209  * @param options routing options for this message
210  * @param bf bloom filter associated with query (can be NULL)
211  * @param bf_mutator mutation value for bf
212  * @param xquery extended query data (can be NULL, depending on type)
213  * @param xquery_size number of bytes in xquery
214  * @param iter function to call on each result
215  * @param iter_cls closure for iter
216  *
217  * @return handle to stop the async get
218  */
219 struct GNUNET_DHT_GetHandle *
220 GNUNET_DHT_get_start (struct GNUNET_DHT_Handle *handle,
221                       struct GNUNET_TIME_Relative timeout,
222                       enum GNUNET_BLOCK_Type type,
223                       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,
228                       const void *xquery,
229                       size_t xquery_size,
230                       GNUNET_DHT_GetIterator iter,
231                       void *iter_cls);
232
233
234 /**
235  * Stop async DHT-get.  Frees associated resources.
236  *
237  * @param get_handle GET operation to stop.
238  *
239  * On return get_handle will no longer be valid, caller
240  * must not use again!!!
241  */
242 void
243 GNUNET_DHT_get_stop (struct GNUNET_DHT_GetHandle *get_handle);
244
245
246 /* ******** Special high-level API for finding peers *********** */
247
248 /**
249  * Iterator called on each result obtained from a find peer
250  * operation
251  *
252  * @param cls closure
253  * @param peer hello of a target (peer near key)
254  */
255 typedef void (*GNUNET_DHT_FindPeerProcessor)(void *cls,
256                                              const struct GNUNET_HELLO_Message *peer);
257
258
259 /**
260  * Perform an asynchronous FIND PEER operation on the DHT.
261  *
262  * @param handle handle to the DHT service
263  * @param timeout timeout for this request to be sent to the
264  *        service
265  * @param key the key to look up
266  * @param options routing options for this message
267  * @param proc function to call on each result
268  * @param proc_cls closure for proc
269  * @return handle to stop the async get, NULL on error
270  */
271 struct GNUNET_DHT_FindPeerHandle *
272 GNUNET_DHT_find_peer_start (struct GNUNET_DHT_Handle *handle,
273                             struct GNUNET_TIME_Relative timeout,
274                             const GNUNET_HashCode *key,
275                             enum GNUNET_DHT_RouteOption options,
276                             GNUNET_DHT_FindPeerProcessor proc,
277                             void *proc_cls);
278
279
280 /**
281  * Stop async find peer.  Frees associated resources.
282  *
283  * @param find_peer_handle GET operation to stop.
284  */
285 void
286 GNUNET_DHT_find_peer_stop (struct GNUNET_DHT_FindPeerHandle *find_peer_handle);
287
288
289
290 /* ***** Special low-level API providing generic routeing abstraction ***** */
291
292 /**
293  * Iterator called on each result obtained from a generic route
294  * operation
295  *
296  * @param cls closure
297  * @param key key that was used
298  * @param outgoing_path NULL-terminated array of pointers
299  *                      to the peers on reverse outgoing
300  *                      path (or NULL if not recorded)
301  *                 to the peers on the PUT path (or NULL if not recorded)
302  * @param reply response
303  */
304 typedef void (*GNUNET_DHT_ReplyProcessor)(void *cls,
305                                           const GNUNET_HashCode *key,
306                                           const struct GNUNET_PeerIdentity * const *outgoing_path,
307                                           const struct GNUNET_MessageHeader *reply);
308
309
310 /**
311  * Perform an asynchronous ROUTE_START operation on the DHT.
312  *
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
323  *                if 'iter' is NULL
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"
330  */
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,
339                         void *iter_cls,
340                         GNUNET_SCHEDULER_Task cont,
341                         void *cont_cls);
342
343
344
345 /**
346  * Stop async route operation.  Frees associated resources.
347  *
348  * @param route_handle  operation to stop.
349  */
350 void
351 GNUNET_DHT_route_stop (struct GNUNET_DHT_RouteHandle *route_handle);
352
353
354 /* ***** Special API for controlling DHT routing maintenance ******* */
355
356
357 /**
358  * Send a message to the DHT telling it to issue a single find
359  * peer request using the peers unique identifier as key.  This
360  * is used to fill the routing table, and is normally controlled
361  * by the DHT itself.  However, for testing and perhaps more
362  * close control over the DHT, this can be explicitly managed.
363  *
364  * @param cont continuation to call when done (transmitting request to service)
365  * @param cont_cls closure for cont
366  * @param handle handle to the DHT service
367  */
368 void
369 GNUNET_DHT_find_peers (struct GNUNET_DHT_Handle *handle,
370                        GNUNET_SCHEDULER_Task cont,
371                        void *cont_cls);
372
373 /* ***** Special API for testing robustness with malicious peers ******* */
374
375 #if HAVE_MALICIOUS
376 /* Note that these functions are NOT considered to be part of the
377    "official" API and hence are NOT subjected to library versioning;
378    only developers testing GNUnet's robustness should have any use for
379    them, applications should never use them.  Applications must NOT
380    define "HAVE_MALICIOUS" before including this header. */
381
382 /**
383  * Send a message to the DHT telling it to start dropping
384  * all requests received.
385  *
386  * @param handle handle to the DHT service
387  * @param cont continuation to call when done (transmitting request to service)
388  * @param cont_cls closure for cont
389  *
390  */
391 void 
392 GNUNET_DHT_set_malicious_dropper (struct GNUNET_DHT_Handle *handle, GNUNET_SCHEDULER_Task cont,
393     void *cont_cls);
394
395
396 /**
397  * Send a message to the DHT telling it to start issuing random PUT
398  * requests every 'frequency' milliseconds.
399  *
400  * @param handle handle to the DHT service
401  * @param frequency delay between sending malicious messages
402  * @param cont continuation to call when done (transmitting request to service)
403  * @param cont_cls closure for cont
404  */
405 void 
406 GNUNET_DHT_set_malicious_putter (struct GNUNET_DHT_Handle *handle,
407          struct GNUNET_TIME_Relative frequency, GNUNET_SCHEDULER_Task cont,
408           void *cont_cls);
409
410
411 /**
412  * Send a message to the DHT telling it to start issuing random GET
413  * requests every 'frequency' milliseconds.
414  *
415  * @param handle handle to the DHT service
416  * @param frequency delay between sending malicious messages
417  * @param cont continuation to call when done (transmitting request to service)
418  * @param cont_cls closure for cont
419  */
420 void
421 GNUNET_DHT_set_malicious_getter (struct GNUNET_DHT_Handle *handle,
422          struct GNUNET_TIME_Relative frequency, GNUNET_SCHEDULER_Task cont,
423           void *cont_cls);
424
425
426 #endif
427
428 #if 0                           /* keep Emacsens' auto-indent happy */
429 {
430 #endif
431 #ifdef __cplusplus
432 }
433 #endif
434
435
436 #endif
437 /* gnunet_dht_service.h */