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