converting scalarproduct alice-ecc to new service API
[oweals/gnunet.git] / src / scalarproduct / gnunet-service-scalarproduct_bob.c
index 2a452c64f401760898eb445a5c11dfaf39f6f9ec..a19c909e13b8675b6b07ce26874819c35256b5de 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, 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
@@ -14,8 +14,8 @@
 
      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/gnunet-service-scalarproduct_bob.c
@@ -103,6 +103,11 @@ struct BobServiceSession
    */
   struct GNUNET_SET_OperationHandle *intersection_op;
 
+  /**
+   * CADET port we are listening on.
+   */
+  struct GNUNET_CADET_Port *port;
+
   /**
    * a(Alice)
    */
@@ -140,30 +145,42 @@ struct BobServiceSession
   struct CadetIncomingSession *cadet;
 
   /**
-   * The computed scalar
+   * How many elements will be supplied in total from the client.
    */
-  gcry_mpi_t product;
+  uint32_t total;
 
   /**
-   * How many elements we were supplied with from the client
+   * Already transferred elements (received) for multipart
+   * messages from client. Always less than @e total.
    */
-  uint32_t total;
+  uint32_t client_received_element_count;
 
   /**
-   * how many elements actually are used for the scalar product.
-   * Size of the arrays in @e r and @e r_prime.
+   * How many elements actually are used for the scalar product.
+   * Size of the arrays in @e r and @e r_prime.  Also sometimes
+   * used as an index into the arrays during construction.
    */
   uint32_t used_element_count;
 
   /**
-   * already transferred elements (sent/received) for multipart messages, less or equal than @e used_element_count for
+   * Counts the number of values received from Alice by us.
+   * Always less than @e used_element_count.
+   */
+  uint32_t cadet_received_element_count;
+
+  /**
+   * Counts the number of values transmitted from us to Alice.
+   * Always less than @e used_element_count.
    */
-  uint32_t transferred_element_count;
+  uint32_t cadet_transmitted_element_count;
 
   /**
-   * Is this session active (#GNUNET_YES), Concluded (#GNUNET_NO), or had an error (#GNUNET_SYSERR)
+   * 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;
 
   /**
    * Are we already in #destroy_service_session()?
@@ -259,12 +276,6 @@ static struct GNUNET_CONTAINER_MultiHashMap *cadet_sessions;
  */
 static struct GNUNET_CADET_Handle *my_cadet;
 
-/**
- * Certain events (callbacks for server & cadet operations) must not
- * be queued after shutdown.
- */
-static int do_shutdown;
-
 
 
 /**
@@ -296,6 +307,25 @@ find_matching_cadet_session (const struct GNUNET_HashCode *key)
 }
 
 
+/**
+ * Callback used to free the elements in the map.
+ *
+ * @param cls NULL
+ * @param key key of the element
+ * @param value the value to free
+ */
+static int
+free_element_cb (void *cls,
+                 const struct GNUNET_HashCode *key,
+                 void *value)
+{
+  struct GNUNET_SCALARPRODUCT_Element *element = value;
+
+  GNUNET_free (element);
+  return GNUNET_OK;
+}
+
+
 /**
  * Destroy session state, we are done with it.
  *
@@ -340,7 +370,9 @@ destroy_service_session (struct BobServiceSession *s)
                                                        s));
   if (NULL != s->intersected_elements)
   {
-    /* FIXME: free elements */
+    GNUNET_CONTAINER_multihashmap_iterate (s->intersected_elements,
+                                           &free_element_cb,
+                                           NULL);
     GNUNET_CONTAINER_multihashmap_destroy (s->intersected_elements);
     s->intersected_elements = NULL;
   }
@@ -376,11 +408,7 @@ destroy_service_session (struct BobServiceSession *s)
     GNUNET_free (s->r_prime);
     s->r_prime = NULL;
   }
-  if (NULL != s->product)
-  {
-    gcry_mpi_release (s->product);
-    s->product = NULL;
-  }
+  GNUNET_CADET_close_port (s->port);
   GNUNET_free (s);
 }
 
@@ -438,15 +466,17 @@ prepare_client_end_notification (struct BobServiceSession *session)
   struct ClientResponseMessage *msg;
   struct GNUNET_MQ_Envelope *e;
 
+  if (NULL == session->client_mq)
+    return; /* no client left to be notified */
   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->range = 0;
   msg->product_length = htonl (0);
-  msg->status = htonl (session->active);
+  msg->status = htonl (session->status);
   GNUNET_MQ_send (session->client_mq,
                   e);
 }
@@ -475,15 +505,20 @@ cb_channel_destruction (void *cls,
               "Peer disconnected, terminating session %s with peer %s\n",
               GNUNET_h2s (&in->session_id),
               GNUNET_i2s (&in->peer));
-  in->channel = NULL;
   if (NULL != (s = in->s))
   {
-    if (GNUNET_YES == s->active)
+    if (GNUNET_SCALARPRODUCT_STATUS_ACTIVE == s->status)
     {
-      s->active = GNUNET_SYSERR;
+      s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
       prepare_client_end_notification (s);
     }
   }
+  if (NULL != in->cadet_mq)
+  {
+    GNUNET_MQ_destroy (in->cadet_mq);
+    in->cadet_mq = NULL;
+  }
+  in->channel = NULL;
   destroy_cadet_session (in);
 }
 
@@ -497,7 +532,7 @@ bob_cadet_done_cb (void *cls)
 {
   struct BobServiceSession *session = cls;
 
-  session->active = GNUNET_NO; /* that means, done */
+  session->status = GNUNET_SCALARPRODUCT_STATUS_SUCCESS;
   prepare_client_end_notification (session);
 }
 
@@ -505,7 +540,7 @@ bob_cadet_done_cb (void *cls)
 /**
  * Maximum count of elements we can put into a multipart message
  */
-#define ELEMENT_CAPACITY ((GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - sizeof (struct MultipartMessage)) / sizeof (struct GNUNET_CRYPTO_PaillierCiphertext))
+#define ELEMENT_CAPACITY ((GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE - 1 - sizeof (struct BobCryptodataMultipartMessage)) / sizeof (struct GNUNET_CRYPTO_PaillierCiphertext))
 
 
 /**
@@ -518,41 +553,46 @@ static void
 transmit_bobs_cryptodata_message_multipart (struct BobServiceSession *s)
 {
   struct GNUNET_CRYPTO_PaillierCiphertext *payload;
-  struct MultipartMessage *msg;
+  struct BobCryptodataMultipartMessage *msg;
   struct GNUNET_MQ_Envelope *e;
   unsigned int i;
   unsigned int j;
   uint32_t todo_count;
 
-  while (s->transferred_element_count != s->used_element_count)
+  while (s->cadet_transmitted_element_count != s->used_element_count)
   {
-    todo_count = s->used_element_count - s->transferred_element_count;
+    todo_count = s->used_element_count - s->cadet_transmitted_element_count;
     if (todo_count > ELEMENT_CAPACITY / 2)
       todo_count = ELEMENT_CAPACITY / 2;
 
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Sending %u additional crypto values to Alice\n",
+                (unsigned int) todo_count);
     e = GNUNET_MQ_msg_extra (msg,
                              todo_count * sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) * 2,
                              GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA_MULTIPART);
     msg->contained_element_count = htonl (todo_count);
     payload = (struct GNUNET_CRYPTO_PaillierCiphertext *) &msg[1];
-    for (i = s->transferred_element_count, j = 0; i < s->transferred_element_count + todo_count; i++)
+    for (i = s->cadet_transmitted_element_count, j = 0; i < s->cadet_transmitted_element_count + todo_count; i++)
     {
       //r[i][p] and r[i][q]
-      memcpy (&payload[j++],
+      GNUNET_memcpy (&payload[j++],
               &s->r[i],
               sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
-      memcpy (&payload[j++],
+      GNUNET_memcpy (&payload[j++],
               &s->r_prime[i],
               sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
     }
-    s->transferred_element_count += todo_count;
-    if (s->transferred_element_count == s->used_element_count)
+    s->cadet_transmitted_element_count += todo_count;
+    if (s->cadet_transmitted_element_count == s->used_element_count)
       GNUNET_MQ_notify_sent (e,
                              &bob_cadet_done_cb,
                              s);
     GNUNET_MQ_send (s->cadet->cadet_mq,
                     e);
   }
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "All values queued for Alice, Bob is done\n");
 }
 
 
@@ -570,46 +610,49 @@ transmit_bobs_cryptodata_message_multipart (struct BobServiceSession *s)
 static void
 transmit_bobs_cryptodata_message (struct BobServiceSession *s)
 {
-  struct ServiceResponseMessage *msg;
+  struct BobCryptodataMessage *msg;
   struct GNUNET_MQ_Envelope *e;
   struct GNUNET_CRYPTO_PaillierCiphertext *payload;
   unsigned int i;
 
-  s->transferred_element_count = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - sizeof (struct ServiceResponseMessage)) /
-    (sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) * 2) - 2;
-  if (s->transferred_element_count > s->used_element_count)
-    s->transferred_element_count = s->used_element_count;
+  s->cadet_transmitted_element_count
+    = ((GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE - 1 - sizeof (struct BobCryptodataMessage))
+       / sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) / 2) - 1;
+  if (s->cadet_transmitted_element_count > s->used_element_count)
+    s->cadet_transmitted_element_count = s->used_element_count;
 
   e = GNUNET_MQ_msg_extra (msg,
-                           (2 + s->transferred_element_count * 2)
+                           (2 + s->cadet_transmitted_element_count * 2)
                            * sizeof (struct GNUNET_CRYPTO_PaillierCiphertext),
                            GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA);
-  msg->total_element_count = htonl (s->total);
-  msg->used_element_count = htonl (s->used_element_count);
-  msg->contained_element_count = htonl (s->transferred_element_count);
-  msg->key = s->session_id;
+  msg->contained_element_count = htonl (s->cadet_transmitted_element_count);
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Sending %u/%u crypto values to Alice\n",
+              (unsigned int) s->cadet_transmitted_element_count,
+              (unsigned int) s->used_element_count);
 
   payload = (struct GNUNET_CRYPTO_PaillierCiphertext *) &msg[1];
-  memcpy (&payload[0],
+  GNUNET_memcpy (&payload[0],
           &s->s,
           sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
-  memcpy (&payload[1],
+  GNUNET_memcpy (&payload[1],
           &s->s_prime,
           sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
 
   payload = &payload[2];
   // convert k[][]
-  for (i = 0; i < s->transferred_element_count; i++)
+  for (i = 0; i < s->cadet_transmitted_element_count; i++)
   {
     //k[i][p] and k[i][q]
-    memcpy (&payload[i * 2],
+    GNUNET_memcpy (&payload[i * 2],
             &s->r[i],
             sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
-    memcpy (&payload[i * 2 + 1],
+    GNUNET_memcpy (&payload[i * 2 + 1],
             &s->r_prime[i],
             sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
   }
-  if (s->transferred_element_count == s->used_element_count)
+  if (s->cadet_transmitted_element_count == s->used_element_count)
     GNUNET_MQ_notify_sent (e,
                            &bob_cadet_done_cb,
                            s);
@@ -617,6 +660,7 @@ transmit_bobs_cryptodata_message (struct BobServiceSession *s)
                   e);
   transmit_bobs_cryptodata_message_multipart (s);
 }
+#undef ELEMENT_CAPACITY
 
 
 /**
@@ -655,8 +699,9 @@ compute_square_sum (const gcry_mpi_t *vector,
  *     S': $S' := E_A(sum r_i^2)$
  *
  * @param request the requesting session + bob's requesting peer
+ * @return #GNUNET_OK on success
  */
-static void
+static int
 compute_service_response (struct BobServiceSession *session)
 {
   uint32_t i;
@@ -709,16 +754,22 @@ compute_service_response (struct BobServiceSession *session)
     // E(S - r_pi - b_pi)
     gcry_mpi_sub (tmp, my_offset, rand[p[i]]);
     gcry_mpi_sub (tmp, tmp, b[p[i]].value);
-    GNUNET_CRYPTO_paillier_encrypt (&session->cadet->remote_pubkey,
-                                    tmp,
-                                    2,
-                                    &r[i]);
+    GNUNET_assert (2 ==
+                   GNUNET_CRYPTO_paillier_encrypt (&session->cadet->remote_pubkey,
+                                                   tmp,
+                                                   2,
+                                                   &r[i]));
 
     // E(S - r_pi - b_pi) * E(S + a_pi) ==  E(2*S + a - r - b)
-    GNUNET_CRYPTO_paillier_hom_add (&session->cadet->remote_pubkey,
-                                    &r[i],
-                                    &a[p[i]],
-                                    &r[i]);
+    if (GNUNET_OK !=
+        GNUNET_CRYPTO_paillier_hom_add (&session->cadet->remote_pubkey,
+                                        &r[i],
+                                        &a[p[i]],
+                                        &r[i]))
+    {
+      GNUNET_break_op (0);
+      goto error_cleanup;
+    }
   }
 
   // Calculate Kq = E(S + a_qi) (+) E(S - r_qi)
@@ -726,48 +777,67 @@ compute_service_response (struct BobServiceSession *session)
   {
     // E(S - r_qi)
     gcry_mpi_sub (tmp, my_offset, rand[q[i]]);
-    GNUNET_assert (2 == GNUNET_CRYPTO_paillier_encrypt (&session->cadet->remote_pubkey,
-                                                        tmp,
-                                                        2,
-                                                        &r_prime[i]));
+    GNUNET_assert (2 ==
+                   GNUNET_CRYPTO_paillier_encrypt (&session->cadet->remote_pubkey,
+                                                   tmp,
+                                                   2,
+                                                   &r_prime[i]));
 
     // E(S - r_qi) * E(S + a_qi) == E(2*S + a_qi - r_qi)
-    GNUNET_assert (1 == GNUNET_CRYPTO_paillier_hom_add (&session->cadet->remote_pubkey,
-                                                        &r_prime[i],
-                                                        &a[q[i]],
-                                                        &r_prime[i]));
+    if (GNUNET_OK !=
+        GNUNET_CRYPTO_paillier_hom_add (&session->cadet->remote_pubkey,
+                                        &r_prime[i],
+                                        &a[q[i]],
+                                        &r_prime[i]))
+    {
+      GNUNET_break_op (0);
+      goto error_cleanup;
+    }
   }
+  gcry_mpi_release (tmp);
 
   // Calculate S' =  E(SUM( r_i^2 ))
   tmp = compute_square_sum (rand, count);
-  GNUNET_CRYPTO_paillier_encrypt (&session->cadet->remote_pubkey,
-                                  tmp,
-                                  1,
-                                  &session->s_prime);
+  GNUNET_assert (1 ==
+                 GNUNET_CRYPTO_paillier_encrypt (&session->cadet->remote_pubkey,
+                                                 tmp,
+                                                 1,
+                                                 &session->s_prime));
+  gcry_mpi_release (tmp);
 
   // Calculate S = E(SUM( (r_i + b_i)^2 ))
   for (i = 0; i < count; i++)
     gcry_mpi_add (rand[i], rand[i], b[i].value);
   tmp = compute_square_sum (rand, count);
-  GNUNET_CRYPTO_paillier_encrypt (&session->cadet->remote_pubkey,
-                                  tmp,
-                                  1,
-                                  &session->s);
+  GNUNET_assert (1 ==
+                 GNUNET_CRYPTO_paillier_encrypt (&session->cadet->remote_pubkey,
+                                                 tmp,
+                                                 1,
+                                                 &session->s));
+  gcry_mpi_release (tmp);
 
   session->r = r;
   session->r_prime = r_prime;
 
-  // release rand, b and a
   for (i = 0; i < count; i++)
     gcry_mpi_release (rand[i]);
-  gcry_mpi_release (tmp);
   GNUNET_free (session->e_a);
   session->e_a = NULL;
   GNUNET_free (p);
   GNUNET_free (q);
   GNUNET_free (rand);
+  return GNUNET_OK;
 
-  // copy the r[], r_prime[], S and Stick into a new message, prepare_service_response frees these
+ error_cleanup:
+  GNUNET_free (r);
+  GNUNET_free (r_prime);
+  gcry_mpi_release (tmp);
+  GNUNET_free (p);
+  GNUNET_free (q);
+  for (i = 0; i < count; i++)
+    gcry_mpi_release (rand[i]);
+  GNUNET_free (rand);
+  return GNUNET_SYSERR;
 }
 
 
@@ -815,8 +885,8 @@ static int
 element_cmp (const void *a,
              const void *b)
 {
-  const struct MpiElement *ma = *(const struct MpiElement **) a;
-  const struct MpiElement *mb = *(const struct MpiElement **) b;
+  const struct MpiElement *ma = a;
+  const struct MpiElement *mb = b;
 
   return GNUNET_CRYPTO_hash_cmp (ma->key,
                                  mb->key);
@@ -833,7 +903,11 @@ element_cmp (const void *a,
 static void
 transmit_cryptographic_reply (struct BobServiceSession *s)
 {
+  struct GNUNET_CADET_Channel *channel;
+
   /* TODO: code duplication with Alice! */
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Received everything, building reply for Alice\n");
   s->sorted_elements
     = GNUNET_malloc (GNUNET_CONTAINER_multihashmap_size (s->intersected_elements) *
                      sizeof (struct MpiElement));
@@ -841,14 +915,18 @@ transmit_cryptographic_reply (struct BobServiceSession *s)
   GNUNET_CONTAINER_multihashmap_iterate (s->intersected_elements,
                                          &copy_element_cb,
                                          s);
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Finished intersection, %d items remain\n",
-       s->used_element_count);
-  qsort (s->intersected_elements,
+  qsort (s->sorted_elements,
          s->used_element_count,
          sizeof (struct MpiElement),
          &element_cmp);
-  compute_service_response (s);
+  if (GNUNET_OK !=
+      compute_service_response (s))
+  {
+    channel = s->cadet->channel;
+    s->cadet->channel = NULL;
+    GNUNET_CADET_channel_destroy (channel);
+    return;
+  }
   transmit_bobs_cryptodata_message (s);
 }
 
@@ -906,22 +984,25 @@ handle_alices_cryptodata_message (void *cls,
   if ( (msize != msg_length) ||
        (0 == contained_elements) ||
        (contained_elements > UINT16_MAX) ||
-       (max < contained_elements + s->transferred_element_count) )
+       (max < contained_elements + s->cadet_received_element_count) )
   {
     GNUNET_break_op (0);
     return GNUNET_SYSERR;
   }
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Received %u crypto values from Alice\n",
+              (unsigned int) contained_elements);
 
   payload = (const struct GNUNET_CRYPTO_PaillierCiphertext *) &msg[1];
   if (NULL == s->e_a)
     s->e_a = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) *
                             max);
-  memcpy (&s->e_a[s->transferred_element_count],
+  GNUNET_memcpy (&s->e_a[s->cadet_received_element_count],
           payload,
           sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) * contained_elements);
-  s->transferred_element_count += contained_elements;
+  s->cadet_received_element_count += contained_elements;
 
-  if ( (s->transferred_element_count == max) &&
+  if ( (s->cadet_received_element_count == max) &&
        (NULL == s->intersection_op) )
   {
     /* intersection has finished also on our side, and
@@ -929,6 +1010,7 @@ handle_alices_cryptodata_message (void *cls,
        CADET response(s) */
     transmit_cryptographic_reply (s);
   }
+  GNUNET_CADET_receive_done (s->cadet->channel);
   return GNUNET_OK;
 }
 
@@ -968,8 +1050,12 @@ cb_intersection_element_removed (void *cls,
     return;
   case GNUNET_SET_STATUS_DONE:
     s->intersection_op = NULL;
-    s->intersection_set = NULL;
-    if (s->transferred_element_count ==
+    GNUNET_break (NULL == s->intersection_set);
+    GNUNET_CADET_receive_done (s->cadet->channel);
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Finished intersection, %d items remain\n",
+         GNUNET_CONTAINER_multihashmap_size (s->intersected_elements));
+    if (s->client_received_element_count ==
         GNUNET_CONTAINER_multihashmap_size (s->intersected_elements))
     {
       /* CADET transmission from Alice is also already done,
@@ -986,8 +1072,12 @@ cb_intersection_element_removed (void *cls,
     LOG (GNUNET_ERROR_TYPE_DEBUG,
          "Set intersection failed!\n");
     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:
@@ -1007,8 +1097,9 @@ static void
 start_intersection (struct BobServiceSession *s)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Got session with key %s and a matching element set, processing.\n",
-              GNUNET_h2s (&s->session_id));
+              "Got session with key %s and %u elements, starting intersection.\n",
+              GNUNET_h2s (&s->session_id),
+              (unsigned int) s->total);
 
   s->intersection_op
     = GNUNET_SET_prepare (&s->cadet->peer,
@@ -1017,8 +1108,17 @@ start_intersection (struct BobServiceSession *s)
                           GNUNET_SET_RESULT_REMOVED,
                           &cb_intersection_element_removed,
                           s);
-  GNUNET_SET_commit (s->intersection_op,
-                     s->intersection_set);
+  if (GNUNET_OK !=
+      GNUNET_SET_commit (s->intersection_op,
+                         s->intersection_set))
+  {
+    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;
 }
 
 
@@ -1042,11 +1142,6 @@ handle_alices_computation_request (void *cls,
   struct BobServiceSession *s;
   const struct ServiceRequestMessage *msg;
 
-  if (ntohs (message->size) != sizeof (struct ServiceRequestMessage))
-  {
-    GNUNET_break_op (0);
-    return GNUNET_SYSERR;
-  }
   msg = (const struct ServiceRequestMessage *) message;
   if (GNUNET_YES == in->in_map)
   {
@@ -1076,7 +1171,7 @@ handle_alices_computation_request (void *cls,
   /* pair them up */
   in->s = s;
   s->cadet = in;
-  if (s->transferred_element_count == s->total)
+  if (s->client_received_element_count == s->total)
     start_intersection (s);
   return GNUNET_OK;
 }
@@ -1087,7 +1182,7 @@ handle_alices_computation_request (void *cls,
  * preliminary initialization, more happens after we get Alice's first
  * message.
  *
- * @param cls closure
+ * @param cls closure with the `struct BobServiceSession`
  * @param channel new handle to the channel
  * @param initiator peer that started the channel
  * @param port unused
@@ -1098,7 +1193,7 @@ static void *
 cb_channel_incoming (void *cls,
                      struct GNUNET_CADET_Channel *channel,
                      const struct GNUNET_PeerIdentity *initiator,
-                     uint32_t port,
+                     const struct GNUNET_HashCode *port,
                      enum GNUNET_CADET_ChannelOption options)
 {
   struct CadetIncomingSession *in;
@@ -1109,6 +1204,7 @@ cb_channel_incoming (void *cls,
   in = GNUNET_new (struct CadetIncomingSession);
   in->peer = *initiator;
   in->channel = channel;
+  in->cadet_mq = GNUNET_CADET_mq_create (in->channel);
   return in;
 }
 
@@ -1126,7 +1222,7 @@ GSS_handle_bob_client_message_multipart (void *cls,
                                          struct GNUNET_SERVER_Client *client,
                                          const struct GNUNET_MessageHeader *message)
 {
-  const struct ComputationMultipartMessage * msg;
+  const struct ComputationBobCryptodataMultipartMessage * msg;
   struct BobServiceSession *s;
   uint32_t contained_count;
   const struct GNUNET_SCALARPRODUCT_Element *elements;
@@ -1146,22 +1242,22 @@ GSS_handle_bob_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) ||
        (UINT16_MAX < 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,
@@ -1171,10 +1267,8 @@ GSS_handle_bob_client_message_multipart (void *cls,
   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,
+    GNUNET_memcpy (elem,
             &elements[i],
             sizeof (struct GNUNET_SCALARPRODUCT_Element));
     if (GNUNET_SYSERR ==
@@ -1194,10 +1288,10 @@ GSS_handle_bob_client_message_multipart (void *cls,
                             &set_elem,
                             NULL, NULL);
   }
-  s->transferred_element_count += contained_count;
+  s->client_received_element_count += contained_count;
   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;
@@ -1269,21 +1363,27 @@ GSS_handle_bob_client_message (void *cls,
                                 GNUNET_SYSERR);
     return;
   }
-  if (NULL != find_matching_client_session (&msg->session_key))
+
+  s = GNUNET_new (struct BobServiceSession);
+  s->status = GNUNET_SCALARPRODUCT_STATUS_ACTIVE;
+  s->client = client;
+  s->client_mq = GNUNET_MQ_queue_for_server_client (client);
+  s->total = total_count;
+  s->client_received_element_count = contained_count;
+  s->session_id = msg->session_key;
+  s->port = GNUNET_CADET_open_port (my_cadet,
+                                    &msg->session_key,
+                                    &cb_channel_incoming,
+                                    s);
+  if (NULL == s->port)
   {
     GNUNET_break (0);
     GNUNET_SERVER_receive_done (client,
                                 GNUNET_SYSERR);
+    GNUNET_free (s);
     return;
   }
 
-  s = GNUNET_new (struct BobServiceSession);
-  s->active = GNUNET_YES;
-  s->client = client;
-  s->client_mq = GNUNET_MQ_queue_for_server_client (client);
-  s->total = total_count;
-  s->transferred_element_count = contained_count;
-  s->session_id = msg->session_key;
   GNUNET_break (GNUNET_YES ==
                 GNUNET_CONTAINER_multihashmap_put (client_sessions,
                                                    &s->session_id,
@@ -1299,7 +1399,7 @@ GSS_handle_bob_client_message (void *cls,
     if (0 == GNUNET_ntohll (elements[i].value))
       continue;
     elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
-    memcpy (elem,
+    GNUNET_memcpy (elem,
             &elements[i],
             sizeof (struct GNUNET_SCALARPRODUCT_Element));
     if (GNUNET_SYSERR ==
@@ -1324,7 +1424,7 @@ GSS_handle_bob_client_message (void *cls,
                                          s);
   GNUNET_SERVER_receive_done (client,
                               GNUNET_YES);
-  if (s->total != s->transferred_element_count)
+  if (s->total != s->client_received_element_count)
   {
     /* multipart msg */
     return;
@@ -1347,16 +1447,13 @@ GSS_handle_bob_client_message (void *cls,
  * Task run during shutdown.
  *
  * @param cls unused
- * @param tc unused
  */
 static void
-shutdown_task (void *cls,
-               const struct GNUNET_SCHEDULER_TaskContext *tc)
+shutdown_task (void *cls)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Shutting down, initiating cleanup.\n");
-  do_shutdown = GNUNET_YES;
-  // FIXME: is there a need to shutdown active sessions?
+  // FIXME: we have to cut our connections to CADET first!
   if (NULL != my_cadet)
   {
     GNUNET_CADET_disconnect (my_cadet);
@@ -1387,8 +1484,7 @@ handle_client_disconnect (void *cls,
   if (NULL == client)
     return;
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Client disconnected from us.\n",
-              client);
+              "Client disconnected from us.\n");
   s = GNUNET_SERVER_client_get_user_context (client,
                                              struct BobServiceSession);
   if (NULL == s)
@@ -1415,7 +1511,7 @@ run (void *cls,
       GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_BOB,
       0},
     { &GSS_handle_bob_client_message_multipart, NULL,
-      GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MUTLIPART_BOB,
+      GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_BOB,
       0},
     { NULL, NULL, 0, 0}
   };
@@ -1428,10 +1524,6 @@ run (void *cls,
       0},
     { NULL, 0, 0}
   };
-  static const uint32_t ports[] = {
-    GNUNET_APPLICATION_TYPE_SCALARPRODUCT,
-    0
-  };
 
   cfg = c;
   /*
@@ -1454,10 +1546,8 @@ run (void *cls,
   cadet_sessions = GNUNET_CONTAINER_multihashmap_create (128,
                                                          GNUNET_YES);
   my_cadet = GNUNET_CADET_connect (cfg, NULL,
-                                   &cb_channel_incoming,
                                    &cb_channel_destruction,
-                                   cadet_handlers,
-                                   ports);
+                                   cadet_handlers);
   if (NULL == my_cadet)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@@ -1465,9 +1555,8 @@ run (void *cls,
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
-                                &shutdown_task,
-                                NULL);
+  GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
+                                NULL);
 }