- proper service-side error reporting for the SP API
[oweals/gnunet.git] / src / scalarproduct / gnunet-service-scalarproduct.c
index 75881afed804069e2d477ab08f47ee32b5b1f2cf..e3a2d5513e9150e80ec60120bc3170c6c6ffdf07 100644 (file)
@@ -28,7 +28,7 @@
 #include <gcrypt.h>
 #include "gnunet_util_lib.h"
 #include "gnunet_core_service.h"
-#include "gnunet_mesh_service.h"
+#include "gnunet_cadet_service.h"
 #include "gnunet_applications.h"
 #include "gnunet_protocols.h"
 #include "gnunet_scalarproduct_service.h"
@@ -51,12 +51,29 @@ enum PeerRole
   BOB
 };
 
-
+/**
+ * DLL for sorting elements
+ */
 struct SortedValue
 {
+  /**
+   * Sorted Values are kept in a DLL
+   */
   struct SortedValue * next;
+  
+  /**
+   * Sorted Values are kept in a DLL
+   */
   struct SortedValue * prev;
+  
+  /**
+   * The element's id+integer-value
+   */
   struct GNUNET_SCALARPRODUCT_Element * elem;
+  
+  /**
+   * the element's value converted to MPI
+   */
   gcry_mpi_t val;
 };
 
@@ -94,7 +111,7 @@ struct ServiceSession
    * Alice or Bob's peerID
    */
   struct GNUNET_PeerIdentity peer;
-
+  
   /**
    * the client this request is related to
    */
@@ -150,7 +167,7 @@ struct ServiceSession
   /**
    * Public key of the remote service, only used by bob
    */
-  struct GNUNET_CRYPTO_PaillierPublicKey remote_pubkey;
+  struct GNUNET_CRYPTO_PaillierPublicKey remote_pubkey;
 
   /**
    * DLL for sorting elements after intersection
@@ -205,7 +222,7 @@ struct ServiceSession
   /**
    * My transmit handle for the current message to a alice/bob
    */
-  struct GNUNET_MESH_TransmitHandle * service_transmit_handle;
+  struct GNUNET_CADET_TransmitHandle * service_transmit_handle;
 
   /**
    * My transmit handle for the current message to the client
@@ -213,9 +230,9 @@ struct ServiceSession
   struct GNUNET_SERVER_TransmitHandle * client_transmit_handle;
 
   /**
-   * channel-handle associated with our mesh handle
+   * channel-handle associated with our cadet handle
    */
-  struct GNUNET_MESH_Channel * channel;
+  struct GNUNET_CADET_Channel * channel;
 
   /**
    * Handle to a task that sends a msg to the our client
@@ -262,7 +279,7 @@ const struct GNUNET_CONFIGURATION_Handle * cfg;
 /**
  * Handle to the core service (NULL until we've connected to it).
  */
-static struct GNUNET_MESH_Handle *my_mesh;
+static struct GNUNET_CADET_Handle *my_cadet;
 
 /**
  * The identity of this host.
@@ -312,7 +329,7 @@ static struct ServiceSession * from_service_head;
 static struct ServiceSession * from_service_tail;
 
 /**
- * Certain events (callbacks for server & mesh operations) must not be queued after shutdown.
+ * Certain events (callbacks for server & cadet operations) must not be queued after shutdown.
  */
 static int do_shutdown;
 
@@ -461,6 +478,10 @@ free_session_variables (struct ServiceSession * session)
     GNUNET_free (session->e_a);
     session->e_a = NULL;
   }
+  if (session->remote_pubkey){
+    GNUNET_free(session->remote_pubkey);
+    session->remote_pubkey=NULL;
+  }
   if (session->sorted_elements) {
     GNUNET_free (session->sorted_elements);
     session->sorted_elements = NULL;
@@ -482,6 +503,10 @@ free_session_variables (struct ServiceSession * session)
     GNUNET_SET_destroy (session->intersection_set);
     session->intersection_set = NULL;
   }
+  if (session->channel){
+    GNUNET_CADET_channel_destroy(session->channel);
+    session->channel = NULL;
+  }
   if (session->msg) {
     GNUNET_free (session->msg);
     session->msg = NULL;
@@ -542,9 +567,9 @@ handle_client_disconnect (void *cls,
   if (!(session->role == BOB && 0/*//TODO: if session concluded*/)) {
     //we MUST terminate any client message underway
     if (session->service_transmit_handle && session->channel)
-      GNUNET_MESH_notify_transmit_ready_cancel (session->service_transmit_handle);
+      GNUNET_CADET_notify_transmit_ready_cancel (session->service_transmit_handle);
     if (session->channel && 0/* //TODO: waiting for service response */)
-      GNUNET_MESH_channel_destroy (session->channel);
+      GNUNET_CADET_channel_destroy (session->channel);
   }
   if (GNUNET_SCHEDULER_NO_TASK != session->client_notification_task) {
     GNUNET_SCHEDULER_cancel (session->client_notification_task);
@@ -660,8 +685,8 @@ prepare_alices_cyrptodata_message (void *cls)
   session->msg = (struct GNUNET_MessageHeader *) msg;
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Transmitting service request.\n"));
 
-  //transmit via mesh messaging
-  session->service_transmit_handle = GNUNET_MESH_notify_transmit_ready (session->channel, GNUNET_YES,
+  //transmit via cadet messaging
+  session->service_transmit_handle = GNUNET_CADET_notify_transmit_ready (session->channel, GNUNET_YES,
                                                                         GNUNET_TIME_UNIT_FOREVER_REL,
                                                                         msg_length,
                                                                         &do_send_message,
@@ -717,7 +742,7 @@ prepare_bobs_cryptodata_message_multipart (void *cls)
   session->transferred_element_count += todo_count;
   session->msg = (struct GNUNET_MessageHeader *) msg;
   session->service_transmit_handle =
-          GNUNET_MESH_notify_transmit_ready (session->channel,
+          GNUNET_CADET_notify_transmit_ready (session->channel,
                                              GNUNET_YES,
                                              GNUNET_TIME_UNIT_FOREVER_REL,
                                              msg_length,
@@ -725,11 +750,17 @@ prepare_bobs_cryptodata_message_multipart (void *cls)
                                              session);
   //disconnect our client
   if (NULL == session->service_transmit_handle) {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Could not send service-response message via mesh!)\n"));
-
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Could not send service-response message via cadet!)\n"));
+    
+    GNUNET_free (msg);
+    session->msg = NULL;
+    GNUNET_CONTAINER_DLL_remove (from_service_head, from_service_tail, session);
+    
     session->response->client_notification_task =
             GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
                                       session->response);
+    free_session_variables(session);
+    GNUNET_free(session);
     return;
   }
   if (session->transferred_element_count != session->used_elements_count) {
@@ -805,7 +836,7 @@ prepare_bobs_cryptodata_message (void *cls,
 
   session->msg = (struct GNUNET_MessageHeader *) msg;
   session->service_transmit_handle =
-          GNUNET_MESH_notify_transmit_ready (session->channel,
+          GNUNET_CADET_notify_transmit_ready (session->channel,
                                              GNUNET_YES,
                                              GNUNET_TIME_UNIT_FOREVER_REL,
                                              msg_length,
@@ -813,11 +844,18 @@ prepare_bobs_cryptodata_message (void *cls,
                                              session);
   //disconnect our client
   if (NULL == session->service_transmit_handle) {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Could not send service-response message via mesh!)\n"));
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Could not send service-response message via cadet!)\n"));
 
+    GNUNET_free (msg);
+    session->msg = NULL;
+    GNUNET_CONTAINER_DLL_remove (from_service_head, from_service_tail, session);
+    
     session->response->client_notification_task =
             GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
                                       session->response);
+    free_session_variables(session);
+    GNUNET_free(session);
+    return;
   }
   if (session->transferred_element_count != session->used_elements_count) {
     // multipart
@@ -895,13 +933,13 @@ compute_service_response (struct ServiceSession * session)
     // E(S - r_pi - b_pi)
     gcry_mpi_sub (tmp, my_offset, rand[p[i]]);
     gcry_mpi_sub (tmp, tmp, b[p[i]]);
-    GNUNET_CRYPTO_paillier_encrypt (&session->remote_pubkey,
+    GNUNET_CRYPTO_paillier_encrypt (session->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->remote_pubkey,
+    GNUNET_CRYPTO_paillier_hom_add (session->remote_pubkey,
                                     &r[i],
                                     &a[p[i]],
                                     &r[i]);
@@ -911,13 +949,13 @@ compute_service_response (struct ServiceSession * session)
   for (i = 0; i < count; i++) {
     // E(S - r_qi)
     gcry_mpi_sub (tmp, my_offset, rand[q[i]]);
-    GNUNET_assert (2 == GNUNET_CRYPTO_paillier_encrypt (&session->remote_pubkey,
+    GNUNET_assert (2 == GNUNET_CRYPTO_paillier_encrypt (session->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->remote_pubkey,
+    GNUNET_assert (1 == GNUNET_CRYPTO_paillier_hom_add (session->remote_pubkey,
                                                         &r_prime[i],
                                                         &a[q[i]],
                                                         &r_prime[i]));
@@ -925,7 +963,7 @@ compute_service_response (struct ServiceSession * session)
 
   // Calculate S' =  E(SUM( r_i^2 ))
   tmp = compute_square_sum (rand, count);
-  GNUNET_CRYPTO_paillier_encrypt (&session->remote_pubkey,
+  GNUNET_CRYPTO_paillier_encrypt (session->remote_pubkey,
                                   tmp,
                                   1,
                                   s_prime);
@@ -934,7 +972,7 @@ compute_service_response (struct ServiceSession * session)
   for (i = 0; i < count; i++)
     gcry_mpi_add (rand[i], rand[i], b[i]);
   tmp = compute_square_sum (rand, count);
-  GNUNET_CRYPTO_paillier_encrypt (&session->remote_pubkey,
+  GNUNET_CRYPTO_paillier_encrypt (session->remote_pubkey,
                                   tmp,
                                   1,
                                   s);
@@ -1010,7 +1048,6 @@ cb_insert_element_sorted (void *cls,
   while (NULL != o);
   // broken DLL
   GNUNET_assert (0);
-  return GNUNET_NO;
 }
 
 
@@ -1058,25 +1095,20 @@ cb_intersection_element_removed (void *cls,
     session->sorted_elements = GNUNET_malloc (session->used_elements_count * sizeof (gcry_mpi_t));
     for (i = 0; NULL != session->a_head; i++) {
       struct SortedValue* a = session->a_head;
-      if (i > session->used_elements_count) {
-        GNUNET_assert (0);
-        return;
-      }
+      GNUNET_assert (i < session->used_elements_count);
+      
       session->sorted_elements[i] = a->val;
       GNUNET_CONTAINER_DLL_remove (session->a_head, session->a_tail, a);
       GNUNET_free (a->elem);
     }
-    if (i != session->used_elements_count)
-      GNUNET_assert (0);
+    GNUNET_assert (i == session->used_elements_count);
 
     if (ALICE == session->role) {
       prepare_alices_cyrptodata_message (session);
       return;
     }
-    else {
-      if (session->used_elements_count == session->transferred_element_count)
-        compute_service_response (session);
-
+    else if (session->used_elements_count == session->transferred_element_count) {
+      compute_service_response (session);
       return;
     }
   default:
@@ -1084,19 +1116,21 @@ cb_intersection_element_removed (void *cls,
   }
 
   //failed if we go here
+  GNUNET_break (0);
+
+  // and notify our client-session that we could not complete the session
   if (ALICE == session->role) {
     session->client_notification_task =
             GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
                                       session);
   }
   else {
-    //TODO: Fail service session, exit tunnel
-
-
+    GNUNET_CONTAINER_DLL_remove (from_service_head, from_service_tail, session);
+    free_session_variables (session);
     session->response->client_notification_task =
             GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
                                       session->response);
-
+    GNUNET_free(session);
   }
 }
 
@@ -1265,8 +1299,8 @@ prepare_alices_computation_request (struct ServiceSession * session)
   session->msg = (struct GNUNET_MessageHeader *) msg;
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Transmitting service request.\n"));
 
-  //transmit via mesh messaging
-  session->service_transmit_handle = GNUNET_MESH_notify_transmit_ready (session->channel, GNUNET_YES,
+  //transmit via cadet messaging
+  session->service_transmit_handle = GNUNET_CADET_notify_transmit_ready (session->channel, GNUNET_YES,
                                                                         GNUNET_TIME_UNIT_FOREVER_REL,
                                                                         sizeof (struct GNUNET_SCALARPRODUCT_service_request),
                                                                         &do_send_message,
@@ -1330,8 +1364,8 @@ prepare_alices_cyrptodata_message_multipart (void *cls)
   session->msg = (struct GNUNET_MessageHeader *) msg;
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Transmitting service request.\n"));
 
-  //transmit via mesh messaging
-  session->service_transmit_handle = GNUNET_MESH_notify_transmit_ready (session->channel, GNUNET_YES,
+  //transmit via cadet messaging
+  session->service_transmit_handle = GNUNET_CADET_notify_transmit_ready (session->channel, GNUNET_YES,
                                                                         GNUNET_TIME_UNIT_FOREVER_REL,
                                                                         msg_length,
                                                                         &do_send_message,
@@ -1404,10 +1438,10 @@ client_request_complete_alice (struct ServiceSession * session)
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               _ ("Creating new channel for session with key %s.\n"),
               GNUNET_h2s (&session->session_id));
-  session->channel = GNUNET_MESH_channel_create (my_mesh, session,
+  session->channel = GNUNET_CADET_channel_create (my_cadet, session,
                                                  &session->peer,
                                                  GNUNET_APPLICATION_TYPE_SCALARPRODUCT,
-                                                 GNUNET_MESH_OPTION_RELIABLE);
+                                                 GNUNET_CADET_OPTION_RELIABLE);
   if (NULL == session->channel) {
     session->response->client_notification_task =
             GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
@@ -1628,9 +1662,9 @@ handle_client_message (void *cls,
  */
 static void *
 cb_channel_incoming (void *cls,
-                          struct GNUNET_MESH_Channel *channel,
+                          struct GNUNET_CADET_Channel *channel,
                           const struct GNUNET_PeerIdentity *initiator,
-                          uint32_t port, enum GNUNET_MESH_ChannelOption options)
+                          uint32_t port, enum GNUNET_CADET_ChannelOption options)
 {
   struct ServiceSession * c = GNUNET_new (struct ServiceSession);
 
@@ -1649,16 +1683,16 @@ cb_channel_incoming (void *cls,
  * Function called whenever a channel is destroyed.  Should clean up
  * any associated state.
  *
- * It must NOT call GNUNET_MESH_channel_destroy on the channel.
+ * It must NOT call GNUNET_CADET_channel_destroy on the channel.
  *
- * @param cls closure (set from GNUNET_MESH_connect)
+ * @param cls closure (set from GNUNET_CADET_connect)
  * @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_MESH_Channel *channel,
+                             const struct GNUNET_CADET_Channel *channel,
                              void *channel_ctx)
 {
   struct ServiceSession * session = channel_ctx;
@@ -1814,7 +1848,7 @@ compute_scalar_product (struct ServiceSession * session)
 /**
  * Handle a multipart-chunk of a request from another service to calculate a scalarproduct with us.
  *
- * @param cls closure (set from #GNUNET_MESH_connect)
+ * @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 message the actual message
@@ -1823,7 +1857,7 @@ compute_scalar_product (struct ServiceSession * session)
  */
 static int
 handle_alices_cyrptodata_message_multipart (void *cls,
-                                            struct GNUNET_MESH_Channel * channel,
+                                            struct GNUNET_CADET_Channel * channel,
                                             void **channel_ctx,
                                             const struct GNUNET_MessageHeader * message)
 {
@@ -1837,7 +1871,7 @@ handle_alices_cyrptodata_message_multipart (void *cls,
   session = (struct ServiceSession *) * channel_ctx;
   //we are not bob
   if ((NULL == session->e_a) || //or we did not expect this message yet 
-      (session->used_elements_count == session->transferred_element_count)) { //we not expecting multipart messages
+      (session->used_elements_count == session->transferred_element_count)) { //we are not expecting multipart messages
     goto except;
   }
   // shorter than minimum?
@@ -1869,15 +1903,24 @@ handle_alices_cyrptodata_message_multipart (void *cls,
 
   return GNUNET_OK;
 except:
+  session->channel = NULL;
   // and notify our client-session that we could not complete the session
-  GNUNET_CONTAINER_DLL_remove (from_service_head, from_service_tail, session);
-  if (session->response)
-    // we just found the responder session in this queue
+  free_session_variables (session);
+  if (NULL != session->client){
+    //Alice
+    session->client_notification_task =
+          GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
+                                    session);
+  }
+  else {
+    //Bob
+    if (NULL != session->response)
     session->response->client_notification_task =
           GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
                                     session->response);
-  free_session_variables (session);
-  GNUNET_free (session);
+    GNUNET_CONTAINER_DLL_remove (from_service_head, from_service_tail, session);
+    GNUNET_free(session);
+  }
   return GNUNET_SYSERR;
 }
 
@@ -1885,7 +1928,7 @@ except:
 /**
  * Handle a request from another service to calculate a scalarproduct with us.
  *
- * @param cls closure (set from #GNUNET_MESH_connect)
+ * @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 message the actual message
@@ -1894,7 +1937,7 @@ except:
  */
 static int
 handle_alices_cyrptodata_message (void *cls,
-                                  struct GNUNET_MESH_Channel * channel,
+                                  struct GNUNET_CADET_Channel * channel,
                                   void **channel_ctx,
                                   const struct GNUNET_MessageHeader * message)
 {
@@ -1948,15 +1991,24 @@ handle_alices_cyrptodata_message (void *cls,
   return GNUNET_OK;
 invalid_msg:
   GNUNET_break_op (0);
-  if ((NULL != session->next) || (NULL != session->prev) || (from_service_head == session))
-    GNUNET_CONTAINER_DLL_remove (from_service_head, from_service_tail, session);
+  session->channel = NULL;
   // and notify our client-session that we could not complete the session
-  if (session->response)
-    // we just found the responder session in this queue
+  free_session_variables (session);
+  if (NULL != session->client){
+    //Alice
+    session->client_notification_task =
+          GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
+                                    session);
+  }
+  else {
+    //Bob
+    if (NULL != session->response)
     session->response->client_notification_task =
           GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
                                     session->response);
-  free_session_variables (session);
+    GNUNET_CONTAINER_DLL_remove (from_service_head, from_service_tail, session);
+    GNUNET_free(session);
+  }
   return GNUNET_SYSERR;
 }
 
@@ -1964,7 +2016,7 @@ invalid_msg:
 /**
  * Handle a request from another service to calculate a scalarproduct with us.
  *
- * @param cls closure (set from #GNUNET_MESH_connect)
+ * @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 message the actual message
@@ -1973,7 +2025,7 @@ invalid_msg:
  */
 static int
 handle_alices_computation_request (void *cls,
-                        struct GNUNET_MESH_Channel * channel,
+                        struct GNUNET_CADET_Channel * channel,
                         void **channel_ctx,
                         const struct GNUNET_MessageHeader * message)
 {
@@ -2025,7 +2077,8 @@ handle_alices_computation_request (void *cls,
   memcpy (&session->session_id, &msg->session_id, sizeof (struct GNUNET_HashCode));
 
   // public key
-  memcpy (&session->remote_pubkey, &msg->public_key, sizeof (struct GNUNET_CRYPTO_PaillierPublicKey));
+  session->remote_pubkey = GNUNET_new (struct GNUNET_CRYPTO_PaillierPublicKey);
+  memcpy (session->remote_pubkey, &msg->public_key, sizeof (struct GNUNET_CRYPTO_PaillierPublicKey));
 
   //check if service queue contains a matching request
   client_session = find_matching_session (from_client_tail,
@@ -2062,15 +2115,24 @@ handle_alices_computation_request (void *cls,
   return GNUNET_OK;
 invalid_msg:
   GNUNET_break_op (0);
-  if ((NULL != session->next) || (NULL != session->prev) || (from_service_head == session))
-    GNUNET_CONTAINER_DLL_remove (from_service_head, from_service_tail, session);
+  session->channel = NULL;
   // and notify our client-session that we could not complete the session
-  if (session->response)
-    // we just found the responder session in this queue
+  free_session_variables (session);
+  if (NULL != session->client){
+    //Alice
+    session->client_notification_task =
+          GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
+                                    session);
+  }
+  else {
+    //Bob
+    if (NULL != session->response)
     session->response->client_notification_task =
           GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
                                     session->response);
-  free_session_variables (session);
+    GNUNET_CONTAINER_DLL_remove (from_service_head, from_service_tail, session);
+    GNUNET_free(session);
+  }
   return GNUNET_SYSERR;
 }
 
@@ -2078,7 +2140,7 @@ invalid_msg:
 /**
  * Handle a multipart chunk of a response we got from another service we wanted to calculate a scalarproduct with.
  *
- * @param cls closure (set from #GNUNET_MESH_connect)
+ * @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 message the actual message
@@ -2087,7 +2149,7 @@ invalid_msg:
  */
 static int
 handle_bobs_cryptodata_multipart (void *cls,
-                                   struct GNUNET_MESH_Channel * channel,
+                                   struct GNUNET_CADET_Channel * channel,
                                    void **channel_ctx,
                                    const struct GNUNET_MessageHeader * message)
 {
@@ -2131,14 +2193,24 @@ handle_bobs_cryptodata_multipart (void *cls,
   session->product = compute_scalar_product (session); //never NULL
 
 invalid_msg:
-  GNUNET_break_op (NULL != session->product); //NULL if we never tried to compute it...
-
+  GNUNET_break_op (NULL != session->product);
+  session->channel = NULL;
   // send message with product to client
-  if (ALICE == session->role) {
-    session->channel = NULL;
+  if (NULL != session->client){
+    //Alice
     session->client_notification_task =
-            GNUNET_SCHEDULER_add_now (&prepare_client_response,
-                                      session);
+          GNUNET_SCHEDULER_add_now (&prepare_client_response,
+                                    session);
+  }
+  else {
+    //Bob
+    if (NULL != session->response)
+    session->response->client_notification_task =
+          GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
+                                    session->response);
+    GNUNET_CONTAINER_DLL_remove (from_service_head, from_service_tail, session);
+    free_session_variables (session);
+    GNUNET_free(session);
   }
   // the channel has done its job, terminate our connection and the channel
   // the peer will be notified that the channel was destroyed via channel_destruction_handler
@@ -2150,7 +2222,7 @@ invalid_msg:
 /**
  * Handle a response we got from another service we wanted to calculate a scalarproduct with.
  *
- * @param cls closure (set from #GNUNET_MESH_connect)
+ * @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 message the actual message
@@ -2159,7 +2231,7 @@ invalid_msg:
  */
 static int
 handle_bobs_cryptodata_message (void *cls,
-                         struct GNUNET_MESH_Channel * channel,
+                         struct GNUNET_CADET_Channel * channel,
                          void **channel_ctx,
                          const struct GNUNET_MessageHeader * message)
 {
@@ -2215,12 +2287,23 @@ handle_bobs_cryptodata_message (void *cls,
 
 invalid_msg:
   GNUNET_break_op (NULL != session->product);
+  session->channel = NULL;
   // send message with product to client
-  if (ALICE == session->role) {
-    session->channel = NULL;
+  if (NULL != session->client){
+    //Alice
     session->client_notification_task =
-            GNUNET_SCHEDULER_add_now (&prepare_client_response,
-                                      session);
+          GNUNET_SCHEDULER_add_now (&prepare_client_response,
+                                    session);
+  }
+  else {
+    //Bob
+    if (NULL != session->response)
+    session->response->client_notification_task =
+          GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
+                                    session->response);
+    GNUNET_CONTAINER_DLL_remove (from_service_head, from_service_tail, session);
+    free_session_variables (session);
+    GNUNET_free(session);
   }
   // the channel has done its job, terminate our connection and the channel
   // the peer will be notified that the channel was destroyed via channel_destruction_handler
@@ -2247,7 +2330,7 @@ shutdown_task (void *cls,
   // terminate all owned open channels.
   for (session = from_client_head; NULL != session; session = session->next) {
     if ((0/*//TODO: not finalized*/) && (NULL != session->channel)) {
-      GNUNET_MESH_channel_destroy (session->channel);
+      GNUNET_CADET_channel_destroy (session->channel);
       session->channel = NULL;
     }
     if (GNUNET_SCHEDULER_NO_TASK != session->client_notification_task) {
@@ -2261,13 +2344,13 @@ shutdown_task (void *cls,
   }
   for (session = from_service_head; NULL != session; session = session->next)
     if (NULL != session->channel) {
-      GNUNET_MESH_channel_destroy (session->channel);
+      GNUNET_CADET_channel_destroy (session->channel);
       session->channel = NULL;
     }
 
-  if (my_mesh) {
-    GNUNET_MESH_disconnect (my_mesh);
-    my_mesh = NULL;
+  if (my_cadet) {
+    GNUNET_CADET_disconnect (my_cadet);
+    my_cadet = NULL;
   }
 }
 
@@ -2290,7 +2373,7 @@ run (void *cls,
     {&handle_client_message_multipart, NULL, GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_MUTLIPART, 0},
     {NULL, NULL, 0, 0}
   };
-  static const struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
+  static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
     { &handle_alices_computation_request, GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_CRYPTODATA, 0},
     { &handle_alices_cyrptodata_message, GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_CRYPTODATA, 0},
     { &handle_alices_cyrptodata_message_multipart, GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_CRYPTODATA_MULTIPART, 0},
@@ -2319,16 +2402,16 @@ run (void *cls,
   GNUNET_break (GNUNET_OK ==
                 GNUNET_CRYPTO_get_peer_identity (c,
                                                  &me));
-  my_mesh = GNUNET_MESH_connect (c, NULL,
+  my_cadet = GNUNET_CADET_connect (c, NULL,
                                  &cb_channel_incoming,
                                  &cb_channel_destruction,
-                                 mesh_handlers, ports);
-  if (!my_mesh) {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Connect to MESH failed\n"));
+                                 cadet_handlers, ports);
+  if (!my_cadet) {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Connect to CADET failed\n"));
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Mesh initialized\n"));
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("CADET initialized\n"));
   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
                                 &shutdown_task,
                                 NULL);