Be a little bit more consistent in style
[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
29 #include <inttypes.h>
30
31 #define LOG(kind,...) GNUNET_log_from (kind, "rps-api",__VA_ARGS__)
32
33 /**
34  * Handler to handle requests from a client.
35  */
36 struct GNUNET_RPS_Handle
37 {
38   /**
39    * The handle to the client configuration.
40    */
41   const struct GNUNET_CONFIGURATION_Handle *cfg;
42
43   /**
44    * The message queue to the client.
45    */
46   struct GNUNET_MQ_Handle *mq;
47
48   /**
49    * Array of Request_Handles.
50    */
51   struct GNUNET_CONTAINER_MultiHashMap32 *req_handlers;
52
53   /**
54    * The id of the last request.
55    */
56   uint32_t current_request_id;
57
58   /**
59    * @brief Callback called on each update of the view
60    */
61   GNUNET_RPS_NotifyReadyCB view_update_cb;
62
63   /**
64    * @brief Closure to each requested update of the view
65    */
66   void *view_update_cls;
67
68   /**
69    * @brief Callback called on each peer of the biased input stream
70    */
71   GNUNET_RPS_NotifyReadyCB stream_input_cb;
72
73   /**
74    * @brief Closure to each requested peer from the biased stream
75    */
76   void *stream_input_cls;
77 };
78
79
80 /**
81  * Handler for a single request from a client.
82  */
83 struct GNUNET_RPS_Request_Handle
84 {
85   /**
86    * The client issuing the request.
87    */
88   struct GNUNET_RPS_Handle *rps_handle;
89
90   /**
91    * The id of the request.
92    */
93   uint32_t id;
94
95   /**
96    * The number of requested peers.
97    */
98   uint32_t num_requests;
99
100   /**
101    * @brief The Sampler for the client request
102    */
103   struct RPS_Sampler *sampler;
104
105   /**
106    * The callback to be called when we receive an answer.
107    */
108   GNUNET_RPS_NotifyReadyCB ready_cb;
109
110   /**
111    * The closure for the callback.
112    */
113   void *ready_cb_cls;
114 };
115
116
117 /**
118  * Struct used to pack the callback, its closure (provided by the caller)
119  * and the connection handler to the service to pass it to a callback function.
120  */
121 struct cb_cls_pack
122 {
123   /**
124    * Callback provided by the client
125    */
126   GNUNET_RPS_NotifyReadyCB cb;
127
128   /**
129    * Closure provided by the client
130    */
131   void *cls;
132
133   /**
134    * Handle to the service connection
135    */
136  struct GNUNET_CLIENT_Connection *service_conn;
137 };
138
139
140 /**
141  * @brief Send a request to the service.
142  *
143  * @param h rps handle
144  * @param id id of the request
145  * @param num_req_peers number of peers
146  */
147 void
148 send_request (const struct GNUNET_RPS_Handle *h,
149               uint32_t id,
150               uint32_t num_req_peers)
151 {
152   struct GNUNET_MQ_Envelope *ev;
153   struct GNUNET_RPS_CS_RequestMessage *msg;
154
155   ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST);
156   msg->num_peers = htonl (num_req_peers);
157   msg->id = htonl (id);
158   GNUNET_MQ_send (h->mq, ev);
159 }
160
161 /**
162  * @brief Iterator function over pending requests
163  *
164  * Implements #GNUNET_CONTAINER_HashMapIterator32
165  *
166  * @param cls rps handle
167  * @param key id of the request
168  * @param value request handle
169  *
170  * @return GNUNET_YES to continue iteration
171  */
172 int
173 resend_requests_iterator (void *cls, uint32_t key, void *value)
174 {
175   const struct GNUNET_RPS_Handle *h = cls;
176   const struct GNUNET_RPS_Request_Handle *req_handle = value;
177   (void) key;
178
179   send_request (h, req_handle->id, req_handle->num_requests);
180   return GNUNET_YES; /* continue iterating */
181 }
182
183 /**
184  * @brief Resend all pending requests
185  *
186  * This is used to resend all pending requests after the client
187  * reconnected to the service, because the service cancels all
188  * pending requests after reconnection.
189  *
190  * @param h rps handle
191  */
192 void
193 resend_requests (struct GNUNET_RPS_Handle *h)
194 {
195   GNUNET_CONTAINER_multihashmap32_iterate (h->req_handlers,
196                                            resend_requests_iterator,
197                                            h);
198 }
199
200
201 /**
202  * This function is called, when the service replies to our request.
203  * It verifies that @a msg is well-formed.
204  *
205  * @param cls the closure
206  * @param msg the message
207  * @return #GNUNET_OK if @a msg is well-formed
208  */
209 static int
210 check_reply (void *cls,
211              const struct GNUNET_RPS_CS_ReplyMessage *msg)
212 {
213   uint16_t msize = ntohs (msg->header.size);
214   uint32_t num_peers = ntohl (msg->num_peers);
215   (void) cls;
216
217   msize -= sizeof (struct GNUNET_RPS_CS_ReplyMessage);
218   if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) ||
219        (msize % sizeof (struct GNUNET_PeerIdentity) != 0) )
220   {
221     GNUNET_break (0);
222     return GNUNET_SYSERR;
223   }
224   return GNUNET_OK;
225 }
226
227
228 /**
229  * This function is called, when the service replies to our request.
230  * It calls the callback the caller gave us with the provided closure
231  * and disconnects afterwards.
232  *
233  * @param cls the closure
234  * @param msg the message
235  */
236 static void
237 handle_reply (void *cls,
238               const struct GNUNET_RPS_CS_ReplyMessage *msg)
239 {
240   struct GNUNET_RPS_Handle *h = cls;
241   struct GNUNET_PeerIdentity *peers;
242   struct GNUNET_RPS_Request_Handle *rh;
243   uint32_t id;
244
245   /* Give the peers back */
246   id = ntohl (msg->id);
247   LOG (GNUNET_ERROR_TYPE_DEBUG,
248        "Service replied with %" PRIu32 " peers for id %" PRIu32 "\n",
249        ntohl (msg->num_peers),
250        id);
251
252   peers = (struct GNUNET_PeerIdentity *) &msg[1];
253   GNUNET_assert (GNUNET_YES ==
254       GNUNET_CONTAINER_multihashmap32_contains (h->req_handlers, id));
255   rh = GNUNET_CONTAINER_multihashmap32_get (h->req_handlers, id);
256   GNUNET_assert (NULL != rh);
257   GNUNET_assert (rh->num_requests == ntohl (msg->num_peers));
258   GNUNET_CONTAINER_multihashmap32_remove_all (h->req_handlers, id);
259   rh->ready_cb (rh->ready_cb_cls,
260                 ntohl (msg->num_peers),
261                 peers);
262 }
263
264
265 /* Get internals for debugging/profiling purposes */
266
267 /**
268  * Request updates of view
269  *
270  * @param rps_handle handle to the rps service
271  * @param num_req_peers number of peers we want to receive
272  *        (0 for infinite updates)
273  * @param cls a closure that will be given to the callback
274  * @param ready_cb the callback called when the peers are available
275  */
276 void
277 GNUNET_RPS_view_request (struct GNUNET_RPS_Handle *rps_handle,
278                          uint32_t num_updates,
279                          GNUNET_RPS_NotifyReadyCB view_update_cb,
280                          void *cls)
281 {
282   struct GNUNET_MQ_Envelope *ev;
283   struct GNUNET_RPS_CS_DEBUG_ViewRequest *msg;
284
285   LOG (GNUNET_ERROR_TYPE_DEBUG,
286        "Client requests %" PRIu32 " view updates\n",
287        num_updates);
288   rps_handle->view_update_cb = view_update_cb;
289   rps_handle->view_update_cls = cls;
290
291   ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REQUEST);
292   msg->num_updates = htonl (num_updates);
293   GNUNET_MQ_send (rps_handle->mq, ev);
294 }
295
296
297 /**
298  * Request biased stream of peers that are being put into the sampler
299  *
300  * @param rps_handle handle to the rps service
301  * @param num_req_peers number of peers we want to receive
302  *        (0 for infinite updates)
303  * @param cls a closure that will be given to the callback
304  * @param ready_cb the callback called when the peers are available
305  */
306 void
307 GNUNET_RPS_stream_request (struct GNUNET_RPS_Handle *rps_handle,
308                            uint32_t num_peers,
309                            GNUNET_RPS_NotifyReadyCB stream_input_cb,
310                            void *cls)
311 {
312   struct GNUNET_MQ_Envelope *ev;
313   struct GNUNET_RPS_CS_DEBUG_StreamRequest *msg;
314
315   LOG (GNUNET_ERROR_TYPE_DEBUG,
316        "Client requests %" PRIu32 " biased stream updates\n",
317        num_peers);
318   rps_handle->stream_input_cb = stream_input_cb;
319   rps_handle->stream_input_cls = cls;
320
321   ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_REQUEST);
322   msg->num_peers = htonl (num_peers);
323   GNUNET_MQ_send (rps_handle->mq, ev);
324 }
325
326
327 /**
328  * This function is called, when the service updates the view.
329  * It verifies that @a msg is well-formed.
330  *
331  * @param cls the closure
332  * @param msg the message
333  * @return #GNUNET_OK if @a msg is well-formed
334  */
335 static int
336 check_view_update (void *cls,
337                    const struct GNUNET_RPS_CS_DEBUG_ViewReply *msg)
338 {
339   uint16_t msize = ntohs (msg->header.size);
340   uint32_t num_peers = ntohl (msg->num_peers);
341   (void) cls;
342
343   msize -= sizeof (struct GNUNET_RPS_CS_DEBUG_ViewReply);
344   if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) ||
345        (msize % sizeof (struct GNUNET_PeerIdentity) != 0) )
346   {
347     GNUNET_break (0);
348     return GNUNET_SYSERR;
349   }
350   return GNUNET_OK;
351 }
352
353
354 /**
355  * This function is called, when the service updated its view.
356  * It calls the callback the caller provided
357  * and disconnects afterwards.
358  *
359  * @param msg the message
360  */
361 static void
362 handle_view_update (void *cls,
363                     const struct GNUNET_RPS_CS_DEBUG_ViewReply *msg)
364 {
365   struct GNUNET_RPS_Handle *h = cls;
366   struct GNUNET_PeerIdentity *peers;
367
368   /* Give the peers back */
369   LOG (GNUNET_ERROR_TYPE_DEBUG,
370        "New view of %" PRIu32 " peers:\n",
371        ntohl (msg->num_peers));
372
373   peers = (struct GNUNET_PeerIdentity *) &msg[1];
374   GNUNET_assert (NULL != h);
375   GNUNET_assert (NULL != h->view_update_cb);
376   h->view_update_cb (h->view_update_cls, ntohl (msg->num_peers), peers);
377 }
378
379
380 /**
381  * This function is called, when the service sends another peer from the biased
382  * stream.
383  * It calls the callback the caller provided
384  * and disconnects afterwards.
385  *
386  * TODO merge with check_view_update
387  *
388  * @param msg the message
389  */
390 static int
391 check_stream_input (void *cls,
392                     const struct GNUNET_RPS_CS_DEBUG_StreamReply *msg)
393 {
394   uint16_t msize = ntohs (msg->header.size);
395   uint32_t num_peers = ntohl (msg->num_peers);
396   (void) cls;
397
398   msize -= sizeof (struct GNUNET_RPS_CS_DEBUG_StreamReply);
399   if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) ||
400        (msize % sizeof (struct GNUNET_PeerIdentity) != 0) )
401   {
402     GNUNET_break (0);
403     return GNUNET_SYSERR;
404   }
405   return GNUNET_OK;
406 }
407
408 /**
409  * This function is called, when the service sends another peer from the biased
410  * stream.
411  * It calls the callback the caller provided
412  * and disconnects afterwards.
413  *
414  * @param msg the message
415  */
416 static void
417 handle_stream_input (void *cls,
418                      const struct GNUNET_RPS_CS_DEBUG_StreamReply *msg)
419 {
420   struct GNUNET_RPS_Handle *h = cls;
421   const struct GNUNET_PeerIdentity *peers;
422
423   /* Give the peers back */
424   LOG (GNUNET_ERROR_TYPE_DEBUG,
425        "New peer of %" PRIu64 " biased input stream\n",
426        ntohl (msg->num_peers));
427
428   peers = (struct GNUNET_PeerIdentity *) &msg[1];
429   GNUNET_assert (NULL != h);
430   GNUNET_assert (NULL != h->stream_input_cb);
431   h->stream_input_cb (h->stream_input_cls, ntohl (msg->num_peers), peers);
432 }
433
434
435 /**
436  * Reconnect to the service
437  */
438 static void
439 reconnect (struct GNUNET_RPS_Handle *h);
440
441
442 /**
443  * Error handler for mq.
444  *
445  * This function is called whan mq encounters an error.
446  * Until now mq doesn't provide useful error messages.
447  *
448  * @param cls the closure
449  * @param error error code without specyfied meaning
450  */
451 static void
452 mq_error_handler (void *cls,
453                   enum GNUNET_MQ_Error error)
454 {
455   struct GNUNET_RPS_Handle *h = cls;
456   //TODO LOG
457   LOG (GNUNET_ERROR_TYPE_WARNING, "Problem with message queue. error: %i\n\
458        1: READ,\n\
459        2: WRITE,\n\
460        4: TIMEOUT\n",
461        error);
462   reconnect (h);
463   /* Resend all pending request as the service destroyed its knowledge
464    * about them */
465   resend_requests (h);
466 }
467
468
469 /**
470  * Reconnect to the service
471  */
472 static void
473 reconnect (struct GNUNET_RPS_Handle *h)
474 {
475   struct GNUNET_MQ_MessageHandler mq_handlers[] = {
476     GNUNET_MQ_hd_var_size (reply,
477                            GNUNET_MESSAGE_TYPE_RPS_CS_REPLY,
478                            struct GNUNET_RPS_CS_ReplyMessage,
479                            h),
480     GNUNET_MQ_hd_var_size (view_update,
481                            GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REPLY,
482                            struct GNUNET_RPS_CS_DEBUG_ViewReply,
483                            h),
484     GNUNET_MQ_hd_var_size (stream_input,
485                            GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_REPLY,
486                            struct GNUNET_RPS_CS_DEBUG_StreamReply,
487                            h),
488     GNUNET_MQ_handler_end ()
489   };
490
491   if (NULL != h->mq)
492     GNUNET_MQ_destroy (h->mq);
493   h->mq = GNUNET_CLIENT_connect (h->cfg,
494                                  "rps",
495                                  mq_handlers,
496                                  &mq_error_handler,
497                                  h);
498 }
499
500
501 /**
502  * Connect to the rps service
503  *
504  * @param cfg configuration to use
505  * @return a handle to the service
506  */
507 struct GNUNET_RPS_Handle *
508 GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
509 {
510   struct GNUNET_RPS_Handle *h;
511
512   h = GNUNET_new (struct GNUNET_RPS_Handle);
513   h->current_request_id = 0;
514   h->cfg = cfg;
515   reconnect (h);
516   if (NULL == h->mq)
517   {
518     GNUNET_free (h);
519     return NULL;
520   }
521   h->req_handlers = GNUNET_CONTAINER_multihashmap32_create (4);
522   return h;
523 }
524
525
526 /**
527  * Request n random peers.
528  *
529  * @param rps_handle handle to the rps service
530  * @param num_req_peers number of peers we want to receive
531  * @param ready_cb the callback called when the peers are available
532  * @param cls closure given to the callback
533  * @return a handle to cancel this request
534  */
535 struct GNUNET_RPS_Request_Handle *
536 GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *rps_handle,
537                           uint32_t num_req_peers,
538                           GNUNET_RPS_NotifyReadyCB ready_cb,
539                           void *cls)
540 {
541   struct GNUNET_RPS_Request_Handle *rh;
542
543   rh = GNUNET_new (struct GNUNET_RPS_Request_Handle);
544   rh->rps_handle = rps_handle;
545   rh->id = rps_handle->current_request_id++;
546   rh->num_peers = num_req_peers;
547   rh->ready_cb = ready_cb;
548   rh->ready_cb_cls = cls;
549
550   LOG (GNUNET_ERROR_TYPE_DEBUG,
551        "Requesting %" PRIu32 " peers with id %" PRIu32 "\n",
552        num_req_peers,
553        rh->id);
554
555   GNUNET_CONTAINER_multihashmap32_put (rps_handle->req_handlers, rh->id, rh,
556       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
557
558   send_request (rps_handle, rh->id, num_req_peers);
559   return rh;
560 }
561
562
563 /**
564  * Seed rps service with peerIDs.
565  *
566  * @param h handle to the rps service
567  * @param n number of peers to seed
568  * @param ids the ids of the peers seeded
569  */
570 void
571 GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h,
572                      uint32_t n,
573                      const struct GNUNET_PeerIdentity *ids)
574 {
575   size_t size_needed;
576   uint32_t num_peers_max;
577   const struct GNUNET_PeerIdentity *tmp_peer_pointer;
578   struct GNUNET_MQ_Envelope *ev;
579   struct GNUNET_RPS_CS_SeedMessage *msg;
580
581   unsigned int i;
582
583   LOG (GNUNET_ERROR_TYPE_DEBUG,
584        "Client wants to seed %" PRIu32 " peers:\n",
585        n);
586   for (i = 0 ; i < n ; i++)
587     LOG (GNUNET_ERROR_TYPE_DEBUG,
588          "%u. peer: %s\n",
589          i,
590          GNUNET_i2s (&ids[i]));
591
592   /* The actual size the message occupies */
593   size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
594     n * sizeof (struct GNUNET_PeerIdentity);
595   /* The number of peers that fits in one message together with
596    * the respective header */
597   num_peers_max = (GNUNET_MAX_MESSAGE_SIZE -
598       sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
599     sizeof (struct GNUNET_PeerIdentity);
600   tmp_peer_pointer = ids;
601
602   while (GNUNET_MAX_MESSAGE_SIZE < size_needed)
603   {
604     ev = GNUNET_MQ_msg_extra (msg, num_peers_max * sizeof (struct GNUNET_PeerIdentity),
605         GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
606     msg->num_peers = htonl (num_peers_max);
607     GNUNET_memcpy (&msg[1], tmp_peer_pointer, num_peers_max * sizeof (struct GNUNET_PeerIdentity));
608     GNUNET_MQ_send (h->mq, ev);
609
610     n -= num_peers_max;
611     size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
612                   n * sizeof (struct GNUNET_PeerIdentity);
613     /* Set pointer to beginning of next block of num_peers_max peers */
614     tmp_peer_pointer = &ids[num_peers_max];
615   }
616
617   ev = GNUNET_MQ_msg_extra (msg, n * sizeof (struct GNUNET_PeerIdentity),
618                             GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
619   msg->num_peers = htonl (n);
620   GNUNET_memcpy (&msg[1], tmp_peer_pointer, n * sizeof (struct GNUNET_PeerIdentity));
621
622   GNUNET_MQ_send (h->mq, ev);
623 }
624
625
626 #ifdef ENABLE_MALICIOUS
627 /**
628  * Turn RPS service to act malicious.
629  *
630  * @param h handle to the rps service
631  * @param type which type of malicious peer to turn to.
632  *             0 Don't act malicious at all
633  *             1 Try to maximise representation
634  *             2 Try to partition the network
635  *               (isolate one peer from the rest)
636  * @param n number of @a ids
637  * @param ids the ids of the malicious peers
638  *            if @type is 2 the last id is the id of the
639  *            peer to be isolated from the rest
640  */
641 void
642 GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h,
643                           uint32_t type,
644                           uint32_t num_peers,
645                           const struct GNUNET_PeerIdentity *peer_ids,
646                           const struct GNUNET_PeerIdentity *target_peer)
647 {
648   size_t size_needed;
649   uint32_t num_peers_max;
650   const struct GNUNET_PeerIdentity *tmp_peer_pointer;
651   struct GNUNET_MQ_Envelope *ev;
652   struct GNUNET_RPS_CS_ActMaliciousMessage *msg;
653
654   unsigned int i;
655
656   LOG (GNUNET_ERROR_TYPE_DEBUG,
657        "Client turns malicious (type %" PRIu32 ") with %" PRIu32 " other peers:\n",
658        type,
659        num_peers);
660   for (i = 0 ; i < num_peers ; i++)
661     LOG (GNUNET_ERROR_TYPE_DEBUG,
662          "%u. peer: %s\n",
663          i,
664          GNUNET_i2s (&peer_ids[i]));
665
666   /* The actual size the message would occupy */
667   size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
668     num_peers * sizeof (struct GNUNET_PeerIdentity);
669   /* The number of peers that fit in one message together with
670    * the respective header */
671   num_peers_max = (GNUNET_MAX_MESSAGE_SIZE -
672       sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
673     sizeof (struct GNUNET_PeerIdentity);
674   tmp_peer_pointer = peer_ids;
675
676   while (GNUNET_MAX_MESSAGE_SIZE < size_needed)
677   {
678     LOG (GNUNET_ERROR_TYPE_DEBUG,
679          "Too many peers to send at once, sending %" PRIu32 " (all we can so far)\n",
680          num_peers_max);
681     ev = GNUNET_MQ_msg_extra (msg,
682                               num_peers_max * sizeof (struct GNUNET_PeerIdentity),
683                               GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
684     msg->type = htonl (type);
685     msg->num_peers = htonl (num_peers_max);
686     if ( (2 == type) ||
687          (3 == type) )
688       msg->attacked_peer = peer_ids[num_peers];
689     GNUNET_memcpy (&msg[1],
690             tmp_peer_pointer,
691             num_peers_max * sizeof (struct GNUNET_PeerIdentity));
692
693     GNUNET_MQ_send (h->mq, ev);
694
695     num_peers -= num_peers_max;
696     size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
697                   num_peers * sizeof (struct GNUNET_PeerIdentity);
698     /* Set pointer to beginning of next block of num_peers_max peers */
699     tmp_peer_pointer = &peer_ids[num_peers_max];
700   }
701
702   ev = GNUNET_MQ_msg_extra (msg,
703                             num_peers * sizeof (struct GNUNET_PeerIdentity),
704                             GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
705   msg->type = htonl (type);
706   msg->num_peers = htonl (num_peers);
707   if ( (2 == type) ||
708        (3 == type) )
709     msg->attacked_peer = *target_peer;
710   GNUNET_memcpy (&msg[1], tmp_peer_pointer, num_peers * sizeof (struct GNUNET_PeerIdentity));
711
712   GNUNET_MQ_send (h->mq, ev);
713 }
714 #endif /* ENABLE_MALICIOUS */
715
716
717 /**
718  * Cancle an issued request.
719  *
720  * @param rh request handle of request to cancle
721  */
722 void
723 GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh)
724 {
725   struct GNUNET_RPS_Handle *h;
726   struct GNUNET_MQ_Envelope *ev;
727   struct GNUNET_RPS_CS_RequestCancelMessage*msg;
728
729   LOG (GNUNET_ERROR_TYPE_DEBUG,
730        "Cancelling request with id %" PRIu32 "\n",
731        rh->id);
732
733   h = rh->rps_handle;
734   GNUNET_assert (GNUNET_CONTAINER_multihashmap32_contains (h->req_handlers,
735         rh->id));
736   GNUNET_CONTAINER_multihashmap32_remove_all (h->req_handlers, rh->id);
737   ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST_CANCEL);
738   msg->id = htonl (rh->id);
739   GNUNET_MQ_send (rh->rps_handle->mq, ev);
740 }
741
742
743 /**
744  * Disconnect from the rps service
745  *
746  * @param h the handle to the rps service
747  */
748 void
749 GNUNET_RPS_disconnect (struct GNUNET_RPS_Handle *h)
750 {
751   GNUNET_MQ_destroy (h->mq);
752   if (0 < GNUNET_CONTAINER_multihashmap32_size (h->req_handlers))
753     LOG (GNUNET_ERROR_TYPE_WARNING,
754         "Still waiting for requests\n");
755   GNUNET_CONTAINER_multihashmap32_destroy (h->req_handlers);
756   GNUNET_free (h);
757 }
758
759
760 /* end of rps_api.c */