converting scalarproduct alice-ecc to new service API
[oweals/gnunet.git] / src / scalarproduct / gnunet-service-scalarproduct_bob.c
index 7341317b78a6d2df2163beecdb0314b8f3a9a056..a19c909e13b8675b6b07ce26874819c35256b5de 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     Copyright (C) 2013, 2014 GNUnet e.V.
+     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
@@ -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)
    */
@@ -403,6 +408,7 @@ destroy_service_session (struct BobServiceSession *s)
     GNUNET_free (s->r_prime);
     s->r_prime = NULL;
   }
+  GNUNET_CADET_close_port (s->port);
   GNUNET_free (s);
 }
 
@@ -460,6 +466,8 @@ 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->status,
@@ -568,10 +576,10 @@ transmit_bobs_cryptodata_message_multipart (struct BobServiceSession *s)
     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));
     }
@@ -625,10 +633,10 @@ transmit_bobs_cryptodata_message (struct BobServiceSession *s)
               (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));
 
@@ -637,10 +645,10 @@ transmit_bobs_cryptodata_message (struct BobServiceSession *s)
   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));
   }
@@ -989,7 +997,7 @@ handle_alices_cryptodata_message (void *cls,
   if (NULL == s->e_a)
     s->e_a = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) *
                             max);
-  memcpy (&s->e_a[s->cadet_received_element_count],
+  GNUNET_memcpy (&s->e_a[s->cadet_received_element_count],
           payload,
           sizeof (struct GNUNET_CRYPTO_PaillierCiphertext) * contained_elements);
   s->cadet_received_element_count += contained_elements;
@@ -1174,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
@@ -1185,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;
@@ -1260,7 +1268,7 @@ GSS_handle_bob_client_message_multipart (void *cls,
   for (i = 0; i < contained_count; i++)
   {
     elem = GNUNET_new (struct GNUNET_SCALARPRODUCT_Element);
-    memcpy (elem,
+    GNUNET_memcpy (elem,
             &elements[i],
             sizeof (struct GNUNET_SCALARPRODUCT_Element));
     if (GNUNET_SYSERR ==
@@ -1355,13 +1363,6 @@ GSS_handle_bob_client_message (void *cls,
                                 GNUNET_SYSERR);
     return;
   }
-  if (NULL != find_matching_client_session (&msg->session_key))
-  {
-    GNUNET_break (0);
-    GNUNET_SERVER_receive_done (client,
-                                GNUNET_SYSERR);
-    return;
-  }
 
   s = GNUNET_new (struct BobServiceSession);
   s->status = GNUNET_SCALARPRODUCT_STATUS_ACTIVE;
@@ -1370,6 +1371,19 @@ GSS_handle_bob_client_message (void *cls,
   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;
+  }
+
   GNUNET_break (GNUNET_YES ==
                 GNUNET_CONTAINER_multihashmap_put (client_sessions,
                                                    &s->session_id,
@@ -1385,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 ==
@@ -1497,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}
   };
@@ -1510,10 +1524,6 @@ run (void *cls,
       0},
     { NULL, 0, 0}
   };
-  static const uint32_t ports[] = {
-    GNUNET_APPLICATION_TYPE_SCALARPRODUCT,
-    0
-  };
 
   cfg = c;
   /*
@@ -1536,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,