fixed error
[oweals/gnunet.git] / src / rps / gnunet-service-rps.c
1 /*
2      This file is part of GNUnet.
3      (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 <math.h>
33 #include <inttypes.h>
34
35 #define LOG(kind, ...) GNUNET_log(kind, __VA_ARGS__)
36
37 // TODO modify @brief in every file
38
39 // TODO take care that messages are not longer than 64k
40
41 // TODO check for overflows
42
43 // TODO align message structs
44
45 // TODO multipeerlist indep of gossiped list
46
47 // (TODO api -- possibility of getting weak random peer immideately)
48
49 // TODO malicious peer
50
51 // TODO switch Slist -> DLL
52
53 /**
54  * Our configuration.
55  */
56 static const struct GNUNET_CONFIGURATION_Handle *cfg;
57
58 /**
59  * Our own identity.
60  */
61 struct GNUNET_PeerIdentity *own_identity;
62
63 /**
64  * Compare two peer identities. Taken from secretsharing.
65  *
66  * @param p1 Some peer identity.
67  * @param p2 Some peer identity.
68  * @return 1 if p1 > p2, -1 if p1 < p2 and 0 if p1 == p2.
69  */
70 static int
71 peer_id_cmp (const void *p1, const void *p2)
72 {
73   return memcmp (p1, p2, sizeof (struct GNUNET_PeerIdentity));
74 }
75
76 /***********************************************************************
77  * Sampler
78  *
79  * WARNING: This section needs to be reviewed regarding the use of
80  * functions providing (pseudo)randomness!
81 ***********************************************************************/
82
83 // TODO care about invalid input of the caller (size 0 or less...)
84
85 /**
86  * A sampler sampling PeerIDs.
87  */
88 struct Sampler
89 {
90   /**
91    * Min-wise linear permutation used by this sampler.
92    *
93    * This is an key later used by a hmac.
94    */
95   struct GNUNET_CRYPTO_AuthKey auth_key;
96
97   /**
98    * The PeerID this sampler currently samples.
99    */
100   struct GNUNET_PeerIdentity *peer_id;
101
102   /**
103    * The according hash value of this PeerID.
104    */
105   struct GNUNET_HashCode peer_id_hash;
106
107   /**
108    * Samplers are kept in a linked list.
109    */
110   struct Sampler *next;
111
112   /**
113    * Samplers are kept in a linked list.
114    */
115   struct Sampler *prev;
116
117 };
118
119 /**
120  * A n-tuple of samplers.
121  */
122 struct Samplers
123 {
124   /**
125    * Number of samplers we hold.
126    */
127   unsigned int size;
128   //size_t size;
129   
130   /**
131    * All PeerIDs in one array.
132    */
133   struct GNUNET_PeerIdentity *peer_ids;
134
135   /**
136    * The head of the DLL.
137    */
138   struct Sampler *head;
139
140   /**
141    * The tail of the DLL.
142    */
143   struct Sampler *tail;
144
145 };
146
147
148 typedef void (* SAMPLER_deleteCB) (void *cls, const struct GNUNET_PeerIdentity *id, struct GNUNET_HashCode hash);
149
150 /**
151  * (Re)Initialise given Sampler with random min-wise independent function.
152  *
153  * In this implementation this means choosing an auth_key for later use in
154  * a hmac at random.
155  *
156  * @param id pointer to the place where this sampler will store the PeerID.
157  *           This will be overwritten.
158  */
159   struct Sampler *
160 SAMPLER_init(struct GNUNET_PeerIdentity *id)
161 {
162   struct Sampler *s;
163   
164   s = GNUNET_new(struct Sampler);
165
166   // I guess I don't need to call GNUNET_CRYPTO_hmac_derive_key()...
167   GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_STRONG,
168                              &(s->auth_key.key),
169                              GNUNET_CRYPTO_HASH_LENGTH);
170
171   //s->peer_id = GNUNET_new( struct GNUNET_PeerIdentity );
172   GNUNET_assert(NULL != id);
173   s->peer_id = id;
174   memcpy(s->peer_id, own_identity, sizeof(struct GNUNET_PeerIdentity));
175   //s->peer_id = own_identity; // Maybe set to own PeerID. So we always have
176                      // a valid PeerID in the sampler.
177                      // Maybe take a PeerID as second argument.
178   LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: initialised with PeerID %s (at %p) \n", GNUNET_i2s(s->peer_id), s->peer_id);
179
180   GNUNET_CRYPTO_hmac(&s->auth_key, s->peer_id,
181                      sizeof(struct GNUNET_PeerIdentity),
182                      &s->peer_id_hash);
183
184   s->prev = NULL;
185   s->next = NULL;
186
187   return s;
188 }
189
190 /**
191  * Compare two hashes.
192  *
193  * Returns if the first one is smaller then the second.
194  * Used by SAMPLER_next() to compare hashes.
195  */
196   int
197 hash_cmp(struct GNUNET_HashCode hash1, struct GNUNET_HashCode hash2)
198 {
199   return memcmp( (const void *) &hash1, (const void *) & hash2, sizeof(struct GNUNET_HashCode));
200 }
201
202 /**
203  * Input an PeerID into the given sampler.
204  */
205   static void
206 SAMPLER_next(struct Sampler *s, const struct GNUNET_PeerIdentity *id, SAMPLER_deleteCB del_cb, void *cb_cls)
207   // TODO set id in peer_ids
208 {
209   struct GNUNET_HashCode other_hash;
210
211   if ( id == s->peer_id )
212   {
213     LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER:          Got PeerID %s\n",
214         GNUNET_i2s(id));
215     LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Have already PeerID %s\n",
216         GNUNET_i2s(s->peer_id));
217   }
218   else
219   {
220     GNUNET_CRYPTO_hmac(&s->auth_key,
221         id,
222         sizeof(struct GNUNET_PeerIdentity),
223         &other_hash);
224
225     if ( NULL == s->peer_id )
226     { // Or whatever is a valid way to say
227       // "we have no PeerID at the moment"
228       LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s; Simply accepting (got NULL previously).\n",
229           GNUNET_i2s(id));
230       memcpy(s->peer_id, id, sizeof(struct GNUNET_PeerIdentity));
231       //s->peer_id = id;
232       s->peer_id_hash = other_hash;
233     }
234     else if ( 0 > hash_cmp(other_hash, s->peer_id_hash) )
235     {
236       LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER:            Got PeerID %s\n",
237           GNUNET_i2s(id));
238       LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Discarding old PeerID %s\n",
239           GNUNET_i2s(s->peer_id));
240
241       if ( NULL != del_cb )
242       {
243         LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Removing old PeerID %s with the delete callback.\n",
244             GNUNET_i2s(s->peer_id));
245         del_cb(cb_cls, s->peer_id, s->peer_id_hash);
246       }
247
248       memcpy(s->peer_id, id, sizeof(struct GNUNET_PeerIdentity));
249       //s->peer_id = id;
250       s->peer_id_hash = other_hash;
251     }
252     else
253     {
254       LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER:         Got PeerID %s\n",
255           GNUNET_i2s(id), id);
256       LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Keeping old PeerID %s\n",
257           GNUNET_i2s(s->peer_id), s->peer_id);
258     }
259   }
260 }
261
262
263 /**
264  * Initialise a tuple of samplers.
265  */
266 struct Samplers *
267 SAMPLER_samplers_init(size_t init_size)
268 {
269   struct Samplers *samplers;
270   struct Sampler *s;
271   uint64_t i;
272
273   samplers = GNUNET_new(struct Samplers);
274   samplers->size = 0;
275   samplers->head = samplers->tail = NULL;
276   samplers->peer_ids = NULL;
277   //samplers->peer_ids = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity);
278
279   for ( i = 0 ; i < init_size ; i++ )
280   {
281     GNUNET_array_append(samplers->peer_ids,
282         samplers->size,
283         *own_identity);
284     //samplers->size++;
285     s = SAMPLER_init(&samplers->peer_ids[i]);
286     GNUNET_CONTAINER_DLL_insert_tail(samplers->head,
287         samplers->tail,
288         s);
289   }
290   //samplers->size = init_size;
291   GNUNET_assert(init_size == samplers->size);
292   return samplers;
293 }
294
295
296 /**
297  * A fuction to update every sampler in the given list
298  */
299   static void
300 SAMPLER_update_list(struct Samplers *samplers, const struct GNUNET_PeerIdentity *id,
301                     SAMPLER_deleteCB del_cb, void *cb_cls)
302 {
303   struct Sampler *sampler;
304
305   sampler = samplers->head;
306   while ( NULL != sampler->next )
307   {
308     SAMPLER_next(sampler, id, del_cb, cb_cls);
309   }
310   
311 }
312
313 /**
314  * Get one random peer out of the sampled peers.
315  *
316  * We might want to reinitialise this sampler after giving the
317  * corrsponding peer to the client.
318  */
319   const struct GNUNET_PeerIdentity* 
320 SAMPLER_get_rand_peer (struct Samplers *samplers)
321 {
322   LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER_get_rand_peer:\n");
323
324   if ( 0 == samplers->size )
325   {
326     LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: List empty - Returning own PeerID %s\n", GNUNET_i2s(own_identity));
327     return own_identity;
328   }
329   else
330   {
331     uint64_t index;
332     struct Sampler *iter;
333     uint64_t i;
334     const struct GNUNET_PeerIdentity *peer;
335
336     /**
337      * Choose the index of the peer we want to give back
338      * at random from the interval of the sampler list
339      */
340     index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
341                                      samplers->size);
342                                      // TODO check that it does not overflow
343     LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: Length of Slist: %" PRIu64 ", index: %" PRIu64 "\n", samplers->size, index);
344
345     iter = samplers->head;
346     for ( i = 0 ; i < index ; i++ )
347     {
348       if ( NULL == iter->next )
349       { // Maybe unneeded
350         iter = samplers->head;
351       }
352     }
353     
354     // TODO something missing?
355
356     peer = iter->peer_id;
357     LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: Returning PeerID %s\n", GNUNET_i2s(peer));
358     LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: (own ID: %s)\n", GNUNET_i2s(own_identity));
359
360     return peer;
361   }
362 }
363
364 /**
365  * Get n random peers out of the sampled peers.
366  *
367  * We might want to reinitialise this sampler after giving the
368  * corrsponding peer to the client.
369  * Random with or without consumption?
370  */
371   const struct GNUNET_PeerIdentity*  // TODO give back simple array
372 SAMPLER_get_n_rand_peers (struct Samplers *samplers, uint64_t n)
373 {
374   // TODO check if we have too much (distinct) sampled peers
375   // If we are not ready yet maybe schedule for later
376   struct GNUNET_PeerIdentity *peers;
377   uint64_t i;
378   
379   peers = GNUNET_malloc(n * sizeof(struct GNUNET_PeerIdentity));
380
381   for ( i = 0 ; i < n ; i++ ) {
382     //peers[i] = SAMPLER_get_rand_peer(samplers);
383     memcpy(&peers[i], SAMPLER_get_rand_peer(samplers), sizeof(struct GNUNET_PeerIdentity));
384   }
385
386   // TODO something else missing?
387   return peers;
388 }
389
390 /**
391  * Counts how many Samplers currently hold a given PeerID.
392  */
393   uint64_t
394 SAMPLER_count_id ( struct Samplers *samplers, struct GNUNET_PeerIdentity *id )
395 {
396   struct Sampler *iter;
397   uint64_t count;
398
399   iter = samplers->head;
400   count = 0;
401   while ( NULL != iter )
402   {
403     if ( peer_id_cmp( iter->peer_id, id) )
404       count++;
405     iter = iter->next;
406   }
407   return count;
408 }
409
410 /**
411  * Gow the size of the tuple of samplers.
412  */
413   void
414 SAMPLER_samplers_grow (struct Samplers * samplers, size_t new_size)
415 {
416   uint64_t i;
417   struct Sampler *sampler;
418
419   if ( new_size > samplers->size )
420   {
421     GNUNET_array_grow(samplers->peer_ids, samplers->size, new_size);
422     for ( i = 0 ; i < new_size - samplers->size ; i++ )
423     {
424       sampler = SAMPLER_init(&samplers->peer_ids[samplers->size + i]);
425       GNUNET_CONTAINER_DLL_insert_tail(samplers->head, samplers->tail, sampler);
426     }
427   }
428   else if ( new_size < samplers->size )
429   {
430     for ( i = 0 ; i < samplers->size - new_size ; i++)
431     {
432       // TODO call delCB on elem?
433       GNUNET_CONTAINER_DLL_remove(samplers->head, samplers->tail, samplers->tail);
434     }
435     GNUNET_array_grow(samplers->peer_ids, samplers->size, new_size);
436   }
437
438   samplers->size = new_size;
439 }
440
441 /***********************************************************************
442  * /Sampler
443 ***********************************************************************/
444
445
446
447 /***********************************************************************
448  * Housekeeping with peers
449 ***********************************************************************/
450
451 /**
452  * Struct used to store the context of a connected client.
453  */
454 struct client_ctx
455 {
456   /**
457    * The message queue to communicate with the client.
458    */
459   struct GNUNET_MQ_Handle *mq;
460 };
461
462 /**
463  * Used to keep track in what lists single peerIDs are.
464  */
465 enum in_list_flag // probably unneeded
466 {
467   in_other_sampler_list = 0x1,
468   in_other_gossip_list  = 0x2, // unneeded?
469   in_own_sampler_list   = 0x4,
470   in_own_gossip_list    = 0x8 // unneeded?
471 };
472
473 /**
474  * Struct used to keep track of other peer's status
475  *
476  * This is stored in a multipeermap.
477  */
478 struct peer_context
479 {
480   /**
481    * In own gossip/sampler list, in other's gossip/sampler list
482    */
483   uint32_t in_flags; // unneeded?
484
485   /**
486    * Message queue open to client
487    */
488   struct GNUNET_MQ_Handle *mq;
489
490   /**
491    * Channel open to client.
492    */
493   struct GNUNET_CADET_Channel *to_channel;
494
495   /**
496    * Channel open from client.
497    */
498   struct GNUNET_CADET_Channel *from_channel; // unneeded
499
500   /**
501    * This is pobably followed by 'statistical' data (when we first saw
502    * him, how did we get his ID, how many pushes (in a timeinterval),
503    * ...)
504    */
505 };
506
507 /***********************************************************************
508  * /Housekeeping with peers
509 ***********************************************************************/
510
511 /**
512  * Set of all peers to keep track of them.
513  */
514 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
515
516
517 // -- gossip list length --
518 // Depends on the (estimated) size of the
519 // network. - Initial size might be the
520 // number of peers cadet provides.
521 // TODO other events to grow/shrink size?
522
523 /**
524  * List of samplers.
525  */
526 struct Samplers *sampler_list; // TODO rename to sampler_list
527
528 /**
529  * Sampler list size // TODO get rid of that
530  *
531  * Adapts to the nse. Size should be in BigTheta(network_size)^(1/3).
532  */
533 size_t sampler_list_size;
534
535
536 /**
537  * The gossiped list of peers.
538  */
539 struct GNUNET_PeerIdentity *gossip_list;
540
541 /**
542  * Size of the gossiped list
543  */
544 unsigned int gossip_list_size;
545
546 /**
547  * Min size of the gossip list
548  */
549 uint64_t gossip_list_min_size;
550
551 ///**
552 // * Max size of the gossip list
553 // * 
554 // * This will probably be left to be set by the client.
555 // */
556 //uint64_t gossip_list_max_size;
557
558
559 /**
560  * The estimated size of the network.
561  *
562  * Influenced by the stdev.
563  */
564 size_t est_size;
565
566
567
568 /**
569  * Percentage of total peer number in the gossip list
570  * to send random PUSHes to
571  */
572 float alpha;
573
574 /**
575  * Percentage of total peer number in the gossip list
576  * to send random PULLs to
577  */
578 float beta;
579
580 /**
581  * The percentage gamma of history updates.
582  * Simply 1 - alpha - beta
583  */
584
585
586
587
588 /**
589  * Identifier for the main task that runs periodically.
590  */
591 GNUNET_SCHEDULER_TaskIdentifier do_round_task;
592
593 /**
594  * Time inverval the do_round task runs in.
595  */
596 struct GNUNET_TIME_Relative round_interval;
597
598
599
600 /**
601  * List to store peers received through pushes temporary.
602  */
603 struct GNUNET_PeerIdentity *push_list;
604
605 /**
606  * Size of the push_list;
607  */
608 unsigned int push_list_size;
609 //size_t push_list_size;
610
611 /**
612  * List to store peers received through pulls temporary.
613  */
614 struct GNUNET_PeerIdentity *pull_list;
615
616 /**
617  * Size of the pull_list;
618  */
619 unsigned int pull_list_size;
620 //size_t pull_list_size;
621
622
623 /**
624  * Handler to NSE.
625  */
626 struct GNUNET_NSE_Handle *nse;
627
628 /**
629  * Handler to CADET.
630  */
631 struct GNUNET_CADET_Handle *cadet_handle;
632
633
634 /***********************************************************************
635  * Util functions
636 ***********************************************************************/
637
638 /**
639  * Get random peer from the gossip list.
640  */
641   struct GNUNET_PeerIdentity *
642 get_rand_gossip_peer()
643 {
644   uint64_t index;
645   struct GNUNET_PeerIdentity *peer;
646
647   // TODO find a better solution.
648   // FIXME if we have only own ID in gossip list this will block
649   // but then we might have a problem nevertheless ?
650
651   do {
652
653     /**;
654      * Choose the index of the peer we want to return
655      * at random from the interval of the gossip list
656      */
657     index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
658                                      gossip_list_size);
659
660     peer = &(gossip_list[index]);
661   } while ( own_identity == peer || NULL == peer );
662
663   return peer;
664 }
665
666 /**
667  * Get the message queue of a specific peer.
668  *
669  * If we already have a message queue open to this client,
670  * simply return it, otherways create one.
671  */
672   struct GNUNET_MQ_Handle *
673 get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, struct GNUNET_PeerIdentity *peer_id)
674 {
675   struct peer_context *ctx;
676   struct GNUNET_MQ_Handle * mq;
677   struct GNUNET_CADET_Channel *channel;
678
679   if ( GNUNET_OK != GNUNET_CONTAINER_multipeermap_contains( peer_map, peer_id ) ) {
680
681     channel = GNUNET_CADET_channel_create(cadet_handle, NULL, peer_id,
682                                   GNUNET_RPS_CADET_PORT,
683                                   GNUNET_CADET_OPTION_RELIABLE);
684     mq = GNUNET_CADET_mq_create(channel);
685
686     ctx = GNUNET_malloc(sizeof(struct peer_context));
687     ctx->in_flags = 0;
688     ctx->to_channel = channel;
689     ctx->mq = mq;
690
691     GNUNET_CONTAINER_multipeermap_put(peer_map, peer_id, ctx,
692                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
693   } else {
694     ctx = GNUNET_CONTAINER_multipeermap_get(peer_map, peer_id);
695     if ( NULL == ctx->mq ) {
696       if ( NULL == ctx->to_channel ) {
697         channel = GNUNET_CADET_channel_create(cadet_handle, NULL, peer_id,
698                                       GNUNET_RPS_CADET_PORT,
699                                       GNUNET_CADET_OPTION_RELIABLE);
700         ctx->to_channel = channel;
701       }
702
703       mq = GNUNET_CADET_mq_create(ctx->to_channel);
704       ctx->mq = mq;
705     }
706   }
707
708   return ctx->mq;
709 }
710
711
712 /***********************************************************************
713  * /Util functions
714 ***********************************************************************/
715
716 /**
717  * Function called by NSE.
718  *
719  * Updates sizes of sampler list and gossip list and adapt those lists
720  * accordingly.
721  */
722   void
723 nse_callback(void *cls, struct GNUNET_TIME_Absolute timestamp, double logestimate, double std_dev)
724 {
725   double estimate;
726   //double scale; // TODO this might go gloabal/config
727
728   LOG(GNUNET_ERROR_TYPE_DEBUG, "Received a ns estimate - logest: %f, std_dev: %f\n", logestimate, std_dev);
729   //scale = .01;
730   estimate = 1 << (uint64_t) round(logestimate);
731   // GNUNET_NSE_log_estimate_to_n (logestimate);
732   estimate = pow(estimate, 1./3);// * (std_dev * scale); // TODO add
733   if ( 0 < estimate ) {
734     LOG(GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
735     est_size = estimate;
736   } else {
737     LOG(GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
738   }
739 }
740
741 /**
742  * Handle RPS request from the client.
743  *
744  * @param cls closure
745  * @param client identification of the client
746  * @param message the actual message
747  */
748 static void
749 // TODO rename
750 handle_cs_request (void *cls,
751             struct GNUNET_SERVER_Client *client,
752             const struct GNUNET_MessageHeader *message)
753 {
754   LOG(GNUNET_ERROR_TYPE_DEBUG, "Client requested (a) random peer(s).\n");
755
756   struct GNUNET_RPS_CS_RequestMessage *msg;
757   //unsigned int n_arr[sampler_list_size];// =
758     //GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list_size);
759   //struct GNUNET_MQ_Handle *mq;
760   struct client_ctx *cli_ctx;
761   struct GNUNET_MQ_Envelope *ev;
762   struct GNUNET_RPS_CS_ReplyMessage *out_msg;
763   uint64_t num_peers;
764   uint64_t i;
765
766   // TODO
767   msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
768   // Does not work because the compiler seems not to find it.
769   cli_ctx = GNUNET_SERVER_client_get_user_context(client, struct client_ctx);
770   if ( NULL == cli_ctx ) {
771     cli_ctx = GNUNET_new(struct client_ctx);
772     cli_ctx->mq = GNUNET_MQ_queue_for_server_client(client);
773     GNUNET_SERVER_client_set_user_context(client, cli_ctx);
774   }
775   
776   //mq = GNUNET_MQ_queue_for_server_client(client);
777     
778   // TODO How many peers do we give back?
779   // Wait until we have enough random peers?
780
781   ev = GNUNET_MQ_msg_extra(out_msg,
782                            GNUNET_ntohll(msg->num_peers) * sizeof(struct GNUNET_PeerIdentity),
783                            GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
784   out_msg->num_peers = GNUNET_ntohll(msg->num_peers);
785
786   num_peers = GNUNET_ntohll(msg->num_peers);
787   //&out_msg[1] = SAMPLER_get_n_rand_peers(sampler_list, num_peers);
788   for ( i = 0 ; i < num_peers ; i++ ) {
789     memcpy(&out_msg[1] + i * sizeof(struct GNUNET_PeerIdentity),
790            SAMPLER_get_rand_peer(sampler_list),
791            sizeof(struct GNUNET_PeerIdentity));
792   }
793   
794   GNUNET_MQ_send(cli_ctx->mq, ev);
795   //GNUNET_MQ_send(mq, ev);
796   //GNUNET_MQ_destroy(mq);
797
798   GNUNET_SERVER_receive_done (client,
799                               GNUNET_OK);
800 }
801
802 /**
803  * Handle a PUSH message from another peer.
804  *
805  * Check the proof of work and store the PeerID
806  * in the temporary list for pushed PeerIDs.
807  *
808  * @param cls Closure
809  * @param channel The channel the PUSH was received over
810  * @param channel_ctx The context associated with this channel
811  * @param msg The message header
812  */
813 static int
814 handle_peer_push (void *cls,
815     struct GNUNET_CADET_Channel *channel,
816     void **channel_ctx,
817     const struct GNUNET_MessageHeader *msg)
818 {
819   LOG(GNUNET_ERROR_TYPE_DEBUG, "PUSH received\n");
820
821   struct GNUNET_PeerIdentity *peer;
822
823   // TODO check the proof of work
824   // and check limit for PUSHes
825   // IF we count per peer PUSHes
826   // maybe remove from gossip/sampler list
827   
828   peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info( channel, GNUNET_CADET_OPTION_PEER );
829   
830   /* Add the sending peer to the push_list */
831   GNUNET_array_append(push_list, push_list_size, *peer);
832   //push_list_size ++;
833
834   return GNUNET_OK;
835 }
836
837 /**
838  * Handle PULL REQUEST request message from another peer.
839  *
840  * Reply with the gossip list of PeerIDs.
841  *
842  * @param cls Closure
843  * @param channel The channel the PUSH was received over
844  * @param channel_ctx The context associated with this channel
845  * @param msg The message header
846  */
847 static int
848 handle_peer_pull_request (void *cls,
849     struct GNUNET_CADET_Channel *channel,
850     void **channel_ctx,
851     const struct GNUNET_MessageHeader *msg)
852 {
853
854   struct GNUNET_PeerIdentity *peer;
855   struct GNUNET_MQ_Handle *mq;
856   //struct GNUNET_RPS_P2P_PullRequestMessage *in_msg;
857   struct GNUNET_MQ_Envelope *ev;
858   struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
859
860   // TODO find some way to keep one peer from spamming with pull requests
861   // allow only one request per time interval ?
862   // otherwise remove from peerlist?
863
864   peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info(channel, GNUNET_CADET_OPTION_PEER);
865   LOG(GNUNET_ERROR_TYPE_DEBUG, "PULL REQUEST from peer %s received\n", GNUNET_i2s(peer));
866
867   mq = GNUNET_CADET_mq_create(channel); // TODO without mq?
868   //mq = get_mq(peer_map, peer);
869
870   //in_msg = (struct GNUNET_RPS_P2P_PullRequestMessage *) msg;
871   // TODO how many peers do we actually send?
872   // GNUNET_ntohll(in_msg->num_peers)
873   ev = GNUNET_MQ_msg_extra(out_msg,
874                            gossip_list_size * sizeof(struct GNUNET_PeerIdentity),
875                            GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
876   out_msg->num_peers = GNUNET_htonll(gossip_list_size);
877   memcpy(&out_msg[1], gossip_list,
878          gossip_list_size * sizeof(struct GNUNET_PeerIdentity));
879
880   GNUNET_MQ_send(mq, ev);
881
882   GNUNET_MQ_destroy(mq);
883
884
885   return GNUNET_OK;
886 }
887
888 /**
889  * Handle PULL REPLY message from another peer.
890  *
891  * Check whether we sent a corresponding request and
892  * whether this reply is the first one.
893  *
894  * @param cls Closure
895  * @param channel The channel the PUSH was received over
896  * @param channel_ctx The context associated with this channel
897  * @param msg The message header
898  */
899 static int
900 handle_peer_pull_reply (void *cls,
901     struct GNUNET_CADET_Channel *channel,
902     void **channel_ctx,
903     const struct GNUNET_MessageHeader *msg)
904 {
905   LOG(GNUNET_ERROR_TYPE_DEBUG, "PULL REPLY received\n");
906
907   struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
908   struct GNUNET_PeerIdentity *peers;
909   uint64_t i;
910
911   // TODO check that we sent a request and that it is the first reply
912
913   in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
914   peers = (struct GNUNET_PeerIdentity *) &msg[1];
915   for ( i = 0 ; i < GNUNET_ntohll(in_msg->num_peers) ; i++ ) {
916     GNUNET_array_append(pull_list, pull_list_size, peers[i]);
917     //pull_list_size++;
918   }
919
920   // TODO maybe a disconnect happens here
921   
922   return GNUNET_OK;
923 }
924
925
926 /**
927  * Callback called when a Sampler is updated.
928  */
929   void
930 delete_cb (void *cls, struct GNUNET_PeerIdentity *id, struct GNUNET_HashCode hash)
931 {
932   size_t s;
933
934   s = SAMPLER_count_id(sampler_list, id);
935   if ( 1 >= s ) {
936     // TODO cleanup peer
937     GNUNET_CONTAINER_multipeermap_remove_all( peer_map, id);
938   }
939 }
940
941
942 /**
943  * Send out PUSHes and PULLs.
944  *
945  * This is executed regylary.
946  */
947 static void
948 do_round(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
949 {
950   LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round\n");
951
952   uint64_t i;
953   //unsigned int *n_arr;
954   struct GNUNET_RPS_P2P_PushMessage        *push_msg;
955   struct GNUNET_RPS_P2P_PullRequestMessage *pull_msg; // FIXME Send empty message
956   struct GNUNET_MQ_Envelope *ev;
957   struct GNUNET_PeerIdentity *peer;
958
959   // TODO print lists, ...
960   // TODO cleanup peer_map
961
962
963   /* If the NSE has changed adapt the lists accordingly */
964   // TODO check nse == 0!
965   LOG(GNUNET_ERROR_TYPE_DEBUG, "Checking size estimate.\n");
966   SAMPLER_samplers_grow(sampler_list, est_size);
967
968   GNUNET_array_grow(gossip_list, gossip_list_size, est_size); // FIXME Do conversion correct or change type
969
970   gossip_list_size = sampler_list_size = est_size;
971
972  
973
974
975   /* Would it make sense to have one shuffeled gossip list and then
976    * to send PUSHes to first alpha peers, PULL requests to next beta peers and
977    * use the rest to update sampler? */
978
979   /* Send PUSHes */
980   //n_arr = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) gossip_list_size);
981   LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to send pushes to %f (%f * %" PRIu64 ") peers.\n",
982       alpha * gossip_list_size, alpha, gossip_list_size);
983   for ( i = 0 ; i < alpha * gossip_list_size ; i++ ) { // TODO compute length
984     peer = get_rand_gossip_peer();
985     // TODO check NULL == peer
986     LOG(GNUNET_ERROR_TYPE_DEBUG, "Sending PUSH to peer %s of gossiped list.\n", GNUNET_i2s(peer));
987
988     ev = GNUNET_MQ_msg(push_msg, GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
989     //ev = GNUNET_MQ_msg_extra();
990     /* TODO Compute proof of work here
991     push_msg; */
992     push_msg->placeholder = 0;
993     GNUNET_MQ_send( get_mq(peer_map, peer), ev );
994
995     // TODO modify in_flags of respective peer?
996   }
997
998
999   /* Send PULL requests */
1000   // TODO
1001   //n_arr = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list_size);
1002   LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to send pulls to %f (%f * %" PRIu64 ") peers.\n",
1003       beta * gossip_list_size, beta, gossip_list_size);
1004   for ( i = 0 ; i < beta * gossip_list_size ; i++ ){ // TODO compute length
1005     peer = get_rand_gossip_peer();
1006     // TODO check NULL == peer
1007     LOG(GNUNET_ERROR_TYPE_DEBUG, "Sending PULL request to peer %s of gossiped list.\n", GNUNET_i2s(peer));
1008
1009     ev = GNUNET_MQ_msg(pull_msg, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
1010     //ev = GNUNET_MQ_msg_extra();
1011     pull_msg->placeholder = 0;
1012     GNUNET_MQ_send( get_mq(peer_map, peer), ev );
1013     // TODO modify in_flags of respective peer?
1014   }
1015
1016
1017
1018
1019   /* Update gossip list */
1020   uint64_t tmp_index;
1021   uint64_t index;
1022
1023   if ( push_list_size <= alpha * gossip_list_size &&
1024        push_list_size != 0 &&
1025        pull_list_size != 0 ) {
1026     LOG(GNUNET_ERROR_TYPE_DEBUG, "Update of the gossip list. ()\n");
1027
1028     for ( i = 0 ; i < alpha * gossip_list_size ; i++ ) { // TODO use SAMPLER_get_n_rand_peers
1029       /* Update gossip list with peers received through PUSHes */
1030       index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
1031                                        push_list_size);
1032       gossip_list[i] = push_list[index];
1033       // TODO change the in_flags accordingly
1034     }
1035
1036     for ( i = 0 ; i < beta * gossip_list_size ; i++ ) {
1037       /* Update gossip list with peers received through PULLs */
1038       tmp_index = i + round(alpha * gossip_list_size);
1039       index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
1040                                        pull_list_size);
1041       gossip_list[tmp_index] = pull_list[index];
1042       // TODO change the in_flags accordingly
1043     }
1044
1045     for ( i = 0 ; i < (1 - (alpha + beta)) * gossip_list_size ; i++ ) {
1046       /* Update gossip list with peers from history */
1047       tmp_index = i + round((alpha + beta) * gossip_list_size);
1048       index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
1049                                        sampler_list->size);
1050       gossip_list[tmp_index] = sampler_list->peer_ids[index];
1051       // TODO change the in_flags accordingly
1052     }
1053
1054   } else {
1055     LOG(GNUNET_ERROR_TYPE_DEBUG, "No update of the gossip list. ()\n");
1056   }
1057   // TODO independent of that also get some peers from CADET_get_peers()?
1058
1059
1060
1061   /* Update samplers */
1062
1063   for ( i = 0 ; i < push_list_size ; i++ )
1064   {
1065     SAMPLER_update_list(sampler_list, &push_list[i], NULL, NULL);
1066     // TODO set in_flag?
1067   }
1068
1069   for ( i = 0 ; i < pull_list_size ; i++ )
1070   {
1071     SAMPLER_update_list(sampler_list, &pull_list[i], NULL, NULL);
1072     // TODO set in_flag?
1073   }
1074
1075
1076   // TODO go over whole peer_map and do cleanups
1077   // delete unneeded peers, set in_flags, check channel/mq
1078
1079
1080   /* Empty push/pull lists */
1081   GNUNET_array_grow(push_list, push_list_size, 0);
1082   push_list_size = 0;
1083   GNUNET_array_grow(pull_list, pull_list_size, 0);
1084   pull_list_size = 0;
1085
1086
1087   /* Schedule next round */
1088   // TODO
1089   do_round_task = GNUNET_SCHEDULER_add_delayed( round_interval, &do_round, NULL );
1090   LOG(GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
1091 }
1092
1093 static void
1094 rps_start (struct GNUNET_SERVER_Handle *server);
1095
1096 /**
1097  * This is called from GNUNET_CADET_get_peers().
1098  *
1099  * It is called on every peer(ID) that cadet somehow has contact with.
1100  * We use those to initialise the sampler.
1101  */
1102 void
1103 init_peer_cb (void *cls,
1104               const struct GNUNET_PeerIdentity *peer,
1105               int tunnel, // "Do we have a tunnel towards this peer?"
1106               unsigned int n_paths, // "Number of known paths towards this peer"
1107               unsigned int best_path) // "How long is the best path?
1108                                       // (0 = unknown, 1 = ourselves, 2 = neighbor)"
1109 {
1110   // FIXME use the magic 0000 PeerID
1111   if ( NULL != peer ) {
1112     LOG(GNUNET_ERROR_TYPE_DEBUG, "Got peer %s (at %p) from CADET\n", GNUNET_i2s(peer), peer);
1113     SAMPLER_update_list(sampler_list, peer, NULL, NULL);
1114     if ( GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains( peer_map, peer ) ) {
1115     } else {
1116       struct peer_context *ctx;
1117
1118       ctx = GNUNET_malloc(sizeof(struct peer_context));
1119       ctx->in_flags = 0;
1120       ctx->mq = NULL;
1121       ctx->to_channel = NULL;
1122       ctx->from_channel = NULL;
1123       GNUNET_CONTAINER_multipeermap_put( peer_map, peer, ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1124     }
1125
1126     uint64_t i;
1127     i = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG, gossip_list_size);
1128     gossip_list[i] = *peer;
1129     // TODO send push/pull to each of those peers?
1130   } else {
1131     rps_start( (struct GNUNET_SERVER_Handle *) cls);
1132   }
1133 }
1134
1135
1136
1137
1138 /**
1139  * Task run during shutdown.
1140  *
1141  * @param cls unused
1142  * @param tc unused
1143  */
1144 static void
1145 shutdown_task (void *cls,
1146                const struct GNUNET_SCHEDULER_TaskContext *tc)
1147 {
1148   LOG(GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
1149
1150   if ( GNUNET_SCHEDULER_NO_TASK != do_round_task )
1151   {
1152     GNUNET_SCHEDULER_cancel (do_round_task);
1153     do_round_task = GNUNET_SCHEDULER_NO_TASK;
1154   }
1155
1156   GNUNET_NSE_disconnect(nse);
1157   GNUNET_CADET_disconnect(cadet_handle);
1158   GNUNET_free(own_identity);
1159   //GNUNET_free(round_interval);
1160   //GNUNET_free(est_size);
1161   //GNUNET_free(gossip_list_size);
1162   //GNUNET_free(sampler_list_size);
1163   GNUNET_free(gossip_list);
1164   // TODO for i in sampler_list free sampler
1165   // TODO destroy sampler_list
1166   // TODO destroy push/pull_list
1167   // TODO delete global data
1168 }
1169
1170
1171 /**
1172  * A client disconnected.  Remove all of its data structure entries.
1173  *
1174  * @param cls closure, NULL
1175  * @param client identification of the client
1176  */
1177 static void
1178 handle_client_disconnect (void *cls,
1179                           struct GNUNET_SERVER_Client * client)
1180 {
1181   // TODO reinitialise that sampler
1182 }
1183
1184 /**
1185  * Handle the channel a peer opens to us.
1186  *
1187  * @param cls The closure
1188  * @param channel The channel the peer wants to establish
1189  * @param initiator The peer's peer ID
1190  * @param port The port the channel is being established over
1191  * @param options Further options
1192  */
1193   static void *
1194 handle_inbound_channel (void *cls,
1195                         struct GNUNET_CADET_Channel *channel,
1196                         const struct GNUNET_PeerIdentity *initiator,
1197                         uint32_t port,
1198                         enum GNUNET_CADET_ChannelOption options)
1199 {
1200   LOG(GNUNET_ERROR_TYPE_DEBUG, "New channel was established to us.\n");
1201
1202   GNUNET_assert( NULL != channel );
1203
1204   // TODO we might even not store the from_channel
1205
1206   if ( GNUNET_CONTAINER_multipeermap_contains( peer_map, initiator ) ) {
1207     ((struct peer_context *) GNUNET_CONTAINER_multipeermap_get( peer_map, initiator ))->from_channel = channel;
1208     // FIXME there might already be an established channel
1209   } else {
1210     struct peer_context *ctx;
1211
1212     ctx = GNUNET_malloc( sizeof(struct peer_context));
1213     ctx->in_flags = in_other_gossip_list;
1214     ctx->mq = NULL; // TODO create mq?
1215     ctx->from_channel = channel;
1216
1217     GNUNET_CONTAINER_multipeermap_put( peer_map, initiator, ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1218   }
1219   return NULL; // TODO
1220 }
1221
1222 /**
1223  * This is called when a remote peer destroys a channel.
1224  *
1225  * @param cls The closure
1226  * @param channel The channel being closed
1227  * @param channel_ctx The context associated with this channel
1228  */
1229 static void
1230 cleanup_channel(void *cls,
1231                 const struct GNUNET_CADET_Channel *channel,
1232                 void *channel_ctx)
1233 {
1234   LOG(GNUNET_ERROR_TYPE_DEBUG, "Channel was destroyed by remote peer.\n");
1235 }
1236
1237 /**
1238  * Actually start the service.
1239  */
1240 static void
1241 rps_start (struct GNUNET_SERVER_Handle *server)
1242 {
1243   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1244     {&handle_cs_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST, 0},
1245     {NULL, NULL, 0, 0}
1246   };
1247
1248   GNUNET_SERVER_add_handlers (server, handlers);
1249   GNUNET_SERVER_disconnect_notify (server,
1250                                    &handle_client_disconnect,
1251                                    NULL);
1252   LOG(GNUNET_ERROR_TYPE_DEBUG, "Ready to receive requests from clients\n");
1253
1254
1255
1256   do_round_task = GNUNET_SCHEDULER_add_delayed( round_interval, &do_round, NULL);
1257   LOG(GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
1258
1259   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1260                                 &shutdown_task,
1261                                 NULL);
1262 }
1263
1264
1265
1266 /**
1267  * Process statistics requests.
1268  *
1269  * @param cls closure
1270  * @param server the initialized server
1271  * @param c configuration to use
1272  */
1273 static void
1274 run (void *cls,
1275      struct GNUNET_SERVER_Handle *server,
1276      const struct GNUNET_CONFIGURATION_Handle *c)
1277 {
1278   // TODO check what this does -- copied from gnunet-boss
1279   // - seems to work as expected
1280   GNUNET_log_setup("rps", GNUNET_error_type_to_string(GNUNET_ERROR_TYPE_DEBUG), NULL);
1281
1282   LOG(GNUNET_ERROR_TYPE_DEBUG, "RPS started\n");
1283
1284   cfg = c;
1285
1286
1287   own_identity = GNUNET_new(struct GNUNET_PeerIdentity);
1288
1289   GNUNET_CRYPTO_get_peer_identity(cfg, own_identity); // TODO check return value
1290
1291   LOG(GNUNET_ERROR_TYPE_DEBUG, "Own identity is %s (at %p).\n", GNUNET_i2s(own_identity), own_identity);
1292
1293
1294
1295   /* Get time interval from the configuration */
1296   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
1297                                                         "ROUNDINTERVAL",
1298                                                         &round_interval))
1299   {
1300     LOG(GNUNET_ERROR_TYPE_DEBUG, "Failed to read ROUNDINTERVAL from config\n");
1301     GNUNET_SCHEDULER_shutdown();
1302     return;
1303   }
1304
1305   /* Get initial size of sampler/gossip list from the configuration */
1306   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "RPS",
1307                                                          "INITSIZE",
1308                                                          (long long unsigned int *) &est_size))
1309   {
1310     LOG(GNUNET_ERROR_TYPE_DEBUG, "Failed to read INITSIZE from config\n");
1311     GNUNET_SCHEDULER_shutdown();
1312     return;
1313   }
1314   LOG(GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", est_size);
1315
1316   gossip_list_size = sampler_list_size = est_size; // TODO rename est_size
1317
1318
1319   gossip_list = NULL;
1320
1321   static unsigned int tmp = 0;
1322
1323   GNUNET_array_grow(gossip_list, tmp, gossip_list_size);
1324
1325
1326
1327   /* connect to NSE */
1328   nse = GNUNET_NSE_connect(cfg, nse_callback, NULL);
1329   // TODO check whether that was successful
1330   // TODO disconnect on shutdown
1331   LOG(GNUNET_ERROR_TYPE_DEBUG, "Connected to NSE\n");
1332
1333
1334   alpha = 0.45;
1335   beta  = 0.45;
1336   // TODO initialise thresholds - ?
1337
1338   ///* Get alpha from the configuration */
1339   //if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_float (cfg, "RPS",
1340   //                                                       "ALPHA",
1341   //                                                       &alpha))
1342   //{
1343   //  LOG(GNUNET_ERROR_TYPE_DEBUG, "No ALPHA specified in the config\n");
1344   //}
1345   //LOG(GNUNET_ERROR_TYPE_DEBUG, "ALPHA is %f\n", alpha);
1346  
1347   ///* Get beta from the configuration */
1348   //if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_float (cfg, "RPS",
1349   //                                                       "BETA",
1350   //                                                       &beta))
1351   //{
1352   //  LOG(GNUNET_ERROR_TYPE_DEBUG, "No BETA specified in the config\n");
1353   //}
1354   //LOG(GNUNET_ERROR_TYPE_DEBUG, "BETA is %f\n", beta);
1355
1356
1357
1358
1359   peer_map = GNUNET_CONTAINER_multipeermap_create(est_size, GNUNET_NO);
1360
1361
1362   /* Initialise sampler and gossip list */
1363
1364   sampler_list = SAMPLER_samplers_init(est_size);
1365
1366   push_list = NULL;
1367   push_list_size = 0;
1368   pull_list = NULL;
1369   pull_list_size = 0;
1370
1371   static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1372     {&handle_peer_push        , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH        , 0},
1373     {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST, 0},
1374     {&handle_peer_pull_reply  , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY  , 0},
1375     {NULL, 0, 0}
1376   };
1377
1378   const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
1379   cadet_handle = GNUNET_CADET_connect(cfg,
1380                                     cls,
1381                                     &handle_inbound_channel,
1382                                     &cleanup_channel,
1383                                     cadet_handlers,
1384                                     ports);
1385   LOG(GNUNET_ERROR_TYPE_DEBUG, "Connected to CADET\n");
1386
1387
1388   LOG(GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
1389   GNUNET_CADET_get_peers(cadet_handle, &init_peer_cb, server);
1390   // FIXME use magic 0000 PeerID to _start_ the service
1391
1392   // TODO send push/pull to each of those peers?
1393 }
1394
1395
1396 /**
1397  * The main function for the rps service.
1398  *
1399  * @param argc number of arguments from the command line
1400  * @param argv command line arguments
1401  * @return 0 ok, 1 on error
1402  */
1403 int
1404 main (int argc, char *const *argv)
1405 {
1406   return (GNUNET_OK ==
1407           GNUNET_SERVICE_run (argc,
1408                               argv,
1409                               "rps",
1410                               GNUNET_SERVICE_OPTION_NONE,
1411                               &run, NULL)) ? 0 : 1;
1412 }
1413
1414 /* end of gnunet-service-rps.c */