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