rps: close port before disconnecting
[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   RPS_sampler_request_cancel (rep_cls->req_handle);
1125   destroy_reply_cls (rep_cls);
1126   GNUNET_SERVICE_client_continue (cli_ctx->client);
1127 }
1128
1129
1130 /**
1131  * @brief This function is called, when the client seeds peers.
1132  * It verifies that @a msg is well-formed.
1133  *
1134  * @param cls the closure (#ClientContext)
1135  * @param msg the message
1136  * @return #GNUNET_OK if @a msg is well-formed
1137  */
1138 static int
1139 check_client_seed (void *cls, const struct GNUNET_RPS_CS_SeedMessage *msg)
1140 {
1141   struct ClientContext *cli_ctx = cls;
1142   uint16_t msize = ntohs (msg->header.size);
1143   uint32_t num_peers = ntohl (msg->num_peers);
1144
1145   msize -= sizeof (struct GNUNET_RPS_CS_SeedMessage);
1146   if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) ||
1147        (msize % sizeof (struct GNUNET_PeerIdentity) != 0) )
1148   {
1149     GNUNET_break (0);
1150     GNUNET_SERVICE_client_drop (cli_ctx->client);
1151     return GNUNET_SYSERR;
1152   }
1153   return GNUNET_OK;
1154 }
1155
1156
1157 /**
1158  * Handle seed from the client.
1159  *
1160  * @param cls closure
1161  * @param message the actual message
1162  */
1163 static void
1164 handle_client_seed (void *cls,
1165                     const struct GNUNET_RPS_CS_SeedMessage *msg)
1166 {
1167   struct ClientContext *cli_ctx = cls;
1168   struct GNUNET_PeerIdentity *peers;
1169   uint32_t num_peers;
1170   uint32_t i;
1171
1172   num_peers = ntohl (msg->num_peers);
1173   peers = (struct GNUNET_PeerIdentity *) &msg[1];
1174   //peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1175   //GNUNET_memcpy (peers, &msg[1], num_peers * sizeof (struct GNUNET_PeerIdentity));
1176
1177   LOG (GNUNET_ERROR_TYPE_DEBUG,
1178        "Client seeded peers:\n");
1179   print_peer_list (peers, num_peers);
1180
1181   for (i = 0; i < num_peers; i++)
1182   {
1183     LOG (GNUNET_ERROR_TYPE_DEBUG,
1184          "Updating samplers with seed %" PRIu32 ": %s\n",
1185          i,
1186          GNUNET_i2s (&peers[i]));
1187
1188     got_peer (&peers[i]);
1189   }
1190
1191   ////GNUNET_free (peers);
1192
1193   GNUNET_SERVICE_client_continue (cli_ctx->client);
1194 }
1195
1196 /**
1197  * Handle a CHECK_LIVE message from another peer.
1198  *
1199  * This does nothing. But without calling #GNUNET_CADET_receive_done()
1200  * the channel is blocked for all other communication.
1201  *
1202  * @param cls Closure
1203  * @param msg The message header
1204  */
1205 static void
1206 handle_peer_check (void *cls,
1207                    const struct GNUNET_MessageHeader *msg)
1208 {
1209   const struct GNUNET_PeerIdentity *peer = cls;
1210
1211   GNUNET_CADET_receive_done (Peers_get_recv_channel (peer));
1212 }
1213
1214 /**
1215  * Handle a PUSH message from another peer.
1216  *
1217  * Check the proof of work and store the PeerID
1218  * in the temporary list for pushed PeerIDs.
1219  *
1220  * @param cls Closure
1221  * @param msg The message header
1222  */
1223 static void
1224 handle_peer_push (void *cls,
1225                   const struct GNUNET_MessageHeader *msg)
1226 {
1227   const struct GNUNET_PeerIdentity *peer = cls;
1228
1229   // (check the proof of work (?))
1230
1231   LOG (GNUNET_ERROR_TYPE_DEBUG,
1232        "Received PUSH (%s)\n",
1233        GNUNET_i2s (peer));
1234
1235   #ifdef ENABLE_MALICIOUS
1236   struct AttackedPeer *tmp_att_peer;
1237
1238   if ( (1 == mal_type) ||
1239        (3 == mal_type) )
1240   { /* Try to maximise representation */
1241     tmp_att_peer = GNUNET_new (struct AttackedPeer);
1242     tmp_att_peer->peer_id = *peer;
1243     if (NULL == att_peer_set)
1244       att_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
1245     if (GNUNET_NO ==
1246         GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1247                                                 peer))
1248     {
1249       GNUNET_CONTAINER_DLL_insert (att_peers_head,
1250                                    att_peers_tail,
1251                                    tmp_att_peer);
1252       add_peer_array_to_set (peer, 1, att_peer_set);
1253     }
1254   }
1255
1256
1257   else if (2 == mal_type)
1258   {
1259     /* We attack one single well-known peer - simply ignore */
1260   }
1261   #endif /* ENABLE_MALICIOUS */
1262
1263   /* Add the sending peer to the push_map */
1264   CustomPeerMap_put (push_map, peer);
1265
1266   GNUNET_CADET_receive_done (Peers_get_recv_channel (peer));
1267 }
1268
1269
1270 /**
1271  * Handle PULL REQUEST request message from another peer.
1272  *
1273  * Reply with the view of PeerIDs.
1274  *
1275  * @param cls Closure
1276  * @param msg The message header
1277  */
1278 static void
1279 handle_peer_pull_request (void *cls,
1280                           const struct GNUNET_MessageHeader *msg)
1281 {
1282   struct GNUNET_PeerIdentity *peer = cls;
1283   const struct GNUNET_PeerIdentity *view_array;
1284
1285   LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REQUEST (%s)\n", GNUNET_i2s (peer));
1286
1287   #ifdef ENABLE_MALICIOUS
1288   if (1 == mal_type
1289       || 3 == mal_type)
1290   { /* Try to maximise representation */
1291     send_pull_reply (peer, mal_peers, num_mal_peers);
1292   }
1293
1294   else if (2 == mal_type)
1295   { /* Try to partition network */
1296     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
1297     {
1298       send_pull_reply (peer, mal_peers, num_mal_peers);
1299     }
1300   }
1301   #endif /* ENABLE_MALICIOUS */
1302
1303   view_array = View_get_as_array ();
1304   send_pull_reply (peer, view_array, View_size ());
1305
1306   GNUNET_CADET_receive_done (Peers_get_recv_channel (peer));
1307 }
1308
1309
1310 /**
1311  * Check whether we sent a corresponding request and
1312  * whether this reply is the first one.
1313  *
1314  * @param cls Closure
1315  * @param msg The message header
1316  */
1317 static int
1318 check_peer_pull_reply (void *cls,
1319                        const struct GNUNET_RPS_P2P_PullReplyMessage *msg)
1320 {
1321   struct GNUNET_PeerIdentity *sender = cls;
1322
1323   if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->header.size))
1324   {
1325     GNUNET_break_op (0);
1326     return GNUNET_SYSERR;
1327   }
1328
1329   if ((ntohs (msg->header.size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1330       sizeof (struct GNUNET_PeerIdentity) != ntohl (msg->num_peers))
1331   {
1332     LOG (GNUNET_ERROR_TYPE_ERROR,
1333         "message says it sends %" PRIu32 " peers, have space for %lu peers\n",
1334         ntohl (msg->num_peers),
1335         (ntohs (msg->header.size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1336             sizeof (struct GNUNET_PeerIdentity));
1337     GNUNET_break_op (0);
1338     return GNUNET_SYSERR;
1339   }
1340
1341   if (GNUNET_YES != Peers_check_peer_flag (sender, Peers_PULL_REPLY_PENDING))
1342   {
1343     LOG (GNUNET_ERROR_TYPE_WARNING,
1344         "Received a pull reply from a peer we didn't request one from!\n");
1345     GNUNET_break_op (0);
1346     return GNUNET_SYSERR;
1347   }
1348   return GNUNET_OK;
1349 }
1350
1351 /**
1352  * Handle PULL REPLY message from another peer.
1353  *
1354  * @param cls Closure
1355  * @param msg The message header
1356  */
1357 static void
1358 handle_peer_pull_reply (void *cls,
1359                         const struct GNUNET_RPS_P2P_PullReplyMessage *msg)
1360 {
1361   const struct GNUNET_PeerIdentity *peers;
1362   struct GNUNET_PeerIdentity *sender = cls;
1363   uint32_t i;
1364 #ifdef ENABLE_MALICIOUS
1365   struct AttackedPeer *tmp_att_peer;
1366 #endif /* ENABLE_MALICIOUS */
1367
1368   LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REPLY (%s)\n", GNUNET_i2s (sender));
1369
1370   #ifdef ENABLE_MALICIOUS
1371   // We shouldn't even receive pull replies as we're not sending
1372   if (2 == mal_type)
1373   {
1374   }
1375   #endif /* ENABLE_MALICIOUS */
1376
1377   /* Do actual logic */
1378   peers = (const struct GNUNET_PeerIdentity *) &msg[1];
1379
1380   LOG (GNUNET_ERROR_TYPE_DEBUG,
1381        "PULL REPLY received, got following %u peers:\n",
1382        ntohl (msg->num_peers));
1383
1384   for (i = 0; i < ntohl (msg->num_peers); i++)
1385   {
1386     LOG (GNUNET_ERROR_TYPE_DEBUG,
1387          "%u. %s\n",
1388          i,
1389          GNUNET_i2s (&peers[i]));
1390
1391     #ifdef ENABLE_MALICIOUS
1392     if ((NULL != att_peer_set) &&
1393         (1 == mal_type || 3 == mal_type))
1394     { /* Add attacked peer to local list */
1395       // TODO check if we sent a request and this was the first reply
1396       if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1397                                                                &peers[i])
1398           && GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mal_peer_set,
1399                                                                   &peers[i])
1400           && 0 != GNUNET_CRYPTO_cmp_peer_identity (&peers[i],
1401                                                    &own_identity))
1402       {
1403         tmp_att_peer = GNUNET_new (struct AttackedPeer);
1404         tmp_att_peer->peer_id = peers[i];
1405         GNUNET_CONTAINER_DLL_insert (att_peers_head,
1406                                      att_peers_tail,
1407                                      tmp_att_peer);
1408         add_peer_array_to_set (&peers[i], 1, att_peer_set);
1409       }
1410       continue;
1411     }
1412     #endif /* ENABLE_MALICIOUS */
1413     if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity,
1414                                               &peers[i]))
1415     {
1416       /* Make sure we 'know' about this peer */
1417       (void) Peers_insert_peer (&peers[i]);
1418
1419       if (GNUNET_YES == Peers_check_peer_valid (&peers[i]))
1420       {
1421         CustomPeerMap_put (pull_map, &peers[i]);
1422       }
1423       else
1424       {
1425         Peers_schedule_operation (&peers[i], insert_in_pull_map);
1426         (void) Peers_issue_peer_liveliness_check (&peers[i]);
1427       }
1428     }
1429   }
1430
1431   Peers_unset_peer_flag (sender, Peers_PULL_REPLY_PENDING);
1432   clean_peer (sender);
1433
1434   GNUNET_CADET_receive_done (Peers_get_recv_channel (sender));
1435 }
1436
1437
1438 /**
1439  * Compute a random delay.
1440  * A uniformly distributed value between mean + spread and mean - spread.
1441  *
1442  * For example for mean 4 min and spread 2 the minimum is (4 min - (1/2 * 4 min))
1443  * It would return a random value between 2 and 6 min.
1444  *
1445  * @param mean the mean
1446  * @param spread the inverse amount of deviation from the mean
1447  */
1448 static struct GNUNET_TIME_Relative
1449 compute_rand_delay (struct GNUNET_TIME_Relative mean,
1450                     unsigned int spread)
1451 {
1452   struct GNUNET_TIME_Relative half_interval;
1453   struct GNUNET_TIME_Relative ret;
1454   unsigned int rand_delay;
1455   unsigned int max_rand_delay;
1456
1457   if (0 == spread)
1458   {
1459     LOG (GNUNET_ERROR_TYPE_WARNING,
1460          "Not accepting spread of 0\n");
1461     GNUNET_break (0);
1462     GNUNET_assert (0);
1463   }
1464   GNUNET_assert (0 != mean.rel_value_us);
1465
1466   /* Compute random time value between spread * mean and spread * mean */
1467   half_interval = GNUNET_TIME_relative_divide (mean, spread);
1468
1469   max_rand_delay = GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us / mean.rel_value_us * (2/spread);
1470   /**
1471    * Compute random value between (0 and 1) * round_interval
1472    * via multiplying round_interval with a 'fraction' (0 to value)/value
1473    */
1474   rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_rand_delay);
1475   ret = GNUNET_TIME_relative_saturating_multiply (mean,  rand_delay);
1476   ret = GNUNET_TIME_relative_divide   (ret, max_rand_delay);
1477   ret = GNUNET_TIME_relative_add      (ret, half_interval);
1478
1479   if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ret.rel_value_us)
1480     LOG (GNUNET_ERROR_TYPE_WARNING,
1481          "Returning FOREVER_REL\n");
1482
1483   return ret;
1484 }
1485
1486
1487 /**
1488  * Send single pull request
1489  *
1490  * @param peer_id the peer to send the pull request to.
1491  */
1492 static void
1493 send_pull_request (const struct GNUNET_PeerIdentity *peer)
1494 {
1495   struct GNUNET_MQ_Envelope *ev;
1496
1497   GNUNET_assert (GNUNET_NO == Peers_check_peer_flag (peer,
1498                                                      Peers_PULL_REPLY_PENDING));
1499   Peers_set_peer_flag (peer, Peers_PULL_REPLY_PENDING);
1500
1501   LOG (GNUNET_ERROR_TYPE_DEBUG,
1502        "Going to send PULL REQUEST to peer %s.\n",
1503        GNUNET_i2s (peer));
1504
1505   ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
1506   Peers_send_message (peer, ev, "PULL REQUEST");
1507 }
1508
1509
1510 /**
1511  * Send single push
1512  *
1513  * @param peer_id the peer to send the push to.
1514  */
1515 static void
1516 send_push (const struct GNUNET_PeerIdentity *peer_id)
1517 {
1518   struct GNUNET_MQ_Envelope *ev;
1519
1520   LOG (GNUNET_ERROR_TYPE_DEBUG,
1521        "Going to send PUSH to peer %s.\n",
1522        GNUNET_i2s (peer_id));
1523
1524   ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
1525   Peers_send_message (peer_id, ev, "PUSH");
1526 }
1527
1528
1529 static void
1530 do_round (void *cls);
1531
1532 static void
1533 do_mal_round (void *cls);
1534
1535 #ifdef ENABLE_MALICIOUS
1536
1537
1538 /**
1539  * @brief This function is called, when the client tells us to act malicious.
1540  * It verifies that @a msg is well-formed.
1541  *
1542  * @param cls the closure (#ClientContext)
1543  * @param msg the message
1544  * @return #GNUNET_OK if @a msg is well-formed
1545  */
1546 static int
1547 check_client_act_malicious (void *cls,
1548                             const struct GNUNET_RPS_CS_ActMaliciousMessage *msg)
1549 {
1550   struct ClientContext *cli_ctx = cls;
1551   uint16_t msize = ntohs (msg->header.size);
1552   uint32_t num_peers = ntohl (msg->num_peers);
1553
1554   msize -= sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage);
1555   if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) ||
1556        (msize % sizeof (struct GNUNET_PeerIdentity) != 0) )
1557   {
1558     LOG (GNUNET_ERROR_TYPE_ERROR,
1559         "message says it sends %" PRIu32 " peers, have space for %lu peers\n",
1560         ntohl (msg->num_peers),
1561         (msize / sizeof (struct GNUNET_PeerIdentity)));
1562     GNUNET_break (0);
1563     GNUNET_SERVICE_client_drop (cli_ctx->client);
1564     return GNUNET_SYSERR;
1565   }
1566   return GNUNET_OK;
1567 }
1568
1569 /**
1570  * Turn RPS service to act malicious.
1571  *
1572  * @param cls Closure
1573  * @param client The client that sent the message
1574  * @param msg The message header
1575  */
1576 static void
1577 handle_client_act_malicious (void *cls,
1578                              const struct GNUNET_RPS_CS_ActMaliciousMessage *msg)
1579 {
1580   struct ClientContext *cli_ctx = cls;
1581   struct GNUNET_PeerIdentity *peers;
1582   uint32_t num_mal_peers_sent;
1583   uint32_t num_mal_peers_old;
1584
1585   /* Do actual logic */
1586   peers = (struct GNUNET_PeerIdentity *) &msg[1];
1587   mal_type = ntohl (msg->type);
1588   if (NULL == mal_peer_set)
1589     mal_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
1590
1591   LOG (GNUNET_ERROR_TYPE_DEBUG,
1592        "Now acting malicious type %" PRIu32 ", got %" PRIu32 " peers.\n",
1593        mal_type,
1594        ntohl (msg->num_peers));
1595
1596   if (1 == mal_type)
1597   { /* Try to maximise representation */
1598     /* Add other malicious peers to those we already know */
1599
1600     num_mal_peers_sent = ntohl (msg->num_peers);
1601     num_mal_peers_old = num_mal_peers;
1602     GNUNET_array_grow (mal_peers,
1603                        num_mal_peers,
1604                        num_mal_peers + num_mal_peers_sent);
1605     GNUNET_memcpy (&mal_peers[num_mal_peers_old],
1606             peers,
1607             num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1608
1609     /* Add all mal peers to mal_peer_set */
1610     add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1611                            num_mal_peers_sent,
1612                            mal_peer_set);
1613
1614     /* Substitute do_round () with do_mal_round () */
1615     GNUNET_SCHEDULER_cancel (do_round_task);
1616     do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1617   }
1618
1619   else if ( (2 == mal_type) ||
1620             (3 == mal_type) )
1621   { /* Try to partition the network */
1622     /* Add other malicious peers to those we already know */
1623
1624     num_mal_peers_sent = ntohl (msg->num_peers) - 1;
1625     num_mal_peers_old = num_mal_peers;
1626     GNUNET_array_grow (mal_peers,
1627                        num_mal_peers,
1628                        num_mal_peers + num_mal_peers_sent);
1629     if (NULL != mal_peers &&
1630         0 != num_mal_peers)
1631     {
1632       GNUNET_memcpy (&mal_peers[num_mal_peers_old],
1633               peers,
1634               num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1635
1636       /* Add all mal peers to mal_peer_set */
1637       add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1638                              num_mal_peers_sent,
1639                              mal_peer_set);
1640     }
1641
1642     /* Store the one attacked peer */
1643     GNUNET_memcpy (&attacked_peer,
1644             &msg->attacked_peer,
1645             sizeof (struct GNUNET_PeerIdentity));
1646     /* Set the flag of the attacked peer to valid to avoid problems */
1647     if (GNUNET_NO == Peers_check_peer_known (&attacked_peer))
1648     {
1649       (void) Peers_issue_peer_liveliness_check (&attacked_peer);
1650     }
1651
1652     LOG (GNUNET_ERROR_TYPE_DEBUG,
1653          "Attacked peer is %s\n",
1654          GNUNET_i2s (&attacked_peer));
1655
1656     /* Substitute do_round () with do_mal_round () */
1657     GNUNET_SCHEDULER_cancel (do_round_task);
1658     do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1659   }
1660   else if (0 == mal_type)
1661   { /* Stop acting malicious */
1662     GNUNET_array_grow (mal_peers, num_mal_peers, 0);
1663
1664     /* Substitute do_mal_round () with do_round () */
1665     GNUNET_SCHEDULER_cancel (do_round_task);
1666     do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
1667   }
1668   else
1669   {
1670     GNUNET_break (0);
1671     GNUNET_SERVICE_client_continue (cli_ctx->client);
1672   }
1673   GNUNET_SERVICE_client_continue (cli_ctx->client);
1674 }
1675
1676
1677 /**
1678  * Send out PUSHes and PULLs maliciously.
1679  *
1680  * This is executed regylary.
1681  */
1682 static void
1683 do_mal_round (void *cls)
1684 {
1685   uint32_t num_pushes;
1686   uint32_t i;
1687   struct GNUNET_TIME_Relative time_next_round;
1688   struct AttackedPeer *tmp_att_peer;
1689
1690   LOG (GNUNET_ERROR_TYPE_DEBUG,
1691        "Going to execute next round maliciously type %" PRIu32 ".\n",
1692       mal_type);
1693   do_round_task = NULL;
1694   GNUNET_assert (mal_type <= 3);
1695   /* Do malicious actions */
1696   if (1 == mal_type)
1697   { /* Try to maximise representation */
1698
1699     /* The maximum of pushes we're going to send this round */
1700     num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit,
1701                                          num_attacked_peers),
1702                              GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
1703
1704     LOG (GNUNET_ERROR_TYPE_DEBUG,
1705          "Going to send %" PRIu32 " pushes\n",
1706          num_pushes);
1707
1708     /* Send PUSHes to attacked peers */
1709     for (i = 0 ; i < num_pushes ; i++)
1710     {
1711       if (att_peers_tail == att_peer_index)
1712         att_peer_index = att_peers_head;
1713       else
1714         att_peer_index = att_peer_index->next;
1715
1716       send_push (&att_peer_index->peer_id);
1717     }
1718
1719     /* Send PULLs to some peers to learn about additional peers to attack */
1720     tmp_att_peer = att_peer_index;
1721     for (i = 0 ; i < num_pushes * alpha ; i++)
1722     {
1723       if (att_peers_tail == tmp_att_peer)
1724         tmp_att_peer = att_peers_head;
1725       else
1726         att_peer_index = tmp_att_peer->next;
1727
1728       send_pull_request (&tmp_att_peer->peer_id);
1729     }
1730   }
1731
1732
1733   else if (2 == mal_type)
1734   { /**
1735      * Try to partition the network
1736      * Send as many pushes to the attacked peer as possible
1737      * That is one push per round as it will ignore more.
1738      */
1739     (void) Peers_issue_peer_liveliness_check (&attacked_peer);
1740     if (GNUNET_YES == Peers_check_peer_flag (&attacked_peer, Peers_ONLINE))
1741       send_push (&attacked_peer);
1742   }
1743
1744
1745   if (3 == mal_type)
1746   { /* Combined attack */
1747
1748     /* Send PUSH to attacked peers */
1749     if (GNUNET_YES == Peers_check_peer_known (&attacked_peer))
1750     {
1751       (void) Peers_issue_peer_liveliness_check (&attacked_peer);
1752       if (GNUNET_YES == Peers_check_peer_flag (&attacked_peer, Peers_ONLINE))
1753       {
1754         LOG (GNUNET_ERROR_TYPE_DEBUG,
1755             "Goding to send push to attacked peer (%s)\n",
1756             GNUNET_i2s (&attacked_peer));
1757         send_push (&attacked_peer);
1758       }
1759     }
1760     (void) Peers_issue_peer_liveliness_check (&attacked_peer);
1761
1762     /* The maximum of pushes we're going to send this round */
1763     num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit - 1,
1764                                          num_attacked_peers),
1765                              GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
1766
1767     LOG (GNUNET_ERROR_TYPE_DEBUG,
1768          "Going to send %" PRIu32 " pushes\n",
1769          num_pushes);
1770
1771     for (i = 0; i < num_pushes; i++)
1772     {
1773       if (att_peers_tail == att_peer_index)
1774         att_peer_index = att_peers_head;
1775       else
1776         att_peer_index = att_peer_index->next;
1777
1778       send_push (&att_peer_index->peer_id);
1779     }
1780
1781     /* Send PULLs to some peers to learn about additional peers to attack */
1782     tmp_att_peer = att_peer_index;
1783     for (i = 0; i < num_pushes * alpha; i++)
1784     {
1785       if (att_peers_tail == tmp_att_peer)
1786         tmp_att_peer = att_peers_head;
1787       else
1788         att_peer_index = tmp_att_peer->next;
1789
1790       send_pull_request (&tmp_att_peer->peer_id);
1791     }
1792   }
1793
1794   /* Schedule next round */
1795   time_next_round = compute_rand_delay (round_interval, 2);
1796
1797   //do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_mal_round,
1798   //NULL);
1799   GNUNET_assert (NULL == do_round_task);
1800   do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round,
1801                                                 &do_mal_round, NULL);
1802   LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
1803 }
1804 #endif /* ENABLE_MALICIOUS */
1805
1806
1807 /**
1808  * Send out PUSHes and PULLs, possibly update #view, samplers.
1809  *
1810  * This is executed regylary.
1811  */
1812 static void
1813 do_round (void *cls)
1814 {
1815   uint32_t i;
1816   const struct GNUNET_PeerIdentity *view_array;
1817   unsigned int *permut;
1818   unsigned int a_peers; /* Number of peers we send pushes to */
1819   unsigned int b_peers; /* Number of peers we send pull requests to */
1820   uint32_t first_border;
1821   uint32_t second_border;
1822   struct GNUNET_PeerIdentity peer;
1823   struct GNUNET_PeerIdentity *update_peer;
1824
1825   LOG (GNUNET_ERROR_TYPE_DEBUG,
1826        "Going to execute next round.\n");
1827   do_round_task = NULL;
1828   LOG (GNUNET_ERROR_TYPE_DEBUG,
1829        "Printing view:\n");
1830   to_file (file_name_view_log,
1831            "___ new round ___");
1832   view_array = View_get_as_array ();
1833   for (i = 0; i < View_size (); i++)
1834   {
1835     LOG (GNUNET_ERROR_TYPE_DEBUG,
1836          "\t%s\n", GNUNET_i2s (&view_array[i]));
1837     to_file (file_name_view_log,
1838              "=%s\t(do round)",
1839              GNUNET_i2s_full (&view_array[i]));
1840   }
1841
1842
1843   /* Send pushes and pull requests */
1844   if (0 < View_size ())
1845   {
1846     permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
1847                                            View_size ());
1848
1849     /* Send PUSHes */
1850     a_peers = ceil (alpha * View_size ());
1851
1852     LOG (GNUNET_ERROR_TYPE_DEBUG,
1853          "Going to send pushes to %u (ceil (%f * %u)) peers.\n",
1854          a_peers, alpha, View_size ());
1855     for (i = 0; i < a_peers; i++)
1856     {
1857       peer = view_array[permut[i]];
1858       if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer)) // TODO
1859       { // FIXME if this fails schedule/loop this for later
1860         send_push (&peer);
1861       }
1862     }
1863
1864     /* Send PULL requests */
1865     b_peers = ceil (beta * View_size ());
1866     first_border = a_peers;
1867     second_border = a_peers + b_peers;
1868     if (second_border > View_size ())
1869     {
1870       first_border = View_size () - b_peers;
1871       second_border = View_size ();
1872     }
1873     LOG (GNUNET_ERROR_TYPE_DEBUG,
1874         "Going to send pulls to %u (ceil (%f * %u)) peers.\n",
1875         b_peers, beta, View_size ());
1876     for (i = first_border; i < second_border; i++)
1877     {
1878       peer = view_array[permut[i]];
1879       if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer) &&
1880           GNUNET_NO == Peers_check_peer_flag (&peer, Peers_PULL_REPLY_PENDING)) // TODO
1881       { // FIXME if this fails schedule/loop this for later
1882         send_pull_request (&peer);
1883       }
1884     }
1885
1886     GNUNET_free (permut);
1887     permut = NULL;
1888   }
1889
1890
1891   /* Update view */
1892   /* TODO see how many peers are in push-/pull- list! */
1893
1894   if ((CustomPeerMap_size (push_map) <= alpha * View_size ()) &&
1895       (0 < CustomPeerMap_size (push_map)) &&
1896       (0 < CustomPeerMap_size (pull_map)))
1897   { /* If conditions for update are fulfilled, update */
1898     LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the view.\n");
1899
1900     uint32_t final_size;
1901     uint32_t peers_to_clean_size;
1902     struct GNUNET_PeerIdentity *peers_to_clean;
1903
1904     peers_to_clean = NULL;
1905     peers_to_clean_size = 0;
1906     GNUNET_array_grow (peers_to_clean, peers_to_clean_size, View_size ());
1907     GNUNET_memcpy (peers_to_clean,
1908             view_array,
1909             View_size () * sizeof (struct GNUNET_PeerIdentity));
1910
1911     /* Seems like recreating is the easiest way of emptying the peermap */
1912     View_clear ();
1913     to_file (file_name_view_log,
1914              "--- emptied ---");
1915
1916     first_border  = GNUNET_MIN (ceil (alpha * sampler_size_est_need),
1917                                 CustomPeerMap_size (push_map));
1918     second_border = first_border +
1919                     GNUNET_MIN (floor (beta  * sampler_size_est_need),
1920                                 CustomPeerMap_size (pull_map));
1921     final_size    = second_border +
1922       ceil ((1 - (alpha + beta)) * sampler_size_est_need);
1923
1924     /* Update view with peers received through PUSHes */
1925     permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
1926                                            CustomPeerMap_size (push_map));
1927     for (i = 0; i < first_border; i++)
1928     {
1929       (void) insert_in_view (CustomPeerMap_get_peer_by_index (push_map,
1930                                                               permut[i]));
1931       to_file (file_name_view_log,
1932                "+%s\t(push list)",
1933                GNUNET_i2s_full (&view_array[i]));
1934       // TODO change the peer_flags accordingly
1935     }
1936     GNUNET_free (permut);
1937     permut = NULL;
1938
1939     /* Update view with peers received through PULLs */
1940     permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
1941                                            CustomPeerMap_size (pull_map));
1942     for (i = first_border; i < second_border; i++)
1943     {
1944       (void) insert_in_view (CustomPeerMap_get_peer_by_index (pull_map,
1945             permut[i - first_border]));
1946       to_file (file_name_view_log,
1947                "+%s\t(pull list)",
1948                GNUNET_i2s_full (&view_array[i]));
1949       // TODO change the peer_flags accordingly
1950     }
1951     GNUNET_free (permut);
1952     permut = NULL;
1953
1954     /* Update view with peers from history */
1955     RPS_sampler_get_n_rand_peers (prot_sampler,
1956                                   hist_update,
1957                                   NULL,
1958                                   final_size - second_border);
1959     // TODO change the peer_flags accordingly
1960
1961     for (i = 0; i < View_size (); i++)
1962       rem_from_list (&peers_to_clean, &peers_to_clean_size, &view_array[i]);
1963
1964     /* Clean peers that were removed from the view */
1965     for (i = 0; i < peers_to_clean_size; i++)
1966     {
1967       to_file (file_name_view_log,
1968                "-%s",
1969                GNUNET_i2s_full (&peers_to_clean[i]));
1970       clean_peer (&peers_to_clean[i]);
1971       //peer_destroy_channel_send (sender);
1972     }
1973
1974     GNUNET_array_grow (peers_to_clean, peers_to_clean_size, 0);
1975   }
1976   else
1977   {
1978     LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the view.\n");
1979   }
1980   // TODO independent of that also get some peers from CADET_get_peers()?
1981
1982   LOG (GNUNET_ERROR_TYPE_DEBUG,
1983        "Received %u pushes and %u pulls last round (alpha (%.2f) * view_size (%u) = %.2f)\n",
1984        CustomPeerMap_size (push_map),
1985        CustomPeerMap_size (pull_map),
1986        alpha,
1987        View_size (),
1988        alpha * View_size ());
1989
1990   /* Update samplers */
1991   for (i = 0; i < CustomPeerMap_size (push_map); i++)
1992   {
1993     update_peer = CustomPeerMap_get_peer_by_index (push_map, i);
1994     LOG (GNUNET_ERROR_TYPE_DEBUG,
1995          "Updating with peer %s from push list\n",
1996          GNUNET_i2s (update_peer));
1997     insert_in_sampler (NULL, update_peer);
1998     clean_peer (update_peer); /* This cleans only if it is not in the view */
1999     //peer_destroy_channel_send (sender);
2000   }
2001
2002   for (i = 0; i < CustomPeerMap_size (pull_map); i++)
2003   {
2004     LOG (GNUNET_ERROR_TYPE_DEBUG,
2005          "Updating with peer %s from pull list\n",
2006          GNUNET_i2s (CustomPeerMap_get_peer_by_index (pull_map, i)));
2007     insert_in_sampler (NULL, CustomPeerMap_get_peer_by_index (pull_map, i));
2008     /* This cleans only if it is not in the view */
2009     clean_peer (CustomPeerMap_get_peer_by_index (pull_map, i));
2010     //peer_destroy_channel_send (sender);
2011   }
2012
2013
2014   /* Empty push/pull lists */
2015   CustomPeerMap_clear (push_map);
2016   CustomPeerMap_clear (pull_map);
2017
2018   struct GNUNET_TIME_Relative time_next_round;
2019
2020   time_next_round = compute_rand_delay (round_interval, 2);
2021
2022   /* Schedule next round */
2023   do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round,
2024                                                 &do_round, NULL);
2025   LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
2026 }
2027
2028
2029 /**
2030  * This is called from GNUNET_CADET_get_peers().
2031  *
2032  * It is called on every peer(ID) that cadet somehow has contact with.
2033  * We use those to initialise the sampler.
2034  */
2035 void
2036 init_peer_cb (void *cls,
2037               const struct GNUNET_PeerIdentity *peer,
2038               int tunnel, // "Do we have a tunnel towards this peer?"
2039               unsigned int n_paths, // "Number of known paths towards this peer"
2040               unsigned int best_path) // "How long is the best path?
2041                                       // (0 = unknown, 1 = ourselves, 2 = neighbor)"
2042 {
2043   if (NULL != peer)
2044   {
2045     LOG (GNUNET_ERROR_TYPE_DEBUG,
2046          "Got peer_id %s from cadet\n",
2047          GNUNET_i2s (peer));
2048     got_peer (peer);
2049   }
2050 }
2051
2052 /**
2053  * @brief Iterator function over stored, valid peers.
2054  *
2055  * We initialise the sampler with those.
2056  *
2057  * @param cls the closure
2058  * @param peer the peer id
2059  * @return #GNUNET_YES if we should continue to
2060  *         iterate,
2061  *         #GNUNET_NO if not.
2062  */
2063 static int
2064 valid_peers_iterator (void *cls,
2065                       const struct GNUNET_PeerIdentity *peer)
2066 {
2067   if (NULL != peer)
2068   {
2069     LOG (GNUNET_ERROR_TYPE_DEBUG,
2070          "Got stored, valid peer %s\n",
2071          GNUNET_i2s (peer));
2072     got_peer (peer);
2073   }
2074   return GNUNET_YES;
2075 }
2076
2077
2078 /**
2079  * Iterator over peers from peerinfo.
2080  *
2081  * @param cls closure
2082  * @param peer id of the peer, NULL for last call
2083  * @param hello hello message for the peer (can be NULL)
2084  * @param error message
2085  */
2086 void
2087 process_peerinfo_peers (void *cls,
2088                         const struct GNUNET_PeerIdentity *peer,
2089                         const struct GNUNET_HELLO_Message *hello,
2090                         const char *err_msg)
2091 {
2092   if (NULL != peer)
2093   {
2094     LOG (GNUNET_ERROR_TYPE_DEBUG,
2095          "Got peer_id %s from peerinfo\n",
2096          GNUNET_i2s (peer));
2097     got_peer (peer);
2098   }
2099 }
2100
2101
2102 /**
2103  * Task run during shutdown.
2104  *
2105  * @param cls unused
2106  */
2107 static void
2108 shutdown_task (void *cls)
2109 {
2110   struct ClientContext *client_ctx;
2111   struct ReplyCls *reply_cls;
2112
2113   LOG (GNUNET_ERROR_TYPE_DEBUG,
2114        "RPS is going down\n");
2115
2116   /* Clean all clients */
2117   for (client_ctx = cli_ctx_head;
2118        NULL != cli_ctx_head;
2119        client_ctx = cli_ctx_head)
2120   {
2121     /* Clean pending requests to the sampler */
2122     for (reply_cls = client_ctx->rep_cls_head;
2123          NULL != client_ctx->rep_cls_head;
2124          reply_cls = client_ctx->rep_cls_head)
2125     {
2126       RPS_sampler_request_cancel (reply_cls->req_handle);
2127       GNUNET_CONTAINER_DLL_remove (client_ctx->rep_cls_head,
2128                                    client_ctx->rep_cls_tail,
2129                                    reply_cls);
2130       GNUNET_free (reply_cls);
2131     }
2132     GNUNET_MQ_destroy (client_ctx->mq);
2133     GNUNET_CONTAINER_DLL_remove (cli_ctx_head, cli_ctx_tail, client_ctx);
2134     GNUNET_free (client_ctx);
2135   }
2136   GNUNET_PEERINFO_notify_cancel (peerinfo_notify_handle);
2137   GNUNET_PEERINFO_disconnect (peerinfo_handle);
2138
2139   if (NULL != do_round_task)
2140   {
2141     GNUNET_SCHEDULER_cancel (do_round_task);
2142     do_round_task = NULL;
2143   }
2144
2145   Peers_terminate ();
2146
2147   GNUNET_NSE_disconnect (nse);
2148   RPS_sampler_destroy (prot_sampler);
2149   RPS_sampler_destroy (client_sampler);
2150   GNUNET_CADET_close_port (cadet_port);
2151   GNUNET_CADET_disconnect (cadet_handle);
2152   View_destroy ();
2153   CustomPeerMap_destroy (push_map);
2154   CustomPeerMap_destroy (pull_map);
2155   #ifdef ENABLE_MALICIOUS
2156   struct AttackedPeer *tmp_att_peer;
2157   GNUNET_free (file_name_view_log);
2158   GNUNET_array_grow (mal_peers, num_mal_peers, 0);
2159   if (NULL != mal_peer_set)
2160     GNUNET_CONTAINER_multipeermap_destroy (mal_peer_set);
2161   if (NULL != att_peer_set)
2162     GNUNET_CONTAINER_multipeermap_destroy (att_peer_set);
2163   while (NULL != att_peers_head)
2164   {
2165     tmp_att_peer = att_peers_head;
2166     GNUNET_CONTAINER_DLL_remove (att_peers_head, att_peers_tail, tmp_att_peer);
2167   }
2168   #endif /* ENABLE_MALICIOUS */
2169 }
2170
2171
2172 /**
2173  * Handle client connecting to the service.
2174  *
2175  * @param cls NULL
2176  * @param client the new client
2177  * @param mq the message queue of @a client
2178  * @return @a client
2179  */
2180 static void *
2181 client_connect_cb (void *cls,
2182                    struct GNUNET_SERVICE_Client *client,
2183                    struct GNUNET_MQ_Handle *mq)
2184 {
2185   struct ClientContext *cli_ctx;
2186
2187   LOG (GNUNET_ERROR_TYPE_DEBUG,
2188        "Client connected\n");
2189   if (NULL == client)
2190     return client; /* Server was destroyed before a client connected. Shutting down */
2191   cli_ctx = GNUNET_new (struct ClientContext);
2192   cli_ctx->mq = GNUNET_SERVICE_client_get_mq (client);
2193   cli_ctx->client = client;
2194   GNUNET_CONTAINER_DLL_insert (cli_ctx_head,
2195                                cli_ctx_tail,
2196                                cli_ctx);
2197   return cli_ctx;
2198 }
2199
2200 /**
2201  * Callback called when a client disconnected from the service
2202  *
2203  * @param cls closure for the service
2204  * @param c the client that disconnected
2205  * @param internal_cls should be equal to @a c
2206  */
2207 static void
2208 client_disconnect_cb (void *cls,
2209                       struct GNUNET_SERVICE_Client *client,
2210                       void *internal_cls)
2211 {
2212   struct ClientContext *cli_ctx = internal_cls;
2213
2214   GNUNET_assert (client == cli_ctx->client);
2215   if (NULL == client)
2216   {/* shutdown task - destroy all clients */
2217     while (NULL != cli_ctx_head)
2218       destroy_cli_ctx (cli_ctx_head);
2219   }
2220   else
2221   { /* destroy this client */
2222     LOG (GNUNET_ERROR_TYPE_DEBUG,
2223         "Client disconnected. Destroy its context.\n");
2224     destroy_cli_ctx (cli_ctx);
2225   }
2226 }
2227
2228
2229 /**
2230  * Handle random peer sampling clients.
2231  *
2232  * @param cls closure
2233  * @param c configuration to use
2234  * @param service the initialized service
2235  */
2236 static void
2237 run (void *cls,
2238      const struct GNUNET_CONFIGURATION_Handle *c,
2239      struct GNUNET_SERVICE_Handle *service)
2240 {
2241   struct GNUNET_MQ_MessageHandler cadet_handlers[] = {
2242     GNUNET_MQ_hd_fixed_size (peer_check,
2243                              GNUNET_MESSAGE_TYPE_RPS_PP_CHECK_LIVE,
2244                              struct GNUNET_MessageHeader,
2245                              NULL),
2246     GNUNET_MQ_hd_fixed_size (peer_push,
2247                              GNUNET_MESSAGE_TYPE_RPS_PP_PUSH,
2248                              struct GNUNET_MessageHeader,
2249                              NULL),
2250     GNUNET_MQ_hd_fixed_size (peer_pull_request,
2251                              GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
2252                              struct GNUNET_MessageHeader,
2253                              NULL),
2254     GNUNET_MQ_hd_var_size (peer_pull_reply,
2255                            GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY,
2256                            struct GNUNET_RPS_P2P_PullReplyMessage,
2257                            NULL),
2258     GNUNET_MQ_handler_end ()
2259   };
2260
2261   int size;
2262   int out_size;
2263   char* fn_valid_peers;
2264   struct GNUNET_HashCode port;
2265
2266   GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
2267   cfg = c;
2268
2269
2270   /* Get own ID */
2271   GNUNET_CRYPTO_get_peer_identity (cfg, &own_identity); // TODO check return value
2272   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2273               "STARTING SERVICE (rps) for peer [%s]\n",
2274               GNUNET_i2s (&own_identity));
2275   #ifdef ENABLE_MALICIOUS
2276   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2277               "Malicious execution compiled in.\n");
2278   #endif /* ENABLE_MALICIOUS */
2279
2280
2281
2282   /* Get time interval from the configuration */
2283   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
2284                                                         "ROUNDINTERVAL",
2285                                                         &round_interval))
2286   {
2287     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2288                                "RPS", "ROUNDINTERVAL");
2289     GNUNET_SCHEDULER_shutdown ();
2290     return;
2291   }
2292
2293   /* Get initial size of sampler/view from the configuration */
2294   if (GNUNET_OK !=
2295       GNUNET_CONFIGURATION_get_value_number (cfg, "RPS", "INITSIZE",
2296         (long long unsigned int *) &sampler_size_est_need))
2297   {
2298     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2299                                "RPS", "INITSIZE");
2300     GNUNET_SCHEDULER_shutdown ();
2301     return;
2302   }
2303   LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %u\n", sampler_size_est_need);
2304
2305   if (GNUNET_OK !=
2306       GNUNET_CONFIGURATION_get_value_filename (cfg,
2307                                                "rps",
2308                                                "FILENAME_VALID_PEERS",
2309                                                &fn_valid_peers))
2310   {
2311     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2312                                "rps", "FILENAME_VALID_PEERS");
2313   }
2314
2315
2316   View_create (4);
2317
2318   /* file_name_view_log */
2319   if (GNUNET_OK != GNUNET_DISK_directory_create ("/tmp/rps/"))
2320   {
2321     LOG (GNUNET_ERROR_TYPE_WARNING,
2322          "Failed to create directory /tmp/rps/\n");
2323   }
2324
2325   size = (14 + strlen (GNUNET_i2s_full (&own_identity)) + 1) * sizeof (char);
2326   file_name_view_log = GNUNET_malloc (size);
2327   out_size = GNUNET_snprintf (file_name_view_log,
2328                               size,
2329                               "/tmp/rps/view-%s",
2330                               GNUNET_i2s_full (&own_identity));
2331   if (size < out_size ||
2332       0 > out_size)
2333   {
2334     LOG (GNUNET_ERROR_TYPE_WARNING,
2335          "Failed to write string to buffer (size: %i, out_size: %i)\n",
2336          size,
2337          out_size);
2338   }
2339
2340
2341   /* connect to NSE */
2342   nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
2343
2344
2345   alpha = 0.45;
2346   beta  = 0.45;
2347
2348
2349   /* Initialise cadet */
2350   cadet_handle = GNUNET_CADET_connect (cfg);
2351   GNUNET_assert (NULL != cadet_handle);
2352   GNUNET_CRYPTO_hash (GNUNET_APPLICATION_PORT_RPS,
2353                       strlen (GNUNET_APPLICATION_PORT_RPS),
2354                       &port);
2355   cadet_port = GNUNET_CADET_open_port (cadet_handle,
2356                                        &port,
2357                                        &Peers_handle_inbound_channel, /* Connect handler */
2358                                        NULL, /* cls */
2359                                        NULL, /* WindowSize handler */
2360                                        cleanup_destroyed_channel, /* Disconnect handler */
2361                                        cadet_handlers);
2362
2363
2364   peerinfo_handle = GNUNET_PEERINFO_connect (cfg);
2365   Peers_initialise (fn_valid_peers, cadet_handle, cleanup_destroyed_channel,
2366                     cadet_handlers, &own_identity);
2367   GNUNET_free (fn_valid_peers);
2368
2369   /* Initialise sampler */
2370   struct GNUNET_TIME_Relative half_round_interval;
2371   struct GNUNET_TIME_Relative  max_round_interval;
2372
2373   half_round_interval = GNUNET_TIME_relative_divide (round_interval, 2);
2374   max_round_interval = GNUNET_TIME_relative_add (round_interval, half_round_interval);
2375
2376   prot_sampler =   RPS_sampler_init     (sampler_size_est_need, max_round_interval);
2377   client_sampler = RPS_sampler_mod_init (sampler_size_est_need, max_round_interval);
2378
2379   /* Initialise push and pull maps */
2380   push_map = CustomPeerMap_create (4);
2381   pull_map = CustomPeerMap_create (4);
2382
2383
2384   //LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
2385   //GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, NULL);
2386   // TODO send push/pull to each of those peers?
2387   // TODO read stored valid peers from last run
2388   LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting stored valid peers\n");
2389   Peers_get_valid_peers (valid_peers_iterator, NULL);
2390
2391   peerinfo_notify_handle = GNUNET_PEERINFO_notify (cfg,
2392                                                    GNUNET_NO,
2393                                                    process_peerinfo_peers,
2394                                                    NULL);
2395
2396   LOG (GNUNET_ERROR_TYPE_INFO, "Ready to receive requests from clients\n");
2397
2398   do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
2399   LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
2400
2401   GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
2402 }
2403
2404
2405 /**
2406  * Define "main" method using service macro.
2407  */
2408 GNUNET_SERVICE_MAIN
2409 ("rps",
2410  GNUNET_SERVICE_OPTION_NONE,
2411  &run,
2412  &client_connect_cb,
2413  &client_disconnect_cb,
2414  NULL,
2415  GNUNET_MQ_hd_fixed_size (client_request,
2416    GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
2417    struct GNUNET_RPS_CS_RequestMessage,
2418    NULL),
2419  GNUNET_MQ_hd_fixed_size (client_request_cancel,
2420    GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST_CANCEL,
2421    struct GNUNET_RPS_CS_RequestCancelMessage,
2422    NULL),
2423  GNUNET_MQ_hd_var_size (client_seed,
2424    GNUNET_MESSAGE_TYPE_RPS_CS_SEED,
2425    struct GNUNET_RPS_CS_SeedMessage,
2426    NULL),
2427 #ifdef ENABLE_MALICIOUS
2428  GNUNET_MQ_hd_var_size (client_act_malicious,
2429    GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS,
2430    struct GNUNET_RPS_CS_ActMaliciousMessage,
2431    NULL),
2432 #endif /* ENABLE_MALICIOUS */
2433  GNUNET_MQ_handler_end());
2434
2435 /* end of gnunet-service-rps.c */