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