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