Merge branch 'master' of ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / scalarproduct / gnunet-service-scalarproduct_alice.c
index 99d1f73d7ab17d5073254b565c2f254893c7312e..6d7a0a3b84e7a18077655fc4311f63d5669211d5 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, 2017 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_alice.c
@@ -77,7 +77,7 @@ struct AliceServiceSession
   /**
    * The client this request is related to.
    */
-  struct GNUNET_SERVER_Client *client;
+  struct GNUNET_SERVICE_Client *client;
 
   /**
    * The message queue for the client.
@@ -164,7 +164,13 @@ 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;
+
+  /**
+   * Already transferred elements from Bob to us.
+   * Less or equal than @e total.
+   */
+  uint32_t cadet_received_element_count;
 
   /**
    * State of this session.   In
@@ -218,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;
 
@@ -242,22 +248,12 @@ destroy_service_session (struct AliceServiceSession *s)
   if (GNUNET_YES == s->in_destroy)
     return;
   s->in_destroy = GNUNET_YES;
-  if (NULL != s->client_mq)
-  {
-    GNUNET_MQ_destroy (s->client_mq);
-    s->client_mq = NULL;
-  }
-  if (NULL != s->cadet_mq)
-  {
-    GNUNET_MQ_destroy (s->cadet_mq);
-    s->cadet_mq = NULL;
-  }
   if (NULL != s->client)
   {
-    GNUNET_SERVER_client_set_user_context (s->client,
-                                           NULL);
-    GNUNET_SERVER_client_disconnect (s->client);
+    struct GNUNET_SERVICE_Client *c = s->client;
+
     s->client = NULL;
+    GNUNET_SERVICE_client_drop (c);
   }
   if (NULL != s->channel)
   {
@@ -267,7 +263,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;
@@ -325,6 +321,8 @@ prepare_client_end_notification (struct AliceServiceSession *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->status,
@@ -400,11 +398,12 @@ 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)
   {
-    memcpy (&msg[1],
+    GNUNET_memcpy (&msg[1],
             product_exported,
             product_length);
     GNUNET_free (product_exported);
@@ -424,34 +423,26 @@ transmit_client_response (struct AliceServiceSession *s)
  *
  * It must NOT call #GNUNET_CADET_channel_destroy() on the channel.
  *
- * @param cls closure (set from #GNUNET_CADET_connect())
+ * @param cls our `struct AliceServiceSession`
  * @param channel connection to the other end (henceforth invalid)
- * @param channel_ctx place where local state associated
- *                   with the channel is stored
  */
 static void
 cb_channel_destruction (void *cls,
-                        const struct GNUNET_CADET_Channel *channel,
-                        void *channel_ctx)
+                        const struct GNUNET_CADET_Channel *channel)
 {
-  struct AliceServiceSession *s = channel_ctx;
+  struct AliceServiceSession *s = cls;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "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_SCALARPRODUCT_STATUS_ACTIVE == s->status)
   {
     /* We didn't get an answer yet, fail with error */
     s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
     prepare_client_end_notification (s);
   }
+  s->channel = NULL;
 }
 
 
@@ -629,52 +620,53 @@ compute_scalar_product (struct AliceServiceSession *session)
 
 
 /**
- * Handle a multipart chunk of a response we got from another service
+ * Check a multipart chunk of a response we got from another service
  * we wanted to calculate a scalarproduct with.
  *
- * @param cls closure (set from #GNUNET_CADET_connect)
- * @param channel connection to the other end
- * @param channel_ctx place to store local state associated with the @a channel
- * @param message the actual message
+ * @param cls the `struct AliceServiceSession`
+ * @param msg the actual message
  * @return #GNUNET_OK to keep the connection open,
  *         #GNUNET_SYSERR to close it (signal serious error)
  */
 static int
-handle_bobs_cryptodata_multipart (void *cls,
-                                  struct GNUNET_CADET_Channel *channel,
-                                  void **channel_ctx,
-                                  const struct GNUNET_MessageHeader *message)
+check_bobs_cryptodata_multipart (void *cls,
+                                 const struct BobCryptodataMultipartMessage *msg)
 {
-  struct AliceServiceSession *s = *channel_ctx;
-  const struct BobCryptodataMultipartMessage *msg;
-  const struct GNUNET_CRYPTO_PaillierCiphertext *payload;
-  size_t i;
+  struct AliceServiceSession *s = cls;
   uint32_t contained;
   size_t msg_size;
   size_t required_size;
 
-  if (NULL == s)
-  {
-    GNUNET_break_op (0);
-    return GNUNET_SYSERR;
-  }
-  msg_size = ntohs (message->size);
-  if (sizeof (struct BobCryptodataMultipartMessage) > msg_size)
-  {
-    GNUNET_break_op (0);
-    return GNUNET_SYSERR;
-  }
-  msg = (const struct BobCryptodataMultipartMessage *) message;
+  msg_size = ntohs (msg->header.size);
   contained = ntohl (msg->contained_element_count);
   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;
   }
+  return GNUNET_OK;
+}
 
+/**
+ * Handle a multipart chunk of a response we got from another service
+ * we wanted to calculate a scalarproduct with.
+ *
+ * @param cls the `struct AliceServiceSession`
+ * @param msg the actual message
+ */
+static void
+handle_bobs_cryptodata_multipart (void *cls,
+                                  const struct BobCryptodataMultipartMessage *msg)
+{
+  struct AliceServiceSession *s = cls;
+  const struct GNUNET_CRYPTO_PaillierCiphertext *payload;
+  size_t i;
+  uint32_t contained;
+
+  contained = ntohl (msg->contained_element_count);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Received %u additional crypto values from Bob\n",
               (unsigned int) contained);
@@ -683,61 +675,42 @@ 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],
-            &payload[2 * i],
-            sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
-    memcpy (&s->r_prime[s->transferred_element_count + i],
-            &payload[2 * i],
-            sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
+    GNUNET_memcpy (&s->r[s->cadet_received_element_count + i],
+                   &payload[2 * i],
+                   sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
+    GNUNET_memcpy (&s->r_prime[s->cadet_received_element_count + i],
+                   &payload[2 * i],
+                   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)
-    return GNUNET_OK;
+  if (s->cadet_received_element_count != s->used_element_count)
+    return; /* more to come */
 
   s->product = compute_scalar_product (s);
   transmit_client_response (s);
-  return GNUNET_OK;
 }
 
 
 /**
- * Handle a response we got from another service we wanted to
+ * Check a response we got from another service we wanted to
  * calculate a scalarproduct with.
  *
- * @param cls closure (set from #GNUNET_CADET_connect)
- * @param channel connection to the other end
- * @param channel_ctx place to store local state associated with the channel
+ * @param cls our `struct AliceServiceSession`
  * @param message the actual message
  * @return #GNUNET_OK to keep the connection open,
  *         #GNUNET_SYSERR to close it (we are done)
  */
 static int
-handle_bobs_cryptodata_message (void *cls,
-                                struct GNUNET_CADET_Channel *channel,
-                                void **channel_ctx,
-                                const struct GNUNET_MessageHeader *message)
+check_bobs_cryptodata_message (void *cls,
+                               const struct BobCryptodataMessage *msg)
 {
-  struct AliceServiceSession *s = *channel_ctx;
-  const struct BobCryptodataMessage *msg;
-  const struct GNUNET_CRYPTO_PaillierCiphertext *payload;
-  uint32_t i;
+  struct AliceServiceSession *s = cls;
   uint32_t contained;
   uint16_t msg_size;
   size_t required_size;
 
-  if (NULL == s)
-  {
-    GNUNET_break_op (0);
-    return GNUNET_SYSERR;
-  }
-  msg_size = ntohs (message->size);
-  if (sizeof (struct BobCryptodataMessage) > msg_size)
-  {
-    GNUNET_break_op (0);
-    return GNUNET_SYSERR;
-  }
-  msg = (const struct BobCryptodataMessage *) message;
+  msg_size = ntohs (msg->header.size);
   contained = ntohl (msg->contained_element_count);
   required_size = sizeof (struct BobCryptodataMessage)
     + 2 * contained * sizeof (struct GNUNET_CRYPTO_PaillierCiphertext)
@@ -749,50 +722,74 @@ 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;
   }
+  return GNUNET_OK;
+}
+
+
+/**
+ * Handle a response we got from another service we wanted to
+ * calculate a scalarproduct with.
+ *
+ * @param cls our `struct AliceServiceSession`
+ * @param msg the actual message
+ */
+static void
+handle_bobs_cryptodata_message (void *cls,
+                                const struct BobCryptodataMessage *msg)
+{
+  struct AliceServiceSession *s = cls;
+  const struct GNUNET_CRYPTO_PaillierCiphertext *payload;
+  uint32_t i;
+  uint32_t contained;
 
+  contained = ntohl (msg->contained_element_count);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Received %u crypto values from Bob\n",
               (unsigned int) contained);
-
   payload = (const struct GNUNET_CRYPTO_PaillierCiphertext *) &msg[1];
-  memcpy (&s->s,
-          &payload[0],
-          sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
-  memcpy (&s->s_prime,
-          &payload[1],
-          sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
+  GNUNET_memcpy (&s->s,
+                 &payload[0],
+                 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
+  GNUNET_memcpy (&s->s_prime,
+                 &payload[1],
+                 sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
   payload = &payload[2];
 
-  s->r = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) * s->used_element_count);
-  s->r_prime = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) * s->used_element_count);
+  s->r = GNUNET_new_array (s->used_element_count,
+                           struct GNUNET_CRYPTO_PaillierCiphertext);
+  s->r_prime = GNUNET_new_array (s->used_element_count,
+                                 struct GNUNET_CRYPTO_PaillierCiphertext);
   for (i = 0; i < contained; i++)
   {
-    memcpy (&s->r[i],
-            &payload[2 * i],
-            sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
-    memcpy (&s->r_prime[i],
-            &payload[2 * i + 1],
-            sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
+    GNUNET_memcpy (&s->r[i],
+                   &payload[2 * i],
+                   sizeof (struct GNUNET_CRYPTO_PaillierCiphertext));
+    GNUNET_memcpy (&s->r_prime[i],
+                   &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;
+    return;
   }
-
   s->product = compute_scalar_product (s);
   transmit_client_response (s);
-  return GNUNET_OK;
 }
 
 
@@ -850,7 +847,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))
 
 
 /**
@@ -891,8 +888,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),
@@ -900,15 +898,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;
@@ -925,11 +924,13 @@ send_alices_cryptodata_message (struct AliceServiceSession *s)
  *
  * @param cls closure with the `struct AliceServiceSession`
  * @param element a result element, only valid if status is #GNUNET_SET_STATUS_OK
+ * @param current_size current set size
  * @param status what has happened with the set intersection?
  */
 static void
 cb_intersection_element_removed (void *cls,
                                  const struct GNUNET_SET_Element *element,
+                                 uint64_t current_size,
                                  enum GNUNET_SET_Status status)
 {
   struct AliceServiceSession *s = cls;
@@ -954,7 +955,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:
@@ -971,7 +976,11 @@ cb_intersection_element_removed (void *cls,
       s->intersection_listen = NULL;
     }
     s->intersection_op = NULL;
-    s->intersection_set = NULL;
+    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;
@@ -1015,6 +1024,7 @@ cb_intersection_request_alice (void *cls,
   s->intersection_op
     = GNUNET_SET_accept (request,
                          GNUNET_SET_RESULT_REMOVED,
+                         (struct GNUNET_SET_Option[]) {{ 0 }},
                          &cb_intersection_element_removed,
                          s);
   if (NULL == s->intersection_op)
@@ -1028,11 +1038,14 @@ cb_intersection_request_alice (void *cls,
       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;
+  GNUNET_SET_listen_cancel (s->intersection_listen);
   s->intersection_listen = NULL;
 }
 
@@ -1045,6 +1058,17 @@ cb_intersection_request_alice (void *cls,
 static void
 client_request_complete_alice (struct AliceServiceSession *s)
 {
+  struct GNUNET_MQ_MessageHandler cadet_handlers[] = {
+    GNUNET_MQ_hd_var_size (bobs_cryptodata_message,
+                           GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA,
+                           struct BobCryptodataMessage,
+                           s),
+    GNUNET_MQ_hd_var_size (bobs_cryptodata_multipart,
+                           GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA_MULTIPART,
+                           struct BobCryptodataMultipartMessage,
+                           s),
+    GNUNET_MQ_handler_end ()
+  };
   struct ServiceRequestMessage *msg;
   struct GNUNET_MQ_Envelope *e;
 
@@ -1052,18 +1076,21 @@ client_request_complete_alice (struct AliceServiceSession *s)
               "Creating new channel for session with key %s.\n",
               GNUNET_h2s (&s->session_id));
   s->channel
-    = GNUNET_CADET_channel_create (my_cadet,
+    = GNUNET_CADET_channel_creatE (my_cadet,
                                    s,
                                    &s->peer,
-                                   GNUNET_APPLICATION_TYPE_SCALARPRODUCT,
-                                   GNUNET_CADET_OPTION_RELIABLE);
+                                   &s->session_id,
+                                   GNUNET_CADET_OPTION_RELIABLE,
+                                   NULL,
+                                   &cb_channel_destruction,
+                                   cadet_handlers);
   if (NULL == s->channel)
   {
     s->status = GNUNET_SCALARPRODUCT_STATUS_FAILURE;
     prepare_client_end_notification (s);
     return;
   }
-  s->cadet_mq = GNUNET_CADET_mq_create (s->channel);
+  s->cadet_mq = GNUNET_CADET_get_mq (s->channel);
   s->intersection_listen
     = GNUNET_SET_listen (cfg,
                          GNUNET_SET_OPERATION_INTERSECTION,
@@ -1089,69 +1116,62 @@ client_request_complete_alice (struct AliceServiceSession *s)
 
 
 /**
- * We're receiving additional set data. Add it to our
- * set and if we are done, initiate the transaction.
+ * We're receiving additional set data. Check if
+ * @a msg is well-formed.
  *
- * @param cls closure
- * @param client identification of the client
- * @param message the actual message
+ * @param cls client identification of the client
+ * @param msg the actual message
+ * @return #GNUNET_OK if @a msg is well-formed
  */
-static void
-GSS_handle_alice_client_message_multipart (void *cls,
-                                           struct GNUNET_SERVER_Client *client,
-                                           const struct GNUNET_MessageHeader *message)
+static int
+check_alice_client_message_multipart (void *cls,
+                                     const struct ComputationBobCryptodataMultipartMessage *msg)
 {
-  const struct ComputationBobCryptodataMultipartMessage * msg;
-  struct AliceServiceSession *s;
+  struct AliceServiceSession *s = cls;
   uint32_t contained_count;
-  const struct GNUNET_SCALARPRODUCT_Element *elements;
-  uint32_t i;
   uint16_t msize;
-  struct GNUNET_SET_Element set_elem;
-  struct GNUNET_SCALARPRODUCT_Element *elem;
 
-  s = GNUNET_SERVER_client_get_user_context (client,
-                                             struct AliceServiceSession);
-  if (NULL == s)
-  {
-    /* session needs to already exist */
-    GNUNET_break (0);
-    GNUNET_SERVER_receive_done (client,
-                                GNUNET_SYSERR);
-    return;
-  }
-  msize = ntohs (message->size);
-  if (msize < sizeof (struct ComputationBobCryptodataMultipartMessage))
-  {
-    GNUNET_break (0);
-    GNUNET_SERVER_receive_done (client,
-                                GNUNET_SYSERR);
-    return;
-  }
-  msg = (const struct ComputationBobCryptodataMultipartMessage *) message;
+  msize = ntohs (msg->header.size);
   contained_count = ntohl (msg->element_count_contained);
-
   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;
+    return GNUNET_SYSERR;
   }
-  s->transferred_element_count += contained_count;
+  return GNUNET_OK;
+}
+
+
+/**
+ * We're receiving additional set data. Add it to our
+ * set and if we are done, initiate the transaction.
+ *
+ * @param cls client identification of the client
+ * @param msg the actual message
+ */
+static void
+handle_alice_client_message_multipart (void *cls,
+                                      const struct ComputationBobCryptodataMultipartMessage *msg)
+{
+  struct AliceServiceSession *s = cls;
+  uint32_t contained_count;
+  const struct GNUNET_SCALARPRODUCT_Element *elements;
+  struct GNUNET_SET_Element set_elem;
+  struct GNUNET_SCALARPRODUCT_Element *elem;
+
+  contained_count = ntohl (msg->element_count_contained);
+  s->client_received_element_count += contained_count;
   elements = (const struct GNUNET_SCALARPRODUCT_Element *) &msg[1];
-  for (i = 0; i < contained_count; i++)
+  for (uint32_t 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],
-            sizeof (struct GNUNET_SCALARPRODUCT_Element));
+    GNUNET_memcpy (elem,
+                  &elements[i],
+                  sizeof (struct GNUNET_SCALARPRODUCT_Element));
     if (GNUNET_SYSERR ==
         GNUNET_CONTAINER_multihashmap_put (s->intersected_elements,
                                            &elem->key,
@@ -1170,9 +1190,8 @@ GSS_handle_alice_client_message_multipart (void *cls,
                             NULL, NULL);
     s->used_element_count++;
   }
-  GNUNET_SERVER_receive_done (client,
-                              GNUNET_OK);
-  if (s->total != s->transferred_element_count)
+  GNUNET_SERVICE_client_continue (s->client);
+  if (s->total != s->client_received_element_count)
   {
     /* more to come */
     return;
@@ -1183,45 +1202,29 @@ GSS_handle_alice_client_message_multipart (void *cls,
 
 /**
  * Handler for Alice's client request message.
- * We are doing request-initiation to compute a scalar product with a peer.
+ * Check that @a msg is well-formed.
  *
- * @param cls closure
- * @param client identification of the client
- * @param message the actual message
+ * @param cls identification of the client
+ * @param msg the actual message
+ * @return #GNUNET_OK if @a msg is well-formed
  */
-static void
-GSS_handle_alice_client_message (void *cls,
-                                 struct GNUNET_SERVER_Client *client,
-                                 const struct GNUNET_MessageHeader *message)
+static int
+check_alice_client_message (void *cls,
+                           const struct AliceComputationMessage *msg)
 {
-  const struct AliceComputationMessage *msg;
-  struct AliceServiceSession *s;
-  uint32_t contained_count;
-  uint32_t total_count;
-  const struct GNUNET_SCALARPRODUCT_Element *elements;
-  uint32_t i;
+  struct AliceServiceSession *s = cls;
   uint16_t msize;
-  struct GNUNET_SET_Element set_elem;
-  struct GNUNET_SCALARPRODUCT_Element *elem;
+  uint32_t total_count;
+  uint32_t contained_count;
 
-  s = GNUNET_SERVER_client_get_user_context (client,
-                                             struct AliceServiceSession);
-  if (NULL != s)
+  if (NULL != s->intersected_elements)
   {
     /* only one concurrent session per client connection allowed,
        simplifies logic a lot... */
     GNUNET_break (0);
-    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
-    return;
-  }
-  msize = ntohs (message->size);
-  if (msize < sizeof (struct AliceComputationMessage))
-  {
-    GNUNET_break (0);
-    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
-    return;
+    return GNUNET_SYSERR;
   }
-  msg = (const struct AliceComputationMessage *) message;
+  msize = ntohs (msg->header.size);
   total_count = ntohl (msg->element_count_total);
   contained_count = ntohl (msg->element_count_contained);
   if ( (0 == total_count) ||
@@ -1230,29 +1233,49 @@ GSS_handle_alice_client_message (void *cls,
                   contained_count * sizeof (struct GNUNET_SCALARPRODUCT_Element))) )
   {
     GNUNET_break_op (0);
-    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
-    return;
+    return GNUNET_SYSERR;
   }
+  return GNUNET_OK;
+}
 
-  s = GNUNET_new (struct AliceServiceSession);
+
+/**
+ * Handler for Alice's client request message.
+ * We are doing request-initiation to compute a scalar product with a peer.
+ *
+ * @param cls identification of the client
+ * @param msg the actual message
+ */
+static void
+handle_alice_client_message (void *cls,
+                            const struct AliceComputationMessage *msg)
+{
+  struct AliceServiceSession *s = cls;
+  uint32_t contained_count;
+  uint32_t total_count;
+  const struct GNUNET_SCALARPRODUCT_Element *elements;
+  struct GNUNET_SET_Element set_elem;
+  struct GNUNET_SCALARPRODUCT_Element *elem;
+
+  total_count = ntohl (msg->element_count_total);
+  contained_count = ntohl (msg->element_count_contained);
   s->peer = msg->peer;
   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,
                                                                   GNUNET_YES);
   s->intersection_set = GNUNET_SET_create (cfg,
                                            GNUNET_SET_OPERATION_INTERSECTION);
-  for (i = 0; i < contained_count; i++)
+
+  for (uint32_t 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 ==
@@ -1274,11 +1297,8 @@ GSS_handle_alice_client_message (void *cls,
                             NULL, NULL);
     s->used_element_count++;
   }
-  GNUNET_SERVER_client_set_user_context (client,
-                                         s);
-  GNUNET_SERVER_receive_done (client,
-                              GNUNET_OK);
-  if (s->total != s->transferred_element_count)
+  GNUNET_SERVICE_client_continue (s->client);
+  if (s->total != s->client_received_element_count)
   {
     /* wait for multipart msg */
     return;
@@ -1291,14 +1311,13 @@ GSS_handle_alice_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");
+  // FIXME: we have to cut our connections to CADET first!
   if (NULL != my_cadet)
   {
     GNUNET_CADET_disconnect (my_cadet);
@@ -1307,6 +1326,30 @@ shutdown_task (void *cls,
 }
 
 
+/**
+ * A client connected.
+ *
+ * Setup the associated data structure.
+ *
+ * @param cls closure, NULL
+ * @param client identification of the client
+ * @param mq message queue to communicate with @a client
+ * @return our `struct AliceServiceSession`
+ */
+static void *
+client_connect_cb (void *cls,
+                  struct GNUNET_SERVICE_Client *client,
+                  struct GNUNET_MQ_Handle *mq)
+{
+  struct AliceServiceSession *s;
+
+  s = GNUNET_new (struct AliceServiceSession);
+  s->client = client;
+  s->client_mq = mq;
+  return s;
+}
+
+
 /**
  * A client disconnected.
  *
@@ -1315,25 +1358,20 @@ shutdown_task (void *cls,
  *
  * @param cls closure, NULL
  * @param client identification of the client
+ * @param app_cls our `struct AliceServiceSession`
  */
 static void
-handle_client_disconnect (void *cls,
-                          struct GNUNET_SERVER_Client *client)
+client_disconnect_cb (void *cls,
+                     struct GNUNET_SERVICE_Client *client,
+                     void *app_cls)
 {
-  struct AliceServiceSession *s;
+  struct AliceServiceSession *s = app_cls;
 
-  if (NULL == client)
-    return;
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Client %p disconnected from us.\n",
               client);
-  s = GNUNET_SERVER_client_get_user_context (client,
-                                             struct AliceServiceSession);
-  if (NULL == s)
-    return;
   s->client = NULL;
-  GNUNET_SERVER_client_set_user_context (client,
-                                         NULL);
+  s->client_mq = NULL;
   destroy_service_session (s);
 }
 
@@ -1342,33 +1380,14 @@ handle_client_disconnect (void *cls,
  * Initialization of the program and message handlers
  *
  * @param cls closure
- * @param server the initialized server
  * @param c configuration to use
+ * @param service the initialized service
  */
 static void
 run (void *cls,
-     struct GNUNET_SERVER_Handle *server,
-     const struct GNUNET_CONFIGURATION_Handle *c)
+     const struct GNUNET_CONFIGURATION_Handle *c,
+     struct GNUNET_SERVICE_Handle *service)
 {
-  static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
-    { &handle_bobs_cryptodata_message,
-      GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA,
-      0},
-    { &handle_bobs_cryptodata_multipart,
-      GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA_MULTIPART,
-      0},
-    { NULL, 0, 0}
-  };
-  static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
-    { &GSS_handle_alice_client_message, NULL,
-      GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE,
-      0},
-    { &GSS_handle_alice_client_message_multipart, NULL,
-      GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MUTLIPART_ALICE,
-      0},
-    { NULL, NULL, 0, 0}
-  };
-
   cfg = c;
   /*
     offset has to be sufficiently small to allow computation of:
@@ -1377,19 +1396,11 @@ run (void *cls,
   my_offset = gcry_mpi_new (GNUNET_CRYPTO_PAILLIER_BITS / 3);
   gcry_mpi_set_bit (my_offset,
                     GNUNET_CRYPTO_PAILLIER_BITS / 3);
-
   GNUNET_CRYPTO_paillier_create (&my_pubkey,
                                  &my_privkey);
-  GNUNET_SERVER_add_handlers (server,
-                              server_handlers);
-  GNUNET_SERVER_disconnect_notify (server,
-                                   &handle_client_disconnect,
-                                   NULL);
-  my_cadet = GNUNET_CADET_connect (cfg, NULL,
-                                   NULL /* no incoming supported */,
-                                   &cb_channel_destruction,
-                                   cadet_handlers,
-                                   NULL);
+  my_cadet = GNUNET_CADET_connecT (cfg);
+  GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
+                                NULL);
   if (NULL == my_cadet)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@@ -1397,29 +1408,28 @@ run (void *cls,
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
-                                &shutdown_task,
-                                NULL);
-
 }
 
 
 /**
- * The main function for the scalarproduct service.
- *
- * @param argc number of arguments from the command line
- * @param argv command line arguments
- * @return 0 ok, 1 on error
+ * Define "main" method using service macro.
  */
-int
-main (int argc,
-      char *const *argv)
-{
-  return (GNUNET_OK ==
-          GNUNET_SERVICE_run (argc, argv,
-                              "scalarproduct-alice",
-                              GNUNET_SERVICE_OPTION_NONE,
-                              &run, NULL)) ? 0 : 1;
-}
+GNUNET_SERVICE_MAIN
+("scalarproduct-alice",
+ GNUNET_SERVICE_OPTION_NONE,
+ &run,
+ &client_connect_cb,
+ &client_disconnect_cb,
+ NULL,
+ GNUNET_MQ_hd_var_size (alice_client_message,
+                       GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE,
+                       struct AliceComputationMessage,
+                       NULL),
+ GNUNET_MQ_hd_var_size (alice_client_message_multipart,
+                       GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MULTIPART_ALICE,
+                       struct ComputationBobCryptodataMultipartMessage,
+                       NULL),
+ GNUNET_MQ_handler_end ());
+
 
 /* end of gnunet-service-scalarproduct_alice.c */