diff options
Diffstat (limited to 'src/seti/gnunet-service-seti.c')
-rw-r--r-- | src/seti/gnunet-service-seti.c | 3274 |
1 files changed, 3274 insertions, 0 deletions
diff --git a/src/seti/gnunet-service-seti.c b/src/seti/gnunet-service-seti.c new file mode 100644 index 000000000..3b8da01cd --- /dev/null +++ b/src/seti/gnunet-service-seti.c | |||
@@ -0,0 +1,3274 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2013-2017, 2020 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/gnunet-service-seti.c | ||
22 | * @brief two-peer set intersection operations | ||
23 | * @author Florian Dold | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "gnunet-service-seti_protocol.h" | ||
27 | #include "gnunet_statistics_service.h" | ||
28 | |||
29 | /** | ||
30 | * How long do we hold on to an incoming channel if there is | ||
31 | * no local listener before giving up? | ||
32 | */ | ||
33 | #define INCOMING_CHANNEL_TIMEOUT GNUNET_TIME_UNIT_MINUTES | ||
34 | |||
35 | |||
36 | /** | ||
37 | * Current phase we are in for a intersection operation. | ||
38 | */ | ||
39 | enum IntersectionOperationPhase | ||
40 | { | ||
41 | /** | ||
42 | * We are just starting. | ||
43 | */ | ||
44 | PHASE_INITIAL, | ||
45 | |||
46 | /** | ||
47 | * We have send the number of our elements to the other | ||
48 | * peer, but did not setup our element set yet. | ||
49 | */ | ||
50 | PHASE_COUNT_SENT, | ||
51 | |||
52 | /** | ||
53 | * We have initialized our set and are now reducing it by exchanging | ||
54 | * Bloom filters until one party notices the their element hashes | ||
55 | * are equal. | ||
56 | */ | ||
57 | PHASE_BF_EXCHANGE, | ||
58 | |||
59 | /** | ||
60 | * We must next send the P2P DONE message (after finishing mostly | ||
61 | * with the local client). Then we will wait for the channel to close. | ||
62 | */ | ||
63 | PHASE_MUST_SEND_DONE, | ||
64 | |||
65 | /** | ||
66 | * We have received the P2P DONE message, and must finish with the | ||
67 | * local client before terminating the channel. | ||
68 | */ | ||
69 | PHASE_DONE_RECEIVED, | ||
70 | |||
71 | /** | ||
72 | * The protocol is over. Results may still have to be sent to the | ||
73 | * client. | ||
74 | */ | ||
75 | PHASE_FINISHED | ||
76 | }; | ||
77 | |||
78 | |||
79 | /** | ||
80 | * Implementation-specific set state. Used as opaque pointer, and | ||
81 | * specified further in the respective implementation. | ||
82 | */ | ||
83 | struct SetState; | ||
84 | |||
85 | /** | ||
86 | * Implementation-specific set operation. Used as opaque pointer, and | ||
87 | * specified further in the respective implementation. | ||
88 | */ | ||
89 | struct OperationState; | ||
90 | |||
91 | /** | ||
92 | * A set that supports a specific operation with other peers. | ||
93 | */ | ||
94 | struct Set; | ||
95 | |||
96 | /** | ||
97 | * Information about an element element in the set. All elements are | ||
98 | * stored in a hash-table from their hash-code to their 'struct | ||
99 | * Element', so that the remove and add operations are reasonably | ||
100 | * fast. | ||
101 | */ | ||
102 | struct ElementEntry; | ||
103 | |||
104 | /** | ||
105 | * Operation context used to execute a set operation. | ||
106 | */ | ||
107 | struct Operation; | ||
108 | |||
109 | |||
110 | /** | ||
111 | * MutationEvent gives information about changes | ||
112 | * to an element (removal / addition) in a set content. | ||
113 | */ | ||
114 | struct MutationEvent | ||
115 | { | ||
116 | /** | ||
117 | * First generation affected by this mutation event. | ||
118 | * | ||
119 | * If @a generation is 0, this mutation event is a list | ||
120 | * sentinel element. | ||
121 | */ | ||
122 | unsigned int generation; | ||
123 | |||
124 | /** | ||
125 | * If @a added is #GNUNET_YES, then this is a | ||
126 | * `remove` event, otherwise it is an `add` event. | ||
127 | */ | ||
128 | int added; | ||
129 | }; | ||
130 | |||
131 | |||
132 | /** | ||
133 | * Information about an element element in the set. All elements are | ||
134 | * stored in a hash-table from their hash-code to their `struct | ||
135 | * Element`, so that the remove and add operations are reasonably | ||
136 | * fast. | ||
137 | */ | ||
138 | struct ElementEntry | ||
139 | { | ||
140 | /** | ||
141 | * The actual element. The data for the element | ||
142 | * should be allocated at the end of this struct. | ||
143 | */ | ||
144 | struct GNUNET_SET_Element element; | ||
145 | |||
146 | /** | ||
147 | * Hash of the element. For set union: Will be used to derive the | ||
148 | * different IBF keys for different salts. | ||
149 | */ | ||
150 | struct GNUNET_HashCode element_hash; | ||
151 | |||
152 | /** | ||
153 | * If @a mutations is not NULL, it contains | ||
154 | * a list of mutations, ordered by increasing generation. | ||
155 | * The list is terminated by a sentinel event with `generation` | ||
156 | * set to 0. | ||
157 | * | ||
158 | * If @a mutations is NULL, then this element exists in all generations | ||
159 | * of the respective set content this element belongs to. | ||
160 | */ | ||
161 | struct MutationEvent *mutations; | ||
162 | |||
163 | /** | ||
164 | * Number of elements in the array @a mutations. | ||
165 | */ | ||
166 | unsigned int mutations_size; | ||
167 | |||
168 | /** | ||
169 | * #GNUNET_YES if the element is a remote element, and does not belong | ||
170 | * to the operation's set. | ||
171 | */ | ||
172 | int remote; | ||
173 | }; | ||
174 | |||
175 | |||
176 | /** | ||
177 | * A listener is inhabited by a client, and waits for evaluation | ||
178 | * requests from remote peers. | ||
179 | */ | ||
180 | struct Listener; | ||
181 | |||
182 | |||
183 | /** | ||
184 | * State we keep per client. | ||
185 | */ | ||
186 | struct ClientState | ||
187 | { | ||
188 | /** | ||
189 | * Set, if associated with the client, otherwise NULL. | ||
190 | */ | ||
191 | struct Set *set; | ||
192 | |||
193 | /** | ||
194 | * Listener, if associated with the client, otherwise NULL. | ||
195 | */ | ||
196 | struct Listener *listener; | ||
197 | |||
198 | /** | ||
199 | * Client handle. | ||
200 | */ | ||
201 | struct GNUNET_SERVICE_Client *client; | ||
202 | |||
203 | /** | ||
204 | * Message queue. | ||
205 | */ | ||
206 | struct GNUNET_MQ_Handle *mq; | ||
207 | }; | ||
208 | |||
209 | |||
210 | /** | ||
211 | * Operation context used to execute a set operation. | ||
212 | */ | ||
213 | struct Operation | ||
214 | { | ||
215 | /** | ||
216 | * Kept in a DLL of the listener, if @e listener is non-NULL. | ||
217 | */ | ||
218 | struct Operation *next; | ||
219 | |||
220 | /** | ||
221 | * Kept in a DLL of the listener, if @e listener is non-NULL. | ||
222 | */ | ||
223 | struct Operation *prev; | ||
224 | |||
225 | /** | ||
226 | * Channel to the peer. | ||
227 | */ | ||
228 | struct GNUNET_CADET_Channel *channel; | ||
229 | |||
230 | /** | ||
231 | * Port this operation runs on. | ||
232 | */ | ||
233 | struct Listener *listener; | ||
234 | |||
235 | /** | ||
236 | * Message queue for the channel. | ||
237 | */ | ||
238 | struct GNUNET_MQ_Handle *mq; | ||
239 | |||
240 | /** | ||
241 | * Context message, may be NULL. | ||
242 | */ | ||
243 | struct GNUNET_MessageHeader *context_msg; | ||
244 | |||
245 | /** | ||
246 | * Set associated with the operation, NULL until the spec has been | ||
247 | * associated with a set. | ||
248 | */ | ||
249 | struct Set *set; | ||
250 | |||
251 | /** | ||
252 | * Operation-specific operation state. Note that the exact | ||
253 | * type depends on this being a union or intersection operation | ||
254 | * (and thus on @e vt). | ||
255 | */ | ||
256 | struct OperationState *state; // FIXME: inline | ||
257 | |||
258 | /** | ||
259 | * The identity of the requesting peer. Needs to | ||
260 | * be stored here as the op spec might not have been created yet. | ||
261 | */ | ||
262 | struct GNUNET_PeerIdentity peer; | ||
263 | |||
264 | /** | ||
265 | * Timeout task, if the incoming peer has not been accepted | ||
266 | * after the timeout, it will be disconnected. | ||
267 | */ | ||
268 | struct GNUNET_SCHEDULER_Task *timeout_task; | ||
269 | |||
270 | /** | ||
271 | * Salt to use for the operation. | ||
272 | */ | ||
273 | uint32_t salt; | ||
274 | |||
275 | /** | ||
276 | * Remote peers element count | ||
277 | */ | ||
278 | uint32_t remote_element_count; | ||
279 | |||
280 | /** | ||
281 | * ID used to identify an operation between service and client | ||
282 | */ | ||
283 | uint32_t client_request_id; | ||
284 | |||
285 | /** | ||
286 | * When are elements sent to the client, and which elements are sent? | ||
287 | */ | ||
288 | enum GNUNET_SET_ResultMode result_mode; | ||
289 | |||
290 | /** | ||
291 | * Always use delta operation instead of sending full sets, | ||
292 | * even it it's less efficient. | ||
293 | */ | ||
294 | int force_delta; | ||
295 | |||
296 | /** | ||
297 | * Always send full sets, even if delta operations would | ||
298 | * be more efficient. | ||
299 | */ | ||
300 | int force_full; | ||
301 | |||
302 | /** | ||
303 | * #GNUNET_YES to fail operations where Byzantine faults | ||
304 | * are suspected | ||
305 | */ | ||
306 | int byzantine; | ||
307 | |||
308 | /** | ||
309 | * Lower bound for the set size, used only when | ||
310 | * byzantine mode is enabled. | ||
311 | */ | ||
312 | int byzantine_lower_bound; | ||
313 | |||
314 | /** | ||
315 | * Unique request id for the request from a remote peer, sent to the | ||
316 | * client, which will accept or reject the request. Set to '0' iff | ||
317 | * the request has not been suggested yet. | ||
318 | */ | ||
319 | uint32_t suggest_id; | ||
320 | |||
321 | /** | ||
322 | * Generation in which the operation handle | ||
323 | * was created. | ||
324 | */ | ||
325 | unsigned int generation_created; | ||
326 | }; | ||
327 | |||
328 | |||
329 | /** | ||
330 | * SetContent stores the actual set elements, which may be shared by | ||
331 | * multiple generations derived from one set. | ||
332 | */ | ||
333 | struct SetContent | ||
334 | { | ||
335 | /** | ||
336 | * Maps `struct GNUNET_HashCode *` to `struct ElementEntry *`. | ||
337 | */ | ||
338 | struct GNUNET_CONTAINER_MultiHashMap *elements; | ||
339 | |||
340 | /** | ||
341 | * Mutations requested by the client that we're | ||
342 | * unable to execute right now because we're iterating | ||
343 | * over the underlying hash map of elements. | ||
344 | */ | ||
345 | struct PendingMutation *pending_mutations_head; | ||
346 | |||
347 | /** | ||
348 | * Mutations requested by the client that we're | ||
349 | * unable to execute right now because we're iterating | ||
350 | * over the underlying hash map of elements. | ||
351 | */ | ||
352 | struct PendingMutation *pending_mutations_tail; | ||
353 | |||
354 | /** | ||
355 | * Number of references to the content. | ||
356 | */ | ||
357 | unsigned int refcount; | ||
358 | |||
359 | /** | ||
360 | * FIXME: document! | ||
361 | */ | ||
362 | unsigned int latest_generation; | ||
363 | |||
364 | /** | ||
365 | * Number of concurrently active iterators. | ||
366 | */ | ||
367 | int iterator_count; | ||
368 | }; | ||
369 | |||
370 | |||
371 | struct GenerationRange | ||
372 | { | ||
373 | /** | ||
374 | * First generation that is excluded. | ||
375 | */ | ||
376 | unsigned int start; | ||
377 | |||
378 | /** | ||
379 | * Generation after the last excluded generation. | ||
380 | */ | ||
381 | unsigned int end; | ||
382 | }; | ||
383 | |||
384 | |||
385 | /** | ||
386 | * Information about a mutation to apply to a set. | ||
387 | */ | ||
388 | struct PendingMutation | ||
389 | { | ||
390 | /** | ||
391 | * Mutations are kept in a DLL. | ||
392 | */ | ||
393 | struct PendingMutation *prev; | ||
394 | |||
395 | /** | ||
396 | * Mutations are kept in a DLL. | ||
397 | */ | ||
398 | struct PendingMutation *next; | ||
399 | |||
400 | /** | ||
401 | * Set this mutation is about. | ||
402 | */ | ||
403 | struct Set *set; | ||
404 | |||
405 | /** | ||
406 | * Message that describes the desired mutation. | ||
407 | * May only be a #GNUNET_MESSAGE_TYPE_SET_ADD or | ||
408 | * #GNUNET_MESSAGE_TYPE_SET_REMOVE. | ||
409 | */ | ||
410 | struct GNUNET_SET_ElementMessage *msg; | ||
411 | }; | ||
412 | |||
413 | |||
414 | /** | ||
415 | * A set that supports a specific operation with other peers. | ||
416 | */ | ||
417 | struct Set | ||
418 | { | ||
419 | /** | ||
420 | * Sets are held in a doubly linked list (in `sets_head` and `sets_tail`). | ||
421 | */ | ||
422 | struct Set *next; | ||
423 | |||
424 | /** | ||
425 | * Sets are held in a doubly linked list. | ||
426 | */ | ||
427 | struct Set *prev; | ||
428 | |||
429 | /** | ||
430 | * Client that owns the set. Only one client may own a set, | ||
431 | * and there can only be one set per client. | ||
432 | */ | ||
433 | struct ClientState *cs; | ||
434 | |||
435 | /** | ||
436 | * Content, possibly shared by multiple sets, | ||
437 | * and thus reference counted. | ||
438 | */ | ||
439 | struct SetContent *content; | ||
440 | |||
441 | /** | ||
442 | * Implementation-specific state. | ||
443 | */ | ||
444 | struct SetState *state; | ||
445 | |||
446 | /** | ||
447 | * Current state of iterating elements for the client. | ||
448 | * NULL if we are not currently iterating. | ||
449 | */ | ||
450 | struct GNUNET_CONTAINER_MultiHashMapIterator *iter; | ||
451 | |||
452 | /** | ||
453 | * Evaluate operations are held in a linked list. | ||
454 | */ | ||
455 | struct Operation *ops_head; | ||
456 | |||
457 | /** | ||
458 | * Evaluate operations are held in a linked list. | ||
459 | */ | ||
460 | struct Operation *ops_tail; | ||
461 | |||
462 | /** | ||
463 | * List of generations we have to exclude, due to lazy copies. | ||
464 | */ | ||
465 | struct GenerationRange *excluded_generations; | ||
466 | |||
467 | /** | ||
468 | * Current generation, that is, number of previously executed | ||
469 | * operations and lazy copies on the underlying set content. | ||
470 | */ | ||
471 | unsigned int current_generation; | ||
472 | |||
473 | /** | ||
474 | * Number of elements in array @a excluded_generations. | ||
475 | */ | ||
476 | unsigned int excluded_generations_size; | ||
477 | |||
478 | /** | ||
479 | * Type of operation supported for this set | ||
480 | */ | ||
481 | enum GNUNET_SET_OperationType operation; | ||
482 | |||
483 | /** | ||
484 | * Generation we're currently iteration over. | ||
485 | */ | ||
486 | unsigned int iter_generation; | ||
487 | |||
488 | /** | ||
489 | * Each @e iter is assigned a unique number, so that the client | ||
490 | * can distinguish iterations. | ||
491 | */ | ||
492 | uint16_t iteration_id; | ||
493 | }; | ||
494 | |||
495 | |||
496 | /** | ||
497 | * State of an evaluate operation with another peer. | ||
498 | */ | ||
499 | struct OperationState | ||
500 | { | ||
501 | /** | ||
502 | * The bf we currently receive | ||
503 | */ | ||
504 | struct GNUNET_CONTAINER_BloomFilter *remote_bf; | ||
505 | |||
506 | /** | ||
507 | * BF of the set's element. | ||
508 | */ | ||
509 | struct GNUNET_CONTAINER_BloomFilter *local_bf; | ||
510 | |||
511 | /** | ||
512 | * Remaining elements in the intersection operation. | ||
513 | * Maps element-id-hashes to 'elements in our set'. | ||
514 | */ | ||
515 | struct GNUNET_CONTAINER_MultiHashMap *my_elements; | ||
516 | |||
517 | /** | ||
518 | * Iterator for sending the final set of @e my_elements to the client. | ||
519 | */ | ||
520 | struct GNUNET_CONTAINER_MultiHashMapIterator *full_result_iter; | ||
521 | |||
522 | /** | ||
523 | * Evaluate operations are held in a linked list. | ||
524 | */ | ||
525 | struct OperationState *next; | ||
526 | |||
527 | /** | ||
528 | * Evaluate operations are held in a linked list. | ||
529 | */ | ||
530 | struct OperationState *prev; | ||
531 | |||
532 | /** | ||
533 | * For multipart BF transmissions, we have to store the | ||
534 | * bloomfilter-data until we fully received it. | ||
535 | */ | ||
536 | char *bf_data; | ||
537 | |||
538 | /** | ||
539 | * XOR of the keys of all of the elements (remaining) in my set. | ||
540 | * Always updated when elements are added or removed to | ||
541 | * @e my_elements. | ||
542 | */ | ||
543 | struct GNUNET_HashCode my_xor; | ||
544 | |||
545 | /** | ||
546 | * XOR of the keys of all of the elements (remaining) in | ||
547 | * the other peer's set. Updated when we receive the | ||
548 | * other peer's Bloom filter. | ||
549 | */ | ||
550 | struct GNUNET_HashCode other_xor; | ||
551 | |||
552 | /** | ||
553 | * How many bytes of @e bf_data are valid? | ||
554 | */ | ||
555 | uint32_t bf_data_offset; | ||
556 | |||
557 | /** | ||
558 | * Current element count contained within @e my_elements. | ||
559 | * (May differ briefly during initialization.) | ||
560 | */ | ||
561 | uint32_t my_element_count; | ||
562 | |||
563 | /** | ||
564 | * size of the bloomfilter in @e bf_data. | ||
565 | */ | ||
566 | uint32_t bf_data_size; | ||
567 | |||
568 | /** | ||
569 | * size of the bloomfilter | ||
570 | */ | ||
571 | uint32_t bf_bits_per_element; | ||
572 | |||
573 | /** | ||
574 | * Salt currently used for BF construction (by us or the other peer, | ||
575 | * depending on where we are in the code). | ||
576 | */ | ||
577 | uint32_t salt; | ||
578 | |||
579 | /** | ||
580 | * Current state of the operation. | ||
581 | */ | ||
582 | enum IntersectionOperationPhase phase; | ||
583 | |||
584 | /** | ||
585 | * Generation in which the operation handle | ||
586 | * was created. | ||
587 | */ | ||
588 | unsigned int generation_created; | ||
589 | |||
590 | /** | ||
591 | * Did we send the client that we are done? | ||
592 | */ | ||
593 | int client_done_sent; | ||
594 | |||
595 | /** | ||
596 | * Set whenever we reach the state where the death of the | ||
597 | * channel is perfectly find and should NOT result in the | ||
598 | * operation being cancelled. | ||
599 | */ | ||
600 | int channel_death_expected; | ||
601 | }; | ||
602 | |||
603 | |||
604 | /** | ||
605 | * Extra state required for efficient set intersection. | ||
606 | * Merely tracks the total number of elements. | ||
607 | */ | ||
608 | struct SetState | ||
609 | { | ||
610 | /** | ||
611 | * Number of currently valid elements in the set which have not been | ||
612 | * removed. | ||
613 | */ | ||
614 | uint32_t current_set_element_count; | ||
615 | }; | ||
616 | |||
617 | |||
618 | /** | ||
619 | * A listener is inhabited by a client, and waits for evaluation | ||
620 | * requests from remote peers. | ||
621 | */ | ||
622 | struct Listener | ||
623 | { | ||
624 | /** | ||
625 | * Listeners are held in a doubly linked list. | ||
626 | */ | ||
627 | struct Listener *next; | ||
628 | |||
629 | /** | ||
630 | * Listeners are held in a doubly linked list. | ||
631 | */ | ||
632 | struct Listener *prev; | ||
633 | |||
634 | /** | ||
635 | * Head of DLL of operations this listener is responsible for. | ||
636 | * Once the client has accepted/declined the operation, the | ||
637 | * operation is moved to the respective set's operation DLLS. | ||
638 | */ | ||
639 | struct Operation *op_head; | ||
640 | |||
641 | /** | ||
642 | * Tail of DLL of operations this listener is responsible for. | ||
643 | * Once the client has accepted/declined the operation, the | ||
644 | * operation is moved to the respective set's operation DLLS. | ||
645 | */ | ||
646 | struct Operation *op_tail; | ||
647 | |||
648 | /** | ||
649 | * Client that owns the listener. | ||
650 | * Only one client may own a listener. | ||
651 | */ | ||
652 | struct ClientState *cs; | ||
653 | |||
654 | /** | ||
655 | * The port we are listening on with CADET. | ||
656 | */ | ||
657 | struct GNUNET_CADET_Port *open_port; | ||
658 | |||
659 | /** | ||
660 | * Application ID for the operation, used to distinguish | ||
661 | * multiple operations of the same type with the same peer. | ||
662 | */ | ||
663 | struct GNUNET_HashCode app_id; | ||
664 | |||
665 | /** | ||
666 | * The type of the operation. | ||
667 | */ | ||
668 | enum GNUNET_SET_OperationType operation; | ||
669 | }; | ||
670 | |||
671 | |||
672 | /** | ||
673 | * Handle to the cadet service, used to listen for and connect to | ||
674 | * remote peers. | ||
675 | */ | ||
676 | static struct GNUNET_CADET_Handle *cadet; | ||
677 | |||
678 | /** | ||
679 | * Statistics handle. | ||
680 | */ | ||
681 | static struct GNUNET_STATISTICS_Handle *_GSS_statistics; | ||
682 | |||
683 | /** | ||
684 | * Listeners are held in a doubly linked list. | ||
685 | */ | ||
686 | static struct Listener *listener_head; | ||
687 | |||
688 | /** | ||
689 | * Listeners are held in a doubly linked list. | ||
690 | */ | ||
691 | static struct Listener *listener_tail; | ||
692 | |||
693 | /** | ||
694 | * Number of active clients. | ||
695 | */ | ||
696 | static unsigned int num_clients; | ||
697 | |||
698 | /** | ||
699 | * Are we in shutdown? if #GNUNET_YES and the number of clients | ||
700 | * drops to zero, disconnect from CADET. | ||
701 | */ | ||
702 | static int in_shutdown; | ||
703 | |||
704 | /** | ||
705 | * Counter for allocating unique IDs for clients, used to identify | ||
706 | * incoming operation requests from remote peers, that the client can | ||
707 | * choose to accept or refuse. 0 must not be used (reserved for | ||
708 | * uninitialized). | ||
709 | */ | ||
710 | static uint32_t suggest_id; | ||
711 | |||
712 | |||
713 | /** | ||
714 | * If applicable in the current operation mode, send a result message | ||
715 | * to the client indicating we removed an element. | ||
716 | * | ||
717 | * @param op intersection operation | ||
718 | * @param element element to send | ||
719 | */ | ||
720 | static void | ||
721 | send_client_removed_element (struct Operation *op, | ||
722 | struct GNUNET_SET_Element *element) | ||
723 | { | ||
724 | struct GNUNET_MQ_Envelope *ev; | ||
725 | struct GNUNET_SET_ResultMessage *rm; | ||
726 | |||
727 | if (GNUNET_SET_RESULT_REMOVED != op->result_mode) | ||
728 | return; /* Wrong mode for transmitting removed elements */ | ||
729 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
730 | "Sending removed element (size %u) to client\n", | ||
731 | element->size); | ||
732 | GNUNET_STATISTICS_update (_GSS_statistics, | ||
733 | "# Element removed messages sent", | ||
734 | 1, | ||
735 | GNUNET_NO); | ||
736 | GNUNET_assert (0 != op->client_request_id); | ||
737 | ev = GNUNET_MQ_msg_extra (rm, | ||
738 | element->size, | ||
739 | GNUNET_MESSAGE_TYPE_SET_RESULT); | ||
740 | if (NULL == ev) | ||
741 | { | ||
742 | GNUNET_break (0); | ||
743 | return; | ||
744 | } | ||
745 | rm->result_status = htons (GNUNET_SET_STATUS_OK); | ||
746 | rm->request_id = htonl (op->client_request_id); | ||
747 | rm->element_type = element->element_type; | ||
748 | GNUNET_memcpy (&rm[1], | ||
749 | element->data, | ||
750 | element->size); | ||
751 | GNUNET_MQ_send (op->set->cs->mq, | ||
752 | ev); | ||
753 | } | ||
754 | |||
755 | |||
756 | /** | ||
757 | * Fills the "my_elements" hashmap with all relevant elements. | ||
758 | * | ||
759 | * @param cls the `struct Operation *` we are performing | ||
760 | * @param key current key code | ||
761 | * @param value the `struct ElementEntry *` from the hash map | ||
762 | * @return #GNUNET_YES (we should continue to iterate) | ||
763 | */ | ||
764 | static int | ||
765 | filtered_map_initialization (void *cls, | ||
766 | const struct GNUNET_HashCode *key, | ||
767 | void *value) | ||
768 | { | ||
769 | struct Operation *op = cls; | ||
770 | struct ElementEntry *ee = value; | ||
771 | struct GNUNET_HashCode mutated_hash; | ||
772 | |||
773 | |||
774 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
775 | "FIMA called for %s:%u\n", | ||
776 | GNUNET_h2s (&ee->element_hash), | ||
777 | ee->element.size); | ||
778 | |||
779 | if (GNUNET_NO == _GSS_is_element_of_operation (ee, op)) | ||
780 | { | ||
781 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
782 | "Reduced initialization, not starting with %s:%u (wrong generation)\n", | ||
783 | GNUNET_h2s (&ee->element_hash), | ||
784 | ee->element.size); | ||
785 | return GNUNET_YES; /* element not valid in our operation's generation */ | ||
786 | } | ||
787 | |||
788 | /* Test if element is in other peer's bloomfilter */ | ||
789 | GNUNET_BLOCK_mingle_hash (&ee->element_hash, | ||
790 | op->state->salt, | ||
791 | &mutated_hash); | ||
792 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
793 | "Testing mingled hash %s with salt %u\n", | ||
794 | GNUNET_h2s (&mutated_hash), | ||
795 | op->state->salt); | ||
796 | if (GNUNET_NO == | ||
797 | GNUNET_CONTAINER_bloomfilter_test (op->state->remote_bf, | ||
798 | &mutated_hash)) | ||
799 | { | ||
800 | /* remove this element */ | ||
801 | send_client_removed_element (op, | ||
802 | &ee->element); | ||
803 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
804 | "Reduced initialization, not starting with %s:%u\n", | ||
805 | GNUNET_h2s (&ee->element_hash), | ||
806 | ee->element.size); | ||
807 | return GNUNET_YES; | ||
808 | } | ||
809 | op->state->my_element_count++; | ||
810 | GNUNET_CRYPTO_hash_xor (&op->state->my_xor, | ||
811 | &ee->element_hash, | ||
812 | &op->state->my_xor); | ||
813 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
814 | "Filtered initialization of my_elements, adding %s:%u\n", | ||
815 | GNUNET_h2s (&ee->element_hash), | ||
816 | ee->element.size); | ||
817 | GNUNET_break (GNUNET_YES == | ||
818 | GNUNET_CONTAINER_multihashmap_put (op->state->my_elements, | ||
819 | &ee->element_hash, | ||
820 | ee, | ||
821 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); | ||
822 | |||
823 | return GNUNET_YES; | ||
824 | } | ||
825 | |||
826 | |||
827 | /** | ||
828 | * Removes elements from our hashmap if they are not contained within the | ||
829 | * provided remote bloomfilter. | ||
830 | * | ||
831 | * @param cls closure with the `struct Operation *` | ||
832 | * @param key current key code | ||
833 | * @param value value in the hash map | ||
834 | * @return #GNUNET_YES (we should continue to iterate) | ||
835 | */ | ||
836 | static int | ||
837 | iterator_bf_reduce (void *cls, | ||
838 | const struct GNUNET_HashCode *key, | ||
839 | void *value) | ||
840 | { | ||
841 | struct Operation *op = cls; | ||
842 | struct ElementEntry *ee = value; | ||
843 | struct GNUNET_HashCode mutated_hash; | ||
844 | |||
845 | GNUNET_BLOCK_mingle_hash (&ee->element_hash, | ||
846 | op->state->salt, | ||
847 | &mutated_hash); | ||
848 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
849 | "Testing mingled hash %s with salt %u\n", | ||
850 | GNUNET_h2s (&mutated_hash), | ||
851 | op->state->salt); | ||
852 | if (GNUNET_NO == | ||
853 | GNUNET_CONTAINER_bloomfilter_test (op->state->remote_bf, | ||
854 | &mutated_hash)) | ||
855 | { | ||
856 | GNUNET_break (0 < op->state->my_element_count); | ||
857 | op->state->my_element_count--; | ||
858 | GNUNET_CRYPTO_hash_xor (&op->state->my_xor, | ||
859 | &ee->element_hash, | ||
860 | &op->state->my_xor); | ||
861 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
862 | "Bloom filter reduction of my_elements, removing %s:%u\n", | ||
863 | GNUNET_h2s (&ee->element_hash), | ||
864 | ee->element.size); | ||
865 | GNUNET_assert (GNUNET_YES == | ||
866 | GNUNET_CONTAINER_multihashmap_remove (op->state->my_elements, | ||
867 | &ee->element_hash, | ||
868 | ee)); | ||
869 | send_client_removed_element (op, | ||
870 | &ee->element); | ||
871 | } | ||
872 | else | ||
873 | { | ||
874 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
875 | "Bloom filter reduction of my_elements, keeping %s:%u\n", | ||
876 | GNUNET_h2s (&ee->element_hash), | ||
877 | ee->element.size); | ||
878 | } | ||
879 | return GNUNET_YES; | ||
880 | } | ||
881 | |||
882 | |||
883 | /** | ||
884 | * Create initial bloomfilter based on all the elements given. | ||
885 | * | ||
886 | * @param cls the `struct Operation *` | ||
887 | * @param key current key code | ||
888 | * @param value the `struct ElementEntry` to process | ||
889 | * @return #GNUNET_YES (we should continue to iterate) | ||
890 | */ | ||
891 | static int | ||
892 | iterator_bf_create (void *cls, | ||
893 | const struct GNUNET_HashCode *key, | ||
894 | void *value) | ||
895 | { | ||
896 | struct Operation *op = cls; | ||
897 | struct ElementEntry *ee = value; | ||
898 | struct GNUNET_HashCode mutated_hash; | ||
899 | |||
900 | GNUNET_BLOCK_mingle_hash (&ee->element_hash, | ||
901 | op->state->salt, | ||
902 | &mutated_hash); | ||
903 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
904 | "Initializing BF with hash %s with salt %u\n", | ||
905 | GNUNET_h2s (&mutated_hash), | ||
906 | op->state->salt); | ||
907 | GNUNET_CONTAINER_bloomfilter_add (op->state->local_bf, | ||
908 | &mutated_hash); | ||
909 | return GNUNET_YES; | ||
910 | } | ||
911 | |||
912 | |||
913 | /** | ||
914 | * Inform the client that the intersection operation has failed, | ||
915 | * and proceed to destroy the evaluate operation. | ||
916 | * | ||
917 | * @param op the intersection operation to fail | ||
918 | */ | ||
919 | static void | ||
920 | fail_intersection_operation (struct Operation *op) | ||
921 | { | ||
922 | struct GNUNET_MQ_Envelope *ev; | ||
923 | struct GNUNET_SET_ResultMessage *msg; | ||
924 | |||
925 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
926 | "Intersection operation failed\n"); | ||
927 | GNUNET_STATISTICS_update (_GSS_statistics, | ||
928 | "# Intersection operations failed", | ||
929 | 1, | ||
930 | GNUNET_NO); | ||
931 | if (NULL != op->state->my_elements) | ||
932 | { | ||
933 | GNUNET_CONTAINER_multihashmap_destroy (op->state->my_elements); | ||
934 | op->state->my_elements = NULL; | ||
935 | } | ||
936 | ev = GNUNET_MQ_msg (msg, | ||
937 | GNUNET_MESSAGE_TYPE_SET_RESULT); | ||
938 | msg->result_status = htons (GNUNET_SET_STATUS_FAILURE); | ||
939 | msg->request_id = htonl (op->client_request_id); | ||
940 | msg->element_type = htons (0); | ||
941 | GNUNET_MQ_send (op->set->cs->mq, | ||
942 | ev); | ||
943 | _GSS_operation_destroy (op, | ||
944 | GNUNET_YES); | ||
945 | } | ||
946 | |||
947 | |||
948 | /** | ||
949 | * Send a bloomfilter to our peer. After the result done message has | ||
950 | * been sent to the client, destroy the evaluate operation. | ||
951 | * | ||
952 | * @param op intersection operation | ||
953 | */ | ||
954 | static void | ||
955 | send_bloomfilter (struct Operation *op) | ||
956 | { | ||
957 | struct GNUNET_MQ_Envelope *ev; | ||
958 | struct BFMessage *msg; | ||
959 | uint32_t bf_size; | ||
960 | uint32_t bf_elementbits; | ||
961 | uint32_t chunk_size; | ||
962 | char *bf_data; | ||
963 | uint32_t offset; | ||
964 | |||
965 | /* We consider the ratio of the set sizes to determine | ||
966 | the number of bits per element, as the smaller set | ||
967 | should use more bits to maximize its set reduction | ||
968 | potential and minimize overall bandwidth consumption. */ | ||
969 | bf_elementbits = 2 + ceil (log2 ((double) | ||
970 | (op->remote_element_count | ||
971 | / (double) op->state->my_element_count))); | ||
972 | if (bf_elementbits < 1) | ||
973 | bf_elementbits = 1; /* make sure k is not 0 */ | ||
974 | /* optimize BF-size to ~50% of bits set */ | ||
975 | bf_size = ceil ((double) (op->state->my_element_count | ||
976 | * bf_elementbits / log (2))); | ||
977 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
978 | "Sending Bloom filter (%u) of size %u bytes\n", | ||
979 | (unsigned int) bf_elementbits, | ||
980 | (unsigned int) bf_size); | ||
981 | op->state->local_bf = GNUNET_CONTAINER_bloomfilter_init (NULL, | ||
982 | bf_size, | ||
983 | bf_elementbits); | ||
984 | op->state->salt = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, | ||
985 | UINT32_MAX); | ||
986 | GNUNET_CONTAINER_multihashmap_iterate (op->state->my_elements, | ||
987 | &iterator_bf_create, | ||
988 | op); | ||
989 | |||
990 | /* send our Bloom filter */ | ||
991 | GNUNET_STATISTICS_update (_GSS_statistics, | ||
992 | "# Intersection Bloom filters sent", | ||
993 | 1, | ||
994 | GNUNET_NO); | ||
995 | chunk_size = 60 * 1024 - sizeof(struct BFMessage); | ||
996 | if (bf_size <= chunk_size) | ||
997 | { | ||
998 | /* singlepart */ | ||
999 | chunk_size = bf_size; | ||
1000 | ev = GNUNET_MQ_msg_extra (msg, | ||
1001 | chunk_size, | ||
1002 | GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF); | ||
1003 | GNUNET_assert (GNUNET_SYSERR != | ||
1004 | GNUNET_CONTAINER_bloomfilter_get_raw_data ( | ||
1005 | op->state->local_bf, | ||
1006 | (char *) &msg[1], | ||
1007 | bf_size)); | ||
1008 | msg->sender_element_count = htonl (op->state->my_element_count); | ||
1009 | msg->bloomfilter_total_length = htonl (bf_size); | ||
1010 | msg->bits_per_element = htonl (bf_elementbits); | ||
1011 | msg->sender_mutator = htonl (op->state->salt); | ||
1012 | msg->element_xor_hash = op->state->my_xor; | ||
1013 | GNUNET_MQ_send (op->mq, ev); | ||
1014 | } | ||
1015 | else | ||
1016 | { | ||
1017 | /* multipart */ | ||
1018 | bf_data = GNUNET_malloc (bf_size); | ||
1019 | GNUNET_assert (GNUNET_SYSERR != | ||
1020 | GNUNET_CONTAINER_bloomfilter_get_raw_data ( | ||
1021 | op->state->local_bf, | ||
1022 | bf_data, | ||
1023 | bf_size)); | ||
1024 | offset = 0; | ||
1025 | while (offset < bf_size) | ||
1026 | { | ||
1027 | if (bf_size - chunk_size < offset) | ||
1028 | chunk_size = bf_size - offset; | ||
1029 | ev = GNUNET_MQ_msg_extra (msg, | ||
1030 | chunk_size, | ||
1031 | GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF); | ||
1032 | GNUNET_memcpy (&msg[1], | ||
1033 | &bf_data[offset], | ||
1034 | chunk_size); | ||
1035 | offset += chunk_size; | ||
1036 | msg->sender_element_count = htonl (op->state->my_element_count); | ||
1037 | msg->bloomfilter_total_length = htonl (bf_size); | ||
1038 | msg->bits_per_element = htonl (bf_elementbits); | ||
1039 | msg->sender_mutator = htonl (op->state->salt); | ||
1040 | msg->element_xor_hash = op->state->my_xor; | ||
1041 | GNUNET_MQ_send (op->mq, ev); | ||
1042 | } | ||
1043 | GNUNET_free (bf_data); | ||
1044 | } | ||
1045 | GNUNET_CONTAINER_bloomfilter_free (op->state->local_bf); | ||
1046 | op->state->local_bf = NULL; | ||
1047 | } | ||
1048 | |||
1049 | |||
1050 | /** | ||
1051 | * Signal to the client that the operation has finished and | ||
1052 | * destroy the operation. | ||
1053 | * | ||
1054 | * @param cls operation to destroy | ||
1055 | */ | ||
1056 | static void | ||
1057 | send_client_done_and_destroy (void *cls) | ||
1058 | { | ||
1059 | struct Operation *op = cls; | ||
1060 | struct GNUNET_MQ_Envelope *ev; | ||
1061 | struct GNUNET_SET_ResultMessage *rm; | ||
1062 | |||
1063 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1064 | "Intersection succeeded, sending DONE to local client\n"); | ||
1065 | GNUNET_STATISTICS_update (_GSS_statistics, | ||
1066 | "# Intersection operations succeeded", | ||
1067 | 1, | ||
1068 | GNUNET_NO); | ||
1069 | ev = GNUNET_MQ_msg (rm, | ||
1070 | GNUNET_MESSAGE_TYPE_SET_RESULT); | ||
1071 | rm->request_id = htonl (op->client_request_id); | ||
1072 | rm->result_status = htons (GNUNET_SET_STATUS_DONE); | ||
1073 | rm->element_type = htons (0); | ||
1074 | GNUNET_MQ_send (op->set->cs->mq, | ||
1075 | ev); | ||
1076 | _GSS_operation_destroy (op, | ||
1077 | GNUNET_YES); | ||
1078 | } | ||
1079 | |||
1080 | |||
1081 | /** | ||
1082 | * Remember that we are done dealing with the local client | ||
1083 | * AND have sent the other peer our message that we are done, | ||
1084 | * so we are not just waiting for the channel to die before | ||
1085 | * telling the local client that we are done as our last act. | ||
1086 | * | ||
1087 | * @param cls the `struct Operation`. | ||
1088 | */ | ||
1089 | static void | ||
1090 | finished_local_operations (void *cls) | ||
1091 | { | ||
1092 | struct Operation *op = cls; | ||
1093 | |||
1094 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1095 | "DONE sent to other peer, now waiting for other end to close the channel\n"); | ||
1096 | op->state->phase = PHASE_FINISHED; | ||
1097 | op->state->channel_death_expected = GNUNET_YES; | ||
1098 | } | ||
1099 | |||
1100 | |||
1101 | /** | ||
1102 | * Notify the other peer that we are done. Once this message | ||
1103 | * is out, we still need to notify the local client that we | ||
1104 | * are done. | ||
1105 | * | ||
1106 | * @param op operation to notify for. | ||
1107 | */ | ||
1108 | static void | ||
1109 | send_p2p_done (struct Operation *op) | ||
1110 | { | ||
1111 | struct GNUNET_MQ_Envelope *ev; | ||
1112 | struct IntersectionDoneMessage *idm; | ||
1113 | |||
1114 | GNUNET_assert (PHASE_MUST_SEND_DONE == op->state->phase); | ||
1115 | GNUNET_assert (GNUNET_NO == op->state->channel_death_expected); | ||
1116 | ev = GNUNET_MQ_msg (idm, | ||
1117 | GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_DONE); | ||
1118 | idm->final_element_count = htonl (op->state->my_element_count); | ||
1119 | idm->element_xor_hash = op->state->my_xor; | ||
1120 | GNUNET_MQ_notify_sent (ev, | ||
1121 | &finished_local_operations, | ||
1122 | op); | ||
1123 | GNUNET_MQ_send (op->mq, | ||
1124 | ev); | ||
1125 | } | ||
1126 | |||
1127 | |||
1128 | /** | ||
1129 | * Send all elements in the full result iterator. | ||
1130 | * | ||
1131 | * @param cls the `struct Operation *` | ||
1132 | */ | ||
1133 | static void | ||
1134 | send_remaining_elements (void *cls) | ||
1135 | { | ||
1136 | struct Operation *op = cls; | ||
1137 | const void *nxt; | ||
1138 | const struct ElementEntry *ee; | ||
1139 | struct GNUNET_MQ_Envelope *ev; | ||
1140 | struct GNUNET_SET_ResultMessage *rm; | ||
1141 | const struct GNUNET_SET_Element *element; | ||
1142 | int res; | ||
1143 | |||
1144 | res = GNUNET_CONTAINER_multihashmap_iterator_next ( | ||
1145 | op->state->full_result_iter, | ||
1146 | NULL, | ||
1147 | &nxt); | ||
1148 | if (GNUNET_NO == res) | ||
1149 | { | ||
1150 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1151 | "Sending done and destroy because iterator ran out\n"); | ||
1152 | GNUNET_CONTAINER_multihashmap_iterator_destroy ( | ||
1153 | op->state->full_result_iter); | ||
1154 | op->state->full_result_iter = NULL; | ||
1155 | if (PHASE_DONE_RECEIVED == op->state->phase) | ||
1156 | { | ||
1157 | op->state->phase = PHASE_FINISHED; | ||
1158 | send_client_done_and_destroy (op); | ||
1159 | } | ||
1160 | else if (PHASE_MUST_SEND_DONE == op->state->phase) | ||
1161 | { | ||
1162 | send_p2p_done (op); | ||
1163 | } | ||
1164 | else | ||
1165 | { | ||
1166 | GNUNET_assert (0); | ||
1167 | } | ||
1168 | return; | ||
1169 | } | ||
1170 | ee = nxt; | ||
1171 | element = &ee->element; | ||
1172 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1173 | "Sending element %s:%u to client (full set)\n", | ||
1174 | GNUNET_h2s (&ee->element_hash), | ||
1175 | element->size); | ||
1176 | GNUNET_assert (0 != op->client_request_id); | ||
1177 | ev = GNUNET_MQ_msg_extra (rm, | ||
1178 | element->size, | ||
1179 | GNUNET_MESSAGE_TYPE_SET_RESULT); | ||
1180 | GNUNET_assert (NULL != ev); | ||
1181 | rm->result_status = htons (GNUNET_SET_STATUS_OK); | ||
1182 | rm->request_id = htonl (op->client_request_id); | ||
1183 | rm->element_type = element->element_type; | ||
1184 | GNUNET_memcpy (&rm[1], | ||
1185 | element->data, | ||
1186 | element->size); | ||
1187 | GNUNET_MQ_notify_sent (ev, | ||
1188 | &send_remaining_elements, | ||
1189 | op); | ||
1190 | GNUNET_MQ_send (op->set->cs->mq, | ||
1191 | ev); | ||
1192 | } | ||
1193 | |||
1194 | |||
1195 | /** | ||
1196 | * Fills the "my_elements" hashmap with the initial set of | ||
1197 | * (non-deleted) elements from the set of the specification. | ||
1198 | * | ||
1199 | * @param cls closure with the `struct Operation *` | ||
1200 | * @param key current key code for the element | ||
1201 | * @param value value in the hash map with the `struct ElementEntry *` | ||
1202 | * @return #GNUNET_YES (we should continue to iterate) | ||
1203 | */ | ||
1204 | static int | ||
1205 | initialize_map_unfiltered (void *cls, | ||
1206 | const struct GNUNET_HashCode *key, | ||
1207 | void *value) | ||
1208 | { | ||
1209 | struct ElementEntry *ee = value; | ||
1210 | struct Operation *op = cls; | ||
1211 | |||
1212 | if (GNUNET_NO == _GSS_is_element_of_operation (ee, op)) | ||
1213 | return GNUNET_YES; /* element not live in operation's generation */ | ||
1214 | GNUNET_CRYPTO_hash_xor (&op->state->my_xor, | ||
1215 | &ee->element_hash, | ||
1216 | &op->state->my_xor); | ||
1217 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1218 | "Initial full initialization of my_elements, adding %s:%u\n", | ||
1219 | GNUNET_h2s (&ee->element_hash), | ||
1220 | ee->element.size); | ||
1221 | GNUNET_break (GNUNET_YES == | ||
1222 | GNUNET_CONTAINER_multihashmap_put (op->state->my_elements, | ||
1223 | &ee->element_hash, | ||
1224 | ee, | ||
1225 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); | ||
1226 | return GNUNET_YES; | ||
1227 | } | ||
1228 | |||
1229 | |||
1230 | /** | ||
1231 | * Send our element count to the peer, in case our element count is | ||
1232 | * lower than theirs. | ||
1233 | * | ||
1234 | * @param op intersection operation | ||
1235 | */ | ||
1236 | static void | ||
1237 | send_element_count (struct Operation *op) | ||
1238 | { | ||
1239 | struct GNUNET_MQ_Envelope *ev; | ||
1240 | struct IntersectionElementInfoMessage *msg; | ||
1241 | |||
1242 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1243 | "Sending our element count (%u)\n", | ||
1244 | op->state->my_element_count); | ||
1245 | ev = GNUNET_MQ_msg (msg, | ||
1246 | GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_ELEMENT_INFO); | ||
1247 | msg->sender_element_count = htonl (op->state->my_element_count); | ||
1248 | GNUNET_MQ_send (op->mq, ev); | ||
1249 | } | ||
1250 | |||
1251 | |||
1252 | /** | ||
1253 | * We go first, initialize our map with all elements and | ||
1254 | * send the first Bloom filter. | ||
1255 | * | ||
1256 | * @param op operation to start exchange for | ||
1257 | */ | ||
1258 | static void | ||
1259 | begin_bf_exchange (struct Operation *op) | ||
1260 | { | ||
1261 | op->state->phase = PHASE_BF_EXCHANGE; | ||
1262 | GNUNET_CONTAINER_multihashmap_iterate (op->set->content->elements, | ||
1263 | &initialize_map_unfiltered, | ||
1264 | op); | ||
1265 | send_bloomfilter (op); | ||
1266 | } | ||
1267 | |||
1268 | |||
1269 | /** | ||
1270 | * Handle the initial `struct IntersectionElementInfoMessage` from a | ||
1271 | * remote peer. | ||
1272 | * | ||
1273 | * @param cls the intersection operation | ||
1274 | * @param mh the header of the message | ||
1275 | */ | ||
1276 | void | ||
1277 | handle_intersection_p2p_element_info (void *cls, | ||
1278 | const struct | ||
1279 | IntersectionElementInfoMessage *msg) | ||
1280 | { | ||
1281 | struct Operation *op = cls; | ||
1282 | |||
1283 | if (GNUNET_SET_OPERATION_INTERSECTION != op->set->operation) | ||
1284 | { | ||
1285 | GNUNET_break_op (0); | ||
1286 | fail_intersection_operation (op); | ||
1287 | return; | ||
1288 | } | ||
1289 | op->remote_element_count = ntohl (msg->sender_element_count); | ||
1290 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1291 | "Received remote element count (%u), I have %u\n", | ||
1292 | op->remote_element_count, | ||
1293 | op->state->my_element_count); | ||
1294 | if (((PHASE_INITIAL != op->state->phase) && | ||
1295 | (PHASE_COUNT_SENT != op->state->phase)) || | ||
1296 | (op->state->my_element_count > op->remote_element_count) || | ||
1297 | (0 == op->state->my_element_count) || | ||
1298 | (0 == op->remote_element_count)) | ||
1299 | { | ||
1300 | GNUNET_break_op (0); | ||
1301 | fail_intersection_operation (op); | ||
1302 | return; | ||
1303 | } | ||
1304 | GNUNET_break (NULL == op->state->remote_bf); | ||
1305 | begin_bf_exchange (op); | ||
1306 | GNUNET_CADET_receive_done (op->channel); | ||
1307 | } | ||
1308 | |||
1309 | |||
1310 | /** | ||
1311 | * Process a Bloomfilter once we got all the chunks. | ||
1312 | * | ||
1313 | * @param op the intersection operation | ||
1314 | */ | ||
1315 | static void | ||
1316 | process_bf (struct Operation *op) | ||
1317 | { | ||
1318 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1319 | "Received BF in phase %u, foreign count is %u, my element count is %u/%u\n", | ||
1320 | op->state->phase, | ||
1321 | op->remote_element_count, | ||
1322 | op->state->my_element_count, | ||
1323 | GNUNET_CONTAINER_multihashmap_size (op->set->content->elements)); | ||
1324 | switch (op->state->phase) | ||
1325 | { | ||
1326 | case PHASE_INITIAL: | ||
1327 | GNUNET_break_op (0); | ||
1328 | fail_intersection_operation (op); | ||
1329 | return; | ||
1330 | |||
1331 | case PHASE_COUNT_SENT: | ||
1332 | /* This is the first BF being sent, build our initial map with | ||
1333 | filtering in place */ | ||
1334 | op->state->my_element_count = 0; | ||
1335 | GNUNET_CONTAINER_multihashmap_iterate (op->set->content->elements, | ||
1336 | &filtered_map_initialization, | ||
1337 | op); | ||
1338 | break; | ||
1339 | |||
1340 | case PHASE_BF_EXCHANGE: | ||
1341 | /* Update our set by reduction */ | ||
1342 | GNUNET_CONTAINER_multihashmap_iterate (op->state->my_elements, | ||
1343 | &iterator_bf_reduce, | ||
1344 | op); | ||
1345 | break; | ||
1346 | |||
1347 | case PHASE_MUST_SEND_DONE: | ||
1348 | GNUNET_break_op (0); | ||
1349 | fail_intersection_operation (op); | ||
1350 | return; | ||
1351 | |||
1352 | case PHASE_DONE_RECEIVED: | ||
1353 | GNUNET_break_op (0); | ||
1354 | fail_intersection_operation (op); | ||
1355 | return; | ||
1356 | |||
1357 | case PHASE_FINISHED: | ||
1358 | GNUNET_break_op (0); | ||
1359 | fail_intersection_operation (op); | ||
1360 | return; | ||
1361 | } | ||
1362 | GNUNET_CONTAINER_bloomfilter_free (op->state->remote_bf); | ||
1363 | op->state->remote_bf = NULL; | ||
1364 | |||
1365 | if ((0 == op->state->my_element_count) || /* fully disjoint */ | ||
1366 | ((op->state->my_element_count == op->remote_element_count) && | ||
1367 | (0 == GNUNET_memcmp (&op->state->my_xor, | ||
1368 | &op->state->other_xor)))) | ||
1369 | { | ||
1370 | /* we are done */ | ||
1371 | op->state->phase = PHASE_MUST_SEND_DONE; | ||
1372 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1373 | "Intersection succeeded, sending DONE to other peer\n"); | ||
1374 | GNUNET_CONTAINER_bloomfilter_free (op->state->local_bf); | ||
1375 | op->state->local_bf = NULL; | ||
1376 | if (GNUNET_SET_RESULT_FULL == op->result_mode) | ||
1377 | { | ||
1378 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1379 | "Sending full result set (%u elements)\n", | ||
1380 | GNUNET_CONTAINER_multihashmap_size (op->state->my_elements)); | ||
1381 | op->state->full_result_iter | ||
1382 | = GNUNET_CONTAINER_multihashmap_iterator_create ( | ||
1383 | op->state->my_elements); | ||
1384 | send_remaining_elements (op); | ||
1385 | return; | ||
1386 | } | ||
1387 | send_p2p_done (op); | ||
1388 | return; | ||
1389 | } | ||
1390 | op->state->phase = PHASE_BF_EXCHANGE; | ||
1391 | send_bloomfilter (op); | ||
1392 | } | ||
1393 | |||
1394 | |||
1395 | /** | ||
1396 | * Check an BF message from a remote peer. | ||
1397 | * | ||
1398 | * @param cls the intersection operation | ||
1399 | * @param msg the header of the message | ||
1400 | * @return #GNUNET_OK if @a msg is well-formed | ||
1401 | */ | ||
1402 | static int | ||
1403 | check_intersection_p2p_bf (void *cls, | ||
1404 | const struct BFMessage *msg) | ||
1405 | { | ||
1406 | struct Operation *op = cls; | ||
1407 | |||
1408 | if (GNUNET_SET_OPERATION_INTERSECTION != op->set->operation) | ||
1409 | { | ||
1410 | GNUNET_break_op (0); | ||
1411 | return GNUNET_SYSERR; | ||
1412 | } | ||
1413 | return GNUNET_OK; | ||
1414 | } | ||
1415 | |||
1416 | |||
1417 | /** | ||
1418 | * Handle an BF message from a remote peer. | ||
1419 | * | ||
1420 | * @param cls the intersection operation | ||
1421 | * @param msg the header of the message | ||
1422 | */ | ||
1423 | static | ||
1424 | handle_intersection_p2p_bf (void *cls, | ||
1425 | const struct BFMessage *msg) | ||
1426 | { | ||
1427 | struct Operation *op = cls; | ||
1428 | uint32_t bf_size; | ||
1429 | uint32_t chunk_size; | ||
1430 | uint32_t bf_bits_per_element; | ||
1431 | |||
1432 | switch (op->state->phase) | ||
1433 | { | ||
1434 | case PHASE_INITIAL: | ||
1435 | GNUNET_break_op (0); | ||
1436 | fail_intersection_operation (op); | ||
1437 | return; | ||
1438 | |||
1439 | case PHASE_COUNT_SENT: | ||
1440 | case PHASE_BF_EXCHANGE: | ||
1441 | bf_size = ntohl (msg->bloomfilter_total_length); | ||
1442 | bf_bits_per_element = ntohl (msg->bits_per_element); | ||
1443 | chunk_size = htons (msg->header.size) - sizeof(struct BFMessage); | ||
1444 | op->state->other_xor = msg->element_xor_hash; | ||
1445 | if (bf_size == chunk_size) | ||
1446 | { | ||
1447 | if (NULL != op->state->bf_data) | ||
1448 | { | ||
1449 | GNUNET_break_op (0); | ||
1450 | fail_intersection_operation (op); | ||
1451 | return; | ||
1452 | } | ||
1453 | /* single part, done here immediately */ | ||
1454 | op->state->remote_bf | ||
1455 | = GNUNET_CONTAINER_bloomfilter_init ((const char *) &msg[1], | ||
1456 | bf_size, | ||
1457 | bf_bits_per_element); | ||
1458 | op->state->salt = ntohl (msg->sender_mutator); | ||
1459 | op->remote_element_count = ntohl (msg->sender_element_count); | ||
1460 | process_bf (op); | ||
1461 | break; | ||
1462 | } | ||
1463 | /* multipart chunk */ | ||
1464 | if (NULL == op->state->bf_data) | ||
1465 | { | ||
1466 | /* first chunk, initialize */ | ||
1467 | op->state->bf_data = GNUNET_malloc (bf_size); | ||
1468 | op->state->bf_data_size = bf_size; | ||
1469 | op->state->bf_bits_per_element = bf_bits_per_element; | ||
1470 | op->state->bf_data_offset = 0; | ||
1471 | op->state->salt = ntohl (msg->sender_mutator); | ||
1472 | op->remote_element_count = ntohl (msg->sender_element_count); | ||
1473 | } | ||
1474 | else | ||
1475 | { | ||
1476 | /* increment */ | ||
1477 | if ((op->state->bf_data_size != bf_size) || | ||
1478 | (op->state->bf_bits_per_element != bf_bits_per_element) || | ||
1479 | (op->state->bf_data_offset + chunk_size > bf_size) || | ||
1480 | (op->state->salt != ntohl (msg->sender_mutator)) || | ||
1481 | (op->remote_element_count != ntohl (msg->sender_element_count))) | ||
1482 | { | ||
1483 | GNUNET_break_op (0); | ||
1484 | fail_intersection_operation (op); | ||
1485 | return; | ||
1486 | } | ||
1487 | } | ||
1488 | GNUNET_memcpy (&op->state->bf_data[op->state->bf_data_offset], | ||
1489 | (const char *) &msg[1], | ||
1490 | chunk_size); | ||
1491 | op->state->bf_data_offset += chunk_size; | ||
1492 | if (op->state->bf_data_offset == bf_size) | ||
1493 | { | ||
1494 | /* last chunk, run! */ | ||
1495 | op->state->remote_bf | ||
1496 | = GNUNET_CONTAINER_bloomfilter_init (op->state->bf_data, | ||
1497 | bf_size, | ||
1498 | bf_bits_per_element); | ||
1499 | GNUNET_free (op->state->bf_data); | ||
1500 | op->state->bf_data = NULL; | ||
1501 | op->state->bf_data_size = 0; | ||
1502 | process_bf (op); | ||
1503 | } | ||
1504 | break; | ||
1505 | |||
1506 | default: | ||
1507 | GNUNET_break_op (0); | ||
1508 | fail_intersection_operation (op); | ||
1509 | return; | ||
1510 | } | ||
1511 | GNUNET_CADET_receive_done (op->channel); | ||
1512 | } | ||
1513 | |||
1514 | |||
1515 | /** | ||
1516 | * Remove all elements from our hashmap. | ||
1517 | * | ||
1518 | * @param cls closure with the `struct Operation *` | ||
1519 | * @param key current key code | ||
1520 | * @param value value in the hash map | ||
1521 | * @return #GNUNET_YES (we should continue to iterate) | ||
1522 | */ | ||
1523 | static int | ||
1524 | filter_all (void *cls, | ||
1525 | const struct GNUNET_HashCode *key, | ||
1526 | void *value) | ||
1527 | { | ||
1528 | struct Operation *op = cls; | ||
1529 | struct ElementEntry *ee = value; | ||
1530 | |||
1531 | GNUNET_break (0 < op->state->my_element_count); | ||
1532 | op->state->my_element_count--; | ||
1533 | GNUNET_CRYPTO_hash_xor (&op->state->my_xor, | ||
1534 | &ee->element_hash, | ||
1535 | &op->state->my_xor); | ||
1536 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1537 | "Final reduction of my_elements, removing %s:%u\n", | ||
1538 | GNUNET_h2s (&ee->element_hash), | ||
1539 | ee->element.size); | ||
1540 | GNUNET_assert (GNUNET_YES == | ||
1541 | GNUNET_CONTAINER_multihashmap_remove (op->state->my_elements, | ||
1542 | &ee->element_hash, | ||
1543 | ee)); | ||
1544 | send_client_removed_element (op, | ||
1545 | &ee->element); | ||
1546 | return GNUNET_YES; | ||
1547 | } | ||
1548 | |||
1549 | |||
1550 | /** | ||
1551 | * Handle a done message from a remote peer | ||
1552 | * | ||
1553 | * @param cls the intersection operation | ||
1554 | * @param mh the message | ||
1555 | */ | ||
1556 | static void | ||
1557 | handle_intersection_p2p_done (void *cls, | ||
1558 | const struct IntersectionDoneMessage *idm) | ||
1559 | { | ||
1560 | struct Operation *op = cls; | ||
1561 | |||
1562 | if (GNUNET_SET_OPERATION_INTERSECTION != op->set->operation) | ||
1563 | { | ||
1564 | GNUNET_break_op (0); | ||
1565 | fail_intersection_operation (op); | ||
1566 | return; | ||
1567 | } | ||
1568 | if (PHASE_BF_EXCHANGE != op->state->phase) | ||
1569 | { | ||
1570 | /* wrong phase to conclude? FIXME: Or should we allow this | ||
1571 | if the other peer has _initially_ already an empty set? */ | ||
1572 | GNUNET_break_op (0); | ||
1573 | fail_intersection_operation (op); | ||
1574 | return; | ||
1575 | } | ||
1576 | if (0 == ntohl (idm->final_element_count)) | ||
1577 | { | ||
1578 | /* other peer determined empty set is the intersection, | ||
1579 | remove all elements */ | ||
1580 | GNUNET_CONTAINER_multihashmap_iterate (op->state->my_elements, | ||
1581 | &filter_all, | ||
1582 | op); | ||
1583 | } | ||
1584 | if ((op->state->my_element_count != ntohl (idm->final_element_count)) || | ||
1585 | (0 != GNUNET_memcmp (&op->state->my_xor, | ||
1586 | &idm->element_xor_hash))) | ||
1587 | { | ||
1588 | /* Other peer thinks we are done, but we disagree on the result! */ | ||
1589 | GNUNET_break_op (0); | ||
1590 | fail_intersection_operation (op); | ||
1591 | return; | ||
1592 | } | ||
1593 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1594 | "Got IntersectionDoneMessage, have %u elements in intersection\n", | ||
1595 | op->state->my_element_count); | ||
1596 | op->state->phase = PHASE_DONE_RECEIVED; | ||
1597 | GNUNET_CADET_receive_done (op->channel); | ||
1598 | |||
1599 | GNUNET_assert (GNUNET_NO == op->state->client_done_sent); | ||
1600 | if (GNUNET_SET_RESULT_FULL == op->result_mode) | ||
1601 | { | ||
1602 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1603 | "Sending full result set to client (%u elements)\n", | ||
1604 | GNUNET_CONTAINER_multihashmap_size (op->state->my_elements)); | ||
1605 | op->state->full_result_iter | ||
1606 | = GNUNET_CONTAINER_multihashmap_iterator_create (op->state->my_elements); | ||
1607 | send_remaining_elements (op); | ||
1608 | return; | ||
1609 | } | ||
1610 | op->state->phase = PHASE_FINISHED; | ||
1611 | send_client_done_and_destroy (op); | ||
1612 | } | ||
1613 | |||
1614 | |||
1615 | /** | ||
1616 | * Initiate a set intersection operation with a remote peer. | ||
1617 | * | ||
1618 | * @param op operation that is created, should be initialized to | ||
1619 | * begin the evaluation | ||
1620 | * @param opaque_context message to be transmitted to the listener | ||
1621 | * to convince it to accept, may be NULL | ||
1622 | * @return operation-specific state to keep in @a op | ||
1623 | */ | ||
1624 | static struct OperationState * | ||
1625 | intersection_evaluate (struct Operation *op, | ||
1626 | const struct GNUNET_MessageHeader *opaque_context) | ||
1627 | { | ||
1628 | struct OperationState *state; | ||
1629 | struct GNUNET_MQ_Envelope *ev; | ||
1630 | struct OperationRequestMessage *msg; | ||
1631 | |||
1632 | ev = GNUNET_MQ_msg_nested_mh (msg, | ||
1633 | GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST, | ||
1634 | opaque_context); | ||
1635 | if (NULL == ev) | ||
1636 | { | ||
1637 | /* the context message is too large!? */ | ||
1638 | GNUNET_break (0); | ||
1639 | return NULL; | ||
1640 | } | ||
1641 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1642 | "Initiating intersection operation evaluation\n"); | ||
1643 | state = GNUNET_new (struct OperationState); | ||
1644 | /* we started the operation, thus we have to send the operation request */ | ||
1645 | state->phase = PHASE_INITIAL; | ||
1646 | state->my_element_count = op->set->state->current_set_element_count; | ||
1647 | state->my_elements | ||
1648 | = GNUNET_CONTAINER_multihashmap_create (state->my_element_count, | ||
1649 | GNUNET_YES); | ||
1650 | |||
1651 | msg->operation = htonl (GNUNET_SET_OPERATION_INTERSECTION); | ||
1652 | msg->element_count = htonl (state->my_element_count); | ||
1653 | GNUNET_MQ_send (op->mq, | ||
1654 | ev); | ||
1655 | state->phase = PHASE_COUNT_SENT; | ||
1656 | if (NULL != opaque_context) | ||
1657 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1658 | "Sent op request with context message\n"); | ||
1659 | else | ||
1660 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1661 | "Sent op request without context message\n"); | ||
1662 | return state; | ||
1663 | } | ||
1664 | |||
1665 | |||
1666 | /** | ||
1667 | * Accept an intersection operation request from a remote peer. Only | ||
1668 | * initializes the private operation state. | ||
1669 | * | ||
1670 | * @param op operation that will be accepted as an intersection operation | ||
1671 | */ | ||
1672 | static struct OperationState * | ||
1673 | intersection_accept (struct Operation *op) | ||
1674 | { | ||
1675 | struct OperationState *state; | ||
1676 | |||
1677 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1678 | "Accepting set intersection operation\n"); | ||
1679 | state = GNUNET_new (struct OperationState); | ||
1680 | state->phase = PHASE_INITIAL; | ||
1681 | state->my_element_count | ||
1682 | = op->set->state->current_set_element_count; | ||
1683 | state->my_elements | ||
1684 | = GNUNET_CONTAINER_multihashmap_create (GNUNET_MIN (state->my_element_count, | ||
1685 | op->remote_element_count), | ||
1686 | GNUNET_YES); | ||
1687 | op->state = state; | ||
1688 | if (op->remote_element_count < state->my_element_count) | ||
1689 | { | ||
1690 | /* If the other peer (Alice) has fewer elements than us (Bob), | ||
1691 | we just send the count as Alice should send the first BF */ | ||
1692 | send_element_count (op); | ||
1693 | state->phase = PHASE_COUNT_SENT; | ||
1694 | return state; | ||
1695 | } | ||
1696 | /* We have fewer elements, so we start with the BF */ | ||
1697 | begin_bf_exchange (op); | ||
1698 | return state; | ||
1699 | } | ||
1700 | |||
1701 | |||
1702 | /** | ||
1703 | * Destroy the intersection operation. Only things specific to the | ||
1704 | * intersection operation are destroyed. | ||
1705 | * | ||
1706 | * @param op intersection operation to destroy | ||
1707 | */ | ||
1708 | static void | ||
1709 | intersection_op_cancel (struct Operation *op) | ||
1710 | { | ||
1711 | /* check if the op was canceled twice */ | ||
1712 | GNUNET_assert (NULL != op->state); | ||
1713 | if (NULL != op->state->remote_bf) | ||
1714 | { | ||
1715 | GNUNET_CONTAINER_bloomfilter_free (op->state->remote_bf); | ||
1716 | op->state->remote_bf = NULL; | ||
1717 | } | ||
1718 | if (NULL != op->state->local_bf) | ||
1719 | { | ||
1720 | GNUNET_CONTAINER_bloomfilter_free (op->state->local_bf); | ||
1721 | op->state->local_bf = NULL; | ||
1722 | } | ||
1723 | if (NULL != op->state->my_elements) | ||
1724 | { | ||
1725 | GNUNET_CONTAINER_multihashmap_destroy (op->state->my_elements); | ||
1726 | op->state->my_elements = NULL; | ||
1727 | } | ||
1728 | if (NULL != op->state->full_result_iter) | ||
1729 | { | ||
1730 | GNUNET_CONTAINER_multihashmap_iterator_destroy ( | ||
1731 | op->state->full_result_iter); | ||
1732 | op->state->full_result_iter = NULL; | ||
1733 | } | ||
1734 | GNUNET_free (op->state); | ||
1735 | op->state = NULL; | ||
1736 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1737 | "Destroying intersection op state done\n"); | ||
1738 | } | ||
1739 | |||
1740 | |||
1741 | /** | ||
1742 | * Create a new set supporting the intersection operation. | ||
1743 | * | ||
1744 | * @return the newly created set | ||
1745 | */ | ||
1746 | static struct SetState * | ||
1747 | intersection_set_create () | ||
1748 | { | ||
1749 | struct SetState *set_state; | ||
1750 | |||
1751 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1752 | "Intersection set created\n"); | ||
1753 | set_state = GNUNET_new (struct SetState); | ||
1754 | set_state->current_set_element_count = 0; | ||
1755 | |||
1756 | return set_state; | ||
1757 | } | ||
1758 | |||
1759 | |||
1760 | /** | ||
1761 | * Add the element from the given element message to the set. | ||
1762 | * | ||
1763 | * @param set_state state of the set want to add to | ||
1764 | * @param ee the element to add to the set | ||
1765 | */ | ||
1766 | static void | ||
1767 | intersection_add (struct SetState *set_state, | ||
1768 | struct ElementEntry *ee) | ||
1769 | { | ||
1770 | set_state->current_set_element_count++; | ||
1771 | } | ||
1772 | |||
1773 | |||
1774 | /** | ||
1775 | * Destroy a set that supports the intersection operation | ||
1776 | * | ||
1777 | * @param set_state the set to destroy | ||
1778 | */ | ||
1779 | static void | ||
1780 | intersection_set_destroy (struct SetState *set_state) | ||
1781 | { | ||
1782 | GNUNET_free (set_state); | ||
1783 | } | ||
1784 | |||
1785 | |||
1786 | /** | ||
1787 | * Remove the element given in the element message from the set. | ||
1788 | * | ||
1789 | * @param set_state state of the set to remove from | ||
1790 | * @param element set element to remove | ||
1791 | */ | ||
1792 | static void | ||
1793 | intersection_remove (struct SetState *set_state, | ||
1794 | struct ElementEntry *element) | ||
1795 | { | ||
1796 | GNUNET_assert (0 < set_state->current_set_element_count); | ||
1797 | set_state->current_set_element_count--; | ||
1798 | } | ||
1799 | |||
1800 | |||
1801 | /** | ||
1802 | * Callback for channel death for the intersection operation. | ||
1803 | * | ||
1804 | * @param op operation that lost the channel | ||
1805 | */ | ||
1806 | static void | ||
1807 | intersection_channel_death (struct Operation *op) | ||
1808 | { | ||
1809 | if (GNUNET_YES == op->state->channel_death_expected) | ||
1810 | { | ||
1811 | /* oh goodie, we are done! */ | ||
1812 | send_client_done_and_destroy (op); | ||
1813 | } | ||
1814 | else | ||
1815 | { | ||
1816 | /* sorry, channel went down early, too bad. */ | ||
1817 | _GSS_operation_destroy (op, | ||
1818 | GNUNET_YES); | ||
1819 | } | ||
1820 | } | ||
1821 | |||
1822 | |||
1823 | /** | ||
1824 | * Get the incoming socket associated with the given id. | ||
1825 | * | ||
1826 | * @param listener the listener to look in | ||
1827 | * @param id id to look for | ||
1828 | * @return the incoming socket associated with the id, | ||
1829 | * or NULL if there is none | ||
1830 | */ | ||
1831 | static struct Operation * | ||
1832 | get_incoming (uint32_t id) | ||
1833 | { | ||
1834 | for (struct Listener *listener = listener_head; NULL != listener; | ||
1835 | listener = listener->next) | ||
1836 | { | ||
1837 | for (struct Operation *op = listener->op_head; NULL != op; op = op->next) | ||
1838 | if (op->suggest_id == id) | ||
1839 | return op; | ||
1840 | } | ||
1841 | return NULL; | ||
1842 | } | ||
1843 | |||
1844 | |||
1845 | /** | ||
1846 | * Destroy an incoming request from a remote peer | ||
1847 | * | ||
1848 | * @param op remote request to destroy | ||
1849 | */ | ||
1850 | static void | ||
1851 | incoming_destroy (struct Operation *op) | ||
1852 | { | ||
1853 | struct Listener *listener; | ||
1854 | |||
1855 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1856 | "Destroying incoming operation %p\n", | ||
1857 | op); | ||
1858 | if (NULL != (listener = op->listener)) | ||
1859 | { | ||
1860 | GNUNET_CONTAINER_DLL_remove (listener->op_head, listener->op_tail, op); | ||
1861 | op->listener = NULL; | ||
1862 | } | ||
1863 | if (NULL != op->timeout_task) | ||
1864 | { | ||
1865 | GNUNET_SCHEDULER_cancel (op->timeout_task); | ||
1866 | op->timeout_task = NULL; | ||
1867 | } | ||
1868 | _GSS_operation_destroy2 (op); | ||
1869 | } | ||
1870 | |||
1871 | |||
1872 | /** | ||
1873 | * Context for the #garbage_collect_cb(). | ||
1874 | */ | ||
1875 | struct GarbageContext | ||
1876 | { | ||
1877 | /** | ||
1878 | * Map for which we are garbage collecting removed elements. | ||
1879 | */ | ||
1880 | struct GNUNET_CONTAINER_MultiHashMap *map; | ||
1881 | |||
1882 | /** | ||
1883 | * Lowest generation for which an operation is still pending. | ||
1884 | */ | ||
1885 | unsigned int min_op_generation; | ||
1886 | |||
1887 | /** | ||
1888 | * Largest generation for which an operation is still pending. | ||
1889 | */ | ||
1890 | unsigned int max_op_generation; | ||
1891 | }; | ||
1892 | |||
1893 | |||
1894 | /** | ||
1895 | * Function invoked to check if an element can be removed from | ||
1896 | * the set's history because it is no longer needed. | ||
1897 | * | ||
1898 | * @param cls the `struct GarbageContext *` | ||
1899 | * @param key key of the element in the map | ||
1900 | * @param value the `struct ElementEntry *` | ||
1901 | * @return #GNUNET_OK (continue to iterate) | ||
1902 | */ | ||
1903 | static int | ||
1904 | garbage_collect_cb (void *cls, const struct GNUNET_HashCode *key, void *value) | ||
1905 | { | ||
1906 | // struct GarbageContext *gc = cls; | ||
1907 | // struct ElementEntry *ee = value; | ||
1908 | |||
1909 | // if (GNUNET_YES != ee->removed) | ||
1910 | // return GNUNET_OK; | ||
1911 | // if ( (gc->max_op_generation < ee->generation_added) || | ||
1912 | // (ee->generation_removed > gc->min_op_generation) ) | ||
1913 | // { | ||
1914 | // GNUNET_assert (GNUNET_YES == | ||
1915 | // GNUNET_CONTAINER_multihashmap_remove (gc->map, | ||
1916 | // key, | ||
1917 | // ee)); | ||
1918 | // GNUNET_free (ee); | ||
1919 | // } | ||
1920 | return GNUNET_OK; | ||
1921 | } | ||
1922 | |||
1923 | |||
1924 | /** | ||
1925 | * Collect and destroy elements that are not needed anymore, because | ||
1926 | * their lifetime (as determined by their generation) does not overlap | ||
1927 | * with any active set operation. | ||
1928 | * | ||
1929 | * @param set set to garbage collect | ||
1930 | */ | ||
1931 | static void | ||
1932 | collect_generation_garbage (struct Set *set) | ||
1933 | { | ||
1934 | struct GarbageContext gc; | ||
1935 | |||
1936 | gc.min_op_generation = UINT_MAX; | ||
1937 | gc.max_op_generation = 0; | ||
1938 | for (struct Operation *op = set->ops_head; NULL != op; op = op->next) | ||
1939 | { | ||
1940 | gc.min_op_generation = | ||
1941 | GNUNET_MIN (gc.min_op_generation, op->generation_created); | ||
1942 | gc.max_op_generation = | ||
1943 | GNUNET_MAX (gc.max_op_generation, op->generation_created); | ||
1944 | } | ||
1945 | gc.map = set->content->elements; | ||
1946 | GNUNET_CONTAINER_multihashmap_iterate (set->content->elements, | ||
1947 | &garbage_collect_cb, | ||
1948 | &gc); | ||
1949 | } | ||
1950 | |||
1951 | |||
1952 | /** | ||
1953 | * Is @a generation in the range of exclusions? | ||
1954 | * | ||
1955 | * @param generation generation to query | ||
1956 | * @param excluded array of generations where the element is excluded | ||
1957 | * @param excluded_size length of the @a excluded array | ||
1958 | * @return #GNUNET_YES if @a generation is in any of the ranges | ||
1959 | */ | ||
1960 | static int | ||
1961 | is_excluded_generation (unsigned int generation, | ||
1962 | struct GenerationRange *excluded, | ||
1963 | unsigned int excluded_size) | ||
1964 | { | ||
1965 | for (unsigned int i = 0; i < excluded_size; i++) | ||
1966 | if ((generation >= excluded[i].start) && (generation < excluded[i].end)) | ||
1967 | return GNUNET_YES; | ||
1968 | return GNUNET_NO; | ||
1969 | } | ||
1970 | |||
1971 | |||
1972 | /** | ||
1973 | * Is element @a ee part of the set during @a query_generation? | ||
1974 | * | ||
1975 | * @param ee element to test | ||
1976 | * @param query_generation generation to query | ||
1977 | * @param excluded array of generations where the element is excluded | ||
1978 | * @param excluded_size length of the @a excluded array | ||
1979 | * @return #GNUNET_YES if the element is in the set, #GNUNET_NO if not | ||
1980 | */ | ||
1981 | static int | ||
1982 | is_element_of_generation (struct ElementEntry *ee, | ||
1983 | unsigned int query_generation, | ||
1984 | struct GenerationRange *excluded, | ||
1985 | unsigned int excluded_size) | ||
1986 | { | ||
1987 | struct MutationEvent *mut; | ||
1988 | int is_present; | ||
1989 | |||
1990 | GNUNET_assert (NULL != ee->mutations); | ||
1991 | if (GNUNET_YES == | ||
1992 | is_excluded_generation (query_generation, excluded, excluded_size)) | ||
1993 | { | ||
1994 | GNUNET_break (0); | ||
1995 | return GNUNET_NO; | ||
1996 | } | ||
1997 | |||
1998 | is_present = GNUNET_NO; | ||
1999 | |||
2000 | /* Could be made faster with binary search, but lists | ||
2001 | are small, so why bother. */ | ||
2002 | for (unsigned int i = 0; i < ee->mutations_size; i++) | ||
2003 | { | ||
2004 | mut = &ee->mutations[i]; | ||
2005 | |||
2006 | if (mut->generation > query_generation) | ||
2007 | { | ||
2008 | /* The mutation doesn't apply to our generation | ||
2009 | anymore. We can'b break here, since mutations aren't | ||
2010 | sorted by generation. */ | ||
2011 | continue; | ||
2012 | } | ||
2013 | |||
2014 | if (GNUNET_YES == | ||
2015 | is_excluded_generation (mut->generation, excluded, excluded_size)) | ||
2016 | { | ||
2017 | /* The generation is excluded (because it belongs to another | ||
2018 | fork via a lazy copy) and thus mutations aren't considered | ||
2019 | for membership testing. */ | ||
2020 | continue; | ||
2021 | } | ||
2022 | |||
2023 | /* This would be an inconsistency in how we manage mutations. */ | ||
2024 | if ((GNUNET_YES == is_present) && (GNUNET_YES == mut->added)) | ||
2025 | GNUNET_assert (0); | ||
2026 | /* Likewise. */ | ||
2027 | if ((GNUNET_NO == is_present) && (GNUNET_NO == mut->added)) | ||
2028 | GNUNET_assert (0); | ||
2029 | |||
2030 | is_present = mut->added; | ||
2031 | } | ||
2032 | |||
2033 | return is_present; | ||
2034 | } | ||
2035 | |||
2036 | |||
2037 | /** | ||
2038 | * Is element @a ee part of the set used by @a op? | ||
2039 | * | ||
2040 | * @param ee element to test | ||
2041 | * @param op operation the defines the set and its generation | ||
2042 | * @return #GNUNET_YES if the element is in the set, #GNUNET_NO if not | ||
2043 | */ | ||
2044 | int | ||
2045 | _GSS_is_element_of_operation (struct ElementEntry *ee, struct Operation *op) | ||
2046 | { | ||
2047 | return is_element_of_generation (ee, | ||
2048 | op->generation_created, | ||
2049 | op->set->excluded_generations, | ||
2050 | op->set->excluded_generations_size); | ||
2051 | } | ||
2052 | |||
2053 | |||
2054 | /** | ||
2055 | * Destroy the given operation. Used for any operation where both | ||
2056 | * peers were known and that thus actually had a vt and channel. Must | ||
2057 | * not be used for operations where 'listener' is still set and we do | ||
2058 | * not know the other peer. | ||
2059 | * | ||
2060 | * Call the implementation-specific cancel function of the operation. | ||
2061 | * Disconnects from the remote peer. Does not disconnect the client, | ||
2062 | * as there may be multiple operations per set. | ||
2063 | * | ||
2064 | * @param op operation to destroy | ||
2065 | * @param gc #GNUNET_YES to perform garbage collection on the set | ||
2066 | */ | ||
2067 | void | ||
2068 | _GSS_operation_destroy (struct Operation *op, int gc) | ||
2069 | { | ||
2070 | struct Set *set = op->set; | ||
2071 | struct GNUNET_CADET_Channel *channel; | ||
2072 | |||
2073 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Destroying operation %p\n", op); | ||
2074 | GNUNET_assert (NULL == op->listener); | ||
2075 | if (NULL != op->state) | ||
2076 | { | ||
2077 | intersection_cancel (op); // FIXME: inline | ||
2078 | op->state = NULL; | ||
2079 | } | ||
2080 | if (NULL != set) | ||
2081 | { | ||
2082 | GNUNET_CONTAINER_DLL_remove (set->ops_head, set->ops_tail, op); | ||
2083 | op->set = NULL; | ||
2084 | } | ||
2085 | if (NULL != op->context_msg) | ||
2086 | { | ||
2087 | GNUNET_free (op->context_msg); | ||
2088 | op->context_msg = NULL; | ||
2089 | } | ||
2090 | if (NULL != (channel = op->channel)) | ||
2091 | { | ||
2092 | /* This will free op; called conditionally as this helper function | ||
2093 | is also called from within the channel disconnect handler. */ | ||
2094 | op->channel = NULL; | ||
2095 | GNUNET_CADET_channel_destroy (channel); | ||
2096 | } | ||
2097 | if ((NULL != set) && (GNUNET_YES == gc)) | ||
2098 | collect_generation_garbage (set); | ||
2099 | /* We rely on the channel end handler to free 'op'. When 'op->channel' was NULL, | ||
2100 | * there was a channel end handler that will free 'op' on the call stack. */ | ||
2101 | } | ||
2102 | |||
2103 | |||
2104 | /** | ||
2105 | * Callback called when a client connects to the service. | ||
2106 | * | ||
2107 | * @param cls closure for the service | ||
2108 | * @param c the new client that connected to the service | ||
2109 | * @param mq the message queue used to send messages to the client | ||
2110 | * @return @a `struct ClientState` | ||
2111 | */ | ||
2112 | static void * | ||
2113 | client_connect_cb (void *cls, | ||
2114 | struct GNUNET_SERVICE_Client *c, | ||
2115 | struct GNUNET_MQ_Handle *mq) | ||
2116 | { | ||
2117 | struct ClientState *cs; | ||
2118 | |||
2119 | num_clients++; | ||
2120 | cs = GNUNET_new (struct ClientState); | ||
2121 | cs->client = c; | ||
2122 | cs->mq = mq; | ||
2123 | return cs; | ||
2124 | } | ||
2125 | |||
2126 | |||
2127 | /** | ||
2128 | * Iterator over hash map entries to free element entries. | ||
2129 | * | ||
2130 | * @param cls closure | ||
2131 | * @param key current key code | ||
2132 | * @param value a `struct ElementEntry *` to be free'd | ||
2133 | * @return #GNUNET_YES (continue to iterate) | ||
2134 | */ | ||
2135 | static int | ||
2136 | destroy_elements_iterator (void *cls, | ||
2137 | const struct GNUNET_HashCode *key, | ||
2138 | void *value) | ||
2139 | { | ||
2140 | struct ElementEntry *ee = value; | ||
2141 | |||
2142 | GNUNET_free (ee->mutations); | ||
2143 | GNUNET_free (ee); | ||
2144 | return GNUNET_YES; | ||
2145 | } | ||
2146 | |||
2147 | |||
2148 | /** | ||
2149 | * Clean up after a client has disconnected | ||
2150 | * | ||
2151 | * @param cls closure, unused | ||
2152 | * @param client the client to clean up after | ||
2153 | * @param internal_cls the `struct ClientState` | ||
2154 | */ | ||
2155 | static void | ||
2156 | client_disconnect_cb (void *cls, | ||
2157 | struct GNUNET_SERVICE_Client *client, | ||
2158 | void *internal_cls) | ||
2159 | { | ||
2160 | struct ClientState *cs = internal_cls; | ||
2161 | struct Operation *op; | ||
2162 | struct Listener *listener; | ||
2163 | struct Set *set; | ||
2164 | |||
2165 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected, cleaning up\n"); | ||
2166 | if (NULL != (set = cs->set)) | ||
2167 | { | ||
2168 | struct SetContent *content = set->content; | ||
2169 | struct PendingMutation *pm; | ||
2170 | struct PendingMutation *pm_current; | ||
2171 | |||
2172 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Destroying client's set\n"); | ||
2173 | /* Destroy pending set operations */ | ||
2174 | while (NULL != set->ops_head) | ||
2175 | _GSS_operation_destroy (set->ops_head, GNUNET_NO); | ||
2176 | |||
2177 | /* Destroy operation-specific state */ | ||
2178 | GNUNET_assert (NULL != set->state); | ||
2179 | intersection_set_destroy (set->state); // FIXME: inline | ||
2180 | set->state = NULL; | ||
2181 | |||
2182 | /* Clean up ongoing iterations */ | ||
2183 | if (NULL != set->iter) | ||
2184 | { | ||
2185 | GNUNET_CONTAINER_multihashmap_iterator_destroy (set->iter); | ||
2186 | set->iter = NULL; | ||
2187 | set->iteration_id++; | ||
2188 | } | ||
2189 | |||
2190 | /* discard any pending mutations that reference this set */ | ||
2191 | pm = content->pending_mutations_head; | ||
2192 | while (NULL != pm) | ||
2193 | { | ||
2194 | pm_current = pm; | ||
2195 | pm = pm->next; | ||
2196 | if (pm_current->set == set) | ||
2197 | { | ||
2198 | GNUNET_CONTAINER_DLL_remove (content->pending_mutations_head, | ||
2199 | content->pending_mutations_tail, | ||
2200 | pm_current); | ||
2201 | GNUNET_free (pm_current); | ||
2202 | } | ||
2203 | } | ||
2204 | |||
2205 | /* free set content (or at least decrement RC) */ | ||
2206 | set->content = NULL; | ||
2207 | GNUNET_assert (0 != content->refcount); | ||
2208 | content->refcount--; | ||
2209 | if (0 == content->refcount) | ||
2210 | { | ||
2211 | GNUNET_assert (NULL != content->elements); | ||
2212 | GNUNET_CONTAINER_multihashmap_iterate (content->elements, | ||
2213 | &destroy_elements_iterator, | ||
2214 | NULL); | ||
2215 | GNUNET_CONTAINER_multihashmap_destroy (content->elements); | ||
2216 | content->elements = NULL; | ||
2217 | GNUNET_free (content); | ||
2218 | } | ||
2219 | GNUNET_free (set->excluded_generations); | ||
2220 | set->excluded_generations = NULL; | ||
2221 | |||
2222 | GNUNET_free (set); | ||
2223 | } | ||
2224 | |||
2225 | if (NULL != (listener = cs->listener)) | ||
2226 | { | ||
2227 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Destroying client's listener\n"); | ||
2228 | GNUNET_CADET_close_port (listener->open_port); | ||
2229 | listener->open_port = NULL; | ||
2230 | while (NULL != (op = listener->op_head)) | ||
2231 | { | ||
2232 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
2233 | "Destroying incoming operation `%u' from peer `%s'\n", | ||
2234 | (unsigned int) op->client_request_id, | ||
2235 | GNUNET_i2s (&op->peer)); | ||
2236 | incoming_destroy (op); | ||
2237 | } | ||
2238 | GNUNET_CONTAINER_DLL_remove (listener_head, listener_tail, listener); | ||
2239 | GNUNET_free (listener); | ||
2240 | } | ||
2241 | GNUNET_free (cs); | ||
2242 | num_clients--; | ||
2243 | if ((GNUNET_YES == in_shutdown) && (0 == num_clients)) | ||
2244 | { | ||
2245 | if (NULL != cadet) | ||
2246 | { | ||
2247 | GNUNET_CADET_disconnect (cadet); | ||
2248 | cadet = NULL; | ||
2249 | } | ||
2250 | } | ||
2251 | } | ||
2252 | |||
2253 | |||
2254 | /** | ||
2255 | * Check a request for a set operation from another peer. | ||
2256 | * | ||
2257 | * @param cls the operation state | ||
2258 | * @param msg the received message | ||
2259 | * @return #GNUNET_OK if the channel should be kept alive, | ||
2260 | * #GNUNET_SYSERR to destroy the channel | ||
2261 | */ | ||
2262 | static int | ||
2263 | check_incoming_msg (void *cls, const struct OperationRequestMessage *msg) | ||
2264 | { | ||
2265 | struct Operation *op = cls; | ||
2266 | struct Listener *listener = op->listener; | ||
2267 | const struct GNUNET_MessageHeader *nested_context; | ||
2268 | |||
2269 | /* double operation request */ | ||
2270 | if (0 != op->suggest_id) | ||
2271 | { | ||
2272 | GNUNET_break_op (0); | ||
2273 | return GNUNET_SYSERR; | ||
2274 | } | ||
2275 | /* This should be equivalent to the previous condition, but can't hurt to check twice */ | ||
2276 | if (NULL == op->listener) | ||
2277 | { | ||
2278 | GNUNET_break (0); | ||
2279 | return GNUNET_SYSERR; | ||
2280 | } | ||
2281 | if (listener->operation != | ||
2282 | (enum GNUNET_SET_OperationType) ntohl (msg->operation)) | ||
2283 | { | ||
2284 | GNUNET_break_op (0); | ||
2285 | return GNUNET_SYSERR; | ||
2286 | } | ||
2287 | nested_context = GNUNET_MQ_extract_nested_mh (msg); | ||
2288 | if ((NULL != nested_context) && | ||
2289 | (ntohs (nested_context->size) > GNUNET_SET_CONTEXT_MESSAGE_MAX_SIZE)) | ||
2290 | { | ||
2291 | GNUNET_break_op (0); | ||
2292 | return GNUNET_SYSERR; | ||
2293 | } | ||
2294 | return GNUNET_OK; | ||
2295 | } | ||
2296 | |||
2297 | |||
2298 | /** | ||
2299 | * Handle a request for a set operation from another peer. Checks if we | ||
2300 | * have a listener waiting for such a request (and in that case initiates | ||
2301 | * asking the listener about accepting the connection). If no listener | ||
2302 | * is waiting, we queue the operation request in hope that a listener | ||
2303 | * shows up soon (before timeout). | ||
2304 | * | ||
2305 | * This msg is expected as the first and only msg handled through the | ||
2306 | * non-operation bound virtual table, acceptance of this operation replaces | ||
2307 | * our virtual table and subsequent msgs would be routed differently (as | ||
2308 | * we then know what type of operation this is). | ||
2309 | * | ||
2310 | * @param cls the operation state | ||
2311 | * @param msg the received message | ||
2312 | * @return #GNUNET_OK if the channel should be kept alive, | ||
2313 | * #GNUNET_SYSERR to destroy the channel | ||
2314 | */ | ||
2315 | static void | ||
2316 | handle_incoming_msg (void *cls, const struct OperationRequestMessage *msg) | ||
2317 | { | ||
2318 | struct Operation *op = cls; | ||
2319 | struct Listener *listener = op->listener; | ||
2320 | const struct GNUNET_MessageHeader *nested_context; | ||
2321 | struct GNUNET_MQ_Envelope *env; | ||
2322 | struct GNUNET_SET_RequestMessage *cmsg; | ||
2323 | |||
2324 | nested_context = GNUNET_MQ_extract_nested_mh (msg); | ||
2325 | /* Make a copy of the nested_context (application-specific context | ||
2326 | information that is opaque to set) so we can pass it to the | ||
2327 | listener later on */ | ||
2328 | if (NULL != nested_context) | ||
2329 | op->context_msg = GNUNET_copy_message (nested_context); | ||
2330 | op->remote_element_count = ntohl (msg->element_count); | ||
2331 | GNUNET_log ( | ||
2332 | GNUNET_ERROR_TYPE_DEBUG, | ||
2333 | "Received P2P operation request (op %u, port %s) for active listener\n", | ||
2334 | (uint32_t) ntohl (msg->operation), | ||
2335 | GNUNET_h2s (&op->listener->app_id)); | ||
2336 | GNUNET_assert (0 == op->suggest_id); | ||
2337 | if (0 == suggest_id) | ||
2338 | suggest_id++; | ||
2339 | op->suggest_id = suggest_id++; | ||
2340 | GNUNET_assert (NULL != op->timeout_task); | ||
2341 | GNUNET_SCHEDULER_cancel (op->timeout_task); | ||
2342 | op->timeout_task = NULL; | ||
2343 | env = GNUNET_MQ_msg_nested_mh (cmsg, | ||
2344 | GNUNET_MESSAGE_TYPE_SETI_REQUEST, | ||
2345 | op->context_msg); | ||
2346 | GNUNET_log ( | ||
2347 | GNUNET_ERROR_TYPE_DEBUG, | ||
2348 | "Suggesting incoming request with accept id %u to listener %p of client %p\n", | ||
2349 | op->suggest_id, | ||
2350 | listener, | ||
2351 | listener->cs); | ||
2352 | cmsg->accept_id = htonl (op->suggest_id); | ||
2353 | cmsg->peer_id = op->peer; | ||
2354 | GNUNET_MQ_send (listener->cs->mq, env); | ||
2355 | /* NOTE: GNUNET_CADET_receive_done() will be called in | ||
2356 | #handle_client_accept() */ | ||
2357 | } | ||
2358 | |||
2359 | |||
2360 | /** | ||
2361 | * Add an element to @a set as specified by @a msg | ||
2362 | * | ||
2363 | * @param set set to manipulate | ||
2364 | * @param msg message specifying the change | ||
2365 | */ | ||
2366 | static void | ||
2367 | execute_add (struct Set *set, const struct GNUNET_SET_ElementMessage *msg) | ||
2368 | { | ||
2369 | struct GNUNET_SET_Element el; | ||
2370 | struct ElementEntry *ee; | ||
2371 | struct GNUNET_HashCode hash; | ||
2372 | |||
2373 | GNUNET_assert (GNUNET_MESSAGE_TYPE_SETI_ADD == ntohs (msg->header.type)); | ||
2374 | el.size = ntohs (msg->header.size) - sizeof(*msg); | ||
2375 | el.data = &msg[1]; | ||
2376 | el.element_type = ntohs (msg->element_type); | ||
2377 | GNUNET_SET_element_hash (&el, &hash); | ||
2378 | ee = GNUNET_CONTAINER_multihashmap_get (set->content->elements, &hash); | ||
2379 | if (NULL == ee) | ||
2380 | { | ||
2381 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2382 | "Client inserts element %s of size %u\n", | ||
2383 | GNUNET_h2s (&hash), | ||
2384 | el.size); | ||
2385 | ee = GNUNET_malloc (el.size + sizeof(*ee)); | ||
2386 | ee->element.size = el.size; | ||
2387 | GNUNET_memcpy (&ee[1], el.data, el.size); | ||
2388 | ee->element.data = &ee[1]; | ||
2389 | ee->element.element_type = el.element_type; | ||
2390 | ee->remote = GNUNET_NO; | ||
2391 | ee->mutations = NULL; | ||
2392 | ee->mutations_size = 0; | ||
2393 | ee->element_hash = hash; | ||
2394 | GNUNET_break (GNUNET_YES == | ||
2395 | GNUNET_CONTAINER_multihashmap_put ( | ||
2396 | set->content->elements, | ||
2397 | &ee->element_hash, | ||
2398 | ee, | ||
2399 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); | ||
2400 | } | ||
2401 | else if (GNUNET_YES == | ||
2402 | is_element_of_generation (ee, | ||
2403 | set->current_generation, | ||
2404 | set->excluded_generations, | ||
2405 | set->excluded_generations_size)) | ||
2406 | { | ||
2407 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2408 | "Client inserted element %s of size %u twice (ignored)\n", | ||
2409 | GNUNET_h2s (&hash), | ||
2410 | el.size); | ||
2411 | |||
2412 | /* same element inserted twice */ | ||
2413 | return; | ||
2414 | } | ||
2415 | |||
2416 | { | ||
2417 | struct MutationEvent mut = { .generation = set->current_generation, | ||
2418 | .added = GNUNET_YES }; | ||
2419 | GNUNET_array_append (ee->mutations, ee->mutations_size, mut); | ||
2420 | } | ||
2421 | // FIXME: inline | ||
2422 | intersection_add (set->state, | ||
2423 | ee); | ||
2424 | } | ||
2425 | |||
2426 | |||
2427 | /** | ||
2428 | * Perform a mutation on a set as specified by the @a msg | ||
2429 | * | ||
2430 | * @param set the set to mutate | ||
2431 | * @param msg specification of what to change | ||
2432 | */ | ||
2433 | static void | ||
2434 | execute_mutation (struct Set *set, const struct GNUNET_SET_ElementMessage *msg) | ||
2435 | { | ||
2436 | switch (ntohs (msg->header.type)) | ||
2437 | { | ||
2438 | case GNUNET_MESSAGE_TYPE_SETI_ADD: // FIXME: inline! | ||
2439 | execute_add (set, msg); | ||
2440 | break; | ||
2441 | default: | ||
2442 | GNUNET_break (0); | ||
2443 | } | ||
2444 | } | ||
2445 | |||
2446 | |||
2447 | /** | ||
2448 | * Execute mutations that were delayed on a set because of | ||
2449 | * pending operations. | ||
2450 | * | ||
2451 | * @param set the set to execute mutations on | ||
2452 | */ | ||
2453 | static void | ||
2454 | execute_delayed_mutations (struct Set *set) | ||
2455 | { | ||
2456 | struct PendingMutation *pm; | ||
2457 | |||
2458 | if (0 != set->content->iterator_count) | ||
2459 | return; /* still cannot do this */ | ||
2460 | while (NULL != (pm = set->content->pending_mutations_head)) | ||
2461 | { | ||
2462 | GNUNET_CONTAINER_DLL_remove (set->content->pending_mutations_head, | ||
2463 | set->content->pending_mutations_tail, | ||
2464 | pm); | ||
2465 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2466 | "Executing pending mutation on %p.\n", | ||
2467 | pm->set); | ||
2468 | execute_mutation (pm->set, pm->msg); | ||
2469 | GNUNET_free (pm->msg); | ||
2470 | GNUNET_free (pm); | ||
2471 | } | ||
2472 | } | ||
2473 | |||
2474 | |||
2475 | /** | ||
2476 | * Send the next element of a set to the set's client. The next element is given by | ||
2477 | * the set's current hashmap iterator. The set's iterator will be set to NULL if there | ||
2478 | * are no more elements in the set. The caller must ensure that the set's iterator is | ||
2479 | * valid. | ||
2480 | * | ||
2481 | * The client will acknowledge each received element with a | ||
2482 | * #GNUNET_MESSAGE_TYPE_SETI_ITER_ACK message. Our | ||
2483 | * #handle_client_iter_ack() will then trigger the next transmission. | ||
2484 | * Note that the #GNUNET_MESSAGE_TYPE_SETI_ITER_DONE is not acknowledged. | ||
2485 | * | ||
2486 | * @param set set that should send its next element to its client | ||
2487 | */ | ||
2488 | static void | ||
2489 | send_client_element (struct Set *set) | ||
2490 | { | ||
2491 | int ret; | ||
2492 | struct ElementEntry *ee; | ||
2493 | struct GNUNET_MQ_Envelope *ev; | ||
2494 | struct GNUNET_SET_IterResponseMessage *msg; | ||
2495 | |||
2496 | GNUNET_assert (NULL != set->iter); | ||
2497 | do | ||
2498 | { | ||
2499 | ret = GNUNET_CONTAINER_multihashmap_iterator_next (set->iter, | ||
2500 | NULL, | ||
2501 | (const void **) &ee); | ||
2502 | if (GNUNET_NO == ret) | ||
2503 | { | ||
2504 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Iteration on %p done.\n", set); | ||
2505 | ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SETI_ITER_DONE); | ||
2506 | GNUNET_CONTAINER_multihashmap_iterator_destroy (set->iter); | ||
2507 | set->iter = NULL; | ||
2508 | set->iteration_id++; | ||
2509 | GNUNET_assert (set->content->iterator_count > 0); | ||
2510 | set->content->iterator_count--; | ||
2511 | execute_delayed_mutations (set); | ||
2512 | GNUNET_MQ_send (set->cs->mq, ev); | ||
2513 | return; | ||
2514 | } | ||
2515 | GNUNET_assert (NULL != ee); | ||
2516 | } | ||
2517 | while (GNUNET_NO == | ||
2518 | is_element_of_generation (ee, | ||
2519 | set->iter_generation, | ||
2520 | set->excluded_generations, | ||
2521 | set->excluded_generations_size)); | ||
2522 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2523 | "Sending iteration element on %p.\n", | ||
2524 | set); | ||
2525 | ev = GNUNET_MQ_msg_extra (msg, | ||
2526 | ee->element.size, | ||
2527 | GNUNET_MESSAGE_TYPE_SETI_ITER_ELEMENT); | ||
2528 | GNUNET_memcpy (&msg[1], ee->element.data, ee->element.size); | ||
2529 | msg->element_type = htons (ee->element.element_type); | ||
2530 | msg->iteration_id = htons (set->iteration_id); | ||
2531 | GNUNET_MQ_send (set->cs->mq, ev); | ||
2532 | } | ||
2533 | |||
2534 | |||
2535 | /** | ||
2536 | * Called when a client wants to iterate the elements of a set. | ||
2537 | * Checks if we have a set associated with the client and if we | ||
2538 | * can right now start an iteration. If all checks out, starts | ||
2539 | * sending the elements of the set to the client. | ||
2540 | * | ||
2541 | * @param cls client that sent the message | ||
2542 | * @param m message sent by the client | ||
2543 | */ | ||
2544 | static void | ||
2545 | handle_client_iterate (void *cls, const struct GNUNET_MessageHeader *m) | ||
2546 | { | ||
2547 | struct ClientState *cs = cls; | ||
2548 | struct Set *set; | ||
2549 | |||
2550 | if (NULL == (set = cs->set)) | ||
2551 | { | ||
2552 | /* attempt to iterate over a non existing set */ | ||
2553 | GNUNET_break (0); | ||
2554 | GNUNET_SERVICE_client_drop (cs->client); | ||
2555 | return; | ||
2556 | } | ||
2557 | if (NULL != set->iter) | ||
2558 | { | ||
2559 | /* Only one concurrent iterate-action allowed per set */ | ||
2560 | GNUNET_break (0); | ||
2561 | GNUNET_SERVICE_client_drop (cs->client); | ||
2562 | return; | ||
2563 | } | ||
2564 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2565 | "Iterating set %p in gen %u with %u content elements\n", | ||
2566 | (void *) set, | ||
2567 | set->current_generation, | ||
2568 | GNUNET_CONTAINER_multihashmap_size (set->content->elements)); | ||
2569 | GNUNET_SERVICE_client_continue (cs->client); | ||
2570 | set->content->iterator_count++; | ||
2571 | set->iter = | ||
2572 | GNUNET_CONTAINER_multihashmap_iterator_create (set->content->elements); | ||
2573 | set->iter_generation = set->current_generation; | ||
2574 | send_client_element (set); | ||
2575 | } | ||
2576 | |||
2577 | |||
2578 | /** | ||
2579 | * Called when a client wants to create a new set. This is typically | ||
2580 | * the first request from a client, and includes the type of set | ||
2581 | * operation to be performed. | ||
2582 | * | ||
2583 | * @param cls client that sent the message | ||
2584 | * @param m message sent by the client | ||
2585 | */ | ||
2586 | static void | ||
2587 | handle_client_create_set (void *cls, const struct GNUNET_SET_CreateMessage *msg) | ||
2588 | { | ||
2589 | struct ClientState *cs = cls; | ||
2590 | struct Set *set; | ||
2591 | |||
2592 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2593 | "Client created new set (operation %u)\n", | ||
2594 | (uint32_t) ntohl (msg->operation)); | ||
2595 | if (NULL != cs->set) | ||
2596 | { | ||
2597 | /* There can only be one set per client */ | ||
2598 | GNUNET_break (0); | ||
2599 | GNUNET_SERVICE_client_drop (cs->client); | ||
2600 | return; | ||
2601 | } | ||
2602 | set = GNUNET_new (struct Set); | ||
2603 | switch (ntohl (msg->operation)) | ||
2604 | { | ||
2605 | case GNUNET_SET_OPERATION_INTERSECTION: | ||
2606 | set->vt = _GSS_intersection_vt (); | ||
2607 | break; | ||
2608 | |||
2609 | case GNUNET_SET_OPERATION_UNION: | ||
2610 | set->vt = _GSS_union_vt (); | ||
2611 | break; | ||
2612 | |||
2613 | default: | ||
2614 | GNUNET_free (set); | ||
2615 | GNUNET_break (0); | ||
2616 | GNUNET_SERVICE_client_drop (cs->client); | ||
2617 | return; | ||
2618 | } | ||
2619 | set->operation = (enum GNUNET_SET_OperationType) ntohl (msg->operation); | ||
2620 | set->state = intersection_set_create (); // FIXME: inline | ||
2621 | if (NULL == set->state) | ||
2622 | { | ||
2623 | /* initialization failed (i.e. out of memory) */ | ||
2624 | GNUNET_free (set); | ||
2625 | GNUNET_SERVICE_client_drop (cs->client); | ||
2626 | return; | ||
2627 | } | ||
2628 | set->content = GNUNET_new (struct SetContent); | ||
2629 | set->content->refcount = 1; | ||
2630 | set->content->elements = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES); | ||
2631 | set->cs = cs; | ||
2632 | cs->set = set; | ||
2633 | GNUNET_SERVICE_client_continue (cs->client); | ||
2634 | } | ||
2635 | |||
2636 | |||
2637 | /** | ||
2638 | * Timeout happens iff: | ||
2639 | * - we suggested an operation to our listener, | ||
2640 | * but did not receive a response in time | ||
2641 | * - we got the channel from a peer but no #GNUNET_MESSAGE_TYPE_SETI_P2P_OPERATION_REQUEST | ||
2642 | * | ||
2643 | * @param cls channel context | ||
2644 | * @param tc context information (why was this task triggered now) | ||
2645 | */ | ||
2646 | static void | ||
2647 | incoming_timeout_cb (void *cls) | ||
2648 | { | ||
2649 | struct Operation *op = cls; | ||
2650 | |||
2651 | op->timeout_task = NULL; | ||
2652 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2653 | "Remote peer's incoming request timed out\n"); | ||
2654 | incoming_destroy (op); | ||
2655 | } | ||
2656 | |||
2657 | |||
2658 | /** | ||
2659 | * Method called whenever another peer has added us to a channel the | ||
2660 | * other peer initiated. Only called (once) upon reception of data | ||
2661 | * from a channel we listen on. | ||
2662 | * | ||
2663 | * The channel context represents the operation itself and gets added | ||
2664 | * to a DLL, from where it gets looked up when our local listener | ||
2665 | * client responds to a proposed/suggested operation or connects and | ||
2666 | * associates with this operation. | ||
2667 | * | ||
2668 | * @param cls closure | ||
2669 | * @param channel new handle to the channel | ||
2670 | * @param source peer that started the channel | ||
2671 | * @return initial channel context for the channel | ||
2672 | * returns NULL on error | ||
2673 | */ | ||
2674 | static void * | ||
2675 | channel_new_cb (void *cls, | ||
2676 | struct GNUNET_CADET_Channel *channel, | ||
2677 | const struct GNUNET_PeerIdentity *source) | ||
2678 | { | ||
2679 | struct Listener *listener = cls; | ||
2680 | struct Operation *op; | ||
2681 | |||
2682 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New incoming channel\n"); | ||
2683 | op = GNUNET_new (struct Operation); | ||
2684 | op->listener = listener; | ||
2685 | op->peer = *source; | ||
2686 | op->channel = channel; | ||
2687 | op->mq = GNUNET_CADET_get_mq (op->channel); | ||
2688 | op->salt = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX); | ||
2689 | op->timeout_task = GNUNET_SCHEDULER_add_delayed (INCOMING_CHANNEL_TIMEOUT, | ||
2690 | &incoming_timeout_cb, | ||
2691 | op); | ||
2692 | GNUNET_CONTAINER_DLL_insert (listener->op_head, listener->op_tail, op); | ||
2693 | return op; | ||
2694 | } | ||
2695 | |||
2696 | |||
2697 | /** | ||
2698 | * Function called whenever a channel is destroyed. Should clean up | ||
2699 | * any associated state. It must NOT call | ||
2700 | * GNUNET_CADET_channel_destroy() on the channel. | ||
2701 | * | ||
2702 | * The peer_disconnect function is part of a a virtual table set initially either | ||
2703 | * when a peer creates a new channel with us, or once we create | ||
2704 | * a new channel ourselves (evaluate). | ||
2705 | * | ||
2706 | * Once we know the exact type of operation (union/intersection), the vt is | ||
2707 | * replaced with an operation specific instance (_GSS_[op]_vt). | ||
2708 | * | ||
2709 | * @param channel_ctx place where local state associated | ||
2710 | * with the channel is stored | ||
2711 | * @param channel connection to the other end (henceforth invalid) | ||
2712 | */ | ||
2713 | static void | ||
2714 | channel_end_cb (void *channel_ctx, const struct GNUNET_CADET_Channel *channel) | ||
2715 | { | ||
2716 | struct Operation *op = channel_ctx; | ||
2717 | |||
2718 | op->channel = NULL; | ||
2719 | _GSS_operation_destroy2 (op); | ||
2720 | } | ||
2721 | |||
2722 | |||
2723 | /** | ||
2724 | * This function probably should not exist | ||
2725 | * and be replaced by inlining more specific | ||
2726 | * logic in the various places where it is called. | ||
2727 | */ | ||
2728 | void | ||
2729 | _GSS_operation_destroy2 (struct Operation *op) | ||
2730 | { | ||
2731 | struct GNUNET_CADET_Channel *channel; | ||
2732 | |||
2733 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "channel_end_cb called\n"); | ||
2734 | if (NULL != (channel = op->channel)) | ||
2735 | { | ||
2736 | /* This will free op; called conditionally as this helper function | ||
2737 | is also called from within the channel disconnect handler. */ | ||
2738 | op->channel = NULL; | ||
2739 | GNUNET_CADET_channel_destroy (channel); | ||
2740 | } | ||
2741 | if (NULL != op->listener) | ||
2742 | { | ||
2743 | incoming_destroy (op); | ||
2744 | return; | ||
2745 | } | ||
2746 | if (NULL != op->set) | ||
2747 | intersection_channel_death (op); // FIXME: inline | ||
2748 | else | ||
2749 | _GSS_operation_destroy (op, GNUNET_YES); | ||
2750 | GNUNET_free (op); | ||
2751 | } | ||
2752 | |||
2753 | |||
2754 | /** | ||
2755 | * Function called whenever an MQ-channel's transmission window size changes. | ||
2756 | * | ||
2757 | * The first callback in an outgoing channel will be with a non-zero value | ||
2758 | * and will mean the channel is connected to the destination. | ||
2759 | * | ||
2760 | * For an incoming channel it will be called immediately after the | ||
2761 | * #GNUNET_CADET_ConnectEventHandler, also with a non-zero value. | ||
2762 | * | ||
2763 | * @param cls Channel closure. | ||
2764 | * @param channel Connection to the other end (henceforth invalid). | ||
2765 | * @param window_size New window size. If the is more messages than buffer size | ||
2766 | * this value will be negative.. | ||
2767 | */ | ||
2768 | static void | ||
2769 | channel_window_cb (void *cls, | ||
2770 | const struct GNUNET_CADET_Channel *channel, | ||
2771 | int window_size) | ||
2772 | { | ||
2773 | /* FIXME: not implemented, we could do flow control here... */ | ||
2774 | } | ||
2775 | |||
2776 | |||
2777 | /** | ||
2778 | * Called when a client wants to create a new listener. | ||
2779 | * | ||
2780 | * @param cls client that sent the message | ||
2781 | * @param msg message sent by the client | ||
2782 | */ | ||
2783 | static void | ||
2784 | handle_client_listen (void *cls, const struct GNUNET_SET_ListenMessage *msg) | ||
2785 | { | ||
2786 | struct ClientState *cs = cls; | ||
2787 | struct GNUNET_MQ_MessageHandler cadet_handlers[] = | ||
2788 | { GNUNET_MQ_hd_var_size (incoming_msg, | ||
2789 | GNUNET_MESSAGE_TYPE_SETI_P2P_OPERATION_REQUEST, | ||
2790 | struct OperationRequestMessage, | ||
2791 | NULL), | ||
2792 | GNUNET_MQ_hd_fixed_size (intersection_p2p_element_info, | ||
2793 | GNUNET_MESSAGE_TYPE_SETI_P2P_ELEMENT_INFO, | ||
2794 | struct IntersectionElementInfoMessage, | ||
2795 | NULL), | ||
2796 | GNUNET_MQ_hd_var_size (intersection_p2p_bf, | ||
2797 | GNUNET_MESSAGE_TYPE_SETI_P2P_BF, | ||
2798 | struct BFMessage, | ||
2799 | NULL), | ||
2800 | GNUNET_MQ_hd_fixed_size (intersection_p2p_done, | ||
2801 | GNUNET_MESSAGE_TYPE_SETI_P2P_DONE, | ||
2802 | struct IntersectionDoneMessage, | ||
2803 | NULL), | ||
2804 | GNUNET_MQ_handler_end () }; | ||
2805 | struct Listener *listener; | ||
2806 | |||
2807 | if (NULL != cs->listener) | ||
2808 | { | ||
2809 | /* max. one active listener per client! */ | ||
2810 | GNUNET_break (0); | ||
2811 | GNUNET_SERVICE_client_drop (cs->client); | ||
2812 | return; | ||
2813 | } | ||
2814 | listener = GNUNET_new (struct Listener); | ||
2815 | listener->cs = cs; | ||
2816 | cs->listener = listener; | ||
2817 | listener->app_id = msg->app_id; | ||
2818 | listener->operation = (enum GNUNET_SET_OperationType) ntohl (msg->operation); | ||
2819 | GNUNET_CONTAINER_DLL_insert (listener_head, listener_tail, listener); | ||
2820 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2821 | "New listener created (op %u, port %s)\n", | ||
2822 | listener->operation, | ||
2823 | GNUNET_h2s (&listener->app_id)); | ||
2824 | listener->open_port = GNUNET_CADET_open_port (cadet, | ||
2825 | &msg->app_id, | ||
2826 | &channel_new_cb, | ||
2827 | listener, | ||
2828 | &channel_window_cb, | ||
2829 | &channel_end_cb, | ||
2830 | cadet_handlers); | ||
2831 | GNUNET_SERVICE_client_continue (cs->client); | ||
2832 | } | ||
2833 | |||
2834 | |||
2835 | /** | ||
2836 | * Called when the listening client rejects an operation | ||
2837 | * request by another peer. | ||
2838 | * | ||
2839 | * @param cls client that sent the message | ||
2840 | * @param msg message sent by the client | ||
2841 | */ | ||
2842 | static void | ||
2843 | handle_client_reject (void *cls, const struct GNUNET_SET_RejectMessage *msg) | ||
2844 | { | ||
2845 | struct ClientState *cs = cls; | ||
2846 | struct Operation *op; | ||
2847 | |||
2848 | op = get_incoming (ntohl (msg->accept_reject_id)); | ||
2849 | if (NULL == op) | ||
2850 | { | ||
2851 | /* no matching incoming operation for this reject; | ||
2852 | could be that the other peer already disconnected... */ | ||
2853 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
2854 | "Client rejected unknown operation %u\n", | ||
2855 | (unsigned int) ntohl (msg->accept_reject_id)); | ||
2856 | GNUNET_SERVICE_client_continue (cs->client); | ||
2857 | return; | ||
2858 | } | ||
2859 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2860 | "Peer request (op %u, app %s) rejected by client\n", | ||
2861 | op->listener->operation, | ||
2862 | GNUNET_h2s (&cs->listener->app_id)); | ||
2863 | _GSS_operation_destroy2 (op); | ||
2864 | GNUNET_SERVICE_client_continue (cs->client); | ||
2865 | } | ||
2866 | |||
2867 | |||
2868 | /** | ||
2869 | * Called when a client wants to add or remove an element to a set it inhabits. | ||
2870 | * | ||
2871 | * @param cls client that sent the message | ||
2872 | * @param msg message sent by the client | ||
2873 | */ | ||
2874 | static int | ||
2875 | check_client_mutation (void *cls, const struct GNUNET_SET_ElementMessage *msg) | ||
2876 | { | ||
2877 | /* NOTE: Technically, we should probably check with the | ||
2878 | block library whether the element we are given is well-formed */ | ||
2879 | return GNUNET_OK; | ||
2880 | } | ||
2881 | |||
2882 | |||
2883 | /** | ||
2884 | * Called when a client wants to add or remove an element to a set it inhabits. | ||
2885 | * | ||
2886 | * @param cls client that sent the message | ||
2887 | * @param msg message sent by the client | ||
2888 | */ | ||
2889 | static void | ||
2890 | handle_client_mutation (void *cls, const struct GNUNET_SET_ElementMessage *msg) | ||
2891 | { | ||
2892 | struct ClientState *cs = cls; | ||
2893 | struct Set *set; | ||
2894 | |||
2895 | if (NULL == (set = cs->set)) | ||
2896 | { | ||
2897 | /* client without a set requested an operation */ | ||
2898 | GNUNET_break (0); | ||
2899 | GNUNET_SERVICE_client_drop (cs->client); | ||
2900 | return; | ||
2901 | } | ||
2902 | GNUNET_SERVICE_client_continue (cs->client); | ||
2903 | |||
2904 | if (0 != set->content->iterator_count) | ||
2905 | { | ||
2906 | struct PendingMutation *pm; | ||
2907 | |||
2908 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Scheduling mutation on set\n"); | ||
2909 | pm = GNUNET_new (struct PendingMutation); | ||
2910 | pm->msg = | ||
2911 | (struct GNUNET_SET_ElementMessage *) GNUNET_copy_message (&msg->header); | ||
2912 | pm->set = set; | ||
2913 | GNUNET_CONTAINER_DLL_insert_tail (set->content->pending_mutations_head, | ||
2914 | set->content->pending_mutations_tail, | ||
2915 | pm); | ||
2916 | return; | ||
2917 | } | ||
2918 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing mutation on set\n"); | ||
2919 | execute_mutation (set, msg); | ||
2920 | } | ||
2921 | |||
2922 | |||
2923 | /** | ||
2924 | * Advance the current generation of a set, | ||
2925 | * adding exclusion ranges if necessary. | ||
2926 | * | ||
2927 | * @param set the set where we want to advance the generation | ||
2928 | */ | ||
2929 | static void | ||
2930 | advance_generation (struct Set *set) | ||
2931 | { | ||
2932 | struct GenerationRange r; | ||
2933 | |||
2934 | if (set->current_generation == set->content->latest_generation) | ||
2935 | { | ||
2936 | set->content->latest_generation++; | ||
2937 | set->current_generation++; | ||
2938 | return; | ||
2939 | } | ||
2940 | |||
2941 | GNUNET_assert (set->current_generation < set->content->latest_generation); | ||
2942 | |||
2943 | r.start = set->current_generation + 1; | ||
2944 | r.end = set->content->latest_generation + 1; | ||
2945 | set->content->latest_generation = r.end; | ||
2946 | set->current_generation = r.end; | ||
2947 | GNUNET_array_append (set->excluded_generations, | ||
2948 | set->excluded_generations_size, | ||
2949 | r); | ||
2950 | } | ||
2951 | |||
2952 | |||
2953 | /** | ||
2954 | * Called when a client wants to initiate a set operation with another | ||
2955 | * peer. Initiates the CADET connection to the listener and sends the | ||
2956 | * request. | ||
2957 | * | ||
2958 | * @param cls client that sent the message | ||
2959 | * @param msg message sent by the client | ||
2960 | * @return #GNUNET_OK if the message is well-formed | ||
2961 | */ | ||
2962 | static int | ||
2963 | check_client_evaluate (void *cls, const struct GNUNET_SET_EvaluateMessage *msg) | ||
2964 | { | ||
2965 | /* FIXME: suboptimal, even if the context below could be NULL, | ||
2966 | there are malformed messages this does not check for... */ | ||
2967 | return GNUNET_OK; | ||
2968 | } | ||
2969 | |||
2970 | |||
2971 | /** | ||
2972 | * Called when a client wants to initiate a set operation with another | ||
2973 | * peer. Initiates the CADET connection to the listener and sends the | ||
2974 | * request. | ||
2975 | * | ||
2976 | * @param cls client that sent the message | ||
2977 | * @param msg message sent by the client | ||
2978 | */ | ||
2979 | static void | ||
2980 | handle_client_evaluate (void *cls, const struct GNUNET_SET_EvaluateMessage *msg) | ||
2981 | { | ||
2982 | struct ClientState *cs = cls; | ||
2983 | struct Operation *op = GNUNET_new (struct Operation); | ||
2984 | const struct GNUNET_MQ_MessageHandler cadet_handlers[] = { | ||
2985 | GNUNET_MQ_hd_var_size (incoming_msg, | ||
2986 | GNUNET_MESSAGE_TYPE_SETI_P2P_OPERATION_REQUEST, | ||
2987 | struct OperationRequestMessage, | ||
2988 | op), | ||
2989 | GNUNET_MQ_hd_fixed_size (intersection_p2p_element_info, | ||
2990 | GNUNET_MESSAGE_TYPE_SETI_P2P_ELEMENT_INFO, | ||
2991 | struct IntersectionElementInfoMessage, | ||
2992 | op), | ||
2993 | GNUNET_MQ_hd_var_size (intersection_p2p_bf, | ||
2994 | GNUNET_MESSAGE_TYPE_SETI_P2P_BF, | ||
2995 | struct BFMessage, | ||
2996 | op), | ||
2997 | GNUNET_MQ_hd_fixed_size (intersection_p2p_done, | ||
2998 | GNUNET_MESSAGE_TYPE_SETI_P2P_DONE, | ||
2999 | struct IntersectionDoneMessage, | ||
3000 | op), | ||
3001 | GNUNET_MQ_handler_end () | ||
3002 | }; | ||
3003 | struct Set *set; | ||
3004 | const struct GNUNET_MessageHeader *context; | ||
3005 | |||
3006 | if (NULL == (set = cs->set)) | ||
3007 | { | ||
3008 | GNUNET_break (0); | ||
3009 | GNUNET_free (op); | ||
3010 | GNUNET_SERVICE_client_drop (cs->client); | ||
3011 | return; | ||
3012 | } | ||
3013 | op->salt = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX); | ||
3014 | op->peer = msg->target_peer; | ||
3015 | op->result_mode = ntohl (msg->result_mode); | ||
3016 | op->client_request_id = ntohl (msg->request_id); | ||
3017 | op->byzantine = msg->byzantine; | ||
3018 | op->byzantine_lower_bound = msg->byzantine_lower_bound; | ||
3019 | op->force_full = msg->force_full; | ||
3020 | op->force_delta = msg->force_delta; | ||
3021 | context = GNUNET_MQ_extract_nested_mh (msg); | ||
3022 | |||
3023 | /* Advance generation values, so that | ||
3024 | mutations won't interfer with the running operation. */ | ||
3025 | op->set = set; | ||
3026 | op->generation_created = set->current_generation; | ||
3027 | advance_generation (set); | ||
3028 | GNUNET_CONTAINER_DLL_insert (set->ops_head, set->ops_tail, op); | ||
3029 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3030 | "Creating new CADET channel to port %s for set operation type %u\n", | ||
3031 | GNUNET_h2s (&msg->app_id), | ||
3032 | set->operation); | ||
3033 | op->channel = GNUNET_CADET_channel_create (cadet, | ||
3034 | op, | ||
3035 | &msg->target_peer, | ||
3036 | &msg->app_id, | ||
3037 | &channel_window_cb, | ||
3038 | &channel_end_cb, | ||
3039 | cadet_handlers); | ||
3040 | op->mq = GNUNET_CADET_get_mq (op->channel); | ||
3041 | op->state = intersection_evaluate (op, context); // FIXME: inline! | ||
3042 | if (NULL == op->state) | ||
3043 | { | ||
3044 | GNUNET_break (0); | ||
3045 | GNUNET_SERVICE_client_drop (cs->client); | ||
3046 | return; | ||
3047 | } | ||
3048 | GNUNET_SERVICE_client_continue (cs->client); | ||
3049 | } | ||
3050 | |||
3051 | |||
3052 | /** | ||
3053 | * Handle a request from the client to cancel a running set operation. | ||
3054 | * | ||
3055 | * @param cls the client | ||
3056 | * @param msg the message | ||
3057 | */ | ||
3058 | static void | ||
3059 | handle_client_cancel (void *cls, const struct GNUNET_SET_CancelMessage *msg) | ||
3060 | { | ||
3061 | struct ClientState *cs = cls; | ||
3062 | struct Set *set; | ||
3063 | struct Operation *op; | ||
3064 | int found; | ||
3065 | |||
3066 | if (NULL == (set = cs->set)) | ||
3067 | { | ||
3068 | /* client without a set requested an operation */ | ||
3069 | GNUNET_break (0); | ||
3070 | GNUNET_SERVICE_client_drop (cs->client); | ||
3071 | return; | ||
3072 | } | ||
3073 | found = GNUNET_NO; | ||
3074 | for (op = set->ops_head; NULL != op; op = op->next) | ||
3075 | { | ||
3076 | if (op->client_request_id == ntohl (msg->request_id)) | ||
3077 | { | ||
3078 | found = GNUNET_YES; | ||
3079 | break; | ||
3080 | } | ||
3081 | } | ||
3082 | if (GNUNET_NO == found) | ||
3083 | { | ||
3084 | /* It may happen that the operation was already destroyed due to | ||
3085 | * the other peer disconnecting. The client may not know about this | ||
3086 | * yet and try to cancel the (just barely non-existent) operation. | ||
3087 | * So this is not a hard error. | ||
3088 | */GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
3089 | "Client canceled non-existent op %u\n", | ||
3090 | (uint32_t) ntohl (msg->request_id)); | ||
3091 | } | ||
3092 | else | ||
3093 | { | ||
3094 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3095 | "Client requested cancel for op %u\n", | ||
3096 | (uint32_t) ntohl (msg->request_id)); | ||
3097 | _GSS_operation_destroy (op, GNUNET_YES); | ||
3098 | } | ||
3099 | GNUNET_SERVICE_client_continue (cs->client); | ||
3100 | } | ||
3101 | |||
3102 | |||
3103 | /** | ||
3104 | * Handle a request from the client to accept a set operation that | ||
3105 | * came from a remote peer. We forward the accept to the associated | ||
3106 | * operation for handling | ||
3107 | * | ||
3108 | * @param cls the client | ||
3109 | * @param msg the message | ||
3110 | */ | ||
3111 | static void | ||
3112 | handle_client_accept (void *cls, const struct GNUNET_SET_AcceptMessage *msg) | ||
3113 | { | ||
3114 | struct ClientState *cs = cls; | ||
3115 | struct Set *set; | ||
3116 | struct Operation *op; | ||
3117 | struct GNUNET_SET_ResultMessage *result_message; | ||
3118 | struct GNUNET_MQ_Envelope *ev; | ||
3119 | struct Listener *listener; | ||
3120 | |||
3121 | if (NULL == (set = cs->set)) | ||
3122 | { | ||
3123 | /* client without a set requested to accept */ | ||
3124 | GNUNET_break (0); | ||
3125 | GNUNET_SERVICE_client_drop (cs->client); | ||
3126 | return; | ||
3127 | } | ||
3128 | op = get_incoming (ntohl (msg->accept_reject_id)); | ||
3129 | if (NULL == op) | ||
3130 | { | ||
3131 | /* It is not an error if the set op does not exist -- it may | ||
3132 | * have been destroyed when the partner peer disconnected. */ | ||
3133 | GNUNET_log ( | ||
3134 | GNUNET_ERROR_TYPE_INFO, | ||
3135 | "Client %p accepted request %u of listener %p that is no longer active\n", | ||
3136 | cs, | ||
3137 | ntohl (msg->accept_reject_id), | ||
3138 | cs->listener); | ||
3139 | ev = GNUNET_MQ_msg (result_message, GNUNET_MESSAGE_TYPE_SETI_RESULT); | ||
3140 | result_message->request_id = msg->request_id; | ||
3141 | result_message->result_status = htons (GNUNET_SET_STATUS_FAILURE); | ||
3142 | GNUNET_MQ_send (set->cs->mq, ev); | ||
3143 | GNUNET_SERVICE_client_continue (cs->client); | ||
3144 | return; | ||
3145 | } | ||
3146 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3147 | "Client accepting request %u\n", | ||
3148 | (uint32_t) ntohl (msg->accept_reject_id)); | ||
3149 | listener = op->listener; | ||
3150 | op->listener = NULL; | ||
3151 | GNUNET_CONTAINER_DLL_remove (listener->op_head, listener->op_tail, op); | ||
3152 | op->set = set; | ||
3153 | GNUNET_CONTAINER_DLL_insert (set->ops_head, set->ops_tail, op); | ||
3154 | op->client_request_id = ntohl (msg->request_id); | ||
3155 | op->result_mode = ntohl (msg->result_mode); | ||
3156 | op->byzantine = msg->byzantine; | ||
3157 | op->byzantine_lower_bound = msg->byzantine_lower_bound; | ||
3158 | op->force_full = msg->force_full; | ||
3159 | op->force_delta = msg->force_delta; | ||
3160 | |||
3161 | /* Advance generation values, so that future mutations do not | ||
3162 | interfer with the running operation. */ | ||
3163 | op->generation_created = set->current_generation; | ||
3164 | advance_generation (set); | ||
3165 | GNUNET_assert (NULL == op->state); | ||
3166 | op->state = intersection_accept (op); // FIXME: inline | ||
3167 | if (NULL == op->state) | ||
3168 | { | ||
3169 | GNUNET_break (0); | ||
3170 | GNUNET_SERVICE_client_drop (cs->client); | ||
3171 | return; | ||
3172 | } | ||
3173 | /* Now allow CADET to continue, as we did not do this in | ||
3174 | #handle_incoming_msg (as we wanted to first see if the | ||
3175 | local client would accept the request). */ | ||
3176 | GNUNET_CADET_receive_done (op->channel); | ||
3177 | GNUNET_SERVICE_client_continue (cs->client); | ||
3178 | } | ||
3179 | |||
3180 | |||
3181 | /** | ||
3182 | * Called to clean up, after a shutdown has been requested. | ||
3183 | * | ||
3184 | * @param cls closure, NULL | ||
3185 | */ | ||
3186 | static void | ||
3187 | shutdown_task (void *cls) | ||
3188 | { | ||
3189 | /* Delay actual shutdown to allow service to disconnect clients */ | ||
3190 | in_shutdown = GNUNET_YES; | ||
3191 | if (0 == num_clients) | ||
3192 | { | ||
3193 | if (NULL != cadet) | ||
3194 | { | ||
3195 | GNUNET_CADET_disconnect (cadet); | ||
3196 | cadet = NULL; | ||
3197 | } | ||
3198 | } | ||
3199 | GNUNET_STATISTICS_destroy (_GSS_statistics, GNUNET_YES); | ||
3200 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "handled shutdown request\n"); | ||
3201 | } | ||
3202 | |||
3203 | |||
3204 | /** | ||
3205 | * Function called by the service's run | ||
3206 | * method to run service-specific setup code. | ||
3207 | * | ||
3208 | * @param cls closure | ||
3209 | * @param cfg configuration to use | ||
3210 | * @param service the initialized service | ||
3211 | */ | ||
3212 | static void | ||
3213 | run (void *cls, | ||
3214 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
3215 | struct GNUNET_SERVICE_Handle *service) | ||
3216 | { | ||
3217 | /* FIXME: need to modify SERVICE (!) API to allow | ||
3218 | us to run a shutdown task *after* clients were | ||
3219 | forcefully disconnected! */ | ||
3220 | GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL); | ||
3221 | _GSS_statistics = GNUNET_STATISTICS_create ("set", cfg); | ||
3222 | cadet = GNUNET_CADET_connect (cfg); | ||
3223 | if (NULL == cadet) | ||
3224 | { | ||
3225 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
3226 | _ ("Could not connect to CADET service\n")); | ||
3227 | GNUNET_SCHEDULER_shutdown (); | ||
3228 | return; | ||
3229 | } | ||
3230 | } | ||
3231 | |||
3232 | |||
3233 | /** | ||
3234 | * Define "main" method using service macro. | ||
3235 | */ | ||
3236 | GNUNET_SERVICE_MAIN ( | ||
3237 | "set", | ||
3238 | GNUNET_SERVICE_OPTION_NONE, | ||
3239 | &run, | ||
3240 | &client_connect_cb, | ||
3241 | &client_disconnect_cb, | ||
3242 | NULL, | ||
3243 | GNUNET_MQ_hd_fixed_size (client_accept, | ||
3244 | GNUNET_MESSAGE_TYPE_SETI_ACCEPT, | ||
3245 | struct GNUNET_SET_AcceptMessage, | ||
3246 | NULL), | ||
3247 | GNUNET_MQ_hd_var_size (client_mutation, | ||
3248 | GNUNET_MESSAGE_TYPE_SETI_ADD, | ||
3249 | struct GNUNET_SET_ElementMessage, | ||
3250 | NULL), | ||
3251 | GNUNET_MQ_hd_fixed_size (client_create_set, | ||
3252 | GNUNET_MESSAGE_TYPE_SETI_CREATE, | ||
3253 | struct GNUNET_SET_CreateMessage, | ||
3254 | NULL), | ||
3255 | GNUNET_MQ_hd_var_size (client_evaluate, | ||
3256 | GNUNET_MESSAGE_TYPE_SETI_EVALUATE, | ||
3257 | struct GNUNET_SET_EvaluateMessage, | ||
3258 | NULL), | ||
3259 | GNUNET_MQ_hd_fixed_size (client_listen, | ||
3260 | GNUNET_MESSAGE_TYPE_SETI_LISTEN, | ||
3261 | struct GNUNET_SET_ListenMessage, | ||
3262 | NULL), | ||
3263 | GNUNET_MQ_hd_fixed_size (client_reject, | ||
3264 | GNUNET_MESSAGE_TYPE_SETI_REJECT, | ||
3265 | struct GNUNET_SET_RejectMessage, | ||
3266 | NULL), | ||
3267 | GNUNET_MQ_hd_fixed_size (client_cancel, | ||
3268 | GNUNET_MESSAGE_TYPE_SETI_CANCEL, | ||
3269 | struct GNUNET_SET_CancelMessage, | ||
3270 | NULL), | ||
3271 | GNUNET_MQ_handler_end ()); | ||
3272 | |||
3273 | |||
3274 | /* end of gnunet-service-seti.c */ | ||