adding number of preferences to allow iterating over preferences
[oweals/gnunet.git] / src / include / gnunet_dht_service.h
1 /*
2       This file is part of GNUnet
3       Copyright (C) 2004-2013 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  * @defgroup dht Distributed Hash Table
26  * @{
27  */
28
29 #ifndef GNUNET_DHT_SERVICE_H
30 #define GNUNET_DHT_SERVICE_H
31
32 #include "gnunet_util_lib.h"
33 #include "gnunet_block_lib.h"
34 #include "gnunet_hello_lib.h"
35
36 #ifdef __cplusplus
37 extern "C"
38 {
39 #if 0                           /* keep Emacsens' auto-indent happy */
40 }
41 #endif
42 #endif
43
44
45 /**
46  * Default republication frequency for stored data in the DHT.
47  */
48 #define GNUNET_DHT_DEFAULT_REPUBLISH_FREQUENCY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 60)
49
50
51
52 /**
53  * Connection to the DHT service.
54  */
55 struct GNUNET_DHT_Handle;
56
57 /**
58  * Handle to control a get operation.
59  */
60 struct GNUNET_DHT_GetHandle;
61
62 /**
63  * Handle to control a find peer operation.
64  */
65 struct GNUNET_DHT_FindPeerHandle;
66
67
68 /**
69  * Options for routing.
70  */
71 enum GNUNET_DHT_RouteOption
72 {
73   /**
74    * Default.  Do nothing special.
75    */
76   GNUNET_DHT_RO_NONE = 0,
77
78   /**
79    * Each peer along the way should look at 'enc' (otherwise
80    * only the k-peers closest to the key should look at it).
81    */
82   GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE = 1,
83
84   /**
85    * We should keep track of the route that the message
86    * took in the P2P network.
87    */
88   GNUNET_DHT_RO_RECORD_ROUTE = 2,
89
90   /**
91    * This is a 'FIND-PEER' request, so approximate results are fine.
92    */
93   GNUNET_DHT_RO_FIND_PEER = 4,
94
95   /**
96    * Possible message option for query key randomization.
97    */
98   GNUNET_DHT_RO_BART = 8,
99
100   /**
101    * Flag given to monitors if this was the last hop for a GET/PUT.
102    */
103   GNUNET_DHT_RO_LAST_HOP = 16
104 };
105
106
107 /**
108  * Initialize the connection with the DHT service.
109  *
110  * @param cfg configuration to use
111  * @param ht_len size of the internal hash table to use for
112  *               processing multiple GET/FIND requests in parallel
113  * @return NULL on error
114  */
115 struct GNUNET_DHT_Handle *
116 GNUNET_DHT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
117                     unsigned int ht_len);
118
119
120 /**
121  * Shutdown connection with the DHT service.
122  *
123  * @param handle connection to shut down
124  */
125 void
126 GNUNET_DHT_disconnect (struct GNUNET_DHT_Handle *handle);
127
128
129 /* *************** Standard API: get and put ******************* */
130
131
132 /**
133  * Opaque handle to cancel a PUT operation.
134  */
135 struct GNUNET_DHT_PutHandle;
136
137
138 /**
139  * Type of a PUT continuation.  You must not call
140  * #GNUNET_DHT_disconnect in this continuation.
141  *
142  * @param cls closure
143  * @param success #GNUNET_OK if the PUT was transmitted,
144  *                #GNUNET_NO on timeout,
145  *                #GNUNET_SYSERR on disconnect from service
146  *                after the PUT message was transmitted
147  *                (so we don't know if it was received or not)
148  */
149 typedef void (*GNUNET_DHT_PutContinuation)(void *cls,
150                                            int success);
151
152
153 /**
154  * Perform a PUT operation storing data in the DHT.
155  *
156  * @param handle handle to DHT service
157  * @param key the key to store under
158  * @param desired_replication_level estimate of how many
159  *                nearest peers this request should reach
160  * @param options routing options for this message
161  * @param type type of the value
162  * @param size number of bytes in @a data; must be less than 64k
163  * @param data the data to store
164  * @param exp desired expiration time for the value
165  * @param timeout how long to wait for transmission of this request
166  * @param cont continuation to call when done (transmitting request to service)
167  *        You must not call #GNUNET_DHT_disconnect in this continuation
168  * @param cont_cls closure for @a cont
169  * @return handle to cancel the "PUT" operation, NULL on error
170  *        (size too big)
171  */
172 struct GNUNET_DHT_PutHandle *
173 GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle,
174                 const struct GNUNET_HashCode *key,
175                 uint32_t desired_replication_level,
176                 enum GNUNET_DHT_RouteOption options,
177                 enum GNUNET_BLOCK_Type type,
178                 size_t size, const void *data,
179                 struct GNUNET_TIME_Absolute exp,
180                 struct GNUNET_TIME_Relative timeout,
181                 GNUNET_DHT_PutContinuation cont,
182                 void *cont_cls);
183
184
185 /**
186  * Cancels a DHT PUT operation.  Note that the PUT request may still
187  * go out over the network (we can't stop that); However, if the PUT
188  * has not yet been sent to the service, cancelling the PUT will stop
189  * this from happening (but there is no way for the user of this API
190  * to tell if that is the case).  The only use for this API is to
191  * prevent a later call to 'cont' from #GNUNET_DHT_put (i.e. because
192  * the system is shutting down).
193  *
194  * @param ph put operation to cancel ('cont' will no longer be called)
195  */
196 void
197 GNUNET_DHT_put_cancel (struct GNUNET_DHT_PutHandle *ph);
198
199
200 /**
201  * Iterator called on each result obtained for a DHT
202  * operation that expects a reply
203  *
204  * @param cls closure
205  * @param exp when will this value expire
206  * @param key key of the result
207  * @param get_path peers on reply path (or NULL if not recorded)
208  *                 [0] = datastore's first neighbor, [length - 1] = local peer
209  * @param get_path_length number of entries in @a get_path
210  * @param put_path peers on the PUT path (or NULL if not recorded)
211  *                 [0] = origin, [length - 1] = datastore
212  * @param put_path_length number of entries in @a put_path
213  * @param type type of the result
214  * @param size number of bytes in @a data
215  * @param data pointer to the result data
216  */
217 typedef void (*GNUNET_DHT_GetIterator) (void *cls,
218                                         struct GNUNET_TIME_Absolute exp,
219                                         const struct GNUNET_HashCode *key,
220                                         const struct GNUNET_PeerIdentity *get_path,
221                                         unsigned int get_path_length,
222                                         const struct GNUNET_PeerIdentity *put_path,
223                                         unsigned int put_path_length,
224                                         enum GNUNET_BLOCK_Type type,
225                                         size_t size, const void *data);
226
227
228 /**
229  * Perform an asynchronous GET operation on the DHT identified. See
230  * also #GNUNET_BLOCK_evaluate.
231  *
232  * @param handle handle to the DHT service
233  * @param type expected type of the response object
234  * @param key the key to look up
235  * @param desired_replication_level estimate of how many
236                   nearest peers this request should reach
237  * @param options routing options for this message
238  * @param xquery extended query data (can be NULL, depending on type)
239  * @param xquery_size number of bytes in @a xquery
240  * @param iter function to call on each result
241  * @param iter_cls closure for @a iter
242  *
243  * @return handle to stop the async get
244  */
245 struct GNUNET_DHT_GetHandle *
246 GNUNET_DHT_get_start (struct GNUNET_DHT_Handle *handle,
247                       enum GNUNET_BLOCK_Type type,
248                       const struct GNUNET_HashCode *key,
249                       uint32_t desired_replication_level,
250                       enum GNUNET_DHT_RouteOption options,
251                       const void *xquery, size_t xquery_size,
252                       GNUNET_DHT_GetIterator iter, void *iter_cls);
253
254
255 /**
256  * Tell the DHT not to return any of the following known results
257  * to this client.
258  *
259  * @param get_handle get operation for which results should be filtered
260  * @param num_results number of results to be blocked that are
261  *        provided in this call (size of the @a results array)
262  * @param results array of hash codes over the 'data' of the results
263  *        to be blocked
264  */
265 void
266 GNUNET_DHT_get_filter_known_results (struct GNUNET_DHT_GetHandle *get_handle,
267                                      unsigned int num_results,
268                                      const struct GNUNET_HashCode *results);
269
270 /**
271  * Stop async DHT-get.  Frees associated resources.
272  *
273  * @param get_handle GET operation to stop.
274  *
275  * On return get_handle will no longer be valid, caller
276  * must not use again!!!
277  */
278 void
279 GNUNET_DHT_get_stop (struct GNUNET_DHT_GetHandle *get_handle);
280
281
282 /* *************** Extended API: monitor ******************* */
283
284 /**
285  * Handle to monitor requests
286  */
287 struct GNUNET_DHT_MonitorHandle;
288
289 /**
290  * Callback called on each GET request going through the DHT.
291  *
292  * @param cls Closure.
293  * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
294  * @param type The type of data in the request.
295  * @param hop_count Hop count so far.
296  * @param path_length number of entries in @a path (or 0 if not recorded).
297  * @param path peers on the GET path (or NULL if not recorded).
298  * @param desired_replication_level Desired replication level.
299  * @param key Key of the requested data.
300  */
301 typedef void (*GNUNET_DHT_MonitorGetCB) (void *cls,
302                                          enum GNUNET_DHT_RouteOption options,
303                                          enum GNUNET_BLOCK_Type type,
304                                          uint32_t hop_count,
305                                          uint32_t desired_replication_level,
306                                          unsigned int path_length,
307                                          const struct GNUNET_PeerIdentity *path,
308                                          const struct GNUNET_HashCode * key);
309
310 /**
311  * Callback called on each GET reply going through the DHT.
312  *
313  * @param cls Closure.
314  * @param type The type of data in the result.
315  * @param get_path Peers on GET path (or NULL if not recorded).
316  * @param get_path_length number of entries in @a get_path.
317  * @param put_path peers on the PUT path (or NULL if not recorded).
318  * @param put_path_length number of entries in @a get_path.
319  * @param exp Expiration time of the data.
320  * @param key Key of the data.
321  * @param data Pointer to the result data.
322  * @param size Number of bytes in @a data.
323  */
324 typedef void (*GNUNET_DHT_MonitorGetRespCB) (void *cls,
325                                              enum GNUNET_BLOCK_Type type,
326                                              const struct GNUNET_PeerIdentity *get_path,
327                                              unsigned int get_path_length,
328                                              const struct GNUNET_PeerIdentity *put_path,
329                                              unsigned int put_path_length,
330                                              struct GNUNET_TIME_Absolute exp,
331                                              const struct GNUNET_HashCode *key,
332                                              const void *data,
333                                              size_t size);
334
335 /**
336  * Callback called on each PUT request going through the DHT.
337  *
338  * @param cls Closure.
339  * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
340  * @param type The type of data in the request.
341  * @param hop_count Hop count so far.
342  * @param path_length number of entries in @a path (or 0 if not recorded).
343  * @param path peers on the PUT path (or NULL if not recorded).
344  * @param desired_replication_level Desired replication level.
345  * @param exp Expiration time of the data.
346  * @param key Key under which data is to be stored.
347  * @param data Pointer to the data carried.
348  * @param size Number of bytes in data.
349  */
350 typedef void (*GNUNET_DHT_MonitorPutCB) (void *cls,
351                                          enum GNUNET_DHT_RouteOption options,
352                                          enum GNUNET_BLOCK_Type type,
353                                          uint32_t hop_count,
354                                          uint32_t desired_replication_level,
355                                          unsigned int path_length,
356                                          const struct GNUNET_PeerIdentity *path,
357                                          struct GNUNET_TIME_Absolute exp,
358                                          const struct GNUNET_HashCode *key,
359                                          const void *data,
360                                          size_t size);
361
362 /**
363  * Start monitoring the local DHT service.
364  *
365  * @param handle Handle to the DHT service.
366  * @param type Type of blocks that are of interest.
367  * @param key Key of data of interest, NULL for all.
368  * @param get_cb Callback to process monitored get messages.
369  * @param get_resp_cb Callback to process monitored get response messages.
370  * @param put_cb Callback to process monitored put messages.
371  * @param cb_cls Closure for callbacks
372  * @return Handle to stop monitoring.
373  */
374 struct GNUNET_DHT_MonitorHandle *
375 GNUNET_DHT_monitor_start (struct GNUNET_DHT_Handle *handle,
376                           enum GNUNET_BLOCK_Type type,
377                           const struct GNUNET_HashCode *key,
378                           GNUNET_DHT_MonitorGetCB get_cb,
379                           GNUNET_DHT_MonitorGetRespCB get_resp_cb,
380                           GNUNET_DHT_MonitorPutCB put_cb,
381                           void *cb_cls);
382
383
384 /**
385  * Stop monitoring.
386  * On return handle will no longer be valid, caller must not use again!!!
387  *
388  * @param handle The handle to the monitor request returned by monitor_start.
389  */
390 void
391 GNUNET_DHT_monitor_stop (struct GNUNET_DHT_MonitorHandle *handle);
392
393
394 #if ENABLE_MALICIOUS
395 /**
396  * Type of a Malicious continuation.  You must not call
397  * #GNUNET_DHT_disconnect in this continuation.
398  *
399  * @param cls closure
400  * @param success #GNUNET_OK if the set malicious request was transmitted,
401  *                #GNUNET_NO on timeout,
402  *                #GNUNET_SYSERR on disconnect from service
403  *                after the PUT message was transmitted
404  *                (so we don't know if it was received or not)
405  */
406 typedef void (*GNUNET_DHT_ActMaliciousContinuation)(void *cls,
407                                                  int success);
408
409 /**
410  * Turn the DHT service to act malicious
411  *
412  * @param handle the DHT handle
413  * @param action 1 to make the service malicious; 0 to make it benign
414           FIXME: perhaps make this an enum of known malicious behaviors?
415  */
416 struct GNUNET_DHT_ActMaliciousHandle *
417 GNUNET_DHT_act_malicious (struct GNUNET_DHT_Handle *handle, 
418                       unsigned int action, 
419                       GNUNET_DHT_PutContinuation cont,
420                       void *cont_cls);
421 #endif
422
423
424 #if 0                           /* keep Emacsens' auto-indent happy */
425 {
426 #endif
427 #ifdef __cplusplus
428 }
429 #endif
430
431 /** @} */ /* end of group dht */
432
433
434 #endif
435 /* gnunet_dht_service.h */