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 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.
16 * @file scalarproduct/scalarproduct_api.c
17 * @brief API for the scalarproduct
18 * @author Christian Fuchs
19 * @author Gaurav Kukreja
20 * @author Christian Grothoff
23 #include "gnunet_util_lib.h"
24 #include "gnunet_statistics_service.h"
25 #include "gnunet_scalarproduct_service.h"
26 #include "gnunet_protocols.h"
27 #include "scalarproduct.h"
29 #define LOG(kind,...) GNUNET_log_from (kind, "scalarproduct-api",__VA_ARGS__)
33 * The abstraction function for our internal callback
35 * @param h computation handle
36 * @param msg response we got, NULL on errors
37 * @param status processing status code
40 (*GNUNET_SCALARPRODUCT_ResponseMessageHandler) (struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
41 const struct ClientResponseMessage *msg,
42 enum GNUNET_SCALARPRODUCT_ResponseStatus status);
46 * A handle returned for each computation
48 struct GNUNET_SCALARPRODUCT_ComputationHandle
53 const struct GNUNET_CONFIGURATION_Handle *cfg;
56 * Current connection to the scalarproduct service.
58 struct GNUNET_MQ_Handle *mq;
61 * Function to call after transmission of the request (Bob).
63 GNUNET_SCALARPRODUCT_ContinuationWithStatus cont_status;
66 * Function to call after transmission of the request (Alice).
68 GNUNET_SCALARPRODUCT_DatumProcessor cont_datum;
71 * Closure for @e cont_status or @e cont_datum.
76 * API internal callback for results and failures to be forwarded to
79 GNUNET_SCALARPRODUCT_ResponseMessageHandler response_proc;
82 * The shared session key identifying this computation
84 struct GNUNET_HashCode key;
90 * Called when a response is received from the service. Perform basic
91 * check that the message is well-formed.
93 * @param cls Pointer to the Master Context
94 * @param message Pointer to the data received in response
95 * @return #GNUNET_OK if @a message is well-formed
98 check_response (void *cls,
99 const struct ClientResponseMessage *message)
101 if (ntohs (message->header.size) !=
102 ntohl (message->product_length) + sizeof (struct ClientResponseMessage))
105 return GNUNET_SYSERR;
112 * Handles the STATUS received from the service for a response, does
113 * not contain a payload. Called when we participate as "Bob" via
114 * #GNUNET_SCALARPRODUCT_accept_computation().
116 * @param h our Handle
117 * @param msg the response received
118 * @param status the condition the request was terminated with (eg: disconnect)
121 process_status_message (struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
122 const struct ClientResponseMessage *msg,
123 enum GNUNET_SCALARPRODUCT_ResponseStatus status)
125 if (NULL != h->cont_status)
126 h->cont_status (h->cont_cls,
128 GNUNET_SCALARPRODUCT_cancel (h);
133 * Called when a response is received from the service. After basic
134 * check, the handler in `h->response_proc` is called. This functions
135 * handles the response to the client which used the API.
137 * @param cls Pointer to the Master Context
138 * @param msg Pointer to the data received in response
141 handle_response (void *cls,
142 const struct ClientResponseMessage *message)
144 struct GNUNET_SCALARPRODUCT_ComputationHandle *h = cls;
145 enum GNUNET_SCALARPRODUCT_ResponseStatus status;
147 status = (enum GNUNET_SCALARPRODUCT_ResponseStatus) ntohl (message->status);
155 * Check if the keys for all given elements are unique.
157 * @param elements elements to check
158 * @param element_count size of the @a elements array
159 * @return #GNUNET_OK if all keys are unique
162 check_unique (const struct GNUNET_SCALARPRODUCT_Element *elements,
163 uint32_t element_count)
165 struct GNUNET_CONTAINER_MultiHashMap *map;
170 map = GNUNET_CONTAINER_multihashmap_create (2 * element_count,
172 for (i=0;i<element_count;i++)
174 GNUNET_CONTAINER_multihashmap_put (map,
177 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
179 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
180 _("Keys given to SCALARPRODUCT not unique!\n"));
183 GNUNET_CONTAINER_multihashmap_destroy (map);
189 * We encountered an error communicating with the set service while
190 * performing a set operation. Report to the application.
192 * @param cls the `struct GNUNET_SCALARPRODUCT_ComputationHandle`
193 * @param error error code
196 mq_error_handler (void *cls,
197 enum GNUNET_MQ_Error error)
199 struct GNUNET_SCALARPRODUCT_ComputationHandle *h = cls;
201 LOG (GNUNET_ERROR_TYPE_INFO,
202 "Disconnected from SCALARPRODUCT service.\n");
205 GNUNET_SCALARPRODUCT_STATUS_DISCONNECTED);
210 * Used by Bob's client to cooperate with Alice,
212 * @param cfg the gnunet configuration handle
213 * @param key Session key unique to the requesting client
214 * @param elements Array of elements of the vector
215 * @param element_count Number of elements in the @a elements vector
216 * @param cont Callback function
217 * @param cont_cls Closure for @a cont
218 * @return a new handle for this computation
220 struct GNUNET_SCALARPRODUCT_ComputationHandle *
221 GNUNET_SCALARPRODUCT_accept_computation (const struct GNUNET_CONFIGURATION_Handle *cfg,
222 const struct GNUNET_HashCode *session_key,
223 const struct GNUNET_SCALARPRODUCT_Element *elements,
224 uint32_t element_count,
225 GNUNET_SCALARPRODUCT_ContinuationWithStatus cont,
228 struct GNUNET_SCALARPRODUCT_ComputationHandle *h
229 = GNUNET_new (struct GNUNET_SCALARPRODUCT_ComputationHandle);
230 struct GNUNET_MQ_MessageHandler handlers[] = {
231 GNUNET_MQ_hd_var_size (response,
232 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT,
233 struct ClientResponseMessage,
235 GNUNET_MQ_handler_end ()
237 struct GNUNET_MQ_Envelope *env;
238 struct BobComputationMessage *msg;
239 struct ComputationBobCryptodataMultipartMessage *mmsg;
243 uint32_t element_count_transfered;
246 if (GNUNET_SYSERR == check_unique (elements,
249 h->cont_status = cont;
250 h->cont_cls = cont_cls;
251 h->response_proc = &process_status_message;
253 h->key = *session_key;
254 h->mq = GNUNET_CLIENT_connect (cfg,
261 /* scalarproduct configuration error */
266 possible = (GNUNET_MAX_MESSAGE_SIZE - 1 - sizeof (struct BobComputationMessage))
267 / sizeof (struct GNUNET_SCALARPRODUCT_Element);
268 todo = GNUNET_MIN (possible,
270 size = todo * sizeof (struct GNUNET_SCALARPRODUCT_Element);
271 env = GNUNET_MQ_msg_extra (msg,
273 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_BOB);
274 msg->element_count_total = htonl (element_count);
275 msg->element_count_contained = htonl (todo);
276 msg->session_key = *session_key;
277 GNUNET_memcpy (&msg[1],
280 element_count_transfered = todo;
281 GNUNET_MQ_send (h->mq,
283 possible = (GNUNET_MAX_MESSAGE_SIZE - 1 - sizeof (*mmsg))
284 / sizeof (struct GNUNET_SCALARPRODUCT_Element);
285 while (element_count_transfered < element_count)
287 todo = GNUNET_MIN (possible,
288 element_count - element_count_transfered);
289 size = todo * sizeof (struct GNUNET_SCALARPRODUCT_Element);
290 env = GNUNET_MQ_msg_extra (mmsg,
292 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_BOB);
293 mmsg->element_count_contained = htonl (todo);
294 GNUNET_memcpy (&mmsg[1],
295 &elements[element_count_transfered],
297 element_count_transfered += todo;
298 GNUNET_MQ_send (h->mq,
306 * Handles the RESULT received from the service for a request, should
307 * contain a result MPI value. Called when we participate as "Alice" via
308 * #GNUNET_SCALARPRODUCT_start_computation().
310 * @param h our Handle
311 * @param msg Pointer to the response received
312 * @param status the condition the request was terminated with (eg: disconnect)
315 process_result_message (struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
316 const struct ClientResponseMessage *msg,
317 enum GNUNET_SCALARPRODUCT_ResponseStatus status)
319 uint32_t product_len;
320 gcry_mpi_t result = NULL;
325 if (GNUNET_SCALARPRODUCT_STATUS_SUCCESS == status)
327 result = gcry_mpi_new (0);
329 product_len = ntohl (msg->product_length);
333 if (0 != (rc = gcry_mpi_scan (&num, GCRYMPI_FMT_STD,
338 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR,
341 gcry_mpi_release (result);
343 status = GNUNET_SCALARPRODUCT_STATUS_INVALID_RESPONSE;
347 if (0 < (int32_t) ntohl (msg->range))
348 gcry_mpi_add (result, result, num);
350 gcry_mpi_sub (result, result, num);
351 gcry_mpi_release (num);
355 if (NULL != h->cont_datum)
356 h->cont_datum (h->cont_cls,
360 gcry_mpi_release (result);
361 GNUNET_SCALARPRODUCT_cancel (h);
366 * Request by Alice's client for computing a scalar product
368 * @param cfg the gnunet configuration handle
369 * @param session_key Session key should be unique to the requesting client
370 * @param peer PeerID of the other peer
371 * @param elements Array of elements of the vector
372 * @param element_count Number of elements in the @a elements vector
373 * @param cont Callback function
374 * @param cont_cls Closure for @a cont
375 * @return a new handle for this computation
377 struct GNUNET_SCALARPRODUCT_ComputationHandle *
378 GNUNET_SCALARPRODUCT_start_computation (const struct GNUNET_CONFIGURATION_Handle *cfg,
379 const struct GNUNET_HashCode *session_key,
380 const struct GNUNET_PeerIdentity *peer,
381 const struct GNUNET_SCALARPRODUCT_Element *elements,
382 uint32_t element_count,
383 GNUNET_SCALARPRODUCT_DatumProcessor cont,
386 struct GNUNET_SCALARPRODUCT_ComputationHandle *h
387 = GNUNET_new (struct GNUNET_SCALARPRODUCT_ComputationHandle);
388 struct GNUNET_MQ_MessageHandler handlers[] = {
389 GNUNET_MQ_hd_var_size (response,
390 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT,
391 struct ClientResponseMessage,
393 GNUNET_MQ_handler_end ()
395 struct GNUNET_MQ_Envelope *env;
396 struct AliceComputationMessage *msg;
397 struct ComputationBobCryptodataMultipartMessage *mmsg;
401 uint32_t element_count_transfered;
403 if (GNUNET_SYSERR == check_unique (elements,
406 h->mq = GNUNET_CLIENT_connect (cfg,
407 "scalarproduct-alice",
413 /* missconfigured scalarproduct service */
418 h->cont_datum = cont;
419 h->cont_cls = cont_cls;
420 h->response_proc = &process_result_message;
422 h->key = *session_key;
424 possible = (GNUNET_MAX_MESSAGE_SIZE - 1 - sizeof (struct AliceComputationMessage))
425 / sizeof (struct GNUNET_SCALARPRODUCT_Element);
426 todo = GNUNET_MIN (possible,
428 size = todo * sizeof (struct GNUNET_SCALARPRODUCT_Element);
429 env = GNUNET_MQ_msg_extra (msg,
431 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE);
432 msg->element_count_total = htonl (element_count);
433 msg->element_count_contained = htonl (todo);
434 msg->reserved = htonl (0);
436 msg->session_key = *session_key;
437 GNUNET_memcpy (&msg[1],
440 GNUNET_MQ_send (h->mq,
442 element_count_transfered = todo;
443 possible = (GNUNET_MAX_MESSAGE_SIZE - 1 - sizeof (*mmsg))
444 / sizeof (struct GNUNET_SCALARPRODUCT_Element);
445 while (element_count_transfered < element_count)
447 todo = GNUNET_MIN (possible,
448 element_count - element_count_transfered);
449 size = todo * sizeof (struct GNUNET_SCALARPRODUCT_Element);
450 env = GNUNET_MQ_msg_extra (mmsg,
452 GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_ALICE);
453 mmsg->element_count_contained = htonl (todo);
454 GNUNET_memcpy (&mmsg[1],
455 &elements[element_count_transfered],
457 element_count_transfered += todo;
458 GNUNET_MQ_send (h->mq,
466 * Cancel an ongoing computation or revoke our collaboration offer.
467 * Closes the connection to the service
469 * @param h computation handle to terminate
472 GNUNET_SCALARPRODUCT_cancel (struct GNUNET_SCALARPRODUCT_ComputationHandle *h)
476 GNUNET_MQ_destroy (h->mq);
483 /* end of scalarproduct_api.c */