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