fix /tmp/rps directory creation, fix linkage, DCE
[oweals/gnunet.git] / src / rps / rps_api.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 it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21 /**
22  * @file rps/rps_api.c
23  * @brief API for rps
24  * @author Julius Bünger
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "rps.h"
29 #include "gnunet_rps_service.h"
30 #include "rps-sampler_client.h"
31
32 #include <inttypes.h>
33
34 #define LOG(kind,...) GNUNET_log_from (kind, "rps-api",__VA_ARGS__)
35
36 /**
37  * Handle for a request to get peers from biased stream of ids
38  */
39 struct GNUNET_RPS_StreamRequestHandle
40 {
41   /**
42    * The client issuing the request.
43    */
44   struct GNUNET_RPS_Handle *rps_handle;
45
46   /**
47    * The callback to be called when we receive an answer.
48    */
49   GNUNET_RPS_NotifyReadyCB ready_cb;
50
51   /**
52    * The closure for the callback.
53    */
54   void *ready_cb_cls;
55
56   /**
57    * @brief Scheduler task for scheduled callback
58    */
59   struct GNUNET_SCHEDULER_Task *callback_task;
60
61   /**
62    * @brief Next element of the DLL
63    */
64   struct GNUNET_RPS_StreamRequestHandle *next;
65
66   /**
67    * @brief Previous element of the DLL
68    */
69   struct GNUNET_RPS_StreamRequestHandle *prev;
70 };
71
72
73 /**
74  * Handler to handle requests from a client.
75  */
76 struct GNUNET_RPS_Handle
77 {
78   /**
79    * The handle to the client configuration.
80    */
81   const struct GNUNET_CONFIGURATION_Handle *cfg;
82
83   /**
84    * The message queue to the client.
85    */
86   struct GNUNET_MQ_Handle *mq;
87
88   /**
89    * @brief Callback called on each update of the view
90    */
91   GNUNET_RPS_NotifyReadyCB view_update_cb;
92
93   /**
94    * @brief Closure to each requested update of the view
95    */
96   void *view_update_cls;
97
98   /**
99    * @brief Closure to each requested peer from the biased stream
100    */
101   void *stream_input_cls;
102
103   /**
104    * @brief Head of the DLL of stream requests
105    */
106   struct GNUNET_RPS_StreamRequestHandle *stream_requests_head;
107
108   /**
109    * @brief Tail of the DLL of stream requests
110    */
111   struct GNUNET_RPS_StreamRequestHandle *stream_requests_tail;
112 };
113
114
115 /**
116  * Handler for a single request from a client.
117  */
118 struct GNUNET_RPS_Request_Handle
119 {
120   /**
121    * The client issuing the request.
122    */
123   struct GNUNET_RPS_Handle *rps_handle;
124
125   /**
126    * The number of requested peers.
127    */
128   uint32_t num_requests;
129
130   /**
131    * @brief The Sampler for the client request
132    */
133   struct RPS_Sampler *sampler;
134
135   /**
136    * @brief Request handle of the request to the sampler - needed to cancel the request
137    */
138   struct RPS_SamplerRequestHandle *sampler_rh;
139
140   /**
141    * @brief Request handle of the request of the biased stream of peers -
142    * needed to cancel the request
143    */
144   struct GNUNET_RPS_StreamRequestHandle *srh;
145
146   /**
147    * The callback to be called when we receive an answer.
148    */
149   GNUNET_RPS_NotifyReadyCB ready_cb;
150
151   /**
152    * The closure for the callback.
153    */
154   void *ready_cb_cls;
155 };
156
157
158 /**
159  * Struct used to pack the callback, its closure (provided by the caller)
160  * and the connection handler to the service to pass it to a callback function.
161  */
162 struct cb_cls_pack
163 {
164   /**
165    * Callback provided by the client
166    */
167   GNUNET_RPS_NotifyReadyCB cb;
168
169   /**
170    * Closure provided by the client
171    */
172   void *cls;
173
174   /**
175    * Handle to the service connection
176    */
177  struct GNUNET_CLIENT_Connection *service_conn;
178 };
179
180
181 /**
182  * @brief Peers received from the biased stream to be passed to all
183  * srh_handlers
184  */
185 static struct GNUNET_PeerIdentity *srh_callback_peers;
186
187 /**
188  * @brief Number of peers in the biased stream that are to be passed to all
189  * srh_handlers
190  */
191 static uint64_t srh_callback_num_peers;
192
193
194 /**
195  * @brief Create a new handle for a stream request
196  *
197  * @param rps_handle The rps handle
198  * @param num_peers The number of desired peers
199  * @param ready_cb The callback to be called, once all peers are ready
200  * @param cls The colsure to provide to the callback
201  *
202  * @return The handle to the stream request
203  */
204 static struct GNUNET_RPS_StreamRequestHandle *
205 new_stream_request (struct GNUNET_RPS_Handle *rps_handle,
206                     GNUNET_RPS_NotifyReadyCB ready_cb,
207                     void *cls)
208 {
209   struct GNUNET_RPS_StreamRequestHandle *srh;
210
211   srh = GNUNET_new (struct GNUNET_RPS_StreamRequestHandle);
212   srh->rps_handle = rps_handle;
213   srh->ready_cb = ready_cb;
214   srh->ready_cb_cls = cls;
215   GNUNET_CONTAINER_DLL_insert (rps_handle->stream_requests_head,
216                                rps_handle->stream_requests_tail,
217                                srh);
218
219   return srh;
220 }
221
222
223 /**
224  * @brief Remove the given stream request from the list of requests and memory
225  *
226  * @param srh The request to be removed
227  */
228 static void
229 remove_stream_request (struct GNUNET_RPS_StreamRequestHandle *srh)
230 {
231   struct GNUNET_RPS_Handle *rps_handle = srh->rps_handle;
232
233   GNUNET_assert (NULL != srh);
234   if (NULL != srh->callback_task)
235   {
236     GNUNET_SCHEDULER_cancel (srh->callback_task);
237     srh->callback_task = NULL;
238   }
239   GNUNET_CONTAINER_DLL_remove (rps_handle->stream_requests_head,
240                                rps_handle->stream_requests_tail,
241                                srh);
242   GNUNET_free (srh);
243 }
244
245
246 /**
247  * @brief Called once the sampler has collected all requested peers.
248  *
249  * Calls the callback provided by the client with the corresponding cls.
250  *
251  * @param peers The array of @a num_peers that has been returned.
252  * @param num_peers The number of peers that have been returned
253  * @param cls The #GNUNET_RPS_Request_Handle
254  */
255 static void
256 peers_ready_cb (const struct GNUNET_PeerIdentity *peers,
257                 uint32_t num_peers,
258                 void *cls)
259 {
260   struct GNUNET_RPS_Request_Handle *rh = cls;
261
262   rh->sampler_rh = NULL;
263   rh->ready_cb (rh->ready_cb_cls,
264                 num_peers,
265                 peers);
266   GNUNET_RPS_stream_cancel (rh->srh);
267   rh->srh = NULL;
268   RPS_sampler_destroy (rh->sampler);
269   rh->sampler = NULL;
270 }
271
272
273 /**
274  * @brief Callback to collect the peers from the biased stream and put those
275  * into the sampler.
276  *
277  * @param cls The #GNUNET_RPS_Request_Handle
278  * @param num_peers The number of peer that have been returned
279  * @param peers The array of @a num_peers that have been returned
280  */
281 static void
282 collect_peers_cb (void *cls,
283                   uint64_t num_peers,
284                   const struct GNUNET_PeerIdentity *peers)
285 {
286   struct GNUNET_RPS_Request_Handle *rh = cls;
287
288   LOG (GNUNET_ERROR_TYPE_DEBUG,
289        "Service sent %" PRIu64 " peers from stream\n",
290        num_peers);
291   for (uint64_t i = 0; i < num_peers; i++)
292   {
293     RPS_sampler_update (rh->sampler, &peers[i]);
294   }
295 }
296
297
298 /* Get internals for debugging/profiling purposes */
299
300 /**
301  * Request updates of view
302  *
303  * @param rps_handle handle to the rps service
304  * @param num_req_peers number of peers we want to receive
305  *        (0 for infinite updates)
306  * @param cls a closure that will be given to the callback
307  * @param ready_cb the callback called when the peers are available
308  */
309 void
310 GNUNET_RPS_view_request (struct GNUNET_RPS_Handle *rps_handle,
311                          uint32_t num_updates,
312                          GNUNET_RPS_NotifyReadyCB view_update_cb,
313                          void *cls)
314 {
315   struct GNUNET_MQ_Envelope *ev;
316   struct GNUNET_RPS_CS_DEBUG_ViewRequest *msg;
317
318   LOG (GNUNET_ERROR_TYPE_DEBUG,
319        "Client requests %" PRIu32 " view updates\n",
320        num_updates);
321   rps_handle->view_update_cb = view_update_cb;
322   rps_handle->view_update_cls = cls;
323
324   ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REQUEST);
325   msg->num_updates = htonl (num_updates);
326   GNUNET_MQ_send (rps_handle->mq, ev);
327 }
328
329
330 void
331 GNUNET_RPS_view_request_cancel (struct GNUNET_RPS_Handle *rps_handle)
332 {
333   struct GNUNET_MQ_Envelope *ev;
334
335   GNUNET_assert (NULL != rps_handle->view_update_cb);
336
337   rps_handle->view_update_cb = NULL;
338
339   ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_CANCEL);
340   GNUNET_MQ_send (rps_handle->mq, ev);
341 }
342
343
344 /**
345  * Request biased stream of peers that are being put into the sampler
346  *
347  * @param rps_handle handle to the rps service
348  * @param cls a closure that will be given to the callback
349  * @param ready_cb the callback called when the peers are available
350  */
351 struct GNUNET_RPS_StreamRequestHandle *
352 GNUNET_RPS_stream_request (struct GNUNET_RPS_Handle *rps_handle,
353                            GNUNET_RPS_NotifyReadyCB stream_input_cb,
354                            void *cls)
355 {
356   struct GNUNET_RPS_StreamRequestHandle *srh;
357   struct GNUNET_MQ_Envelope *ev;
358   struct GNUNET_RPS_CS_DEBUG_StreamRequest *msg;
359
360   srh = new_stream_request (rps_handle,
361                             stream_input_cb,
362                             cls);
363   LOG (GNUNET_ERROR_TYPE_DEBUG, "Client requests biased stream updates\n");
364
365   ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_REQUEST);
366   GNUNET_MQ_send (rps_handle->mq, ev);
367   return srh;
368 }
369
370
371 /**
372  * This function is called, when the service updates the view.
373  * It verifies that @a msg is well-formed.
374  *
375  * @param cls the closure
376  * @param msg the message
377  * @return #GNUNET_OK if @a msg is well-formed
378  */
379 static int
380 check_view_update (void *cls,
381                    const struct GNUNET_RPS_CS_DEBUG_ViewReply *msg)
382 {
383   uint16_t msize = ntohs (msg->header.size);
384   uint32_t num_peers = ntohl (msg->num_peers);
385   (void) cls;
386
387   msize -= sizeof (struct GNUNET_RPS_CS_DEBUG_ViewReply);
388   if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) ||
389        (msize % sizeof (struct GNUNET_PeerIdentity) != 0) )
390   {
391     GNUNET_break (0);
392     return GNUNET_SYSERR;
393   }
394   return GNUNET_OK;
395 }
396
397
398 /**
399  * This function is called, when the service updated its view.
400  * It calls the callback the caller provided
401  * and disconnects afterwards.
402  *
403  * @param msg the message
404  */
405 static void
406 handle_view_update (void *cls,
407                     const struct GNUNET_RPS_CS_DEBUG_ViewReply *msg)
408 {
409   struct GNUNET_RPS_Handle *h = cls;
410   struct GNUNET_PeerIdentity *peers;
411
412   /* Give the peers back */
413   LOG (GNUNET_ERROR_TYPE_DEBUG,
414        "New view of %" PRIu32 " peers:\n",
415        ntohl (msg->num_peers));
416
417   peers = (struct GNUNET_PeerIdentity *) &msg[1];
418   GNUNET_assert (NULL != h);
419   GNUNET_assert (NULL != h->view_update_cb);
420   h->view_update_cb (h->view_update_cls, ntohl (msg->num_peers), peers);
421 }
422
423
424 /**
425  * @brief Send message to service that this client does not want to receive
426  * further updates from the biased peer stream
427  *
428  * @param rps_handle The handle representing the service to the client
429  */
430 static void
431 cancel_stream (struct GNUNET_RPS_Handle *rps_handle)
432 {
433   struct GNUNET_MQ_Envelope *ev;
434
435   ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_CANCEL);
436   GNUNET_MQ_send (rps_handle->mq, ev);
437 }
438
439
440 /**
441  * @brief Cancel a specific request for updates from the biased peer stream
442  *
443  * @param srh The request handle to cancel
444  */
445 void
446 GNUNET_RPS_stream_cancel (struct GNUNET_RPS_StreamRequestHandle *srh)
447 {
448   struct GNUNET_RPS_Handle *rps_handle;
449
450   rps_handle = srh->rps_handle;
451   remove_stream_request (srh);
452   if (NULL == rps_handle->stream_requests_head)
453     cancel_stream (rps_handle);
454 }
455
456
457 /**
458  * This function is called, when the service sends another peer from the biased
459  * stream.
460  * It calls the callback the caller provided
461  * and disconnects afterwards.
462  *
463  * TODO merge with check_view_update
464  *
465  * @param msg the message
466  */
467 static int
468 check_stream_input (void *cls,
469                     const struct GNUNET_RPS_CS_DEBUG_StreamReply *msg)
470 {
471   uint16_t msize = ntohs (msg->header.size);
472   uint32_t num_peers = ntohl (msg->num_peers);
473   (void) cls;
474
475   msize -= sizeof (struct GNUNET_RPS_CS_DEBUG_StreamReply);
476   if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) ||
477        (msize % sizeof (struct GNUNET_PeerIdentity) != 0) )
478   {
479     GNUNET_break (0);
480     return GNUNET_SYSERR;
481   }
482   return GNUNET_OK;
483 }
484
485
486 /**
487  * @brief Called by the scheduler to call the callbacks of the srh handlers
488  *
489  * @param cls Stream request handle
490  */
491 static void
492 srh_callback_scheduled (void *cls)
493 {
494   struct GNUNET_RPS_StreamRequestHandle *srh = cls;
495
496   srh->callback_task = NULL;
497   srh->ready_cb (srh->ready_cb_cls,
498                  srh_callback_num_peers,
499                  srh_callback_peers);
500 }
501
502
503 /**
504  * This function is called, when the service sends another peer from the biased
505  * stream.
506  * It calls the callback the caller provided
507  * and disconnects afterwards.
508  *
509  * @param msg the message
510  */
511 static void
512 handle_stream_input (void *cls,
513                      const struct GNUNET_RPS_CS_DEBUG_StreamReply *msg)
514 {
515   struct GNUNET_RPS_Handle *h = cls;
516   //const struct GNUNET_PeerIdentity *peers;
517   uint64_t num_peers;
518   struct GNUNET_RPS_StreamRequestHandle *srh_iter;
519   struct GNUNET_RPS_StreamRequestHandle *srh_next;
520
521   //peers = (struct GNUNET_PeerIdentity *) &msg[1];
522   num_peers = ntohl (msg->num_peers);
523   srh_callback_num_peers = num_peers;
524   GNUNET_free_non_null (srh_callback_peers);
525   srh_callback_peers = GNUNET_new_array (num_peers,
526                                          struct GNUNET_PeerIdentity);
527   GNUNET_memcpy (srh_callback_peers,
528                  &msg[1],
529                  num_peers * sizeof (struct GNUNET_PeerIdentity));
530   LOG (GNUNET_ERROR_TYPE_DEBUG,
531        "Received %" PRIu64 " peer(s) from stream input.\n",
532        num_peers);
533   for (srh_iter = h->stream_requests_head;
534        NULL != srh_iter;
535        srh_iter = srh_next)
536   {
537     LOG (GNUNET_ERROR_TYPE_DEBUG, "Calling srh \n");
538     /* Store next pointer - srh might be removed/freed in callback */
539     srh_next = srh_iter->next;
540     if (NULL != srh_iter->callback_task)
541       GNUNET_SCHEDULER_cancel (srh_iter->callback_task);
542     srh_iter->callback_task =
543       GNUNET_SCHEDULER_add_now (&srh_callback_scheduled,
544                                 srh_iter);
545   }
546
547   if (NULL == h->stream_requests_head)
548   {
549     cancel_stream (h);
550   }
551 }
552
553
554 /**
555  * Reconnect to the service
556  */
557 static void
558 reconnect (struct GNUNET_RPS_Handle *h);
559
560
561 /**
562  * Error handler for mq.
563  *
564  * This function is called whan mq encounters an error.
565  * Until now mq doesn't provide useful error messages.
566  *
567  * @param cls the closure
568  * @param error error code without specyfied meaning
569  */
570 static void
571 mq_error_handler (void *cls,
572                   enum GNUNET_MQ_Error error)
573 {
574   struct GNUNET_RPS_Handle *h = cls;
575   //TODO LOG
576   LOG (GNUNET_ERROR_TYPE_WARNING, "Problem with message queue. error: %i\n\
577        1: READ,\n\
578        2: WRITE,\n\
579        4: TIMEOUT\n",
580        // TODO: write GNUNET_MQ_strerror (error)
581        error);
582   reconnect (h);
583   /* Resend all pending request as the service destroyed its knowledge
584    * about them */
585 }
586
587
588 /**
589  * @brief Create the hash value from the share value that defines the sub
590  * (-group)
591  *
592  * @param share_val Share value
593  * @param hash[out] Pointer to the location in which the hash will be stored.
594  */
595 static void
596 hash_from_share_val (const char *share_val,
597                      struct GNUNET_HashCode *hash)
598 {
599   GNUNET_CRYPTO_kdf (hash,
600                      sizeof (struct GNUNET_HashCode),
601                      "rps",
602                      strlen ("rps"),
603                      share_val,
604                      strlen (share_val),
605                      NULL, 0);
606 }
607
608
609 /**
610  * Reconnect to the service
611  */
612 static void
613 reconnect (struct GNUNET_RPS_Handle *h)
614 {
615   struct GNUNET_MQ_MessageHandler mq_handlers[] = {
616     GNUNET_MQ_hd_var_size (view_update,
617                            GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REPLY,
618                            struct GNUNET_RPS_CS_DEBUG_ViewReply,
619                            h),
620     GNUNET_MQ_hd_var_size (stream_input,
621                            GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_REPLY,
622                            struct GNUNET_RPS_CS_DEBUG_StreamReply,
623                            h),
624     GNUNET_MQ_handler_end ()
625   };
626
627   if (NULL != h->mq)
628     GNUNET_MQ_destroy (h->mq);
629   h->mq = GNUNET_CLIENT_connect (h->cfg,
630                                  "rps",
631                                  mq_handlers,
632                                  &mq_error_handler,
633                                  h);
634 }
635
636
637 /**
638  * Connect to the rps service
639  *
640  * @param cfg configuration to use
641  * @return a handle to the service
642  */
643 struct GNUNET_RPS_Handle *
644 GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
645 {
646   struct GNUNET_RPS_Handle *h;
647
648   h = GNUNET_new (struct GNUNET_RPS_Handle);
649   h->cfg = cfg;
650   reconnect (h);
651   if (NULL == h->mq)
652   {
653     GNUNET_free (h);
654     return NULL;
655   }
656   return h;
657 }
658
659
660 /**
661  * @brief Start a sub with the given shared value
662  *
663  * @param h Handle to rps
664  * @param shared_value The shared value that defines the members of the sub (-gorup)
665  */
666 void
667 GNUNET_RPS_sub_start (struct GNUNET_RPS_Handle *h,
668                       const char *shared_value)
669 {
670   struct GNUNET_RPS_CS_SubStartMessage *msg;
671   struct GNUNET_MQ_Envelope *ev;
672
673   ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_SUB_START);
674   hash_from_share_val (shared_value, &msg->hash);
675   msg->round_interval = GNUNET_TIME_relative_hton (// TODO read from config!
676     GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30));
677   GNUNET_assert (0 != msg->round_interval.rel_value_us__);
678
679   GNUNET_MQ_send (h->mq, ev);
680 }
681
682
683 /**
684  * @brief Stop a sub with the given shared value
685  *
686  * @param h Handle to rps
687  * @param shared_value The shared value that defines the members of the sub (-gorup)
688  */
689 void
690 GNUNET_RPS_sub_stop (struct GNUNET_RPS_Handle *h,
691                      const char *shared_value)
692 {
693   struct GNUNET_RPS_CS_SubStopMessage *msg;
694   struct GNUNET_MQ_Envelope *ev;
695
696   ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_SUB_STOP);
697   hash_from_share_val (shared_value, &msg->hash);
698
699   GNUNET_MQ_send (h->mq, ev);
700 }
701
702
703 /**
704  * Request n random peers.
705  *
706  * @param rps_handle handle to the rps service
707  * @param num_req_peers number of peers we want to receive
708  * @param ready_cb the callback called when the peers are available
709  * @param cls closure given to the callback
710  * @return a handle to cancel this request
711  */
712 struct GNUNET_RPS_Request_Handle *
713 GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *rps_handle,
714                           uint32_t num_req_peers,
715                           GNUNET_RPS_NotifyReadyCB ready_cb,
716                           void *cls)
717 {
718   struct GNUNET_RPS_Request_Handle *rh;
719
720   LOG (GNUNET_ERROR_TYPE_INFO,
721        "Client requested %" PRIu32 " peers\n",
722        num_req_peers);
723   rh = GNUNET_new (struct GNUNET_RPS_Request_Handle);
724   rh->rps_handle = rps_handle;
725   rh->num_requests = num_req_peers;
726   rh->sampler = RPS_sampler_mod_init (num_req_peers,
727                                       GNUNET_TIME_UNIT_SECONDS); // TODO remove this time-stuff
728   rh->sampler_rh = RPS_sampler_get_n_rand_peers (rh->sampler,
729                                                  num_req_peers,
730                                                  peers_ready_cb,
731                                                  rh);
732   rh->srh = GNUNET_RPS_stream_request (rps_handle,
733                                        collect_peers_cb,
734                                        rh); /* cls */
735   rh->ready_cb = ready_cb;
736   rh->ready_cb_cls = cls;
737
738   return rh;
739 }
740
741
742 /**
743  * Seed rps service with peerIDs.
744  *
745  * @param h handle to the rps service
746  * @param n number of peers to seed
747  * @param ids the ids of the peers seeded
748  */
749 void
750 GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h,
751                      uint32_t n,
752                      const struct GNUNET_PeerIdentity *ids)
753 {
754   size_t size_needed;
755   uint32_t num_peers_max;
756   const struct GNUNET_PeerIdentity *tmp_peer_pointer;
757   struct GNUNET_MQ_Envelope *ev;
758   struct GNUNET_RPS_CS_SeedMessage *msg;
759
760   LOG (GNUNET_ERROR_TYPE_DEBUG,
761        "Client wants to seed %" PRIu32 " peers:\n",
762        n);
763   for (unsigned int i = 0 ; i < n ; i++)
764     LOG (GNUNET_ERROR_TYPE_DEBUG,
765          "%u. peer: %s\n",
766          i,
767          GNUNET_i2s (&ids[i]));
768
769   /* The actual size the message occupies */
770   size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
771     n * sizeof (struct GNUNET_PeerIdentity);
772   /* The number of peers that fits in one message together with
773    * the respective header */
774   num_peers_max = (GNUNET_MAX_MESSAGE_SIZE -
775       sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
776     sizeof (struct GNUNET_PeerIdentity);
777   tmp_peer_pointer = ids;
778
779   while (GNUNET_MAX_MESSAGE_SIZE < size_needed)
780   {
781     ev = GNUNET_MQ_msg_extra (msg,
782                               num_peers_max * sizeof (struct GNUNET_PeerIdentity),
783                               GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
784     msg->num_peers = htonl (num_peers_max);
785     GNUNET_memcpy (&msg[1],
786                    tmp_peer_pointer,
787                    num_peers_max * sizeof (struct GNUNET_PeerIdentity));
788     GNUNET_MQ_send (h->mq,
789                     ev);
790     n -= num_peers_max;
791     size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
792                   n * sizeof (struct GNUNET_PeerIdentity);
793     /* Set pointer to beginning of next block of num_peers_max peers */
794     tmp_peer_pointer = &ids[num_peers_max];
795   }
796
797   ev = GNUNET_MQ_msg_extra (msg,
798                             n * sizeof (struct GNUNET_PeerIdentity),
799                             GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
800   msg->num_peers = htonl (n);
801   GNUNET_memcpy (&msg[1],
802                  tmp_peer_pointer,
803                  n * sizeof (struct GNUNET_PeerIdentity));
804   GNUNET_MQ_send (h->mq,
805                   ev);
806 }
807
808
809 #if ENABLE_MALICIOUS
810 /**
811  * Turn RPS service to act malicious.
812  *
813  * @param h handle to the rps service
814  * @param type which type of malicious peer to turn to.
815  *             0 Don't act malicious at all
816  *             1 Try to maximise representation
817  *             2 Try to partition the network
818  *               (isolate one peer from the rest)
819  * @param n number of @a ids
820  * @param ids the ids of the malicious peers
821  *            if @type is 2 the last id is the id of the
822  *            peer to be isolated from the rest
823  */
824 void
825 GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h,
826                           uint32_t type,
827                           uint32_t num_peers,
828                           const struct GNUNET_PeerIdentity *peer_ids,
829                           const struct GNUNET_PeerIdentity *target_peer)
830 {
831   size_t size_needed;
832   uint32_t num_peers_max;
833   const struct GNUNET_PeerIdentity *tmp_peer_pointer;
834   struct GNUNET_MQ_Envelope *ev;
835   struct GNUNET_RPS_CS_ActMaliciousMessage *msg;
836
837   unsigned int i;
838
839   LOG (GNUNET_ERROR_TYPE_DEBUG,
840        "Client turns malicious (type %" PRIu32 ") with %" PRIu32 " other peers:\n",
841        type,
842        num_peers);
843   for (i = 0 ; i < num_peers ; i++)
844     LOG (GNUNET_ERROR_TYPE_DEBUG,
845          "%u. peer: %s\n",
846          i,
847          GNUNET_i2s (&peer_ids[i]));
848
849   /* The actual size the message would occupy */
850   size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
851     num_peers * sizeof (struct GNUNET_PeerIdentity);
852   /* The number of peers that fit in one message together with
853    * the respective header */
854   num_peers_max = (GNUNET_MAX_MESSAGE_SIZE -
855       sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
856     sizeof (struct GNUNET_PeerIdentity);
857   tmp_peer_pointer = peer_ids;
858
859   while (GNUNET_MAX_MESSAGE_SIZE < size_needed)
860   {
861     LOG (GNUNET_ERROR_TYPE_DEBUG,
862          "Too many peers to send at once, sending %" PRIu32 " (all we can so far)\n",
863          num_peers_max);
864     ev = GNUNET_MQ_msg_extra (msg,
865                               num_peers_max * sizeof (struct GNUNET_PeerIdentity),
866                               GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
867     msg->type = htonl (type);
868     msg->num_peers = htonl (num_peers_max);
869     if ( (2 == type) ||
870          (3 == type) )
871       msg->attacked_peer = peer_ids[num_peers];
872     GNUNET_memcpy (&msg[1],
873             tmp_peer_pointer,
874             num_peers_max * sizeof (struct GNUNET_PeerIdentity));
875
876     GNUNET_MQ_send (h->mq, ev);
877
878     num_peers -= num_peers_max;
879     size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
880                   num_peers * sizeof (struct GNUNET_PeerIdentity);
881     /* Set pointer to beginning of next block of num_peers_max peers */
882     tmp_peer_pointer = &peer_ids[num_peers_max];
883   }
884
885   ev = GNUNET_MQ_msg_extra (msg,
886                             num_peers * sizeof (struct GNUNET_PeerIdentity),
887                             GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
888   msg->type = htonl (type);
889   msg->num_peers = htonl (num_peers);
890   if ( (2 == type) ||
891        (3 == type) )
892     msg->attacked_peer = *target_peer;
893   GNUNET_memcpy (&msg[1],
894                  tmp_peer_pointer,
895                  num_peers * sizeof (struct GNUNET_PeerIdentity));
896
897   GNUNET_MQ_send (h->mq, ev);
898 }
899 #endif /* ENABLE_MALICIOUS */
900
901
902 /**
903  * Cancle an issued request.
904  *
905  * @param rh request handle of request to cancle
906  */
907 void
908 GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh)
909 {
910   struct GNUNET_RPS_Handle *h;
911
912   h = rh->rps_handle;
913   GNUNET_assert (NULL != rh);
914   GNUNET_assert (h == rh->srh->rps_handle);
915   GNUNET_RPS_stream_cancel (rh->srh);
916   rh->srh = NULL;
917   if (NULL == h->stream_requests_head) cancel_stream(h);
918   if (NULL != rh->sampler_rh)
919   {
920     RPS_sampler_request_cancel (rh->sampler_rh);
921   }
922   RPS_sampler_destroy (rh->sampler);
923   GNUNET_free (rh);
924 }
925
926
927 /**
928  * Disconnect from the rps service
929  *
930  * @param h the handle to the rps service
931  */
932 void
933 GNUNET_RPS_disconnect (struct GNUNET_RPS_Handle *h)
934 {
935   if (NULL != h->stream_requests_head)
936   {
937     struct GNUNET_RPS_StreamRequestHandle *srh_next;
938
939     LOG (GNUNET_ERROR_TYPE_WARNING,
940         "Still waiting for replies\n");
941     for (struct GNUNET_RPS_StreamRequestHandle *srh_iter = h->stream_requests_head;
942          NULL != srh_iter;
943          srh_iter = srh_next)
944     {
945       srh_next = srh_iter->next;
946       GNUNET_RPS_stream_cancel (srh_iter);
947     }
948   }
949   if (NULL != srh_callback_peers)
950   {
951     GNUNET_free (srh_callback_peers);
952     srh_callback_peers = NULL;
953   }
954   if (NULL != h->view_update_cb)
955   {
956     LOG (GNUNET_ERROR_TYPE_WARNING,
957         "Still waiting for view updates\n");
958     GNUNET_RPS_view_request_cancel (h);
959   }
960   GNUNET_MQ_destroy (h->mq);
961   GNUNET_free (h);
962 }
963
964
965 /* end of rps_api.c */