2 This file is part of GNUnet.
3 (C) 2012, 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.
23 * @brief api for the set service
24 * @author Florian Dold
27 #include "gnunet_util_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_client_lib.h"
30 #include "gnunet_set_service.h"
34 #define LOG(kind,...) GNUNET_log_from (kind, "set-api",__VA_ARGS__)
38 * Opaque handle to a set.
40 struct GNUNET_SET_Handle
42 struct GNUNET_CLIENT_Connection *client;
43 struct GNUNET_MQ_Handle *mq;
44 unsigned int messages_since_ack;
48 * Opaque handle to a set operation request from another peer.
50 struct GNUNET_SET_Request
56 struct GNUNET_SET_OperationHandle
58 GNUNET_SET_ResultIterator result_cb;
62 * Local set used for the operation,
63 * NULL if no set has been provided by conclude yet.
65 struct GNUNET_SET_Handle *set;
68 * Request ID to identify the operation within the set.
73 * Message sent to the server on calling conclude,
74 * NULL if conclude has been called.
76 struct GNUNET_MQ_Envelope *conclude_mqm;
79 * Address of the request if in the conclude message,
80 * used to patch the request id into the message when the set is known.
82 uint32_t *request_id_addr;
87 * Opaque handle to a listen operation.
89 struct GNUNET_SET_ListenHandle
91 struct GNUNET_CLIENT_Connection *client;
92 struct GNUNET_MQ_Handle* mq;
93 GNUNET_SET_ListenCallback listen_cb;
99 * Handle result message for a set operation.
102 * @param mh the message
105 handle_result (void *cls, const struct GNUNET_MessageHeader *mh)
107 struct GNUNET_SET_ResultMessage *msg = (struct GNUNET_SET_ResultMessage *) mh;
108 struct GNUNET_SET_Handle *set = cls;
109 struct GNUNET_SET_OperationHandle *oh;
110 struct GNUNET_SET_Element e;
113 GNUNET_assert (NULL != set);
114 GNUNET_assert (NULL != set->mq);
116 if (set->messages_since_ack >= GNUNET_SET_ACK_WINDOW/2)
118 struct GNUNET_MQ_Envelope *mqm;
119 mqm = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_ACK);
120 GNUNET_MQ_send (set->mq, mqm);
122 oh = GNUNET_MQ_assoc_get (set->mq, ntohl (msg->request_id));
123 GNUNET_assert (NULL != oh);
124 /* status is not STATUS_OK => there's no attached element,
125 * and this is the last result message we get */
126 if (htons (msg->result_status) != GNUNET_SET_STATUS_OK)
128 GNUNET_MQ_assoc_remove (set->mq, ntohl (msg->request_id));
129 if (NULL != oh->result_cb)
130 oh->result_cb (oh->result_cls, NULL, htons (msg->result_status));
136 e.size = ntohs (mh->size) - sizeof (struct GNUNET_SET_ResultMessage);
137 e.type = msg->element_type;
138 if (NULL != oh->result_cb)
139 oh->result_cb (oh->result_cls, &e, htons (msg->result_status));
143 * Handle request message for a listen operation
145 * @param cls the listen handle
146 * @param mh the message
149 handle_request (void *cls, const struct GNUNET_MessageHeader *mh)
151 struct GNUNET_SET_RequestMessage *msg = (struct GNUNET_SET_RequestMessage *) mh;
152 struct GNUNET_SET_ListenHandle *lh = cls;
153 struct GNUNET_SET_Request *req;
154 struct GNUNET_MessageHeader *context_msg;
156 LOG (GNUNET_ERROR_TYPE_INFO, "processing request\n");
157 req = GNUNET_new (struct GNUNET_SET_Request);
158 req->accept_id = ntohl (msg->accept_id);
159 context_msg = GNUNET_MQ_extract_nested_mh (msg);
160 /* calling GNUNET_SET_accept in the listen cb will set req->accepted */
161 lh->listen_cb (lh->listen_cls, &msg->peer_id, context_msg, req);
163 if (GNUNET_NO == req->accepted)
165 struct GNUNET_MQ_Envelope *mqm;
166 struct GNUNET_SET_AcceptRejectMessage *amsg;
168 mqm = GNUNET_MQ_msg (amsg, GNUNET_MESSAGE_TYPE_SET_REJECT);
169 /* no request id, as we refused */
170 amsg->request_id = htonl (0);
171 amsg->accept_reject_id = msg->accept_id;
172 GNUNET_MQ_send (lh->mq, mqm);
174 LOG (GNUNET_ERROR_TYPE_INFO, "rejecting request\n");
177 LOG (GNUNET_ERROR_TYPE_INFO, "processed op request from service\n");
179 /* the accept-case is handled in GNUNET_SET_accept,
180 * as we have the accept message available there */
185 * Create an empty set, supporting the specified operation.
187 * @param cfg configuration to use for connecting to the
189 * @param op operation supported by the set
190 * Note that the operation has to be specified
191 * beforehand, as certain set operations need to maintain
192 * data structures spefific to the operation
193 * @return a handle to the set
195 struct GNUNET_SET_Handle *
196 GNUNET_SET_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
197 enum GNUNET_SET_OperationType op)
199 struct GNUNET_SET_Handle *set;
200 struct GNUNET_MQ_Envelope *mqm;
201 struct GNUNET_SET_CreateMessage *msg;
202 static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
203 {handle_result, GNUNET_MESSAGE_TYPE_SET_RESULT},
204 GNUNET_MQ_HANDLERS_END
207 set = GNUNET_new (struct GNUNET_SET_Handle);
208 set->client = GNUNET_CLIENT_connect ("set", cfg);
209 LOG (GNUNET_ERROR_TYPE_INFO, "set client created\n");
210 GNUNET_assert (NULL != set->client);
211 set->mq = GNUNET_MQ_queue_for_connection_client (set->client, mq_handlers, set);
212 GNUNET_assert (NULL != set->mq);
213 mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_CREATE);
214 msg->operation = htons (op);
215 GNUNET_MQ_send (set->mq, mqm);
221 * Add an element to the given set.
222 * After the element has been added (in the sense of being
223 * transmitted to the set service), cont will be called.
224 * Calls to add_element can be queued
226 * @param set set to add element to
227 * @param element element to add to the set
228 * @param cont continuation called after the element has been added
229 * @param cont_cls closure for cont
232 GNUNET_SET_add_element (struct GNUNET_SET_Handle *set,
233 const struct GNUNET_SET_Element *element,
234 GNUNET_SET_Continuation cont,
237 struct GNUNET_MQ_Envelope *mqm;
238 struct GNUNET_SET_ElementMessage *msg;
240 mqm = GNUNET_MQ_msg_extra (msg, element->size, GNUNET_MESSAGE_TYPE_SET_ADD);
241 msg->element_type = element->type;
242 memcpy (&msg[1], element->data, element->size);
243 GNUNET_MQ_notify_sent (mqm, cont, cont_cls);
244 GNUNET_MQ_send (set->mq, mqm);
249 * Remove an element to the given set.
250 * After the element has been removed (in the sense of the
251 * request being transmitted to the set service), cont will be called.
252 * Calls to remove_element can be queued
254 * @param set set to remove element from
255 * @param element element to remove from the set
256 * @param cont continuation called after the element has been removed
257 * @param cont_cls closure for cont
260 GNUNET_SET_remove_element (struct GNUNET_SET_Handle *set,
261 const struct GNUNET_SET_Element *element,
262 GNUNET_SET_Continuation cont,
265 struct GNUNET_MQ_Envelope *mqm;
266 struct GNUNET_SET_ElementMessage *msg;
268 mqm = GNUNET_MQ_msg_extra (msg, element->size, GNUNET_MESSAGE_TYPE_SET_REMOVE);
269 msg->element_type = element->type;
270 memcpy (&msg[1], element->data, element->size);
271 GNUNET_MQ_notify_sent (mqm, cont, cont_cls);
272 GNUNET_MQ_send (set->mq, mqm);
277 * Destroy the set handle, and free all associated resources.
280 GNUNET_SET_destroy (struct GNUNET_SET_Handle *set)
282 GNUNET_CLIENT_disconnect (set->client);
284 GNUNET_MQ_destroy (set->mq);
290 * Prepare a set operation to be evaluated with another peer.
291 * The evaluation will not start until the client provides
292 * a local set with GNUNET_SET_commit.
294 * @param other_peer peer with the other set
295 * @param app_id hash for the application using the set
296 * @param context_msg additional information for the request
297 * @param salt salt used for the set operation; sometimes set operations
298 * fail due to hash collisions, using a different salt for each operation
299 * makes it harder for an attacker to exploit this
300 * @param result_mode specified how results will be returned,
301 * see 'GNUNET_SET_ResultMode'.
302 * @param result_cb called on error or success
303 * @param result_cls closure for result_cb
304 * @return a handle to cancel the operation
306 struct GNUNET_SET_OperationHandle *
307 GNUNET_SET_prepare (const struct GNUNET_PeerIdentity *other_peer,
308 const struct GNUNET_HashCode *app_id,
309 const struct GNUNET_MessageHeader *context_msg,
311 enum GNUNET_SET_ResultMode result_mode,
312 GNUNET_SET_ResultIterator result_cb,
315 struct GNUNET_MQ_Envelope *mqm;
316 struct GNUNET_SET_OperationHandle *oh;
317 struct GNUNET_SET_EvaluateMessage *msg;
319 oh = GNUNET_new (struct GNUNET_SET_OperationHandle);
320 oh->result_cb = result_cb;
321 oh->result_cls = result_cls;
323 mqm = GNUNET_MQ_msg_nested_mh (msg, GNUNET_MESSAGE_TYPE_SET_EVALUATE, context_msg);
325 msg->app_id = *app_id;
326 msg->target_peer = *other_peer;
329 oh->conclude_mqm = mqm;
330 oh->request_id_addr = &msg->request_id;
337 * Wait for set operation requests for the given application id
339 * @param cfg configuration to use for connecting to
341 * @param operation operation we want to listen for
342 * @param app_id id of the application that handles set operation requests
343 * @param listen_cb called for each incoming request matching the operation
345 * @param listen_cls handle for listen_cb
346 * @return a handle that can be used to cancel the listen operation
348 struct GNUNET_SET_ListenHandle *
349 GNUNET_SET_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
350 enum GNUNET_SET_OperationType operation,
351 const struct GNUNET_HashCode *app_id,
352 GNUNET_SET_ListenCallback listen_cb,
355 struct GNUNET_SET_ListenHandle *lh;
356 struct GNUNET_MQ_Envelope *mqm;
357 struct GNUNET_SET_ListenMessage *msg;
358 static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
359 {handle_request, GNUNET_MESSAGE_TYPE_SET_REQUEST},
360 GNUNET_MQ_HANDLERS_END
363 lh = GNUNET_new (struct GNUNET_SET_ListenHandle);
364 lh->client = GNUNET_CLIENT_connect ("set", cfg);
365 lh->listen_cb = listen_cb;
366 lh->listen_cls = listen_cls;
367 GNUNET_assert (NULL != lh->client);
368 lh->mq = GNUNET_MQ_queue_for_connection_client (lh->client, mq_handlers, lh);
369 mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_LISTEN);
370 msg->operation = htons (operation);
371 msg->app_id = *app_id;
372 GNUNET_MQ_send (lh->mq, mqm);
379 * Cancel the given listen operation.
381 * @param lh handle for the listen operation
384 GNUNET_SET_listen_cancel (struct GNUNET_SET_ListenHandle *lh)
386 GNUNET_CLIENT_disconnect (lh->client);
387 GNUNET_MQ_destroy (lh->mq);
393 * Accept a request we got via GNUNET_SET_listen. Must be called during
394 * GNUNET_SET_listen, as the 'struct GNUNET_SET_Request' becomes invalid
396 * Call GNUNET_SET_conclude to provide the local set to use for the operation,
397 * and to begin the exchange with the remote peer.
399 * @param request request to accept
400 * @param result_mode specified how results will be returned,
401 * see 'GNUNET_SET_ResultMode'.
402 * @param result_cb callback for the results
403 * @param result_cls closure for result_cb
404 * @return a handle to cancel the operation
406 struct GNUNET_SET_OperationHandle *
407 GNUNET_SET_accept (struct GNUNET_SET_Request *request,
408 enum GNUNET_SET_ResultMode result_mode,
409 GNUNET_SET_ResultIterator result_cb,
412 struct GNUNET_MQ_Envelope *mqm;
413 struct GNUNET_SET_OperationHandle *oh;
414 struct GNUNET_SET_AcceptRejectMessage *msg;
416 GNUNET_assert (GNUNET_NO == request->accepted);
417 request->accepted = GNUNET_YES;
419 oh = GNUNET_new (struct GNUNET_SET_OperationHandle);
420 oh->result_cb = result_cb;
421 oh->result_cls = result_cls;
423 mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_ACCEPT);
424 msg->accept_reject_id = htonl (request->accept_id);
426 oh->conclude_mqm = mqm;
427 oh->request_id_addr = &msg->request_id;
434 * Cancel the given set operation.
436 * @param oh set operation to cancel
439 GNUNET_SET_operation_cancel (struct GNUNET_SET_OperationHandle *oh)
441 struct GNUNET_MQ_Envelope *mqm;
442 struct GNUNET_SET_OperationHandle *h_assoc;
446 h_assoc = GNUNET_MQ_assoc_remove (oh->set->mq, oh->request_id);
447 GNUNET_assert (h_assoc == oh);
448 mqm = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_CANCEL);
449 GNUNET_MQ_send (oh->set->mq, mqm);
452 if (NULL != oh->conclude_mqm)
453 GNUNET_MQ_discard (oh->conclude_mqm);
460 * Commit a set to be used with a set operation.
461 * This function is called once we have fully constructed
462 * the set that we want to use for the operation. At this
463 * time, the P2P protocol can then begin to exchange the
464 * set information and call the result callback with the
465 * result information.
467 * @param oh handle to the set operation
468 * @param set the set to use for the operation
471 GNUNET_SET_commit (struct GNUNET_SET_OperationHandle *oh,
472 struct GNUNET_SET_Handle *set)
474 GNUNET_assert (NULL == oh->set);
475 GNUNET_assert (NULL != oh->conclude_mqm);
477 oh->request_id = GNUNET_MQ_assoc_add (oh->set->mq, oh);
478 *oh->request_id_addr = htonl (oh->request_id);
479 GNUNET_MQ_send (oh->set->mq, oh->conclude_mqm);
480 oh->conclude_mqm = NULL;
481 oh->request_id_addr = NULL;