- not required anymore
[oweals/gnunet.git] / src / namestore / gnunet-service-namestore.c
index 5584884d85dfb47745c879eaf73e4a05fbbcc766..67c7e6e57d0847f273b5a76531214072ee87ccc6 100644 (file)
@@ -43,11 +43,15 @@ struct GNUNET_NAMESTORE_ZoneIteration
 
   struct GNUNET_NAMESTORE_Client * client;
 
+  int has_zone;
+
   GNUNET_HashCode zone;
 
   uint64_t request_id;
   uint32_t offset;
 
+
+
 };
 
 
@@ -166,6 +170,7 @@ client_disconnect_notification (void *cls, struct GNUNET_SERVER_Client *client)
     GNUNET_free (no);
   }
 
+
   GNUNET_SERVER_client_drop(nc->client);
   GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc);
   GNUNET_free (nc);
@@ -344,7 +349,6 @@ static void handle_lookup_name (void *cls,
   char * name;
   uint32_t rid = 0;
   uint32_t type = 0;
-  int res;
 
   if (ntohs (message->size) < sizeof (struct LookupNameMessage))
   {
@@ -392,7 +396,7 @@ static void handle_lookup_name (void *cls,
   lnc.record_type = type;
   lnc.name = name;
   lnc.zone = &ln_msg->zone;
-  res = GSN_database->iterate_records(GSN_database->cls, &ln_msg->zone, name, 0, &handle_lookup_name_it, &lnc);
+  GSN_database->iterate_records(GSN_database->cls, &ln_msg->zone, name, 0, &handle_lookup_name_it, &lnc);
 
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
@@ -510,7 +514,7 @@ send:
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_PUT_RESPONSE");
   rpr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE);
   rpr_msg.gns_header.header.size = htons (sizeof (struct RecordPutResponseMessage));
-  rpr_msg.gns_header.r_id = rp_msg->gns_header.r_id;
+  rpr_msg.gns_header.r_id = htonl (rid);
   rpr_msg.op_result = htonl (res);
   GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rpr_msg, GNUNET_NO);
 
@@ -540,6 +544,7 @@ handle_create_record_it (void *cls,
   struct CreateRecordContext * crc = cls;
   struct GNUNET_CRYPTO_RsaSignature *signature_new = NULL;
   struct GNUNET_NAMESTORE_RecordData *rd_new = NULL;
+  struct GNUNET_TIME_Absolute block_expiration;
   int res;
   int exist = GNUNET_SYSERR;
   int update = GNUNET_NO;
@@ -606,15 +611,24 @@ handle_create_record_it (void *cls,
     }
   }
 
+  block_expiration = GNUNET_TIME_absolute_max(crc->expire, expire);
+  if (block_expiration.abs_value != expire.abs_value)
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updated block expiration time\n");
+
+
   /* Database operation */
   GNUNET_assert ((rd_new != NULL) && (rd_count_new > 0));
   res = GSN_database->put_records(GSN_database->cls,
                                 (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *) crc->pubkey,
-                                crc->expire,
+                                block_expiration,
                                 crc->name,
                                 rd_count_new, rd_new,
                                 signature_new);
   GNUNET_break (GNUNET_OK == res);
+  if (res == GNUNET_OK)
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully put record for `%s' in database \n", crc->name);
+  else
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to put record for `%s' in database \n", crc->name);
   res = GNUNET_YES;
 
 end:
@@ -722,6 +736,7 @@ static void handle_record_create (void *cls,
   }
 
   struct GNUNET_NAMESTORE_RecordData rd[rd_count];
+
   res = GNUNET_NAMESTORE_records_deserialize(rd_ser_len, rd_ser, rd_count, rd);
   if ((res != GNUNET_OK) || (rd_count != 1))
   {
@@ -735,6 +750,8 @@ static void handle_record_create (void *cls,
   GNUNET_CRYPTO_rsa_key_get_public(pkey, &pub);
   GNUNET_CRYPTO_hash (&pub, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &pubkey_hash);
 
+  crc.expire = GNUNET_TIME_absolute_ntoh(rp_msg->expire);
+  crc.res = GNUNET_SYSERR;
   crc.pkey = pkey;
   crc.pubkey = &pub;
   crc.rd = rd;
@@ -987,7 +1004,7 @@ send:
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_REMOVE_RESPONSE");
   rrr_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE);
   rrr_msg.gns_header.header.size = htons (sizeof (struct RecordRemoveResponseMessage));
-  rrr_msg.gns_header.r_id = rr_msg->gns_header.r_id;
+  rrr_msg.gns_header.r_id = htonl (rid);
   rrr_msg.op_result = htonl (res);
   GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rrr_msg, GNUNET_NO);
 
@@ -1098,8 +1115,6 @@ static void handle_zone_to_name (void *cls,
   struct ZoneToNameCtx ztn_ctx;
   size_t msg_size = 0;
   uint32_t rid = 0;
-  int res;
-
 
   if (ntohs (message->size) != sizeof (struct ZoneToNameMessage))
   {
@@ -1136,7 +1151,7 @@ static void handle_zone_to_name (void *cls,
       GNUNET_h2s (&ztn_msg->value_zone));
   GNUNET_free (z_tmp);
 
-  res = GSN_database->zone_to_name (GSN_database->cls, &ztn_msg->zone, &ztn_msg->value_zone, &handle_zone_to_name_it, &ztn_ctx);
+  GSN_database->zone_to_name (GSN_database->cls, &ztn_msg->zone, &ztn_msg->value_zone, &handle_zone_to_name_it, &ztn_ctx);
 
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
@@ -1166,40 +1181,78 @@ void zone_iteration_proc (void *cls,
                          const struct GNUNET_NAMESTORE_RecordData *rd,
                          const struct GNUNET_CRYPTO_RsaSignature *signature)
 {
-  struct ZoneIterationProcResult *zipr = cls;
-  size_t len;
-  if (zone_key != NULL)
-  {
-    zipr->zone_key = *zone_key;
-    zipr->have_zone_key = GNUNET_YES;
-  }
-  else
-    zipr->have_zone_key = GNUNET_NO;
-
-  zipr->expire = expire;
-
-  if (name != NULL)
-  {
-    memcpy (zipr->name, name, strlen(name) + 1);
-    zipr->have_name = GNUNET_YES;
-  }
-  else
-    zipr->have_name = GNUNET_NO;
+  struct GNUNET_NAMESTORE_ZoneIteration *zi = cls;
+  struct GNUNET_NAMESTORE_Client *nc = zi->client;
+  //size_t len;
 
-  if (signature != NULL)
+  if ((zone_key == NULL) && (name == NULL))
   {
-    zipr->signature = *signature;
-    zipr->have_signature = GNUNET_YES;
+    struct ZoneIterationResponseMessage zir_msg;
+    if (zi->has_zone == GNUNET_YES)
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No more results for zone `%s'\n", GNUNET_h2s(&zi->zone));
+    else
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No more results for all zones\n");
+
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending empty `%s' message\n", "ZONE_ITERATION_RESPONSE");
+    zir_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE);
+    zir_msg.gns_header.header.size = htons (sizeof (struct ZoneIterationResponseMessage));
+    zir_msg.gns_header.r_id = htonl(zi->request_id);
+    zir_msg.expire = GNUNET_TIME_absolute_hton(GNUNET_TIME_absolute_get_zero());
+    zir_msg.name_len = htons (0);
+    zir_msg.reserved = htons (0);
+    zir_msg.rd_count = htons (0);
+    zir_msg.rd_len = htons (0);
+    memset (&zir_msg.public_key, '\0', sizeof (zir_msg.public_key));
+    memset (&zir_msg.signature, '\0', sizeof (zir_msg.signature));
+    GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &zir_msg, GNUNET_NO);
+
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removing zone iterator\n");
+    GNUNET_CONTAINER_DLL_remove (nc->op_head, nc->op_tail, zi);
+    GNUNET_free (zi);
+    return;
   }
   else
-    zipr->have_signature = GNUNET_NO;
-
-  if ((rd_count > 0) && (rd != NULL))
   {
-    len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
-    zipr->rd_ser = GNUNET_malloc (len);
-    GNUNET_NAMESTORE_records_serialize(rd_count, rd, len, zipr->rd_ser);
-    zipr->rd_ser_len = len;
+    struct ZoneIterationResponseMessage *zir_msg;
+    if (zi->has_zone == GNUNET_YES)
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending name `%s' for iteration over zone `%s'\n",
+          name, GNUNET_h2s(&zi->zone));
+    if (zi->has_zone == GNUNET_NO)
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending name `%s' for iteration over all zones\n",
+          name);
+
+    size_t name_len;
+    size_t rd_ser_len;
+    size_t msg_size;
+    char *name_tmp;
+    char *rd_tmp;
+    name_len = strlen (name) +1;
+
+    rd_ser_len = GNUNET_NAMESTORE_records_get_size(rd_count, rd);
+    char rd_ser[rd_ser_len];
+    GNUNET_NAMESTORE_records_serialize(rd_count, rd, rd_ser_len, rd_ser);
+    msg_size = sizeof (struct ZoneIterationResponseMessage) + name_len + rd_ser_len;
+    zir_msg = GNUNET_malloc(msg_size);
+
+    name_tmp = (char *) &zir_msg[1];
+    rd_tmp = &name_tmp[name_len];
+
+    zir_msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE);
+    zir_msg->gns_header.header.size = htons (msg_size);
+    zir_msg->gns_header.r_id = htonl(zi->request_id);
+    zir_msg->expire = GNUNET_TIME_absolute_hton(expire);
+    zir_msg->reserved = htons (0);
+    zir_msg->name_len = htons (name_len);
+    zir_msg->rd_count = htons (rd_count);
+    zir_msg->rd_len = htons (rd_ser_len);
+    zir_msg->signature = *signature;
+    zir_msg->public_key = *zone_key;
+    memcpy (name_tmp, name, name_len);
+    memcpy (rd_tmp, rd_ser, rd_ser_len);
+
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending empty `%s' message with size %u\n", "ZONE_ITERATION_RESPONSE", msg_size);
+    GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) zir_msg, GNUNET_NO);
+    GNUNET_free (zir_msg);
   }
 }
 
@@ -1212,8 +1265,6 @@ static void handle_iteration_start (void *cls,
   struct ZoneIterationStartMessage * zis_msg = (struct ZoneIterationStartMessage *) message;
   struct GNUNET_NAMESTORE_Client *nc;
   struct GNUNET_NAMESTORE_ZoneIteration *zi;
-  struct ZoneIterationResponseMessage zir_msg;
-  struct ZoneIterationProcResult zipr;
   int res;
 
   nc = client_lookup(client);
@@ -1230,33 +1281,25 @@ static void handle_iteration_start (void *cls,
   zi->client = nc;
   zi->zone = zis_msg->zone;
 
-  GNUNET_CONTAINER_DLL_insert (nc->op_head, nc->op_tail, zi);
-
-  res = GSN_database->iterate_records (GSN_database->cls, &zis_msg->zone, NULL, zi->offset , &zone_iteration_proc, &zipr);
-  switch (res) {
-    case GNUNET_OK:
-      /* GNUNET_OK on success */
-
-      break;
-    case GNUNET_SYSERR:
-      /* GNUNET_SYSERR on error */
-      break;
-    case GNUNET_NO:
-      /* GNUNET_NO if there were no results, */
-      break;
-    default:
-      break;
+  GNUNET_HashCode dummy;
+  GNUNET_HashCode *zone_tmp;
+  memset (&dummy, '\0', sizeof (dummy));
+  if (0 == memcmp (&dummy, &zis_msg->zone, sizeof (dummy)))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting to iterate over all zones\n");
+    zi->has_zone = GNUNET_NO;
+    zone_tmp = NULL;
+  }
+  else
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting to iterate over zone  `%s'\n", GNUNET_h2s (&zis_msg->zone));
+    zi->has_zone = GNUNET_YES;
+    zone_tmp = &zis_msg->zone;
   }
 
+  GNUNET_CONTAINER_DLL_insert (nc->op_head, nc->op_tail, zi);
 
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "ZONE_ITERATION_RESPONSE");
-  zir_msg.gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE);
-  zir_msg.gns_header.header.size = htons (sizeof (struct ZoneIterationResponseMessage));
-  zir_msg.gns_header.r_id = htonl(zi->request_id);
-
-  GNUNET_SERVER_notification_context_unicast (snc, zi->client->client, (const struct GNUNET_MessageHeader *) &zir_msg, GNUNET_NO);
-
+  res = GSN_database->iterate_records (GSN_database->cls, zone_tmp , NULL, zi->offset , &zone_iteration_proc, zi);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
@@ -1293,7 +1336,10 @@ static void handle_iteration_stop (void *cls,
   }
 
   GNUNET_CONTAINER_DLL_remove(nc->op_head, nc->op_tail, zi);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopped zone iteration for zone `%s'\n", GNUNET_h2s (&zi->zone));
+  if (GNUNET_YES == zi->has_zone)
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopped zone iteration for zone `%s'\n", GNUNET_h2s (&zi->zone));
+  else
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopped zone iteration all zones\n");
   GNUNET_free (zi);
 
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
@@ -1307,9 +1353,9 @@ static void handle_iteration_next (void *cls,
 
   struct GNUNET_NAMESTORE_Client *nc;
   struct GNUNET_NAMESTORE_ZoneIteration *zi;
+  GNUNET_HashCode *zone_tmp;
   struct ZoneIterationStopMessage * zis_msg = (struct ZoneIterationStopMessage *) message;
   uint32_t rid;
-  int res;
 
   nc = client_lookup(client);
   if (nc == NULL)
@@ -1332,8 +1378,14 @@ static void handle_iteration_next (void *cls,
     return;
   }
 
+  if (GNUNET_YES == zi->has_zone)
+    zone_tmp = &zi->zone;
+  else
+    zone_tmp = NULL;
+
   zi->offset++;
-  res = GSN_database->iterate_records (GSN_database->cls, &zi->zone, NULL, zi->offset , &zone_iteration_proc, zi);
+  GSN_database->iterate_records (GSN_database->cls, zone_tmp, NULL, zi->offset , &zone_iteration_proc, zi);
+  GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }