log: add \n
[oweals/gnunet.git] / src / set / gnunet-service-set_union.c
index c8f3ba24b319ac024927969e781622bda9e06cbd..e22465fd3c0b8dac29ee8b3a3a11993d16e0671f 100644 (file)
@@ -1,6 +1,6 @@
 /*
       This file is part of GNUnet
-      Copyright (C) 2013-2015 Christian Grothoff (and other contributing authors)
+      Copyright (C) 2013-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
@@ -19,6 +19,7 @@
 */
 /**
  * @file set/gnunet-service-set_union.c
+
  * @brief two-peer set operations
  * @author Florian Dold
  */
@@ -60,7 +61,7 @@
  * Choose this value so that computing the IBF is still cheaper
  * than transmitting all values.
  */
-#define MAX_IBF_ORDER (16)
+#define MAX_IBF_ORDER (20)
 
 /**
  * Number of buckets used in the ibf per estimated
@@ -448,11 +449,12 @@ op_register_element (struct Operation *op,
 
 static void
 salt_key (const struct IBF_Key *k_in,
-          uint32_t salt, 
+          uint32_t salt,
           struct IBF_Key *k_out)
 {
   int s = salt % 64;
   uint64_t x = k_in->key_val;
+  /* rotate ibf key */
   x = (x >> s) | (x << (64 - s));
   k_out->key_val = x;
 }
@@ -460,12 +462,12 @@ salt_key (const struct IBF_Key *k_in,
 
 static void
 unsalt_key (const struct IBF_Key *k_in,
-            uint32_t salt, 
+            uint32_t salt,
             struct IBF_Key *k_out)
 {
-  int s = -(salt % 64);
+  int s = salt % 64;
   uint64_t x = k_in->key_val;
-  x = (x >> s) | (x << (64 - s));
+  x = (x << s) | (x >> (64 - s));
   k_out->key_val = x;
 }
 
@@ -613,9 +615,10 @@ send_ibf (struct Operation *op,
     ev = GNUNET_MQ_msg_extra (msg,
                               buckets_in_message * IBF_BUCKET_SIZE,
                               GNUNET_MESSAGE_TYPE_SET_UNION_P2P_IBF);
-    msg->reserved = 0;
+    msg->reserved1 = 0;
+    msg->reserved2 = 0;
     msg->order = ibf_order;
-    msg->offset = htons (buckets_sent);
+    msg->offset = htonl (buckets_sent);
     msg->salt = htonl (op->state->salt_send);
     ibf_write_slice (ibf, buckets_sent,
                      buckets_in_message, &msg[1]);
@@ -660,7 +663,7 @@ send_strata_estimator (struct Operation *op)
   ev = GNUNET_MQ_msg_header_extra (strata_msg,
                                    len,
                                    type);
-  memcpy (&strata_msg[1],
+  GNUNET_memcpy (&strata_msg[1],
           buf,
           len);
   GNUNET_free (buf);
@@ -749,6 +752,7 @@ handle_p2p_strata_estimator (void *cls,
   {
     /* decompression failed */
     fail_union_operation (op);
+    strata_estimator_destroy (remote_se);
     return GNUNET_SYSERR;
   }
   GNUNET_assert (NULL != op->state->se);
@@ -854,6 +858,7 @@ decode_and_send (struct Operation *op)
   if (GNUNET_OK !=
       prepare_ibf (op, op->state->remote_ibf->size))
   {
+    GNUNET_break (0);
     /* allocation failed */
     return GNUNET_SYSERR;
   }
@@ -868,7 +873,7 @@ decode_and_send (struct Operation *op)
        diff_ibf->size);
 
   num_decoded = 0;
-  last_key.key_val = 0;
+  key.key_val = 0; /* just to avoid compiler thinking we use undef'ed variable */
 
   while (1)
   {
@@ -885,7 +890,8 @@ decode_and_send (struct Operation *op)
            (unsigned long) key.key_val);
       num_decoded += 1;
       if ( (num_decoded > diff_ibf->size) ||
-           (num_decoded > 1 && last_key.key_val == key.key_val) )
+           ( (num_decoded > 1) &&
+             (last_key.key_val == key.key_val) ) )
       {
         LOG (GNUNET_ERROR_TYPE_DEBUG,
              "detected cyclic ibf (decoded %u/%u)\n",
@@ -968,7 +974,7 @@ decode_and_send (struct Operation *op)
                                 sizeof (struct IBF_Key),
                                 GNUNET_MESSAGE_TYPE_SET_UNION_P2P_INQUIRY);
       msg->salt = htonl (op->state->salt_receive);
-      memcpy (&msg[1],
+      GNUNET_memcpy (&msg[1],
               &key,
               sizeof (struct IBF_Key));
       LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -1022,6 +1028,7 @@ handle_p2p_ibf (void *cls,
          1 << msg->order);
     op->state->remote_ibf = ibf_create (1<<msg->order, SE_IBF_HASH_NUM);
     op->state->salt_receive = ntohl (msg->salt);
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving new IBF with salt %u\n", op->state->salt_receive);
     if (NULL == op->state->remote_ibf)
     {
       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@@ -1030,7 +1037,7 @@ handle_p2p_ibf (void *cls,
       return GNUNET_SYSERR;
     }
     op->state->ibf_buckets_received = 0;
-    if (0 != ntohs (msg->offset))
+    if (0 != ntohl (msg->offset))
     {
       GNUNET_break_op (0);
       fail_union_operation (op);
@@ -1039,9 +1046,19 @@ handle_p2p_ibf (void *cls,
   }
   else if (op->state->phase == PHASE_EXPECT_IBF_CONT)
   {
-    if ( (ntohs (msg->offset) != op->state->ibf_buckets_received) ||
-         (1<<msg->order != op->state->remote_ibf->size) ||
-         (ntohl (msg->salt) != op->state->salt_receive))
+    if (ntohl (msg->offset) != op->state->ibf_buckets_received)
+    {
+      GNUNET_break_op (0);
+      fail_union_operation (op);
+      return GNUNET_SYSERR;
+    }
+    if (1<<msg->order != op->state->remote_ibf->size)
+    {
+      GNUNET_break_op (0);
+      fail_union_operation (op);
+      return GNUNET_SYSERR;
+    }
+    if (ntohl (msg->salt) != op->state->salt_receive)
     {
       GNUNET_break_op (0);
       fail_union_operation (op);
@@ -1125,7 +1142,7 @@ send_client_element (struct Operation *op,
   rm->result_status = htons (status);
   rm->request_id = htonl (op->spec->client_request_id);
   rm->element_type = element->element_type;
-  memcpy (&rm[1], element->data, element->size);
+  GNUNET_memcpy (&rm[1], element->data, element->size);
   GNUNET_MQ_send (op->spec->set->client_mq, ev);
 }
 
@@ -1223,7 +1240,7 @@ handle_p2p_elements (void *cls,
 
   element_size = ntohs (mh->size) - sizeof (struct GNUNET_SET_ElementMessage);
   ee = GNUNET_malloc (sizeof (struct ElementEntry) + element_size);
-  memcpy (&ee[1], &emsg[1], element_size);
+  GNUNET_memcpy (&ee[1], &emsg[1], element_size);
   ee->element.size = element_size;
   ee->element.data = &ee[1];
   ee->element.element_type = ntohs (emsg->element_type);
@@ -1381,7 +1398,7 @@ handle_p2p_demand (void *cls,
       return;
     }
     ev = GNUNET_MQ_msg_extra (emsg, ee->element.size, GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS);
-    memcpy (&emsg[1], ee->element.data, ee->element.size);
+    GNUNET_memcpy (&emsg[1], ee->element.data, ee->element.size);
     emsg->reserved = htons (0);
     emsg->element_type = htons (ee->element.element_type);
     LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -1581,7 +1598,6 @@ union_evaluate (struct Operation *op,
     return;
   }
   msg->operation = htonl (GNUNET_SET_OPERATION_UNION);
-  msg->app_id = op->spec->app_id;
   GNUNET_MQ_send (op->mq,
                   ev);