2 This file is part of GNUnet.
3 (C) 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.
22 * @file scalarproduct/scalarproduct_api.c
23 * @brief API for the scalarproduct
24 * @author Christian Fuchs
25 * @author Gaurav Kukreja
29 #include "gnunet_util_lib.h"
30 #include "gnunet_statistics_service.h"
31 #include "gnunet_scalarproduct_service.h"
32 #include "gnunet_protocols.h"
33 #include "scalarproduct.h"
35 #define LOG(kind,...) GNUNET_log_from (kind, "scalarproduct-api",__VA_ARGS__)
37 /**************************************************************
38 *** Datatype Declarations **********
39 **************************************************************/
42 * the abstraction function for our internal callback
44 typedef void (*GNUNET_SCALARPRODUCT_ResponseMessageHandler) (void *cls,
45 const struct GNUNET_MessageHeader *msg,
46 enum GNUNET_SCALARPRODUCT_ResponseStatus status);
49 * Entry in the request queue per client
51 struct GNUNET_SCALARPRODUCT_ComputationHandle
54 * This is a linked list.
56 struct GNUNET_SCALARPRODUCT_ComputationHandle *next;
59 * This is a linked list.
61 struct GNUNET_SCALARPRODUCT_ComputationHandle *prev;
66 const struct GNUNET_CONFIGURATION_Handle *cfg;
69 * Current connection to the scalarproduct service.
71 struct GNUNET_CLIENT_Connection *client;
74 * Handle for statistics.
76 struct GNUNET_STATISTICS_Handle *stats;
79 * The shared session key identifying this computation
81 struct GNUNET_HashCode * key;
84 * Current transmit handle.
86 struct GNUNET_CLIENT_TransmitHandle *th;
91 uint16_t message_size;
94 * Message to be sent to the scalarproduct service
96 struct GNUNET_SCALARPRODUCT_client_request * msg;
101 * Function to call after transmission of the request.
103 GNUNET_SCALARPRODUCT_ContinuationWithStatus cont_status;
106 * Function to call after transmission of the request.
108 GNUNET_SCALARPRODUCT_DatumProcessor cont_datum;
112 * Closure for 'cont'.
117 * Response Processor for response from the service. This function calls the
118 * continuation function provided by the client.
120 GNUNET_SCALARPRODUCT_ResponseMessageHandler response_proc;
123 /**************************************************************
124 *** Global Variables **********
125 **************************************************************/
127 * Head of the active sessions queue
129 static struct GNUNET_SCALARPRODUCT_ComputationHandle *head;
131 * Tail of the active sessions queue
133 static struct GNUNET_SCALARPRODUCT_ComputationHandle *tail;
135 /**************************************************************
136 *** Function Declarations **********
137 **************************************************************/
140 GNUNET_SCALARPRODUCT_cancel (struct GNUNET_SCALARPRODUCT_ComputationHandle * h);
142 /**************************************************************
143 *** Static Function Declarations **********
144 **************************************************************/
148 * Handles the RESULT received in reply of prepare_response from the
151 * @param cls Handle to the Master Context
152 * @param msg Pointer to the response received
155 process_status_message (void *cls,
156 const struct GNUNET_MessageHeader *msg,
157 enum GNUNET_SCALARPRODUCT_ResponseStatus status)
159 struct GNUNET_SCALARPRODUCT_ComputationHandle *qe = cls;
161 qe->cont_status (qe->cont_cls, status);
166 * Handles the RESULT received in reply of prepare_response from the
169 * @param cls Handle to the Master Context
170 * @param msg Pointer to the response received
173 process_result_message (void *cls,
174 const struct GNUNET_MessageHeader *msg,
175 enum GNUNET_SCALARPRODUCT_ResponseStatus status)
177 struct GNUNET_SCALARPRODUCT_ComputationHandle *qe = cls;
178 const struct GNUNET_SCALARPRODUCT_client_response *message =
179 (const struct GNUNET_SCALARPRODUCT_client_response *) msg;
180 gcry_mpi_t result = NULL;
183 if (GNUNET_SCALARPRODUCT_Status_Success == status)
185 size_t product_len = ntohl (message->product_length);
186 result = gcry_mpi_new (0);
193 if (0 != (rc = gcry_mpi_scan (&num, GCRYMPI_FMT_STD, &msg[1], product_len, &read)))
195 LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
196 gcry_mpi_release (result);
198 status = GNUNET_SCALARPRODUCT_Status_InvalidResponse;
202 if (message->range > 0)
203 gcry_mpi_add (result, result, num);
205 gcry_mpi_sub (result, result, num);
206 gcry_mpi_release (num);
210 qe->cont_datum (qe->cont_cls, status, result);
215 * Called when a response is received from the service. After basic check
216 * handler in qe->response_proc is called. This functions handles the response
217 * to the client which used the API.
219 * @param cls Pointer to the Master Context
220 * @param msg Pointer to the data received in response
223 receive_cb (void *cls, const struct GNUNET_MessageHeader *msg)
225 struct GNUNET_SCALARPRODUCT_ComputationHandle *qe = cls;
226 const struct GNUNET_SCALARPRODUCT_client_response *message =
227 (const struct GNUNET_SCALARPRODUCT_client_response *) msg;
228 enum GNUNET_SCALARPRODUCT_ResponseStatus status = GNUNET_SCALARPRODUCT_Status_InvalidResponse;
232 LOG (GNUNET_ERROR_TYPE_WARNING, "Disconnected by Service.\n");
233 status = GNUNET_SCALARPRODUCT_Status_ServiceDisconnected;
235 else if (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SERVICE_TO_CLIENT != ntohs (msg->type))
237 LOG (GNUNET_ERROR_TYPE_WARNING, "Invalid message type received\n");
239 else if (0 < ntohl (message->product_length) || (0 == message->range))
241 // response for the responder client, successful
242 GNUNET_STATISTICS_update (qe->stats,
243 gettext_noop ("# SUC responder result messages received"), 1,
246 status = GNUNET_SCALARPRODUCT_Status_Success;
249 if (qe->cont_datum != NULL)
250 qe->response_proc (qe, msg, status);
257 * Transmits the request to the VectorProduct Sevice
260 * @param size Size of the buffer
261 * @param buf Pointer to the buffer
263 * @return Size of the message sent
266 transmit_request (void *cls, size_t size,
269 struct GNUNET_SCALARPRODUCT_ComputationHandle *qe = cls;
273 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to transmit request to SCALARPRODUCT.\n");
274 GNUNET_STATISTICS_update (qe->stats,
275 gettext_noop ("# transmission request failures"),
278 // notify caller about the error, done here.
279 if (qe->cont_datum != NULL)
280 qe->response_proc (qe, NULL, GNUNET_SCALARPRODUCT_Status_Failure);
282 GNUNET_SCALARPRODUCT_cancel (cls);
285 memcpy (buf, qe->msg, size);
287 GNUNET_free (qe->msg);
291 GNUNET_CLIENT_receive (qe->client, &receive_cb, qe,
292 GNUNET_TIME_UNIT_FOREVER_REL);
294 #if INSANE_STATISTICS
295 GNUNET_STATISTICS_update (qe->stats,
296 gettext_noop ("# bytes sent to scalarproduct"), 1,
303 /**************************************************************
305 **************************************************************/
309 * Used by Bob's client to cooperate with Alice,
311 * @param h handle to the master context
312 * @param key Session key - unique to the requesting client
313 * @param elements Array of elements of the vector
314 * @param element_count Number of elements in the vector
315 * @param cont Callback function
316 * @param cont_cls Closure for the callback function
318 struct GNUNET_SCALARPRODUCT_ComputationHandle *
319 GNUNET_SCALARPRODUCT_response (const struct GNUNET_CONFIGURATION_Handle *cfg,
320 const struct GNUNET_HashCode * key,
321 const int32_t * elements,
322 uint32_t element_count,
323 GNUNET_SCALARPRODUCT_ContinuationWithStatus cont,
326 struct GNUNET_SCALARPRODUCT_ComputationHandle *h;
327 struct GNUNET_SCALARPRODUCT_client_request *msg;
332 GNUNET_assert (GNUNET_SERVER_MAX_MESSAGE_SIZE >= sizeof (struct GNUNET_SCALARPRODUCT_client_request)
333 +element_count * sizeof (int32_t));
334 h = GNUNET_new (struct GNUNET_SCALARPRODUCT_ComputationHandle);
335 h->client = GNUNET_CLIENT_connect ("scalarproduct", cfg);
338 LOG (GNUNET_ERROR_TYPE_ERROR,
339 _ ("Failed to connect to the scalarproduct service\n"));
343 h->stats = GNUNET_STATISTICS_create ("scalarproduct-api", cfg);
346 LOG (GNUNET_ERROR_TYPE_ERROR,
347 _ ("Failed to send a message to the statistics service\n"));
348 GNUNET_CLIENT_disconnect (h->client);
353 size = sizeof (struct GNUNET_SCALARPRODUCT_client_request) +element_count * sizeof (int32_t);
355 h->cont_status = cont;
356 h->cont_cls = cont_cls;
357 h->response_proc = &process_status_message;
359 memcpy (&h->key, key, sizeof (struct GNUNET_HashCode));
361 msg = (struct GNUNET_SCALARPRODUCT_client_request*) GNUNET_malloc (size);
363 msg->header.size = htons (size);
364 msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_BOB);
365 msg->element_count = htonl (element_count);
367 vector = (int32_t*) & msg[1];
368 // copy each element over to the message
369 for (i = 0; i < element_count; i++)
370 vector[i] = htonl (elements[i]);
372 memcpy (&msg->key, key, sizeof (struct GNUNET_HashCode));
374 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, size,
375 GNUNET_TIME_UNIT_FOREVER_REL,
376 GNUNET_YES, // retry is OK in the initial stage
377 &transmit_request, h);
380 LOG (GNUNET_ERROR_TYPE_ERROR,
381 _ ("Failed to send a message to the scalarproduct service\n"));
382 GNUNET_STATISTICS_destroy (h->stats, GNUNET_YES);
383 GNUNET_CLIENT_disconnect (h->client);
384 GNUNET_free (h->msg);
388 GNUNET_CONTAINER_DLL_insert (head, tail, h);
394 * Request by Alice's client for computing a scalar product
396 * @param h handle to the master context
397 * @param key Session key - unique to the requesting client
398 * @param peer PeerID of the other peer
399 * @param elements Array of elements of the vector
400 * @param element_count Number of elements in the vector
401 * @param mask Array of the mask
402 * @param mask_bytes number of bytes in the mask
403 * @param cont Callback function
404 * @param cont_cls Closure for the callback function
406 struct GNUNET_SCALARPRODUCT_ComputationHandle *
407 GNUNET_SCALARPRODUCT_request (const struct GNUNET_CONFIGURATION_Handle *cfg,
408 const struct GNUNET_HashCode * key,
409 const struct GNUNET_PeerIdentity *peer,
410 const int32_t * elements,
411 uint32_t element_count,
412 const unsigned char * mask,
414 GNUNET_SCALARPRODUCT_DatumProcessor cont,
417 struct GNUNET_SCALARPRODUCT_ComputationHandle *h;
418 struct GNUNET_SCALARPRODUCT_client_request *msg;
423 GNUNET_assert (GNUNET_SERVER_MAX_MESSAGE_SIZE >= sizeof (struct GNUNET_SCALARPRODUCT_client_request)
424 +element_count * sizeof (int32_t)
427 h = GNUNET_new (struct GNUNET_SCALARPRODUCT_ComputationHandle);
428 h->client = GNUNET_CLIENT_connect ("scalarproduct", cfg);
431 LOG (GNUNET_ERROR_TYPE_ERROR,
432 _ ("Failed to connect to the scalarproduct service\n"));
436 h->stats = GNUNET_STATISTICS_create ("scalarproduct-api", cfg);
439 LOG (GNUNET_ERROR_TYPE_ERROR,
440 _ ("Failed to send a message to the statistics service\n"));
441 GNUNET_CLIENT_disconnect (h->client);
446 size = sizeof (struct GNUNET_SCALARPRODUCT_client_request) + element_count * sizeof (int32_t) + mask_bytes;
448 h->cont_datum = cont;
449 h->cont_cls = cont_cls;
450 h->response_proc = &process_result_message;
452 memcpy (&h->key, key, sizeof (struct GNUNET_HashCode));
454 msg = (struct GNUNET_SCALARPRODUCT_client_request*) GNUNET_malloc (size);
456 msg->header.size = htons (size);
457 msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE);
458 msg->element_count = htonl (element_count);
459 msg->mask_length = htonl (mask_bytes);
461 vector = (int32_t*) & msg[1];
462 // copy each element over to the message
463 for (i = 0; i < element_count; i++)
464 vector[i] = htonl (elements[i]);
466 memcpy (&msg->peer, peer, sizeof (struct GNUNET_PeerIdentity));
467 memcpy (&msg->key, key, sizeof (struct GNUNET_HashCode));
468 memcpy (&vector[element_count], mask, mask_bytes);
470 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, size,
471 GNUNET_TIME_UNIT_FOREVER_REL,
472 GNUNET_YES, // retry is OK in the initial stage
473 &transmit_request, h);
476 LOG (GNUNET_ERROR_TYPE_ERROR,
477 _ ("Failed to send a message to the scalarproduct service\n"));
478 GNUNET_STATISTICS_destroy (h->stats, GNUNET_YES);
479 GNUNET_CLIENT_disconnect (h->client);
480 GNUNET_free (h->msg);
484 GNUNET_CONTAINER_DLL_insert (head, tail, h);
490 * Disconnect from the scalarproduct service.
492 * @param h a computation handle to cancel
495 GNUNET_SCALARPRODUCT_cancel (struct GNUNET_SCALARPRODUCT_ComputationHandle * h)
497 struct GNUNET_SCALARPRODUCT_ComputationHandle * qe;
499 for (qe = head; head != NULL; qe = head)
503 GNUNET_CONTAINER_DLL_remove (head, tail, qe);
505 GNUNET_CLIENT_notify_transmit_ready_cancel (qe->th);
506 GNUNET_CLIENT_disconnect (qe->client);
507 GNUNET_STATISTICS_destroy (qe->stats, GNUNET_YES);
508 GNUNET_free (qe->msg);
515 * Cancel ALL our ongoing scalar product computations and collaboration offers.
516 * Closes ALL connections to the service
519 GNUNET_SCALARPRODUCT_disconnect ()
521 struct GNUNET_SCALARPRODUCT_ComputationHandle * qe;
523 LOG (GNUNET_ERROR_TYPE_INFO, "Disconnecting from VectorProduct\n");
524 for (qe = head; head != NULL; qe = head)
526 GNUNET_CONTAINER_DLL_remove (head, tail, qe);
528 GNUNET_CLIENT_notify_transmit_ready_cancel (qe->th);
529 GNUNET_CLIENT_disconnect (qe->client);
530 GNUNET_STATISTICS_destroy (qe->stats, GNUNET_YES);
531 GNUNET_free (qe->msg);
536 /* end of ext_api.c */