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