- use tunnel encryption state to select decryption key
[oweals/gnunet.git] / src / scalarproduct / scalarproduct_api.c
index 09c726c8898ff9ac0345976690e80840288b38c4..dcacd85c98152c7ee1f11d21977b4e6738915610 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"
@@ -95,6 +95,9 @@ struct GNUNET_SCALARPRODUCT_ComputationHandle
    */
   struct GNUNET_SCALARPRODUCT_client_request * msg;
 
+  /**
+   * The msg handler callback
+   */
   union
   {
   /**
@@ -145,11 +148,11 @@ GNUNET_SCALARPRODUCT_cancel (struct GNUNET_SCALARPRODUCT_ComputationHandle * h);
 
 
 /**
- * Handles the RESULT received in reply of prepare_response from the 
- * service
- * 
- * @param cls Handle to the Master Context
+ * Handles the STATUS received from the service for a response, does not contain a payload
+ *
+ * @param cls our Handle
  * @param msg Pointer to the response received
+ * @param status the condition the request was terminated with (eg: disconnect)
  */
 static void
 process_status_message (void *cls,
@@ -163,11 +166,11 @@ process_status_message (void *cls,
 
 
 /**
- * Handles the RESULT received in reply of prepare_response from the 
- * service
- * 
- * @param cls Handle to the Master Context
+ * Handles the RESULT received from the service for a request, should contain a result MPI value
+ *
+ * @param cls our Handle
  * @param msg Pointer to the response received
+ * @param status the condition the request was terminated with (eg: disconnect)
  */
 static void
 process_result_message (void *cls,
@@ -190,7 +193,7 @@ process_result_message (void *cls,
           gcry_mpi_t num;
           size_t read = 0;
 
-          if (0 != (rc = gcry_mpi_scan (&num, GCRYMPI_FMT_STD, &msg[1], product_len, &read)))
+          if (0 != (rc = gcry_mpi_scan (&num, GCRYMPI_FMT_STD, &message[1], product_len, &read)))
             {
               LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
               gcry_mpi_release (result);
@@ -199,9 +202,9 @@ process_result_message (void *cls,
             }
           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);
             }
@@ -212,10 +215,10 @@ process_result_message (void *cls,
 
 
 /**
- * Called when a response is received from the service. After basic check
+ * Called when a response is received from the service. After basic check, the
  * 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
  */
@@ -245,7 +248,7 @@ receive_cb (void *cls, const struct GNUNET_MessageHeader *msg)
 
       status = GNUNET_SCALARPRODUCT_Status_Success;
     }
-  
+
   if (qe->cont_datum != NULL)
     qe->response_proc (qe, msg, status);
 
@@ -255,12 +258,12 @@ receive_cb (void *cls, const struct GNUNET_MessageHeader *msg)
 
 
 /**
- * Transmits the request to the VectorProduct Sevice
- * 
+ * Transmits the request to the VectorProduct Service
+ *
  * @param cls Closure
  * @param size Size of the buffer
  * @param buf Pointer to the buffer
- * 
+ *
  * @return Size of the message sent
  */
 static size_t
@@ -279,7 +282,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;
     }
@@ -307,22 +310,24 @@ transmit_request (void *cls, size_t size,
 
 
 /**
- * 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
+ * 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 vector
  * @param cont Callback function
  * @param cont_cls Closure for the callback function
+ * 
+ * @return a new handle for this computation
  */
 struct GNUNET_SCALARPRODUCT_ComputationHandle *
-GNUNET_SCALARPRODUCT_response (const struct GNUNET_CONFIGURATION_Handle *cfg,
+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)
+                               void * cont_cls)
 {
   struct GNUNET_SCALARPRODUCT_ComputationHandle *h;
   struct GNUNET_SCALARPRODUCT_client_request *msg;
@@ -358,7 +363,7 @@ GNUNET_SCALARPRODUCT_response (const struct GNUNET_CONFIGURATION_Handle *cfg,
   h->response_proc = &process_status_message;
   h->cfg = cfg;
   memcpy (&h->key, key, sizeof (struct GNUNET_HashCode));
-  
+
   msg = (struct GNUNET_SCALARPRODUCT_client_request*) GNUNET_malloc (size);
   h->msg = msg;
   msg->header.size = htons (size);
@@ -393,9 +398,9 @@ 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 cfg the gnunet configuration handle
+ * @param 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
@@ -403,9 +408,11 @@ GNUNET_SCALARPRODUCT_response (const struct GNUNET_CONFIGURATION_Handle *cfg,
  * @param mask_bytes number of bytes in the mask
  * @param cont Callback function
  * @param cont_cls Closure for the callback function
+ * 
+ * @return a new handle for this computation
  */
 struct GNUNET_SCALARPRODUCT_ComputationHandle *
-GNUNET_SCALARPRODUCT_request (const struct GNUNET_CONFIGURATION_Handle *cfg,
+GNUNET_SCALARPRODUCT_request (const struct GNUNET_CONFIGURATION_Handle * cfg,
                               const struct GNUNET_HashCode * key,
                               const struct GNUNET_PeerIdentity *peer,
                               const int32_t * elements,
@@ -413,7 +420,7 @@ GNUNET_SCALARPRODUCT_request (const struct GNUNET_CONFIGURATION_Handle *cfg,
                               const unsigned char * mask,
                               uint32_t mask_bytes,
                               GNUNET_SCALARPRODUCT_DatumProcessor cont,
-                              void *cont_cls)
+                              void * cont_cls)
 {
   struct GNUNET_SCALARPRODUCT_ComputationHandle *h;
   struct GNUNET_SCALARPRODUCT_client_request *msg;
@@ -451,7 +458,7 @@ GNUNET_SCALARPRODUCT_request (const struct GNUNET_CONFIGURATION_Handle *cfg,
   h->response_proc = &process_result_message;
   h->cfg = cfg;
   memcpy (&h->key, key, sizeof (struct GNUNET_HashCode));
-  
+
   msg = (struct GNUNET_SCALARPRODUCT_client_request*) GNUNET_malloc (size);
   h->msg = msg;
   msg->header.size = htons (size);
@@ -463,7 +470,7 @@ GNUNET_SCALARPRODUCT_request (const struct GNUNET_CONFIGURATION_Handle *cfg,
   // 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_bytes);
@@ -488,9 +495,10 @@ GNUNET_SCALARPRODUCT_request (const struct GNUNET_CONFIGURATION_Handle *cfg,
 
 
 /**
- * Disconnect from the scalarproduct service.
- * 
- * @param h a computation handle to cancel
+ * Cancel an ongoing computation or revoke our collaboration offer.
+ * Closes the connection to the service
+ *
+ * @param h computation handle to terminate
  */
 void
 GNUNET_SCALARPRODUCT_cancel (struct GNUNET_SCALARPRODUCT_ComputationHandle * h)
@@ -502,18 +510,18 @@ GNUNET_SCALARPRODUCT_cancel (struct GNUNET_SCALARPRODUCT_ComputationHandle * h)
       if (qe == h)
         {
           GNUNET_CONTAINER_DLL_remove (head, tail, qe);
-          if (NULL == qe->th)
+          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 (qe->msg);
+          GNUNET_free_non_null (qe->msg);
           GNUNET_free (qe);
           break;
         }
     }
 }
 /**
- * Cancel ALL our ongoing scalar product computations and collaboration offers.
+ * Cancel ALL ongoing computation or revoke our collaboration offer.
  * Closes ALL connections to the service
  */
 void
@@ -525,13 +533,13 @@ GNUNET_SCALARPRODUCT_disconnect ()
     for (qe = head; head != NULL; qe = head)
     {
         GNUNET_CONTAINER_DLL_remove (head, tail, qe);
-        if (NULL == qe->th)
+        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 (qe->msg);
+        GNUNET_free_non_null (qe->msg);
         GNUNET_free (qe);
     }
 }
 
-/* end of ext_api.c */
+/* end of scalarproduct_api.c */