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 to single requests from the 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.
101 * The callback to be called when we receive an answer.
103 GNUNET_RPS_NotifyReadyCB ready_cb;
106 * The closure for the callback.
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.
119 * Callback provided by the client
121 GNUNET_RPS_NotifyReadyCB cb;
124 * Closure provided by the client
129 * Handle to the service connection
131 struct GNUNET_CLIENT_Connection *service_conn;
135 * @brief Send a request to the service.
137 * @param h rps handle
138 * @param id id of the request
139 * @param num_req_peers number of peers
142 send_request (const struct GNUNET_RPS_Handle *h,
144 uint32_t num_req_peers)
146 struct GNUNET_MQ_Envelope *ev;
147 struct GNUNET_RPS_CS_RequestMessage *msg;
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);
156 * @brief Iterator function over pending requests
158 * Implements #GNUNET_CONTAINER_HashMapIterator32
160 * @param cls rps handle
161 * @param key id of the request
162 * @param value request handle
164 * @return GNUNET_YES to continue iteration
167 resend_requests_iterator (void *cls, uint32_t key, void *value)
169 const struct GNUNET_RPS_Handle *h = cls;
170 const struct GNUNET_RPS_Request_Handle *req_handle = value;
173 send_request (h, req_handle->id, req_handle->num_peers);
174 return GNUNET_YES; /* continue iterating */
178 * @brief Resend all pending requests
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.
184 * @param h rps handle
187 resend_requests (struct GNUNET_RPS_Handle *h)
189 GNUNET_CONTAINER_multihashmap32_iterate (h->req_handlers,
190 resend_requests_iterator,
196 * This function is called, when the service replies to our request.
197 * It verifies that @a msg is well-formed.
199 * @param cls the closure
200 * @param msg the message
201 * @return #GNUNET_OK if @a msg is well-formed
204 check_reply (void *cls,
205 const struct GNUNET_RPS_CS_ReplyMessage *msg)
207 uint16_t msize = ntohs (msg->header.size);
208 uint32_t num_peers = ntohl (msg->num_peers);
211 msize -= sizeof (struct GNUNET_RPS_CS_ReplyMessage);
212 if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) ||
213 (msize % sizeof (struct GNUNET_PeerIdentity) != 0) )
216 return GNUNET_SYSERR;
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.
227 * @param cls the closure
228 * @param msg the message
231 handle_reply (void *cls,
232 const struct GNUNET_RPS_CS_ReplyMessage *msg)
234 struct GNUNET_RPS_Handle *h = cls;
235 struct GNUNET_PeerIdentity *peers;
236 struct GNUNET_RPS_Request_Handle *rh;
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),
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),
259 /* Get internals for debugging/profiling purposes */
262 * Request updates of view
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
271 GNUNET_RPS_view_request (struct GNUNET_RPS_Handle *rps_handle,
272 uint32_t num_updates,
273 GNUNET_RPS_NotifyReadyCB view_update_cb,
276 struct GNUNET_MQ_Envelope *ev;
277 struct GNUNET_RPS_CS_DEBUG_ViewRequest *msg;
279 LOG (GNUNET_ERROR_TYPE_DEBUG,
280 "Client requests %" PRIu32 " view updates\n",
282 rps_handle->view_update_cb = view_update_cb;
283 rps_handle->view_update_cls = cls;
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);
292 * Request biased stream of peers that are being put into the sampler
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
301 GNUNET_RPS_stream_request (struct GNUNET_RPS_Handle *rps_handle,
303 GNUNET_RPS_NotifyReadyCB stream_input_cb,
306 struct GNUNET_MQ_Envelope *ev;
307 struct GNUNET_RPS_CS_DEBUG_StreamRequest *msg;
309 LOG (GNUNET_ERROR_TYPE_DEBUG,
310 "Client requests %" PRIu32 " biased stream updates\n",
312 rps_handle->stream_input_cb = stream_input_cb;
313 rps_handle->stream_input_cls = cls;
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);
322 * This function is called, when the service updates the view.
323 * It verifies that @a msg is well-formed.
325 * @param cls the closure
326 * @param msg the message
327 * @return #GNUNET_OK if @a msg is well-formed
330 check_view_update (void *cls,
331 const struct GNUNET_RPS_CS_DEBUG_ViewReply *msg)
333 uint16_t msize = ntohs (msg->header.size);
334 uint32_t num_peers = ntohl (msg->num_peers);
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) )
342 return GNUNET_SYSERR;
349 * This function is called, when the service updated its view.
350 * It calls the callback the caller provided
351 * and disconnects afterwards.
353 * @param msg the message
356 handle_view_update (void *cls,
357 const struct GNUNET_RPS_CS_DEBUG_ViewReply *msg)
359 struct GNUNET_RPS_Handle *h = cls;
360 struct GNUNET_PeerIdentity *peers;
362 /* Give the peers back */
363 LOG (GNUNET_ERROR_TYPE_DEBUG,
364 "New view of %" PRIu32 " peers:\n",
365 ntohl (msg->num_peers));
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);
375 * This function is called, when the service sends another peer from the biased
377 * It calls the callback the caller provided
378 * and disconnects afterwards.
380 * TODO merge with check_view_update
382 * @param msg the message
385 check_stream_input (void *cls,
386 const struct GNUNET_RPS_CS_DEBUG_StreamReply *msg)
388 uint16_t msize = ntohs (msg->header.size);
389 uint32_t num_peers = ntohl (msg->num_peers);
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) )
397 return GNUNET_SYSERR;
403 * This function is called, when the service sends another peer from the biased
405 * It calls the callback the caller provided
406 * and disconnects afterwards.
408 * @param msg the message
411 handle_stream_input (void *cls,
412 const struct GNUNET_RPS_CS_DEBUG_StreamReply *msg)
414 struct GNUNET_RPS_Handle *h = cls;
415 const struct GNUNET_PeerIdentity *peers;
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));
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);
430 * Reconnect to the service
433 reconnect (struct GNUNET_RPS_Handle *h);
437 * Error handler for mq.
439 * This function is called whan mq encounters an error.
440 * Until now mq doesn't provide useful error messages.
442 * @param cls the closure
443 * @param error error code without specyfied meaning
446 mq_error_handler (void *cls,
447 enum GNUNET_MQ_Error error)
449 struct GNUNET_RPS_Handle *h = cls;
451 LOG (GNUNET_ERROR_TYPE_WARNING, "Problem with message queue. error: %i\n\
457 /* Resend all pending request as the service destroyed its knowledge
464 * Reconnect to the service
467 reconnect (struct GNUNET_RPS_Handle *h)
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,
474 GNUNET_MQ_hd_var_size (view_update,
475 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REPLY,
476 struct GNUNET_RPS_CS_DEBUG_ViewReply,
478 GNUNET_MQ_hd_var_size (stream_input,
479 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_REPLY,
480 struct GNUNET_RPS_CS_DEBUG_StreamReply,
482 GNUNET_MQ_handler_end ()
486 GNUNET_MQ_destroy (h->mq);
487 h->mq = GNUNET_CLIENT_connect (h->cfg,
496 * Connect to the rps service
498 * @param cfg configuration to use
499 * @return a handle to the service
501 struct GNUNET_RPS_Handle *
502 GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
504 struct GNUNET_RPS_Handle *h;
506 h = GNUNET_new (struct GNUNET_RPS_Handle);
507 h->current_request_id = 0;
515 h->req_handlers = GNUNET_CONTAINER_multihashmap32_create (4);
521 * Request n random peers.
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
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,
535 struct GNUNET_RPS_Request_Handle *rh;
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;
544 LOG (GNUNET_ERROR_TYPE_DEBUG,
545 "Requesting %" PRIu32 " peers with id %" PRIu32 "\n",
549 GNUNET_CONTAINER_multihashmap32_put (rps_handle->req_handlers, rh->id, rh,
550 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
552 send_request (rps_handle, rh->id, num_req_peers);
558 * Seed rps service with peerIDs.
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
565 GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h,
567 const struct GNUNET_PeerIdentity *ids)
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;
577 LOG (GNUNET_ERROR_TYPE_DEBUG,
578 "Client wants to seed %" PRIu32 " peers:\n",
580 for (i = 0 ; i < n ; i++)
581 LOG (GNUNET_ERROR_TYPE_DEBUG,
584 GNUNET_i2s (&ids[i]));
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;
596 while (GNUNET_MAX_MESSAGE_SIZE < size_needed)
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);
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];
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));
616 GNUNET_MQ_send (h->mq, ev);
620 #ifdef ENABLE_MALICIOUS
622 * Turn RPS service to act malicious.
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
636 GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h,
639 const struct GNUNET_PeerIdentity *peer_ids,
640 const struct GNUNET_PeerIdentity *target_peer)
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;
650 LOG (GNUNET_ERROR_TYPE_DEBUG,
651 "Client turns malicious (type %" PRIu32 ") with %" PRIu32 " other peers:\n",
654 for (i = 0 ; i < num_peers ; i++)
655 LOG (GNUNET_ERROR_TYPE_DEBUG,
658 GNUNET_i2s (&peer_ids[i]));
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;
670 while (GNUNET_MAX_MESSAGE_SIZE < size_needed)
672 LOG (GNUNET_ERROR_TYPE_DEBUG,
673 "Too many peers to send at once, sending %" PRIu32 " (all we can so far)\n",
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);
682 msg->attacked_peer = peer_ids[num_peers];
683 GNUNET_memcpy (&msg[1],
685 num_peers_max * sizeof (struct GNUNET_PeerIdentity));
687 GNUNET_MQ_send (h->mq, ev);
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];
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);
703 msg->attacked_peer = *target_peer;
704 GNUNET_memcpy (&msg[1], tmp_peer_pointer, num_peers * sizeof (struct GNUNET_PeerIdentity));
706 GNUNET_MQ_send (h->mq, ev);
708 #endif /* ENABLE_MALICIOUS */
712 * Cancle an issued request.
714 * @param rh request handle of request to cancle
717 GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh)
719 struct GNUNET_RPS_Handle *h;
720 struct GNUNET_MQ_Envelope *ev;
721 struct GNUNET_RPS_CS_RequestCancelMessage*msg;
723 LOG (GNUNET_ERROR_TYPE_DEBUG,
724 "Cancelling request with id %" PRIu32 "\n",
728 GNUNET_assert (GNUNET_CONTAINER_multihashmap32_contains (h->req_handlers,
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);
738 * Disconnect from the rps service
740 * @param h the handle to the rps service
743 GNUNET_RPS_disconnect (struct GNUNET_RPS_Handle *h)
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);
754 /* end of rps_api.c */