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"
28 #include "gnunet-service-rps_sampler.h"
32 #define LOG(kind,...) GNUNET_log_from (kind, "rps-api",__VA_ARGS__)
35 * Handle for a request to get peers from biased stream of ids
37 struct GNUNET_RPS_StreamRequestHandle
40 * The client issuing the request.
42 struct GNUNET_RPS_Handle *rps_handle;
45 * The number of requested peers.
47 uint32_t num_peers_left;
50 * The callback to be called when we receive an answer.
52 GNUNET_RPS_NotifyReadyCB ready_cb;
55 * The closure for the callback.
60 * @brief Next element of the DLL
62 struct GNUNET_RPS_StreamRequestHandle *next;
65 * @brief Previous element of the DLL
67 struct GNUNET_RPS_StreamRequestHandle *prev;
72 * Handler to handle requests from a client.
74 struct GNUNET_RPS_Handle
77 * The handle to the client configuration.
79 const struct GNUNET_CONFIGURATION_Handle *cfg;
82 * The message queue to the client.
84 struct GNUNET_MQ_Handle *mq;
87 * @brief Callback called on each update of the view
89 GNUNET_RPS_NotifyReadyCB view_update_cb;
92 * @brief Closure to each requested update of the view
94 void *view_update_cls;
97 * @brief Closure to each requested peer from the biased stream
99 void *stream_input_cls;
102 * @brief Head of the DLL of stream requests
104 struct GNUNET_RPS_StreamRequestHandle *stream_requests_head;
107 * @brief Tail of the DLL of stream requests
109 struct GNUNET_RPS_StreamRequestHandle *stream_requests_tail;
114 * Handler for a single request from a client.
116 struct GNUNET_RPS_Request_Handle
119 * The client issuing the request.
121 struct GNUNET_RPS_Handle *rps_handle;
124 * The number of requested peers.
126 uint32_t num_requests;
129 * @brief The Sampler for the client request
131 struct RPS_Sampler *sampler;
134 * @brief Request handle of the request to the sampler - needed to cancel the request
136 struct RPS_SamplerRequestHandle *sampler_rh;
139 * @brief Request handle of the request of the biased stream of peers -
140 * needed to cancel the request
142 struct GNUNET_RPS_StreamRequestHandle *srh;
145 * The callback to be called when we receive an answer.
147 GNUNET_RPS_NotifyReadyCB ready_cb;
150 * The closure for the callback.
157 * Struct used to pack the callback, its closure (provided by the caller)
158 * and the connection handler to the service to pass it to a callback function.
163 * Callback provided by the client
165 GNUNET_RPS_NotifyReadyCB cb;
168 * Closure provided by the client
173 * Handle to the service connection
175 struct GNUNET_CLIENT_Connection *service_conn;
180 * @brief Create a new handle for a stream request
182 * @param rps_handle The rps handle
183 * @param num_peers The number of desired peers
184 * @param ready_cb The callback to be called, once all peers are ready
185 * @param cls The colsure to provide to the callback
187 * @return The handle to the stream request
189 static struct GNUNET_RPS_StreamRequestHandle *
190 new_stream_request (struct GNUNET_RPS_Handle *rps_handle,
192 GNUNET_RPS_NotifyReadyCB ready_cb,
195 struct GNUNET_RPS_StreamRequestHandle *srh;
197 srh = GNUNET_new (struct GNUNET_RPS_StreamRequestHandle);
199 srh->rps_handle = rps_handle;
200 srh->num_peers_left = num_peers;
201 srh->ready_cb = ready_cb;
202 srh->ready_cb_cls = cls;
203 GNUNET_CONTAINER_DLL_insert (rps_handle->stream_requests_head,
204 rps_handle->stream_requests_tail,
212 * @brief Remove the given stream request from the list of requests and memory
214 * @param srh The request to be removed
215 * @param srh_head Head of the DLL to remove request from
216 * @param srh_tail Tail of the DLL to remove request from
219 remove_stream_request (struct GNUNET_RPS_StreamRequestHandle *srh,
220 struct GNUNET_RPS_StreamRequestHandle *srh_head,
221 struct GNUNET_RPS_StreamRequestHandle *srh_tail)
223 GNUNET_CONTAINER_DLL_remove (srh_head,
232 * @brief Called once the sampler has collected all requested peers.
234 * Calls the callback provided by the client with the corresponding cls.
236 * @param peers The array of @a num_peers that has been returned.
237 * @param num_peers The number of peers that have been returned
238 * @param cls The #GNUNET_RPS_Request_Handle
241 peers_ready_cb (const struct GNUNET_PeerIdentity *peers,
245 struct GNUNET_RPS_Request_Handle *rh = cls;
247 rh->ready_cb (rh->ready_cb_cls,
250 // TODO cleanup, sampler, rh, cancel stuff
251 // TODO screw this function. We can give the cb,cls directly to the sampler.
256 * @brief Callback to collect the peers from the biased stream and put those
259 * @param cls The #GNUNET_RPS_Request_Handle
260 * @param num_peers The number of peer that have been returned
261 * @param peers The array of @a num_peers that have been returned
264 collect_peers_cb (void *cls,
266 const struct GNUNET_PeerIdentity *peers)
268 struct GNUNET_RPS_Request_Handle *rh = cls;
270 for (uint64_t i = 0; i < num_peers; i++)
272 RPS_sampler_update (rh->sampler, &peers[i]);
278 * @brief Create new request handle
280 * @param rps_handle Handle to the service
281 * @param num_requests Number of requests
282 * @param ready_cb Callback
285 * @return The newly created request handle
287 static struct GNUNET_RPS_Request_Handle *
288 new_request_handle (struct GNUNET_RPS_Handle *rps_handle,
289 uint64_t num_requests,
290 GNUNET_RPS_NotifyReadyCB ready_cb,
293 struct GNUNET_RPS_Request_Handle *rh;
295 rh = GNUNET_new (struct GNUNET_RPS_Request_Handle);
296 rh->rps_handle = rps_handle;
297 rh->num_requests = num_requests;
298 rh->sampler = RPS_sampler_mod_init (num_requests,
299 GNUNET_TIME_UNIT_SECONDS); // TODO remove this time-stuff
300 rh->sampler_rh = RPS_sampler_get_n_rand_peers (rh->sampler,
304 rh->srh = GNUNET_RPS_stream_request (rps_handle,
305 0, /* infinite updates */
308 rh->ready_cb = ready_cb;
309 rh->ready_cb_cls = cls;
315 /* Get internals for debugging/profiling purposes */
318 * Request updates of view
320 * @param rps_handle handle to the rps service
321 * @param num_req_peers number of peers we want to receive
322 * (0 for infinite updates)
323 * @param cls a closure that will be given to the callback
324 * @param ready_cb the callback called when the peers are available
327 GNUNET_RPS_view_request (struct GNUNET_RPS_Handle *rps_handle,
328 uint32_t num_updates,
329 GNUNET_RPS_NotifyReadyCB view_update_cb,
332 struct GNUNET_MQ_Envelope *ev;
333 struct GNUNET_RPS_CS_DEBUG_ViewRequest *msg;
335 LOG (GNUNET_ERROR_TYPE_DEBUG,
336 "Client requests %" PRIu32 " view updates\n",
338 rps_handle->view_update_cb = view_update_cb;
339 rps_handle->view_update_cls = cls;
341 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REQUEST);
342 msg->num_updates = htonl (num_updates);
343 GNUNET_MQ_send (rps_handle->mq, ev);
348 * Request biased stream of peers that are being put into the sampler
350 * @param rps_handle handle to the rps service
351 * @param num_req_peers number of peers we want to receive
352 * (0 for infinite updates)
353 * @param cls a closure that will be given to the callback
354 * @param ready_cb the callback called when the peers are available
356 struct GNUNET_RPS_StreamRequestHandle *
357 GNUNET_RPS_stream_request (struct GNUNET_RPS_Handle *rps_handle,
359 GNUNET_RPS_NotifyReadyCB stream_input_cb,
362 struct GNUNET_RPS_StreamRequestHandle *srh;
363 struct GNUNET_MQ_Envelope *ev;
364 struct GNUNET_RPS_CS_DEBUG_StreamRequest *msg;
366 srh = new_stream_request (rps_handle,
367 num_peers, /* num requests */
370 LOG (GNUNET_ERROR_TYPE_DEBUG,
371 "Client requests %" PRIu32 " biased stream updates\n",
374 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_REQUEST);
375 GNUNET_MQ_send (rps_handle->mq, ev);
381 * This function is called, when the service updates the view.
382 * It verifies that @a msg is well-formed.
384 * @param cls the closure
385 * @param msg the message
386 * @return #GNUNET_OK if @a msg is well-formed
389 check_view_update (void *cls,
390 const struct GNUNET_RPS_CS_DEBUG_ViewReply *msg)
392 uint16_t msize = ntohs (msg->header.size);
393 uint32_t num_peers = ntohl (msg->num_peers);
396 msize -= sizeof (struct GNUNET_RPS_CS_DEBUG_ViewReply);
397 if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) ||
398 (msize % sizeof (struct GNUNET_PeerIdentity) != 0) )
401 return GNUNET_SYSERR;
408 * This function is called, when the service updated its view.
409 * It calls the callback the caller provided
410 * and disconnects afterwards.
412 * @param msg the message
415 handle_view_update (void *cls,
416 const struct GNUNET_RPS_CS_DEBUG_ViewReply *msg)
418 struct GNUNET_RPS_Handle *h = cls;
419 struct GNUNET_PeerIdentity *peers;
421 /* Give the peers back */
422 LOG (GNUNET_ERROR_TYPE_DEBUG,
423 "New view of %" PRIu32 " peers:\n",
424 ntohl (msg->num_peers));
426 peers = (struct GNUNET_PeerIdentity *) &msg[1];
427 GNUNET_assert (NULL != h);
428 GNUNET_assert (NULL != h->view_update_cb);
429 h->view_update_cb (h->view_update_cls, ntohl (msg->num_peers), peers);
434 * @brief Send message to service that this client does not want to receive
435 * further updates from the biased peer stream
437 * @param rps_handle The handle representing the service to the client
440 cancel_stream (struct GNUNET_RPS_Handle *rps_handle)
442 struct GNUNET_MQ_Envelope *ev;
444 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_CANCEL);
445 GNUNET_MQ_send (rps_handle->mq, ev);
450 * @brief Cancel a specific request for updates from the biased peer stream
452 * @param srh The request handle to cancel
455 GNUNET_RPS_stream_cancel (struct GNUNET_RPS_StreamRequestHandle *srh)
457 struct GNUNET_RPS_Handle *rps_handle;
459 rps_handle = srh->rps_handle;
460 GNUNET_CONTAINER_DLL_remove (rps_handle->stream_requests_head,
461 rps_handle->stream_requests_tail,
464 if (NULL == rps_handle->stream_requests_head) cancel_stream (rps_handle);
469 * This function is called, when the service sends another peer from the biased
471 * It calls the callback the caller provided
472 * and disconnects afterwards.
474 * TODO merge with check_view_update
476 * @param msg the message
479 check_stream_input (void *cls,
480 const struct GNUNET_RPS_CS_DEBUG_StreamReply *msg)
482 uint16_t msize = ntohs (msg->header.size);
483 uint32_t num_peers = ntohl (msg->num_peers);
486 msize -= sizeof (struct GNUNET_RPS_CS_DEBUG_StreamReply);
487 if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) ||
488 (msize % sizeof (struct GNUNET_PeerIdentity) != 0) )
491 return GNUNET_SYSERR;
497 * This function is called, when the service sends another peer from the biased
499 * It calls the callback the caller provided
500 * and disconnects afterwards.
502 * @param msg the message
505 handle_stream_input (void *cls,
506 const struct GNUNET_RPS_CS_DEBUG_StreamReply *msg)
508 struct GNUNET_RPS_Handle *h = cls;
509 const struct GNUNET_PeerIdentity *peers;
510 /* The following two pointers are used to prevent that new handles are
511 * inserted into the DLL, that is currently iterated over, from within a call
512 * to that handler_cb, are executed and in turn again add themselves to the
513 * iterated DLL infinitely */
514 struct GNUNET_RPS_StreamRequestHandle *srh_head_tmp;
515 struct GNUNET_RPS_StreamRequestHandle *srh_tail_tmp;
517 uint64_t num_peers_return;
519 peers = (struct GNUNET_PeerIdentity *) &msg[1];
520 num_peers = ntohl (msg->num_peers);
521 LOG (GNUNET_ERROR_TYPE_DEBUG,
522 "Received %" PRIu64 " peer(s) from stream input.\n",
524 srh_head_tmp = h->stream_requests_head;
525 srh_tail_tmp = h->stream_requests_tail;
526 h->stream_requests_head = NULL;
527 h->stream_requests_tail = NULL;
528 for (struct GNUNET_RPS_StreamRequestHandle *srh_iter = srh_head_tmp;
530 srh_iter = srh_iter->next)
532 LOG (GNUNET_ERROR_TYPE_DEBUG,
533 "Calling srh - left: %" PRIu64 "\n",
534 srh_iter->num_peers_left);
535 if (0 == srh_iter->num_peers_left) /* infinite updates */
537 num_peers_return = num_peers;
539 else if (num_peers > srh_iter->num_peers_left)
541 num_peers_return = num_peers - srh_iter->num_peers_left;
543 else /* num_peers <= srh_iter->num_peers_left */
545 num_peers_return = srh_iter->num_peers_left - num_peers;
547 srh_iter->ready_cb (srh_iter->ready_cb_cls,
550 if (0 == srh_iter->num_peers_left) ;
551 else if (num_peers_return >= srh_iter->num_peers_left)
553 remove_stream_request (srh_iter,
559 srh_iter->num_peers_left -= num_peers_return;
562 for (struct GNUNET_RPS_StreamRequestHandle *srh_iter = srh_head_tmp;
564 srh_iter = srh_iter->next)
566 GNUNET_CONTAINER_DLL_insert (h->stream_requests_head,
567 h->stream_requests_tail,
571 if (NULL == h->stream_requests_head)
579 * Reconnect to the service
582 reconnect (struct GNUNET_RPS_Handle *h);
586 * Error handler for mq.
588 * This function is called whan mq encounters an error.
589 * Until now mq doesn't provide useful error messages.
591 * @param cls the closure
592 * @param error error code without specyfied meaning
595 mq_error_handler (void *cls,
596 enum GNUNET_MQ_Error error)
598 struct GNUNET_RPS_Handle *h = cls;
600 LOG (GNUNET_ERROR_TYPE_WARNING, "Problem with message queue. error: %i\n\
606 /* Resend all pending request as the service destroyed its knowledge
612 * Reconnect to the service
615 reconnect (struct GNUNET_RPS_Handle *h)
617 struct GNUNET_MQ_MessageHandler mq_handlers[] = {
618 GNUNET_MQ_hd_var_size (view_update,
619 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REPLY,
620 struct GNUNET_RPS_CS_DEBUG_ViewReply,
622 GNUNET_MQ_hd_var_size (stream_input,
623 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_REPLY,
624 struct GNUNET_RPS_CS_DEBUG_StreamReply,
626 GNUNET_MQ_handler_end ()
630 GNUNET_MQ_destroy (h->mq);
631 h->mq = GNUNET_CLIENT_connect (h->cfg,
640 * Connect to the rps service
642 * @param cfg configuration to use
643 * @return a handle to the service
645 struct GNUNET_RPS_Handle *
646 GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
648 struct GNUNET_RPS_Handle *h;
650 h = GNUNET_new (struct GNUNET_RPS_Handle);
663 * Request n random peers.
665 * @param rps_handle handle to the rps service
666 * @param num_req_peers number of peers we want to receive
667 * @param ready_cb the callback called when the peers are available
668 * @param cls closure given to the callback
669 * @return a handle to cancel this request
671 struct GNUNET_RPS_Request_Handle *
672 GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *rps_handle,
673 uint32_t num_req_peers,
674 GNUNET_RPS_NotifyReadyCB ready_cb,
677 struct GNUNET_RPS_Request_Handle *rh;
679 rh = new_request_handle (rps_handle,
690 * Seed rps service with peerIDs.
692 * @param h handle to the rps service
693 * @param n number of peers to seed
694 * @param ids the ids of the peers seeded
697 GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h,
699 const struct GNUNET_PeerIdentity *ids)
702 uint32_t num_peers_max;
703 const struct GNUNET_PeerIdentity *tmp_peer_pointer;
704 struct GNUNET_MQ_Envelope *ev;
705 struct GNUNET_RPS_CS_SeedMessage *msg;
709 LOG (GNUNET_ERROR_TYPE_DEBUG,
710 "Client wants to seed %" PRIu32 " peers:\n",
712 for (i = 0 ; i < n ; i++)
713 LOG (GNUNET_ERROR_TYPE_DEBUG,
716 GNUNET_i2s (&ids[i]));
718 /* The actual size the message occupies */
719 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
720 n * sizeof (struct GNUNET_PeerIdentity);
721 /* The number of peers that fits in one message together with
722 * the respective header */
723 num_peers_max = (GNUNET_MAX_MESSAGE_SIZE -
724 sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
725 sizeof (struct GNUNET_PeerIdentity);
726 tmp_peer_pointer = ids;
728 while (GNUNET_MAX_MESSAGE_SIZE < size_needed)
730 ev = GNUNET_MQ_msg_extra (msg, num_peers_max * sizeof (struct GNUNET_PeerIdentity),
731 GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
732 msg->num_peers = htonl (num_peers_max);
733 GNUNET_memcpy (&msg[1], tmp_peer_pointer, num_peers_max * sizeof (struct GNUNET_PeerIdentity));
734 GNUNET_MQ_send (h->mq, ev);
737 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
738 n * sizeof (struct GNUNET_PeerIdentity);
739 /* Set pointer to beginning of next block of num_peers_max peers */
740 tmp_peer_pointer = &ids[num_peers_max];
743 ev = GNUNET_MQ_msg_extra (msg, n * sizeof (struct GNUNET_PeerIdentity),
744 GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
745 msg->num_peers = htonl (n);
746 GNUNET_memcpy (&msg[1], tmp_peer_pointer, n * sizeof (struct GNUNET_PeerIdentity));
748 GNUNET_MQ_send (h->mq, ev);
752 #ifdef ENABLE_MALICIOUS
754 * Turn RPS service to act malicious.
756 * @param h handle to the rps service
757 * @param type which type of malicious peer to turn to.
758 * 0 Don't act malicious at all
759 * 1 Try to maximise representation
760 * 2 Try to partition the network
761 * (isolate one peer from the rest)
762 * @param n number of @a ids
763 * @param ids the ids of the malicious peers
764 * if @type is 2 the last id is the id of the
765 * peer to be isolated from the rest
768 GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h,
771 const struct GNUNET_PeerIdentity *peer_ids,
772 const struct GNUNET_PeerIdentity *target_peer)
775 uint32_t num_peers_max;
776 const struct GNUNET_PeerIdentity *tmp_peer_pointer;
777 struct GNUNET_MQ_Envelope *ev;
778 struct GNUNET_RPS_CS_ActMaliciousMessage *msg;
782 LOG (GNUNET_ERROR_TYPE_DEBUG,
783 "Client turns malicious (type %" PRIu32 ") with %" PRIu32 " other peers:\n",
786 for (i = 0 ; i < num_peers ; i++)
787 LOG (GNUNET_ERROR_TYPE_DEBUG,
790 GNUNET_i2s (&peer_ids[i]));
792 /* The actual size the message would occupy */
793 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
794 num_peers * sizeof (struct GNUNET_PeerIdentity);
795 /* The number of peers that fit in one message together with
796 * the respective header */
797 num_peers_max = (GNUNET_MAX_MESSAGE_SIZE -
798 sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
799 sizeof (struct GNUNET_PeerIdentity);
800 tmp_peer_pointer = peer_ids;
802 while (GNUNET_MAX_MESSAGE_SIZE < size_needed)
804 LOG (GNUNET_ERROR_TYPE_DEBUG,
805 "Too many peers to send at once, sending %" PRIu32 " (all we can so far)\n",
807 ev = GNUNET_MQ_msg_extra (msg,
808 num_peers_max * sizeof (struct GNUNET_PeerIdentity),
809 GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
810 msg->type = htonl (type);
811 msg->num_peers = htonl (num_peers_max);
814 msg->attacked_peer = peer_ids[num_peers];
815 GNUNET_memcpy (&msg[1],
817 num_peers_max * sizeof (struct GNUNET_PeerIdentity));
819 GNUNET_MQ_send (h->mq, ev);
821 num_peers -= num_peers_max;
822 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
823 num_peers * sizeof (struct GNUNET_PeerIdentity);
824 /* Set pointer to beginning of next block of num_peers_max peers */
825 tmp_peer_pointer = &peer_ids[num_peers_max];
828 ev = GNUNET_MQ_msg_extra (msg,
829 num_peers * sizeof (struct GNUNET_PeerIdentity),
830 GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
831 msg->type = htonl (type);
832 msg->num_peers = htonl (num_peers);
835 msg->attacked_peer = *target_peer;
836 GNUNET_memcpy (&msg[1], tmp_peer_pointer, num_peers * sizeof (struct GNUNET_PeerIdentity));
838 GNUNET_MQ_send (h->mq, ev);
840 #endif /* ENABLE_MALICIOUS */
844 * Cancle an issued request.
846 * @param rh request handle of request to cancle
849 GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh)
851 struct GNUNET_RPS_Handle *h;
856 remove_stream_request (rh->srh,
857 h->stream_requests_head,
858 h->stream_requests_tail);
860 if (NULL == h->stream_requests_head) cancel_stream(h);
861 if (NULL != rh->sampler_rh)
863 RPS_sampler_request_cancel (rh->sampler_rh);
865 RPS_sampler_destroy (rh->sampler);
871 * Disconnect from the rps service
873 * @param h the handle to the rps service
876 GNUNET_RPS_disconnect (struct GNUNET_RPS_Handle *h)
878 GNUNET_MQ_destroy (h->mq);
879 if (NULL != h->stream_requests_head)
881 LOG (GNUNET_ERROR_TYPE_WARNING,
882 "Still waiting for requests\n");
888 /* end of rps_api.c */