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