- distribute peers equally among island nodes on SuperMUC
[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 "platform.h"
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    * There was a timeout.
91    */
92   GNUNET_SET_STATUS_TIMEOUT,
93   /**
94    * The other peer refused to to the operation with us,
95    * or something went wrong.
96    */
97   GNUNET_SET_STATUS_FAILURE,
98   /**
99    * Success, all elements have been sent.
100    */
101   GNUNET_SET_STATUS_DONE
102 };
103
104 /**
105  * The way results are given to the client.
106  */
107 enum GNUNET_SET_ResultMode
108 {
109   /**
110    * Client gets every element in the resulting set.
111    */
112   GNUNET_SET_RESULT_FULL,
113   /**
114    * Client gets only elements that have been added to the set.
115    * Only works with set union.
116    */
117   GNUNET_SET_RESULT_ADDED,
118   /**
119    * Client gets only elements that have been removed from the set.
120    * Only works with set intersection.
121    */
122   GNUNET_SET_RESULT_REMOVED
123 };
124
125 /**
126  * Element stored in a set.
127  */
128 struct GNUNET_SET_Element
129 {
130   /**
131    * Number of bytes in the buffer pointed to by data.
132    */
133   uint16_t size;
134
135   /**
136    * Application-specific element type.
137    */
138   uint16_t type;
139
140   /**
141    * Actual data of the element
142    */
143   void *data;
144 };
145
146
147 /**
148  * Continuation used for some of the set operations
149  *
150  * @cls closure
151  */
152 typedef void (*GNUNET_SET_Continuation) (void *cls);
153
154
155 /**
156  * Callback for set operation results. Called for each element
157  * in the result set.
158  *
159  * @param cls closure
160  * @param element a result element, only valid if status is GNUNET_SET_STATUS_OK
161  * @param status see enum GNUNET_SET_Status
162  */
163 typedef void (*GNUNET_SET_ResultIterator) (void *cls,
164                                            struct GNUNET_SET_Element *element,
165                                            enum GNUNET_SET_Status status);
166
167
168 /**
169  * Called when another peer wants to do a set operation with the
170  * local peer
171  *
172  * @param other_peer the other peer
173  * @param context_msg message with application specific information from
174  *        the other peer
175  * @param request request from the other peer, use GNUNET_SET_accept
176  *        to accept it, otherwise the request will be refused
177  *        Note that we don't use a return value here, as it is also
178  *        necessary to specify the set we want to do the operation with,
179  *        whith sometimes can be derived from the context message.
180  *        Also necessary to specify the timeout.
181  */
182 typedef void
183 (*GNUNET_SET_ListenCallback) (void *cls,
184                               const struct GNUNET_PeerIdentity *other_peer,
185                               const struct GNUNET_MessageHeader *context_msg,
186                               struct GNUNET_SET_Request *request);
187
188
189
190 /**
191  * Create an empty set, supporting the specified operation.
192  *
193  * @param cfg configuration to use for connecting to the
194  *        set service
195  * @param op operation supported by the set
196  *        Note that the operation has to be specified
197  *        beforehand, as certain set operations need to maintain
198  *        data structures spefific to the operation
199  * @return a handle to the set
200  */
201 struct GNUNET_SET_Handle *
202 GNUNET_SET_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
203                    enum GNUNET_SET_OperationType op);
204
205
206 /**
207  * Add an element to the given set.
208  * After the element has been added (in the sense of being
209  * transmitted to the set service), cont will be called.
210  * Calls to add_element can be queued
211  *
212  * @param set set to add element to
213  * @param element element to add to the set
214  * @param cont continuation called after the element has been added
215  * @param cont_cls closure for cont
216  */
217 void
218 GNUNET_SET_add_element (struct GNUNET_SET_Handle *set,
219                         const struct GNUNET_SET_Element *element,
220                         GNUNET_SET_Continuation cont,
221                         void *cont_cls);
222
223
224 /**
225  * Remove an element to the given set.
226  * After the element has been removed (in the sense of the
227  * request being transmitted to the set service), cont will be called.
228  * Calls to remove_element can be queued
229  *
230  * @param set set to remove element from
231  * @param element element to remove from the set
232  * @param cont continuation called after the element has been removed
233  * @param cont_cls closure for cont
234  */
235 void
236 GNUNET_SET_remove_element (struct GNUNET_SET_Handle *set,
237                            const struct GNUNET_SET_Element *element,
238                            GNUNET_SET_Continuation cont,
239                            void *cont_cls);
240
241
242 /**
243  * Destroy the set handle, and free all associated resources.
244  */
245 void
246 GNUNET_SET_destroy (struct GNUNET_SET_Handle *set);
247
248
249 /**
250  * Evaluate a set operation with our set and the set of another peer.
251  *
252  * @param set set to use
253  * @param salt salt for HKDF (explain more here)
254  * @param other_peer peer with the other set
255  * @param app_id hash for the application using the set
256  * @param context_msg additional information for the request
257  * @param salt salt used for the set operation; sometimes set operations
258  *        fail due to hash collisions, using a different salt for each operation
259  *        makes it harder for an attacker to exploit this
260  * @param timeout result_cb will be called with GNUNET_SET_STATUS_TIMEOUT
261  *        if the operation is not done after the specified time
262  * @param result_mode specified how results will be returned,
263  *        see 'GNUNET_SET_ResultMode'.
264  * @param result_cb called on error or success
265  * @param result_cls closure for result_cb
266  * @return a handle to cancel the operation
267  */
268 struct GNUNET_SET_OperationHandle *
269 GNUNET_SET_evaluate (struct GNUNET_SET_Handle *set,
270                      const struct GNUNET_PeerIdentity *other_peer,
271                      const struct GNUNET_HashCode *app_id,
272                      const struct GNUNET_MessageHeader *context_msg,
273                      uint16_t salt,
274                      struct GNUNET_TIME_Relative timeout,
275                      enum GNUNET_SET_ResultMode result_mode,
276                      GNUNET_SET_ResultIterator result_cb,
277                      void *result_cls);
278
279
280 /**
281  * Wait for set operation requests for the given application id
282  * 
283  * @param cfg configuration to use for connecting to
284  *            the set service
285  * @param operation operation we want to listen for
286  * @param app_id id of the application that handles set operation requests
287  * @param listen_cb called for each incoming request matching the operation
288  *                  and application id
289  * @param listen_cls handle for listen_cb
290  * @return a handle that can be used to cancel the listen operation
291  */
292 struct GNUNET_SET_ListenHandle *
293 GNUNET_SET_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
294                    enum GNUNET_SET_OperationType op_type,
295                    const struct GNUNET_HashCode *app_id,
296                    GNUNET_SET_ListenCallback listen_cb,
297                    void *listen_cls);
298
299
300
301 /**
302  * Cancel the given listen operation.
303  *
304  * @param lh handle for the listen operation
305  */
306 void
307 GNUNET_SET_listen_cancel (struct GNUNET_SET_ListenHandle *lh);
308
309
310
311 /**
312  * Accept a request we got via GNUNET_SET_listen.
313  *
314  * @param request request to accept
315  * @param set set used for the requested operation 
316  * @param timeout timeout for the set operation
317  * @param result_mode specified how results will be returned,
318  *        see 'GNUNET_SET_ResultMode'.
319  * @param result_cb callback for the results
320  * @param result_cls closure for result_cb
321  * @return a handle to cancel the operation
322  */
323 struct GNUNET_SET_OperationHandle *
324 GNUNET_SET_accept (struct GNUNET_SET_Request *request,
325                    struct GNUNET_SET_Handle *set,
326                    struct GNUNET_TIME_Relative timeout,
327                    enum GNUNET_SET_ResultMode result_mode,
328                    GNUNET_SET_ResultIterator result_cb,
329                    void *cls);
330
331
332 /**
333  * Cancel the given set operation.
334  *
335  * @param oh set operation to cancel
336  */
337 void
338 GNUNET_SET_operation_cancel (struct GNUNET_SET_OperationHandle *oh);
339
340
341 #if 0                           /* keep Emacsens' auto-indent happy */
342 {
343 #endif
344 #ifdef __cplusplus
345 }
346 #endif
347
348 #endif