46929c36636d84dcfcdabcfbe298087e57e1cf7a
[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  * Default republication frequency for stored data in the DHT.
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 *GNUNET_DHT_connect (const struct
127                                               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 GNUNET_DHT_disconnect (struct GNUNET_DHT_Handle *handle);
137
138
139 /* *************** Standard API: get and put ******************* */
140
141 /**
142  * Perform a PUT operation storing data in the DHT.
143  *
144  * @param handle handle to DHT service
145  * @param key the key to store under
146  * @param desired_replication_level estimate of how many
147  *                nearest peers this request should reach
148  * @param options routing options for this message
149  * @param type type of the value
150  * @param size number of bytes in data; must be less than 64k
151  * @param data the data to store
152  * @param exp desired expiration time for the value
153  * @param timeout how long to wait for transmission of this request
154  * @param cont continuation to call when done (transmitting request to service)
155  * @param cont_cls closure for cont
156  */
157 void GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle,
158                      const GNUNET_HashCode * key,
159                      uint32_t desired_replication_level,
160                      enum GNUNET_DHT_RouteOption options,
161                      enum GNUNET_BLOCK_Type type, size_t size, const char *data,
162                      struct GNUNET_TIME_Absolute exp,
163                      struct GNUNET_TIME_Relative timeout,
164                      GNUNET_SCHEDULER_Task cont, void *cont_cls);
165
166
167 /**
168  * Iterator called on each result obtained for a DHT
169  * operation that expects a reply
170  *
171  * @param cls closure
172  * @param exp when will this value expire
173  * @param key key of the result
174  * @param get_path NULL-terminated array of pointers
175  *                 to the peers on reverse GET path (or NULL if not recorded)
176  * @param put_path NULL-terminated array of pointers
177  *                 to the peers on the PUT path (or NULL if not recorded)
178  * @param type type of the result
179  * @param size number of bytes in data
180  * @param data pointer to the result data
181  */
182 typedef void (*GNUNET_DHT_GetIterator) (void *cls,
183                                         struct GNUNET_TIME_Absolute exp,
184                                         const GNUNET_HashCode * key,
185                                         const struct GNUNET_PeerIdentity *
186                                         const *get_path,
187                                         const struct GNUNET_PeerIdentity *
188                                         const *put_path,
189                                         enum GNUNET_BLOCK_Type type,
190                                         size_t size, const void *data);
191
192
193
194 /**
195  * Perform an asynchronous GET operation on the DHT identified. See
196  * also "GNUNET_BLOCK_evaluate".
197  *
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
211  *
212  * @return handle to stop the async get
213  */
214 struct GNUNET_DHT_GetHandle *GNUNET_DHT_get_start (struct GNUNET_DHT_Handle
215                                                    *handle,
216                                                    struct GNUNET_TIME_Relative
217                                                    timeout,
218                                                    enum GNUNET_BLOCK_Type type,
219                                                    const GNUNET_HashCode * key,
220                                                    uint32_t
221                                                    desired_replication_level,
222                                                    enum GNUNET_DHT_RouteOption
223                                                    options,
224                                                    const struct
225                                                    GNUNET_CONTAINER_BloomFilter
226                                                    *bf, int32_t bf_mutator,
227                                                    const void *xquery,
228                                                    size_t xquery_size,
229                                                    GNUNET_DHT_GetIterator iter,
230                                                    void *iter_cls);
231
232
233 /**
234  * Stop async DHT-get.  Frees associated resources.
235  *
236  * @param get_handle GET operation to stop.
237  *
238  * On return get_handle will no longer be valid, caller
239  * must not use again!!!
240  */
241 void GNUNET_DHT_get_stop (struct GNUNET_DHT_GetHandle *get_handle);
242
243
244 /* ******** Special high-level API for finding peers *********** */
245
246 /**
247  * Iterator called on each result obtained from a find peer
248  * operation
249  *
250  * @param cls closure
251  * @param peer hello of a target (peer near key)
252  */
253 typedef void (*GNUNET_DHT_FindPeerProcessor) (void *cls,
254                                               const struct GNUNET_HELLO_Message
255                                               * peer);
256
257
258 /**
259  * Perform an asynchronous FIND PEER operation on the DHT.
260  *
261  * @param handle handle to the DHT service
262  * @param timeout timeout for this request to be sent to the
263  *        service
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
269  */
270 struct GNUNET_DHT_FindPeerHandle *GNUNET_DHT_find_peer_start (struct
271                                                               GNUNET_DHT_Handle
272                                                               *handle,
273                                                               struct
274                                                               GNUNET_TIME_Relative
275                                                               timeout,
276                                                               const
277                                                               GNUNET_HashCode *
278                                                               key,
279                                                               enum
280                                                               GNUNET_DHT_RouteOption
281                                                               options,
282                                                               GNUNET_DHT_FindPeerProcessor
283                                                               proc,
284                                                               void *proc_cls);
285
286
287 /**
288  * Stop async find peer.  Frees associated resources.
289  *
290  * @param find_peer_handle GET operation to stop.
291  */
292 void GNUNET_DHT_find_peer_stop (struct GNUNET_DHT_FindPeerHandle
293                                 *find_peer_handle);
294
295
296
297 /* ***** Special low-level API providing generic routeing abstraction ***** */
298
299 /**
300  * Iterator called on each result obtained from a generic route
301  * operation
302  *
303  * @param cls closure
304  * @param key key that was used
305  * @param outgoing_path NULL-terminated array of pointers
306  *                      to the peers on reverse outgoing
307  *                      path (or NULL if not recorded)
308  *                 to the peers on the PUT path (or NULL if not recorded)
309  * @param reply response
310  */
311 typedef void (*GNUNET_DHT_ReplyProcessor) (void *cls,
312                                            const GNUNET_HashCode * key,
313                                            const struct GNUNET_PeerIdentity *
314                                            const *outgoing_path,
315                                            const struct GNUNET_MessageHeader *
316                                            reply);
317
318
319 /**
320  * Perform an asynchronous ROUTE_START operation on the DHT.
321  *
322  * @param handle handle to the DHT service
323  * @param key the key to look up
324  * @param desired_replication_level how many peers should ultimately receive
325  *                this message (advisory only, target may be too high for the
326  *                given DHT or not hit exactly).
327  * @param options options for routing
328  * @param enc send the encapsulated message to a peer close to the key
329  * @param timeout when to abort with an error if we fail to get
330  *                a confirmation for the request (when necessary) or how long
331  *                to wait for transmission to the service; only applies
332  *                if 'iter' is NULL
333  * @param iter function to call on each result, NULL if no replies are expected
334  * @param iter_cls closure for iter
335  * @param cont continuation to call when the request has been transmitted
336  *             the first time to the service
337  * @param cont_cls closure for cont
338  * @return handle to stop the request, NULL if the request is "fire and forget"
339  */
340 struct GNUNET_DHT_RouteHandle *GNUNET_DHT_route_start (struct GNUNET_DHT_Handle
341                                                        *handle,
342                                                        const GNUNET_HashCode *
343                                                        key,
344                                                        uint32_t
345                                                        desired_replication_level,
346                                                        enum
347                                                        GNUNET_DHT_RouteOption
348                                                        options,
349                                                        const struct
350                                                        GNUNET_MessageHeader
351                                                        *enc,
352                                                        struct
353                                                        GNUNET_TIME_Relative
354                                                        timeout,
355                                                        GNUNET_DHT_ReplyProcessor
356                                                        iter, void *iter_cls,
357                                                        GNUNET_SCHEDULER_Task
358                                                        cont, void *cont_cls);
359
360
361
362 /**
363  * Stop async route operation.  Frees associated resources.
364  *
365  * @param route_handle  operation to stop.
366  */
367 void GNUNET_DHT_route_stop (struct GNUNET_DHT_RouteHandle *route_handle);
368
369
370 /* ***** Special API for controlling DHT routing maintenance ******* */
371
372
373 /**
374  * Send a message to the DHT telling it to issue a single find
375  * peer request using the peers unique identifier as key.  This
376  * is used to fill the routing table, and is normally controlled
377  * by the DHT itself.  However, for testing and perhaps more
378  * close control over the DHT, this can be explicitly managed.
379  *
380  * @param cont continuation to call when done (transmitting request to service)
381  * @param cont_cls closure for cont
382  * @param handle handle to the DHT service
383  */
384 void GNUNET_DHT_find_peers (struct GNUNET_DHT_Handle *handle,
385                             GNUNET_SCHEDULER_Task cont, void *cont_cls);
386
387 /* ***** Special API for testing robustness with malicious peers ******* */
388
389 #if HAVE_MALICIOUS
390 /* Note that these functions are NOT considered to be part of the
391    "official" API and hence are NOT subjected to library versioning;
392    only developers testing GNUnet's robustness should have any use for
393    them, applications should never use them.  Applications must NOT
394    define "HAVE_MALICIOUS" before including this header. */
395
396 /**
397  * Send a message to the DHT telling it to start dropping
398  * all requests received.
399  *
400  * @param handle handle to the DHT service
401  * @param cont continuation to call when done (transmitting request to service)
402  * @param cont_cls closure for cont
403  *
404  */
405 void GNUNET_DHT_set_malicious_dropper (struct GNUNET_DHT_Handle *handle,
406                                        GNUNET_SCHEDULER_Task cont,
407                                        void *cont_cls);
408
409
410 /**
411  * Send a message to the DHT telling it to start issuing random PUT
412  * requests every 'frequency' milliseconds.
413  *
414  * @param handle handle to the DHT service
415  * @param frequency delay between sending malicious messages
416  * @param cont continuation to call when done (transmitting request to service)
417  * @param cont_cls closure for cont
418  */
419 void GNUNET_DHT_set_malicious_putter (struct GNUNET_DHT_Handle *handle,
420                                       struct GNUNET_TIME_Relative frequency,
421                                       GNUNET_SCHEDULER_Task cont,
422                                       void *cont_cls);
423
424
425 /**
426  * Send a message to the DHT telling it to start issuing random GET
427  * requests every 'frequency' milliseconds.
428  *
429  * @param handle handle to the DHT service
430  * @param frequency delay between sending malicious messages
431  * @param cont continuation to call when done (transmitting request to service)
432  * @param cont_cls closure for cont
433  */
434 void GNUNET_DHT_set_malicious_getter (struct GNUNET_DHT_Handle *handle,
435                                       struct GNUNET_TIME_Relative frequency,
436                                       GNUNET_SCHEDULER_Task cont,
437                                       void *cont_cls);
438
439
440 #endif
441
442 #if 0                           /* keep Emacsens' auto-indent happy */
443 {
444 #endif
445 #ifdef __cplusplus
446 }
447 #endif
448
449
450 #endif
451 /* gnunet_dht_service.h */