diff options
Diffstat (limited to 'src/set/set_api.c')
-rw-r--r-- | src/set/set_api.c | 1260 |
1 files changed, 0 insertions, 1260 deletions
diff --git a/src/set/set_api.c b/src/set/set_api.c deleted file mode 100644 index 6b3dc940c..000000000 --- a/src/set/set_api.c +++ /dev/null | |||
@@ -1,1260 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2012-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 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /** | ||
21 | * @file set/set_api.c | ||
22 | * @brief api for the set service | ||
23 | * @author Florian Dold | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_protocols.h" | ||
29 | #include "gnunet_set_service.h" | ||
30 | #include "set.h" | ||
31 | |||
32 | |||
33 | #define LOG(kind, ...) GNUNET_log_from (kind, "set-api", __VA_ARGS__) | ||
34 | |||
35 | struct SetCopyRequest | ||
36 | { | ||
37 | struct SetCopyRequest *next; | ||
38 | |||
39 | struct SetCopyRequest *prev; | ||
40 | |||
41 | void *cls; | ||
42 | |||
43 | GNUNET_SET_CopyReadyCallback cb; | ||
44 | }; | ||
45 | |||
46 | /** | ||
47 | * Opaque handle to a set. | ||
48 | */ | ||
49 | struct GNUNET_SET_Handle | ||
50 | { | ||
51 | /** | ||
52 | * Message queue for @e client. | ||
53 | */ | ||
54 | struct GNUNET_MQ_Handle *mq; | ||
55 | |||
56 | /** | ||
57 | * Linked list of operations on the set. | ||
58 | */ | ||
59 | struct GNUNET_SET_OperationHandle *ops_head; | ||
60 | |||
61 | /** | ||
62 | * Linked list of operations on the set. | ||
63 | */ | ||
64 | struct GNUNET_SET_OperationHandle *ops_tail; | ||
65 | |||
66 | /** | ||
67 | * Callback for the current iteration over the set, | ||
68 | * NULL if no iterator is active. | ||
69 | */ | ||
70 | GNUNET_SET_ElementIterator iterator; | ||
71 | |||
72 | /** | ||
73 | * Closure for @e iterator | ||
74 | */ | ||
75 | void *iterator_cls; | ||
76 | |||
77 | /** | ||
78 | * Should the set be destroyed once all operations are gone? | ||
79 | * #GNUNET_SYSERR if #GNUNET_SET_destroy() must raise this flag, | ||
80 | * #GNUNET_YES if #GNUNET_SET_destroy() did raise this flag. | ||
81 | */ | ||
82 | int destroy_requested; | ||
83 | |||
84 | /** | ||
85 | * Has the set become invalid (e.g. service died)? | ||
86 | */ | ||
87 | int invalid; | ||
88 | |||
89 | /** | ||
90 | * Both client and service count the number of iterators | ||
91 | * created so far to match replies with iterators. | ||
92 | */ | ||
93 | uint16_t iteration_id; | ||
94 | |||
95 | /** | ||
96 | * Configuration, needed when creating (lazy) copies. | ||
97 | */ | ||
98 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
99 | |||
100 | /** | ||
101 | * Doubly linked list of copy requests. | ||
102 | */ | ||
103 | struct SetCopyRequest *copy_req_head; | ||
104 | |||
105 | /** | ||
106 | * Doubly linked list of copy requests. | ||
107 | */ | ||
108 | struct SetCopyRequest *copy_req_tail; | ||
109 | }; | ||
110 | |||
111 | |||
112 | /** | ||
113 | * Handle for a set operation request from another peer. | ||
114 | */ | ||
115 | struct GNUNET_SET_Request | ||
116 | { | ||
117 | /** | ||
118 | * Id of the request, used to identify the request when | ||
119 | * accepting/rejecting it. | ||
120 | */ | ||
121 | uint32_t accept_id; | ||
122 | |||
123 | /** | ||
124 | * Has the request been accepted already? | ||
125 | * #GNUNET_YES/#GNUNET_NO | ||
126 | */ | ||
127 | int accepted; | ||
128 | }; | ||
129 | |||
130 | |||
131 | /** | ||
132 | * Handle to an operation. Only known to the service after committing | ||
133 | * the handle with a set. | ||
134 | */ | ||
135 | struct GNUNET_SET_OperationHandle | ||
136 | { | ||
137 | /** | ||
138 | * Function to be called when we have a result, | ||
139 | * or an error. | ||
140 | */ | ||
141 | GNUNET_SET_ResultIterator result_cb; | ||
142 | |||
143 | /** | ||
144 | * Closure for @e result_cb. | ||
145 | */ | ||
146 | void *result_cls; | ||
147 | |||
148 | /** | ||
149 | * Local set used for the operation, | ||
150 | * NULL if no set has been provided by conclude yet. | ||
151 | */ | ||
152 | struct GNUNET_SET_Handle *set; | ||
153 | |||
154 | /** | ||
155 | * Message sent to the server on calling conclude, | ||
156 | * NULL if conclude has been called. | ||
157 | */ | ||
158 | struct GNUNET_MQ_Envelope *conclude_mqm; | ||
159 | |||
160 | /** | ||
161 | * Address of the request if in the conclude message, | ||
162 | * used to patch the request id into the message when the set is known. | ||
163 | */ | ||
164 | uint32_t *request_id_addr; | ||
165 | |||
166 | /** | ||
167 | * Handles are kept in a linked list. | ||
168 | */ | ||
169 | struct GNUNET_SET_OperationHandle *prev; | ||
170 | |||
171 | /** | ||
172 | * Handles are kept in a linked list. | ||
173 | */ | ||
174 | struct GNUNET_SET_OperationHandle *next; | ||
175 | |||
176 | /** | ||
177 | * Request ID to identify the operation within the set. | ||
178 | */ | ||
179 | uint32_t request_id; | ||
180 | }; | ||
181 | |||
182 | |||
183 | /** | ||
184 | * Opaque handle to a listen operation. | ||
185 | */ | ||
186 | struct GNUNET_SET_ListenHandle | ||
187 | { | ||
188 | /** | ||
189 | * Message queue for the client. | ||
190 | */ | ||
191 | struct GNUNET_MQ_Handle*mq; | ||
192 | |||
193 | /** | ||
194 | * Configuration handle for the listener, stored | ||
195 | * here to be able to reconnect transparently on | ||
196 | * connection failure. | ||
197 | */ | ||
198 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
199 | |||
200 | /** | ||
201 | * Function to call on a new incoming request, | ||
202 | * or on error. | ||
203 | */ | ||
204 | GNUNET_SET_ListenCallback listen_cb; | ||
205 | |||
206 | /** | ||
207 | * Closure for @e listen_cb. | ||
208 | */ | ||
209 | void *listen_cls; | ||
210 | |||
211 | /** | ||
212 | * Application ID we listen for. | ||
213 | */ | ||
214 | struct GNUNET_HashCode app_id; | ||
215 | |||
216 | /** | ||
217 | * Time to wait until we try to reconnect on failure. | ||
218 | */ | ||
219 | struct GNUNET_TIME_Relative reconnect_backoff; | ||
220 | |||
221 | /** | ||
222 | * Task for reconnecting when the listener fails. | ||
223 | */ | ||
224 | struct GNUNET_SCHEDULER_Task *reconnect_task; | ||
225 | |||
226 | /** | ||
227 | * Operation we listen for. | ||
228 | */ | ||
229 | enum GNUNET_SET_OperationType operation; | ||
230 | }; | ||
231 | |||
232 | |||
233 | /* mutual recursion with handle_copy_lazy */ | ||
234 | static struct GNUNET_SET_Handle * | ||
235 | create_internal (const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
236 | enum GNUNET_SET_OperationType op, | ||
237 | const uint32_t *cookie); | ||
238 | |||
239 | |||
240 | /** | ||
241 | * Handle element for iteration over the set. Notifies the | ||
242 | * iterator and sends an acknowledgement to the service. | ||
243 | * | ||
244 | * @param cls the `struct GNUNET_SET_Handle *` | ||
245 | * @param msg the message | ||
246 | */ | ||
247 | static void | ||
248 | handle_copy_lazy (void *cls, | ||
249 | const struct GNUNET_SET_CopyLazyResponseMessage *msg) | ||
250 | { | ||
251 | struct GNUNET_SET_Handle *set = cls; | ||
252 | struct SetCopyRequest *req; | ||
253 | struct GNUNET_SET_Handle *new_set; | ||
254 | |||
255 | req = set->copy_req_head; | ||
256 | if (NULL == req) | ||
257 | { | ||
258 | /* Service sent us unsolicited lazy copy response */ | ||
259 | GNUNET_break (0); | ||
260 | return; | ||
261 | } | ||
262 | |||
263 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
264 | "Handling response to lazy copy\n"); | ||
265 | GNUNET_CONTAINER_DLL_remove (set->copy_req_head, | ||
266 | set->copy_req_tail, | ||
267 | req); | ||
268 | // We pass none as operation here, since it doesn't matter when | ||
269 | // cloning. | ||
270 | new_set = create_internal (set->cfg, | ||
271 | GNUNET_SET_OPERATION_NONE, | ||
272 | &msg->cookie); | ||
273 | req->cb (req->cls, new_set); | ||
274 | GNUNET_free (req); | ||
275 | } | ||
276 | |||
277 | |||
278 | /** | ||
279 | * Check that the given @a msg is well-formed. | ||
280 | * | ||
281 | * @param cls closure | ||
282 | * @param msg message to check | ||
283 | * @return #GNUNET_OK if message is well-formed | ||
284 | */ | ||
285 | static int | ||
286 | check_iter_element (void *cls, | ||
287 | const struct GNUNET_SET_IterResponseMessage *msg) | ||
288 | { | ||
289 | /* minimum size was already checked, everything else is OK! */ | ||
290 | return GNUNET_OK; | ||
291 | } | ||
292 | |||
293 | |||
294 | /** | ||
295 | * Handle element for iteration over the set. Notifies the | ||
296 | * iterator and sends an acknowledgement to the service. | ||
297 | * | ||
298 | * @param cls the `struct GNUNET_SET_Handle *` | ||
299 | * @param mh the message | ||
300 | */ | ||
301 | static void | ||
302 | handle_iter_element (void *cls, | ||
303 | const struct GNUNET_SET_IterResponseMessage *msg) | ||
304 | { | ||
305 | struct GNUNET_SET_Handle *set = cls; | ||
306 | GNUNET_SET_ElementIterator iter = set->iterator; | ||
307 | struct GNUNET_SET_Element element; | ||
308 | struct GNUNET_SET_IterAckMessage *ack_msg; | ||
309 | struct GNUNET_MQ_Envelope *ev; | ||
310 | uint16_t msize; | ||
311 | |||
312 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
313 | "Received element in set iteration\n"); | ||
314 | msize = ntohs (msg->header.size); | ||
315 | if (set->iteration_id != ntohs (msg->iteration_id)) | ||
316 | { | ||
317 | /* element from a previous iteration, skip! */ | ||
318 | iter = NULL; | ||
319 | } | ||
320 | if (NULL != iter) | ||
321 | { | ||
322 | element.size = msize - sizeof(struct GNUNET_SET_IterResponseMessage); | ||
323 | element.element_type = ntohs (msg->element_type); | ||
324 | element.data = &msg[1]; | ||
325 | iter (set->iterator_cls, | ||
326 | &element); | ||
327 | } | ||
328 | ev = GNUNET_MQ_msg (ack_msg, | ||
329 | GNUNET_MESSAGE_TYPE_SET_ITER_ACK); | ||
330 | ack_msg->send_more = htonl ((NULL != iter)); | ||
331 | GNUNET_MQ_send (set->mq, ev); | ||
332 | } | ||
333 | |||
334 | |||
335 | /** | ||
336 | * Handle message signalling conclusion of iteration over the set. | ||
337 | * Notifies the iterator that we are done. | ||
338 | * | ||
339 | * @param cls the set | ||
340 | * @param mh the message | ||
341 | */ | ||
342 | static void | ||
343 | handle_iter_done (void *cls, | ||
344 | const struct GNUNET_MessageHeader *mh) | ||
345 | { | ||
346 | struct GNUNET_SET_Handle *set = cls; | ||
347 | GNUNET_SET_ElementIterator iter = set->iterator; | ||
348 | |||
349 | if (NULL == iter) | ||
350 | { | ||
351 | /* FIXME: if this is true, could cancel+start a fresh one | ||
352 | cause elements to go to the wrong iteration? */ | ||
353 | LOG (GNUNET_ERROR_TYPE_INFO, | ||
354 | "Service completed set iteration that was already cancelled\n"); | ||
355 | return; | ||
356 | } | ||
357 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
358 | "Set iteration completed\n"); | ||
359 | set->destroy_requested = GNUNET_SYSERR; | ||
360 | set->iterator = NULL; | ||
361 | set->iteration_id++; | ||
362 | iter (set->iterator_cls, | ||
363 | NULL); | ||
364 | if (GNUNET_SYSERR == set->destroy_requested) | ||
365 | set->destroy_requested = GNUNET_NO; | ||
366 | if (GNUNET_YES == set->destroy_requested) | ||
367 | GNUNET_SET_destroy (set); | ||
368 | } | ||
369 | |||
370 | |||
371 | /** | ||
372 | * Check that the given @a msg is well-formed. | ||
373 | * | ||
374 | * @param cls closure | ||
375 | * @param msg message to check | ||
376 | * @return #GNUNET_OK if message is well-formed | ||
377 | */ | ||
378 | static int | ||
379 | check_result (void *cls, | ||
380 | const struct GNUNET_SET_ResultMessage *msg) | ||
381 | { | ||
382 | /* minimum size was already checked, everything else is OK! */ | ||
383 | return GNUNET_OK; | ||
384 | } | ||
385 | |||
386 | |||
387 | /** | ||
388 | * Handle result message for a set operation. | ||
389 | * | ||
390 | * @param cls the set | ||
391 | * @param mh the message | ||
392 | */ | ||
393 | static void | ||
394 | handle_result (void *cls, | ||
395 | const struct GNUNET_SET_ResultMessage *msg) | ||
396 | { | ||
397 | struct GNUNET_SET_Handle *set = cls; | ||
398 | struct GNUNET_SET_OperationHandle *oh; | ||
399 | struct GNUNET_SET_Element e; | ||
400 | enum GNUNET_SET_Status result_status; | ||
401 | int destroy_set; | ||
402 | |||
403 | GNUNET_assert (NULL != set->mq); | ||
404 | result_status = (enum GNUNET_SET_Status) ntohs (msg->result_status); | ||
405 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
406 | "Got result message with status %d\n", | ||
407 | result_status); | ||
408 | |||
409 | oh = GNUNET_MQ_assoc_get (set->mq, | ||
410 | ntohl (msg->request_id)); | ||
411 | if (NULL == oh) | ||
412 | { | ||
413 | /* 'oh' can be NULL if we canceled the operation, but the service | ||
414 | did not get the cancel message yet. */ | ||
415 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
416 | "Ignoring result from canceled operation\n"); | ||
417 | return; | ||
418 | } | ||
419 | |||
420 | switch (result_status) | ||
421 | { | ||
422 | case GNUNET_SET_STATUS_OK: | ||
423 | case GNUNET_SET_STATUS_ADD_LOCAL: | ||
424 | case GNUNET_SET_STATUS_ADD_REMOTE: | ||
425 | goto do_element; | ||
426 | |||
427 | case GNUNET_SET_STATUS_FAILURE: | ||
428 | case GNUNET_SET_STATUS_DONE: | ||
429 | goto do_final; | ||
430 | |||
431 | case GNUNET_SET_STATUS_HALF_DONE: | ||
432 | /* not used anymore */ | ||
433 | GNUNET_assert (0); | ||
434 | } | ||
435 | |||
436 | do_final: | ||
437 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
438 | "Treating result as final status\n"); | ||
439 | GNUNET_MQ_assoc_remove (set->mq, | ||
440 | ntohl (msg->request_id)); | ||
441 | GNUNET_CONTAINER_DLL_remove (set->ops_head, | ||
442 | set->ops_tail, | ||
443 | oh); | ||
444 | /* Need to do this calculation _before_ the result callback, | ||
445 | as IF the application still has a valid set handle, it | ||
446 | may trigger destruction of the set during the callback. */ | ||
447 | destroy_set = (GNUNET_YES == set->destroy_requested) && | ||
448 | (NULL == set->ops_head); | ||
449 | if (NULL != oh->result_cb) | ||
450 | { | ||
451 | oh->result_cb (oh->result_cls, | ||
452 | NULL, | ||
453 | GNUNET_ntohll (msg->current_size), | ||
454 | result_status); | ||
455 | } | ||
456 | else | ||
457 | { | ||
458 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
459 | "No callback for final status\n"); | ||
460 | } | ||
461 | if (destroy_set) | ||
462 | GNUNET_SET_destroy (set); | ||
463 | GNUNET_free (oh); | ||
464 | return; | ||
465 | |||
466 | do_element: | ||
467 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
468 | "Treating result as element\n"); | ||
469 | e.data = &msg[1]; | ||
470 | e.size = ntohs (msg->header.size) - sizeof(struct GNUNET_SET_ResultMessage); | ||
471 | e.element_type = ntohs (msg->element_type); | ||
472 | if (NULL != oh->result_cb) | ||
473 | oh->result_cb (oh->result_cls, | ||
474 | &e, | ||
475 | GNUNET_ntohll (msg->current_size), | ||
476 | result_status); | ||
477 | } | ||
478 | |||
479 | |||
480 | /** | ||
481 | * Destroy the given set operation. | ||
482 | * | ||
483 | * @param oh set operation to destroy | ||
484 | */ | ||
485 | static void | ||
486 | set_operation_destroy (struct GNUNET_SET_OperationHandle *oh) | ||
487 | { | ||
488 | struct GNUNET_SET_Handle *set = oh->set; | ||
489 | struct GNUNET_SET_OperationHandle *h_assoc; | ||
490 | |||
491 | if (NULL != oh->conclude_mqm) | ||
492 | GNUNET_MQ_discard (oh->conclude_mqm); | ||
493 | /* is the operation already committed? */ | ||
494 | if (NULL != set) | ||
495 | { | ||
496 | GNUNET_CONTAINER_DLL_remove (set->ops_head, | ||
497 | set->ops_tail, | ||
498 | oh); | ||
499 | h_assoc = GNUNET_MQ_assoc_remove (set->mq, | ||
500 | oh->request_id); | ||
501 | GNUNET_assert ((NULL == h_assoc) || | ||
502 | (h_assoc == oh)); | ||
503 | } | ||
504 | GNUNET_free (oh); | ||
505 | } | ||
506 | |||
507 | |||
508 | /** | ||
509 | * Cancel the given set operation. We need to send an explicit cancel | ||
510 | * message, as all operations one one set communicate using one | ||
511 | * handle. | ||
512 | * | ||
513 | * @param oh set operation to cancel | ||
514 | */ | ||
515 | void | ||
516 | GNUNET_SET_operation_cancel (struct GNUNET_SET_OperationHandle *oh) | ||
517 | { | ||
518 | struct GNUNET_SET_Handle *set = oh->set; | ||
519 | struct GNUNET_SET_CancelMessage *m; | ||
520 | struct GNUNET_MQ_Envelope *mqm; | ||
521 | |||
522 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
523 | "Cancelling SET operation\n"); | ||
524 | if (NULL != set) | ||
525 | { | ||
526 | mqm = GNUNET_MQ_msg (m, GNUNET_MESSAGE_TYPE_SET_CANCEL); | ||
527 | m->request_id = htonl (oh->request_id); | ||
528 | GNUNET_MQ_send (set->mq, mqm); | ||
529 | } | ||
530 | set_operation_destroy (oh); | ||
531 | if ((NULL != set) && | ||
532 | (GNUNET_YES == set->destroy_requested) && | ||
533 | (NULL == set->ops_head)) | ||
534 | { | ||
535 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
536 | "Destroying set after operation cancel\n"); | ||
537 | GNUNET_SET_destroy (set); | ||
538 | } | ||
539 | } | ||
540 | |||
541 | |||
542 | /** | ||
543 | * We encountered an error communicating with the set service while | ||
544 | * performing a set operation. Report to the application. | ||
545 | * | ||
546 | * @param cls the `struct GNUNET_SET_Handle` | ||
547 | * @param error error code | ||
548 | */ | ||
549 | static void | ||
550 | handle_client_set_error (void *cls, | ||
551 | enum GNUNET_MQ_Error error) | ||
552 | { | ||
553 | struct GNUNET_SET_Handle *set = cls; | ||
554 | GNUNET_SET_ElementIterator iter = set->iterator; | ||
555 | |||
556 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
557 | "Handling client set error %d\n", | ||
558 | error); | ||
559 | while (NULL != set->ops_head) | ||
560 | { | ||
561 | if ((NULL != set->ops_head->result_cb) && | ||
562 | (GNUNET_NO == set->destroy_requested)) | ||
563 | set->ops_head->result_cb (set->ops_head->result_cls, | ||
564 | NULL, | ||
565 | 0, | ||
566 | GNUNET_SET_STATUS_FAILURE); | ||
567 | set_operation_destroy (set->ops_head); | ||
568 | } | ||
569 | set->iterator = NULL; | ||
570 | set->iteration_id++; | ||
571 | set->invalid = GNUNET_YES; | ||
572 | if (NULL != iter) | ||
573 | iter (set->iterator_cls, | ||
574 | NULL); | ||
575 | } | ||
576 | |||
577 | |||
578 | /** | ||
579 | * FIXME. | ||
580 | */ | ||
581 | static struct GNUNET_SET_Handle * | ||
582 | create_internal (const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
583 | enum GNUNET_SET_OperationType op, | ||
584 | const uint32_t *cookie) | ||
585 | { | ||
586 | struct GNUNET_SET_Handle *set = GNUNET_new (struct GNUNET_SET_Handle); | ||
587 | struct GNUNET_MQ_MessageHandler mq_handlers[] = { | ||
588 | GNUNET_MQ_hd_var_size (result, | ||
589 | GNUNET_MESSAGE_TYPE_SET_RESULT, | ||
590 | struct GNUNET_SET_ResultMessage, | ||
591 | set), | ||
592 | GNUNET_MQ_hd_var_size (iter_element, | ||
593 | GNUNET_MESSAGE_TYPE_SET_ITER_ELEMENT, | ||
594 | struct GNUNET_SET_IterResponseMessage, | ||
595 | set), | ||
596 | GNUNET_MQ_hd_fixed_size (iter_done, | ||
597 | GNUNET_MESSAGE_TYPE_SET_ITER_DONE, | ||
598 | struct GNUNET_MessageHeader, | ||
599 | set), | ||
600 | GNUNET_MQ_hd_fixed_size (copy_lazy, | ||
601 | GNUNET_MESSAGE_TYPE_SET_COPY_LAZY_RESPONSE, | ||
602 | struct GNUNET_SET_CopyLazyResponseMessage, | ||
603 | set), | ||
604 | GNUNET_MQ_handler_end () | ||
605 | }; | ||
606 | struct GNUNET_MQ_Envelope *mqm; | ||
607 | struct GNUNET_SET_CreateMessage *create_msg; | ||
608 | struct GNUNET_SET_CopyLazyConnectMessage *copy_msg; | ||
609 | |||
610 | set->cfg = cfg; | ||
611 | set->mq = GNUNET_CLIENT_connect (cfg, | ||
612 | "set", | ||
613 | mq_handlers, | ||
614 | &handle_client_set_error, | ||
615 | set); | ||
616 | if (NULL == set->mq) | ||
617 | { | ||
618 | GNUNET_free (set); | ||
619 | return NULL; | ||
620 | } | ||
621 | if (NULL == cookie) | ||
622 | { | ||
623 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
624 | "Creating new set (operation %u)\n", | ||
625 | op); | ||
626 | mqm = GNUNET_MQ_msg (create_msg, | ||
627 | GNUNET_MESSAGE_TYPE_SET_CREATE); | ||
628 | create_msg->operation = htonl (op); | ||
629 | } | ||
630 | else | ||
631 | { | ||
632 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
633 | "Creating new set (lazy copy)\n"); | ||
634 | mqm = GNUNET_MQ_msg (copy_msg, | ||
635 | GNUNET_MESSAGE_TYPE_SET_COPY_LAZY_CONNECT); | ||
636 | copy_msg->cookie = *cookie; | ||
637 | } | ||
638 | GNUNET_MQ_send (set->mq, | ||
639 | mqm); | ||
640 | return set; | ||
641 | } | ||
642 | |||
643 | |||
644 | /** | ||
645 | * Create an empty set, supporting the specified operation. | ||
646 | * | ||
647 | * @param cfg configuration to use for connecting to the | ||
648 | * set service | ||
649 | * @param op operation supported by the set | ||
650 | * Note that the operation has to be specified | ||
651 | * beforehand, as certain set operations need to maintain | ||
652 | * data structures specific to the operation | ||
653 | * @return a handle to the set | ||
654 | */ | ||
655 | struct GNUNET_SET_Handle * | ||
656 | GNUNET_SET_create (const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
657 | enum GNUNET_SET_OperationType op) | ||
658 | { | ||
659 | struct GNUNET_SET_Handle *set; | ||
660 | |||
661 | set = create_internal (cfg, | ||
662 | op, | ||
663 | NULL); | ||
664 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
665 | "Creating set %p for operation %d\n", | ||
666 | set, | ||
667 | op); | ||
668 | return set; | ||
669 | } | ||
670 | |||
671 | |||
672 | /** | ||
673 | * Add an element to the given set. After the element has been added | ||
674 | * (in the sense of being transmitted to the set service), @a cont | ||
675 | * will be called. Multiple calls to GNUNET_SET_add_element() can be | ||
676 | * queued. | ||
677 | * | ||
678 | * @param set set to add element to | ||
679 | * @param element element to add to the set | ||
680 | * @param cont continuation called after the element has been added | ||
681 | * @param cont_cls closure for @a cont | ||
682 | * @return #GNUNET_OK on success, #GNUNET_SYSERR if the | ||
683 | * set is invalid (e.g. the set service crashed) | ||
684 | */ | ||
685 | int | ||
686 | GNUNET_SET_add_element (struct GNUNET_SET_Handle *set, | ||
687 | const struct GNUNET_SET_Element *element, | ||
688 | GNUNET_SET_Continuation cont, | ||
689 | void *cont_cls) | ||
690 | { | ||
691 | struct GNUNET_MQ_Envelope *mqm; | ||
692 | struct GNUNET_SET_ElementMessage *msg; | ||
693 | |||
694 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
695 | "adding element of type %u to set %p\n", | ||
696 | (unsigned int) element->element_type, | ||
697 | set); | ||
698 | GNUNET_assert (NULL != set); | ||
699 | if (GNUNET_YES == set->invalid) | ||
700 | { | ||
701 | if (NULL != cont) | ||
702 | cont (cont_cls); | ||
703 | return GNUNET_SYSERR; | ||
704 | } | ||
705 | mqm = GNUNET_MQ_msg_extra (msg, | ||
706 | element->size, | ||
707 | GNUNET_MESSAGE_TYPE_SET_ADD); | ||
708 | msg->element_type = htons (element->element_type); | ||
709 | GNUNET_memcpy (&msg[1], | ||
710 | element->data, | ||
711 | element->size); | ||
712 | GNUNET_MQ_notify_sent (mqm, | ||
713 | cont, cont_cls); | ||
714 | GNUNET_MQ_send (set->mq, mqm); | ||
715 | return GNUNET_OK; | ||
716 | } | ||
717 | |||
718 | |||
719 | /** | ||
720 | * Remove an element to the given set. After the element has been | ||
721 | * removed (in the sense of the request being transmitted to the set | ||
722 | * service), @a cont will be called. Multiple calls to | ||
723 | * GNUNET_SET_remove_element() can be queued | ||
724 | * | ||
725 | * @param set set to remove element from | ||
726 | * @param element element to remove from the set | ||
727 | * @param cont continuation called after the element has been removed | ||
728 | * @param cont_cls closure for @a cont | ||
729 | * @return #GNUNET_OK on success, #GNUNET_SYSERR if the | ||
730 | * set is invalid (e.g. the set service crashed) | ||
731 | */ | ||
732 | int | ||
733 | GNUNET_SET_remove_element (struct GNUNET_SET_Handle *set, | ||
734 | const struct GNUNET_SET_Element *element, | ||
735 | GNUNET_SET_Continuation cont, | ||
736 | void *cont_cls) | ||
737 | { | ||
738 | struct GNUNET_MQ_Envelope *mqm; | ||
739 | struct GNUNET_SET_ElementMessage *msg; | ||
740 | |||
741 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
742 | "Removing element from set %p\n", | ||
743 | set); | ||
744 | if (GNUNET_YES == set->invalid) | ||
745 | { | ||
746 | if (NULL != cont) | ||
747 | cont (cont_cls); | ||
748 | return GNUNET_SYSERR; | ||
749 | } | ||
750 | mqm = GNUNET_MQ_msg_extra (msg, | ||
751 | element->size, | ||
752 | GNUNET_MESSAGE_TYPE_SET_REMOVE); | ||
753 | msg->element_type = htons (element->element_type); | ||
754 | GNUNET_memcpy (&msg[1], | ||
755 | element->data, | ||
756 | element->size); | ||
757 | GNUNET_MQ_notify_sent (mqm, | ||
758 | cont, cont_cls); | ||
759 | GNUNET_MQ_send (set->mq, mqm); | ||
760 | return GNUNET_OK; | ||
761 | } | ||
762 | |||
763 | |||
764 | /** | ||
765 | * Destroy the set handle if no operations are left, mark the set | ||
766 | * for destruction otherwise. | ||
767 | * | ||
768 | * @param set set handle to destroy | ||
769 | */ | ||
770 | void | ||
771 | GNUNET_SET_destroy (struct GNUNET_SET_Handle *set) | ||
772 | { | ||
773 | /* destroying set while iterator is active is currently | ||
774 | not supported; we should expand the API to allow | ||
775 | clients to explicitly cancel the iteration! */ | ||
776 | GNUNET_assert (NULL != set); | ||
777 | if ((NULL != set->ops_head) || | ||
778 | (NULL != set->iterator) || | ||
779 | (GNUNET_SYSERR == set->destroy_requested)) | ||
780 | { | ||
781 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
782 | "Set operations are pending, delaying set destruction\n"); | ||
783 | set->destroy_requested = GNUNET_YES; | ||
784 | return; | ||
785 | } | ||
786 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
787 | "Really destroying set\n"); | ||
788 | if (NULL != set->mq) | ||
789 | { | ||
790 | GNUNET_MQ_destroy (set->mq); | ||
791 | set->mq = NULL; | ||
792 | } | ||
793 | GNUNET_free (set); | ||
794 | } | ||
795 | |||
796 | |||
797 | /** | ||
798 | * Prepare a set operation to be evaluated with another peer. | ||
799 | * The evaluation will not start until the client provides | ||
800 | * a local set with #GNUNET_SET_commit(). | ||
801 | * | ||
802 | * @param other_peer peer with the other set | ||
803 | * @param app_id hash for the application using the set | ||
804 | * @param context_msg additional information for the request | ||
805 | * @param result_mode specified how results will be returned, | ||
806 | * see `enum GNUNET_SET_ResultMode`. | ||
807 | * @param result_cb called on error or success | ||
808 | * @param result_cls closure for @e result_cb | ||
809 | * @return a handle to cancel the operation | ||
810 | */ | ||
811 | struct GNUNET_SET_OperationHandle * | ||
812 | GNUNET_SET_prepare (const struct GNUNET_PeerIdentity *other_peer, | ||
813 | const struct GNUNET_HashCode *app_id, | ||
814 | const struct GNUNET_MessageHeader *context_msg, | ||
815 | enum GNUNET_SET_ResultMode result_mode, | ||
816 | struct GNUNET_SET_Option options[], | ||
817 | GNUNET_SET_ResultIterator result_cb, | ||
818 | void *result_cls) | ||
819 | { | ||
820 | struct GNUNET_MQ_Envelope *mqm; | ||
821 | struct GNUNET_SET_OperationHandle *oh; | ||
822 | struct GNUNET_SET_EvaluateMessage *msg; | ||
823 | struct GNUNET_SET_Option *opt; | ||
824 | |||
825 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
826 | "Client prepares set operation (%d)\n", | ||
827 | result_mode); | ||
828 | oh = GNUNET_new (struct GNUNET_SET_OperationHandle); | ||
829 | oh->result_cb = result_cb; | ||
830 | oh->result_cls = result_cls; | ||
831 | mqm = GNUNET_MQ_msg_nested_mh (msg, | ||
832 | GNUNET_MESSAGE_TYPE_SET_EVALUATE, | ||
833 | context_msg); | ||
834 | msg->app_id = *app_id; | ||
835 | msg->result_mode = htonl (result_mode); | ||
836 | msg->target_peer = *other_peer; | ||
837 | for (opt = options; opt->type != 0; opt++) | ||
838 | { | ||
839 | switch (opt->type) | ||
840 | { | ||
841 | case GNUNET_SET_OPTION_BYZANTINE: | ||
842 | msg->byzantine = GNUNET_YES; | ||
843 | msg->byzantine_lower_bound = opt->v.num; | ||
844 | break; | ||
845 | |||
846 | case GNUNET_SET_OPTION_FORCE_FULL: | ||
847 | msg->force_full = GNUNET_YES; | ||
848 | break; | ||
849 | |||
850 | case GNUNET_SET_OPTION_FORCE_DELTA: | ||
851 | msg->force_delta = GNUNET_YES; | ||
852 | break; | ||
853 | |||
854 | default: | ||
855 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
856 | "Option with type %d not recognized\n", (int) opt->type); | ||
857 | } | ||
858 | } | ||
859 | oh->conclude_mqm = mqm; | ||
860 | oh->request_id_addr = &msg->request_id; | ||
861 | |||
862 | return oh; | ||
863 | } | ||
864 | |||
865 | |||
866 | /** | ||
867 | * Connect to the set service in order to listen for requests. | ||
868 | * | ||
869 | * @param cls the `struct GNUNET_SET_ListenHandle *` to connect | ||
870 | */ | ||
871 | static void | ||
872 | listen_connect (void *cls); | ||
873 | |||
874 | |||
875 | /** | ||
876 | * Check validity of request message for a listen operation | ||
877 | * | ||
878 | * @param cls the listen handle | ||
879 | * @param msg the message | ||
880 | * @return #GNUNET_OK if the message is well-formed | ||
881 | */ | ||
882 | static int | ||
883 | check_request (void *cls, | ||
884 | const struct GNUNET_SET_RequestMessage *msg) | ||
885 | { | ||
886 | const struct GNUNET_MessageHeader *context_msg; | ||
887 | |||
888 | if (ntohs (msg->header.size) == sizeof(*msg)) | ||
889 | return GNUNET_OK; /* no context message is OK */ | ||
890 | context_msg = GNUNET_MQ_extract_nested_mh (msg); | ||
891 | if (NULL == context_msg) | ||
892 | { | ||
893 | /* malformed context message is NOT ok */ | ||
894 | GNUNET_break_op (0); | ||
895 | return GNUNET_SYSERR; | ||
896 | } | ||
897 | return GNUNET_OK; | ||
898 | } | ||
899 | |||
900 | |||
901 | /** | ||
902 | * Handle request message for a listen operation | ||
903 | * | ||
904 | * @param cls the listen handle | ||
905 | * @param msg the message | ||
906 | */ | ||
907 | static void | ||
908 | handle_request (void *cls, | ||
909 | const struct GNUNET_SET_RequestMessage *msg) | ||
910 | { | ||
911 | struct GNUNET_SET_ListenHandle *lh = cls; | ||
912 | struct GNUNET_SET_Request req; | ||
913 | const struct GNUNET_MessageHeader *context_msg; | ||
914 | struct GNUNET_MQ_Envelope *mqm; | ||
915 | struct GNUNET_SET_RejectMessage *rmsg; | ||
916 | |||
917 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
918 | "Processing incoming operation request with id %u\n", | ||
919 | ntohl (msg->accept_id)); | ||
920 | /* we got another valid request => reset the backoff */ | ||
921 | lh->reconnect_backoff = GNUNET_TIME_UNIT_MILLISECONDS; | ||
922 | req.accept_id = ntohl (msg->accept_id); | ||
923 | req.accepted = GNUNET_NO; | ||
924 | context_msg = GNUNET_MQ_extract_nested_mh (msg); | ||
925 | /* calling #GNUNET_SET_accept() in the listen cb will set req->accepted */ | ||
926 | lh->listen_cb (lh->listen_cls, | ||
927 | &msg->peer_id, | ||
928 | context_msg, | ||
929 | &req); | ||
930 | if (GNUNET_YES == req.accepted) | ||
931 | return; /* the accept-case is handled in #GNUNET_SET_accept() */ | ||
932 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
933 | "Rejected request %u\n", | ||
934 | ntohl (msg->accept_id)); | ||
935 | mqm = GNUNET_MQ_msg (rmsg, | ||
936 | GNUNET_MESSAGE_TYPE_SET_REJECT); | ||
937 | rmsg->accept_reject_id = msg->accept_id; | ||
938 | GNUNET_MQ_send (lh->mq, mqm); | ||
939 | } | ||
940 | |||
941 | |||
942 | /** | ||
943 | * Our connection with the set service encountered an error, | ||
944 | * re-initialize with exponential back-off. | ||
945 | * | ||
946 | * @param cls the `struct GNUNET_SET_ListenHandle *` | ||
947 | * @param error reason for the disconnect | ||
948 | */ | ||
949 | static void | ||
950 | handle_client_listener_error (void *cls, | ||
951 | enum GNUNET_MQ_Error error) | ||
952 | { | ||
953 | struct GNUNET_SET_ListenHandle *lh = cls; | ||
954 | |||
955 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
956 | "Listener broke down (%d), re-connecting\n", | ||
957 | (int) error); | ||
958 | GNUNET_MQ_destroy (lh->mq); | ||
959 | lh->mq = NULL; | ||
960 | lh->reconnect_task = GNUNET_SCHEDULER_add_delayed (lh->reconnect_backoff, | ||
961 | &listen_connect, | ||
962 | lh); | ||
963 | lh->reconnect_backoff = GNUNET_TIME_STD_BACKOFF (lh->reconnect_backoff); | ||
964 | } | ||
965 | |||
966 | |||
967 | /** | ||
968 | * Connect to the set service in order to listen for requests. | ||
969 | * | ||
970 | * @param cls the `struct GNUNET_SET_ListenHandle *` to connect | ||
971 | */ | ||
972 | static void | ||
973 | listen_connect (void *cls) | ||
974 | { | ||
975 | struct GNUNET_SET_ListenHandle *lh = cls; | ||
976 | struct GNUNET_MQ_MessageHandler mq_handlers[] = { | ||
977 | GNUNET_MQ_hd_var_size (request, | ||
978 | GNUNET_MESSAGE_TYPE_SET_REQUEST, | ||
979 | struct GNUNET_SET_RequestMessage, | ||
980 | lh), | ||
981 | GNUNET_MQ_handler_end () | ||
982 | }; | ||
983 | struct GNUNET_MQ_Envelope *mqm; | ||
984 | struct GNUNET_SET_ListenMessage *msg; | ||
985 | |||
986 | lh->reconnect_task = NULL; | ||
987 | GNUNET_assert (NULL == lh->mq); | ||
988 | lh->mq = GNUNET_CLIENT_connect (lh->cfg, | ||
989 | "set", | ||
990 | mq_handlers, | ||
991 | &handle_client_listener_error, | ||
992 | lh); | ||
993 | if (NULL == lh->mq) | ||
994 | return; | ||
995 | mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_LISTEN); | ||
996 | msg->operation = htonl (lh->operation); | ||
997 | msg->app_id = lh->app_id; | ||
998 | GNUNET_MQ_send (lh->mq, | ||
999 | mqm); | ||
1000 | } | ||
1001 | |||
1002 | |||
1003 | /** | ||
1004 | * Wait for set operation requests for the given application id | ||
1005 | * | ||
1006 | * @param cfg configuration to use for connecting to | ||
1007 | * the set service, needs to be valid for the lifetime of the listen handle | ||
1008 | * @param operation operation we want to listen for | ||
1009 | * @param app_id id of the application that handles set operation requests | ||
1010 | * @param listen_cb called for each incoming request matching the operation | ||
1011 | * and application id | ||
1012 | * @param listen_cls handle for @a listen_cb | ||
1013 | * @return a handle that can be used to cancel the listen operation | ||
1014 | */ | ||
1015 | struct GNUNET_SET_ListenHandle * | ||
1016 | GNUNET_SET_listen (const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
1017 | enum GNUNET_SET_OperationType operation, | ||
1018 | const struct GNUNET_HashCode *app_id, | ||
1019 | GNUNET_SET_ListenCallback listen_cb, | ||
1020 | void *listen_cls) | ||
1021 | { | ||
1022 | struct GNUNET_SET_ListenHandle *lh; | ||
1023 | |||
1024 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1025 | "Starting listener for app %s\n", | ||
1026 | GNUNET_h2s (app_id)); | ||
1027 | lh = GNUNET_new (struct GNUNET_SET_ListenHandle); | ||
1028 | lh->listen_cb = listen_cb; | ||
1029 | lh->listen_cls = listen_cls; | ||
1030 | lh->cfg = cfg; | ||
1031 | lh->operation = operation; | ||
1032 | lh->app_id = *app_id; | ||
1033 | lh->reconnect_backoff = GNUNET_TIME_UNIT_MILLISECONDS; | ||
1034 | listen_connect (lh); | ||
1035 | if (NULL == lh->mq) | ||
1036 | { | ||
1037 | GNUNET_free (lh); | ||
1038 | return NULL; | ||
1039 | } | ||
1040 | return lh; | ||
1041 | } | ||
1042 | |||
1043 | |||
1044 | /** | ||
1045 | * Cancel the given listen operation. | ||
1046 | * | ||
1047 | * @param lh handle for the listen operation | ||
1048 | */ | ||
1049 | void | ||
1050 | GNUNET_SET_listen_cancel (struct GNUNET_SET_ListenHandle *lh) | ||
1051 | { | ||
1052 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1053 | "Canceling listener %s\n", | ||
1054 | GNUNET_h2s (&lh->app_id)); | ||
1055 | if (NULL != lh->mq) | ||
1056 | { | ||
1057 | GNUNET_MQ_destroy (lh->mq); | ||
1058 | lh->mq = NULL; | ||
1059 | } | ||
1060 | if (NULL != lh->reconnect_task) | ||
1061 | { | ||
1062 | GNUNET_SCHEDULER_cancel (lh->reconnect_task); | ||
1063 | lh->reconnect_task = NULL; | ||
1064 | } | ||
1065 | GNUNET_free (lh); | ||
1066 | } | ||
1067 | |||
1068 | |||
1069 | /** | ||
1070 | * Accept a request we got via #GNUNET_SET_listen. Must be called during | ||
1071 | * #GNUNET_SET_listen, as the 'struct GNUNET_SET_Request' becomes invalid | ||
1072 | * afterwards. | ||
1073 | * Call #GNUNET_SET_commit to provide the local set to use for the operation, | ||
1074 | * and to begin the exchange with the remote peer. | ||
1075 | * | ||
1076 | * @param request request to accept | ||
1077 | * @param result_mode specified how results will be returned, | ||
1078 | * see `enum GNUNET_SET_ResultMode`. | ||
1079 | * @param result_cb callback for the results | ||
1080 | * @param result_cls closure for @a result_cb | ||
1081 | * @return a handle to cancel the operation | ||
1082 | */ | ||
1083 | struct GNUNET_SET_OperationHandle * | ||
1084 | GNUNET_SET_accept (struct GNUNET_SET_Request *request, | ||
1085 | enum GNUNET_SET_ResultMode result_mode, | ||
1086 | struct GNUNET_SET_Option options[], | ||
1087 | GNUNET_SET_ResultIterator result_cb, | ||
1088 | void *result_cls) | ||
1089 | { | ||
1090 | struct GNUNET_MQ_Envelope *mqm; | ||
1091 | struct GNUNET_SET_OperationHandle *oh; | ||
1092 | struct GNUNET_SET_AcceptMessage *msg; | ||
1093 | |||
1094 | GNUNET_assert (GNUNET_NO == request->accepted); | ||
1095 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1096 | "Client accepts set operation (%d) with id %u\n", | ||
1097 | result_mode, | ||
1098 | request->accept_id); | ||
1099 | request->accepted = GNUNET_YES; | ||
1100 | mqm = GNUNET_MQ_msg (msg, | ||
1101 | GNUNET_MESSAGE_TYPE_SET_ACCEPT); | ||
1102 | msg->accept_reject_id = htonl (request->accept_id); | ||
1103 | msg->result_mode = htonl (result_mode); | ||
1104 | oh = GNUNET_new (struct GNUNET_SET_OperationHandle); | ||
1105 | oh->result_cb = result_cb; | ||
1106 | oh->result_cls = result_cls; | ||
1107 | oh->conclude_mqm = mqm; | ||
1108 | oh->request_id_addr = &msg->request_id; | ||
1109 | return oh; | ||
1110 | } | ||
1111 | |||
1112 | |||
1113 | /** | ||
1114 | * Commit a set to be used with a set operation. | ||
1115 | * This function is called once we have fully constructed | ||
1116 | * the set that we want to use for the operation. At this | ||
1117 | * time, the P2P protocol can then begin to exchange the | ||
1118 | * set information and call the result callback with the | ||
1119 | * result information. | ||
1120 | * | ||
1121 | * @param oh handle to the set operation | ||
1122 | * @param set the set to use for the operation | ||
1123 | * @return #GNUNET_OK on success, #GNUNET_SYSERR if the | ||
1124 | * set is invalid (e.g. the set service crashed) | ||
1125 | */ | ||
1126 | int | ||
1127 | GNUNET_SET_commit (struct GNUNET_SET_OperationHandle *oh, | ||
1128 | struct GNUNET_SET_Handle *set) | ||
1129 | { | ||
1130 | if (NULL != oh->set) | ||
1131 | { | ||
1132 | /* Some other set was already committed for this | ||
1133 | * operation, there is a logic bug in the client of this API */ | ||
1134 | GNUNET_break (0); | ||
1135 | return GNUNET_OK; | ||
1136 | } | ||
1137 | GNUNET_assert (NULL != set); | ||
1138 | if (GNUNET_YES == set->invalid) | ||
1139 | return GNUNET_SYSERR; | ||
1140 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1141 | "Client commits to SET\n"); | ||
1142 | GNUNET_assert (NULL != oh->conclude_mqm); | ||
1143 | oh->set = set; | ||
1144 | GNUNET_CONTAINER_DLL_insert (set->ops_head, | ||
1145 | set->ops_tail, | ||
1146 | oh); | ||
1147 | oh->request_id = GNUNET_MQ_assoc_add (set->mq, | ||
1148 | oh); | ||
1149 | *oh->request_id_addr = htonl (oh->request_id); | ||
1150 | GNUNET_MQ_send (set->mq, | ||
1151 | oh->conclude_mqm); | ||
1152 | oh->conclude_mqm = NULL; | ||
1153 | oh->request_id_addr = NULL; | ||
1154 | return GNUNET_OK; | ||
1155 | } | ||
1156 | |||
1157 | |||
1158 | /** | ||
1159 | * Iterate over all elements in the given set. Note that this | ||
1160 | * operation involves transferring every element of the set from the | ||
1161 | * service to the client, and is thus costly. | ||
1162 | * | ||
1163 | * @param set the set to iterate over | ||
1164 | * @param iter the iterator to call for each element | ||
1165 | * @param iter_cls closure for @a iter | ||
1166 | * @return #GNUNET_YES if the iteration started successfully, | ||
1167 | * #GNUNET_NO if another iteration is active | ||
1168 | * #GNUNET_SYSERR if the set is invalid (e.g. the server crashed, disconnected) | ||
1169 | */ | ||
1170 | int | ||
1171 | GNUNET_SET_iterate (struct GNUNET_SET_Handle *set, | ||
1172 | GNUNET_SET_ElementIterator iter, | ||
1173 | void *iter_cls) | ||
1174 | { | ||
1175 | struct GNUNET_MQ_Envelope *ev; | ||
1176 | |||
1177 | GNUNET_assert (NULL != iter); | ||
1178 | if (GNUNET_YES == set->invalid) | ||
1179 | return GNUNET_SYSERR; | ||
1180 | if (NULL != set->iterator) | ||
1181 | return GNUNET_NO; | ||
1182 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1183 | "Iterating over set\n"); | ||
1184 | set->iterator = iter; | ||
1185 | set->iterator_cls = iter_cls; | ||
1186 | ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_ITER_REQUEST); | ||
1187 | GNUNET_MQ_send (set->mq, ev); | ||
1188 | return GNUNET_YES; | ||
1189 | } | ||
1190 | |||
1191 | |||
1192 | void | ||
1193 | GNUNET_SET_copy_lazy (struct GNUNET_SET_Handle *set, | ||
1194 | GNUNET_SET_CopyReadyCallback cb, | ||
1195 | void *cls) | ||
1196 | { | ||
1197 | struct GNUNET_MQ_Envelope *ev; | ||
1198 | struct SetCopyRequest *req; | ||
1199 | |||
1200 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1201 | "Creating lazy copy of set\n"); | ||
1202 | ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_COPY_LAZY_PREPARE); | ||
1203 | GNUNET_MQ_send (set->mq, ev); | ||
1204 | |||
1205 | req = GNUNET_new (struct SetCopyRequest); | ||
1206 | req->cb = cb; | ||
1207 | req->cls = cls; | ||
1208 | GNUNET_CONTAINER_DLL_insert (set->copy_req_head, | ||
1209 | set->copy_req_tail, | ||
1210 | req); | ||
1211 | } | ||
1212 | |||
1213 | |||
1214 | /** | ||
1215 | * Create a copy of an element. The copy | ||
1216 | * must be GNUNET_free-d by the caller. | ||
1217 | * | ||
1218 | * @param element the element to copy | ||
1219 | * @return the copied element | ||
1220 | */ | ||
1221 | struct GNUNET_SET_Element * | ||
1222 | GNUNET_SET_element_dup (const struct GNUNET_SET_Element *element) | ||
1223 | { | ||
1224 | struct GNUNET_SET_Element *copy; | ||
1225 | |||
1226 | copy = GNUNET_malloc (element->size + sizeof(struct GNUNET_SET_Element)); | ||
1227 | copy->size = element->size; | ||
1228 | copy->element_type = element->element_type; | ||
1229 | copy->data = ©[1]; | ||
1230 | GNUNET_memcpy (©[1], | ||
1231 | element->data, | ||
1232 | copy->size); | ||
1233 | return copy; | ||
1234 | } | ||
1235 | |||
1236 | |||
1237 | /** | ||
1238 | * Hash a set element. | ||
1239 | * | ||
1240 | * @param element the element that should be hashed | ||
1241 | * @param[out] ret_hash a pointer to where the hash of @a element | ||
1242 | * should be stored | ||
1243 | */ | ||
1244 | void | ||
1245 | GNUNET_SET_element_hash (const struct GNUNET_SET_Element *element, | ||
1246 | struct GNUNET_HashCode *ret_hash) | ||
1247 | { | ||
1248 | struct GNUNET_HashContext *ctx = GNUNET_CRYPTO_hash_context_start (); | ||
1249 | |||
1250 | /* It's not guaranteed that the element data is always after the element header, | ||
1251 | so we need to hash the chunks separately. */ | ||
1252 | GNUNET_CRYPTO_hash_context_read (ctx, &element->size, sizeof(uint16_t)); | ||
1253 | GNUNET_CRYPTO_hash_context_read (ctx, &element->element_type, | ||
1254 | sizeof(uint16_t)); | ||
1255 | GNUNET_CRYPTO_hash_context_read (ctx, element->data, element->size); | ||
1256 | GNUNET_CRYPTO_hash_context_finish (ctx, ret_hash); | ||
1257 | } | ||
1258 | |||
1259 | |||
1260 | /* end of set_api.c */ | ||