From 03374cc39814fa4bfb8d96e1165c688c44d8342f Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sat, 9 Jul 2016 14:29:34 +0000 Subject: [PATCH] make namestore API less brittle/sublte to use --- src/gns/gnunet-service-gns.c | 238 +++++++++++------- src/gns/gnunet-service-gns_shorten.c | 20 +- .../gnunet-service-identity-provider.c | 186 +++++++++----- .../plugin_rest_identity_provider.c | 97 ++++--- src/include/gnunet_namestore_service.h | 82 +++--- src/namestore/gnunet-namestore-fcfsd.c | 142 +++++++---- src/namestore/gnunet-namestore.c | 110 +++++++- src/namestore/namestore_api.c | 92 +++++-- src/namestore/namestore_api_monitor.c | 52 ++-- src/namestore/plugin_rest_namestore.c | 86 +++++-- src/namestore/test_namestore_api.conf | 3 + .../test_namestore_api_lookup_nick.c | 16 +- .../test_namestore_api_lookup_private.c | 19 +- src/namestore/test_namestore_api_monitoring.c | 23 +- .../test_namestore_api_monitoring_existing.c | 17 ++ .../test_namestore_api_zone_iteration.c | 92 ++++--- .../test_namestore_api_zone_iteration_nick.c | 53 +++- ...mestore_api_zone_iteration_specific_zone.c | 162 +++++++----- .../test_namestore_api_zone_iteration_stop.c | 125 ++++++--- 19 files changed, 1158 insertions(+), 457 deletions(-) diff --git a/src/gns/gnunet-service-gns.c b/src/gns/gnunet-service-gns.c index 2a9d86a8b..ea271e9dd 100644 --- a/src/gns/gnunet-service-gns.c +++ b/src/gns/gnunet-service-gns.c @@ -46,10 +46,15 @@ #define INITIAL_PUT_INTERVAL GNUNET_TIME_UNIT_MILLISECONDS /** - * The upper bound for the zone iteration interval in milliseconds + * The lower bound for the zone iteration interval */ #define MINIMUM_ZONE_ITERATION_INTERVAL GNUNET_TIME_UNIT_SECONDS +/** + * The upper bound for the zone iteration interval + */ +#define MAXIMUM_ZONE_ITERATION_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15) + /** * The default put interval for the zone iteration. In case * no option is found @@ -401,6 +406,12 @@ dht_put_continuation (void *cls, } else next_put_interval = put_interval; + next_put_interval = GNUNET_TIME_relative_min (next_put_interval, + MAXIMUM_ZONE_ITERATION_INTERVAL); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "PUT complete, next PUT in %s!\n", + GNUNET_STRINGS_relative_time_to_string (next_put_interval, + GNUNET_YES)); GNUNET_STATISTICS_set (statistics, "Current zone iteration interval (ms)", @@ -509,7 +520,8 @@ perform_dht_put (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key, label, GNUNET_STRINGS_absolute_time_to_string (expire), GNUNET_h2s (&query)); - ret = GNUNET_DHT_put (dht_handle, &query, + ret = GNUNET_DHT_put (dht_handle, + &query, DHT_GNS_REPLICATION_LEVEL, GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, GNUNET_BLOCK_TYPE_GNS_NAMERECORD, @@ -523,6 +535,98 @@ perform_dht_put (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key, } +/** + * We encountered an error in our zone iteration. + */ +static void +zone_iteration_error (void *cls) +{ + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Got disconnected from namestore database, retrying.\n"); + namestore_iter = NULL; + /* We end up here on error/disconnect/shutdown, so potentially + while a zone publish task or a DHT put is still running; hence + we need to cancel those. */ + if (NULL != zone_publish_task) + { + GNUNET_SCHEDULER_cancel (zone_publish_task); + zone_publish_task = NULL; + } + if (NULL != active_put) + { + GNUNET_DHT_put_cancel (active_put); + active_put = NULL; + } + zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start, + NULL); +} + + +/** + * Zone iteration is completed. + */ +static void +zone_iteration_finished (void *cls) +{ + /* we're done with one iteration, calculate when to do the next one */ + namestore_iter = NULL; + last_num_public_records = num_public_records; + first_zone_iteration = GNUNET_NO; + if (0 == num_public_records) + { + /** + * If no records are known (startup) or none present + * we can safely set the interval to the value for a single + * record + */ + put_interval = zone_publish_time_window; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, + "No records in namestore database.\n"); + } + else + { + /* If records are present, next publication is based on the minimum + * relative expiration time of the records published divided by 4 + */ + zone_publish_time_window + = GNUNET_TIME_relative_min (GNUNET_TIME_relative_divide (min_relative_record_time, 4), + zone_publish_time_window_default); + put_interval = GNUNET_TIME_relative_divide (zone_publish_time_window, + num_public_records); + } + /* reset for next iteration */ + min_relative_record_time = GNUNET_TIME_UNIT_FOREVER_REL; + put_interval = GNUNET_TIME_relative_max (MINIMUM_ZONE_ITERATION_INTERVAL, + put_interval); + put_interval = GNUNET_TIME_relative_min (put_interval, + MAXIMUM_ZONE_ITERATION_INTERVAL); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Zone iteration finished. Adjusted zone iteration interval to %s\n", + GNUNET_STRINGS_relative_time_to_string (put_interval, + GNUNET_YES)); + GNUNET_STATISTICS_set (statistics, + "Current zone iteration interval (in ms)", + put_interval.rel_value_us / 1000LL, + GNUNET_NO); + GNUNET_STATISTICS_update (statistics, + "Number of zone iterations", + 1, + GNUNET_NO); + GNUNET_STATISTICS_set (statistics, + "Number of public records in DHT", + last_num_public_records, + GNUNET_NO); + GNUNET_assert (NULL == zone_publish_task); + if (0 == num_public_records) + zone_publish_task = GNUNET_SCHEDULER_add_delayed (put_interval, + &publish_zone_dht_start, + NULL); + else + zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start, + NULL); +} + + /** * Function used to put all records successively into the DHT. * @@ -542,95 +646,22 @@ put_gns_record (void *cls, struct GNUNET_GNSRECORD_Data rd_public[rd_count]; unsigned int rd_public_count; - if ( (NULL == key) && - (NULL == label) && - (0 == rd_count) ) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Got disconnected from namestore database, retrying.\n"); - namestore_iter = NULL; - if (NULL != zone_publish_task) - { - GNUNET_SCHEDULER_cancel (zone_publish_task); - zone_publish_task = NULL; - } - zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start, - NULL); - return; - } - if (NULL == label) - { - /* we're done with one iteration, calculate when to do the next one */ - namestore_iter = NULL; - last_num_public_records = num_public_records; - first_zone_iteration = GNUNET_NO; - if (0 == num_public_records) - { - /** - * If no records are known (startup) or none present - * we can safely set the interval to the value for a single - * record - */ - put_interval = zone_publish_time_window; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, - "No records in namestore database.\n"); - } - else - { - /* If records are present, next publication is based on the minimum - * relative expiration time of the records published divided by 4 - */ - zone_publish_time_window = GNUNET_TIME_relative_min ( - GNUNET_TIME_relative_divide (min_relative_record_time, 4), - zone_publish_time_window_default); - put_interval = GNUNET_TIME_relative_divide (zone_publish_time_window, - num_public_records); - } - /* reset for next iteration */ - min_relative_record_time = GNUNET_TIME_UNIT_FOREVER_REL; - put_interval = GNUNET_TIME_relative_max (MINIMUM_ZONE_ITERATION_INTERVAL, - put_interval); - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Zone iteration finished. Adjusted zone iteration interval to %s\n", - GNUNET_STRINGS_relative_time_to_string (put_interval, - GNUNET_YES)); - GNUNET_STATISTICS_set (statistics, - "Current zone iteration interval (in ms)", - put_interval.rel_value_us / 1000LL, - GNUNET_NO); - GNUNET_STATISTICS_update (statistics, - "Number of zone iterations", - 1, - GNUNET_NO); - GNUNET_STATISTICS_set (statistics, - "Number of public records in DHT", - last_num_public_records, - GNUNET_NO); - GNUNET_assert (NULL == zone_publish_task); - if (0 == num_public_records) - zone_publish_task = GNUNET_SCHEDULER_add_delayed (put_interval, - &publish_zone_dht_start, - NULL); - else - zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start, - NULL); - return; - } - rd_public_count = convert_records_for_export (rd, rd_count, rd_public); - /* We got a set of records to publish */ if (0 == rd_public_count) { GNUNET_assert (NULL == zone_publish_task); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Record set empty, moving to next record set\n"); zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_next, - NULL); + NULL); return; } - + /* We got a set of records to publish */ + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Starting DHT PUT\n"); active_put = perform_dht_put (key, label, rd_public, @@ -662,7 +693,11 @@ publish_zone_dht_start (void *cls) namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle, NULL, /* All zones */ + &zone_iteration_error, + NULL, &put_gns_record, + NULL, + &zone_iteration_finished, NULL); } @@ -694,13 +729,16 @@ handle_monitor_event (void *cls, label); /* filter out records that are not public, and convert to absolute expiration time. */ - rd_public_count = convert_records_for_export (rd, rd_count, + rd_public_count = convert_records_for_export (rd, + rd_count, rd_public); if (0 == rd_public_count) return; /* nothing to do */ ma = GNUNET_new (struct MonitorActivity); - ma->ph = perform_dht_put (zone, label, - rd, rd_count, + ma->ph = perform_dht_put (zone, + label, + rd, + rd_count, ma); if (NULL == ma->ph) { @@ -880,6 +918,35 @@ monitor_sync_event (void *cls) } +/** + * The zone monitor is now in SYNC with the current state of the + * name store. Start to perform periodic iterations. + * + * @param cls NULL + */ +static void +handle_monitor_error (void *cls) +{ + if (NULL != zone_publish_task) + { + GNUNET_SCHEDULER_cancel (zone_publish_task); + zone_publish_task = NULL; + } + if (NULL != namestore_iter) + { + GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter); + namestore_iter = NULL; + } + if (NULL != active_put) + { + GNUNET_DHT_put_cancel (active_put); + active_put = NULL; + } + zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start, + NULL); +} + + /** * Method called to inform about the ego to be used for the master zone * for DNS interceptions. @@ -1032,7 +1099,10 @@ run (void *cls, zmon = GNUNET_NAMESTORE_zone_monitor_start (c, NULL, GNUNET_NO, + &handle_monitor_error, + NULL, &handle_monitor_event, + NULL, &monitor_sync_event, NULL); GNUNET_break (NULL != zmon); diff --git a/src/gns/gnunet-service-gns_shorten.c b/src/gns/gnunet-service-gns_shorten.c index dd546ae56..9aa0419aa 100644 --- a/src/gns/gnunet-service-gns_shorten.c +++ b/src/gns/gnunet-service-gns_shorten.c @@ -325,6 +325,21 @@ process_pseu_lookup_ns (void *cls, } +/** + * Encountered an error in zone-to-name lookup, give up on shortening. + */ +static void +zone_to_name_error_cb (void *cls) +{ + struct GetPseuAuthorityHandle* gph = cls; + + gph->namestore_task = NULL; + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "Shortening aborted, internal error talking to namestore\n"); + free_get_pseu_authority_handle (gph); +} + + /** * Callback called by namestore for a zone to name result. We're * trying to see if a short name for a given zone already exists. @@ -343,9 +358,6 @@ process_zone_to_name_discover (void *cls, const struct GNUNET_GNSRECORD_Data *rd) { struct GetPseuAuthorityHandle* gph = cls; -#if 0 - struct GNUNET_HashCode lookup_key; -#endif gph->namestore_task = NULL; if (0 != rd_len) @@ -412,6 +424,8 @@ GNS_shorten_start (const char *original_label, gph->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle, shorten_zone, pub, + &zone_to_name_error_cb, + gph, &process_zone_to_name_discover, gph); } diff --git a/src/identity-provider/gnunet-service-identity-provider.c b/src/identity-provider/gnunet-service-identity-provider.c index 7a68fe902..228e6cdc5 100644 --- a/src/identity-provider/gnunet-service-identity-provider.c +++ b/src/identity-provider/gnunet-service-identity-provider.c @@ -531,6 +531,32 @@ clear_ego_attrs (void *cls, } +static void +token_collect_error_cb (void *cls) +{ + // struct EgoEntry *ego_entry = cls; + + GNUNET_assert (0); // FIXME: handle properly! +} + + +static void +token_collect_finished_cb (void *cls) +{ + struct EgoEntry *ego_entry = cls; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + ">>> Updating Ego finished\n"); + //Clear attribute map for ego + GNUNET_CONTAINER_multihashmap_iterate (ego_entry->attr_map, + &clear_ego_attrs, + ego_entry); + GNUNET_CONTAINER_multihashmap_clear (ego_entry->attr_map); + update_task = GNUNET_SCHEDULER_add_now (&update_identities, + ego_entry->next); +} + + /** * * Update all ID_TOKEN records for an identity and store them @@ -553,21 +579,6 @@ token_collect (void *cls, const struct GNUNET_GNSRECORD_Data *token_metadata_record; struct GNUNET_CRYPTO_EcdsaPublicKey *aud_key; - if (NULL == lbl) - { - //Done - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - ">>> Updating Ego finished\n"); - //Clear attribute map for ego - GNUNET_CONTAINER_multihashmap_iterate (ego_entry->attr_map, - &clear_ego_attrs, - ego_entry); - GNUNET_CONTAINER_multihashmap_clear (ego_entry->attr_map); - update_task = GNUNET_SCHEDULER_add_now (&update_identities, - ego_entry->next); - return; - } - //There should be only a single record for a token under a label if (2 != rd_count) { @@ -614,6 +625,28 @@ token_collect (void *cls, } +static void +attribute_collect_error_cb (void *cls) +{ + // struct EgoEntry *ego_entry = cls; + + GNUNET_assert (0); // FIXME: handle properly! +} + + +static void +attribute_collect_finished_cb (void *cls) +{ + struct EgoEntry *ego_entry = cls; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + ">>> Updating Attributes finished\n"); + ego_entry->attributes_dirty = GNUNET_NO; + update_task = GNUNET_SCHEDULER_add_now (&update_identities, + ego_entry); +} + + /** * * Collect all ID_ATTR records for an identity and store them @@ -639,17 +672,6 @@ attribute_collect (void *cls, char *val_str; int i; - if (NULL == lbl) - { - //Done - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - ">>> Updating Attributes finished\n"); - ego_entry->attributes_dirty = GNUNET_NO; - update_task = GNUNET_SCHEDULER_add_now (&update_identities, - ego_entry); - return; - } - if (0 == rd_count) { GNUNET_NAMESTORE_zone_iterator_next (ns_it); @@ -703,7 +725,6 @@ attribute_collect (void *cls, attr, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); GNUNET_NAMESTORE_zone_iterator_next (ns_it); - return; } /** @@ -718,6 +739,7 @@ update_identities(void *cls) { struct EgoEntry *next_ego = cls; const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key; + update_task = NULL; if (NULL == next_ego) { @@ -740,7 +762,11 @@ update_identities(void *cls) //Starting over. We must update the Attributes for they might have changed. ns_it = GNUNET_NAMESTORE_zone_iteration_start (ns_handle, priv_key, + &attribute_collect_error_cb, + next_ego, &attribute_collect, + next_ego, + &attribute_collect_finished_cb, next_ego); } @@ -750,7 +776,11 @@ update_identities(void *cls) next_ego->attributes_dirty = GNUNET_YES; ns_it = GNUNET_NAMESTORE_zone_iteration_start (ns_handle, priv_key, + &token_collect_error_cb, + next_ego, &token_collect, + next_ego, + &token_collect_finished_cb, next_ego); } } @@ -939,7 +969,7 @@ store_token_issue_cont (void *cls, struct GNUNET_IDENTITY_PROVIDER_IssueResultMessage *irm; char *ticket_str; char *token_str; - + handle->ns_qe = NULL; if (GNUNET_SYSERR == success) { @@ -1077,6 +1107,27 @@ sign_and_return_token (void *cls) GNUNET_free (token_metadata); } + +static void +attr_collect_error (void *cls) +{ + // struct IssueHandle *handle = cls; + + GNUNET_assert (0); // FIXME: handle error! +} + + +static void +attr_collect_finished (void *cls) +{ + struct IssueHandle *handle = cls; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding attribute END: \n"); + handle->ns_it = NULL; + GNUNET_SCHEDULER_add_now (&sign_and_return_token, handle); +} + + /** * Collect attributes for token */ @@ -1087,19 +1138,11 @@ attr_collect (void *cls, unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd) { + struct IssueHandle *handle = cls; int i; char* data; - struct IssueHandle *handle = cls; struct GNUNET_HashCode key; - if (NULL == label) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding attribute END: \n"); - handle->ns_it = NULL; - GNUNET_SCHEDULER_add_now (&sign_and_return_token, handle); - return; - } - GNUNET_CRYPTO_hash (label, strlen (label), &key); @@ -1278,6 +1321,41 @@ handle_exchange_message (void *cls, } +static void +find_existing_token_error (void *cls) +{ + // struct IssueHandle *handle = cls; + + GNUNET_assert (0); // FIXME: handle properly +} + + +static void +find_existing_token_finished (void *cls) +{ + struct IssueHandle *handle = cls; + uint64_t rnd_key; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + ">>> No existing token found\n"); + rnd_key = + GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG, + UINT64_MAX); + GNUNET_STRINGS_base64_encode ((char*)&rnd_key, + sizeof (uint64_t), + &handle->label); + handle->ns_it = NULL; + handle->ns_it = GNUNET_NAMESTORE_zone_iteration_start (ns_handle, + &handle->iss_key, + &attr_collect_error, + handle, + &attr_collect, + handle, + &attr_collect_finished, + handle); +} + + /** * * Look for existing token @@ -1301,30 +1379,9 @@ find_existing_token (void *cls, struct GNUNET_CRYPTO_EcdsaPublicKey *aud_key; struct GNUNET_HashCode key; int scope_count_token; - uint64_t rnd_key; char *scope; char *tmp_scopes; - if (NULL == lbl) - { - //Done - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - ">>> No existing token found\n"); - //Label - rnd_key = - GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG, - UINT64_MAX); - GNUNET_STRINGS_base64_encode ((char*)&rnd_key, - sizeof (uint64_t), - &handle->label); - handle->ns_it = NULL; - handle->ns_it = GNUNET_NAMESTORE_zone_iteration_start (ns_handle, - &handle->iss_key, - &attr_collect, - handle); - return; - } - //There should be only a single record for a token under a label if (2 != rd_count) { @@ -1335,7 +1392,9 @@ find_existing_token (void *cls, if (rd[0].record_type == GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA) { token_metadata_record = &rd[0]; - } else { + } + else + { token_metadata_record = &rd[1]; } if (token_metadata_record->record_type != GNUNET_GNSRECORD_TYPE_ID_TOKEN_METADATA) @@ -1399,7 +1458,11 @@ find_existing_token (void *cls, handle->ns_it = NULL; handle->ns_it = GNUNET_NAMESTORE_zone_iteration_start (ns_handle, &handle->iss_key, + &attr_collect_error, + handle, &attr_collect, + handle, + &attr_collect_finished, handle); return; @@ -1477,10 +1540,15 @@ handle_issue_message (void *cls, issue_handle->ns_it = GNUNET_NAMESTORE_zone_iteration_start (ns_handle, &im->iss_key, + &find_existing_token_error, + issue_handle, &find_existing_token, + issue_handle, + &find_existing_token_finished, issue_handle); } + /** * Main function that will be run * diff --git a/src/identity-provider/plugin_rest_identity_provider.c b/src/identity-provider/plugin_rest_identity_provider.c index c0b018798..fa97137d7 100644 --- a/src/identity-provider/plugin_rest_identity_provider.c +++ b/src/identity-provider/plugin_rest_identity_provider.c @@ -647,6 +647,16 @@ return_token_list (void *cls) cleanup_handle (handle); } + +static void +token_collect_error_cb (void *cls) +{ + struct RequestHandle *handle = cls; + + do_error (handle); +} + + /** * Collect all tokens for an ego * @@ -658,45 +668,68 @@ token_collect (void *cls, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, const char *label, unsigned int rd_count, - const struct GNUNET_GNSRECORD_Data *rd) + const struct GNUNET_GNSRECORD_Data *rd); + + +static void +token_collect_finished_cb (void *cls) { - int i; - char* data; struct RequestHandle *handle = cls; struct EgoEntry *ego_tmp; - struct GNUNET_JSONAPI_Resource *json_resource; const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv_key; - json_t *issuer; - json_t *token; - - if (NULL == label) - { - ego_tmp = handle->ego_head; - GNUNET_CONTAINER_DLL_remove (handle->ego_head, - handle->ego_tail, - ego_tmp); - GNUNET_free (ego_tmp->identifier); - GNUNET_free (ego_tmp->keystring); - GNUNET_free (ego_tmp); - if (NULL == handle->ego_head) - { - //Done - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding token END\n"); - handle->ns_it = NULL; - GNUNET_SCHEDULER_add_now (&return_token_list, handle); - return; - } + ego_tmp = handle->ego_head; + GNUNET_CONTAINER_DLL_remove (handle->ego_head, + handle->ego_tail, + ego_tmp); + GNUNET_free (ego_tmp->identifier); + GNUNET_free (ego_tmp->keystring); + GNUNET_free (ego_tmp); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Next ego: %s\n", handle->ego_head->identifier); - priv_key = GNUNET_IDENTITY_ego_get_private_key (handle->ego_head->ego); - handle->ns_it = GNUNET_NAMESTORE_zone_iteration_start (handle->ns_handle, - priv_key, - &token_collect, - handle); + if (NULL == handle->ego_head) + { + //Done + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding token END\n"); + handle->ns_it = NULL; + GNUNET_SCHEDULER_add_now (&return_token_list, handle); return; } + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Next ego: %s\n", + handle->ego_head->identifier); + priv_key = GNUNET_IDENTITY_ego_get_private_key (handle->ego_head->ego); + handle->ns_it = GNUNET_NAMESTORE_zone_iteration_start (handle->ns_handle, + priv_key, + &token_collect_error_cb, + handle, + &token_collect, + handle, + &token_collect_finished_cb, + handle); +} + + +/** + * Collect all tokens for an ego + * + * TODO move this into the identity-provider service + * + */ +static void +token_collect (void *cls, + const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, + const char *label, + unsigned int rd_count, + const struct GNUNET_GNSRECORD_Data *rd) +{ + struct RequestHandle *handle = cls; + int i; + char* data; + struct GNUNET_JSONAPI_Resource *json_resource; + json_t *issuer; + json_t *token; + for (i = 0; i < rd_count; i++) { if (rd[i].record_type == GNUNET_GNSRECORD_TYPE_ID_TOKEN) @@ -789,7 +822,11 @@ list_token_cont (struct GNUNET_REST_RequestHandle *con_handle, handle->ns_handle = GNUNET_NAMESTORE_connect (cfg); handle->ns_it = GNUNET_NAMESTORE_zone_iteration_start (handle->ns_handle, priv_key, + &token_collect_error_cb, + handle, &token_collect, + handle, + &token_collect_finished_cb, handle); } diff --git a/src/include/gnunet_namestore_service.h b/src/include/gnunet_namestore_service.h index 1918ca1ff..0add8852a 100644 --- a/src/include/gnunet_namestore_service.h +++ b/src/include/gnunet_namestore_service.h @@ -133,8 +133,8 @@ GNUNET_NAMESTORE_records_store (struct GNUNET_NAMESTORE_Handle *h, * Process a record that was stored in the namestore. * * @param cls closure - * @param zone private key of the zone; NULL on disconnect - * @param label label of the records; NULL on disconnect + * @param zone private key of the zone + * @param label label of the records * @param rd_count number of entries in @a rd array, 0 if label was deleted * @param rd array of records with data to store */ @@ -170,7 +170,11 @@ GNUNET_NAMESTORE_set_nick (struct GNUNET_NAMESTORE_Handle *h, * @param h handle to the namestore * @param pkey private key of the zone * @param label name that is being mapped - * @param rm function to call with the result (with 0 records if we don't have that label) + * @param error_cb function to call on error (i.e. disconnect) + * the handle is afterwards invalid + * @param error_cb_cls closure for @a error_cb + * @param rm function to call with the result (with 0 records if we don't have that label); + * the handle is afterwards invalid * @param rm_cls closure for @a rm * @return handle to abort the request */ @@ -178,6 +182,8 @@ struct GNUNET_NAMESTORE_QueueEntry * GNUNET_NAMESTORE_records_lookup (struct GNUNET_NAMESTORE_Handle *h, const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey, const char *label, + GNUNET_SCHEDULER_TaskCallback error_cb, + void *error_cb_cls, GNUNET_NAMESTORE_RecordMonitor rm, void *rm_cls); @@ -189,8 +195,12 @@ GNUNET_NAMESTORE_records_lookup (struct GNUNET_NAMESTORE_Handle *h, * @param h handle to the namestore * @param zone public key of the zone to look up in, never NULL * @param value_zone public key of the target zone (value), never NULL + * @param error_cb function to call on error (i.e. disconnect) + * the handle is afterwards invalid + * @param error_cb_cls closure for @a error_cb * @param proc function to call on the matching records, or with - * NULL (rd_count == 0) if there are no matching records + * NULL (rd_count == 0) if there are no matching records; + * the handle is afterwards invalid * @param proc_cls closure for @a proc * @return a handle that can be used to * cancel @@ -199,7 +209,10 @@ struct GNUNET_NAMESTORE_QueueEntry * GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone, - GNUNET_NAMESTORE_RecordMonitor proc, void *proc_cls); + GNUNET_SCHEDULER_TaskCallback error_cb, + void *error_cb_cls, + GNUNET_NAMESTORE_RecordMonitor proc, + void *proc_cls); /** @@ -216,25 +229,38 @@ GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe); /** * Starts a new zone iteration (used to periodically PUT all of our - * records into our DHT). This MUST lock the struct GNUNET_NAMESTORE_Handle - * for any other calls than #GNUNET_NAMESTORE_zone_iterator_next and + * records into our DHT). This MUST lock the `struct GNUNET_NAMESTORE_Handle` + * for any other calls than #GNUNET_NAMESTORE_zone_iterator_next() and * #GNUNET_NAMESTORE_zone_iteration_stop. @a proc will be called once * immediately, and then again after - * #GNUNET_NAMESTORE_zone_iterator_next is invoked. + * #GNUNET_NAMESTORE_zone_iterator_next() is invoked. + * + * On error (disconnect), @a error_cb will be invoked. + * On normal completion, @a finish_cb proc will be + * invoked. * * @param h handle to the namestore * @param zone zone to access, NULL for all zones + * @param error_cb function to call on error (i.e. disconnect), + * the handle is afterwards invalid + * @param error_cb_cls closure for @a error_cb * @param proc function to call on each name from the zone; it * will be called repeatedly with a value (if available) - * and always once at the end with a label of NULL. * @param proc_cls closure for @a proc + * @param finish_cb function to call on completion + * the handle is afterwards invalid + * @param finish_cb_cls closure for @a finish_cb * @return an iterator handle to use for iteration */ struct GNUNET_NAMESTORE_ZoneIterator * GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, + GNUNET_SCHEDULER_TaskCallback error_cb, + void *error_cb_cls, GNUNET_NAMESTORE_RecordMonitor proc, - void *proc_cls); + void *proc_cls, + GNUNET_SCHEDULER_TaskCallback finish_cb, + void *finish_cb_cls); /** @@ -264,45 +290,43 @@ GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it); struct GNUNET_NAMESTORE_ZoneMonitor; -/** - * Function called once the monitor has caught up with the current - * state of the database. Will be called AGAIN after each disconnect - * (record monitor called with 'NULL' for zone_key) once we're again - * in sync. - * - * @param cls closure - */ -typedef void -(*GNUNET_NAMESTORE_RecordsSynchronizedCallback)(void *cls); - - /** * Begin monitoring a zone for changes. Will first call the @a * monitor function on all existing records in the selected zone(s) if * @a iterate_first is #GNUNET_YES. In any case, we will then call @a * sync_cb, and then afterwards call the @a monitor whenever a record - * changes. If the namestore disconnects, the @a monitor function is - * called with a disconnect event; if the connection is + * changes. If the namestore disconnects, the @a error_cb function is + * called with a disconnect event. Once the connection is * re-established, the process begins from the start (depending on @a - * iterate_first, we first do all existing records, then @a sync, then - * updates). + * iterate_first, we will again first do all existing records, then @a + * sync, then updates). * * @param cfg configuration to use to connect to namestore * @param zone zone to monitor, NULL for all zones * @param iterate_first #GNUNET_YES to first iterate over all existing records, * #GNUNET_NO to only return changes that happen from now on + * @param error_cb function to call on error (i.e. disconnect); note that + * unlike the other error callbacks in this API, a call to this + * function does NOT destroy the monitor handle, it merely signals + * that monitoring is down. You need to still explicitly call + * #GNUNET_NAMESTORE_zone_monitor_stop(). + * @param error_cb_cls closure for @a error_cb * @param monitor function to call on zone changes + * @param monitor_cls closure for @a monitor * @param sync_cb function called when we're in sync with the namestore - * @param cls closure for @a monitor and @a sync_cb + * @param sync_cb_cls closure for @a sync_cb * @return handle to stop monitoring */ struct GNUNET_NAMESTORE_ZoneMonitor * GNUNET_NAMESTORE_zone_monitor_start (const struct GNUNET_CONFIGURATION_Handle *cfg, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, int iterate_first, + GNUNET_SCHEDULER_TaskCallback error_cb, + void *error_cb_cls, GNUNET_NAMESTORE_RecordMonitor monitor, - GNUNET_NAMESTORE_RecordsSynchronizedCallback sync_cb, - void *cls); + void *monitor_cls, + GNUNET_SCHEDULER_TaskCallback sync_cb, + void *sync_cb_cls); /** diff --git a/src/namestore/gnunet-namestore-fcfsd.c b/src/namestore/gnunet-namestore-fcfsd.c index ed7009e02..b38761d5a 100644 --- a/src/namestore/gnunet-namestore-fcfsd.c +++ b/src/namestore/gnunet-namestore-fcfsd.c @@ -246,6 +246,62 @@ run_httpd_now () } +/** + * Function called on error in zone iteration. + */ +static void +zone_iteration_error (void *cls) +{ + struct ZoneinfoRequest *zr = cls; + struct MHD_Response *response; + + zr->list_it = NULL; + response = MHD_create_response_from_buffer (strlen ("internal error"), + (void *) "internal error", + MHD_RESPMEM_PERSISTENT); + MHD_queue_response (zr->connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + response); + MHD_destroy_response (response); + GNUNET_free (zr->zoneinfo); + GNUNET_free (zr); + run_httpd_now (); +} + + +/** + * Function called once the zone iteration is done. + */ +static void +zone_iteration_end (void *cls) +{ + struct ZoneinfoRequest *zr = cls; + struct MHD_Response *response; + char* full_page; + + zr->list_it = NULL; + + /* return static form */ + GNUNET_asprintf (&full_page, + ZONEINFO_PAGE, + zr->zoneinfo, + zr->zoneinfo); + response = MHD_create_response_from_buffer (strlen (full_page), + (void *) full_page, + MHD_RESPMEM_MUST_FREE); + MHD_add_response_header (response, + MHD_HTTP_HEADER_CONTENT_TYPE, + MIME_HTML); + MHD_queue_response (zr->connection, + MHD_HTTP_OK, + response); + MHD_destroy_response (response); + GNUNET_free (zr->zoneinfo); + GNUNET_free (zr); + run_httpd_now (); +} + + /** * Process a record that was stored in the namestore, adding * the information to the HTML. @@ -264,38 +320,10 @@ iterate_cb (void *cls, const struct GNUNET_GNSRECORD_Data *rd) { struct ZoneinfoRequest *zr = cls; - struct MHD_Response *response; - char* full_page; size_t bytes_free; char* pkey; char* new_buf; - - if (NULL == name) - { - zr->list_it = NULL; - - /* return static form */ - GNUNET_asprintf (&full_page, - ZONEINFO_PAGE, - zr->zoneinfo, - zr->zoneinfo); - response = MHD_create_response_from_buffer (strlen (full_page), - (void *) full_page, - MHD_RESPMEM_MUST_FREE); - MHD_add_response_header (response, - MHD_HTTP_HEADER_CONTENT_TYPE, - MIME_HTML); - MHD_queue_response (zr->connection, - MHD_HTTP_OK, - response); - MHD_destroy_response (response); - GNUNET_free (zr->zoneinfo); - GNUNET_free (zr); - run_httpd_now (); - return; - } - if (1 != rd_len) { GNUNET_NAMESTORE_zone_iterator_next (zr->list_it); @@ -354,8 +382,12 @@ serve_zoneinfo_page (struct MHD_Connection *connection) zr->write_offset = 0; zr->list_it = GNUNET_NAMESTORE_zone_iteration_start (ns, &fcfs_zone_pkey, + &zone_iteration_error, + zr, &iterate_cb, - zr); + zr, + &zone_iteration_end, + zr); return MHD_YES; } @@ -511,6 +543,21 @@ put_continuation (void *cls, } +/** + * Function called if we had an error in zone-to-name mapping. + */ +static void +zone_to_name_error (void *cls) +{ + struct Request *request = cls; + + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Error when mapping zone to name\n")); + request->phase = RP_FAIL; + run_httpd_now (); +} + + /** * Test if a name mapping was found, if so, refuse. If not, initiate storing of the record. * @@ -529,6 +576,7 @@ zone_to_name_cb (void *cls, { struct Request *request = cls; struct GNUNET_GNSRECORD_Data r; + request->qe = NULL; if (0 != rd_count) @@ -540,15 +588,6 @@ zone_to_name_cb (void *cls, run_httpd_now (); return; } - if (NULL == zone_key) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Error when mapping zone to name\n")); - request->phase = RP_FAIL; - run_httpd_now (); - return; - } - r.data = &request->pub; r.data_size = sizeof (request->pub); r.expiration_time = UINT64_MAX; @@ -563,6 +602,20 @@ zone_to_name_cb (void *cls, } +/** + * We encountered an error in the name lookup. + */ +static void +lookup_block_error (void *cls) +{ + struct Request *request = cls; + + request->qe = NULL; + request->phase = RP_FAIL; + run_httpd_now (); +} + + /** * We got a block back from the namestore. Decrypt it * and continue to process the result. @@ -585,7 +638,6 @@ lookup_block_processor (void *cls, request->qe = NULL; if (0 == rd_count) { - if (GNUNET_OK != GNUNET_CRYPTO_ecdsa_public_key_from_string (request->public_key, strlen (request->public_key), @@ -599,6 +651,8 @@ lookup_block_processor (void *cls, request->qe = GNUNET_NAMESTORE_zone_to_name (ns, &fcfs_zone_pkey, &request->pub, + &zone_to_name_error, + request, &zone_to_name_cb, request); return; @@ -728,10 +782,12 @@ create_response (void *cls, } request->phase = RP_LOOKUP; request->qe = GNUNET_NAMESTORE_records_lookup (ns, - &fcfs_zone_pkey, - request->domain_name, - &lookup_block_processor, - request); + &fcfs_zone_pkey, + request->domain_name, + &lookup_block_error, + request, + &lookup_block_processor, + request); break; case RP_LOOKUP: break; diff --git a/src/namestore/gnunet-namestore.c b/src/namestore/gnunet-namestore.c index 43cabed2a..7a671f1b6 100644 --- a/src/namestore/gnunet-namestore.c +++ b/src/namestore/gnunet-namestore.c @@ -342,6 +342,31 @@ del_continuation (void *cls, } +/** + * Function called when we are done with a zone iteration. + */ +static void +zone_iteration_finished (void *cls) +{ + list_it = NULL; + test_finished (); +} + + +/** + * Function called when we encountered an error in a zone iteration. + */ +static void +zone_iteration_error_cb (void *cls) +{ + list_it = NULL; + fprintf (stderr, + "Error iterating over zone\n"); + ret = 1; + test_finished (); +} + + /** * Process a record that was stored in the namestore. * @@ -365,12 +390,6 @@ display_record (void *cls, struct GNUNET_TIME_Absolute at; struct GNUNET_TIME_Relative rt; - if (NULL == rname) - { - list_it = NULL; - test_finished (); - return; - } if ( (NULL != name) && (0 != strcmp (name, rname)) ) { @@ -432,6 +451,31 @@ sync_cb (void *cls) } +/** + * Function called on errors while monitoring. + * + * @param cls NULL + */ +static void +monitor_error_cb (void *cls) +{ + FPRINTF (stderr, "%s", "Monitor disconnected and out of sync.\n"); +} + + +/** + * Function called if lookup fails. + */ +static void +lookup_error_cb (void *cls) +{ + add_qe = NULL; + GNUNET_break (0); + ret = 1; + test_finished (); +} + + /** * We're storing a record; this function is given the existing record * so that we can merge the information. @@ -454,8 +498,7 @@ get_existing_record (void *cls, unsigned int i; add_qe = NULL; - if ( (NULL != zone_key) && - (0 != strcmp (rec_name, name)) ) + if (0 != strcmp (rec_name, name)) { GNUNET_break (0); ret = 1; @@ -565,6 +608,19 @@ get_existing_record (void *cls, } +/** + * Function called if we encountered an error in zone-to-name. + */ +static void +reverse_error_cb (void *cls) +{ + reverse_qe = NULL; + FPRINTF (stdout, + "%s.zkey\n", + reverse_pkey); +} + + /** * Function called with the result of our attempt to obtain a name for a given * public key. @@ -595,6 +651,19 @@ handle_reverse_lookup (void *cls, } +/** + * Function called if lookup for deletion fails. + */ +static void +del_lookup_error_cb (void *cls) +{ + del_qe = NULL; + GNUNET_break (0); + ret = 1; + test_finished (); +} + + /** * We were asked to delete something; this function is called with * the existing records. Now we should determine what should be @@ -802,8 +871,13 @@ testservice_task (void *cls, ret = 1; return; } - add_qe = GNUNET_NAMESTORE_records_lookup (ns, &zone_pkey, name, - &get_existing_record, NULL ); + add_qe = GNUNET_NAMESTORE_records_lookup (ns, + &zone_pkey, + name, + &lookup_error_cb, + NULL, + &get_existing_record, + NULL); } if (del) { @@ -819,6 +893,8 @@ testservice_task (void *cls, del_qe = GNUNET_NAMESTORE_records_lookup (ns, &zone_pkey, name, + &del_lookup_error_cb, + NULL, &del_monitor, NULL); } @@ -826,7 +902,11 @@ testservice_task (void *cls, { list_it = GNUNET_NAMESTORE_zone_iteration_start (ns, &zone_pkey, + &zone_iteration_error_cb, + NULL, &display_record, + NULL, + &zone_iteration_finished, NULL); } if (NULL != reverse_pkey) @@ -846,6 +926,8 @@ testservice_task (void *cls, reverse_qe = GNUNET_NAMESTORE_zone_to_name (ns, &zone_pkey, &pubkey, + &reverse_error_cb, + NULL, &handle_reverse_lookup, NULL); } @@ -860,7 +942,10 @@ testservice_task (void *cls, "gnunet://gns/%52s/%63s", sh, sname)) ) || - (GNUNET_OK != GNUNET_CRYPTO_ecdsa_public_key_from_string (sh, strlen (sh), &pkey)) ) + (GNUNET_OK != + GNUNET_CRYPTO_ecdsa_public_key_from_string (sh, + strlen (sh), + &pkey)) ) { fprintf (stderr, _("Invalid URI `%s'\n"), @@ -912,7 +997,10 @@ testservice_task (void *cls, zm = GNUNET_NAMESTORE_zone_monitor_start (cfg, &zone_pkey, GNUNET_YES, + &monitor_error_cb, + NULL, &display_record, + NULL, &sync_cb, NULL); } diff --git a/src/namestore/namestore_api.c b/src/namestore/namestore_api.c index b8da654ce..61d8385b6 100644 --- a/src/namestore/namestore_api.c +++ b/src/namestore/namestore_api.c @@ -83,6 +83,16 @@ struct GNUNET_NAMESTORE_QueueEntry */ void *proc_cls; + /** + * Function to call on errors. + */ + GNUNET_SCHEDULER_TaskCallback error_cb; + + /** + * Closure for @e error_cb. + */ + void *error_cb_cls; + /** * Envelope of the message to send to the service, if not yet * sent. @@ -118,6 +128,16 @@ struct GNUNET_NAMESTORE_ZoneIterator */ struct GNUNET_NAMESTORE_Handle *h; + /** + * Function to call on completion. + */ + GNUNET_SCHEDULER_TaskCallback finish_cb; + + /** + * Closure for @e error_cb. + */ + void *finish_cb_cls; + /** * The continuation to call with the results */ @@ -128,6 +148,16 @@ struct GNUNET_NAMESTORE_ZoneIterator */ void *proc_cls; + /** + * Function to call on errors. + */ + GNUNET_SCHEDULER_TaskCallback error_cb; + + /** + * Closure for @e error_cb. + */ + void *error_cb_cls; + /** * Envelope of the message to send to the service, if not yet * sent. @@ -543,9 +573,11 @@ handle_record_result (void *cls, ntohl (msg->gns_header.r_id)); qe = find_qe (h, ntohl (msg->gns_header.r_id)); - if ( (NULL == ze) && (NULL == qe) ) + if ( (NULL == ze) && + (NULL == qe) ) return; /* rid not found */ - if ( (NULL != ze) && (NULL != qe) ) + if ( (NULL != ze) && + (NULL != qe) ) { GNUNET_break (0); /* rid ambigous */ force_reconnect (h); @@ -564,8 +596,8 @@ handle_record_result (void *cls, force_reconnect (h); return; } - if (NULL != ze->proc) - ze->proc (ze->proc_cls, NULL, NULL, 0, NULL); + if (NULL != ze->finish_cb) + ze->finish_cb (ze->finish_cb_cls); free_ze (ze); return; } @@ -706,7 +738,8 @@ handle_zone_to_name_response (void *cls, qe->proc (qe->proc_cls, &msg->zone, name_tmp, - rd_count, rd); + rd_count, + rd); /* return is important here: break would call continuation with error! */ free_qe (qe); return; @@ -717,8 +750,8 @@ handle_zone_to_name_response (void *cls, return; } /* error case, call continuation with error */ - if (NULL != qe->proc) - qe->proc (qe->proc_cls, NULL, NULL, 0, NULL); + if (NULL != qe->error_cb) + qe->error_cb (qe->error_cb_cls); free_qe (qe); } @@ -826,18 +859,18 @@ force_reconnect (struct GNUNET_NAMESTORE_Handle *h) h->mq = NULL; while (NULL != (ze = h->z_head)) { - /* FIXME: This does not allow clients to distinguish - iteration error from successful termination! */ - if (NULL != ze->proc) - ze->proc (ze->proc_cls, NULL, NULL, 0, NULL); + if (NULL != ze->error_cb) + ze->error_cb (ze->error_cb_cls); free_ze (ze); } while (NULL != (qe = h->op_head)) { - /* FIXME: This does not allow clients to distinguish - iteration error from successful termination! */ - if (NULL != qe->proc) - qe->proc (qe->proc_cls, NULL, NULL, 0, NULL); + if (NULL != qe->error_cb) + qe->error_cb (qe->error_cb_cls); + if (NULL != qe->cont) + qe->cont (qe->cont_cls, + GNUNET_SYSERR, + "failure in communication with namestore service"); free_qe (qe); } @@ -1058,6 +1091,8 @@ GNUNET_NAMESTORE_set_nick (struct GNUNET_NAMESTORE_Handle *h, * @param h handle to the namestore * @param pkey private key of the zone * @param label name that is being mapped (at most 255 characters long) + * @param error_cb function to call on error (i.e. disconnect) + * @param error_cb_cls closure for @a error_cb * @param rm function to call with the result (with 0 records if we don't have that label) * @param rm_cls closure for @a rm * @return handle to abort the request @@ -1066,6 +1101,8 @@ struct GNUNET_NAMESTORE_QueueEntry * GNUNET_NAMESTORE_records_lookup (struct GNUNET_NAMESTORE_Handle *h, const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey, const char *label, + GNUNET_SCHEDULER_TaskCallback error_cb, + void *error_cb_cls, GNUNET_NAMESTORE_RecordMonitor rm, void *rm_cls) { @@ -1082,6 +1119,8 @@ GNUNET_NAMESTORE_records_lookup (struct GNUNET_NAMESTORE_Handle *h, qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry); qe->h = h; + qe->error_cb = error_cb; + qe->error_cb_cls = error_cb_cls; qe->proc = rm; qe->proc_cls = rm_cls; qe->op_id = get_op_id(h); @@ -1114,6 +1153,8 @@ GNUNET_NAMESTORE_records_lookup (struct GNUNET_NAMESTORE_Handle *h, * @param h handle to the namestore * @param zone public key of the zone to look up in, never NULL * @param value_zone public key of the target zone (value), never NULL + * @param error_cb function to call on error (i.e. disconnect) + * @param error_cb_cls closure for @a error_cb * @param proc function to call on the matching records, or with * NULL (rd_count == 0) if there are no matching records * @param proc_cls closure for @a proc @@ -1124,6 +1165,8 @@ struct GNUNET_NAMESTORE_QueueEntry * GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone, + GNUNET_SCHEDULER_TaskCallback error_cb, + void *error_cb_cls, GNUNET_NAMESTORE_RecordMonitor proc, void *proc_cls) { @@ -1135,6 +1178,8 @@ GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h, rid = get_op_id(h); qe = GNUNET_new (struct GNUNET_NAMESTORE_QueueEntry); qe->h = h; + qe->error_cb = error_cb; + qe->error_cb_cls = error_cb_cls; qe->proc = proc; qe->proc_cls = proc_cls; qe->op_id = rid; @@ -1166,26 +1211,39 @@ GNUNET_NAMESTORE_zone_to_name (struct GNUNET_NAMESTORE_Handle *h, * * @param h handle to the namestore * @param zone zone to access, NULL for all zones + * @param error_cb function to call on error (i.e. disconnect) + * @param error_cb_cls closure for @a error_cb * @param proc function to call on each name from the zone; it * will be called repeatedly with a value (if available) - * and always once at the end with a name of NULL. * @param proc_cls closure for @a proc + * @param finish_cb function to call on completion + * @param finish_cb_cls closure for @a finish_cb * @return an iterator handle to use for iteration */ struct GNUNET_NAMESTORE_ZoneIterator * GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, + GNUNET_SCHEDULER_TaskCallback error_cb, + void *error_cb_cls, GNUNET_NAMESTORE_RecordMonitor proc, - void *proc_cls) + void *proc_cls, + GNUNET_SCHEDULER_TaskCallback finish_cb, + void *finish_cb_cls) { struct GNUNET_NAMESTORE_ZoneIterator *it; struct GNUNET_MQ_Envelope *env; struct ZoneIterationStartMessage *msg; uint32_t rid; + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Sending ZONE_ITERATION_START message\n"); rid = get_op_id (h); it = GNUNET_new (struct GNUNET_NAMESTORE_ZoneIterator); it->h = h; + it->error_cb = error_cb; + it->error_cb_cls = error_cb_cls; + it->finish_cb = finish_cb; + it->finish_cb_cls = finish_cb_cls; it->proc = proc; it->proc_cls = proc_cls; it->op_id = rid; diff --git a/src/namestore/namestore_api_monitor.c b/src/namestore/namestore_api_monitor.c index 85131f9cc..7e75b07e5 100644 --- a/src/namestore/namestore_api_monitor.c +++ b/src/namestore/namestore_api_monitor.c @@ -50,20 +50,35 @@ struct GNUNET_NAMESTORE_ZoneMonitor */ struct GNUNET_MQ_Handle *mq; + /** + * Function to call on errors. + */ + GNUNET_SCHEDULER_TaskCallback error_cb; + + /** + * Closure for @e error_cb. + */ + void *error_cb_cls; + /** * Function to call on events. */ GNUNET_NAMESTORE_RecordMonitor monitor; + /** + * Closure for @e monitor. + */ + void *monitor_cls; + /** * Function called when we've synchronized. */ - GNUNET_NAMESTORE_RecordsSynchronizedCallback sync_cb; + GNUNET_SCHEDULER_TaskCallback sync_cb; /** - * Closure for @e monitor and @e sync_cb. + * Closure for @e sync_cb. */ - void *cls; + void *sync_cb_cls; /** * Monitored zone. @@ -100,7 +115,7 @@ handle_sync (void *cls, struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls; if (NULL != zm->sync_cb) - zm->sync_cb (zm->cls); + zm->sync_cb (zm->sync_cb_cls); } @@ -193,7 +208,7 @@ handle_result (void *cls, rd_ser_tmp, rd_count, rd)); - zm->monitor (zm->cls, + zm->monitor (zm->monitor_cls, &lrm->private_key, name_tmp, rd_count, @@ -245,11 +260,7 @@ reconnect (struct GNUNET_NAMESTORE_ZoneMonitor *zm) if (NULL != zm->mq) { GNUNET_MQ_destroy (zm->mq); - zm->monitor (zm->cls, - NULL, - NULL, - 0, - NULL); + zm->error_cb (zm->error_cb_cls); } zm->mq = GNUNET_CLIENT_connecT (zm->cfg, "namestore", @@ -278,18 +289,28 @@ reconnect (struct GNUNET_NAMESTORE_ZoneMonitor *zm) * @param zone zone to monitor * @param iterate_first #GNUNET_YES to first iterate over all existing records, * #GNUNET_NO to only return changes that happen from now on + * @param error_cb function to call on error (i.e. disconnect); note that + * unlike the other error callbacks in this API, a call to this + * function does NOT destroy the monitor handle, it merely signals + * that monitoring is down. You need to still explicitly call + * #GNUNET_NAMESTORE_zone_monitor_stop(). + * @param error_cb_cls closure for @a error_cb * @param monitor function to call on zone changes + * @param monitor_cls closure for @a monitor * @param sync_cb function called when we're in sync with the namestore - * @param cls closure for @a monitor and @a sync_cb + * @param cls closure for @a sync_cb * @return handle to stop monitoring */ struct GNUNET_NAMESTORE_ZoneMonitor * GNUNET_NAMESTORE_zone_monitor_start (const struct GNUNET_CONFIGURATION_Handle *cfg, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, int iterate_first, + GNUNET_SCHEDULER_TaskCallback error_cb, + void *error_cb_cls, GNUNET_NAMESTORE_RecordMonitor monitor, - GNUNET_NAMESTORE_RecordsSynchronizedCallback sync_cb, - void *cls) + void *monitor_cls, + GNUNET_SCHEDULER_TaskCallback sync_cb, + void *sync_cb_cls) { struct GNUNET_NAMESTORE_ZoneMonitor *zm; @@ -297,9 +318,12 @@ GNUNET_NAMESTORE_zone_monitor_start (const struct GNUNET_CONFIGURATION_Handle *c if (NULL != zone) zm->zone = *zone; zm->iterate_first = iterate_first; + zm->error_cb = error_cb; + zm->error_cb_cls = error_cb_cls; zm->monitor = monitor; + zm->monitor_cls = monitor_cls; zm->sync_cb = sync_cb; - zm->cls = cls; + zm->sync_cb_cls = sync_cb_cls; zm->cfg = cfg; reconnect (zm); if (NULL == zm->mq) diff --git a/src/namestore/plugin_rest_namestore.c b/src/namestore/plugin_rest_namestore.c index 35d3595ce..58ab46ca9 100644 --- a/src/namestore/plugin_rest_namestore.c +++ b/src/namestore/plugin_rest_namestore.c @@ -387,6 +387,38 @@ cleanup_handle_delayed (void *cls) } +/** + * Iteration over all results finished, build final + * response. + * + * @param cls the `struct RequestHandle` + */ +static void +namestore_list_finished (void *cls) +{ + struct RequestHandle *handle = cls; + char *result; + struct MHD_Response *resp; + + handle->list_it = NULL; + if (GNUNET_SYSERR == + GNUNET_JSONAPI_document_serialize (handle->resp_object, + &result)) + { + do_error (handle); + return; + } + resp = GNUNET_REST_create_response (result); + handle->proc (handle->proc_cls, + resp, + MHD_HTTP_OK); + GNUNET_free_non_null (result); + GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, + handle); +} + + + /** * Create a response with requested records * @@ -401,31 +433,13 @@ namestore_list_response (void *cls, { struct RequestHandle *handle = cls; struct GNUNET_JSONAPI_Resource *json_resource; - struct MHD_Response *resp; json_t *result_array; json_t *record_obj; int i; - char *result; if (NULL == handle->resp_object) handle->resp_object = GNUNET_JSONAPI_document_new (); - if (NULL == rname) - { - handle->list_it = NULL; - //Handle response - if (GNUNET_SYSERR == GNUNET_JSONAPI_document_serialize (handle->resp_object, &result)) - { - GNUNET_SCHEDULER_add_now (&do_error, handle); - return; - } - resp = GNUNET_REST_create_response (result); - handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); - GNUNET_free_non_null (result); - GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle); - return; - } - if ( (NULL != handle->name) && (0 != strcmp (handle->name, rname)) ) { @@ -464,6 +478,7 @@ namestore_list_response (void *cls, GNUNET_NAMESTORE_zone_iterator_next (handle->list_it); } + static void create_finished (void *cls, int32_t success, const char *emsg) { @@ -506,11 +521,10 @@ create_new_record_cont (void *cls, struct RequestHandle *handle = cls; handle->add_qe = NULL; - if ( (NULL != zone_key) && - (0 != strcmp (rec_name, handle->name)) ) + if (0 != strcmp (rec_name, handle->name)) { GNUNET_break (0); - GNUNET_SCHEDULER_add_now (&do_error, handle); + do_error (handle); return; } @@ -533,6 +547,7 @@ create_new_record_cont (void *cls, handle); } + static void del_finished (void *cls, int32_t success, @@ -565,6 +580,7 @@ del_finished (void *cls, GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle); } + static void del_cont (void *cls, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, @@ -573,13 +589,14 @@ del_cont (void *cls, const struct GNUNET_GNSRECORD_Data *rd) { struct RequestHandle *handle = cls; + handle->add_qe = NULL; if (0 == rd_count) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("There are no records under label `%s' that could be deleted.\n"), label); - GNUNET_SCHEDULER_add_now (&do_error, handle); + do_error (handle); return; } @@ -591,6 +608,7 @@ del_cont (void *cls, handle); } + static void namestore_delete_cont (struct GNUNET_REST_RequestHandle *con, const char *url, @@ -607,10 +625,13 @@ namestore_delete_cont (struct GNUNET_REST_RequestHandle *con, handle->add_qe = GNUNET_NAMESTORE_records_lookup (handle->ns_handle, &handle->zone_pkey, handle->name, + &do_error, + handle, &del_cont, handle); } + static int json_to_gnsrecord (const json_t *records_json, struct GNUNET_GNSRECORD_Data **rd, @@ -713,6 +734,7 @@ json_to_gnsrecord (const json_t *records_json, return GNUNET_OK; } + static void namestore_create_cont (struct GNUNET_REST_RequestHandle *con, const char *url, @@ -730,7 +752,7 @@ namestore_create_cont (struct GNUNET_REST_RequestHandle *con, GNUNET_JSON_spec_jsonapi_document (&json_obj), GNUNET_JSON_spec_end() }; - + if (strlen (GNUNET_REST_API_NS_NAMESTORE) != strlen (handle->url)) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, @@ -805,9 +827,13 @@ namestore_create_cont (struct GNUNET_REST_RequestHandle *con, handle->add_qe = GNUNET_NAMESTORE_records_lookup (handle->ns_handle, &handle->zone_pkey, handle->name, - &create_new_record_cont, handle ); + &do_error, + handle, + &create_new_record_cont, + handle); } + static void namestore_zkey_response (void *cls, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, @@ -847,10 +873,9 @@ namestore_zkey_response (void *cls, GNUNET_JSONAPI_document_delete (json_obj); GNUNET_free (result); GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle); - return; - } + static void namestore_zkey_cont (struct GNUNET_REST_RequestHandle *con, const char *url, @@ -887,22 +912,31 @@ namestore_zkey_cont (struct GNUNET_REST_RequestHandle *con, handle->reverse_qe = GNUNET_NAMESTORE_zone_to_name (handle->ns_handle, &handle->zone_pkey, &pubkey, + &do_error, + handle, &namestore_zkey_response, handle); } + static void namestore_info_cont (struct GNUNET_REST_RequestHandle *con, const char *url, void *cls) { struct RequestHandle *handle = cls; + handle->list_it = GNUNET_NAMESTORE_zone_iteration_start (handle->ns_handle, &handle->zone_pkey, + &do_error, + handle, &namestore_list_response, + handle, + &namestore_list_finished, handle); } + static char* get_name_from_url (const char* url) { diff --git a/src/namestore/test_namestore_api.conf b/src/namestore/test_namestore_api.conf index 5db120ea7..a1a674d89 100644 --- a/src/namestore/test_namestore_api.conf +++ b/src/namestore/test_namestore_api.conf @@ -13,3 +13,6 @@ TEMPORARY_TABLE = YES [nse] WORKBITS = 0 + +[transport] +PLUGINS = diff --git a/src/namestore/test_namestore_api_lookup_nick.c b/src/namestore/test_namestore_api_lookup_nick.c index d6d3945b7..de958cee2 100644 --- a/src/namestore/test_namestore_api_lookup_nick.c +++ b/src/namestore/test_namestore_api_lookup_nick.c @@ -221,6 +221,13 @@ lookup_it (void *cls, } +static void +fail_cb (void *cls) +{ + GNUNET_assert (0); +} + + static void put_cont (void *cls, int32_t success, const char *emsg) { @@ -240,9 +247,16 @@ put_cont (void *cls, int32_t success, const char *emsg) return; } /* Lookup */ - nsqe = GNUNET_NAMESTORE_records_lookup (nsh, privkey, name, lookup_it, NULL); + nsqe = GNUNET_NAMESTORE_records_lookup (nsh, + privkey, + name, + &fail_cb, + NULL, + &lookup_it, + NULL); } + static void nick_cont (void *cls, int32_t success, const char *emsg) { diff --git a/src/namestore/test_namestore_api_lookup_private.c b/src/namestore/test_namestore_api_lookup_private.c index 92b2cad6f..57505c48b 100644 --- a/src/namestore/test_namestore_api_lookup_private.c +++ b/src/namestore/test_namestore_api_lookup_private.c @@ -103,7 +103,9 @@ lookup_it (void *cls, { nsqe = NULL; - if (0 != memcmp(privkey, zone, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) + if (0 != memcmp (privkey, + zone, + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) { GNUNET_break(0); GNUNET_SCHEDULER_cancel (endbadly_task); @@ -143,6 +145,13 @@ lookup_it (void *cls, } +static void +fail_cb (void *cls) +{ + GNUNET_assert (0); +} + + static void put_cont (void *cls, int32_t success, const char *emsg) { @@ -162,7 +171,13 @@ put_cont (void *cls, int32_t success, const char *emsg) return; } /* Lookup */ - nsqe = GNUNET_NAMESTORE_records_lookup (nsh, privkey, name, lookup_it, NULL); + nsqe = GNUNET_NAMESTORE_records_lookup (nsh, + privkey, + name, + &fail_cb, + NULL, + &lookup_it, + NULL); } diff --git a/src/namestore/test_namestore_api_monitoring.c b/src/namestore/test_namestore_api_monitoring.c index 2ea271d50..efbd6badf 100644 --- a/src/namestore/test_namestore_api_monitoring.c +++ b/src/namestore/test_namestore_api_monitoring.c @@ -215,11 +215,11 @@ put_cont (void *cls, int32_t success, const char *emsg) char *label = cls; if (0 == strcmp (label, s_name_1)) - ns_ops[0] = NULL; + ns_ops[0] = NULL; else if (0 == strcmp (label, s_name_2)) - ns_ops[1] = NULL; + ns_ops[1] = NULL; else if (0 == strcmp (label, s_name_3)) - ns_ops[2] = NULL; + ns_ops[2] = NULL; if (success == GNUNET_OK) { @@ -260,6 +260,20 @@ create_record (unsigned int count) } +static void +fail_cb (void *cls) +{ + GNUNET_assert (0); +} + + +static void +sync_cb (void *cls) +{ + /* do nothing */ +} + + static void run (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg, @@ -288,8 +302,11 @@ run (void *cls, zm = GNUNET_NAMESTORE_zone_monitor_start (cfg, privkey, GNUNET_YES, + &fail_cb, + NULL, &zone_proc, NULL, + &sync_cb, NULL); if (NULL == zm) { diff --git a/src/namestore/test_namestore_api_monitoring_existing.c b/src/namestore/test_namestore_api_monitoring_existing.c index eae10e2ae..cd1838b5c 100644 --- a/src/namestore/test_namestore_api_monitoring_existing.c +++ b/src/namestore/test_namestore_api_monitoring_existing.c @@ -59,6 +59,7 @@ struct GNUNET_NAMESTORE_QueueEntry * ns_ops[3]; static char *directory; + static void do_shutdown () { @@ -204,7 +205,20 @@ zone_proc (void *cls, else GNUNET_SCHEDULER_add_now (&end, NULL); } +} + +static void +fail_cb (void *cls) +{ + GNUNET_assert (0); +} + + +static void +sync_cb (void *cls) +{ + /* do nothing */ } @@ -240,8 +254,11 @@ put_cont (void *cls, int32_t success, const char *emsg) zm = GNUNET_NAMESTORE_zone_monitor_start (cfg, privkey, GNUNET_YES, + &fail_cb, + NULL, &zone_proc, NULL, + &sync_cb, NULL); if (NULL == zm) { diff --git a/src/namestore/test_namestore_api_zone_iteration.c b/src/namestore/test_namestore_api_zone_iteration.c index 070c06870..8960be55d 100644 --- a/src/namestore/test_namestore_api_zone_iteration.c +++ b/src/namestore/test_namestore_api_zone_iteration.c @@ -155,6 +155,32 @@ end (void *cls) } +static void +zone_end (void *cls) +{ + GNUNET_break (3 == returned_records); + if (3 == returned_records) + { + res = 0; /* Last iteraterator callback, we are done */ + zi = NULL; + } + else + res = 1; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Received last result, iteration done after receing %u results\n", + returned_records); + GNUNET_SCHEDULER_add_now (&end, NULL); +} + + +static void +fail_cb (void *cls) +{ + GNUNET_assert (0); +} + + static void zone_proc (void *cls, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, @@ -164,23 +190,6 @@ zone_proc (void *cls, { int failed = GNUNET_NO; - if ((zone == NULL) && (label == NULL)) - { - GNUNET_break (3 == returned_records); - if (3 == returned_records) - { - res = 0; /* Last iteraterator callback, we are done */ - zi = NULL; - } - else - res = 1; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Received last result, iteration done after receing %u results\n", - returned_records); - GNUNET_SCHEDULER_add_now (&end, NULL); - return; - } GNUNET_assert (NULL != zone); if (0 == memcmp (zone, privkey, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) { @@ -304,8 +313,12 @@ put_cont (void *cls, int32_t success, const char *emsg) returned_records = 0; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All records created, starting iteration over all zones \n"); zi = GNUNET_NAMESTORE_zone_iteration_start (nsh, + NULL, + &fail_cb, NULL, &zone_proc, + NULL, + &zone_end, NULL); if (zi == NULL) { @@ -352,8 +365,6 @@ empty_zone_proc (void *cls, unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd) { - char *hostkey_file; - GNUNET_assert (nsh == cls); if (NULL != zone) { @@ -375,18 +386,31 @@ empty_zone_proc (void *cls, endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL); return; } + GNUNET_assert (0); +} +static void +empty_zone_end (void *cls) +{ + char *hostkey_file; + zi = NULL; - GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR, - "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey"); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file); + GNUNET_asprintf (&hostkey_file, + "zonefiles%s%s", + DIR_SEPARATOR_STR, + "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Using zonekey file `%s' \n", + hostkey_file); privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file); GNUNET_free (hostkey_file); GNUNET_assert (privkey != NULL); - GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR, - "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey"); + GNUNET_asprintf (&hostkey_file, + "zonefiles%s%s", + DIR_SEPARATOR_STR, + "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey"); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file); privkey2 = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file); GNUNET_free (hostkey_file); @@ -396,7 +420,9 @@ empty_zone_proc (void *cls, GNUNET_asprintf(&s_name_1, "dummy1"); s_rd_1 = create_record(1); - GNUNET_NAMESTORE_records_store (nsh, privkey, s_name_1, + GNUNET_NAMESTORE_records_store (nsh, + privkey, + s_name_1, 1, s_rd_1, &put_cont, NULL); @@ -404,8 +430,12 @@ empty_zone_proc (void *cls, "Created record 2 \n"); GNUNET_asprintf(&s_name_2, "dummy2"); s_rd_2 = create_record(1); - GNUNET_NAMESTORE_records_store (nsh, privkey, s_name_2, - 1, s_rd_2, &put_cont, NULL); + GNUNET_NAMESTORE_records_store (nsh, + privkey, + s_name_2, + 1, s_rd_2, + &put_cont, + NULL); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 3\n"); @@ -436,7 +466,13 @@ run (void *cls, GNUNET_break (NULL != nsh); /* first, iterate over empty namestore */ zi = GNUNET_NAMESTORE_zone_iteration_start(nsh, - NULL, &empty_zone_proc, nsh); + NULL, + &fail_cb, + NULL, + &empty_zone_proc, + nsh, + &empty_zone_end, + NULL); if (NULL == zi) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n"); diff --git a/src/namestore/test_namestore_api_zone_iteration_nick.c b/src/namestore/test_namestore_api_zone_iteration_nick.c index 362533ef9..791702f97 100644 --- a/src/namestore/test_namestore_api_zone_iteration_nick.c +++ b/src/namestore/test_namestore_api_zone_iteration_nick.c @@ -198,6 +198,18 @@ check_zone_2 (const char *label, } +static void +zone_proc_end (void *cls) +{ + zi = NULL; + res = 0; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Received last result, iteration done after receing %u results\n", + returned_records); + GNUNET_SCHEDULER_add_now (&end, NULL); +} + + static void zone_proc (void *cls, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, @@ -206,16 +218,7 @@ zone_proc (void *cls, const struct GNUNET_GNSRECORD_Data *rd) { int failed = GNUNET_NO; - if ((zone == NULL) && (label == NULL)) - { - zi = NULL; - res = 0; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Received last result, iteration done after receing %u results\n", - returned_records); - GNUNET_SCHEDULER_add_now (&end, NULL); - return; - } + GNUNET_assert (NULL != zone); if (0 == memcmp (zone, privkey, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) { @@ -253,6 +256,13 @@ zone_proc (void *cls, } +static void +fail_cb (void *cls) +{ + GNUNET_assert (0); +} + + static void put_cont (void *cls, int32_t success, const char *emsg) { @@ -280,8 +290,12 @@ put_cont (void *cls, int32_t success, const char *emsg) returned_records = 0; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All records created, starting iteration over all zones \n"); zi = GNUNET_NAMESTORE_zone_iteration_start (nsh, + NULL, + &fail_cb, NULL, &zone_proc, + NULL, + &zone_proc_end, NULL); if (zi == NULL) { @@ -378,8 +392,8 @@ empty_zone_proc (void *cls, unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd) { - char *hostkey_file; GNUNET_assert (nsh == cls); + if (NULL != zone) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, @@ -400,6 +414,15 @@ empty_zone_proc (void *cls, endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL); return; } + GNUNET_assert (0); +} + + +static void +empty_zone_end (void *cls) +{ + char *hostkey_file; + GNUNET_assert (nsh == cls); zi = NULL; GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR, @@ -445,7 +468,13 @@ run (void *cls, /* first, iterate over empty namestore */ zi = GNUNET_NAMESTORE_zone_iteration_start(nsh, - NULL, &empty_zone_proc, nsh); + NULL, + &fail_cb, + NULL, + &empty_zone_proc, + nsh, + &empty_zone_end, + nsh); if (NULL == zi) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, diff --git a/src/namestore/test_namestore_api_zone_iteration_specific_zone.c b/src/namestore/test_namestore_api_zone_iteration_specific_zone.c index 1a0279f50..c5ae927b0 100644 --- a/src/namestore/test_namestore_api_zone_iteration_specific_zone.c +++ b/src/namestore/test_namestore_api_zone_iteration_specific_zone.c @@ -155,31 +155,22 @@ end (void *cls) } +static void +fail_cb (void *cls) +{ + GNUNET_assert (0); +} + + static void zone_proc (void *cls, - const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, - const char *label, - unsigned int rd_count, - const struct GNUNET_GNSRECORD_Data *rd) + const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, + const char *label, + unsigned int rd_count, + const struct GNUNET_GNSRECORD_Data *rd) { int failed = GNUNET_NO; - if ((zone == NULL) && (label == NULL)) - { - GNUNET_break (2 == returned_records); - if (2 == returned_records) - { - res = 0; /* Last iteraterator callback, we are done */ - zi = NULL; - } - else - res = 1; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Received last result, iteration done after receing %u results\n", - returned_records ); - GNUNET_SCHEDULER_add_now (&end, NULL); - return; - } GNUNET_assert (NULL != zone); if (0 == memcmp (zone, privkey, @@ -257,6 +248,25 @@ zone_proc (void *cls, } +static void +zone_proc_end (void *cls) +{ + GNUNET_break (2 == returned_records); + if (2 == returned_records) + { + res = 0; /* Last iteraterator callback, we are done */ + zi = NULL; + } + else + res = 1; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Received last result, iteration done after receing %u results\n", + returned_records); + GNUNET_SCHEDULER_add_now (&end, NULL); +} + + static void put_cont (void *cls, int32_t success, const char *emsg) { @@ -288,8 +298,12 @@ put_cont (void *cls, int32_t success, const char *emsg) "All records created, starting iteration over all zones \n"); zi = GNUNET_NAMESTORE_zone_iteration_start(nsh, privkey, + &fail_cb, + NULL, &zone_proc, - NULL); + NULL, + &zone_proc_end, + NULL); if (zi == NULL) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, @@ -336,42 +350,55 @@ empty_zone_proc (void *cls, unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd) { - char *hostkey_file; - GNUNET_assert (nsh == cls); if (NULL != zone) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Expected empty zone but received zone private key\n")); - GNUNET_break (0); - if (endbadly_task != NULL) - GNUNET_SCHEDULER_cancel (endbadly_task); - endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL); - return; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Expected empty zone but received zone private key\n")); + GNUNET_break (0); + if (endbadly_task != NULL) + GNUNET_SCHEDULER_cancel (endbadly_task); + endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL); + return; + } if ((NULL != label) || (NULL != rd) || (0 != rd_count)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Expected no zone content but received data\n")); - GNUNET_break (0); - if (endbadly_task != NULL) - GNUNET_SCHEDULER_cancel (endbadly_task); - endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL); - return; - } + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Expected no zone content but received data\n")); + GNUNET_break (0); + if (endbadly_task != NULL) + GNUNET_SCHEDULER_cancel (endbadly_task); + endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL); + return; + } + GNUNET_assert (0); +} +static void +empty_zone_proc_end (void *cls) +{ + char *hostkey_file; + zi = NULL; - GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR, - "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey"); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file); - privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file); + GNUNET_asprintf (&hostkey_file, + "zonefiles%s%s", + DIR_SEPARATOR_STR, + "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Using zonekey file `%s'\n", + hostkey_file); + privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file (hostkey_file); GNUNET_free (hostkey_file); GNUNET_assert (privkey != NULL); - GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR, - "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey"); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file); + GNUNET_asprintf(&hostkey_file, + "zonefiles%s%s", + DIR_SEPARATOR_STR, + "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Using zonekey file `%s' \n", + hostkey_file); privkey2 = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file); GNUNET_free (hostkey_file); GNUNET_assert (privkey2 != NULL); @@ -380,25 +407,39 @@ empty_zone_proc (void *cls, "Created record 1\n"); GNUNET_asprintf(&s_name_1, "dummy1"); - s_rd_1 = create_record(1); - GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_1, - 1, s_rd_1, &put_cont, NULL); + s_rd_1 = create_record (1); + GNUNET_NAMESTORE_records_store (nsh, + privkey, + s_name_1, + 1, + s_rd_1, + &put_cont, + NULL); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n"); GNUNET_asprintf(&s_name_2, "dummy2"); - s_rd_2 = create_record(1); - GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_2, - 1, s_rd_2, &put_cont, NULL); + s_rd_2 = create_record (1); + GNUNET_NAMESTORE_records_store (nsh, + privkey, + s_name_2, + 1, + s_rd_2, + &put_cont, + NULL); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 3\n"); /* name in different zone */ GNUNET_asprintf(&s_name_3, "dummy3"); - s_rd_3 = create_record(1); - GNUNET_NAMESTORE_records_store(nsh, privkey2, s_name_3, - 1, s_rd_3, &put_cont, NULL); + s_rd_3 = create_record (1); + GNUNET_NAMESTORE_records_store (nsh, + privkey2, + s_name_3, + 1, s_rd_3, + &put_cont, + NULL); } @@ -423,7 +464,12 @@ run (void *cls, /* first, iterate over empty namestore */ zi = GNUNET_NAMESTORE_zone_iteration_start (nsh, NULL, - &empty_zone_proc, nsh); + &fail_cb, + NULL, + &empty_zone_proc, + nsh, + &empty_zone_proc_end, + nsh); if (NULL == zi) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, diff --git a/src/namestore/test_namestore_api_zone_iteration_stop.c b/src/namestore/test_namestore_api_zone_iteration_stop.c index 36f527da0..a5f040150 100644 --- a/src/namestore/test_namestore_api_zone_iteration_stop.c +++ b/src/namestore/test_namestore_api_zone_iteration_stop.c @@ -155,6 +155,13 @@ end (void *cls) } +static void +fail_cb (void *cls) +{ + GNUNET_assert (0); +} + + static void zone_proc (void *cls, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, @@ -163,20 +170,7 @@ zone_proc (void *cls, const struct GNUNET_GNSRECORD_Data *rd) { int failed = GNUNET_NO; - if ((zone == NULL) && (label == NULL)) - { - GNUNET_break (1 <= returned_records); - if (1 >= returned_records) - res = 1; /* Last iteraterator callback, we are done */ - else - res = 0; - zi = NULL; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Received last result, iteration done after receing %u results\n", - returned_records ); - GNUNET_SCHEDULER_add_now (&end, NULL); - return; - } + GNUNET_assert (NULL != zone); if (0 == memcmp (zone, privkey, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) { @@ -287,6 +281,22 @@ zone_proc (void *cls, } +static void +zone_proc_end (void *cls) +{ + GNUNET_break (1 <= returned_records); + if (1 >= returned_records) + res = 1; /* Last iteraterator callback, we are done */ + else + res = 0; + zi = NULL; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Received last result, iteration done after receing %u results\n", + returned_records); + GNUNET_SCHEDULER_add_now (&end, NULL); +} + + static void put_cont (void *cls, int32_t success, const char *emsg) { @@ -312,14 +322,20 @@ put_cont (void *cls, int32_t success, const char *emsg) { res = 1; returned_records = 0; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All records created, starting iteration over all zones \n"); - zi = GNUNET_NAMESTORE_zone_iteration_start(nsh, - NULL, - &zone_proc, - NULL); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "All records created, starting iteration over all zones \n"); + zi = GNUNET_NAMESTORE_zone_iteration_start (nsh, + NULL, + &fail_cb, + NULL, + &zone_proc, + NULL, + &zone_proc_end, + NULL); if (zi == NULL) { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n"); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Failed to create zone iterator\n"); GNUNET_break (0); if (NULL != endbadly_task) GNUNET_SCHEDULER_cancel (endbadly_task); @@ -362,7 +378,6 @@ empty_zone_proc (void *cls, unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd) { - char *hostkey_file; GNUNET_assert (nsh == cls); if (NULL != zone) @@ -385,42 +400,72 @@ empty_zone_proc (void *cls, endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL); return; } + GNUNET_assert (0); +} + +static void +empty_zone_proc_end (void *cls) +{ + char *hostkey_file; + + GNUNET_assert (nsh == cls); zi = NULL; - GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR, - "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey"); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file); + GNUNET_asprintf(&hostkey_file, + "zonefiles%s%s", + DIR_SEPARATOR_STR, + "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Using zonekey file `%s' \n", + hostkey_file); privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file); GNUNET_free (hostkey_file); GNUNET_assert (privkey != NULL); - GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR, - "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey"); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file); + GNUNET_asprintf (&hostkey_file, + "zonefiles%s%s", + DIR_SEPARATOR_STR, + "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Using zonekey file `%s'\n", + hostkey_file); privkey2 = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file); GNUNET_free (hostkey_file); GNUNET_assert (privkey2 != NULL); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Created record 1\n"); - GNUNET_asprintf(&s_name_1, "dummy1"); + GNUNET_asprintf(&s_name_1, + "dummy1"); s_rd_1 = create_record(1); GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_1, 1, s_rd_1, &put_cont, NULL); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n"); - GNUNET_asprintf(&s_name_2, "dummy2"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Created record 2 \n"); + GNUNET_asprintf(&s_name_2, + "dummy2"); s_rd_2 = create_record(1); - GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_2, - 1, s_rd_2, &put_cont, NULL); + GNUNET_NAMESTORE_records_store (nsh, + privkey, + s_name_2, + 1, + s_rd_2, + &put_cont, NULL); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 3\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Created record 3\n"); /* name in different zone */ GNUNET_asprintf(&s_name_3, "dummy3"); s_rd_3 = create_record(1); - GNUNET_NAMESTORE_records_store(nsh, privkey2, s_name_3, - 1, s_rd_3, &put_cont, NULL); + GNUNET_NAMESTORE_records_store(nsh, + privkey2, + s_name_3, + 1, + s_rd_3, + &put_cont, NULL); } @@ -441,7 +486,13 @@ run (void *cls, GNUNET_break (NULL != nsh); /* first, iterate over empty namestore */ zi = GNUNET_NAMESTORE_zone_iteration_start(nsh, - NULL, &empty_zone_proc, nsh); + NULL, + &fail_cb, + NULL, + &empty_zone_proc, + nsh, + &empty_zone_proc_end, + nsh); if (NULL == zi) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, -- 2.25.1