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