f4c68bab6b19851b311c48bd11e7486165a20171
[oweals/gnunet.git] / src / include / gnunet_dht_service.h
1 /*
2       This file is part of GNUnet
3       (C) 2004, 2005, 2006, 2008, 2009 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 2, 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
32 #ifdef __cplusplus
33 extern "C"
34 {
35 #if 0                           /* keep Emacsens' auto-indent happy */
36 }
37 #endif
38 #endif
39
40
41 /**
42  * Connection to the DHT service.
43  */
44 struct GNUNET_DHT_Handle;
45
46 /**
47  * Handle to control a route operation.
48  */
49 struct GNUNET_DHT_RouteHandle;
50
51 /**
52  * Handle to control a get operation.
53  */
54 struct GNUNET_DHT_GetHandle;
55
56 /**
57  * Handle to control a find peer operation.
58  */
59 struct GNUNET_DHT_FindPeerHandle;
60
61
62 /**
63  * Iterator called on each result obtained from a generic route
64  * operation
65  */
66 typedef void (*GNUNET_DHT_MessageCallback)(void *cls,
67                                            int code);
68
69 /**
70  * Initialize the connection with the DHT service.
71  *
72  * @param sched scheduler to use
73  * @param cfg configuration to use
74  * @param ht_len size of the internal hash table to use for
75  *               processing multiple GET/FIND requests in parallel
76  * @return NULL on error
77  */
78 struct GNUNET_DHT_Handle *
79 GNUNET_DHT_connect (struct GNUNET_SCHEDULER_Handle *sched,
80                     const struct GNUNET_CONFIGURATION_Handle *cfg,
81                     unsigned int ht_len);
82
83
84 /**
85  * Shutdown connection with the DHT service.
86  *
87  * @param handle connection to shut down
88  */
89 void
90 GNUNET_DHT_disconnect (struct GNUNET_DHT_Handle *handle);
91
92
93 /**
94  * Perform a PUT operation on the DHT identified by 'table' storing
95  * a binding of 'key' to 'value'.  The peer does not have to be part
96  * of the table (if so, we will attempt to locate a peer that is!)
97  *
98  * @param handle handle to DHT service
99  * @param key the key to store under
100  * @param type type of the value
101  * @param size number of bytes in data; must be less than 64k
102  * @param data the data to store
103  * @param exp desired expiration time for the data
104  * @param timeout when to abort with an error if we fail to get
105  *                a confirmation for the PUT from the local DHT service
106  * @param cont continuation to call when done;
107  *             reason will be TIMEOUT on error,
108  *             reason will be PREREQ_DONE on success
109  * @param cont_cls closure for cont
110  */
111 void
112 GNUNET_DHT_put (struct GNUNET_DHT_Handle *handle,
113                 const GNUNET_HashCode * key,
114                 uint32_t type,
115                 uint32_t size,
116                 const char *data,
117                 struct GNUNET_TIME_Absolute exp,
118                 struct GNUNET_TIME_Relative timeout,
119                 GNUNET_SCHEDULER_Task cont,
120                 void *cont_cls);
121
122
123 /**
124  * Iterator called on each result obtained for a DHT
125  * operation that expects a reply
126  *
127  * @param cls closure
128  * @param exp when will this value expire
129  * @param key key of the result
130  * @param type type of the result
131  * @param size number of bytes in data
132  * @param data pointer to the result data
133  */
134 typedef void (*GNUNET_DHT_GetIterator)(void *cls,
135                                     struct GNUNET_TIME_Absolute exp,
136                                     const GNUNET_HashCode * key,
137                                     uint32_t type,
138                                     uint32_t size,
139                                     const void *data);
140
141
142
143 /**
144  * Perform an asynchronous GET operation on the DHT.
145  *
146  * @param handle handle to the DHT service
147  * @param timeout timeout for this request to be sent to the
148  *        service
149  * @param type expected type of the response object
150  * @param key the key to look up
151  * @param iter function to call on each result
152  * @param iter_cls closure for iter
153  * @param cont continuation to call once message sent
154  * @param cont_cls closure for continuation
155  *
156  * @return handle to stop the async get, NULL on error
157  */
158 struct GNUNET_DHT_GetHandle *
159 GNUNET_DHT_get_start (struct GNUNET_DHT_Handle *handle,
160                       struct GNUNET_TIME_Relative timeout,
161                       uint32_t type,
162                       const GNUNET_HashCode * key,
163                       GNUNET_DHT_GetIterator iter,
164                       void *iter_cls,
165                       GNUNET_SCHEDULER_Task cont,
166                       void *cont_cls);
167
168 /**
169  * Stop async DHT-get.  Frees associated resources.
170  *
171  * @param get_handle GET operation to stop.
172  */
173 void
174 GNUNET_DHT_get_stop (struct GNUNET_DHT_GetHandle *get_handle, GNUNET_SCHEDULER_Task cont, void *cont_cls);
175
176
177 /**
178  * Options for routing.
179  */
180 enum GNUNET_DHT_RouteOption
181   {
182     /**
183      * Default.  Do nothing special.
184      */
185     GNUNET_DHT_RO_NONE = 0,
186
187     /**
188      * Each peer along the way should look at 'enc' (otherwise
189      * only the k-peers closest to the key should look at it).
190      */
191     GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE = 1
192   };
193
194
195 /**
196  * Iterator called on each result obtained from a find peer
197  * operation
198  *
199  * @param cls closure
200  * @param reply response
201  */
202 typedef void (*GNUNET_DHT_FindPeerProcessor)(void *cls,
203                                           const struct GNUNET_PeerIdentity *peer,
204                                           const struct GNUNET_MessageHeader *reply);
205
206
207 /**
208  * Perform an asynchronous FIND PEER operation on the DHT.
209  *
210  * @param handle handle to the DHT service
211  * @param timeout timeout for this request to be sent to the
212  *        service
213  * @param options routing options for this message
214  * @param message a message to inject at found peers (may be null)
215  * @param key the key to look up
216  * @param proc function to call on each result
217  * @param proc_cls closure for proc
218  * @param cont continuation to call once message sent
219  * @param cont_cls closure for continuation
220  *
221  * @return handle to stop the async get, NULL on error
222  */
223 struct GNUNET_DHT_FindPeerHandle *
224 GNUNET_DHT_find_peer_start (struct GNUNET_DHT_Handle *handle,
225                       struct GNUNET_TIME_Relative timeout,
226                       enum GNUNET_DHT_RouteOption options,
227                       struct GNUNET_MessageHeader *message,
228                       const GNUNET_HashCode * key,
229                       GNUNET_DHT_FindPeerProcessor proc,
230                       void *proc_cls,
231                       GNUNET_SCHEDULER_Task cont,
232                       void *cont_cls);
233
234 /**
235  * Stop async find peer.  Frees associated resources.
236  *
237  * @param find_peer_handle GET operation to stop.
238  */
239 void
240 GNUNET_DHT_find_peer_stop (struct GNUNET_DHT_FindPeerHandle *find_peer_handle, GNUNET_SCHEDULER_Task cont, void *cont_cls);
241
242 /**
243  * Iterator called on each result obtained from a generic route
244  * operation
245  */
246 typedef void (*GNUNET_DHT_ReplyProcessor)(void *cls,
247                                           const struct GNUNET_MessageHeader *reply);
248
249 /**
250  * Perform an asynchronous FIND_PEER operation on the DHT.
251  *
252  * @param handle handle to the DHT service
253  * @param key the key to look up
254  * @param desired_replication_level how many peers should ultimately receive
255  *                this message (advisory only, target may be too high for the
256  *                given DHT or not hit exactly).
257  * @param options options for routing
258  * @param enc send the encapsulated message to a peer close to the key
259  * @param timeout when to abort with an error if we fail to get
260  *                a confirmation for the request (when necessary) or how long
261  *                to wait for transmission to the service
262  * @param iter function to call on each result, NULL if no replies are expected
263  * @param iter_cls closure for iter
264
265  * @param cont continuation to call when done, GNUNET_SYSERR if failed
266  *             GNUNET_OK otherwise
267  * @param cont_cls closure for cont
268  * @return handle to stop the request
269  */
270 struct GNUNET_DHT_RouteHandle *
271 GNUNET_DHT_route_start (struct GNUNET_DHT_Handle *handle,
272                         const GNUNET_HashCode *key,
273                         unsigned int desired_replication_level,
274                         enum GNUNET_DHT_RouteOption options,
275                         const struct GNUNET_MessageHeader *enc,
276                         struct GNUNET_TIME_Relative timeout,
277                         GNUNET_DHT_ReplyProcessor iter,
278                         void *iter_cls,
279                         GNUNET_SCHEDULER_Task cont,
280                         void *cont_cls);
281
282 void
283 GNUNET_DHT_route_stop (struct GNUNET_DHT_RouteHandle *route_handle, GNUNET_SCHEDULER_Task cont, void *cont_cls);
284
285
286 #if 0                           /* keep Emacsens' auto-indent happy */
287 {
288 #endif
289 #ifdef __cplusplus
290 }
291 #endif
292
293
294 #endif
295 /* gnunet_dht_service.h */