2 This file is part of GNUnet
3 (C) 2013 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file include/gnunet_set_service.h
23 * @brief two-peer set operations
24 * @author Florian Dold
25 * @author Christian Grothoff
28 #ifndef GNUNET_SET_SERVICE_H
29 #define GNUNET_SET_SERVICE_H
34 #if 0 /* keep Emacsens' auto-indent happy */
39 #include "gnunet_common.h"
40 #include "gnunet_time_lib.h"
41 #include "gnunet_configuration_lib.h"
45 * Maximum size of a context message for set operation requests.
47 #define GNUNET_SET_CONTEXT_MESSAGE_MAX_SIZE ((1<<16) - 1024)
51 * Opaque handle to a set.
53 struct GNUNET_SET_Handle;
56 * Opaque handle to a set operation request from another peer.
58 struct GNUNET_SET_Request;
61 * Opaque handle to a listen operation.
63 struct GNUNET_SET_ListenHandle;
66 * Opaque handle to a set operation.
68 struct GNUNET_SET_OperationHandle;
72 * The operation that a set set supports.
74 enum GNUNET_SET_OperationType
77 * A purely local set that does not support any
80 GNUNET_SET_OPERATION_NONE,
83 * Set intersection, only return elements that are in both sets.
85 GNUNET_SET_OPERATION_INTERSECTION,
88 * Set union, return all elements that are in at least one of the sets.
90 GNUNET_SET_OPERATION_UNION
94 * Status for the result callback
96 enum GNUNET_SET_Status
101 GNUNET_SET_STATUS_OK,
104 * There was a timeout.
106 GNUNET_SET_STATUS_TIMEOUT,
109 * The other peer refused to to the operation with us,
110 * or something went wrong.
112 GNUNET_SET_STATUS_FAILURE,
115 * Success, all elements have been returned (but the other
116 * peer might still be receiving some from us, so we are not done).
118 GNUNET_SET_STATUS_HALF_DONE,
121 * Success, all elements have been sent (and received).
123 GNUNET_SET_STATUS_DONE
128 * The way results are given to the client.
130 enum GNUNET_SET_ResultMode
133 * Client gets every element in the resulting set.
135 GNUNET_SET_RESULT_FULL,
137 * Client gets only elements that have been added to the set.
138 * Only works with set union.
140 GNUNET_SET_RESULT_ADDED,
142 * Client gets only elements that have been removed from the set.
143 * Only works with set intersection.
145 GNUNET_SET_RESULT_REMOVED
150 * Element stored in a set.
152 struct GNUNET_SET_Element
155 * Number of bytes in the buffer pointed to by data.
160 * Application-specific element type.
165 * Actual data of the element
172 * Continuation used for some of the set operations
176 typedef void (*GNUNET_SET_Continuation) (void *cls);
180 * Callback for set operation results. Called for each element
184 * @param element a result element, only valid if status is GNUNET_SET_STATUS_OK
185 * @param status see enum GNUNET_SET_Status
187 typedef void (*GNUNET_SET_ResultIterator) (void *cls,
188 const struct GNUNET_SET_Element *element,
189 enum GNUNET_SET_Status status);
192 * Iterator for set elements.
195 * @param element the current element, NULL if all elements have been
197 * @return GNUNET_YES to continue iterating, GNUNET_NO to stop.
199 typedef int (*GNUNET_SET_ElementIterator) (void *cls,
200 const struct GNUNET_SET_Element *element);
204 * Called when another peer wants to do a set operation with the
205 * local peer. If a listen error occurs, the 'request' is NULL.
208 * @param other_peer the other peer
209 * @param context_msg message with application specific information from
211 * @param request request from the other peer, use GNUNET_SET_accept
212 * Will be NULL if the listener failed.
213 * to accept it, otherwise the request will be refused
214 * Note that we can't just return value from the listen callback,
215 * as it is also necessary to specify the set we want to do the
216 * operation with, whith sometimes can be derived from the context
217 * message. It's necessary to specify the timeout.
220 (*GNUNET_SET_ListenCallback) (void *cls,
221 const struct GNUNET_PeerIdentity *other_peer,
222 const struct GNUNET_MessageHeader *context_msg,
223 struct GNUNET_SET_Request *request);
228 * Create an empty set, supporting the specified operation.
230 * @param cfg configuration to use for connecting to the
232 * @param op operation supported by the set
233 * Note that the operation has to be specified
234 * beforehand, as certain set operations need to maintain
235 * data structures spefific to the operation
236 * @return a handle to the set
238 struct GNUNET_SET_Handle *
239 GNUNET_SET_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
240 enum GNUNET_SET_OperationType op);
244 * Add an element to the given set.
245 * After the element has been added (in the sense of being
246 * transmitted to the set service), cont will be called.
247 * Calls to add_element can be queued
249 * @param set set to add element to
250 * @param element element to add to the set
251 * @param cont continuation called after the element has been added
252 * @param cont_cls closure for cont
253 * @return GNUNET_OK on success, GNUNET_SYSERR if the
254 * set is invalid (e.g. the set service crashed)
257 GNUNET_SET_add_element (struct GNUNET_SET_Handle *set,
258 const struct GNUNET_SET_Element *element,
259 GNUNET_SET_Continuation cont,
264 * Remove an element to the given set.
265 * After the element has been removed (in the sense of the
266 * request being transmitted to the set service), cont will be called.
267 * Calls to remove_element can be queued
269 * @param set set to remove element from
270 * @param element element to remove from the set
271 * @param cont continuation called after the element has been removed
272 * @param cont_cls closure for cont
273 * @return GNUNET_OK on success, GNUNET_SYSERR if the
274 * set is invalid (e.g. the set service crashed)
277 GNUNET_SET_remove_element (struct GNUNET_SET_Handle *set,
278 const struct GNUNET_SET_Element *element,
279 GNUNET_SET_Continuation cont,
284 * Destroy the set handle, and free all associated resources.
287 GNUNET_SET_destroy (struct GNUNET_SET_Handle *set);
291 * Prepare a set operation to be evaluated with another peer.
292 * The evaluation will not start until the client provides
293 * a local set with GNUNET_SET_commit.
295 * @param other_peer peer with the other set
296 * @param app_id hash for the application using the set
297 * @param context_msg additional information for the request
298 * @param salt salt used for the set operation; sometimes set operations
299 * fail due to hash collisions, using a different salt for each operation
300 * makes it harder for an attacker to exploit this
301 * @param result_mode specified how results will be returned,
302 * see 'GNUNET_SET_ResultMode'.
303 * @param result_cb called on error or success
304 * @param result_cls closure for result_cb
305 * @return a handle to cancel the operation
307 struct GNUNET_SET_OperationHandle *
308 GNUNET_SET_prepare (const struct GNUNET_PeerIdentity *other_peer,
309 const struct GNUNET_HashCode *app_id,
310 const struct GNUNET_MessageHeader *context_msg,
312 enum GNUNET_SET_ResultMode result_mode,
313 GNUNET_SET_ResultIterator result_cb,
318 * Wait for set operation requests for the given application id
320 * @param cfg configuration to use for connecting to
322 * @param operation operation we want to listen for
323 * @param app_id id of the application that handles set operation requests
324 * @param listen_cb called for each incoming request matching the operation
326 * @param listen_cls handle for listen_cb
327 * @return a handle that can be used to cancel the listen operation
329 struct GNUNET_SET_ListenHandle *
330 GNUNET_SET_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
331 enum GNUNET_SET_OperationType op_type,
332 const struct GNUNET_HashCode *app_id,
333 GNUNET_SET_ListenCallback listen_cb,
338 * Cancel the given listen operation.
340 * @param lh handle for the listen operation
343 GNUNET_SET_listen_cancel (struct GNUNET_SET_ListenHandle *lh);
347 * Accept a request we got via GNUNET_SET_listen. Must be called during
348 * GNUNET_SET_listen, as the 'struct GNUNET_SET_Request' becomes invalid
350 * Call GNUNET_SET_commit to provide the local set to use for the operation,
351 * and to begin the exchange with the remote peer.
353 * @param request request to accept
354 * @param result_mode specified how results will be returned,
355 * see 'GNUNET_SET_ResultMode'.
356 * @param result_cb callback for the results
357 * @param result_cls closure for result_cb
358 * @return a handle to cancel the operation
360 struct GNUNET_SET_OperationHandle *
361 GNUNET_SET_accept (struct GNUNET_SET_Request *request,
362 enum GNUNET_SET_ResultMode result_mode,
363 GNUNET_SET_ResultIterator result_cb,
368 * Commit a set to be used with a set operation.
369 * This function is called once we have fully constructed
370 * the set that we want to use for the operation. At this
371 * time, the P2P protocol can then begin to exchange the
372 * set information and call the result callback with the
373 * result information.
375 * @param oh handle to the set operation
376 * @param set the set to use for the operation
377 * @return GNUNET_OK on success, GNUNET_SYSERR if the
378 * set is invalid (e.g. the set service crashed)
381 GNUNET_SET_commit (struct GNUNET_SET_OperationHandle *oh,
382 struct GNUNET_SET_Handle *set);
386 * Cancel the given set operation.
387 * May not be called after the operation's GNUNET_SET_ResultIterator has been
388 * called with a status that indicates error, timeout or done.
390 * @param oh set operation to cancel
393 GNUNET_SET_operation_cancel (struct GNUNET_SET_OperationHandle *oh);
397 * Iterate over all elements in the given set.
398 * Note that this operation involves transferring every element of the set
399 * from the service to the client, and is thus costly.
400 * Only one iteration per set may be active at the same time.
402 * @param set the set to iterate over
403 * @param iter the iterator to call for each element
404 * @param cls closure for 'iter'
405 * @return GNUNET_YES if every element was iterated over, GNUNET_NO
406 * if the iterator prematurely stopped, GNUNET_SYSERR if the set
407 * is invalid (e.g. the server crashed, disconnected)
410 GNUNET_SET_iterate (struct GNUNET_SET_Handle *set, GNUNET_SET_ElementIterator iter, void *cls);
413 #if 0 /* keep Emacsens' auto-indent happy */