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