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