codesonar fixes
[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  * Connection to the DHT service.
56  */
57 struct GNUNET_DHT_Handle;
58
59 /**
60  * Handle to control a route operation.
61  */
62 struct GNUNET_DHT_RouteHandle;
63
64 /**
65  * Handle to control a get operation.
66  */
67 struct GNUNET_DHT_GetHandle;
68
69 /**
70  * Handle to control a find peer operation.
71  */
72 struct GNUNET_DHT_FindPeerHandle;
73
74
75 /**
76  * Options for routing.
77  */
78 enum GNUNET_DHT_RouteOption
79   {
80     /**
81      * Default.  Do nothing special.
82      */
83     GNUNET_DHT_RO_NONE = 0,
84
85     /**
86      * Each peer along the way should look at 'enc' (otherwise
87      * only the k-peers closest to the key should look at it).
88      */
89     GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE = 1,
90
91     /**
92      * We should keep track of the route that the message
93      * took in the P2P network.
94      */
95     GNUNET_DHT_RO_RECORD_ROUTE = 2
96   };
97
98
99 /**
100  * Initialize the connection with the DHT service.
101  *
102  * @param cfg configuration to use
103  * @param ht_len size of the internal hash table to use for
104  *               processing multiple GET/FIND requests in parallel
105  * @return NULL on error
106  */
107 struct GNUNET_DHT_Handle *
108 GNUNET_DHT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
109                     unsigned int ht_len);
110
111
112 /**
113  * Shutdown connection with the DHT service.
114  *
115  * @param handle connection to shut down
116  */
117 void
118 GNUNET_DHT_disconnect (struct GNUNET_DHT_Handle *handle);
119
120
121 /* *************** Standard API: get and put ******************* */
122
123 /**
124  * Perform a PUT operation on the DHT identified by 'table' storing
125  * a binding of 'key' to 'value'.  The peer does not have to be part
126  * of the table (if so, we will attempt to locate a peer that is!)
127  *
128  * @param handle handle to DHT service
129  * @param key the key to store data under
130  * @param options routing options for this message
131  * @param type type of the value
132  * @param size number of bytes in data; must be less than 64k
133  * @param data the data to store
134  * @param exp desired expiration time for the data
135  * @param timeout when to abort if we fail to transmit the request 
136  *                for the PUT to the local DHT service
137  * @param cont continuation to call when done (transmitting request to service)
138  * @param cont_cls closure for cont
139  */
140 void
141 GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle,
142                 const GNUNET_HashCode * key,
143                 enum GNUNET_DHT_RouteOption options,
144                 enum GNUNET_BLOCK_Type type,
145                 size_t size,
146                 const char *data,
147                 struct GNUNET_TIME_Absolute exp,
148                 struct GNUNET_TIME_Relative timeout,
149                 GNUNET_SCHEDULER_Task cont,
150                 void *cont_cls);
151
152
153 /**
154  * Iterator called on each result obtained for a DHT
155  * operation that expects a reply
156  *
157  * @param cls closure
158  * @param exp when will this value expire
159  * @param key key of the result
160  * @param get_path NULL-terminated array of pointers
161  *                 to the peers on reverse GET path (or NULL if not recorded)
162  * @param put_path NULL-terminated array of pointers
163  *                 to the peers on the PUT path (or NULL if not recorded)
164  * @param type type of the result
165  * @param size number of bytes in data
166  * @param data pointer to the result data
167  */
168 typedef void (*GNUNET_DHT_GetIterator)(void *cls,
169                                        struct GNUNET_TIME_Absolute exp,
170                                        const GNUNET_HashCode * key,
171                                        const struct GNUNET_PeerIdentity * const *get_path,
172                                        const struct GNUNET_PeerIdentity * const *put_path,
173                                        enum GNUNET_BLOCK_Type type,
174                                        size_t size,
175                                        const void *data);
176
177
178
179 /**
180  * Perform an asynchronous GET operation on the DHT.  See
181  * also "GNUNET_BLOCK_evaluate".
182  *
183  * @param handle handle to the DHT service
184  * @param timeout timeout for this request to be sent to the
185  *        service (this is NOT a timeout for receiving responses)
186  * @param type expected type of the response object (GNUNET_BLOCK_TYPE_FS_*)
187  * @param key the key to look up
188  * @param options routing options for this message
189  * @param bf bloom filter associated with query (can be NULL)
190  * @param bf_mutator mutation value for bf
191  * @param xquery extrended query data (can be NULL, depending on type)
192  * @param xquery_size number of bytes in xquery
193  * @param iter function to call on each result
194  * @param iter_cls closure for iter
195  * @return handle to stop the async get, NULL on error
196  */
197 struct GNUNET_DHT_GetHandle *
198 GNUNET_DHT_get_start (struct GNUNET_DHT_Handle *handle,
199                       struct GNUNET_TIME_Relative timeout,
200                       enum GNUNET_BLOCK_Type type,
201                       const GNUNET_HashCode * key,
202                       enum GNUNET_DHT_RouteOption options,
203                       const struct GNUNET_CONTAINER_BloomFilter *bf,
204                       int32_t bf_mutator,
205                       const void *xquery,
206                       size_t xquery_size,
207                       GNUNET_DHT_GetIterator iter,
208                       void *iter_cls);
209
210
211 /**
212  * Stop async DHT-get.  Frees associated resources.
213  *
214  * @param get_handle GET operation to stop.
215  */
216 void
217 GNUNET_DHT_get_stop (struct GNUNET_DHT_GetHandle *get_handle);
218
219
220 /* ******** Special high-level API for finding peers *********** */
221
222 /**
223  * Iterator called on each result obtained from a find peer
224  * operation
225  *
226  * @param cls closure
227  * @param peer hello of a target (peer near key)
228  */
229 typedef void (*GNUNET_DHT_FindPeerProcessor)(void *cls,
230                                              const struct GNUNET_HELLO_Message *peer);
231
232
233 /**
234  * Perform an asynchronous FIND PEER operation on the DHT.
235  *
236  * @param handle handle to the DHT service
237  * @param timeout timeout for this request to be sent to the
238  *        service
239  * @param key the key to look up
240  * @param options routing options for this message
241  * @param proc function to call on each result
242  * @param proc_cls closure for proc
243  * @return handle to stop the async get, NULL on error
244  */
245 struct GNUNET_DHT_FindPeerHandle *
246 GNUNET_DHT_find_peer_start (struct GNUNET_DHT_Handle *handle,
247                             struct GNUNET_TIME_Relative timeout,
248                             const GNUNET_HashCode *key,
249                             enum GNUNET_DHT_RouteOption options,
250                             GNUNET_DHT_FindPeerProcessor proc,
251                             void *proc_cls);
252
253
254 /**
255  * Stop async find peer.  Frees associated resources.
256  *
257  * @param find_peer_handle GET operation to stop.
258  */
259 void
260 GNUNET_DHT_find_peer_stop (struct GNUNET_DHT_FindPeerHandle *find_peer_handle);
261
262
263
264 /* ***** Special low-level API providing generic routeing abstraction ***** */
265
266 /**
267  * Iterator called on each result obtained from a generic route
268  * operation
269  *
270  * @param cls closure
271  * @param key key that was used
272  * @param get_path NULL-terminated array of pointers
273  *                 to the peers on reverse GET path (or NULL if not recorded)
274  * @param put_path NULL-terminated array of pointers
275  *                 to the peers on the PUT path (or NULL if not recorded)
276  * @param reply response
277  */
278 typedef void (*GNUNET_DHT_ReplyProcessor)(void *cls,
279                                           const GNUNET_HashCode *key,
280                                           const struct GNUNET_PeerIdentity * const *get_path,
281                                           const struct GNUNET_PeerIdentity * const *put_path,
282                                           const struct GNUNET_MessageHeader *reply);
283
284
285 /**
286  * Perform an asynchronous ROUTE_START operation on the DHT.
287  *
288  * @param handle handle to the DHT service
289  * @param key the key to look up
290  * @param desired_replication_level how many peers should ultimately receive
291  *                this message (advisory only, target may be too high for the
292  *                given DHT or not hit exactly).
293  * @param options options for routing
294  * @param enc send the encapsulated message to a peer close to the key
295  * @param timeout when to abort with an error if we fail to get
296  *                a confirmation for the request (when necessary) or how long
297  *                to wait for transmission to the service; only applies
298  *                if 'iter' is NULL
299  * @param iter function to call on each result, NULL if no replies are expected
300  * @param iter_cls closure for iter
301  * @param cont continuation to call when the request has been transmitted
302  *             the first time to the service
303  * @param cont_cls closure for cont
304  * @return handle to stop the request, NULL if the request is "fire and forget"
305  */
306 struct GNUNET_DHT_RouteHandle *
307 GNUNET_DHT_route_start (struct GNUNET_DHT_Handle *handle,
308                         const GNUNET_HashCode *key,
309                         uint32_t desired_replication_level,
310                         enum GNUNET_DHT_RouteOption options,
311                         const struct GNUNET_MessageHeader *enc,
312                         struct GNUNET_TIME_Relative timeout,
313                         GNUNET_DHT_ReplyProcessor iter,
314                         void *iter_cls,
315                         GNUNET_SCHEDULER_Task cont,
316                         void *cont_cls);
317
318
319
320 /**
321  * Stop async route operation.  Frees associated resources.
322  *
323  * @param route_handle  operation to stop.
324  */
325 void
326 GNUNET_DHT_route_stop (struct GNUNET_DHT_RouteHandle *route_handle);
327
328
329 /* ***** Special API for controlling DHT routing maintenance ******* */
330
331
332 /**
333  * Send a message to the DHT telling it to issue a single find
334  * peer request using the peers unique identifier as key.  This
335  * is used to fill the routing table, and is normally controlled
336  * by the DHT itself.  However, for testing and perhaps more
337  * close control over the DHT, this can be explicitly managed.
338  *
339  * @param cont continuation to call when done (transmitting request to service)
340  * @param cont_cls closure for cont
341  * @param handle handle to the DHT service
342  */
343 void
344 GNUNET_DHT_find_peers (struct GNUNET_DHT_Handle *handle,
345                        GNUNET_SCHEDULER_Task cont,
346                        void *cont_cls);
347
348 /* ***** Special API for testing robustness with malicious peers ******* */
349
350 #if HAVE_MALICIOUS
351 /* Note that these functions are NOT considered to be part of the
352    "official" API and hence are NOT subjected to library versioning;
353    only developers testing GNUnet's robustness should have any use for
354    them, applications should never use them.  Applications must NOT
355    define "HAVE_MALICIOUS" before including this header. */
356
357 /**
358  * Send a message to the DHT telling it to start dropping
359  * all requests received.
360  *
361  * @param handle handle to the DHT service
362  */
363 void 
364 GNUNET_DHT_set_malicious_dropper (struct GNUNET_DHT_Handle *handle);
365
366
367 /**
368  * Send a message to the DHT telling it to start issuing random PUT
369  * requests every 'frequency' milliseconds.
370  *
371  * @param handle handle to the DHT service
372  * @param frequency delay (in milliseconds) between sending malicious messages
373  */
374 void 
375 GNUNET_DHT_set_malicious_putter (struct GNUNET_DHT_Handle *handle,
376                                  struct GNUNET_TIME_Relative frequency);
377
378
379 /**
380  * Send a message to the DHT telling it to start issuing random GET
381  * requests every 'frequency' milliseconds.
382  *
383  * @param handle handle to the DHT service
384  * @param frequency delay between sending malicious messages
385  */
386 void 
387 GNUNET_DHT_set_malicious_getter (struct GNUNET_DHT_Handle *handle, 
388                                  struct GNUNET_TIME_Relative frequency);
389
390
391 #endif
392
393 #if 0                           /* keep Emacsens' auto-indent happy */
394 {
395 #endif
396 #ifdef __cplusplus
397 }
398 #endif
399
400
401 #endif
402 /* gnunet_dht_service.h */