diff options
Diffstat (limited to 'src/zklaim/zklaim_api.c')
-rw-r--r-- | src/zklaim/zklaim_api.c | 433 |
1 files changed, 433 insertions, 0 deletions
diff --git a/src/zklaim/zklaim_api.c b/src/zklaim/zklaim_api.c new file mode 100644 index 000000000..f54ede342 --- /dev/null +++ b/src/zklaim/zklaim_api.c | |||
@@ -0,0 +1,433 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2013, 2016 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet 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 GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | |||
19 | /** | ||
20 | * @file zklaim/zklaim_api.c | ||
21 | * @brief api to interact with the zklaim service | ||
22 | * @author Martin Schanzenbach | ||
23 | */ | ||
24 | #include "platform.h" | ||
25 | #include "gnunet_util_lib.h" | ||
26 | #include "gnunet_constants.h" | ||
27 | #include "gnunet_protocols.h" | ||
28 | #include "gnunet_zklaim_service.h" | ||
29 | #include "zklaim/zklaim.h" | ||
30 | #include "zklaim_api.h" | ||
31 | |||
32 | #define LOG(kind,...) GNUNET_log_from (kind, "zklaim-api",__VA_ARGS__) | ||
33 | |||
34 | /** | ||
35 | * Handle for an ego. | ||
36 | */ | ||
37 | struct GNUNET_ZKLAIM_Context | ||
38 | { | ||
39 | /** | ||
40 | * ZKlaim context. | ||
41 | */ | ||
42 | struct zklaim_ctx *zk_ctx; | ||
43 | |||
44 | /** | ||
45 | * Current name associated with this context. | ||
46 | */ | ||
47 | char *name; | ||
48 | |||
49 | /** | ||
50 | * Attributes associated with context | ||
51 | */ | ||
52 | char *attrs; | ||
53 | |||
54 | /** | ||
55 | * Client context associated with this ego. | ||
56 | */ | ||
57 | void *ctx; | ||
58 | |||
59 | }; | ||
60 | |||
61 | |||
62 | /** | ||
63 | * Handle for an operation with the service. | ||
64 | */ | ||
65 | struct GNUNET_ZKLAIM_Operation | ||
66 | { | ||
67 | |||
68 | /** | ||
69 | * Main handle. | ||
70 | */ | ||
71 | struct GNUNET_ZKLAIM_Handle *h; | ||
72 | |||
73 | /** | ||
74 | * We keep operations in a DLL. | ||
75 | */ | ||
76 | struct GNUNET_ZKLAIM_Operation *next; | ||
77 | |||
78 | /** | ||
79 | * We keep operations in a DLL. | ||
80 | */ | ||
81 | struct GNUNET_ZKLAIM_Operation *prev; | ||
82 | |||
83 | /** | ||
84 | * Message to send to the zklaim service. | ||
85 | * Allocated at the end of this struct. | ||
86 | */ | ||
87 | const struct GNUNET_MessageHeader *msg; | ||
88 | |||
89 | /** | ||
90 | * Continuation to invoke with the result of the transmission; @e cb | ||
91 | * will be NULL in this case. | ||
92 | */ | ||
93 | GNUNET_ZKLAIM_ContinuationWithStatus cont; | ||
94 | |||
95 | /** | ||
96 | * Closure for @e cont or @e cb. | ||
97 | */ | ||
98 | void *cls; | ||
99 | |||
100 | }; | ||
101 | |||
102 | |||
103 | /** | ||
104 | * Handle for the service. | ||
105 | */ | ||
106 | struct GNUNET_ZKLAIM_Handle | ||
107 | { | ||
108 | /** | ||
109 | * Configuration to use. | ||
110 | */ | ||
111 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
112 | |||
113 | /** | ||
114 | * Connection to service. | ||
115 | */ | ||
116 | struct GNUNET_MQ_Handle *mq; | ||
117 | |||
118 | /** | ||
119 | * Hash map from the hash of the public key to the | ||
120 | * respective `GNUNET_ZKLAIM_Ego` handle. | ||
121 | */ | ||
122 | struct GNUNET_CONTAINER_MultiHashMap *egos; | ||
123 | |||
124 | /** | ||
125 | * Closure for @e cb. | ||
126 | */ | ||
127 | void *cb_cls; | ||
128 | |||
129 | /** | ||
130 | * Head of active operations. | ||
131 | */ | ||
132 | struct GNUNET_ZKLAIM_Operation *op_head; | ||
133 | |||
134 | /** | ||
135 | * Tail of active operations. | ||
136 | */ | ||
137 | struct GNUNET_ZKLAIM_Operation *op_tail; | ||
138 | |||
139 | /** | ||
140 | * Task doing exponential back-off trying to reconnect. | ||
141 | */ | ||
142 | struct GNUNET_SCHEDULER_Task *reconnect_task; | ||
143 | |||
144 | /** | ||
145 | * Time for next connect retry. | ||
146 | */ | ||
147 | struct GNUNET_TIME_Relative reconnect_delay; | ||
148 | |||
149 | }; | ||
150 | |||
151 | |||
152 | /** | ||
153 | * Try again to connect to the zklaim service. | ||
154 | * | ||
155 | * @param cls handle to the zklaim service. | ||
156 | */ | ||
157 | static void | ||
158 | reconnect (void *cls); | ||
159 | |||
160 | /** | ||
161 | * Reschedule a connect attempt to the service. | ||
162 | * | ||
163 | * @param h transport service to reconnect | ||
164 | */ | ||
165 | static void | ||
166 | reschedule_connect (struct GNUNET_ZKLAIM_Handle *h) | ||
167 | { | ||
168 | struct GNUNET_ZKLAIM_Operation *op; | ||
169 | |||
170 | GNUNET_assert (NULL == h->reconnect_task); | ||
171 | |||
172 | if (NULL != h->mq) | ||
173 | { | ||
174 | GNUNET_MQ_destroy (h->mq); | ||
175 | h->mq = NULL; | ||
176 | } | ||
177 | while (NULL != (op = h->op_head)) | ||
178 | { | ||
179 | GNUNET_CONTAINER_DLL_remove (h->op_head, | ||
180 | h->op_tail, | ||
181 | op); | ||
182 | if (NULL != op->cont) | ||
183 | op->cont (op->cls, | ||
184 | GNUNET_SYSERR, | ||
185 | "Error in communication with the zklaim service"); | ||
186 | GNUNET_free (op); | ||
187 | } | ||
188 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
189 | "Scheduling task to reconnect to zklaim service in %s.\n", | ||
190 | GNUNET_STRINGS_relative_time_to_string (h->reconnect_delay, | ||
191 | GNUNET_YES)); | ||
192 | h->reconnect_task = | ||
193 | GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, | ||
194 | &reconnect, | ||
195 | h); | ||
196 | h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay); | ||
197 | } | ||
198 | |||
199 | |||
200 | /** | ||
201 | * Generic error handler, called with the appropriate error code and | ||
202 | * the same closure specified at the creation of the message queue. | ||
203 | * Not every message queue implementation supports an error handler. | ||
204 | * | ||
205 | * @param cls closure with the `struct GNUNET_ZKLAIM_Handle *` | ||
206 | * @param error error code | ||
207 | */ | ||
208 | static void | ||
209 | mq_error_handler (void *cls, | ||
210 | enum GNUNET_MQ_Error error) | ||
211 | { | ||
212 | struct GNUNET_ZKLAIM_Handle *h = cls; | ||
213 | |||
214 | reschedule_connect (h); | ||
215 | } | ||
216 | |||
217 | |||
218 | /** | ||
219 | * We received a result code from the service. Check the message | ||
220 | * is well-formed. | ||
221 | * | ||
222 | * @param cls closure | ||
223 | * @param rcm result message received | ||
224 | * @return #GNUNET_OK if the message is well-formed | ||
225 | */ | ||
226 | static int | ||
227 | check_zklaim_result_code (void *cls, | ||
228 | const struct ResultCodeMessage *rcm) | ||
229 | { | ||
230 | uint16_t size = ntohs (rcm->header.size) - sizeof (*rcm); | ||
231 | const char *str = (const char *) &rcm[1]; | ||
232 | |||
233 | if (0 == size) | ||
234 | return GNUNET_OK; | ||
235 | if ('\0' != str[size - 1]) | ||
236 | { | ||
237 | GNUNET_break (0); | ||
238 | return GNUNET_SYSERR; | ||
239 | } | ||
240 | return GNUNET_OK; | ||
241 | } | ||
242 | |||
243 | |||
244 | /** | ||
245 | * We received a result code from the service. | ||
246 | * | ||
247 | * @param cls closure | ||
248 | * @param rcm result message received | ||
249 | */ | ||
250 | static void | ||
251 | handle_zklaim_result_code (void *cls, | ||
252 | const struct ResultCodeMessage *rcm) | ||
253 | { | ||
254 | struct GNUNET_ZKLAIM_Handle *h = cls; | ||
255 | struct GNUNET_ZKLAIM_Operation *op; | ||
256 | uint16_t size = ntohs (rcm->header.size) - sizeof (*rcm); | ||
257 | const char *str = (0 == size) ? NULL : (const char *) &rcm[1]; | ||
258 | |||
259 | op = h->op_head; | ||
260 | if (NULL == op) | ||
261 | { | ||
262 | GNUNET_break (0); | ||
263 | reschedule_connect (h); | ||
264 | return; | ||
265 | } | ||
266 | GNUNET_CONTAINER_DLL_remove (h->op_head, | ||
267 | h->op_tail, | ||
268 | op); | ||
269 | if (NULL != op->cont) | ||
270 | op->cont (op->cls, | ||
271 | GNUNET_OK, | ||
272 | str); | ||
273 | GNUNET_free (op); | ||
274 | } | ||
275 | |||
276 | |||
277 | /** | ||
278 | * Try again to connect to the zklaim service. | ||
279 | * | ||
280 | * @param cls handle to the zklaim service. | ||
281 | */ | ||
282 | static void | ||
283 | reconnect (void *cls) | ||
284 | { | ||
285 | struct GNUNET_ZKLAIM_Handle *h = cls; | ||
286 | struct GNUNET_MQ_MessageHandler handlers[] = { | ||
287 | GNUNET_MQ_hd_var_size (zklaim_result_code, | ||
288 | GNUNET_MESSAGE_TYPE_ZKLAIM_RESULT_CODE, | ||
289 | struct ResultCodeMessage, | ||
290 | h), | ||
291 | GNUNET_MQ_handler_end () | ||
292 | }; | ||
293 | |||
294 | h->reconnect_task = NULL; | ||
295 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
296 | "Connecting to zklaim service.\n"); | ||
297 | GNUNET_assert (NULL == h->mq); | ||
298 | h->mq = GNUNET_CLIENT_connect (h->cfg, | ||
299 | "zklaim", | ||
300 | handlers, | ||
301 | &mq_error_handler, | ||
302 | h); | ||
303 | if (NULL == h->mq) | ||
304 | return; | ||
305 | } | ||
306 | |||
307 | |||
308 | /** | ||
309 | * Connect to the zklaim service. | ||
310 | * | ||
311 | * @param cfg the configuration to use | ||
312 | * @param cb function to call on all zklaim events, can be NULL | ||
313 | * @param cb_cls closure for @a cb | ||
314 | * @return handle to use | ||
315 | */ | ||
316 | struct GNUNET_ZKLAIM_Handle * | ||
317 | GNUNET_ZKLAIM_connect (const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
318 | { | ||
319 | struct GNUNET_ZKLAIM_Handle *h; | ||
320 | |||
321 | h = GNUNET_new (struct GNUNET_ZKLAIM_Handle); | ||
322 | h->cfg = cfg; | ||
323 | reconnect (h); | ||
324 | if (NULL == h->mq) | ||
325 | { | ||
326 | GNUNET_free (h); | ||
327 | return NULL; | ||
328 | } | ||
329 | return h; | ||
330 | } | ||
331 | |||
332 | |||
333 | /** | ||
334 | * Create a new zklaim with the given name. | ||
335 | * | ||
336 | * @param h zklaim service to use | ||
337 | * @param name desired name | ||
338 | * @param cont function to call with the result (will only be called once) | ||
339 | * @param cont_cls closure for @a cont | ||
340 | * @return handle to abort the operation | ||
341 | */ | ||
342 | struct GNUNET_ZKLAIM_Operation * | ||
343 | GNUNET_ZKLAIM_context_create (struct GNUNET_ZKLAIM_Handle *h, | ||
344 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *pk, | ||
345 | const char *name, | ||
346 | const char *attr_list, | ||
347 | GNUNET_ZKLAIM_ContinuationWithStatus cont, | ||
348 | void *cont_cls) | ||
349 | { | ||
350 | struct GNUNET_ZKLAIM_Operation *op; | ||
351 | struct GNUNET_MQ_Envelope *env; | ||
352 | struct CreateRequestMessage *crm; | ||
353 | size_t slen; | ||
354 | |||
355 | if (NULL == h->mq) | ||
356 | return NULL; | ||
357 | slen = strlen (name) + 1; | ||
358 | if (slen >= GNUNET_MAX_MESSAGE_SIZE - sizeof (struct CreateRequestMessage)) | ||
359 | { | ||
360 | GNUNET_break (0); | ||
361 | return NULL; | ||
362 | } | ||
363 | op = GNUNET_new (struct GNUNET_ZKLAIM_Operation); | ||
364 | op->h = h; | ||
365 | op->cont = cont; | ||
366 | op->cls = cont_cls; | ||
367 | GNUNET_CONTAINER_DLL_insert_tail (h->op_head, | ||
368 | h->op_tail, | ||
369 | op); | ||
370 | env = GNUNET_MQ_msg_extra (crm, | ||
371 | slen, | ||
372 | GNUNET_MESSAGE_TYPE_ZKLAIM_CREATE); | ||
373 | crm->name_len = htons (slen); | ||
374 | crm->reserved = htons (0); | ||
375 | crm->private_key = *pk; | ||
376 | GNUNET_memcpy (&crm[1], | ||
377 | name, | ||
378 | slen); | ||
379 | GNUNET_MQ_send (h->mq, | ||
380 | env); | ||
381 | //TODO add attrs | ||
382 | return op; | ||
383 | } | ||
384 | |||
385 | |||
386 | /** | ||
387 | * Cancel an zklaim operation. Note that the operation MAY still | ||
388 | * be executed; this merely cancels the continuation; if the request | ||
389 | * was already transmitted, the service may still choose to complete | ||
390 | * the operation. | ||
391 | * | ||
392 | * @param op operation to cancel | ||
393 | */ | ||
394 | void | ||
395 | GNUNET_ZKLAIM_cancel (struct GNUNET_ZKLAIM_Operation *op) | ||
396 | { | ||
397 | op->cont = NULL; | ||
398 | } | ||
399 | |||
400 | |||
401 | /** | ||
402 | * Disconnect from zklaim service | ||
403 | * | ||
404 | * @param h handle to destroy | ||
405 | */ | ||
406 | void | ||
407 | GNUNET_ZKLAIM_disconnect (struct GNUNET_ZKLAIM_Handle *h) | ||
408 | { | ||
409 | struct GNUNET_ZKLAIM_Operation *op; | ||
410 | |||
411 | GNUNET_assert (NULL != h); | ||
412 | if (h->reconnect_task != NULL) | ||
413 | { | ||
414 | GNUNET_SCHEDULER_cancel (h->reconnect_task); | ||
415 | h->reconnect_task = NULL; | ||
416 | } | ||
417 | while (NULL != (op = h->op_head)) | ||
418 | { | ||
419 | GNUNET_break (NULL == op->cont); | ||
420 | GNUNET_CONTAINER_DLL_remove (h->op_head, | ||
421 | h->op_tail, | ||
422 | op); | ||
423 | GNUNET_free (op); | ||
424 | } | ||
425 | if (NULL != h->mq) | ||
426 | { | ||
427 | GNUNET_MQ_destroy (h->mq); | ||
428 | h->mq = NULL; | ||
429 | } | ||
430 | GNUNET_free (h); | ||
431 | } | ||
432 | |||
433 | /* end of zklaim_api.c */ | ||