4ff5ca5e671ed32bd177e09b33569db9e4d51ca3
[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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, 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_nse_service.h"
30 #include "rps.h"
31
32 #include "gnunet-service-rps_sampler.h"
33
34 #include <math.h>
35 #include <inttypes.h>
36
37 #define LOG(kind, ...) GNUNET_log(kind, __VA_ARGS__)
38
39 // TODO modify @brief in every file
40
41 // TODO check for overflows
42
43 // TODO align message structs
44
45 // (TODO api -- possibility of getting weak random peer immideately)
46
47 // TODO malicious peer
48
49 // TODO connect to friends
50
51 // TODO store peers somewhere
52
53 // TODO ignore list?
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  * Struct used to store the context of a connected client.
79  */
80 struct client_ctx
81 {
82   /**
83    * The message queue to communicate with the client.
84    */
85   struct GNUNET_MQ_Handle *mq;
86 };
87
88 /**
89  * Used to keep track in what lists single peerIDs are.
90  */
91 enum PeerFlags
92 {
93   PULL_REPLY_PENDING   = 0x01,
94   IN_OTHER_GOSSIP_LIST = 0x02, // unneeded?
95   IN_OWN_SAMPLER_LIST  = 0x04, // unneeded?
96   IN_OWN_GOSSIP_LIST   = 0x08, // unneeded?
97
98   /**
99    * We set this bit when we can be sure the other peer is/was live.
100    */
101   VALID                = 0x10
102 };
103
104
105 /**
106  * Functions of this type can be used to be stored at a peer for later execution.
107  */
108 typedef void (* PeerOp) (void *cls, const struct GNUNET_PeerIdentity *peer);
109
110 /**
111  * Outstanding operation on peer consisting of callback and closure
112  */
113 struct PeerOutstandingOp
114 {
115   /**
116    * Callback
117    */
118   PeerOp op;
119
120   /**
121    * Closure
122    */
123   void *op_cls;
124 };
125
126
127 /**
128  * Struct used to keep track of other peer's status
129  *
130  * This is stored in a multipeermap.
131  */
132 struct PeerContext
133 {
134   /**
135    * In own gossip/sampler list, in other's gossip/sampler list
136    */
137   uint32_t peer_flags;
138
139   /**
140    * Message queue open to client
141    */
142   struct GNUNET_MQ_Handle *mq;
143
144   /**
145    * Channel open to client.
146    */
147   struct GNUNET_CADET_Channel *send_channel;
148
149   /**
150    * Channel open from client.
151    */
152   struct GNUNET_CADET_Channel *recv_channel; // unneeded?
153
154   /**
155    * Array of outstanding operations on this peer.
156    */
157   struct PeerOutstandingOp *outstanding_ops;
158
159   /**
160    * Number of outstanding operations.
161    */
162   unsigned int num_outstanding_ops;
163   //size_t num_outstanding_ops;
164
165   /**
166    * Handle to the callback given to cadet_ntfy_tmt_rdy()
167    *
168    * To be canceled on shutdown.
169    */
170   struct GNUNET_CADET_TransmitHandle *is_live_task;
171
172   /**
173    * Identity of the peer
174    */
175   struct GNUNET_PeerIdentity peer_id;
176
177   /**
178    * This is pobably followed by 'statistical' data (when we first saw
179    * him, how did we get his ID, how many pushes (in a timeinterval),
180    * ...)
181    */
182 };
183
184 /***********************************************************************
185  * /Housekeeping with peers
186 ***********************************************************************/
187
188
189
190
191
192 /***********************************************************************
193  * Globals
194 ***********************************************************************/
195
196 /**
197  * Sampler used for the Brahms protocol itself.
198  */
199 static struct RPS_Sampler *prot_sampler;
200
201 /**
202  * Sampler used for the clients.
203  */
204 static struct RPS_Sampler *client_sampler;
205
206 /**
207  * Set of all peers to keep track of them.
208  */
209 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
210
211
212 /**
213  * The gossiped list of peers.
214  */
215 static struct GNUNET_PeerIdentity *gossip_list;
216
217 /**
218  * Size of the gossiped list
219  */
220 //static unsigned int gossip_list_size;
221 static uint32_t gossip_list_size;
222
223
224 /**
225  * The size of sampler we need to be able to satisfy the client's need of
226  * random peers.
227  */
228 static unsigned int sampler_size_client_need;
229
230 /**
231  * The size of sampler we need to be able to satisfy the Brahms protocol's
232  * need of random peers.
233  *
234  * This is directly taken as the #gossip_list_size on update of the
235  * #gossip_list
236  *
237  * This is one minimum size the sampler grows to.
238  */
239 static unsigned int sampler_size_est_need;
240
241
242 /**
243  * Percentage of total peer number in the gossip list
244  * to send random PUSHes to
245  */
246 static float alpha;
247
248 /**
249  * Percentage of total peer number in the gossip list
250  * to send random PULLs to
251  */
252 static float beta;
253
254 /**
255  * The percentage gamma of history updates.
256  * Simply 1 - alpha - beta
257  */
258
259
260 /**
261  * Identifier for the main task that runs periodically.
262  */
263 static struct GNUNET_SCHEDULER_Task *do_round_task;
264
265 /**
266  * Time inverval the do_round task runs in.
267  */
268 static struct GNUNET_TIME_Relative round_interval;
269
270
271
272 /**
273  * List to store peers received through pushes temporary.
274  *
275  * TODO -> multipeermap
276  */
277 static struct GNUNET_PeerIdentity *push_list;
278
279 /**
280  * Size of the push_list;
281  */
282 static unsigned int push_list_size;
283 //size_t push_list_size;
284
285 /**
286  * List to store peers received through pulls temporary.
287  *
288  * TODO -> multipeermap
289  */
290 static struct GNUNET_PeerIdentity *pull_list;
291
292 /**
293  * Size of the pull_list;
294  */
295 static unsigned int pull_list_size;
296 //size_t pull_list_size;
297
298
299 /**
300  * Handler to NSE.
301  */
302 static struct GNUNET_NSE_Handle *nse;
303
304 /**
305  * Handler to CADET.
306  */
307 static struct GNUNET_CADET_Handle *cadet_handle;
308
309
310 /**
311  * Request counter.
312  *
313  * Only needed in the beginning to check how many of the 64 deltas
314  * we already have
315  */
316 static unsigned int req_counter;
317
318 /**
319  * Time of the last request we received.
320  *
321  * Used to compute the expected request rate.
322  */
323 static struct GNUNET_TIME_Absolute last_request;
324
325 /**
326  * Size of #request_deltas.
327  */
328 #define REQUEST_DELTAS_SIZE 64
329 static unsigned int request_deltas_size = REQUEST_DELTAS_SIZE;
330
331 /**
332  * Last 64 deltas between requests
333  */
334 static struct GNUNET_TIME_Relative request_deltas[REQUEST_DELTAS_SIZE];
335
336 /**
337  * The prediction of the rate of requests
338  */
339 static struct GNUNET_TIME_Relative  request_rate;
340
341
342 /**
343  * List with the peers we sent requests to.
344  */
345 struct GNUNET_PeerIdentity *pending_pull_reply_list;
346
347 /**
348  * Size of #pending_pull_reply_list.
349  */
350 uint32_t pending_pull_reply_list_size;
351
352
353 /**
354  * Number of history update tasks.
355  */
356 uint32_t num_hist_update_tasks;
357
358
359 #if ENABLE_MALICIOUS
360 /**
361  * Type of malicious peer
362  *
363  * 0 Don't act malicious at all - Default
364  * 1 Try to maximise representation
365  * 2 Try to partition the network
366  */
367 uint32_t mal_type = 0;
368
369 /**
370  * Other malicious peers
371  */
372 static struct GNUNET_PeerIdentity *mal_peers;
373
374 /**
375  * Number of other malicious peers
376  */
377 static uint32_t num_mal_peers;
378
379 /**
380  * If type is 2 this is the attacked peer
381  */
382 struct struct GNUNET_PeerIdentity attacked_peer;
383 #endif /* ENABLE_MALICIOUS */
384
385
386 /***********************************************************************
387  * /Globals
388 ***********************************************************************/
389
390
391
392
393
394
395 /***********************************************************************
396  * Util functions
397 ***********************************************************************/
398
399 /**
400  * Set a peer flag of given peer context.
401  */
402 #define set_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags |= mask)
403
404 /**
405  * Get peer flag of given peer context.
406  */
407 #define get_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags & mask ? GNUNET_YES : GNUNET_NO)
408
409 /**
410  * Unset flag of given peer context.
411  */
412 #define unset_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags &= (~mask))
413
414
415 /**
416  * Clean the send channel of a peer
417  */
418 void
419 peer_clean (const struct GNUNET_PeerIdentity *peer);
420
421
422 /**
423  * Check if peer is already in peer array.
424  */
425   int
426 in_arr (const struct GNUNET_PeerIdentity *array,
427         unsigned int arr_size,
428         const struct GNUNET_PeerIdentity *peer)
429 {
430   GNUNET_assert (NULL != peer);
431
432   if (0 == arr_size)
433     return GNUNET_NO;
434
435   GNUNET_assert (NULL != array);
436
437   unsigned int i;
438
439   for (i = 0; i < arr_size ; i++)
440     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&array[i], peer))
441       return GNUNET_YES;
442   return GNUNET_NO;
443 }
444
445
446 /**
447  * Print peerlist to log.
448  */
449 void
450 print_peer_list (struct GNUNET_PeerIdentity *list, unsigned int len)
451 {
452   unsigned int i;
453
454   LOG (GNUNET_ERROR_TYPE_DEBUG,
455        "Printing peer list of length %u at %p:\n",
456        len,
457        list);
458   for (i = 0 ; i < len ; i++)
459   {
460     LOG (GNUNET_ERROR_TYPE_DEBUG,
461          "%u. peer: %s\n",
462          i, GNUNET_i2s (&list[i]));
463   }
464 }
465
466
467 /**
468  * Remove peer from list.
469  */
470   void
471 rem_from_list (struct GNUNET_PeerIdentity **peer_list,
472                unsigned int *list_size,
473                const struct GNUNET_PeerIdentity *peer)
474 {
475   unsigned int i;
476   struct GNUNET_PeerIdentity *tmp;
477
478   tmp = *peer_list;
479
480   LOG (GNUNET_ERROR_TYPE_DEBUG,
481        "Removing peer %s from list at %p\n",
482        GNUNET_i2s (peer),
483        tmp);
484
485   for ( i = 0 ; i < *list_size ; i++ )
486   {
487     if (0 == GNUNET_CRYPTO_cmp_peer_identity (&tmp[i], peer))
488     {
489       if (i < *list_size -1)
490       { /* Not at the last entry -- shift peers left */
491         memcpy (&tmp[i], &tmp[i +1],
492                 ((*list_size) - i -1) * sizeof (struct GNUNET_PeerIdentity));
493       }
494       /* Remove last entry (should be now useless PeerID) */
495       GNUNET_array_grow (tmp, *list_size, (*list_size) -1);
496     }
497   }
498   *peer_list = tmp;
499 }
500
501 /**
502  * Get random peer from the given list but don't return one from the @a ignore_list.
503  */
504   struct GNUNET_PeerIdentity *
505 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list,
506                            uint32_t list_size,
507                            const struct GNUNET_PeerIdentity *ignore_list,
508                            uint32_t ignore_size)
509 {
510   uint32_t r_index;
511   uint32_t tmp_size;
512   struct GNUNET_PeerIdentity *tmp_peer_list;
513   struct GNUNET_PeerIdentity *peer;
514
515   GNUNET_assert (NULL != peer_list);
516   if (0 == list_size)
517     return NULL;
518
519   tmp_size = 0;
520   tmp_peer_list = NULL;
521   GNUNET_array_grow (tmp_peer_list, tmp_size, list_size);
522   memcpy (tmp_peer_list,
523           peer_list,
524           list_size * sizeof (struct GNUNET_PeerIdentity));
525   peer = GNUNET_new (struct GNUNET_PeerIdentity);
526
527   /**;
528    * Choose the r_index of the peer we want to return
529    * at random from the interval of the gossip list
530    */
531   r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
532                                       tmp_size);
533   *peer = tmp_peer_list[r_index];
534
535   while (in_arr (ignore_list, ignore_size, peer))
536   {
537     rem_from_list (&tmp_peer_list, &tmp_size, peer);
538
539     print_peer_list (tmp_peer_list, tmp_size);
540
541     if (0 == tmp_size)
542     {
543       GNUNET_free (peer);
544       return NULL;
545     }
546
547     /**;
548      * Choose the r_index of the peer we want to return
549      * at random from the interval of the gossip list
550      */
551     r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
552                                         tmp_size);
553     *peer = tmp_peer_list[r_index];
554   }
555
556
557   GNUNET_array_grow (tmp_peer_list, tmp_size, 0);
558
559   return peer;
560 }
561
562
563 /**
564  * Get the context of a peer. If not existing, create.
565  */
566   struct PeerContext *
567 get_peer_ctx (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
568               const struct GNUNET_PeerIdentity *peer)
569 {
570   struct PeerContext *ctx;
571
572   if ( GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
573   {
574     ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
575   }
576   else
577   {
578     ctx = GNUNET_new (struct PeerContext);
579     ctx->peer_flags = 0;
580     ctx->mq = NULL;
581     ctx->send_channel = NULL;
582     ctx->recv_channel = NULL;
583     ctx->outstanding_ops = NULL;
584     ctx->num_outstanding_ops = 0;
585     ctx->is_live_task = NULL;
586     ctx->peer_id = *peer;
587     (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
588                                               GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
589   }
590   return ctx;
591 }
592
593
594 /**
595  * Put random peer from sampler into the gossip list as history update.
596  */
597   void
598 hist_update (void *cls, struct GNUNET_PeerIdentity *ids, uint32_t num_peers)
599 {
600   GNUNET_assert (1 == num_peers);
601
602   if (gossip_list_size < sampler_size_est_need)
603     GNUNET_array_append (gossip_list, gossip_list_size, *ids);
604
605   if (0 < num_hist_update_tasks)
606     num_hist_update_tasks--;
607 }
608
609
610 /**
611  * Set the peer flag to living and call the outstanding operations on this peer.
612  */
613 static size_t
614 peer_is_live (struct PeerContext *peer_ctx)
615 {
616   struct GNUNET_PeerIdentity *peer;
617
618   peer = &peer_ctx->peer_id;
619   set_peer_flag (peer_ctx, VALID);
620
621   LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer %s is live\n", GNUNET_i2s (peer));
622
623   if (0 != peer_ctx->num_outstanding_ops)
624   { /* Call outstanding operations */
625     unsigned int i;
626
627     for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
628       peer_ctx->outstanding_ops[i].op (peer_ctx->outstanding_ops[i].op_cls, peer);
629     GNUNET_array_grow (peer_ctx->outstanding_ops, peer_ctx->num_outstanding_ops, 0);
630   }
631
632   return 0;
633 }
634
635
636 /**
637  * Callback that is called when a channel was effectively established.
638  * This is given to ntfy_tmt_rdy and called when the channel was
639  * successfully established.
640  */
641 static size_t
642 cadet_ntfy_tmt_rdy_cb (void *cls, size_t size, void *buf)
643 {
644   struct PeerContext *peer_ctx = (struct PeerContext *) cls;
645
646   if (NULL != buf
647       && 0 != size)
648     peer_is_live (peer_ctx);
649
650   //if (NULL != peer_ctx->is_live_task)
651   //{
652   //  LOG (GNUNET_ERROR_TYPE_DEBUG,
653   //       "Trying to cancle is_live_task for peer %s\n",
654   //       GNUNET_i2s (&peer_ctx->peer_id));
655   //  GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
656   //  peer_ctx->is_live_task = NULL;
657   //}
658   peer_ctx->is_live_task = NULL;
659
660   return 0;
661 }
662
663
664 /**
665  * Get the channel of a peer. If not existing, create.
666  */
667   struct GNUNET_CADET_Channel *
668 get_channel (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
669              const struct GNUNET_PeerIdentity *peer)
670 {
671   struct PeerContext *ctx;
672
673   LOG (GNUNET_ERROR_TYPE_DEBUG,
674        "Trying to establish channel to peer %s\n",
675        GNUNET_i2s (peer));
676
677   ctx = get_peer_ctx (peer_map, peer);
678   if (NULL == ctx->send_channel)
679   {
680     ctx->send_channel = GNUNET_CADET_channel_create (cadet_handle,
681                                                      NULL,
682                                                      peer,
683                                                      GNUNET_RPS_CADET_PORT,
684                                                      GNUNET_CADET_OPTION_RELIABLE);
685
686     /* If we don't know whether peer is live,
687      * get notified when we know it is live. */
688     if (GNUNET_YES != get_peer_flag (ctx, VALID)
689         && NULL != ctx->is_live_task
690         && NULL == ctx->recv_channel
691         && NULL == ctx->is_live_task)
692     {
693       ctx->peer_id = *peer;
694       LOG (GNUNET_ERROR_TYPE_DEBUG,
695            "Get informed about peer %s getting live\n",
696            GNUNET_i2s (peer));
697       ctx->is_live_task =
698           GNUNET_CADET_notify_transmit_ready (ctx->send_channel,
699                                               GNUNET_NO,
700                                               GNUNET_TIME_UNIT_FOREVER_REL,
701                                               sizeof (struct GNUNET_MessageHeader),
702                                               cadet_ntfy_tmt_rdy_cb,
703                                               ctx);
704     }
705     // FIXME check whether this is NULL
706
707     // do I have to explicitly put it in the peer_map?
708     (void) GNUNET_CONTAINER_multipeermap_put
709       (peer_map,
710        peer,
711        ctx,
712        GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
713   }
714   return ctx->send_channel;
715 }
716
717
718 /**
719  * Get the message queue of a specific peer.
720  *
721  * If we already have a message queue open to this client,
722  * simply return it, otherways create one.
723  */
724   struct GNUNET_MQ_Handle *
725 get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
726         const struct GNUNET_PeerIdentity *peer_id)
727 {
728   struct PeerContext *peer_ctx;
729
730   peer_ctx = get_peer_ctx (peer_map, peer_id);
731
732   GNUNET_assert (NULL == peer_ctx->is_live_task);
733
734   if (NULL == peer_ctx->mq)
735   {
736     (void) get_channel (peer_map, peer_id);
737     peer_ctx->mq = GNUNET_CADET_mq_create (peer_ctx->send_channel);
738     //do I have to explicitly put it in the peer_map?
739     (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer_id, peer_ctx,
740                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
741   }
742   return peer_ctx->mq;
743 }
744
745
746 /**
747  * Sum all time relatives of an array.
748   */
749   struct GNUNET_TIME_Relative
750 T_relative_sum (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
751 {
752   struct GNUNET_TIME_Relative sum;
753   uint32_t i;
754
755   sum = GNUNET_TIME_UNIT_ZERO;
756   for ( i = 0 ; i < arr_size ; i++ )
757   {
758     sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
759   }
760   return sum;
761 }
762
763
764 /**
765  * Compute the average of given time relatives.
766  */
767   struct GNUNET_TIME_Relative
768 T_relative_avg (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
769 {
770   return GNUNET_TIME_relative_divide (T_relative_sum (rel_array, arr_size), arr_size);
771 }
772
773
774 /**
775  * Insert PeerID in #pull_list
776  *
777  * Called once we know a peer is live.
778  */
779   void
780 insert_in_pull_list (void *cls, const struct GNUNET_PeerIdentity *peer)
781 {
782   if (GNUNET_NO == in_arr (pull_list, pull_list_size, peer))
783     GNUNET_array_append (pull_list, pull_list_size, *peer);
784
785   peer_clean (peer);
786 }
787
788 /**
789  * Check whether #insert_in_pull_list was already scheduled
790  */
791   int
792 insert_in_pull_list_scheduled (const struct PeerContext *peer_ctx)
793 {
794   unsigned int i;
795
796   for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
797     if (insert_in_pull_list == peer_ctx->outstanding_ops[i].op)
798       return GNUNET_YES;
799   return GNUNET_NO;
800 }
801
802
803 /**
804  * Insert PeerID in #gossip_list
805  *
806  * Called once we know a peer is live.
807  */
808   void
809 insert_in_gossip_list (void *cls, const struct GNUNET_PeerIdentity *peer)
810 {
811   if (GNUNET_NO == in_arr (gossip_list, gossip_list_size, peer))
812     GNUNET_array_append (gossip_list, gossip_list_size, *peer);
813
814   (void) get_channel (peer_map, peer);
815 }
816
817 /**
818  * Check whether #insert_in_gossip_list was already scheduled
819  */
820   int
821 insert_in_gossip_list_scheduled (const struct PeerContext *peer_ctx)
822 {
823   unsigned int i;
824
825   for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
826     if (insert_in_gossip_list == peer_ctx->outstanding_ops[i].op)
827       return GNUNET_YES;
828   return GNUNET_NO;
829 }
830
831
832 /**
833  * Update sampler with given PeerID.
834  */
835   void
836 insert_in_sampler (void *cls, const struct GNUNET_PeerIdentity *peer)
837 {
838   LOG (GNUNET_ERROR_TYPE_DEBUG,
839        "Updating samplers with peer %s from insert_in_sampler()\n",
840        GNUNET_i2s (peer));
841   RPS_sampler_update (prot_sampler,   peer);
842   RPS_sampler_update (client_sampler, peer);
843 }
844
845
846 /**
847  * Check whether #insert_in_sampler was already scheduled
848  */
849 static int
850 insert_in_sampler_scheduled (const struct PeerContext *peer_ctx)
851 {
852   unsigned int i;
853
854   for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
855     if (insert_in_sampler== peer_ctx->outstanding_ops[i].op)
856       return GNUNET_YES;
857   return GNUNET_NO;
858 }
859
860
861 /**
862  * Wrapper around #RPS_sampler_resize()
863  *
864  * If we do not have enough sampler elements, double current sampler size
865  * If we have more than enough sampler elements, halv current sampler size
866  */
867 static void
868 resize_wrapper (struct RPS_Sampler *sampler, uint32_t new_size)
869 {
870   unsigned int sampler_size;
871
872   // TODO statistics
873   // TODO respect the min, max
874   sampler_size = RPS_sampler_get_size (sampler);
875   if (sampler_size > new_size * 4)
876   { /* Shrinking */
877     RPS_sampler_resize (sampler, sampler_size / 2);
878   }
879   else if (sampler_size < new_size)
880   { /* Growing */
881     RPS_sampler_resize (sampler, sampler_size * 2);
882   }
883   LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
884 }
885
886
887 /**
888  * Wrapper around #RPS_sampler_resize() resizing the client sampler
889  */
890 static void
891 client_resize_wrapper ()
892 {
893   uint32_t bigger_size;
894   unsigned int sampler_size;
895
896   // TODO statistics
897
898   sampler_size = RPS_sampler_get_size (client_sampler);
899
900   if (sampler_size_est_need > sampler_size_client_need)
901     bigger_size = sampler_size_est_need;
902   else
903     bigger_size = sampler_size_client_need;
904
905   // TODO respect the min, max
906   resize_wrapper (client_sampler, bigger_size);
907   LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
908 }
909
910
911 /**
912  * Estimate request rate
913  *
914  * Called every time we receive a request from the client.
915  */
916   void
917 est_request_rate()
918 {
919   struct GNUNET_TIME_Relative max_round_duration;
920
921   if (request_deltas_size > req_counter)
922     req_counter++;
923   if ( 1 < req_counter)
924   {
925     /* Shift last request deltas to the right */
926     memcpy (&request_deltas[1],
927         request_deltas,
928         (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
929
930     /* Add current delta to beginning */
931     request_deltas[0] =
932         GNUNET_TIME_absolute_get_difference (last_request,
933                                              GNUNET_TIME_absolute_get ());
934     request_rate = T_relative_avg (request_deltas, req_counter);
935
936     /* Compute the duration a round will maximally take */
937     max_round_duration =
938         GNUNET_TIME_relative_add (round_interval,
939                                   GNUNET_TIME_relative_divide (round_interval, 2));
940
941     /* Set the estimated size the sampler has to have to
942      * satisfy the current client request rate */
943     sampler_size_client_need =
944         max_round_duration.rel_value_us / request_rate.rel_value_us;
945
946     /* Resize the sampler */
947     client_resize_wrapper ();
948   }
949   last_request = GNUNET_TIME_absolute_get ();
950 }
951
952
953 /***********************************************************************
954  * /Util functions
955 ***********************************************************************/
956
957
958
959
960
961 /**
962  * Function called by NSE.
963  *
964  * Updates sizes of sampler list and gossip list and adapt those lists
965  * accordingly.
966  */
967   void
968 nse_callback (void *cls, struct GNUNET_TIME_Absolute timestamp,
969               double logestimate, double std_dev)
970 {
971   double estimate;
972   //double scale; // TODO this might go gloabal/config
973
974   LOG (GNUNET_ERROR_TYPE_DEBUG,
975        "Received a ns estimate - logest: %f, std_dev: %f (old_size: %u)\n",
976        logestimate, std_dev, RPS_sampler_get_size (prot_sampler));
977   //scale = .01;
978   estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
979   // GNUNET_NSE_log_estimate_to_n (logestimate);
980   estimate = pow (estimate, 1.0 / 3);
981   // TODO add if std_dev is a number
982   // estimate += (std_dev * scale);
983   if (2 < ceil (estimate))
984   {
985     LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
986     sampler_size_est_need = estimate;
987   } else
988     LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
989
990   /* If the NSE has changed adapt the lists accordingly */
991   resize_wrapper (prot_sampler, sampler_size_est_need);
992   client_resize_wrapper ();
993 }
994
995
996 /**
997  * Callback called once the requested PeerIDs are ready.
998  *
999  * Sends those to the requesting client.
1000  */
1001 void client_respond (void *cls,
1002     struct GNUNET_PeerIdentity *ids, uint32_t num_peers)
1003 {
1004   LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler returned %" PRIX32 " peers\n", num_peers);
1005   struct GNUNET_MQ_Envelope *ev;
1006   struct GNUNET_RPS_CS_ReplyMessage *out_msg;
1007   struct GNUNET_SERVER_Client *client;
1008   uint32_t size_needed;
1009   struct client_ctx *cli_ctx;
1010
1011   client = (struct GNUNET_SERVER_Client *) cls;
1012
1013   size_needed = sizeof (struct GNUNET_RPS_CS_ReplyMessage) +
1014                 num_peers * sizeof (struct GNUNET_PeerIdentity);
1015
1016   GNUNET_assert (GNUNET_SERVER_MAX_MESSAGE_SIZE >= size_needed);
1017
1018   ev = GNUNET_MQ_msg_extra (out_msg,
1019                             num_peers * sizeof (struct GNUNET_PeerIdentity),
1020                             GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
1021   out_msg->num_peers = htonl (num_peers);
1022
1023   memcpy (&out_msg[1],
1024       ids,
1025       num_peers * sizeof (struct GNUNET_PeerIdentity));
1026   GNUNET_free (ids);
1027
1028   cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct client_ctx);
1029   if ( NULL == cli_ctx ) {
1030     cli_ctx = GNUNET_new (struct client_ctx);
1031     cli_ctx->mq = GNUNET_MQ_queue_for_server_client (client);
1032     GNUNET_SERVER_client_set_user_context (client, cli_ctx);
1033   }
1034
1035   GNUNET_MQ_send (cli_ctx->mq, ev);
1036 }
1037
1038
1039 /**
1040  * Handle RPS request from the client.
1041  *
1042  * @param cls closure
1043  * @param client identification of the client
1044  * @param message the actual message
1045  */
1046 static void
1047 handle_client_request (void *cls,
1048             struct GNUNET_SERVER_Client *client,
1049             const struct GNUNET_MessageHeader *message)
1050 {
1051   struct GNUNET_RPS_CS_RequestMessage *msg;
1052   uint32_t num_peers;
1053   uint32_t size_needed;
1054   uint32_t i;
1055
1056   msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
1057
1058   num_peers = ntohl (msg->num_peers);
1059   size_needed = sizeof (struct GNUNET_RPS_CS_ReplyMessage) +
1060                 num_peers * sizeof (struct GNUNET_PeerIdentity);
1061
1062   if (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
1063   {
1064     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1065     return;
1066   }
1067
1068   for (i = 0 ; i < num_peers ; i++)
1069     est_request_rate();
1070
1071   LOG (GNUNET_ERROR_TYPE_DEBUG, "Client requested %" PRIX32 " random peer(s).\n", num_peers);
1072
1073   RPS_sampler_get_n_rand_peers (client_sampler, client_respond,
1074                                 client, num_peers, GNUNET_YES);
1075
1076   GNUNET_SERVER_receive_done (client,
1077                               GNUNET_OK);
1078 }
1079
1080
1081 /**
1082  * Handle seed from the client.
1083  *
1084  * @param cls closure
1085  * @param client identification of the client
1086  * @param message the actual message
1087  */
1088   static void
1089 handle_client_seed (void *cls,
1090             struct GNUNET_SERVER_Client *client,
1091             const struct GNUNET_MessageHeader *message)
1092 {
1093   struct GNUNET_RPS_CS_SeedMessage *in_msg;
1094   struct GNUNET_PeerIdentity *peers;
1095   uint32_t num_peers;
1096   uint32_t i;
1097
1098   if (sizeof (struct GNUNET_RPS_CS_SeedMessage) > ntohs (message->size))
1099   {
1100     GNUNET_break_op (0);
1101     GNUNET_SERVER_receive_done (client,
1102                                 GNUNET_SYSERR);
1103   }
1104
1105   in_msg = (struct GNUNET_RPS_CS_SeedMessage *) message;
1106   num_peers = ntohl (in_msg->num_peers);
1107   peers = (struct GNUNET_PeerIdentity *) &in_msg[1];
1108   //peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1109   //memcpy (peers, &in_msg[1], num_peers * sizeof (struct GNUNET_PeerIdentity));
1110
1111   if ((ntohs (message->size) - sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
1112       sizeof (struct GNUNET_PeerIdentity) != num_peers)
1113   {
1114     GNUNET_break_op (0);
1115     GNUNET_SERVER_receive_done (client,
1116                                 GNUNET_SYSERR);
1117   }
1118
1119   LOG (GNUNET_ERROR_TYPE_DEBUG,
1120        "Client seeded peers:\n");
1121   print_peer_list (peers, num_peers);
1122
1123   // TODO check for validity of ids
1124
1125   for (i = 0 ; i < num_peers ; i++)
1126   {
1127     LOG (GNUNET_ERROR_TYPE_DEBUG,
1128          "Updating samplers with seed %" PRIX32 ": %s\n",
1129          i,
1130          GNUNET_i2s (&peers[i]));
1131
1132     RPS_sampler_update (prot_sampler,   &peers[i]);
1133     RPS_sampler_update (client_sampler, &peers[i]);
1134   }
1135
1136   //GNUNET_free (peers);
1137
1138   GNUNET_SERVER_receive_done (client,
1139                                                 GNUNET_OK);
1140 }
1141
1142
1143 /**
1144  * Handle a PUSH message from another peer.
1145  *
1146  * Check the proof of work and store the PeerID
1147  * in the temporary list for pushed PeerIDs.
1148  *
1149  * @param cls Closure
1150  * @param channel The channel the PUSH was received over
1151  * @param channel_ctx The context associated with this channel
1152  * @param msg The message header
1153  */
1154 static int
1155 handle_peer_push (void *cls,
1156     struct GNUNET_CADET_Channel *channel,
1157     void **channel_ctx,
1158     const struct GNUNET_MessageHeader *msg)
1159 {
1160   const struct GNUNET_PeerIdentity *peer;
1161
1162   // (check the proof of work)
1163
1164   peer = (const struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
1165   // FIXME wait for cadet to change this function
1166   LOG (GNUNET_ERROR_TYPE_DEBUG, "PUSH received (%s)\n", GNUNET_i2s (peer));
1167
1168   /* Add the sending peer to the push_list */
1169   if (GNUNET_NO == in_arr (push_list, push_list_size, peer))
1170     GNUNET_array_append (push_list, push_list_size, *peer);
1171
1172   return GNUNET_OK;
1173 }
1174
1175 /**
1176  * Handle PULL REQUEST request message from another peer.
1177  *
1178  * Reply with the gossip list of PeerIDs.
1179  *
1180  * @param cls Closure
1181  * @param channel The channel the PUSH was received over
1182  * @param channel_ctx The context associated with this channel
1183  * @param msg The message header
1184  */
1185 static int
1186 handle_peer_pull_request (void *cls,
1187     struct GNUNET_CADET_Channel *channel,
1188     void **channel_ctx,
1189     const struct GNUNET_MessageHeader *msg)
1190 {
1191   struct GNUNET_PeerIdentity *peer;
1192   uint32_t send_size;
1193   struct GNUNET_MQ_Handle *mq;
1194   struct GNUNET_MQ_Envelope *ev;
1195   struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
1196
1197
1198   peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (channel,
1199                                                                        GNUNET_CADET_OPTION_PEER);
1200   // FIXME wait for cadet to change this function
1201
1202   /* Compute actual size */
1203   send_size = sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) +
1204               gossip_list_size * sizeof (struct GNUNET_PeerIdentity);
1205
1206   if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size)
1207     /* Compute number of peers to send
1208      * If too long, simply truncate */
1209     send_size =
1210       (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE -
1211        sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1212        sizeof (struct GNUNET_PeerIdentity);
1213   else
1214     send_size = gossip_list_size;
1215
1216   LOG (GNUNET_ERROR_TYPE_DEBUG,
1217       "PULL REQUEST from peer %s received, going to send %u peers\n",
1218       GNUNET_i2s (peer), send_size);
1219
1220   mq = get_mq (peer_map, peer);
1221
1222   ev = GNUNET_MQ_msg_extra (out_msg,
1223                            send_size * sizeof (struct GNUNET_PeerIdentity),
1224                            GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
1225   //out_msg->num_peers = htonl (gossip_list_size);
1226   out_msg->num_peers = htonl (send_size);
1227   memcpy (&out_msg[1], gossip_list,
1228          send_size * sizeof (struct GNUNET_PeerIdentity));
1229
1230   GNUNET_MQ_send (mq, ev);
1231
1232   return GNUNET_OK;
1233 }
1234
1235
1236 /**
1237  * Handle PULL REPLY message from another peer.
1238  *
1239  * Check whether we sent a corresponding request and
1240  * whether this reply is the first one.
1241  *
1242  * @param cls Closure
1243  * @param channel The channel the PUSH was received over
1244  * @param channel_ctx The context associated with this channel
1245  * @param msg The message header
1246  */
1247   static int
1248 handle_peer_pull_reply (void *cls,
1249                         struct GNUNET_CADET_Channel *channel,
1250                         void **channel_ctx,
1251                         const struct GNUNET_MessageHeader *msg)
1252 {
1253   LOG (GNUNET_ERROR_TYPE_DEBUG, "PULL REPLY received\n");
1254
1255   struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
1256   struct GNUNET_PeerIdentity *peers;
1257   struct PeerContext *peer_ctx;
1258   struct GNUNET_PeerIdentity *sender;
1259   struct PeerContext *sender_ctx;
1260   struct PeerOutstandingOp out_op;
1261   uint32_t i;
1262
1263   /* Check for protocol violation */
1264   if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->size))
1265   {
1266     GNUNET_break_op (0);
1267     return GNUNET_SYSERR;
1268   }
1269
1270   in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
1271   if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1272       sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1273   {
1274     LOG (GNUNET_ERROR_TYPE_ERROR,
1275         "message says it sends %" PRIu64 " peers, have space for %i peers\n",
1276         ntohl (in_msg->num_peers),
1277         (ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1278             sizeof (struct GNUNET_PeerIdentity));
1279     GNUNET_break_op (0);
1280     return GNUNET_SYSERR;
1281   }
1282
1283   sender = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
1284       (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
1285        // Guess simply casting isn't the nicest way...
1286        // FIXME wait for cadet to change this function
1287   sender_ctx = get_peer_ctx (peer_map, sender);
1288
1289   if (GNUNET_YES == get_peer_flag (sender_ctx, PULL_REPLY_PENDING))
1290   {
1291     GNUNET_break_op (0);
1292     return GNUNET_OK;
1293   }
1294
1295   /* Do actual logic */
1296   peers = (struct GNUNET_PeerIdentity *) &msg[1];
1297   for (i = 0 ; i < ntohl (in_msg->num_peers) ; i++)
1298   {
1299     peer_ctx = get_peer_ctx (peer_map, &peers[i]);
1300     if (GNUNET_YES == get_peer_flag (peer_ctx, VALID)
1301         || NULL != peer_ctx->send_channel
1302         || NULL != peer_ctx->recv_channel)
1303     {
1304       if (GNUNET_NO == in_arr (pull_list, pull_list_size, &peers[i])
1305           && GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peers[i]))
1306         GNUNET_array_append (pull_list, pull_list_size, peers[i]);
1307     }
1308     else if (GNUNET_NO == insert_in_pull_list_scheduled (peer_ctx))
1309     {
1310       out_op.op = insert_in_pull_list;
1311       out_op.op_cls = NULL;
1312       GNUNET_array_append (peer_ctx->outstanding_ops,
1313                            peer_ctx->num_outstanding_ops,
1314                            out_op);
1315     }
1316   }
1317
1318   unset_peer_flag (sender_ctx, PULL_REPLY_PENDING);
1319   rem_from_list (&pending_pull_reply_list, &pending_pull_reply_list_size, sender);
1320
1321   return GNUNET_OK;
1322 }
1323
1324
1325 #if ENABLE_MALICIOUS
1326 /**
1327  * Turn RPS service to act malicious.
1328  *
1329  * @param cls Closure
1330  * @param channel The channel the PUSH was received over
1331  * @param channel_ctx The context associated with this channel
1332  * @param msg The message header
1333  */
1334   static int
1335 handle_peer_act_malicious (void *cls,
1336                            struct GNUNET_CADET_Channel *channel,
1337                            void **channel_ctx,
1338                            const struct GNUNET_MessageHeader *msg)
1339 {
1340   struct GNUNET_PeerIdentity *peers;
1341
1342   LOG (GNUNET_ERROR_TYPE_DEBUG, "PULL REPLY received\n");
1343
1344   /* Check for protocol violation */
1345   if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->size))
1346   {
1347     GNUNET_break_op (0);
1348     return GNUNET_SYSERR;
1349   }
1350
1351   in_msg = (struct GNUNET_RPS_CS_ActMaliciousMessage *) msg;
1352   if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
1353       sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1354   {
1355     LOG (GNUNET_ERROR_TYPE_ERROR,
1356         "message says it sends %" PRIu64 " peers, have space for %i peers\n",
1357         ntohl (in_msg->num_peers),
1358         (ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
1359             sizeof (struct GNUNET_PeerIdentity));
1360     GNUNET_break_op (0);
1361     return GNUNET_SYSERR;
1362   }
1363
1364   sender = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
1365       (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
1366        // Guess simply casting isn't the nicest way...
1367        // FIXME wait for cadet to change this function
1368   sender_ctx = get_peer_ctx (peer_map, sender);
1369
1370   if (GNUNET_YES == get_peer_flag (sender_ctx, PULL_REPLY_PENDING))
1371   {
1372     GNUNET_break_op (0);
1373     return GNUNET_OK;
1374   }
1375
1376   /* Do actual logic */
1377   peers = (struct GNUNET_PeerIdentity *) &msg[1];
1378   num_peers = ntohl (in_msg->num_peers);
1379   mal_type = ntohl (in_msg->type);
1380
1381   if (1 == mal_type)
1382   { /* Try to maximise representation */
1383     num_mal_peers = ntohl (in_msg->num_peers);
1384     mal_peers = GNUNET_new_array (num_mal_peers,
1385                                   struct GNUNET_PeerIdentity);
1386     memcpy (mal_peers, peers, num_mal_peers);
1387   }
1388   else if (2 == mal_type)
1389   { /* Try to partition the network */
1390     num_mal_peers = ntohl (in_msg->num_peers) - 1;
1391     mal_peers = GNUNET_new_array (num_mal_peers,
1392                                   struct GNUNET_PeerIdentity);
1393     memcpy (mal_peers, peers, num_mal_peers);
1394     attacked_peer = peers[num_mal_peers];
1395   }
1396 }
1397 #endif /* ENABLE_MALICIOUS */
1398
1399
1400 /**
1401  * Send out PUSHes and PULLs.
1402  *
1403  * This is executed regylary.
1404  */
1405 static void
1406 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1407 {
1408   LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round\n");
1409
1410   uint32_t i;
1411   unsigned int *permut;
1412   unsigned int n_peers; /* Number of peers we send pushes/pulls to */
1413   struct GNUNET_MQ_Envelope *ev;
1414   struct GNUNET_PeerIdentity peer;
1415   struct GNUNET_PeerIdentity *tmp_peer;
1416   struct GNUNET_MQ_Handle *mq;
1417
1418   LOG (GNUNET_ERROR_TYPE_DEBUG,
1419        "Printing gossip list:\n");
1420   for (i = 0 ; i < gossip_list_size ; i++)
1421     LOG (GNUNET_ERROR_TYPE_DEBUG,
1422          "\t%s\n", GNUNET_i2s (&gossip_list[i]));
1423   // TODO log lists, ...
1424
1425   /* Would it make sense to have one shuffeled gossip list and then
1426    * to send PUSHes to first alpha peers, PULL requests to next beta peers and
1427    * use the rest to update sampler?
1428    * in essence get random peers with consumption */
1429
1430   /* Send PUSHes */
1431   if (0 < gossip_list_size)
1432   {
1433     permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
1434                                            (unsigned int) gossip_list_size);
1435     n_peers = ceil (alpha * gossip_list_size);
1436     LOG (GNUNET_ERROR_TYPE_DEBUG,
1437          "Going to send pushes to %u ceil (%f * %u) peers.\n",
1438          n_peers, alpha, gossip_list_size);
1439     for (i = 0 ; i < n_peers ; i++)
1440     {
1441       peer = gossip_list[permut[i]];
1442       if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer)) // TODO
1443       { // FIXME if this fails schedule/loop this for later
1444         LOG (GNUNET_ERROR_TYPE_DEBUG,
1445              "Sending PUSH to peer %s of gossiped list.\n",
1446              GNUNET_i2s (&peer));
1447
1448         ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
1449         mq = get_mq (peer_map, &peer);
1450         GNUNET_MQ_send (mq, ev);
1451       }
1452     }
1453     GNUNET_free (permut);
1454   }
1455
1456
1457   /* Send PULL requests */
1458   //permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list->size);
1459   n_peers = ceil (beta * gossip_list_size);
1460   LOG (GNUNET_ERROR_TYPE_DEBUG,
1461        "Going to send pulls to %u ceil (%f * %u) peers.\n",
1462        n_peers, beta, gossip_list_size);
1463   for (i = 0 ; i < n_peers ; i++)
1464   {
1465     tmp_peer = get_rand_peer_ignore_list (gossip_list, gossip_list_size,
1466         pending_pull_reply_list, pending_pull_reply_list_size);
1467     if (NULL != tmp_peer)
1468     {
1469       peer = *tmp_peer;
1470       GNUNET_free (tmp_peer);
1471
1472       GNUNET_array_append (pending_pull_reply_list, pending_pull_reply_list_size, peer);
1473
1474       if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer))
1475       { // FIXME if this fails schedule/loop this for later
1476         LOG (GNUNET_ERROR_TYPE_DEBUG,
1477              "Sending PULL request to peer %s of gossiped list.\n",
1478              GNUNET_i2s (&peer));
1479
1480         ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
1481         mq = get_mq (peer_map, &peer);
1482         GNUNET_MQ_send (mq, ev);
1483       }
1484     }
1485   }
1486
1487
1488   /* Update gossip list */
1489
1490   if ( push_list_size <= alpha * gossip_list_size &&
1491        push_list_size != 0 &&
1492        pull_list_size != 0 )
1493   {
1494     LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the gossip list.\n");
1495
1496     uint32_t first_border;
1497     uint32_t second_border;
1498     uint32_t r_index;
1499     uint32_t peers_to_clean_size;
1500     struct GNUNET_PeerIdentity *peers_to_clean;
1501
1502     peers_to_clean = NULL;
1503     peers_to_clean_size = 0;
1504     GNUNET_array_grow (peers_to_clean, peers_to_clean_size, gossip_list_size);
1505     memcpy (peers_to_clean,
1506             gossip_list,
1507             gossip_list_size * sizeof (struct GNUNET_PeerIdentity));
1508
1509     first_border  =                ceil (alpha * sampler_size_est_need);
1510     second_border = first_border + ceil (beta  * sampler_size_est_need);
1511
1512     GNUNET_array_grow (gossip_list, gossip_list_size, second_border);
1513
1514     for (i = 0 ; i < first_border ; i++)
1515     { // TODO use RPS_sampler_get_n_rand_peers
1516       /* Update gossip list with peers received through PUSHes */
1517       r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
1518                                        push_list_size);
1519       gossip_list[i] = push_list[r_index];
1520       // TODO change the peer_flags accordingly
1521     }
1522
1523     for (i = first_border ; i < second_border ; i++)
1524     {
1525       /* Update gossip list with peers received through PULLs */
1526       r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
1527                                        pull_list_size);
1528       gossip_list[i] = pull_list[r_index];
1529       // TODO change the peer_flags accordingly
1530     }
1531
1532     for (i = second_border ; i < sampler_size_est_need ; i++)
1533     {
1534       /* Update gossip list with peers from history */
1535       RPS_sampler_get_n_rand_peers (prot_sampler, hist_update, NULL, 1, GNUNET_NO);
1536       num_hist_update_tasks++;
1537       // TODO change the peer_flags accordingly
1538     }
1539
1540     for (i = 0 ; i < gossip_list_size ; i++)
1541       rem_from_list (&peers_to_clean, &peers_to_clean_size, &gossip_list[i]);
1542
1543     for (i = 0 ; i < peers_to_clean_size ; i++)
1544       peer_clean (&peers_to_clean[i]);
1545
1546     GNUNET_free (peers_to_clean);
1547   }
1548   else
1549   {
1550     LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the gossip list.\n");
1551   }
1552   // TODO independent of that also get some peers from CADET_get_peers()?
1553
1554
1555   /* Update samplers */
1556   for ( i = 0 ; i < push_list_size ; i++ )
1557   {
1558     LOG (GNUNET_ERROR_TYPE_DEBUG,
1559          "Updating with peer %s from push list\n",
1560          GNUNET_i2s (&push_list[i]));
1561     RPS_sampler_update (prot_sampler,   &push_list[i]);
1562     RPS_sampler_update (client_sampler, &push_list[i]);
1563     // TODO set in_flag?
1564   }
1565
1566   for ( i = 0 ; i < pull_list_size ; i++ )
1567   {
1568     LOG (GNUNET_ERROR_TYPE_DEBUG,
1569          "Updating with peer %s from pull list\n",
1570          GNUNET_i2s (&pull_list[i]));
1571     RPS_sampler_update (prot_sampler,   &push_list[i]);
1572     RPS_sampler_update (client_sampler, &push_list[i]);
1573     // TODO set in_flag?
1574   }
1575
1576
1577   /* Empty push/pull lists */
1578   GNUNET_array_grow (push_list, push_list_size, 0);
1579   GNUNET_array_grow (pull_list, pull_list_size, 0);
1580
1581   struct GNUNET_TIME_Relative time_next_round;
1582   struct GNUNET_TIME_Relative half_round_interval;
1583   unsigned int rand_delay;
1584
1585
1586   /* Compute random time value between .5 * round_interval and 1.5 *round_interval */
1587   half_round_interval = GNUNET_TIME_relative_divide (round_interval, 2);
1588   do
1589   {
1590   /*
1591    * Compute random value between (0 and 1) * round_interval
1592    * via multiplying round_interval with a 'fraction' (0 to value)/value
1593    */
1594   rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT_MAX/10);
1595   time_next_round = GNUNET_TIME_relative_multiply (round_interval,  rand_delay);
1596   time_next_round = GNUNET_TIME_relative_divide   (time_next_round, UINT_MAX/10);
1597   time_next_round = GNUNET_TIME_relative_add      (time_next_round, half_round_interval);
1598   } while (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == time_next_round.rel_value_us);
1599
1600   /* Schedule next round */
1601   do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_round, NULL);
1602   LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
1603 }
1604
1605
1606 static void
1607 rps_start (struct GNUNET_SERVER_Handle *server);
1608
1609
1610 /**
1611  * This is called from GNUNET_CADET_get_peers().
1612  *
1613  * It is called on every peer(ID) that cadet somehow has contact with.
1614  * We use those to initialise the sampler.
1615  */
1616 void
1617 init_peer_cb (void *cls,
1618               const struct GNUNET_PeerIdentity *peer,
1619               int tunnel, // "Do we have a tunnel towards this peer?"
1620               unsigned int n_paths, // "Number of known paths towards this peer"
1621               unsigned int best_path) // "How long is the best path?
1622                                       // (0 = unknown, 1 = ourselves, 2 = neighbor)"
1623 {
1624   struct PeerOutstandingOp out_op;
1625   struct PeerContext *peer_ctx;
1626
1627   if (NULL != peer
1628       && 0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, peer))
1629   {
1630     LOG (GNUNET_ERROR_TYPE_DEBUG,
1631         "Got peer %s (at %p) from CADET (gossip_list_size: %u)\n",
1632         GNUNET_i2s (peer), peer, gossip_list_size);
1633
1634     // maybe create a function for that
1635     peer_ctx = get_peer_ctx (peer_map, peer);
1636     if (GNUNET_YES != get_peer_flag (peer_ctx, VALID))
1637     {
1638       if (GNUNET_NO == insert_in_sampler_scheduled (peer_ctx))
1639       {
1640         out_op.op = insert_in_sampler;
1641         out_op.op_cls = NULL;
1642         GNUNET_array_append (peer_ctx->outstanding_ops,
1643                              peer_ctx->num_outstanding_ops,
1644                              out_op);
1645       }
1646
1647       if (GNUNET_NO == insert_in_gossip_list_scheduled (peer_ctx))
1648       {
1649         out_op.op = insert_in_gossip_list;
1650         out_op.op_cls = NULL;
1651         GNUNET_array_append (peer_ctx->outstanding_ops,
1652                              peer_ctx->num_outstanding_ops,
1653                              out_op);
1654       }
1655
1656       /* Trigger livelyness test on peer */
1657       (void) get_channel (peer_map, peer);
1658     }
1659
1660     // send push/pull to each of those peers?
1661   }
1662 }
1663
1664
1665 /**
1666  * Clean the send channel of a peer
1667  */
1668 void
1669 peer_clean (const struct GNUNET_PeerIdentity *peer)
1670 {
1671   struct PeerContext *peer_ctx;
1672   struct GNUNET_CADET_Channel *channel;
1673
1674   if (GNUNET_YES != in_arr (gossip_list, gossip_list_size, peer)
1675       && GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
1676   {
1677     peer_ctx = get_peer_ctx (peer_map, peer);
1678     if (NULL != peer_ctx->send_channel)
1679     {
1680       channel = peer_ctx->send_channel;
1681       peer_ctx->send_channel = NULL;
1682       GNUNET_CADET_channel_destroy (channel);
1683     }
1684   }
1685 }
1686
1687
1688 /**
1689  * Callback used to remove peers from the multipeermap.
1690  */
1691   int
1692 peer_remove_cb (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1693 {
1694   struct PeerContext *peer_ctx;
1695   const struct GNUNET_CADET_Channel *channel =
1696     (const struct GNUNET_CADET_Channel *) cls;
1697   struct GNUNET_CADET_Channel *recv;
1698   struct GNUNET_CADET_Channel *send;
1699
1700   if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, value))
1701   {
1702     peer_ctx = (struct PeerContext *) value;
1703
1704     if (0 != peer_ctx->num_outstanding_ops)
1705       GNUNET_array_grow (peer_ctx->outstanding_ops,
1706                          peer_ctx->num_outstanding_ops,
1707                          0);
1708
1709     if (NULL != peer_ctx->mq)
1710       GNUNET_MQ_destroy (peer_ctx->mq);
1711
1712     if (NULL != peer_ctx->is_live_task)
1713     {
1714     LOG (GNUNET_ERROR_TYPE_DEBUG,
1715          "Trying to cancle is_live_task for peer %s\n",
1716          GNUNET_i2s (key));
1717       GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
1718       peer_ctx->is_live_task = NULL;
1719     }
1720
1721     send = peer_ctx->send_channel;
1722     peer_ctx->send_channel = NULL;
1723     if (NULL != send
1724         && channel != send)
1725     {
1726       GNUNET_CADET_channel_destroy (send);
1727     }
1728
1729     recv = peer_ctx->send_channel;
1730     peer_ctx->recv_channel = NULL;
1731     if (NULL != recv
1732         && channel != recv)
1733     {
1734       GNUNET_CADET_channel_destroy (recv);
1735     }
1736
1737     if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_remove_all (peer_map, key))
1738       LOG (GNUNET_ERROR_TYPE_WARNING, "removing peer from peer_map failed\n");
1739     else
1740       GNUNET_free (peer_ctx);
1741   }
1742
1743   return GNUNET_YES;
1744 }
1745
1746
1747 /**
1748  * Task run during shutdown.
1749  *
1750  * @param cls unused
1751  * @param tc unused
1752  */
1753 static void
1754 shutdown_task (void *cls,
1755                      const struct GNUNET_SCHEDULER_TaskContext *tc)
1756 {
1757   LOG (GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
1758
1759   if (NULL != do_round_task)
1760   {
1761     GNUNET_SCHEDULER_cancel (do_round_task);
1762     do_round_task = NULL;
1763   }
1764
1765
1766   {
1767   if (GNUNET_SYSERR ==
1768         GNUNET_CONTAINER_multipeermap_iterate (peer_map, peer_remove_cb, NULL))
1769     LOG (GNUNET_ERROR_TYPE_WARNING,
1770         "Iterating over peers to disconnect from them was cancelled\n");
1771   }
1772
1773   GNUNET_NSE_disconnect (nse);
1774   GNUNET_CADET_disconnect (cadet_handle);
1775   RPS_sampler_destroy (prot_sampler);
1776   RPS_sampler_destroy (client_sampler);
1777   LOG (GNUNET_ERROR_TYPE_DEBUG,
1778        "Size of the peermap: %u\n",
1779        GNUNET_CONTAINER_multipeermap_size (peer_map));
1780   GNUNET_break (0 == GNUNET_CONTAINER_multipeermap_size (peer_map));
1781   GNUNET_CONTAINER_multipeermap_destroy (peer_map);
1782   GNUNET_array_grow (gossip_list, gossip_list_size, 0);
1783   GNUNET_array_grow (push_list, push_list_size, 0);
1784   GNUNET_array_grow (pull_list, pull_list_size, 0);
1785 }
1786
1787
1788 /**
1789  * A client disconnected.  Remove all of its data structure entries.
1790  *
1791  * @param cls closure, NULL
1792  * @param client identification of the client
1793  */
1794 static void
1795 handle_client_disconnect (void *cls,
1796                           struct GNUNET_SERVER_Client * client)
1797 {
1798 }
1799
1800
1801 /**
1802  * Handle the channel a peer opens to us.
1803  *
1804  * @param cls The closure
1805  * @param channel The channel the peer wants to establish
1806  * @param initiator The peer's peer ID
1807  * @param port The port the channel is being established over
1808  * @param options Further options
1809  */
1810   static void *
1811 handle_inbound_channel (void *cls,
1812                         struct GNUNET_CADET_Channel *channel,
1813                         const struct GNUNET_PeerIdentity *initiator,
1814                         uint32_t port,
1815                         enum GNUNET_CADET_ChannelOption options)
1816 {
1817   struct PeerContext *peer_ctx;
1818   struct GNUNET_PeerIdentity peer;
1819
1820   peer = *initiator;
1821   LOG (GNUNET_ERROR_TYPE_DEBUG,
1822       "New channel was established to us (Peer %s).\n",
1823       GNUNET_i2s (&peer));
1824
1825   GNUNET_assert (NULL != channel);
1826
1827   // we might not even store the recv_channel
1828
1829   peer_ctx = get_peer_ctx (peer_map, &peer);
1830   // FIXME what do we do if a channel is established twice?
1831   //       overwrite? Clean old channel? ...?
1832   //if (NULL != peer_ctx->recv_channel)
1833   //{
1834   //  peer_ctx->recv_channel = channel;
1835   //}
1836   peer_ctx->recv_channel = channel;
1837
1838   peer_ctx->mq = NULL;
1839
1840   (void) GNUNET_CONTAINER_multipeermap_put (peer_map, &peer, peer_ctx,
1841       GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1842
1843   peer_is_live (peer_ctx);
1844
1845   return NULL; // TODO
1846 }
1847
1848
1849 /**
1850  * This is called when a remote peer destroys a channel.
1851  *
1852  * @param cls The closure
1853  * @param channel The channel being closed
1854  * @param channel_ctx The context associated with this channel
1855  */
1856   static void
1857 cleanup_channel (void *cls,
1858                 const struct GNUNET_CADET_Channel *channel,
1859                 void *channel_ctx)
1860 {
1861   struct GNUNET_PeerIdentity *peer;
1862   struct PeerContext *peer_ctx;
1863
1864   peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
1865       (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
1866        // Guess simply casting isn't the nicest way...
1867        // FIXME wait for cadet to change this function
1868   LOG (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up channel to peer %s\n",
1869        GNUNET_i2s (peer));
1870
1871   if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
1872   {
1873     peer_ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
1874
1875     if (NULL == peer_ctx) /* It could have been removed by shutdown_task */
1876       return;
1877
1878     if (channel == peer_ctx->send_channel)
1879     { /* Peer probably went down */
1880       rem_from_list (&gossip_list, &gossip_list_size, peer);
1881       rem_from_list (&pending_pull_reply_list, &pending_pull_reply_list_size, peer);
1882
1883       /* Somwewhat {ab,re}use the iterator function */
1884       /* Cast to void is ok, because it's used as void in peer_remove_cb */
1885       (void) peer_remove_cb ((void *) channel, peer, peer_ctx);
1886     }
1887     else
1888       peer_ctx->recv_channel = NULL;
1889   }
1890 }
1891
1892
1893 /**
1894  * Actually start the service.
1895  */
1896   static void
1897 rps_start (struct GNUNET_SERVER_Handle *server)
1898 {
1899   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1900     {&handle_client_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
1901       sizeof (struct GNUNET_RPS_CS_RequestMessage)},
1902     {&handle_client_seed,    NULL, GNUNET_MESSAGE_TYPE_RPS_CS_SEED, 0},
1903     {NULL, NULL, 0, 0}
1904   };
1905
1906   GNUNET_SERVER_add_handlers (server, handlers);
1907   GNUNET_SERVER_disconnect_notify (server,
1908                                    &handle_client_disconnect,
1909                                    NULL);
1910   LOG (GNUNET_ERROR_TYPE_DEBUG, "Ready to receive requests from clients\n");
1911
1912
1913   do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
1914   LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
1915
1916   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1917                                                         &shutdown_task,
1918                                                         NULL);
1919 }
1920
1921
1922 /**
1923  * Process statistics requests.
1924  *
1925  * @param cls closure
1926  * @param server the initialized server
1927  * @param c configuration to use
1928  */
1929   static void
1930 run (void *cls,
1931      struct GNUNET_SERVER_Handle *server,
1932      const struct GNUNET_CONFIGURATION_Handle *c)
1933 {
1934   // TODO check what this does -- copied from gnunet-boss
1935   // - seems to work as expected
1936   GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
1937   cfg = c;
1938
1939
1940   /* Get own ID */
1941   GNUNET_CRYPTO_get_peer_identity (cfg, &own_identity); // TODO check return value
1942   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1943               "STARTING SERVICE (rps) for peer [%s]\n",
1944               GNUNET_i2s (&own_identity));
1945
1946
1947   /* Get time interval from the configuration */
1948   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
1949                                                         "ROUNDINTERVAL",
1950                                                         &round_interval))
1951   {
1952     LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read ROUNDINTERVAL from config\n");
1953     GNUNET_SCHEDULER_shutdown ();
1954     return;
1955   }
1956
1957   /* Get initial size of sampler/gossip list from the configuration */
1958   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "RPS",
1959                                                          "INITSIZE",
1960                                                          (long long unsigned int *) &sampler_size_est_need))
1961   {
1962     LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read INITSIZE from config\n");
1963     GNUNET_SCHEDULER_shutdown ();
1964     return;
1965   }
1966   LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", sampler_size_est_need);
1967
1968
1969   gossip_list = NULL;
1970
1971
1972   /* connect to NSE */
1973   nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
1974   // TODO check whether that was successful
1975   LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to NSE\n");
1976
1977
1978   alpha = 0.45;
1979   beta  = 0.45;
1980
1981   peer_map = GNUNET_CONTAINER_multipeermap_create (sampler_size_est_need, GNUNET_NO);
1982
1983
1984   /* Initialise cadet */
1985   static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1986     {&handle_peer_push        , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH        ,
1987       sizeof (struct GNUNET_MessageHeader)},
1988     {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
1989       sizeof (struct GNUNET_MessageHeader)},
1990     {&handle_peer_pull_reply  , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY  , 0},
1991     #if ENABLE_MALICIOUS
1992     {&handle_peer_act_malicious, GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS , 0},
1993     #endif /* ENABLE_MALICIOUS */
1994     {NULL, 0, 0}
1995   };
1996
1997   const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
1998   cadet_handle = GNUNET_CADET_connect (cfg,
1999                                        cls,
2000                                        &handle_inbound_channel,
2001                                        &cleanup_channel,
2002                                        cadet_handlers,
2003                                        ports);
2004   LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to CADET\n");
2005
2006
2007   /* Initialise sampler */
2008   struct GNUNET_TIME_Relative half_round_interval;
2009   struct GNUNET_TIME_Relative  max_round_interval;
2010
2011   half_round_interval = GNUNET_TIME_relative_multiply (round_interval, .5);
2012   max_round_interval = GNUNET_TIME_relative_add (round_interval, half_round_interval);
2013
2014   prot_sampler =   RPS_sampler_init (sampler_size_est_need, max_round_interval);
2015   client_sampler = RPS_sampler_init (sampler_size_est_need, max_round_interval);
2016
2017   /* Initialise push and pull maps */
2018   push_list = NULL;
2019   push_list_size = 0;
2020   pull_list = NULL;
2021   pull_list_size = 0;
2022   pending_pull_reply_list = NULL;
2023   pending_pull_reply_list_size = 0;
2024
2025
2026   num_hist_update_tasks = 0;
2027
2028
2029   LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
2030   GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, NULL);
2031   // TODO send push/pull to each of those peers?
2032
2033
2034   rps_start (server);
2035 }
2036
2037
2038 /**
2039  * The main function for the rps service.
2040  *
2041  * @param argc number of arguments from the command line
2042  * @param argv command line arguments
2043  * @return 0 ok, 1 on error
2044  */
2045   int
2046 main (int argc, char *const *argv)
2047 {
2048   return (GNUNET_OK ==
2049           GNUNET_SERVICE_run (argc,
2050                               argv,
2051                               "rps",
2052                               GNUNET_SERVICE_OPTION_NONE,
2053                               &run, NULL)) ? 0 : 1;
2054 }
2055
2056 /* end of gnunet-service-rps.c */