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