From 7d9a40327587ed99fb20f4c4a5669069c7a51e48 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Fri, 20 Mar 2015 23:51:28 +0100 Subject: first stab at establishing proper plugin API, main HTTP code compiles, other binaries FTBFS right now --- src/mint/plugin.c | 183 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 src/mint/plugin.c (limited to 'src/mint/plugin.c') diff --git a/src/mint/plugin.c b/src/mint/plugin.c new file mode 100644 index 00000000..91cd3f40 --- /dev/null +++ b/src/mint/plugin.c @@ -0,0 +1,183 @@ +/* + This file is part of TALER + Copyright (C) 2015 Christian Grothoff (and other contributing authors) + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, If not, see +*/ +/** + * @file mint/plugin.c + * @brief Logic to load database plugin + * @author Christian Grothoff + */ +#include "platform.h" +#include "plugin.h" +#include + + +/** + * Global variable with the plugin (once loaded). + */ +struct TALER_MINTDB_Plugin *plugin; + +/** + * Libtool search path before we started. + */ +static char *old_dlsearchpath; + + +/** + * Initialize the plugin. + * + * @param cfg configuration to use + * @return #GNUNET_OK on success + */ +int +TALER_MINT_plugin_load (struct GNUNET_CONFIGURATION_Handle *cfg) +{ + return GNUNET_SYSERR; +} + + +/** + * Shutdown the plugin. + */ +void +TALER_MINT_plugin_unload () +{ + if (NULL == plugin) + return; +} + + +/** + * Setup libtool paths. + */ +void __attribute__ ((constructor)) +plugin_init () +{ + int err; + const char *opath; + char *path; + char *cpath; + + err = lt_dlinit (); + if (err > 0) + { + FPRINTF (stderr, + _("Initialization of plugin mechanism failed: %s!\n"), + lt_dlerror ()); + return; + } + opath = lt_dlgetsearchpath (); + if (NULL != opath) + old_dlsearchpath = GNUNET_strdup (opath); + path = TALER_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR); + if (NULL != path) + { + if (NULL != opath) + { + GNUNET_asprintf (&cpath, "%s:%s", opath, path); + lt_dlsetsearchpath (cpath); + GNUNET_free (path); + GNUNET_free (cpath); + } + else + { + lt_dlsetsearchpath (path); + GNUNET_free (path); + } + } +} + + +/** + * Shutdown libtool. + */ +void __attribute__ ((destructor)) +plugin_fini () +{ + lt_dlsetsearchpath (old_dlsearchpath); + if (NULL != old_dlsearchpath) + { + GNUNET_free (old_dlsearchpath); + old_dlsearchpath = NULL; + } + lt_dlexit (); +} + + +// FIXME: decide if we should keep these in each plugin, here +// or yet again somewhere else entirely (plugin_common.c?) + +/** + * Free memory associated with the given reserve history. + * + * @param rh history to free. + */ +void +TALER_MINT_DB_free_reserve_history (struct ReserveHistory *rh) +{ + struct BankTransfer *bt; + struct CollectableBlindcoin *cbc; + struct ReserveHistory *backref; + + while (NULL != rh) + { + switch(rh->type) + { + case TALER_MINT_DB_RO_BANK_TO_MINT: + bt = rh->details.bank; + if (NULL != bt->wire) + json_decref ((json_t *) bt->wire); /* FIXME: avoid cast? */ + GNUNET_free (bt); + break; + case TALER_MINT_DB_RO_WITHDRAW_COIN: + cbc = rh->details.withdraw; + GNUNET_CRYPTO_rsa_signature_free (cbc->sig); + GNUNET_CRYPTO_rsa_public_key_free (cbc->denom_pub); + GNUNET_free (cbc); + break; + } + backref = rh; + rh = rh->next; + GNUNET_free (backref); + } +} + + +/** + * Free memory of the link data list. + * + * @param ldl link data list to release + */ +void +TALER_db_link_data_list_free (struct LinkDataList *ldl) +{ + GNUNET_break (0); // FIXME +} + + +/** + * Free linked list of transactions. + * + * @param list list to free + */ +void +TALER_MINT_DB_free_coin_transaction_list (struct TALER_MINT_DB_TransactionList *list) +{ + // FIXME: check logic! + GNUNET_break (0); +} + + + +/* end of plugin.c */ -- cgit v1.2.3 From f7025fd6303b754f601bccf0c01272cf35e0b991 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sat, 21 Mar 2015 14:21:00 +0100 Subject: fix testcase FTBFS --- src/mint/plugin.c | 2 +- src/mint/plugin.h | 2 +- src/mint/plugin_mintdb_postgres.c | 10 +- src/mint/taler-mint-httpd.h | 1 + src/mint/test_mint_db.c | 189 +++++++++++++++++++++++++------------- src/mint/test_mint_deposits.c | 94 +++++-------------- 6 files changed, 157 insertions(+), 141 deletions(-) (limited to 'src/mint/plugin.c') diff --git a/src/mint/plugin.c b/src/mint/plugin.c index 91cd3f40..67cabd81 100644 --- a/src/mint/plugin.c +++ b/src/mint/plugin.c @@ -41,7 +41,7 @@ static char *old_dlsearchpath; * @return #GNUNET_OK on success */ int -TALER_MINT_plugin_load (struct GNUNET_CONFIGURATION_Handle *cfg) +TALER_MINT_plugin_load (const struct GNUNET_CONFIGURATION_Handle *cfg) { return GNUNET_SYSERR; } diff --git a/src/mint/plugin.h b/src/mint/plugin.h index 01b99ebc..bb1f0ecb 100644 --- a/src/mint/plugin.h +++ b/src/mint/plugin.h @@ -37,7 +37,7 @@ extern struct TALER_MINTDB_Plugin *plugin; * @return #GNUNET_OK on success */ int -TALER_MINT_plugin_load (struct GNUNET_CONFIGURATION_Handle *cfg); +TALER_MINT_plugin_load (const struct GNUNET_CONFIGURATION_Handle *cfg); /** diff --git a/src/mint/plugin_mintdb_postgres.c b/src/mint/plugin_mintdb_postgres.c index 8935fe03..078e6e1b 100644 --- a/src/mint/plugin_mintdb_postgres.c +++ b/src/mint/plugin_mintdb_postgres.c @@ -148,14 +148,15 @@ postgres_drop_temporary (void *cls, /** * Create the necessary tables if they are not present * - * @param pc our overall context + * @param cls the `struct PostgresClosure` with the plugin-specific state * @param temporary should we use a temporary schema * @return #GNUNET_OK upon success; #GNUNET_SYSERR upon failure */ static int -postgres_create_tables (struct PostgresClosure *pc, +postgres_create_tables (void *cls, int temporary) { + struct PostgresClosure *pc = cls; PGresult *result; PGconn *conn; @@ -168,8 +169,8 @@ postgres_create_tables (struct PostgresClosure *pc, GNUNET_break (0); return GNUNET_SYSERR; } - if ((GNUNET_YES == temporary) - && (GNUNET_SYSERR == set_temporary_schema (conn))) + if ( (GNUNET_YES == temporary) && + (GNUNET_SYSERR == set_temporary_schema (conn))) { PQfinish (conn); return GNUNET_SYSERR; @@ -287,6 +288,7 @@ postgres_create_tables (struct PostgresClosure *pc, ",wire TEXT NOT NULL" ")"); #undef SQLEXEC + PQfinish (conn); return GNUNET_OK; diff --git a/src/mint/taler-mint-httpd.h b/src/mint/taler-mint-httpd.h index a86b06e4..36d150bb 100644 --- a/src/mint/taler-mint-httpd.h +++ b/src/mint/taler-mint-httpd.h @@ -23,6 +23,7 @@ #ifndef TALER_MINT_HTTPD_H #define TALER_MINT_HTTPD_H +#include /** * Cut-and-choose size for refreshing. diff --git a/src/mint/test_mint_db.c b/src/mint/test_mint_db.c index 2bb25aa6..750303a0 100644 --- a/src/mint/test_mint_db.c +++ b/src/mint/test_mint_db.c @@ -13,15 +13,13 @@ You should have received a copy of the GNU General Public License along with TALER; see the file COPYING. If not, If not, see */ - /** * @file mint/test_mint_db.c * @brief test cases for DB interaction functions * @author Sree Harsha Totakura */ - #include "platform.h" -#include "mint_db.h" +#include "plugin.h" static int result; @@ -45,7 +43,7 @@ static int result; /** * Checks if the given reserve has the given amount of balance and expiry * - * @param db the database connection + * @param session the database connection * @param pub the public key of the reserve * @param value balance value * @param fraction balance fraction @@ -54,16 +52,21 @@ static int result; * @return #GNUNET_OK if the given reserve has the same balance and expiration * as the given parameters; #GNUNET_SYSERR if not */ -int -check_reserve (PGconn *db, +static int +check_reserve (struct TALER_MINTDB_Session *session, struct GNUNET_CRYPTO_EddsaPublicKey *pub, - uint32_t value, uint32_t fraction, const char *currency, + uint32_t value, + uint32_t fraction, + const char *currency, uint64_t expiry) { struct Reserve reserve; reserve.pub = pub; - FAILIF (GNUNET_OK != TALER_MINT_DB_reserve_get (db, &reserve)); + FAILIF (GNUNET_OK != + plugin->reserve_get (plugin->cls, + session, + &reserve)); FAILIF (value != reserve.balance.value); FAILIF (fraction != reserve.balance.fraction); FAILIF (0 != strcmp (currency, reserve.balance.currency)); @@ -81,7 +84,8 @@ struct DenomKeyPair struct GNUNET_CRYPTO_rsa_PublicKey *pub; }; -struct DenomKeyPair * + +static struct DenomKeyPair * create_denom_key_pair (unsigned int size) { struct DenomKeyPair *dkp; @@ -93,6 +97,7 @@ create_denom_key_pair (unsigned int size) return dkp; } + static void destroy_denon_key_pair (struct DenomKeyPair *dkp) { @@ -107,13 +112,15 @@ destroy_denon_key_pair (struct DenomKeyPair *dkp) * @param cls closure * @param args remaining command-line arguments * @param cfgfile name of the configuration file used (for saving, can be NULL!) - * @param config configuration + * @param cfg configuration */ static void -run (void *cls, char *const *args, const char *cfgfile, - const struct GNUNET_CONFIGURATION_Handle *config) +run (void *cls, + char *const *args, + const char *cfgfile, + const struct GNUNET_CONFIGURATION_Handle *cfg) { - PGconn *db; + struct TALER_MINTDB_Session *session; struct GNUNET_CRYPTO_EddsaPublicKey reserve_pub; struct Reserve reserve; struct GNUNET_TIME_Absolute expiry; @@ -139,23 +146,27 @@ run (void *cls, char *const *args, const char *cfgfile, \"address\": \"foobar\"}"; unsigned int cnt; - db = NULL; dkp = NULL; rh = NULL; wire = NULL; ZR_BLK (&cbc); ZR_BLK (&cbc2); - if (GNUNET_OK != TALER_MINT_DB_init ("postgres:///taler")) + if (GNUNET_OK != + TALER_MINT_plugin_load (cfg)) { result = 1; return; } - if (GNUNET_OK != TALER_MINT_DB_create_tables (GNUNET_YES)) + if (GNUNET_OK != + plugin->create_tables (plugin->cls, + GNUNET_YES)) { result = 2; goto drop; } - if (NULL == (db = TALER_MINT_DB_get_connection(GNUNET_YES))) + if (NULL == + (session = plugin->get_session (plugin->cls, + GNUNET_YES))) { result = 3; goto drop; @@ -168,60 +179,85 @@ run (void *cls, char *const *args, const char *cfgfile, expiry = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), GNUNET_TIME_UNIT_HOURS); result = 4; - FAILIF (GNUNET_OK != TALER_MINT_DB_reserves_in_insert (db, - &reserve, - &amount, - expiry)); - FAILIF (GNUNET_OK != check_reserve (db, - &reserve_pub, - amount.value, - amount.fraction, - amount.currency, - expiry.abs_value_us)); - FAILIF (GNUNET_OK != TALER_MINT_DB_reserves_in_insert (db, - &reserve, - &amount, - expiry)); - FAILIF (GNUNET_OK != check_reserve (db, - &reserve_pub, - ++amount.value, - ++amount.fraction, - amount.currency, - expiry.abs_value_us)); + FAILIF (GNUNET_OK != + plugin->reserves_in_insert (plugin->cls, + session, + &reserve, + &amount, + expiry)); + FAILIF (GNUNET_OK != + check_reserve (session, + &reserve_pub, + amount.value, + amount.fraction, + amount.currency, + expiry.abs_value_us)); + FAILIF (GNUNET_OK != + plugin->reserves_in_insert (plugin->cls, + session, + &reserve, + &amount, + expiry)); + FAILIF (GNUNET_OK != + check_reserve (session, + &reserve_pub, + ++amount.value, + ++amount.fraction, + amount.currency, + expiry.abs_value_us)); dkp = create_denom_key_pair (1024); RND_BLK(&h_blind); RND_BLK(&cbc.reserve_sig); cbc.denom_pub = dkp->pub; cbc.sig = GNUNET_CRYPTO_rsa_sign (dkp->priv, &h_blind, sizeof (h_blind)); - (void) memcpy (&cbc.reserve_pub, &reserve_pub, sizeof (reserve_pub)); + (void) memcpy (&cbc.reserve_pub, + &reserve_pub, + sizeof (reserve_pub)); amount.value--; amount.fraction--; - FAILIF (GNUNET_OK != TALER_MINT_DB_insert_collectable_blindcoin (db, - &h_blind, - amount, - &cbc)); - FAILIF (GNUNET_OK != check_reserve (db, - &reserve_pub, - amount.value, - amount.fraction, - amount.currency, - expiry.abs_value_us)); - FAILIF (GNUNET_YES != TALER_MINT_DB_get_collectable_blindcoin (db, - &h_blind, - &cbc2)); + FAILIF (GNUNET_OK != + plugin->insert_collectable_blindcoin (plugin->cls, + session, + &h_blind, + amount, + &cbc)); + FAILIF (GNUNET_OK != + check_reserve (session, + &reserve_pub, + amount.value, + amount.fraction, + amount.currency, + expiry.abs_value_us)); + FAILIF (GNUNET_YES != + plugin->get_collectable_blindcoin (plugin->cls, + session, + &h_blind, + &cbc2)); FAILIF (NULL == cbc2.denom_pub); - FAILIF (0 != memcmp (&cbc2.reserve_sig, &cbc.reserve_sig, sizeof (cbc2.reserve_sig))); - FAILIF (0 != memcmp (&cbc2.reserve_pub, &cbc.reserve_pub, sizeof (cbc2.reserve_pub))); - FAILIF (GNUNET_OK != GNUNET_CRYPTO_rsa_verify (&h_blind, cbc2.sig, dkp->pub)); - rh_head = rh = TALER_MINT_DB_get_reserve_history (db, &reserve_pub); + FAILIF (0 != memcmp (&cbc2.reserve_sig, + &cbc.reserve_sig, + sizeof (cbc2.reserve_sig))); + FAILIF (0 != memcmp (&cbc2.reserve_pub, + &cbc.reserve_pub, + sizeof (cbc2.reserve_pub))); + FAILIF (GNUNET_OK != + GNUNET_CRYPTO_rsa_verify (&h_blind, + cbc2.sig, + dkp->pub)); + rh = plugin->get_reserve_history (plugin->cls, + session, + &reserve_pub); FAILIF (NULL == rh); + rh_head = rh; for (cnt=0; NULL != rh_head; rh_head=rh_head->next, cnt++) { switch (rh_head->type) { case TALER_MINT_DB_RO_BANK_TO_MINT: bt = rh_head->details.bank; - FAILIF (0 != memcmp (&bt->reserve_pub, &reserve_pub, sizeof (reserve_pub))); + FAILIF (0 != memcmp (&bt->reserve_pub, + &reserve_pub, + sizeof (reserve_pub))); FAILIF (1 != bt->amount.value); FAILIF (1 != bt->amount.fraction); FAILIF (0 != strcmp (CURRENCY, bt->amount.currency)); @@ -251,17 +287,35 @@ run (void *cls, char *const *args, const char *cfgfile, deposit.transaction_id = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX); deposit.amount = amount; - FAILIF (GNUNET_OK != TALER_MINT_DB_insert_deposit (db, &deposit)); - FAILIF (GNUNET_YES != TALER_MINT_DB_have_deposit (db, &deposit)); - (void) memcpy (&deposit2, &deposit, sizeof (deposit)); + FAILIF (GNUNET_OK != + plugin->insert_deposit (plugin->cls, + session, &deposit)); + FAILIF (GNUNET_YES != + plugin->have_deposit (plugin->cls, + session, + &deposit)); + (void) memcpy (&deposit2, + &deposit, + sizeof (deposit)); deposit2.transaction_id++; /* should fail if transaction id is different */ - FAILIF (GNUNET_NO != TALER_MINT_DB_have_deposit (db, &deposit2)); + FAILIF (GNUNET_NO != + plugin->have_deposit (plugin->cls, + session, + &deposit2)); deposit2.transaction_id = deposit.transaction_id; RND_BLK (&deposit2.merchant_pub); /* should fail if merchant is different */ - FAILIF (GNUNET_NO != TALER_MINT_DB_have_deposit (db, &deposit2)); - (void) memcpy (&deposit2.merchant_pub, &deposit.merchant_pub, sizeof (deposit.merchant_pub)); + FAILIF (GNUNET_NO != + plugin->have_deposit (plugin->cls, + session, + &deposit2)); + (void) memcpy (&deposit2.merchant_pub, + &deposit.merchant_pub, + sizeof (deposit.merchant_pub)); RND_BLK (&deposit2.coin.coin_pub); /* should fail if coin is different */ - FAILIF (GNUNET_NO != TALER_MINT_DB_have_deposit (db, &deposit2)); + FAILIF (GNUNET_NO != + plugin->have_deposit (plugin->cls, + session, + &deposit2)); result = 0; drop: @@ -270,8 +324,10 @@ run (void *cls, char *const *args, const char *cfgfile, if (NULL != rh) TALER_MINT_DB_free_reserve_history (rh); rh = NULL; - if (NULL != db) - GNUNET_break (GNUNET_OK == TALER_MINT_DB_drop_temporary (db)); + if (NULL != session) + GNUNET_break (GNUNET_OK == + plugin->drop_temporary (plugin->cls, + session)); if (NULL != dkp) destroy_denon_key_pair (dkp); if (NULL != cbc.sig) @@ -285,7 +341,8 @@ run (void *cls, char *const *args, const char *cfgfile, int -main (int argc, char *const argv[]) +main (int argc, + char *const argv[]) { static const struct GNUNET_GETOPT_CommandLineOption options[] = { GNUNET_GETOPT_OPTION_END diff --git a/src/mint/test_mint_deposits.c b/src/mint/test_mint_deposits.c index 5ad8006e..c829e6e1 100644 --- a/src/mint/test_mint_deposits.c +++ b/src/mint/test_mint_deposits.c @@ -13,17 +13,15 @@ You should have received a copy of the GNU General Public License along with TALER; see the file COPYING. If not, If not, see */ - /** * @file mint/test_mint_deposits.c * @brief testcase for mint deposits * @author Sree Harsha Totakura */ - #include "platform.h" #include #include -#include "mint_db.h" +#include "plugin.h" #include "db_pq.h" #include "taler-mint-httpd.h" @@ -43,11 +41,6 @@ } while (0) -/** - * DB connection handle - */ -static PGconn *conn; - /** * Should we not interact with a temporary table? */ @@ -59,64 +52,19 @@ static int persistent; static int result; -int -TALER_MINT_DB_init_deposits (PGconn *conn, int tmp) -{ - const char *tmp_str = (1 == tmp) ? "TEMPORARY" : ""; - char *sql; - PGresult *res; - int ret; - - res = NULL; - (void) GNUNET_asprintf (&sql, - "CREATE %1$s TABLE IF NOT EXISTS deposits (" - " coin_pub BYTEA NOT NULL PRIMARY KEY CHECK (length(coin_pub)=32)" - ",denom_pub BYTEA NOT NULL CHECK (length(denom_pub)=32)" - ",transaction_id INT8 NOT NULL" - ",amount_value INT4 NOT NULL" - ",amount_fraction INT4 NOT NULL" - ",amount_currency VARCHAR(4) NOT NULL" - ",merchant_pub BYTEA NOT NULL" - ",h_contract BYTEA NOT NULL CHECK (length(h_contract)=64)" - ",h_wire BYTEA NOT NULL CHECK (length(h_wire)=64)" - ",coin_sig BYTEA NOT NULL CHECK (length(coin_sig)=64)" - ",wire TEXT NOT NULL" - ")", - tmp_str); - res = PQexec (conn, sql); - GNUNET_free (sql); - if (PGRES_COMMAND_OK != PQresultStatus (res)) - { - break_db_err (res); - ret = GNUNET_SYSERR; - } - else - ret = GNUNET_OK; - PQclear (res); - return ret; -} - - -static void -do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) -{ - if (NULL != conn) - PQfinish (conn); - conn = NULL; -} - - /** * Main function that will be run by the scheduler. * * @param cls closure * @param args remaining command-line arguments * @param cfgfile name of the configuration file used (for saving, can be NULL!) - * @param config configuration + * @param cfg configuration */ static void -run (void *cls, char *const *args, const char *cfgfile, - const struct GNUNET_CONFIGURATION_Handle *config) +run (void *cls, + char *const *args, + const char *cfgfile, + const struct GNUNET_CONFIGURATION_Handle *cfg) { static const char wire[] = "{" "\"type\":\"SEPA\"," @@ -126,13 +74,16 @@ run (void *cls, char *const *args, const char *cfgfile, "}"; struct Deposit *deposit; uint64_t transaction_id; + struct TALER_MINTDB_Session *session; deposit = NULL; - GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, - &do_shutdown, NULL); - EXITIF (NULL == (conn = PQconnectdb(DB_URI))); - EXITIF (GNUNET_OK != TALER_MINT_DB_init_deposits (conn, !persistent)); - EXITIF (GNUNET_OK != TALER_MINT_DB_prepare (conn)); + EXITIF (GNUNET_OK != TALER_MINT_plugin_load (cfg)); + EXITIF (GNUNET_OK != + plugin->create_tables (plugin->cls, + ! persistent)); + session = plugin->get_session (plugin->cls, + ! persistent); + EXITIF (NULL == session); deposit = GNUNET_malloc (sizeof (struct Deposit) + sizeof (wire)); /* Makeup a random coin public key */ GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, @@ -151,20 +102,25 @@ run (void *cls, char *const *args, const char *cfgfile, strcpy (deposit->amount.currency, MINT_CURRENCY); /* Copy wireformat */ deposit->wire = json_loads (wire, 0, NULL); - EXITIF (GNUNET_OK != TALER_MINT_DB_insert_deposit (conn, - deposit)); - EXITIF (GNUNET_OK != TALER_MINT_DB_have_deposit (conn, - deposit)); + EXITIF (GNUNET_OK != + plugin->insert_deposit (plugin->cls, + session, + deposit)); + EXITIF (GNUNET_OK != + plugin->have_deposit (plugin->cls, + session, + deposit)); result = GNUNET_OK; EXITIF_exit: GNUNET_free_non_null (deposit); - GNUNET_SCHEDULER_shutdown (); return; } -int main(int argc, char *const argv[]) +int +main (int argc, + char *const argv[]) { static const struct GNUNET_GETOPT_CommandLineOption options[] = { {'T', "persist", NULL, -- cgit v1.2.3 From 0d3ec509d7192e973123de1ab390826fff4df230 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 22 Mar 2015 13:02:11 +0100 Subject: move free functions into plugin --- src/mint/Makefile.am | 2 ++ src/mint/plugin.c | 65 --------------------------------------- src/mint/plugin.h | 27 ---------------- src/mint/plugin_mintdb_postgres.c | 28 +++++++++++------ src/mint/taler-mint-httpd_db.c | 39 ++++++++++++++--------- src/mint/taler_mintdb_plugin.h | 40 ++++++++++++++++++++++-- 6 files changed, 82 insertions(+), 119 deletions(-) (limited to 'src/mint/plugin.c') diff --git a/src/mint/Makefile.am b/src/mint/Makefile.am index c4f99af6..1eba7d61 100644 --- a/src/mint/Makefile.am +++ b/src/mint/Makefile.am @@ -6,6 +6,8 @@ plugindir = $(libdir)/taler plugin_LTLIBRARIES = \ libtaler_plugin_mintdb_postgres.la +EXTRA_DIST = plugin_mintdb_common.c + libtaler_plugin_mintdb_postgres_la_SOURCES = \ plugin_mintdb_postgres.c libtaler_plugin_mintdb_postgres_la_LIBADD = \ diff --git a/src/mint/plugin.c b/src/mint/plugin.c index 67cabd81..4fb75f87 100644 --- a/src/mint/plugin.c +++ b/src/mint/plugin.c @@ -115,69 +115,4 @@ plugin_fini () } -// FIXME: decide if we should keep these in each plugin, here -// or yet again somewhere else entirely (plugin_common.c?) - -/** - * Free memory associated with the given reserve history. - * - * @param rh history to free. - */ -void -TALER_MINT_DB_free_reserve_history (struct ReserveHistory *rh) -{ - struct BankTransfer *bt; - struct CollectableBlindcoin *cbc; - struct ReserveHistory *backref; - - while (NULL != rh) - { - switch(rh->type) - { - case TALER_MINT_DB_RO_BANK_TO_MINT: - bt = rh->details.bank; - if (NULL != bt->wire) - json_decref ((json_t *) bt->wire); /* FIXME: avoid cast? */ - GNUNET_free (bt); - break; - case TALER_MINT_DB_RO_WITHDRAW_COIN: - cbc = rh->details.withdraw; - GNUNET_CRYPTO_rsa_signature_free (cbc->sig); - GNUNET_CRYPTO_rsa_public_key_free (cbc->denom_pub); - GNUNET_free (cbc); - break; - } - backref = rh; - rh = rh->next; - GNUNET_free (backref); - } -} - - -/** - * Free memory of the link data list. - * - * @param ldl link data list to release - */ -void -TALER_db_link_data_list_free (struct LinkDataList *ldl) -{ - GNUNET_break (0); // FIXME -} - - -/** - * Free linked list of transactions. - * - * @param list list to free - */ -void -TALER_MINT_DB_free_coin_transaction_list (struct TALER_MINT_DB_TransactionList *list) -{ - // FIXME: check logic! - GNUNET_break (0); -} - - - /* end of plugin.c */ diff --git a/src/mint/plugin.h b/src/mint/plugin.h index bb1f0ecb..0dfb866d 100644 --- a/src/mint/plugin.h +++ b/src/mint/plugin.h @@ -47,31 +47,4 @@ void TALER_MINT_plugin_unload (void); -/** - * Free memory associated with the given reserve history. - * - * @param rh history to free. - */ -void -TALER_MINT_DB_free_reserve_history (struct ReserveHistory *rh); - - -/** - * Free memory of the link data list. - * - * @param ldl link data list to release - */ -void -TALER_db_link_data_list_free (struct LinkDataList *ldl); - - -/** - * Free linked list of transactions. - * - * @param list list to free - */ -void -TALER_MINT_DB_free_coin_transaction_list (struct TALER_MINT_DB_TransactionList *list); - - #endif diff --git a/src/mint/plugin_mintdb_postgres.c b/src/mint/plugin_mintdb_postgres.c index c49ea139..adc85251 100644 --- a/src/mint/plugin_mintdb_postgres.c +++ b/src/mint/plugin_mintdb_postgres.c @@ -28,6 +28,7 @@ #include #include +#include "plugin_mintdb_common.c" #define TALER_TEMP_SCHEMA_NAME "taler_temporary" @@ -1313,7 +1314,8 @@ postgres_get_reserve_history (void *cls, PQclear (result); if (GNUNET_SYSERR == ret) { - TALER_MINT_DB_free_reserve_history (rh); + common_free_reserve_history (cls, + rh); rh = NULL; } return rh; @@ -2064,9 +2066,9 @@ postgres_insert_refresh_collectable (void *cls, * @return all known link data for the coin */ static struct LinkDataList * -postgres_get_link (void *cls, - struct TALER_MINTDB_Session *session, - const struct GNUNET_CRYPTO_EcdsaPublicKey *coin_pub) +postgres_get_link_data_list (void *cls, + struct TALER_MINTDB_Session *session, + const struct GNUNET_CRYPTO_EcdsaPublicKey *coin_pub) { // FIXME: check logic! struct LinkDataList *ldl; @@ -2116,7 +2118,8 @@ postgres_get_link (void *cls, { PQclear (result); GNUNET_break (0); - TALER_db_link_data_list_free (ldl); + common_free_link_data_list (cls, + ldl); return NULL; } if (ld_buf_size < sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)) @@ -2125,7 +2128,8 @@ postgres_get_link (void *cls, GNUNET_free (pk_buf); GNUNET_free (sig_buf); GNUNET_free (ld_buf); - TALER_db_link_data_list_free (ldl); + common_free_link_data_list (cls, + ldl); return NULL; } // FIXME: use util API for this! @@ -2154,7 +2158,8 @@ postgres_get_link (void *cls, GNUNET_free (link_enc); GNUNET_break (0); PQclear (result); - TALER_db_link_data_list_free (ldl); + common_free_link_data_list (cls, + ldl); return NULL; } pos = GNUNET_new (struct LinkDataList); @@ -2171,8 +2176,8 @@ postgres_get_link (void *cls, /** * Obtain shared secret and transfer public key from the public key of * the coin. This information and the link information returned by - * #TALER_db_get_link() enable the owner of an old coin to determine - * the private keys of the new coins after the melt. + * #postgres_get_link_data_list() enable the owner of an old coin to + * determine the private keys of the new coins after the melt. * * @param cls the `struct PostgresClosure` with the plugin-specific state * @param session database connection @@ -2304,6 +2309,7 @@ libtaler_plugin_mintdb_postgres_init (void *cls) plugin->get_collectable_blindcoin = &postgres_get_collectable_blindcoin; plugin->insert_collectable_blindcoin = &postgres_insert_collectable_blindcoin; plugin->get_reserve_history = &postgres_get_reserve_history; + plugin->free_reserve_history = &common_free_reserve_history; plugin->have_deposit = &postgres_have_deposit; plugin->insert_deposit = &postgres_insert_deposit; plugin->get_refresh_session = &postgres_get_refresh_session; @@ -2317,11 +2323,13 @@ libtaler_plugin_mintdb_postgres_init (void *cls) plugin->insert_refresh_commit_link = &postgres_insert_refresh_commit_link; plugin->get_refresh_commit_link = &postgres_get_refresh_commit_link; plugin->insert_refresh_collectable = &postgres_insert_refresh_collectable; - plugin->get_link = &postgres_get_link; + plugin->get_link_data_list = &postgres_get_link_data_list; + plugin->free_link_data_list = &common_free_link_data_list; plugin->get_transfer = &postgres_get_transfer; // plugin->have_lock = &postgres_have_lock; // plugin->insert_lock = &postgres_insert_lock; plugin->get_coin_transactions = &postgres_get_coin_transactions; + plugin->free_coin_transaction_list = &common_free_coin_transaction_list; return plugin; } diff --git a/src/mint/taler-mint-httpd_db.c b/src/mint/taler-mint-httpd_db.c index 292ef68f..35c6dfb9 100644 --- a/src/mint/taler-mint-httpd_db.c +++ b/src/mint/taler-mint-httpd_db.c @@ -106,7 +106,8 @@ TALER_MINT_db_execute_deposit (struct MHD_Connection *connection, &deposit->amount)) { GNUNET_break (0); - TALER_MINT_DB_free_coin_transaction_list (tl); + plugin->free_coin_transaction_list (plugin->cls, + tl); return TALER_MINT_reply_internal_db_error (connection); } @@ -125,7 +126,8 @@ TALER_MINT_db_execute_deposit (struct MHD_Connection *connection, &fee_deposit)) ) { GNUNET_break (0); - TALER_MINT_DB_free_coin_transaction_list (tl); + plugin->free_coin_transaction_list (plugin->cls, + tl); return TALER_MINT_reply_internal_db_error (connection); } break; @@ -140,7 +142,8 @@ TALER_MINT_db_execute_deposit (struct MHD_Connection *connection, &fee_refresh)) ) { GNUNET_break (0); - TALER_MINT_DB_free_coin_transaction_list (tl); + plugin->free_coin_transaction_list (plugin->cls, + tl); return TALER_MINT_reply_internal_db_error (connection); } break; @@ -163,10 +166,12 @@ TALER_MINT_db_execute_deposit (struct MHD_Connection *connection, session); ret = TALER_MINT_reply_deposit_insufficient_funds (connection, tl); - TALER_MINT_DB_free_coin_transaction_list (tl); + plugin->free_coin_transaction_list (plugin->cls, + tl); return ret; } - TALER_MINT_DB_free_coin_transaction_list (tl); + plugin->free_coin_transaction_list (plugin->cls, + tl); if (GNUNET_OK != plugin->insert_deposit (plugin->cls, @@ -228,7 +233,8 @@ TALER_MINT_db_execute_withdraw_status (struct MHD_Connection *connection, "error", "Reserve not found"); res = TALER_MINT_reply_withdraw_status_success (connection, rh); - TALER_MINT_DB_free_reserve_history (rh); + plugin->free_reserve_history (plugin->cls, + rh); return res; } @@ -414,10 +420,12 @@ TALER_MINT_db_execute_withdraw_sign (struct MHD_Connection *connection, session); res = TALER_MINT_reply_withdraw_sign_insufficient_funds (connection, rh); - TALER_MINT_DB_free_reserve_history (rh); + plugin->free_reserve_history (plugin->cls, + rh); return res; } - TALER_MINT_DB_free_reserve_history (rh); + plugin->free_reserve_history (plugin->cls, + rh); /* Balance is good, sign the coin! */ sig = GNUNET_CRYPTO_rsa_sign (dki->denom_priv, @@ -532,10 +540,12 @@ refresh_accept_melts (struct MHD_Connection *connection, coin_details->melt_amount, coin_residual)) ? GNUNET_NO : GNUNET_SYSERR; - TALER_MINT_DB_free_coin_transaction_list (tl); + plugin->free_coin_transaction_list (plugin->cls, + tl); return res; } - TALER_MINT_DB_free_coin_transaction_list (tl); + plugin->free_coin_transaction_list (plugin->cls, + tl); melt.coin = *coin_public_info; melt.coin_sig = coin_details->melt_sig; @@ -1242,9 +1252,9 @@ TALER_MINT_db_execute_refresh_link (struct MHD_Connection *connection, } GNUNET_assert (GNUNET_OK == res); - ldl = plugin->get_link (plugin->cls, - session, - coin_pub); + ldl = plugin->get_link_data_list (plugin->cls, + session, + coin_pub); if (NULL == ldl) { return TALER_MINT_reply_json_pack (connection, @@ -1257,7 +1267,8 @@ TALER_MINT_db_execute_refresh_link (struct MHD_Connection *connection, &transfer_pub, &shared_secret_enc, ldl); - TALER_db_link_data_list_free (ldl); + plugin->free_link_data_list (plugin->cls, + ldl); return res; } diff --git a/src/mint/taler_mintdb_plugin.h b/src/mint/taler_mintdb_plugin.h index d330b817..eabb00d9 100644 --- a/src/mint/taler_mintdb_plugin.h +++ b/src/mint/taler_mintdb_plugin.h @@ -658,6 +658,17 @@ struct TALER_MINTDB_Plugin const struct GNUNET_CRYPTO_EddsaPublicKey *reserve_pub); + /** + * Free memory associated with the given reserve history. + * + * @param cls the @e cls of this struct with the plugin-specific state + * @param rh history to free. + */ + void + (*free_reserve_history) (void *cls, + struct ReserveHistory *rh); + + /** * Check if we have the specified deposit already in the database. * @@ -922,9 +933,20 @@ struct TALER_MINTDB_Plugin * @return all known link data for the coin */ struct LinkDataList * - (*get_link) (void *cls, - struct TALER_MINTDB_Session *db_conn, - const struct GNUNET_CRYPTO_EcdsaPublicKey *coin_pub); + (*get_link_data_list) (void *cls, + struct TALER_MINTDB_Session *db_conn, + const struct GNUNET_CRYPTO_EcdsaPublicKey *coin_pub); + + + /** + * Free memory of the link data list. + * + * @param cls the @e cls of this struct with the plugin-specific state + * @param ldl link data list to release + */ + void + (*free_link_data_list) (void *cls, + struct LinkDataList *ldl); /** @@ -996,6 +1018,18 @@ struct TALER_MINTDB_Plugin struct TALER_MINTDB_Session *db_conn, const struct GNUNET_CRYPTO_EcdsaPublicKey *coin_pub); + + /** + * Free linked list of transactions. + * + * @param cls the @e cls of this struct with the plugin-specific state + * @param list list to free + */ + void + (*free_coin_transaction_list) (void *cls, + struct TALER_MINT_DB_TransactionList *list); + + }; -- cgit v1.2.3