-preparations for replacement of try_connect call
[oweals/gnunet.git] / src / rps / gnunet-service-rps.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C)
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_util_lib.h"
28 #include "gnunet_cadet_service.h"
29 #include "gnunet_peerinfo_service.h"
30 #include "gnunet_nse_service.h"
31 #include "rps.h"
32 #include "rps-test_util.h"
33
34 #include "gnunet-service-rps_sampler.h"
35
36 #include <math.h>
37 #include <inttypes.h>
38
39 #define LOG(kind, ...) GNUNET_log(kind, __VA_ARGS__)
40
41 // TODO modify @brief in every file
42
43 // TODO check for overflows
44
45 // TODO align message structs
46
47 // TODO connect to friends
48
49 // TODO store peers somewhere persistent
50
51 // TODO blacklist? (-> mal peer detection on top of brahms)
52
53 // TODO API request_cancel
54
55 // hist_size_init, hist_size_max
56
57 /**
58  * Our configuration.
59  */
60 static const struct GNUNET_CONFIGURATION_Handle *cfg;
61
62 /**
63  * Our own identity.
64  */
65 static struct GNUNET_PeerIdentity own_identity;
66
67
68   struct GNUNET_PeerIdentity *
69 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list, unsigned int size,
70                            const struct GNUNET_PeerIdentity *ignore_list, unsigned int ignore_size);
71
72
73 /***********************************************************************
74  * Housekeeping with peers
75 ***********************************************************************/
76
77 /**
78  * Closure used to pass the client and the id to the callback
79  * that replies to a client's request
80  */
81 struct ReplyCls
82 {
83   /**
84    * DLL
85    */
86   struct ReplyCls *next;
87   struct ReplyCls *prev;
88
89   /**
90    * The identifier of the request
91    */
92   uint32_t id;
93
94   /**
95    * The handle to the request
96    */
97   struct RPS_SamplerRequestHandle *req_handle;
98
99   /**
100    * The client handle to send the reply to
101    */
102   struct GNUNET_SERVER_Client *client;
103 };
104
105
106 /**
107  * Struct used to store the context of a connected client.
108  */
109 struct ClientContext
110 {
111   /**
112    * DLL
113    */
114   struct ClientContext *next;
115   struct ClientContext *prev;
116
117   /**
118    * The message queue to communicate with the client.
119    */
120   struct GNUNET_MQ_Handle *mq;
121
122   /**
123    * DLL with handles to single requests from the client
124    */
125   struct ReplyCls *rep_cls_head;
126   struct ReplyCls *rep_cls_tail;
127 };
128
129 /**
130  * DLL with all clients currently connected to us
131  */
132 struct ClientContext *cli_ctx_head;
133 struct ClientContext *cli_ctx_tail;
134
135 /**
136  * Used to keep track in what lists single peerIDs are.
137  */
138 enum PeerFlags
139 {
140   /**
141    * If we are waiting for a reply from that peer (sent a pull request).
142    */
143   PULL_REPLY_PENDING   = 0x01,
144
145   IN_OTHER_GOSSIP_LIST = 0x02, // unneeded?
146   IN_OWN_SAMPLER_LIST  = 0x04, // unneeded?
147   IN_OWN_GOSSIP_LIST   = 0x08, // unneeded?
148
149   /**
150    * We set this bit when we can be sure the other peer is/was live.
151    */
152   VALID                = 0x10,
153
154   /**
155    * We set this bit when we are going to destroy the channel to this peer.
156    * When cleanup_channel is called, we know that we wanted to destroy it.
157    * Otherwise the channel to the other peer was destroyed.
158    */
159   TO_DESTROY           = 0x20,
160 };
161
162
163 /**
164  * Functions of this type can be used to be stored at a peer for later execution.
165  */
166 typedef void (* PeerOp) (void *cls, const struct GNUNET_PeerIdentity *peer);
167
168 /**
169  * Outstanding operation on peer consisting of callback and closure
170  */
171 struct PeerOutstandingOp
172 {
173   /**
174    * Callback
175    */
176   PeerOp op;
177
178   /**
179    * Closure
180    */
181   void *op_cls;
182 };
183
184
185 /**
186  * List containing all messages that are yet to be send
187  */
188 struct PendingMessage
189 {
190   /**
191    * DLL next, prev
192    */
193   struct PendingMessage *next;
194   struct PendingMessage *prev;
195
196   /**
197    * The envelope to the corresponding message
198    */
199   struct GNUNET_MQ_Envelope *ev;
200
201   /**
202    * The corresponding context
203    */
204   struct PeerContext *peer_ctx;
205
206   /**
207    * The message type
208    */
209   const char *type;
210 };
211
212
213 /**
214  * Struct used to keep track of other peer's status
215  *
216  * This is stored in a multipeermap.
217  */
218 struct PeerContext
219 {
220   /**
221    * Message queue open to client
222    */
223   struct GNUNET_MQ_Handle *mq;
224
225   /**
226    * Channel open to client.
227    */
228   struct GNUNET_CADET_Channel *send_channel;
229
230   /**
231    * Channel open from client.
232    */
233   struct GNUNET_CADET_Channel *recv_channel; // unneeded?
234
235   /**
236    * Array of outstanding operations on this peer.
237    */
238   struct PeerOutstandingOp *outstanding_ops;
239
240   /**
241    * Handle to the callback given to cadet_ntfy_tmt_rdy()
242    *
243    * To be canceled on shutdown.
244    */
245   struct GNUNET_CADET_TransmitHandle *transmit_handle;
246
247   /**
248    * Number of outstanding operations.
249    */
250   unsigned int num_outstanding_ops;
251
252   /**
253    * Identity of the peer
254    */
255   struct GNUNET_PeerIdentity peer_id;
256   
257   /**
258    * Flags indicating status of peer
259    */
260   uint32_t peer_flags;
261
262   /**
263    * DLL with all messages that are yet to be sent
264    */
265   struct PendingMessage *pending_messages_head;
266   struct PendingMessage *pending_messages_tail;
267
268   /**
269    * This is pobably followed by 'statistical' data (when we first saw
270    * him, how did we get his ID, how many pushes (in a timeinterval),
271    * ...)
272    */
273 };
274
275 /***********************************************************************
276  * /Housekeeping with peers
277 ***********************************************************************/
278
279
280
281
282
283 /***********************************************************************
284  * Globals
285 ***********************************************************************/
286
287 /**
288  * Sampler used for the Brahms protocol itself.
289  */
290 static struct RPS_Sampler *prot_sampler;
291
292 /**
293  * Sampler used for the clients.
294  */
295 static struct RPS_Sampler *client_sampler;
296
297 /**
298  * Set of all peers to keep track of them.
299  */
300 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
301
302
303 /**
304  * Name to log view to
305  */
306 static char *file_name_view_log;
307
308 /**
309  * The "local view" containing peers we learned from gossip and history
310  */
311 static struct GNUNET_CONTAINER_MultiPeerMap *view;
312
313 /**
314  * An array containing the peers of the local view.
315  *
316  * This is created every time we send a pull reply if it has changed since the
317  * last pull reply we sent.
318  */
319 static struct GNUNET_PeerIdentity *view_array;
320
321
322 /**
323  * The size of sampler we need to be able to satisfy the client's need of
324  * random peers.
325  */
326 static unsigned int sampler_size_client_need;
327
328 /**
329  * The size of sampler we need to be able to satisfy the Brahms protocol's
330  * need of random peers.
331  *
332  * This is one minimum size the sampler grows to.
333  */
334 static unsigned int sampler_size_est_need;
335
336
337 /**
338  * Percentage of total peer number in the view
339  * to send random PUSHes to
340  */
341 static float alpha;
342
343 /**
344  * Percentage of total peer number in the view
345  * to send random PULLs to
346  */
347 static float beta;
348
349 /**
350  * The percentage gamma of history updates.
351  * Simply 1 - alpha - beta
352  */
353
354
355 /**
356  * Identifier for the main task that runs periodically.
357  */
358 static struct GNUNET_SCHEDULER_Task *do_round_task;
359
360 /**
361  * Time inverval the do_round task runs in.
362  */
363 static struct GNUNET_TIME_Relative round_interval;
364
365
366
367 /**
368  * List to store peers received through pushes temporary.
369  *
370  * TODO -> multipeermap
371  */
372 static struct GNUNET_PeerIdentity *push_list;
373
374 /**
375  * Size of the push_list;
376  */
377 static unsigned int push_list_size;
378 //size_t push_list_size;
379
380 /**
381  * List to store peers received through pulls temporary.
382  *
383  * TODO -> multipeermap
384  */
385 static struct GNUNET_PeerIdentity *pull_list;
386
387 /**
388  * Size of the pull_list;
389  */
390 static unsigned int pull_list_size;
391 //size_t pull_list_size;
392
393
394 /**
395  * Handler to NSE.
396  */
397 static struct GNUNET_NSE_Handle *nse;
398
399 /**
400  * Handler to CADET.
401  */
402 static struct GNUNET_CADET_Handle *cadet_handle;
403
404 /**
405  * Handler to PEERINFO.
406  */
407 static struct GNUNET_PEERINFO_Handle *peerinfo_handle;
408
409 /**
410  * Handle for cancellation of iteration over peers.
411  */
412 struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify_handle;
413
414
415 /**
416  * Request counter.
417  *
418  * Only needed in the beginning to check how many of the 64 deltas
419  * we already have
420  */
421 static unsigned int req_counter;
422
423 /**
424  * Time of the last request we received.
425  *
426  * Used to compute the expected request rate.
427  */
428 static struct GNUNET_TIME_Absolute last_request;
429
430 /**
431  * Size of #request_deltas.
432  */
433 #define REQUEST_DELTAS_SIZE 64
434 static unsigned int request_deltas_size = REQUEST_DELTAS_SIZE;
435
436 /**
437  * Last 64 deltas between requests
438  */
439 static struct GNUNET_TIME_Relative request_deltas[REQUEST_DELTAS_SIZE];
440
441 /**
442  * The prediction of the rate of requests
443  */
444 static struct GNUNET_TIME_Relative  request_rate;
445
446
447 /**
448  * Number of history update tasks.
449  */
450 uint32_t num_hist_update_tasks;
451
452
453 #ifdef ENABLE_MALICIOUS
454 /**
455  * Type of malicious peer
456  *
457  * 0 Don't act malicious at all - Default
458  * 1 Try to maximise representation
459  * 2 Try to partition the network
460  * 3 Combined attack
461  */
462 uint32_t mal_type = 0;
463
464 /**
465  * Other malicious peers
466  */
467 static struct GNUNET_PeerIdentity *mal_peers = NULL;
468
469 /**
470  * Hashmap of malicious peers used as set.
471  * Used to more efficiently check whether we know that peer.
472  */
473 static struct GNUNET_CONTAINER_MultiPeerMap *mal_peer_set = NULL;
474
475 /**
476  * Number of other malicious peers
477  */
478 static uint32_t num_mal_peers;
479
480
481 /**
482  * If type is 2 This struct is used to store the attacked peers in a DLL
483  */
484 struct AttackedPeer
485 {
486   /**
487    * DLL
488    */
489   struct AttackedPeer *next;
490   struct AttackedPeer *prev;
491
492   /**
493    * PeerID
494    */
495   struct GNUNET_PeerIdentity peer_id;
496 };
497
498 /**
499  * If type is 2 this is the DLL of attacked peers
500  */
501 static struct AttackedPeer *att_peers_head = NULL;
502 static struct AttackedPeer *att_peers_tail = NULL;
503
504 /**
505  * This index is used to point to an attacked peer to
506  * implement the round-robin-ish way to select attacked peers.
507  */
508 static struct AttackedPeer *att_peer_index = NULL;
509
510 /**
511  * Hashmap of attacked peers used as set.
512  * Used to more efficiently check whether we know that peer.
513  */
514 static struct GNUNET_CONTAINER_MultiPeerMap *att_peer_set = NULL;
515
516 /**
517  * Number of attacked peers
518  */
519 static uint32_t num_attacked_peers = 0;
520
521
522 /**
523  * If type is 1 this is the attacked peer
524  */
525 static struct GNUNET_PeerIdentity attacked_peer;
526
527 /**
528  * The limit of PUSHes we can send in one round.
529  * This is an assumption of the Brahms protocol and either implemented
530  * via proof of work
531  * or
532  * assumend to be the bandwidth limitation.
533  */
534 static uint32_t push_limit = 10000;
535 #endif /* ENABLE_MALICIOUS */
536
537
538 /***********************************************************************
539  * /Globals
540 ***********************************************************************/
541
542
543
544
545
546
547 /***********************************************************************
548  * Util functions
549 ***********************************************************************/
550
551 /**
552  * Set a peer flag of given peer context.
553  */
554 #define set_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags |= mask)
555
556 /**
557  * Get peer flag of given peer context.
558  */
559 #define get_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags & mask ? GNUNET_YES : GNUNET_NO)
560
561 /**
562  * Unset flag of given peer context.
563  */
564 #define unset_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags &= (~mask))
565
566 /**
567  * Clean the send channel of a peer
568  */
569 void
570 peer_clean (const struct GNUNET_PeerIdentity *peer);
571
572
573 /**
574  * Check if peer is already in peer array.
575  */
576   int
577 in_arr (const struct GNUNET_PeerIdentity *array,
578         unsigned int arr_size,
579         const struct GNUNET_PeerIdentity *peer)
580 {
581   GNUNET_assert (NULL != peer);
582
583   if (0 == arr_size)
584     return GNUNET_NO;
585
586   GNUNET_assert (NULL != array);
587
588   unsigned int i;
589
590   for (i = 0; i < arr_size ; i++)
591     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&array[i], peer))
592       return GNUNET_YES;
593   return GNUNET_NO;
594 }
595
596
597 /**
598  * Print peerlist to log.
599  */
600 void
601 print_peer_list (struct GNUNET_PeerIdentity *list, unsigned int len)
602 {
603   unsigned int i;
604
605   LOG (GNUNET_ERROR_TYPE_DEBUG,
606        "Printing peer list of length %u at %p:\n",
607        len,
608        list);
609   for (i = 0 ; i < len ; i++)
610   {
611     LOG (GNUNET_ERROR_TYPE_DEBUG,
612          "%u. peer: %s\n",
613          i, GNUNET_i2s (&list[i]));
614   }
615 }
616
617
618 /**
619  * Remove peer from list.
620  */
621   void
622 rem_from_list (struct GNUNET_PeerIdentity **peer_list,
623                unsigned int *list_size,
624                const struct GNUNET_PeerIdentity *peer)
625 {
626   unsigned int i;
627   struct GNUNET_PeerIdentity *tmp;
628
629   tmp = *peer_list;
630
631   LOG (GNUNET_ERROR_TYPE_DEBUG,
632        "Removing peer %s from list at %p\n",
633        GNUNET_i2s (peer),
634        tmp);
635
636   for ( i = 0 ; i < *list_size ; i++ )
637   {
638     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&tmp[i], peer))
639     {
640       if (i < *list_size -1)
641       { /* Not at the last entry -- shift peers left */
642         memcpy (&tmp[i], &tmp[i +1],
643                 ((*list_size) - i -1) * sizeof (struct GNUNET_PeerIdentity));
644       }
645       /* Remove last entry (should be now useless PeerID) */
646       GNUNET_array_grow (tmp, *list_size, (*list_size) -1);
647     }
648   }
649   *peer_list = tmp;
650 }
651
652 /**
653  * Get random peer from the given list but don't return one from the @a ignore_list.
654  */
655   struct GNUNET_PeerIdentity *
656 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list,
657                            uint32_t list_size,
658                            const struct GNUNET_PeerIdentity *ignore_list,
659                            uint32_t ignore_size)
660 {
661   uint32_t r_index;
662   uint32_t tmp_size;
663   struct GNUNET_PeerIdentity *tmp_peer_list;
664   struct GNUNET_PeerIdentity *peer;
665
666   GNUNET_assert (NULL != peer_list);
667   if (0 == list_size)
668     return NULL;
669
670   tmp_size = 0;
671   tmp_peer_list = NULL;
672   GNUNET_array_grow (tmp_peer_list, tmp_size, list_size);
673   memcpy (tmp_peer_list,
674           peer_list,
675           list_size * sizeof (struct GNUNET_PeerIdentity));
676   peer = GNUNET_new (struct GNUNET_PeerIdentity);
677
678   /**;
679    * Choose the r_index of the peer we want to return
680    * at random from the interval of the view
681    */
682   r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
683                                       tmp_size);
684   *peer = tmp_peer_list[r_index];
685
686   while (in_arr (ignore_list, ignore_size, peer))
687   {
688     rem_from_list (&tmp_peer_list, &tmp_size, peer);
689
690     print_peer_list (tmp_peer_list, tmp_size);
691
692     if (0 == tmp_size)
693     {
694       GNUNET_free (peer);
695       return NULL;
696     }
697
698     /**;
699      * Choose the r_index of the peer we want to return
700      * at random from the interval of the view
701      */
702     r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
703                                         tmp_size);
704     *peer = tmp_peer_list[r_index];
705   }
706
707
708   GNUNET_array_grow (tmp_peer_list, tmp_size, 0);
709
710   return peer;
711 }
712
713
714 /**
715  * Get the context of a peer. If not existing, create.
716  */
717   struct PeerContext *
718 get_peer_ctx (const struct GNUNET_PeerIdentity *peer)
719 {
720   struct PeerContext *ctx;
721   int ret;
722
723   ret = GNUNET_CONTAINER_multipeermap_contains (peer_map, peer);
724   GNUNET_assert (GNUNET_YES == ret);
725   ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
726   GNUNET_assert (NULL != ctx);
727   return ctx;
728 }
729
730 /**
731  * Create a new peer context and insert it into the peer map
732  */
733 struct PeerContext *
734 create_peer_ctx (const struct GNUNET_PeerIdentity *peer)
735 {
736   struct PeerContext *ctx;
737   int ret;
738
739   ctx = GNUNET_new (struct PeerContext);
740   ctx->peer_id = *peer;
741   ret = GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
742                                            GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
743   GNUNET_assert (GNUNET_OK == ret);
744   return ctx;
745 }
746
747
748 /**
749  * Set the peer flag to living and call the outstanding operations on this peer.
750  */
751 static size_t
752 peer_is_live (struct PeerContext *peer_ctx)
753 {
754   struct GNUNET_PeerIdentity *peer;
755
756   /* Cancle transmit_handle if still scheduled */
757   if (NULL != peer_ctx->transmit_handle)
758   {
759     GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->transmit_handle);
760     peer_ctx->transmit_handle = NULL;
761   }
762
763   peer = &peer_ctx->peer_id;
764   set_peer_flag (peer_ctx, VALID);
765
766   LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer %s is live\n", GNUNET_i2s (peer));
767
768   if (0 < peer_ctx->num_outstanding_ops)
769   { /* Call outstanding operations */
770     unsigned int i;
771
772     for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
773       peer_ctx->outstanding_ops[i].op (peer_ctx->outstanding_ops[i].op_cls, peer);
774     GNUNET_array_grow (peer_ctx->outstanding_ops, peer_ctx->num_outstanding_ops, 0);
775   }
776
777   return 0;
778 }
779
780
781 /**
782  * Callback that is called when a channel was effectively established.
783  * This is given to ntfy_tmt_rdy and called when the channel was
784  * successfully established.
785  */
786 static size_t
787 cadet_ntfy_tmt_rdy_cb (void *cls, size_t size, void *buf)
788 {
789   struct PeerContext *peer_ctx = (struct PeerContext *) cls;
790
791   peer_ctx->transmit_handle = NULL;
792   LOG (GNUNET_ERROR_TYPE_DEBUG,
793        "Set ->transmit_handle = NULL for peer %s\n",
794        GNUNET_i2s (&peer_ctx->peer_id));
795
796   if (NULL != buf
797       && 0 != size)
798   {
799     peer_is_live (peer_ctx);
800   }
801   else
802   {
803     LOG (GNUNET_ERROR_TYPE_WARNING,
804          "Problems establishing a connection to peer %s in order to check liveliness\n",
805          GNUNET_i2s (&peer_ctx->peer_id));
806     // TODO reschedule? cleanup?
807   }
808
809   //if (NULL != peer_ctx->transmit_handle)
810   //{
811   //  LOG (GNUNET_ERROR_TYPE_DEBUG,
812   //       "Trying to cancle transmit_handle for peer %s\n",
813   //       GNUNET_i2s (&peer_ctx->peer_id));
814   //  GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->transmit_handle);
815   //  peer_ctx->transmit_handle = NULL;
816   //}
817
818   return 0;
819 }
820
821
822 /**
823  * Get the channel of a peer. If not existing, create.
824  */
825   struct GNUNET_CADET_Channel *
826 get_channel (const struct GNUNET_PeerIdentity *peer)
827 {
828   struct PeerContext *peer_ctx;
829
830   peer_ctx = get_peer_ctx (peer);
831   if (NULL == peer_ctx->send_channel)
832   {
833     LOG (GNUNET_ERROR_TYPE_DEBUG,
834          "Trying to establish channel to peer %s\n",
835          GNUNET_i2s (peer));
836
837     peer_ctx->send_channel =
838       GNUNET_CADET_channel_create (cadet_handle,
839                                    NULL,
840                                    peer,
841                                    GNUNET_RPS_CADET_PORT,
842                                    GNUNET_CADET_OPTION_RELIABLE);
843
844   }
845   return peer_ctx->send_channel;
846 }
847
848
849 /**
850  * Get the message queue of a specific peer.
851  *
852  * If we already have a message queue open to this client,
853  * simply return it, otherways create one.
854  */
855   struct GNUNET_MQ_Handle *
856 get_mq (const struct GNUNET_PeerIdentity *peer_id)
857 {
858   struct PeerContext *peer_ctx;
859
860   peer_ctx = get_peer_ctx (peer_id);
861
862   GNUNET_assert (NULL == peer_ctx->transmit_handle);
863
864   if (NULL == peer_ctx->mq)
865   {
866     (void) get_channel (peer_id);
867     peer_ctx->mq = GNUNET_CADET_mq_create (peer_ctx->send_channel);
868     //do I have to explicitly put it in the peer_map?
869     (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer_id, peer_ctx,
870                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
871   }
872   return peer_ctx->mq;
873 }
874
875
876 /**
877  * Issue check whether peer is live
878  *
879  * @param peer_ctx the context of the peer
880  */
881 void
882 check_peer_live (struct PeerContext *peer_ctx)
883 {
884   LOG (GNUNET_ERROR_TYPE_DEBUG,
885        "Get informed about peer %s getting live\n",
886        GNUNET_i2s (&peer_ctx->peer_id));
887
888   if (NULL == peer_ctx->transmit_handle &&
889       NULL == peer_ctx->send_channel)
890   {
891     (void) get_channel (&peer_ctx->peer_id);
892     peer_ctx->transmit_handle =
893         GNUNET_CADET_notify_transmit_ready (peer_ctx->send_channel,
894                                             GNUNET_NO,
895                                             GNUNET_TIME_UNIT_FOREVER_REL,
896                                             sizeof (struct GNUNET_MessageHeader),
897                                             cadet_ntfy_tmt_rdy_cb,
898                                             peer_ctx);
899   }
900   else if (NULL != peer_ctx->transmit_handle)
901     LOG (GNUNET_ERROR_TYPE_DEBUG,
902          "Already waiting for notification\n");
903   else if (NULL != peer_ctx->send_channel)
904     LOG (GNUNET_ERROR_TYPE_DEBUG,
905          "Already have established channel to peer\n");
906 }
907
908
909 /**
910  * Sum all time relatives of an array.
911   */
912   struct GNUNET_TIME_Relative
913 T_relative_sum (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
914 {
915   struct GNUNET_TIME_Relative sum;
916   uint32_t i;
917
918   sum = GNUNET_TIME_UNIT_ZERO;
919   for ( i = 0 ; i < arr_size ; i++ )
920   {
921     sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
922   }
923   return sum;
924 }
925
926
927 /**
928  * Compute the average of given time relatives.
929  */
930   struct GNUNET_TIME_Relative
931 T_relative_avg (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
932 {
933   return GNUNET_TIME_relative_divide (T_relative_sum (rel_array, arr_size), arr_size);
934 }
935
936
937 /**
938  * Insert PeerID in #pull_list
939  *
940  * Called once we know a peer is live.
941  */
942   void
943 insert_in_pull_list (void *cls, const struct GNUNET_PeerIdentity *peer)
944 {
945   if (GNUNET_NO == in_arr (pull_list, pull_list_size, peer))
946     GNUNET_array_append (pull_list, pull_list_size, *peer);
947 }
948
949 /**
950  * Check whether #insert_in_pull_list was already scheduled
951  */
952   int
953 insert_in_pull_list_scheduled (const struct PeerContext *peer_ctx)
954 {
955   unsigned int i;
956
957   for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
958     if (insert_in_pull_list == peer_ctx->outstanding_ops[i].op)
959       return GNUNET_YES;
960   return GNUNET_NO;
961 }
962
963
964 /**
965  * Insert PeerID in #view
966  *
967  * Called once we know a peer is live.
968  */
969   void
970 insert_in_view (void *cls, const struct GNUNET_PeerIdentity *peer)
971 {
972   if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (view, peer))
973       return;
974   LOG (GNUNET_ERROR_TYPE_DEBUG,
975       "Going to put %s into view\n",
976       GNUNET_i2s (peer));
977   if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_put (view,
978         peer,
979         NULL,
980         GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
981   {
982     LOG (GNUNET_ERROR_TYPE_WARNING,
983         "Failed to put peer into view. (insert_in_view)\n");
984   }
985   if (NULL != view_array)
986   {
987     GNUNET_free (view_array);
988     view_array = NULL;
989   }
990   if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
991     create_peer_ctx (peer);
992   (void) get_channel (peer);
993 }
994
995 /**
996  * Check whether #insert_in_view was already scheduled
997  */
998   int
999 insert_in_view_scheduled (const struct PeerContext *peer_ctx)
1000 {
1001   unsigned int i;
1002
1003   for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
1004     if (insert_in_view == peer_ctx->outstanding_ops[i].op)
1005       return GNUNET_YES;
1006   return GNUNET_NO;
1007 }
1008
1009
1010 /**
1011  * Update sampler with given PeerID.
1012  */
1013   void
1014 insert_in_sampler (void *cls, const struct GNUNET_PeerIdentity *peer)
1015 {
1016   LOG (GNUNET_ERROR_TYPE_DEBUG,
1017        "Updating samplers with peer %s from insert_in_sampler()\n",
1018        GNUNET_i2s (peer));
1019   RPS_sampler_update (prot_sampler,   peer);
1020   RPS_sampler_update (client_sampler, peer);
1021   if (0 < RPS_sampler_count_id (prot_sampler, peer))
1022   {
1023     if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
1024       (void) create_peer_ctx (peer);
1025     (void) get_channel (peer);
1026   }
1027 }
1028
1029
1030 /**
1031  * Check whether #insert_in_sampler was already scheduled
1032  */
1033 static int
1034 insert_in_sampler_scheduled (const struct PeerContext *peer_ctx)
1035 {
1036   unsigned int i;
1037
1038   for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
1039     if (insert_in_sampler== peer_ctx->outstanding_ops[i].op)
1040       return GNUNET_YES;
1041   return GNUNET_NO;
1042 }
1043
1044 /**
1045  * Put random peer from sampler into the view as history update.
1046  */
1047   void
1048 hist_update (void *cls, struct GNUNET_PeerIdentity *ids, uint32_t num_peers)
1049 {
1050   unsigned int i;
1051
1052   for (i = 0; i < GNUNET_MIN (
1053        sampler_size_est_need - GNUNET_CONTAINER_multipeermap_size (view),
1054        num_peers); i++)
1055   {
1056     insert_in_view (NULL, &ids[i]);
1057     to_file (file_name_view_log,
1058              "+%s\t(hist)",
1059              GNUNET_i2s_full (ids));
1060   }
1061   if (0 < num_hist_update_tasks)
1062     num_hist_update_tasks--;
1063 }
1064
1065
1066
1067 /**
1068  * Wrapper around #RPS_sampler_resize()
1069  *
1070  * If we do not have enough sampler elements, double current sampler size
1071  * If we have more than enough sampler elements, halv current sampler size
1072  */
1073 static void
1074 resize_wrapper (struct RPS_Sampler *sampler, uint32_t new_size)
1075 {
1076   unsigned int sampler_size;
1077
1078   // TODO statistics
1079   // TODO respect the min, max
1080   sampler_size = RPS_sampler_get_size (sampler);
1081   if (sampler_size > new_size * 4)
1082   { /* Shrinking */
1083     RPS_sampler_resize (sampler, sampler_size / 2);
1084   }
1085   else if (sampler_size < new_size)
1086   { /* Growing */
1087     RPS_sampler_resize (sampler, sampler_size * 2);
1088   }
1089   LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
1090 }
1091
1092
1093 /**
1094  * Wrapper around #RPS_sampler_resize() resizing the client sampler
1095  */
1096 static void
1097 client_resize_wrapper ()
1098 {
1099   uint32_t bigger_size;
1100
1101   // TODO statistics
1102
1103   bigger_size = GNUNET_MAX (sampler_size_est_need, sampler_size_client_need);
1104
1105   // TODO respect the min, max
1106   resize_wrapper (client_sampler, bigger_size);
1107   LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size_client is now %" PRIu32 "\n",
1108       bigger_size);
1109 }
1110
1111
1112 /**
1113  * Estimate request rate
1114  *
1115  * Called every time we receive a request from the client.
1116  */
1117   void
1118 est_request_rate()
1119 {
1120   struct GNUNET_TIME_Relative max_round_duration;
1121
1122   if (request_deltas_size > req_counter)
1123     req_counter++;
1124   if ( 1 < req_counter)
1125   {
1126     /* Shift last request deltas to the right */
1127     memcpy (&request_deltas[1],
1128         request_deltas,
1129         (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
1130
1131     /* Add current delta to beginning */
1132     request_deltas[0] =
1133         GNUNET_TIME_absolute_get_difference (last_request,
1134                                              GNUNET_TIME_absolute_get ());
1135     request_rate = T_relative_avg (request_deltas, req_counter);
1136
1137     /* Compute the duration a round will maximally take */
1138     max_round_duration =
1139         GNUNET_TIME_relative_add (round_interval,
1140                                   GNUNET_TIME_relative_divide (round_interval, 2));
1141
1142     /* Set the estimated size the sampler has to have to
1143      * satisfy the current client request rate */
1144     sampler_size_client_need =
1145         max_round_duration.rel_value_us / request_rate.rel_value_us;
1146
1147     /* Resize the sampler */
1148     client_resize_wrapper ();
1149   }
1150   last_request = GNUNET_TIME_absolute_get ();
1151 }
1152
1153
1154 /**
1155  * Add all peers in @a peer_array to @a peer_map used as set.
1156  *
1157  * @param peer_array array containing the peers
1158  * @param num_peers number of peers in @peer_array
1159  * @param peer_map the peermap to use as set
1160  */
1161 static void
1162 add_peer_array_to_set (const struct GNUNET_PeerIdentity *peer_array,
1163                        unsigned int num_peers,
1164                        struct GNUNET_CONTAINER_MultiPeerMap *peer_map)
1165 {
1166   unsigned int i;
1167   if (NULL == peer_map)
1168   {
1169     LOG (GNUNET_ERROR_TYPE_WARNING,
1170          "Trying to add peers to non-existing peermap.\n");
1171     return;
1172   }
1173
1174   for (i = 0; i < num_peers; i++)
1175   {
1176     GNUNET_CONTAINER_multipeermap_put (peer_map,
1177                                        &peer_array[i],
1178                                        NULL,
1179                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1180   }
1181 }
1182
1183
1184 /**
1185  * @brief Add an envelope to a message passed to mq to list of pending messages
1186  *
1187  * @param peer peer the message was sent to
1188  * @param ev envelope to the message
1189  * @param type type of the message to be sent
1190  */
1191 static struct PendingMessage *
1192 insert_pending_message (const struct GNUNET_PeerIdentity *peer,
1193                         struct GNUNET_MQ_Envelope *ev,
1194                         const char *type)
1195 {
1196   struct PendingMessage *pending_msg;
1197   struct PeerContext *peer_ctx;
1198
1199   peer_ctx = get_peer_ctx (peer);
1200   pending_msg = GNUNET_new (struct PendingMessage);
1201   pending_msg->ev = ev;
1202   pending_msg->peer_ctx = peer_ctx;
1203   pending_msg->type = type;
1204   GNUNET_CONTAINER_DLL_insert (peer_ctx->pending_messages_head,
1205                                peer_ctx->pending_messages_tail,
1206                                pending_msg);
1207   return pending_msg;
1208 }
1209
1210 static void
1211 remove_pending_message (struct PendingMessage *pending_msg)
1212 {
1213   struct PeerContext *peer_ctx;
1214
1215   peer_ctx = pending_msg->peer_ctx;
1216   GNUNET_CONTAINER_DLL_remove (peer_ctx->pending_messages_head,
1217                                peer_ctx->pending_messages_tail,
1218                                pending_msg);
1219   GNUNET_free (pending_msg);
1220 }
1221
1222
1223 /**
1224  * @brief This is called once a message is sent.
1225  *
1226  * @param cls type of the message that was sent
1227  */
1228 static void
1229 mq_notify_sent_cb (void *cls)
1230 {
1231   struct PendingMessage *pending_msg = (struct PendingMessage *) cls;
1232   LOG (GNUNET_ERROR_TYPE_DEBUG,
1233       "%s was sent.\n",
1234       pending_msg->type);
1235   remove_pending_message (pending_msg);
1236 }
1237
1238
1239 /**
1240  * Send a PULL REPLY to @a peer_id
1241  *
1242  * @param peer_id the peer to send the reply to.
1243  * @param peer_ids the peers to send to @a peer_id
1244  * @param num_peer_ids the number of peers to send to @a peer_id
1245  */
1246 static void
1247 send_pull_reply (const struct GNUNET_PeerIdentity *peer_id,
1248                  const struct GNUNET_PeerIdentity *peer_ids,
1249                  unsigned int num_peer_ids)
1250 {
1251   uint32_t send_size;
1252   struct GNUNET_MQ_Handle *mq;
1253   struct GNUNET_MQ_Envelope *ev;
1254   struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
1255   struct PendingMessage *pending_msg;
1256
1257   /* Compute actual size */
1258   send_size = sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) +
1259               num_peer_ids * sizeof (struct GNUNET_PeerIdentity);
1260
1261   if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size)
1262     /* Compute number of peers to send
1263      * If too long, simply truncate */
1264     // TODO select random ones via permutation
1265     //      or even better: do good protocol design
1266     send_size =
1267       (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE -
1268        sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1269        sizeof (struct GNUNET_PeerIdentity);
1270   else
1271     send_size = num_peer_ids;
1272
1273   LOG (GNUNET_ERROR_TYPE_DEBUG,
1274       "Going to send PULL REPLY with %u peers to %s\n",
1275       send_size, GNUNET_i2s (peer_id));
1276
1277   mq = get_mq (peer_id);
1278
1279   ev = GNUNET_MQ_msg_extra (out_msg,
1280                             send_size * sizeof (struct GNUNET_PeerIdentity),
1281                             GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
1282   out_msg->num_peers = htonl (send_size);
1283   memcpy (&out_msg[1], peer_ids,
1284          send_size * sizeof (struct GNUNET_PeerIdentity));
1285
1286   pending_msg = insert_pending_message (peer_id, ev, "PULL REPLY");
1287   GNUNET_MQ_notify_sent (ev,
1288       mq_notify_sent_cb,
1289       pending_msg);
1290   GNUNET_MQ_send (mq, ev);
1291 }
1292
1293
1294 /**
1295  * This function is called on new peer_ids from 'external' sources
1296  * (client seed, cadet get_peers(), ...)
1297  *
1298  * @param peer_id the new peer_id
1299  */
1300 static void
1301 new_peer_id (const struct GNUNET_PeerIdentity *peer_id)
1302 {
1303   struct PeerOutstandingOp out_op;
1304   struct PeerContext *peer_ctx;
1305
1306   if ((NULL == peer_id) ||
1307       (0 == GNUNET_CRYPTO_cmp_peer_identity (&own_identity, peer_id)))
1308     return;
1309   LOG (GNUNET_ERROR_TYPE_DEBUG,
1310        "Got peer_id %s (at %p, view size: %u)\n",
1311        GNUNET_i2s (peer_id),
1312        peer_id,
1313        GNUNET_CONTAINER_multipeermap_size (view));
1314
1315   /* if the seed peer is already know, skip context creation */
1316   if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer_id))
1317     peer_ctx = create_peer_ctx (peer_id);
1318   else
1319     peer_ctx = get_peer_ctx (peer_id);
1320
1321   if (GNUNET_NO == get_peer_flag (peer_ctx, VALID))
1322   {
1323     if (GNUNET_NO == insert_in_sampler_scheduled (peer_ctx))
1324     {
1325       out_op.op = insert_in_sampler;
1326       out_op.op_cls = NULL;
1327       GNUNET_array_append (peer_ctx->outstanding_ops,
1328                            peer_ctx->num_outstanding_ops,
1329                            out_op);
1330     }
1331
1332     if (GNUNET_NO == insert_in_view_scheduled (peer_ctx))
1333     {
1334       out_op.op = insert_in_view;
1335       out_op.op_cls = NULL;
1336       GNUNET_array_append (peer_ctx->outstanding_ops,
1337                            peer_ctx->num_outstanding_ops,
1338                            out_op);
1339     }
1340
1341     /* Trigger livelyness test on peer */
1342     check_peer_live (peer_ctx);
1343   }
1344   // else...?
1345
1346   // send push/pull to each of those peers?
1347 }
1348
1349
1350 /***********************************************************************
1351  * /Util functions
1352 ***********************************************************************/
1353
1354 static void
1355 destroy_reply_cls (struct ReplyCls *rep_cls)
1356 {
1357   struct ClientContext *cli_ctx;
1358
1359   cli_ctx = GNUNET_SERVER_client_get_user_context (rep_cls->client,
1360                                                    struct ClientContext);
1361   GNUNET_assert (NULL != cli_ctx);
1362   GNUNET_CONTAINER_DLL_remove (cli_ctx->rep_cls_head,
1363                                cli_ctx->rep_cls_tail,
1364                                rep_cls);
1365   GNUNET_free (rep_cls);
1366 }
1367
1368 static void
1369 destroy_cli_ctx (struct ClientContext *cli_ctx)
1370 {
1371   GNUNET_assert (NULL != cli_ctx);
1372   if (NULL != cli_ctx->rep_cls_head)
1373   {
1374     LOG (GNUNET_ERROR_TYPE_WARNING,
1375          "Trying to destroy the context of a client that still has pending requests. Going to clean those\n");
1376     while (NULL != cli_ctx->rep_cls_head)
1377       destroy_reply_cls (cli_ctx->rep_cls_head);
1378   }
1379   GNUNET_CONTAINER_DLL_remove (cli_ctx_head,
1380                                cli_ctx_tail,
1381                                cli_ctx);
1382   GNUNET_free (cli_ctx);
1383 }
1384
1385
1386 /**
1387  * Function called by NSE.
1388  *
1389  * Updates sizes of sampler list and view and adapt those lists
1390  * accordingly.
1391  */
1392   void
1393 nse_callback (void *cls, struct GNUNET_TIME_Absolute timestamp,
1394               double logestimate, double std_dev)
1395 {
1396   double estimate;
1397   //double scale; // TODO this might go gloabal/config
1398
1399   LOG (GNUNET_ERROR_TYPE_DEBUG,
1400        "Received a ns estimate - logest: %f, std_dev: %f (old_size: %u)\n",
1401        logestimate, std_dev, RPS_sampler_get_size (prot_sampler));
1402   //scale = .01;
1403   estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
1404   // GNUNET_NSE_log_estimate_to_n (logestimate);
1405   estimate = pow (estimate, 1.0 / 3);
1406   // TODO add if std_dev is a number
1407   // estimate += (std_dev * scale);
1408   if (2 < ceil (estimate))
1409   {
1410     LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
1411     sampler_size_est_need = estimate;
1412   } else
1413     LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
1414
1415   /* If the NSE has changed adapt the lists accordingly */
1416   resize_wrapper (prot_sampler, sampler_size_est_need);
1417   client_resize_wrapper ();
1418 }
1419
1420
1421 /**
1422  * Callback called once the requested PeerIDs are ready.
1423  *
1424  * Sends those to the requesting client.
1425  */
1426 void client_respond (void *cls,
1427     struct GNUNET_PeerIdentity *peer_ids, uint32_t num_peers)
1428 {
1429   uint32_t i;
1430   struct GNUNET_MQ_Envelope *ev;
1431   struct GNUNET_RPS_CS_ReplyMessage *out_msg;
1432   struct ReplyCls *reply_cls = (struct ReplyCls *) cls;
1433   uint32_t size_needed;
1434   struct ClientContext *cli_ctx;
1435
1436   GNUNET_assert (NULL != reply_cls);
1437   LOG (GNUNET_ERROR_TYPE_DEBUG,
1438        "sampler returned %" PRIu32 " peers:\n",
1439        num_peers);
1440   for (i = 0; i < num_peers; i++)
1441   {
1442     LOG (GNUNET_ERROR_TYPE_DEBUG,
1443          "  %lu: %s\n",
1444          i,
1445          GNUNET_i2s (&peer_ids[i]));
1446   }
1447
1448   size_needed = sizeof (struct GNUNET_RPS_CS_ReplyMessage) +
1449                 num_peers * sizeof (struct GNUNET_PeerIdentity);
1450
1451   GNUNET_assert (GNUNET_SERVER_MAX_MESSAGE_SIZE >= size_needed);
1452
1453   ev = GNUNET_MQ_msg_extra (out_msg,
1454                             num_peers * sizeof (struct GNUNET_PeerIdentity),
1455                             GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
1456   out_msg->num_peers = htonl (num_peers);
1457   out_msg->id = htonl (reply_cls->id);
1458
1459   memcpy (&out_msg[1],
1460           peer_ids,
1461           num_peers * sizeof (struct GNUNET_PeerIdentity));
1462   GNUNET_free (peer_ids);
1463
1464   cli_ctx = GNUNET_SERVER_client_get_user_context (reply_cls->client,
1465                                                    struct ClientContext);
1466   GNUNET_assert (NULL != cli_ctx);
1467   destroy_reply_cls (reply_cls);
1468   GNUNET_MQ_send (cli_ctx->mq, ev);
1469 }
1470
1471
1472 /**
1473  * Handle RPS request from the client.
1474  *
1475  * @param cls closure
1476  * @param client identification of the client
1477  * @param message the actual message
1478  */
1479 static void
1480 handle_client_request (void *cls,
1481                        struct GNUNET_SERVER_Client *client,
1482                        const struct GNUNET_MessageHeader *message)
1483 {
1484   struct GNUNET_RPS_CS_RequestMessage *msg;
1485   uint32_t num_peers;
1486   uint32_t size_needed;
1487   struct ReplyCls *reply_cls;
1488   uint32_t i;
1489   struct ClientContext *cli_ctx;
1490
1491   msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
1492
1493   num_peers = ntohl (msg->num_peers);
1494   size_needed = sizeof (struct GNUNET_RPS_CS_RequestMessage) +
1495                 num_peers * sizeof (struct GNUNET_PeerIdentity);
1496
1497   if (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
1498   {
1499     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1500                 "Message received from client has size larger than expected\n");
1501     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1502     return;
1503   }
1504
1505   for (i = 0 ; i < num_peers ; i++)
1506     est_request_rate();
1507
1508   LOG (GNUNET_ERROR_TYPE_DEBUG,
1509        "Client requested %" PRIu32 " random peer(s).\n",
1510        num_peers);
1511
1512   reply_cls = GNUNET_new (struct ReplyCls);
1513   reply_cls->id = ntohl (msg->id);
1514   reply_cls->client = client;
1515   reply_cls->req_handle = RPS_sampler_get_n_rand_peers (client_sampler,
1516                                                         client_respond,
1517                                                         reply_cls,
1518                                                         num_peers);
1519
1520   cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientContext);
1521   GNUNET_assert (NULL != cli_ctx);
1522   GNUNET_CONTAINER_DLL_insert (cli_ctx->rep_cls_head,
1523                                cli_ctx->rep_cls_tail,
1524                                reply_cls);
1525   GNUNET_SERVER_receive_done (client,
1526                               GNUNET_OK);
1527 }
1528
1529
1530 /**
1531  * @brief Handle a message that requests the cancellation of a request
1532  *
1533  * @param cls unused
1534  * @param client the client that requests the cancellation
1535  * @param message the message containing the id of the request
1536  */
1537 static void
1538 handle_client_request_cancel (void *cls,
1539                               struct GNUNET_SERVER_Client *client,
1540                               const struct GNUNET_MessageHeader *message)
1541 {
1542   struct GNUNET_RPS_CS_RequestCancelMessage *msg =
1543     (struct GNUNET_RPS_CS_RequestCancelMessage *) message;
1544   struct ClientContext *cli_ctx;
1545   struct ReplyCls *rep_cls;
1546
1547   cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientContext);
1548   GNUNET_assert (NULL != cli_ctx->rep_cls_head);
1549   rep_cls = cli_ctx->rep_cls_head;
1550   LOG (GNUNET_ERROR_TYPE_DEBUG,
1551       "Client cancels request with id %lu\n",
1552       ntohl (msg->id));
1553   while ( (NULL != rep_cls->next) &&
1554           (rep_cls->id != ntohl (msg->id)) )
1555     rep_cls = rep_cls->next;
1556   GNUNET_assert (rep_cls->id == ntohl (msg->id));
1557   RPS_sampler_request_cancel (rep_cls->req_handle);
1558   destroy_reply_cls (rep_cls);
1559   GNUNET_SERVER_receive_done (client,
1560                               GNUNET_OK);
1561 }
1562
1563
1564 /**
1565  * Handle seed from the client.
1566  *
1567  * @param cls closure
1568  * @param client identification of the client
1569  * @param message the actual message
1570  */
1571   static void
1572 handle_client_seed (void *cls,
1573                     struct GNUNET_SERVER_Client *client,
1574                     const struct GNUNET_MessageHeader *message)
1575 {
1576   struct GNUNET_RPS_CS_SeedMessage *in_msg;
1577   struct GNUNET_PeerIdentity *peers;
1578   uint32_t num_peers;
1579   uint32_t i;
1580
1581   if (sizeof (struct GNUNET_RPS_CS_SeedMessage) > ntohs (message->size))
1582   {
1583     GNUNET_break_op (0);
1584     GNUNET_SERVER_receive_done (client,
1585                                 GNUNET_SYSERR);
1586   }
1587
1588   in_msg = (struct GNUNET_RPS_CS_SeedMessage *) message;
1589   num_peers = ntohl (in_msg->num_peers);
1590   peers = (struct GNUNET_PeerIdentity *) &in_msg[1];
1591   //peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1592   //memcpy (peers, &in_msg[1], num_peers * sizeof (struct GNUNET_PeerIdentity));
1593
1594   if ((ntohs (message->size) - sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
1595       sizeof (struct GNUNET_PeerIdentity) != num_peers)
1596   {
1597     GNUNET_break_op (0);
1598     GNUNET_SERVER_receive_done (client,
1599                                 GNUNET_SYSERR);
1600     return;
1601   }
1602
1603   LOG (GNUNET_ERROR_TYPE_DEBUG,
1604        "Client seeded peers:\n");
1605   print_peer_list (peers, num_peers);
1606
1607   for (i = 0 ; i < num_peers ; i++)
1608   {
1609     LOG (GNUNET_ERROR_TYPE_DEBUG,
1610          "Updating samplers with seed %" PRIu32 ": %s\n",
1611          i,
1612          GNUNET_i2s (&peers[i]));
1613
1614     new_peer_id (&peers[i]);
1615
1616     //RPS_sampler_update (prot_sampler,   &peers[i]);
1617     //RPS_sampler_update (client_sampler, &peers[i]);
1618   }
1619
1620   ////GNUNET_free (peers);
1621
1622   GNUNET_SERVER_receive_done (client,
1623                                                 GNUNET_OK);
1624 }
1625
1626
1627 /**
1628  * Handle a PUSH message from another peer.
1629  *
1630  * Check the proof of work and store the PeerID
1631  * in the temporary list for pushed PeerIDs.
1632  *
1633  * @param cls Closure
1634  * @param channel The channel the PUSH was received over
1635  * @param channel_ctx The context associated with this channel
1636  * @param msg The message header
1637  */
1638 static int
1639 handle_peer_push (void *cls,
1640     struct GNUNET_CADET_Channel *channel,
1641     void **channel_ctx,
1642     const struct GNUNET_MessageHeader *msg)
1643 {
1644   const struct GNUNET_PeerIdentity *peer;
1645
1646   // (check the proof of work)
1647
1648   peer = (const struct GNUNET_PeerIdentity *)
1649     GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
1650   // FIXME wait for cadet to change this function
1651
1652   LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PUSH (%s)\n", GNUNET_i2s (peer));
1653
1654   #ifdef ENABLE_MALICIOUS
1655   struct AttackedPeer *tmp_att_peer;
1656
1657   tmp_att_peer = GNUNET_new (struct AttackedPeer);
1658   memcpy (&tmp_att_peer->peer_id, peer, sizeof (struct GNUNET_PeerIdentity));
1659   if (1 == mal_type
1660       || 3 == mal_type)
1661   { /* Try to maximise representation */
1662     if (NULL == att_peer_set)
1663       att_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
1664     if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1665                                                              peer))
1666     {
1667       GNUNET_CONTAINER_DLL_insert (att_peers_head,
1668                                    att_peers_tail,
1669                                    tmp_att_peer);
1670       add_peer_array_to_set (peer, 1, att_peer_set);
1671     }
1672     return GNUNET_OK;
1673   }
1674
1675
1676   else if (2 == mal_type)
1677   { /* We attack one single well-known peer - simply ignore */
1678     return GNUNET_OK;
1679   }
1680   else
1681   {
1682     GNUNET_free (tmp_att_peer);
1683   }
1684
1685   #endif /* ENABLE_MALICIOUS */
1686
1687   /* Add the sending peer to the push_list */
1688   if (GNUNET_NO == in_arr (push_list, push_list_size, peer))
1689   {
1690     GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer));
1691     GNUNET_array_append (push_list, push_list_size, *peer);
1692   }
1693
1694   GNUNET_CADET_receive_done (channel);
1695   return GNUNET_OK;
1696 }
1697
1698
1699 /**
1700  * Iterator over hash map entries.
1701  * Called from #generate_view_array and writes every peer id into #view_array.
1702  *
1703  * @param cls closure - the pointer to the counter
1704  * @param key current public key
1705  * @param value value in the hash map
1706  * @return #GNUNET_YES if we should continue to
1707  * iterate,
1708  * #GNUNET_NO if not.
1709  */
1710 static int
1711 dump_id_to_view_array (void *cls,
1712                        const struct GNUNET_PeerIdentity *key,
1713                        void *value)
1714 {
1715   unsigned int *i = (unsigned int *) cls;
1716
1717   view_array[(*i)++] = *key;
1718   return GNUNET_YES;
1719 }
1720
1721
1722 /**
1723  * Makes sure the view_array is filled with the peer ids currently in #view.
1724  * Called from within #do_round before sending pushes and pulls and from
1725  * #handle_peer_pull_request when a reply is sent.
1726  */
1727 static void
1728 generate_view_array (unsigned int view_size)
1729 {
1730   unsigned int *i;
1731   int ret;
1732
1733   if (NULL == view_array)
1734   {
1735     view_array = GNUNET_new_array (view_size,
1736                                    struct GNUNET_PeerIdentity);
1737     i = GNUNET_new (unsigned int);
1738     *i = 0;
1739
1740     ret = GNUNET_CONTAINER_multipeermap_iterate (view,
1741                                                  dump_id_to_view_array,
1742                                                  i);
1743     GNUNET_assert (view_size == ret);
1744     GNUNET_assert (view_size == *i);
1745
1746     GNUNET_free (i);
1747   }
1748 }
1749
1750
1751 /**
1752  * Handle PULL REQUEST request message from another peer.
1753  *
1754  * Reply with the view of PeerIDs.
1755  *
1756  * @param cls Closure
1757  * @param channel The channel the PULL REQUEST was received over
1758  * @param channel_ctx The context associated with this channel
1759  * @param msg The message header
1760  */
1761 static int
1762 handle_peer_pull_request (void *cls,
1763     struct GNUNET_CADET_Channel *channel,
1764     void **channel_ctx,
1765     const struct GNUNET_MessageHeader *msg)
1766 {
1767   struct GNUNET_PeerIdentity *peer;
1768   unsigned int view_size;
1769
1770   peer = (struct GNUNET_PeerIdentity *)
1771     GNUNET_CADET_channel_get_info (channel,
1772                                    GNUNET_CADET_OPTION_PEER);
1773   // FIXME wait for cadet to change this function
1774
1775   LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REQUEST (%s)\n", GNUNET_i2s (peer));
1776
1777   #ifdef ENABLE_MALICIOUS
1778   if (1 == mal_type
1779       || 3 == mal_type)
1780   { /* Try to maximise representation */
1781     send_pull_reply (peer, mal_peers, num_mal_peers);
1782     return GNUNET_OK;
1783   }
1784
1785   else if (2 == mal_type)
1786   { /* Try to partition network */
1787     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
1788     {
1789       send_pull_reply (peer, mal_peers, num_mal_peers);
1790     }
1791     return GNUNET_OK;
1792   }
1793   #endif /* ENABLE_MALICIOUS */
1794
1795   view_size = GNUNET_CONTAINER_multipeermap_size (view);
1796   generate_view_array (view_size);
1797
1798   send_pull_reply (peer, view_array, view_size);
1799
1800   GNUNET_CADET_receive_done (channel);
1801   return GNUNET_OK;
1802 }
1803
1804
1805 /**
1806  * Handle PULL REPLY message from another peer.
1807  *
1808  * Check whether we sent a corresponding request and
1809  * whether this reply is the first one.
1810  *
1811  * @param cls Closure
1812  * @param channel The channel the PUSH was received over
1813  * @param channel_ctx The context associated with this channel
1814  * @param msg The message header
1815  */
1816   static int
1817 handle_peer_pull_reply (void *cls,
1818                         struct GNUNET_CADET_Channel *channel,
1819                         void **channel_ctx,
1820                         const struct GNUNET_MessageHeader *msg)
1821 {
1822   struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
1823   struct GNUNET_PeerIdentity *peers;
1824   struct PeerContext *peer_ctx;
1825   struct GNUNET_PeerIdentity *sender;
1826   struct PeerContext *sender_ctx;
1827   struct PeerOutstandingOp out_op;
1828   uint32_t i;
1829 #ifdef ENABLE_MALICIOUS
1830   struct AttackedPeer *tmp_att_peer;
1831 #endif /* ENABLE_MALICIOUS */
1832
1833   /* Check for protocol violation */
1834   if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->size))
1835   {
1836     GNUNET_break_op (0);
1837     GNUNET_CADET_receive_done (channel);
1838     return GNUNET_SYSERR;
1839   }
1840
1841   in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
1842   if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1843       sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1844   {
1845     LOG (GNUNET_ERROR_TYPE_ERROR,
1846         "message says it sends %" PRIu32 " peers, have space for %i peers\n",
1847         ntohl (in_msg->num_peers),
1848         (ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1849             sizeof (struct GNUNET_PeerIdentity));
1850     GNUNET_break_op (0);
1851     GNUNET_CADET_receive_done (channel);
1852     return GNUNET_SYSERR;
1853   }
1854
1855   // Guess simply casting isn't the nicest way...
1856   // FIXME wait for cadet to change this function
1857   sender = (struct GNUNET_PeerIdentity *)
1858       GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
1859   sender_ctx = get_peer_ctx (sender);
1860
1861   LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REPLY (%s)\n", GNUNET_i2s (sender));
1862
1863   if (GNUNET_YES != get_peer_flag (sender_ctx, PULL_REPLY_PENDING))
1864   {
1865     LOG (GNUNET_ERROR_TYPE_WARNING,
1866         "Received a pull reply from a peer we didn't request one from!\n");
1867     GNUNET_break_op (0);
1868     GNUNET_CADET_receive_done (channel);
1869     return GNUNET_OK;
1870   }
1871
1872
1873   #ifdef ENABLE_MALICIOUS
1874   // We shouldn't even receive pull replies as we're not sending
1875   if (2 == mal_type)
1876     return GNUNET_OK;
1877   #endif /* ENABLE_MALICIOUS */
1878
1879   /* Do actual logic */
1880   peers = (struct GNUNET_PeerIdentity *) &in_msg[1];
1881
1882   LOG (GNUNET_ERROR_TYPE_DEBUG,
1883        "PULL REPLY received, got following %u peers:\n",
1884        ntohl (in_msg->num_peers));
1885
1886   for (i = 0 ; i < ntohl (in_msg->num_peers) ; i++)
1887   {
1888     LOG (GNUNET_ERROR_TYPE_DEBUG,
1889          "%u. %s\n",
1890          i,
1891          GNUNET_i2s (&peers[i]));
1892
1893     #ifdef ENABLE_MALICIOUS
1894     if ((NULL != att_peer_set) &&
1895         (1 == mal_type || 3 == mal_type))
1896     { /* Add attacked peer to local list */
1897       // TODO check if we sent a request and this was the first reply
1898       if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1899                                                                &peers[i])
1900           && GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mal_peer_set,
1901                                                                   &peers[i])
1902           && 0 != GNUNET_CRYPTO_cmp_peer_identity (&peers[i],
1903                                                    &own_identity))
1904       {
1905         tmp_att_peer = GNUNET_new (struct AttackedPeer);
1906         tmp_att_peer->peer_id = peers[i];
1907         GNUNET_CONTAINER_DLL_insert (att_peers_head,
1908                                      att_peers_tail,
1909                                      tmp_att_peer);
1910         add_peer_array_to_set (&peers[i], 1, att_peer_set);
1911       }
1912       continue;
1913     }
1914     #endif /* ENABLE_MALICIOUS */
1915     if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity,
1916                                               &peers[i]))
1917     {
1918       if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map,
1919             &peers[i]))
1920         peer_ctx = create_peer_ctx (&peers[i]);
1921       else
1922         peer_ctx = get_peer_ctx (&peers[i]);
1923
1924       if (GNUNET_YES == get_peer_flag (peer_ctx, VALID))
1925       {
1926         if (GNUNET_NO == in_arr (pull_list, pull_list_size, &peers[i]))
1927           GNUNET_array_append (pull_list, pull_list_size, peers[i]);
1928       }
1929       else if (GNUNET_NO == insert_in_pull_list_scheduled (peer_ctx))
1930       {
1931         out_op.op = insert_in_pull_list;
1932         out_op.op_cls = NULL;
1933         GNUNET_array_append (peer_ctx->outstanding_ops,
1934                              peer_ctx->num_outstanding_ops,
1935                              out_op);
1936         check_peer_live (peer_ctx);
1937       }
1938     }
1939   }
1940
1941   unset_peer_flag (sender_ctx, PULL_REPLY_PENDING);
1942
1943   peer_clean (sender);
1944
1945   GNUNET_CADET_receive_done (channel);
1946   return GNUNET_OK;
1947 }
1948
1949
1950 /**
1951  * Compute a random delay.
1952  * A uniformly distributed value between mean + spread and mean - spread.
1953  *
1954  * For example for mean 4 min and spread 2 the minimum is (4 min - (1/2 * 4 min))
1955  * It would return a random value between 2 and 6 min.
1956  *
1957  * @param mean the mean
1958  * @param spread the inverse amount of deviation from the mean
1959  */
1960 static struct GNUNET_TIME_Relative
1961 compute_rand_delay (struct GNUNET_TIME_Relative mean, unsigned int spread)
1962 {
1963   struct GNUNET_TIME_Relative half_interval;
1964   struct GNUNET_TIME_Relative ret;
1965   unsigned int rand_delay;
1966   unsigned int max_rand_delay;
1967
1968   if (0 == spread)
1969   {
1970     LOG (GNUNET_ERROR_TYPE_WARNING,
1971          "Not accepting spread of 0\n");
1972     GNUNET_break (0);
1973   }
1974
1975   /* Compute random time value between spread * mean and spread * mean */
1976   half_interval = GNUNET_TIME_relative_divide (mean, spread);
1977
1978   max_rand_delay = GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us / mean.rel_value_us * (2/spread);
1979   /**
1980    * Compute random value between (0 and 1) * round_interval
1981    * via multiplying round_interval with a 'fraction' (0 to value)/value
1982    */
1983   rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_rand_delay);
1984   ret = GNUNET_TIME_relative_multiply (mean,  rand_delay);
1985   ret = GNUNET_TIME_relative_divide   (ret, max_rand_delay);
1986   ret = GNUNET_TIME_relative_add      (ret, half_interval);
1987
1988   if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ret.rel_value_us)
1989     LOG (GNUNET_ERROR_TYPE_WARNING,
1990          "Returning FOREVER_REL\n");
1991
1992   return ret;
1993 }
1994
1995
1996 /**
1997  * Send single pull request
1998  *
1999  * @param peer_id the peer to send the pull request to.
2000  */
2001 static void
2002 send_pull_request (const struct GNUNET_PeerIdentity *peer_id)
2003 {
2004   struct GNUNET_MQ_Envelope *ev;
2005   struct GNUNET_MQ_Handle *mq;
2006   struct PeerContext *peer_ctx;
2007   struct PendingMessage *pending_msg;
2008
2009   peer_ctx = get_peer_ctx (peer_id);
2010   GNUNET_assert (GNUNET_NO == get_peer_flag (peer_ctx, PULL_REPLY_PENDING));
2011   set_peer_flag (peer_ctx, PULL_REPLY_PENDING);
2012
2013   LOG (GNUNET_ERROR_TYPE_DEBUG,
2014        "Going to send PULL REQUEST to peer %s.\n",
2015        GNUNET_i2s (peer_id));
2016
2017   ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
2018   mq = get_mq (peer_id);
2019   pending_msg = insert_pending_message (peer_id, ev, "PULL REQUEST");
2020   GNUNET_MQ_notify_sent (ev,
2021       mq_notify_sent_cb,
2022       pending_msg);
2023   GNUNET_MQ_send (mq, ev);
2024 }
2025
2026
2027 /**
2028  * Send single push
2029  *
2030  * @param peer_id the peer to send the push to.
2031  */
2032 static void
2033 send_push (const struct GNUNET_PeerIdentity *peer_id)
2034 {
2035   struct GNUNET_MQ_Envelope *ev;
2036   struct GNUNET_MQ_Handle *mq;
2037   struct PendingMessage *pending_msg;
2038
2039   LOG (GNUNET_ERROR_TYPE_DEBUG,
2040        "Going to send PUSH to peer %s.\n",
2041        GNUNET_i2s (peer_id));
2042
2043   ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
2044   mq = get_mq (peer_id);
2045   pending_msg = insert_pending_message (peer_id, ev, "PUSH");
2046   GNUNET_MQ_notify_sent (ev,
2047       mq_notify_sent_cb,
2048       pending_msg);
2049   GNUNET_MQ_send (mq, ev);
2050 }
2051
2052
2053 static void
2054 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2055
2056 static void
2057 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2058
2059
2060 #ifdef ENABLE_MALICIOUS
2061 /**
2062  * Turn RPS service to act malicious.
2063  *
2064  * @param cls Closure
2065  * @param client The client that sent the message
2066  * @param msg The message header
2067  */
2068   static void
2069 handle_client_act_malicious (void *cls,
2070                              struct GNUNET_SERVER_Client *client,
2071                              const struct GNUNET_MessageHeader *msg)
2072 {
2073   struct GNUNET_RPS_CS_ActMaliciousMessage *in_msg;
2074   struct GNUNET_PeerIdentity *peers;
2075   uint32_t num_mal_peers_sent;
2076   uint32_t num_mal_peers_old;
2077
2078   /* Check for protocol violation */
2079   if (sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage) > ntohs (msg->size))
2080   {
2081     GNUNET_break_op (0);
2082   }
2083
2084   in_msg = (struct GNUNET_RPS_CS_ActMaliciousMessage *) msg;
2085   if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
2086       sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
2087   {
2088     LOG (GNUNET_ERROR_TYPE_ERROR,
2089         "message says it sends %" PRIu64 " peers, have space for %i peers\n",
2090         ntohl (in_msg->num_peers),
2091         (ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
2092             sizeof (struct GNUNET_PeerIdentity));
2093     GNUNET_break_op (0);
2094   }
2095
2096
2097   /* Do actual logic */
2098   peers = (struct GNUNET_PeerIdentity *) &msg[1];
2099   mal_type = ntohl (in_msg->type);
2100   if (NULL == mal_peer_set)
2101     mal_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
2102
2103   LOG (GNUNET_ERROR_TYPE_DEBUG,
2104        "Now acting malicious type %" PRIu32 ", got %" PRIu32 " peers.\n",
2105        mal_type,
2106        ntohl (in_msg->num_peers));
2107
2108   if (1 == mal_type)
2109   { /* Try to maximise representation */
2110     /* Add other malicious peers to those we already know */
2111
2112     num_mal_peers_sent = ntohl (in_msg->num_peers);
2113     num_mal_peers_old = num_mal_peers;
2114     GNUNET_array_grow (mal_peers,
2115                        num_mal_peers,
2116                        num_mal_peers + num_mal_peers_sent);
2117     memcpy (&mal_peers[num_mal_peers_old],
2118             peers,
2119             num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
2120
2121     /* Add all mal peers to mal_peer_set */
2122     add_peer_array_to_set (&mal_peers[num_mal_peers_old],
2123                            num_mal_peers_sent,
2124                            mal_peer_set);
2125
2126     /* Substitute do_round () with do_mal_round () */
2127     GNUNET_SCHEDULER_cancel (do_round_task);
2128     do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
2129   }
2130
2131   else if (2 == mal_type
2132            || 3 == mal_type)
2133   { /* Try to partition the network */
2134     /* Add other malicious peers to those we already know */
2135     struct PeerContext *att_ctx;
2136
2137     num_mal_peers_sent = ntohl (in_msg->num_peers) - 1;
2138     num_mal_peers_old = num_mal_peers;
2139     GNUNET_array_grow (mal_peers,
2140                        num_mal_peers,
2141                        num_mal_peers + num_mal_peers_sent);
2142     if (NULL != mal_peers &&
2143         0 != num_mal_peers)
2144     {
2145       memcpy (&mal_peers[num_mal_peers_old],
2146               peers,
2147               num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
2148
2149       /* Add all mal peers to mal_peer_set */
2150       add_peer_array_to_set (&mal_peers[num_mal_peers_old],
2151                              num_mal_peers_sent,
2152                              mal_peer_set);
2153     }
2154
2155     /* Store the one attacked peer */
2156     memcpy (&attacked_peer,
2157             &in_msg->attacked_peer,
2158             sizeof (struct GNUNET_PeerIdentity));
2159     /* Set the flag of the attacked peer to valid to avoid problems */
2160     if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map,
2161           &attacked_peer))
2162     {
2163       att_ctx = create_peer_ctx (&attacked_peer);
2164       check_peer_live (att_ctx);
2165     }
2166
2167     LOG (GNUNET_ERROR_TYPE_DEBUG,
2168          "Attacked peer is %s\n",
2169          GNUNET_i2s (&attacked_peer));
2170
2171     /* Substitute do_round () with do_mal_round () */
2172     GNUNET_SCHEDULER_cancel (do_round_task);
2173     do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
2174   }
2175   else if (0 == mal_type)
2176   { /* Stop acting malicious */
2177     GNUNET_array_grow (mal_peers, num_mal_peers, 0);
2178
2179     /* Substitute do_mal_round () with do_round () */
2180     GNUNET_SCHEDULER_cancel (do_round_task);
2181     do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
2182   }
2183   else
2184   {
2185     GNUNET_break (0);
2186   }
2187
2188   GNUNET_SERVER_receive_done (client,   GNUNET_OK);
2189 }
2190
2191
2192 /**
2193  * Send out PUSHes and PULLs maliciously.
2194  *
2195  * This is executed regylary.
2196  */
2197 static void
2198 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2199 {
2200   uint32_t num_pushes;
2201   uint32_t i;
2202   struct GNUNET_TIME_Relative time_next_round;
2203   struct AttackedPeer *tmp_att_peer;
2204   struct PeerContext *peer_ctx;
2205
2206   LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round maliciously type %" PRIu32 ".\n",
2207       mal_type);
2208   do_round_task = NULL;
2209   GNUNET_assert (mal_type <= 3);
2210   /* Do malicious actions */
2211   if (1 == mal_type)
2212   { /* Try to maximise representation */
2213
2214     /* The maximum of pushes we're going to send this round */
2215     num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit,
2216                                          num_attacked_peers),
2217                              GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
2218
2219     LOG (GNUNET_ERROR_TYPE_DEBUG,
2220          "Going to send %" PRIu32 " pushes\n",
2221          num_pushes);
2222
2223     /* Send PUSHes to attacked peers */
2224     for (i = 0 ; i < num_pushes ; i++)
2225     {
2226       if (att_peers_tail == att_peer_index)
2227         att_peer_index = att_peers_head;
2228       else
2229         att_peer_index = att_peer_index->next;
2230
2231       send_push (&att_peer_index->peer_id);
2232     }
2233
2234     /* Send PULLs to some peers to learn about additional peers to attack */
2235     tmp_att_peer = att_peer_index;
2236     for (i = 0 ; i < num_pushes * alpha ; i++)
2237     {
2238       if (att_peers_tail == tmp_att_peer)
2239         tmp_att_peer = att_peers_head;
2240       else
2241         att_peer_index = tmp_att_peer->next;
2242
2243       send_pull_request (&tmp_att_peer->peer_id);
2244     }
2245   }
2246
2247
2248   else if (2 == mal_type)
2249   { /**
2250      * Try to partition the network
2251      * Send as many pushes to the attacked peer as possible
2252      * That is one push per round as it will ignore more.
2253      */
2254     peer_ctx = get_peer_ctx (&attacked_peer);
2255     if (GNUNET_YES == get_peer_flag (peer_ctx, VALID))
2256       send_push (&attacked_peer);
2257   }
2258
2259
2260   if (3 == mal_type)
2261   { /* Combined attack */
2262
2263     /* Send PUSH to attacked peers */
2264     if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map,
2265           &attacked_peer))
2266     {
2267       peer_ctx = get_peer_ctx (&attacked_peer);
2268       if (GNUNET_YES == get_peer_flag (peer_ctx, VALID))
2269       {
2270         LOG (GNUNET_ERROR_TYPE_DEBUG,
2271             "Goding to send push to attacked peer (%s)\n",
2272             GNUNET_i2s (&attacked_peer));
2273         send_push (&attacked_peer);
2274       }
2275       else
2276         check_peer_live (peer_ctx);
2277     }
2278     else
2279       peer_ctx = create_peer_ctx (&attacked_peer);
2280     check_peer_live (peer_ctx);
2281
2282     /* The maximum of pushes we're going to send this round */
2283     num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit - 1,
2284                                          num_attacked_peers),
2285                              GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
2286
2287     LOG (GNUNET_ERROR_TYPE_DEBUG,
2288          "Going to send %" PRIu32 " pushes\n",
2289          num_pushes);
2290
2291     for (i = 0 ; i < num_pushes ; i++)
2292     {
2293       if (att_peers_tail == att_peer_index)
2294         att_peer_index = att_peers_head;
2295       else
2296         att_peer_index = att_peer_index->next;
2297
2298       send_push (&att_peer_index->peer_id);
2299     }
2300
2301     /* Send PULLs to some peers to learn about additional peers to attack */
2302     tmp_att_peer = att_peer_index;
2303     for (i = 0 ; i < num_pushes * alpha ; i++)
2304     {
2305       if (att_peers_tail == tmp_att_peer)
2306         tmp_att_peer = att_peers_head;
2307       else
2308         att_peer_index = tmp_att_peer->next;
2309
2310       send_pull_request (&tmp_att_peer->peer_id);
2311     }
2312   }
2313
2314   /* Schedule next round */
2315   time_next_round = compute_rand_delay (round_interval, 2);
2316
2317   //do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_mal_round,
2318   //NULL);
2319   GNUNET_assert (NULL == do_round_task);
2320   do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_mal_round, NULL);
2321   LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
2322 }
2323 #endif /* ENABLE_MALICIOUS */
2324
2325
2326 /**
2327  * Send out PUSHes and PULLs, possibly update #view, samplers.
2328  *
2329  * This is executed regylary.
2330  */
2331 static void
2332 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2333 {
2334   LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round.\n");
2335
2336   uint32_t i;
2337   unsigned int view_size;
2338   unsigned int *permut;
2339   unsigned int a_peers; /* Number of peers we send pushes to */
2340   unsigned int b_peers; /* Number of peers we send pull requests to */
2341   uint32_t first_border;
2342   uint32_t second_border;
2343   struct GNUNET_PeerIdentity peer;
2344   struct PeerContext *peer_ctx;
2345
2346   do_round_task = NULL;
2347   LOG (GNUNET_ERROR_TYPE_DEBUG,
2348        "Printing view:\n");
2349   to_file (file_name_view_log,
2350            "___ new round ___");
2351   view_size = GNUNET_CONTAINER_multipeermap_size (view);
2352   generate_view_array (view_size);
2353   for (i = 0 ; i < view_size ; i++)
2354   {
2355     LOG (GNUNET_ERROR_TYPE_DEBUG,
2356          "\t%s\n", GNUNET_i2s (&view_array[i]));
2357     to_file (file_name_view_log,
2358              "=%s\t(do round)",
2359              GNUNET_i2s_full (&view_array[i]));
2360   }
2361
2362
2363   /* Send pushes and pull requests */
2364   if (0 < view_size)
2365   {
2366     permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2367                                            (unsigned int) view_size);
2368
2369     /* Send PUSHes */
2370     a_peers = ceil (alpha * view_size);
2371
2372     LOG (GNUNET_ERROR_TYPE_DEBUG,
2373          "Going to send pushes to %u (ceil (%f * %u)) peers.\n",
2374          a_peers, alpha, view_size);
2375     for (i = 0; i < a_peers; i++)
2376     {
2377       peer = view_array[permut[i]];
2378       if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer)) // TODO
2379       { // FIXME if this fails schedule/loop this for later
2380         send_push (&peer);
2381       }
2382     }
2383
2384     /* Send PULL requests */
2385     b_peers = ceil (beta * view_size);
2386     first_border = a_peers;
2387     second_border = a_peers + b_peers;
2388     if (second_border > view_size)
2389     {
2390       first_border = view_size - b_peers;
2391       second_border = view_size;
2392     }
2393     LOG (GNUNET_ERROR_TYPE_DEBUG,
2394         "Going to send pulls to %u (ceil (%f * %u)) peers.\n",
2395         b_peers, beta, view_size);
2396     for (i = first_border; i < second_border; i++)
2397     {
2398       peer = view_array[permut[i]];
2399       peer_ctx = get_peer_ctx (&peer);
2400       if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer) &&
2401           GNUNET_NO == get_peer_flag (peer_ctx, PULL_REPLY_PENDING)) // TODO
2402       { // FIXME if this fails schedule/loop this for later
2403         send_pull_request (&peer);
2404       }
2405     }
2406
2407     GNUNET_free (permut);
2408     permut = NULL;
2409   }
2410
2411
2412   /* Update view */
2413   /* TODO see how many peers are in push-/pull- list! */
2414
2415   if (push_list_size <= alpha * view_size &&
2416       0 < push_list_size &&
2417       0 < pull_list_size)
2418   {
2419     LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the view.\n");
2420
2421     uint32_t final_size;
2422     uint32_t peers_to_clean_size;
2423     struct GNUNET_PeerIdentity *peers_to_clean;
2424
2425     peers_to_clean = NULL;
2426     peers_to_clean_size = 0;
2427     GNUNET_array_grow (peers_to_clean, peers_to_clean_size, view_size);
2428     memcpy (peers_to_clean,
2429             view_array,
2430             view_size * sizeof (struct GNUNET_PeerIdentity));
2431
2432     /* Seems like recreating is the easiest way of emptying the peermap */
2433     GNUNET_CONTAINER_multipeermap_destroy (view);
2434     view = GNUNET_CONTAINER_multipeermap_create (view_size, GNUNET_NO);
2435     to_file (file_name_view_log,
2436              "--- emptied ---");
2437
2438     first_border  = GNUNET_MIN (ceil (alpha * sampler_size_est_need),
2439                                 push_list_size);
2440     second_border = first_border +
2441                     GNUNET_MIN (floor (beta  * sampler_size_est_need),
2442                                 pull_list_size);
2443     final_size    = second_border +
2444       ceil ((1 - (alpha + beta)) * sampler_size_est_need);
2445
2446     GNUNET_array_grow (view_array, view_size, second_border);
2447
2448     /* Update view with peers received through PUSHes */
2449     permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2450                                            push_list_size);
2451     for (i = 0; i < first_border; i++)
2452     {
2453       view_array[i] = push_list[permut[i]];
2454       GNUNET_CONTAINER_multipeermap_put (view, &push_list[permut[i]], NULL,
2455           GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2456
2457       to_file (file_name_view_log,
2458                "+%s\t(push list)",
2459                GNUNET_i2s_full (&view_array[i]));
2460       // TODO change the peer_flags accordingly
2461     }
2462     GNUNET_free (permut);
2463     permut = NULL;
2464
2465     /* Update view with peers received through PULLs */
2466     permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2467                                            pull_list_size);
2468     for (i = first_border; i < second_border; i++)
2469     {
2470       view_array[i] = pull_list[permut[i - first_border]];
2471       GNUNET_CONTAINER_multipeermap_put (view,
2472           &pull_list[permut[i - first_border]],
2473           NULL,
2474           GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2475
2476       to_file (file_name_view_log,
2477                "+%s\t(pull list)",
2478                GNUNET_i2s_full (&view_array[i]));
2479       // TODO change the peer_flags accordingly
2480     }
2481     GNUNET_free (permut);
2482     permut = NULL;
2483
2484     /* Update view with peers from history */
2485     RPS_sampler_get_n_rand_peers (prot_sampler,
2486                                   hist_update,
2487                                   NULL,
2488                                   final_size - second_border);
2489     num_hist_update_tasks = final_size - second_border;
2490     // TODO change the peer_flags accordingly
2491
2492     for (i = 0; i < view_size; i++)
2493       rem_from_list (&peers_to_clean, &peers_to_clean_size, &view_array[i]);
2494
2495     /* Clean peers that were removed from the view */
2496     for (i = 0; i < peers_to_clean_size; i++)
2497     {
2498       to_file (file_name_view_log,
2499                "-%s",
2500                GNUNET_i2s_full (&peers_to_clean[i]));
2501       peer_clean (&peers_to_clean[i]);
2502     }
2503
2504     GNUNET_array_grow (peers_to_clean, peers_to_clean_size, 0);
2505     peers_to_clean = NULL;
2506   }
2507   else
2508   {
2509     LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the view.\n");
2510   }
2511   // TODO independent of that also get some peers from CADET_get_peers()?
2512
2513   LOG (GNUNET_ERROR_TYPE_DEBUG,
2514        "Received %u pushes and %u pulls last round (alpha (%.2f) * view_size (%u) = %.2f)\n",
2515        push_list_size,
2516        pull_list_size,
2517        alpha,
2518        view_size,
2519        alpha * view_size);
2520
2521   /* Update samplers */
2522   for (i = 0; i < push_list_size; i++)
2523   {
2524     LOG (GNUNET_ERROR_TYPE_DEBUG,
2525          "Updating with peer %s from push list\n",
2526          GNUNET_i2s (&push_list[i]));
2527     insert_in_sampler (NULL, &push_list[i]);
2528     peer_clean (&push_list[i]); /* This cleans only if it is not in the view */
2529   }
2530
2531   for (i = 0; i < pull_list_size; i++)
2532   {
2533     LOG (GNUNET_ERROR_TYPE_DEBUG,
2534          "Updating with peer %s from pull list\n",
2535          GNUNET_i2s (&pull_list[i]));
2536     insert_in_sampler (NULL, &pull_list[i]);
2537     peer_clean (&pull_list[i]); /* This cleans only if it is not in the view */
2538   }
2539
2540
2541   /* Empty push/pull lists */
2542   GNUNET_array_grow (push_list, push_list_size, 0);
2543   GNUNET_array_grow (pull_list, pull_list_size, 0);
2544
2545   struct GNUNET_TIME_Relative time_next_round;
2546
2547   time_next_round = compute_rand_delay (round_interval, 2);
2548
2549   /* Schedule next round */
2550   do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_round, NULL);
2551   LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
2552 }
2553
2554
2555 static void
2556 rps_start (struct GNUNET_SERVER_Handle *server);
2557
2558
2559 /**
2560  * This is called from GNUNET_CADET_get_peers().
2561  *
2562  * It is called on every peer(ID) that cadet somehow has contact with.
2563  * We use those to initialise the sampler.
2564  */
2565 void
2566 init_peer_cb (void *cls,
2567               const struct GNUNET_PeerIdentity *peer,
2568               int tunnel, // "Do we have a tunnel towards this peer?"
2569               unsigned int n_paths, // "Number of known paths towards this peer"
2570               unsigned int best_path) // "How long is the best path?
2571                                       // (0 = unknown, 1 = ourselves, 2 = neighbor)"
2572 {
2573   if (NULL != peer)
2574   {
2575     LOG (GNUNET_ERROR_TYPE_DEBUG,
2576          "Got peer_id %s from cadet\n",
2577          GNUNET_i2s (peer));
2578     new_peer_id (peer);
2579   }
2580 }
2581
2582
2583 /**
2584  * Iterator over peers from peerinfo.
2585  *
2586  * @param cls closure
2587  * @param peer id of the peer, NULL for last call
2588  * @param hello hello message for the peer (can be NULL)
2589  * @param error message
2590  */
2591 void
2592 process_peerinfo_peers (void *cls,
2593                         const struct GNUNET_PeerIdentity *peer,
2594                         const struct GNUNET_HELLO_Message *hello,
2595                         const char *err_msg)
2596 {
2597   if (NULL != peer)
2598   {
2599     LOG (GNUNET_ERROR_TYPE_DEBUG,
2600          "Got peer_id %s from peerinfo\n",
2601          GNUNET_i2s (peer));
2602     new_peer_id (peer);
2603   }
2604 }
2605
2606
2607 /**
2608  * Callback used to remove peers from the multipeermap.
2609  */
2610   int
2611 peer_remove_cb (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
2612 {
2613   struct PeerContext *peer_ctx;
2614   const struct GNUNET_CADET_Channel *channel =
2615     (const struct GNUNET_CADET_Channel *) cls;
2616
2617   peer_ctx = (struct PeerContext *) value;
2618   set_peer_flag (peer_ctx, TO_DESTROY);
2619
2620   LOG (GNUNET_ERROR_TYPE_DEBUG,
2621        "Going to remove peer %s\n",
2622        GNUNET_i2s (&peer_ctx->peer_id));
2623
2624   /* Remove it from the sampler used for the Brahms protocol */
2625   RPS_sampler_reinitialise_by_value (prot_sampler, key);
2626
2627   /* If operations are still scheduled for this peer cancel those */
2628   if (0 != peer_ctx->num_outstanding_ops)
2629   {
2630     GNUNET_array_grow (peer_ctx->outstanding_ops,
2631                        peer_ctx->num_outstanding_ops,
2632                        0);
2633   }
2634
2635   /* If we are still waiting for notification whether this peer is live
2636    * cancel the according task */
2637   if (NULL != peer_ctx->transmit_handle)
2638   {
2639     LOG (GNUNET_ERROR_TYPE_DEBUG,
2640          "Trying to cancle transmit_handle for peer %s\n",
2641          GNUNET_i2s (key));
2642     GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->transmit_handle);
2643     peer_ctx->transmit_handle = NULL;
2644   }
2645
2646   unset_peer_flag (peer_ctx, PULL_REPLY_PENDING);
2647
2648   /* Remove peer from view */
2649   if (GNUNET_CONTAINER_multipeermap_contains (view, key))
2650   {
2651     to_file (file_name_view_log,
2652         "-%s\t(cleanup channel, other peer)",
2653         GNUNET_i2s_full (key));
2654     GNUNET_CONTAINER_multipeermap_remove_all (view, key);
2655     if (NULL != view_array)
2656     {
2657       GNUNET_free (view_array);
2658       view_array = NULL;
2659     }
2660   }
2661
2662   /* Remove from push and pull lists */
2663   if (GNUNET_YES == in_arr (push_list, push_list_size, key))
2664     rem_from_list (&push_list, &push_list_size, key);
2665   if (GNUNET_YES == in_arr (pull_list, pull_list_size, key))
2666     rem_from_list (&pull_list, &pull_list_size, key);
2667
2668   /* Cancle messages that have not been sent yet */
2669   while (NULL != peer_ctx->pending_messages_head)
2670   {
2671     LOG (GNUNET_ERROR_TYPE_DEBUG,
2672         "Removing unsent %s\n",
2673         peer_ctx->pending_messages_head->type);
2674     /* We are not able to cancel messages as #GNUNET_CADET_mq_create () does not
2675      * set a #GNUNET_MQ_CancelImpl */
2676     /* GNUNET_MQ_send_cancel (peer_ctx->pending_messages_head->ev); */
2677     remove_pending_message (peer_ctx->pending_messages_head);
2678   }
2679
2680   /* If there is still a mq destroy it */
2681   if (NULL != peer_ctx->mq)
2682   {
2683     GNUNET_MQ_destroy (peer_ctx->mq);
2684     peer_ctx->mq = NULL;
2685   }
2686
2687
2688   /* Remove the send_channel
2689    * This function should be called again from #cleanup_channel (callback
2690    * called on the destruction of channels) and clean up the rest. */
2691   if (NULL != peer_ctx->send_channel &&
2692       channel != peer_ctx->send_channel)
2693   {
2694     GNUNET_CADET_channel_destroy (peer_ctx->send_channel);
2695     peer_ctx->send_channel = NULL;
2696   }
2697
2698   /* Remove the recv_channel
2699    * This function should be called again from #cleanup_channel (callback
2700    * called on the destruction of channels) and clean up the rest. */
2701   if (NULL != peer_ctx->recv_channel &&
2702       channel != peer_ctx->recv_channel)
2703   {
2704     GNUNET_CADET_channel_destroy (peer_ctx->recv_channel);
2705     peer_ctx->recv_channel = NULL;
2706   }
2707
2708   /* If there is no channel we have to remove the context now */
2709   if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_remove_all (peer_map, key))
2710     LOG (GNUNET_ERROR_TYPE_WARNING, "removing peer from peer_map failed\n");
2711
2712   GNUNET_free (peer_ctx);
2713
2714   return GNUNET_YES;
2715 }
2716
2717
2718 /**
2719  * Clean the send channel of a peer
2720  * If there is also no channel to receive messages from that peer, remove it
2721  * from the peermap.
2722  */
2723 void
2724 peer_clean (const struct GNUNET_PeerIdentity *peer)
2725 {
2726   struct PeerContext *peer_ctx;
2727   /* struct GNUNET_CADET_Channel *channel; */
2728
2729   if ( (0 == RPS_sampler_count_id (prot_sampler, peer)) &&
2730        (GNUNET_NO  == GNUNET_CONTAINER_multipeermap_contains (view, peer)) &&
2731        (GNUNET_NO  == in_arr (push_list, push_list_size, peer)) &&
2732        (GNUNET_NO  == in_arr (pull_list, pull_list_size, peer)) &&
2733        (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer)) )
2734   {
2735     peer_ctx = get_peer_ctx (peer);
2736
2737     if ( (NULL == peer_ctx->recv_channel) &&
2738          (NULL == peer_ctx->pending_messages_head) &&
2739          (GNUNET_NO == get_peer_flag (peer_ctx, PULL_REPLY_PENDING)) )
2740     {
2741       #ifdef ENABLE_MALICIOUS
2742       if (0 != GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
2743         peer_remove_cb (NULL, peer, peer_ctx);
2744       #else /* ENABLE_MALICIOUS */
2745       peer_remove_cb (NULL, peer, peer_ctx);
2746       #endif /* ENABLE_MALICIOUS */
2747     }
2748   }
2749 }
2750
2751
2752 /**
2753  * Task run during shutdown.
2754  *
2755  * @param cls unused
2756  * @param tc unused
2757  */
2758 static void
2759 shutdown_task (void *cls,
2760                      const struct GNUNET_SCHEDULER_TaskContext *tc)
2761 {
2762
2763   LOG (GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
2764
2765   GNUNET_PEERINFO_notify_cancel (peerinfo_notify_handle);
2766   GNUNET_PEERINFO_disconnect (peerinfo_handle);
2767
2768   if (NULL != do_round_task)
2769   {
2770     GNUNET_SCHEDULER_cancel (do_round_task);
2771     do_round_task = NULL;
2772   }
2773
2774   {
2775   if (GNUNET_SYSERR ==
2776         GNUNET_CONTAINER_multipeermap_iterate (peer_map, peer_remove_cb, NULL))
2777     LOG (GNUNET_ERROR_TYPE_WARNING,
2778         "Iterating over peers to disconnect from them was cancelled\n");
2779   }
2780
2781   GNUNET_NSE_disconnect (nse);
2782   RPS_sampler_destroy (prot_sampler);
2783   RPS_sampler_destroy (client_sampler);
2784   LOG (GNUNET_ERROR_TYPE_DEBUG,
2785        "Size of the peermap: %u\n",
2786        GNUNET_CONTAINER_multipeermap_size (peer_map));
2787   GNUNET_break (0 == GNUNET_CONTAINER_multipeermap_size (peer_map));
2788   GNUNET_CADET_disconnect (cadet_handle);
2789   GNUNET_CONTAINER_multipeermap_destroy (peer_map);
2790   GNUNET_CONTAINER_multipeermap_destroy (view);
2791   view = NULL;
2792   GNUNET_array_grow (push_list, push_list_size, 0);
2793   GNUNET_array_grow (pull_list, pull_list_size, 0);
2794   #ifdef ENABLE_MALICIOUS
2795   struct AttackedPeer *tmp_att_peer;
2796   GNUNET_array_grow (mal_peers, num_mal_peers, 0);
2797   if (NULL != mal_peer_set)
2798     GNUNET_CONTAINER_multipeermap_destroy (mal_peer_set);
2799   if (NULL != att_peer_set)
2800     GNUNET_CONTAINER_multipeermap_destroy (att_peer_set);
2801   while (NULL != att_peers_head)
2802   {
2803     tmp_att_peer = att_peers_head;
2804     GNUNET_CONTAINER_DLL_remove (att_peers_head, att_peers_tail, tmp_att_peer);
2805   }
2806   #endif /* ENABLE_MALICIOUS */
2807 }
2808
2809
2810 /**
2811  * @brief Get informed about a connecting client.
2812  *
2813  * @param cls unused
2814  * @param client the client that connects
2815  */
2816 static void
2817 handle_client_connect (void *cls,
2818                        struct GNUNET_SERVER_Client *client)
2819 {
2820   struct ClientContext *cli_ctx;
2821
2822   LOG (GNUNET_ERROR_TYPE_DEBUG,
2823        "Client connected\n");
2824   if (NULL == client)
2825     return; /* Server was destroyed before a client connected. Shutting down */
2826   cli_ctx = GNUNET_new (struct ClientContext);
2827   cli_ctx->mq = GNUNET_MQ_queue_for_server_client (client);
2828   GNUNET_SERVER_client_set_user_context (client, cli_ctx);
2829   GNUNET_CONTAINER_DLL_insert (cli_ctx_head,
2830                                cli_ctx_tail,
2831                                cli_ctx);
2832 }
2833
2834 /**
2835  * A client disconnected.  Remove all of its data structure entries.
2836  *
2837  * @param cls closure, NULL
2838  * @param client identification of the client
2839  */
2840 static void
2841 handle_client_disconnect (void *cls,
2842                                             struct GNUNET_SERVER_Client *client)
2843 {
2844   struct ClientContext *cli_ctx;
2845
2846   if (NULL == client)
2847   {/* shutdown task */
2848     while (NULL != cli_ctx_head)
2849       destroy_cli_ctx (cli_ctx_head);
2850   }
2851   else
2852   {
2853     cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientContext);
2854     destroy_cli_ctx (cli_ctx);
2855   }
2856 }
2857
2858
2859 /**
2860  * Handle the channel a peer opens to us.
2861  *
2862  * @param cls The closure
2863  * @param channel The channel the peer wants to establish
2864  * @param initiator The peer's peer ID
2865  * @param port The port the channel is being established over
2866  * @param options Further options
2867  */
2868   static void *
2869 handle_inbound_channel (void *cls,
2870                         struct GNUNET_CADET_Channel *channel,
2871                         const struct GNUNET_PeerIdentity *initiator,
2872                         uint32_t port,
2873                         enum GNUNET_CADET_ChannelOption options)
2874 {
2875   struct PeerContext *peer_ctx;
2876
2877   LOG (GNUNET_ERROR_TYPE_DEBUG,
2878       "New channel was established to us (Peer %s).\n",
2879       GNUNET_i2s (initiator));
2880   GNUNET_assert (NULL != channel); /* according to cadet API */
2881   if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, initiator))
2882     peer_ctx = create_peer_ctx (initiator);
2883   else
2884     peer_ctx = get_peer_ctx (initiator);
2885   /* We only accept one incoming channel from peers */
2886   if (NULL != peer_ctx->recv_channel)
2887   {
2888     GNUNET_CADET_channel_destroy (channel);
2889     return NULL;
2890   }
2891   peer_ctx->recv_channel = channel;
2892   peer_is_live (peer_ctx);
2893   return NULL;
2894 }
2895
2896
2897 /**
2898  * This is called when a channel is destroyed.
2899  *
2900  * @param cls The closure
2901  * @param channel The channel being closed
2902  * @param channel_ctx The context associated with this channel
2903  */
2904   static void
2905 cleanup_channel (void *cls,
2906                 const struct GNUNET_CADET_Channel *channel,
2907                 void *channel_ctx)
2908 {
2909   struct GNUNET_PeerIdentity *peer;
2910   struct PeerContext *peer_ctx;
2911
2912   peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
2913       (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
2914        // Guess simply casting isn't the nicest way...
2915        // FIXME wait for cadet to change this function
2916
2917   if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
2918   {/* We don't want to implicitly create a context that we're about to kill */
2919     peer_ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
2920     if (NULL == peer_ctx) /* It could have been removed by shutdown_task */
2921       return;
2922
2923     if (get_peer_flag (peer_ctx, TO_DESTROY))
2924     {/* We initiatad the destruction of this particular peer */
2925       if (channel == peer_ctx->send_channel)
2926         peer_ctx->send_channel = NULL;
2927       else if (channel == peer_ctx->recv_channel)
2928         peer_ctx->recv_channel = NULL;
2929
2930       to_file (file_name_view_log,
2931                "-%s\t(cleanup channel, ourself)",
2932                GNUNET_i2s_full (peer));
2933     }
2934
2935     else
2936     { /* We did not initiate the destruction of this peer */
2937       if (channel == peer_ctx->send_channel)
2938       { /* Something (but us) killd the channel - clean up peer */
2939         LOG (GNUNET_ERROR_TYPE_DEBUG,
2940             "send channel (%s) was destroyed - cleaning up\n",
2941             GNUNET_i2s (peer));
2942         peer_ctx->send_channel = NULL;
2943         /* Somwewhat {ab,re}use the iterator function */
2944         /* Cast to void is ok, because it's used as void in peer_remove_cb */
2945         (void) peer_remove_cb ((void *) channel, peer, peer_ctx);
2946       }
2947       else if (channel == peer_ctx->recv_channel)
2948       { /* Other peer doesn't want to send us messages anymore */
2949         LOG (GNUNET_ERROR_TYPE_DEBUG,
2950              "Peer %s destroyed recv channel - cleaning up channel\n",
2951              GNUNET_i2s (peer));
2952         peer_ctx->recv_channel = NULL;
2953       }
2954       else
2955       {
2956         LOG (GNUNET_ERROR_TYPE_WARNING,
2957              "unknown channel (%s) was destroyed\n",
2958              GNUNET_i2s (peer));
2959       }
2960     }
2961   }
2962
2963   else
2964   { /* We don't know a context to that peer */
2965     LOG (GNUNET_ERROR_TYPE_DEBUG,
2966          "channel (%s) without associated context was destroyed\n",
2967          GNUNET_i2s (peer));
2968   }
2969 }
2970
2971
2972 /**
2973  * Actually start the service.
2974  */
2975   static void
2976 rps_start (struct GNUNET_SERVER_Handle *server)
2977 {
2978   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
2979     {&handle_client_request,        NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
2980       sizeof (struct GNUNET_RPS_CS_RequestMessage)},
2981     {&handle_client_request_cancel, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST_CANCEL,
2982       sizeof (struct GNUNET_RPS_CS_RequestCancelMessage)},
2983     {&handle_client_seed,           NULL, GNUNET_MESSAGE_TYPE_RPS_CS_SEED, 0},
2984     #ifdef ENABLE_MALICIOUS
2985     {&handle_client_act_malicious,  NULL, GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS , 0},
2986     #endif /* ENABLE_MALICIOUS */
2987     {NULL, NULL, 0, 0}
2988   };
2989
2990   GNUNET_SERVER_add_handlers (server, handlers);
2991   GNUNET_SERVER_connect_notify (server,
2992                                 &handle_client_connect,
2993                                 NULL);
2994   GNUNET_SERVER_disconnect_notify (server,
2995                                    &handle_client_disconnect,
2996                                    NULL);
2997   LOG (GNUNET_ERROR_TYPE_INFO, "Ready to receive requests from clients\n");
2998
2999
3000   do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
3001   LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
3002
3003   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
3004                                                         &shutdown_task,
3005                                                         NULL);
3006 }
3007
3008
3009 /**
3010  * Process statistics requests.
3011  *
3012  * @param cls closure
3013  * @param server the initialized server
3014  * @param c configuration to use
3015  */
3016   static void
3017 run (void *cls,
3018      struct GNUNET_SERVER_Handle *server,
3019      const struct GNUNET_CONFIGURATION_Handle *c)
3020 {
3021   int size;
3022   int out_size;
3023
3024   // TODO check what this does -- copied from gnunet-boss
3025   // - seems to work as expected
3026   GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
3027   cfg = c;
3028
3029
3030   /* Get own ID */
3031   GNUNET_CRYPTO_get_peer_identity (cfg, &own_identity); // TODO check return value
3032   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3033               "STARTING SERVICE (rps) for peer [%s]\n",
3034               GNUNET_i2s (&own_identity));
3035   #ifdef ENABLE_MALICIOUS
3036   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3037               "Malicious execution compiled in.\n");
3038   #endif /* ENABLE_MALICIOUS */
3039
3040
3041
3042   /* Get time interval from the configuration */
3043   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
3044                                                         "ROUNDINTERVAL",
3045                                                         &round_interval))
3046   {
3047     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
3048                                "RPS", "ROUNDINTERVAL");
3049     GNUNET_SCHEDULER_shutdown ();
3050     return;
3051   }
3052
3053   /* Get initial size of sampler/view from the configuration */
3054   if (GNUNET_OK !=
3055       GNUNET_CONFIGURATION_get_value_number (cfg, "RPS", "INITSIZE",
3056         (long long unsigned int *) &sampler_size_est_need))
3057   {
3058     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
3059                                "RPS", "INITSIZE");
3060     GNUNET_SCHEDULER_shutdown ();
3061     return;
3062   }
3063   LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", sampler_size_est_need);
3064
3065
3066   view = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO);
3067
3068   /* file_name_view_log */
3069   if (GNUNET_OK != GNUNET_DISK_directory_create ("/tmp/rps/"))
3070   {
3071     LOG (GNUNET_ERROR_TYPE_WARNING,
3072          "Failed to create directory /tmp/rps/\n");
3073   }
3074
3075   size = (14 + strlen (GNUNET_i2s_full (&own_identity)) + 1) * sizeof (char);
3076   file_name_view_log = GNUNET_malloc (size);
3077   out_size = GNUNET_snprintf (file_name_view_log,
3078                               size,
3079                               "/tmp/rps/view-%s",
3080                               GNUNET_i2s_full (&own_identity));
3081   if (size < out_size ||
3082       0 > out_size)
3083   {
3084     LOG (GNUNET_ERROR_TYPE_WARNING,
3085          "Failed to write string to buffer (size: %i, out_size: %i)\n",
3086          size,
3087          out_size);
3088   }
3089
3090
3091   /* connect to NSE */
3092   nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
3093
3094
3095   alpha = 0.45;
3096   beta  = 0.45;
3097
3098   peer_map = GNUNET_CONTAINER_multipeermap_create (sampler_size_est_need, GNUNET_NO);
3099
3100
3101   /* Initialise cadet */
3102   static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
3103     {&handle_peer_push        , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH        ,
3104       sizeof (struct GNUNET_MessageHeader)},
3105     {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
3106       sizeof (struct GNUNET_MessageHeader)},
3107     {&handle_peer_pull_reply  , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY  , 0},
3108     {NULL, 0, 0}
3109   };
3110
3111   const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
3112   cadet_handle = GNUNET_CADET_connect (cfg,
3113                                        cls,
3114                                        &handle_inbound_channel,
3115                                        &cleanup_channel,
3116                                        cadet_handlers,
3117                                        ports);
3118
3119   peerinfo_handle = GNUNET_PEERINFO_connect (cfg);
3120
3121   /* Initialise sampler */
3122   struct GNUNET_TIME_Relative half_round_interval;
3123   struct GNUNET_TIME_Relative  max_round_interval;
3124
3125   half_round_interval = GNUNET_TIME_relative_multiply (round_interval, .5);
3126   max_round_interval = GNUNET_TIME_relative_add (round_interval, half_round_interval);
3127
3128   prot_sampler =   RPS_sampler_init     (sampler_size_est_need, max_round_interval);
3129   client_sampler = RPS_sampler_mod_init (sampler_size_est_need, max_round_interval);
3130
3131   /* Initialise push and pull maps */
3132   push_list = NULL;
3133   push_list_size = 0;
3134   pull_list = NULL;
3135   pull_list_size = 0;
3136
3137
3138   num_hist_update_tasks = 0;
3139
3140
3141   LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
3142   GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, NULL);
3143   // TODO send push/pull to each of those peers?
3144
3145   peerinfo_notify_handle = GNUNET_PEERINFO_notify (cfg,
3146                                                    GNUNET_NO,
3147                                                    process_peerinfo_peers,
3148                                                    NULL);
3149
3150   rps_start (server);
3151 }
3152
3153
3154 /**
3155  * The main function for the rps service.
3156  *
3157  * @param argc number of arguments from the command line
3158  * @param argv command line arguments
3159  * @return 0 ok, 1 on error
3160  */
3161   int
3162 main (int argc, char *const *argv)
3163 {
3164   return (GNUNET_OK ==
3165           GNUNET_SERVICE_run (argc,
3166                               argv,
3167                               "rps",
3168                               GNUNET_SERVICE_OPTION_NONE,
3169                               &run, NULL)) ? 0 : 1;
3170 }
3171
3172 /* end of gnunet-service-rps.c */