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 Callback called on each update of the view
66 void *view_update_cls;
71 * Handler to single requests from the client.
73 struct GNUNET_RPS_Request_Handle
76 * The client issuing the request.
78 struct GNUNET_RPS_Handle *rps_handle;
81 * The id of the request.
86 * The number of requested peers.
91 * The callback to be called when we receive an answer.
93 GNUNET_RPS_NotifyReadyCB ready_cb;
96 * The closure for the callback.
103 * Struct used to pack the callback, its closure (provided by the caller)
104 * and the connection handler to the service to pass it to a callback function.
109 * Callback provided by the client
111 GNUNET_RPS_NotifyReadyCB cb;
114 * Closure provided by the client
119 * Handle to the service connection
121 struct GNUNET_CLIENT_Connection *service_conn;
125 * @brief Send a request to the service.
127 * @param h rps handle
128 * @param id id of the request
129 * @param num_req_peers number of peers
132 send_request (const struct GNUNET_RPS_Handle *h,
134 uint32_t num_req_peers)
136 struct GNUNET_MQ_Envelope *ev;
137 struct GNUNET_RPS_CS_RequestMessage *msg;
139 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST);
140 msg->num_peers = htonl (num_req_peers);
141 msg->id = htonl (id);
142 GNUNET_MQ_send (h->mq, ev);
146 * @brief Iterator function over pending requests
148 * Implements #GNUNET_CONTAINER_HashMapIterator32
150 * @param cls rps handle
151 * @param key id of the request
152 * @param value request handle
154 * @return GNUNET_YES to continue iteration
157 resend_requests_iterator (void *cls, uint32_t key, void *value)
159 const struct GNUNET_RPS_Handle *h = cls;
160 const struct GNUNET_RPS_Request_Handle *req_handle = value;
163 send_request (h, req_handle->id, req_handle->num_peers);
164 return GNUNET_YES; /* continue iterating */
168 * @brief Resend all pending requests
170 * This is used to resend all pending requests after the client
171 * reconnected to the service, because the service cancels all
172 * pending requests after reconnection.
174 * @param h rps handle
177 resend_requests (struct GNUNET_RPS_Handle *h)
179 GNUNET_CONTAINER_multihashmap32_iterate (h->req_handlers,
180 resend_requests_iterator,
186 * This function is called, when the service replies to our request.
187 * It verifies that @a msg is well-formed.
189 * @param cls the closure
190 * @param msg the message
191 * @return #GNUNET_OK if @a msg is well-formed
194 check_reply (void *cls,
195 const struct GNUNET_RPS_CS_ReplyMessage *msg)
197 uint16_t msize = ntohs (msg->header.size);
198 uint32_t num_peers = ntohl (msg->num_peers);
201 msize -= sizeof (struct GNUNET_RPS_CS_ReplyMessage);
202 if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) ||
203 (msize % sizeof (struct GNUNET_PeerIdentity) != 0) )
206 return GNUNET_SYSERR;
213 * This function is called, when the service replies to our request.
214 * It calls the callback the caller gave us with the provided closure
215 * and disconnects afterwards.
217 * @param cls the closure
218 * @param msg the message
221 handle_reply (void *cls,
222 const struct GNUNET_RPS_CS_ReplyMessage *msg)
224 struct GNUNET_RPS_Handle *h = cls;
225 struct GNUNET_PeerIdentity *peers;
226 struct GNUNET_RPS_Request_Handle *rh;
229 /* Give the peers back */
230 id = ntohl (msg->id);
231 LOG (GNUNET_ERROR_TYPE_DEBUG,
232 "Service replied with %" PRIu32 " peers for id %" PRIu32 "\n",
233 ntohl (msg->num_peers),
236 peers = (struct GNUNET_PeerIdentity *) &msg[1];
237 GNUNET_assert (GNUNET_YES ==
238 GNUNET_CONTAINER_multihashmap32_contains (h->req_handlers, id));
239 rh = GNUNET_CONTAINER_multihashmap32_get (h->req_handlers, id);
240 GNUNET_assert (NULL != rh);
241 GNUNET_assert (rh->num_peers == ntohl (msg->num_peers));
242 GNUNET_CONTAINER_multihashmap32_remove_all (h->req_handlers, id);
243 rh->ready_cb (rh->ready_cb_cls,
244 ntohl (msg->num_peers),
249 /* Get internals for debugging/profiling purposes */
252 * Request updates of view
254 * @param rps_handle handle to the rps service
255 * @param num_req_peers number of peers we want to receive
256 * (0 for infinite updates)
257 * @param cls a closure that will be given to the callback
258 * @param ready_cb the callback called when the peers are available
261 GNUNET_RPS_view_request (struct GNUNET_RPS_Handle *rps_handle,
262 uint32_t num_updates,
263 GNUNET_RPS_ViewUpdateCB view_update_cb,
266 struct GNUNET_MQ_Envelope *ev;
267 struct GNUNET_RPS_CS_DEBUG_ViewRequest *msg;
269 LOG (GNUNET_ERROR_TYPE_DEBUG,
270 "Client requests %" PRIu32 " view updates\n",
272 rps_handle->view_update_cb = view_update_cb;
273 rps_handle->view_update_cls = cls;
275 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REQUEST);
276 msg->num_updates = htonl (num_updates);
277 GNUNET_MQ_send (rps_handle->mq, ev);
281 * This function is called, when the service updates the view.
282 * It verifies that @a msg is well-formed.
284 * @param cls the closure
285 * @param msg the message
286 * @return #GNUNET_OK if @a msg is well-formed
289 check_view_update (void *cls,
290 const struct GNUNET_RPS_CS_DEBUG_ViewReply *msg)
292 uint16_t msize = ntohs (msg->header.size);
293 uint32_t num_peers = ntohl (msg->num_peers);
296 msize -= sizeof (struct GNUNET_RPS_CS_DEBUG_ViewReply);
297 if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) ||
298 (msize % sizeof (struct GNUNET_PeerIdentity) != 0) )
301 return GNUNET_SYSERR;
307 * This function is called, when the service updated its view.
308 * It calls the callback the caller provided
309 * and disconnects afterwards.
311 * @param msg the message
314 handle_view_update (void *cls,
315 const struct GNUNET_RPS_CS_DEBUG_ViewReply *msg)
317 struct GNUNET_RPS_Handle *h = cls;
318 struct GNUNET_PeerIdentity *peers;
320 /* Give the peers back */
321 LOG (GNUNET_ERROR_TYPE_DEBUG,
322 "New view of %" PRIu32 " peers:\n",
323 ntohl (msg->num_peers));
325 peers = (struct GNUNET_PeerIdentity *) &msg[1];
326 GNUNET_assert (NULL != h);
327 GNUNET_assert (NULL != h->view_update_cb);
328 h->view_update_cb (h->view_update_cls, ntohl (msg->num_peers), peers);
334 * Reconnect to the service
337 reconnect (struct GNUNET_RPS_Handle *h);
341 * Error handler for mq.
343 * This function is called whan mq encounters an error.
344 * Until now mq doesn't provide useful error messages.
346 * @param cls the closure
347 * @param error error code without specyfied meaning
350 mq_error_handler (void *cls,
351 enum GNUNET_MQ_Error error)
353 struct GNUNET_RPS_Handle *h = cls;
355 LOG (GNUNET_ERROR_TYPE_WARNING, "Problem with message queue. error: %i\n\
361 /* Resend all pending request as the service destroyed its knowledge
368 * Reconnect to the service
371 reconnect (struct GNUNET_RPS_Handle *h)
373 struct GNUNET_MQ_MessageHandler mq_handlers[] = {
374 GNUNET_MQ_hd_var_size (reply,
375 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY,
376 struct GNUNET_RPS_CS_ReplyMessage,
378 GNUNET_MQ_hd_var_size (view_update,
379 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REPLY,
380 struct GNUNET_RPS_CS_DEBUG_ViewReply,
382 GNUNET_MQ_handler_end ()
386 GNUNET_MQ_destroy (h->mq);
387 h->mq = GNUNET_CLIENT_connect (h->cfg,
396 * Connect to the rps service
398 * @param cfg configuration to use
399 * @return a handle to the service
401 struct GNUNET_RPS_Handle *
402 GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
404 struct GNUNET_RPS_Handle *h;
406 h = GNUNET_new (struct GNUNET_RPS_Handle);
407 h->current_request_id = 0;
415 h->req_handlers = GNUNET_CONTAINER_multihashmap32_create (4);
421 * Request n random peers.
423 * @param rps_handle handle to the rps service
424 * @param num_req_peers number of peers we want to receive
425 * @param ready_cb the callback called when the peers are available
426 * @param cls closure given to the callback
427 * @return a handle to cancel this request
429 struct GNUNET_RPS_Request_Handle *
430 GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *rps_handle,
431 uint32_t num_req_peers,
432 GNUNET_RPS_NotifyReadyCB ready_cb,
435 struct GNUNET_RPS_Request_Handle *rh;
437 rh = GNUNET_new (struct GNUNET_RPS_Request_Handle);
438 rh->rps_handle = rps_handle;
439 rh->id = rps_handle->current_request_id++;
440 rh->num_peers = num_req_peers;
441 rh->ready_cb = ready_cb;
442 rh->ready_cb_cls = cls;
444 LOG (GNUNET_ERROR_TYPE_DEBUG,
445 "Requesting %" PRIu32 " peers with id %" PRIu32 "\n",
449 GNUNET_CONTAINER_multihashmap32_put (rps_handle->req_handlers, rh->id, rh,
450 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
452 send_request (rps_handle, rh->id, num_req_peers);
458 * Seed rps service with peerIDs.
460 * @param h handle to the rps service
461 * @param n number of peers to seed
462 * @param ids the ids of the peers seeded
465 GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h,
467 const struct GNUNET_PeerIdentity *ids)
470 uint32_t num_peers_max;
471 const struct GNUNET_PeerIdentity *tmp_peer_pointer;
472 struct GNUNET_MQ_Envelope *ev;
473 struct GNUNET_RPS_CS_SeedMessage *msg;
477 LOG (GNUNET_ERROR_TYPE_DEBUG,
478 "Client wants to seed %" PRIu32 " peers:\n",
480 for (i = 0 ; i < n ; i++)
481 LOG (GNUNET_ERROR_TYPE_DEBUG,
484 GNUNET_i2s (&ids[i]));
486 /* The actual size the message occupies */
487 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
488 n * sizeof (struct GNUNET_PeerIdentity);
489 /* The number of peers that fits in one message together with
490 * the respective header */
491 num_peers_max = (GNUNET_MAX_MESSAGE_SIZE -
492 sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
493 sizeof (struct GNUNET_PeerIdentity);
494 tmp_peer_pointer = ids;
496 while (GNUNET_MAX_MESSAGE_SIZE < size_needed)
498 ev = GNUNET_MQ_msg_extra (msg, num_peers_max * sizeof (struct GNUNET_PeerIdentity),
499 GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
500 msg->num_peers = htonl (num_peers_max);
501 GNUNET_memcpy (&msg[1], tmp_peer_pointer, num_peers_max * sizeof (struct GNUNET_PeerIdentity));
502 GNUNET_MQ_send (h->mq, ev);
505 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
506 n * sizeof (struct GNUNET_PeerIdentity);
507 /* Set pointer to beginning of next block of num_peers_max peers */
508 tmp_peer_pointer = &ids[num_peers_max];
511 ev = GNUNET_MQ_msg_extra (msg, n * sizeof (struct GNUNET_PeerIdentity),
512 GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
513 msg->num_peers = htonl (n);
514 GNUNET_memcpy (&msg[1], tmp_peer_pointer, n * sizeof (struct GNUNET_PeerIdentity));
516 GNUNET_MQ_send (h->mq, ev);
520 #ifdef ENABLE_MALICIOUS
522 * Turn RPS service to act malicious.
524 * @param h handle to the rps service
525 * @param type which type of malicious peer to turn to.
526 * 0 Don't act malicious at all
527 * 1 Try to maximise representation
528 * 2 Try to partition the network
529 * (isolate one peer from the rest)
530 * @param n number of @a ids
531 * @param ids the ids of the malicious peers
532 * if @type is 2 the last id is the id of the
533 * peer to be isolated from the rest
536 GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h,
539 const struct GNUNET_PeerIdentity *peer_ids,
540 const struct GNUNET_PeerIdentity *target_peer)
543 uint32_t num_peers_max;
544 const struct GNUNET_PeerIdentity *tmp_peer_pointer;
545 struct GNUNET_MQ_Envelope *ev;
546 struct GNUNET_RPS_CS_ActMaliciousMessage *msg;
550 LOG (GNUNET_ERROR_TYPE_DEBUG,
551 "Client turns malicious (type %" PRIu32 ") with %" PRIu32 " other peers:\n",
554 for (i = 0 ; i < num_peers ; i++)
555 LOG (GNUNET_ERROR_TYPE_DEBUG,
558 GNUNET_i2s (&peer_ids[i]));
560 /* The actual size the message would occupy */
561 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
562 num_peers * sizeof (struct GNUNET_PeerIdentity);
563 /* The number of peers that fit in one message together with
564 * the respective header */
565 num_peers_max = (GNUNET_MAX_MESSAGE_SIZE -
566 sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
567 sizeof (struct GNUNET_PeerIdentity);
568 tmp_peer_pointer = peer_ids;
570 while (GNUNET_MAX_MESSAGE_SIZE < size_needed)
572 LOG (GNUNET_ERROR_TYPE_DEBUG,
573 "Too many peers to send at once, sending %" PRIu32 " (all we can so far)\n",
575 ev = GNUNET_MQ_msg_extra (msg,
576 num_peers_max * sizeof (struct GNUNET_PeerIdentity),
577 GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
578 msg->type = htonl (type);
579 msg->num_peers = htonl (num_peers_max);
582 msg->attacked_peer = peer_ids[num_peers];
583 GNUNET_memcpy (&msg[1],
585 num_peers_max * sizeof (struct GNUNET_PeerIdentity));
587 GNUNET_MQ_send (h->mq, ev);
589 num_peers -= num_peers_max;
590 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
591 num_peers * sizeof (struct GNUNET_PeerIdentity);
592 /* Set pointer to beginning of next block of num_peers_max peers */
593 tmp_peer_pointer = &peer_ids[num_peers_max];
596 ev = GNUNET_MQ_msg_extra (msg,
597 num_peers * sizeof (struct GNUNET_PeerIdentity),
598 GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
599 msg->type = htonl (type);
600 msg->num_peers = htonl (num_peers);
603 msg->attacked_peer = *target_peer;
604 GNUNET_memcpy (&msg[1], tmp_peer_pointer, num_peers * sizeof (struct GNUNET_PeerIdentity));
606 GNUNET_MQ_send (h->mq, ev);
608 #endif /* ENABLE_MALICIOUS */
612 * Cancle an issued request.
614 * @param rh request handle of request to cancle
617 GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh)
619 struct GNUNET_RPS_Handle *h;
620 struct GNUNET_MQ_Envelope *ev;
621 struct GNUNET_RPS_CS_RequestCancelMessage*msg;
623 LOG (GNUNET_ERROR_TYPE_DEBUG,
624 "Cancelling request with id %" PRIu32 "\n",
628 GNUNET_assert (GNUNET_CONTAINER_multihashmap32_contains (h->req_handlers,
630 GNUNET_CONTAINER_multihashmap32_remove_all (h->req_handlers, rh->id);
631 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST_CANCEL);
632 msg->id = htonl (rh->id);
633 GNUNET_MQ_send (rh->rps_handle->mq, ev);
638 * Disconnect from the rps service
640 * @param h the handle to the rps service
643 GNUNET_RPS_disconnect (struct GNUNET_RPS_Handle *h)
645 GNUNET_MQ_destroy (h->mq);
646 if (0 < GNUNET_CONTAINER_multihashmap32_size (h->req_handlers))
647 LOG (GNUNET_ERROR_TYPE_WARNING,
648 "Still waiting for requests\n");
649 GNUNET_CONTAINER_multihashmap32_destroy (h->req_handlers);
654 /* end of rps_api.c */