/*
This file is part of GNUnet.
- (C) 2013 Christian Grothoff (and other contributing authors)
+ Copyright (C) 2013, 2014, 2016 GNUnet e.V.
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
You should have received a copy of the GNU General Public License
along with GNUnet; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA.
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
*/
-
/**
* @file scalarproduct/scalarproduct_api.c
* @brief API for the scalarproduct
* @author Christian Fuchs
* @author Gaurav Kukreja
- *
+ * @author Christian Grothoff
*/
#include "platform.h"
#include "gnunet_util_lib.h"
#define LOG(kind,...) GNUNET_log_from (kind, "scalarproduct-api",__VA_ARGS__)
-/**************************************************************
- *** Datatype Declarations **********
- **************************************************************/
/**
- * Entry in the request queue per client
+ * The abstraction function for our internal callback
+ *
+ * @param h computation handle
+ * @param msg response we got, NULL on errors
+ * @param status processing status code
*/
-struct GNUNET_SCALARPRODUCT_QueueEntry
-{
- /**
- * This is a linked list.
- */
- struct GNUNET_SCALARPRODUCT_QueueEntry *next;
+typedef void
+(*GNUNET_SCALARPRODUCT_ResponseMessageHandler) (struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
+ const struct ClientResponseMessage *msg,
+ enum GNUNET_SCALARPRODUCT_ResponseStatus status);
+
+/**
+ * A handle returned for each computation
+ */
+struct GNUNET_SCALARPRODUCT_ComputationHandle
+{
/**
- * This is a linked list.
+ * Our configuration.
*/
- struct GNUNET_SCALARPRODUCT_QueueEntry *prev;
+ const struct GNUNET_CONFIGURATION_Handle *cfg;
/**
- * Handle to the master context.
+ * Current connection to the scalarproduct service.
*/
- struct GNUNET_SCALARPRODUCT_Handle *h;
+ struct GNUNET_MQ_Handle *mq;
/**
- * Size of the message
+ * Function to call after transmission of the request (Bob).
*/
- uint16_t message_size;
+ GNUNET_SCALARPRODUCT_ContinuationWithStatus cont_status;
/**
- * Message to be sent to the scalarproduct service
+ * Function to call after transmission of the request (Alice).
*/
- struct GNUNET_SCALARPRODUCT_client_request* msg;
-
- union
- {
- /**
- * Function to call after transmission of the request.
- */
- GNUNET_SCALARPRODUCT_ContinuationWithStatus cont_status;
-
- /**
- * Function to call after transmission of the request.
- */
- GNUNET_SCALARPRODUCT_DatumProcessor cont_datum;
- };
+ GNUNET_SCALARPRODUCT_DatumProcessor cont_datum;
/**
- * Closure for 'cont'.
+ * Closure for @e cont_status or @e cont_datum.
*/
void *cont_cls;
/**
- * Has this message been transmitted to the service?
- * Only ever GNUNET_YES for the head of the queue.
- * Note that the overall struct should end at a
- * multiple of 64 bits.
+ * API internal callback for results and failures to be forwarded to
+ * the client.
*/
- int16_t was_transmitted;
+ GNUNET_SCALARPRODUCT_ResponseMessageHandler response_proc;
/**
- * Response Processor for response from the service. This function calls the
- * continuation function provided by the client.
+ * The shared session key identifying this computation
*/
- GNUNET_SCALARPRODUCT_ResponseMessageHandler response_proc;
-};
-
-/**************************************************************
- *** Function Declarations **********
- **************************************************************/
-
-/**
- * Creates a new entry at the tail of the DLL
- *
- * @param h handle to the master context
- *
- * @return pointer to the entry
- */
-static struct GNUNET_SCALARPRODUCT_QueueEntry *
-make_queue_entry (struct GNUNET_SCALARPRODUCT_Handle *h);
+ struct GNUNET_HashCode key;
-/**
- * Removes the head entry from the queue
- *
- * @param h Handle to the master context
- */
-static struct GNUNET_SCALARPRODUCT_QueueEntry *
-free_queue_head_entry (struct GNUNET_SCALARPRODUCT_Handle * h);
+};
-/**
- * Triggered when timeout occurs for a request in queue
- *
- * @param cls The pointer to the QueueEntry
- * @param tc Task Context
- */
-static void
-timeout_queue_entry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
/**
- * Called when a response is received from the service. After basic check
- * handler in qe->response_proc is called. This functions handles the response
- * to the client which used the API.
- *
+ * Called when a response is received from the service. Perform basic
+ * check that the message is well-formed.
+ *
* @param cls Pointer to the Master Context
- * @param msg Pointer to the data received in response
- */
-static void
-receive_cb (void *cls, const struct GNUNET_MessageHeader *msg);
-
-/**
- * Transmits the request to the VectorProduct Sevice
- *
- * @param cls Closure
- * @param size Size of the buffer
- * @param buf Pointer to the buffer
- *
- * @return Size of the message sent
+ * @param message Pointer to the data received in response
+ * @return #GNUNET_OK if @a message is well-formed
*/
-static size_t transmit_request (void *cls, size_t size,
- void *buf);
-
-/**
- * Issues transmit request for the new entries in the queue
- *
- * @param h handle to the master context
- */
-static void
-process_queue (struct GNUNET_SCALARPRODUCT_Handle *h);
-
-/**************************************************************
- *** Static Function Declarations **********
- **************************************************************/
-
-
-/**
- * Creates a new entry at the tail of the DLL
- *
- * @param h handle to the master context
- *
- * @return pointer to the entry
- */
-static struct GNUNET_SCALARPRODUCT_QueueEntry *
-make_queue_entry (struct GNUNET_SCALARPRODUCT_Handle *h)
+static int
+check_response (void *cls,
+ const struct ClientResponseMessage *message)
{
- struct GNUNET_SCALARPRODUCT_QueueEntry *qe;
-
- qe = GNUNET_new (struct GNUNET_SCALARPRODUCT_QueueEntry);
-
- // if queue empty
- if (NULL == h->queue_head && NULL == h->queue_tail)
- {
- qe->next = NULL;
- qe->prev = NULL;
- h->queue_head = qe;
- h->queue_tail = qe;
- }
- else
- {
- qe->prev = h->queue_tail;
- h->queue_tail->next = qe;
- h->queue_tail = qe;
- }
-
- return qe;
-}
-
-
-/**
- * Removes the head entry from the queue
- *
- * @param h Handle to the master context
- */
-static struct GNUNET_SCALARPRODUCT_QueueEntry *
-free_queue_head_entry (struct GNUNET_SCALARPRODUCT_Handle * h)
-{
- struct GNUNET_SCALARPRODUCT_QueueEntry * qe = NULL;
-
- GNUNET_assert (NULL != h);
- if (NULL == h->queue_head && NULL == h->queue_tail)
- {
- // The queue is empty. Just return.
- qe = NULL;
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Queue was empty when free_queue_head_entry was called.\n");
- }
- else if (h->queue_head == h->queue_tail) //only one entry
- {
- qe = h->queue_head;
- qe->next = NULL;
- qe->prev = NULL;
- h->queue_head = NULL;
- h->queue_tail = NULL;
- }
- else
- {
- qe = h->queue_head;
- h->queue_head = h->queue_head->next;
- h->queue_head->prev = NULL;
- qe->next = NULL;
- qe->prev = NULL;
- }
- return qe;
+ if (ntohs (message->header.size) !=
+ ntohl (message->product_length) + sizeof (struct ClientResponseMessage))
+ {
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
+ }
+ return GNUNET_OK;
}
/**
- * Triggered when timeout occurs for a request in queue
- *
- * @param cls The pointer to the QueueEntry
- * @param tc Task Context
+ * Handles the STATUS received from the service for a response, does
+ * not contain a payload. Called when we participate as "Bob" via
+ * #GNUNET_SCALARPRODUCT_accept_computation().
+ *
+ * @param h our Handle
+ * @param msg the response received
+ * @param status the condition the request was terminated with (eg: disconnect)
*/
static void
-timeout_queue_entry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+process_status_message (struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
+ const struct ClientResponseMessage *msg,
+ enum GNUNET_SCALARPRODUCT_ResponseStatus status)
{
- struct GNUNET_SCALARPRODUCT_QueueEntry * qe = cls;
-
- // Update Statistics
- GNUNET_STATISTICS_update (qe->h->stats,
- gettext_noop ("# queue entry timeouts"), 1,
- GNUNET_NO);
-
- // Clear the timeout_task
- qe->timeout_task = GNUNET_SCHEDULER_NO_TASK;
-
- // transmit_request is supposed to cancel timeout task.
- // If message was not transmitted, there is definitely an error.
- GNUNET_assert (GNUNET_NO == qe->was_transmitted);
-
- LOG (GNUNET_ERROR_TYPE_INFO, "Timeout of request in datastore queue\n");
-
- // remove the queue_entry for the queue
- GNUNET_CONTAINER_DLL_remove (qe->h->queue_head, qe->h->queue_tail, qe);
- qe->response_proc (qe, NULL, GNUNET_SCALARPRODUCT_Status_Timeout);
+ if (NULL != h->cont_status)
+ h->cont_status (h->cont_cls,
+ status);
+ GNUNET_SCALARPRODUCT_cancel (h);
}
/**
- * Handles the RESULT received in reply of prepare_response from the
- * service
- *
- * @param cls Handle to the Master Context
- * @param msg Pointer to the response received
+ * Called when a response is received from the service. After basic
+ * check, the handler in `h->response_proc` is called. This functions
+ * handles the response to the client which used the API.
+ *
+ * @param cls Pointer to the Master Context
+ * @param msg Pointer to the data received in response
*/
static void
-process_status_message (void *cls,
- const struct GNUNET_MessageHeader *msg,
- enum GNUNET_SCALARPRODUCT_ResponseStatus status)
+handle_response (void *cls,
+ const struct ClientResponseMessage *message)
{
- struct GNUNET_SCALARPRODUCT_QueueEntry *qe = cls;
-
- GNUNET_assert (qe != NULL);
+ struct GNUNET_SCALARPRODUCT_ComputationHandle *h = cls;
+ enum GNUNET_SCALARPRODUCT_ResponseStatus status;
- if (qe->cont_status != NULL)
- qe->cont_status (qe->cont_cls, &qe->msg->key, status);
+ status = (enum GNUNET_SCALARPRODUCT_ResponseStatus) ntohl (message->status);
+ h->response_proc (h,
+ message,
+ status);
}
/**
- * Handles the RESULT received in reply of prepare_response from the
- * service
- *
- * @param cls Handle to the Master Context
- * @param msg Pointer to the response received
+ * Check if the keys for all given elements are unique.
+ *
+ * @param elements elements to check
+ * @param element_count size of the @a elements array
+ * @return #GNUNET_OK if all keys are unique
*/
-static void
-process_result_message (void *cls,
- const struct GNUNET_MessageHeader *msg,
- enum GNUNET_SCALARPRODUCT_ResponseStatus status)
+static int
+check_unique (const struct GNUNET_SCALARPRODUCT_Element *elements,
+ uint32_t element_count)
{
- struct GNUNET_SCALARPRODUCT_QueueEntry *qe = cls;
-
- GNUNET_assert (qe != NULL);
-
- if (msg == NULL && qe->cont_datum != NULL)
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Timeout reached or session terminated.\n");
- }
- if (qe->cont_datum != NULL)
+ struct GNUNET_CONTAINER_MultiHashMap *map;
+ uint32_t i;
+ int ok;
+
+ ok = GNUNET_OK;
+ map = GNUNET_CONTAINER_multihashmap_create (2 * element_count,
+ GNUNET_YES);
+ for (i=0;i<element_count;i++)
+ if (GNUNET_OK !=
+ GNUNET_CONTAINER_multihashmap_put (map,
+ &elements[i].key,
+ map,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
{
- qe->cont_datum (qe->cont_cls, &qe->msg->key, &qe->msg->peer, status, (struct GNUNET_SCALARPRODUCT_client_response *) msg);
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ _("Keys given to SCALARPRODUCT not unique!\n"));
+ ok = GNUNET_SYSERR;
}
+ GNUNET_CONTAINER_multihashmap_destroy (map);
+ return ok;
}
/**
- * Called when a response is received from the service. After basic check
- * handler in qe->response_proc is called. This functions handles the response
- * to the client which used the API.
- *
- * @param cls Pointer to the Master Context
- * @param msg Pointer to the data received in response
+ * We encountered an error communicating with the set service while
+ * performing a set operation. Report to the application.
+ *
+ * @param cls the `struct GNUNET_SCALARPRODUCT_ComputationHandle`
+ * @param error error code
*/
static void
-receive_cb (void *cls, const struct GNUNET_MessageHeader *msg)
+mq_error_handler (void *cls,
+ enum GNUNET_MQ_Error error)
{
- struct GNUNET_SCALARPRODUCT_Handle *h = cls;
- struct GNUNET_SCALARPRODUCT_QueueEntry *qe;
- int16_t was_transmitted;
- struct GNUNET_SCALARPRODUCT_client_response *message =
- (struct GNUNET_SCALARPRODUCT_client_response *) msg;
+ struct GNUNET_SCALARPRODUCT_ComputationHandle *h = cls;
- h->in_receive = GNUNET_NO;
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Received reply from VectorProduct\n");
-
- if (NULL == (qe = free_queue_head_entry (h)))
- {
- /**
- * The queue head will be NULL if the client disconnected,
- * * In case of Alice, client disconnected after sending request, before receiving response
- * * In case of Bob, client disconnected after preparing response, before getting request from Alice.
- */
- process_queue (h);
- return;
- }
-
- if (h->client == NULL)
- {
- // GKUKREJA : handle this correctly
- /**
- * The queue head will be NULL if the client disconnected,
- * * In case of Alice, client disconnected after sending request, before receiving response
- * * In case of Bob, client disconnected after preparing response, before getting request from Alice.
- */
- process_queue (h);
- return;
- }
-
- was_transmitted = qe->was_transmitted;
- // Control will only come here, when the request was transmitted to service,
- // and service responded.
- GNUNET_assert (was_transmitted == GNUNET_YES);
-
- if (msg == NULL)
- {
- LOG (GNUNET_ERROR_TYPE_WARNING, "Service responded with NULL!\n");
- qe->response_proc (qe, NULL, GNUNET_SCALARPRODUCT_Status_Failure);
- }
- else if ((ntohs (msg->type) != GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SERVICE_TO_CLIENT))
- {
- LOG (GNUNET_ERROR_TYPE_WARNING, "Invalid Message Received\n");
- qe->response_proc (qe, msg, GNUNET_SCALARPRODUCT_Status_InvalidResponse);
- }
- else if (ntohl (message->product_length) == 0)
- {
- // response for the responder client, successful
- GNUNET_STATISTICS_update (h->stats,
- gettext_noop ("# SUC responder result messages received"), 1,
- GNUNET_NO);
-
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Received message from service without product attached.\n");
- qe->response_proc (qe, msg, GNUNET_SCALARPRODUCT_Status_Success);
- }
- else if (ntohl (message->product_length) > 0)
- {
- // response for the requester client, successful
- GNUNET_STATISTICS_update (h->stats,
- gettext_noop ("# SUC requester result messages received"), 1,
- GNUNET_NO);
-
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Received message from requester service for requester client.\n");
- qe->response_proc (qe, msg, GNUNET_SCALARPRODUCT_Status_Success);
- }
-
- GNUNET_free (qe);
- process_queue (h);
+ LOG (GNUNET_ERROR_TYPE_INFO,
+ "Disconnected from SCALARPRODUCT service.\n");
+ h->response_proc (h,
+ NULL,
+ GNUNET_SCALARPRODUCT_STATUS_DISCONNECTED);
}
/**
- * Transmits the request to the VectorProduct Sevice
- *
- * @param cls Closure
- * @param size Size of the buffer
- * @param buf Pointer to the buffer
- *
- * @return Size of the message sent
+ * Used by Bob's client to cooperate with Alice,
+ *
+ * @param cfg the gnunet configuration handle
+ * @param key Session key unique to the requesting client
+ * @param elements Array of elements of the vector
+ * @param element_count Number of elements in the @a elements vector
+ * @param cont Callback function
+ * @param cont_cls Closure for @a cont
+ * @return a new handle for this computation
*/
-static size_t
-transmit_request (void *cls, size_t size,
- void *buf)
+struct GNUNET_SCALARPRODUCT_ComputationHandle *
+GNUNET_SCALARPRODUCT_accept_computation (const struct GNUNET_CONFIGURATION_Handle *cfg,
+ const struct GNUNET_HashCode *session_key,
+ const struct GNUNET_SCALARPRODUCT_Element *elements,
+ uint32_t element_count,
+ GNUNET_SCALARPRODUCT_ContinuationWithStatus cont,
+ void *cont_cls)
{
- struct GNUNET_SCALARPRODUCT_Handle *h = cls;
- struct GNUNET_SCALARPRODUCT_QueueEntry *qe;
- size_t msize;
-
- if (NULL == (qe = h->queue_head))
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Queue head is NULL!\n\n");
- return 0;
- }
-
- GNUNET_SCHEDULER_cancel (qe->timeout_task);
- qe->timeout_task = GNUNET_SCHEDULER_NO_TASK;
-
- h->th = NULL;
- if (NULL == (qe = h->queue_head))
- return 0; /* no entry in queue */
- if (buf == NULL)
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to transmit request to SCALARPRODUCT.\n");
- GNUNET_STATISTICS_update (h->stats,
- gettext_noop ("# transmission request failures"),
- 1, GNUNET_NO);
- GNUNET_SCALARPRODUCT_disconnect (h);
- return 0;
- }
- if (size < (msize = qe->message_size))
- {
- process_queue (h);
- return 0;
- }
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting %u byte request to SCALARPRODUCT\n",
- msize);
-
- memcpy (buf, qe->msg, size);
- GNUNET_free (qe->msg);
- qe->was_transmitted = GNUNET_YES;
-
- GNUNET_assert (GNUNET_NO == h->in_receive);
- h->in_receive = GNUNET_YES;
-
- GNUNET_CLIENT_receive (h->client, &receive_cb, h,
- GNUNET_TIME_UNIT_FOREVER_REL);
-
-#if INSANE_STATISTICS
- GNUNET_STATISTICS_update (h->stats,
- gettext_noop ("# bytes sent to scalarproduct"), 1,
- GNUNET_NO);
-#endif
- return size;
+ struct GNUNET_SCALARPRODUCT_ComputationHandle *h
+ = GNUNET_new (struct GNUNET_SCALARPRODUCT_ComputationHandle);
+ struct GNUNET_MQ_MessageHandler handlers[] = {
+ GNUNET_MQ_hd_var_size (response,
+ GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT,
+ struct ClientResponseMessage,
+ h),
+ GNUNET_MQ_handler_end ()
+ };
+ struct GNUNET_MQ_Envelope *env;
+ struct BobComputationMessage *msg;
+ struct ComputationBobCryptodataMultipartMessage *mmsg;
+ uint32_t size;
+ uint16_t possible;
+ uint16_t todo;
+ uint32_t element_count_transfered;
+
+
+ if (GNUNET_SYSERR == check_unique (elements,
+ element_count))
+ return NULL;
+ h->cont_status = cont;
+ h->cont_cls = cont_cls;
+ h->response_proc = &process_status_message;
+ h->cfg = cfg;
+ h->key = *session_key;
+ h->mq = GNUNET_CLIENT_connect (cfg,
+ "scalarproduct-bob",
+ handlers,
+ &mq_error_handler,
+ h);
+ if (NULL == h->mq)
+ {
+ /* scalarproduct configuration error */
+ GNUNET_break (0);
+ GNUNET_free (h);
+ return NULL;
+ }
+ possible = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - sizeof (struct BobComputationMessage))
+ / sizeof (struct GNUNET_SCALARPRODUCT_Element);
+ todo = GNUNET_MIN (possible,
+ element_count);
+ size = todo * sizeof (struct GNUNET_SCALARPRODUCT_Element);
+ env = GNUNET_MQ_msg_extra (msg,
+ size,
+ GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_BOB);
+ msg->element_count_total = htonl (element_count);
+ msg->element_count_contained = htonl (todo);
+ msg->session_key = *session_key;
+ GNUNET_memcpy (&msg[1],
+ elements,
+ size);
+ element_count_transfered = todo;
+ GNUNET_MQ_send (h->mq,
+ env);
+ possible = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - sizeof (*mmsg))
+ / sizeof (struct GNUNET_SCALARPRODUCT_Element);
+ while (element_count_transfered < element_count)
+ {
+ todo = GNUNET_MIN (possible,
+ element_count - element_count_transfered);
+ size = todo * sizeof (struct GNUNET_SCALARPRODUCT_Element);
+ env = GNUNET_MQ_msg_extra (mmsg,
+ size,
+ GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_BOB);
+ mmsg->element_count_contained = htonl (todo);
+ GNUNET_memcpy (&mmsg[1],
+ &elements[element_count_transfered],
+ size);
+ element_count_transfered += todo;
+ GNUNET_MQ_send (h->mq,
+ env);
+ }
+ return h;
}
/**
- * Issues transmit request for the new entries in the queue
- *
- * @param h handle to the master context
+ * Handles the RESULT received from the service for a request, should
+ * contain a result MPI value. Called when we participate as "Alice" via
+ * #GNUNET_SCALARPRODUCT_start_computation().
+ *
+ * @param h our Handle
+ * @param msg Pointer to the response received
+ * @param status the condition the request was terminated with (eg: disconnect)
*/
static void
-process_queue (struct GNUNET_SCALARPRODUCT_Handle *h)
+process_result_message (struct GNUNET_SCALARPRODUCT_ComputationHandle *h,
+ const struct ClientResponseMessage *msg,
+ enum GNUNET_SCALARPRODUCT_ResponseStatus status)
{
- struct GNUNET_SCALARPRODUCT_QueueEntry *qe;
-
- if (NULL == (qe = h->queue_head))
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Queue empty\n");
- return; /* no entry in queue */
- }
- if (qe->was_transmitted == GNUNET_YES)
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Head request already transmitted\n");
- return; /* waiting for replies */
- }
- if (h->th != NULL)
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Pending transmission request\n");
- return; /* request pending */
- }
- if (h->client == NULL)
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Not connected\n");
- return; /* waiting for reconnect */
- }
- if (GNUNET_YES == h->in_receive)
- {
- /* wait for response to previous query */
- return;
- }
-
- h->th =
- GNUNET_CLIENT_notify_transmit_ready (h->client, qe->message_size,
- GNUNET_TIME_UNIT_FOREVER_REL,
- GNUNET_YES,
- &transmit_request, h);
-
- if (h->th == NULL)
- {
- LOG (GNUNET_ERROR_TYPE_ERROR,
- _ ("Failed to send a message to the scalarproduct service\n"));
- return;
- }
-
- GNUNET_assert (GNUNET_NO == h->in_receive);
- GNUNET_break (NULL != h->th);
-}
-
-
-
-/**************************************************************
- *** API **********
- **************************************************************/
+ uint32_t product_len;
+ gcry_mpi_t result = NULL;
+ gcry_error_t rc;
+ gcry_mpi_t num;
+ size_t rsize;
+ if (GNUNET_SCALARPRODUCT_STATUS_SUCCESS == status)
+ {
+ result = gcry_mpi_new (0);
-/**
- * Used by Bob's client to cooperate with Alice,
- *
- * @param h handle to the master context
- * @param key Session key - unique to the requesting client
- * @param elements Array of elements of the vector
- * @param element_count Number of elements in the vector
- * @param cont Callback function
- * @param cont_cls Closure for the callback function
- */
-struct GNUNET_SCALARPRODUCT_Handle *
-GNUNET_SCALARPRODUCT_response (const struct GNUNET_CONFIGURATION_Handle *cfg,
- const struct GNUNET_HashCode * key,
- const int32_t * elements,
- uint32_t element_count,
- GNUNET_SCALARPRODUCT_ContinuationWithStatus cont,
- void *cont_cls)
-{
- struct GNUNET_SCALARPRODUCT_Handle *h;
- struct GNUNET_SCALARPRODUCT_client_request *msg;
- int32_t * vector;
- uint16_t size;
- uint64_t i;
-
- GNUNET_assert(key);
- GNUNET_assert(elements);
- GNUNET_assert(cont);
- GNUNET_assert(element_count > 1);
- GNUNET_assert (GNUNET_SERVER_MAX_MESSAGE_SIZE >= sizeof (struct GNUNET_SCALARPRODUCT_client_request)
- + element_count * sizeof (int32_t));
- h = GNUNET_new (struct GNUNET_SCALARPRODUCT_Handle);
- h->client = GNUNET_CLIENT_connect ("scalarproduct", cfg);
- if (!h->client)
+ product_len = ntohl (msg->product_length);
+ if (0 < product_len)
{
- LOG (GNUNET_ERROR_TYPE_ERROR,
- _ ("Failed to connect to the scalarproduct service\n"));
- GNUNET_free(h);
- return NULL;
+ rsize = 0;
+ if (0 != (rc = gcry_mpi_scan (&num, GCRYMPI_FMT_STD,
+ &msg[1],
+ product_len,
+ &rsize)))
+ {
+ LOG_GCRY (GNUNET_ERROR_TYPE_ERROR,
+ "gcry_mpi_scan",
+ rc);
+ gcry_mpi_release (result);
+ result = NULL;
+ status = GNUNET_SCALARPRODUCT_STATUS_INVALID_RESPONSE;
+ }
+ else
+ {
+ if (0 < (int32_t) ntohl (msg->range))
+ gcry_mpi_add (result, result, num);
+ else
+ gcry_mpi_sub (result, result, num);
+ gcry_mpi_release (num);
+ }
}
- h->stats = GNUNET_STATISTICS_create ("scalarproduct-api", cfg);
- if (!h->th){
- LOG (GNUNET_ERROR_TYPE_ERROR,
- _("Failed to send a message to the statistics service\n"));
- GNUNET_CLIENT_disconnect(h->client);
- GNUNET_free(h);
- return NULL;
}
-
- size = sizeof (struct GNUNET_SCALARPRODUCT_client_request) + element_count * sizeof (int32_t);
-
- h->cont_datum = cont;
- h->cont_cls = cont_cls;
- h->response_proc = &process_result_message;
- h->cfg = cfg;
- h->msg = GNUNET_malloc (size);
- memcpy (&h->key, key, sizeof (struct GNUNET_HashCode));
-
- msg = (struct GNUNET_SCALARPRODUCT_client_request*) h->msg;
- msg->header.size = htons (size);
- msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE);
- msg->element_count = htonl (element_count);
-
- vector = (int32_t*) &msg[1];
- // copy each element over to the message
- for (i = 0; i < element_count; i++)
- vector[i] = htonl(elements[i]);
-
- memcpy (&msg->key, key, sizeof (struct GNUNET_HashCode));
-
-
- h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, size,
- GNUNET_TIME_UNIT_FOREVER_REL,
- GNUNET_YES, // retry is OK in the initial stage
- &transmit_request, h);
- if (!h->th)
- {
- LOG (GNUNET_ERROR_TYPE_ERROR,
- _ ("Failed to send a message to the scalarproduct service\n"));
- GNUNET_STATISTICS_destroy(h->GNUNET_YES);
- GNUNET_CLIENT_disconnect(h->client);
- GNUNET_free(h->msg);
- GNUNET_free(h);
- return NULL;
- }
- return h;
+ if (NULL != h->cont_datum)
+ h->cont_datum (h->cont_cls,
+ status,
+ result);
+ if (NULL != result)
+ gcry_mpi_release (result);
+ GNUNET_SCALARPRODUCT_cancel (h);
}
/**
* Request by Alice's client for computing a scalar product
- *
- * @param h handle to the master context
- * @param key Session key - unique to the requesting client
+ *
+ * @param cfg the gnunet configuration handle
+ * @param session_key Session key should be unique to the requesting client
* @param peer PeerID of the other peer
* @param elements Array of elements of the vector
- * @param element_count Number of elements in the vector
- * @param mask Array of the mask
- * @param mask_bytes number of bytes in the mask
+ * @param element_count Number of elements in the @a elements vector
* @param cont Callback function
- * @param cont_cls Closure for the callback function
+ * @param cont_cls Closure for @a cont
+ * @return a new handle for this computation
*/
-struct GNUNET_SCALARPRODUCT_Handle *
-GNUNET_SCALARPRODUCT_request (const struct GNUNET_CONFIGURATION_Handle *cfg,
- const struct GNUNET_HashCode * key,
- const struct GNUNET_PeerIdentity *peer,
- const int32_t * elements,
- uint32_t element_count,
- const unsigned char * mask,
- uint32_t mask_bytes,
- GNUNET_SCALARPRODUCT_DatumProcessor cont,
- void *cont_cls)
+struct GNUNET_SCALARPRODUCT_ComputationHandle *
+GNUNET_SCALARPRODUCT_start_computation (const struct GNUNET_CONFIGURATION_Handle *cfg,
+ const struct GNUNET_HashCode *session_key,
+ const struct GNUNET_PeerIdentity *peer,
+ const struct GNUNET_SCALARPRODUCT_Element *elements,
+ uint32_t element_count,
+ GNUNET_SCALARPRODUCT_DatumProcessor cont,
+ void *cont_cls)
{
- struct GNUNET_CLIENT_Connection *client;
- struct GNUNET_SCALARPRODUCT_Handle *h;
- struct GNUNET_SCALARPRODUCT_client_request *msg;
- int32_t * vector;
- uint16_t size;
- uint64_t i;
-
- GNUNET_assert(key);
- GNUNET_assert(peer);
- GNUNET_assert(elements);
- GNUNET_assert(mask);
- GNUNET_assert(cont);
- GNUNET_assert(element_count > 1);
- GNUNET_assert(mask_bytes != 0);
- GNUNET_assert (GNUNET_SERVER_MAX_MESSAGE_SIZE >= sizeof (struct GNUNET_SCALARPRODUCT_client_request)
- + element_count * sizeof (int32_t)
- + mask_length);
- client = GNUNET_CLIENT_connect ("scalarproduct", cfg);
-
- if (!client)
- {
- LOG (GNUNET_ERROR_TYPE_ERROR,
- _ ("Failed to connect to the scalarproduct service\n"));
- return NULL;
- }
- size = sizeof (struct GNUNET_SCALARPRODUCT_client_request) + element_count * sizeof (int32_t) + mask_length;
-
- h = GNUNET_new (struct GNUNET_SCALARPRODUCT_Handle);
+ struct GNUNET_SCALARPRODUCT_ComputationHandle *h
+ = GNUNET_new (struct GNUNET_SCALARPRODUCT_ComputationHandle);
+ struct GNUNET_MQ_MessageHandler handlers[] = {
+ GNUNET_MQ_hd_var_size (response,
+ GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT,
+ struct ClientResponseMessage,
+ h),
+ GNUNET_MQ_handler_end ()
+ };
+ struct GNUNET_MQ_Envelope *env;
+ struct AliceComputationMessage *msg;
+ struct ComputationBobCryptodataMultipartMessage *mmsg;
+ uint32_t size;
+ uint16_t possible;
+ uint16_t todo;
+ uint32_t element_count_transfered;
+
+ if (GNUNET_SYSERR == check_unique (elements,
+ element_count))
+ return NULL;
+ h->mq = GNUNET_CLIENT_connect (cfg,
+ "scalarproduct-alice",
+ handlers,
+ &mq_error_handler,
+ h);
+ if (NULL == h->mq)
+ {
+ /* missconfigured scalarproduct service */
+ GNUNET_break (0);
+ GNUNET_free (h);
+ return NULL;
+ }
h->cont_datum = cont;
h->cont_cls = cont_cls;
- h->response_proc = &process_status_message;
- h->client = client;
+ h->response_proc = &process_result_message;
h->cfg = cfg;
- h->msg = GNUNET_malloc (size);
- memcpy (&h->key, key, sizeof (struct GNUNET_HashCode));
-
- msg = (struct GNUNET_SCALARPRODUCT_client_request*) h->msg;
- msg->header.size = htons (size);
- msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE);
- msg->element_count = htons (element_count);
- msg->mask_length = htons (mask_length);
-
- vector = (int32_t*) &msg[1];
- // copy each element over to the message
- for (i = 0; i < element_count; i++)
- vector[i] = htonl(elements[i]);
-
- memcpy (&msg->peer, peer, sizeof (struct GNUNET_PeerIdentity));
- memcpy (&msg->key, key, sizeof (struct GNUNET_HashCode));
- memcpy (&vector[element_count], mask, mask_length);
-
- h->stats = GNUNET_STATISTICS_create ("scalarproduct-api", cfg);
- h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, size,
- GNUNET_TIME_UNIT_FOREVER_REL,
- GNUNET_YES, // retry is OK in the initial stage
- &transmit_request, h);
- if ( !h->th)
- {
- LOG (GNUNET_ERROR_TYPE_ERROR,
- _ ("Failed to send a message to the scalarproduct service\n"));
- return NULL;
- }
+ h->key = *session_key;
+
+ possible = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - sizeof (struct AliceComputationMessage))
+ / sizeof (struct GNUNET_SCALARPRODUCT_Element);
+ todo = GNUNET_MIN (possible,
+ element_count);
+ size = todo * sizeof (struct GNUNET_SCALARPRODUCT_Element);
+ env = GNUNET_MQ_msg_extra (msg,
+ size,
+ GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE);
+ msg->element_count_total = htonl (element_count);
+ msg->element_count_contained = htonl (todo);
+ msg->reserved = htonl (0);
+ msg->peer = *peer;
+ msg->session_key = *session_key;
+ GNUNET_memcpy (&msg[1],
+ elements,
+ size);
+ GNUNET_MQ_send (h->mq,
+ env);
+ element_count_transfered = todo;
+ possible = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - sizeof (*mmsg))
+ / sizeof (struct GNUNET_SCALARPRODUCT_Element);
+ while (element_count_transfered < element_count)
+ {
+ todo = GNUNET_MIN (possible,
+ element_count - element_count_transfered);
+ size = todo * sizeof (struct GNUNET_SCALARPRODUCT_Element);
+ env = GNUNET_MQ_msg_extra (mmsg,
+ size,
+ GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_ALICE);
+ mmsg->element_count_contained = htonl (todo);
+ GNUNET_memcpy (&mmsg[1],
+ &elements[element_count_transfered],
+ size);
+ element_count_transfered += todo;
+ GNUNET_MQ_send (h->mq,
+ env);
+ }
return h;
}
+
/**
- * Disconnect from the scalarproduct service.
- *
- * @param h handle to the scalarproduct
+ * Cancel an ongoing computation or revoke our collaboration offer.
+ * Closes the connection to the service
+ *
+ * @param h computation handle to terminate
*/
void
-GNUNET_SCALARPRODUCT_disconnect (struct GNUNET_SCALARPRODUCT_Handle * h)
+GNUNET_SCALARPRODUCT_cancel (struct GNUNET_SCALARPRODUCT_ComputationHandle *h)
{
- struct GNUNET_SCALARPRODUCT_QueueEntry * qe;
-
- LOG (GNUNET_ERROR_TYPE_INFO,
- "Disconnecting from VectorProduct\n");
-
- while (NULL != h->queue_head)
- {
- GNUNET_assert (NULL != (qe = free_queue_head_entry (h)));
- qe->response_proc (qe, NULL, GNUNET_SCALARPRODUCT_Status_ServiceDisconnected);
- }
-
- if (h->client != NULL)
- {
- GNUNET_CLIENT_disconnect (h->client);
- h->client = NULL;
- }
-
- GNUNET_STATISTICS_destroy (h->stats, GNUNET_NO);
- h->stats = NULL;
+ if (NULL != h->mq)
+ {
+ GNUNET_MQ_destroy (h->mq);
+ h->mq = NULL;
+ }
+ GNUNET_free (h);
}
-/* end of ext_api.c */
+
+/* end of scalarproduct_api.c */