LCOV - code coverage report
Current view: top level - lib - auditor_api_exchanges.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 45 91 49.5 %
Date: 2021-08-30 06:43:37 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2018 Taler Systems SA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify it under the
       6             :   terms of the GNU 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 General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU General Public License along with
      14             :   TALER; see the file COPYING.  If not, see
      15             :   <http://www.gnu.org/licenses/>
      16             : */
      17             : /**
      18             :  * @file lib/auditor_api_exchanges.c
      19             :  * @brief Implementation of the /exchanges request of the auditor's HTTP API
      20             :  * @author Christian Grothoff
      21             :  */
      22             : #include "platform.h"
      23             : #include <jansson.h>
      24             : #include <microhttpd.h> /* just for HTTP status codes */
      25             : #include <gnunet/gnunet_util_lib.h>
      26             : #include <gnunet/gnunet_json_lib.h>
      27             : #include <gnunet/gnunet_curl_lib.h>
      28             : #include "taler_json_lib.h"
      29             : #include "taler_auditor_service.h"
      30             : #include "auditor_api_handle.h"
      31             : #include "taler_signatures.h"
      32             : #include "auditor_api_curl_defaults.h"
      33             : 
      34             : /**
      35             :  * How many exchanges do we allow a single auditor to
      36             :  * audit at most?
      37             :  */
      38             : #define MAX_EXCHANGES 1024
      39             : 
      40             : 
      41             : /**
      42             :  * @brief A ListExchanges Handle
      43             :  */
      44             : struct TALER_AUDITOR_ListExchangesHandle
      45             : {
      46             : 
      47             :   /**
      48             :    * The connection to auditor this request handle will use
      49             :    */
      50             :   struct TALER_AUDITOR_Handle *auditor;
      51             : 
      52             :   /**
      53             :    * The url for this request.
      54             :    */
      55             :   char *url;
      56             : 
      57             :   /**
      58             :    * Handle for the request.
      59             :    */
      60             :   struct GNUNET_CURL_Job *job;
      61             : 
      62             :   /**
      63             :    * Function to call with the result.
      64             :    */
      65             :   TALER_AUDITOR_ListExchangesResultCallback cb;
      66             : 
      67             :   /**
      68             :    * Closure for @a cb.
      69             :    */
      70             :   void *cb_cls;
      71             : 
      72             : };
      73             : 
      74             : 
      75             : /**
      76             :  * Function called when we're done processing the
      77             :  * HTTP /exchanges request.
      78             :  *
      79             :  * @param cls the `struct TALER_AUDITOR_ListExchangesHandle`
      80             :  * @param response_code HTTP response code, 0 on error
      81             :  * @param djson parsed JSON result, NULL on error
      82             :  */
      83             : static void
      84           1 : handle_exchanges_finished (void *cls,
      85             :                            long response_code,
      86             :                            const void *djson)
      87             : {
      88           1 :   const json_t *json = djson;
      89             :   const json_t *ja;
      90             :   unsigned int ja_len;
      91           1 :   struct TALER_AUDITOR_ListExchangesHandle *leh = cls;
      92           1 :   struct TALER_AUDITOR_HttpResponse hr = {
      93             :     .reply = json,
      94           1 :     .http_status = (unsigned int) response_code
      95             :   };
      96             : 
      97           1 :   leh->job = NULL;
      98           1 :   switch (response_code)
      99             :   {
     100           0 :   case 0:
     101           0 :     hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     102           0 :     break;
     103           1 :   case MHD_HTTP_OK:
     104           1 :     ja = json_object_get (json,
     105             :                           "exchanges");
     106           1 :     if ( (NULL == ja) ||
     107           1 :          (! json_is_array (ja)) )
     108             :     {
     109           0 :       GNUNET_break (0);
     110           0 :       hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
     111           0 :       hr.http_status = 0;
     112           0 :       break;
     113             :     }
     114             : 
     115           1 :     ja_len = json_array_size (ja);
     116           1 :     if (ja_len > MAX_EXCHANGES)
     117             :     {
     118           0 :       GNUNET_break (0);
     119           0 :       hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
     120           0 :       hr.http_status = 0;
     121           0 :       break;
     122             :     }
     123           1 :     {
     124           1 :       struct TALER_AUDITOR_ExchangeInfo ei[ja_len];
     125             :       int ok;
     126             : 
     127           1 :       ok = GNUNET_YES;
     128           2 :       for (unsigned int i = 0; i<ja_len; i++)
     129             :       {
     130             :         struct GNUNET_JSON_Specification spec[] = {
     131           1 :           GNUNET_JSON_spec_fixed_auto ("master_pub", &ei[i].master_pub),
     132           1 :           GNUNET_JSON_spec_string ("exchange_url", &ei[i].exchange_url),
     133           1 :           GNUNET_JSON_spec_end ()
     134             :         };
     135             : 
     136           1 :         if (GNUNET_OK !=
     137           1 :             GNUNET_JSON_parse (json_array_get (ja,
     138             :                                                i),
     139             :                                spec,
     140             :                                NULL, NULL))
     141             :         {
     142           0 :           GNUNET_break_op (0);
     143           0 :           ok = GNUNET_NO;
     144           0 :           hr.ec = TALER_EC_GENERIC_REPLY_MALFORMED;
     145           0 :           hr.http_status = 0;
     146           0 :           break;
     147             :         }
     148             :       }
     149           1 :       if (GNUNET_YES != ok)
     150           0 :         break;
     151           1 :       leh->cb (leh->cb_cls,
     152             :                &hr,
     153             :                ja_len,
     154             :                ei);
     155           1 :       TALER_AUDITOR_list_exchanges_cancel (leh);
     156           1 :       return;
     157             :     }
     158           0 :   case MHD_HTTP_BAD_REQUEST:
     159             :     /* This should never happen, either us or the auditor is buggy
     160             :        (or API version conflict); just pass JSON reply to the application */
     161           0 :     hr.ec = TALER_JSON_get_error_code (json);
     162           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     163           0 :     break;
     164           0 :   case MHD_HTTP_NOT_FOUND:
     165             :     /* Nothing really to verify, this should never
     166             :        happen, we should pass the JSON reply to the application */
     167           0 :     hr.ec = TALER_JSON_get_error_code (json);
     168           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     169           0 :     break;
     170           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     171             :     /* Server had an internal issue; we should retry, but this API
     172             :        leaves this to the application */
     173           0 :     hr.ec = TALER_JSON_get_error_code (json);
     174           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     175           0 :     break;
     176           0 :   default:
     177             :     /* unexpected response code */
     178           0 :     hr.ec = TALER_JSON_get_error_code (json);
     179           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     180           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     181             :                 "Unexpected response code %u/%d for auditor list-exchanges request\n",
     182             :                 (unsigned int) response_code,
     183             :                 (int) hr.ec);
     184           0 :     GNUNET_break_op (0);
     185           0 :     break;
     186             :   }
     187           0 :   if (NULL != leh->cb)
     188           0 :     leh->cb (leh->cb_cls,
     189             :              &hr,
     190             :              0,
     191             :              NULL);
     192           0 :   TALER_AUDITOR_list_exchanges_cancel (leh);
     193             : }
     194             : 
     195             : 
     196             : /**
     197             :  * Submit an /exchanges request to the auditor and get the
     198             :  * auditor's response.  If the auditor's reply is not
     199             :  * well-formed, we return an HTTP status code of zero to @a cb.
     200             :  *
     201             :  * @param auditor the auditor handle; the auditor must be ready to operate
     202             :  * @param cb the callback to call when a reply for this request is available
     203             :  * @param cb_cls closure for the above callback
     204             :  * @return a handle for this request; NULL if the inputs are invalid (i.e.
     205             :  *         signatures fail to verify).  In this case, the callback is not called.
     206             :  */
     207             : struct TALER_AUDITOR_ListExchangesHandle *
     208           1 : TALER_AUDITOR_list_exchanges (struct TALER_AUDITOR_Handle *auditor,
     209             :                               TALER_AUDITOR_ListExchangesResultCallback cb,
     210             :                               void *cb_cls)
     211             : {
     212             :   struct TALER_AUDITOR_ListExchangesHandle *leh;
     213             :   struct GNUNET_CURL_Context *ctx;
     214             :   CURL *eh;
     215             : 
     216           1 :   GNUNET_assert (GNUNET_YES ==
     217             :                  TALER_AUDITOR_handle_is_ready_ (auditor));
     218             : 
     219           1 :   leh = GNUNET_new (struct TALER_AUDITOR_ListExchangesHandle);
     220           1 :   leh->auditor = auditor;
     221           1 :   leh->cb = cb;
     222           1 :   leh->cb_cls = cb_cls;
     223           1 :   leh->url = TALER_AUDITOR_path_to_url_ (auditor, "/exchanges");
     224           1 :   if (NULL == leh->url)
     225             :   {
     226           0 :     GNUNET_free (leh);
     227           0 :     return NULL;
     228             :   }
     229           1 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     230             :               "URL for list-exchanges: `%s'\n",
     231             :               leh->url);
     232           1 :   eh = TALER_AUDITOR_curl_easy_get_ (leh->url);
     233           1 :   if (NULL == eh)
     234             :   {
     235           0 :     GNUNET_break (0);
     236           0 :     GNUNET_free (leh->url);
     237           0 :     GNUNET_free (leh);
     238           0 :     return NULL;
     239             :   }
     240           1 :   ctx = TALER_AUDITOR_handle_to_context_ (auditor);
     241           1 :   leh->job = GNUNET_CURL_job_add (ctx,
     242             :                                   eh,
     243             :                                   &handle_exchanges_finished,
     244             :                                   leh);
     245           1 :   return leh;
     246             : }
     247             : 
     248             : 
     249             : /**
     250             :  * Cancel a list exchanges request.  This function cannot be used
     251             :  * on a request handle if a response is already served for it.
     252             :  *
     253             :  * @param leh the list exchanges request handle
     254             :  */
     255             : void
     256           1 : TALER_AUDITOR_list_exchanges_cancel (struct
     257             :                                      TALER_AUDITOR_ListExchangesHandle *leh)
     258             : {
     259           1 :   if (NULL != leh->job)
     260             :   {
     261           0 :     GNUNET_CURL_job_cancel (leh->job);
     262           0 :     leh->job = NULL;
     263             :   }
     264           1 :   GNUNET_free (leh->url);
     265           1 :   GNUNET_free (leh);
     266           1 : }
     267             : 
     268             : 
     269             : /* end of auditor_api_exchanges.c */

Generated by: LCOV version 1.14