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