diff options
author | Marcello Stanisci <marcello.stanisci@inria.fr> | 2016-06-23 17:55:41 +0200 |
---|---|---|
committer | Marcello Stanisci <marcello.stanisci@inria.fr> | 2016-06-23 17:55:41 +0200 |
commit | 8f8d1a202d46c4315affbecdc683d9a3013be0c7 (patch) | |
tree | db167ceb93ebd259b6ef574189047bddba76adff | |
parent | d623b402412f00e84d4108cd5a156c90915245ab (diff) |
preliminary modification to liberate the
merchant from using global variables for its instance (fail to compile)
-rw-r--r-- | src/backend/taler-merchant-httpd.c | 60 | ||||
-rw-r--r-- | src/backend/taler-merchant-httpd_contract.c | 11 | ||||
-rw-r--r-- | src/backend/taler-merchant-httpd_pay.c | 32 |
3 files changed, 47 insertions, 56 deletions
diff --git a/src/backend/taler-merchant-httpd.c b/src/backend/taler-merchant-httpd.c index 25de5e45..d259d5cb 100644 --- a/src/backend/taler-merchant-httpd.c +++ b/src/backend/taler-merchant-httpd.c @@ -53,36 +53,11 @@ struct MerchantInstance **instances; /** - * Our wire format details in JSON format (with salt). - */ -struct json_t *j_wire; - -/** - * Hash of our wire format details as given in #j_wire. - */ -struct GNUNET_HashCode h_wire; - -/** - * Merchant's private key - */ -struct TALER_MerchantPrivateKeyP privkey; - -/** - * Merchant's public key - */ -struct TALER_MerchantPublicKeyP pubkey; - -/** * The port we are running on */ static long long unsigned port; /** - * File holding the merchant's private key - */ -static char *keyfile; - -/** * This value tells the exchange by which date this merchant would like * to receive the funds for a deposited payment */ @@ -276,11 +251,6 @@ do_shutdown (void *cls) } TMH_EXCHANGES_done (); TMH_AUDITORS_done (); - if (NULL != j_wire) - { - json_decref (j_wire); - j_wire = NULL; - } if (NULL != instances) { unsigned int i; @@ -471,7 +441,7 @@ instances_iterator_cb (void *cls, if (GNUNET_YES != GNUNET_DISK_file_test (mi->keyfile)) GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Merchant private key `%s' does not exist yet, creating it!\n", - keyfile); + mi->keyfile); if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create_from_file (mi->keyfile))) { @@ -526,6 +496,34 @@ instances_iterator_cb (void *cls, } /** + * Extract merchant instance from the given JSON + * + * @param json the JSON to inspect; it is not required to + * comply with any particular format. It will only be checked + * if the field "receiver" is there. + * @return a pointer to a #struct MerchantInstance. This will be + * the 'default' merchant if the frontend did not specif any + * "receiver" field. The user should not care to free the returned + * value, as it is taken from a global array that will be freed + * by the general shutdown routine. NULL if the frontend specified + * a wrong instance + */ +struct MerchantInstance * +get_instance (struct json_t *json) +{ + unsigned int i; + struct json_t *receiver; + /*FIXME who decrefs receiver?*/ + if (NULL == (receiver = json_object_get (json, "receiver"))) + receiver = json_string ("default"); + + for (i=0; NULL != instances[i]; i++) + if (0 == strcmp (json_string_value (receiver), instances[i]->id)) + return instances[i]; + return NULL; +} + +/** * Iterate over each merchant instance, in order to populate * each instance's own data * diff --git a/src/backend/taler-merchant-httpd_contract.c b/src/backend/taler-merchant-httpd_contract.c index 722146b5..c1181219 100644 --- a/src/backend/taler-merchant-httpd_contract.c +++ b/src/backend/taler-merchant-httpd_contract.c @@ -114,6 +114,8 @@ json_parse_cleanup (struct TM_HandlerContext *hc) GNUNET_free (jpc); } +extern struct MerchantInstance * +get_instance (struct json_t *json); /** * Manage a contract request. In practical terms, it adds the fields @@ -142,6 +144,7 @@ MH_handler_contract (struct TMH_RequestHandler *rh, json_t *root; json_t *jcontract; int res; + struct MerchantInstance *mi; struct TMH_JsonParseContext *ctx; struct TALER_ContractPS contract; struct GNUNET_CRYPTO_EddsaSignature contract_sig; @@ -219,7 +222,7 @@ MH_handler_contract (struct TMH_RequestHandler *rh, "products in contract request malformed"); } - + mi = get_instance (root); /* add fields to the contract that the backend should provide */ json_object_set (jcontract, "exchanges", @@ -229,10 +232,10 @@ MH_handler_contract (struct TMH_RequestHandler *rh, j_auditors); json_object_set_new (jcontract, "H_wire", - GNUNET_JSON_from_data_auto (&h_wire)); + GNUNET_JSON_from_data_auto (&mi->h_wire)); json_object_set_new (jcontract, "merchant_pub", - GNUNET_JSON_from_data_auto (&pubkey)); + GNUNET_JSON_from_data_auto (&mi->pubkey)); /* create contract signature */ contract.purpose.purpose = htonl (TALER_SIGNATURE_MERCHANT_CONTRACT); @@ -245,7 +248,7 @@ MH_handler_contract (struct TMH_RequestHandler *rh, GNUNET_assert (GNUNET_OK == TALER_JSON_hash (jcontract, &contract.h_contract)); - GNUNET_CRYPTO_eddsa_sign (&privkey.eddsa_priv, + GNUNET_CRYPTO_eddsa_sign (&mi->privkey.eddsa_priv, &contract.purpose, &contract_sig); diff --git a/src/backend/taler-merchant-httpd_pay.c b/src/backend/taler-merchant-httpd_pay.c index b40c1dbc..6a51aecc 100644 --- a/src/backend/taler-merchant-httpd_pay.c +++ b/src/backend/taler-merchant-httpd_pay.c @@ -236,9 +236,9 @@ struct PayContext int transaction_exits; /** - * Bank details of the payment's receiver (in JSON format) + * Instance of the payment's receiver (in JSON format) */ - struct json_t *receiver_j_wire; + struct MerchantInstance *mi; }; @@ -659,18 +659,18 @@ process_pay_with_exchange (void *cls, if (GNUNET_YES == dc->found_in_db) continue; - GNUNET_assert (NULL != pc->receiver_j_wire); + GNUNET_assert (NULL != pc->mi->j_wire); dc->dh = TALER_EXCHANGE_deposit (mh, &dc->amount_with_fee, pc->wire_transfer_deadline, - pc->receiver_j_wire, + pc->mi->j_wire, &pc->h_contract, &dc->coin_pub, &dc->ub_sig, &dc->denom, pc->timestamp, pc->transaction_id, - &pubkey, + &pc->mi->pubkey, pc->refund_deadline, &dc->coin_sig, &deposit_cb, @@ -789,7 +789,7 @@ check_transaction_exists (void *cls, &pc->h_contract, sizeof (struct GNUNET_HashCode))) && (0 == memcmp (h_xwire, - &h_wire, + &pc->mi->h_wire, sizeof (struct GNUNET_HashCode))) && (timestamp.abs_value_us == pc->timestamp.abs_value_us) && (refund.abs_value_us == pc->refund_deadline.abs_value_us) && @@ -805,6 +805,8 @@ check_transaction_exists (void *cls, } } +extern struct MerchantInstance * +get_instance (struct json_t *json); /** * Accomplish this payment. @@ -828,8 +830,6 @@ MH_handler_pay (struct TMH_RequestHandler *rh, struct PayContext *pc; int res; json_t *root; - struct json_t *receiver; - unsigned int i; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "In handler for /pay.\n"); @@ -917,6 +917,7 @@ MH_handler_pay (struct TMH_RequestHandler *rh, GNUNET_break (0); return (GNUNET_NO == res) ? MHD_YES : MHD_NO; } + pc->mi = get_instance (root); pc->chosen_exchange = GNUNET_strdup (chosen_exchange); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Parsed JSON for /pay.\n"); @@ -932,7 +933,7 @@ MH_handler_pay (struct TMH_RequestHandler *rh, GNUNET_CRYPTO_eddsa_verify (TALER_SIGNATURE_MERCHANT_CONTRACT, &cp.purpose, &merchant_sig.eddsa_sig, - &pubkey.eddsa_pub)) + &pc->mi->pubkey.eddsa_pub)) { GNUNET_break (0); GNUNET_JSON_parse_free (spec); @@ -1066,7 +1067,6 @@ MH_handler_pay (struct TMH_RequestHandler *rh, json_decref (root); return ret; } - /* Check if transaction is already known, if not store it. */ if (GNUNET_SYSERR == db->find_transaction_by_id (db->cls, @@ -1093,7 +1093,7 @@ MH_handler_pay (struct TMH_RequestHandler *rh, pc->transaction_id, pc->chosen_exchange, &pc->h_contract, - &h_wire, + &pc->mi->h_wire, pc->timestamp, pc->refund_deadline, &pc->amount)) @@ -1107,16 +1107,6 @@ MH_handler_pay (struct TMH_RequestHandler *rh, MHD_suspend_connection (connection); - - if (NULL == (receiver = json_object_get (root, "receiver"))) - receiver = json_string ("default"); - - for (i=0; NULL != instances[i]; i++) - { - if (0 == strcmp (json_string_value (receiver), instances[i]->id)) - pc->receiver_j_wire = instances[i]->j_wire; - } - /* Find the responsible exchange, this may take a while... */ pc->fo = TMH_EXCHANGES_find_exchange (pc->chosen_exchange, &process_pay_with_exchange, |