Merge branch 'master' of git+ssh://gnunet.org/gnunet
[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 "rps-sampler_client.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 void
348 GNUNET_RPS_view_request_cancel (struct GNUNET_RPS_Handle *rps_handle)
349 {
350   struct GNUNET_MQ_Envelope *ev;
351
352   GNUNET_assert (NULL != rps_handle->view_update_cb);
353
354   rps_handle->view_update_cb = NULL;
355
356   ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_CANCEL);
357   GNUNET_MQ_send (rps_handle->mq, ev);
358 }
359
360
361 /**
362  * Request biased stream of peers that are being put into the sampler
363  *
364  * @param rps_handle handle to the rps service
365  * @param num_req_peers number of peers we want to receive
366  *        (0 for infinite updates)
367  * @param cls a closure that will be given to the callback
368  * @param ready_cb the callback called when the peers are available
369  */
370 struct GNUNET_RPS_StreamRequestHandle *
371 GNUNET_RPS_stream_request (struct GNUNET_RPS_Handle *rps_handle,
372                            uint32_t num_peers,
373                            GNUNET_RPS_NotifyReadyCB stream_input_cb,
374                            void *cls)
375 {
376   struct GNUNET_RPS_StreamRequestHandle *srh;
377   struct GNUNET_MQ_Envelope *ev;
378   struct GNUNET_RPS_CS_DEBUG_StreamRequest *msg;
379
380   srh = new_stream_request (rps_handle,
381                             num_peers, /* num requests */
382                             stream_input_cb,
383                             cls);
384   LOG (GNUNET_ERROR_TYPE_DEBUG,
385        "Client requests %" PRIu32 " biased stream updates\n",
386        num_peers);
387
388   ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_REQUEST);
389   GNUNET_MQ_send (rps_handle->mq, ev);
390   return srh;
391 }
392
393
394 /**
395  * This function is called, when the service updates the view.
396  * It verifies that @a msg is well-formed.
397  *
398  * @param cls the closure
399  * @param msg the message
400  * @return #GNUNET_OK if @a msg is well-formed
401  */
402 static int
403 check_view_update (void *cls,
404                    const struct GNUNET_RPS_CS_DEBUG_ViewReply *msg)
405 {
406   uint16_t msize = ntohs (msg->header.size);
407   uint32_t num_peers = ntohl (msg->num_peers);
408   (void) cls;
409
410   msize -= sizeof (struct GNUNET_RPS_CS_DEBUG_ViewReply);
411   if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) ||
412        (msize % sizeof (struct GNUNET_PeerIdentity) != 0) )
413   {
414     GNUNET_break (0);
415     return GNUNET_SYSERR;
416   }
417   return GNUNET_OK;
418 }
419
420
421 /**
422  * This function is called, when the service updated its view.
423  * It calls the callback the caller provided
424  * and disconnects afterwards.
425  *
426  * @param msg the message
427  */
428 static void
429 handle_view_update (void *cls,
430                     const struct GNUNET_RPS_CS_DEBUG_ViewReply *msg)
431 {
432   struct GNUNET_RPS_Handle *h = cls;
433   struct GNUNET_PeerIdentity *peers;
434
435   /* Give the peers back */
436   LOG (GNUNET_ERROR_TYPE_DEBUG,
437        "New view of %" PRIu32 " peers:\n",
438        ntohl (msg->num_peers));
439
440   peers = (struct GNUNET_PeerIdentity *) &msg[1];
441   GNUNET_assert (NULL != h);
442   GNUNET_assert (NULL != h->view_update_cb);
443   h->view_update_cb (h->view_update_cls, ntohl (msg->num_peers), peers);
444 }
445
446
447 /**
448  * @brief Send message to service that this client does not want to receive
449  * further updates from the biased peer stream
450  *
451  * @param rps_handle The handle representing the service to the client
452  */
453 static void
454 cancel_stream (struct GNUNET_RPS_Handle *rps_handle)
455 {
456   struct GNUNET_MQ_Envelope *ev;
457
458   ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_CANCEL);
459   GNUNET_MQ_send (rps_handle->mq, ev);
460 }
461
462
463 /**
464  * @brief Cancel a specific request for updates from the biased peer stream
465  *
466  * @param srh The request handle to cancel
467  */
468 void
469 GNUNET_RPS_stream_cancel (struct GNUNET_RPS_StreamRequestHandle *srh)
470 {
471   struct GNUNET_RPS_Handle *rps_handle;
472
473   rps_handle = srh->rps_handle;
474   GNUNET_CONTAINER_DLL_remove (rps_handle->stream_requests_head,
475                                rps_handle->stream_requests_tail,
476                                srh);
477   GNUNET_free (srh);
478   if (NULL == rps_handle->stream_requests_head) cancel_stream (rps_handle);
479 }
480
481
482 /**
483  * This function is called, when the service sends another peer from the biased
484  * stream.
485  * It calls the callback the caller provided
486  * and disconnects afterwards.
487  *
488  * TODO merge with check_view_update
489  *
490  * @param msg the message
491  */
492 static int
493 check_stream_input (void *cls,
494                     const struct GNUNET_RPS_CS_DEBUG_StreamReply *msg)
495 {
496   uint16_t msize = ntohs (msg->header.size);
497   uint32_t num_peers = ntohl (msg->num_peers);
498   (void) cls;
499
500   msize -= sizeof (struct GNUNET_RPS_CS_DEBUG_StreamReply);
501   if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) ||
502        (msize % sizeof (struct GNUNET_PeerIdentity) != 0) )
503   {
504     GNUNET_break (0);
505     return GNUNET_SYSERR;
506   }
507   return GNUNET_OK;
508 }
509
510 /**
511  * This function is called, when the service sends another peer from the biased
512  * stream.
513  * It calls the callback the caller provided
514  * and disconnects afterwards.
515  *
516  * @param msg the message
517  */
518 static void
519 handle_stream_input (void *cls,
520                      const struct GNUNET_RPS_CS_DEBUG_StreamReply *msg)
521 {
522   struct GNUNET_RPS_Handle *h = cls;
523   const struct GNUNET_PeerIdentity *peers;
524   /* The following two pointers are used to prevent that new handles are
525    * inserted into the DLL, that is currently iterated over, from within a call
526    * to that handler_cb, are executed and in turn again add themselves to the
527    * iterated DLL infinitely */
528   struct GNUNET_RPS_StreamRequestHandle *srh_head_tmp;
529   struct GNUNET_RPS_StreamRequestHandle *srh_tail_tmp;
530   uint64_t num_peers;
531   uint64_t num_peers_return;
532
533   peers = (struct GNUNET_PeerIdentity *) &msg[1];
534   num_peers = ntohl (msg->num_peers);
535   LOG (GNUNET_ERROR_TYPE_DEBUG,
536        "Received %" PRIu64 " peer(s) from stream input.\n",
537        num_peers);
538   srh_head_tmp = h->stream_requests_head;
539   srh_tail_tmp = h->stream_requests_tail;
540   h->stream_requests_head = NULL;
541   h->stream_requests_tail = NULL;
542   for (struct GNUNET_RPS_StreamRequestHandle *srh_iter = srh_head_tmp;
543        NULL != srh_iter;
544        srh_iter = srh_iter->next)
545   {
546     LOG (GNUNET_ERROR_TYPE_DEBUG,
547         "Calling srh - left: %" PRIu64 "\n",
548         srh_iter->num_peers_left);
549     if (0 == srh_iter->num_peers_left) /* infinite updates */
550     {
551       num_peers_return = num_peers;
552     }
553     else if (num_peers > srh_iter->num_peers_left)
554     {
555       num_peers_return = num_peers - srh_iter->num_peers_left;
556     }
557     else /* num_peers <= srh_iter->num_peers_left */
558     {
559       num_peers_return = srh_iter->num_peers_left - num_peers;
560     }
561     srh_iter->ready_cb (srh_iter->ready_cb_cls,
562                         num_peers_return,
563                         peers);
564     if (0 == srh_iter->num_peers_left) ;
565     else if (num_peers_return >= srh_iter->num_peers_left)
566     {
567       remove_stream_request (srh_iter,
568                              srh_head_tmp,
569                              srh_tail_tmp);
570     }
571     else
572     {
573       srh_iter->num_peers_left -= num_peers_return;
574     }
575   }
576   for (struct GNUNET_RPS_StreamRequestHandle *srh_iter = srh_head_tmp;
577        NULL != srh_iter;
578        srh_iter = srh_iter->next)
579   {
580       GNUNET_CONTAINER_DLL_insert (h->stream_requests_head,
581                                    h->stream_requests_tail,
582                                    srh_iter);
583   }
584
585   if (NULL == h->stream_requests_head)
586   {
587     cancel_stream (h);
588   }
589 }
590
591
592 /**
593  * Reconnect to the service
594  */
595 static void
596 reconnect (struct GNUNET_RPS_Handle *h);
597
598
599 /**
600  * Error handler for mq.
601  *
602  * This function is called whan mq encounters an error.
603  * Until now mq doesn't provide useful error messages.
604  *
605  * @param cls the closure
606  * @param error error code without specyfied meaning
607  */
608 static void
609 mq_error_handler (void *cls,
610                   enum GNUNET_MQ_Error error)
611 {
612   struct GNUNET_RPS_Handle *h = cls;
613   //TODO LOG
614   LOG (GNUNET_ERROR_TYPE_WARNING, "Problem with message queue. error: %i\n\
615        1: READ,\n\
616        2: WRITE,\n\
617        4: TIMEOUT\n",
618        error);
619   reconnect (h);
620   /* Resend all pending request as the service destroyed its knowledge
621    * about them */
622 }
623
624
625 /**
626  * Reconnect to the service
627  */
628 static void
629 reconnect (struct GNUNET_RPS_Handle *h)
630 {
631   struct GNUNET_MQ_MessageHandler mq_handlers[] = {
632     GNUNET_MQ_hd_var_size (view_update,
633                            GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REPLY,
634                            struct GNUNET_RPS_CS_DEBUG_ViewReply,
635                            h),
636     GNUNET_MQ_hd_var_size (stream_input,
637                            GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_REPLY,
638                            struct GNUNET_RPS_CS_DEBUG_StreamReply,
639                            h),
640     GNUNET_MQ_handler_end ()
641   };
642
643   if (NULL != h->mq)
644     GNUNET_MQ_destroy (h->mq);
645   h->mq = GNUNET_CLIENT_connect (h->cfg,
646                                  "rps",
647                                  mq_handlers,
648                                  &mq_error_handler,
649                                  h);
650 }
651
652
653 /**
654  * Connect to the rps service
655  *
656  * @param cfg configuration to use
657  * @return a handle to the service
658  */
659 struct GNUNET_RPS_Handle *
660 GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
661 {
662   struct GNUNET_RPS_Handle *h;
663
664   h = GNUNET_new (struct GNUNET_RPS_Handle);
665   h->cfg = cfg;
666   reconnect (h);
667   if (NULL == h->mq)
668   {
669     GNUNET_free (h);
670     return NULL;
671   }
672   return h;
673 }
674
675
676 /**
677  * Request n random peers.
678  *
679  * @param rps_handle handle to the rps service
680  * @param num_req_peers number of peers we want to receive
681  * @param ready_cb the callback called when the peers are available
682  * @param cls closure given to the callback
683  * @return a handle to cancel this request
684  */
685 struct GNUNET_RPS_Request_Handle *
686 GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *rps_handle,
687                           uint32_t num_req_peers,
688                           GNUNET_RPS_NotifyReadyCB ready_cb,
689                           void *cls)
690 {
691   struct GNUNET_RPS_Request_Handle *rh;
692
693   rh = new_request_handle (rps_handle,
694                            num_req_peers,
695                            ready_cb,
696                            cls);
697
698
699   return rh;
700 }
701
702
703 /**
704  * Seed rps service with peerIDs.
705  *
706  * @param h handle to the rps service
707  * @param n number of peers to seed
708  * @param ids the ids of the peers seeded
709  */
710 void
711 GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h,
712                      uint32_t n,
713                      const struct GNUNET_PeerIdentity *ids)
714 {
715   size_t size_needed;
716   uint32_t num_peers_max;
717   const struct GNUNET_PeerIdentity *tmp_peer_pointer;
718   struct GNUNET_MQ_Envelope *ev;
719   struct GNUNET_RPS_CS_SeedMessage *msg;
720
721   unsigned int i;
722
723   LOG (GNUNET_ERROR_TYPE_DEBUG,
724        "Client wants to seed %" PRIu32 " peers:\n",
725        n);
726   for (i = 0 ; i < n ; i++)
727     LOG (GNUNET_ERROR_TYPE_DEBUG,
728          "%u. peer: %s\n",
729          i,
730          GNUNET_i2s (&ids[i]));
731
732   /* The actual size the message occupies */
733   size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
734     n * sizeof (struct GNUNET_PeerIdentity);
735   /* The number of peers that fits in one message together with
736    * the respective header */
737   num_peers_max = (GNUNET_MAX_MESSAGE_SIZE -
738       sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
739     sizeof (struct GNUNET_PeerIdentity);
740   tmp_peer_pointer = ids;
741
742   while (GNUNET_MAX_MESSAGE_SIZE < size_needed)
743   {
744     ev = GNUNET_MQ_msg_extra (msg, num_peers_max * sizeof (struct GNUNET_PeerIdentity),
745         GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
746     msg->num_peers = htonl (num_peers_max);
747     GNUNET_memcpy (&msg[1], tmp_peer_pointer, num_peers_max * sizeof (struct GNUNET_PeerIdentity));
748     GNUNET_MQ_send (h->mq, ev);
749
750     n -= num_peers_max;
751     size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
752                   n * sizeof (struct GNUNET_PeerIdentity);
753     /* Set pointer to beginning of next block of num_peers_max peers */
754     tmp_peer_pointer = &ids[num_peers_max];
755   }
756
757   ev = GNUNET_MQ_msg_extra (msg, n * sizeof (struct GNUNET_PeerIdentity),
758                             GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
759   msg->num_peers = htonl (n);
760   GNUNET_memcpy (&msg[1], tmp_peer_pointer, n * sizeof (struct GNUNET_PeerIdentity));
761
762   GNUNET_MQ_send (h->mq, ev);
763 }
764
765
766 #ifdef ENABLE_MALICIOUS
767 /**
768  * Turn RPS service to act malicious.
769  *
770  * @param h handle to the rps service
771  * @param type which type of malicious peer to turn to.
772  *             0 Don't act malicious at all
773  *             1 Try to maximise representation
774  *             2 Try to partition the network
775  *               (isolate one peer from the rest)
776  * @param n number of @a ids
777  * @param ids the ids of the malicious peers
778  *            if @type is 2 the last id is the id of the
779  *            peer to be isolated from the rest
780  */
781 void
782 GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h,
783                           uint32_t type,
784                           uint32_t num_peers,
785                           const struct GNUNET_PeerIdentity *peer_ids,
786                           const struct GNUNET_PeerIdentity *target_peer)
787 {
788   size_t size_needed;
789   uint32_t num_peers_max;
790   const struct GNUNET_PeerIdentity *tmp_peer_pointer;
791   struct GNUNET_MQ_Envelope *ev;
792   struct GNUNET_RPS_CS_ActMaliciousMessage *msg;
793
794   unsigned int i;
795
796   LOG (GNUNET_ERROR_TYPE_DEBUG,
797        "Client turns malicious (type %" PRIu32 ") with %" PRIu32 " other peers:\n",
798        type,
799        num_peers);
800   for (i = 0 ; i < num_peers ; i++)
801     LOG (GNUNET_ERROR_TYPE_DEBUG,
802          "%u. peer: %s\n",
803          i,
804          GNUNET_i2s (&peer_ids[i]));
805
806   /* The actual size the message would occupy */
807   size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
808     num_peers * sizeof (struct GNUNET_PeerIdentity);
809   /* The number of peers that fit in one message together with
810    * the respective header */
811   num_peers_max = (GNUNET_MAX_MESSAGE_SIZE -
812       sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
813     sizeof (struct GNUNET_PeerIdentity);
814   tmp_peer_pointer = peer_ids;
815
816   while (GNUNET_MAX_MESSAGE_SIZE < size_needed)
817   {
818     LOG (GNUNET_ERROR_TYPE_DEBUG,
819          "Too many peers to send at once, sending %" PRIu32 " (all we can so far)\n",
820          num_peers_max);
821     ev = GNUNET_MQ_msg_extra (msg,
822                               num_peers_max * sizeof (struct GNUNET_PeerIdentity),
823                               GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
824     msg->type = htonl (type);
825     msg->num_peers = htonl (num_peers_max);
826     if ( (2 == type) ||
827          (3 == type) )
828       msg->attacked_peer = peer_ids[num_peers];
829     GNUNET_memcpy (&msg[1],
830             tmp_peer_pointer,
831             num_peers_max * sizeof (struct GNUNET_PeerIdentity));
832
833     GNUNET_MQ_send (h->mq, ev);
834
835     num_peers -= num_peers_max;
836     size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
837                   num_peers * sizeof (struct GNUNET_PeerIdentity);
838     /* Set pointer to beginning of next block of num_peers_max peers */
839     tmp_peer_pointer = &peer_ids[num_peers_max];
840   }
841
842   ev = GNUNET_MQ_msg_extra (msg,
843                             num_peers * sizeof (struct GNUNET_PeerIdentity),
844                             GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
845   msg->type = htonl (type);
846   msg->num_peers = htonl (num_peers);
847   if ( (2 == type) ||
848        (3 == type) )
849     msg->attacked_peer = *target_peer;
850   GNUNET_memcpy (&msg[1], tmp_peer_pointer, num_peers * sizeof (struct GNUNET_PeerIdentity));
851
852   GNUNET_MQ_send (h->mq, ev);
853 }
854 #endif /* ENABLE_MALICIOUS */
855
856
857 /**
858  * Cancle an issued request.
859  *
860  * @param rh request handle of request to cancle
861  */
862 void
863 GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh)
864 {
865   struct GNUNET_RPS_Handle *h;
866
867   h = rh->rps_handle;
868   if (NULL != rh->srh)
869   {
870     remove_stream_request (rh->srh,
871                            h->stream_requests_head,
872                            h->stream_requests_tail);
873   }
874   if (NULL == h->stream_requests_head) cancel_stream(h);
875   if (NULL != rh->sampler_rh)
876   {
877     RPS_sampler_request_cancel (rh->sampler_rh);
878   }
879   RPS_sampler_destroy (rh->sampler);
880   GNUNET_free (rh);
881 }
882
883
884 /**
885  * Disconnect from the rps service
886  *
887  * @param h the handle to the rps service
888  */
889 void
890 GNUNET_RPS_disconnect (struct GNUNET_RPS_Handle *h)
891 {
892   GNUNET_MQ_destroy (h->mq);
893   if (NULL != h->stream_requests_head)
894   {
895     LOG (GNUNET_ERROR_TYPE_WARNING,
896         "Still waiting for replies\n");
897   }
898   if (NULL != h->view_update_cb)
899   {
900     LOG (GNUNET_ERROR_TYPE_WARNING,
901         "Still waiting for view updates\n");
902   }
903   GNUNET_free (h);
904 }
905
906
907 /* end of rps_api.c */