fix tests and do not assert since this will break make check
[oweals/gnunet.git] / src / namestore / gnunet-service-namestore.c
index 74bd98d811bafff2e69935283eabfca5a523bc13..f793c50d7fda8c8f521f4f21b84d4c6027cbbe85 100644 (file)
@@ -143,11 +143,6 @@ struct ZoneMonitor
    */
   struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
 
-  /**
-   * The operation id fot the zone iteration in the response for the client
-   */
-  uint32_t request_id;
-
   /**
    * Task active during initial iteration.
    */
@@ -198,6 +193,11 @@ struct CacheOperation
 };
 
 
+/**
+ * Public key of all zeros.
+ */
+static const struct GNUNET_CRYPTO_EcdsaPrivateKey zero;
+
 /**
  * Configuration handle.
  */
@@ -562,6 +562,141 @@ refresh_block (struct GNUNET_SERVER_Client *client,
 }
 
 
+struct RecordLookupContext
+{
+  const char *label;
+
+  int found;
+
+  unsigned int res_rd_count;
+
+  size_t rd_ser_len;
+
+  char *res_rd;
+};
+
+static void lookup_it (void *cls,
+                       const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
+                       const char *label,
+                       unsigned int rd_count,
+                       const struct GNUNET_GNSRECORD_Data *rd)
+{
+  struct RecordLookupContext *rlc = cls;
+
+  if (0 == strcmp (label, rlc->label))
+  {
+    rlc->found = GNUNET_YES;
+    if (0 != rd_count)
+    {
+      rlc->rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count, rd);
+      rlc->res_rd_count = rd_count;
+      rlc->res_rd = GNUNET_malloc (rlc->rd_ser_len);
+      GNUNET_GNSRECORD_records_serialize (rd_count, rd, rlc->rd_ser_len , rlc->res_rd);
+    }
+    else
+    {
+      rlc->rd_ser_len = 0;
+      rlc->res_rd_count = 0;
+      rlc->res_rd = NULL;
+    }
+  }
+}
+
+
+/**
+ * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP message
+ *
+ * @param cls unused
+ * @param client client sending the message
+ * @param message message of type 'struct RecordCreateMessage'
+ */
+static void
+handle_record_lookup (void *cls,
+                     struct GNUNET_SERVER_Client *client,
+                     const struct GNUNET_MessageHeader *message)
+{
+  const struct LabelLookupMessage *ll_msg;
+  struct LabelLookupResponseMessage *llr_msg;
+  struct RecordLookupContext rlc;
+  const char *name_tmp;
+  char *res_name;
+  uint32_t name_len;
+  size_t src_size;
+  size_t res_size;
+  int res;
+
+  if (ntohs (message->size) < sizeof (struct LabelLookupMessage))
+  {
+    GNUNET_break (0);
+    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+    return;
+  }
+
+  ll_msg = (const struct LabelLookupMessage *) message;
+  name_len = ntohl (ll_msg->label_len);
+  src_size = ntohs (message->size);
+
+  if (name_len !=  src_size - sizeof (struct LabelLookupMessage))
+  {
+    GNUNET_break (0);
+    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+    return;
+  }
+
+  name_tmp = (const char *) &ll_msg[1];
+  if ('\0' != name_tmp[name_len -1])
+  {
+    GNUNET_break (0);
+    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+    return;
+  }
+
+  GNUNET_SERVER_receive_done (client, GNUNET_OK);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Received `%s' message for name `%s'\n",
+              "NAMESTORE_RECORD_LOOKUP", name_tmp);
+
+  if (NULL == (client_lookup (client)))
+  {
+    GNUNET_break (0);
+    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+    return;
+  }
+
+  rlc.label = name_tmp;
+  rlc.found = GNUNET_NO;
+  rlc.res_rd_count = 0;
+  rlc.rd_ser_len = 0;
+  rlc.res_rd = NULL;
+
+  res = GSN_database->lookup_records (GSN_database->cls,
+        &ll_msg->zone, name_tmp, &lookup_it, &rlc);
+
+  res_size = sizeof (struct LabelLookupResponseMessage) + name_len + rlc.rd_ser_len;
+  llr_msg = GNUNET_malloc (res_size);
+  llr_msg->gns_header.header.size = htons (res_size);
+  llr_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE);
+  llr_msg->gns_header.r_id = ll_msg->gns_header.r_id;
+  llr_msg->private_key = ll_msg->zone;
+  llr_msg->name_len = htons (name_len);
+  llr_msg->rd_count = htons (rlc.res_rd_count);
+  llr_msg->rd_len = htons (rlc.rd_ser_len);
+  res_name = (char *) &llr_msg[1];
+  if  ((GNUNET_YES == rlc.found) && (GNUNET_OK == res))
+    llr_msg->found = ntohs (GNUNET_YES);
+  else
+    llr_msg->found = ntohs (GNUNET_NO);
+  memcpy (&llr_msg[1], name_tmp, name_len);
+  memcpy (&res_name[name_len], rlc.res_rd, rlc.rd_ser_len);
+
+  GNUNET_SERVER_notification_context_unicast (snc, client, &llr_msg->gns_header.header,
+      GNUNET_NO);
+
+  GNUNET_free_non_null (rlc.res_rd);
+  GNUNET_free (llr_msg);
+}
+
+
 /**
  * Handles a #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE message
  *
@@ -674,14 +809,36 @@ handle_record_store (void *cls,
       if (GNUNET_OK == res)
       {
         for (zm = monitor_head; NULL != zm; zm = zm->next)
-          if (0 == memcmp (&rp_msg->private_key, &zm->zone,
-                           sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
+        {
+          if ( (0 == memcmp (&rp_msg->private_key, &zm->zone,
+                             sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) ||
+               (0 == memcmp (&zm->zone,
+                             &zero,
+                             sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) )
+          {
+            GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                        "Notifying monitor about changes under label `%s'\n",
+                        conv_name);
             send_lookup_response (monitor_nc,
                                   zm->nc->client,
-                                  zm->request_id,
+                                  0,
                                   &rp_msg->private_key,
                                   conv_name,
                                   rd_count, rd);
+          }
+          else
+            GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                        "Monitor is for another zone\n");
+        }
+        if (NULL == monitor_head)
+          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                      "No monitors active\n");
+      }
+      else
+      {
+        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                    "Error storing record: %d\n",
+                    res);
       }
     }
     if (GNUNET_OK == res)
@@ -867,7 +1024,7 @@ enum ZoneIterationResult
 
 /**
  * Context for record remove operations passed from
- * #run_zone_iteration_round to #zone_iteraterate_proc as closure
+ * #run_zone_iteration_round to #zone_iterate_proc as closure
  */
 struct ZoneIterationProcResult
 {
@@ -898,7 +1055,7 @@ struct ZoneIterationProcResult
  * @param rd record data
  */
 static void
-zone_iteraterate_proc (void *cls,
+zone_iterate_proc (void *cls,
                        const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
                        const char *name,
                        unsigned int rd_count,
@@ -956,7 +1113,6 @@ zone_iteraterate_proc (void *cls,
 static void
 run_zone_iteration_round (struct ZoneIteration *zi)
 {
-  static struct GNUNET_CRYPTO_EcdsaPrivateKey zero;
   struct ZoneIterationProcResult proc;
   struct RecordResultMessage rrm;
   int ret;
@@ -972,7 +1128,7 @@ run_zone_iteration_round (struct ZoneIteration *zi)
                                              ? NULL
                                              : &zi->zone,
                                              zi->offset,
-                                             &zone_iteraterate_proc, &proc)))
+                                             &zone_iterate_proc, &proc)))
     {
       GNUNET_break (0);
       break;
@@ -1098,7 +1254,9 @@ handle_iteration_next (void *cls,
   const struct ZoneIterationNextMessage *zis_msg;
   uint32_t rid;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ZONE_ITERATION_NEXT");
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Received `%s' message\n",
+              "ZONE_ITERATION_NEXT");
   if (NULL == (nc = client_lookup(client)))
   {
     GNUNET_break (0);
@@ -1177,7 +1335,7 @@ monitor_iterate_cb (void *cls,
   }
   send_lookup_response (monitor_nc,
                        zm->nc->client,
-                       zm->request_id,
+                       0,
                        zone_key,
                        name,
                        rd_count,
@@ -1206,7 +1364,6 @@ handle_monitor_start (void *cls,
              "ZONE_MONITOR_START");
   zis_msg = (const struct ZoneMonitorStartMessage *) message;
   zm = GNUNET_new (struct ZoneMonitor);
-  zm->request_id = ntohl (zis_msg->gns_header.r_id);
   zm->offset = 0;
   zm->nc = client_lookup (client);
   zm->zone = zis_msg->zone;
@@ -1215,7 +1372,10 @@ handle_monitor_start (void *cls,
   GNUNET_SERVER_disable_receive_done_warning (client);
   GNUNET_SERVER_notification_context_add (monitor_nc,
                                          client);
-  zm->task = GNUNET_SCHEDULER_add_now (&monitor_next, zm);
+  if (GNUNET_YES == ntohl (zis_msg->iterate_first))
+    zm->task = GNUNET_SCHEDULER_add_now (&monitor_next, zm);
+  else
+    monitor_sync (zm);
 }
 
 
@@ -1234,7 +1394,9 @@ monitor_next (void *cls,
 
   zm->task = GNUNET_SCHEDULER_NO_TASK;
   ret = GSN_database->iterate_records (GSN_database->cls,
-                                      &zm->zone,
+                                       (0 == memcmp (&zm->zone, &zero, sizeof (zero)))
+                                       ? NULL
+                                       : &zm->zone,
                                       zm->offset++,
                                       &monitor_iterate_cb, zm);
   if (GNUNET_SYSERR == ret)
@@ -1265,6 +1427,8 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
     {&handle_record_store, NULL,
      GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE, 0},
+    {&handle_record_lookup, NULL,
+     GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP, 0},
     {&handle_zone_to_name, NULL,
      GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME, sizeof (struct ZoneToNameMessage) },
     {&handle_iteration_start, NULL,