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