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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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
46 GNUNET_SCALARPRODUCT_ComputationHandle
49 ClientResponseMessage *msg,
51 GNUNET_SCALARPRODUCT_ResponseStatus
56 * A handle returned for each computation
58 struct GNUNET_SCALARPRODUCT_ComputationHandle
63 const struct GNUNET_CONFIGURATION_Handle *cfg;
66 * Current connection to the scalarproduct service.
68 struct GNUNET_MQ_Handle *mq;
71 * Function to call after transmission of the request (Bob).
73 GNUNET_SCALARPRODUCT_ContinuationWithStatus cont_status;
76 * Function to call after transmission of the request (Alice).
78 GNUNET_SCALARPRODUCT_DatumProcessor cont_datum;
81 * Closure for @e cont_status or @e cont_datum.
86 * API internal callback for results and failures to be forwarded to
89 GNUNET_SCALARPRODUCT_ResponseMessageHandler response_proc;
92 * The shared session key identifying this computation
94 struct GNUNET_HashCode key;
99 * Called when a response is received from the service. Perform basic
100 * check that the message is well-formed.
102 * @param cls Pointer to the Master Context
103 * @param message Pointer to the data received in response
104 * @return #GNUNET_OK if @a message is well-formed
107 check_response (void *cls,
108 const struct ClientResponseMessage *message)
110 if (ntohs (message->header.size) !=
111 ntohl (message->product_length) + sizeof(struct ClientResponseMessage))
114 return GNUNET_SYSERR;
121 * Handles the STATUS received from the service for a response, does
122 * not contain a payload. Called when we participate as "Bob" via
123 * #GNUNET_SCALARPRODUCT_accept_computation().
125 * @param h our Handle
126 * @param msg the response received
127 * @param status the condition the request was terminated with (eg: disconnect)
130 process_status_message (struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
131 const struct ClientResponseMessage *msg,
132 enum GNUNET_SCALARPRODUCT_ResponseStatus status)
134 if (NULL != h->cont_status)
135 h->cont_status (h->cont_cls,
137 GNUNET_SCALARPRODUCT_cancel (h);
142 * Called when a response is received from the service. After basic
143 * check, the handler in `h->response_proc` is called. This functions
144 * handles the response to the client which used the API.
146 * @param cls Pointer to the Master Context
147 * @param msg Pointer to the data received in response
150 handle_response (void *cls,
151 const struct ClientResponseMessage *message)
153 struct GNUNET_SCALARPRODUCT_ComputationHandle *h = cls;
154 enum GNUNET_SCALARPRODUCT_ResponseStatus status;
156 status = (enum GNUNET_SCALARPRODUCT_ResponseStatus) ntohl (message->status);
164 * Check if the keys for all given elements are unique.
166 * @param elements elements to check
167 * @param element_count size of the @a elements array
168 * @return #GNUNET_OK if all keys are unique
171 check_unique (const struct GNUNET_SCALARPRODUCT_Element *elements,
172 uint32_t element_count)
174 struct GNUNET_CONTAINER_MultiHashMap *map;
179 map = GNUNET_CONTAINER_multihashmap_create (2 * element_count,
181 for (i = 0; i < element_count; i++)
183 GNUNET_CONTAINER_multihashmap_put (map,
186 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
188 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
189 _ ("Keys given to SCALARPRODUCT not unique!\n"));
192 GNUNET_CONTAINER_multihashmap_destroy (map);
198 * We encountered an error communicating with the set service while
199 * performing a set operation. Report to the application.
201 * @param cls the `struct GNUNET_SCALARPRODUCT_ComputationHandle`
202 * @param error error code
205 mq_error_handler (void *cls,
206 enum GNUNET_MQ_Error error)
208 struct GNUNET_SCALARPRODUCT_ComputationHandle *h = cls;
210 LOG (GNUNET_ERROR_TYPE_INFO,
211 "Disconnected from SCALARPRODUCT service.\n");
214 GNUNET_SCALARPRODUCT_STATUS_DISCONNECTED);
219 * Used by Bob's client to cooperate with Alice,
221 * @param cfg the gnunet configuration handle
222 * @param key Session key unique to the requesting client
223 * @param elements Array of elements of the vector
224 * @param element_count Number of elements in the @a elements vector
225 * @param cont Callback function
226 * @param cont_cls Closure for @a cont
227 * @return a new handle for this computation
229 struct GNUNET_SCALARPRODUCT_ComputationHandle *
230 GNUNET_SCALARPRODUCT_accept_computation (const struct
231 GNUNET_CONFIGURATION_Handle *cfg,
233 GNUNET_HashCode *session_key,
235 GNUNET_SCALARPRODUCT_Element *elements,
236 uint32_t element_count,
237 GNUNET_SCALARPRODUCT_ContinuationWithStatus
241 struct GNUNET_SCALARPRODUCT_ComputationHandle *h
242 = GNUNET_new (struct GNUNET_SCALARPRODUCT_ComputationHandle);
243 struct GNUNET_MQ_MessageHandler handlers[] = {
244 GNUNET_MQ_hd_var_size (response,
245 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT,
246 struct ClientResponseMessage,
248 GNUNET_MQ_handler_end ()
250 struct GNUNET_MQ_Envelope *env;
251 struct BobComputationMessage *msg;
252 struct ComputationBobCryptodataMultipartMessage *mmsg;
256 uint32_t element_count_transfered;
259 if (GNUNET_SYSERR == check_unique (elements,
262 h->cont_status = cont;
263 h->cont_cls = cont_cls;
264 h->response_proc = &process_status_message;
266 h->key = *session_key;
267 h->mq = GNUNET_CLIENT_connect (cfg,
274 /* scalarproduct configuration error */
279 possible = (GNUNET_MAX_MESSAGE_SIZE - 1 - sizeof(struct
280 BobComputationMessage))
281 / sizeof(struct GNUNET_SCALARPRODUCT_Element);
282 todo = GNUNET_MIN (possible,
284 size = todo * sizeof(struct GNUNET_SCALARPRODUCT_Element);
285 env = GNUNET_MQ_msg_extra (msg,
287 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_BOB);
288 msg->element_count_total = htonl (element_count);
289 msg->element_count_contained = htonl (todo);
290 msg->session_key = *session_key;
291 GNUNET_memcpy (&msg[1],
294 element_count_transfered = todo;
295 GNUNET_MQ_send (h->mq,
297 possible = (GNUNET_MAX_MESSAGE_SIZE - 1 - sizeof(*mmsg))
298 / sizeof(struct GNUNET_SCALARPRODUCT_Element);
299 while (element_count_transfered < element_count)
301 todo = GNUNET_MIN (possible,
302 element_count - element_count_transfered);
303 size = todo * sizeof(struct GNUNET_SCALARPRODUCT_Element);
304 env = GNUNET_MQ_msg_extra (mmsg,
306 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_BOB);
307 mmsg->element_count_contained = htonl (todo);
308 GNUNET_memcpy (&mmsg[1],
309 &elements[element_count_transfered],
311 element_count_transfered += todo;
312 GNUNET_MQ_send (h->mq,
320 * Handles the RESULT received from the service for a request, should
321 * contain a result MPI value. Called when we participate as "Alice" via
322 * #GNUNET_SCALARPRODUCT_start_computation().
324 * @param h our Handle
325 * @param msg Pointer to the response received
326 * @param status the condition the request was terminated with (eg: disconnect)
329 process_result_message (struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
330 const struct ClientResponseMessage *msg,
331 enum GNUNET_SCALARPRODUCT_ResponseStatus status)
333 uint32_t product_len;
334 gcry_mpi_t result = NULL;
339 if (GNUNET_SCALARPRODUCT_STATUS_SUCCESS == status)
341 result = gcry_mpi_new (0);
343 product_len = ntohl (msg->product_length);
347 if (0 != (rc = gcry_mpi_scan (&num, GCRYMPI_FMT_STD,
352 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR,
355 gcry_mpi_release (result);
357 status = GNUNET_SCALARPRODUCT_STATUS_INVALID_RESPONSE;
361 if (0 < (int32_t) ntohl (msg->range))
362 gcry_mpi_add (result, result, num);
364 gcry_mpi_sub (result, result, num);
365 gcry_mpi_release (num);
369 if (NULL != h->cont_datum)
370 h->cont_datum (h->cont_cls,
374 gcry_mpi_release (result);
375 GNUNET_SCALARPRODUCT_cancel (h);
380 * Request by Alice's client for computing a scalar product
382 * @param cfg the gnunet configuration handle
383 * @param session_key Session key should be unique to the requesting client
384 * @param peer PeerID of the other peer
385 * @param elements Array of elements of the vector
386 * @param element_count Number of elements in the @a elements vector
387 * @param cont Callback function
388 * @param cont_cls Closure for @a cont
389 * @return a new handle for this computation
391 struct GNUNET_SCALARPRODUCT_ComputationHandle *
392 GNUNET_SCALARPRODUCT_start_computation (const struct
393 GNUNET_CONFIGURATION_Handle *cfg,
395 GNUNET_HashCode *session_key,
396 const struct GNUNET_PeerIdentity *peer,
398 GNUNET_SCALARPRODUCT_Element *elements,
399 uint32_t element_count,
400 GNUNET_SCALARPRODUCT_DatumProcessor cont,
403 struct GNUNET_SCALARPRODUCT_ComputationHandle *h
404 = GNUNET_new (struct GNUNET_SCALARPRODUCT_ComputationHandle);
405 struct GNUNET_MQ_MessageHandler handlers[] = {
406 GNUNET_MQ_hd_var_size (response,
407 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT,
408 struct ClientResponseMessage,
410 GNUNET_MQ_handler_end ()
412 struct GNUNET_MQ_Envelope *env;
413 struct AliceComputationMessage *msg;
414 struct ComputationBobCryptodataMultipartMessage *mmsg;
418 uint32_t element_count_transfered;
420 if (GNUNET_SYSERR == check_unique (elements,
423 h->mq = GNUNET_CLIENT_connect (cfg,
424 "scalarproduct-alice",
430 /* missconfigured scalarproduct service */
435 h->cont_datum = cont;
436 h->cont_cls = cont_cls;
437 h->response_proc = &process_result_message;
439 h->key = *session_key;
441 possible = (GNUNET_MAX_MESSAGE_SIZE - 1 - sizeof(struct
442 AliceComputationMessage))
443 / sizeof(struct GNUNET_SCALARPRODUCT_Element);
444 todo = GNUNET_MIN (possible,
446 size = todo * sizeof(struct GNUNET_SCALARPRODUCT_Element);
447 env = GNUNET_MQ_msg_extra (msg,
449 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE);
450 msg->element_count_total = htonl (element_count);
451 msg->element_count_contained = htonl (todo);
452 msg->reserved = htonl (0);
454 msg->session_key = *session_key;
455 GNUNET_memcpy (&msg[1],
458 GNUNET_MQ_send (h->mq,
460 element_count_transfered = todo;
461 possible = (GNUNET_MAX_MESSAGE_SIZE - 1 - sizeof(*mmsg))
462 / sizeof(struct GNUNET_SCALARPRODUCT_Element);
463 while (element_count_transfered < element_count)
465 todo = GNUNET_MIN (possible,
466 element_count - element_count_transfered);
467 size = todo * sizeof(struct GNUNET_SCALARPRODUCT_Element);
468 env = GNUNET_MQ_msg_extra (mmsg,
470 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_ALICE);
471 mmsg->element_count_contained = htonl (todo);
472 GNUNET_memcpy (&mmsg[1],
473 &elements[element_count_transfered],
475 element_count_transfered += todo;
476 GNUNET_MQ_send (h->mq,
484 * Cancel an ongoing computation or revoke our collaboration offer.
485 * Closes the connection to the service
487 * @param h computation handle to terminate
490 GNUNET_SCALARPRODUCT_cancel (struct GNUNET_SCALARPRODUCT_ComputationHandle *h)
494 GNUNET_MQ_destroy (h->mq);
501 /* end of scalarproduct_api.c */