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