frosix

Multiparty signature service (experimental)
Log | Files | Refs | README | LICENSE

frosix_api_seed_get.c (4013B)


      1 /*
      2   This file is part of ANASTASIS
      3   Copyright (C) 2014-2019 Anastasis SARL
      4 
      5   ANASTASIS is free software; you can redistribute it and/or modify
      6   it under the terms of the GNU General Public License as
      7   published by the Free Software Foundation; either version 2.1,
      8   or (at your option) any later version.
      9 
     10   ANASTASIS is distributed in the hope that it will be useful, but
     11   WITHOUT ANY WARRANTY; without even the implied warranty of
     12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13   GNU General Public License for more details.
     14 
     15   You should have received a copy of the GNU General Public
     16   License along with ANASTASIS; see the file COPYING.LGPL.  If not,
     17   see <http://www.gnu.org/licenses/>
     18 */
     19 
     20 /**
     21  * @file restclient/frosix_api_seed_get.c
     22  * @brief Implementation of the /policy GET and POST
     23  * @author Christian Grothoff
     24  * @author Dennis Neufeld
     25  * @author Dominik Meister
     26  */
     27 #include "platform.h"
     28 #include <curl/curl.h>
     29 #include <microhttpd.h> /* just for HTTP status codes */
     30 #include "frosix_service.h"
     31 #include "frosix_api_curl_defaults.h"
     32 
     33 
     34 void
     35 FROSIX_seed_get_cancel (struct FROSIX_SeedGetOperation *sgo)
     36 {
     37   if (NULL != sgo->job)
     38   {
     39     GNUNET_CURL_job_cancel (sgo->job);
     40     sgo->job = NULL;
     41   }
     42   GNUNET_free (sgo->url);
     43   GNUNET_free (sgo);
     44 }
     45 
     46 
     47 /**
     48  * Process GET /seed response
     49  */
     50 static void
     51 handle_seed_get_finished (void *cls,
     52                           long response_code,
     53                           const void *data,
     54                           size_t data_size)
     55 {
     56   struct FROSIX_SeedGetOperation *sgo = cls;
     57 
     58   sgo->job = NULL;
     59   switch (response_code)
     60   {
     61   case 0:
     62     /* Hard error */
     63     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     64                 "Backend didn't even return from GET /seed\n");
     65     break;
     66   case MHD_HTTP_OK:
     67     {
     68       struct FROSIX_ProviderSeed ps;
     69 
     70       if (sizeof (*ps.seed) != data_size)
     71       {
     72         GNUNET_break_op (0);
     73         response_code = 0;
     74         break;
     75       }
     76       /* Sucess, call callback with all details! */
     77       ps.provider_index = sgo->provider_index;
     78       memset (&ps.seed, 0, sizeof (ps.seed));
     79       ps.seed = data;
     80 
     81       sgo->cb (sgo->cb_cls,
     82                response_code,
     83                &ps);
     84       sgo->cb = NULL;
     85       FROSIX_seed_get_cancel (sgo);
     86       return;
     87     }
     88   case MHD_HTTP_BAD_REQUEST:
     89     /* This should never happen, either us or the anastasis server is buggy
     90        (or API version conflict); just pass JSON reply to the application */
     91     break;
     92   case MHD_HTTP_NOT_FOUND:
     93     /* Nothing really to verify */
     94     break;
     95   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     96     /* Server had an internal issue; we should retry, but this API
     97        leaves this to the application */
     98     break;
     99   default:
    100     /* unexpected response code */
    101     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    102                 "Unexpected response code %u\n",
    103                 (unsigned int) response_code);
    104     GNUNET_break (0);
    105     response_code = 0;
    106     break;
    107   }
    108   sgo->cb (sgo->cb_cls,
    109            response_code,
    110            NULL);
    111   sgo->cb = NULL;
    112   FROSIX_seed_get_cancel (sgo);
    113 }
    114 
    115 
    116 struct FROSIX_SeedGetOperation *
    117 FROSIX_seed_get (
    118   struct GNUNET_CURL_Context *ctx,
    119   const char *backend_url,
    120   uint8_t provider_index,
    121   FROSIX_SeedGetCallback cb,
    122   void *cb_cls)
    123 {
    124   struct FROSIX_SeedGetOperation *sgo;
    125 
    126   GNUNET_assert (NULL != cb);
    127   sgo = GNUNET_new (struct FROSIX_SeedGetOperation);
    128   sgo->url = TALER_url_join (backend_url,
    129                              "seed",
    130                              NULL);
    131   sgo->provider_index = provider_index;
    132 
    133   sgo->ctx = ctx;
    134   sgo->cb = cb;
    135   sgo->cb_cls = cb_cls;
    136   {
    137     CURL *eh;
    138 
    139     eh = FROSIX_curl_easy_get_ (sgo->url);
    140     sgo->job = GNUNET_CURL_job_add_raw (ctx,
    141                                         eh,
    142                                         NULL,
    143                                         &handle_seed_get_finished,
    144                                         sgo);
    145   }
    146 
    147   if (NULL == sgo->job)
    148   {
    149     GNUNET_free (sgo->url);
    150     GNUNET_free (sgo);
    151     return NULL;
    152   }
    153 
    154   return sgo;
    155 }