diff options
author | Özgür Kesim <oec-taler@kesim.org> | 2022-04-21 12:54:59 +0200 |
---|---|---|
committer | Özgür Kesim <oec-taler@kesim.org> | 2022-04-21 12:54:59 +0200 |
commit | 137bd971544f3b0332af32e68ae37fb9e74bae69 (patch) | |
tree | dddda27b440a775e289a419cc3ead054a095c816 /src/extensions | |
parent | 4af1772f1205de93cbf04e20c6dd2d309c7fe0df (diff) |
cleanup of extension API
- removed TALER_extensions_init()
- added TALER_extension_age_restriction_register()
Diffstat (limited to 'src/extensions')
-rw-r--r-- | src/extensions/extension_age_restriction.c | 47 | ||||
-rw-r--r-- | src/extensions/extensions.c | 136 |
2 files changed, 89 insertions, 94 deletions
diff --git a/src/extensions/extension_age_restriction.c b/src/extensions/extension_age_restriction.c index fd883ae4a..fb0146b88 100644 --- a/src/extensions/extension_age_restriction.c +++ b/src/extensions/extension_age_restriction.c @@ -35,7 +35,7 @@ struct age_restriction_config /** * Global config for this extension */ -static struct age_restriction_config _config = {0}; +static struct age_restriction_config TE_age_restriction_config = {0}; /** * @param groups String representation of the age groups. Must be of the form @@ -141,7 +141,6 @@ TALER_age_mask_to_string ( * ================================================== */ - /** * @brief implements the TALER_Extension.disable interface. */ @@ -160,8 +159,8 @@ age_restriction_disable ( this->config_json = NULL; } - _config.mask.bits = 0; - _config.num_groups = 0; + TE_age_restriction_config.mask.bits = 0; + TE_age_restriction_config.num_groups = 0; } @@ -227,13 +226,14 @@ age_restriction_load_taler_config ( GNUNET_log (GNUNET_ERROR_TYPE_INFO, "setting age mask to %x with #groups: %d\n", mask.bits, __builtin_popcount (mask.bits) - 1); - _config.mask.bits = mask.bits; - _config.num_groups = __builtin_popcount (mask.bits) - 1; /* no underflow, first bit always set */ - this->config = &_config; - - /* Note: we do now have _config set, however this->config_json is NOT set, - * i.e. the extension is not yet active! For age restriction to become - * active, load_json_config must have been called. */ + TE_age_restriction_config.mask.bits = mask.bits; + TE_age_restriction_config.num_groups = __builtin_popcount (mask.bits) - 1; /* no underflow, first bit always set */ + this->config = &TE_age_restriction_config; + + /* Note: we do now have TE_age_restriction_config set, however + * this->config_json is NOT set, i.e. the extension is not yet active! For + * age restriction to become active, load_json_config must have been + * called. */ } @@ -266,8 +266,8 @@ age_restriction_load_json_config ( if (TALER_Extension_AgeRestriction != this->type) return GNUNET_SYSERR; - _config.mask.bits = mask.bits; - _config.num_groups = 0; + TE_age_restriction_config.mask.bits = mask.bits; + TE_age_restriction_config.num_groups = 0; if (mask.bits > 0) { @@ -275,10 +275,10 @@ age_restriction_load_json_config ( if (0 == (mask.bits & 1)) return GNUNET_SYSERR; - _config.num_groups = __builtin_popcount (mask.bits) - 1; + TE_age_restriction_config.num_groups = __builtin_popcount (mask.bits) - 1; } - this->config = &_config; + this->config = &TE_age_restriction_config; if (NULL != this->config_json) json_decref (this->config_json); @@ -313,7 +313,7 @@ age_restriction_config_to_json ( return json_copy (this->config_json); } - mask_str = TALER_age_mask_to_string (&_config.mask); + mask_str = TALER_age_mask_to_string (&TE_age_restriction_config.mask); conf = GNUNET_JSON_PACK ( GNUNET_JSON_pack_string ("age_groups", mask_str) ); @@ -340,7 +340,7 @@ age_restriction_test_json_config ( /* The extension for age restriction */ -struct TALER_Extension _extension_age_restriction = { +struct TALER_Extension TE_age_restriction = { .next = NULL, .type = TALER_Extension_AgeRestriction, .name = "age_restriction", @@ -355,24 +355,31 @@ struct TALER_Extension _extension_age_restriction = { .load_taler_config = &age_restriction_load_taler_config, }; +enum GNUNET_GenericReturnValue +TALER_extension_age_restriction_register () +{ + return TALER_extensions_add (&TE_age_restriction); +} + + bool TALER_extensions_age_restriction_is_configured () { - return (0 != _config.mask.bits); + return (0 != TE_age_restriction_config.mask.bits); } struct TALER_AgeMask TALER_extensions_age_restriction_ageMask () { - return _config.mask; + return TE_age_restriction_config.mask; } size_t TALER_extensions_age_restriction_num_groups () { - return _config.num_groups; + return TE_age_restriction_config.num_groups; } diff --git a/src/extensions/extensions.c b/src/extensions/extensions.c index 0b95e1862..55a7dcd81 100644 --- a/src/extensions/extensions.c +++ b/src/extensions/extensions.c @@ -26,77 +26,58 @@ /* head of the list of all registered extensions */ -// FIXME: remove unnecessary initializers. -// FIXME: remove unncessary "_" prefix. -static struct TALER_Extension *_extensions = NULL; -static bool _initialized = false; - - -void -TALER_extensions_init () -{ - // FIXME: a bit ugly. Why not have the age_restriction - // module have an initializer that registers itself here? - extern struct TALER_Extension _extension_age_restriction; - if (! _initialized) - _extensions = &_extension_age_restriction; - - _initialized = true; -} +static struct TALER_Extension *TE_extensions = NULL; const struct TALER_Extension * TALER_extensions_get_head () { - return _extensions; + return TE_extensions; } -// FIXME: 'new' is a C++ keyword, to NOT use for variable names enum GNUNET_GenericReturnValue TALER_extensions_add ( - const struct TALER_Extension *new) + const struct TALER_Extension *extension) { - struct TALER_Extension *ext; // FIXME: limit scope to for() loop - - if (_initialized) - return GNUNET_SYSERR; - - GNUNET_assert (NULL != _extensions); - /* Sanity checks */ - // FIXME: bracket all expressions - if (NULL == new || - NULL == new->name || - NULL == new->version || - NULL == new->disable || - NULL == new->test_json_config || - NULL == new->load_json_config || - NULL == new->config_to_json || - NULL == new->load_taler_config || - NULL == new->next) + if ((NULL == extension) || + (NULL == extension->name) || + (NULL == extension->version) || + (NULL == extension->disable) || + (NULL == extension->test_json_config) || + (NULL == extension->load_json_config) || + (NULL == extension->config_to_json) || + (NULL == extension->load_taler_config)) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "invalid extension\n"); return GNUNET_SYSERR; } - /* Check for collisions */ - for (ext = _extensions; NULL != ext; ext = ext->next) + if (NULL == TE_extensions) /* first extension ?*/ + TE_extensions = (struct TALER_Extension *) extension; + else { - if (new->type == ext->type || - 0 == strcmp (new->name, - ext->name)) + struct TALER_Extension *iter; + + /* Check for collisions */ + for (iter = TE_extensions; NULL != iter; iter = iter->next) { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "extension collision for `%s'\n", - new->name); - return GNUNET_NO; + if (extension->type == iter->type || + 0 == strcasecmp (extension->name, + iter->name)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "extension collision for `%s'\n", + extension->name); + return GNUNET_NO; + } } - } - /* No collisions found, so add this extension to the list */ - ext->next = (struct TALER_Extension *) new; + /* No collisions found, so add this extension to the list */ + iter->next = (struct TALER_Extension *) extension; + } return GNUNET_OK; } @@ -106,7 +87,7 @@ const struct TALER_Extension * TALER_extensions_get_by_type ( enum TALER_Extension_Type type) { - for (const struct TALER_Extension *it = _extensions; + for (const struct TALER_Extension *it = TE_extensions; NULL != it; it = it->next) { @@ -135,7 +116,7 @@ const struct TALER_Extension * TALER_extensions_get_by_name ( const char *name) { - for (const struct TALER_Extension *it = _extensions; + for (const struct TALER_Extension *it = TE_extensions; NULL != it; it = it->next) { @@ -169,37 +150,46 @@ TALER_extensions_verify_json_config_signature ( } -// FIXME: use CamelCase to follow conventions -// FIXME: document struct and members -struct load_conf_closure +/* + * Closure used in TALER_extensions_load_taler_config during call to + * GNUNET_CONFIGURATION_iterate_sections with configure_extension. + */ +struct LoadConfClosure { const struct GNUNET_CONFIGURATION_Handle *cfg; enum GNUNET_GenericReturnValue error; }; -// FIXME: document +/* + * Used in TALER_extensions_load_taler_config during call to + * GNUNET_CONFIGURATION_iterate_sections to load the configuration + * of supported extensions. + * + * @param cls Closure of type LoadConfClosure + * @param section name of the current section + */ static void -collect_extensions ( +configure_extension ( void *cls, const char *section) { - struct load_conf_closure *col = cls; + struct LoadConfClosure *col = cls; const char *name; const struct TALER_Extension *extension; if (GNUNET_OK != col->error) return; + if (0 != strncasecmp (section, TALER_EXTENSION_SECTION_PREFIX, sizeof(TALER_EXTENSION_SECTION_PREFIX) - 1)) - { return; - } name = section + sizeof(TALER_EXTENSION_SECTION_PREFIX) - 1; - if (NULL == (extension = TALER_extensions_get_by_name (name))) + if (NULL == + (extension = TALER_extensions_get_by_name (name))) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unsupported extension `%s` (section [%s]).\n", name, @@ -227,13 +217,13 @@ enum GNUNET_GenericReturnValue TALER_extensions_load_taler_config ( const struct GNUNET_CONFIGURATION_Handle *cfg) { - struct load_conf_closure col = { + struct LoadConfClosure col = { .cfg = cfg, .error = GNUNET_OK, }; GNUNET_CONFIGURATION_iterate_sections (cfg, - &collect_extensions, + &configure_extension, &col); return col.error; } @@ -258,19 +248,17 @@ TALER_extensions_is_json_config ( GNUNET_JSON_spec_end () }; - ret = GNUNET_JSON_parse (obj, - spec, - NULL, - NULL); - // FIXME: convention says, 'true' path is for - // error handling. - if (GNUNET_OK == ret) - { - *config = json_copy (cfg); - GNUNET_JSON_parse_free (spec); - } + if (GNUNET_OK != + (ret = GNUNET_JSON_parse (obj, + spec, + NULL, + NULL))) + return ret; + + *config = json_copy (cfg); + GNUNET_JSON_parse_free (spec); - return ret; + return GNUNET_OK; } |