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