uncrustify as demanded.
[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(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    * Default.  Do nothing special.
80    */
81   GNUNET_DHT_RO_NONE = 0,
82
83   /**
84    * Each peer along the way should look at 'enc' (otherwise
85    * only the k-peers closest to the key should look at it).
86    */
87   GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE = 1,
88
89   /**
90    * We should keep track of the route that the message
91    * took in the P2P network.
92    */
93   GNUNET_DHT_RO_RECORD_ROUTE = 2,
94
95   /**
96    * This is a 'FIND-PEER' request, so approximate results are fine.
97    */
98   GNUNET_DHT_RO_FIND_PEER = 4,
99
100   /**
101    * Possible message option for query key randomization.
102    */
103   GNUNET_DHT_RO_BART = 8,
104
105   /**
106    * Flag given to monitors if this was the last hop for a GET/PUT.
107    */
108   GNUNET_DHT_RO_LAST_HOP = 16
109 };
110
111
112 /**
113  * Initialize the connection with the DHT service.
114  *
115  * @param cfg configuration to use
116  * @param ht_len size of the internal hash table to use for
117  *               processing multiple GET/FIND requests in parallel
118  * @return NULL on error
119  */
120 struct GNUNET_DHT_Handle *
121 GNUNET_DHT_connect(const struct GNUNET_CONFIGURATION_Handle *cfg,
122                    unsigned int ht_len);
123
124
125 /**
126  * Shutdown connection with the DHT service.
127  *
128  * @param handle connection to shut down
129  */
130 void
131 GNUNET_DHT_disconnect(struct GNUNET_DHT_Handle *handle);
132
133
134 /* *************** Standard API: get and put ******************* */
135
136
137 /**
138  * Opaque handle to cancel a PUT operation.
139  */
140 struct GNUNET_DHT_PutHandle;
141
142
143 /**
144  * Perform a PUT operation storing data in the DHT.
145  *
146  * @param handle handle to DHT service
147  * @param key the key to store under
148  * @param desired_replication_level estimate of how many
149  *                nearest peers this request should reach
150  * @param options routing options for this message
151  * @param type type of the value
152  * @param size number of bytes in @a data; must be less than 64k
153  * @param data the data to store
154  * @param exp desired expiration time for the value
155  * @param cont continuation to call when done (transmitting request to service)
156  *        You must not call #GNUNET_DHT_disconnect in this continuation
157  * @param cont_cls closure for @a cont
158  * @return handle to cancel the "PUT" operation, NULL on error
159  *        (size too big)
160  */
161 struct GNUNET_DHT_PutHandle *
162 GNUNET_DHT_put(struct GNUNET_DHT_Handle *handle,
163                const struct GNUNET_HashCode *key,
164                uint32_t desired_replication_level,
165                enum GNUNET_DHT_RouteOption options,
166                enum GNUNET_BLOCK_Type type,
167                size_t size,
168                const void *data,
169                struct GNUNET_TIME_Absolute exp,
170                GNUNET_SCHEDULER_TaskCallback cont,
171                void *cont_cls);
172
173
174 /**
175  * Cancels a DHT PUT operation.  Note that the PUT request may still
176  * go out over the network (we can't stop that); However, if the PUT
177  * has not yet been sent to the service, cancelling the PUT will stop
178  * this from happening (but there is no way for the user of this API
179  * to tell if that is the case).  The only use for this API is to
180  * prevent a later call to 'cont' from #GNUNET_DHT_put (i.e. because
181  * the system is shutting down).
182  *
183  * @param ph put operation to cancel ('cont' will no longer be called)
184  */
185 void
186 GNUNET_DHT_put_cancel(struct GNUNET_DHT_PutHandle *ph);
187
188
189 /**
190  * Iterator called on each result obtained for a DHT
191  * operation that expects a reply
192  *
193  * @param cls closure
194  * @param exp when will this value expire
195  * @param key key of the result
196  * @param get_path peers on reply path (or NULL if not recorded)
197  *                 [0] = datastore's first neighbor, [length - 1] = local peer
198  * @param get_path_length number of entries in @a get_path
199  * @param put_path peers on the PUT path (or NULL if not recorded)
200  *                 [0] = origin, [length - 1] = datastore
201  * @param put_path_length number of entries in @a put_path
202  * @param type type of the result
203  * @param size number of bytes in @a data
204  * @param data pointer to the result data
205  */
206 typedef void
207 (*GNUNET_DHT_GetIterator) (void *cls,
208                            struct GNUNET_TIME_Absolute exp,
209                            const struct GNUNET_HashCode *key,
210                            const struct GNUNET_PeerIdentity *get_path,
211                            unsigned int get_path_length,
212                            const struct GNUNET_PeerIdentity *put_path,
213                            unsigned int put_path_length,
214                            enum GNUNET_BLOCK_Type type,
215                            size_t size,
216                            const void *data);
217
218
219 /**
220  * Perform an asynchronous GET operation on the DHT identified. See
221  * also #GNUNET_BLOCK_evaluate.
222  *
223  * @param handle handle to the DHT service
224  * @param type expected type of the response object
225  * @param key the key to look up
226  * @param desired_replication_level estimate of how many
227                   nearest peers this request should reach
228  * @param options routing options for this message
229  * @param xquery extended query data (can be NULL, depending on type)
230  * @param xquery_size number of bytes in @a xquery
231  * @param iter function to call on each result
232  * @param iter_cls closure for @a iter
233  * @return handle to stop the async get
234  */
235 struct GNUNET_DHT_GetHandle *
236 GNUNET_DHT_get_start(struct GNUNET_DHT_Handle *handle,
237                      enum GNUNET_BLOCK_Type type,
238                      const struct GNUNET_HashCode *key,
239                      uint32_t desired_replication_level,
240                      enum GNUNET_DHT_RouteOption options,
241                      const void *xquery,
242                      size_t xquery_size,
243                      GNUNET_DHT_GetIterator iter,
244                      void *iter_cls);
245
246
247 /**
248  * Tell the DHT not to return any of the following known results
249  * to this client.
250  *
251  * @param get_handle get operation for which results should be filtered
252  * @param num_results number of results to be blocked that are
253  *        provided in this call (size of the @a results array)
254  * @param results array of hash codes over the `data` of the results
255  *        to be blocked
256  */
257 void
258 GNUNET_DHT_get_filter_known_results(struct GNUNET_DHT_GetHandle *get_handle,
259                                     unsigned int num_results,
260                                     const struct GNUNET_HashCode *results);
261
262 /**
263  * Stop async DHT-get.  Frees associated resources.
264  *
265  * @param get_handle GET operation to stop.
266  */
267 void
268 GNUNET_DHT_get_stop(struct GNUNET_DHT_GetHandle *get_handle);
269
270
271 /* *************** Extended API: monitor ******************* */
272
273 /**
274  * Handle to monitor requests
275  */
276 struct GNUNET_DHT_MonitorHandle;
277
278 /**
279  * Callback called on each GET request going through the DHT.
280  *
281  * @param cls Closure.
282  * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
283  * @param type The type of data in the request.
284  * @param hop_count Hop count so far.
285  * @param path_length number of entries in @a path (or 0 if not recorded).
286  * @param path peers on the GET path (or NULL if not recorded).
287  * @param desired_replication_level Desired replication level.
288  * @param key Key of the requested data.
289  */
290 typedef void
291 (*GNUNET_DHT_MonitorGetCB) (void *cls,
292                             enum GNUNET_DHT_RouteOption options,
293                             enum GNUNET_BLOCK_Type type,
294                             uint32_t hop_count,
295                             uint32_t desired_replication_level,
296                             unsigned int path_length,
297                             const struct GNUNET_PeerIdentity *path,
298                             const struct GNUNET_HashCode *key);
299
300
301 /**
302  * Callback called on each GET reply going through the DHT.
303  *
304  * @param cls Closure.
305  * @param type The type of data in the result.
306  * @param get_path Peers on GET path (or NULL if not recorded).
307  * @param get_path_length number of entries in @a get_path.
308  * @param put_path peers on the PUT path (or NULL if not recorded).
309  * @param put_path_length number of entries in @a get_path.
310  * @param exp Expiration time of the data.
311  * @param key Key of the data.
312  * @param data Pointer to the result data.
313  * @param size Number of bytes in @a data.
314  */
315 typedef void
316 (*GNUNET_DHT_MonitorGetRespCB) (void *cls,
317                                 enum GNUNET_BLOCK_Type type,
318                                 const struct GNUNET_PeerIdentity *get_path,
319                                 unsigned int get_path_length,
320                                 const struct GNUNET_PeerIdentity *put_path,
321                                 unsigned int put_path_length,
322                                 struct GNUNET_TIME_Absolute exp,
323                                 const struct GNUNET_HashCode *key,
324                                 const void *data,
325                                 size_t size);
326
327
328 /**
329  * Callback called on each PUT request going through the DHT.
330  *
331  * @param cls Closure.
332  * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
333  * @param type The type of data in the request.
334  * @param hop_count Hop count so far.
335  * @param path_length number of entries in @a path (or 0 if not recorded).
336  * @param path peers on the PUT path (or NULL if not recorded).
337  * @param desired_replication_level Desired replication level.
338  * @param exp Expiration time of the data.
339  * @param key Key under which data is to be stored.
340  * @param data Pointer to the data carried.
341  * @param size Number of bytes in @a data.
342  */
343 typedef void
344 (*GNUNET_DHT_MonitorPutCB) (void *cls,
345                             enum GNUNET_DHT_RouteOption options,
346                             enum GNUNET_BLOCK_Type type,
347                             uint32_t hop_count,
348                             uint32_t desired_replication_level,
349                             unsigned int path_length,
350                             const struct GNUNET_PeerIdentity *path,
351                             struct GNUNET_TIME_Absolute exp,
352                             const struct GNUNET_HashCode *key,
353                             const void *data,
354                             size_t size);
355
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 */