introducing GNUNET_CRYPTO_ecdhe_create2() to avoid malloc nonsense
[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
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
156 (*GNUNET_DHT_PutContinuation)(void *cls,
157                               int success);
158
159
160 /**
161  * Perform a PUT operation storing data in the DHT.
162  *
163  * @param handle handle to DHT service
164  * @param key the key to store under
165  * @param desired_replication_level estimate of how many
166  *                nearest peers this request should reach
167  * @param options routing options for this message
168  * @param type type of the value
169  * @param size number of bytes in @a data; must be less than 64k
170  * @param data the data to store
171  * @param exp desired expiration time for the value
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,
185                 const void *data,
186                 struct GNUNET_TIME_Absolute exp,
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
224 (*GNUNET_DHT_GetIterator) (void *cls,
225                            struct GNUNET_TIME_Absolute exp,
226                            const struct GNUNET_HashCode *key,
227                            const struct GNUNET_PeerIdentity *get_path,
228                            unsigned int get_path_length,
229                            const struct GNUNET_PeerIdentity *put_path,
230                            unsigned int put_path_length,
231                            enum GNUNET_BLOCK_Type type,
232                            size_t size,
233                            const void *data);
234
235
236 /**
237  * Perform an asynchronous GET operation on the DHT identified. See
238  * also #GNUNET_BLOCK_evaluate.
239  *
240  * @param handle handle to the DHT service
241  * @param type expected type of the response object
242  * @param key the key to look up
243  * @param desired_replication_level estimate of how many
244                   nearest peers this request should reach
245  * @param options routing options for this message
246  * @param xquery extended query data (can be NULL, depending on type)
247  * @param xquery_size number of bytes in @a xquery
248  * @param iter function to call on each result
249  * @param iter_cls closure for @a iter
250  * @return handle to stop the async get
251  */
252 struct GNUNET_DHT_GetHandle *
253 GNUNET_DHT_get_start (struct GNUNET_DHT_Handle *handle,
254                       enum GNUNET_BLOCK_Type type,
255                       const struct GNUNET_HashCode *key,
256                       uint32_t desired_replication_level,
257                       enum GNUNET_DHT_RouteOption options,
258                       const void *xquery,
259                       size_t xquery_size,
260                       GNUNET_DHT_GetIterator iter,
261                       void *iter_cls);
262
263
264 /**
265  * Tell the DHT not to return any of the following known results
266  * to this client.
267  *
268  * @param get_handle get operation for which results should be filtered
269  * @param num_results number of results to be blocked that are
270  *        provided in this call (size of the @a results array)
271  * @param results array of hash codes over the `data` of the results
272  *        to be blocked
273  */
274 void
275 GNUNET_DHT_get_filter_known_results (struct GNUNET_DHT_GetHandle *get_handle,
276                                      unsigned int num_results,
277                                      const struct GNUNET_HashCode *results);
278
279 /**
280  * Stop async DHT-get.  Frees associated resources.
281  *
282  * @param get_handle GET operation to stop.
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
308 (*GNUNET_DHT_MonitorGetCB) (void *cls,
309                             enum GNUNET_DHT_RouteOption options,
310                             enum GNUNET_BLOCK_Type type,
311                             uint32_t hop_count,
312                             uint32_t desired_replication_level,
313                             unsigned int path_length,
314                             const struct GNUNET_PeerIdentity *path,
315                             const struct GNUNET_HashCode *key);
316
317
318 /**
319  * Callback called on each GET reply going through the DHT.
320  *
321  * @param cls Closure.
322  * @param type The type of data in the result.
323  * @param get_path Peers on GET path (or NULL if not recorded).
324  * @param get_path_length number of entries in @a get_path.
325  * @param put_path peers on the PUT path (or NULL if not recorded).
326  * @param put_path_length number of entries in @a get_path.
327  * @param exp Expiration time of the data.
328  * @param key Key of the data.
329  * @param data Pointer to the result data.
330  * @param size Number of bytes in @a data.
331  */
332 typedef void
333 (*GNUNET_DHT_MonitorGetRespCB) (void *cls,
334                                 enum GNUNET_BLOCK_Type type,
335                                 const struct GNUNET_PeerIdentity *get_path,
336                                 unsigned int get_path_length,
337                                 const struct GNUNET_PeerIdentity *put_path,
338                                 unsigned int put_path_length,
339                                 struct GNUNET_TIME_Absolute exp,
340                                 const struct GNUNET_HashCode *key,
341                                 const void *data,
342                                 size_t size);
343
344
345 /**
346  * Callback called on each PUT request going through the DHT.
347  *
348  * @param cls Closure.
349  * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
350  * @param type The type of data in the request.
351  * @param hop_count Hop count so far.
352  * @param path_length number of entries in @a path (or 0 if not recorded).
353  * @param path peers on the PUT path (or NULL if not recorded).
354  * @param desired_replication_level Desired replication level.
355  * @param exp Expiration time of the data.
356  * @param key Key under which data is to be stored.
357  * @param data Pointer to the data carried.
358  * @param size Number of bytes in @a data.
359  */
360 typedef void
361 (*GNUNET_DHT_MonitorPutCB) (void *cls,
362                             enum GNUNET_DHT_RouteOption options,
363                             enum GNUNET_BLOCK_Type type,
364                             uint32_t hop_count,
365                             uint32_t desired_replication_level,
366                             unsigned int path_length,
367                             const struct GNUNET_PeerIdentity *path,
368                             struct GNUNET_TIME_Absolute exp,
369                             const struct GNUNET_HashCode *key,
370                             const void *data,
371                             size_t size);
372
373
374
375 /**
376  * Start monitoring the local DHT service.
377  *
378  * @param handle Handle to the DHT service.
379  * @param type Type of blocks that are of interest.
380  * @param key Key of data of interest, NULL for all.
381  * @param get_cb Callback to process monitored get messages.
382  * @param get_resp_cb Callback to process monitored get response messages.
383  * @param put_cb Callback to process monitored put messages.
384  * @param cb_cls Closure for callbacks
385  * @return Handle to stop monitoring.
386  */
387 struct GNUNET_DHT_MonitorHandle *
388 GNUNET_DHT_monitor_start (struct GNUNET_DHT_Handle *handle,
389                           enum GNUNET_BLOCK_Type type,
390                           const struct GNUNET_HashCode *key,
391                           GNUNET_DHT_MonitorGetCB get_cb,
392                           GNUNET_DHT_MonitorGetRespCB get_resp_cb,
393                           GNUNET_DHT_MonitorPutCB put_cb,
394                           void *cb_cls);
395
396
397 /**
398  * Stop monitoring.
399  * On return handle will no longer be valid, caller must not use it anymore.
400  *
401  * @param handle The handle to the monitor request returned by
402  *         #GNUNET_DHT_monitor_start().
403  */
404 void
405 GNUNET_DHT_monitor_stop (struct GNUNET_DHT_MonitorHandle *handle);
406
407
408 #if 0                           /* keep Emacsens' auto-indent happy */
409 {
410 #endif
411 #ifdef __cplusplus
412 }
413 #endif
414
415 #endif
416
417 /** @} */  /* end of group dht */