tighten formatting rules
[oweals/gnunet.git] / src / include / gnunet_dht_service.h
1 /*
2       This file is part of GNUnet
3       Copyright (C) 2004-2013, 2016 GNUnet e.V.
4
5       GNUnet is free software: you can redistribute it and/or modify it
6       under the terms of the GNU Affero General Public License as published
7       by the Free Software Foundation, either version 3 of the License,
8       or (at your 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       Affero General Public License for more details.
14
15       You should have received a copy of the GNU Affero General Public License
16       along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
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 ( \
55     GNUNET_TIME_UNIT_MINUTES, 60)
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  * Perform a PUT operation storing data in the DHT.
146  *
147  * @param handle handle to DHT service
148  * @param key the key to store under
149  * @param desired_replication_level estimate of how many
150  *                nearest peers this request should reach
151  * @param options routing options for this message
152  * @param type type of the value
153  * @param size number of bytes in @a data; must be less than 64k
154  * @param data the data to store
155  * @param exp desired expiration time for the value
156  * @param cont continuation to call when done (transmitting request to service)
157  *        You must not call #GNUNET_DHT_disconnect in this continuation
158  * @param cont_cls closure for @a cont
159  * @return handle to cancel the "PUT" operation, NULL on error
160  *        (size too big)
161  */
162 struct GNUNET_DHT_PutHandle *
163 GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle,
164                 const struct GNUNET_HashCode *key,
165                 uint32_t desired_replication_level,
166                 enum GNUNET_DHT_RouteOption options,
167                 enum GNUNET_BLOCK_Type type,
168                 size_t size,
169                 const void *data,
170                 struct GNUNET_TIME_Absolute exp,
171                 GNUNET_SCHEDULER_TaskCallback cont,
172                 void *cont_cls);
173
174
175 /**
176  * Cancels a DHT PUT operation.  Note that the PUT request may still
177  * go out over the network (we can't stop that); However, if the PUT
178  * has not yet been sent to the service, cancelling the PUT will stop
179  * this from happening (but there is no way for the user of this API
180  * to tell if that is the case).  The only use for this API is to
181  * prevent a later call to 'cont' from #GNUNET_DHT_put (i.e. because
182  * the system is shutting down).
183  *
184  * @param ph put operation to cancel ('cont' will no longer be called)
185  */
186 void
187 GNUNET_DHT_put_cancel (struct GNUNET_DHT_PutHandle *ph);
188
189
190 /**
191  * Iterator called on each result obtained for a DHT
192  * operation that expects a reply
193  *
194  * @param cls closure
195  * @param exp when will this value expire
196  * @param key key of the result
197  * @param get_path peers on reply path (or NULL if not recorded)
198  *                 [0] = datastore's first neighbor, [length - 1] = local peer
199  * @param get_path_length number of entries in @a get_path
200  * @param put_path peers on the PUT path (or NULL if not recorded)
201  *                 [0] = origin, [length - 1] = datastore
202  * @param put_path_length number of entries in @a put_path
203  * @param type type of the result
204  * @param size number of bytes in @a data
205  * @param data pointer to the result data
206  */
207 typedef void
208 (*GNUNET_DHT_GetIterator) (void *cls,
209                            struct GNUNET_TIME_Absolute exp,
210                            const struct GNUNET_HashCode *key,
211                            const struct GNUNET_PeerIdentity *get_path,
212                            unsigned int get_path_length,
213                            const struct GNUNET_PeerIdentity *put_path,
214                            unsigned int put_path_length,
215                            enum GNUNET_BLOCK_Type type,
216                            size_t size,
217                            const void *data);
218
219
220 /**
221  * Perform an asynchronous GET operation on the DHT identified. See
222  * also #GNUNET_BLOCK_evaluate.
223  *
224  * @param handle handle to the DHT service
225  * @param type expected type of the response object
226  * @param key the key to look up
227  * @param desired_replication_level estimate of how many
228                   nearest peers this request should reach
229  * @param options routing options for this message
230  * @param xquery extended query data (can be NULL, depending on type)
231  * @param xquery_size number of bytes in @a xquery
232  * @param iter function to call on each result
233  * @param iter_cls closure for @a iter
234  * @return handle to stop the async get
235  */
236 struct GNUNET_DHT_GetHandle *
237 GNUNET_DHT_get_start (struct GNUNET_DHT_Handle *handle,
238                       enum GNUNET_BLOCK_Type type,
239                       const struct GNUNET_HashCode *key,
240                       uint32_t desired_replication_level,
241                       enum GNUNET_DHT_RouteOption options,
242                       const void *xquery,
243                       size_t xquery_size,
244                       GNUNET_DHT_GetIterator iter,
245                       void *iter_cls);
246
247
248 /**
249  * Tell the DHT not to return any of the following known results
250  * to this client.
251  *
252  * @param get_handle get operation for which results should be filtered
253  * @param num_results number of results to be blocked that are
254  *        provided in this call (size of the @a results array)
255  * @param results array of hash codes over the `data` of the results
256  *        to be blocked
257  */
258 void
259 GNUNET_DHT_get_filter_known_results (struct GNUNET_DHT_GetHandle *get_handle,
260                                      unsigned int num_results,
261                                      const struct GNUNET_HashCode *results);
262
263 /**
264  * Stop async DHT-get.  Frees associated resources.
265  *
266  * @param get_handle GET operation to stop.
267  */
268 void
269 GNUNET_DHT_get_stop (struct GNUNET_DHT_GetHandle *get_handle);
270
271
272 /* *************** Extended API: monitor ******************* */
273
274 /**
275  * Handle to monitor requests
276  */
277 struct GNUNET_DHT_MonitorHandle;
278
279 /**
280  * Callback called on each GET request going through the DHT.
281  *
282  * @param cls Closure.
283  * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
284  * @param type The type of data in the request.
285  * @param hop_count Hop count so far.
286  * @param path_length number of entries in @a path (or 0 if not recorded).
287  * @param path peers on the GET path (or NULL if not recorded).
288  * @param desired_replication_level Desired replication level.
289  * @param key Key of the requested data.
290  */
291 typedef void
292 (*GNUNET_DHT_MonitorGetCB) (void *cls,
293                             enum GNUNET_DHT_RouteOption options,
294                             enum GNUNET_BLOCK_Type type,
295                             uint32_t hop_count,
296                             uint32_t desired_replication_level,
297                             unsigned int path_length,
298                             const struct GNUNET_PeerIdentity *path,
299                             const struct GNUNET_HashCode *key);
300
301
302 /**
303  * Callback called on each GET reply going through the DHT.
304  *
305  * @param cls Closure.
306  * @param type The type of data in the result.
307  * @param get_path Peers on GET path (or NULL if not recorded).
308  * @param get_path_length number of entries in @a get_path.
309  * @param put_path peers on the PUT path (or NULL if not recorded).
310  * @param put_path_length number of entries in @a get_path.
311  * @param exp Expiration time of the data.
312  * @param key Key of the data.
313  * @param data Pointer to the result data.
314  * @param size Number of bytes in @a data.
315  */
316 typedef void
317 (*GNUNET_DHT_MonitorGetRespCB) (void *cls,
318                                 enum GNUNET_BLOCK_Type type,
319                                 const struct GNUNET_PeerIdentity *get_path,
320                                 unsigned int get_path_length,
321                                 const struct GNUNET_PeerIdentity *put_path,
322                                 unsigned int put_path_length,
323                                 struct GNUNET_TIME_Absolute exp,
324                                 const struct GNUNET_HashCode *key,
325                                 const void *data,
326                                 size_t size);
327
328
329 /**
330  * Callback called on each PUT request going through the DHT.
331  *
332  * @param cls Closure.
333  * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
334  * @param type The type of data in the request.
335  * @param hop_count Hop count so far.
336  * @param path_length number of entries in @a path (or 0 if not recorded).
337  * @param path peers on the PUT path (or NULL if not recorded).
338  * @param desired_replication_level Desired replication level.
339  * @param exp Expiration time of the data.
340  * @param key Key under which data is to be stored.
341  * @param data Pointer to the data carried.
342  * @param size Number of bytes in @a data.
343  */
344 typedef void
345 (*GNUNET_DHT_MonitorPutCB) (void *cls,
346                             enum GNUNET_DHT_RouteOption options,
347                             enum GNUNET_BLOCK_Type type,
348                             uint32_t hop_count,
349                             uint32_t desired_replication_level,
350                             unsigned int path_length,
351                             const struct GNUNET_PeerIdentity *path,
352                             struct GNUNET_TIME_Absolute exp,
353                             const struct GNUNET_HashCode *key,
354                             const void *data,
355                             size_t size);
356
357
358 /**
359  * Start monitoring the local DHT service.
360  *
361  * @param handle Handle to the DHT service.
362  * @param type Type of blocks that are of interest.
363  * @param key Key of data of interest, NULL for all.
364  * @param get_cb Callback to process monitored get messages.
365  * @param get_resp_cb Callback to process monitored get response messages.
366  * @param put_cb Callback to process monitored put messages.
367  * @param cb_cls Closure for callbacks
368  * @return Handle to stop monitoring.
369  */
370 struct GNUNET_DHT_MonitorHandle *
371 GNUNET_DHT_monitor_start (struct GNUNET_DHT_Handle *handle,
372                           enum GNUNET_BLOCK_Type type,
373                           const struct GNUNET_HashCode *key,
374                           GNUNET_DHT_MonitorGetCB get_cb,
375                           GNUNET_DHT_MonitorGetRespCB get_resp_cb,
376                           GNUNET_DHT_MonitorPutCB put_cb,
377                           void *cb_cls);
378
379
380 /**
381  * Stop monitoring.
382  * On return handle will no longer be valid, caller must not use it anymore.
383  *
384  * @param handle The handle to the monitor request returned by
385  *         #GNUNET_DHT_monitor_start().
386  */
387 void
388 GNUNET_DHT_monitor_stop (struct GNUNET_DHT_MonitorHandle *handle);
389
390
391 #if 0                           /* keep Emacsens' auto-indent happy */
392 {
393 #endif
394 #ifdef __cplusplus
395 }
396 #endif
397
398 #endif
399
400 /** @} */  /* end of group dht */