2 This file is part of GNUnet.
3 Copyright (C) 2013, 2014, 2016 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file scalarproduct/scalarproduct_api.c
22 * @brief API for the scalarproduct
23 * @author Christian Fuchs
24 * @author Gaurav Kukreja
25 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_statistics_service.h"
30 #include "gnunet_scalarproduct_service.h"
31 #include "gnunet_protocols.h"
32 #include "scalarproduct.h"
34 #define LOG(kind,...) GNUNET_log_from (kind, "scalarproduct-api",__VA_ARGS__)
38 * The abstraction function for our internal callback
40 * @param h computation handle
41 * @param msg response we got, NULL on errors
42 * @param status processing status code
45 (*GNUNET_SCALARPRODUCT_ResponseMessageHandler) (struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
46 const struct ClientResponseMessage *msg,
47 enum GNUNET_SCALARPRODUCT_ResponseStatus status);
51 * A handle returned for each computation
53 struct GNUNET_SCALARPRODUCT_ComputationHandle
58 const struct GNUNET_CONFIGURATION_Handle *cfg;
61 * Current connection to the scalarproduct service.
63 struct GNUNET_MQ_Handle *mq;
66 * Function to call after transmission of the request (Bob).
68 GNUNET_SCALARPRODUCT_ContinuationWithStatus cont_status;
71 * Function to call after transmission of the request (Alice).
73 GNUNET_SCALARPRODUCT_DatumProcessor cont_datum;
76 * Closure for @e cont_status or @e cont_datum.
81 * API internal callback for results and failures to be forwarded to
84 GNUNET_SCALARPRODUCT_ResponseMessageHandler response_proc;
87 * The shared session key identifying this computation
89 struct GNUNET_HashCode key;
95 * Called when a response is received from the service. Perform basic
96 * check that the message is well-formed.
98 * @param cls Pointer to the Master Context
99 * @param message Pointer to the data received in response
100 * @return #GNUNET_OK if @a message is well-formed
103 check_response (void *cls,
104 const struct ClientResponseMessage *message)
106 if (ntohs (message->header.size) !=
107 ntohl (message->product_length) + sizeof (struct ClientResponseMessage))
110 return GNUNET_SYSERR;
117 * Handles the STATUS received from the service for a response, does
118 * not contain a payload. Called when we participate as "Bob" via
119 * #GNUNET_SCALARPRODUCT_accept_computation().
121 * @param h our Handle
122 * @param msg the response received
123 * @param status the condition the request was terminated with (eg: disconnect)
126 process_status_message (struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
127 const struct ClientResponseMessage *msg,
128 enum GNUNET_SCALARPRODUCT_ResponseStatus status)
130 if (NULL != h->cont_status)
131 h->cont_status (h->cont_cls,
133 GNUNET_SCALARPRODUCT_cancel (h);
138 * Called when a response is received from the service. After basic
139 * check, the handler in `h->response_proc` is called. This functions
140 * handles the response to the client which used the API.
142 * @param cls Pointer to the Master Context
143 * @param msg Pointer to the data received in response
146 handle_response (void *cls,
147 const struct ClientResponseMessage *message)
149 struct GNUNET_SCALARPRODUCT_ComputationHandle *h = cls;
150 enum GNUNET_SCALARPRODUCT_ResponseStatus status;
152 status = (enum GNUNET_SCALARPRODUCT_ResponseStatus) ntohl (message->status);
160 * Check if the keys for all given elements are unique.
162 * @param elements elements to check
163 * @param element_count size of the @a elements array
164 * @return #GNUNET_OK if all keys are unique
167 check_unique (const struct GNUNET_SCALARPRODUCT_Element *elements,
168 uint32_t element_count)
170 struct GNUNET_CONTAINER_MultiHashMap *map;
175 map = GNUNET_CONTAINER_multihashmap_create (2 * element_count,
177 for (i=0;i<element_count;i++)
179 GNUNET_CONTAINER_multihashmap_put (map,
182 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
184 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
185 _("Keys given to SCALARPRODUCT not unique!\n"));
188 GNUNET_CONTAINER_multihashmap_destroy (map);
194 * We encountered an error communicating with the set service while
195 * performing a set operation. Report to the application.
197 * @param cls the `struct GNUNET_SCALARPRODUCT_ComputationHandle`
198 * @param error error code
201 mq_error_handler (void *cls,
202 enum GNUNET_MQ_Error error)
204 struct GNUNET_SCALARPRODUCT_ComputationHandle *h = cls;
206 LOG (GNUNET_ERROR_TYPE_INFO,
207 "Disconnected from SCALARPRODUCT service.\n");
210 GNUNET_SCALARPRODUCT_STATUS_DISCONNECTED);
215 * Used by Bob's client to cooperate with Alice,
217 * @param cfg the gnunet configuration handle
218 * @param key Session key unique to the requesting client
219 * @param elements Array of elements of the vector
220 * @param element_count Number of elements in the @a elements vector
221 * @param cont Callback function
222 * @param cont_cls Closure for @a cont
223 * @return a new handle for this computation
225 struct GNUNET_SCALARPRODUCT_ComputationHandle *
226 GNUNET_SCALARPRODUCT_accept_computation (const struct GNUNET_CONFIGURATION_Handle *cfg,
227 const struct GNUNET_HashCode *session_key,
228 const struct GNUNET_SCALARPRODUCT_Element *elements,
229 uint32_t element_count,
230 GNUNET_SCALARPRODUCT_ContinuationWithStatus cont,
233 struct GNUNET_SCALARPRODUCT_ComputationHandle *h
234 = GNUNET_new (struct GNUNET_SCALARPRODUCT_ComputationHandle);
235 struct GNUNET_MQ_MessageHandler handlers[] = {
236 GNUNET_MQ_hd_var_size (response,
237 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT,
238 struct ClientResponseMessage,
240 GNUNET_MQ_handler_end ()
242 struct GNUNET_MQ_Envelope *env;
243 struct BobComputationMessage *msg;
244 struct ComputationBobCryptodataMultipartMessage *mmsg;
248 uint32_t element_count_transfered;
251 if (GNUNET_SYSERR == check_unique (elements,
254 h->cont_status = cont;
255 h->cont_cls = cont_cls;
256 h->response_proc = &process_status_message;
258 h->key = *session_key;
259 h->mq = GNUNET_CLIENT_connect (cfg,
266 /* scalarproduct configuration error */
271 possible = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - sizeof (struct BobComputationMessage))
272 / sizeof (struct GNUNET_SCALARPRODUCT_Element);
273 todo = GNUNET_MIN (possible,
275 size = todo * sizeof (struct GNUNET_SCALARPRODUCT_Element);
276 env = GNUNET_MQ_msg_extra (msg,
278 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_BOB);
279 msg->element_count_total = htonl (element_count);
280 msg->element_count_contained = htonl (todo);
281 msg->session_key = *session_key;
282 GNUNET_memcpy (&msg[1],
285 element_count_transfered = todo;
286 GNUNET_MQ_send (h->mq,
288 possible = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - sizeof (*mmsg))
289 / sizeof (struct GNUNET_SCALARPRODUCT_Element);
290 while (element_count_transfered < element_count)
292 todo = GNUNET_MIN (possible,
293 element_count - element_count_transfered);
294 size = todo * sizeof (struct GNUNET_SCALARPRODUCT_Element);
295 env = GNUNET_MQ_msg_extra (mmsg,
297 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_BOB);
298 mmsg->element_count_contained = htonl (todo);
299 GNUNET_memcpy (&mmsg[1],
300 &elements[element_count_transfered],
302 element_count_transfered += todo;
303 GNUNET_MQ_send (h->mq,
311 * Handles the RESULT received from the service for a request, should
312 * contain a result MPI value. Called when we participate as "Alice" via
313 * #GNUNET_SCALARPRODUCT_start_computation().
315 * @param h our Handle
316 * @param msg Pointer to the response received
317 * @param status the condition the request was terminated with (eg: disconnect)
320 process_result_message (struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
321 const struct ClientResponseMessage *msg,
322 enum GNUNET_SCALARPRODUCT_ResponseStatus status)
324 uint32_t product_len;
325 gcry_mpi_t result = NULL;
330 if (GNUNET_SCALARPRODUCT_STATUS_SUCCESS == status)
332 result = gcry_mpi_new (0);
334 product_len = ntohl (msg->product_length);
338 if (0 != (rc = gcry_mpi_scan (&num, GCRYMPI_FMT_STD,
343 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR,
346 gcry_mpi_release (result);
348 status = GNUNET_SCALARPRODUCT_STATUS_INVALID_RESPONSE;
352 if (0 < (int32_t) ntohl (msg->range))
353 gcry_mpi_add (result, result, num);
355 gcry_mpi_sub (result, result, num);
356 gcry_mpi_release (num);
360 if (NULL != h->cont_datum)
361 h->cont_datum (h->cont_cls,
365 gcry_mpi_release (result);
366 GNUNET_SCALARPRODUCT_cancel (h);
371 * Request by Alice's client for computing a scalar product
373 * @param cfg the gnunet configuration handle
374 * @param session_key Session key should be unique to the requesting client
375 * @param peer PeerID of the other peer
376 * @param elements Array of elements of the vector
377 * @param element_count Number of elements in the @a elements vector
378 * @param cont Callback function
379 * @param cont_cls Closure for @a cont
380 * @return a new handle for this computation
382 struct GNUNET_SCALARPRODUCT_ComputationHandle *
383 GNUNET_SCALARPRODUCT_start_computation (const struct GNUNET_CONFIGURATION_Handle *cfg,
384 const struct GNUNET_HashCode *session_key,
385 const struct GNUNET_PeerIdentity *peer,
386 const struct GNUNET_SCALARPRODUCT_Element *elements,
387 uint32_t element_count,
388 GNUNET_SCALARPRODUCT_DatumProcessor cont,
391 struct GNUNET_SCALARPRODUCT_ComputationHandle *h
392 = GNUNET_new (struct GNUNET_SCALARPRODUCT_ComputationHandle);
393 struct GNUNET_MQ_MessageHandler handlers[] = {
394 GNUNET_MQ_hd_var_size (response,
395 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT,
396 struct ClientResponseMessage,
398 GNUNET_MQ_handler_end ()
400 struct GNUNET_MQ_Envelope *env;
401 struct AliceComputationMessage *msg;
402 struct ComputationBobCryptodataMultipartMessage *mmsg;
406 uint32_t element_count_transfered;
408 if (GNUNET_SYSERR == check_unique (elements,
411 h->mq = GNUNET_CLIENT_connect (cfg,
412 "scalarproduct-alice",
418 /* missconfigured scalarproduct service */
423 h->cont_datum = cont;
424 h->cont_cls = cont_cls;
425 h->response_proc = &process_result_message;
427 h->key = *session_key;
429 possible = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - sizeof (struct AliceComputationMessage))
430 / sizeof (struct GNUNET_SCALARPRODUCT_Element);
431 todo = GNUNET_MIN (possible,
433 size = todo * sizeof (struct GNUNET_SCALARPRODUCT_Element);
434 env = GNUNET_MQ_msg_extra (msg,
436 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE);
437 msg->element_count_total = htonl (element_count);
438 msg->element_count_contained = htonl (todo);
439 msg->reserved = htonl (0);
441 msg->session_key = *session_key;
442 GNUNET_memcpy (&msg[1],
445 GNUNET_MQ_send (h->mq,
447 element_count_transfered = todo;
448 possible = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - sizeof (*mmsg))
449 / sizeof (struct GNUNET_SCALARPRODUCT_Element);
450 while (element_count_transfered < element_count)
452 todo = GNUNET_MIN (possible,
453 element_count - element_count_transfered);
454 size = todo * sizeof (struct GNUNET_SCALARPRODUCT_Element);
455 env = GNUNET_MQ_msg_extra (mmsg,
457 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_ALICE);
458 mmsg->element_count_contained = htonl (todo);
459 GNUNET_memcpy (&mmsg[1],
460 &elements[element_count_transfered],
462 element_count_transfered += todo;
463 GNUNET_MQ_send (h->mq,
471 * Cancel an ongoing computation or revoke our collaboration offer.
472 * Closes the connection to the service
474 * @param h computation handle to terminate
477 GNUNET_SCALARPRODUCT_cancel (struct GNUNET_SCALARPRODUCT_ComputationHandle *h)
481 GNUNET_MQ_destroy (h->mq);
488 /* end of scalarproduct_api.c */