NAMESTORE/JSON: fix parsing exp and flags
[oweals/gnunet.git] / src / namestore / gnunet-service-namestore.c
index d9e9a9e82c05ce0c3a6fe3deec288b2bf5c6fb1b..45be0fe750b859912344c69cc430339a2ef2bae7 100644 (file)
@@ -14,6 +14,8 @@
 
      You should have received a copy of the GNU Affero General Public License
      along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+     SPDX-License-Identifier: AGPL3.0-or-later
 */
 
 /**
@@ -110,6 +112,19 @@ struct ZoneIteration
    */
   uint32_t offset;
 
+  /**
+   * Number of pending cache operations triggered by this zone iteration which we
+   * need to wait for before allowing the client to continue.
+   */
+  unsigned int cache_ops;
+
+  /**
+   * Set to #GNUNET_YES if the last iteration exhausted the limit set by the
+   * client and we should send the #GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT_END
+   * message and free the data structure once @e cache_ops is zero.
+   */
+  int send_end;
+
 };
 
 
@@ -244,10 +259,16 @@ struct CacheOperation
   struct GNUNET_NAMECACHE_QueueEntry *qe;
 
   /**
-   * Client to notify about the result.
+   * Client to notify about the result, can be NULL.
    */
   struct NamestoreClient *nc;
 
+  /**
+   * Zone iteration to call #zone_iteration_done_client_continue()
+   * for if applicable, can be NULL.
+   */
+  struct ZoneIteration *zi;
+
   /**
    * Client's request ID.
    */
@@ -297,12 +318,12 @@ struct StoreActivity
 
 /**
  * Entry in list of cached nick resolutions.
- */ 
+ */
 struct NickCache
 {
   /**
    * Zone the cache entry is for.
-   */ 
+   */
   struct GNUNET_CRYPTO_EcdsaPrivateKey zone;
 
   /**
@@ -318,7 +339,7 @@ struct NickCache
 
 
 /**
- * We cache nick records to reduce DB load. 
+ * We cache nick records to reduce DB load.
  */
 static struct NickCache nick_cache[NC_SIZE];
 
@@ -468,7 +489,7 @@ free_store_activity (struct StoreActivity *sa)
  * record, which (if found) is then copied to @a cls for future use.
  *
  * @param cls a `struct GNUNET_GNSRECORD_Data **` for storing the nick (if found)
- * @param seq sequence number of the record
+ * @param seq sequence number of the record, MUST NOT BE ZERO
  * @param private_key the private key of the zone (unused)
  * @param label should be #GNUNET_GNS_EMPTY_LABEL_AT
  * @param rd_count number of records in @a rd
@@ -485,7 +506,7 @@ lookup_nick_it (void *cls,
   struct GNUNET_GNSRECORD_Data **res = cls;
 
   (void) private_key;
-  (void) seq;
+  GNUNET_assert (0 != seq);
   if (0 != strcmp (label, GNUNET_GNS_EMPTY_LABEL_AT))
   {
     GNUNET_break (0);
@@ -586,7 +607,7 @@ get_nick_record (const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone)
       return nick;
     }
   }
-  
+
   nick = NULL;
   res = GSN_database->lookup_records (GSN_database->cls,
                                      zone,
@@ -830,6 +851,35 @@ send_store_response (struct NamestoreClient *nc,
 }
 
 
+/**
+ * Function called once we are done with the zone iteration and
+ * allow the zone iteration client to send us more messages.
+ *
+ * @param zi zone iteration we are processing
+ */
+static void
+zone_iteration_done_client_continue (struct ZoneIteration *zi)
+{
+  struct GNUNET_MQ_Envelope *env;
+  struct GNUNET_NAMESTORE_Header *em;
+
+  GNUNET_SERVICE_client_continue (zi->nc->client);
+  if (! zi->send_end)
+    return;
+  /* send empty response to indicate end of list */
+  env = GNUNET_MQ_msg (em,
+                       GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT_END);
+  em->r_id = htonl (zi->request_id);
+  GNUNET_MQ_send (zi->nc->mq,
+                  env);
+
+  GNUNET_CONTAINER_DLL_remove (zi->nc->op_head,
+                              zi->nc->op_tail,
+                              zi);
+  GNUNET_free (zi);
+}
+
+
 /**
  * Cache operation complete, clean up.
  *
@@ -843,6 +893,7 @@ finish_cache_operation (void *cls,
                         const char *emsg)
 {
   struct CacheOperation *cop = cls;
+  struct ZoneIteration *zi;
 
   if (NULL != emsg)
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
@@ -858,6 +909,15 @@ finish_cache_operation (void *cls,
     send_store_response (cop->nc,
                          success,
                          cop->rid);
+  if (NULL != (zi = cop->zi))
+    {
+      zi->cache_ops--;
+      if (0 == zi->cache_ops)
+      {
+       /* unchoke zone iteration, cache has caught up */
+       zone_iteration_done_client_continue (zi);
+      }
+    }
   GNUNET_free (cop);
 }
 
@@ -867,6 +927,7 @@ finish_cache_operation (void *cls,
  * refresh the corresponding (encrypted) block in the namecache.
  *
  * @param nc client responsible for the request, can be NULL
+ * @param zi zone iteration response for the request, can be NULL
  * @param rid request ID of the client
  * @param zone_key private key of the zone
  * @param name label for the records
@@ -875,6 +936,7 @@ finish_cache_operation (void *cls,
  */
 static void
 refresh_block (struct NamestoreClient *nc,
+              struct ZoneIteration *zi,
                uint32_t rid,
                const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
                const char *name,
@@ -950,6 +1012,9 @@ refresh_block (struct NamestoreClient *nc,
                             GNUNET_NO);
   cop = GNUNET_new (struct CacheOperation);
   cop->nc = nc;
+  cop->zi = zi;
+  if (NULL != zi)
+    zi->cache_ops++;
   cop->rid = rid;
   GNUNET_CONTAINER_DLL_insert (cop_head,
                                cop_tail,
@@ -1052,6 +1117,7 @@ continue_store_activity (struct StoreActivity *sa)
     }
     /* great, done with the monitors, unpack (again) for refresh_block operation */
     refresh_block (sa->nc,
+                  NULL,
                    rid,
                    &rp_msg->private_key,
                    sa->conv_name,
@@ -1204,9 +1270,16 @@ struct RecordLookupContext
 
 
 /**
- * FIXME.
+ * Function called by the namestore plugin when we are trying to lookup
+ * a record as part of #handle_record_lookup().  Merges all results into
+ * the context.
  *
- * @param seq sequence number of the record
+ * @param cls closure with a `struct RecordLookupContext`
+ * @param seq unique serial number of the record, MUST NOT BE ZERO
+ * @param zone_key private key of the zone
+ * @param label name that is being mapped (at most 255 characters long)
+ * @param rd_count number of entries in @a rd array
+ * @param rd array of records with data to store
  */
 static void
 lookup_it (void *cls,
@@ -1219,7 +1292,7 @@ lookup_it (void *cls,
   struct RecordLookupContext *rlc = cls;
 
   (void) private_key;
-  (void) seq;
+  GNUNET_assert (0 != seq);
   if (0 != strcmp (label,
                    rlc->label))
     return;
@@ -1543,7 +1616,7 @@ handle_record_store (void *cls,
                            conv_name)) ||
              (GNUNET_GNSRECORD_TYPE_NICK != rd[i].record_type) )
           rd_clean_off++;
-       
+
        if ( (0 == strcmp (GNUNET_GNS_EMPTY_LABEL_AT,
                            conv_name)) &&
             (GNUNET_GNSRECORD_TYPE_NICK == rd[i].record_type) )
@@ -1614,7 +1687,7 @@ struct ZoneToNameCtx
  * Zone to name iterator
  *
  * @param cls struct ZoneToNameCtx *
- * @param seq sequence number of the record
+ * @param seq sequence number of the record, MUST NOT BE ZERO
  * @param zone_key the zone key
  * @param name name
  * @param rd_count number of records in @a rd
@@ -1638,7 +1711,7 @@ handle_zone_to_name_it (void *cls,
   char *name_tmp;
   char *rd_tmp;
 
-  (void) seq;
+  GNUNET_assert (0 != seq);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Found result for zone-to-name lookup: `%s'\n",
              name);
@@ -1756,7 +1829,7 @@ struct ZoneIterationProcResult
  * Process results for zone iteration from database
  *
  * @param cls struct ZoneIterationProcResult
- * @param seq sequence number of the record
+ * @param seq sequence number of the record, MUST NOT BE ZERO
  * @param zone_key the zone key
  * @param name name
  * @param rd_count number of records for this name
@@ -1773,6 +1846,7 @@ zone_iterate_proc (void *cls,
   struct ZoneIterationProcResult *proc = cls;
   int do_refresh_block;
 
+  GNUNET_assert (0 != seq);
   if ( (NULL == zone_key) &&
        (NULL == name) )
   {
@@ -1812,6 +1886,7 @@ zone_iterate_proc (void *cls,
     }
   if (GNUNET_YES == do_refresh_block)
     refresh_block (NULL,
+                  proc->zi,
                    0,
                    zone_key,
                    name,
@@ -1831,8 +1906,6 @@ run_zone_iteration_round (struct ZoneIteration *zi,
                           uint64_t limit)
 {
   struct ZoneIterationProcResult proc;
-  struct GNUNET_MQ_Envelope *env;
-  struct GNUNET_NAMESTORE_Header *em;
   struct GNUNET_TIME_Absolute start;
   struct GNUNET_TIME_Relative duration;
 
@@ -1865,27 +1938,12 @@ run_zone_iteration_round (struct ZoneIteration *zi,
                          duration.rel_value_us,
                          GNUNET_NO);
   if (0 == proc.limit)
-  {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "Returned %llu results, more results available\n",
                 (unsigned long long) limit);
-    return; /* more results later after we get the
-               #GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT message */
-  }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Completed iteration after %llu/%llu results\n",
-              (unsigned long long) (limit - proc.limit),
-              (unsigned long long) limit);
-  /* send empty response to indicate end of list */
-  env = GNUNET_MQ_msg (em,
-                       GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT_END);
-  em->r_id = htonl (zi->request_id);
-  GNUNET_MQ_send (zi->nc->mq,
-                  env);
-  GNUNET_CONTAINER_DLL_remove (zi->nc->op_head,
-                               zi->nc->op_tail,
-                               zi);
-  GNUNET_free (zi);
+  zi->send_end = (0 != proc.limit);
+  if (0 == zi->cache_ops)
+    zone_iteration_done_client_continue (zi);
 }
 
 
@@ -1915,7 +1973,6 @@ handle_iteration_start (void *cls,
                                zi);
   run_zone_iteration_round (zi,
                             1);
-  GNUNET_SERVICE_client_continue (nc->client);
 }
 
 
@@ -1987,7 +2044,6 @@ handle_iteration_next (void *cls,
   }
   run_zone_iteration_round (zi,
                             limit);
-  GNUNET_SERVICE_client_continue (nc->client);
 }
 
 
@@ -2068,7 +2124,7 @@ monitor_iteration_next (void *cls);
  * A #GNUNET_NAMESTORE_RecordIterator for monitors.
  *
  * @param cls a 'struct ZoneMonitor *' with information about the monitor
- * @param seq sequence number of the record
+ * @param seq sequence number of the record, MUST NOT BE ZERO
  * @param zone_key zone key of the zone
  * @param name name
  * @param rd_count number of records in @a rd
@@ -2084,6 +2140,7 @@ monitor_iterate_cb (void *cls,
 {
   struct ZoneMonitor *zm = cls;
 
+  GNUNET_assert (0 != seq);
   zm->seq = seq;
   GNUNET_assert (NULL != name);
   GNUNET_STATISTICS_update (statistics,