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