- fixes
[oweals/gnunet.git] / src / scalarproduct / scalarproduct_api.c
index 633034b31453ff463bc3f7e6bc6723b84b587a28..0f8a409cc48a149028c8151f0cdeaa4ffe891766 100644 (file)
@@ -23,7 +23,7 @@
  * @brief API for the scalarproduct
  * @author Christian Fuchs
  * @author Gaurav Kukreja
- * 
+ *
  */
 #include "platform.h"
 #include "gnunet_util_lib.h"
  ***  Datatype Declarations                          **********
  **************************************************************/
 
+/**
+ * the abstraction function for our internal callback
+ */
+typedef void (*GNUNET_SCALARPRODUCT_ResponseMessageHandler) (void *cls,
+                                                             const struct GNUNET_MessageHeader *msg,
+                                                             enum GNUNET_SCALARPRODUCT_ResponseStatus status);
+
 /**
  * Entry in the request queue per client
  */
@@ -71,7 +78,7 @@ struct GNUNET_SCALARPRODUCT_ComputationHandle
   /**
    * The shared session key identifying this computation
    */
-  struct GNUNET_HashCode key;
+  struct GNUNET_HashCode key;
 
   /**
    * Current transmit handle.
@@ -90,15 +97,15 @@ struct GNUNET_SCALARPRODUCT_ComputationHandle
 
   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;
+  /**
+   * 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;
   };
 
   /**
@@ -138,9 +145,9 @@ GNUNET_SCALARPRODUCT_cancel (struct GNUNET_SCALARPRODUCT_ComputationHandle * h);
 
 
 /**
- * Handles the RESULT received in reply of prepare_response from the 
+ * 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
  */
@@ -156,9 +163,9 @@ process_status_message (void *cls,
 
 
 /**
- * Handles the RESULT received in reply of prepare_response from the 
+ * 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
  */
@@ -171,9 +178,9 @@ process_result_message (void *cls,
   const struct GNUNET_SCALARPRODUCT_client_response *message =
           (const struct GNUNET_SCALARPRODUCT_client_response *) msg;
   gcry_mpi_t result = NULL;
+  gcry_error_t rc;
 
-  if (GNUNET_SCALARPRODUCT_Status_Success == status
-      && qe->cont_datum != NULL)
+  if (GNUNET_SCALARPRODUCT_Status_Success == status)
     {
       size_t product_len = ntohl (message->product_length);
       result = gcry_mpi_new (0);
@@ -183,18 +190,18 @@ process_result_message (void *cls,
           gcry_mpi_t num;
           size_t read = 0;
 
-          if (0 != gcry_mpi_scan (&num, GCRYMPI_FMT_USG, &msg[1], product_len, &read))
+          if (0 != (rc = gcry_mpi_scan (&num, GCRYMPI_FMT_STD, &message[1], product_len, &read)))
             {
-              LOG (GNUNET_ERROR_TYPE_ERROR, "Could not convert to mpi to value!\n");
+              LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
               gcry_mpi_release (result);
               result = NULL;
               status = GNUNET_SCALARPRODUCT_Status_InvalidResponse;
             }
           else
             {
-              if (message->range > 0)
+              if (0 < message->range)
                 gcry_mpi_add (result, result, num);
-              else
+              else if (0 > message->range)
                 gcry_mpi_sub (result, result, num);
               gcry_mpi_release (num);
             }
@@ -208,7 +215,7 @@ process_result_message (void *cls,
  * 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
  */
@@ -242,17 +249,18 @@ receive_cb (void *cls, const struct GNUNET_MessageHeader *msg)
   if (qe->cont_datum != NULL)
     qe->response_proc (qe, msg, status);
 
+  GNUNET_CONTAINER_DLL_remove (head, tail, qe);
   GNUNET_free (qe);
 }
 
 
 /**
  * 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
  */
 static size_t
@@ -271,6 +279,7 @@ transmit_request (void *cls, size_t size,
       // notify caller about the error, done here.
       if (qe->cont_datum != NULL)
         qe->response_proc (qe, NULL, GNUNET_SCALARPRODUCT_Status_Failure);
+
       GNUNET_SCALARPRODUCT_cancel (cls);
       return 0;
     }
@@ -298,8 +307,8 @@ transmit_request (void *cls, size_t size,
 
 
 /**
- * Used by Bob's client to cooperate with Alice, 
- * 
+ * 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
@@ -321,12 +330,8 @@ GNUNET_SCALARPRODUCT_response (const struct GNUNET_CONFIGURATION_Handle *cfg,
   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));
+                 + element_count * sizeof (int32_t));
   h = GNUNET_new (struct GNUNET_SCALARPRODUCT_ComputationHandle);
   h->client = GNUNET_CLIENT_connect ("scalarproduct", cfg);
   if (!h->client)
@@ -337,7 +342,7 @@ GNUNET_SCALARPRODUCT_response (const struct GNUNET_CONFIGURATION_Handle *cfg,
       return NULL;
     }
   h->stats = GNUNET_STATISTICS_create ("scalarproduct-api", cfg);
-  if (!h->th)
+  if (!h->stats)
     {
       LOG (GNUNET_ERROR_TYPE_ERROR,
            _ ("Failed to send a message to the statistics service\n"));
@@ -346,18 +351,18 @@ GNUNET_SCALARPRODUCT_response (const struct GNUNET_CONFIGURATION_Handle *cfg,
       return NULL;
     }
 
-  size = sizeof (struct GNUNET_SCALARPRODUCT_client_request) +element_count * sizeof (int32_t);
+  size = sizeof (struct GNUNET_SCALARPRODUCT_client_request) + element_count * sizeof (int32_t);
 
   h->cont_status = cont;
   h->cont_cls = cont_cls;
-  h->response_proc = &process_result_message;
+  h->response_proc = &process_status_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 = (struct GNUNET_SCALARPRODUCT_client_request*) GNUNET_malloc (size);
+  h->msg = msg;
   msg->header.size = htons (size);
-  msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE);
+  msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_BOB);
   msg->element_count = htonl (element_count);
 
   vector = (int32_t*) & msg[1];
@@ -388,7 +393,7 @@ GNUNET_SCALARPRODUCT_response (const struct GNUNET_CONFIGURATION_Handle *cfg,
 
 /**
  * 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 peer PeerID of the other peer
@@ -430,7 +435,7 @@ GNUNET_SCALARPRODUCT_request (const struct GNUNET_CONFIGURATION_Handle *cfg,
       return NULL;
     }
   h->stats = GNUNET_STATISTICS_create ("scalarproduct-api", cfg);
-  if (!h->th)
+  if (!h->stats)
     {
       LOG (GNUNET_ERROR_TYPE_ERROR,
            _ ("Failed to send a message to the statistics service\n"));
@@ -439,20 +444,20 @@ GNUNET_SCALARPRODUCT_request (const struct GNUNET_CONFIGURATION_Handle *cfg,
       return NULL;
     }
 
-  size = sizeof (struct GNUNET_SCALARPRODUCT_client_request) +element_count * sizeof (int32_t) + mask_bytes;
+  size = sizeof (struct GNUNET_SCALARPRODUCT_client_request) + element_count * sizeof (int32_t) + mask_bytes;
 
   h->cont_datum = cont;
   h->cont_cls = cont_cls;
-  h->response_proc = &process_status_message;
+  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 = (struct GNUNET_SCALARPRODUCT_client_request*) GNUNET_malloc (size);
+  h->msg = 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_bytes);
+  msg->element_count = htonl (element_count);
+  msg->mask_length = htonl (mask_bytes);
 
   vector = (int32_t*) & msg[1];
   // copy each element over to the message
@@ -484,7 +489,7 @@ GNUNET_SCALARPRODUCT_request (const struct GNUNET_CONFIGURATION_Handle *cfg,
 
 /**
  * Disconnect from the scalarproduct service.
- * 
+ *
  * @param h a computation handle to cancel
  */
 void
@@ -497,17 +502,36 @@ GNUNET_SCALARPRODUCT_cancel (struct GNUNET_SCALARPRODUCT_ComputationHandle * h)
       if (qe == h)
         {
           GNUNET_CONTAINER_DLL_remove (head, tail, qe);
-          LOG (GNUNET_ERROR_TYPE_INFO,
-               "Disconnecting from VectorProduct\n");
-          if (NULL == qe->th)
+          if (NULL != qe->th)
             GNUNET_CLIENT_notify_transmit_ready_cancel (qe->th);
-          GNUNET_CLIENT_disconnect (h->client);
-          GNUNET_STATISTICS_destroy (h->stats, GNUNET_YES);
-          GNUNET_free (qe->msg);
+          GNUNET_CLIENT_disconnect (qe->client);
+          GNUNET_STATISTICS_destroy (qe->stats, GNUNET_YES);
+          GNUNET_free_non_null (qe->msg);
           GNUNET_free (qe);
           break;
         }
     }
 }
+/**
+ * Cancel ALL our ongoing scalar product computations and collaboration offers.
+ * Closes ALL connections to the service
+ */
+void
+GNUNET_SCALARPRODUCT_disconnect ()
+{
+    struct GNUNET_SCALARPRODUCT_ComputationHandle * qe;
+
+    LOG (GNUNET_ERROR_TYPE_INFO, "Disconnecting from VectorProduct\n");
+    for (qe = head; head != NULL; qe = head)
+    {
+        GNUNET_CONTAINER_DLL_remove (head, tail, qe);
+        if (NULL != qe->th)
+            GNUNET_CLIENT_notify_transmit_ready_cancel (qe->th);
+        GNUNET_CLIENT_disconnect (qe->client);
+        GNUNET_STATISTICS_destroy (qe->stats, GNUNET_YES);
+        GNUNET_free_non_null (qe->msg);
+        GNUNET_free (qe);
+    }
+}
 
 /* end of ext_api.c */