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