2 This file is part of GNUnet.
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.
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.
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/>.
22 * @author Julius Bünger
25 #include "gnunet_util_lib.h"
27 #include "gnunet_rps_service.h"
31 #define LOG(kind,...) GNUNET_log_from (kind, "rps-api",__VA_ARGS__)
34 * Handler to handle requests from a client.
36 struct GNUNET_RPS_Handle
39 * The handle to the client configuration.
41 const struct GNUNET_CONFIGURATION_Handle *cfg;
44 * The message queue to the client.
46 struct GNUNET_MQ_Handle *mq;
49 * Array of Request_Handles.
51 struct GNUNET_CONTAINER_MultiHashMap32 *req_handlers;
54 * The id of the last request.
56 uint32_t current_request_id;
59 * @brief Callback called on each update of the view
61 GNUNET_RPS_NotifyReadyCB view_update_cb;
64 * @brief Closure to each requested update of the view
66 void *view_update_cls;
69 * @brief Callback called on each peer of the biased input stream
71 GNUNET_RPS_NotifyReadyCB stream_input_cb;
74 * @brief Closure to each requested peer from the biased stream
76 void *stream_input_cls;
81 * Handler for a single request from a client.
83 struct GNUNET_RPS_Request_Handle
86 * The client issuing the request.
88 struct GNUNET_RPS_Handle *rps_handle;
91 * The id of the request.
96 * The number of requested peers.
98 uint32_t num_requests;
101 * @brief The Sampler for the client request
103 struct RPS_Sampler *sampler;
106 * The callback to be called when we receive an answer.
108 GNUNET_RPS_NotifyReadyCB ready_cb;
111 * The closure for the callback.
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.
124 * Callback provided by the client
126 GNUNET_RPS_NotifyReadyCB cb;
129 * Closure provided by the client
134 * Handle to the service connection
136 struct GNUNET_CLIENT_Connection *service_conn;
141 * @brief Send a request to the service.
143 * @param h rps handle
144 * @param id id of the request
145 * @param num_req_peers number of peers
148 send_request (const struct GNUNET_RPS_Handle *h,
150 uint32_t num_req_peers)
152 struct GNUNET_MQ_Envelope *ev;
153 struct GNUNET_RPS_CS_RequestMessage *msg;
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);
162 * @brief Iterator function over pending requests
164 * Implements #GNUNET_CONTAINER_HashMapIterator32
166 * @param cls rps handle
167 * @param key id of the request
168 * @param value request handle
170 * @return GNUNET_YES to continue iteration
173 resend_requests_iterator (void *cls, uint32_t key, void *value)
175 const struct GNUNET_RPS_Handle *h = cls;
176 const struct GNUNET_RPS_Request_Handle *req_handle = value;
179 send_request (h, req_handle->id, req_handle->num_requests);
180 return GNUNET_YES; /* continue iterating */
184 * @brief Resend all pending requests
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.
190 * @param h rps handle
193 resend_requests (struct GNUNET_RPS_Handle *h)
195 GNUNET_CONTAINER_multihashmap32_iterate (h->req_handlers,
196 resend_requests_iterator,
202 * This function is called, when the service replies to our request.
203 * It verifies that @a msg is well-formed.
205 * @param cls the closure
206 * @param msg the message
207 * @return #GNUNET_OK if @a msg is well-formed
210 check_reply (void *cls,
211 const struct GNUNET_RPS_CS_ReplyMessage *msg)
213 uint16_t msize = ntohs (msg->header.size);
214 uint32_t num_peers = ntohl (msg->num_peers);
217 msize -= sizeof (struct GNUNET_RPS_CS_ReplyMessage);
218 if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) ||
219 (msize % sizeof (struct GNUNET_PeerIdentity) != 0) )
222 return GNUNET_SYSERR;
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.
233 * @param cls the closure
234 * @param msg the message
237 handle_reply (void *cls,
238 const struct GNUNET_RPS_CS_ReplyMessage *msg)
240 struct GNUNET_RPS_Handle *h = cls;
241 struct GNUNET_PeerIdentity *peers;
242 struct GNUNET_RPS_Request_Handle *rh;
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),
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),
265 /* Get internals for debugging/profiling purposes */
268 * Request updates of view
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
277 GNUNET_RPS_view_request (struct GNUNET_RPS_Handle *rps_handle,
278 uint32_t num_updates,
279 GNUNET_RPS_NotifyReadyCB view_update_cb,
282 struct GNUNET_MQ_Envelope *ev;
283 struct GNUNET_RPS_CS_DEBUG_ViewRequest *msg;
285 LOG (GNUNET_ERROR_TYPE_DEBUG,
286 "Client requests %" PRIu32 " view updates\n",
288 rps_handle->view_update_cb = view_update_cb;
289 rps_handle->view_update_cls = cls;
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);
298 * Request biased stream of peers that are being put into the sampler
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
307 GNUNET_RPS_stream_request (struct GNUNET_RPS_Handle *rps_handle,
309 GNUNET_RPS_NotifyReadyCB stream_input_cb,
312 struct GNUNET_MQ_Envelope *ev;
313 struct GNUNET_RPS_CS_DEBUG_StreamRequest *msg;
315 LOG (GNUNET_ERROR_TYPE_DEBUG,
316 "Client requests %" PRIu32 " biased stream updates\n",
318 rps_handle->stream_input_cb = stream_input_cb;
319 rps_handle->stream_input_cls = cls;
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);
328 * This function is called, when the service updates the view.
329 * It verifies that @a msg is well-formed.
331 * @param cls the closure
332 * @param msg the message
333 * @return #GNUNET_OK if @a msg is well-formed
336 check_view_update (void *cls,
337 const struct GNUNET_RPS_CS_DEBUG_ViewReply *msg)
339 uint16_t msize = ntohs (msg->header.size);
340 uint32_t num_peers = ntohl (msg->num_peers);
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) )
348 return GNUNET_SYSERR;
355 * This function is called, when the service updated its view.
356 * It calls the callback the caller provided
357 * and disconnects afterwards.
359 * @param msg the message
362 handle_view_update (void *cls,
363 const struct GNUNET_RPS_CS_DEBUG_ViewReply *msg)
365 struct GNUNET_RPS_Handle *h = cls;
366 struct GNUNET_PeerIdentity *peers;
368 /* Give the peers back */
369 LOG (GNUNET_ERROR_TYPE_DEBUG,
370 "New view of %" PRIu32 " peers:\n",
371 ntohl (msg->num_peers));
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);
381 * This function is called, when the service sends another peer from the biased
383 * It calls the callback the caller provided
384 * and disconnects afterwards.
386 * TODO merge with check_view_update
388 * @param msg the message
391 check_stream_input (void *cls,
392 const struct GNUNET_RPS_CS_DEBUG_StreamReply *msg)
394 uint16_t msize = ntohs (msg->header.size);
395 uint32_t num_peers = ntohl (msg->num_peers);
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) )
403 return GNUNET_SYSERR;
409 * This function is called, when the service sends another peer from the biased
411 * It calls the callback the caller provided
412 * and disconnects afterwards.
414 * @param msg the message
417 handle_stream_input (void *cls,
418 const struct GNUNET_RPS_CS_DEBUG_StreamReply *msg)
420 struct GNUNET_RPS_Handle *h = cls;
421 const struct GNUNET_PeerIdentity *peers;
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));
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);
436 * Reconnect to the service
439 reconnect (struct GNUNET_RPS_Handle *h);
443 * Error handler for mq.
445 * This function is called whan mq encounters an error.
446 * Until now mq doesn't provide useful error messages.
448 * @param cls the closure
449 * @param error error code without specyfied meaning
452 mq_error_handler (void *cls,
453 enum GNUNET_MQ_Error error)
455 struct GNUNET_RPS_Handle *h = cls;
457 LOG (GNUNET_ERROR_TYPE_WARNING, "Problem with message queue. error: %i\n\
463 /* Resend all pending request as the service destroyed its knowledge
470 * Reconnect to the service
473 reconnect (struct GNUNET_RPS_Handle *h)
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,
480 GNUNET_MQ_hd_var_size (view_update,
481 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REPLY,
482 struct GNUNET_RPS_CS_DEBUG_ViewReply,
484 GNUNET_MQ_hd_var_size (stream_input,
485 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_REPLY,
486 struct GNUNET_RPS_CS_DEBUG_StreamReply,
488 GNUNET_MQ_handler_end ()
492 GNUNET_MQ_destroy (h->mq);
493 h->mq = GNUNET_CLIENT_connect (h->cfg,
502 * Connect to the rps service
504 * @param cfg configuration to use
505 * @return a handle to the service
507 struct GNUNET_RPS_Handle *
508 GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
510 struct GNUNET_RPS_Handle *h;
512 h = GNUNET_new (struct GNUNET_RPS_Handle);
513 h->current_request_id = 0;
521 h->req_handlers = GNUNET_CONTAINER_multihashmap32_create (4);
527 * Request n random peers.
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
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,
541 struct GNUNET_RPS_Request_Handle *rh;
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;
550 LOG (GNUNET_ERROR_TYPE_DEBUG,
551 "Requesting %" PRIu32 " peers with id %" PRIu32 "\n",
555 GNUNET_CONTAINER_multihashmap32_put (rps_handle->req_handlers, rh->id, rh,
556 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
558 send_request (rps_handle, rh->id, num_req_peers);
564 * Seed rps service with peerIDs.
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
571 GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h,
573 const struct GNUNET_PeerIdentity *ids)
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;
583 LOG (GNUNET_ERROR_TYPE_DEBUG,
584 "Client wants to seed %" PRIu32 " peers:\n",
586 for (i = 0 ; i < n ; i++)
587 LOG (GNUNET_ERROR_TYPE_DEBUG,
590 GNUNET_i2s (&ids[i]));
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;
602 while (GNUNET_MAX_MESSAGE_SIZE < size_needed)
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);
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];
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));
622 GNUNET_MQ_send (h->mq, ev);
626 #ifdef ENABLE_MALICIOUS
628 * Turn RPS service to act malicious.
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
642 GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h,
645 const struct GNUNET_PeerIdentity *peer_ids,
646 const struct GNUNET_PeerIdentity *target_peer)
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;
656 LOG (GNUNET_ERROR_TYPE_DEBUG,
657 "Client turns malicious (type %" PRIu32 ") with %" PRIu32 " other peers:\n",
660 for (i = 0 ; i < num_peers ; i++)
661 LOG (GNUNET_ERROR_TYPE_DEBUG,
664 GNUNET_i2s (&peer_ids[i]));
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;
676 while (GNUNET_MAX_MESSAGE_SIZE < size_needed)
678 LOG (GNUNET_ERROR_TYPE_DEBUG,
679 "Too many peers to send at once, sending %" PRIu32 " (all we can so far)\n",
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);
688 msg->attacked_peer = peer_ids[num_peers];
689 GNUNET_memcpy (&msg[1],
691 num_peers_max * sizeof (struct GNUNET_PeerIdentity));
693 GNUNET_MQ_send (h->mq, ev);
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];
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);
709 msg->attacked_peer = *target_peer;
710 GNUNET_memcpy (&msg[1], tmp_peer_pointer, num_peers * sizeof (struct GNUNET_PeerIdentity));
712 GNUNET_MQ_send (h->mq, ev);
714 #endif /* ENABLE_MALICIOUS */
718 * Cancle an issued request.
720 * @param rh request handle of request to cancle
723 GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh)
725 struct GNUNET_RPS_Handle *h;
726 struct GNUNET_MQ_Envelope *ev;
727 struct GNUNET_RPS_CS_RequestCancelMessage*msg;
729 LOG (GNUNET_ERROR_TYPE_DEBUG,
730 "Cancelling request with id %" PRIu32 "\n",
734 GNUNET_assert (GNUNET_CONTAINER_multihashmap32_contains (h->req_handlers,
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);
744 * Disconnect from the rps service
746 * @param h the handle to the rps service
749 GNUNET_RPS_disconnect (struct GNUNET_RPS_Handle *h)
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);
760 /* end of rps_api.c */