18eab8a9f28ff90c815ada48a516cbfa3ad364d5
[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
113 /**
114  * Initialize the connection with the DHT service.
115  *
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
120  */
121 struct GNUNET_DHT_Handle *
122 GNUNET_DHT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
123                     unsigned int ht_len);
124
125
126 /**
127  * Shutdown connection with the DHT service.
128  *
129  * @param handle connection to shut down
130  */
131 void
132 GNUNET_DHT_disconnect (struct GNUNET_DHT_Handle *handle);
133
134
135 /* *************** Standard API: get and put ******************* */
136
137 /**
138  * Perform a PUT operation storing data in the DHT.
139  *
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
153  */
154 void
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,
160                 size_t size,
161                 const char *data,
162                 struct GNUNET_TIME_Absolute exp,
163                 struct GNUNET_TIME_Relative timeout,
164                 GNUNET_SCHEDULER_Task cont,
165                 void *cont_cls);
166
167
168 /**
169  * Iterator called on each result obtained for a DHT
170  * operation that expects a reply
171  *
172  * @param cls closure
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
182  */
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,
189                                        size_t size,
190                                        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 *
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,
222                       int32_t bf_mutator,
223                       const void *xquery,
224                       size_t xquery_size,
225                       GNUNET_DHT_GetIterator iter,
226                       void *iter_cls);
227
228
229 /**
230  * Stop async DHT-get.  Frees associated resources.
231  *
232  * @param get_handle GET operation to stop.
233  *
234  * On return get_handle will no longer be valid, caller
235  * must not use again!!!
236  */
237 void
238 GNUNET_DHT_get_stop (struct GNUNET_DHT_GetHandle *get_handle);
239
240
241 /* ******** Special high-level API for finding peers *********** */
242
243 /**
244  * Iterator called on each result obtained from a find peer
245  * operation
246  *
247  * @param cls closure
248  * @param peer hello of a target (peer near key)
249  */
250 typedef void (*GNUNET_DHT_FindPeerProcessor)(void *cls,
251                                              const struct GNUNET_HELLO_Message *peer);
252
253
254 /**
255  * Perform an asynchronous FIND PEER operation on the DHT.
256  *
257  * @param handle handle to the DHT service
258  * @param timeout timeout for this request to be sent to the
259  *        service
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
265  */
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,
272                             void *proc_cls);
273
274
275 /**
276  * Stop async find peer.  Frees associated resources.
277  *
278  * @param find_peer_handle GET operation to stop.
279  */
280 void
281 GNUNET_DHT_find_peer_stop (struct GNUNET_DHT_FindPeerHandle *find_peer_handle);
282
283
284
285 /* ***** Special low-level API providing generic routeing abstraction ***** */
286
287 /**
288  * Iterator called on each result obtained from a generic route
289  * operation
290  *
291  * @param cls closure
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
298  */
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);
304
305
306 /**
307  * Perform an asynchronous ROUTE_START operation on the DHT.
308  *
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
319  *                if 'iter' is NULL
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"
326  */
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,
335                         void *iter_cls,
336                         GNUNET_SCHEDULER_Task cont,
337                         void *cont_cls);
338
339
340
341 /**
342  * Stop async route operation.  Frees associated resources.
343  *
344  * @param route_handle  operation to stop.
345  */
346 void
347 GNUNET_DHT_route_stop (struct GNUNET_DHT_RouteHandle *route_handle);
348
349
350 /* ***** Special API for controlling DHT routing maintenance ******* */
351
352
353 /**
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.
359  *
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
363  */
364 void
365 GNUNET_DHT_find_peers (struct GNUNET_DHT_Handle *handle,
366                        GNUNET_SCHEDULER_Task cont,
367                        void *cont_cls);
368
369 /* ***** Special API for testing robustness with malicious peers ******* */
370
371 #if HAVE_MALICIOUS
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. */
377
378 /**
379  * Send a message to the DHT telling it to start dropping
380  * all requests received.
381  *
382  * @param handle handle to the DHT service
383  */
384 void 
385 GNUNET_DHT_set_malicious_dropper (struct GNUNET_DHT_Handle *handle);
386
387
388 /**
389  * Send a message to the DHT telling it to start issuing random PUT
390  * requests every 'frequency' milliseconds.
391  *
392  * @param handle handle to the DHT service
393  * @param frequency delay (in milliseconds) between sending malicious messages
394  */
395 void 
396 GNUNET_DHT_set_malicious_putter (struct GNUNET_DHT_Handle *handle,
397                                  struct GNUNET_TIME_Relative frequency);
398
399
400 /**
401  * Send a message to the DHT telling it to start issuing random GET
402  * requests every 'frequency' milliseconds.
403  *
404  * @param handle handle to the DHT service
405  * @param frequency delay between sending malicious messages
406  */
407 void 
408 GNUNET_DHT_set_malicious_getter (struct GNUNET_DHT_Handle *handle, 
409                                  struct GNUNET_TIME_Relative frequency);
410
411
412 #endif
413
414 #if 0                           /* keep Emacsens' auto-indent happy */
415 {
416 #endif
417 #ifdef __cplusplus
418 }
419 #endif
420
421
422 #endif
423 /* gnunet_dht_service.h */