rps: fix double call
[oweals/gnunet.git] / src / rps / gnunet-service-rps.c
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
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
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_peerinfo_service.h"
31 #include "gnunet_nse_service.h"
32 #include "rps.h"
33 #include "rps-test_util.h"
34 #include "gnunet-service-rps_sampler.h"
35 #include "gnunet-service-rps_custommap.h"
36 #include "gnunet-service-rps_peers.h"
37 #include "gnunet-service-rps_view.h"
38
39 #include <math.h>
40 #include <inttypes.h>
41
42 #define LOG(kind, ...) GNUNET_log(kind, __VA_ARGS__)
43
44 // TODO modify @brief in every file
45
46 // TODO check for overflows
47
48 // TODO align message structs
49
50 // TODO connect to friends
51
52 // TODO store peers somewhere persistent
53
54 // TODO blacklist? (-> mal peer detection on top of brahms)
55
56 // hist_size_init, hist_size_max
57
58 /**
59  * Our configuration.
60  */
61 static const struct GNUNET_CONFIGURATION_Handle *cfg;
62
63 /**
64  * Our own identity.
65  */
66 static struct GNUNET_PeerIdentity own_identity;
67
68
69 /***********************************************************************
70  * Housekeeping with clients
71 ***********************************************************************/
72
73 /**
74  * Closure used to pass the client and the id to the callback
75  * that replies to a client's request
76  */
77 struct ReplyCls
78 {
79   /**
80    * DLL
81    */
82   struct ReplyCls *next;
83   struct ReplyCls *prev;
84
85   /**
86    * The identifier of the request
87    */
88   uint32_t id;
89
90   /**
91    * The handle to the request
92    */
93   struct RPS_SamplerRequestHandle *req_handle;
94
95   /**
96    * The client handle to send the reply to
97    */
98   struct ClientContext *cli_ctx;
99 };
100
101
102 /**
103  * Struct used to store the context of a connected client.
104  */
105 struct ClientContext
106 {
107   /**
108    * DLL
109    */
110   struct ClientContext *next;
111   struct ClientContext *prev;
112
113   /**
114    * The message queue to communicate with the client.
115    */
116   struct GNUNET_MQ_Handle *mq;
117
118   /**
119    * DLL with handles to single requests from the client
120    */
121   struct ReplyCls *rep_cls_head;
122   struct ReplyCls *rep_cls_tail;
123
124   /**
125    * The client handle to send the reply to
126    */
127   struct GNUNET_SERVICE_Client *client;
128 };
129
130 /**
131  * DLL with all clients currently connected to us
132  */
133 struct ClientContext *cli_ctx_head;
134 struct ClientContext *cli_ctx_tail;
135
136 /***********************************************************************
137  * /Housekeeping with clients
138 ***********************************************************************/
139
140
141
142
143
144 /***********************************************************************
145  * Globals
146 ***********************************************************************/
147
148 /**
149  * Sampler used for the Brahms protocol itself.
150  */
151 static struct RPS_Sampler *prot_sampler;
152
153 /**
154  * Sampler used for the clients.
155  */
156 static struct RPS_Sampler *client_sampler;
157
158 /**
159  * Name to log view to
160  */
161 static char *file_name_view_log;
162
163 /**
164  * The size of sampler we need to be able to satisfy the client's need
165  * of random peers.
166  */
167 static unsigned int sampler_size_client_need;
168
169 /**
170  * The size of sampler we need to be able to satisfy the Brahms protocol's
171  * need of random peers.
172  *
173  * This is one minimum size the sampler grows to.
174  */
175 static unsigned int sampler_size_est_need;
176
177 /**
178  * Percentage of total peer number in the view
179  * to send random PUSHes to
180  */
181 static float alpha;
182
183 /**
184  * Percentage of total peer number in the view
185  * to send random PULLs to
186  */
187 static float beta;
188
189 /**
190  * Identifier for the main task that runs periodically.
191  */
192 static struct GNUNET_SCHEDULER_Task *do_round_task;
193
194 /**
195  * Time inverval the do_round task runs in.
196  */
197 static struct GNUNET_TIME_Relative round_interval;
198
199 /**
200  * List to store peers received through pushes temporary.
201  */
202 static struct CustomPeerMap *push_map;
203
204 /**
205  * List to store peers received through pulls temporary.
206  */
207 static struct CustomPeerMap *pull_map;
208
209 /**
210  * Handler to NSE.
211  */
212 static struct GNUNET_NSE_Handle *nse;
213
214 /**
215  * Handler to CADET.
216  */
217 static struct GNUNET_CADET_Handle *cadet_handle;
218
219 /**
220  * @brief Port to communicate to other peers.
221  */
222 static struct GNUNET_CADET_Port *cadet_port;
223
224 /**
225  * Handler to PEERINFO.
226  */
227 static struct GNUNET_PEERINFO_Handle *peerinfo_handle;
228
229 /**
230  * Handle for cancellation of iteration over peers.
231  */
232 static struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify_handle;
233
234 /**
235  * Request counter.
236  *
237  * Counts how many requets clients already issued.
238  * Only needed in the beginning to check how many of the 64 deltas
239  * we already have
240  */
241 static unsigned int req_counter;
242
243 /**
244  * Time of the last request we received.
245  *
246  * Used to compute the expected request rate.
247  */
248 static struct GNUNET_TIME_Absolute last_request;
249
250 /**
251  * Size of #request_deltas.
252  */
253 #define REQUEST_DELTAS_SIZE 64
254 static unsigned int request_deltas_size = REQUEST_DELTAS_SIZE;
255
256 /**
257  * Last 64 deltas between requests
258  */
259 static struct GNUNET_TIME_Relative request_deltas[REQUEST_DELTAS_SIZE];
260
261 /**
262  * The prediction of the rate of requests
263  */
264 static struct GNUNET_TIME_Relative request_rate;
265
266
267 #ifdef ENABLE_MALICIOUS
268 /**
269  * Type of malicious peer
270  *
271  * 0 Don't act malicious at all - Default
272  * 1 Try to maximise representation
273  * 2 Try to partition the network
274  * 3 Combined attack
275  */
276 static uint32_t mal_type;
277
278 /**
279  * Other malicious peers
280  */
281 static struct GNUNET_PeerIdentity *mal_peers;
282
283 /**
284  * Hashmap of malicious peers used as set.
285  * Used to more efficiently check whether we know that peer.
286  */
287 static struct GNUNET_CONTAINER_MultiPeerMap *mal_peer_set;
288
289 /**
290  * Number of other malicious peers
291  */
292 static uint32_t num_mal_peers;
293
294
295 /**
296  * If type is 2 This struct is used to store the attacked peers in a DLL
297  */
298 struct AttackedPeer
299 {
300   /**
301    * DLL
302    */
303   struct AttackedPeer *next;
304   struct AttackedPeer *prev;
305
306   /**
307    * PeerID
308    */
309   struct GNUNET_PeerIdentity peer_id;
310 };
311
312 /**
313  * If type is 2 this is the DLL of attacked peers
314  */
315 static struct AttackedPeer *att_peers_head;
316 static struct AttackedPeer *att_peers_tail;
317
318 /**
319  * This index is used to point to an attacked peer to
320  * implement the round-robin-ish way to select attacked peers.
321  */
322 static struct AttackedPeer *att_peer_index;
323
324 /**
325  * Hashmap of attacked peers used as set.
326  * Used to more efficiently check whether we know that peer.
327  */
328 static struct GNUNET_CONTAINER_MultiPeerMap *att_peer_set;
329
330 /**
331  * Number of attacked peers
332  */
333 static uint32_t num_attacked_peers;
334
335 /**
336  * If type is 1 this is the attacked peer
337  */
338 static struct GNUNET_PeerIdentity attacked_peer;
339
340 /**
341  * The limit of PUSHes we can send in one round.
342  * This is an assumption of the Brahms protocol and either implemented
343  * via proof of work
344  * or
345  * assumend to be the bandwidth limitation.
346  */
347 static uint32_t push_limit = 10000;
348 #endif /* ENABLE_MALICIOUS */
349
350
351 /***********************************************************************
352  * /Globals
353 ***********************************************************************/
354
355
356 /***********************************************************************
357  * Util functions
358 ***********************************************************************/
359
360
361 /**
362  * Print peerlist to log.
363  */
364 static void
365 print_peer_list (struct GNUNET_PeerIdentity *list,
366                  unsigned int len)
367 {
368   unsigned int i;
369
370   LOG (GNUNET_ERROR_TYPE_DEBUG,
371        "Printing peer list of length %u at %p:\n",
372        len,
373        list);
374   for (i = 0 ; i < len ; i++)
375   {
376     LOG (GNUNET_ERROR_TYPE_DEBUG,
377          "%u. peer: %s\n",
378          i, GNUNET_i2s (&list[i]));
379   }
380 }
381
382
383 /**
384  * Remove peer from list.
385  */
386 static void
387 rem_from_list (struct GNUNET_PeerIdentity **peer_list,
388                unsigned int *list_size,
389                const struct GNUNET_PeerIdentity *peer)
390 {
391   unsigned int i;
392   struct GNUNET_PeerIdentity *tmp;
393
394   tmp = *peer_list;
395
396   LOG (GNUNET_ERROR_TYPE_DEBUG,
397        "Removing peer %s from list at %p\n",
398        GNUNET_i2s (peer),
399        tmp);
400
401   for ( i = 0 ; i < *list_size ; i++ )
402   {
403     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&tmp[i], peer))
404     {
405       if (i < *list_size -1)
406       { /* Not at the last entry -- shift peers left */
407         memmove (&tmp[i], &tmp[i +1],
408                 ((*list_size) - i -1) * sizeof (struct GNUNET_PeerIdentity));
409       }
410       /* Remove last entry (should be now useless PeerID) */
411       GNUNET_array_grow (tmp, *list_size, (*list_size) -1);
412     }
413   }
414   *peer_list = tmp;
415 }
416
417
418 /**
419  * Sum all time relatives of an array.
420  */
421 static struct GNUNET_TIME_Relative
422 T_relative_sum (const struct GNUNET_TIME_Relative *rel_array,
423                 uint32_t arr_size)
424 {
425   struct GNUNET_TIME_Relative sum;
426   uint32_t i;
427
428   sum = GNUNET_TIME_UNIT_ZERO;
429   for ( i = 0 ; i < arr_size ; i++ )
430   {
431     sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
432   }
433   return sum;
434 }
435
436
437 /**
438  * Compute the average of given time relatives.
439  */
440 static struct GNUNET_TIME_Relative
441 T_relative_avg (const struct GNUNET_TIME_Relative *rel_array,
442                 uint32_t arr_size)
443 {
444   return GNUNET_TIME_relative_divide (T_relative_sum (rel_array,
445                                                       arr_size),
446                                       arr_size);
447 }
448
449
450 /**
451  * Insert PeerID in #view
452  *
453  * Called once we know a peer is live.
454  * Implements #PeerOp
455  *
456  * @return GNUNET_OK if peer was actually inserted
457  *         GNUNET_NO if peer was not inserted
458  */
459 static void
460 insert_in_view_op (void *cls,
461                 const struct GNUNET_PeerIdentity *peer);
462
463 /**
464  * Insert PeerID in #view
465  *
466  * Called once we know a peer is live.
467  *
468  * @return GNUNET_OK if peer was actually inserted
469  *         GNUNET_NO if peer was not inserted
470  */
471 static int
472 insert_in_view (const struct GNUNET_PeerIdentity *peer)
473 {
474   int online;
475
476   online = Peers_check_peer_flag (peer, Peers_ONLINE);
477   if ( (GNUNET_NO == online) ||
478        (GNUNET_SYSERR == online) ) /* peer is not even known */
479   {
480     (void) Peers_issue_peer_liveliness_check (peer);
481     (void) Peers_schedule_operation (peer, insert_in_view_op);
482     return GNUNET_NO;
483   }
484   /* Open channel towards peer to keep connection open */
485   Peers_indicate_sending_intention (peer);
486   return View_put (peer);
487 }
488
489 /**
490  * Put random peer from sampler into the view as history update.
491  */
492 static void
493 hist_update (void *cls,
494              struct GNUNET_PeerIdentity *ids,
495              uint32_t num_peers)
496 {
497   unsigned int i;
498
499   for (i = 0; i < num_peers; i++)
500   {
501     (void) insert_in_view (&ids[i]);
502     to_file (file_name_view_log,
503              "+%s\t(hist)",
504              GNUNET_i2s_full (ids));
505   }
506 }
507
508
509 /**
510  * Wrapper around #RPS_sampler_resize()
511  *
512  * If we do not have enough sampler elements, double current sampler size
513  * If we have more than enough sampler elements, halv current sampler size
514  */
515 static void
516 resize_wrapper (struct RPS_Sampler *sampler, uint32_t new_size)
517 {
518   unsigned int sampler_size;
519
520   // TODO statistics
521   // TODO respect the min, max
522   sampler_size = RPS_sampler_get_size (sampler);
523   if (sampler_size > new_size * 4)
524   { /* Shrinking */
525     RPS_sampler_resize (sampler, sampler_size / 2);
526   }
527   else if (sampler_size < new_size)
528   { /* Growing */
529     RPS_sampler_resize (sampler, sampler_size * 2);
530   }
531   LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
532 }
533
534
535 /**
536  * Wrapper around #RPS_sampler_resize() resizing the client sampler
537  */
538 static void
539 client_resize_wrapper ()
540 {
541   uint32_t bigger_size;
542
543   // TODO statistics
544
545   bigger_size = GNUNET_MAX (sampler_size_est_need, sampler_size_client_need);
546
547   // TODO respect the min, max
548   resize_wrapper (client_sampler, bigger_size);
549   LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size_client is now %" PRIu32 "\n",
550       bigger_size);
551 }
552
553
554 /**
555  * Estimate request rate
556  *
557  * Called every time we receive a request from the client.
558  */
559 static void
560 est_request_rate()
561 {
562   struct GNUNET_TIME_Relative max_round_duration;
563
564   if (request_deltas_size > req_counter)
565     req_counter++;
566   if ( 1 < req_counter)
567   {
568     /* Shift last request deltas to the right */
569     memmove (&request_deltas[1],
570         request_deltas,
571         (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
572
573     /* Add current delta to beginning */
574     request_deltas[0] =
575         GNUNET_TIME_absolute_get_difference (last_request,
576                                              GNUNET_TIME_absolute_get ());
577     request_rate = T_relative_avg (request_deltas, req_counter);
578     request_rate = (request_rate.rel_value_us < 1) ?
579       GNUNET_TIME_relative_get_unit_ () : request_rate;
580
581     /* Compute the duration a round will maximally take */
582     max_round_duration =
583         GNUNET_TIME_relative_add (round_interval,
584                                   GNUNET_TIME_relative_divide (round_interval, 2));
585
586     /* Set the estimated size the sampler has to have to
587      * satisfy the current client request rate */
588     sampler_size_client_need =
589         max_round_duration.rel_value_us / request_rate.rel_value_us;
590
591     /* Resize the sampler */
592     client_resize_wrapper ();
593   }
594   last_request = GNUNET_TIME_absolute_get ();
595 }
596
597
598 /**
599  * Add all peers in @a peer_array to @a peer_map used as set.
600  *
601  * @param peer_array array containing the peers
602  * @param num_peers number of peers in @peer_array
603  * @param peer_map the peermap to use as set
604  */
605 static void
606 add_peer_array_to_set (const struct GNUNET_PeerIdentity *peer_array,
607                        unsigned int num_peers,
608                        struct GNUNET_CONTAINER_MultiPeerMap *peer_map)
609 {
610   unsigned int i;
611   if (NULL == peer_map)
612   {
613     LOG (GNUNET_ERROR_TYPE_WARNING,
614          "Trying to add peers to non-existing peermap.\n");
615     return;
616   }
617
618   for (i = 0; i < num_peers; i++)
619   {
620     GNUNET_CONTAINER_multipeermap_put (peer_map,
621                                        &peer_array[i],
622                                        NULL,
623                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
624   }
625 }
626
627
628 /**
629  * Send a PULL REPLY to @a peer_id
630  *
631  * @param peer_id the peer to send the reply to.
632  * @param peer_ids the peers to send to @a peer_id
633  * @param num_peer_ids the number of peers to send to @a peer_id
634  */
635 static void
636 send_pull_reply (const struct GNUNET_PeerIdentity *peer_id,
637                  const struct GNUNET_PeerIdentity *peer_ids,
638                  unsigned int num_peer_ids)
639 {
640   uint32_t send_size;
641   struct GNUNET_MQ_Envelope *ev;
642   struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
643
644   /* Compute actual size */
645   send_size = sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) +
646               num_peer_ids * sizeof (struct GNUNET_PeerIdentity);
647
648   if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size)
649     /* Compute number of peers to send
650      * If too long, simply truncate */
651     // TODO select random ones via permutation
652     //      or even better: do good protocol design
653     send_size =
654       (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE -
655        sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
656        sizeof (struct GNUNET_PeerIdentity);
657   else
658     send_size = num_peer_ids;
659
660   LOG (GNUNET_ERROR_TYPE_DEBUG,
661       "Going to send PULL REPLY with %u peers to %s\n",
662       send_size, GNUNET_i2s (peer_id));
663
664   ev = GNUNET_MQ_msg_extra (out_msg,
665                             send_size * sizeof (struct GNUNET_PeerIdentity),
666                             GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
667   out_msg->num_peers = htonl (send_size);
668   GNUNET_memcpy (&out_msg[1], peer_ids,
669          send_size * sizeof (struct GNUNET_PeerIdentity));
670
671   Peers_send_message (peer_id, ev, "PULL REPLY");
672 }
673
674
675 /**
676  * Insert PeerID in #pull_map
677  *
678  * Called once we know a peer is live.
679  */
680 static void
681 insert_in_pull_map (void *cls,
682                     const struct GNUNET_PeerIdentity *peer)
683 {
684   CustomPeerMap_put (pull_map, peer);
685 }
686
687
688 /**
689  * Insert PeerID in #view
690  *
691  * Called once we know a peer is live.
692  * Implements #PeerOp
693  */
694 static void
695 insert_in_view_op (void *cls,
696                 const struct GNUNET_PeerIdentity *peer)
697 {
698   (void) insert_in_view (peer);
699 }
700
701
702 /**
703  * Update sampler with given PeerID.
704  * Implements #PeerOp
705  */
706 static void
707 insert_in_sampler (void *cls,
708                    const struct GNUNET_PeerIdentity *peer)
709 {
710   LOG (GNUNET_ERROR_TYPE_DEBUG,
711        "Updating samplers with peer %s from insert_in_sampler()\n",
712        GNUNET_i2s (peer));
713   RPS_sampler_update (prot_sampler,   peer);
714   RPS_sampler_update (client_sampler, peer);
715   if (0 < RPS_sampler_count_id (prot_sampler, peer))
716   {
717     /* Make sure we 'know' about this peer */
718     (void) Peers_issue_peer_liveliness_check (peer);
719     /* Establish a channel towards that peer to indicate we are going to send
720      * messages to it */
721     //Peers_indicate_sending_intention (peer);
722   }
723 }
724
725 /**
726  * @brief This is called on peers from external sources (cadet, peerinfo, ...)
727  *        If the peer is not known, liveliness check is issued and it is
728  *        scheduled to be inserted in sampler and view.
729  *
730  * "External sources" refer to every source except the gossip.
731  *
732  * @param peer peer to insert
733  */
734 static void
735 got_peer (const struct GNUNET_PeerIdentity *peer)
736 {
737   /* If we did not know this peer already, insert it into sampler and view */
738   if (GNUNET_YES == Peers_issue_peer_liveliness_check (peer))
739   {
740     Peers_schedule_operation (peer, insert_in_sampler);
741     Peers_schedule_operation (peer, insert_in_view_op);
742   }
743 }
744
745 /**
746  * @brief Checks if there is a sending channel and if it is needed
747  *
748  * @param peer the peer whose sending channel is checked
749  * @return GNUNET_YES if sending channel exists and is still needed
750  *         GNUNET_NO  otherwise
751  */
752 static int
753 check_sending_channel_needed (const struct GNUNET_PeerIdentity *peer)
754 {
755   /* struct GNUNET_CADET_Channel *channel; */
756   if (GNUNET_NO == Peers_check_peer_known (peer))
757   {
758     return GNUNET_NO;
759   }
760   if (GNUNET_YES == Peers_check_sending_channel_exists (peer))
761   {
762     if ( (0 < RPS_sampler_count_id (prot_sampler, peer)) ||
763          (GNUNET_YES == View_contains_peer (peer)) ||
764          (GNUNET_YES == CustomPeerMap_contains_peer (push_map, peer)) ||
765          (GNUNET_YES == CustomPeerMap_contains_peer (pull_map, peer)) ||
766          (GNUNET_YES == Peers_check_peer_flag (peer, Peers_PULL_REPLY_PENDING)))
767     { /* If we want to keep the connection to peer open */
768       return GNUNET_YES;
769     }
770     return GNUNET_NO;
771   }
772   return GNUNET_NO;
773 }
774
775 /**
776  * @brief remove peer from our knowledge, the view, push and pull maps and
777  * samplers.
778  *
779  * @param peer the peer to remove
780  */
781 static void
782 remove_peer (const struct GNUNET_PeerIdentity *peer)
783 {
784   (void) View_remove_peer (peer);
785   CustomPeerMap_remove_peer (pull_map, peer);
786   CustomPeerMap_remove_peer (push_map, peer);
787   RPS_sampler_reinitialise_by_value (prot_sampler, peer);
788   RPS_sampler_reinitialise_by_value (client_sampler, peer);
789   Peers_remove_peer (peer);
790 }
791
792
793 /**
794  * @brief Remove data that is not needed anymore.
795  *
796  * If the sending channel is no longer needed it is destroyed.
797  *
798  * @param peer the peer whose data is about to be cleaned
799  */
800 static void
801 clean_peer (const struct GNUNET_PeerIdentity *peer)
802 {
803   if (GNUNET_NO == check_sending_channel_needed (peer))
804   {
805     LOG (GNUNET_ERROR_TYPE_DEBUG,
806         "Going to remove send channel to peer %s\n",
807         GNUNET_i2s (peer));
808     #ifdef ENABLE_MALICIOUS
809     if (0 != GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
810       (void) Peers_destroy_sending_channel (peer);
811     #else /* ENABLE_MALICIOUS */
812     (void) Peers_destroy_sending_channel (peer);
813     #endif /* ENABLE_MALICIOUS */
814   }
815
816   if ( (GNUNET_NO == Peers_check_peer_send_intention (peer)) &&
817        (GNUNET_NO == View_contains_peer (peer)) &&
818        (GNUNET_NO == CustomPeerMap_contains_peer (push_map, peer)) &&
819        (GNUNET_NO == CustomPeerMap_contains_peer (push_map, peer)) &&
820        (0 == RPS_sampler_count_id (prot_sampler,   peer)) &&
821        (0 == RPS_sampler_count_id (client_sampler, peer)) &&
822        (GNUNET_NO != Peers_check_removable (peer)) )
823   { /* We can safely remove this peer */
824     LOG (GNUNET_ERROR_TYPE_DEBUG,
825         "Going to remove peer %s\n",
826         GNUNET_i2s (peer));
827     remove_peer (peer);
828     return;
829   }
830 }
831
832 /**
833  * @brief This is called when a channel is destroyed.
834  *
835  * Removes peer completely from our knowledge if the send_channel was destroyed
836  * Otherwise simply delete the recv_channel
837  * Also check if the knowledge about this peer is still needed.
838  * If not, remove this peer from our knowledge.
839  *
840  * @param cls The closure
841  * @param channel The channel being closed
842  * @param channel_ctx The context associated with this channel
843  */
844 static void
845 cleanup_destroyed_channel (void *cls,
846                            const struct GNUNET_CADET_Channel *channel)
847 {
848   struct GNUNET_PeerIdentity *peer = cls;
849   uint32_t *channel_flag;
850
851   if (GNUNET_NO == Peers_check_peer_known (peer))
852   { /* We don't know a context to that peer */
853     LOG (GNUNET_ERROR_TYPE_WARNING,
854          "channel (%s) without associated context was destroyed\n",
855          GNUNET_i2s (peer));
856     return;
857   }
858
859   if (GNUNET_YES == Peers_check_peer_flag (peer, Peers_TO_DESTROY))
860   { /* We are in the middle of removing that peer from our knowledge. In this
861        case simply make sure that the channels are cleaned. */
862     Peers_cleanup_destroyed_channel (cls, channel);
863     to_file (file_name_view_log,
864              "-%s\t(cleanup channel, ourself)",
865              GNUNET_i2s_full (peer));
866     return;
867   }
868
869   if (GNUNET_YES ==
870       Peers_check_channel_role (peer, channel, Peers_CHANNEL_ROLE_SENDING))
871   { /* Channel used for sending was destroyed */
872     /* Possible causes of channel destruction:
873      *  - ourselves  -> cleaning send channel -> clean context
874      *  - other peer -> peer probably went down -> remove
875      */
876     channel_flag = Peers_get_channel_flag (peer, Peers_CHANNEL_ROLE_SENDING);
877     if (GNUNET_YES == Peers_check_channel_flag (channel_flag, Peers_CHANNEL_CLEAN))
878     { /* We are about to clean the sending channel. Clean the respective
879        * context */
880       Peers_cleanup_destroyed_channel (cls, channel);
881       return;
882     }
883     else
884     { /* Other peer destroyed our sending channel that he is supposed to keep
885        * open. It probably went down. Remove it from our knowledge. */
886       Peers_cleanup_destroyed_channel (cls, channel);
887       remove_peer (peer);
888       return;
889     }
890   }
891   else if (GNUNET_YES ==
892       Peers_check_channel_role (peer, channel, Peers_CHANNEL_ROLE_RECEIVING))
893   { /* Channel used for receiving was destroyed */
894     /* Possible causes of channel destruction:
895      *  - ourselves  -> peer tried to establish channel twice -> clean context
896      *  - other peer -> peer doesn't want to send us data -> clean
897      */
898     channel_flag = Peers_get_channel_flag (peer, Peers_CHANNEL_ROLE_RECEIVING);
899     if (GNUNET_YES ==
900         Peers_check_channel_flag (channel_flag, Peers_CHANNEL_ESTABLISHED_TWICE))
901     { /* Other peer tried to establish a channel to us twice. We do not accept
902        * that. Clean the context. */
903       Peers_cleanup_destroyed_channel (cls, channel);
904       return;
905     }
906     else
907     { /* Other peer doesn't want to send us data anymore. We are free to clean
908        * it. */
909       Peers_cleanup_destroyed_channel (cls, channel);
910       clean_peer (peer);
911       return;
912     }
913   }
914   else
915   {
916     LOG (GNUNET_ERROR_TYPE_WARNING,
917         "Destroyed channel is neither sending nor receiving channel\n");
918   }
919 }
920
921 /***********************************************************************
922  * /Util functions
923 ***********************************************************************/
924
925 static void
926 destroy_reply_cls (struct ReplyCls *rep_cls)
927 {
928   struct ClientContext *cli_ctx;
929
930   cli_ctx = rep_cls->cli_ctx;
931   GNUNET_assert (NULL != cli_ctx);
932   if (NULL != rep_cls->req_handle)
933   {
934     RPS_sampler_request_cancel (rep_cls->req_handle);
935   }
936   GNUNET_CONTAINER_DLL_remove (cli_ctx->rep_cls_head,
937                                cli_ctx->rep_cls_tail,
938                                rep_cls);
939   GNUNET_free (rep_cls);
940 }
941
942
943 static void
944 destroy_cli_ctx (struct ClientContext *cli_ctx)
945 {
946   GNUNET_assert (NULL != cli_ctx);
947   if (NULL != cli_ctx->rep_cls_head)
948   {
949     LOG (GNUNET_ERROR_TYPE_WARNING,
950          "Trying to destroy the context of a client that still has pending requests. Going to clean those\n");
951     while (NULL != cli_ctx->rep_cls_head)
952       destroy_reply_cls (cli_ctx->rep_cls_head);
953   }
954   GNUNET_CONTAINER_DLL_remove (cli_ctx_head,
955                                cli_ctx_tail,
956                                cli_ctx);
957   GNUNET_free (cli_ctx);
958 }
959
960
961 /**
962  * Function called by NSE.
963  *
964  * Updates sizes of sampler list and view and adapt those lists
965  * accordingly.
966  */
967 static void
968 nse_callback (void *cls,
969               struct GNUNET_TIME_Absolute timestamp,
970               double logestimate, double std_dev)
971 {
972   double estimate;
973   //double scale; // TODO this might go gloabal/config
974
975   LOG (GNUNET_ERROR_TYPE_DEBUG,
976        "Received a ns estimate - logest: %f, std_dev: %f (old_size: %u)\n",
977        logestimate, std_dev, RPS_sampler_get_size (prot_sampler));
978   //scale = .01;
979   estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
980   // GNUNET_NSE_log_estimate_to_n (logestimate);
981   estimate = pow (estimate, 1.0 / 3);
982   // TODO add if std_dev is a number
983   // estimate += (std_dev * scale);
984   if (2 < ceil (estimate))
985   {
986     LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
987     sampler_size_est_need = estimate;
988   } else
989     LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
990
991   /* If the NSE has changed adapt the lists accordingly */
992   resize_wrapper (prot_sampler, sampler_size_est_need);
993   client_resize_wrapper ();
994 }
995
996
997 /**
998  * Callback called once the requested PeerIDs are ready.
999  *
1000  * Sends those to the requesting client.
1001  */
1002 static void
1003 client_respond (void *cls,
1004                 struct GNUNET_PeerIdentity *peer_ids,
1005                 uint32_t num_peers)
1006 {
1007   struct ReplyCls *reply_cls = cls;
1008   uint32_t i;
1009   struct GNUNET_MQ_Envelope *ev;
1010   struct GNUNET_RPS_CS_ReplyMessage *out_msg;
1011   uint32_t size_needed;
1012   struct ClientContext *cli_ctx;
1013
1014   GNUNET_assert (NULL != reply_cls);
1015   LOG (GNUNET_ERROR_TYPE_DEBUG,
1016        "sampler returned %" PRIu32 " peers:\n",
1017        num_peers);
1018   for (i = 0; i < num_peers; i++)
1019   {
1020     LOG (GNUNET_ERROR_TYPE_DEBUG,
1021          "  %" PRIu32 ": %s\n",
1022          i,
1023          GNUNET_i2s (&peer_ids[i]));
1024   }
1025
1026   size_needed = sizeof (struct GNUNET_RPS_CS_ReplyMessage) +
1027                 num_peers * sizeof (struct GNUNET_PeerIdentity);
1028
1029   GNUNET_assert (GNUNET_MAX_MESSAGE_SIZE >= size_needed);
1030
1031   ev = GNUNET_MQ_msg_extra (out_msg,
1032                             num_peers * sizeof (struct GNUNET_PeerIdentity),
1033                             GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
1034   out_msg->num_peers = htonl (num_peers);
1035   out_msg->id = htonl (reply_cls->id);
1036
1037   GNUNET_memcpy (&out_msg[1],
1038           peer_ids,
1039           num_peers * sizeof (struct GNUNET_PeerIdentity));
1040   GNUNET_free (peer_ids);
1041
1042   cli_ctx = reply_cls->cli_ctx;
1043   GNUNET_assert (NULL != cli_ctx);
1044   reply_cls->req_handle = NULL;
1045   destroy_reply_cls (reply_cls);
1046   GNUNET_MQ_send (cli_ctx->mq, ev);
1047 }
1048
1049
1050 /**
1051  * Handle RPS request from the client.
1052  *
1053  * @param cls closure
1054  * @param message the actual message
1055  */
1056 static void
1057 handle_client_request (void *cls,
1058                        const struct GNUNET_RPS_CS_RequestMessage *msg)
1059 {
1060   struct ClientContext *cli_ctx = cls;
1061   uint32_t num_peers;
1062   uint32_t size_needed;
1063   struct ReplyCls *reply_cls;
1064   uint32_t i;
1065
1066   num_peers = ntohl (msg->num_peers);
1067   size_needed = sizeof (struct GNUNET_RPS_CS_RequestMessage) +
1068                 num_peers * sizeof (struct GNUNET_PeerIdentity);
1069
1070   if (GNUNET_MAX_MESSAGE_SIZE < size_needed)
1071   {
1072     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1073                 "Message received from client has size larger than expected\n");
1074     GNUNET_SERVICE_client_drop (cli_ctx->client);
1075     return;
1076   }
1077
1078   for (i = 0 ; i < num_peers ; i++)
1079     est_request_rate();
1080
1081   LOG (GNUNET_ERROR_TYPE_DEBUG,
1082        "Client requested %" PRIu32 " random peer(s).\n",
1083        num_peers);
1084
1085   reply_cls = GNUNET_new (struct ReplyCls);
1086   reply_cls->id = ntohl (msg->id);
1087   reply_cls->cli_ctx = cli_ctx;
1088   reply_cls->req_handle = RPS_sampler_get_n_rand_peers (client_sampler,
1089                                                         client_respond,
1090                                                         reply_cls,
1091                                                         num_peers);
1092
1093   GNUNET_assert (NULL != cli_ctx);
1094   GNUNET_CONTAINER_DLL_insert (cli_ctx->rep_cls_head,
1095                                cli_ctx->rep_cls_tail,
1096                                reply_cls);
1097   GNUNET_SERVICE_client_continue (cli_ctx->client);
1098 }
1099
1100
1101 /**
1102  * @brief Handle a message that requests the cancellation of a request
1103  *
1104  * @param cls unused
1105  * @param message the message containing the id of the request
1106  */
1107 static void
1108 handle_client_request_cancel (void *cls,
1109                               const struct GNUNET_RPS_CS_RequestCancelMessage *msg)
1110 {
1111   struct ClientContext *cli_ctx = cls;
1112   struct ReplyCls *rep_cls;
1113
1114   GNUNET_assert (NULL != cli_ctx);
1115   GNUNET_assert (NULL != cli_ctx->rep_cls_head);
1116   rep_cls = cli_ctx->rep_cls_head;
1117   LOG (GNUNET_ERROR_TYPE_DEBUG,
1118       "Client cancels request with id %" PRIu32 "\n",
1119       ntohl (msg->id));
1120   while ( (NULL != rep_cls->next) &&
1121           (rep_cls->id != ntohl (msg->id)) )
1122     rep_cls = rep_cls->next;
1123   GNUNET_assert (rep_cls->id == ntohl (msg->id));
1124   destroy_reply_cls (rep_cls);
1125   GNUNET_SERVICE_client_continue (cli_ctx->client);
1126 }
1127
1128
1129 /**
1130  * @brief This function is called, when the client seeds peers.
1131  * It verifies that @a msg is well-formed.
1132  *
1133  * @param cls the closure (#ClientContext)
1134  * @param msg the message
1135  * @return #GNUNET_OK if @a msg is well-formed
1136  */
1137 static int
1138 check_client_seed (void *cls, const struct GNUNET_RPS_CS_SeedMessage *msg)
1139 {
1140   struct ClientContext *cli_ctx = cls;
1141   uint16_t msize = ntohs (msg->header.size);
1142   uint32_t num_peers = ntohl (msg->num_peers);
1143
1144   msize -= sizeof (struct GNUNET_RPS_CS_SeedMessage);
1145   if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) ||
1146        (msize % sizeof (struct GNUNET_PeerIdentity) != 0) )
1147   {
1148     GNUNET_break (0);
1149     GNUNET_SERVICE_client_drop (cli_ctx->client);
1150     return GNUNET_SYSERR;
1151   }
1152   return GNUNET_OK;
1153 }
1154
1155
1156 /**
1157  * Handle seed from the client.
1158  *
1159  * @param cls closure
1160  * @param message the actual message
1161  */
1162 static void
1163 handle_client_seed (void *cls,
1164                     const struct GNUNET_RPS_CS_SeedMessage *msg)
1165 {
1166   struct ClientContext *cli_ctx = cls;
1167   struct GNUNET_PeerIdentity *peers;
1168   uint32_t num_peers;
1169   uint32_t i;
1170
1171   num_peers = ntohl (msg->num_peers);
1172   peers = (struct GNUNET_PeerIdentity *) &msg[1];
1173   //peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1174   //GNUNET_memcpy (peers, &msg[1], num_peers * sizeof (struct GNUNET_PeerIdentity));
1175
1176   LOG (GNUNET_ERROR_TYPE_DEBUG,
1177        "Client seeded peers:\n");
1178   print_peer_list (peers, num_peers);
1179
1180   for (i = 0; i < num_peers; i++)
1181   {
1182     LOG (GNUNET_ERROR_TYPE_DEBUG,
1183          "Updating samplers with seed %" PRIu32 ": %s\n",
1184          i,
1185          GNUNET_i2s (&peers[i]));
1186
1187     got_peer (&peers[i]);
1188   }
1189
1190   ////GNUNET_free (peers);
1191
1192   GNUNET_SERVICE_client_continue (cli_ctx->client);
1193 }
1194
1195 /**
1196  * Handle a CHECK_LIVE message from another peer.
1197  *
1198  * This does nothing. But without calling #GNUNET_CADET_receive_done()
1199  * the channel is blocked for all other communication.
1200  *
1201  * @param cls Closure
1202  * @param msg The message header
1203  */
1204 static void
1205 handle_peer_check (void *cls,
1206                    const struct GNUNET_MessageHeader *msg)
1207 {
1208   const struct GNUNET_PeerIdentity *peer = cls;
1209
1210   GNUNET_CADET_receive_done (Peers_get_recv_channel (peer));
1211 }
1212
1213 /**
1214  * Handle a PUSH message from another peer.
1215  *
1216  * Check the proof of work and store the PeerID
1217  * in the temporary list for pushed PeerIDs.
1218  *
1219  * @param cls Closure
1220  * @param msg The message header
1221  */
1222 static void
1223 handle_peer_push (void *cls,
1224                   const struct GNUNET_MessageHeader *msg)
1225 {
1226   const struct GNUNET_PeerIdentity *peer = cls;
1227
1228   // (check the proof of work (?))
1229
1230   LOG (GNUNET_ERROR_TYPE_DEBUG,
1231        "Received PUSH (%s)\n",
1232        GNUNET_i2s (peer));
1233
1234   #ifdef ENABLE_MALICIOUS
1235   struct AttackedPeer *tmp_att_peer;
1236
1237   if ( (1 == mal_type) ||
1238        (3 == mal_type) )
1239   { /* Try to maximise representation */
1240     tmp_att_peer = GNUNET_new (struct AttackedPeer);
1241     tmp_att_peer->peer_id = *peer;
1242     if (NULL == att_peer_set)
1243       att_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
1244     if (GNUNET_NO ==
1245         GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1246                                                 peer))
1247     {
1248       GNUNET_CONTAINER_DLL_insert (att_peers_head,
1249                                    att_peers_tail,
1250                                    tmp_att_peer);
1251       add_peer_array_to_set (peer, 1, att_peer_set);
1252     }
1253   }
1254
1255
1256   else if (2 == mal_type)
1257   {
1258     /* We attack one single well-known peer - simply ignore */
1259   }
1260   #endif /* ENABLE_MALICIOUS */
1261
1262   /* Add the sending peer to the push_map */
1263   CustomPeerMap_put (push_map, peer);
1264
1265   GNUNET_CADET_receive_done (Peers_get_recv_channel (peer));
1266 }
1267
1268
1269 /**
1270  * Handle PULL REQUEST request message from another peer.
1271  *
1272  * Reply with the view of PeerIDs.
1273  *
1274  * @param cls Closure
1275  * @param msg The message header
1276  */
1277 static void
1278 handle_peer_pull_request (void *cls,
1279                           const struct GNUNET_MessageHeader *msg)
1280 {
1281   struct GNUNET_PeerIdentity *peer = cls;
1282   const struct GNUNET_PeerIdentity *view_array;
1283
1284   LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REQUEST (%s)\n", GNUNET_i2s (peer));
1285
1286   #ifdef ENABLE_MALICIOUS
1287   if (1 == mal_type
1288       || 3 == mal_type)
1289   { /* Try to maximise representation */
1290     send_pull_reply (peer, mal_peers, num_mal_peers);
1291   }
1292
1293   else if (2 == mal_type)
1294   { /* Try to partition network */
1295     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
1296     {
1297       send_pull_reply (peer, mal_peers, num_mal_peers);
1298     }
1299   }
1300   #endif /* ENABLE_MALICIOUS */
1301
1302   view_array = View_get_as_array ();
1303   send_pull_reply (peer, view_array, View_size ());
1304
1305   GNUNET_CADET_receive_done (Peers_get_recv_channel (peer));
1306 }
1307
1308
1309 /**
1310  * Check whether we sent a corresponding request and
1311  * whether this reply is the first one.
1312  *
1313  * @param cls Closure
1314  * @param msg The message header
1315  */
1316 static int
1317 check_peer_pull_reply (void *cls,
1318                        const struct GNUNET_RPS_P2P_PullReplyMessage *msg)
1319 {
1320   struct GNUNET_PeerIdentity *sender = cls;
1321
1322   if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->header.size))
1323   {
1324     GNUNET_break_op (0);
1325     return GNUNET_SYSERR;
1326   }
1327
1328   if ((ntohs (msg->header.size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1329       sizeof (struct GNUNET_PeerIdentity) != ntohl (msg->num_peers))
1330   {
1331     LOG (GNUNET_ERROR_TYPE_ERROR,
1332         "message says it sends %" PRIu32 " peers, have space for %lu peers\n",
1333         ntohl (msg->num_peers),
1334         (ntohs (msg->header.size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1335             sizeof (struct GNUNET_PeerIdentity));
1336     GNUNET_break_op (0);
1337     return GNUNET_SYSERR;
1338   }
1339
1340   if (GNUNET_YES != Peers_check_peer_flag (sender, Peers_PULL_REPLY_PENDING))
1341   {
1342     LOG (GNUNET_ERROR_TYPE_WARNING,
1343         "Received a pull reply from a peer we didn't request one from!\n");
1344     GNUNET_break_op (0);
1345     return GNUNET_SYSERR;
1346   }
1347   return GNUNET_OK;
1348 }
1349
1350 /**
1351  * Handle PULL REPLY message from another peer.
1352  *
1353  * @param cls Closure
1354  * @param msg The message header
1355  */
1356 static void
1357 handle_peer_pull_reply (void *cls,
1358                         const struct GNUNET_RPS_P2P_PullReplyMessage *msg)
1359 {
1360   const struct GNUNET_PeerIdentity *peers;
1361   struct GNUNET_PeerIdentity *sender = cls;
1362   uint32_t i;
1363 #ifdef ENABLE_MALICIOUS
1364   struct AttackedPeer *tmp_att_peer;
1365 #endif /* ENABLE_MALICIOUS */
1366
1367   LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REPLY (%s)\n", GNUNET_i2s (sender));
1368
1369   #ifdef ENABLE_MALICIOUS
1370   // We shouldn't even receive pull replies as we're not sending
1371   if (2 == mal_type)
1372   {
1373   }
1374   #endif /* ENABLE_MALICIOUS */
1375
1376   /* Do actual logic */
1377   peers = (const struct GNUNET_PeerIdentity *) &msg[1];
1378
1379   LOG (GNUNET_ERROR_TYPE_DEBUG,
1380        "PULL REPLY received, got following %u peers:\n",
1381        ntohl (msg->num_peers));
1382
1383   for (i = 0; i < ntohl (msg->num_peers); i++)
1384   {
1385     LOG (GNUNET_ERROR_TYPE_DEBUG,
1386          "%u. %s\n",
1387          i,
1388          GNUNET_i2s (&peers[i]));
1389
1390     #ifdef ENABLE_MALICIOUS
1391     if ((NULL != att_peer_set) &&
1392         (1 == mal_type || 3 == mal_type))
1393     { /* Add attacked peer to local list */
1394       // TODO check if we sent a request and this was the first reply
1395       if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1396                                                                &peers[i])
1397           && GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mal_peer_set,
1398                                                                   &peers[i])
1399           && 0 != GNUNET_CRYPTO_cmp_peer_identity (&peers[i],
1400                                                    &own_identity))
1401       {
1402         tmp_att_peer = GNUNET_new (struct AttackedPeer);
1403         tmp_att_peer->peer_id = peers[i];
1404         GNUNET_CONTAINER_DLL_insert (att_peers_head,
1405                                      att_peers_tail,
1406                                      tmp_att_peer);
1407         add_peer_array_to_set (&peers[i], 1, att_peer_set);
1408       }
1409       continue;
1410     }
1411     #endif /* ENABLE_MALICIOUS */
1412     if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity,
1413                                               &peers[i]))
1414     {
1415       /* Make sure we 'know' about this peer */
1416       (void) Peers_insert_peer (&peers[i]);
1417
1418       if (GNUNET_YES == Peers_check_peer_valid (&peers[i]))
1419       {
1420         CustomPeerMap_put (pull_map, &peers[i]);
1421       }
1422       else
1423       {
1424         Peers_schedule_operation (&peers[i], insert_in_pull_map);
1425         (void) Peers_issue_peer_liveliness_check (&peers[i]);
1426       }
1427     }
1428   }
1429
1430   Peers_unset_peer_flag (sender, Peers_PULL_REPLY_PENDING);
1431   clean_peer (sender);
1432
1433   GNUNET_CADET_receive_done (Peers_get_recv_channel (sender));
1434 }
1435
1436
1437 /**
1438  * Compute a random delay.
1439  * A uniformly distributed value between mean + spread and mean - spread.
1440  *
1441  * For example for mean 4 min and spread 2 the minimum is (4 min - (1/2 * 4 min))
1442  * It would return a random value between 2 and 6 min.
1443  *
1444  * @param mean the mean
1445  * @param spread the inverse amount of deviation from the mean
1446  */
1447 static struct GNUNET_TIME_Relative
1448 compute_rand_delay (struct GNUNET_TIME_Relative mean,
1449                     unsigned int spread)
1450 {
1451   struct GNUNET_TIME_Relative half_interval;
1452   struct GNUNET_TIME_Relative ret;
1453   unsigned int rand_delay;
1454   unsigned int max_rand_delay;
1455
1456   if (0 == spread)
1457   {
1458     LOG (GNUNET_ERROR_TYPE_WARNING,
1459          "Not accepting spread of 0\n");
1460     GNUNET_break (0);
1461     GNUNET_assert (0);
1462   }
1463   GNUNET_assert (0 != mean.rel_value_us);
1464
1465   /* Compute random time value between spread * mean and spread * mean */
1466   half_interval = GNUNET_TIME_relative_divide (mean, spread);
1467
1468   max_rand_delay = GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us / mean.rel_value_us * (2/spread);
1469   /**
1470    * Compute random value between (0 and 1) * round_interval
1471    * via multiplying round_interval with a 'fraction' (0 to value)/value
1472    */
1473   rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_rand_delay);
1474   ret = GNUNET_TIME_relative_saturating_multiply (mean,  rand_delay);
1475   ret = GNUNET_TIME_relative_divide   (ret, max_rand_delay);
1476   ret = GNUNET_TIME_relative_add      (ret, half_interval);
1477
1478   if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ret.rel_value_us)
1479     LOG (GNUNET_ERROR_TYPE_WARNING,
1480          "Returning FOREVER_REL\n");
1481
1482   return ret;
1483 }
1484
1485
1486 /**
1487  * Send single pull request
1488  *
1489  * @param peer_id the peer to send the pull request to.
1490  */
1491 static void
1492 send_pull_request (const struct GNUNET_PeerIdentity *peer)
1493 {
1494   struct GNUNET_MQ_Envelope *ev;
1495
1496   GNUNET_assert (GNUNET_NO == Peers_check_peer_flag (peer,
1497                                                      Peers_PULL_REPLY_PENDING));
1498   Peers_set_peer_flag (peer, Peers_PULL_REPLY_PENDING);
1499
1500   LOG (GNUNET_ERROR_TYPE_DEBUG,
1501        "Going to send PULL REQUEST to peer %s.\n",
1502        GNUNET_i2s (peer));
1503
1504   ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
1505   Peers_send_message (peer, ev, "PULL REQUEST");
1506 }
1507
1508
1509 /**
1510  * Send single push
1511  *
1512  * @param peer_id the peer to send the push to.
1513  */
1514 static void
1515 send_push (const struct GNUNET_PeerIdentity *peer_id)
1516 {
1517   struct GNUNET_MQ_Envelope *ev;
1518
1519   LOG (GNUNET_ERROR_TYPE_DEBUG,
1520        "Going to send PUSH to peer %s.\n",
1521        GNUNET_i2s (peer_id));
1522
1523   ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
1524   Peers_send_message (peer_id, ev, "PUSH");
1525 }
1526
1527
1528 static void
1529 do_round (void *cls);
1530
1531 static void
1532 do_mal_round (void *cls);
1533
1534 #ifdef ENABLE_MALICIOUS
1535
1536
1537 /**
1538  * @brief This function is called, when the client tells us to act malicious.
1539  * It verifies that @a msg is well-formed.
1540  *
1541  * @param cls the closure (#ClientContext)
1542  * @param msg the message
1543  * @return #GNUNET_OK if @a msg is well-formed
1544  */
1545 static int
1546 check_client_act_malicious (void *cls,
1547                             const struct GNUNET_RPS_CS_ActMaliciousMessage *msg)
1548 {
1549   struct ClientContext *cli_ctx = cls;
1550   uint16_t msize = ntohs (msg->header.size);
1551   uint32_t num_peers = ntohl (msg->num_peers);
1552
1553   msize -= sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage);
1554   if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) ||
1555        (msize % sizeof (struct GNUNET_PeerIdentity) != 0) )
1556   {
1557     LOG (GNUNET_ERROR_TYPE_ERROR,
1558         "message says it sends %" PRIu32 " peers, have space for %lu peers\n",
1559         ntohl (msg->num_peers),
1560         (msize / sizeof (struct GNUNET_PeerIdentity)));
1561     GNUNET_break (0);
1562     GNUNET_SERVICE_client_drop (cli_ctx->client);
1563     return GNUNET_SYSERR;
1564   }
1565   return GNUNET_OK;
1566 }
1567
1568 /**
1569  * Turn RPS service to act malicious.
1570  *
1571  * @param cls Closure
1572  * @param client The client that sent the message
1573  * @param msg The message header
1574  */
1575 static void
1576 handle_client_act_malicious (void *cls,
1577                              const struct GNUNET_RPS_CS_ActMaliciousMessage *msg)
1578 {
1579   struct ClientContext *cli_ctx = cls;
1580   struct GNUNET_PeerIdentity *peers;
1581   uint32_t num_mal_peers_sent;
1582   uint32_t num_mal_peers_old;
1583
1584   /* Do actual logic */
1585   peers = (struct GNUNET_PeerIdentity *) &msg[1];
1586   mal_type = ntohl (msg->type);
1587   if (NULL == mal_peer_set)
1588     mal_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
1589
1590   LOG (GNUNET_ERROR_TYPE_DEBUG,
1591        "Now acting malicious type %" PRIu32 ", got %" PRIu32 " peers.\n",
1592        mal_type,
1593        ntohl (msg->num_peers));
1594
1595   if (1 == mal_type)
1596   { /* Try to maximise representation */
1597     /* Add other malicious peers to those we already know */
1598
1599     num_mal_peers_sent = ntohl (msg->num_peers);
1600     num_mal_peers_old = num_mal_peers;
1601     GNUNET_array_grow (mal_peers,
1602                        num_mal_peers,
1603                        num_mal_peers + num_mal_peers_sent);
1604     GNUNET_memcpy (&mal_peers[num_mal_peers_old],
1605             peers,
1606             num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1607
1608     /* Add all mal peers to mal_peer_set */
1609     add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1610                            num_mal_peers_sent,
1611                            mal_peer_set);
1612
1613     /* Substitute do_round () with do_mal_round () */
1614     GNUNET_SCHEDULER_cancel (do_round_task);
1615     do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1616   }
1617
1618   else if ( (2 == mal_type) ||
1619             (3 == mal_type) )
1620   { /* Try to partition the network */
1621     /* Add other malicious peers to those we already know */
1622
1623     num_mal_peers_sent = ntohl (msg->num_peers) - 1;
1624     num_mal_peers_old = num_mal_peers;
1625     GNUNET_array_grow (mal_peers,
1626                        num_mal_peers,
1627                        num_mal_peers + num_mal_peers_sent);
1628     if (NULL != mal_peers &&
1629         0 != num_mal_peers)
1630     {
1631       GNUNET_memcpy (&mal_peers[num_mal_peers_old],
1632               peers,
1633               num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1634
1635       /* Add all mal peers to mal_peer_set */
1636       add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1637                              num_mal_peers_sent,
1638                              mal_peer_set);
1639     }
1640
1641     /* Store the one attacked peer */
1642     GNUNET_memcpy (&attacked_peer,
1643             &msg->attacked_peer,
1644             sizeof (struct GNUNET_PeerIdentity));
1645     /* Set the flag of the attacked peer to valid to avoid problems */
1646     if (GNUNET_NO == Peers_check_peer_known (&attacked_peer))
1647     {
1648       (void) Peers_issue_peer_liveliness_check (&attacked_peer);
1649     }
1650
1651     LOG (GNUNET_ERROR_TYPE_DEBUG,
1652          "Attacked peer is %s\n",
1653          GNUNET_i2s (&attacked_peer));
1654
1655     /* Substitute do_round () with do_mal_round () */
1656     GNUNET_SCHEDULER_cancel (do_round_task);
1657     do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1658   }
1659   else if (0 == mal_type)
1660   { /* Stop acting malicious */
1661     GNUNET_array_grow (mal_peers, num_mal_peers, 0);
1662
1663     /* Substitute do_mal_round () with do_round () */
1664     GNUNET_SCHEDULER_cancel (do_round_task);
1665     do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
1666   }
1667   else
1668   {
1669     GNUNET_break (0);
1670     GNUNET_SERVICE_client_continue (cli_ctx->client);
1671   }
1672   GNUNET_SERVICE_client_continue (cli_ctx->client);
1673 }
1674
1675
1676 /**
1677  * Send out PUSHes and PULLs maliciously.
1678  *
1679  * This is executed regylary.
1680  */
1681 static void
1682 do_mal_round (void *cls)
1683 {
1684   uint32_t num_pushes;
1685   uint32_t i;
1686   struct GNUNET_TIME_Relative time_next_round;
1687   struct AttackedPeer *tmp_att_peer;
1688
1689   LOG (GNUNET_ERROR_TYPE_DEBUG,
1690        "Going to execute next round maliciously type %" PRIu32 ".\n",
1691       mal_type);
1692   do_round_task = NULL;
1693   GNUNET_assert (mal_type <= 3);
1694   /* Do malicious actions */
1695   if (1 == mal_type)
1696   { /* Try to maximise representation */
1697
1698     /* The maximum of pushes we're going to send this round */
1699     num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit,
1700                                          num_attacked_peers),
1701                              GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
1702
1703     LOG (GNUNET_ERROR_TYPE_DEBUG,
1704          "Going to send %" PRIu32 " pushes\n",
1705          num_pushes);
1706
1707     /* Send PUSHes to attacked peers */
1708     for (i = 0 ; i < num_pushes ; i++)
1709     {
1710       if (att_peers_tail == att_peer_index)
1711         att_peer_index = att_peers_head;
1712       else
1713         att_peer_index = att_peer_index->next;
1714
1715       send_push (&att_peer_index->peer_id);
1716     }
1717
1718     /* Send PULLs to some peers to learn about additional peers to attack */
1719     tmp_att_peer = att_peer_index;
1720     for (i = 0 ; i < num_pushes * alpha ; i++)
1721     {
1722       if (att_peers_tail == tmp_att_peer)
1723         tmp_att_peer = att_peers_head;
1724       else
1725         att_peer_index = tmp_att_peer->next;
1726
1727       send_pull_request (&tmp_att_peer->peer_id);
1728     }
1729   }
1730
1731
1732   else if (2 == mal_type)
1733   { /**
1734      * Try to partition the network
1735      * Send as many pushes to the attacked peer as possible
1736      * That is one push per round as it will ignore more.
1737      */
1738     (void) Peers_issue_peer_liveliness_check (&attacked_peer);
1739     if (GNUNET_YES == Peers_check_peer_flag (&attacked_peer, Peers_ONLINE))
1740       send_push (&attacked_peer);
1741   }
1742
1743
1744   if (3 == mal_type)
1745   { /* Combined attack */
1746
1747     /* Send PUSH to attacked peers */
1748     if (GNUNET_YES == Peers_check_peer_known (&attacked_peer))
1749     {
1750       (void) Peers_issue_peer_liveliness_check (&attacked_peer);
1751       if (GNUNET_YES == Peers_check_peer_flag (&attacked_peer, Peers_ONLINE))
1752       {
1753         LOG (GNUNET_ERROR_TYPE_DEBUG,
1754             "Goding to send push to attacked peer (%s)\n",
1755             GNUNET_i2s (&attacked_peer));
1756         send_push (&attacked_peer);
1757       }
1758     }
1759     (void) Peers_issue_peer_liveliness_check (&attacked_peer);
1760
1761     /* The maximum of pushes we're going to send this round */
1762     num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit - 1,
1763                                          num_attacked_peers),
1764                              GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
1765
1766     LOG (GNUNET_ERROR_TYPE_DEBUG,
1767          "Going to send %" PRIu32 " pushes\n",
1768          num_pushes);
1769
1770     for (i = 0; i < num_pushes; i++)
1771     {
1772       if (att_peers_tail == att_peer_index)
1773         att_peer_index = att_peers_head;
1774       else
1775         att_peer_index = att_peer_index->next;
1776
1777       send_push (&att_peer_index->peer_id);
1778     }
1779
1780     /* Send PULLs to some peers to learn about additional peers to attack */
1781     tmp_att_peer = att_peer_index;
1782     for (i = 0; i < num_pushes * alpha; i++)
1783     {
1784       if (att_peers_tail == tmp_att_peer)
1785         tmp_att_peer = att_peers_head;
1786       else
1787         att_peer_index = tmp_att_peer->next;
1788
1789       send_pull_request (&tmp_att_peer->peer_id);
1790     }
1791   }
1792
1793   /* Schedule next round */
1794   time_next_round = compute_rand_delay (round_interval, 2);
1795
1796   //do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_mal_round,
1797   //NULL);
1798   GNUNET_assert (NULL == do_round_task);
1799   do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round,
1800                                                 &do_mal_round, NULL);
1801   LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
1802 }
1803 #endif /* ENABLE_MALICIOUS */
1804
1805
1806 /**
1807  * Send out PUSHes and PULLs, possibly update #view, samplers.
1808  *
1809  * This is executed regylary.
1810  */
1811 static void
1812 do_round (void *cls)
1813 {
1814   uint32_t i;
1815   const struct GNUNET_PeerIdentity *view_array;
1816   unsigned int *permut;
1817   unsigned int a_peers; /* Number of peers we send pushes to */
1818   unsigned int b_peers; /* Number of peers we send pull requests to */
1819   uint32_t first_border;
1820   uint32_t second_border;
1821   struct GNUNET_PeerIdentity peer;
1822   struct GNUNET_PeerIdentity *update_peer;
1823
1824   LOG (GNUNET_ERROR_TYPE_DEBUG,
1825        "Going to execute next round.\n");
1826   do_round_task = NULL;
1827   LOG (GNUNET_ERROR_TYPE_DEBUG,
1828        "Printing view:\n");
1829   to_file (file_name_view_log,
1830            "___ new round ___");
1831   view_array = View_get_as_array ();
1832   for (i = 0; i < View_size (); i++)
1833   {
1834     LOG (GNUNET_ERROR_TYPE_DEBUG,
1835          "\t%s\n", GNUNET_i2s (&view_array[i]));
1836     to_file (file_name_view_log,
1837              "=%s\t(do round)",
1838              GNUNET_i2s_full (&view_array[i]));
1839   }
1840
1841
1842   /* Send pushes and pull requests */
1843   if (0 < View_size ())
1844   {
1845     permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
1846                                            View_size ());
1847
1848     /* Send PUSHes */
1849     a_peers = ceil (alpha * View_size ());
1850
1851     LOG (GNUNET_ERROR_TYPE_DEBUG,
1852          "Going to send pushes to %u (ceil (%f * %u)) peers.\n",
1853          a_peers, alpha, View_size ());
1854     for (i = 0; i < a_peers; i++)
1855     {
1856       peer = view_array[permut[i]];
1857       if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer)) // TODO
1858       { // FIXME if this fails schedule/loop this for later
1859         send_push (&peer);
1860       }
1861     }
1862
1863     /* Send PULL requests */
1864     b_peers = ceil (beta * View_size ());
1865     first_border = a_peers;
1866     second_border = a_peers + b_peers;
1867     if (second_border > View_size ())
1868     {
1869       first_border = View_size () - b_peers;
1870       second_border = View_size ();
1871     }
1872     LOG (GNUNET_ERROR_TYPE_DEBUG,
1873         "Going to send pulls to %u (ceil (%f * %u)) peers.\n",
1874         b_peers, beta, View_size ());
1875     for (i = first_border; i < second_border; i++)
1876     {
1877       peer = view_array[permut[i]];
1878       if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer) &&
1879           GNUNET_NO == Peers_check_peer_flag (&peer, Peers_PULL_REPLY_PENDING)) // TODO
1880       { // FIXME if this fails schedule/loop this for later
1881         send_pull_request (&peer);
1882       }
1883     }
1884
1885     GNUNET_free (permut);
1886     permut = NULL;
1887   }
1888
1889
1890   /* Update view */
1891   /* TODO see how many peers are in push-/pull- list! */
1892
1893   if ((CustomPeerMap_size (push_map) <= alpha * View_size ()) &&
1894       (0 < CustomPeerMap_size (push_map)) &&
1895       (0 < CustomPeerMap_size (pull_map)))
1896   { /* If conditions for update are fulfilled, update */
1897     LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the view.\n");
1898
1899     uint32_t final_size;
1900     uint32_t peers_to_clean_size;
1901     struct GNUNET_PeerIdentity *peers_to_clean;
1902
1903     peers_to_clean = NULL;
1904     peers_to_clean_size = 0;
1905     GNUNET_array_grow (peers_to_clean, peers_to_clean_size, View_size ());
1906     GNUNET_memcpy (peers_to_clean,
1907             view_array,
1908             View_size () * sizeof (struct GNUNET_PeerIdentity));
1909
1910     /* Seems like recreating is the easiest way of emptying the peermap */
1911     View_clear ();
1912     to_file (file_name_view_log,
1913              "--- emptied ---");
1914
1915     first_border  = GNUNET_MIN (ceil (alpha * sampler_size_est_need),
1916                                 CustomPeerMap_size (push_map));
1917     second_border = first_border +
1918                     GNUNET_MIN (floor (beta  * sampler_size_est_need),
1919                                 CustomPeerMap_size (pull_map));
1920     final_size    = second_border +
1921       ceil ((1 - (alpha + beta)) * sampler_size_est_need);
1922
1923     /* Update view with peers received through PUSHes */
1924     permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
1925                                            CustomPeerMap_size (push_map));
1926     for (i = 0; i < first_border; i++)
1927     {
1928       (void) insert_in_view (CustomPeerMap_get_peer_by_index (push_map,
1929                                                               permut[i]));
1930       to_file (file_name_view_log,
1931                "+%s\t(push list)",
1932                GNUNET_i2s_full (&view_array[i]));
1933       // TODO change the peer_flags accordingly
1934     }
1935     GNUNET_free (permut);
1936     permut = NULL;
1937
1938     /* Update view with peers received through PULLs */
1939     permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
1940                                            CustomPeerMap_size (pull_map));
1941     for (i = first_border; i < second_border; i++)
1942     {
1943       (void) insert_in_view (CustomPeerMap_get_peer_by_index (pull_map,
1944             permut[i - first_border]));
1945       to_file (file_name_view_log,
1946                "+%s\t(pull list)",
1947                GNUNET_i2s_full (&view_array[i]));
1948       // TODO change the peer_flags accordingly
1949     }
1950     GNUNET_free (permut);
1951     permut = NULL;
1952
1953     /* Update view with peers from history */
1954     RPS_sampler_get_n_rand_peers (prot_sampler,
1955                                   hist_update,
1956                                   NULL,
1957                                   final_size - second_border);
1958     // TODO change the peer_flags accordingly
1959
1960     for (i = 0; i < View_size (); i++)
1961       rem_from_list (&peers_to_clean, &peers_to_clean_size, &view_array[i]);
1962
1963     /* Clean peers that were removed from the view */
1964     for (i = 0; i < peers_to_clean_size; i++)
1965     {
1966       to_file (file_name_view_log,
1967                "-%s",
1968                GNUNET_i2s_full (&peers_to_clean[i]));
1969       clean_peer (&peers_to_clean[i]);
1970       //peer_destroy_channel_send (sender);
1971     }
1972
1973     GNUNET_array_grow (peers_to_clean, peers_to_clean_size, 0);
1974   }
1975   else
1976   {
1977     LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the view.\n");
1978   }
1979   // TODO independent of that also get some peers from CADET_get_peers()?
1980
1981   LOG (GNUNET_ERROR_TYPE_DEBUG,
1982        "Received %u pushes and %u pulls last round (alpha (%.2f) * view_size (%u) = %.2f)\n",
1983        CustomPeerMap_size (push_map),
1984        CustomPeerMap_size (pull_map),
1985        alpha,
1986        View_size (),
1987        alpha * View_size ());
1988
1989   /* Update samplers */
1990   for (i = 0; i < CustomPeerMap_size (push_map); i++)
1991   {
1992     update_peer = CustomPeerMap_get_peer_by_index (push_map, i);
1993     LOG (GNUNET_ERROR_TYPE_DEBUG,
1994          "Updating with peer %s from push list\n",
1995          GNUNET_i2s (update_peer));
1996     insert_in_sampler (NULL, update_peer);
1997     clean_peer (update_peer); /* This cleans only if it is not in the view */
1998     //peer_destroy_channel_send (sender);
1999   }
2000
2001   for (i = 0; i < CustomPeerMap_size (pull_map); i++)
2002   {
2003     LOG (GNUNET_ERROR_TYPE_DEBUG,
2004          "Updating with peer %s from pull list\n",
2005          GNUNET_i2s (CustomPeerMap_get_peer_by_index (pull_map, i)));
2006     insert_in_sampler (NULL, CustomPeerMap_get_peer_by_index (pull_map, i));
2007     /* This cleans only if it is not in the view */
2008     clean_peer (CustomPeerMap_get_peer_by_index (pull_map, i));
2009     //peer_destroy_channel_send (sender);
2010   }
2011
2012
2013   /* Empty push/pull lists */
2014   CustomPeerMap_clear (push_map);
2015   CustomPeerMap_clear (pull_map);
2016
2017   struct GNUNET_TIME_Relative time_next_round;
2018
2019   time_next_round = compute_rand_delay (round_interval, 2);
2020
2021   /* Schedule next round */
2022   do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round,
2023                                                 &do_round, NULL);
2024   LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
2025 }
2026
2027
2028 /**
2029  * This is called from GNUNET_CADET_get_peers().
2030  *
2031  * It is called on every peer(ID) that cadet somehow has contact with.
2032  * We use those to initialise the sampler.
2033  */
2034 void
2035 init_peer_cb (void *cls,
2036               const struct GNUNET_PeerIdentity *peer,
2037               int tunnel, // "Do we have a tunnel towards this peer?"
2038               unsigned int n_paths, // "Number of known paths towards this peer"
2039               unsigned int best_path) // "How long is the best path?
2040                                       // (0 = unknown, 1 = ourselves, 2 = neighbor)"
2041 {
2042   if (NULL != peer)
2043   {
2044     LOG (GNUNET_ERROR_TYPE_DEBUG,
2045          "Got peer_id %s from cadet\n",
2046          GNUNET_i2s (peer));
2047     got_peer (peer);
2048   }
2049 }
2050
2051 /**
2052  * @brief Iterator function over stored, valid peers.
2053  *
2054  * We initialise the sampler with those.
2055  *
2056  * @param cls the closure
2057  * @param peer the peer id
2058  * @return #GNUNET_YES if we should continue to
2059  *         iterate,
2060  *         #GNUNET_NO if not.
2061  */
2062 static int
2063 valid_peers_iterator (void *cls,
2064                       const struct GNUNET_PeerIdentity *peer)
2065 {
2066   if (NULL != peer)
2067   {
2068     LOG (GNUNET_ERROR_TYPE_DEBUG,
2069          "Got stored, valid peer %s\n",
2070          GNUNET_i2s (peer));
2071     got_peer (peer);
2072   }
2073   return GNUNET_YES;
2074 }
2075
2076
2077 /**
2078  * Iterator over peers from peerinfo.
2079  *
2080  * @param cls closure
2081  * @param peer id of the peer, NULL for last call
2082  * @param hello hello message for the peer (can be NULL)
2083  * @param error message
2084  */
2085 void
2086 process_peerinfo_peers (void *cls,
2087                         const struct GNUNET_PeerIdentity *peer,
2088                         const struct GNUNET_HELLO_Message *hello,
2089                         const char *err_msg)
2090 {
2091   if (NULL != peer)
2092   {
2093     LOG (GNUNET_ERROR_TYPE_DEBUG,
2094          "Got peer_id %s from peerinfo\n",
2095          GNUNET_i2s (peer));
2096     got_peer (peer);
2097   }
2098 }
2099
2100
2101 /**
2102  * Task run during shutdown.
2103  *
2104  * @param cls unused
2105  */
2106 static void
2107 shutdown_task (void *cls)
2108 {
2109   struct ClientContext *client_ctx;
2110   struct ReplyCls *reply_cls;
2111
2112   LOG (GNUNET_ERROR_TYPE_DEBUG,
2113        "RPS is going down\n");
2114
2115   /* Clean all clients */
2116   for (client_ctx = cli_ctx_head;
2117        NULL != cli_ctx_head;
2118        client_ctx = cli_ctx_head)
2119   {
2120     /* Clean pending requests to the sampler */
2121     for (reply_cls = client_ctx->rep_cls_head;
2122          NULL != client_ctx->rep_cls_head;
2123          reply_cls = client_ctx->rep_cls_head)
2124     {
2125       RPS_sampler_request_cancel (reply_cls->req_handle);
2126       GNUNET_CONTAINER_DLL_remove (client_ctx->rep_cls_head,
2127                                    client_ctx->rep_cls_tail,
2128                                    reply_cls);
2129       GNUNET_free (reply_cls);
2130     }
2131     GNUNET_CONTAINER_DLL_remove (cli_ctx_head, cli_ctx_tail, client_ctx);
2132     GNUNET_free (client_ctx);
2133   }
2134   GNUNET_PEERINFO_notify_cancel (peerinfo_notify_handle);
2135   GNUNET_PEERINFO_disconnect (peerinfo_handle);
2136
2137   if (NULL != do_round_task)
2138   {
2139     GNUNET_SCHEDULER_cancel (do_round_task);
2140     do_round_task = NULL;
2141   }
2142
2143   Peers_terminate ();
2144
2145   GNUNET_NSE_disconnect (nse);
2146   RPS_sampler_destroy (prot_sampler);
2147   RPS_sampler_destroy (client_sampler);
2148   GNUNET_CADET_close_port (cadet_port);
2149   GNUNET_CADET_disconnect (cadet_handle);
2150   View_destroy ();
2151   CustomPeerMap_destroy (push_map);
2152   CustomPeerMap_destroy (pull_map);
2153   #ifdef ENABLE_MALICIOUS
2154   struct AttackedPeer *tmp_att_peer;
2155   GNUNET_free (file_name_view_log);
2156   GNUNET_array_grow (mal_peers, num_mal_peers, 0);
2157   if (NULL != mal_peer_set)
2158     GNUNET_CONTAINER_multipeermap_destroy (mal_peer_set);
2159   if (NULL != att_peer_set)
2160     GNUNET_CONTAINER_multipeermap_destroy (att_peer_set);
2161   while (NULL != att_peers_head)
2162   {
2163     tmp_att_peer = att_peers_head;
2164     GNUNET_CONTAINER_DLL_remove (att_peers_head, att_peers_tail, tmp_att_peer);
2165   }
2166   #endif /* ENABLE_MALICIOUS */
2167 }
2168
2169
2170 /**
2171  * Handle client connecting to the service.
2172  *
2173  * @param cls NULL
2174  * @param client the new client
2175  * @param mq the message queue of @a client
2176  * @return @a client
2177  */
2178 static void *
2179 client_connect_cb (void *cls,
2180                    struct GNUNET_SERVICE_Client *client,
2181                    struct GNUNET_MQ_Handle *mq)
2182 {
2183   struct ClientContext *cli_ctx;
2184
2185   LOG (GNUNET_ERROR_TYPE_DEBUG,
2186        "Client connected\n");
2187   if (NULL == client)
2188     return client; /* Server was destroyed before a client connected. Shutting down */
2189   cli_ctx = GNUNET_new (struct ClientContext);
2190   cli_ctx->mq = GNUNET_SERVICE_client_get_mq (client);
2191   cli_ctx->client = client;
2192   GNUNET_CONTAINER_DLL_insert (cli_ctx_head,
2193                                cli_ctx_tail,
2194                                cli_ctx);
2195   return cli_ctx;
2196 }
2197
2198 /**
2199  * Callback called when a client disconnected from the service
2200  *
2201  * @param cls closure for the service
2202  * @param c the client that disconnected
2203  * @param internal_cls should be equal to @a c
2204  */
2205 static void
2206 client_disconnect_cb (void *cls,
2207                       struct GNUNET_SERVICE_Client *client,
2208                       void *internal_cls)
2209 {
2210   struct ClientContext *cli_ctx = internal_cls;
2211
2212   GNUNET_assert (client == cli_ctx->client);
2213   if (NULL == client)
2214   {/* shutdown task - destroy all clients */
2215     while (NULL != cli_ctx_head)
2216       destroy_cli_ctx (cli_ctx_head);
2217   }
2218   else
2219   { /* destroy this client */
2220     LOG (GNUNET_ERROR_TYPE_DEBUG,
2221         "Client disconnected. Destroy its context.\n");
2222     destroy_cli_ctx (cli_ctx);
2223   }
2224 }
2225
2226
2227 /**
2228  * Handle random peer sampling clients.
2229  *
2230  * @param cls closure
2231  * @param c configuration to use
2232  * @param service the initialized service
2233  */
2234 static void
2235 run (void *cls,
2236      const struct GNUNET_CONFIGURATION_Handle *c,
2237      struct GNUNET_SERVICE_Handle *service)
2238 {
2239   struct GNUNET_MQ_MessageHandler cadet_handlers[] = {
2240     GNUNET_MQ_hd_fixed_size (peer_check,
2241                              GNUNET_MESSAGE_TYPE_RPS_PP_CHECK_LIVE,
2242                              struct GNUNET_MessageHeader,
2243                              NULL),
2244     GNUNET_MQ_hd_fixed_size (peer_push,
2245                              GNUNET_MESSAGE_TYPE_RPS_PP_PUSH,
2246                              struct GNUNET_MessageHeader,
2247                              NULL),
2248     GNUNET_MQ_hd_fixed_size (peer_pull_request,
2249                              GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
2250                              struct GNUNET_MessageHeader,
2251                              NULL),
2252     GNUNET_MQ_hd_var_size (peer_pull_reply,
2253                            GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY,
2254                            struct GNUNET_RPS_P2P_PullReplyMessage,
2255                            NULL),
2256     GNUNET_MQ_handler_end ()
2257   };
2258
2259   int size;
2260   int out_size;
2261   char* fn_valid_peers;
2262   struct GNUNET_HashCode port;
2263
2264   GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
2265   cfg = c;
2266
2267
2268   /* Get own ID */
2269   GNUNET_CRYPTO_get_peer_identity (cfg, &own_identity); // TODO check return value
2270   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2271               "STARTING SERVICE (rps) for peer [%s]\n",
2272               GNUNET_i2s (&own_identity));
2273   #ifdef ENABLE_MALICIOUS
2274   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2275               "Malicious execution compiled in.\n");
2276   #endif /* ENABLE_MALICIOUS */
2277
2278
2279
2280   /* Get time interval from the configuration */
2281   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
2282                                                         "ROUNDINTERVAL",
2283                                                         &round_interval))
2284   {
2285     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2286                                "RPS", "ROUNDINTERVAL");
2287     GNUNET_SCHEDULER_shutdown ();
2288     return;
2289   }
2290
2291   /* Get initial size of sampler/view from the configuration */
2292   if (GNUNET_OK !=
2293       GNUNET_CONFIGURATION_get_value_number (cfg, "RPS", "INITSIZE",
2294         (long long unsigned int *) &sampler_size_est_need))
2295   {
2296     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2297                                "RPS", "INITSIZE");
2298     GNUNET_SCHEDULER_shutdown ();
2299     return;
2300   }
2301   LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %u\n", sampler_size_est_need);
2302
2303   if (GNUNET_OK !=
2304       GNUNET_CONFIGURATION_get_value_filename (cfg,
2305                                                "rps",
2306                                                "FILENAME_VALID_PEERS",
2307                                                &fn_valid_peers))
2308   {
2309     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2310                                "rps", "FILENAME_VALID_PEERS");
2311   }
2312
2313
2314   View_create (4);
2315
2316   /* file_name_view_log */
2317   if (GNUNET_OK != GNUNET_DISK_directory_create ("/tmp/rps/"))
2318   {
2319     LOG (GNUNET_ERROR_TYPE_WARNING,
2320          "Failed to create directory /tmp/rps/\n");
2321   }
2322
2323   size = (14 + strlen (GNUNET_i2s_full (&own_identity)) + 1) * sizeof (char);
2324   file_name_view_log = GNUNET_malloc (size);
2325   out_size = GNUNET_snprintf (file_name_view_log,
2326                               size,
2327                               "/tmp/rps/view-%s",
2328                               GNUNET_i2s_full (&own_identity));
2329   if (size < out_size ||
2330       0 > out_size)
2331   {
2332     LOG (GNUNET_ERROR_TYPE_WARNING,
2333          "Failed to write string to buffer (size: %i, out_size: %i)\n",
2334          size,
2335          out_size);
2336   }
2337
2338
2339   /* connect to NSE */
2340   nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
2341
2342
2343   alpha = 0.45;
2344   beta  = 0.45;
2345
2346
2347   /* Initialise cadet */
2348   cadet_handle = GNUNET_CADET_connect (cfg);
2349   GNUNET_assert (NULL != cadet_handle);
2350   GNUNET_CRYPTO_hash (GNUNET_APPLICATION_PORT_RPS,
2351                       strlen (GNUNET_APPLICATION_PORT_RPS),
2352                       &port);
2353   cadet_port = GNUNET_CADET_open_port (cadet_handle,
2354                                        &port,
2355                                        &Peers_handle_inbound_channel, /* Connect handler */
2356                                        NULL, /* cls */
2357                                        NULL, /* WindowSize handler */
2358                                        cleanup_destroyed_channel, /* Disconnect handler */
2359                                        cadet_handlers);
2360
2361
2362   peerinfo_handle = GNUNET_PEERINFO_connect (cfg);
2363   Peers_initialise (fn_valid_peers, cadet_handle, cleanup_destroyed_channel,
2364                     cadet_handlers, &own_identity);
2365   GNUNET_free (fn_valid_peers);
2366
2367   /* Initialise sampler */
2368   struct GNUNET_TIME_Relative half_round_interval;
2369   struct GNUNET_TIME_Relative  max_round_interval;
2370
2371   half_round_interval = GNUNET_TIME_relative_divide (round_interval, 2);
2372   max_round_interval = GNUNET_TIME_relative_add (round_interval, half_round_interval);
2373
2374   prot_sampler =   RPS_sampler_init     (sampler_size_est_need, max_round_interval);
2375   client_sampler = RPS_sampler_mod_init (sampler_size_est_need, max_round_interval);
2376
2377   /* Initialise push and pull maps */
2378   push_map = CustomPeerMap_create (4);
2379   pull_map = CustomPeerMap_create (4);
2380
2381
2382   //LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
2383   //GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, NULL);
2384   // TODO send push/pull to each of those peers?
2385   // TODO read stored valid peers from last run
2386   LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting stored valid peers\n");
2387   Peers_get_valid_peers (valid_peers_iterator, NULL);
2388
2389   peerinfo_notify_handle = GNUNET_PEERINFO_notify (cfg,
2390                                                    GNUNET_NO,
2391                                                    process_peerinfo_peers,
2392                                                    NULL);
2393
2394   LOG (GNUNET_ERROR_TYPE_INFO, "Ready to receive requests from clients\n");
2395
2396   do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
2397   LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
2398
2399   GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
2400 }
2401
2402
2403 /**
2404  * Define "main" method using service macro.
2405  */
2406 GNUNET_SERVICE_MAIN
2407 ("rps",
2408  GNUNET_SERVICE_OPTION_NONE,
2409  &run,
2410  &client_connect_cb,
2411  &client_disconnect_cb,
2412  NULL,
2413  GNUNET_MQ_hd_fixed_size (client_request,
2414    GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
2415    struct GNUNET_RPS_CS_RequestMessage,
2416    NULL),
2417  GNUNET_MQ_hd_fixed_size (client_request_cancel,
2418    GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST_CANCEL,
2419    struct GNUNET_RPS_CS_RequestCancelMessage,
2420    NULL),
2421  GNUNET_MQ_hd_var_size (client_seed,
2422    GNUNET_MESSAGE_TYPE_RPS_CS_SEED,
2423    struct GNUNET_RPS_CS_SeedMessage,
2424    NULL),
2425 #ifdef ENABLE_MALICIOUS
2426  GNUNET_MQ_hd_var_size (client_act_malicious,
2427    GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS,
2428    struct GNUNET_RPS_CS_ActMaliciousMessage,
2429    NULL),
2430 #endif /* ENABLE_MALICIOUS */
2431  GNUNET_MQ_handler_end());
2432
2433 /* end of gnunet-service-rps.c */