LCOV - code coverage report
Current view: top level - exchange - taler-exchange-httpd_wire.c (source / functions) Hit Total Coverage
Test: rcoverage.info Lines: 31 46 67.4 %
Date: 2017-09-17 17:24:28 Functions: 4 5 80.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2015-2017 GNUnet e.V. and INRIA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify it under the
       6             :   terms of the GNU Affero General Public License as published by the Free Software
       7             :   Foundation; either version 3, or (at your option) any later version.
       8             : 
       9             :   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
      10             :   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
      11             :   A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU Affero General Public License along with
      14             :   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
      15             : */
      16             : /**
      17             :  * @file taler-exchange-httpd_wire.c
      18             :  * @brief Handle /wire requests
      19             :  * @author Christian Grothoff
      20             :  */
      21             : #include "platform.h"
      22             : #include <gnunet/gnunet_json_lib.h>
      23             : #include "taler-exchange-httpd_keystate.h"
      24             : #include "taler-exchange-httpd_responses.h"
      25             : #include "taler-exchange-httpd_validation.h"
      26             : #include "taler-exchange-httpd_wire.h"
      27             : #include "taler_json_lib.h"
      28             : #include <jansson.h>
      29             : 
      30             : /**
      31             :  * Cached JSON for /wire response.
      32             :  */
      33             : static json_t *wire_methods;
      34             : 
      35             : 
      36             : /**
      37             :  * Convert fee structure to JSON result to be returned
      38             :  * as part of a /wire response.
      39             :  *
      40             :  * @param af fee structure to convert
      41             :  * @return NULL on error, otherwise json data structure for /wire.
      42             :  */
      43             : static json_t *
      44           8 : fees_to_json (struct TALER_EXCHANGEDB_AggregateFees *af)
      45             : {
      46             :   json_t *a;
      47             : 
      48           8 :   a = json_array ();
      49          32 :   while (NULL != af)
      50             :   {
      51          32 :     if ( (GNUNET_NO == GNUNET_TIME_round_abs (&af->start_date)) ||
      52          16 :          (GNUNET_NO == GNUNET_TIME_round_abs (&af->end_date)) )
      53             :     {
      54           0 :       json_decref (a);
      55           0 :       return NULL;
      56             :     }
      57          16 :     if (0 !=
      58          48 :         json_array_append_new (a,
      59             :                                json_pack ("{s:o, s:o, s:o, s:o, s:o}",
      60          16 :                                           "wire_fee", TALER_JSON_from_amount (&af->wire_fee),
      61          16 :                                           "closing_fee", TALER_JSON_from_amount (&af->closing_fee),
      62             :                                           "start_date", GNUNET_JSON_from_time_abs (af->start_date),
      63             :                                           "end_date", GNUNET_JSON_from_time_abs (af->end_date),
      64          16 :                                           "sig", GNUNET_JSON_from_data_auto (&af->master_sig))))
      65             :     {
      66           0 :       GNUNET_break (0);
      67           0 :       json_decref (a);
      68           0 :       return NULL;
      69             :     }
      70          16 :     af = af->next;
      71             :   }
      72           8 :   return a;
      73             : }
      74             : 
      75             : 
      76             : /**
      77             :  * Obtain fee structure for @a wire_plugin_name wire transfers.
      78             :  *
      79             :  * @param wire_plugin_name name of the plugin to load fees for
      80             :  * @return JSON object (to be freed by caller) with fee structure
      81             :  */
      82             : json_t *
      83           9 : TEH_WIRE_get_fees (const char *wire_plugin_name)
      84             : {
      85             :   struct TALER_EXCHANGEDB_AggregateFees *af;
      86             :   json_t *j;
      87             :   struct GNUNET_TIME_Absolute now;
      88             : 
      89           9 :   af = TALER_EXCHANGEDB_fees_read (cfg,
      90             :                                    wire_plugin_name);
      91           9 :   now = GNUNET_TIME_absolute_get ();
      92          26 :   while ( (NULL != af) &&
      93           8 :           (af->end_date.abs_value_us < now.abs_value_us) )
      94             :   {
      95           0 :     struct TALER_EXCHANGEDB_AggregateFees *n = af->next;
      96             : 
      97           0 :     GNUNET_free (af);
      98           0 :     af = n;
      99             :   }
     100           9 :   if (NULL == af)
     101             :   {
     102           1 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     103             :                 "Failed to find current wire transfer fees for `%s'\n",
     104             :                 wire_plugin_name);
     105           1 :     return NULL;
     106             :   }
     107           8 :   j = fees_to_json (af);
     108           8 :   TALER_EXCHANGEDB_fees_free (af);
     109           8 :   return j;
     110             : }
     111             : 
     112             : 
     113             : /**
     114             :  * Handle a "/wire" request.
     115             :  *
     116             :  * @param rh context of the handler
     117             :  * @param connection the MHD connection to handle
     118             :  * @param[in,out] connection_cls the connection's closure (can be updated)
     119             :  * @param upload_data upload data
     120             :  * @param[in,out] upload_data_size number of bytes (left) in @a upload_data
     121             :  * @return MHD result code
     122             :   */
     123             : int
     124           2 : TEH_WIRE_handler_wire (struct TEH_RequestHandler *rh,
     125             :                        struct MHD_Connection *connection,
     126             :                        void **connection_cls,
     127             :                        const char *upload_data,
     128             :                        size_t *upload_data_size)
     129             : {
     130           2 :   GNUNET_assert (NULL != wire_methods);
     131           2 :   return TEH_RESPONSE_reply_json (connection,
     132             :                                   wire_methods,
     133             :                                   MHD_HTTP_OK);
     134             : }
     135             : 
     136             : 
     137             : /**
     138             :  * Initialize wire subsystem.
     139             :  *
     140             :  * @return #GNUNET_OK on success, #GNUNET_SYSERR if we found no valid
     141             :  *         wire methods
     142             :  */
     143             : int
     144           2 : TEH_WIRE_init ()
     145             : {
     146           2 :   wire_methods = TEH_VALIDATION_get_wire_methods ();
     147           4 :   if ( (NULL == wire_methods) ||
     148           2 :        (0 == json_object_size (wire_methods)) )
     149             :   {
     150           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     151             :                 "Failed to find properly configured wire transfer method\n");
     152           0 :     return GNUNET_SYSERR;
     153             :   }
     154           2 :   return GNUNET_OK;
     155             : }
     156             : 
     157             : 
     158             : /**
     159             :  * Initialize libgcrypt.
     160             :  */
     161             : void  __attribute__ ((destructor))
     162           0 : TEH_wire_cleanup ()
     163             : {
     164           0 :   if (NULL != wire_methods)
     165             :   {
     166           0 :     json_decref (wire_methods);
     167           0 :     wire_methods = NULL;
     168             :   }
     169           0 : }
     170             : 
     171             : 
     172             : 
     173             : /* end of taler-exchange-httpd_wire.c */

Generated by: LCOV version 1.13