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