-improve UDP logging
[oweals/gnunet.git] / src / scalarproduct / gnunet-service-scalarproduct_alice.c
index e75915f9aa516b589bc1d68f548623561b81fbcd..0f6b7ca32fd99fdbfde1ea6315bb69600a1596d4 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2013, 2014 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2013, 2014 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -164,14 +164,21 @@ struct AliceServiceSession
    * Already transferred elements from client to us.
    * Less or equal than @e total.
    */
-  uint32_t transferred_element_count;
+  uint32_t client_received_element_count;
 
   /**
-   * Is this session active (#GNUNET_YES), Concluded (#GNUNET_NO), or
-   * had an error (#GNUNET_SYSERR).
-   * FIXME: replace with proper enum for status codes!
+   * Already transferred elements from Bob to us.
+   * Less or equal than @e total.
+   */
+  uint32_t cadet_received_element_count;
+
+  /**
+   * State of this session.   In
+   * #GNUNET_SCALARPRODUCT_STATUS_ACTIVE while operation is
+   * ongoing, afterwards in #GNUNET_SCALARPRODUCT_STATUS_SUCCESS or
+   * #GNUNET_SCALARPRODUCT_STATUS_FAILURE.
    */
-  int32_t active;
+  enum GNUNET_SCALARPRODUCT_ResponseStatus status;
 
   /**
    * Flag to prevent recursive calls to #destroy_service_session() from
@@ -217,9 +224,9 @@ static struct GNUNET_CADET_Handle *my_cadet;
  * @return #GNUNET_OK (continue to iterate)
  */
 static int
-free_element (void *cls,
-              const struct GNUNET_HashCode *key,
-              void *value)
+free_element_cb (void *cls,
+                 const struct GNUNET_HashCode *key,
+                 void *value)
 {
   struct GNUNET_SCALARPRODUCT_Element *e = value;
 
@@ -266,7 +273,7 @@ destroy_service_session (struct AliceServiceSession *s)
   if (NULL != s->intersected_elements)
   {
     GNUNET_CONTAINER_multihashmap_iterate (s->intersected_elements,
-                                           &free_element,
+                                           &free_element_cb,
                                            s);
     GNUNET_CONTAINER_multihashmap_destroy (s->intersected_elements);
     s->intersected_elements = NULL;
@@ -326,12 +333,12 @@ prepare_client_end_notification (struct AliceServiceSession *session)
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Sending session-end notification with status %d to client for session %s\n",
-              session->active,
+              session->status,
               GNUNET_h2s (&session->session_id));
   e = GNUNET_MQ_msg (msg,
                      GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT);
   msg->product_length = htonl (0);
-  msg->status = htonl (session->active);
+  msg->status = htonl (session->status);
   GNUNET_MQ_send (session->client_mq,
                   e);
 }
@@ -399,6 +406,7 @@ transmit_client_response (struct AliceServiceSession *s)
   e = GNUNET_MQ_msg_extra (msg,
                            product_length,
                            GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT);
+  msg->status = htonl (GNUNET_SCALARPRODUCT_STATUS_SUCCESS);
   msg->range = htonl (range);
   msg->product_length = htonl (product_length);
   if (NULL != product_exported)
@@ -439,11 +447,16 @@ cb_channel_destruction (void *cls,
               "Peer disconnected, terminating session %s with peer %s\n",
               GNUNET_h2s (&s->session_id),
               GNUNET_i2s (&s->peer));
+  if (NULL != s->cadet_mq)
+  {
+    GNUNET_MQ_destroy (s->cadet_mq);
+    s->cadet_mq = NULL;
+  }
   s->channel = NULL;
-  if (GNUNET_YES == s->active)
+  if (GNUNET_SCALARPRODUCT_STATUS_ACTIVE == s->status)
   {
     /* We didn't get an answer yet, fail with error */
-    s->active = GNUNET_SYSERR;
+    s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
     prepare_client_end_notification (s);
   }
 }
@@ -640,7 +653,7 @@ handle_bobs_cryptodata_multipart (void *cls,
                                   const struct GNUNET_MessageHeader *message)
 {
   struct AliceServiceSession *s = *channel_ctx;
-  const struct MultipartMessage *msg;
+  const struct BobCryptodataMultipartMessage *msg;
   const struct GNUNET_CRYPTO_PaillierCiphertext *payload;
   size_t i;
   uint32_t contained;
@@ -653,17 +666,17 @@ handle_bobs_cryptodata_multipart (void *cls,
     return GNUNET_SYSERR;
   }
   msg_size = ntohs (message->size);
-  if (sizeof (struct MultipartMessage) > msg_size)
+  if (sizeof (struct BobCryptodataMultipartMessage) > msg_size)
   {
     GNUNET_break_op (0);
     return GNUNET_SYSERR;
   }
-  msg = (const struct MultipartMessage *) message;
+  msg = (const struct BobCryptodataMultipartMessage *) message;
   contained = ntohl (msg->contained_element_count);
-  required_size = sizeof (struct MultipartMessage)
+  required_size = sizeof (struct BobCryptodataMultipartMessage)
     + 2 * contained * sizeof (struct GNUNET_CRYPTO_PaillierCiphertext);
   if ( (required_size != msg_size) ||
-       (s->transferred_element_count + contained > s->used_element_count) )
+       (s->cadet_received_element_count + contained > s->used_element_count) )
   {
     GNUNET_break (0);
     return GNUNET_SYSERR;
@@ -677,15 +690,16 @@ handle_bobs_cryptodata_multipart (void *cls,
   /* Convert each k[][perm] to its MPI_value */
   for (i = 0; i < contained; i++)
   {
-    memcpy (&s->r[s->transferred_element_count + i],
+    memcpy (&s->r[s->cadet_received_element_count + i],
             &payload[2 * i],
             sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
-    memcpy (&s->r_prime[s->transferred_element_count + i],
+    memcpy (&s->r_prime[s->cadet_received_element_count + i],
             &payload[2 * i],
             sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
   }
-  s->transferred_element_count += contained;
-  if (s->transferred_element_count != s->used_element_count)
+  s->cadet_received_element_count += contained;
+  GNUNET_CADET_receive_done (s->channel);
+  if (s->cadet_received_element_count != s->used_element_count)
     return GNUNET_OK;
 
   s->product = compute_scalar_product (s);
@@ -712,7 +726,7 @@ handle_bobs_cryptodata_message (void *cls,
                                 const struct GNUNET_MessageHeader *message)
 {
   struct AliceServiceSession *s = *channel_ctx;
-  const struct ServiceResponseMessage *msg;
+  const struct BobCryptodataMessage *msg;
   const struct GNUNET_CRYPTO_PaillierCiphertext *payload;
   uint32_t i;
   uint32_t contained;
@@ -725,14 +739,14 @@ handle_bobs_cryptodata_message (void *cls,
     return GNUNET_SYSERR;
   }
   msg_size = ntohs (message->size);
-  if (sizeof (struct ServiceResponseMessage) > msg_size)
+  if (sizeof (struct BobCryptodataMessage) > msg_size)
   {
     GNUNET_break_op (0);
     return GNUNET_SYSERR;
   }
-  msg = (const struct ServiceResponseMessage *) message;
+  msg = (const struct BobCryptodataMessage *) message;
   contained = ntohl (msg->contained_element_count);
-  required_size = sizeof (struct ServiceResponseMessage)
+  required_size = sizeof (struct BobCryptodataMessage)
     + 2 * contained * sizeof (struct GNUNET_CRYPTO_PaillierCiphertext)
     + 2 * sizeof (struct GNUNET_CRYPTO_PaillierCiphertext);
   if ( (msg_size != required_size) ||
@@ -742,14 +756,18 @@ handle_bobs_cryptodata_message (void *cls,
     GNUNET_break_op (0);
     return GNUNET_SYSERR;
   }
-  if ( (NULL == s->sorted_elements) ||
-       (s->used_element_count != s->transferred_element_count) )
+  if (NULL == s->sorted_elements)
+  {
+    /* we're not ready yet, how can Bob be? */
+    GNUNET_break_op (0);
+    return GNUNET_SYSERR;
+  }
+  if (s->total != s->client_received_element_count)
   {
     /* we're not ready yet, how can Bob be? */
     GNUNET_break_op (0);
     return GNUNET_SYSERR;
   }
-
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Received %u crypto values from Bob\n",
               (unsigned int) contained);
@@ -774,9 +792,10 @@ handle_bobs_cryptodata_message (void *cls,
             &payload[2 * i + 1],
             sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
   }
-  s->transferred_element_count = contained;
+  s->cadet_received_element_count = contained;
+  GNUNET_CADET_receive_done (s->channel);
 
-  if (s->transferred_element_count != s->used_element_count)
+  if (s->cadet_received_element_count != s->used_element_count)
   {
     /* More to come */
     return GNUNET_OK;
@@ -842,7 +861,7 @@ element_cmp (const void *a,
  * Maximum number of elements we can put into a single cryptodata
  * message
  */
-#define ELEMENT_CAPACITY ((GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - sizeof (struct AliceCryptodataMessage)) / sizeof (struct GNUNET_CRYPTO_PaillierCiphertext))
+#define ELEMENT_CAPACITY ((GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE - 1 - sizeof (struct AliceCryptodataMessage)) / sizeof (struct GNUNET_CRYPTO_PaillierCiphertext))
 
 
 /**
@@ -883,8 +902,9 @@ send_alices_cryptodata_message (struct AliceServiceSession *s)
     if (todo_count > ELEMENT_CAPACITY)
       todo_count = ELEMENT_CAPACITY;
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Sending %u crypto values to Bob\n",
-                (unsigned int) todo_count);
+                "Sending %u/%u crypto values to Bob\n",
+                (unsigned int) todo_count,
+                (unsigned int) s->used_element_count);
 
     e = GNUNET_MQ_msg_extra (msg,
                              todo_count * sizeof (struct GNUNET_CRYPTO_PaillierCiphertext),
@@ -892,15 +912,16 @@ send_alices_cryptodata_message (struct AliceServiceSession *s)
     msg->contained_element_count = htonl (todo_count);
     payload = (struct GNUNET_CRYPTO_PaillierCiphertext *) &msg[1];
     a = gcry_mpi_new (0);
-    for (i = off; i < todo_count; i++)
+    for (i = off; i < off + todo_count; i++)
     {
       gcry_mpi_add (a,
                     s->sorted_elements[i].value,
                     my_offset);
-      GNUNET_CRYPTO_paillier_encrypt (&my_pubkey,
-                                      a,
-                                      3,
-                                      &payload[i - off]);
+      GNUNET_assert (3 ==
+                     GNUNET_CRYPTO_paillier_encrypt (&my_pubkey,
+                                                     a,
+                                                     3,
+                                                     &payload[i - off]));
     }
     gcry_mpi_release (a);
     off += todo_count;
@@ -946,7 +967,11 @@ cb_intersection_element_removed (void *cls,
     return;
   case GNUNET_SET_STATUS_DONE:
     s->intersection_op = NULL;
-    s->intersection_set = NULL;
+    if (NULL != s->intersection_set)
+    {
+      GNUNET_SET_destroy (s->intersection_set);
+      s->intersection_set = NULL;
+    }
     send_alices_cryptodata_message (s);
     return;
   case GNUNET_SET_STATUS_HALF_DONE:
@@ -963,8 +988,12 @@ cb_intersection_element_removed (void *cls,
       s->intersection_listen = NULL;
     }
     s->intersection_op = NULL;
-    s->intersection_set = NULL;
-    s->active = GNUNET_SYSERR;
+    if (NULL != s->intersection_set)
+    {
+      GNUNET_SET_destroy (s->intersection_set);
+      s->intersection_set = NULL;
+    }
+    s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
     prepare_client_end_notification (s);
     return;
   default:
@@ -1012,7 +1041,7 @@ cb_intersection_request_alice (void *cls,
   if (NULL == s->intersection_op)
   {
     GNUNET_break (0);
-    s->active = GNUNET_SYSERR;
+    s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
     prepare_client_end_notification (s);
     return;
   }
@@ -1020,11 +1049,14 @@ cb_intersection_request_alice (void *cls,
       GNUNET_SET_commit (s->intersection_op,
                          s->intersection_set))
   {
-    s->active = GNUNET_SYSERR;
+    GNUNET_break (0);
+    s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
     prepare_client_end_notification (s);
     return;
   }
+  GNUNET_SET_destroy (s->intersection_set);
   s->intersection_set = NULL;
+  GNUNET_SET_listen_cancel (s->intersection_listen);
   s->intersection_listen = NULL;
 }
 
@@ -1051,7 +1083,7 @@ client_request_complete_alice (struct AliceServiceSession *s)
                                    GNUNET_CADET_OPTION_RELIABLE);
   if (NULL == s->channel)
   {
-    s->active = GNUNET_SYSERR;
+    s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
     prepare_client_end_notification (s);
     return;
   }
@@ -1064,7 +1096,7 @@ client_request_complete_alice (struct AliceServiceSession *s)
                          s);
   if (NULL == s->intersection_listen)
   {
-    s->active = GNUNET_SYSERR;
+    s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
     GNUNET_CADET_channel_destroy (s->channel);
     s->channel = NULL;
     prepare_client_end_notification (s);
@@ -1074,6 +1106,7 @@ client_request_complete_alice (struct AliceServiceSession *s)
   e = GNUNET_MQ_msg (msg,
                      GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SESSION_INITIALIZATION);
   msg->session_id = s->session_id;
+  msg->public_key = my_pubkey;
   GNUNET_MQ_send (s->cadet_mq,
                   e);
 }
@@ -1092,7 +1125,7 @@ GSS_handle_alice_client_message_multipart (void *cls,
                                            struct GNUNET_SERVER_Client *client,
                                            const struct GNUNET_MessageHeader *message)
 {
-  const struct ComputationMultipartMessage * msg;
+  const struct ComputationBobCryptodataMultipartMessage * msg;
   struct AliceServiceSession *s;
   uint32_t contained_count;
   const struct GNUNET_SCALARPRODUCT_Element *elements;
@@ -1112,33 +1145,31 @@ GSS_handle_alice_client_message_multipart (void *cls,
     return;
   }
   msize = ntohs (message->size);
-  if (msize < sizeof (struct ComputationMultipartMessage))
+  if (msize < sizeof (struct ComputationBobCryptodataMultipartMessage))
   {
     GNUNET_break (0);
     GNUNET_SERVER_receive_done (client,
                                 GNUNET_SYSERR);
     return;
   }
-  msg = (const struct ComputationMultipartMessage *) message;
+  msg = (const struct ComputationBobCryptodataMultipartMessage *) message;
   contained_count = ntohl (msg->element_count_contained);
 
-  if ( (msize != (sizeof (struct ComputationMultipartMessage) +
+  if ( (msize != (sizeof (struct ComputationBobCryptodataMultipartMessage) +
                   contained_count * sizeof (struct GNUNET_SCALARPRODUCT_Element))) ||
        (0 == contained_count) ||
-       (s->total == s->transferred_element_count) ||
-       (s->total < s->transferred_element_count + contained_count) )
+       (s->total == s->client_received_element_count) ||
+       (s->total < s->client_received_element_count + contained_count) )
   {
     GNUNET_break_op (0);
     GNUNET_SERVER_receive_done (client,
                                 GNUNET_SYSERR);
     return;
   }
-  s->transferred_element_count += contained_count;
+  s->client_received_element_count += contained_count;
   elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
   for (i = 0; i < contained_count; i++)
   {
-    if (0 == GNUNET_ntohll (elements[i].value))
-      continue;
     elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
     memcpy (elem,
             &elements[i],
@@ -1163,7 +1194,7 @@ GSS_handle_alice_client_message_multipart (void *cls,
   }
   GNUNET_SERVER_receive_done (client,
                               GNUNET_OK);
-  if (s->total != s->transferred_element_count)
+  if (s->total != s->client_received_element_count)
   {
     /* more to come */
     return;
@@ -1227,11 +1258,11 @@ GSS_handle_alice_client_message (void *cls,
 
   s = GNUNET_new (struct AliceServiceSession);
   s->peer = msg->peer;
-  s->active = GNUNET_YES;
+  s->status = GNUNET_SCALARPRODUCT_STATUS_ACTIVE;
   s->client = client;
   s->client_mq = GNUNET_MQ_queue_for_server_client (client);
   s->total = total_count;
-  s->transferred_element_count = contained_count;
+  s->client_received_element_count = contained_count;
   s->session_id = msg->session_key;
   elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
   s->intersected_elements = GNUNET_CONTAINER_multihashmap_create (s->total,
@@ -1269,7 +1300,7 @@ GSS_handle_alice_client_message (void *cls,
                                          s);
   GNUNET_SERVER_receive_done (client,
                               GNUNET_OK);
-  if (s->total != s->transferred_element_count)
+  if (s->total != s->client_received_element_count)
   {
     /* wait for multipart msg */
     return;
@@ -1290,6 +1321,7 @@ shutdown_task (void *cls,
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Shutting down, initiating cleanup.\n");
+  // FIXME: we have to cut our connections to CADET first!
   if (NULL != my_cadet)
   {
     GNUNET_CADET_disconnect (my_cadet);