2 This file is part of GNUnet.
3 (C) 2013, 2014 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.
21 * @file scalarproduct/scalarproduct_api.c
22 * @brief API for the scalarproduct
23 * @author Christian Fuchs
24 * @author Gaurav Kukreja
25 * @author Christian Grothoff
30 #include "gnunet_util_lib.h"
31 #include "gnunet_statistics_service.h"
32 #include "gnunet_scalarproduct_service.h"
33 #include "gnunet_protocols.h"
34 #include "scalarproduct.h"
36 #define LOG(kind,...) GNUNET_log_from (kind, "scalarproduct-api",__VA_ARGS__)
40 * The abstraction function for our internal callback
42 * @param h computation handle
43 * @param msg response we got, NULL on errors
44 * @param status processing status code
47 (*GNUNET_SCALARPRODUCT_ResponseMessageHandler) (struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
48 const struct ClientResponseMessage *msg,
49 enum GNUNET_SCALARPRODUCT_ResponseStatus status);
53 * A handle returned for each computation
55 struct GNUNET_SCALARPRODUCT_ComputationHandle
60 const struct GNUNET_CONFIGURATION_Handle *cfg;
63 * Current connection to the scalarproduct service.
65 struct GNUNET_CLIENT_Connection *client;
68 * Current transmit handle.
70 struct GNUNET_CLIENT_TransmitHandle *th;
73 * the client's elements which
75 struct GNUNET_SCALARPRODUCT_Element *elements;
78 * Message to be sent to the scalarproduct service
80 struct GNUNET_MessageHeader *msg;
83 * Function to call after transmission of the request (Bob).
85 GNUNET_SCALARPRODUCT_ContinuationWithStatus cont_status;
88 * Function to call after transmission of the request (Alice).
90 GNUNET_SCALARPRODUCT_DatumProcessor cont_datum;
93 * Closure for @e cont_status or @e cont_datum.
98 * API internal callback for results and failures to be forwarded to
101 GNUNET_SCALARPRODUCT_ResponseMessageHandler response_proc;
104 * The shared session key identifying this computation
106 struct GNUNET_HashCode key;
109 * count of all @e elements we offer for computation
111 uint32_t element_count_total;
114 * count of the transfered @e elements we offer for computation
116 uint32_t element_count_transfered;
119 * Type to use for the multipart messages.
127 * Called when a response is received from the service. After basic
128 * check, the handler in `h->response_proc` is called. This functions
129 * handles the response to the client which used the API.
131 * @param cls Pointer to the Master Context
132 * @param msg Pointer to the data received in response
135 receive_cb (void *cls,
136 const struct GNUNET_MessageHeader *msg)
138 struct GNUNET_SCALARPRODUCT_ComputationHandle *h = cls;
139 const struct ClientResponseMessage *message;
140 enum GNUNET_SCALARPRODUCT_ResponseStatus status;
144 LOG (GNUNET_ERROR_TYPE_INFO,
145 "Disconnected from SCALARPRODUCT service.\n");
148 GNUNET_SCALARPRODUCT_STATUS_DISCONNECTED);
151 if (ntohs (msg->size) < sizeof (struct ClientResponseMessage))
156 GNUNET_SCALARPRODUCT_STATUS_INVALID_RESPONSE);
159 message = (const struct ClientResponseMessage *) msg;
160 if (ntohs (msg->size) !=
161 ntohl (message->product_length) + sizeof (struct ClientResponseMessage))
166 GNUNET_SCALARPRODUCT_STATUS_INVALID_RESPONSE);
169 status = (enum GNUNET_SCALARPRODUCT_ResponseStatus) ntohl (message->status);
177 * Transmits the request to the SCALARPRODUCT service
179 * @param cls Closure with the `struct GNUNET_SCALARPRODUCT_ComputationHandle`
180 * @param size Size of the buffer @a buf
181 * @param buf Pointer to the buffer
182 * @return Size of the message sent
185 do_send_message (void *cls,
189 struct GNUNET_SCALARPRODUCT_ComputationHandle *h = cls;
190 struct ComputationBobCryptodataMultipartMessage *msg;
198 LOG (GNUNET_ERROR_TYPE_DEBUG,
199 "Failed to transmit request to SCALARPRODUCT.\n");
200 /* notify caller about the error, done here */
201 h->response_proc (h, NULL,
202 GNUNET_SCALARPRODUCT_STATUS_FAILURE);
205 ret = ntohs (h->msg->size);
206 memcpy (buf, h->msg, ret);
207 GNUNET_free (h->msg);
211 if (h->element_count_total == h->element_count_transfered)
213 GNUNET_CLIENT_receive (h->client,
215 GNUNET_TIME_UNIT_FOREVER_REL);
219 todo = h->element_count_total - h->element_count_transfered;
220 nsize = sizeof (struct ComputationBobCryptodataMultipartMessage)
221 + todo * sizeof (struct GNUNET_SCALARPRODUCT_Element);
222 if (GNUNET_SERVER_MAX_MESSAGE_SIZE <= size)
224 /* cannot do all of them, limit to what is possible in one message */
225 todo = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - sizeof (struct ComputationBobCryptodataMultipartMessage))
226 / sizeof (struct GNUNET_SCALARPRODUCT_Element);
227 nsize = sizeof (struct ComputationBobCryptodataMultipartMessage)
228 + todo * sizeof (struct GNUNET_SCALARPRODUCT_Element);
231 msg = GNUNET_malloc (nsize);
232 h->msg = &msg->header;
233 msg->header.size = htons (nsize);
234 msg->header.type = htons (h->mp_type);
235 msg->element_count_contained = htonl (todo);
237 &h->elements[h->element_count_transfered],
238 todo * sizeof (struct GNUNET_SCALARPRODUCT_Element));
239 h->element_count_transfered += todo;
240 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, nsize,
241 GNUNET_TIME_UNIT_FOREVER_REL,
243 &do_send_message, h);
244 GNUNET_assert (NULL != h->th);
250 * Handles the STATUS received from the service for a response, does
251 * not contain a payload. Called when we participate as "Bob" via
252 * #GNUNET_SCALARPRODUCT_accept_computation().
254 * @param h our Handle
255 * @param msg the response received
256 * @param status the condition the request was terminated with (eg: disconnect)
259 process_status_message (struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
260 const struct ClientResponseMessage *msg,
261 enum GNUNET_SCALARPRODUCT_ResponseStatus status)
263 if (NULL != h->cont_status)
264 h->cont_status (h->cont_cls,
266 GNUNET_SCALARPRODUCT_cancel (h);
271 * Used by Bob's client to cooperate with Alice,
273 * @param cfg the gnunet configuration handle
274 * @param key Session key unique to the requesting client
275 * @param elements Array of elements of the vector
276 * @param element_count Number of elements in the @a elements vector
277 * @param cont Callback function
278 * @param cont_cls Closure for @a cont
279 * @return a new handle for this computation
281 struct GNUNET_SCALARPRODUCT_ComputationHandle *
282 GNUNET_SCALARPRODUCT_accept_computation (const struct GNUNET_CONFIGURATION_Handle *cfg,
283 const struct GNUNET_HashCode *session_key,
284 const struct GNUNET_SCALARPRODUCT_Element *elements,
285 uint32_t element_count,
286 GNUNET_SCALARPRODUCT_ContinuationWithStatus cont,
289 struct GNUNET_SCALARPRODUCT_ComputationHandle *h;
290 struct BobComputationMessage *msg;
294 h = GNUNET_new (struct GNUNET_SCALARPRODUCT_ComputationHandle);
295 h->cont_status = cont;
296 h->cont_cls = cont_cls;
297 h->response_proc = &process_status_message;
299 h->key = *session_key;
300 h->client = GNUNET_CLIENT_connect ("scalarproduct-bob", cfg);
301 h->element_count_total = element_count;
302 h->mp_type = GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MUTLIPART_BOB;
303 if (NULL == h->client)
305 /* scalarproduct configuration error */
310 size = sizeof (struct BobComputationMessage)
311 + element_count * sizeof (struct GNUNET_SCALARPRODUCT_Element);
312 if (GNUNET_SERVER_MAX_MESSAGE_SIZE > size)
314 possible = element_count;
315 h->element_count_transfered = element_count;
319 /* create a multipart msg, first we calculate a new msg size for the head msg */
320 possible = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - sizeof (struct BobComputationMessage))
321 / sizeof (struct GNUNET_SCALARPRODUCT_Element);
322 h->element_count_transfered = possible;
323 size = sizeof (struct BobComputationMessage)
324 + possible * sizeof (struct GNUNET_SCALARPRODUCT_Element);
325 h->elements = GNUNET_malloc (sizeof(struct GNUNET_SCALARPRODUCT_Element) * element_count);
328 sizeof (struct GNUNET_SCALARPRODUCT_Element) * element_count);
331 msg = GNUNET_malloc (size);
332 h->msg = &msg->header;
333 msg->header.size = htons (size);
334 msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_BOB);
335 msg->element_count_total = htonl (element_count);
336 msg->element_count_contained = htonl (possible);
337 msg->session_key = *session_key;
340 possible * sizeof (struct GNUNET_SCALARPRODUCT_Element));
341 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, size,
342 GNUNET_TIME_UNIT_FOREVER_REL,
343 GNUNET_YES, /* retry is OK in the initial stage */
344 &do_send_message, h);
345 GNUNET_assert (NULL != h->th);
351 * Handles the RESULT received from the service for a request, should
352 * contain a result MPI value. Called when we participate as "Alice" via
353 * #GNUNET_SCALARPRODUCT_start_computation().
355 * @param h our Handle
356 * @param msg Pointer to the response received
357 * @param status the condition the request was terminated with (eg: disconnect)
360 process_result_message (struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
361 const struct ClientResponseMessage *msg,
362 enum GNUNET_SCALARPRODUCT_ResponseStatus status)
364 uint32_t product_len;
365 gcry_mpi_t result = NULL;
370 if (GNUNET_SCALARPRODUCT_STATUS_SUCCESS == status)
372 result = gcry_mpi_new (0);
374 product_len = ntohl (msg->product_length);
378 if (0 != (rc = gcry_mpi_scan (&num, GCRYMPI_FMT_STD,
383 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR,
386 gcry_mpi_release (result);
388 status = GNUNET_SCALARPRODUCT_STATUS_INVALID_RESPONSE;
392 if (0 < ntohl (msg->range))
393 gcry_mpi_add (result, result, num);
394 else if (0 > ntohl (msg->range))
395 gcry_mpi_sub (result, result, num);
396 gcry_mpi_release (num);
400 if (NULL != h->cont_datum)
401 h->cont_datum (h->cont_cls,
405 gcry_mpi_release (result);
406 GNUNET_SCALARPRODUCT_cancel (h);
411 * Request by Alice's client for computing a scalar product
413 * @param cfg the gnunet configuration handle
414 * @param session_key Session key should be unique to the requesting client
415 * @param peer PeerID of the other peer
416 * @param elements Array of elements of the vector
417 * @param element_count Number of elements in the @a elements vector
418 * @param cont Callback function
419 * @param cont_cls Closure for @a cont
420 * @return a new handle for this computation
422 struct GNUNET_SCALARPRODUCT_ComputationHandle *
423 GNUNET_SCALARPRODUCT_start_computation (const struct GNUNET_CONFIGURATION_Handle *cfg,
424 const struct GNUNET_HashCode *session_key,
425 const struct GNUNET_PeerIdentity *peer,
426 const struct GNUNET_SCALARPRODUCT_Element *elements,
427 uint32_t element_count,
428 GNUNET_SCALARPRODUCT_DatumProcessor cont,
431 struct GNUNET_SCALARPRODUCT_ComputationHandle *h;
432 struct AliceComputationMessage *msg;
436 h = GNUNET_new (struct GNUNET_SCALARPRODUCT_ComputationHandle);
437 h->client = GNUNET_CLIENT_connect ("scalarproduct-alice", cfg);
438 if (NULL == h->client)
440 /* missconfigured scalarproduct service */
445 h->element_count_total = element_count;
446 h->cont_datum = cont;
447 h->cont_cls = cont_cls;
448 h->response_proc = &process_result_message;
450 h->key = *session_key;
451 h->mp_type = GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MUTLIPART_ALICE;
452 size = sizeof (struct AliceComputationMessage)
453 + element_count * sizeof (struct GNUNET_SCALARPRODUCT_Element);
454 if (GNUNET_SERVER_MAX_MESSAGE_SIZE > size)
456 possible = element_count;
457 h->element_count_transfered = element_count;
461 /* create a multipart msg, first we calculate a new msg size for the head msg */
462 possible = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - sizeof (struct AliceComputationMessage))
463 / sizeof (struct GNUNET_SCALARPRODUCT_Element);
464 h->element_count_transfered = possible;
465 size = sizeof (struct AliceComputationMessage)
466 + possible * sizeof (struct GNUNET_SCALARPRODUCT_Element);
467 h->elements = GNUNET_malloc (sizeof(struct GNUNET_SCALARPRODUCT_Element) * element_count);
470 sizeof (struct GNUNET_SCALARPRODUCT_Element) * element_count);
473 msg = GNUNET_malloc (size);
474 h->msg = &msg->header;
475 msg->header.size = htons (size);
476 msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE);
477 msg->element_count_total = htonl (element_count);
478 msg->element_count_contained = htonl (possible);
479 msg->reserved = htonl (0);
481 msg->session_key = *session_key;
484 sizeof (struct GNUNET_SCALARPRODUCT_Element) * possible);
485 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, size,
486 GNUNET_TIME_UNIT_FOREVER_REL,
487 GNUNET_YES, /* retry is OK in the initial stage */
488 &do_send_message, h);
489 GNUNET_assert (NULL != h->th);
495 * Cancel an ongoing computation or revoke our collaboration offer.
496 * Closes the connection to the service
498 * @param h computation handle to terminate
501 GNUNET_SCALARPRODUCT_cancel (struct GNUNET_SCALARPRODUCT_ComputationHandle *h)
505 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
508 GNUNET_free_non_null (h->elements);
509 GNUNET_free_non_null (h->msg);
510 if (NULL != h->client)
512 GNUNET_CLIENT_disconnect (h->client);
519 /* end of scalarproduct_api.c */