diff options
author | Martin Schanzenbach <schanzen@gnunet.org> | 2023-10-18 14:46:22 +0200 |
---|---|---|
committer | Martin Schanzenbach <schanzen@gnunet.org> | 2023-10-18 14:46:22 +0200 |
commit | 76299f0b66a3f8ce86df90171b450da6b9cd9b7c (patch) | |
tree | 381f49fb3208a4b9ae19781372ffd6c492eae19c /src/rps/gnunet-service-rps.c | |
parent | 2f93ff3b6d3524e1e6dc23f70966fbae3ca9d3af (diff) | |
download | gnunet-76299f0b66a3f8ce86df90171b450da6b9cd9b7c.tar.gz gnunet-76299f0b66a3f8ce86df90171b450da6b9cd9b7c.zip |
BUILD: Move experimental components to contrib
Diffstat (limited to 'src/rps/gnunet-service-rps.c')
-rw-r--r-- | src/rps/gnunet-service-rps.c | 5009 |
1 files changed, 0 insertions, 5009 deletions
diff --git a/src/rps/gnunet-service-rps.c b/src/rps/gnunet-service-rps.c deleted file mode 100644 index 76e33c87b..000000000 --- a/src/rps/gnunet-service-rps.c +++ /dev/null | |||
@@ -1,5009 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2013-2015 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 | /** | ||
22 | * @file rps/gnunet-service-rps.c | ||
23 | * @brief rps service implementation | ||
24 | * @author Julius Bünger | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_applications.h" | ||
28 | #include "gnunet_util_lib.h" | ||
29 | #include "gnunet_cadet_service.h" | ||
30 | #include "gnunet_core_service.h" | ||
31 | #include "gnunet_peerstore_service.h" | ||
32 | #include "gnunet_nse_service.h" | ||
33 | #include "gnunet_statistics_service.h" | ||
34 | #include "rps.h" | ||
35 | #include "rps-test_util.h" | ||
36 | #include "gnunet-service-rps_sampler.h" | ||
37 | #include "gnunet-service-rps_custommap.h" | ||
38 | #include "gnunet-service-rps_view.h" | ||
39 | #include "gnunet_constants.h" | ||
40 | |||
41 | #include <math.h> | ||
42 | #include <inttypes.h> | ||
43 | #include <string.h> | ||
44 | |||
45 | #define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__) | ||
46 | |||
47 | // TODO check for overflows | ||
48 | |||
49 | // TODO align message structs | ||
50 | |||
51 | // TODO connect to friends | ||
52 | |||
53 | // TODO blacklist? (-> mal peer detection on top of brahms) | ||
54 | |||
55 | // hist_size_init, hist_size_max | ||
56 | |||
57 | /*********************************************************************** | ||
58 | * Old gnunet-service-rps_peers.c | ||
59 | ***********************************************************************/ | ||
60 | |||
61 | /** | ||
62 | * Set a peer flag of given peer context. | ||
63 | */ | ||
64 | #define SET_PEER_FLAG(peer_ctx, mask) ((peer_ctx->peer_flags) |= (mask)) | ||
65 | |||
66 | /** | ||
67 | * Get peer flag of given peer context. | ||
68 | */ | ||
69 | #define check_peer_flag_set(peer_ctx, mask) \ | ||
70 | ((peer_ctx->peer_flags) & (mask) ? GNUNET_YES : GNUNET_NO) | ||
71 | |||
72 | /** | ||
73 | * Unset flag of given peer context. | ||
74 | */ | ||
75 | #define UNSET_PEER_FLAG(peer_ctx, mask) ((peer_ctx->peer_flags) &= ~(mask)) | ||
76 | |||
77 | /** | ||
78 | * Get channel flag of given channel context. | ||
79 | */ | ||
80 | #define check_channel_flag_set(channel_flags, mask) \ | ||
81 | ((*channel_flags) & (mask) ? GNUNET_YES : GNUNET_NO) | ||
82 | |||
83 | /** | ||
84 | * Unset flag of given channel context. | ||
85 | */ | ||
86 | #define unset_channel_flag(channel_flags, mask) ((*channel_flags) &= ~(mask)) | ||
87 | |||
88 | |||
89 | /** | ||
90 | * Pending operation on peer consisting of callback and closure | ||
91 | * | ||
92 | * When an operation cannot be executed right now this struct is used to store | ||
93 | * the callback and closure for later execution. | ||
94 | */ | ||
95 | struct PeerPendingOp | ||
96 | { | ||
97 | /** | ||
98 | * Callback | ||
99 | */ | ||
100 | PeerOp op; | ||
101 | |||
102 | /** | ||
103 | * Closure | ||
104 | */ | ||
105 | void *op_cls; | ||
106 | }; | ||
107 | |||
108 | /** | ||
109 | * List containing all messages that are yet to be send | ||
110 | * | ||
111 | * This is used to keep track of all messages that have not been sent yet. When | ||
112 | * a peer is to be removed the pending messages can be removed properly. | ||
113 | */ | ||
114 | struct PendingMessage | ||
115 | { | ||
116 | /** | ||
117 | * DLL next, prev | ||
118 | */ | ||
119 | struct PendingMessage *next; | ||
120 | struct PendingMessage *prev; | ||
121 | |||
122 | /** | ||
123 | * The envelope to the corresponding message | ||
124 | */ | ||
125 | struct GNUNET_MQ_Envelope *ev; | ||
126 | |||
127 | /** | ||
128 | * The corresponding context | ||
129 | */ | ||
130 | struct PeerContext *peer_ctx; | ||
131 | |||
132 | /** | ||
133 | * The message type | ||
134 | */ | ||
135 | const char *type; | ||
136 | }; | ||
137 | |||
138 | /** | ||
139 | * @brief Context for a channel | ||
140 | */ | ||
141 | struct ChannelCtx; | ||
142 | |||
143 | /** | ||
144 | * Struct used to keep track of other peer's status | ||
145 | * | ||
146 | * This is stored in a multipeermap. | ||
147 | * It contains information such as cadet channels, a message queue for sending, | ||
148 | * status about the channels, the pending operations on this peer and some flags | ||
149 | * about the status of the peer itself. (online, valid, ...) | ||
150 | */ | ||
151 | struct PeerContext | ||
152 | { | ||
153 | /** | ||
154 | * The Sub this context belongs to. | ||
155 | */ | ||
156 | struct Sub *sub; | ||
157 | |||
158 | /** | ||
159 | * Message queue open to client | ||
160 | */ | ||
161 | struct GNUNET_MQ_Handle *mq; | ||
162 | |||
163 | /** | ||
164 | * Channel open to client. | ||
165 | */ | ||
166 | struct ChannelCtx *send_channel_ctx; | ||
167 | |||
168 | /** | ||
169 | * Channel open from client. | ||
170 | */ | ||
171 | struct ChannelCtx *recv_channel_ctx; | ||
172 | |||
173 | /** | ||
174 | * Array of pending operations on this peer. | ||
175 | */ | ||
176 | struct PeerPendingOp *pending_ops; | ||
177 | |||
178 | /** | ||
179 | * Handle to the callback given to cadet_ntfy_tmt_rdy() | ||
180 | * | ||
181 | * To be canceled on shutdown. | ||
182 | */ | ||
183 | struct PendingMessage *online_check_pending; | ||
184 | |||
185 | /** | ||
186 | * Number of pending operations. | ||
187 | */ | ||
188 | unsigned int num_pending_ops; | ||
189 | |||
190 | /** | ||
191 | * Identity of the peer | ||
192 | */ | ||
193 | struct GNUNET_PeerIdentity peer_id; | ||
194 | |||
195 | /** | ||
196 | * Flags indicating status of peer | ||
197 | */ | ||
198 | uint32_t peer_flags; | ||
199 | |||
200 | /** | ||
201 | * Last time we received something from that peer. | ||
202 | */ | ||
203 | struct GNUNET_TIME_Absolute last_message_recv; | ||
204 | |||
205 | /** | ||
206 | * Last time we received a keepalive message. | ||
207 | */ | ||
208 | struct GNUNET_TIME_Absolute last_keepalive; | ||
209 | |||
210 | /** | ||
211 | * DLL with all messages that are yet to be sent | ||
212 | */ | ||
213 | struct PendingMessage *pending_messages_head; | ||
214 | struct PendingMessage *pending_messages_tail; | ||
215 | |||
216 | /** | ||
217 | * This is pobably followed by 'statistical' data (when we first saw | ||
218 | * it, how did we get its ID, how many pushes (in a timeinterval), | ||
219 | * ...) | ||
220 | */ | ||
221 | uint32_t round_pull_req; | ||
222 | }; | ||
223 | |||
224 | /** | ||
225 | * @brief Closure to #valid_peer_iterator | ||
226 | */ | ||
227 | struct PeersIteratorCls | ||
228 | { | ||
229 | /** | ||
230 | * Iterator function | ||
231 | */ | ||
232 | PeersIterator iterator; | ||
233 | |||
234 | /** | ||
235 | * Closure to iterator | ||
236 | */ | ||
237 | void *cls; | ||
238 | }; | ||
239 | |||
240 | /** | ||
241 | * @brief Context for a channel | ||
242 | */ | ||
243 | struct ChannelCtx | ||
244 | { | ||
245 | /** | ||
246 | * @brief The channel itself | ||
247 | */ | ||
248 | struct GNUNET_CADET_Channel *channel; | ||
249 | |||
250 | /** | ||
251 | * @brief The peer context associated with the channel | ||
252 | */ | ||
253 | struct PeerContext *peer_ctx; | ||
254 | |||
255 | /** | ||
256 | * @brief When channel destruction needs to be delayed (because it is called | ||
257 | * from within the cadet routine of another channel destruction) this task | ||
258 | * refers to the respective _SCHEDULER_Task. | ||
259 | */ | ||
260 | struct GNUNET_SCHEDULER_Task *destruction_task; | ||
261 | }; | ||
262 | |||
263 | |||
264 | #if ENABLE_MALICIOUS | ||
265 | |||
266 | /** | ||
267 | * If type is 2 This struct is used to store the attacked peers in a DLL | ||
268 | */ | ||
269 | struct AttackedPeer | ||
270 | { | ||
271 | /** | ||
272 | * DLL | ||
273 | */ | ||
274 | struct AttackedPeer *next; | ||
275 | struct AttackedPeer *prev; | ||
276 | |||
277 | /** | ||
278 | * PeerID | ||
279 | */ | ||
280 | struct GNUNET_PeerIdentity peer_id; | ||
281 | }; | ||
282 | |||
283 | #endif /* ENABLE_MALICIOUS */ | ||
284 | |||
285 | /** | ||
286 | * @brief This number determines the number of slots for files that represent | ||
287 | * histograms | ||
288 | */ | ||
289 | #define HISTOGRAM_FILE_SLOTS 32 | ||
290 | |||
291 | /** | ||
292 | * @brief The size (in bytes) a file needs to store the histogram | ||
293 | * | ||
294 | * Per slot: 1 newline, up to 4 chars, | ||
295 | * Additionally: 1 null termination | ||
296 | */ | ||
297 | #define SIZE_DUMP_FILE (HISTOGRAM_FILE_SLOTS * 5) + 1 | ||
298 | |||
299 | /** | ||
300 | * @brief One Sub. | ||
301 | * | ||
302 | * Essentially one instance of brahms that only connects to other instances | ||
303 | * with the same (secret) value. | ||
304 | */ | ||
305 | struct Sub | ||
306 | { | ||
307 | /** | ||
308 | * @brief Hash of the shared value that defines Subs. | ||
309 | */ | ||
310 | struct GNUNET_HashCode hash; | ||
311 | |||
312 | /** | ||
313 | * @brief Port to communicate to other peers. | ||
314 | */ | ||
315 | struct GNUNET_CADET_Port *cadet_port; | ||
316 | |||
317 | /** | ||
318 | * @brief Hashmap of valid peers. | ||
319 | */ | ||
320 | struct GNUNET_CONTAINER_MultiPeerMap *valid_peers; | ||
321 | |||
322 | /** | ||
323 | * @brief Filename of the file that stores the valid peers persistently. | ||
324 | */ | ||
325 | char *filename_valid_peers; | ||
326 | |||
327 | /** | ||
328 | * Set of all peers to keep track of them. | ||
329 | */ | ||
330 | struct GNUNET_CONTAINER_MultiPeerMap *peer_map; | ||
331 | |||
332 | /** | ||
333 | * @brief This is the minimum estimate used as sampler size. | ||
334 | * | ||
335 | * It is configured by the user. | ||
336 | */ | ||
337 | unsigned int sampler_size_est_min; | ||
338 | |||
339 | /** | ||
340 | * The size of sampler we need to be able to satisfy the Brahms protocol's | ||
341 | * need of random peers. | ||
342 | * | ||
343 | * This is one minimum size the sampler grows to. | ||
344 | */ | ||
345 | unsigned int sampler_size_est_need; | ||
346 | |||
347 | /** | ||
348 | * Time interval the do_round task runs in. | ||
349 | */ | ||
350 | struct GNUNET_TIME_Relative round_interval; | ||
351 | |||
352 | /** | ||
353 | * Sampler used for the Brahms protocol itself. | ||
354 | */ | ||
355 | struct RPS_Sampler *sampler; | ||
356 | |||
357 | #ifdef TO_FILE_FULL | ||
358 | /** | ||
359 | * Name to log view to | ||
360 | */ | ||
361 | char *file_name_view_log; | ||
362 | #endif /* TO_FILE_FULL */ | ||
363 | |||
364 | #ifdef TO_FILE | ||
365 | #ifdef TO_FILE_FULL | ||
366 | /** | ||
367 | * Name to log number of observed peers to | ||
368 | */ | ||
369 | char *file_name_observed_log; | ||
370 | #endif /* TO_FILE_FULL */ | ||
371 | |||
372 | /** | ||
373 | * @brief Count the observed peers | ||
374 | */ | ||
375 | uint32_t num_observed_peers; | ||
376 | |||
377 | /** | ||
378 | * @brief Multipeermap (ab-) used to count unique peer_ids | ||
379 | */ | ||
380 | struct GNUNET_CONTAINER_MultiPeerMap *observed_unique_peers; | ||
381 | #endif /* TO_FILE */ | ||
382 | |||
383 | /** | ||
384 | * List to store peers received through pushes temporary. | ||
385 | */ | ||
386 | struct CustomPeerMap *push_map; | ||
387 | |||
388 | /** | ||
389 | * List to store peers received through pulls temporary. | ||
390 | */ | ||
391 | struct CustomPeerMap *pull_map; | ||
392 | |||
393 | /** | ||
394 | * @brief This is the estimate used as view size. | ||
395 | * | ||
396 | * It is initialised with the minimum | ||
397 | */ | ||
398 | unsigned int view_size_est_need; | ||
399 | |||
400 | /** | ||
401 | * @brief This is the minimum estimate used as view size. | ||
402 | * | ||
403 | * It is configured by the user. | ||
404 | */ | ||
405 | unsigned int view_size_est_min; | ||
406 | |||
407 | /** | ||
408 | * @brief The view. | ||
409 | */ | ||
410 | struct View *view; | ||
411 | |||
412 | /** | ||
413 | * Identifier for the main task that runs periodically. | ||
414 | */ | ||
415 | struct GNUNET_SCHEDULER_Task *do_round_task; | ||
416 | |||
417 | /* === stats === */ | ||
418 | |||
419 | /** | ||
420 | * @brief Counts the executed rounds. | ||
421 | */ | ||
422 | uint32_t num_rounds; | ||
423 | |||
424 | /** | ||
425 | * @brief This array accumulates the number of received pushes per round. | ||
426 | * | ||
427 | * Number at index i represents the number of rounds with i observed pushes. | ||
428 | */ | ||
429 | uint32_t push_recv[HISTOGRAM_FILE_SLOTS]; | ||
430 | |||
431 | /** | ||
432 | * @brief Histogram of deltas between the expected and actual number of | ||
433 | * received pushes. | ||
434 | * | ||
435 | * As half of the entries are expected to be negative, this is shifted by | ||
436 | * #HISTOGRAM_FILE_SLOTS/2. | ||
437 | */ | ||
438 | uint32_t push_delta[HISTOGRAM_FILE_SLOTS]; | ||
439 | |||
440 | /** | ||
441 | * @brief Number of pull replies with this delay measured in rounds. | ||
442 | * | ||
443 | * Number at index i represents the number of pull replies with a delay of i | ||
444 | * rounds. | ||
445 | */ | ||
446 | uint32_t pull_delays[HISTOGRAM_FILE_SLOTS]; | ||
447 | }; | ||
448 | |||
449 | |||
450 | /*********************************************************************** | ||
451 | * Globals | ||
452 | ***********************************************************************/ | ||
453 | |||
454 | /** | ||
455 | * Our configuration. | ||
456 | */ | ||
457 | static const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
458 | |||
459 | /** | ||
460 | * Handle to the statistics service. | ||
461 | */ | ||
462 | struct GNUNET_STATISTICS_Handle *stats; | ||
463 | |||
464 | /** | ||
465 | * Handler to CADET. | ||
466 | */ | ||
467 | struct GNUNET_CADET_Handle *cadet_handle; | ||
468 | |||
469 | /** | ||
470 | * Handle to CORE | ||
471 | */ | ||
472 | struct GNUNET_CORE_Handle *core_handle; | ||
473 | |||
474 | /** | ||
475 | * @brief PeerMap to keep track of connected peers. | ||
476 | */ | ||
477 | struct GNUNET_CONTAINER_MultiPeerMap *map_single_hop; | ||
478 | |||
479 | /** | ||
480 | * Our own identity. | ||
481 | */ | ||
482 | static struct GNUNET_PeerIdentity own_identity; | ||
483 | |||
484 | /** | ||
485 | * Percentage of total peer number in the view | ||
486 | * to send random PUSHes to | ||
487 | */ | ||
488 | static float alpha; | ||
489 | |||
490 | /** | ||
491 | * Percentage of total peer number in the view | ||
492 | * to send random PULLs to | ||
493 | */ | ||
494 | static float beta; | ||
495 | |||
496 | /** | ||
497 | * Handler to NSE. | ||
498 | */ | ||
499 | static struct GNUNET_NSE_Handle *nse; | ||
500 | |||
501 | /** | ||
502 | * Handle to the PEERSTORE service. | ||
503 | */ | ||
504 | static struct GNUNET_PEERSTORE_Handle *peerstore; | ||
505 | |||
506 | /** | ||
507 | * Our peerstore notification context. We use notification | ||
508 | * to instantly learn about new peers as they are discovered. | ||
509 | */ | ||
510 | static struct GNUNET_PEERSTORE_NotifyContext *peerstore_notify; | ||
511 | |||
512 | |||
513 | #if ENABLE_MALICIOUS | ||
514 | /** | ||
515 | * Type of malicious peer | ||
516 | * | ||
517 | * 0 Don't act malicious at all - Default | ||
518 | * 1 Try to maximise representation | ||
519 | * 2 Try to partition the network | ||
520 | * 3 Combined attack | ||
521 | */ | ||
522 | static uint32_t mal_type; | ||
523 | |||
524 | /** | ||
525 | * Other malicious peers | ||
526 | */ | ||
527 | static struct GNUNET_PeerIdentity *mal_peers; | ||
528 | |||
529 | /** | ||
530 | * Hashmap of malicious peers used as set. | ||
531 | * Used to more efficiently check whether we know that peer. | ||
532 | */ | ||
533 | static struct GNUNET_CONTAINER_MultiPeerMap *mal_peer_set; | ||
534 | |||
535 | /** | ||
536 | * Number of other malicious peers | ||
537 | */ | ||
538 | static uint32_t num_mal_peers; | ||
539 | |||
540 | |||
541 | /** | ||
542 | * If type is 2 this is the DLL of attacked peers | ||
543 | */ | ||
544 | static struct AttackedPeer *att_peers_head; | ||
545 | static struct AttackedPeer *att_peers_tail; | ||
546 | |||
547 | /** | ||
548 | * This index is used to point to an attacked peer to | ||
549 | * implement the round-robin-ish way to select attacked peers. | ||
550 | */ | ||
551 | static struct AttackedPeer *att_peer_index; | ||
552 | |||
553 | /** | ||
554 | * Hashmap of attacked peers used as set. | ||
555 | * Used to more efficiently check whether we know that peer. | ||
556 | */ | ||
557 | static struct GNUNET_CONTAINER_MultiPeerMap *att_peer_set; | ||
558 | |||
559 | /** | ||
560 | * Number of attacked peers | ||
561 | */ | ||
562 | static uint32_t num_attacked_peers; | ||
563 | |||
564 | /** | ||
565 | * If type is 1 this is the attacked peer | ||
566 | */ | ||
567 | static struct GNUNET_PeerIdentity attacked_peer; | ||
568 | |||
569 | /** | ||
570 | * The limit of PUSHes we can send in one round. | ||
571 | * This is an assumption of the Brahms protocol and either implemented | ||
572 | * via proof of work | ||
573 | * or | ||
574 | * assumed to be the bandwidth limitation. | ||
575 | */ | ||
576 | static uint32_t push_limit = 10000; | ||
577 | #endif /* ENABLE_MALICIOUS */ | ||
578 | |||
579 | /** | ||
580 | * @brief Main Sub. | ||
581 | * | ||
582 | * This is run in any case by all peers and connects to all peers without | ||
583 | * specifying a shared value. | ||
584 | */ | ||
585 | static struct Sub *msub; | ||
586 | |||
587 | /** | ||
588 | * @brief Maximum number of valid peers to keep. | ||
589 | * TODO read from config | ||
590 | */ | ||
591 | static const uint32_t num_valid_peers_max = UINT32_MAX; | ||
592 | |||
593 | /*********************************************************************** | ||
594 | * /Globals | ||
595 | ***********************************************************************/ | ||
596 | |||
597 | |||
598 | static void | ||
599 | do_round (void *cls); | ||
600 | |||
601 | #if ENABLE_MALICIOUS | ||
602 | static void | ||
603 | do_mal_round (void *cls); | ||
604 | |||
605 | #endif /* ENABLE_MALICIOUS */ | ||
606 | |||
607 | |||
608 | /** | ||
609 | * @brief Get the #PeerContext associated with a peer | ||
610 | * | ||
611 | * @param peer_map The peer map containing the context | ||
612 | * @param peer the peer id | ||
613 | * | ||
614 | * @return the #PeerContext | ||
615 | */ | ||
616 | static struct PeerContext * | ||
617 | get_peer_ctx (const struct GNUNET_CONTAINER_MultiPeerMap *peer_map, | ||
618 | const struct GNUNET_PeerIdentity *peer) | ||
619 | { | ||
620 | struct PeerContext *ctx; | ||
621 | int ret; | ||
622 | |||
623 | ret = GNUNET_CONTAINER_multipeermap_contains (peer_map, peer); | ||
624 | GNUNET_assert (GNUNET_YES == ret); | ||
625 | ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer); | ||
626 | GNUNET_assert (NULL != ctx); | ||
627 | return ctx; | ||
628 | } | ||
629 | |||
630 | |||
631 | /** | ||
632 | * @brief Check whether we have information about the given peer. | ||
633 | * | ||
634 | * FIXME probably deprecated. Make this the new _online. | ||
635 | * | ||
636 | * @param peer_map The peer map to check for the existence of @a peer | ||
637 | * @param peer peer in question | ||
638 | * | ||
639 | * @return #GNUNET_YES if peer is known | ||
640 | * #GNUNET_NO if peer is not known | ||
641 | */ | ||
642 | static int | ||
643 | check_peer_known (const struct GNUNET_CONTAINER_MultiPeerMap *peer_map, | ||
644 | const struct GNUNET_PeerIdentity *peer) | ||
645 | { | ||
646 | if (NULL != peer_map) | ||
647 | { | ||
648 | return GNUNET_CONTAINER_multipeermap_contains (peer_map, peer); | ||
649 | } | ||
650 | else | ||
651 | { | ||
652 | return GNUNET_NO; | ||
653 | } | ||
654 | } | ||
655 | |||
656 | |||
657 | /** | ||
658 | * @brief Create a new #PeerContext and insert it into the peer map | ||
659 | * | ||
660 | * @param sub The Sub this context belongs to. | ||
661 | * @param peer the peer to create the #PeerContext for | ||
662 | * | ||
663 | * @return the #PeerContext | ||
664 | */ | ||
665 | static struct PeerContext * | ||
666 | create_peer_ctx (struct Sub *sub, | ||
667 | const struct GNUNET_PeerIdentity *peer) | ||
668 | { | ||
669 | struct PeerContext *ctx; | ||
670 | int ret; | ||
671 | |||
672 | GNUNET_assert (GNUNET_NO == check_peer_known (sub->peer_map, peer)); | ||
673 | |||
674 | ctx = GNUNET_new (struct PeerContext); | ||
675 | ctx->peer_id = *peer; | ||
676 | ctx->sub = sub; | ||
677 | ret = GNUNET_CONTAINER_multipeermap_put (sub->peer_map, peer, ctx, | ||
678 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); | ||
679 | GNUNET_assert (GNUNET_OK == ret); | ||
680 | if (sub == msub) | ||
681 | { | ||
682 | GNUNET_STATISTICS_set (stats, | ||
683 | "# known peers", | ||
684 | GNUNET_CONTAINER_multipeermap_size (sub->peer_map), | ||
685 | GNUNET_NO); | ||
686 | } | ||
687 | return ctx; | ||
688 | } | ||
689 | |||
690 | |||
691 | /** | ||
692 | * @brief Create or get a #PeerContext | ||
693 | * | ||
694 | * @param sub The Sub to which the created context belongs to | ||
695 | * @param peer the peer to get the associated context to | ||
696 | * | ||
697 | * @return the context | ||
698 | */ | ||
699 | static struct PeerContext * | ||
700 | create_or_get_peer_ctx (struct Sub *sub, | ||
701 | const struct GNUNET_PeerIdentity *peer) | ||
702 | { | ||
703 | if (GNUNET_NO == check_peer_known (sub->peer_map, peer)) | ||
704 | { | ||
705 | return create_peer_ctx (sub, peer); | ||
706 | } | ||
707 | return get_peer_ctx (sub->peer_map, peer); | ||
708 | } | ||
709 | |||
710 | |||
711 | /** | ||
712 | * @brief Check whether we have a connection to this @a peer | ||
713 | * | ||
714 | * Also sets the #Peers_ONLINE flag accordingly | ||
715 | * | ||
716 | * @param peer_ctx Context of the peer of which connectivity is to be checked | ||
717 | * | ||
718 | * @return #GNUNET_YES if we are connected | ||
719 | * #GNUNET_NO otherwise | ||
720 | */ | ||
721 | static int | ||
722 | check_connected (struct PeerContext *peer_ctx) | ||
723 | { | ||
724 | /* If we don't know about this peer we don't know whether it's online */ | ||
725 | if (GNUNET_NO == check_peer_known (peer_ctx->sub->peer_map, | ||
726 | &peer_ctx->peer_id)) | ||
727 | { | ||
728 | return GNUNET_NO; | ||
729 | } | ||
730 | /* Get the context */ | ||
731 | peer_ctx = get_peer_ctx (peer_ctx->sub->peer_map, &peer_ctx->peer_id); | ||
732 | /* If we have no channel to this peer we don't know whether it's online */ | ||
733 | if ((NULL == peer_ctx->send_channel_ctx) && | ||
734 | (NULL == peer_ctx->recv_channel_ctx)) | ||
735 | { | ||
736 | UNSET_PEER_FLAG (peer_ctx, Peers_ONLINE); | ||
737 | return GNUNET_NO; | ||
738 | } | ||
739 | /* Otherwise (if we have a channel, we know that it's online */ | ||
740 | SET_PEER_FLAG (peer_ctx, Peers_ONLINE); | ||
741 | return GNUNET_YES; | ||
742 | } | ||
743 | |||
744 | |||
745 | /** | ||
746 | * @brief The closure to #get_rand_peer_iterator. | ||
747 | */ | ||
748 | struct GetRandPeerIteratorCls | ||
749 | { | ||
750 | /** | ||
751 | * @brief The index of the peer to return. | ||
752 | * Will be decreased until 0. | ||
753 | * Then current peer is returned. | ||
754 | */ | ||
755 | uint32_t index; | ||
756 | |||
757 | /** | ||
758 | * @brief Pointer to peer to return. | ||
759 | */ | ||
760 | const struct GNUNET_PeerIdentity *peer; | ||
761 | }; | ||
762 | |||
763 | |||
764 | /** | ||
765 | * @brief Iterator function for #get_random_peer_from_peermap. | ||
766 | * | ||
767 | * Implements #GNUNET_CONTAINER_PeerMapIterator. | ||
768 | * Decreases the index until the index is null. | ||
769 | * Then returns the current peer. | ||
770 | * | ||
771 | * @param cls the #GetRandPeerIteratorCls containing index and peer | ||
772 | * @param peer current peer | ||
773 | * @param value unused | ||
774 | * | ||
775 | * @return #GNUNET_YES if we should continue to | ||
776 | * iterate, | ||
777 | * #GNUNET_NO if not. | ||
778 | */ | ||
779 | static int | ||
780 | get_rand_peer_iterator (void *cls, | ||
781 | const struct GNUNET_PeerIdentity *peer, | ||
782 | void *value) | ||
783 | { | ||
784 | struct GetRandPeerIteratorCls *iterator_cls = cls; | ||
785 | |||
786 | (void) value; | ||
787 | |||
788 | if (0 >= iterator_cls->index) | ||
789 | { | ||
790 | iterator_cls->peer = peer; | ||
791 | return GNUNET_NO; | ||
792 | } | ||
793 | iterator_cls->index--; | ||
794 | return GNUNET_YES; | ||
795 | } | ||
796 | |||
797 | |||
798 | /** | ||
799 | * @brief Get a random peer from @a peer_map | ||
800 | * | ||
801 | * @param valid_peers Peer map containing valid peers from which to select a | ||
802 | * random one | ||
803 | * | ||
804 | * @return a random peer | ||
805 | */ | ||
806 | static const struct GNUNET_PeerIdentity * | ||
807 | get_random_peer_from_peermap (struct GNUNET_CONTAINER_MultiPeerMap *valid_peers) | ||
808 | { | ||
809 | struct GetRandPeerIteratorCls *iterator_cls; | ||
810 | const struct GNUNET_PeerIdentity *ret; | ||
811 | |||
812 | iterator_cls = GNUNET_new (struct GetRandPeerIteratorCls); | ||
813 | iterator_cls->index = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, | ||
814 | GNUNET_CONTAINER_multipeermap_size ( | ||
815 | valid_peers)); | ||
816 | (void) GNUNET_CONTAINER_multipeermap_iterate (valid_peers, | ||
817 | get_rand_peer_iterator, | ||
818 | iterator_cls); | ||
819 | ret = iterator_cls->peer; | ||
820 | GNUNET_free (iterator_cls); | ||
821 | return ret; | ||
822 | } | ||
823 | |||
824 | |||
825 | /** | ||
826 | * @brief Add a given @a peer to valid peers. | ||
827 | * | ||
828 | * If valid peers are already #num_valid_peers_max, delete a peer previously. | ||
829 | * | ||
830 | * @param peer The peer that is added to the valid peers. | ||
831 | * @param valid_peers Peer map of valid peers to which to add the @a peer | ||
832 | * | ||
833 | * @return #GNUNET_YES if no other peer had to be removed | ||
834 | * #GNUNET_NO otherwise | ||
835 | */ | ||
836 | static int | ||
837 | add_valid_peer (const struct GNUNET_PeerIdentity *peer, | ||
838 | struct GNUNET_CONTAINER_MultiPeerMap *valid_peers) | ||
839 | { | ||
840 | const struct GNUNET_PeerIdentity *rand_peer; | ||
841 | int ret; | ||
842 | |||
843 | ret = GNUNET_YES; | ||
844 | /* Remove random peers until there is space for a new one */ | ||
845 | while (num_valid_peers_max <= | ||
846 | GNUNET_CONTAINER_multipeermap_size (valid_peers)) | ||
847 | { | ||
848 | rand_peer = get_random_peer_from_peermap (valid_peers); | ||
849 | GNUNET_CONTAINER_multipeermap_remove_all (valid_peers, rand_peer); | ||
850 | ret = GNUNET_NO; | ||
851 | } | ||
852 | (void) GNUNET_CONTAINER_multipeermap_put (valid_peers, peer, NULL, | ||
853 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); | ||
854 | if (valid_peers == msub->valid_peers) | ||
855 | { | ||
856 | GNUNET_STATISTICS_set (stats, | ||
857 | "# valid peers", | ||
858 | GNUNET_CONTAINER_multipeermap_size (valid_peers), | ||
859 | GNUNET_NO); | ||
860 | } | ||
861 | return ret; | ||
862 | } | ||
863 | |||
864 | |||
865 | static void | ||
866 | remove_pending_message (struct PendingMessage *pending_msg, int cancel); | ||
867 | |||
868 | /** | ||
869 | * @brief Set the peer flag to living and | ||
870 | * call the pending operations on this peer. | ||
871 | * | ||
872 | * Also adds peer to #valid_peers. | ||
873 | * | ||
874 | * @param peer_ctx the #PeerContext of the peer to set online | ||
875 | */ | ||
876 | static void | ||
877 | set_peer_online (struct PeerContext *peer_ctx) | ||
878 | { | ||
879 | struct GNUNET_PeerIdentity *peer; | ||
880 | unsigned int i; | ||
881 | |||
882 | peer = &peer_ctx->peer_id; | ||
883 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
884 | "Peer %s is online and valid, calling %i pending operations on it\n", | ||
885 | GNUNET_i2s (peer), | ||
886 | peer_ctx->num_pending_ops); | ||
887 | |||
888 | if (NULL != peer_ctx->online_check_pending) | ||
889 | { | ||
890 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
891 | "Removing pending online check for peer %s\n", | ||
892 | GNUNET_i2s (&peer_ctx->peer_id)); | ||
893 | // TODO wait until cadet sets mq->cancel_impl | ||
894 | // GNUNET_MQ_send_cancel (peer_ctx->online_check_pending->ev); | ||
895 | remove_pending_message (peer_ctx->online_check_pending, GNUNET_YES); | ||
896 | peer_ctx->online_check_pending = NULL; | ||
897 | } | ||
898 | |||
899 | SET_PEER_FLAG (peer_ctx, Peers_ONLINE); | ||
900 | |||
901 | /* Call pending operations */ | ||
902 | for (i = 0; i < peer_ctx->num_pending_ops; i++) | ||
903 | { | ||
904 | peer_ctx->pending_ops[i].op (peer_ctx->pending_ops[i].op_cls, peer); | ||
905 | } | ||
906 | GNUNET_array_grow (peer_ctx->pending_ops, peer_ctx->num_pending_ops, 0); | ||
907 | } | ||
908 | |||
909 | |||
910 | static void | ||
911 | cleanup_destroyed_channel (void *cls, | ||
912 | const struct GNUNET_CADET_Channel *channel); | ||
913 | |||
914 | /* Declaration of handlers */ | ||
915 | static void | ||
916 | handle_peer_check (void *cls, | ||
917 | const struct GNUNET_MessageHeader *msg); | ||
918 | |||
919 | static void | ||
920 | handle_peer_push (void *cls, | ||
921 | const struct GNUNET_MessageHeader *msg); | ||
922 | |||
923 | static void | ||
924 | handle_peer_pull_request (void *cls, | ||
925 | const struct GNUNET_MessageHeader *msg); | ||
926 | |||
927 | static int | ||
928 | check_peer_pull_reply (void *cls, | ||
929 | const struct GNUNET_RPS_P2P_PullReplyMessage *msg); | ||
930 | |||
931 | static void | ||
932 | handle_peer_pull_reply (void *cls, | ||
933 | const struct GNUNET_RPS_P2P_PullReplyMessage *msg); | ||
934 | |||
935 | /* End declaration of handlers */ | ||
936 | |||
937 | /** | ||
938 | * @brief Allocate memory for a new channel context and insert it into DLL | ||
939 | * | ||
940 | * @param peer_ctx context of the according peer | ||
941 | * | ||
942 | * @return The channel context | ||
943 | */ | ||
944 | static struct ChannelCtx * | ||
945 | add_channel_ctx (struct PeerContext *peer_ctx) | ||
946 | { | ||
947 | struct ChannelCtx *channel_ctx; | ||
948 | |||
949 | channel_ctx = GNUNET_new (struct ChannelCtx); | ||
950 | channel_ctx->peer_ctx = peer_ctx; | ||
951 | return channel_ctx; | ||
952 | } | ||
953 | |||
954 | |||
955 | /** | ||
956 | * @brief Free memory and NULL pointers. | ||
957 | * | ||
958 | * @param channel_ctx The channel context. | ||
959 | */ | ||
960 | static void | ||
961 | remove_channel_ctx (struct ChannelCtx *channel_ctx) | ||
962 | { | ||
963 | struct PeerContext *peer_ctx = channel_ctx->peer_ctx; | ||
964 | |||
965 | if (NULL != channel_ctx->destruction_task) | ||
966 | { | ||
967 | GNUNET_SCHEDULER_cancel (channel_ctx->destruction_task); | ||
968 | channel_ctx->destruction_task = NULL; | ||
969 | } | ||
970 | |||
971 | if (NULL == peer_ctx) | ||
972 | return; | ||
973 | if (channel_ctx == peer_ctx->send_channel_ctx) | ||
974 | { | ||
975 | peer_ctx->send_channel_ctx = NULL; | ||
976 | peer_ctx->mq = NULL; | ||
977 | } | ||
978 | else if (channel_ctx == peer_ctx->recv_channel_ctx) | ||
979 | { | ||
980 | peer_ctx->recv_channel_ctx = NULL; | ||
981 | } | ||
982 | GNUNET_free (channel_ctx); | ||
983 | } | ||
984 | |||
985 | |||
986 | /** | ||
987 | * @brief Get the channel of a peer. If not existing, create. | ||
988 | * | ||
989 | * @param peer_ctx Context of the peer of which to get the channel | ||
990 | * @return the #GNUNET_CADET_Channel used to send data to @a peer_ctx | ||
991 | */ | ||
992 | struct GNUNET_CADET_Channel * | ||
993 | get_channel (struct PeerContext *peer_ctx) | ||
994 | { | ||
995 | /* There exists a copy-paste-clone in run() */ | ||
996 | struct GNUNET_MQ_MessageHandler cadet_handlers[] = { | ||
997 | GNUNET_MQ_hd_fixed_size (peer_check, | ||
998 | GNUNET_MESSAGE_TYPE_RPS_PP_CHECK_LIVE, | ||
999 | struct GNUNET_MessageHeader, | ||
1000 | NULL), | ||
1001 | GNUNET_MQ_hd_fixed_size (peer_push, | ||
1002 | GNUNET_MESSAGE_TYPE_RPS_PP_PUSH, | ||
1003 | struct GNUNET_MessageHeader, | ||
1004 | NULL), | ||
1005 | GNUNET_MQ_hd_fixed_size (peer_pull_request, | ||
1006 | GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST, | ||
1007 | struct GNUNET_MessageHeader, | ||
1008 | NULL), | ||
1009 | GNUNET_MQ_hd_var_size (peer_pull_reply, | ||
1010 | GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY, | ||
1011 | struct GNUNET_RPS_P2P_PullReplyMessage, | ||
1012 | NULL), | ||
1013 | GNUNET_MQ_handler_end () | ||
1014 | }; | ||
1015 | |||
1016 | |||
1017 | if (NULL == peer_ctx->send_channel_ctx) | ||
1018 | { | ||
1019 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1020 | "Trying to establish channel to peer %s\n", | ||
1021 | GNUNET_i2s (&peer_ctx->peer_id)); | ||
1022 | peer_ctx->send_channel_ctx = add_channel_ctx (peer_ctx); | ||
1023 | peer_ctx->send_channel_ctx->channel = | ||
1024 | GNUNET_CADET_channel_create (cadet_handle, | ||
1025 | peer_ctx->send_channel_ctx, /* context */ | ||
1026 | &peer_ctx->peer_id, | ||
1027 | &peer_ctx->sub->hash, | ||
1028 | NULL, /* WindowSize handler */ | ||
1029 | &cleanup_destroyed_channel, /* Disconnect handler */ | ||
1030 | cadet_handlers); | ||
1031 | } | ||
1032 | GNUNET_assert (NULL != peer_ctx->send_channel_ctx); | ||
1033 | GNUNET_assert (NULL != peer_ctx->send_channel_ctx->channel); | ||
1034 | return peer_ctx->send_channel_ctx->channel; | ||
1035 | } | ||
1036 | |||
1037 | |||
1038 | /** | ||
1039 | * Get the message queue (#GNUNET_MQ_Handle) of a specific peer. | ||
1040 | * | ||
1041 | * If we already have a message queue open to this client, | ||
1042 | * simply return it, otherwise create one. | ||
1043 | * | ||
1044 | * @param peer_ctx Context of the peer of which to get the mq | ||
1045 | * @return the #GNUNET_MQ_Handle | ||
1046 | */ | ||
1047 | static struct GNUNET_MQ_Handle * | ||
1048 | get_mq (struct PeerContext *peer_ctx) | ||
1049 | { | ||
1050 | if (NULL == peer_ctx->mq) | ||
1051 | { | ||
1052 | peer_ctx->mq = GNUNET_CADET_get_mq (get_channel (peer_ctx)); | ||
1053 | } | ||
1054 | return peer_ctx->mq; | ||
1055 | } | ||
1056 | |||
1057 | |||
1058 | /** | ||
1059 | * @brief Add an envelope to a message passed to mq to list of pending messages | ||
1060 | * | ||
1061 | * @param peer_ctx Context of the peer for which to insert the envelope | ||
1062 | * @param ev envelope to the message | ||
1063 | * @param type type of the message to be sent | ||
1064 | * @return pointer to pending message | ||
1065 | */ | ||
1066 | static struct PendingMessage * | ||
1067 | insert_pending_message (struct PeerContext *peer_ctx, | ||
1068 | struct GNUNET_MQ_Envelope *ev, | ||
1069 | const char *type) | ||
1070 | { | ||
1071 | struct PendingMessage *pending_msg; | ||
1072 | |||
1073 | pending_msg = GNUNET_new (struct PendingMessage); | ||
1074 | pending_msg->ev = ev; | ||
1075 | pending_msg->peer_ctx = peer_ctx; | ||
1076 | pending_msg->type = type; | ||
1077 | GNUNET_CONTAINER_DLL_insert (peer_ctx->pending_messages_head, | ||
1078 | peer_ctx->pending_messages_tail, | ||
1079 | pending_msg); | ||
1080 | return pending_msg; | ||
1081 | } | ||
1082 | |||
1083 | |||
1084 | /** | ||
1085 | * @brief Remove a pending message from the respective DLL | ||
1086 | * | ||
1087 | * @param pending_msg the pending message to remove | ||
1088 | * @param cancel whether to cancel the pending message, too | ||
1089 | */ | ||
1090 | static void | ||
1091 | remove_pending_message (struct PendingMessage *pending_msg, int cancel) | ||
1092 | { | ||
1093 | struct PeerContext *peer_ctx; | ||
1094 | |||
1095 | (void) cancel; | ||
1096 | |||
1097 | peer_ctx = pending_msg->peer_ctx; | ||
1098 | GNUNET_assert (NULL != peer_ctx); | ||
1099 | GNUNET_CONTAINER_DLL_remove (peer_ctx->pending_messages_head, | ||
1100 | peer_ctx->pending_messages_tail, | ||
1101 | pending_msg); | ||
1102 | // TODO wait for the cadet implementation of message cancellation | ||
1103 | // if (GNUNET_YES == cancel) | ||
1104 | // { | ||
1105 | // GNUNET_MQ_send_cancel (pending_msg->ev); | ||
1106 | // } | ||
1107 | GNUNET_free (pending_msg); | ||
1108 | } | ||
1109 | |||
1110 | |||
1111 | /** | ||
1112 | * @brief This is called in response to the first message we sent as a | ||
1113 | * online check. | ||
1114 | * | ||
1115 | * @param cls #PeerContext of peer with pending online check | ||
1116 | */ | ||
1117 | static void | ||
1118 | mq_online_check_successful (void *cls) | ||
1119 | { | ||
1120 | struct PeerContext *peer_ctx = cls; | ||
1121 | |||
1122 | if (NULL != peer_ctx->online_check_pending) | ||
1123 | { | ||
1124 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1125 | "Online check for peer %s was successful\n", | ||
1126 | GNUNET_i2s (&peer_ctx->peer_id)); | ||
1127 | remove_pending_message (peer_ctx->online_check_pending, GNUNET_YES); | ||
1128 | peer_ctx->online_check_pending = NULL; | ||
1129 | set_peer_online (peer_ctx); | ||
1130 | (void) add_valid_peer (&peer_ctx->peer_id, peer_ctx->sub->valid_peers); | ||
1131 | } | ||
1132 | } | ||
1133 | |||
1134 | |||
1135 | /** | ||
1136 | * Issue a check whether peer is online | ||
1137 | * | ||
1138 | * @param peer_ctx the context of the peer | ||
1139 | */ | ||
1140 | static void | ||
1141 | check_peer_online (struct PeerContext *peer_ctx) | ||
1142 | { | ||
1143 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1144 | "Get informed about peer %s getting online\n", | ||
1145 | GNUNET_i2s (&peer_ctx->peer_id)); | ||
1146 | |||
1147 | struct GNUNET_MQ_Handle *mq; | ||
1148 | struct GNUNET_MQ_Envelope *ev; | ||
1149 | |||
1150 | ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_CHECK_LIVE); | ||
1151 | peer_ctx->online_check_pending = | ||
1152 | insert_pending_message (peer_ctx, ev, "Check online"); | ||
1153 | mq = get_mq (peer_ctx); | ||
1154 | GNUNET_MQ_notify_sent (ev, | ||
1155 | mq_online_check_successful, | ||
1156 | peer_ctx); | ||
1157 | GNUNET_MQ_send (mq, ev); | ||
1158 | if (peer_ctx->sub == msub) | ||
1159 | { | ||
1160 | GNUNET_STATISTICS_update (stats, | ||
1161 | "# pending online checks", | ||
1162 | 1, | ||
1163 | GNUNET_NO); | ||
1164 | } | ||
1165 | } | ||
1166 | |||
1167 | |||
1168 | /** | ||
1169 | * @brief Check whether function of type #PeerOp was already scheduled | ||
1170 | * | ||
1171 | * The array with pending operations will probably never grow really big, so | ||
1172 | * iterating over it should be ok. | ||
1173 | * | ||
1174 | * @param peer_ctx Context of the peer to check for the operation | ||
1175 | * @param peer_op the operation (#PeerOp) on the peer | ||
1176 | * | ||
1177 | * @return #GNUNET_YES if this operation is scheduled on that peer | ||
1178 | * #GNUNET_NO otherwise | ||
1179 | */ | ||
1180 | static int | ||
1181 | check_operation_scheduled (const struct PeerContext *peer_ctx, | ||
1182 | const PeerOp peer_op) | ||
1183 | { | ||
1184 | unsigned int i; | ||
1185 | |||
1186 | for (i = 0; i < peer_ctx->num_pending_ops; i++) | ||
1187 | if (peer_op == peer_ctx->pending_ops[i].op) | ||
1188 | return GNUNET_YES; | ||
1189 | return GNUNET_NO; | ||
1190 | } | ||
1191 | |||
1192 | |||
1193 | /** | ||
1194 | * @brief Callback for scheduler to destroy a channel | ||
1195 | * | ||
1196 | * @param channel_ctx Context of the channel | ||
1197 | */ | ||
1198 | static void | ||
1199 | destroy_channel (struct ChannelCtx *channel_ctx) | ||
1200 | { | ||
1201 | struct GNUNET_CADET_Channel *channel; | ||
1202 | |||
1203 | if (NULL != channel_ctx->destruction_task) | ||
1204 | { | ||
1205 | GNUNET_SCHEDULER_cancel (channel_ctx->destruction_task); | ||
1206 | channel_ctx->destruction_task = NULL; | ||
1207 | } | ||
1208 | GNUNET_assert (channel_ctx->channel != NULL); | ||
1209 | channel = channel_ctx->channel; | ||
1210 | channel_ctx->channel = NULL; | ||
1211 | GNUNET_CADET_channel_destroy (channel); | ||
1212 | remove_channel_ctx (channel_ctx); | ||
1213 | } | ||
1214 | |||
1215 | |||
1216 | /** | ||
1217 | * @brief Destroy a cadet channel. | ||
1218 | * | ||
1219 | * This satisfies the function signature of #GNUNET_SCHEDULER_TaskCallback. | ||
1220 | * | ||
1221 | * @param cls | ||
1222 | */ | ||
1223 | static void | ||
1224 | destroy_channel_cb (void *cls) | ||
1225 | { | ||
1226 | struct ChannelCtx *channel_ctx = cls; | ||
1227 | |||
1228 | channel_ctx->destruction_task = NULL; | ||
1229 | destroy_channel (channel_ctx); | ||
1230 | } | ||
1231 | |||
1232 | |||
1233 | /** | ||
1234 | * @brief Schedule the destruction of a channel for immediately afterwards. | ||
1235 | * | ||
1236 | * In case a channel is to be destroyed from within the callback to the | ||
1237 | * destruction of another channel (send channel), we cannot call | ||
1238 | * GNUNET_CADET_channel_destroy directly, but need to use this scheduling | ||
1239 | * construction. | ||
1240 | * | ||
1241 | * @param channel_ctx channel to be destroyed. | ||
1242 | */ | ||
1243 | static void | ||
1244 | schedule_channel_destruction (struct ChannelCtx *channel_ctx) | ||
1245 | { | ||
1246 | GNUNET_assert (NULL == | ||
1247 | channel_ctx->destruction_task); | ||
1248 | GNUNET_assert (NULL != | ||
1249 | channel_ctx->channel); | ||
1250 | channel_ctx->destruction_task = | ||
1251 | GNUNET_SCHEDULER_add_now (&destroy_channel_cb, | ||
1252 | channel_ctx); | ||
1253 | } | ||
1254 | |||
1255 | |||
1256 | /** | ||
1257 | * @brief Remove peer | ||
1258 | * | ||
1259 | * - Empties the list with pending operations | ||
1260 | * - Empties the list with pending messages | ||
1261 | * - Cancels potentially existing online check | ||
1262 | * - Schedules closing of send and recv channels | ||
1263 | * - Removes peer from peer map | ||
1264 | * | ||
1265 | * @param peer_ctx Context of the peer to be destroyed | ||
1266 | * @return #GNUNET_YES if peer was removed | ||
1267 | * #GNUNET_NO otherwise | ||
1268 | */ | ||
1269 | static int | ||
1270 | destroy_peer (struct PeerContext *peer_ctx) | ||
1271 | { | ||
1272 | GNUNET_assert (NULL != peer_ctx); | ||
1273 | GNUNET_assert (NULL != peer_ctx->sub->peer_map); | ||
1274 | if (GNUNET_NO == | ||
1275 | GNUNET_CONTAINER_multipeermap_contains (peer_ctx->sub->peer_map, | ||
1276 | &peer_ctx->peer_id)) | ||
1277 | { | ||
1278 | return GNUNET_NO; | ||
1279 | } | ||
1280 | SET_PEER_FLAG (peer_ctx, Peers_TO_DESTROY); | ||
1281 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1282 | "Going to remove peer %s\n", | ||
1283 | GNUNET_i2s (&peer_ctx->peer_id)); | ||
1284 | UNSET_PEER_FLAG (peer_ctx, Peers_ONLINE); | ||
1285 | |||
1286 | /* Clear list of pending operations */ | ||
1287 | // TODO this probably leaks memory | ||
1288 | // ('only' the cls to the function. Not sure what to do with it) | ||
1289 | GNUNET_array_grow (peer_ctx->pending_ops, | ||
1290 | peer_ctx->num_pending_ops, | ||
1291 | 0); | ||
1292 | /* Remove all pending messages */ | ||
1293 | while (NULL != peer_ctx->pending_messages_head) | ||
1294 | { | ||
1295 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1296 | "Removing unsent %s\n", | ||
1297 | peer_ctx->pending_messages_head->type); | ||
1298 | /* Cancel pending message, too */ | ||
1299 | if ((NULL != peer_ctx->online_check_pending) && | ||
1300 | (0 == memcmp (peer_ctx->pending_messages_head, | ||
1301 | peer_ctx->online_check_pending, | ||
1302 | sizeof(struct PendingMessage)))) | ||
1303 | { | ||
1304 | peer_ctx->online_check_pending = NULL; | ||
1305 | if (peer_ctx->sub == msub) | ||
1306 | { | ||
1307 | GNUNET_STATISTICS_update (stats, | ||
1308 | "# pending online checks", | ||
1309 | -1, | ||
1310 | GNUNET_NO); | ||
1311 | } | ||
1312 | } | ||
1313 | remove_pending_message (peer_ctx->pending_messages_head, | ||
1314 | GNUNET_YES); | ||
1315 | } | ||
1316 | |||
1317 | /* If we are still waiting for notification whether this peer is online | ||
1318 | * cancel the according task */ | ||
1319 | if (NULL != peer_ctx->online_check_pending) | ||
1320 | { | ||
1321 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1322 | "Removing pending online check for peer %s\n", | ||
1323 | GNUNET_i2s (&peer_ctx->peer_id)); | ||
1324 | // TODO wait until cadet sets mq->cancel_impl | ||
1325 | // GNUNET_MQ_send_cancel (peer_ctx->online_check_pending->ev); | ||
1326 | remove_pending_message (peer_ctx->online_check_pending, | ||
1327 | GNUNET_YES); | ||
1328 | peer_ctx->online_check_pending = NULL; | ||
1329 | } | ||
1330 | |||
1331 | if (NULL != peer_ctx->send_channel_ctx) | ||
1332 | { | ||
1333 | /* This is possibly called from within channel destruction */ | ||
1334 | peer_ctx->send_channel_ctx->peer_ctx = NULL; | ||
1335 | schedule_channel_destruction (peer_ctx->send_channel_ctx); | ||
1336 | peer_ctx->send_channel_ctx = NULL; | ||
1337 | peer_ctx->mq = NULL; | ||
1338 | } | ||
1339 | if (NULL != peer_ctx->recv_channel_ctx) | ||
1340 | { | ||
1341 | /* This is possibly called from within channel destruction */ | ||
1342 | peer_ctx->recv_channel_ctx->peer_ctx = NULL; | ||
1343 | schedule_channel_destruction (peer_ctx->recv_channel_ctx); | ||
1344 | peer_ctx->recv_channel_ctx = NULL; | ||
1345 | } | ||
1346 | |||
1347 | if (GNUNET_YES != | ||
1348 | GNUNET_CONTAINER_multipeermap_remove_all (peer_ctx->sub->peer_map, | ||
1349 | &peer_ctx->peer_id)) | ||
1350 | { | ||
1351 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
1352 | "removing peer from peer_ctx->sub->peer_map failed\n"); | ||
1353 | } | ||
1354 | if (peer_ctx->sub == msub) | ||
1355 | { | ||
1356 | GNUNET_STATISTICS_set (stats, | ||
1357 | "# known peers", | ||
1358 | GNUNET_CONTAINER_multipeermap_size ( | ||
1359 | peer_ctx->sub->peer_map), | ||
1360 | GNUNET_NO); | ||
1361 | } | ||
1362 | GNUNET_free (peer_ctx); | ||
1363 | return GNUNET_YES; | ||
1364 | } | ||
1365 | |||
1366 | |||
1367 | /** | ||
1368 | * Iterator over hash map entries. Deletes all contexts of peers. | ||
1369 | * | ||
1370 | * @param cls closure | ||
1371 | * @param key current public key | ||
1372 | * @param value value in the hash map | ||
1373 | * @return #GNUNET_YES if we should continue to iterate, | ||
1374 | * #GNUNET_NO if not. | ||
1375 | */ | ||
1376 | static int | ||
1377 | peermap_clear_iterator (void *cls, | ||
1378 | const struct GNUNET_PeerIdentity *key, | ||
1379 | void *value) | ||
1380 | { | ||
1381 | struct Sub *sub = cls; | ||
1382 | |||
1383 | (void) value; | ||
1384 | |||
1385 | destroy_peer (get_peer_ctx (sub->peer_map, key)); | ||
1386 | return GNUNET_YES; | ||
1387 | } | ||
1388 | |||
1389 | |||
1390 | /** | ||
1391 | * @brief This is called once a message is sent. | ||
1392 | * | ||
1393 | * Removes the pending message | ||
1394 | * | ||
1395 | * @param cls type of the message that was sent | ||
1396 | */ | ||
1397 | static void | ||
1398 | mq_notify_sent_cb (void *cls) | ||
1399 | { | ||
1400 | struct PendingMessage *pending_msg = (struct PendingMessage *) cls; | ||
1401 | |||
1402 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1403 | "%s was sent.\n", | ||
1404 | pending_msg->type); | ||
1405 | if (pending_msg->peer_ctx->sub == msub) | ||
1406 | { | ||
1407 | if (0 == strncmp ("PULL REPLY", pending_msg->type, 10)) | ||
1408 | GNUNET_STATISTICS_update (stats, "# pull replies sent", 1, GNUNET_NO); | ||
1409 | if (0 == strncmp ("PULL REQUEST", pending_msg->type, 12)) | ||
1410 | GNUNET_STATISTICS_update (stats, "# pull requests sent", 1, GNUNET_NO); | ||
1411 | if (0 == strncmp ("PUSH", pending_msg->type, 4)) | ||
1412 | GNUNET_STATISTICS_update (stats, "# pushes sent", 1, GNUNET_NO); | ||
1413 | if ((0 == strncmp ("PULL REQUEST", pending_msg->type, 12)) && | ||
1414 | (NULL != map_single_hop) && | ||
1415 | (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (map_single_hop, | ||
1416 | &pending_msg-> | ||
1417 | peer_ctx->peer_id)) ) | ||
1418 | GNUNET_STATISTICS_update (stats, | ||
1419 | "# pull requests sent (multi-hop peer)", | ||
1420 | 1, | ||
1421 | GNUNET_NO); | ||
1422 | } | ||
1423 | /* Do not cancel message */ | ||
1424 | remove_pending_message (pending_msg, GNUNET_NO); | ||
1425 | } | ||
1426 | |||
1427 | |||
1428 | /** | ||
1429 | * @brief Iterator function for #store_valid_peers. | ||
1430 | * | ||
1431 | * Implements #GNUNET_CONTAINER_PeerMapIterator. | ||
1432 | * Writes single peer to disk. | ||
1433 | * | ||
1434 | * @param cls the file handle to write to. | ||
1435 | * @param peer current peer | ||
1436 | * @param value unused | ||
1437 | * | ||
1438 | * @return #GNUNET_YES if we should continue to | ||
1439 | * iterate, | ||
1440 | * #GNUNET_NO if not. | ||
1441 | */ | ||
1442 | static int | ||
1443 | store_peer_presistently_iterator (void *cls, | ||
1444 | const struct GNUNET_PeerIdentity *peer, | ||
1445 | void *value) | ||
1446 | { | ||
1447 | const struct GNUNET_DISK_FileHandle *fh = cls; | ||
1448 | char peer_string[128]; | ||
1449 | int size; | ||
1450 | ssize_t ret; | ||
1451 | |||
1452 | (void) value; | ||
1453 | |||
1454 | if (NULL == peer) | ||
1455 | { | ||
1456 | return GNUNET_YES; | ||
1457 | } | ||
1458 | size = GNUNET_snprintf (peer_string, | ||
1459 | sizeof(peer_string), | ||
1460 | "%s\n", | ||
1461 | GNUNET_i2s_full (peer)); | ||
1462 | GNUNET_assert (53 == size); | ||
1463 | ret = GNUNET_DISK_file_write (fh, | ||
1464 | peer_string, | ||
1465 | size); | ||
1466 | GNUNET_assert (size == ret); | ||
1467 | return GNUNET_YES; | ||
1468 | } | ||
1469 | |||
1470 | |||
1471 | /** | ||
1472 | * @brief Store the peers currently in #valid_peers to disk. | ||
1473 | * | ||
1474 | * @param sub Sub for which to store the valid peers | ||
1475 | */ | ||
1476 | static void | ||
1477 | store_valid_peers (const struct Sub *sub) | ||
1478 | { | ||
1479 | struct GNUNET_DISK_FileHandle *fh; | ||
1480 | uint32_t number_written_peers; | ||
1481 | int ret; | ||
1482 | |||
1483 | if (0 == strncmp ("DISABLE", sub->filename_valid_peers, 7)) | ||
1484 | { | ||
1485 | return; | ||
1486 | } | ||
1487 | |||
1488 | ret = GNUNET_DISK_directory_create_for_file (sub->filename_valid_peers); | ||
1489 | if (GNUNET_SYSERR == ret) | ||
1490 | { | ||
1491 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
1492 | "Not able to create directory for file `%s'\n", | ||
1493 | sub->filename_valid_peers); | ||
1494 | GNUNET_break (0); | ||
1495 | } | ||
1496 | else if (GNUNET_NO == ret) | ||
1497 | { | ||
1498 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
1499 | "Directory for file `%s' exists but is not writable for us\n", | ||
1500 | sub->filename_valid_peers); | ||
1501 | GNUNET_break (0); | ||
1502 | } | ||
1503 | fh = GNUNET_DISK_file_open (sub->filename_valid_peers, | ||
1504 | GNUNET_DISK_OPEN_WRITE | ||
1505 | | GNUNET_DISK_OPEN_CREATE, | ||
1506 | GNUNET_DISK_PERM_USER_READ | ||
1507 | | GNUNET_DISK_PERM_USER_WRITE); | ||
1508 | if (NULL == fh) | ||
1509 | { | ||
1510 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
1511 | "Not able to write valid peers to file `%s'\n", | ||
1512 | sub->filename_valid_peers); | ||
1513 | return; | ||
1514 | } | ||
1515 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1516 | "Writing %u valid peers to disk\n", | ||
1517 | GNUNET_CONTAINER_multipeermap_size (sub->valid_peers)); | ||
1518 | number_written_peers = | ||
1519 | GNUNET_CONTAINER_multipeermap_iterate (sub->valid_peers, | ||
1520 | store_peer_presistently_iterator, | ||
1521 | fh); | ||
1522 | GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh)); | ||
1523 | GNUNET_assert (number_written_peers == | ||
1524 | GNUNET_CONTAINER_multipeermap_size (sub->valid_peers)); | ||
1525 | } | ||
1526 | |||
1527 | |||
1528 | /** | ||
1529 | * @brief Convert string representation of peer id to peer id. | ||
1530 | * | ||
1531 | * Counterpart to #GNUNET_i2s_full. | ||
1532 | * | ||
1533 | * @param string_repr The string representation of the peer id | ||
1534 | * | ||
1535 | * @return The peer id | ||
1536 | */ | ||
1537 | static const struct GNUNET_PeerIdentity * | ||
1538 | s2i_full (const char *string_repr) | ||
1539 | { | ||
1540 | struct GNUNET_PeerIdentity *peer; | ||
1541 | size_t len; | ||
1542 | int ret; | ||
1543 | |||
1544 | peer = GNUNET_new (struct GNUNET_PeerIdentity); | ||
1545 | len = strlen (string_repr); | ||
1546 | if (52 > len) | ||
1547 | { | ||
1548 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
1549 | "Not able to convert string representation of PeerID to PeerID\n" | ||
1550 | "String representation: %s (len %lu) - too short\n", | ||
1551 | string_repr, | ||
1552 | len); | ||
1553 | GNUNET_break (0); | ||
1554 | } | ||
1555 | else if (52 < len) | ||
1556 | { | ||
1557 | len = 52; | ||
1558 | } | ||
1559 | ret = GNUNET_CRYPTO_eddsa_public_key_from_string (string_repr, | ||
1560 | len, | ||
1561 | &peer->public_key); | ||
1562 | if (GNUNET_OK != ret) | ||
1563 | { | ||
1564 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
1565 | "Not able to convert string representation of PeerID to PeerID\n" | ||
1566 | "String representation: %s\n", | ||
1567 | string_repr); | ||
1568 | GNUNET_break (0); | ||
1569 | } | ||
1570 | return peer; | ||
1571 | } | ||
1572 | |||
1573 | |||
1574 | /** | ||
1575 | * @brief Restore the peers on disk to #valid_peers. | ||
1576 | * | ||
1577 | * @param sub Sub for which to restore the valid peers | ||
1578 | */ | ||
1579 | static void | ||
1580 | restore_valid_peers (const struct Sub *sub) | ||
1581 | { | ||
1582 | off_t file_size; | ||
1583 | uint32_t num_peers; | ||
1584 | struct GNUNET_DISK_FileHandle *fh; | ||
1585 | char *buf; | ||
1586 | ssize_t size_read; | ||
1587 | char *iter_buf; | ||
1588 | char *str_repr; | ||
1589 | const struct GNUNET_PeerIdentity *peer; | ||
1590 | |||
1591 | if (0 == strncmp ("DISABLE", sub->filename_valid_peers, 7)) | ||
1592 | { | ||
1593 | return; | ||
1594 | } | ||
1595 | |||
1596 | if (GNUNET_OK != GNUNET_DISK_file_test (sub->filename_valid_peers)) | ||
1597 | { | ||
1598 | return; | ||
1599 | } | ||
1600 | fh = GNUNET_DISK_file_open (sub->filename_valid_peers, | ||
1601 | GNUNET_DISK_OPEN_READ, | ||
1602 | GNUNET_DISK_PERM_NONE); | ||
1603 | GNUNET_assert (NULL != fh); | ||
1604 | GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_handle_size (fh, &file_size)); | ||
1605 | num_peers = file_size / 53; | ||
1606 | buf = GNUNET_malloc (file_size); | ||
1607 | size_read = GNUNET_DISK_file_read (fh, buf, file_size); | ||
1608 | GNUNET_assert (size_read == file_size); | ||
1609 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1610 | "Restoring %" PRIu32 " peers from file `%s'\n", | ||
1611 | num_peers, | ||
1612 | sub->filename_valid_peers); | ||
1613 | for (iter_buf = buf; iter_buf < buf + file_size - 1; iter_buf += 53) | ||
1614 | { | ||
1615 | str_repr = GNUNET_strndup (iter_buf, 53); | ||
1616 | peer = s2i_full (str_repr); | ||
1617 | GNUNET_free (str_repr); | ||
1618 | add_valid_peer (peer, sub->valid_peers); | ||
1619 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1620 | "Restored valid peer %s from disk\n", | ||
1621 | GNUNET_i2s_full (peer)); | ||
1622 | } | ||
1623 | iter_buf = NULL; | ||
1624 | GNUNET_free (buf); | ||
1625 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1626 | "num_peers: %" PRIu32 ", _size (sub->valid_peers): %u\n", | ||
1627 | num_peers, | ||
1628 | GNUNET_CONTAINER_multipeermap_size (sub->valid_peers)); | ||
1629 | if (num_peers != GNUNET_CONTAINER_multipeermap_size (sub->valid_peers)) | ||
1630 | { | ||
1631 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
1632 | "Number of restored peers does not match file size. Have probably duplicates.\n"); | ||
1633 | } | ||
1634 | GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh)); | ||
1635 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1636 | "Restored %u valid peers from disk\n", | ||
1637 | GNUNET_CONTAINER_multipeermap_size (sub->valid_peers)); | ||
1638 | } | ||
1639 | |||
1640 | |||
1641 | /** | ||
1642 | * @brief Delete storage of peers that was created with #initialise_peers () | ||
1643 | * | ||
1644 | * @param sub Sub for which the storage is deleted | ||
1645 | */ | ||
1646 | static void | ||
1647 | peers_terminate (struct Sub *sub) | ||
1648 | { | ||
1649 | if (GNUNET_SYSERR == | ||
1650 | GNUNET_CONTAINER_multipeermap_iterate (sub->peer_map, | ||
1651 | &peermap_clear_iterator, | ||
1652 | sub)) | ||
1653 | { | ||
1654 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
1655 | "Iteration destroying peers was aborted.\n"); | ||
1656 | } | ||
1657 | GNUNET_CONTAINER_multipeermap_destroy (sub->peer_map); | ||
1658 | sub->peer_map = NULL; | ||
1659 | store_valid_peers (sub); | ||
1660 | GNUNET_free (sub->filename_valid_peers); | ||
1661 | sub->filename_valid_peers = NULL; | ||
1662 | GNUNET_CONTAINER_multipeermap_destroy (sub->valid_peers); | ||
1663 | sub->valid_peers = NULL; | ||
1664 | } | ||
1665 | |||
1666 | |||
1667 | /** | ||
1668 | * Iterator over #valid_peers hash map entries. | ||
1669 | * | ||
1670 | * @param cls Closure that contains iterator function and closure | ||
1671 | * @param peer current peer id | ||
1672 | * @param value value in the hash map - unused | ||
1673 | * @return #GNUNET_YES if we should continue to | ||
1674 | * iterate, | ||
1675 | * #GNUNET_NO if not. | ||
1676 | */ | ||
1677 | static int | ||
1678 | valid_peer_iterator (void *cls, | ||
1679 | const struct GNUNET_PeerIdentity *peer, | ||
1680 | void *value) | ||
1681 | { | ||
1682 | struct PeersIteratorCls *it_cls = cls; | ||
1683 | |||
1684 | (void) value; | ||
1685 | |||
1686 | return it_cls->iterator (it_cls->cls, peer); | ||
1687 | } | ||
1688 | |||
1689 | |||
1690 | /** | ||
1691 | * @brief Get all currently known, valid peer ids. | ||
1692 | * | ||
1693 | * @param valid_peers Peer map containing the valid peers in question | ||
1694 | * @param iterator function to call on each peer id | ||
1695 | * @param it_cls extra argument to @a iterator | ||
1696 | * @return the number of key value pairs processed, | ||
1697 | * #GNUNET_SYSERR if it aborted iteration | ||
1698 | */ | ||
1699 | static int | ||
1700 | get_valid_peers (struct GNUNET_CONTAINER_MultiPeerMap *valid_peers, | ||
1701 | PeersIterator iterator, | ||
1702 | void *it_cls) | ||
1703 | { | ||
1704 | struct PeersIteratorCls *cls; | ||
1705 | int ret; | ||
1706 | |||
1707 | cls = GNUNET_new (struct PeersIteratorCls); | ||
1708 | cls->iterator = iterator; | ||
1709 | cls->cls = it_cls; | ||
1710 | ret = GNUNET_CONTAINER_multipeermap_iterate (valid_peers, | ||
1711 | valid_peer_iterator, | ||
1712 | cls); | ||
1713 | GNUNET_free (cls); | ||
1714 | return ret; | ||
1715 | } | ||
1716 | |||
1717 | |||
1718 | /** | ||
1719 | * @brief Add peer to known peers. | ||
1720 | * | ||
1721 | * This function is called on new peer_ids from 'external' sources | ||
1722 | * (client seed, cadet get_peers(), ...) | ||
1723 | * | ||
1724 | * @param sub Sub with the peer map that the @a peer will be added to | ||
1725 | * @param peer the new #GNUNET_PeerIdentity | ||
1726 | * | ||
1727 | * @return #GNUNET_YES if peer was inserted | ||
1728 | * #GNUNET_NO otherwise | ||
1729 | */ | ||
1730 | static int | ||
1731 | insert_peer (struct Sub *sub, | ||
1732 | const struct GNUNET_PeerIdentity *peer) | ||
1733 | { | ||
1734 | if (GNUNET_YES == check_peer_known (sub->peer_map, peer)) | ||
1735 | { | ||
1736 | return GNUNET_NO; /* We already know this peer - nothing to do */ | ||
1737 | } | ||
1738 | (void) create_peer_ctx (sub, peer); | ||
1739 | return GNUNET_YES; | ||
1740 | } | ||
1741 | |||
1742 | |||
1743 | /** | ||
1744 | * @brief Check whether flags on a peer are set. | ||
1745 | * | ||
1746 | * @param peer_map Peer map that is expected to contain the @a peer | ||
1747 | * @param peer the peer to check the flag of | ||
1748 | * @param flags the flags to check | ||
1749 | * | ||
1750 | * @return #GNUNET_SYSERR if peer is not known | ||
1751 | * #GNUNET_YES if all given flags are set | ||
1752 | * #GNUNET_NO otherwise | ||
1753 | */ | ||
1754 | static int | ||
1755 | check_peer_flag (const struct GNUNET_CONTAINER_MultiPeerMap *peer_map, | ||
1756 | const struct GNUNET_PeerIdentity *peer, | ||
1757 | enum Peers_PeerFlags flags) | ||
1758 | { | ||
1759 | struct PeerContext *peer_ctx; | ||
1760 | |||
1761 | if (GNUNET_NO == check_peer_known (peer_map, peer)) | ||
1762 | { | ||
1763 | return GNUNET_SYSERR; | ||
1764 | } | ||
1765 | peer_ctx = get_peer_ctx (peer_map, peer); | ||
1766 | return check_peer_flag_set (peer_ctx, flags); | ||
1767 | } | ||
1768 | |||
1769 | |||
1770 | /** | ||
1771 | * @brief Try connecting to a peer to see whether it is online | ||
1772 | * | ||
1773 | * If not known yet, insert into known peers | ||
1774 | * | ||
1775 | * @param sub Sub which would contain the @a peer | ||
1776 | * @param peer the peer whose online is to be checked | ||
1777 | * @return #GNUNET_YES if the check was issued | ||
1778 | * #GNUNET_NO otherwise | ||
1779 | */ | ||
1780 | static int | ||
1781 | issue_peer_online_check (struct Sub *sub, | ||
1782 | const struct GNUNET_PeerIdentity *peer) | ||
1783 | { | ||
1784 | struct PeerContext *peer_ctx; | ||
1785 | |||
1786 | (void) insert_peer (sub, peer); // TODO even needed? | ||
1787 | peer_ctx = get_peer_ctx (sub->peer_map, peer); | ||
1788 | if ((GNUNET_NO == check_peer_flag (sub->peer_map, peer, Peers_ONLINE)) && | ||
1789 | (NULL == peer_ctx->online_check_pending)) | ||
1790 | { | ||
1791 | check_peer_online (peer_ctx); | ||
1792 | return GNUNET_YES; | ||
1793 | } | ||
1794 | return GNUNET_NO; | ||
1795 | } | ||
1796 | |||
1797 | |||
1798 | /** | ||
1799 | * @brief Check if peer is removable. | ||
1800 | * | ||
1801 | * Check if | ||
1802 | * - a recv channel exists | ||
1803 | * - there are pending messages | ||
1804 | * - there is no pending pull reply | ||
1805 | * | ||
1806 | * @param peer_ctx Context of the peer in question | ||
1807 | * @return #GNUNET_YES if peer is removable | ||
1808 | * #GNUNET_NO if peer is NOT removable | ||
1809 | * #GNUNET_SYSERR if peer is not known | ||
1810 | */ | ||
1811 | static int | ||
1812 | check_removable (const struct PeerContext *peer_ctx) | ||
1813 | { | ||
1814 | if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains ( | ||
1815 | peer_ctx->sub->peer_map, | ||
1816 | &peer_ctx->peer_id)) | ||
1817 | { | ||
1818 | return GNUNET_SYSERR; | ||
1819 | } | ||
1820 | |||
1821 | if ((NULL != peer_ctx->recv_channel_ctx) || | ||
1822 | (NULL != peer_ctx->pending_messages_head) || | ||
1823 | (GNUNET_YES == check_peer_flag_set (peer_ctx, Peers_PULL_REPLY_PENDING))) | ||
1824 | { | ||
1825 | return GNUNET_NO; | ||
1826 | } | ||
1827 | return GNUNET_YES; | ||
1828 | } | ||
1829 | |||
1830 | |||
1831 | /** | ||
1832 | * @brief Check whether @a peer is actually a peer. | ||
1833 | * | ||
1834 | * A valid peer is a peer that we know exists eg. we were connected to once. | ||
1835 | * | ||
1836 | * @param valid_peers Peer map that would contain the @a peer | ||
1837 | * @param peer peer in question | ||
1838 | * | ||
1839 | * @return #GNUNET_YES if peer is valid | ||
1840 | * #GNUNET_NO if peer is not valid | ||
1841 | */ | ||
1842 | static int | ||
1843 | check_peer_valid (const struct GNUNET_CONTAINER_MultiPeerMap *valid_peers, | ||
1844 | const struct GNUNET_PeerIdentity *peer) | ||
1845 | { | ||
1846 | return GNUNET_CONTAINER_multipeermap_contains (valid_peers, peer); | ||
1847 | } | ||
1848 | |||
1849 | |||
1850 | /** | ||
1851 | * @brief Indicate that we want to send to the other peer | ||
1852 | * | ||
1853 | * This establishes a sending channel | ||
1854 | * | ||
1855 | * @param peer_ctx Context of the target peer | ||
1856 | */ | ||
1857 | static void | ||
1858 | indicate_sending_intention (struct PeerContext *peer_ctx) | ||
1859 | { | ||
1860 | GNUNET_assert (GNUNET_YES == check_peer_known (peer_ctx->sub->peer_map, | ||
1861 | &peer_ctx->peer_id)); | ||
1862 | (void) get_channel (peer_ctx); | ||
1863 | } | ||
1864 | |||
1865 | |||
1866 | /** | ||
1867 | * @brief Check whether other peer has the intention to send/opened channel | ||
1868 | * towars us | ||
1869 | * | ||
1870 | * @param peer_ctx Context of the peer in question | ||
1871 | * | ||
1872 | * @return #GNUNET_YES if peer has the intention to send | ||
1873 | * #GNUNET_NO otherwise | ||
1874 | */ | ||
1875 | static int | ||
1876 | check_peer_send_intention (const struct PeerContext *peer_ctx) | ||
1877 | { | ||
1878 | if (NULL != peer_ctx->recv_channel_ctx) | ||
1879 | { | ||
1880 | return GNUNET_YES; | ||
1881 | } | ||
1882 | return GNUNET_NO; | ||
1883 | } | ||
1884 | |||
1885 | |||
1886 | /** | ||
1887 | * Handle the channel a peer opens to us. | ||
1888 | * | ||
1889 | * @param cls The closure - Sub | ||
1890 | * @param channel The channel the peer wants to establish | ||
1891 | * @param initiator The peer's peer ID | ||
1892 | * | ||
1893 | * @return initial channel context for the channel | ||
1894 | * (can be NULL -- that's not an error) | ||
1895 | */ | ||
1896 | static void * | ||
1897 | handle_inbound_channel (void *cls, | ||
1898 | struct GNUNET_CADET_Channel *channel, | ||
1899 | const struct GNUNET_PeerIdentity *initiator) | ||
1900 | { | ||
1901 | struct PeerContext *peer_ctx; | ||
1902 | struct ChannelCtx *channel_ctx; | ||
1903 | struct Sub *sub = cls; | ||
1904 | |||
1905 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1906 | "New channel was established to us (Peer %s).\n", | ||
1907 | GNUNET_i2s (initiator)); | ||
1908 | GNUNET_assert (NULL != channel); /* according to cadet API */ | ||
1909 | /* Make sure we 'know' about this peer */ | ||
1910 | peer_ctx = create_or_get_peer_ctx (sub, initiator); | ||
1911 | set_peer_online (peer_ctx); | ||
1912 | (void) add_valid_peer (&peer_ctx->peer_id, peer_ctx->sub->valid_peers); | ||
1913 | channel_ctx = add_channel_ctx (peer_ctx); | ||
1914 | channel_ctx->channel = channel; | ||
1915 | /* We only accept one incoming channel per peer */ | ||
1916 | if (GNUNET_YES == check_peer_send_intention (get_peer_ctx (sub->peer_map, | ||
1917 | initiator))) | ||
1918 | { | ||
1919 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
1920 | "Already got one receive channel. Destroying old one.\n"); | ||
1921 | GNUNET_break_op (0); | ||
1922 | destroy_channel (peer_ctx->recv_channel_ctx); | ||
1923 | peer_ctx->recv_channel_ctx = channel_ctx; | ||
1924 | /* return the channel context */ | ||
1925 | return channel_ctx; | ||
1926 | } | ||
1927 | peer_ctx->recv_channel_ctx = channel_ctx; | ||
1928 | return channel_ctx; | ||
1929 | } | ||
1930 | |||
1931 | |||
1932 | /** | ||
1933 | * @brief Check whether a sending channel towards the given peer exists | ||
1934 | * | ||
1935 | * @param peer_ctx Context of the peer in question | ||
1936 | * | ||
1937 | * @return #GNUNET_YES if a sending channel towards that peer exists | ||
1938 | * #GNUNET_NO otherwise | ||
1939 | */ | ||
1940 | static int | ||
1941 | check_sending_channel_exists (const struct PeerContext *peer_ctx) | ||
1942 | { | ||
1943 | if (GNUNET_NO == check_peer_known (peer_ctx->sub->peer_map, | ||
1944 | &peer_ctx->peer_id)) | ||
1945 | { /* If no such peer exists, there is no channel */ | ||
1946 | return GNUNET_NO; | ||
1947 | } | ||
1948 | if (NULL == peer_ctx->send_channel_ctx) | ||
1949 | { | ||
1950 | return GNUNET_NO; | ||
1951 | } | ||
1952 | return GNUNET_YES; | ||
1953 | } | ||
1954 | |||
1955 | |||
1956 | /** | ||
1957 | * @brief Destroy the send channel of a peer e.g. stop indicating a sending | ||
1958 | * intention to another peer | ||
1959 | * | ||
1960 | * @param peer_ctx Context to the peer | ||
1961 | * @return #GNUNET_YES if channel was destroyed | ||
1962 | * #GNUNET_NO otherwise | ||
1963 | */ | ||
1964 | static int | ||
1965 | destroy_sending_channel (struct PeerContext *peer_ctx) | ||
1966 | { | ||
1967 | if (GNUNET_NO == check_peer_known (peer_ctx->sub->peer_map, | ||
1968 | &peer_ctx->peer_id)) | ||
1969 | { | ||
1970 | return GNUNET_NO; | ||
1971 | } | ||
1972 | if (NULL != peer_ctx->send_channel_ctx) | ||
1973 | { | ||
1974 | destroy_channel (peer_ctx->send_channel_ctx); | ||
1975 | (void) check_connected (peer_ctx); | ||
1976 | return GNUNET_YES; | ||
1977 | } | ||
1978 | return GNUNET_NO; | ||
1979 | } | ||
1980 | |||
1981 | |||
1982 | /** | ||
1983 | * @brief Send a message to another peer. | ||
1984 | * | ||
1985 | * Keeps track about pending messages so they can be properly removed when the | ||
1986 | * peer is destroyed. | ||
1987 | * | ||
1988 | * @param peer_ctx Context of the peer to which the message is to be sent | ||
1989 | * @param ev envelope of the message | ||
1990 | * @param type type of the message | ||
1991 | */ | ||
1992 | static void | ||
1993 | send_message (struct PeerContext *peer_ctx, | ||
1994 | struct GNUNET_MQ_Envelope *ev, | ||
1995 | const char *type) | ||
1996 | { | ||
1997 | struct PendingMessage *pending_msg; | ||
1998 | struct GNUNET_MQ_Handle *mq; | ||
1999 | |||
2000 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2001 | "Sending message to %s of type %s\n", | ||
2002 | GNUNET_i2s (&peer_ctx->peer_id), | ||
2003 | type); | ||
2004 | pending_msg = insert_pending_message (peer_ctx, ev, type); | ||
2005 | mq = get_mq (peer_ctx); | ||
2006 | GNUNET_MQ_notify_sent (ev, | ||
2007 | mq_notify_sent_cb, | ||
2008 | pending_msg); | ||
2009 | GNUNET_MQ_send (mq, ev); | ||
2010 | } | ||
2011 | |||
2012 | |||
2013 | /** | ||
2014 | * @brief Schedule a operation on given peer | ||
2015 | * | ||
2016 | * Avoids scheduling an operation twice. | ||
2017 | * | ||
2018 | * @param peer_ctx Context of the peer for which to schedule the operation | ||
2019 | * @param peer_op the operation to schedule | ||
2020 | * @param cls Closure to @a peer_op | ||
2021 | * | ||
2022 | * @return #GNUNET_YES if the operation was scheduled | ||
2023 | * #GNUNET_NO otherwise | ||
2024 | */ | ||
2025 | static int | ||
2026 | schedule_operation (struct PeerContext *peer_ctx, | ||
2027 | const PeerOp peer_op, | ||
2028 | void *cls) | ||
2029 | { | ||
2030 | struct PeerPendingOp pending_op; | ||
2031 | |||
2032 | GNUNET_assert (GNUNET_YES == check_peer_known (peer_ctx->sub->peer_map, | ||
2033 | &peer_ctx->peer_id)); | ||
2034 | |||
2035 | // TODO if ONLINE execute immediately | ||
2036 | |||
2037 | if (GNUNET_NO == check_operation_scheduled (peer_ctx, peer_op)) | ||
2038 | { | ||
2039 | pending_op.op = peer_op; | ||
2040 | pending_op.op_cls = cls; | ||
2041 | GNUNET_array_append (peer_ctx->pending_ops, | ||
2042 | peer_ctx->num_pending_ops, | ||
2043 | pending_op); | ||
2044 | return GNUNET_YES; | ||
2045 | } | ||
2046 | return GNUNET_NO; | ||
2047 | } | ||
2048 | |||
2049 | |||
2050 | /*********************************************************************** | ||
2051 | * /Old gnunet-service-rps_peers.c | ||
2052 | ***********************************************************************/ | ||
2053 | |||
2054 | |||
2055 | /*********************************************************************** | ||
2056 | * Housekeeping with clients | ||
2057 | ***********************************************************************/ | ||
2058 | |||
2059 | /** | ||
2060 | * Closure used to pass the client and the id to the callback | ||
2061 | * that replies to a client's request | ||
2062 | */ | ||
2063 | struct ReplyCls | ||
2064 | { | ||
2065 | /** | ||
2066 | * DLL | ||
2067 | */ | ||
2068 | struct ReplyCls *next; | ||
2069 | struct ReplyCls *prev; | ||
2070 | |||
2071 | /** | ||
2072 | * The identifier of the request | ||
2073 | */ | ||
2074 | uint32_t id; | ||
2075 | |||
2076 | /** | ||
2077 | * The handle to the request | ||
2078 | */ | ||
2079 | struct RPS_SamplerRequestHandle *req_handle; | ||
2080 | |||
2081 | /** | ||
2082 | * The client handle to send the reply to | ||
2083 | */ | ||
2084 | struct ClientContext *cli_ctx; | ||
2085 | }; | ||
2086 | |||
2087 | |||
2088 | /** | ||
2089 | * Struct used to store the context of a connected client. | ||
2090 | */ | ||
2091 | struct ClientContext | ||
2092 | { | ||
2093 | /** | ||
2094 | * DLL | ||
2095 | */ | ||
2096 | struct ClientContext *next; | ||
2097 | struct ClientContext *prev; | ||
2098 | |||
2099 | /** | ||
2100 | * The message queue to communicate with the client. | ||
2101 | */ | ||
2102 | struct GNUNET_MQ_Handle *mq; | ||
2103 | |||
2104 | /** | ||
2105 | * @brief How many updates this client expects to receive. | ||
2106 | */ | ||
2107 | int64_t view_updates_left; | ||
2108 | |||
2109 | /** | ||
2110 | * @brief Whether this client wants to receive stream updates. | ||
2111 | * Either #GNUNET_YES or #GNUNET_NO | ||
2112 | */ | ||
2113 | int8_t stream_update; | ||
2114 | |||
2115 | /** | ||
2116 | * The client handle to send the reply to | ||
2117 | */ | ||
2118 | struct GNUNET_SERVICE_Client *client; | ||
2119 | |||
2120 | /** | ||
2121 | * The #Sub this context belongs to | ||
2122 | */ | ||
2123 | struct Sub *sub; | ||
2124 | }; | ||
2125 | |||
2126 | /** | ||
2127 | * DLL with all clients currently connected to us | ||
2128 | */ | ||
2129 | struct ClientContext *cli_ctx_head; | ||
2130 | struct ClientContext *cli_ctx_tail; | ||
2131 | |||
2132 | /*********************************************************************** | ||
2133 | * /Housekeeping with clients | ||
2134 | ***********************************************************************/ | ||
2135 | |||
2136 | |||
2137 | /*********************************************************************** | ||
2138 | * Util functions | ||
2139 | ***********************************************************************/ | ||
2140 | |||
2141 | |||
2142 | /** | ||
2143 | * Print peerlist to log. | ||
2144 | */ | ||
2145 | static void | ||
2146 | print_peer_list (struct GNUNET_PeerIdentity *list, | ||
2147 | unsigned int len) | ||
2148 | { | ||
2149 | unsigned int i; | ||
2150 | |||
2151 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
2152 | "Printing peer list of length %u at %p:\n", | ||
2153 | len, | ||
2154 | list); | ||
2155 | for (i = 0; i < len; i++) | ||
2156 | { | ||
2157 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
2158 | "%u. peer: %s\n", | ||
2159 | i, GNUNET_i2s (&list[i])); | ||
2160 | } | ||
2161 | } | ||
2162 | |||
2163 | |||
2164 | /** | ||
2165 | * Remove peer from list. | ||
2166 | */ | ||
2167 | static void | ||
2168 | rem_from_list (struct GNUNET_PeerIdentity **peer_list, | ||
2169 | unsigned int *list_size, | ||
2170 | const struct GNUNET_PeerIdentity *peer) | ||
2171 | { | ||
2172 | unsigned int i; | ||
2173 | struct GNUNET_PeerIdentity *tmp; | ||
2174 | |||
2175 | tmp = *peer_list; | ||
2176 | |||
2177 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
2178 | "Removing peer %s from list at %p\n", | ||
2179 | GNUNET_i2s (peer), | ||
2180 | tmp); | ||
2181 | |||
2182 | for (i = 0; i < *list_size; i++) | ||
2183 | { | ||
2184 | if (0 == GNUNET_memcmp (&tmp[i], peer)) | ||
2185 | { | ||
2186 | if (i < *list_size - 1) | ||
2187 | { /* Not at the last entry -- shift peers left */ | ||
2188 | memmove (&tmp[i], &tmp[i + 1], | ||
2189 | ((*list_size) - i - 1) * sizeof(struct GNUNET_PeerIdentity)); | ||
2190 | } | ||
2191 | /* Remove last entry (should be now useless PeerID) */ | ||
2192 | GNUNET_array_grow (tmp, *list_size, (*list_size) - 1); | ||
2193 | } | ||
2194 | } | ||
2195 | *peer_list = tmp; | ||
2196 | } | ||
2197 | |||
2198 | |||
2199 | /** | ||
2200 | * Insert PeerID in #view | ||
2201 | * | ||
2202 | * Called once we know a peer is online. | ||
2203 | * Implements #PeerOp | ||
2204 | * | ||
2205 | * @return GNUNET_OK if peer was actually inserted | ||
2206 | * GNUNET_NO if peer was not inserted | ||
2207 | */ | ||
2208 | static void | ||
2209 | insert_in_view_op (void *cls, | ||
2210 | const struct GNUNET_PeerIdentity *peer); | ||
2211 | |||
2212 | /** | ||
2213 | * Insert PeerID in #view | ||
2214 | * | ||
2215 | * Called once we know a peer is online. | ||
2216 | * | ||
2217 | * @param sub Sub in with the view to insert in | ||
2218 | * @param peer the peer to insert | ||
2219 | * | ||
2220 | * @return GNUNET_OK if peer was actually inserted | ||
2221 | * GNUNET_NO if peer was not inserted | ||
2222 | */ | ||
2223 | static int | ||
2224 | insert_in_view (struct Sub *sub, | ||
2225 | const struct GNUNET_PeerIdentity *peer) | ||
2226 | { | ||
2227 | struct PeerContext *peer_ctx; | ||
2228 | int online; | ||
2229 | int ret; | ||
2230 | |||
2231 | online = check_peer_flag (sub->peer_map, peer, Peers_ONLINE); | ||
2232 | peer_ctx = get_peer_ctx (sub->peer_map, peer); // TODO indirection needed? | ||
2233 | if ((GNUNET_NO == online) || | ||
2234 | (GNUNET_SYSERR == online)) /* peer is not even known */ | ||
2235 | { | ||
2236 | (void) issue_peer_online_check (sub, peer); | ||
2237 | (void) schedule_operation (peer_ctx, insert_in_view_op, sub); | ||
2238 | return GNUNET_NO; | ||
2239 | } | ||
2240 | /* Open channel towards peer to keep connection open */ | ||
2241 | indicate_sending_intention (peer_ctx); | ||
2242 | ret = View_put (sub->view, peer); | ||
2243 | if (peer_ctx->sub == msub) | ||
2244 | { | ||
2245 | GNUNET_STATISTICS_set (stats, | ||
2246 | "view size", | ||
2247 | View_size (peer_ctx->sub->view), | ||
2248 | GNUNET_NO); | ||
2249 | } | ||
2250 | return ret; | ||
2251 | } | ||
2252 | |||
2253 | |||
2254 | /** | ||
2255 | * @brief Send view to client | ||
2256 | * | ||
2257 | * @param cli_ctx the context of the client | ||
2258 | * @param view_array the peerids of the view as array (can be empty) | ||
2259 | * @param view_size the size of the view array (can be 0) | ||
2260 | */ | ||
2261 | static void | ||
2262 | send_view (const struct ClientContext *cli_ctx, | ||
2263 | const struct GNUNET_PeerIdentity *view_array, | ||
2264 | uint64_t view_size) | ||
2265 | { | ||
2266 | struct GNUNET_MQ_Envelope *ev; | ||
2267 | struct GNUNET_RPS_CS_DEBUG_ViewReply *out_msg; | ||
2268 | struct Sub *sub; | ||
2269 | |||
2270 | if (NULL == view_array) | ||
2271 | { | ||
2272 | if (NULL == cli_ctx->sub) | ||
2273 | sub = msub; | ||
2274 | else | ||
2275 | sub = cli_ctx->sub; | ||
2276 | view_size = View_size (sub->view); | ||
2277 | view_array = View_get_as_array (sub->view); | ||
2278 | } | ||
2279 | |||
2280 | ev = GNUNET_MQ_msg_extra (out_msg, | ||
2281 | view_size * sizeof(struct GNUNET_PeerIdentity), | ||
2282 | GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REPLY); | ||
2283 | out_msg->num_peers = htonl (view_size); | ||
2284 | |||
2285 | GNUNET_memcpy (&out_msg[1], | ||
2286 | view_array, | ||
2287 | view_size * sizeof(struct GNUNET_PeerIdentity)); | ||
2288 | GNUNET_MQ_send (cli_ctx->mq, ev); | ||
2289 | } | ||
2290 | |||
2291 | |||
2292 | /** | ||
2293 | * @brief Send peer from biased stream to client. | ||
2294 | * | ||
2295 | * TODO merge with send_view, parameterise | ||
2296 | * | ||
2297 | * @param cli_ctx the context of the client | ||
2298 | * @param view_array the peerids of the view as array (can be empty) | ||
2299 | * @param view_size the size of the view array (can be 0) | ||
2300 | */ | ||
2301 | static void | ||
2302 | send_stream_peers (const struct ClientContext *cli_ctx, | ||
2303 | uint64_t num_peers, | ||
2304 | const struct GNUNET_PeerIdentity *peers) | ||
2305 | { | ||
2306 | struct GNUNET_MQ_Envelope *ev; | ||
2307 | struct GNUNET_RPS_CS_DEBUG_StreamReply *out_msg; | ||
2308 | |||
2309 | GNUNET_assert (NULL != peers); | ||
2310 | |||
2311 | ev = GNUNET_MQ_msg_extra (out_msg, | ||
2312 | num_peers * sizeof(struct GNUNET_PeerIdentity), | ||
2313 | GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_REPLY); | ||
2314 | out_msg->num_peers = htonl (num_peers); | ||
2315 | |||
2316 | GNUNET_memcpy (&out_msg[1], | ||
2317 | peers, | ||
2318 | num_peers * sizeof(struct GNUNET_PeerIdentity)); | ||
2319 | GNUNET_MQ_send (cli_ctx->mq, ev); | ||
2320 | } | ||
2321 | |||
2322 | |||
2323 | /** | ||
2324 | * @brief sends updates to clients that are interested | ||
2325 | * | ||
2326 | * @param sub Sub for which to notify clients | ||
2327 | */ | ||
2328 | static void | ||
2329 | clients_notify_view_update (const struct Sub *sub) | ||
2330 | { | ||
2331 | struct ClientContext *cli_ctx_iter; | ||
2332 | uint64_t num_peers; | ||
2333 | const struct GNUNET_PeerIdentity *view_array; | ||
2334 | |||
2335 | num_peers = View_size (sub->view); | ||
2336 | view_array = View_get_as_array (sub->view); | ||
2337 | /* check size of view is small enough */ | ||
2338 | if (GNUNET_MAX_MESSAGE_SIZE < num_peers) | ||
2339 | { | ||
2340 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
2341 | "View is too big to send\n"); | ||
2342 | return; | ||
2343 | } | ||
2344 | |||
2345 | for (cli_ctx_iter = cli_ctx_head; | ||
2346 | NULL != cli_ctx_iter; | ||
2347 | cli_ctx_iter = cli_ctx_iter->next) | ||
2348 | { | ||
2349 | if (1 < cli_ctx_iter->view_updates_left) | ||
2350 | { | ||
2351 | /* Client wants to receive limited amount of updates */ | ||
2352 | cli_ctx_iter->view_updates_left -= 1; | ||
2353 | } | ||
2354 | else if (1 == cli_ctx_iter->view_updates_left) | ||
2355 | { | ||
2356 | /* Last update of view for client */ | ||
2357 | cli_ctx_iter->view_updates_left = -1; | ||
2358 | } | ||
2359 | else if (0 > cli_ctx_iter->view_updates_left) | ||
2360 | { | ||
2361 | /* Client is not interested in updates */ | ||
2362 | continue; | ||
2363 | } | ||
2364 | /* else _updates_left == 0 - infinite amount of updates */ | ||
2365 | |||
2366 | /* send view */ | ||
2367 | send_view (cli_ctx_iter, view_array, num_peers); | ||
2368 | } | ||
2369 | } | ||
2370 | |||
2371 | |||
2372 | /** | ||
2373 | * @brief sends updates to clients that are interested | ||
2374 | * | ||
2375 | * @param num_peers Number of peers to send | ||
2376 | * @param peers the array of peers to send | ||
2377 | */ | ||
2378 | static void | ||
2379 | clients_notify_stream_peer (const struct Sub *sub, | ||
2380 | uint64_t num_peers, | ||
2381 | const struct GNUNET_PeerIdentity *peers) | ||
2382 | // TODO enum StreamPeerSource) | ||
2383 | { | ||
2384 | struct ClientContext *cli_ctx_iter; | ||
2385 | |||
2386 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
2387 | "Got peer (%s) from biased stream - update all clients\n", | ||
2388 | GNUNET_i2s (peers)); | ||
2389 | |||
2390 | for (cli_ctx_iter = cli_ctx_head; | ||
2391 | NULL != cli_ctx_iter; | ||
2392 | cli_ctx_iter = cli_ctx_iter->next) | ||
2393 | { | ||
2394 | if ((GNUNET_YES == cli_ctx_iter->stream_update) && | ||
2395 | ((sub == cli_ctx_iter->sub) || (sub == msub) )) | ||
2396 | { | ||
2397 | send_stream_peers (cli_ctx_iter, num_peers, peers); | ||
2398 | } | ||
2399 | } | ||
2400 | } | ||
2401 | |||
2402 | |||
2403 | /** | ||
2404 | * Put random peer from sampler into the view as history update. | ||
2405 | * | ||
2406 | * @param ids Array of Peers to insert into view | ||
2407 | * @param num_peers Number of peers to insert | ||
2408 | * @param cls Closure - The Sub for which this is to be done | ||
2409 | */ | ||
2410 | static void | ||
2411 | hist_update (const struct GNUNET_PeerIdentity *ids, | ||
2412 | uint32_t num_peers, | ||
2413 | void *cls) | ||
2414 | { | ||
2415 | unsigned int i; | ||
2416 | struct Sub *sub = cls; | ||
2417 | |||
2418 | for (i = 0; i < num_peers; i++) | ||
2419 | { | ||
2420 | int inserted; | ||
2421 | if (GNUNET_YES != check_peer_known (sub->peer_map, &ids[i])) | ||
2422 | { | ||
2423 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
2424 | "Peer in history update not known!\n"); | ||
2425 | continue; | ||
2426 | } | ||
2427 | inserted = insert_in_view (sub, &ids[i]); | ||
2428 | if (GNUNET_OK == inserted) | ||
2429 | { | ||
2430 | clients_notify_stream_peer (sub, 1, &ids[i]); | ||
2431 | } | ||
2432 | #ifdef TO_FILE_FULL | ||
2433 | to_file (sub->file_name_view_log, | ||
2434 | "+%s\t(history)", | ||
2435 | GNUNET_i2s_full (ids)); | ||
2436 | #endif /* TO_FILE_FULL */ | ||
2437 | } | ||
2438 | clients_notify_view_update (sub); | ||
2439 | } | ||
2440 | |||
2441 | |||
2442 | /** | ||
2443 | * Wrapper around #RPS_sampler_resize() | ||
2444 | * | ||
2445 | * If we do not have enough sampler elements, double current sampler size | ||
2446 | * If we have more than enough sampler elements, halv current sampler size | ||
2447 | * | ||
2448 | * @param sampler The sampler to resize | ||
2449 | * @param new_size New size to which to resize | ||
2450 | */ | ||
2451 | static void | ||
2452 | resize_wrapper (struct RPS_Sampler *sampler, uint32_t new_size) | ||
2453 | { | ||
2454 | unsigned int sampler_size; | ||
2455 | |||
2456 | // TODO statistics | ||
2457 | // TODO respect the min, max | ||
2458 | sampler_size = RPS_sampler_get_size (sampler); | ||
2459 | if (sampler_size > new_size * 4) | ||
2460 | { /* Shrinking */ | ||
2461 | RPS_sampler_resize (sampler, sampler_size / 2); | ||
2462 | } | ||
2463 | else if (sampler_size < new_size) | ||
2464 | { /* Growing */ | ||
2465 | RPS_sampler_resize (sampler, sampler_size * 2); | ||
2466 | } | ||
2467 | LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size); | ||
2468 | } | ||
2469 | |||
2470 | |||
2471 | #if ENABLE_MALICIOUS | ||
2472 | /** | ||
2473 | * Add all peers in @a peer_array to @a peer_map used as set. | ||
2474 | * | ||
2475 | * @param peer_array array containing the peers | ||
2476 | * @param num_peers number of peers in @peer_array | ||
2477 | * @param peer_map the peermap to use as set | ||
2478 | */ | ||
2479 | static void | ||
2480 | add_peer_array_to_set (const struct GNUNET_PeerIdentity *peer_array, | ||
2481 | unsigned int num_peers, | ||
2482 | struct GNUNET_CONTAINER_MultiPeerMap *peer_map) | ||
2483 | { | ||
2484 | unsigned int i; | ||
2485 | |||
2486 | if (NULL == peer_map) | ||
2487 | { | ||
2488 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
2489 | "Trying to add peers to non-existing peermap.\n"); | ||
2490 | return; | ||
2491 | } | ||
2492 | |||
2493 | for (i = 0; i < num_peers; i++) | ||
2494 | { | ||
2495 | GNUNET_CONTAINER_multipeermap_put (peer_map, | ||
2496 | &peer_array[i], | ||
2497 | NULL, | ||
2498 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); | ||
2499 | if (msub->peer_map == peer_map) | ||
2500 | { | ||
2501 | GNUNET_STATISTICS_set (stats, | ||
2502 | "# known peers", | ||
2503 | GNUNET_CONTAINER_multipeermap_size (peer_map), | ||
2504 | GNUNET_NO); | ||
2505 | } | ||
2506 | } | ||
2507 | } | ||
2508 | |||
2509 | |||
2510 | #endif /* ENABLE_MALICIOUS */ | ||
2511 | |||
2512 | |||
2513 | /** | ||
2514 | * Send a PULL REPLY to @a peer_id | ||
2515 | * | ||
2516 | * @param peer_ctx Context of the peer to send the reply to | ||
2517 | * @param peer_ids the peers to send to @a peer_id | ||
2518 | * @param num_peer_ids the number of peers to send to @a peer_id | ||
2519 | */ | ||
2520 | static void | ||
2521 | send_pull_reply (struct PeerContext *peer_ctx, | ||
2522 | const struct GNUNET_PeerIdentity *peer_ids, | ||
2523 | unsigned int num_peer_ids) | ||
2524 | { | ||
2525 | uint32_t send_size; | ||
2526 | struct GNUNET_MQ_Envelope *ev; | ||
2527 | struct GNUNET_RPS_P2P_PullReplyMessage *out_msg; | ||
2528 | |||
2529 | /* Compute actual size */ | ||
2530 | send_size = sizeof(struct GNUNET_RPS_P2P_PullReplyMessage) | ||
2531 | + num_peer_ids * sizeof(struct GNUNET_PeerIdentity); | ||
2532 | |||
2533 | if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size) | ||
2534 | /* Compute number of peers to send | ||
2535 | * If too long, simply truncate */ | ||
2536 | // TODO select random ones via permutation | ||
2537 | // or even better: do good protocol design | ||
2538 | send_size = | ||
2539 | (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE | ||
2540 | - sizeof(struct GNUNET_RPS_P2P_PullReplyMessage)) | ||
2541 | / sizeof(struct GNUNET_PeerIdentity); | ||
2542 | else | ||
2543 | send_size = num_peer_ids; | ||
2544 | |||
2545 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
2546 | "Going to send PULL REPLY with %u peers to %s\n", | ||
2547 | send_size, GNUNET_i2s (&peer_ctx->peer_id)); | ||
2548 | |||
2549 | ev = GNUNET_MQ_msg_extra (out_msg, | ||
2550 | send_size * sizeof(struct GNUNET_PeerIdentity), | ||
2551 | GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY); | ||
2552 | out_msg->num_peers = htonl (send_size); | ||
2553 | GNUNET_memcpy (&out_msg[1], peer_ids, | ||
2554 | send_size * sizeof(struct GNUNET_PeerIdentity)); | ||
2555 | |||
2556 | send_message (peer_ctx, ev, "PULL REPLY"); | ||
2557 | if (peer_ctx->sub == msub) | ||
2558 | { | ||
2559 | GNUNET_STATISTICS_update (stats, "# pull reply send issued", 1, GNUNET_NO); | ||
2560 | } | ||
2561 | // TODO check with send intention: as send_channel is used/opened we indicate | ||
2562 | // a sending intention without intending it. | ||
2563 | // -> clean peer afterwards? | ||
2564 | // -> use recv_channel? | ||
2565 | } | ||
2566 | |||
2567 | |||
2568 | /** | ||
2569 | * Insert PeerID in #pull_map | ||
2570 | * | ||
2571 | * Called once we know a peer is online. | ||
2572 | * | ||
2573 | * @param cls Closure - Sub with the pull map to insert into | ||
2574 | * @param peer Peer to insert | ||
2575 | */ | ||
2576 | static void | ||
2577 | insert_in_pull_map (void *cls, | ||
2578 | const struct GNUNET_PeerIdentity *peer) | ||
2579 | { | ||
2580 | struct Sub *sub = cls; | ||
2581 | |||
2582 | CustomPeerMap_put (sub->pull_map, peer); | ||
2583 | } | ||
2584 | |||
2585 | |||
2586 | /** | ||
2587 | * Insert PeerID in #view | ||
2588 | * | ||
2589 | * Called once we know a peer is online. | ||
2590 | * Implements #PeerOp | ||
2591 | * | ||
2592 | * @param cls Closure - Sub with view to insert peer into | ||
2593 | * @param peer the peer to insert | ||
2594 | */ | ||
2595 | static void | ||
2596 | insert_in_view_op (void *cls, | ||
2597 | const struct GNUNET_PeerIdentity *peer) | ||
2598 | { | ||
2599 | struct Sub *sub = cls; | ||
2600 | int inserted; | ||
2601 | |||
2602 | inserted = insert_in_view (sub, peer); | ||
2603 | if (GNUNET_OK == inserted) | ||
2604 | { | ||
2605 | clients_notify_stream_peer (sub, 1, peer); | ||
2606 | } | ||
2607 | } | ||
2608 | |||
2609 | |||
2610 | /** | ||
2611 | * Update sampler with given PeerID. | ||
2612 | * Implements #PeerOp | ||
2613 | * | ||
2614 | * @param cls Closure - Sub containing the sampler to insert into | ||
2615 | * @param peer Peer to insert | ||
2616 | */ | ||
2617 | static void | ||
2618 | insert_in_sampler (void *cls, | ||
2619 | const struct GNUNET_PeerIdentity *peer) | ||
2620 | { | ||
2621 | struct Sub *sub = cls; | ||
2622 | |||
2623 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
2624 | "Updating samplers with peer %s from insert_in_sampler()\n", | ||
2625 | GNUNET_i2s (peer)); | ||
2626 | RPS_sampler_update (sub->sampler, peer); | ||
2627 | if (0 < RPS_sampler_count_id (sub->sampler, peer)) | ||
2628 | { | ||
2629 | /* Make sure we 'know' about this peer */ | ||
2630 | (void) issue_peer_online_check (sub, peer); | ||
2631 | /* Establish a channel towards that peer to indicate we are going to send | ||
2632 | * messages to it */ | ||
2633 | // indicate_sending_intention (peer); | ||
2634 | } | ||
2635 | if (sub == msub) | ||
2636 | { | ||
2637 | GNUNET_STATISTICS_update (stats, | ||
2638 | "# observed peers in gossip", | ||
2639 | 1, | ||
2640 | GNUNET_NO); | ||
2641 | } | ||
2642 | #ifdef TO_FILE | ||
2643 | sub->num_observed_peers++; | ||
2644 | (void) GNUNET_CONTAINER_multipeermap_put | ||
2645 | (sub->observed_unique_peers, | ||
2646 | peer, | ||
2647 | NULL, | ||
2648 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); | ||
2649 | uint32_t num_observed_unique_peers = | ||
2650 | GNUNET_CONTAINER_multipeermap_size (sub->observed_unique_peers); | ||
2651 | GNUNET_STATISTICS_set (stats, | ||
2652 | "# unique peers in gossip", | ||
2653 | num_observed_unique_peers, | ||
2654 | GNUNET_NO); | ||
2655 | #ifdef TO_FILE_FULL | ||
2656 | to_file (sub->file_name_observed_log, | ||
2657 | "%" PRIu32 " %" PRIu32 " %f\n", | ||
2658 | sub->num_observed_peers, | ||
2659 | num_observed_unique_peers, | ||
2660 | 1.0 * num_observed_unique_peers / sub->num_observed_peers) | ||
2661 | #endif /* TO_FILE_FULL */ | ||
2662 | #endif /* TO_FILE */ | ||
2663 | } | ||
2664 | |||
2665 | |||
2666 | /** | ||
2667 | * @brief This is called on peers from external sources (cadet, peerinfo, ...) | ||
2668 | * If the peer is not known, online check is issued and it is | ||
2669 | * scheduled to be inserted in sampler and view. | ||
2670 | * | ||
2671 | * "External sources" refer to every source except the gossip. | ||
2672 | * | ||
2673 | * @param sub Sub for which @a peer was received | ||
2674 | * @param peer peer to insert/peer received | ||
2675 | */ | ||
2676 | static void | ||
2677 | got_peer (struct Sub *sub, | ||
2678 | const struct GNUNET_PeerIdentity *peer) | ||
2679 | { | ||
2680 | /* If we did not know this peer already, insert it into sampler and view */ | ||
2681 | if (GNUNET_YES == issue_peer_online_check (sub, peer)) | ||
2682 | { | ||
2683 | schedule_operation (get_peer_ctx (sub->peer_map, peer), | ||
2684 | &insert_in_sampler, sub); | ||
2685 | schedule_operation (get_peer_ctx (sub->peer_map, peer), | ||
2686 | &insert_in_view_op, sub); | ||
2687 | } | ||
2688 | if (sub == msub) | ||
2689 | { | ||
2690 | GNUNET_STATISTICS_update (stats, | ||
2691 | "# learnd peers", | ||
2692 | 1, | ||
2693 | GNUNET_NO); | ||
2694 | } | ||
2695 | } | ||
2696 | |||
2697 | |||
2698 | /** | ||
2699 | * @brief Checks if there is a sending channel and if it is needed | ||
2700 | * | ||
2701 | * @param peer_ctx Context of the peer to check | ||
2702 | * @return GNUNET_YES if sending channel exists and is still needed | ||
2703 | * GNUNET_NO otherwise | ||
2704 | */ | ||
2705 | static int | ||
2706 | check_sending_channel_needed (const struct PeerContext *peer_ctx) | ||
2707 | { | ||
2708 | /* struct GNUNET_CADET_Channel *channel; */ | ||
2709 | if (GNUNET_NO == check_peer_known (peer_ctx->sub->peer_map, | ||
2710 | &peer_ctx->peer_id)) | ||
2711 | { | ||
2712 | return GNUNET_NO; | ||
2713 | } | ||
2714 | if (GNUNET_YES == check_sending_channel_exists (peer_ctx)) | ||
2715 | { | ||
2716 | if ((0 < RPS_sampler_count_id (peer_ctx->sub->sampler, | ||
2717 | &peer_ctx->peer_id)) || | ||
2718 | (GNUNET_YES == View_contains_peer (peer_ctx->sub->view, | ||
2719 | &peer_ctx->peer_id)) || | ||
2720 | (GNUNET_YES == CustomPeerMap_contains_peer (peer_ctx->sub->push_map, | ||
2721 | &peer_ctx->peer_id)) || | ||
2722 | (GNUNET_YES == CustomPeerMap_contains_peer (peer_ctx->sub->pull_map, | ||
2723 | &peer_ctx->peer_id)) || | ||
2724 | (GNUNET_YES == check_peer_flag (peer_ctx->sub->peer_map, | ||
2725 | &peer_ctx->peer_id, | ||
2726 | Peers_PULL_REPLY_PENDING))) | ||
2727 | { /* If we want to keep the connection to peer open */ | ||
2728 | return GNUNET_YES; | ||
2729 | } | ||
2730 | return GNUNET_NO; | ||
2731 | } | ||
2732 | return GNUNET_NO; | ||
2733 | } | ||
2734 | |||
2735 | |||
2736 | /** | ||
2737 | * @brief remove peer from our knowledge, the view, push and pull maps and | ||
2738 | * samplers. | ||
2739 | * | ||
2740 | * @param sub Sub with the data structures the peer is to be removed from | ||
2741 | * @param peer the peer to remove | ||
2742 | */ | ||
2743 | static void | ||
2744 | remove_peer (struct Sub *sub, | ||
2745 | const struct GNUNET_PeerIdentity *peer) | ||
2746 | { | ||
2747 | (void) View_remove_peer (sub->view, | ||
2748 | peer); | ||
2749 | CustomPeerMap_remove_peer (sub->pull_map, | ||
2750 | peer); | ||
2751 | CustomPeerMap_remove_peer (sub->push_map, | ||
2752 | peer); | ||
2753 | RPS_sampler_reinitialise_by_value (sub->sampler, | ||
2754 | peer); | ||
2755 | /* We want to destroy the peer now. | ||
2756 | * Sometimes, it just seems that it's already been removed from the peer_map, | ||
2757 | * so check the peer_map first. */ | ||
2758 | if (GNUNET_YES == check_peer_known (sub->peer_map, | ||
2759 | peer)) | ||
2760 | { | ||
2761 | destroy_peer (get_peer_ctx (sub->peer_map, | ||
2762 | peer)); | ||
2763 | } | ||
2764 | } | ||
2765 | |||
2766 | |||
2767 | /** | ||
2768 | * @brief Remove data that is not needed anymore. | ||
2769 | * | ||
2770 | * If the sending channel is no longer needed it is destroyed. | ||
2771 | * | ||
2772 | * @param sub Sub in which the current peer is to be cleaned | ||
2773 | * @param peer the peer whose data is about to be cleaned | ||
2774 | */ | ||
2775 | static void | ||
2776 | clean_peer (struct Sub *sub, | ||
2777 | const struct GNUNET_PeerIdentity *peer) | ||
2778 | { | ||
2779 | if (GNUNET_NO == check_sending_channel_needed (get_peer_ctx (sub->peer_map, | ||
2780 | peer))) | ||
2781 | { | ||
2782 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
2783 | "Going to remove send channel to peer %s\n", | ||
2784 | GNUNET_i2s (peer)); | ||
2785 | #if ENABLE_MALICIOUS | ||
2786 | if (0 != GNUNET_memcmp (&attacked_peer, | ||
2787 | peer)) | ||
2788 | (void) destroy_sending_channel (get_peer_ctx (sub->peer_map, | ||
2789 | peer)); | ||
2790 | #else /* ENABLE_MALICIOUS */ | ||
2791 | (void) destroy_sending_channel (get_peer_ctx (sub->peer_map, | ||
2792 | peer)); | ||
2793 | #endif /* ENABLE_MALICIOUS */ | ||
2794 | } | ||
2795 | |||
2796 | if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (sub->peer_map, | ||
2797 | peer)) | ||
2798 | { | ||
2799 | /* Peer was already removed by callback on destroyed channel */ | ||
2800 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
2801 | "Peer was removed from our knowledge during cleanup\n"); | ||
2802 | return; | ||
2803 | } | ||
2804 | |||
2805 | if ((GNUNET_NO == check_peer_send_intention (get_peer_ctx (sub->peer_map, | ||
2806 | peer))) && | ||
2807 | (GNUNET_NO == View_contains_peer (sub->view, peer)) && | ||
2808 | (GNUNET_NO == CustomPeerMap_contains_peer (sub->push_map, peer)) && | ||
2809 | (GNUNET_NO == CustomPeerMap_contains_peer (sub->pull_map, peer)) && | ||
2810 | (0 == RPS_sampler_count_id (sub->sampler, peer)) && | ||
2811 | (GNUNET_YES == check_removable (get_peer_ctx (sub->peer_map, peer)))) | ||
2812 | { /* We can safely remove this peer */ | ||
2813 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
2814 | "Going to remove peer %s\n", | ||
2815 | GNUNET_i2s (peer)); | ||
2816 | remove_peer (sub, peer); | ||
2817 | return; | ||
2818 | } | ||
2819 | } | ||
2820 | |||
2821 | |||
2822 | /** | ||
2823 | * @brief This is called when a channel is destroyed. | ||
2824 | * | ||
2825 | * Removes peer completely from our knowledge if the send_channel was destroyed | ||
2826 | * Otherwise simply delete the recv_channel | ||
2827 | * Also check if the knowledge about this peer is still needed. | ||
2828 | * If not, remove this peer from our knowledge. | ||
2829 | * | ||
2830 | * @param cls The closure - Context to the channel | ||
2831 | * @param channel The channel being closed | ||
2832 | */ | ||
2833 | static void | ||
2834 | cleanup_destroyed_channel (void *cls, | ||
2835 | const struct GNUNET_CADET_Channel *channel) | ||
2836 | { | ||
2837 | struct ChannelCtx *channel_ctx = cls; | ||
2838 | struct PeerContext *peer_ctx = channel_ctx->peer_ctx; | ||
2839 | |||
2840 | (void) channel; | ||
2841 | |||
2842 | channel_ctx->channel = NULL; | ||
2843 | if ((NULL != peer_ctx) && | ||
2844 | (peer_ctx->send_channel_ctx == channel_ctx) && | ||
2845 | (GNUNET_YES == check_sending_channel_needed (peer_ctx)) ) | ||
2846 | { | ||
2847 | remove_channel_ctx (channel_ctx); | ||
2848 | remove_peer (peer_ctx->sub, &peer_ctx->peer_id); | ||
2849 | } | ||
2850 | else | ||
2851 | { | ||
2852 | /* We need this if-else construct because we need to make sure the channel | ||
2853 | * (context) is cleaned up before removing the peer, but still need to | ||
2854 | * compare it while checking the condition */ | ||
2855 | remove_channel_ctx (channel_ctx); | ||
2856 | } | ||
2857 | } | ||
2858 | |||
2859 | |||
2860 | /*********************************************************************** | ||
2861 | * /Util functions | ||
2862 | ***********************************************************************/ | ||
2863 | |||
2864 | |||
2865 | /*********************************************************************** | ||
2866 | * Sub | ||
2867 | ***********************************************************************/ | ||
2868 | |||
2869 | /** | ||
2870 | * @brief Create a new Sub | ||
2871 | * | ||
2872 | * @param hash Hash of value shared among rps instances on other hosts that | ||
2873 | * defines a subgroup to sample from. | ||
2874 | * @param sampler_size Size of the sampler | ||
2875 | * @param round_interval Interval (in average) between two rounds | ||
2876 | * | ||
2877 | * @return Sub | ||
2878 | */ | ||
2879 | struct Sub * | ||
2880 | new_sub (const struct GNUNET_HashCode *hash, | ||
2881 | uint32_t sampler_size, | ||
2882 | struct GNUNET_TIME_Relative round_interval) | ||
2883 | { | ||
2884 | struct Sub *sub; | ||
2885 | |||
2886 | sub = GNUNET_new (struct Sub); | ||
2887 | |||
2888 | /* With the hash generated from the secret value this service only connects | ||
2889 | * to rps instances that share the value */ | ||
2890 | struct GNUNET_MQ_MessageHandler cadet_handlers[] = { | ||
2891 | GNUNET_MQ_hd_fixed_size (peer_check, | ||
2892 | GNUNET_MESSAGE_TYPE_RPS_PP_CHECK_LIVE, | ||
2893 | struct GNUNET_MessageHeader, | ||
2894 | NULL), | ||
2895 | GNUNET_MQ_hd_fixed_size (peer_push, | ||
2896 | GNUNET_MESSAGE_TYPE_RPS_PP_PUSH, | ||
2897 | struct GNUNET_MessageHeader, | ||
2898 | NULL), | ||
2899 | GNUNET_MQ_hd_fixed_size (peer_pull_request, | ||
2900 | GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST, | ||
2901 | struct GNUNET_MessageHeader, | ||
2902 | NULL), | ||
2903 | GNUNET_MQ_hd_var_size (peer_pull_reply, | ||
2904 | GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY, | ||
2905 | struct GNUNET_RPS_P2P_PullReplyMessage, | ||
2906 | NULL), | ||
2907 | GNUNET_MQ_handler_end () | ||
2908 | }; | ||
2909 | sub->hash = *hash; | ||
2910 | sub->cadet_port = | ||
2911 | GNUNET_CADET_open_port (cadet_handle, | ||
2912 | &sub->hash, | ||
2913 | &handle_inbound_channel, /* Connect handler */ | ||
2914 | sub, /* cls */ | ||
2915 | NULL, /* WindowSize handler */ | ||
2916 | &cleanup_destroyed_channel, /* Disconnect handler */ | ||
2917 | cadet_handlers); | ||
2918 | if (NULL == sub->cadet_port) | ||
2919 | { | ||
2920 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
2921 | "Cadet port `%s' is already in use.\n", | ||
2922 | GNUNET_APPLICATION_PORT_RPS); | ||
2923 | GNUNET_assert (0); | ||
2924 | } | ||
2925 | |||
2926 | /* Set up general data structure to keep track about peers */ | ||
2927 | sub->valid_peers = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO); | ||
2928 | if (GNUNET_OK != | ||
2929 | GNUNET_CONFIGURATION_get_value_filename (cfg, | ||
2930 | "rps", | ||
2931 | "FILENAME_VALID_PEERS", | ||
2932 | &sub->filename_valid_peers)) | ||
2933 | { | ||
2934 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | ||
2935 | "rps", | ||
2936 | "FILENAME_VALID_PEERS"); | ||
2937 | } | ||
2938 | if (0 != strncmp ("DISABLE", sub->filename_valid_peers, 7)) | ||
2939 | { | ||
2940 | char *tmp_filename_valid_peers; | ||
2941 | char str_hash[105]; | ||
2942 | |||
2943 | GNUNET_snprintf (str_hash, | ||
2944 | sizeof(str_hash), "%s", | ||
2945 | GNUNET_h2s_full (hash)); | ||
2946 | tmp_filename_valid_peers = sub->filename_valid_peers; | ||
2947 | GNUNET_asprintf (&sub->filename_valid_peers, | ||
2948 | "%s%s", | ||
2949 | tmp_filename_valid_peers, | ||
2950 | str_hash); | ||
2951 | GNUNET_free (tmp_filename_valid_peers); | ||
2952 | } | ||
2953 | sub->peer_map = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO); | ||
2954 | |||
2955 | /* Set up the sampler */ | ||
2956 | sub->sampler_size_est_min = sampler_size; | ||
2957 | sub->sampler_size_est_need = sampler_size;; | ||
2958 | LOG (GNUNET_ERROR_TYPE_DEBUG, "MINSIZE is %u\n", sub->sampler_size_est_min); | ||
2959 | GNUNET_assert (0 != round_interval.rel_value_us); | ||
2960 | sub->round_interval = round_interval; | ||
2961 | sub->sampler = RPS_sampler_init (sampler_size, | ||
2962 | round_interval); | ||
2963 | |||
2964 | /* Logging of internals */ | ||
2965 | #ifdef TO_FILE_FULL | ||
2966 | // FIXME: The service cannot know the index, which is required by this | ||
2967 | // function: | ||
2968 | // sub->file_name_view_log = store_prefix_file_name (&own_identity, "view"); | ||
2969 | #endif /* TO_FILE_FULL */ | ||
2970 | #ifdef TO_FILE | ||
2971 | #ifdef TO_FILE_FULL | ||
2972 | // FIXME: The service cannot know the index, which is required by this | ||
2973 | // function: | ||
2974 | // sub->file_name_observed_log = store_prefix_file_name (&own_identity, | ||
2975 | // "observed"); | ||
2976 | #endif /* TO_FILE_FULL */ | ||
2977 | sub->num_observed_peers = 0; | ||
2978 | sub->observed_unique_peers = GNUNET_CONTAINER_multipeermap_create (1, | ||
2979 | GNUNET_NO); | ||
2980 | #endif /* TO_FILE */ | ||
2981 | |||
2982 | /* Set up data structures for gossip */ | ||
2983 | sub->push_map = CustomPeerMap_create (4); | ||
2984 | sub->pull_map = CustomPeerMap_create (4); | ||
2985 | sub->view_size_est_min = sampler_size;; | ||
2986 | sub->view = View_create (sub->view_size_est_min); | ||
2987 | if (sub == msub) | ||
2988 | { | ||
2989 | GNUNET_STATISTICS_set (stats, | ||
2990 | "view size aim", | ||
2991 | sub->view_size_est_min, | ||
2992 | GNUNET_NO); | ||
2993 | } | ||
2994 | |||
2995 | /* Start executing rounds */ | ||
2996 | sub->do_round_task = GNUNET_SCHEDULER_add_now (&do_round, sub); | ||
2997 | |||
2998 | return sub; | ||
2999 | } | ||
3000 | |||
3001 | |||
3002 | #ifdef TO_FILE | ||
3003 | // /** | ||
3004 | // * @brief Write all numbers in the given array into the given file | ||
3005 | // * | ||
3006 | // * Single numbers divided by a newline | ||
3007 | // * | ||
3008 | // * FIXME: The call to store_prefix_file_name expects the index of the peer, | ||
3009 | // * which cannot be known to the service. | ||
3010 | // * Write a dedicated function that uses the peer id. | ||
3011 | // * | ||
3012 | // * @param hist_array[] the array to dump | ||
3013 | // * @param file_name file to dump into | ||
3014 | // */ | ||
3015 | // static void | ||
3016 | // write_histogram_to_file (const uint32_t hist_array[], | ||
3017 | // const char *file_name) | ||
3018 | // { | ||
3019 | // char collect_str[SIZE_DUMP_FILE + 1] = ""; | ||
3020 | // char *recv_str_iter; | ||
3021 | // char *file_name_full; | ||
3022 | // | ||
3023 | // recv_str_iter = collect_str; | ||
3024 | // file_name_full = store_prefix_file_name (&own_identity, | ||
3025 | // file_name); | ||
3026 | // for (uint32_t i = 0; i < HISTOGRAM_FILE_SLOTS; i++) | ||
3027 | // { | ||
3028 | // char collect_str_tmp[8]; | ||
3029 | // | ||
3030 | // GNUNET_snprintf (collect_str_tmp, | ||
3031 | // sizeof(collect_str_tmp), | ||
3032 | // "%" PRIu32 "\n", | ||
3033 | // hist_array[i]); | ||
3034 | // recv_str_iter = stpncpy (recv_str_iter, | ||
3035 | // collect_str_tmp, | ||
3036 | // 6); | ||
3037 | // } | ||
3038 | // (void) stpcpy (recv_str_iter, | ||
3039 | // "\n"); | ||
3040 | // LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
3041 | // "Writing push stats to disk\n"); | ||
3042 | // to_file_w_len (file_name_full, | ||
3043 | // SIZE_DUMP_FILE, "%s", | ||
3044 | // collect_str); | ||
3045 | // GNUNET_free (file_name_full); | ||
3046 | // } | ||
3047 | |||
3048 | |||
3049 | #endif /* TO_FILE */ | ||
3050 | |||
3051 | |||
3052 | /** | ||
3053 | * @brief Destroy Sub. | ||
3054 | * | ||
3055 | * @param sub Sub to destroy | ||
3056 | */ | ||
3057 | static void | ||
3058 | destroy_sub (struct Sub *sub) | ||
3059 | { | ||
3060 | GNUNET_assert (NULL != sub); | ||
3061 | GNUNET_assert (NULL != sub->do_round_task); | ||
3062 | GNUNET_SCHEDULER_cancel (sub->do_round_task); | ||
3063 | sub->do_round_task = NULL; | ||
3064 | |||
3065 | /* Disconnect from cadet */ | ||
3066 | GNUNET_CADET_close_port (sub->cadet_port); | ||
3067 | sub->cadet_port = NULL; | ||
3068 | |||
3069 | /* Clean up data structures for peers */ | ||
3070 | RPS_sampler_destroy (sub->sampler); | ||
3071 | sub->sampler = NULL; | ||
3072 | View_destroy (sub->view); | ||
3073 | sub->view = NULL; | ||
3074 | CustomPeerMap_destroy (sub->push_map); | ||
3075 | sub->push_map = NULL; | ||
3076 | CustomPeerMap_destroy (sub->pull_map); | ||
3077 | sub->pull_map = NULL; | ||
3078 | peers_terminate (sub); | ||
3079 | |||
3080 | /* Free leftover data structures */ | ||
3081 | #ifdef TO_FILE_FULL | ||
3082 | GNUNET_free (sub->file_name_view_log); | ||
3083 | sub->file_name_view_log = NULL; | ||
3084 | #endif /* TO_FILE_FULL */ | ||
3085 | #ifdef TO_FILE | ||
3086 | #ifdef TO_FILE_FULL | ||
3087 | GNUNET_free (sub->file_name_observed_log); | ||
3088 | sub->file_name_observed_log = NULL; | ||
3089 | #endif /* TO_FILE_FULL */ | ||
3090 | |||
3091 | // FIXME: Currently this calls malfunctionning code | ||
3092 | // /* Write push frequencies to disk */ | ||
3093 | // write_histogram_to_file (sub->push_recv, | ||
3094 | // "push_recv"); | ||
3095 | |||
3096 | // /* Write push deltas to disk */ | ||
3097 | // write_histogram_to_file (sub->push_delta, | ||
3098 | // "push_delta"); | ||
3099 | |||
3100 | // /* Write pull delays to disk */ | ||
3101 | // write_histogram_to_file (sub->pull_delays, | ||
3102 | // "pull_delays"); | ||
3103 | |||
3104 | GNUNET_CONTAINER_multipeermap_destroy (sub->observed_unique_peers); | ||
3105 | sub->observed_unique_peers = NULL; | ||
3106 | #endif /* TO_FILE */ | ||
3107 | |||
3108 | GNUNET_free (sub); | ||
3109 | } | ||
3110 | |||
3111 | |||
3112 | /*********************************************************************** | ||
3113 | * /Sub | ||
3114 | ***********************************************************************/ | ||
3115 | |||
3116 | |||
3117 | /*********************************************************************** | ||
3118 | * Core handlers | ||
3119 | ***********************************************************************/ | ||
3120 | |||
3121 | /** | ||
3122 | * @brief Callback on initialisation of Core. | ||
3123 | * | ||
3124 | * @param cls - unused | ||
3125 | * @param my_identity - unused | ||
3126 | */ | ||
3127 | void | ||
3128 | core_init (void *cls, | ||
3129 | const struct GNUNET_PeerIdentity *my_identity) | ||
3130 | { | ||
3131 | (void) cls; | ||
3132 | (void) my_identity; | ||
3133 | |||
3134 | map_single_hop = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO); | ||
3135 | } | ||
3136 | |||
3137 | |||
3138 | /** | ||
3139 | * @brief Callback for core. | ||
3140 | * Method called whenever a given peer connects. | ||
3141 | * | ||
3142 | * @param cls closure - unused | ||
3143 | * @param peer peer identity this notification is about | ||
3144 | * @return closure given to #core_disconnects as peer_cls | ||
3145 | */ | ||
3146 | void * | ||
3147 | core_connects (void *cls, | ||
3148 | const struct GNUNET_PeerIdentity *peer, | ||
3149 | struct GNUNET_MQ_Handle *mq) | ||
3150 | { | ||
3151 | (void) cls; | ||
3152 | (void) mq; | ||
3153 | |||
3154 | GNUNET_assert (GNUNET_YES == | ||
3155 | GNUNET_CONTAINER_multipeermap_put (map_single_hop, | ||
3156 | peer, | ||
3157 | NULL, | ||
3158 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); | ||
3159 | return NULL; | ||
3160 | } | ||
3161 | |||
3162 | |||
3163 | /** | ||
3164 | * @brief Callback for core. | ||
3165 | * Method called whenever a peer disconnects. | ||
3166 | * | ||
3167 | * @param cls closure - unused | ||
3168 | * @param peer peer identity this notification is about | ||
3169 | * @param peer_cls closure given in #core_connects - unused | ||
3170 | */ | ||
3171 | void | ||
3172 | core_disconnects (void *cls, | ||
3173 | const struct GNUNET_PeerIdentity *peer, | ||
3174 | void *peer_cls) | ||
3175 | { | ||
3176 | (void) cls; | ||
3177 | (void) peer_cls; | ||
3178 | |||
3179 | GNUNET_CONTAINER_multipeermap_remove_all (map_single_hop, peer); | ||
3180 | } | ||
3181 | |||
3182 | |||
3183 | /*********************************************************************** | ||
3184 | * /Core handlers | ||
3185 | ***********************************************************************/ | ||
3186 | |||
3187 | |||
3188 | /** | ||
3189 | * @brief Destroy the context for a (connected) client | ||
3190 | * | ||
3191 | * @param cli_ctx Context to destroy | ||
3192 | */ | ||
3193 | static void | ||
3194 | destroy_cli_ctx (struct ClientContext *cli_ctx) | ||
3195 | { | ||
3196 | GNUNET_assert (NULL != cli_ctx); | ||
3197 | GNUNET_CONTAINER_DLL_remove (cli_ctx_head, | ||
3198 | cli_ctx_tail, | ||
3199 | cli_ctx); | ||
3200 | if (NULL != cli_ctx->sub) | ||
3201 | { | ||
3202 | destroy_sub (cli_ctx->sub); | ||
3203 | cli_ctx->sub = NULL; | ||
3204 | } | ||
3205 | GNUNET_free (cli_ctx); | ||
3206 | } | ||
3207 | |||
3208 | |||
3209 | /** | ||
3210 | * @brief Update sizes in sampler and view on estimate update from nse service | ||
3211 | * | ||
3212 | * @param sub Sub | ||
3213 | * @param logestimate the log(Base 2) value of the current network size estimate | ||
3214 | * @param std_dev standard deviation for the estimate | ||
3215 | */ | ||
3216 | static void | ||
3217 | adapt_sizes (struct Sub *sub, double logestimate, double std_dev) | ||
3218 | { | ||
3219 | double estimate; | ||
3220 | |||
3221 | // double scale; // TODO this might go global/config | ||
3222 | |||
3223 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
3224 | "Received a ns estimate - logest: %f, std_dev: %f (old_size: %u)\n", | ||
3225 | logestimate, std_dev, RPS_sampler_get_size (sub->sampler)); | ||
3226 | // scale = .01; | ||
3227 | estimate = GNUNET_NSE_log_estimate_to_n (logestimate); | ||
3228 | // GNUNET_NSE_log_estimate_to_n (logestimate); | ||
3229 | estimate = pow (estimate, 1.0 / 3); | ||
3230 | // TODO add if std_dev is a number | ||
3231 | // estimate += (std_dev * scale); | ||
3232 | if (sub->view_size_est_min < ceil (estimate)) | ||
3233 | { | ||
3234 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate); | ||
3235 | sub->sampler_size_est_need = estimate; | ||
3236 | sub->view_size_est_need = estimate; | ||
3237 | } | ||
3238 | else | ||
3239 | { | ||
3240 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate); | ||
3241 | // sub->sampler_size_est_need = sub->view_size_est_min; | ||
3242 | sub->view_size_est_need = sub->view_size_est_min; | ||
3243 | } | ||
3244 | if (sub == msub) | ||
3245 | { | ||
3246 | GNUNET_STATISTICS_set (stats, | ||
3247 | "view size aim", | ||
3248 | sub->view_size_est_need, | ||
3249 | GNUNET_NO); | ||
3250 | } | ||
3251 | |||
3252 | /* If the NSE has changed adapt the lists accordingly */ | ||
3253 | resize_wrapper (sub->sampler, sub->sampler_size_est_need); | ||
3254 | View_change_len (sub->view, sub->view_size_est_need); | ||
3255 | } | ||
3256 | |||
3257 | |||
3258 | /** | ||
3259 | * Function called by NSE. | ||
3260 | * | ||
3261 | * Updates sizes of sampler list and view and adapt those lists | ||
3262 | * accordingly. | ||
3263 | * | ||
3264 | * implements #GNUNET_NSE_Callback | ||
3265 | * | ||
3266 | * @param cls Closure - unused | ||
3267 | * @param timestamp time when the estimate was received from the server (or created by the server) | ||
3268 | * @param logestimate the log(Base 2) value of the current network size estimate | ||
3269 | * @param std_dev standard deviation for the estimate | ||
3270 | */ | ||
3271 | static void | ||
3272 | nse_callback (void *cls, | ||
3273 | struct GNUNET_TIME_Absolute timestamp, | ||
3274 | double logestimate, double std_dev) | ||
3275 | { | ||
3276 | (void) cls; | ||
3277 | (void) timestamp; | ||
3278 | struct ClientContext *cli_ctx_iter; | ||
3279 | |||
3280 | adapt_sizes (msub, logestimate, std_dev); | ||
3281 | for (cli_ctx_iter = cli_ctx_head; | ||
3282 | NULL != cli_ctx_iter; | ||
3283 | cli_ctx_iter = cli_ctx_iter->next) | ||
3284 | { | ||
3285 | if (NULL != cli_ctx_iter->sub) | ||
3286 | { | ||
3287 | adapt_sizes (cli_ctx_iter->sub, logestimate, std_dev); | ||
3288 | } | ||
3289 | } | ||
3290 | } | ||
3291 | |||
3292 | |||
3293 | /** | ||
3294 | * @brief This function is called, when the client seeds peers. | ||
3295 | * It verifies that @a msg is well-formed. | ||
3296 | * | ||
3297 | * @param cls the closure (#ClientContext) | ||
3298 | * @param msg the message | ||
3299 | * @return #GNUNET_OK if @a msg is well-formed | ||
3300 | * #GNUNET_SYSERR otherwise | ||
3301 | */ | ||
3302 | static int | ||
3303 | check_client_seed (void *cls, const struct GNUNET_RPS_CS_SeedMessage *msg) | ||
3304 | { | ||
3305 | struct ClientContext *cli_ctx = cls; | ||
3306 | uint16_t msize = ntohs (msg->header.size); | ||
3307 | uint32_t num_peers = ntohl (msg->num_peers); | ||
3308 | |||
3309 | msize -= sizeof(struct GNUNET_RPS_CS_SeedMessage); | ||
3310 | if ((msize / sizeof(struct GNUNET_PeerIdentity) != num_peers) || | ||
3311 | (msize % sizeof(struct GNUNET_PeerIdentity) != 0)) | ||
3312 | { | ||
3313 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
3314 | "message says it sends %" PRIu32 " peers, have space for %lu peers\n", | ||
3315 | ntohl (msg->num_peers), | ||
3316 | (msize / sizeof(struct GNUNET_PeerIdentity))); | ||
3317 | GNUNET_break (0); | ||
3318 | GNUNET_SERVICE_client_drop (cli_ctx->client); | ||
3319 | return GNUNET_SYSERR; | ||
3320 | } | ||
3321 | return GNUNET_OK; | ||
3322 | } | ||
3323 | |||
3324 | |||
3325 | /** | ||
3326 | * Handle seed from the client. | ||
3327 | * | ||
3328 | * @param cls closure | ||
3329 | * @param msg the actual message | ||
3330 | */ | ||
3331 | static void | ||
3332 | handle_client_seed (void *cls, | ||
3333 | const struct GNUNET_RPS_CS_SeedMessage *msg) | ||
3334 | { | ||
3335 | struct ClientContext *cli_ctx = cls; | ||
3336 | struct GNUNET_PeerIdentity *peers; | ||
3337 | uint32_t num_peers; | ||
3338 | uint32_t i; | ||
3339 | |||
3340 | num_peers = ntohl (msg->num_peers); | ||
3341 | peers = (struct GNUNET_PeerIdentity *) &msg[1]; | ||
3342 | |||
3343 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
3344 | "Client seeded peers:\n"); | ||
3345 | print_peer_list (peers, num_peers); | ||
3346 | |||
3347 | for (i = 0; i < num_peers; i++) | ||
3348 | { | ||
3349 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
3350 | "Updating samplers with seed %" PRIu32 ": %s\n", | ||
3351 | i, | ||
3352 | GNUNET_i2s (&peers[i])); | ||
3353 | |||
3354 | if (NULL != msub) | ||
3355 | got_peer (msub, &peers[i]); /* Condition needed? */ | ||
3356 | if (NULL != cli_ctx->sub) | ||
3357 | got_peer (cli_ctx->sub, &peers[i]); | ||
3358 | } | ||
3359 | GNUNET_SERVICE_client_continue (cli_ctx->client); | ||
3360 | } | ||
3361 | |||
3362 | |||
3363 | /** | ||
3364 | * Handle RPS request from the client. | ||
3365 | * | ||
3366 | * @param cls Client context | ||
3367 | * @param msg Message containing the number of updates the client wants to | ||
3368 | * receive | ||
3369 | */ | ||
3370 | static void | ||
3371 | handle_client_view_request (void *cls, | ||
3372 | const struct GNUNET_RPS_CS_DEBUG_ViewRequest *msg) | ||
3373 | { | ||
3374 | struct ClientContext *cli_ctx = cls; | ||
3375 | uint64_t num_updates; | ||
3376 | |||
3377 | num_updates = ntohl (msg->num_updates); | ||
3378 | |||
3379 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
3380 | "Client requested %" PRIu64 " updates of view.\n", | ||
3381 | num_updates); | ||
3382 | |||
3383 | GNUNET_assert (NULL != cli_ctx); | ||
3384 | cli_ctx->view_updates_left = num_updates; | ||
3385 | send_view (cli_ctx, NULL, 0); | ||
3386 | GNUNET_SERVICE_client_continue (cli_ctx->client); | ||
3387 | } | ||
3388 | |||
3389 | |||
3390 | /** | ||
3391 | * @brief Handle the cancellation of the view updates. | ||
3392 | * | ||
3393 | * @param cls The client context | ||
3394 | * @param msg Unused | ||
3395 | */ | ||
3396 | static void | ||
3397 | handle_client_view_cancel (void *cls, | ||
3398 | const struct GNUNET_MessageHeader *msg) | ||
3399 | { | ||
3400 | struct ClientContext *cli_ctx = cls; | ||
3401 | |||
3402 | (void) msg; | ||
3403 | |||
3404 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
3405 | "Client does not want to receive updates of view any more.\n"); | ||
3406 | |||
3407 | GNUNET_assert (NULL != cli_ctx); | ||
3408 | cli_ctx->view_updates_left = 0; | ||
3409 | GNUNET_SERVICE_client_continue (cli_ctx->client); | ||
3410 | if (GNUNET_YES == cli_ctx->stream_update) | ||
3411 | { | ||
3412 | destroy_cli_ctx (cli_ctx); | ||
3413 | } | ||
3414 | } | ||
3415 | |||
3416 | |||
3417 | /** | ||
3418 | * Handle RPS request for biased stream from the client. | ||
3419 | * | ||
3420 | * @param cls Client context | ||
3421 | * @param msg unused | ||
3422 | */ | ||
3423 | static void | ||
3424 | handle_client_stream_request (void *cls, | ||
3425 | const struct | ||
3426 | GNUNET_RPS_CS_DEBUG_StreamRequest *msg) | ||
3427 | { | ||
3428 | struct ClientContext *cli_ctx = cls; | ||
3429 | |||
3430 | (void) msg; | ||
3431 | |||
3432 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
3433 | "Client requested peers from biased stream.\n"); | ||
3434 | cli_ctx->stream_update = GNUNET_YES; | ||
3435 | |||
3436 | GNUNET_assert (NULL != cli_ctx); | ||
3437 | GNUNET_SERVICE_client_continue (cli_ctx->client); | ||
3438 | } | ||
3439 | |||
3440 | |||
3441 | /** | ||
3442 | * @brief Handles the cancellation of the stream of biased peer ids | ||
3443 | * | ||
3444 | * @param cls The client context | ||
3445 | * @param msg unused | ||
3446 | */ | ||
3447 | static void | ||
3448 | handle_client_stream_cancel (void *cls, | ||
3449 | const struct GNUNET_MessageHeader *msg) | ||
3450 | { | ||
3451 | struct ClientContext *cli_ctx = cls; | ||
3452 | |||
3453 | (void) msg; | ||
3454 | |||
3455 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
3456 | "Client canceled receiving peers from biased stream.\n"); | ||
3457 | cli_ctx->stream_update = GNUNET_NO; | ||
3458 | |||
3459 | GNUNET_assert (NULL != cli_ctx); | ||
3460 | GNUNET_SERVICE_client_continue (cli_ctx->client); | ||
3461 | } | ||
3462 | |||
3463 | |||
3464 | /** | ||
3465 | * @brief Create and start a Sub. | ||
3466 | * | ||
3467 | * @param cls Closure - unused | ||
3468 | * @param msg Message containing the necessary information | ||
3469 | */ | ||
3470 | static void | ||
3471 | handle_client_start_sub (void *cls, | ||
3472 | const struct GNUNET_RPS_CS_SubStartMessage *msg) | ||
3473 | { | ||
3474 | struct ClientContext *cli_ctx = cls; | ||
3475 | |||
3476 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Client requested start of a new sub.\n"); | ||
3477 | if ((NULL != cli_ctx->sub) && | ||
3478 | (0 != memcmp (&cli_ctx->sub->hash, | ||
3479 | &msg->hash, | ||
3480 | sizeof(struct GNUNET_HashCode))) ) | ||
3481 | { | ||
3482 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
3483 | "Already have a Sub with different share for this client. Remove old one, add new.\n"); | ||
3484 | destroy_sub (cli_ctx->sub); | ||
3485 | cli_ctx->sub = NULL; | ||
3486 | } | ||
3487 | cli_ctx->sub = new_sub (&msg->hash, | ||
3488 | msub->sampler_size_est_min, // TODO make api input? | ||
3489 | GNUNET_TIME_relative_ntoh (msg->round_interval)); | ||
3490 | GNUNET_SERVICE_client_continue (cli_ctx->client); | ||
3491 | } | ||
3492 | |||
3493 | |||
3494 | /** | ||
3495 | * @brief Destroy the Sub | ||
3496 | * | ||
3497 | * @param cls Closure - unused | ||
3498 | * @param msg Message containing the hash that identifies the Sub | ||
3499 | */ | ||
3500 | static void | ||
3501 | handle_client_stop_sub (void *cls, | ||
3502 | const struct GNUNET_RPS_CS_SubStopMessage *msg) | ||
3503 | { | ||
3504 | struct ClientContext *cli_ctx = cls; | ||
3505 | |||
3506 | GNUNET_assert (NULL != cli_ctx->sub); | ||
3507 | if (0 != memcmp (&cli_ctx->sub->hash, &msg->hash, sizeof(struct | ||
3508 | GNUNET_HashCode))) | ||
3509 | { | ||
3510 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
3511 | "Share of current sub and request differ!\n"); | ||
3512 | } | ||
3513 | destroy_sub (cli_ctx->sub); | ||
3514 | cli_ctx->sub = NULL; | ||
3515 | GNUNET_SERVICE_client_continue (cli_ctx->client); | ||
3516 | } | ||
3517 | |||
3518 | |||
3519 | /** | ||
3520 | * Handle a CHECK_LIVE message from another peer. | ||
3521 | * | ||
3522 | * This does nothing. But without calling #GNUNET_CADET_receive_done() | ||
3523 | * the channel is blocked for all other communication. | ||
3524 | * | ||
3525 | * @param cls Closure - Context of channel | ||
3526 | * @param msg Message - unused | ||
3527 | */ | ||
3528 | static void | ||
3529 | handle_peer_check (void *cls, | ||
3530 | const struct GNUNET_MessageHeader *msg) | ||
3531 | { | ||
3532 | const struct ChannelCtx *channel_ctx = cls; | ||
3533 | const struct GNUNET_PeerIdentity *peer = &channel_ctx->peer_ctx->peer_id; | ||
3534 | |||
3535 | (void) msg; | ||
3536 | |||
3537 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
3538 | "Received CHECK_LIVE (%s)\n", GNUNET_i2s (peer)); | ||
3539 | if (channel_ctx->peer_ctx->sub == msub) | ||
3540 | { | ||
3541 | GNUNET_STATISTICS_update (stats, | ||
3542 | "# pending online checks", | ||
3543 | -1, | ||
3544 | GNUNET_NO); | ||
3545 | } | ||
3546 | |||
3547 | GNUNET_CADET_receive_done (channel_ctx->channel); | ||
3548 | } | ||
3549 | |||
3550 | |||
3551 | /** | ||
3552 | * Handle a PUSH message from another peer. | ||
3553 | * | ||
3554 | * Check the proof of work and store the PeerID | ||
3555 | * in the temporary list for pushed PeerIDs. | ||
3556 | * | ||
3557 | * @param cls Closure - Context of channel | ||
3558 | * @param msg Message - unused | ||
3559 | */ | ||
3560 | static void | ||
3561 | handle_peer_push (void *cls, | ||
3562 | const struct GNUNET_MessageHeader *msg) | ||
3563 | { | ||
3564 | const struct ChannelCtx *channel_ctx = cls; | ||
3565 | const struct GNUNET_PeerIdentity *peer = &channel_ctx->peer_ctx->peer_id; | ||
3566 | |||
3567 | (void) msg; | ||
3568 | |||
3569 | // (check the proof of work (?)) | ||
3570 | |||
3571 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
3572 | "Received PUSH (%s)\n", | ||
3573 | GNUNET_i2s (peer)); | ||
3574 | if (channel_ctx->peer_ctx->sub == msub) | ||
3575 | { | ||
3576 | GNUNET_STATISTICS_update (stats, "# push message received", 1, GNUNET_NO); | ||
3577 | if ((NULL != map_single_hop) && | ||
3578 | (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (map_single_hop, | ||
3579 | peer))) | ||
3580 | { | ||
3581 | GNUNET_STATISTICS_update (stats, | ||
3582 | "# push message received (multi-hop peer)", | ||
3583 | 1, | ||
3584 | GNUNET_NO); | ||
3585 | } | ||
3586 | } | ||
3587 | |||
3588 | #if ENABLE_MALICIOUS | ||
3589 | struct AttackedPeer *tmp_att_peer; | ||
3590 | |||
3591 | if ((1 == mal_type) || | ||
3592 | (3 == mal_type)) | ||
3593 | { /* Try to maximise representation */ | ||
3594 | tmp_att_peer = GNUNET_new (struct AttackedPeer); | ||
3595 | tmp_att_peer->peer_id = *peer; | ||
3596 | if (NULL == att_peer_set) | ||
3597 | att_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO); | ||
3598 | if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set, | ||
3599 | peer)) | ||
3600 | { | ||
3601 | GNUNET_CONTAINER_DLL_insert (att_peers_head, | ||
3602 | att_peers_tail, | ||
3603 | tmp_att_peer); | ||
3604 | add_peer_array_to_set (peer, 1, att_peer_set); | ||
3605 | } | ||
3606 | else | ||
3607 | { | ||
3608 | GNUNET_free (tmp_att_peer); | ||
3609 | } | ||
3610 | } | ||
3611 | |||
3612 | |||
3613 | else if (2 == mal_type) | ||
3614 | { | ||
3615 | /* We attack one single well-known peer - simply ignore */ | ||
3616 | } | ||
3617 | #endif /* ENABLE_MALICIOUS */ | ||
3618 | |||
3619 | /* Add the sending peer to the push_map */ | ||
3620 | CustomPeerMap_put (channel_ctx->peer_ctx->sub->push_map, peer); | ||
3621 | |||
3622 | GNUNET_break_op (check_peer_known (channel_ctx->peer_ctx->sub->peer_map, | ||
3623 | &channel_ctx->peer_ctx->peer_id)); | ||
3624 | GNUNET_CADET_receive_done (channel_ctx->channel); | ||
3625 | } | ||
3626 | |||
3627 | |||
3628 | /** | ||
3629 | * Handle PULL REQUEST request message from another peer. | ||
3630 | * | ||
3631 | * Reply with the view of PeerIDs. | ||
3632 | * | ||
3633 | * @param cls Closure - Context of channel | ||
3634 | * @param msg Message - unused | ||
3635 | */ | ||
3636 | static void | ||
3637 | handle_peer_pull_request (void *cls, | ||
3638 | const struct GNUNET_MessageHeader *msg) | ||
3639 | { | ||
3640 | const struct ChannelCtx *channel_ctx = cls; | ||
3641 | struct PeerContext *peer_ctx = channel_ctx->peer_ctx; | ||
3642 | const struct GNUNET_PeerIdentity *peer = &peer_ctx->peer_id; | ||
3643 | const struct GNUNET_PeerIdentity *view_array; | ||
3644 | |||
3645 | (void) msg; | ||
3646 | |||
3647 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REQUEST (%s)\n", GNUNET_i2s ( | ||
3648 | peer)); | ||
3649 | if (peer_ctx->sub == msub) | ||
3650 | { | ||
3651 | GNUNET_STATISTICS_update (stats, | ||
3652 | "# pull request message received", | ||
3653 | 1, | ||
3654 | GNUNET_NO); | ||
3655 | if ((NULL != map_single_hop) && | ||
3656 | (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (map_single_hop, | ||
3657 | &peer_ctx->peer_id))) | ||
3658 | { | ||
3659 | GNUNET_STATISTICS_update (stats, | ||
3660 | "# pull request message received (multi-hop peer)", | ||
3661 | 1, | ||
3662 | GNUNET_NO); | ||
3663 | } | ||
3664 | } | ||
3665 | |||
3666 | #if ENABLE_MALICIOUS | ||
3667 | if ((1 == mal_type) | ||
3668 | || (3 == mal_type)) | ||
3669 | { /* Try to maximise representation */ | ||
3670 | send_pull_reply (peer_ctx, mal_peers, num_mal_peers); | ||
3671 | } | ||
3672 | |||
3673 | else if (2 == mal_type) | ||
3674 | { /* Try to partition network */ | ||
3675 | if (0 == GNUNET_memcmp (&attacked_peer, peer)) | ||
3676 | { | ||
3677 | send_pull_reply (peer_ctx, mal_peers, num_mal_peers); | ||
3678 | } | ||
3679 | } | ||
3680 | #endif /* ENABLE_MALICIOUS */ | ||
3681 | |||
3682 | GNUNET_break_op (check_peer_known (channel_ctx->peer_ctx->sub->peer_map, | ||
3683 | &channel_ctx->peer_ctx->peer_id)); | ||
3684 | GNUNET_CADET_receive_done (channel_ctx->channel); | ||
3685 | view_array = View_get_as_array (channel_ctx->peer_ctx->sub->view); | ||
3686 | send_pull_reply (peer_ctx, | ||
3687 | view_array, | ||
3688 | View_size (channel_ctx->peer_ctx->sub->view)); | ||
3689 | } | ||
3690 | |||
3691 | |||
3692 | /** | ||
3693 | * Check whether we sent a corresponding request and | ||
3694 | * whether this reply is the first one. | ||
3695 | * | ||
3696 | * @param cls Closure - Context of channel | ||
3697 | * @param msg Message containing the replied peers | ||
3698 | */ | ||
3699 | static int | ||
3700 | check_peer_pull_reply (void *cls, | ||
3701 | const struct GNUNET_RPS_P2P_PullReplyMessage *msg) | ||
3702 | { | ||
3703 | struct ChannelCtx *channel_ctx = cls; | ||
3704 | struct PeerContext *sender_ctx = channel_ctx->peer_ctx; | ||
3705 | |||
3706 | if (sizeof(struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->header.size)) | ||
3707 | { | ||
3708 | GNUNET_break_op (0); | ||
3709 | return GNUNET_SYSERR; | ||
3710 | } | ||
3711 | |||
3712 | if ((ntohs (msg->header.size) - sizeof(struct | ||
3713 | GNUNET_RPS_P2P_PullReplyMessage)) | ||
3714 | / sizeof(struct GNUNET_PeerIdentity) != ntohl (msg->num_peers)) | ||
3715 | { | ||
3716 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
3717 | "message says it sends %" PRIu32 " peers, have space for %lu peers\n", | ||
3718 | ntohl (msg->num_peers), | ||
3719 | (ntohs (msg->header.size) - sizeof(struct | ||
3720 | GNUNET_RPS_P2P_PullReplyMessage)) | ||
3721 | / sizeof(struct GNUNET_PeerIdentity)); | ||
3722 | GNUNET_break_op (0); | ||
3723 | return GNUNET_SYSERR; | ||
3724 | } | ||
3725 | |||
3726 | if (GNUNET_YES != check_peer_flag (sender_ctx->sub->peer_map, | ||
3727 | &sender_ctx->peer_id, | ||
3728 | Peers_PULL_REPLY_PENDING)) | ||
3729 | { | ||
3730 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
3731 | "Received a pull reply from a peer (%s) we didn't request one from!\n", | ||
3732 | GNUNET_i2s (&sender_ctx->peer_id)); | ||
3733 | if (sender_ctx->sub == msub) | ||
3734 | { | ||
3735 | GNUNET_STATISTICS_update (stats, | ||
3736 | "# unrequested pull replies", | ||
3737 | 1, | ||
3738 | GNUNET_NO); | ||
3739 | } | ||
3740 | } | ||
3741 | return GNUNET_OK; | ||
3742 | } | ||
3743 | |||
3744 | |||
3745 | /** | ||
3746 | * Handle PULL REPLY message from another peer. | ||
3747 | * | ||
3748 | * @param cls Closure | ||
3749 | * @param msg The message header | ||
3750 | */ | ||
3751 | static void | ||
3752 | handle_peer_pull_reply (void *cls, | ||
3753 | const struct GNUNET_RPS_P2P_PullReplyMessage *msg) | ||
3754 | { | ||
3755 | const struct ChannelCtx *channel_ctx = cls; | ||
3756 | const struct GNUNET_PeerIdentity *sender = &channel_ctx->peer_ctx->peer_id; | ||
3757 | const struct GNUNET_PeerIdentity *peers; | ||
3758 | struct Sub *sub = channel_ctx->peer_ctx->sub; | ||
3759 | uint32_t i; | ||
3760 | |||
3761 | #if ENABLE_MALICIOUS | ||
3762 | struct AttackedPeer *tmp_att_peer; | ||
3763 | #endif /* ENABLE_MALICIOUS */ | ||
3764 | |||
3765 | sub->pull_delays[sub->num_rounds - channel_ctx->peer_ctx->round_pull_req]++; | ||
3766 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REPLY (%s)\n", GNUNET_i2s ( | ||
3767 | sender)); | ||
3768 | if (channel_ctx->peer_ctx->sub == msub) | ||
3769 | { | ||
3770 | GNUNET_STATISTICS_update (stats, | ||
3771 | "# pull reply messages received", | ||
3772 | 1, | ||
3773 | GNUNET_NO); | ||
3774 | if ((NULL != map_single_hop) && | ||
3775 | (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (map_single_hop, | ||
3776 | &channel_ctx-> | ||
3777 | peer_ctx->peer_id)) ) | ||
3778 | { | ||
3779 | GNUNET_STATISTICS_update (stats, | ||
3780 | "# pull reply messages received (multi-hop peer)", | ||
3781 | 1, | ||
3782 | GNUNET_NO); | ||
3783 | } | ||
3784 | } | ||
3785 | |||
3786 | #if ENABLE_MALICIOUS | ||
3787 | // We shouldn't even receive pull replies as we're not sending | ||
3788 | if (2 == mal_type) | ||
3789 | { | ||
3790 | } | ||
3791 | #endif /* ENABLE_MALICIOUS */ | ||
3792 | |||
3793 | /* Do actual logic */ | ||
3794 | peers = (const struct GNUNET_PeerIdentity *) &msg[1]; | ||
3795 | |||
3796 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
3797 | "PULL REPLY received, got following %u peers:\n", | ||
3798 | ntohl (msg->num_peers)); | ||
3799 | |||
3800 | for (i = 0; i < ntohl (msg->num_peers); i++) | ||
3801 | { | ||
3802 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
3803 | "%u. %s\n", | ||
3804 | i, | ||
3805 | GNUNET_i2s (&peers[i])); | ||
3806 | |||
3807 | #if ENABLE_MALICIOUS | ||
3808 | if ((NULL != att_peer_set) && | ||
3809 | ((1 == mal_type) || (3 == mal_type) )) | ||
3810 | { /* Add attacked peer to local list */ | ||
3811 | // TODO check if we sent a request and this was the first reply | ||
3812 | if ((GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set, | ||
3813 | &peers[i])) | ||
3814 | && (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mal_peer_set, | ||
3815 | &peers[i])) ) | ||
3816 | { | ||
3817 | tmp_att_peer = GNUNET_new (struct AttackedPeer); | ||
3818 | tmp_att_peer->peer_id = peers[i]; | ||
3819 | GNUNET_CONTAINER_DLL_insert (att_peers_head, | ||
3820 | att_peers_tail, | ||
3821 | tmp_att_peer); | ||
3822 | add_peer_array_to_set (&peers[i], 1, att_peer_set); | ||
3823 | } | ||
3824 | continue; | ||
3825 | } | ||
3826 | #endif /* ENABLE_MALICIOUS */ | ||
3827 | /* Make sure we 'know' about this peer */ | ||
3828 | (void) insert_peer (channel_ctx->peer_ctx->sub, | ||
3829 | &peers[i]); | ||
3830 | |||
3831 | if (GNUNET_YES == check_peer_valid (channel_ctx->peer_ctx->sub->valid_peers, | ||
3832 | &peers[i])) | ||
3833 | { | ||
3834 | CustomPeerMap_put (channel_ctx->peer_ctx->sub->pull_map, | ||
3835 | &peers[i]); | ||
3836 | } | ||
3837 | else | ||
3838 | { | ||
3839 | schedule_operation (channel_ctx->peer_ctx, | ||
3840 | insert_in_pull_map, | ||
3841 | channel_ctx->peer_ctx->sub); /* cls */ | ||
3842 | (void) issue_peer_online_check (channel_ctx->peer_ctx->sub, | ||
3843 | &peers[i]); | ||
3844 | } | ||
3845 | } | ||
3846 | |||
3847 | UNSET_PEER_FLAG (get_peer_ctx (channel_ctx->peer_ctx->sub->peer_map, | ||
3848 | sender), | ||
3849 | Peers_PULL_REPLY_PENDING); | ||
3850 | clean_peer (channel_ctx->peer_ctx->sub, | ||
3851 | sender); | ||
3852 | |||
3853 | GNUNET_break_op (check_peer_known (channel_ctx->peer_ctx->sub->peer_map, | ||
3854 | sender)); | ||
3855 | GNUNET_CADET_receive_done (channel_ctx->channel); | ||
3856 | } | ||
3857 | |||
3858 | |||
3859 | /** | ||
3860 | * Compute a random delay. | ||
3861 | * A uniformly distributed value between mean + spread and mean - spread. | ||
3862 | * | ||
3863 | * For example for mean 4 min and spread 2 the minimum is (4 min - (1/2 * 4 min)) | ||
3864 | * It would return a random value between 2 and 6 min. | ||
3865 | * | ||
3866 | * @param mean the mean time until the next round | ||
3867 | * @param spread the inverse amount of deviation from the mean | ||
3868 | */ | ||
3869 | static struct GNUNET_TIME_Relative | ||
3870 | compute_rand_delay (struct GNUNET_TIME_Relative mean, | ||
3871 | unsigned int spread) | ||
3872 | { | ||
3873 | struct GNUNET_TIME_Relative half_interval; | ||
3874 | struct GNUNET_TIME_Relative ret; | ||
3875 | unsigned int rand_delay; | ||
3876 | unsigned int max_rand_delay; | ||
3877 | |||
3878 | if (0 == spread) | ||
3879 | { | ||
3880 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
3881 | "Not accepting spread of 0\n"); | ||
3882 | GNUNET_break (0); | ||
3883 | GNUNET_assert (0); | ||
3884 | } | ||
3885 | GNUNET_assert (0 != mean.rel_value_us); | ||
3886 | |||
3887 | /* Compute random time value between spread * mean and spread * mean */ | ||
3888 | half_interval = GNUNET_TIME_relative_divide (mean, spread); | ||
3889 | |||
3890 | max_rand_delay = GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us | ||
3891 | / mean.rel_value_us * (2 / spread); | ||
3892 | /** | ||
3893 | * Compute random value between (0 and 1) * round_interval | ||
3894 | * via multiplying round_interval with a 'fraction' (0 to value)/value | ||
3895 | */ | ||
3896 | rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, | ||
3897 | max_rand_delay); | ||
3898 | ret = GNUNET_TIME_relative_saturating_multiply (mean, rand_delay); | ||
3899 | ret = GNUNET_TIME_relative_divide (ret, max_rand_delay); | ||
3900 | ret = GNUNET_TIME_relative_add (ret, half_interval); | ||
3901 | |||
3902 | if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ret.rel_value_us) | ||
3903 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
3904 | "Returning FOREVER_REL\n"); | ||
3905 | |||
3906 | return ret; | ||
3907 | } | ||
3908 | |||
3909 | |||
3910 | /** | ||
3911 | * Send single pull request | ||
3912 | * | ||
3913 | * @param peer_ctx Context to the peer to send request to | ||
3914 | */ | ||
3915 | static void | ||
3916 | send_pull_request (struct PeerContext *peer_ctx) | ||
3917 | { | ||
3918 | struct GNUNET_MQ_Envelope *ev; | ||
3919 | |||
3920 | GNUNET_assert (GNUNET_NO == check_peer_flag (peer_ctx->sub->peer_map, | ||
3921 | &peer_ctx->peer_id, | ||
3922 | Peers_PULL_REPLY_PENDING)); | ||
3923 | SET_PEER_FLAG (peer_ctx, | ||
3924 | Peers_PULL_REPLY_PENDING); | ||
3925 | peer_ctx->round_pull_req = peer_ctx->sub->num_rounds; | ||
3926 | |||
3927 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
3928 | "Going to send PULL REQUEST to peer %s.\n", | ||
3929 | GNUNET_i2s (&peer_ctx->peer_id)); | ||
3930 | |||
3931 | ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST); | ||
3932 | send_message (peer_ctx, | ||
3933 | ev, | ||
3934 | "PULL REQUEST"); | ||
3935 | if (peer_ctx->sub) | ||
3936 | { | ||
3937 | GNUNET_STATISTICS_update (stats, | ||
3938 | "# pull request send issued", | ||
3939 | 1, | ||
3940 | GNUNET_NO); | ||
3941 | if ((NULL != map_single_hop) && | ||
3942 | (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (map_single_hop, | ||
3943 | &peer_ctx->peer_id))) | ||
3944 | { | ||
3945 | GNUNET_STATISTICS_update (stats, | ||
3946 | "# pull request send issued (multi-hop peer)", | ||
3947 | 1, | ||
3948 | GNUNET_NO); | ||
3949 | } | ||
3950 | } | ||
3951 | } | ||
3952 | |||
3953 | |||
3954 | /** | ||
3955 | * Send single push | ||
3956 | * | ||
3957 | * @param peer_ctx Context of peer to send push to | ||
3958 | */ | ||
3959 | static void | ||
3960 | send_push (struct PeerContext *peer_ctx) | ||
3961 | { | ||
3962 | struct GNUNET_MQ_Envelope *ev; | ||
3963 | |||
3964 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
3965 | "Going to send PUSH to peer %s.\n", | ||
3966 | GNUNET_i2s (&peer_ctx->peer_id)); | ||
3967 | |||
3968 | ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH); | ||
3969 | send_message (peer_ctx, ev, "PUSH"); | ||
3970 | if (peer_ctx->sub) | ||
3971 | { | ||
3972 | GNUNET_STATISTICS_update (stats, | ||
3973 | "# push send issued", | ||
3974 | 1, | ||
3975 | GNUNET_NO); | ||
3976 | if ((NULL != map_single_hop) && | ||
3977 | (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (map_single_hop, | ||
3978 | &peer_ctx->peer_id))) | ||
3979 | { | ||
3980 | GNUNET_STATISTICS_update (stats, | ||
3981 | "# push send issued (multi-hop peer)", | ||
3982 | 1, | ||
3983 | GNUNET_NO); | ||
3984 | } | ||
3985 | } | ||
3986 | } | ||
3987 | |||
3988 | |||
3989 | #if ENABLE_MALICIOUS | ||
3990 | |||
3991 | |||
3992 | /** | ||
3993 | * @brief This function is called, when the client tells us to act malicious. | ||
3994 | * It verifies that @a msg is well-formed. | ||
3995 | * | ||
3996 | * @param cls the closure (#ClientContext) | ||
3997 | * @param msg the message | ||
3998 | * @return #GNUNET_OK if @a msg is well-formed | ||
3999 | */ | ||
4000 | static int | ||
4001 | check_client_act_malicious (void *cls, | ||
4002 | const struct GNUNET_RPS_CS_ActMaliciousMessage *msg) | ||
4003 | { | ||
4004 | struct ClientContext *cli_ctx = cls; | ||
4005 | uint16_t msize = ntohs (msg->header.size); | ||
4006 | uint32_t num_peers = ntohl (msg->num_peers); | ||
4007 | |||
4008 | msize -= sizeof(struct GNUNET_RPS_CS_ActMaliciousMessage); | ||
4009 | if ((msize / sizeof(struct GNUNET_PeerIdentity) != num_peers) || | ||
4010 | (msize % sizeof(struct GNUNET_PeerIdentity) != 0)) | ||
4011 | { | ||
4012 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
4013 | "message says it sends %" PRIu32 " peers, have space for %lu peers\n", | ||
4014 | ntohl (msg->num_peers), | ||
4015 | (msize / sizeof(struct GNUNET_PeerIdentity))); | ||
4016 | GNUNET_break (0); | ||
4017 | GNUNET_SERVICE_client_drop (cli_ctx->client); | ||
4018 | return GNUNET_SYSERR; | ||
4019 | } | ||
4020 | return GNUNET_OK; | ||
4021 | } | ||
4022 | |||
4023 | |||
4024 | /** | ||
4025 | * Turn RPS service to act malicious. | ||
4026 | * | ||
4027 | * @param cls Closure | ||
4028 | * @param client The client that sent the message | ||
4029 | * @param msg The message header | ||
4030 | */ | ||
4031 | static void | ||
4032 | handle_client_act_malicious (void *cls, | ||
4033 | const struct | ||
4034 | GNUNET_RPS_CS_ActMaliciousMessage *msg) | ||
4035 | { | ||
4036 | struct ClientContext *cli_ctx = cls; | ||
4037 | struct GNUNET_PeerIdentity *peers; | ||
4038 | uint32_t num_mal_peers_sent; | ||
4039 | uint32_t num_mal_peers_old; | ||
4040 | struct Sub *sub = cli_ctx->sub; | ||
4041 | |||
4042 | if (NULL == sub) | ||
4043 | sub = msub; | ||
4044 | /* Do actual logic */ | ||
4045 | peers = (struct GNUNET_PeerIdentity *) &msg[1]; | ||
4046 | mal_type = ntohl (msg->type); | ||
4047 | if (NULL == mal_peer_set) | ||
4048 | mal_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO); | ||
4049 | |||
4050 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
4051 | "Now acting malicious type %" PRIu32 ", got %" PRIu32 " peers.\n", | ||
4052 | mal_type, | ||
4053 | ntohl (msg->num_peers)); | ||
4054 | |||
4055 | if (1 == mal_type) | ||
4056 | { /* Try to maximise representation */ | ||
4057 | /* Add other malicious peers to those we already know */ | ||
4058 | |||
4059 | num_mal_peers_sent = ntohl (msg->num_peers); | ||
4060 | num_mal_peers_old = num_mal_peers; | ||
4061 | GNUNET_array_grow (mal_peers, | ||
4062 | num_mal_peers, | ||
4063 | num_mal_peers + num_mal_peers_sent); | ||
4064 | GNUNET_memcpy (&mal_peers[num_mal_peers_old], | ||
4065 | peers, | ||
4066 | num_mal_peers_sent * sizeof(struct GNUNET_PeerIdentity)); | ||
4067 | |||
4068 | /* Add all mal peers to mal_peer_set */ | ||
4069 | add_peer_array_to_set (&mal_peers[num_mal_peers_old], | ||
4070 | num_mal_peers_sent, | ||
4071 | mal_peer_set); | ||
4072 | |||
4073 | /* Substitute do_round () with do_mal_round () */ | ||
4074 | GNUNET_assert (NULL != sub->do_round_task); | ||
4075 | GNUNET_SCHEDULER_cancel (sub->do_round_task); | ||
4076 | sub->do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, sub); | ||
4077 | } | ||
4078 | |||
4079 | else if ((2 == mal_type) || | ||
4080 | (3 == mal_type)) | ||
4081 | { /* Try to partition the network */ | ||
4082 | /* Add other malicious peers to those we already know */ | ||
4083 | |||
4084 | num_mal_peers_sent = ntohl (msg->num_peers) - 1; | ||
4085 | num_mal_peers_old = num_mal_peers; | ||
4086 | GNUNET_assert (GNUNET_MAX_MALLOC_CHECKED > num_mal_peers_sent); | ||
4087 | GNUNET_array_grow (mal_peers, | ||
4088 | num_mal_peers, | ||
4089 | num_mal_peers + num_mal_peers_sent); | ||
4090 | if ((NULL != mal_peers) && | ||
4091 | (0 != num_mal_peers) ) | ||
4092 | { | ||
4093 | GNUNET_memcpy (&mal_peers[num_mal_peers_old], | ||
4094 | peers, | ||
4095 | num_mal_peers_sent * sizeof(struct GNUNET_PeerIdentity)); | ||
4096 | |||
4097 | /* Add all mal peers to mal_peer_set */ | ||
4098 | add_peer_array_to_set (&mal_peers[num_mal_peers_old], | ||
4099 | num_mal_peers_sent, | ||
4100 | mal_peer_set); | ||
4101 | } | ||
4102 | |||
4103 | /* Store the one attacked peer */ | ||
4104 | GNUNET_memcpy (&attacked_peer, | ||
4105 | &msg->attacked_peer, | ||
4106 | sizeof(struct GNUNET_PeerIdentity)); | ||
4107 | /* Set the flag of the attacked peer to valid to avoid problems */ | ||
4108 | if (GNUNET_NO == check_peer_known (sub->peer_map, &attacked_peer)) | ||
4109 | { | ||
4110 | (void) issue_peer_online_check (sub, &attacked_peer); | ||
4111 | } | ||
4112 | |||
4113 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
4114 | "Attacked peer is %s\n", | ||
4115 | GNUNET_i2s (&attacked_peer)); | ||
4116 | |||
4117 | /* Substitute do_round () with do_mal_round () */ | ||
4118 | if (NULL != sub->do_round_task) | ||
4119 | { | ||
4120 | /* Probably in shutdown */ | ||
4121 | GNUNET_SCHEDULER_cancel (sub->do_round_task); | ||
4122 | sub->do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, sub); | ||
4123 | } | ||
4124 | } | ||
4125 | else if (0 == mal_type) | ||
4126 | { /* Stop acting malicious */ | ||
4127 | GNUNET_array_grow (mal_peers, num_mal_peers, 0); | ||
4128 | |||
4129 | /* Substitute do_mal_round () with do_round () */ | ||
4130 | GNUNET_SCHEDULER_cancel (sub->do_round_task); | ||
4131 | sub->do_round_task = GNUNET_SCHEDULER_add_now (&do_round, sub); | ||
4132 | } | ||
4133 | else | ||
4134 | { | ||
4135 | GNUNET_break (0); | ||
4136 | GNUNET_SERVICE_client_continue (cli_ctx->client); | ||
4137 | } | ||
4138 | GNUNET_SERVICE_client_continue (cli_ctx->client); | ||
4139 | } | ||
4140 | |||
4141 | |||
4142 | /** | ||
4143 | * Send out PUSHes and PULLs maliciously. | ||
4144 | * | ||
4145 | * This is executed regylary. | ||
4146 | * | ||
4147 | * @param cls Closure - Sub | ||
4148 | */ | ||
4149 | static void | ||
4150 | do_mal_round (void *cls) | ||
4151 | { | ||
4152 | uint32_t num_pushes; | ||
4153 | uint32_t i; | ||
4154 | struct GNUNET_TIME_Relative time_next_round; | ||
4155 | struct AttackedPeer *tmp_att_peer; | ||
4156 | struct Sub *sub = cls; | ||
4157 | |||
4158 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
4159 | "Going to execute next round maliciously type %" PRIu32 ".\n", | ||
4160 | mal_type); | ||
4161 | sub->do_round_task = NULL; | ||
4162 | GNUNET_assert (mal_type <= 3); | ||
4163 | /* Do malicious actions */ | ||
4164 | if (1 == mal_type) | ||
4165 | { /* Try to maximise representation */ | ||
4166 | /* The maximum of pushes we're going to send this round */ | ||
4167 | num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit, | ||
4168 | num_attacked_peers), | ||
4169 | GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE); | ||
4170 | |||
4171 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
4172 | "Going to send %" PRIu32 " pushes\n", | ||
4173 | num_pushes); | ||
4174 | |||
4175 | /* Send PUSHes to attacked peers */ | ||
4176 | for (i = 0; i < num_pushes; i++) | ||
4177 | { | ||
4178 | if (att_peers_tail == att_peer_index) | ||
4179 | att_peer_index = att_peers_head; | ||
4180 | else | ||
4181 | att_peer_index = att_peer_index->next; | ||
4182 | |||
4183 | send_push (get_peer_ctx (sub->peer_map, &att_peer_index->peer_id)); | ||
4184 | } | ||
4185 | |||
4186 | /* Send PULLs to some peers to learn about additional peers to attack */ | ||
4187 | tmp_att_peer = att_peer_index; | ||
4188 | for (i = 0; i < num_pushes * alpha; i++) | ||
4189 | { | ||
4190 | if (att_peers_tail == tmp_att_peer) | ||
4191 | tmp_att_peer = att_peers_head; | ||
4192 | else | ||
4193 | att_peer_index = tmp_att_peer->next; | ||
4194 | |||
4195 | send_pull_request (get_peer_ctx (sub->peer_map, &tmp_att_peer->peer_id)); | ||
4196 | } | ||
4197 | } | ||
4198 | |||
4199 | |||
4200 | else if (2 == mal_type) | ||
4201 | { /** | ||
4202 | * Try to partition the network | ||
4203 | * Send as many pushes to the attacked peer as possible | ||
4204 | * That is one push per round as it will ignore more. | ||
4205 | */ | ||
4206 | (void) issue_peer_online_check (sub, &attacked_peer); | ||
4207 | if (GNUNET_YES == check_peer_flag (sub->peer_map, | ||
4208 | &attacked_peer, | ||
4209 | Peers_ONLINE)) | ||
4210 | send_push (get_peer_ctx (sub->peer_map, &attacked_peer)); | ||
4211 | } | ||
4212 | |||
4213 | |||
4214 | if (3 == mal_type) | ||
4215 | { /* Combined attack */ | ||
4216 | /* Send PUSH to attacked peers */ | ||
4217 | if (GNUNET_YES == check_peer_known (sub->peer_map, &attacked_peer)) | ||
4218 | { | ||
4219 | (void) issue_peer_online_check (sub, &attacked_peer); | ||
4220 | if (GNUNET_YES == check_peer_flag (sub->peer_map, | ||
4221 | &attacked_peer, | ||
4222 | Peers_ONLINE)) | ||
4223 | { | ||
4224 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
4225 | "Goding to send push to attacked peer (%s)\n", | ||
4226 | GNUNET_i2s (&attacked_peer)); | ||
4227 | send_push (get_peer_ctx (sub->peer_map, &attacked_peer)); | ||
4228 | } | ||
4229 | } | ||
4230 | (void) issue_peer_online_check (sub, &attacked_peer); | ||
4231 | |||
4232 | /* The maximum of pushes we're going to send this round */ | ||
4233 | num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit - 1, | ||
4234 | num_attacked_peers), | ||
4235 | GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE); | ||
4236 | |||
4237 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
4238 | "Going to send %" PRIu32 " pushes\n", | ||
4239 | num_pushes); | ||
4240 | |||
4241 | for (i = 0; i < num_pushes; i++) | ||
4242 | { | ||
4243 | if (att_peers_tail == att_peer_index) | ||
4244 | att_peer_index = att_peers_head; | ||
4245 | else | ||
4246 | att_peer_index = att_peer_index->next; | ||
4247 | |||
4248 | send_push (get_peer_ctx (sub->peer_map, &att_peer_index->peer_id)); | ||
4249 | } | ||
4250 | |||
4251 | /* Send PULLs to some peers to learn about additional peers to attack */ | ||
4252 | tmp_att_peer = att_peer_index; | ||
4253 | for (i = 0; i < num_pushes * alpha; i++) | ||
4254 | { | ||
4255 | if (att_peers_tail == tmp_att_peer) | ||
4256 | tmp_att_peer = att_peers_head; | ||
4257 | else | ||
4258 | att_peer_index = tmp_att_peer->next; | ||
4259 | |||
4260 | send_pull_request (get_peer_ctx (sub->peer_map, &tmp_att_peer->peer_id)); | ||
4261 | } | ||
4262 | } | ||
4263 | |||
4264 | /* Schedule next round */ | ||
4265 | time_next_round = compute_rand_delay (sub->round_interval, 2); | ||
4266 | |||
4267 | GNUNET_assert (NULL == sub->do_round_task); | ||
4268 | sub->do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, | ||
4269 | &do_mal_round, sub); | ||
4270 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n"); | ||
4271 | } | ||
4272 | |||
4273 | |||
4274 | #endif /* ENABLE_MALICIOUS */ | ||
4275 | |||
4276 | |||
4277 | /** | ||
4278 | * Send out PUSHes and PULLs, possibly update #view, samplers. | ||
4279 | * | ||
4280 | * This is executed regylary. | ||
4281 | * | ||
4282 | * @param cls Closure - Sub | ||
4283 | */ | ||
4284 | static void | ||
4285 | do_round (void *cls) | ||
4286 | { | ||
4287 | unsigned int i; | ||
4288 | const struct GNUNET_PeerIdentity *view_array; | ||
4289 | unsigned int *permut; | ||
4290 | unsigned int a_peers; /* Number of peers we send pushes to */ | ||
4291 | unsigned int b_peers; /* Number of peers we send pull requests to */ | ||
4292 | uint32_t first_border; | ||
4293 | uint32_t second_border; | ||
4294 | struct GNUNET_PeerIdentity peer; | ||
4295 | struct GNUNET_PeerIdentity *update_peer; | ||
4296 | struct Sub *sub = cls; | ||
4297 | |||
4298 | sub->num_rounds++; | ||
4299 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
4300 | "Going to execute next round.\n"); | ||
4301 | if (sub == msub) | ||
4302 | { | ||
4303 | GNUNET_STATISTICS_update (stats, "# rounds", 1, GNUNET_NO); | ||
4304 | } | ||
4305 | sub->do_round_task = NULL; | ||
4306 | #ifdef TO_FILE_FULL | ||
4307 | to_file (sub->file_name_view_log, | ||
4308 | "___ new round ___"); | ||
4309 | #endif /* TO_FILE_FULL */ | ||
4310 | view_array = View_get_as_array (sub->view); | ||
4311 | for (i = 0; i < View_size (sub->view); i++) | ||
4312 | { | ||
4313 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
4314 | "\t%s\n", GNUNET_i2s (&view_array[i])); | ||
4315 | #ifdef TO_FILE_FULL | ||
4316 | to_file (sub->file_name_view_log, | ||
4317 | "=%s\t(do round)", | ||
4318 | GNUNET_i2s_full (&view_array[i])); | ||
4319 | #endif /* TO_FILE_FULL */ | ||
4320 | } | ||
4321 | |||
4322 | |||
4323 | /* Send pushes and pull requests */ | ||
4324 | if (0 < View_size (sub->view)) | ||
4325 | { | ||
4326 | permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG, | ||
4327 | View_size (sub->view)); | ||
4328 | |||
4329 | /* Send PUSHes */ | ||
4330 | a_peers = ceil (alpha * View_size (sub->view)); | ||
4331 | |||
4332 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
4333 | "Going to send pushes to %u (ceil (%f * %u)) peers.\n", | ||
4334 | a_peers, alpha, View_size (sub->view)); | ||
4335 | for (i = 0; i < a_peers; i++) | ||
4336 | { | ||
4337 | peer = view_array[permut[i]]; | ||
4338 | // FIXME if this fails schedule/loop this for later | ||
4339 | send_push (get_peer_ctx (sub->peer_map, &peer)); | ||
4340 | } | ||
4341 | |||
4342 | /* Send PULL requests */ | ||
4343 | b_peers = ceil (beta * View_size (sub->view)); | ||
4344 | first_border = a_peers; | ||
4345 | second_border = a_peers + b_peers; | ||
4346 | if (second_border > View_size (sub->view)) | ||
4347 | { | ||
4348 | first_border = View_size (sub->view) - b_peers; | ||
4349 | second_border = View_size (sub->view); | ||
4350 | } | ||
4351 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
4352 | "Going to send pulls to %u (ceil (%f * %u)) peers.\n", | ||
4353 | b_peers, beta, View_size (sub->view)); | ||
4354 | for (i = first_border; i < second_border; i++) | ||
4355 | { | ||
4356 | peer = view_array[permut[i]]; | ||
4357 | if (GNUNET_NO == check_peer_flag (sub->peer_map, | ||
4358 | &peer, | ||
4359 | Peers_PULL_REPLY_PENDING)) | ||
4360 | { // FIXME if this fails schedule/loop this for later | ||
4361 | send_pull_request (get_peer_ctx (sub->peer_map, &peer)); | ||
4362 | } | ||
4363 | } | ||
4364 | |||
4365 | GNUNET_free (permut); | ||
4366 | permut = NULL; | ||
4367 | } | ||
4368 | |||
4369 | |||
4370 | /* Update view */ | ||
4371 | /* TODO see how many peers are in push-/pull- list! */ | ||
4372 | |||
4373 | if ((CustomPeerMap_size (sub->push_map) <= alpha * sub->view_size_est_need) && | ||
4374 | (0 < CustomPeerMap_size (sub->push_map)) && | ||
4375 | (0 < CustomPeerMap_size (sub->pull_map))) | ||
4376 | { /* If conditions for update are fulfilled, update */ | ||
4377 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the view.\n"); | ||
4378 | |||
4379 | uint32_t final_size; | ||
4380 | uint32_t peers_to_clean_size; | ||
4381 | struct GNUNET_PeerIdentity *peers_to_clean; | ||
4382 | |||
4383 | peers_to_clean = NULL; | ||
4384 | peers_to_clean_size = 0; | ||
4385 | GNUNET_array_grow (peers_to_clean, | ||
4386 | peers_to_clean_size, | ||
4387 | View_size (sub->view)); | ||
4388 | GNUNET_memcpy (peers_to_clean, | ||
4389 | view_array, | ||
4390 | View_size (sub->view) * sizeof(struct GNUNET_PeerIdentity)); | ||
4391 | |||
4392 | /* Seems like recreating is the easiest way of emptying the peermap */ | ||
4393 | View_clear (sub->view); | ||
4394 | #ifdef TO_FILE_FULL | ||
4395 | to_file (sub->file_name_view_log, | ||
4396 | "--- emptied ---"); | ||
4397 | #endif /* TO_FILE_FULL */ | ||
4398 | |||
4399 | first_border = GNUNET_MIN (ceil (alpha * sub->view_size_est_need), | ||
4400 | CustomPeerMap_size (sub->push_map)); | ||
4401 | second_border = first_border | ||
4402 | + GNUNET_MIN (floor (beta * sub->view_size_est_need), | ||
4403 | CustomPeerMap_size (sub->pull_map)); | ||
4404 | final_size = second_border | ||
4405 | + ceil ((1 - (alpha + beta)) * sub->view_size_est_need); | ||
4406 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
4407 | "first border: %" PRIu32 ", second border: %" PRIu32 ", final size: %" | ||
4408 | PRIu32 "\n", | ||
4409 | first_border, | ||
4410 | second_border, | ||
4411 | final_size); | ||
4412 | |||
4413 | /* Update view with peers received through PUSHes */ | ||
4414 | permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG, | ||
4415 | CustomPeerMap_size (sub->push_map)); | ||
4416 | for (i = 0; i < first_border; i++) | ||
4417 | { | ||
4418 | int inserted; | ||
4419 | inserted = insert_in_view (sub, | ||
4420 | CustomPeerMap_get_peer_by_index (sub->push_map, | ||
4421 | permut[i])); | ||
4422 | if (GNUNET_OK == inserted) | ||
4423 | { | ||
4424 | clients_notify_stream_peer (sub, | ||
4425 | 1, | ||
4426 | CustomPeerMap_get_peer_by_index ( | ||
4427 | sub->push_map, permut[i])); | ||
4428 | } | ||
4429 | #ifdef TO_FILE_FULL | ||
4430 | to_file (sub->file_name_view_log, | ||
4431 | "+%s\t(push list)", | ||
4432 | GNUNET_i2s_full (&view_array[i])); | ||
4433 | #endif /* TO_FILE_FULL */ | ||
4434 | // TODO change the peer_flags accordingly | ||
4435 | } | ||
4436 | GNUNET_free (permut); | ||
4437 | permut = NULL; | ||
4438 | |||
4439 | /* Update view with peers received through PULLs */ | ||
4440 | permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG, | ||
4441 | CustomPeerMap_size (sub->pull_map)); | ||
4442 | for (i = first_border; i < second_border; i++) | ||
4443 | { | ||
4444 | int inserted; | ||
4445 | inserted = insert_in_view (sub, | ||
4446 | CustomPeerMap_get_peer_by_index (sub->pull_map, | ||
4447 | permut[i | ||
4448 | - | ||
4449 | first_border | ||
4450 | ])); | ||
4451 | if (GNUNET_OK == inserted) | ||
4452 | { | ||
4453 | clients_notify_stream_peer (sub, | ||
4454 | 1, | ||
4455 | CustomPeerMap_get_peer_by_index ( | ||
4456 | sub->pull_map, | ||
4457 | permut[i | ||
4458 | - first_border])); | ||
4459 | } | ||
4460 | #ifdef TO_FILE_FULL | ||
4461 | to_file (sub->file_name_view_log, | ||
4462 | "+%s\t(pull list)", | ||
4463 | GNUNET_i2s_full (&view_array[i])); | ||
4464 | #endif /* TO_FILE_FULL */ | ||
4465 | // TODO change the peer_flags accordingly | ||
4466 | } | ||
4467 | GNUNET_free (permut); | ||
4468 | permut = NULL; | ||
4469 | |||
4470 | /* Update view with peers from history */ | ||
4471 | RPS_sampler_get_n_rand_peers (sub->sampler, | ||
4472 | final_size - second_border, | ||
4473 | hist_update, | ||
4474 | sub); | ||
4475 | // TODO change the peer_flags accordingly | ||
4476 | |||
4477 | for (i = 0; i < View_size (sub->view); i++) | ||
4478 | rem_from_list (&peers_to_clean, &peers_to_clean_size, &view_array[i]); | ||
4479 | |||
4480 | /* Clean peers that were removed from the view */ | ||
4481 | for (i = 0; i < peers_to_clean_size; i++) | ||
4482 | { | ||
4483 | #ifdef TO_FILE_FULL | ||
4484 | to_file (sub->file_name_view_log, | ||
4485 | "-%s", | ||
4486 | GNUNET_i2s_full (&peers_to_clean[i])); | ||
4487 | #endif /* TO_FILE_FULL */ | ||
4488 | clean_peer (sub, &peers_to_clean[i]); | ||
4489 | } | ||
4490 | |||
4491 | GNUNET_array_grow (peers_to_clean, peers_to_clean_size, 0); | ||
4492 | clients_notify_view_update (sub); | ||
4493 | } | ||
4494 | else | ||
4495 | { | ||
4496 | LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the view.\n"); | ||
4497 | if (sub == msub) | ||
4498 | { | ||
4499 | GNUNET_STATISTICS_update (stats, "# rounds blocked", 1, GNUNET_NO); | ||
4500 | if ((CustomPeerMap_size (sub->push_map) > alpha | ||
4501 | * sub->view_size_est_need) && | ||
4502 | ! (0 >= CustomPeerMap_size (sub->pull_map))) | ||
4503 | GNUNET_STATISTICS_update (stats, "# rounds blocked - too many pushes", | ||
4504 | 1, GNUNET_NO); | ||
4505 | if ((CustomPeerMap_size (sub->push_map) > alpha | ||
4506 | * sub->view_size_est_need) && | ||
4507 | (0 >= CustomPeerMap_size (sub->pull_map))) | ||
4508 | GNUNET_STATISTICS_update (stats, | ||
4509 | "# rounds blocked - too many pushes, no pull replies", | ||
4510 | 1, GNUNET_NO); | ||
4511 | if ((0 >= CustomPeerMap_size (sub->push_map)) && | ||
4512 | ! (0 >= CustomPeerMap_size (sub->pull_map))) | ||
4513 | GNUNET_STATISTICS_update (stats, "# rounds blocked - no pushes", 1, | ||
4514 | GNUNET_NO); | ||
4515 | if ((0 >= CustomPeerMap_size (sub->push_map)) && | ||
4516 | (0 >= CustomPeerMap_size (sub->pull_map))) | ||
4517 | GNUNET_STATISTICS_update (stats, | ||
4518 | "# rounds blocked - no pushes, no pull replies", | ||
4519 | 1, GNUNET_NO); | ||
4520 | if ((0 >= CustomPeerMap_size (sub->pull_map)) && | ||
4521 | (CustomPeerMap_size (sub->push_map) > alpha | ||
4522 | * sub->view_size_est_need) && | ||
4523 | (0 >= CustomPeerMap_size (sub->push_map)) ) | ||
4524 | GNUNET_STATISTICS_update (stats, "# rounds blocked - no pull replies", | ||
4525 | 1, GNUNET_NO); | ||
4526 | } | ||
4527 | } | ||
4528 | // TODO independent of that also get some peers from CADET_get_peers()? | ||
4529 | if (CustomPeerMap_size (sub->push_map) < HISTOGRAM_FILE_SLOTS) | ||
4530 | { | ||
4531 | sub->push_recv[CustomPeerMap_size (sub->push_map)]++; | ||
4532 | } | ||
4533 | else | ||
4534 | { | ||
4535 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
4536 | "Push map size too big for histogram (%u, %u)\n", | ||
4537 | CustomPeerMap_size (sub->push_map), | ||
4538 | HISTOGRAM_FILE_SLOTS); | ||
4539 | } | ||
4540 | // FIXME check bounds of histogram | ||
4541 | sub->push_delta[(int32_t) (CustomPeerMap_size (sub->push_map) | ||
4542 | - (alpha * sub->view_size_est_need)) | ||
4543 | + (HISTOGRAM_FILE_SLOTS / 2)]++; | ||
4544 | if (sub == msub) | ||
4545 | { | ||
4546 | GNUNET_STATISTICS_set (stats, | ||
4547 | "# peers in push map at end of round", | ||
4548 | CustomPeerMap_size (sub->push_map), | ||
4549 | GNUNET_NO); | ||
4550 | GNUNET_STATISTICS_set (stats, | ||
4551 | "# peers in pull map at end of round", | ||
4552 | CustomPeerMap_size (sub->pull_map), | ||
4553 | GNUNET_NO); | ||
4554 | GNUNET_STATISTICS_set (stats, | ||
4555 | "# peers in view at end of round", | ||
4556 | View_size (sub->view), | ||
4557 | GNUNET_NO); | ||
4558 | GNUNET_STATISTICS_set (stats, | ||
4559 | "# expected pushes", | ||
4560 | alpha * sub->view_size_est_need, | ||
4561 | GNUNET_NO); | ||
4562 | GNUNET_STATISTICS_set (stats, | ||
4563 | "delta expected - received pushes", | ||
4564 | CustomPeerMap_size (sub->push_map) - (alpha | ||
4565 | * sub-> | ||
4566 | view_size_est_need), | ||
4567 | GNUNET_NO); | ||
4568 | } | ||
4569 | |||
4570 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
4571 | "Received %u pushes and %u pulls last round (alpha (%.2f) * view_size (sub->view%u) = %.2f)\n", | ||
4572 | CustomPeerMap_size (sub->push_map), | ||
4573 | CustomPeerMap_size (sub->pull_map), | ||
4574 | alpha, | ||
4575 | View_size (sub->view), | ||
4576 | alpha * View_size (sub->view)); | ||
4577 | |||
4578 | /* Update samplers */ | ||
4579 | for (i = 0; i < CustomPeerMap_size (sub->push_map); i++) | ||
4580 | { | ||
4581 | update_peer = CustomPeerMap_get_peer_by_index (sub->push_map, i); | ||
4582 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
4583 | "Updating with peer %s from push list\n", | ||
4584 | GNUNET_i2s (update_peer)); | ||
4585 | insert_in_sampler (sub, update_peer); | ||
4586 | clean_peer (sub, update_peer); /* This cleans only if it is not in the view */ | ||
4587 | } | ||
4588 | |||
4589 | for (i = 0; i < CustomPeerMap_size (sub->pull_map); i++) | ||
4590 | { | ||
4591 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
4592 | "Updating with peer %s from pull list\n", | ||
4593 | GNUNET_i2s (CustomPeerMap_get_peer_by_index (sub->pull_map, i))); | ||
4594 | insert_in_sampler (sub, CustomPeerMap_get_peer_by_index (sub->pull_map, i)); | ||
4595 | /* This cleans only if it is not in the view */ | ||
4596 | clean_peer (sub, CustomPeerMap_get_peer_by_index (sub->pull_map, i)); | ||
4597 | } | ||
4598 | |||
4599 | |||
4600 | /* Empty push/pull lists */ | ||
4601 | CustomPeerMap_clear (sub->push_map); | ||
4602 | CustomPeerMap_clear (sub->pull_map); | ||
4603 | |||
4604 | if (sub == msub) | ||
4605 | { | ||
4606 | GNUNET_STATISTICS_set (stats, | ||
4607 | "view size", | ||
4608 | View_size (sub->view), | ||
4609 | GNUNET_NO); | ||
4610 | } | ||
4611 | |||
4612 | struct GNUNET_TIME_Relative time_next_round; | ||
4613 | |||
4614 | time_next_round = compute_rand_delay (sub->round_interval, 2); | ||
4615 | |||
4616 | /* Schedule next round */ | ||
4617 | sub->do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, | ||
4618 | &do_round, sub); | ||
4619 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n"); | ||
4620 | } | ||
4621 | |||
4622 | |||
4623 | /** | ||
4624 | * This is called from GNUNET_CADET_get_peers(). | ||
4625 | * | ||
4626 | * It is called on every peer(ID) that cadet somehow has contact with. | ||
4627 | * We use those to initialise the sampler. | ||
4628 | * | ||
4629 | * implements #GNUNET_CADET_PeersCB | ||
4630 | * | ||
4631 | * @param cls Closure - Sub | ||
4632 | * @param peer Peer, or NULL on "EOF". | ||
4633 | * @param tunnel Do we have a tunnel towards this peer? | ||
4634 | * @param n_paths Number of known paths towards this peer. | ||
4635 | * @param best_path How long is the best path? | ||
4636 | * (0 = unknown, 1 = ourselves, 2 = neighbor) | ||
4637 | */ | ||
4638 | void | ||
4639 | init_peer_cb (void *cls, | ||
4640 | const struct GNUNET_PeerIdentity *peer, | ||
4641 | int tunnel, /* "Do we have a tunnel towards this peer?" */ | ||
4642 | unsigned int n_paths, /* "Number of known paths towards this peer" */ | ||
4643 | unsigned int best_path) /* "How long is the best path? | ||
4644 | * (0 = unknown, 1 = ourselves, 2 = neighbor)" */ | ||
4645 | { | ||
4646 | struct Sub *sub = cls; | ||
4647 | |||
4648 | (void) tunnel; | ||
4649 | (void) n_paths; | ||
4650 | (void) best_path; | ||
4651 | |||
4652 | if (NULL != peer) | ||
4653 | { | ||
4654 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
4655 | "Got peer_id %s from cadet\n", | ||
4656 | GNUNET_i2s (peer)); | ||
4657 | got_peer (sub, peer); | ||
4658 | } | ||
4659 | } | ||
4660 | |||
4661 | |||
4662 | /** | ||
4663 | * @brief Iterator function over stored, valid peers. | ||
4664 | * | ||
4665 | * We initialise the sampler with those. | ||
4666 | * | ||
4667 | * @param cls Closure - Sub | ||
4668 | * @param peer the peer id | ||
4669 | * @return #GNUNET_YES if we should continue to | ||
4670 | * iterate, | ||
4671 | * #GNUNET_NO if not. | ||
4672 | */ | ||
4673 | static int | ||
4674 | valid_peers_iterator (void *cls, | ||
4675 | const struct GNUNET_PeerIdentity *peer) | ||
4676 | { | ||
4677 | struct Sub *sub = cls; | ||
4678 | |||
4679 | if (NULL != peer) | ||
4680 | { | ||
4681 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
4682 | "Got stored, valid peer %s\n", | ||
4683 | GNUNET_i2s (peer)); | ||
4684 | got_peer (sub, peer); | ||
4685 | } | ||
4686 | return GNUNET_YES; | ||
4687 | } | ||
4688 | |||
4689 | |||
4690 | /** | ||
4691 | * Iterator over peers from peerinfo. | ||
4692 | * | ||
4693 | * @param cls Closure - Sub | ||
4694 | * @param peer id of the peer, NULL for last call | ||
4695 | * @param hello hello message for the peer (can be NULL) | ||
4696 | * @param err_msg error message | ||
4697 | */ | ||
4698 | void | ||
4699 | process_peerinfo_peers (void *cls, | ||
4700 | const struct GNUNET_PeerIdentity *peer, | ||
4701 | const struct GNUNET_MessageHeader *hello, | ||
4702 | const char *emsg) | ||
4703 | { | ||
4704 | struct Sub *sub = cls; | ||
4705 | |||
4706 | (void) hello; | ||
4707 | (void) emsg; | ||
4708 | |||
4709 | if (NULL != peer) | ||
4710 | { | ||
4711 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
4712 | "Got peer_id %s from peerinfo\n", | ||
4713 | GNUNET_i2s (peer)); | ||
4714 | got_peer (sub, peer); | ||
4715 | } | ||
4716 | } | ||
4717 | |||
4718 | |||
4719 | /** | ||
4720 | * Task run during shutdown. | ||
4721 | * | ||
4722 | * @param cls Closure - unused | ||
4723 | */ | ||
4724 | static void | ||
4725 | shutdown_task (void *cls) | ||
4726 | { | ||
4727 | (void) cls; | ||
4728 | struct ClientContext *client_ctx; | ||
4729 | |||
4730 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
4731 | "RPS service is going down\n"); | ||
4732 | |||
4733 | /* Clean all clients */ | ||
4734 | for (client_ctx = cli_ctx_head; | ||
4735 | NULL != cli_ctx_head; | ||
4736 | client_ctx = cli_ctx_head) | ||
4737 | { | ||
4738 | destroy_cli_ctx (client_ctx); | ||
4739 | } | ||
4740 | if (NULL != msub) | ||
4741 | { | ||
4742 | destroy_sub (msub); | ||
4743 | msub = NULL; | ||
4744 | } | ||
4745 | |||
4746 | /* Disconnect from other services */ | ||
4747 | GNUNET_PEERSTORE_hello_changed_notify_cancel (peerstore_notify); | ||
4748 | GNUNET_PEERSTORE_disconnect (peerstore, GNUNET_YES); | ||
4749 | peerstore = NULL; | ||
4750 | GNUNET_NSE_disconnect (nse); | ||
4751 | if (NULL != map_single_hop) | ||
4752 | { | ||
4753 | /* core_init was called - core was initialised */ | ||
4754 | /* disconnect first, so no callback tries to access missing peermap */ | ||
4755 | GNUNET_CORE_disconnect (core_handle); | ||
4756 | core_handle = NULL; | ||
4757 | GNUNET_CONTAINER_multipeermap_destroy (map_single_hop); | ||
4758 | map_single_hop = NULL; | ||
4759 | } | ||
4760 | |||
4761 | if (NULL != stats) | ||
4762 | { | ||
4763 | GNUNET_STATISTICS_destroy (stats, | ||
4764 | GNUNET_NO); | ||
4765 | stats = NULL; | ||
4766 | } | ||
4767 | GNUNET_CADET_disconnect (cadet_handle); | ||
4768 | cadet_handle = NULL; | ||
4769 | #if ENABLE_MALICIOUS | ||
4770 | struct AttackedPeer *tmp_att_peer; | ||
4771 | GNUNET_array_grow (mal_peers, | ||
4772 | num_mal_peers, | ||
4773 | 0); | ||
4774 | if (NULL != mal_peer_set) | ||
4775 | GNUNET_CONTAINER_multipeermap_destroy (mal_peer_set); | ||
4776 | if (NULL != att_peer_set) | ||
4777 | GNUNET_CONTAINER_multipeermap_destroy (att_peer_set); | ||
4778 | while (NULL != att_peers_head) | ||
4779 | { | ||
4780 | tmp_att_peer = att_peers_head; | ||
4781 | GNUNET_CONTAINER_DLL_remove (att_peers_head, | ||
4782 | att_peers_tail, | ||
4783 | tmp_att_peer); | ||
4784 | GNUNET_free (tmp_att_peer); | ||
4785 | } | ||
4786 | #endif /* ENABLE_MALICIOUS */ | ||
4787 | close_all_files (); | ||
4788 | } | ||
4789 | |||
4790 | |||
4791 | /** | ||
4792 | * Handle client connecting to the service. | ||
4793 | * | ||
4794 | * @param cls unused | ||
4795 | * @param client the new client | ||
4796 | * @param mq the message queue of @a client | ||
4797 | * @return @a client | ||
4798 | */ | ||
4799 | static void * | ||
4800 | client_connect_cb (void *cls, | ||
4801 | struct GNUNET_SERVICE_Client *client, | ||
4802 | struct GNUNET_MQ_Handle *mq) | ||
4803 | { | ||
4804 | struct ClientContext *cli_ctx; | ||
4805 | |||
4806 | (void) cls; | ||
4807 | |||
4808 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
4809 | "Client connected\n"); | ||
4810 | if (NULL == client) | ||
4811 | return client; /* Server was destroyed before a client connected. Shutting down */ | ||
4812 | cli_ctx = GNUNET_new (struct ClientContext); | ||
4813 | cli_ctx->mq = mq; | ||
4814 | cli_ctx->view_updates_left = -1; | ||
4815 | cli_ctx->stream_update = GNUNET_NO; | ||
4816 | cli_ctx->client = client; | ||
4817 | GNUNET_CONTAINER_DLL_insert (cli_ctx_head, | ||
4818 | cli_ctx_tail, | ||
4819 | cli_ctx); | ||
4820 | return cli_ctx; | ||
4821 | } | ||
4822 | |||
4823 | |||
4824 | /** | ||
4825 | * Callback called when a client disconnected from the service | ||
4826 | * | ||
4827 | * @param cls closure for the service | ||
4828 | * @param client the client that disconnected | ||
4829 | * @param internal_cls should be equal to @a c | ||
4830 | */ | ||
4831 | static void | ||
4832 | client_disconnect_cb (void *cls, | ||
4833 | struct GNUNET_SERVICE_Client *client, | ||
4834 | void *internal_cls) | ||
4835 | { | ||
4836 | struct ClientContext *cli_ctx = internal_cls; | ||
4837 | |||
4838 | (void) cls; | ||
4839 | GNUNET_assert (client == cli_ctx->client); | ||
4840 | if (NULL == client) | ||
4841 | { /* shutdown task - destroy all clients */ | ||
4842 | while (NULL != cli_ctx_head) | ||
4843 | destroy_cli_ctx (cli_ctx_head); | ||
4844 | } | ||
4845 | else | ||
4846 | { /* destroy this client */ | ||
4847 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
4848 | "Client disconnected. Destroy its context.\n"); | ||
4849 | destroy_cli_ctx (cli_ctx); | ||
4850 | } | ||
4851 | } | ||
4852 | |||
4853 | |||
4854 | /** | ||
4855 | * Handle random peer sampling clients. | ||
4856 | * | ||
4857 | * @param cls closure | ||
4858 | * @param c configuration to use | ||
4859 | * @param service the initialized service | ||
4860 | */ | ||
4861 | static void | ||
4862 | run (void *cls, | ||
4863 | const struct GNUNET_CONFIGURATION_Handle *c, | ||
4864 | struct GNUNET_SERVICE_Handle *service) | ||
4865 | { | ||
4866 | struct GNUNET_TIME_Relative round_interval; | ||
4867 | long long unsigned int sampler_size; | ||
4868 | char hash_port_string[] = GNUNET_APPLICATION_PORT_RPS; | ||
4869 | struct GNUNET_HashCode hash; | ||
4870 | |||
4871 | (void) cls; | ||
4872 | (void) service; | ||
4873 | |||
4874 | GNUNET_log_setup ("rps", | ||
4875 | GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), | ||
4876 | NULL); | ||
4877 | cfg = c; | ||
4878 | /* Get own ID */ | ||
4879 | GNUNET_CRYPTO_get_peer_identity (cfg, | ||
4880 | &own_identity); // TODO check return value | ||
4881 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
4882 | "STARTING SERVICE (rps) for peer [%s]\n", | ||
4883 | GNUNET_i2s (&own_identity)); | ||
4884 | #if ENABLE_MALICIOUS | ||
4885 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
4886 | "Malicious execution compiled in.\n"); | ||
4887 | #endif /* ENABLE_MALICIOUS */ | ||
4888 | |||
4889 | /* Get time interval from the configuration */ | ||
4890 | if (GNUNET_OK != | ||
4891 | GNUNET_CONFIGURATION_get_value_time (cfg, | ||
4892 | "RPS", | ||
4893 | "ROUNDINTERVAL", | ||
4894 | &round_interval)) | ||
4895 | { | ||
4896 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | ||
4897 | "RPS", "ROUNDINTERVAL"); | ||
4898 | GNUNET_SCHEDULER_shutdown (); | ||
4899 | return; | ||
4900 | } | ||
4901 | |||
4902 | /* Get initial size of sampler/view from the configuration */ | ||
4903 | if (GNUNET_OK != | ||
4904 | GNUNET_CONFIGURATION_get_value_number (cfg, | ||
4905 | "RPS", | ||
4906 | "MINSIZE", | ||
4907 | &sampler_size)) | ||
4908 | { | ||
4909 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | ||
4910 | "RPS", "MINSIZE"); | ||
4911 | GNUNET_SCHEDULER_shutdown (); | ||
4912 | return; | ||
4913 | } | ||
4914 | |||
4915 | cadet_handle = GNUNET_CADET_connect (cfg); | ||
4916 | GNUNET_assert (NULL != cadet_handle); | ||
4917 | core_handle = GNUNET_CORE_connect (cfg, | ||
4918 | NULL, /* cls */ | ||
4919 | core_init, /* init */ | ||
4920 | core_connects, /* connects */ | ||
4921 | core_disconnects, /* disconnects */ | ||
4922 | NULL); /* handlers */ | ||
4923 | GNUNET_assert (NULL != core_handle); | ||
4924 | |||
4925 | |||
4926 | alpha = 0.45; | ||
4927 | beta = 0.45; | ||
4928 | |||
4929 | |||
4930 | /* Set up main Sub */ | ||
4931 | GNUNET_CRYPTO_hash (hash_port_string, | ||
4932 | strlen (hash_port_string), | ||
4933 | &hash); | ||
4934 | msub = new_sub (&hash, | ||
4935 | sampler_size, /* Will be overwritten by config */ | ||
4936 | round_interval); | ||
4937 | |||
4938 | |||
4939 | peerstore = GNUNET_PEERSTORE_connect (cfg); | ||
4940 | |||
4941 | /* connect to NSE */ | ||
4942 | nse = GNUNET_NSE_connect (cfg, nse_callback, NULL); | ||
4943 | |||
4944 | // LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n"); | ||
4945 | // GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, msub); | ||
4946 | // TODO send push/pull to each of those peers? | ||
4947 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting stored valid peers\n"); | ||
4948 | restore_valid_peers (msub); | ||
4949 | get_valid_peers (msub->valid_peers, valid_peers_iterator, msub); | ||
4950 | |||
4951 | peerstore_notify = GNUNET_PEERSTORE_hello_changed_notify (peerstore, | ||
4952 | GNUNET_NO, | ||
4953 | process_peerinfo_peers, | ||
4954 | msub); | ||
4955 | |||
4956 | LOG (GNUNET_ERROR_TYPE_INFO, "Ready to receive requests from clients\n"); | ||
4957 | |||
4958 | GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL); | ||
4959 | stats = GNUNET_STATISTICS_create ("rps", cfg); | ||
4960 | } | ||
4961 | |||
4962 | |||
4963 | /** | ||
4964 | * Define "main" method using service macro. | ||
4965 | */ | ||
4966 | GNUNET_SERVICE_MAIN | ||
4967 | ("rps", | ||
4968 | GNUNET_SERVICE_OPTION_NONE, | ||
4969 | &run, | ||
4970 | &client_connect_cb, | ||
4971 | &client_disconnect_cb, | ||
4972 | NULL, | ||
4973 | GNUNET_MQ_hd_var_size (client_seed, | ||
4974 | GNUNET_MESSAGE_TYPE_RPS_CS_SEED, | ||
4975 | struct GNUNET_RPS_CS_SeedMessage, | ||
4976 | NULL), | ||
4977 | #if ENABLE_MALICIOUS | ||
4978 | GNUNET_MQ_hd_var_size (client_act_malicious, | ||
4979 | GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS, | ||
4980 | struct GNUNET_RPS_CS_ActMaliciousMessage, | ||
4981 | NULL), | ||
4982 | #endif /* ENABLE_MALICIOUS */ | ||
4983 | GNUNET_MQ_hd_fixed_size (client_view_request, | ||
4984 | GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REQUEST, | ||
4985 | struct GNUNET_RPS_CS_DEBUG_ViewRequest, | ||
4986 | NULL), | ||
4987 | GNUNET_MQ_hd_fixed_size (client_view_cancel, | ||
4988 | GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_CANCEL, | ||
4989 | struct GNUNET_MessageHeader, | ||
4990 | NULL), | ||
4991 | GNUNET_MQ_hd_fixed_size (client_stream_request, | ||
4992 | GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_REQUEST, | ||
4993 | struct GNUNET_RPS_CS_DEBUG_StreamRequest, | ||
4994 | NULL), | ||
4995 | GNUNET_MQ_hd_fixed_size (client_stream_cancel, | ||
4996 | GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_CANCEL, | ||
4997 | struct GNUNET_MessageHeader, | ||
4998 | NULL), | ||
4999 | GNUNET_MQ_hd_fixed_size (client_start_sub, | ||
5000 | GNUNET_MESSAGE_TYPE_RPS_CS_SUB_START, | ||
5001 | struct GNUNET_RPS_CS_SubStartMessage, | ||
5002 | NULL), | ||
5003 | GNUNET_MQ_hd_fixed_size (client_stop_sub, | ||
5004 | GNUNET_MESSAGE_TYPE_RPS_CS_SUB_STOP, | ||
5005 | struct GNUNET_RPS_CS_SubStopMessage, | ||
5006 | NULL), | ||
5007 | GNUNET_MQ_handler_end ()); | ||
5008 | |||
5009 | /* end of gnunet-service-rps.c */ | ||