anastasis_api_redux_state.c (4822B)
1 /* 2 This file is part of Anastasis 3 Copyright (C) 2020, 2021, 2022 Anastasis SARL 4 5 Anastasis 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 Anastasis 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 Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file reducer/anastasis_api_redux_state.c 18 * @brief lookup helpers for the typed reducer state 19 * @author Christian Grothoff 20 */ 21 #include "platform.h" 22 #include "anastasis_redux.h" 23 #include "anastasis_api_redux.h" 24 #include "anastasis_api_redux_state.h" 25 26 27 void 28 ANASTASIS_REDUX_provider_url_set_ (struct ANASTASIS_ProviderUrl *pu, 29 const char *url) 30 { 31 GNUNET_free (pu->url); 32 pu->url = GNUNET_strdup (url); 33 } 34 35 36 struct ANASTASIS_ReduxProvider * 37 ANASTASIS_REDUX_provider_find_ (const struct ANASTASIS_ReduxCommon *common, 38 const char *url) 39 { 40 for (unsigned int i = 0; i < common->providers_len; i++) 41 if (0 == strcmp (common->providers[i].url.url, 42 url)) 43 return &common->providers[i]; 44 return NULL; 45 } 46 47 48 struct ANASTASIS_ReduxProvider * 49 ANASTASIS_REDUX_provider_get_ (struct ANASTASIS_ReduxCommon *common, 50 const char *url) 51 { 52 struct ANASTASIS_ReduxProvider *p; 53 unsigned int off; 54 55 p = ANASTASIS_REDUX_provider_find_ (common, 56 url); 57 if (NULL != p) 58 return p; 59 off = common->providers_len; 60 GNUNET_array_append (common->providers, 61 common->providers_len, 62 (struct ANASTASIS_ReduxProvider) { 63 .status = ANASTASIS_RPS_NOT_CONTACTED 64 }); 65 common->have_providers = true; 66 p = &common->providers[off]; 67 ANASTASIS_REDUX_provider_url_set_ (&p->url, 68 url); 69 return p; 70 } 71 72 73 struct ANASTASIS_ReduxFeedback * 74 ANASTASIS_REDUX_feedback_find_ (const struct ANASTASIS_ReduxRecovery *rr, 75 const struct ANASTASIS_CRYPTO_TruthUUIDP *uuid) 76 { 77 for (unsigned int i = 0; i < rr->challenge_feedback_len; i++) 78 if (0 == 79 GNUNET_memcmp (&rr->challenge_feedback[i].uuid, 80 uuid)) 81 return &rr->challenge_feedback[i]; 82 return NULL; 83 } 84 85 86 struct ANASTASIS_ReduxFeedback * 87 ANASTASIS_REDUX_feedback_get_ (struct ANASTASIS_ReduxRecovery *rr, 88 const struct ANASTASIS_CRYPTO_TruthUUIDP *uuid) 89 { 90 struct ANASTASIS_ReduxFeedback *fb; 91 unsigned int off; 92 93 fb = ANASTASIS_REDUX_feedback_find_ (rr, 94 uuid); 95 if (NULL != fb) 96 { 97 /* Overwriting an earlier verdict: drop whatever the old variant 98 owned so the caller can fill in a fresh one. */ 99 ANASTASIS_REDUX_feedback_clear_ (fb); 100 return fb; 101 } 102 off = rr->challenge_feedback_len; 103 GNUNET_array_append (rr->challenge_feedback, 104 rr->challenge_feedback_len, 105 (struct ANASTASIS_ReduxFeedback) { 106 .uuid = *uuid 107 }); 108 rr->have_challenge_feedback = true; 109 return &rr->challenge_feedback[off]; 110 } 111 112 113 struct ANASTASIS_ReduxChallengeInfo * 114 ANASTASIS_REDUX_challenge_find_ (const struct ANASTASIS_ReduxRecovery *rr, 115 const struct 116 ANASTASIS_CRYPTO_TruthUUIDP *uuid) 117 { 118 if (NULL == rr->ri) 119 return NULL; 120 for (unsigned int i = 0; i < rr->ri->challenges_len; i++) 121 if (0 == 122 GNUNET_memcmp (&rr->ri->challenges[i].uuid, 123 uuid)) 124 return &rr->ri->challenges[i]; 125 return NULL; 126 } 127 128 129 void 130 ANASTASIS_REDUX_return_ (struct ANASTASIS_ReduxState *rs, 131 ANASTASIS_ActionCallback cb, 132 void *cb_cls, 133 enum TALER_ErrorCode ec) 134 { 135 json_t *json; 136 137 json = ANASTASIS_REDUX_state_serialize_ (rs); 138 GNUNET_assert (NULL != json); 139 cb (cb_cls, 140 ec, 141 json); 142 json_decref (json); 143 ANASTASIS_REDUX_state_free_ (rs); 144 } 145 146 147 void 148 ANASTASIS_REDUX_fail_ (struct ANASTASIS_ReduxState *rs, 149 ANASTASIS_ActionCallback cb, 150 void *cb_cls, 151 enum TALER_ErrorCode ec, 152 const char *detail) 153 { 154 ANASTASIS_REDUX_state_free_ (rs); 155 ANASTASIS_redux_fail_ (cb, 156 cb_cls, 157 ec, 158 detail); 159 } 160 161 162 /* end of anastasis_api_redux_state.c */