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