ffc7f1c5b0395574074ff9ba2275d1f91a4e33e6
[oweals/gnunet.git] / src / include / gnunet_set_service.h
1 /*
2       This file is part of GNUnet
3       (C) 2013 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_set_service.h
23  * @brief two-peer set operations
24  * @author Florian Dold
25  * @author Christian Grothoff
26  */
27
28 #ifndef GNUNET_SET_SERVICE_H
29 #define GNUNET_SET_SERVICE_H
30
31 #ifdef __cplusplus
32 extern "C"
33 {
34 #if 0                           /* keep Emacsens' auto-indent happy */
35 }
36 #endif
37 #endif
38
39 #include "gnunet_common.h"
40 #include "gnunet_time_lib.h"
41 #include "gnunet_configuration_lib.h"
42
43
44 /**
45  * Maximum size of a context message for set operation requests.
46  */
47 #define GNUNET_SET_CONTEXT_MESSAGE_MAX_SIZE ((1<<16) - 1024)
48
49
50 /**
51  * Opaque handle to a set.
52  */
53 struct GNUNET_SET_Handle;
54
55 /**
56  * Opaque handle to a set operation request from another peer.
57  */
58 struct GNUNET_SET_Request;
59
60 /**
61  * Opaque handle to a listen operation.
62  */
63 struct GNUNET_SET_ListenHandle;
64
65 /**
66  * Opaque handle to a set operation.
67  */
68 struct GNUNET_SET_OperationHandle;
69
70
71 /**
72  * The operation that a set set supports.
73  */
74 enum GNUNET_SET_OperationType
75 {
76   /**
77    * Set intersection, only return elements that are in both sets.
78    */
79   GNUNET_SET_OPERATION_INTERSECTION,
80   /**
81    * Set union, return all elements that are in at least one of the sets.
82    */
83   GNUNET_SET_OPERATION_UNION
84 };
85
86 /**
87  * Status for the result callback
88  */
89 enum GNUNET_SET_Status
90 {
91   /**
92    * Everything went ok.
93    */
94   GNUNET_SET_STATUS_OK,
95
96   /**
97    * There was a timeout.
98    */
99   GNUNET_SET_STATUS_TIMEOUT,
100
101   /**
102    * The other peer refused to to the operation with us,
103    * or something went wrong.
104    */
105   GNUNET_SET_STATUS_FAILURE,
106
107   /**
108    * Success, all elements have been returned (but the other
109    * peer might still be receiving some from us, so we are not done).
110    */
111   GNUNET_SET_STATUS_HALF_DONE,
112
113   /**
114    * Success, all elements have been sent (and received).
115    */
116   GNUNET_SET_STATUS_DONE
117 };
118
119 /**
120  * The way results are given to the client.
121  */
122 enum GNUNET_SET_ResultMode
123 {
124   /**
125    * Client gets every element in the resulting set.
126    */
127   GNUNET_SET_RESULT_FULL,
128   /**
129    * Client gets only elements that have been added to the set.
130    * Only works with set union.
131    */
132   GNUNET_SET_RESULT_ADDED,
133   /**
134    * Client gets only elements that have been removed from the set.
135    * Only works with set intersection.
136    */
137   GNUNET_SET_RESULT_REMOVED
138 };
139
140 /**
141  * Element stored in a set.
142  */
143 struct GNUNET_SET_Element
144 {
145   /**
146    * Number of bytes in the buffer pointed to by data.
147    */
148   uint16_t size;
149
150   /**
151    * Application-specific element type.
152    */
153   uint16_t type;
154
155   /**
156    * Actual data of the element
157    */
158   const void *data;
159 };
160
161
162 /**
163  * Continuation used for some of the set operations
164  *
165  * @param cls closure
166  */
167 typedef void (*GNUNET_SET_Continuation) (void *cls);
168
169
170 /**
171  * Callback for set operation results. Called for each element
172  * in the result set.
173  *
174  * @param cls closure
175  * @param element a result element, only valid if status is GNUNET_SET_STATUS_OK
176  * @param status see enum GNUNET_SET_Status
177  */
178 typedef void (*GNUNET_SET_ResultIterator) (void *cls,
179                                            const struct GNUNET_SET_Element *element,
180                                            enum GNUNET_SET_Status status);
181
182
183 /**
184  * Called when another peer wants to do a set operation with the
185  * local peer.
186  *
187  * @param cls closure
188  * @param other_peer the other peer
189  * @param context_msg message with application specific information from
190  *        the other peer
191  * @param request request from the other peer, use GNUNET_SET_accept
192  *        to accept it, otherwise the request will be refused
193  *        Note that we don't use a return value here, as it is also
194  *        necessary to specify the set we want to do the operation with,
195  *        whith sometimes can be derived from the context message.
196  *        Also necessary to specify the timeout.
197  */
198 typedef void
199 (*GNUNET_SET_ListenCallback) (void *cls,
200                               const struct GNUNET_PeerIdentity *other_peer,
201                               const struct GNUNET_MessageHeader *context_msg,
202                               struct GNUNET_SET_Request *request);
203
204
205
206 /**
207  * Create an empty set, supporting the specified operation.
208  *
209  * @param cfg configuration to use for connecting to the
210  *        set service
211  * @param op operation supported by the set
212  *        Note that the operation has to be specified
213  *        beforehand, as certain set operations need to maintain
214  *        data structures spefific to the operation
215  * @return a handle to the set
216  */
217 struct GNUNET_SET_Handle *
218 GNUNET_SET_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
219                    enum GNUNET_SET_OperationType op);
220
221
222 /**
223  * Add an element to the given set.
224  * After the element has been added (in the sense of being
225  * transmitted to the set service), cont will be called.
226  * Calls to add_element can be queued
227  *
228  * @param set set to add element to
229  * @param element element to add to the set
230  * @param cont continuation called after the element has been added
231  * @param cont_cls closure for cont
232  */
233 void
234 GNUNET_SET_add_element (struct GNUNET_SET_Handle *set,
235                         const struct GNUNET_SET_Element *element,
236                         GNUNET_SET_Continuation cont,
237                         void *cont_cls);
238
239
240 /**
241  * Remove an element to the given set.
242  * After the element has been removed (in the sense of the
243  * request being transmitted to the set service), cont will be called.
244  * Calls to remove_element can be queued
245  *
246  * @param set set to remove element from
247  * @param element element to remove from the set
248  * @param cont continuation called after the element has been removed
249  * @param cont_cls closure for cont
250  */
251 void
252 GNUNET_SET_remove_element (struct GNUNET_SET_Handle *set,
253                            const struct GNUNET_SET_Element *element,
254                            GNUNET_SET_Continuation cont,
255                            void *cont_cls);
256
257
258 /**
259  * Destroy the set handle, and free all associated resources.
260  */
261 void
262 GNUNET_SET_destroy (struct GNUNET_SET_Handle *set);
263
264
265 /**
266  * Prepare a set operation to be evaluated with another peer.
267  * The evaluation will not start until the client provides
268  * a local set with GNUNET_SET_commit.
269  *
270  * @param other_peer peer with the other set
271  * @param app_id hash for the application using the set
272  * @param context_msg additional information for the request
273  * @param salt salt used for the set operation; sometimes set operations
274  *        fail due to hash collisions, using a different salt for each operation
275  *        makes it harder for an attacker to exploit this
276  * @param result_mode specified how results will be returned,
277  *        see 'GNUNET_SET_ResultMode'.
278  * @param result_cb called on error or success
279  * @param result_cls closure for result_cb
280  * @return a handle to cancel the operation
281  */
282 struct GNUNET_SET_OperationHandle *
283 GNUNET_SET_prepare (const struct GNUNET_PeerIdentity *other_peer,
284                     const struct GNUNET_HashCode *app_id,
285                     const struct GNUNET_MessageHeader *context_msg,
286                     uint16_t salt,
287                     enum GNUNET_SET_ResultMode result_mode,
288                     GNUNET_SET_ResultIterator result_cb,
289                     void *result_cls);
290
291
292 /**
293  * Wait for set operation requests for the given application id
294  * 
295  * @param cfg configuration to use for connecting to
296  *            the set service
297  * @param operation operation we want to listen for
298  * @param app_id id of the application that handles set operation requests
299  * @param listen_cb called for each incoming request matching the operation
300  *                  and application id
301  * @param listen_cls handle for listen_cb
302  * @return a handle that can be used to cancel the listen operation
303  */
304 struct GNUNET_SET_ListenHandle *
305 GNUNET_SET_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
306                    enum GNUNET_SET_OperationType op_type,
307                    const struct GNUNET_HashCode *app_id,
308                    GNUNET_SET_ListenCallback listen_cb,
309                    void *listen_cls);
310
311
312 /**
313  * Cancel the given listen operation.
314  *
315  * @param lh handle for the listen operation
316  */
317 void
318 GNUNET_SET_listen_cancel (struct GNUNET_SET_ListenHandle *lh);
319
320
321 /**
322  * Accept a request we got via GNUNET_SET_listen.  Must be called during
323  * GNUNET_SET_listen, as the 'struct GNUNET_SET_Request' becomes invalid
324  * afterwards.
325  * Call GNUNET_SET_commit to provide the local set to use for the operation,
326  * and to begin the exchange with the remote peer. 
327  *
328  * @param request request to accept
329  * @param result_mode specified how results will be returned,
330  *        see 'GNUNET_SET_ResultMode'.
331  * @param result_cb callback for the results
332  * @param result_cls closure for result_cb
333  * @return a handle to cancel the operation
334  */
335 struct GNUNET_SET_OperationHandle *
336 GNUNET_SET_accept (struct GNUNET_SET_Request *request,
337                    enum GNUNET_SET_ResultMode result_mode,
338                    GNUNET_SET_ResultIterator result_cb,
339                    void *result_cls);
340
341
342 /**
343  * Commit a set to be used with a set operation.
344  * This function is called once we have fully constructed
345  * the set that we want to use for the operation.  At this
346  * time, the P2P protocol can then begin to exchange the
347  * set information and call the result callback with the
348  * result information.
349  *
350  * @param oh handle to the set operation 
351  * @param set the set to use for the operation
352  */
353 void
354 GNUNET_SET_commit (struct GNUNET_SET_OperationHandle *oh,
355                    struct GNUNET_SET_Handle *set);
356
357
358 /**
359  * Cancel the given set operation.
360  * May not be called after the operation's GNUNET_SET_ResultIterator has been
361  * called with a status that indicates error, timeout or done.
362  *
363  * @param oh set operation to cancel
364  */
365 void
366 GNUNET_SET_operation_cancel (struct GNUNET_SET_OperationHandle *oh);
367
368
369 #if 0                           /* keep Emacsens' auto-indent happy */
370 {
371 #endif
372 #ifdef __cplusplus
373 }
374 #endif
375
376 #endif