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 * Entry in the request queue per client
44 struct GNUNET_SCALARPRODUCT_ComputationHandle
47 * This is a linked list.
49 struct GNUNET_SCALARPRODUCT_ComputationHandle *next;
52 * This is a linked list.
54 struct GNUNET_SCALARPRODUCT_ComputationHandle *prev;
59 const struct GNUNET_CONFIGURATION_Handle *cfg;
62 * Current connection to the scalarproduct service.
64 struct GNUNET_CLIENT_Connection *client;
67 * Handle for statistics.
69 struct GNUNET_STATISTICS_Handle *stats;
72 * The shared session key identifying this computation
74 struct GNUNET_HashCode * key;
77 * Current transmit handle.
79 struct GNUNET_CLIENT_TransmitHandle *th;
84 uint16_t message_size;
87 * Message to be sent to the scalarproduct service
89 struct GNUNET_SCALARPRODUCT_client_request * msg;
94 * Function to call after transmission of the request.
96 GNUNET_SCALARPRODUCT_ContinuationWithStatus cont_status;
99 * Function to call after transmission of the request.
101 GNUNET_SCALARPRODUCT_DatumProcessor cont_datum;
105 * Closure for 'cont'.
110 * Response Processor for response from the service. This function calls the
111 * continuation function provided by the client.
113 GNUNET_SCALARPRODUCT_ResponseMessageHandler response_proc;
116 /**************************************************************
117 *** Global Variables **********
118 **************************************************************/
120 * Head of the active sessions queue
122 static struct GNUNET_SCALARPRODUCT_ComputationHandle *head;
124 * Tail of the active sessions queue
126 static struct GNUNET_SCALARPRODUCT_ComputationHandle *tail;
128 /**************************************************************
129 *** Function Declarations **********
130 **************************************************************/
133 * Called when a response is received from the service. After basic check
134 * handler in qe->response_proc is called. This functions handles the response
135 * 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 receive_cb (void *cls, const struct GNUNET_MessageHeader *msg);
144 * Transmits the request to the VectorProduct Sevice
147 * @param size Size of the buffer
148 * @param buf Pointer to the buffer
150 * @return Size of the message sent
152 static size_t transmit_request (void *cls, size_t size,
155 /**************************************************************
156 *** Static Function Declarations **********
157 **************************************************************/
160 * Handles the RESULT received in reply of prepare_response from the
163 * @param cls Handle to the Master Context
164 * @param msg Pointer to the response received
167 process_status_message (void *cls,
168 const struct GNUNET_MessageHeader *msg,
169 enum GNUNET_SCALARPRODUCT_ResponseStatus status)
171 struct GNUNET_SCALARPRODUCT_ComputationHandle *qe = cls;
173 qe->cont_status (qe->cont_cls, status);
178 * Handles the RESULT received in reply of prepare_response from the
181 * @param cls Handle to the Master Context
182 * @param msg Pointer to the response received
185 process_result_message (void *cls,
186 const struct GNUNET_MessageHeader *msg,
187 enum GNUNET_SCALARPRODUCT_ResponseStatus status)
189 struct GNUNET_SCALARPRODUCT_ComputationHandle *qe = cls;
190 const struct GNUNET_SCALARPRODUCT_client_response *message =
191 (const struct GNUNET_SCALARPRODUCT_client_response *) msg;
192 gcry_mpi_t result = NULL;
194 if (GNUNET_SCALARPRODUCT_Status_Success == status
195 && qe->cont_datum != NULL)
197 size_t product_len = ntohl(message->product_length);
198 result = gcry_mpi_new(0);
205 if (0 != gcry_mpi_scan (&num, GCRYMPI_FMT_USG, &msg[1], product_len, &read)){
206 LOG (GNUNET_ERROR_TYPE_ERROR, "Could not convert to mpi to value!\n");
207 gcry_mpi_release(result);
209 status = GNUNET_SCALARPRODUCT_Status_InvalidResponse;
213 if (message->range > 0)
214 gcry_mpi_add(result, result, num);
216 gcry_mpi_sub(result, result, num);
217 gcry_mpi_release(num);
221 qe->cont_datum (qe->cont_cls, status, result);
226 * Called when a response is received from the service. After basic check
227 * handler in qe->response_proc is called. This functions handles the response
228 * to the client which used the API.
230 * @param cls Pointer to the Master Context
231 * @param msg Pointer to the data received in response
234 receive_cb (void *cls, const struct GNUNET_MessageHeader *msg)
236 struct GNUNET_SCALARPRODUCT_ComputationHandle *qe = cls;
237 const struct GNUNET_SCALARPRODUCT_client_response *message =
238 (const struct GNUNET_SCALARPRODUCT_client_response *) msg;
239 enum GNUNET_SCALARPRODUCT_ResponseStatus status = GNUNET_SCALARPRODUCT_Status_InvalidResponse;
243 LOG (GNUNET_ERROR_TYPE_WARNING, "Disconnected by Service.\n");
244 status = GNUNET_SCALARPRODUCT_Status_ServiceDisconnected;
246 else if ( GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SERVICE_TO_CLIENT != ntohs (msg->type))
248 LOG (GNUNET_ERROR_TYPE_WARNING, "Invalid message type received\n");
250 else if (0 < ntohl (message->product_length) || (0 == message->range))
252 // response for the responder client, successful
253 GNUNET_STATISTICS_update (qe->stats,
254 gettext_noop ("# SUC responder result messages received"), 1,
257 status = GNUNET_SCALARPRODUCT_Status_Success;
260 if (qe->cont_datum != NULL)
261 qe->response_proc (qe, msg, status);
268 * Transmits the request to the VectorProduct Sevice
271 * @param size Size of the buffer
272 * @param buf Pointer to the buffer
274 * @return Size of the message sent
277 transmit_request (void *cls, size_t size,
280 struct GNUNET_SCALARPRODUCT_ComputationHandle *qe = cls;
284 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to transmit request to SCALARPRODUCT.\n");
285 GNUNET_STATISTICS_update (qe->stats,
286 gettext_noop ("# transmission request failures"),
289 // notify caller about the error, done here.
290 if (qe->cont_datum != NULL)
291 qe->response_proc (qe, NULL, GNUNET_SCALARPRODUCT_Status_Failure);
292 GNUNET_SCALARPRODUCT_cancel(cls);
295 memcpy (buf, qe->msg, size);
297 GNUNET_free (qe->msg);
301 GNUNET_CLIENT_receive (qe->client, &receive_cb, qe,
302 GNUNET_TIME_UNIT_FOREVER_REL);
304 #if INSANE_STATISTICS
305 GNUNET_STATISTICS_update (qe->stats,
306 gettext_noop ("# bytes sent to scalarproduct"), 1,
313 /**************************************************************
315 **************************************************************/
319 * Used by Bob's client to cooperate with Alice,
321 * @param h handle to the master context
322 * @param key Session key - unique to the requesting client
323 * @param elements Array of elements of the vector
324 * @param element_count Number of elements in the vector
325 * @param cont Callback function
326 * @param cont_cls Closure for the callback function
328 struct GNUNET_SCALARPRODUCT_ComputationHandle *
329 GNUNET_SCALARPRODUCT_response (const struct GNUNET_CONFIGURATION_Handle *cfg,
330 const struct GNUNET_HashCode * key,
331 const int32_t * elements,
332 uint32_t element_count,
333 GNUNET_SCALARPRODUCT_ContinuationWithStatus cont,
336 struct GNUNET_SCALARPRODUCT_ComputationHandle *h;
337 struct GNUNET_SCALARPRODUCT_client_request *msg;
343 GNUNET_assert(elements);
345 GNUNET_assert(element_count > 1);
346 GNUNET_assert (GNUNET_SERVER_MAX_MESSAGE_SIZE >= sizeof (struct GNUNET_SCALARPRODUCT_client_request)
347 + element_count * sizeof (int32_t));
348 h = GNUNET_new (struct GNUNET_SCALARPRODUCT_ComputationHandle);
349 h->client = GNUNET_CLIENT_connect ("scalarproduct", cfg);
352 LOG (GNUNET_ERROR_TYPE_ERROR,
353 _ ("Failed to connect to the scalarproduct service\n"));
357 h->stats = GNUNET_STATISTICS_create ("scalarproduct-api", cfg);
359 LOG (GNUNET_ERROR_TYPE_ERROR,
360 _("Failed to send a message to the statistics service\n"));
361 GNUNET_CLIENT_disconnect(h->client);
366 size = sizeof (struct GNUNET_SCALARPRODUCT_client_request) + element_count * sizeof (int32_t);
368 h->cont_status = cont;
369 h->cont_cls = cont_cls;
370 h->response_proc = &process_result_message;
372 h->msg = GNUNET_malloc (size);
373 memcpy (&h->key, key, sizeof (struct GNUNET_HashCode));
375 msg = (struct GNUNET_SCALARPRODUCT_client_request*) h->msg;
376 msg->header.size = htons (size);
377 msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE);
378 msg->element_count = htonl (element_count);
380 vector = (int32_t*) &msg[1];
381 // copy each element over to the message
382 for (i = 0; i < element_count; i++)
383 vector[i] = htonl(elements[i]);
385 memcpy (&msg->key, key, sizeof (struct GNUNET_HashCode));
387 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, size,
388 GNUNET_TIME_UNIT_FOREVER_REL,
389 GNUNET_YES, // retry is OK in the initial stage
390 &transmit_request, h);
393 LOG (GNUNET_ERROR_TYPE_ERROR,
394 _ ("Failed to send a message to the scalarproduct service\n"));
395 GNUNET_STATISTICS_destroy(h->stats, GNUNET_YES);
396 GNUNET_CLIENT_disconnect(h->client);
401 GNUNET_CONTAINER_DLL_insert (head, tail, h);
407 * Request by Alice's client for computing a scalar product
409 * @param h handle to the master context
410 * @param key Session key - unique to the requesting client
411 * @param peer PeerID of the other peer
412 * @param elements Array of elements of the vector
413 * @param element_count Number of elements in the vector
414 * @param mask Array of the mask
415 * @param mask_bytes number of bytes in the mask
416 * @param cont Callback function
417 * @param cont_cls Closure for the callback function
419 struct GNUNET_SCALARPRODUCT_ComputationHandle *
420 GNUNET_SCALARPRODUCT_request (const struct GNUNET_CONFIGURATION_Handle *cfg,
421 const struct GNUNET_HashCode * key,
422 const struct GNUNET_PeerIdentity *peer,
423 const int32_t * elements,
424 uint32_t element_count,
425 const unsigned char * mask,
427 GNUNET_SCALARPRODUCT_DatumProcessor cont,
430 struct GNUNET_SCALARPRODUCT_ComputationHandle *h;
431 struct GNUNET_SCALARPRODUCT_client_request *msg;
436 GNUNET_assert (GNUNET_SERVER_MAX_MESSAGE_SIZE >= sizeof (struct GNUNET_SCALARPRODUCT_client_request)
437 + element_count * sizeof (int32_t)
440 h = GNUNET_new (struct GNUNET_SCALARPRODUCT_ComputationHandle);
441 h->client = GNUNET_CLIENT_connect ("scalarproduct", cfg);
444 LOG (GNUNET_ERROR_TYPE_ERROR,
445 _ ("Failed to connect to the scalarproduct service\n"));
449 h->stats = GNUNET_STATISTICS_create ("scalarproduct-api", cfg);
451 LOG (GNUNET_ERROR_TYPE_ERROR,
452 _("Failed to send a message to the statistics service\n"));
453 GNUNET_CLIENT_disconnect(h->client);
458 size = sizeof (struct GNUNET_SCALARPRODUCT_client_request) + element_count * sizeof (int32_t) + mask_bytes;
460 h->cont_datum = cont;
461 h->cont_cls = cont_cls;
462 h->response_proc = &process_status_message;
464 h->msg = GNUNET_malloc (size);
465 memcpy (&h->key, key, sizeof (struct GNUNET_HashCode));
467 msg = (struct GNUNET_SCALARPRODUCT_client_request*) h->msg;
468 msg->header.size = htons (size);
469 msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE);
470 msg->element_count = htons (element_count);
471 msg->mask_length = htons (mask_bytes);
473 vector = (int32_t*) &msg[1];
474 // copy each element over to the message
475 for (i = 0; i < element_count; i++)
476 vector[i] = htonl(elements[i]);
478 memcpy (&msg->peer, peer, sizeof (struct GNUNET_PeerIdentity));
479 memcpy (&msg->key, key, sizeof (struct GNUNET_HashCode));
480 memcpy (&vector[element_count], mask, mask_bytes);
482 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, size,
483 GNUNET_TIME_UNIT_FOREVER_REL,
484 GNUNET_YES, // retry is OK in the initial stage
485 &transmit_request, h);
488 LOG (GNUNET_ERROR_TYPE_ERROR,
489 _ ("Failed to send a message to the scalarproduct service\n"));
490 GNUNET_STATISTICS_destroy(h->stats, GNUNET_YES);
491 GNUNET_CLIENT_disconnect(h->client);
496 GNUNET_CONTAINER_DLL_insert (head, tail, h);
501 * Disconnect from the scalarproduct service.
503 * @param h a computation handle to cancel
506 GNUNET_SCALARPRODUCT_cancel (struct GNUNET_SCALARPRODUCT_ComputationHandle * h)
508 struct GNUNET_SCALARPRODUCT_ComputationHandle * qe;
510 for (qe = head; head != NULL; qe = head)
514 GNUNET_CONTAINER_DLL_remove (head, tail, qe);
515 LOG (GNUNET_ERROR_TYPE_INFO,
516 "Disconnecting from VectorProduct\n");
518 GNUNET_CLIENT_notify_transmit_ready_cancel (qe->th);
519 GNUNET_CLIENT_disconnect (h->client);
520 GNUNET_STATISTICS_destroy (h->stats, GNUNET_YES);
521 GNUNET_free (qe->msg);
528 /* end of ext_api.c */