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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
24 * @author Julius Bünger
27 #include "gnunet_util_lib.h"
29 #include "gnunet_rps_service.h"
30 #include "rps-sampler_client.h"
32 #include "gnunet_nse_service.h"
36 #define LOG(kind,...) GNUNET_log_from (kind, "rps-api",__VA_ARGS__)
39 * Handle for a request to get peers from biased stream of ids
41 struct GNUNET_RPS_StreamRequestHandle
44 * The client issuing the request.
46 struct GNUNET_RPS_Handle *rps_handle;
49 * The callback to be called when we receive an answer.
51 GNUNET_RPS_NotifyReadyCB ready_cb;
54 * The closure for the callback.
59 * @brief Scheduler task for scheduled callback
61 struct GNUNET_SCHEDULER_Task *callback_task;
64 * @brief Next element of the DLL
66 struct GNUNET_RPS_StreamRequestHandle *next;
69 * @brief Previous element of the DLL
71 struct GNUNET_RPS_StreamRequestHandle *prev;
76 * Handler to handle requests from a client.
78 struct GNUNET_RPS_Handle
81 * The handle to the client configuration.
83 const struct GNUNET_CONFIGURATION_Handle *cfg;
86 * The message queue to the client.
88 struct GNUNET_MQ_Handle *mq;
91 * @brief Callback called on each update of the view
93 GNUNET_RPS_NotifyReadyCB view_update_cb;
96 * @brief Closure to each requested update of the view
98 void *view_update_cls;
101 * @brief Closure to each requested peer from the biased stream
103 void *stream_input_cls;
106 * @brief Head of the DLL of stream requests
108 struct GNUNET_RPS_StreamRequestHandle *stream_requests_head;
111 * @brief Tail of the DLL of stream requests
113 struct GNUNET_RPS_StreamRequestHandle *stream_requests_tail;
116 * @brief Handle to nse service
118 struct GNUNET_NSE_Handle *nse;
121 * @brief Pointer to the head element in DLL of request handles
123 struct GNUNET_RPS_Request_Handle *rh_head;
126 * @brief Pointer to the tail element in DLL of request handles
128 struct GNUNET_RPS_Request_Handle *rh_tail;
131 * @brief Pointer to the head element in DLL of single request handles
133 struct GNUNET_RPS_Request_Handle_Single_Info *rhs_head;
136 * @brief Pointer to the tail element in DLL of single request handles
138 struct GNUNET_RPS_Request_Handle_Single_Info *rhs_tail;
141 * @brief The desired probability with which we want to have observed all
144 float desired_probability;
147 * @brief A factor that catches the 'bias' of a random stream of peer ids.
149 * As introduced by Brahms: Factor between the number of unique ids in a
150 * truly random stream and number of unique ids in the gossip stream.
152 float deficiency_factor;
157 * Handler for a single request from a client.
159 struct GNUNET_RPS_Request_Handle
162 * The client issuing the request.
164 struct GNUNET_RPS_Handle *rps_handle;
167 * The number of requested peers.
169 uint32_t num_requests;
172 * @brief The Sampler for the client request
174 struct RPS_Sampler *sampler;
177 * @brief Request handle of the request to the sampler - needed to cancel the request
179 struct RPS_SamplerRequestHandle *sampler_rh;
182 * @brief Request handle of the request of the biased stream of peers -
183 * needed to cancel the request
185 struct GNUNET_RPS_StreamRequestHandle *srh;
188 * The callback to be called when we receive an answer.
190 GNUNET_RPS_NotifyReadyCB ready_cb;
193 * The closure for the callback.
198 * @brief Pointer to next element in DLL
200 struct GNUNET_RPS_Request_Handle *next;
203 * @brief Pointer to previous element in DLL
205 struct GNUNET_RPS_Request_Handle *prev;
210 * Handler for a single request from a client.
212 struct GNUNET_RPS_Request_Handle_Single_Info
215 * The client issuing the request.
217 struct GNUNET_RPS_Handle *rps_handle;
220 * @brief The Sampler for the client request
222 struct RPS_Sampler *sampler;
225 * @brief Request handle of the request to the sampler - needed to cancel the request
227 struct RPS_SamplerRequestHandleSingleInfo *sampler_rh;
230 * @brief Request handle of the request of the biased stream of peers -
231 * needed to cancel the request
233 struct GNUNET_RPS_StreamRequestHandle *srh;
236 * The callback to be called when we receive an answer.
238 GNUNET_RPS_NotifyReadySingleInfoCB ready_cb;
241 * The closure for the callback.
246 * @brief Pointer to next element in DLL
248 struct GNUNET_RPS_Request_Handle_Single_Info *next;
251 * @brief Pointer to previous element in DLL
253 struct GNUNET_RPS_Request_Handle_Single_Info *prev;
258 * Struct used to pack the callback, its closure (provided by the caller)
259 * and the connection handler to the service to pass it to a callback function.
264 * Callback provided by the client
266 GNUNET_RPS_NotifyReadyCB cb;
269 * Closure provided by the client
274 * Handle to the service connection
276 struct GNUNET_CLIENT_Connection *service_conn;
281 * @brief Peers received from the biased stream to be passed to all
284 static struct GNUNET_PeerIdentity *srh_callback_peers;
287 * @brief Number of peers in the biased stream that are to be passed to all
290 static uint64_t srh_callback_num_peers;
294 * @brief Create a new handle for a stream request
296 * @param rps_handle The rps handle
297 * @param num_peers The number of desired peers
298 * @param ready_cb The callback to be called, once all peers are ready
299 * @param cls The colsure to provide to the callback
301 * @return The handle to the stream request
303 static struct GNUNET_RPS_StreamRequestHandle *
304 new_stream_request (struct GNUNET_RPS_Handle *rps_handle,
305 GNUNET_RPS_NotifyReadyCB ready_cb,
308 struct GNUNET_RPS_StreamRequestHandle *srh;
310 srh = GNUNET_new (struct GNUNET_RPS_StreamRequestHandle);
311 srh->rps_handle = rps_handle;
312 srh->ready_cb = ready_cb;
313 srh->ready_cb_cls = cls;
314 GNUNET_CONTAINER_DLL_insert (rps_handle->stream_requests_head,
315 rps_handle->stream_requests_tail,
323 * @brief Remove the given stream request from the list of requests and memory
325 * @param srh The request to be removed
328 remove_stream_request (struct GNUNET_RPS_StreamRequestHandle *srh)
330 struct GNUNET_RPS_Handle *rps_handle = srh->rps_handle;
332 GNUNET_assert (NULL != srh);
333 if (NULL != srh->callback_task)
335 GNUNET_SCHEDULER_cancel (srh->callback_task);
336 srh->callback_task = NULL;
338 GNUNET_CONTAINER_DLL_remove (rps_handle->stream_requests_head,
339 rps_handle->stream_requests_tail,
346 * @brief Called once the sampler has collected all requested peers.
348 * Calls the callback provided by the client with the corresponding cls.
350 * @param peers The array of @a num_peers that has been returned.
351 * @param num_peers The number of peers that have been returned
352 * @param cls The #GNUNET_RPS_Request_Handle
355 peers_ready_cb (const struct GNUNET_PeerIdentity *peers,
359 struct GNUNET_RPS_Request_Handle *rh = cls;
361 rh->sampler_rh = NULL;
362 rh->ready_cb (rh->ready_cb_cls,
365 GNUNET_RPS_request_cancel (rh);
370 * @brief Called once the sampler has collected the requested peer.
372 * Calls the callback provided by the client with the corresponding cls.
374 * @param peers The array of @a num_peers that has been returned.
375 * @param num_peers The number of peers that have been returned
376 * @param cls The #GNUNET_RPS_Request_Handle
377 * @param probability Probability with which all IDs have been observed
378 * @param num_observed Number of observed IDs
381 peer_info_ready_cb (const struct GNUNET_PeerIdentity *peers,
384 uint32_t num_observed)
386 struct GNUNET_RPS_Request_Handle_Single_Info *rh = cls;
388 rh->sampler_rh = NULL;
389 rh->ready_cb (rh->ready_cb_cls,
393 GNUNET_RPS_request_single_info_cancel (rh);
398 * @brief Callback to collect the peers from the biased stream and put those
401 * @param cls The #GNUNET_RPS_Request_Handle
402 * @param num_peers The number of peer that have been returned
403 * @param peers The array of @a num_peers that have been returned
406 collect_peers_cb (void *cls,
408 const struct GNUNET_PeerIdentity *peers)
410 struct GNUNET_RPS_Request_Handle *rh = cls;
412 LOG (GNUNET_ERROR_TYPE_DEBUG,
413 "Service sent %" PRIu64 " peers from stream\n",
415 for (uint64_t i = 0; i < num_peers; i++)
417 RPS_sampler_update (rh->sampler, &peers[i]);
423 * @brief Callback to collect the peers from the biased stream and put those
426 * This version is for the modified #GNUNET_RPS_Request_Handle_Single_Info
428 * @param cls The #GNUNET_RPS_Request_Handle
429 * @param num_peers The number of peer that have been returned
430 * @param peers The array of @a num_peers that have been returned
433 collect_peers_info_cb (void *cls,
435 const struct GNUNET_PeerIdentity *peers)
437 struct GNUNET_RPS_Request_Handle_Single_Info *rhs = cls;
439 LOG (GNUNET_ERROR_TYPE_DEBUG,
440 "Service sent %" PRIu64 " peers from stream\n",
442 for (uint64_t i = 0; i < num_peers; i++)
444 RPS_sampler_update (rhs->sampler, &peers[i]);
449 /* Get internals for debugging/profiling purposes */
452 * Request updates of view
454 * @param rps_handle handle to the rps service
455 * @param num_req_peers number of peers we want to receive
456 * (0 for infinite updates)
457 * @param cls a closure that will be given to the callback
458 * @param ready_cb the callback called when the peers are available
461 GNUNET_RPS_view_request (struct GNUNET_RPS_Handle *rps_handle,
462 uint32_t num_updates,
463 GNUNET_RPS_NotifyReadyCB view_update_cb,
466 struct GNUNET_MQ_Envelope *ev;
467 struct GNUNET_RPS_CS_DEBUG_ViewRequest *msg;
469 LOG (GNUNET_ERROR_TYPE_DEBUG,
470 "Client requests %" PRIu32 " view updates\n",
472 rps_handle->view_update_cb = view_update_cb;
473 rps_handle->view_update_cls = cls;
475 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REQUEST);
476 msg->num_updates = htonl (num_updates);
477 GNUNET_MQ_send (rps_handle->mq, ev);
482 GNUNET_RPS_view_request_cancel (struct GNUNET_RPS_Handle *rps_handle)
484 struct GNUNET_MQ_Envelope *ev;
486 GNUNET_assert (NULL != rps_handle->view_update_cb);
488 rps_handle->view_update_cb = NULL;
490 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_CANCEL);
491 GNUNET_MQ_send (rps_handle->mq, ev);
496 * Request biased stream of peers that are being put into the sampler
498 * @param rps_handle handle to the rps service
499 * @param cls a closure that will be given to the callback
500 * @param ready_cb the callback called when the peers are available
502 struct GNUNET_RPS_StreamRequestHandle *
503 GNUNET_RPS_stream_request (struct GNUNET_RPS_Handle *rps_handle,
504 GNUNET_RPS_NotifyReadyCB stream_input_cb,
507 struct GNUNET_RPS_StreamRequestHandle *srh;
508 struct GNUNET_MQ_Envelope *ev;
509 struct GNUNET_RPS_CS_DEBUG_StreamRequest *msg;
511 srh = new_stream_request (rps_handle,
514 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client requests biased stream updates\n");
516 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_REQUEST);
517 GNUNET_MQ_send (rps_handle->mq, ev);
523 * This function is called, when the service updates the view.
524 * It verifies that @a msg is well-formed.
526 * @param cls the closure
527 * @param msg the message
528 * @return #GNUNET_OK if @a msg is well-formed
531 check_view_update (void *cls,
532 const struct GNUNET_RPS_CS_DEBUG_ViewReply *msg)
534 uint16_t msize = ntohs (msg->header.size);
535 uint32_t num_peers = ntohl (msg->num_peers);
538 msize -= sizeof (struct GNUNET_RPS_CS_DEBUG_ViewReply);
539 if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) ||
540 (msize % sizeof (struct GNUNET_PeerIdentity) != 0) )
543 return GNUNET_SYSERR;
550 * This function is called, when the service updated its view.
551 * It calls the callback the caller provided
552 * and disconnects afterwards.
554 * @param msg the message
557 handle_view_update (void *cls,
558 const struct GNUNET_RPS_CS_DEBUG_ViewReply *msg)
560 struct GNUNET_RPS_Handle *h = cls;
561 struct GNUNET_PeerIdentity *peers;
563 /* Give the peers back */
564 LOG (GNUNET_ERROR_TYPE_DEBUG,
565 "New view of %" PRIu32 " peers:\n",
566 ntohl (msg->num_peers));
568 peers = (struct GNUNET_PeerIdentity *) &msg[1];
569 GNUNET_assert (NULL != h);
570 GNUNET_assert (NULL != h->view_update_cb);
571 h->view_update_cb (h->view_update_cls, ntohl (msg->num_peers), peers);
576 * @brief Send message to service that this client does not want to receive
577 * further updates from the biased peer stream
579 * @param rps_handle The handle representing the service to the client
582 cancel_stream (struct GNUNET_RPS_Handle *rps_handle)
584 struct GNUNET_MQ_Envelope *ev;
586 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_CANCEL);
587 GNUNET_MQ_send (rps_handle->mq, ev);
592 * @brief Cancel a specific request for updates from the biased peer stream
594 * @param srh The request handle to cancel
597 GNUNET_RPS_stream_cancel (struct GNUNET_RPS_StreamRequestHandle *srh)
599 struct GNUNET_RPS_Handle *rps_handle;
601 rps_handle = srh->rps_handle;
602 remove_stream_request (srh);
603 if (NULL == rps_handle->stream_requests_head)
604 cancel_stream (rps_handle);
609 * This function is called, when the service sends another peer from the biased
611 * It calls the callback the caller provided
612 * and disconnects afterwards.
614 * TODO merge with check_view_update
616 * @param msg the message
619 check_stream_input (void *cls,
620 const struct GNUNET_RPS_CS_DEBUG_StreamReply *msg)
622 uint16_t msize = ntohs (msg->header.size);
623 uint32_t num_peers = ntohl (msg->num_peers);
626 msize -= sizeof (struct GNUNET_RPS_CS_DEBUG_StreamReply);
627 if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) ||
628 (msize % sizeof (struct GNUNET_PeerIdentity) != 0) )
631 return GNUNET_SYSERR;
638 * @brief Called by the scheduler to call the callbacks of the srh handlers
640 * @param cls Stream request handle
643 srh_callback_scheduled (void *cls)
645 struct GNUNET_RPS_StreamRequestHandle *srh = cls;
647 srh->callback_task = NULL;
648 srh->ready_cb (srh->ready_cb_cls,
649 srh_callback_num_peers,
655 * This function is called, when the service sends another peer from the biased
657 * It calls the callback the caller provided
658 * and disconnects afterwards.
660 * @param msg the message
663 handle_stream_input (void *cls,
664 const struct GNUNET_RPS_CS_DEBUG_StreamReply *msg)
666 struct GNUNET_RPS_Handle *h = cls;
667 //const struct GNUNET_PeerIdentity *peers;
669 struct GNUNET_RPS_StreamRequestHandle *srh_iter;
670 struct GNUNET_RPS_StreamRequestHandle *srh_next;
672 //peers = (struct GNUNET_PeerIdentity *) &msg[1];
673 num_peers = ntohl (msg->num_peers);
674 srh_callback_num_peers = num_peers;
675 GNUNET_free_non_null (srh_callback_peers);
676 srh_callback_peers = GNUNET_new_array (num_peers,
677 struct GNUNET_PeerIdentity);
678 GNUNET_memcpy (srh_callback_peers,
680 num_peers * sizeof (struct GNUNET_PeerIdentity));
681 LOG (GNUNET_ERROR_TYPE_DEBUG,
682 "Received %" PRIu64 " peer(s) from stream input.\n",
684 for (srh_iter = h->stream_requests_head;
688 LOG (GNUNET_ERROR_TYPE_DEBUG, "Calling srh \n");
689 /* Store next pointer - srh might be removed/freed in callback */
690 srh_next = srh_iter->next;
691 if (NULL != srh_iter->callback_task)
692 GNUNET_SCHEDULER_cancel (srh_iter->callback_task);
693 srh_iter->callback_task =
694 GNUNET_SCHEDULER_add_now (&srh_callback_scheduled,
698 if (NULL == h->stream_requests_head)
706 * Reconnect to the service
709 reconnect (struct GNUNET_RPS_Handle *h);
713 * Error handler for mq.
715 * This function is called whan mq encounters an error.
716 * Until now mq doesn't provide useful error messages.
718 * @param cls the closure
719 * @param error error code without specyfied meaning
722 mq_error_handler (void *cls,
723 enum GNUNET_MQ_Error error)
725 struct GNUNET_RPS_Handle *h = cls;
727 LOG (GNUNET_ERROR_TYPE_WARNING, "Problem with message queue. error: %i\n\
731 // TODO: write GNUNET_MQ_strerror (error)
734 /* Resend all pending request as the service destroyed its knowledge
740 * @brief Create the hash value from the share value that defines the sub
743 * @param share_val Share value
744 * @param hash[out] Pointer to the location in which the hash will be stored.
747 hash_from_share_val (const char *share_val,
748 struct GNUNET_HashCode *hash)
750 GNUNET_CRYPTO_kdf (hash,
751 sizeof (struct GNUNET_HashCode),
761 * @brief Callback for network size estimate - called with new estimates about
762 * the network size, updates all samplers with the new estimate
764 * Implements #GNUNET_NSE_Callback
766 * @param cls the rps handle
767 * @param timestamp unused
768 * @param logestimate the estimate
769 * @param std_dev the standard distribution
773 struct GNUNET_TIME_Absolute timestamp,
777 struct GNUNET_RPS_Handle *h = cls;
781 for (struct GNUNET_RPS_Request_Handle *rh_iter = h->rh_head;
782 NULL != rh_iter && NULL != rh_iter->next;
783 rh_iter = rh_iter->next)
785 RPS_sampler_update_with_nw_size (rh_iter->sampler,
786 GNUNET_NSE_log_estimate_to_n (logestimate));
788 for (struct GNUNET_RPS_Request_Handle_Single_Info *rhs_iter = h->rhs_head;
789 NULL != rhs_iter && NULL != rhs_iter->next;
790 rhs_iter = rhs_iter->next)
792 RPS_sampler_update_with_nw_size (rhs_iter->sampler,
793 GNUNET_NSE_log_estimate_to_n (logestimate));
799 * Reconnect to the service
802 reconnect (struct GNUNET_RPS_Handle *h)
804 struct GNUNET_MQ_MessageHandler mq_handlers[] = {
805 GNUNET_MQ_hd_var_size (view_update,
806 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REPLY,
807 struct GNUNET_RPS_CS_DEBUG_ViewReply,
809 GNUNET_MQ_hd_var_size (stream_input,
810 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_REPLY,
811 struct GNUNET_RPS_CS_DEBUG_StreamReply,
813 GNUNET_MQ_handler_end ()
817 GNUNET_MQ_destroy (h->mq);
818 h->mq = GNUNET_CLIENT_connect (h->cfg,
824 GNUNET_NSE_disconnect (h->nse);
825 h->nse = GNUNET_NSE_connect (h->cfg, &nse_cb, h);
830 * Connect to the rps service
832 * @param cfg configuration to use
833 * @return a handle to the service, NULL on error
835 struct GNUNET_RPS_Handle *
836 GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
838 struct GNUNET_RPS_Handle *h;
840 h = GNUNET_new (struct GNUNET_RPS_Handle);
843 GNUNET_CONFIGURATION_get_value_float (cfg,
845 "DESIRED_PROBABILITY",
846 &h->desired_probability))
848 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
849 "RPS", "DESIRED_PROBABILITY");
853 if (0 > h->desired_probability ||
854 1 < h->desired_probability)
856 LOG (GNUNET_ERROR_TYPE_ERROR,
857 "The desired probability must be in the interval [0;1]\n");
862 GNUNET_CONFIGURATION_get_value_float (cfg,
865 &h->deficiency_factor))
867 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
868 "RPS", "DEFICIENCY_FACTOR");
872 if (0 > h->desired_probability ||
873 1 < h->desired_probability)
875 LOG (GNUNET_ERROR_TYPE_ERROR,
876 "The deficiency factor must be in the interval [0;1]\n");
891 * @brief Start a sub with the given shared value
893 * @param h Handle to rps
894 * @param shared_value The shared value that defines the members of the sub (-gorup)
897 GNUNET_RPS_sub_start (struct GNUNET_RPS_Handle *h,
898 const char *shared_value)
900 struct GNUNET_RPS_CS_SubStartMessage *msg;
901 struct GNUNET_MQ_Envelope *ev;
903 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_SUB_START);
904 hash_from_share_val (shared_value, &msg->hash);
905 msg->round_interval = GNUNET_TIME_relative_hton (// TODO read from config!
906 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30));
907 GNUNET_assert (0 != msg->round_interval.rel_value_us__);
909 GNUNET_MQ_send (h->mq, ev);
914 * @brief Stop a sub with the given shared value
916 * @param h Handle to rps
917 * @param shared_value The shared value that defines the members of the sub (-gorup)
920 GNUNET_RPS_sub_stop (struct GNUNET_RPS_Handle *h,
921 const char *shared_value)
923 struct GNUNET_RPS_CS_SubStopMessage *msg;
924 struct GNUNET_MQ_Envelope *ev;
926 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_SUB_STOP);
927 hash_from_share_val (shared_value, &msg->hash);
929 GNUNET_MQ_send (h->mq, ev);
934 * Request n random peers.
936 * @param rps_handle handle to the rps service
937 * @param num_req_peers number of peers we want to receive
938 * @param ready_cb the callback called when the peers are available
939 * @param cls closure given to the callback
940 * @return a handle to cancel this request
942 struct GNUNET_RPS_Request_Handle *
943 GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *rps_handle,
944 uint32_t num_req_peers,
945 GNUNET_RPS_NotifyReadyCB ready_cb,
948 struct GNUNET_RPS_Request_Handle *rh;
950 LOG (GNUNET_ERROR_TYPE_INFO,
951 "Client requested %" PRIu32 " peers\n",
953 rh = GNUNET_new (struct GNUNET_RPS_Request_Handle);
954 rh->rps_handle = rps_handle;
955 rh->num_requests = num_req_peers;
956 rh->sampler = RPS_sampler_mod_init (num_req_peers,
957 GNUNET_TIME_UNIT_SECONDS); // TODO remove this time-stuff
958 RPS_sampler_set_desired_probability (rh->sampler,
959 rps_handle->desired_probability);
960 RPS_sampler_set_deficiency_factor (rh->sampler,
961 rps_handle->deficiency_factor);
962 rh->sampler_rh = RPS_sampler_get_n_rand_peers (rh->sampler,
966 rh->srh = GNUNET_RPS_stream_request (rps_handle,
969 rh->ready_cb = ready_cb;
970 rh->ready_cb_cls = cls;
971 GNUNET_CONTAINER_DLL_insert (rps_handle->rh_head,
980 * Request one random peer, getting additional information.
982 * @param rps_handle handle to the rps service
983 * @param ready_cb the callback called when the peers are available
984 * @param cls closure given to the callback
985 * @return a handle to cancel this request
987 struct GNUNET_RPS_Request_Handle_Single_Info *
988 GNUNET_RPS_request_peer_info (struct GNUNET_RPS_Handle *rps_handle,
989 GNUNET_RPS_NotifyReadySingleInfoCB ready_cb,
992 struct GNUNET_RPS_Request_Handle_Single_Info *rhs;
993 uint32_t num_req_peers = 1;
995 LOG (GNUNET_ERROR_TYPE_INFO,
996 "Client requested peer with additional info\n");
997 rhs = GNUNET_new (struct GNUNET_RPS_Request_Handle_Single_Info);
998 rhs->rps_handle = rps_handle;
999 rhs->sampler = RPS_sampler_mod_init (num_req_peers,
1000 GNUNET_TIME_UNIT_SECONDS); // TODO remove this time-stuff
1001 RPS_sampler_set_desired_probability (rhs->sampler,
1002 rps_handle->desired_probability);
1003 RPS_sampler_set_deficiency_factor (rhs->sampler,
1004 rps_handle->deficiency_factor);
1005 rhs->sampler_rh = RPS_sampler_get_rand_peer_info (rhs->sampler,
1008 rhs->srh = GNUNET_RPS_stream_request (rps_handle,
1009 collect_peers_info_cb,
1011 rhs->ready_cb = ready_cb;
1012 rhs->ready_cb_cls = cls;
1013 GNUNET_CONTAINER_DLL_insert (rps_handle->rhs_head,
1014 rps_handle->rhs_tail,
1022 * Seed rps service with peerIDs.
1024 * @param h handle to the rps service
1025 * @param n number of peers to seed
1026 * @param ids the ids of the peers seeded
1029 GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h,
1031 const struct GNUNET_PeerIdentity *ids)
1034 uint32_t num_peers_max;
1035 const struct GNUNET_PeerIdentity *tmp_peer_pointer;
1036 struct GNUNET_MQ_Envelope *ev;
1037 struct GNUNET_RPS_CS_SeedMessage *msg;
1039 LOG (GNUNET_ERROR_TYPE_DEBUG,
1040 "Client wants to seed %" PRIu32 " peers:\n",
1042 for (unsigned int i = 0 ; i < n ; i++)
1043 LOG (GNUNET_ERROR_TYPE_DEBUG,
1046 GNUNET_i2s (&ids[i]));
1048 /* The actual size the message occupies */
1049 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
1050 n * sizeof (struct GNUNET_PeerIdentity);
1051 /* The number of peers that fits in one message together with
1052 * the respective header */
1053 num_peers_max = (GNUNET_MAX_MESSAGE_SIZE -
1054 sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
1055 sizeof (struct GNUNET_PeerIdentity);
1056 tmp_peer_pointer = ids;
1058 while (GNUNET_MAX_MESSAGE_SIZE < size_needed)
1060 ev = GNUNET_MQ_msg_extra (msg,
1061 num_peers_max * sizeof (struct GNUNET_PeerIdentity),
1062 GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
1063 msg->num_peers = htonl (num_peers_max);
1064 GNUNET_memcpy (&msg[1],
1066 num_peers_max * sizeof (struct GNUNET_PeerIdentity));
1067 GNUNET_MQ_send (h->mq,
1070 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
1071 n * sizeof (struct GNUNET_PeerIdentity);
1072 /* Set pointer to beginning of next block of num_peers_max peers */
1073 tmp_peer_pointer = &ids[num_peers_max];
1076 ev = GNUNET_MQ_msg_extra (msg,
1077 n * sizeof (struct GNUNET_PeerIdentity),
1078 GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
1079 msg->num_peers = htonl (n);
1080 GNUNET_memcpy (&msg[1],
1082 n * sizeof (struct GNUNET_PeerIdentity));
1083 GNUNET_MQ_send (h->mq,
1088 #if ENABLE_MALICIOUS
1090 * Turn RPS service to act malicious.
1092 * @param h handle to the rps service
1093 * @param type which type of malicious peer to turn to.
1094 * 0 Don't act malicious at all
1095 * 1 Try to maximise representation
1096 * 2 Try to partition the network
1097 * (isolate one peer from the rest)
1098 * @param n number of @a ids
1099 * @param ids the ids of the malicious peers
1100 * if @type is 2 the last id is the id of the
1101 * peer to be isolated from the rest
1104 GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h,
1107 const struct GNUNET_PeerIdentity *peer_ids,
1108 const struct GNUNET_PeerIdentity *target_peer)
1111 uint32_t num_peers_max;
1112 const struct GNUNET_PeerIdentity *tmp_peer_pointer;
1113 struct GNUNET_MQ_Envelope *ev;
1114 struct GNUNET_RPS_CS_ActMaliciousMessage *msg;
1118 LOG (GNUNET_ERROR_TYPE_DEBUG,
1119 "Client turns malicious (type %" PRIu32 ") with %" PRIu32 " other peers:\n",
1122 for (i = 0 ; i < num_peers ; i++)
1123 LOG (GNUNET_ERROR_TYPE_DEBUG,
1126 GNUNET_i2s (&peer_ids[i]));
1128 /* The actual size the message would occupy */
1129 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
1130 num_peers * sizeof (struct GNUNET_PeerIdentity);
1131 /* The number of peers that fit in one message together with
1132 * the respective header */
1133 num_peers_max = (GNUNET_MAX_MESSAGE_SIZE -
1134 sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
1135 sizeof (struct GNUNET_PeerIdentity);
1136 tmp_peer_pointer = peer_ids;
1138 while (GNUNET_MAX_MESSAGE_SIZE < size_needed)
1140 LOG (GNUNET_ERROR_TYPE_DEBUG,
1141 "Too many peers to send at once, sending %" PRIu32 " (all we can so far)\n",
1143 ev = GNUNET_MQ_msg_extra (msg,
1144 num_peers_max * sizeof (struct GNUNET_PeerIdentity),
1145 GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
1146 msg->type = htonl (type);
1147 msg->num_peers = htonl (num_peers_max);
1150 msg->attacked_peer = peer_ids[num_peers];
1151 GNUNET_memcpy (&msg[1],
1153 num_peers_max * sizeof (struct GNUNET_PeerIdentity));
1155 GNUNET_MQ_send (h->mq, ev);
1157 num_peers -= num_peers_max;
1158 size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) +
1159 num_peers * sizeof (struct GNUNET_PeerIdentity);
1160 /* Set pointer to beginning of next block of num_peers_max peers */
1161 tmp_peer_pointer = &peer_ids[num_peers_max];
1164 ev = GNUNET_MQ_msg_extra (msg,
1165 num_peers * sizeof (struct GNUNET_PeerIdentity),
1166 GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
1167 msg->type = htonl (type);
1168 msg->num_peers = htonl (num_peers);
1171 msg->attacked_peer = *target_peer;
1172 GNUNET_memcpy (&msg[1],
1174 num_peers * sizeof (struct GNUNET_PeerIdentity));
1176 GNUNET_MQ_send (h->mq, ev);
1178 #endif /* ENABLE_MALICIOUS */
1182 * Cancle an issued request.
1184 * @param rh request handle of request to cancle
1187 GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh)
1189 struct GNUNET_RPS_Handle *h;
1192 GNUNET_assert (NULL != rh);
1193 GNUNET_assert (NULL != rh->srh);
1194 GNUNET_assert (h == rh->srh->rps_handle);
1195 GNUNET_RPS_stream_cancel (rh->srh);
1197 if (NULL == h->stream_requests_head) cancel_stream(h);
1198 if (NULL != rh->sampler_rh)
1200 RPS_sampler_request_cancel (rh->sampler_rh);
1202 RPS_sampler_destroy (rh->sampler);
1204 GNUNET_CONTAINER_DLL_remove (h->rh_head,
1212 * Cancle an issued single info request.
1214 * @param rhs request handle of request to cancle
1217 GNUNET_RPS_request_single_info_cancel (
1218 struct GNUNET_RPS_Request_Handle_Single_Info *rhs)
1220 struct GNUNET_RPS_Handle *h;
1222 h = rhs->rps_handle;
1223 GNUNET_assert (NULL != rhs);
1224 GNUNET_assert (NULL != rhs->srh);
1225 GNUNET_assert (h == rhs->srh->rps_handle);
1226 GNUNET_RPS_stream_cancel (rhs->srh);
1228 if (NULL == h->stream_requests_head) cancel_stream(h);
1229 if (NULL != rhs->sampler_rh)
1231 RPS_sampler_request_single_info_cancel (rhs->sampler_rh);
1233 RPS_sampler_destroy (rhs->sampler);
1234 rhs->sampler = NULL;
1235 GNUNET_CONTAINER_DLL_remove (h->rhs_head,
1243 * Disconnect from the rps service
1245 * @param h the handle to the rps service
1248 GNUNET_RPS_disconnect (struct GNUNET_RPS_Handle *h)
1250 if (NULL != h->stream_requests_head)
1252 struct GNUNET_RPS_StreamRequestHandle *srh_next;
1254 LOG (GNUNET_ERROR_TYPE_WARNING,
1255 "Still waiting for replies\n");
1256 for (struct GNUNET_RPS_StreamRequestHandle *srh_iter = h->stream_requests_head;
1258 srh_iter = srh_next)
1260 srh_next = srh_iter->next;
1261 GNUNET_RPS_stream_cancel (srh_iter);
1264 if (NULL != h->rh_head)
1266 LOG (GNUNET_ERROR_TYPE_WARNING,
1267 "Not all requests were cancelled!\n");
1268 for (struct GNUNET_RPS_Request_Handle *rh_iter = h->rh_head;
1270 rh_iter = h->rh_head)
1272 GNUNET_RPS_request_cancel (rh_iter);
1275 if (NULL != h->rhs_head)
1277 LOG (GNUNET_ERROR_TYPE_WARNING,
1278 "Not all requests were cancelled!\n");
1279 for (struct GNUNET_RPS_Request_Handle_Single_Info *rhs_iter = h->rhs_head;
1280 h->rhs_head != NULL;
1281 rhs_iter = h->rhs_head)
1283 GNUNET_RPS_request_single_info_cancel (rhs_iter);
1286 if (NULL != srh_callback_peers)
1288 GNUNET_free (srh_callback_peers);
1289 srh_callback_peers = NULL;
1291 if (NULL != h->view_update_cb)
1293 LOG (GNUNET_ERROR_TYPE_WARNING,
1294 "Still waiting for view updates\n");
1295 GNUNET_RPS_view_request_cancel (h);
1298 GNUNET_NSE_disconnect (h->nse);
1299 GNUNET_MQ_destroy (h->mq);
1304 /* end of rps_api.c */