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_ViewUpdateCB 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_StreamInputCB 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_ViewUpdateCB 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_StreamInputCB 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 * @param msg the message
383 handle_stream_input (void *cls,
384 const struct GNUNET_RPS_CS_DEBUG_StreamReply *msg)
386 struct GNUNET_RPS_Handle *h = cls;
388 /* Give the peers back */
389 LOG (GNUNET_ERROR_TYPE_DEBUG,
390 "New peer of biased input stream\n");
392 GNUNET_assert (NULL != h);
393 GNUNET_assert (NULL != h->stream_input_cb);
394 h->stream_input_cb (h->stream_input_cb, &msg->peer);
399 * Reconnect to the service
402 reconnect (struct GNUNET_RPS_Handle *h);
406 * Error handler for mq.
408 * This function is called whan mq encounters an error.
409 * Until now mq doesn't provide useful error messages.
411 * @param cls the closure
412 * @param error error code without specyfied meaning
415 mq_error_handler (void *cls,
416 enum GNUNET_MQ_Error error)
418 struct GNUNET_RPS_Handle *h = cls;
420 LOG (GNUNET_ERROR_TYPE_WARNING, "Problem with message queue. error: %i\n\
426 /* Resend all pending request as the service destroyed its knowledge
433 * Reconnect to the service
436 reconnect (struct GNUNET_RPS_Handle *h)
438 struct GNUNET_MQ_MessageHandler mq_handlers[] = {
439 GNUNET_MQ_hd_var_size (reply,
440 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY,
441 struct GNUNET_RPS_CS_ReplyMessage,
443 GNUNET_MQ_hd_var_size (view_update,
444 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REPLY,
445 struct GNUNET_RPS_CS_DEBUG_ViewReply,
447 GNUNET_MQ_hd_fixed_size (stream_input,
448 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_REPLY,
449 struct GNUNET_RPS_CS_DEBUG_StreamReply,
451 GNUNET_MQ_handler_end ()
455 GNUNET_MQ_destroy (h->mq);
456 h->mq = GNUNET_CLIENT_connect (h->cfg,
465 * Connect to the rps service
467 * @param cfg configuration to use
468 * @return a handle to the service
470 struct GNUNET_RPS_Handle *
471 GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
473 struct GNUNET_RPS_Handle *h;
475 h = GNUNET_new (struct GNUNET_RPS_Handle);
476 h->current_request_id = 0;
484 h->req_handlers = GNUNET_CONTAINER_multihashmap32_create (4);
490 * Request n random peers.
492 * @param rps_handle handle to the rps service
493 * @param num_req_peers number of peers we want to receive
494 * @param ready_cb the callback called when the peers are available
495 * @param cls closure given to the callback
496 * @return a handle to cancel this request
498 struct GNUNET_RPS_Request_Handle *
499 GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *rps_handle,
500 uint32_t num_req_peers,
501 GNUNET_RPS_NotifyReadyCB ready_cb,
504 struct GNUNET_RPS_Request_Handle *rh;
506 rh = GNUNET_new (struct GNUNET_RPS_Request_Handle);
507 rh->rps_handle = rps_handle;
508 rh->id = rps_handle->current_request_id++;
509 rh->num_peers = num_req_peers;
510 rh->ready_cb = ready_cb;
511 rh->ready_cb_cls = cls;
513 LOG (GNUNET_ERROR_TYPE_DEBUG,
514 "Requesting %" PRIu32 " peers with id %" PRIu32 "\n",
518 GNUNET_CONTAINER_multihashmap32_put (rps_handle->req_handlers, rh->id, rh,
519 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
521 send_request (rps_handle, rh->id, num_req_peers);
527 * Seed rps service with peerIDs.
529 * @param h handle to the rps service
530 * @param n number of peers to seed
531 * @param ids the ids of the peers seeded
534 GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h,
536 const struct GNUNET_PeerIdentity *ids)
539 uint32_t num_peers_max;
540 const struct GNUNET_PeerIdentity *tmp_peer_pointer;
541 struct GNUNET_MQ_Envelope *ev;
542 struct GNUNET_RPS_CS_SeedMessage *msg;
546 LOG (GNUNET_ERROR_TYPE_DEBUG,
547 "Client wants to seed %" PRIu32 " peers:\n",
549 for (i = 0 ; i < n ; i++)
550 LOG (GNUNET_ERROR_TYPE_DEBUG,
553 GNUNET_i2s (&ids[i]));
555 /* The actual size the message occupies */
556 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
557 n * sizeof (struct GNUNET_PeerIdentity);
558 /* The number of peers that fits in one message together with
559 * the respective header */
560 num_peers_max = (GNUNET_MAX_MESSAGE_SIZE -
561 sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
562 sizeof (struct GNUNET_PeerIdentity);
563 tmp_peer_pointer = ids;
565 while (GNUNET_MAX_MESSAGE_SIZE < size_needed)
567 ev = GNUNET_MQ_msg_extra (msg, num_peers_max * sizeof (struct GNUNET_PeerIdentity),
568 GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
569 msg->num_peers = htonl (num_peers_max);
570 GNUNET_memcpy (&msg[1], tmp_peer_pointer, num_peers_max * sizeof (struct GNUNET_PeerIdentity));
571 GNUNET_MQ_send (h->mq, ev);
574 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
575 n * sizeof (struct GNUNET_PeerIdentity);
576 /* Set pointer to beginning of next block of num_peers_max peers */
577 tmp_peer_pointer = &ids[num_peers_max];
580 ev = GNUNET_MQ_msg_extra (msg, n * sizeof (struct GNUNET_PeerIdentity),
581 GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
582 msg->num_peers = htonl (n);
583 GNUNET_memcpy (&msg[1], tmp_peer_pointer, n * sizeof (struct GNUNET_PeerIdentity));
585 GNUNET_MQ_send (h->mq, ev);
589 #ifdef ENABLE_MALICIOUS
591 * Turn RPS service to act malicious.
593 * @param h handle to the rps service
594 * @param type which type of malicious peer to turn to.
595 * 0 Don't act malicious at all
596 * 1 Try to maximise representation
597 * 2 Try to partition the network
598 * (isolate one peer from the rest)
599 * @param n number of @a ids
600 * @param ids the ids of the malicious peers
601 * if @type is 2 the last id is the id of the
602 * peer to be isolated from the rest
605 GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h,
608 const struct GNUNET_PeerIdentity *peer_ids,
609 const struct GNUNET_PeerIdentity *target_peer)
612 uint32_t num_peers_max;
613 const struct GNUNET_PeerIdentity *tmp_peer_pointer;
614 struct GNUNET_MQ_Envelope *ev;
615 struct GNUNET_RPS_CS_ActMaliciousMessage *msg;
619 LOG (GNUNET_ERROR_TYPE_DEBUG,
620 "Client turns malicious (type %" PRIu32 ") with %" PRIu32 " other peers:\n",
623 for (i = 0 ; i < num_peers ; i++)
624 LOG (GNUNET_ERROR_TYPE_DEBUG,
627 GNUNET_i2s (&peer_ids[i]));
629 /* The actual size the message would occupy */
630 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
631 num_peers * sizeof (struct GNUNET_PeerIdentity);
632 /* The number of peers that fit in one message together with
633 * the respective header */
634 num_peers_max = (GNUNET_MAX_MESSAGE_SIZE -
635 sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
636 sizeof (struct GNUNET_PeerIdentity);
637 tmp_peer_pointer = peer_ids;
639 while (GNUNET_MAX_MESSAGE_SIZE < size_needed)
641 LOG (GNUNET_ERROR_TYPE_DEBUG,
642 "Too many peers to send at once, sending %" PRIu32 " (all we can so far)\n",
644 ev = GNUNET_MQ_msg_extra (msg,
645 num_peers_max * sizeof (struct GNUNET_PeerIdentity),
646 GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
647 msg->type = htonl (type);
648 msg->num_peers = htonl (num_peers_max);
651 msg->attacked_peer = peer_ids[num_peers];
652 GNUNET_memcpy (&msg[1],
654 num_peers_max * sizeof (struct GNUNET_PeerIdentity));
656 GNUNET_MQ_send (h->mq, ev);
658 num_peers -= num_peers_max;
659 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
660 num_peers * sizeof (struct GNUNET_PeerIdentity);
661 /* Set pointer to beginning of next block of num_peers_max peers */
662 tmp_peer_pointer = &peer_ids[num_peers_max];
665 ev = GNUNET_MQ_msg_extra (msg,
666 num_peers * sizeof (struct GNUNET_PeerIdentity),
667 GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
668 msg->type = htonl (type);
669 msg->num_peers = htonl (num_peers);
672 msg->attacked_peer = *target_peer;
673 GNUNET_memcpy (&msg[1], tmp_peer_pointer, num_peers * sizeof (struct GNUNET_PeerIdentity));
675 GNUNET_MQ_send (h->mq, ev);
677 #endif /* ENABLE_MALICIOUS */
681 * Cancle an issued request.
683 * @param rh request handle of request to cancle
686 GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh)
688 struct GNUNET_RPS_Handle *h;
689 struct GNUNET_MQ_Envelope *ev;
690 struct GNUNET_RPS_CS_RequestCancelMessage*msg;
692 LOG (GNUNET_ERROR_TYPE_DEBUG,
693 "Cancelling request with id %" PRIu32 "\n",
697 GNUNET_assert (GNUNET_CONTAINER_multihashmap32_contains (h->req_handlers,
699 GNUNET_CONTAINER_multihashmap32_remove_all (h->req_handlers, rh->id);
700 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST_CANCEL);
701 msg->id = htonl (rh->id);
702 GNUNET_MQ_send (rh->rps_handle->mq, ev);
707 * Disconnect from the rps service
709 * @param h the handle to the rps service
712 GNUNET_RPS_disconnect (struct GNUNET_RPS_Handle *h)
714 GNUNET_MQ_destroy (h->mq);
715 if (0 < GNUNET_CONTAINER_multihashmap32_size (h->req_handlers))
716 LOG (GNUNET_ERROR_TYPE_WARNING,
717 "Still waiting for requests\n");
718 GNUNET_CONTAINER_multihashmap32_destroy (h->req_handlers);
723 /* end of rps_api.c */