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);
539 msize -= sizeof(struct GNUNET_RPS_CS_DEBUG_ViewReply);
540 if ((msize / sizeof(struct GNUNET_PeerIdentity) != num_peers) ||
541 (msize % sizeof(struct GNUNET_PeerIdentity) != 0))
544 return GNUNET_SYSERR;
551 * This function is called, when the service updated its view.
552 * It calls the callback the caller provided
553 * and disconnects afterwards.
555 * @param msg the message
558 handle_view_update (void *cls,
559 const struct GNUNET_RPS_CS_DEBUG_ViewReply *msg)
561 struct GNUNET_RPS_Handle *h = cls;
562 struct GNUNET_PeerIdentity *peers;
564 /* Give the peers back */
565 LOG (GNUNET_ERROR_TYPE_DEBUG,
566 "New view of %" PRIu32 " peers:\n",
567 ntohl (msg->num_peers));
569 peers = (struct GNUNET_PeerIdentity *) &msg[1];
570 GNUNET_assert (NULL != h);
571 GNUNET_assert (NULL != h->view_update_cb);
572 h->view_update_cb (h->view_update_cls, ntohl (msg->num_peers), peers);
577 * @brief Send message to service that this client does not want to receive
578 * further updates from the biased peer stream
580 * @param rps_handle The handle representing the service to the client
583 cancel_stream (struct GNUNET_RPS_Handle *rps_handle)
585 struct GNUNET_MQ_Envelope *ev;
587 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_CANCEL);
588 GNUNET_MQ_send (rps_handle->mq, ev);
593 * @brief Cancel a specific request for updates from the biased peer stream
595 * @param srh The request handle to cancel
598 GNUNET_RPS_stream_cancel (struct GNUNET_RPS_StreamRequestHandle *srh)
600 struct GNUNET_RPS_Handle *rps_handle;
602 rps_handle = srh->rps_handle;
603 remove_stream_request (srh);
604 if (NULL == rps_handle->stream_requests_head)
605 cancel_stream (rps_handle);
610 * This function is called, when the service sends another peer from the biased
612 * It calls the callback the caller provided
613 * and disconnects afterwards.
615 * TODO merge with check_view_update
617 * @param msg the message
620 check_stream_input (void *cls,
621 const struct GNUNET_RPS_CS_DEBUG_StreamReply *msg)
623 uint16_t msize = ntohs (msg->header.size);
624 uint32_t num_peers = ntohl (msg->num_peers);
628 msize -= sizeof(struct GNUNET_RPS_CS_DEBUG_StreamReply);
629 if ((msize / sizeof(struct GNUNET_PeerIdentity) != num_peers) ||
630 (msize % sizeof(struct GNUNET_PeerIdentity) != 0))
633 return GNUNET_SYSERR;
640 * @brief Called by the scheduler to call the callbacks of the srh handlers
642 * @param cls Stream request handle
645 srh_callback_scheduled (void *cls)
647 struct GNUNET_RPS_StreamRequestHandle *srh = cls;
649 srh->callback_task = NULL;
650 srh->ready_cb (srh->ready_cb_cls,
651 srh_callback_num_peers,
657 * This function is called, when the service sends another peer from the biased
659 * It calls the callback the caller provided
660 * and disconnects afterwards.
662 * @param msg the message
665 handle_stream_input (void *cls,
666 const struct GNUNET_RPS_CS_DEBUG_StreamReply *msg)
668 struct GNUNET_RPS_Handle *h = cls;
669 // const struct GNUNET_PeerIdentity *peers;
671 struct GNUNET_RPS_StreamRequestHandle *srh_iter;
672 struct GNUNET_RPS_StreamRequestHandle *srh_next;
674 // peers = (struct GNUNET_PeerIdentity *) &msg[1];
675 num_peers = ntohl (msg->num_peers);
676 srh_callback_num_peers = num_peers;
677 GNUNET_free_non_null (srh_callback_peers);
678 srh_callback_peers = GNUNET_new_array (num_peers,
679 struct GNUNET_PeerIdentity);
680 GNUNET_memcpy (srh_callback_peers,
682 num_peers * sizeof(struct GNUNET_PeerIdentity));
683 LOG (GNUNET_ERROR_TYPE_DEBUG,
684 "Received %" PRIu64 " peer(s) from stream input.\n",
686 for (srh_iter = h->stream_requests_head;
690 LOG (GNUNET_ERROR_TYPE_DEBUG, "Calling srh \n");
691 /* Store next pointer - srh might be removed/freed in callback */
692 srh_next = srh_iter->next;
693 if (NULL != srh_iter->callback_task)
694 GNUNET_SCHEDULER_cancel (srh_iter->callback_task);
695 srh_iter->callback_task =
696 GNUNET_SCHEDULER_add_now (&srh_callback_scheduled,
700 if (NULL == h->stream_requests_head)
708 * Reconnect to the service
711 reconnect (struct GNUNET_RPS_Handle *h);
715 * Error handler for mq.
717 * This function is called whan mq encounters an error.
718 * Until now mq doesn't provide useful error messages.
720 * @param cls the closure
721 * @param error error code without specyfied meaning
724 mq_error_handler (void *cls,
725 enum GNUNET_MQ_Error error)
727 struct GNUNET_RPS_Handle *h = cls;
730 LOG (GNUNET_ERROR_TYPE_WARNING,
731 "Problem with message queue. error: %i\n\
735 // TODO: write GNUNET_MQ_strerror (error)
738 /* Resend all pending request as the service destroyed its knowledge
744 * @brief Create the hash value from the share value that defines the sub
747 * @param share_val Share value
748 * @param hash[out] Pointer to the location in which the hash will be stored.
751 hash_from_share_val (const char *share_val,
752 struct GNUNET_HashCode *hash)
754 GNUNET_CRYPTO_kdf (hash,
755 sizeof(struct GNUNET_HashCode),
765 * @brief Callback for network size estimate - called with new estimates about
766 * the network size, updates all samplers with the new estimate
768 * Implements #GNUNET_NSE_Callback
770 * @param cls the rps handle
771 * @param timestamp unused
772 * @param logestimate the estimate
773 * @param std_dev the standard distribution
777 struct GNUNET_TIME_Absolute timestamp,
781 struct GNUNET_RPS_Handle *h = cls;
786 for (struct GNUNET_RPS_Request_Handle *rh_iter = h->rh_head;
787 NULL != rh_iter && NULL != rh_iter->next;
788 rh_iter = rh_iter->next)
790 RPS_sampler_update_with_nw_size (rh_iter->sampler,
791 GNUNET_NSE_log_estimate_to_n (
794 for (struct GNUNET_RPS_Request_Handle_Single_Info *rhs_iter = h->rhs_head;
795 NULL != rhs_iter && NULL != rhs_iter->next;
796 rhs_iter = rhs_iter->next)
798 RPS_sampler_update_with_nw_size (rhs_iter->sampler,
799 GNUNET_NSE_log_estimate_to_n (
806 * Reconnect to the service
809 reconnect (struct GNUNET_RPS_Handle *h)
811 struct GNUNET_MQ_MessageHandler mq_handlers[] = {
812 GNUNET_MQ_hd_var_size (view_update,
813 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REPLY,
814 struct GNUNET_RPS_CS_DEBUG_ViewReply,
816 GNUNET_MQ_hd_var_size (stream_input,
817 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_REPLY,
818 struct GNUNET_RPS_CS_DEBUG_StreamReply,
820 GNUNET_MQ_handler_end ()
824 GNUNET_MQ_destroy (h->mq);
825 h->mq = GNUNET_CLIENT_connect (h->cfg,
831 GNUNET_NSE_disconnect (h->nse);
832 h->nse = GNUNET_NSE_connect (h->cfg, &nse_cb, h);
837 * Connect to the rps service
839 * @param cfg configuration to use
840 * @return a handle to the service, NULL on error
842 struct GNUNET_RPS_Handle *
843 GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
845 struct GNUNET_RPS_Handle *h;
847 h = GNUNET_new (struct GNUNET_RPS_Handle);
850 GNUNET_CONFIGURATION_get_value_float (cfg,
852 "DESIRED_PROBABILITY",
853 &h->desired_probability))
855 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
856 "RPS", "DESIRED_PROBABILITY");
860 if ((0 > h->desired_probability) ||
861 (1 < h->desired_probability) )
863 LOG (GNUNET_ERROR_TYPE_ERROR,
864 "The desired probability must be in the interval [0;1]\n");
869 GNUNET_CONFIGURATION_get_value_float (cfg,
872 &h->deficiency_factor))
874 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
875 "RPS", "DEFICIENCY_FACTOR");
879 if ((0 > h->desired_probability) ||
880 (1 < h->desired_probability) )
882 LOG (GNUNET_ERROR_TYPE_ERROR,
883 "The deficiency factor must be in the interval [0;1]\n");
898 * @brief Start a sub with the given shared value
900 * @param h Handle to rps
901 * @param shared_value The shared value that defines the members of the sub (-gorup)
904 GNUNET_RPS_sub_start (struct GNUNET_RPS_Handle *h,
905 const char *shared_value)
907 struct GNUNET_RPS_CS_SubStartMessage *msg;
908 struct GNUNET_MQ_Envelope *ev;
910 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_SUB_START);
911 hash_from_share_val (shared_value, &msg->hash);
912 msg->round_interval = GNUNET_TIME_relative_hton ( // TODO read from config!
913 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30));
914 GNUNET_assert (0 != msg->round_interval.rel_value_us__);
916 GNUNET_MQ_send (h->mq, ev);
921 * @brief Stop a sub with the given shared value
923 * @param h Handle to rps
924 * @param shared_value The shared value that defines the members of the sub (-gorup)
927 GNUNET_RPS_sub_stop (struct GNUNET_RPS_Handle *h,
928 const char *shared_value)
930 struct GNUNET_RPS_CS_SubStopMessage *msg;
931 struct GNUNET_MQ_Envelope *ev;
933 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_SUB_STOP);
934 hash_from_share_val (shared_value, &msg->hash);
936 GNUNET_MQ_send (h->mq, ev);
941 * Request n random peers.
943 * @param rps_handle handle to the rps service
944 * @param num_req_peers number of peers we want to receive
945 * @param ready_cb the callback called when the peers are available
946 * @param cls closure given to the callback
947 * @return a handle to cancel this request
949 struct GNUNET_RPS_Request_Handle *
950 GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *rps_handle,
951 uint32_t num_req_peers,
952 GNUNET_RPS_NotifyReadyCB ready_cb,
955 struct GNUNET_RPS_Request_Handle *rh;
957 LOG (GNUNET_ERROR_TYPE_INFO,
958 "Client requested %" PRIu32 " peers\n",
960 rh = GNUNET_new (struct GNUNET_RPS_Request_Handle);
961 rh->rps_handle = rps_handle;
962 rh->num_requests = num_req_peers;
963 rh->sampler = RPS_sampler_mod_init (num_req_peers,
964 GNUNET_TIME_UNIT_SECONDS); // TODO remove this time-stuff
965 RPS_sampler_set_desired_probability (rh->sampler,
966 rps_handle->desired_probability);
967 RPS_sampler_set_deficiency_factor (rh->sampler,
968 rps_handle->deficiency_factor);
969 rh->sampler_rh = RPS_sampler_get_n_rand_peers (rh->sampler,
973 rh->srh = GNUNET_RPS_stream_request (rps_handle,
976 rh->ready_cb = ready_cb;
977 rh->ready_cb_cls = cls;
978 GNUNET_CONTAINER_DLL_insert (rps_handle->rh_head,
987 * Request one random peer, getting additional information.
989 * @param rps_handle handle to the rps service
990 * @param ready_cb the callback called when the peers are available
991 * @param cls closure given to the callback
992 * @return a handle to cancel this request
994 struct GNUNET_RPS_Request_Handle_Single_Info *
995 GNUNET_RPS_request_peer_info (struct GNUNET_RPS_Handle *rps_handle,
996 GNUNET_RPS_NotifyReadySingleInfoCB ready_cb,
999 struct GNUNET_RPS_Request_Handle_Single_Info *rhs;
1000 uint32_t num_req_peers = 1;
1002 LOG (GNUNET_ERROR_TYPE_INFO,
1003 "Client requested peer with additional info\n");
1004 rhs = GNUNET_new (struct GNUNET_RPS_Request_Handle_Single_Info);
1005 rhs->rps_handle = rps_handle;
1006 rhs->sampler = RPS_sampler_mod_init (num_req_peers,
1007 GNUNET_TIME_UNIT_SECONDS); // TODO remove this time-stuff
1008 RPS_sampler_set_desired_probability (rhs->sampler,
1009 rps_handle->desired_probability);
1010 RPS_sampler_set_deficiency_factor (rhs->sampler,
1011 rps_handle->deficiency_factor);
1012 rhs->sampler_rh = RPS_sampler_get_rand_peer_info (rhs->sampler,
1015 rhs->srh = GNUNET_RPS_stream_request (rps_handle,
1016 collect_peers_info_cb,
1018 rhs->ready_cb = ready_cb;
1019 rhs->ready_cb_cls = cls;
1020 GNUNET_CONTAINER_DLL_insert (rps_handle->rhs_head,
1021 rps_handle->rhs_tail,
1029 * Seed rps service with peerIDs.
1031 * @param h handle to the rps service
1032 * @param n number of peers to seed
1033 * @param ids the ids of the peers seeded
1036 GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h,
1038 const struct GNUNET_PeerIdentity *ids)
1041 uint32_t num_peers_max;
1042 const struct GNUNET_PeerIdentity *tmp_peer_pointer;
1043 struct GNUNET_MQ_Envelope *ev;
1044 struct GNUNET_RPS_CS_SeedMessage *msg;
1046 LOG (GNUNET_ERROR_TYPE_DEBUG,
1047 "Client wants to seed %" PRIu32 " peers:\n",
1049 for (unsigned int i = 0; i < n; i++)
1050 LOG (GNUNET_ERROR_TYPE_DEBUG,
1053 GNUNET_i2s (&ids[i]));
1055 /* The actual size the message occupies */
1056 size_needed = sizeof(struct GNUNET_RPS_CS_SeedMessage)
1057 + n * sizeof(struct GNUNET_PeerIdentity);
1058 /* The number of peers that fits in one message together with
1059 * the respective header */
1060 num_peers_max = (GNUNET_MAX_MESSAGE_SIZE
1061 - sizeof(struct GNUNET_RPS_CS_SeedMessage))
1062 / sizeof(struct GNUNET_PeerIdentity);
1063 tmp_peer_pointer = ids;
1065 while (GNUNET_MAX_MESSAGE_SIZE < size_needed)
1067 ev = GNUNET_MQ_msg_extra (msg,
1068 num_peers_max * sizeof(struct
1069 GNUNET_PeerIdentity),
1070 GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
1071 msg->num_peers = htonl (num_peers_max);
1072 GNUNET_memcpy (&msg[1],
1074 num_peers_max * sizeof(struct GNUNET_PeerIdentity));
1075 GNUNET_MQ_send (h->mq,
1078 size_needed = sizeof(struct GNUNET_RPS_CS_SeedMessage)
1079 + n * sizeof(struct GNUNET_PeerIdentity);
1080 /* Set pointer to beginning of next block of num_peers_max peers */
1081 tmp_peer_pointer = &ids[num_peers_max];
1084 ev = GNUNET_MQ_msg_extra (msg,
1085 n * sizeof(struct GNUNET_PeerIdentity),
1086 GNUNET_MESSAGE_TYPE_RPS_CS_SEED);
1087 msg->num_peers = htonl (n);
1088 GNUNET_memcpy (&msg[1],
1090 n * sizeof(struct GNUNET_PeerIdentity));
1091 GNUNET_MQ_send (h->mq,
1096 #if ENABLE_MALICIOUS
1098 * Turn RPS service to act malicious.
1100 * @param h handle to the rps service
1101 * @param type which type of malicious peer to turn to.
1102 * 0 Don't act malicious at all
1103 * 1 Try to maximise representation
1104 * 2 Try to partition the network
1105 * (isolate one peer from the rest)
1106 * @param n number of @a ids
1107 * @param ids the ids of the malicious peers
1108 * if @type is 2 the last id is the id of the
1109 * peer to be isolated from the rest
1112 GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h,
1115 const struct GNUNET_PeerIdentity *peer_ids,
1116 const struct GNUNET_PeerIdentity *target_peer)
1119 uint32_t num_peers_max;
1120 const struct GNUNET_PeerIdentity *tmp_peer_pointer;
1121 struct GNUNET_MQ_Envelope *ev;
1122 struct GNUNET_RPS_CS_ActMaliciousMessage *msg;
1126 LOG (GNUNET_ERROR_TYPE_DEBUG,
1127 "Client turns malicious (type %" PRIu32 ") with %" PRIu32
1131 for (i = 0; i < num_peers; i++)
1132 LOG (GNUNET_ERROR_TYPE_DEBUG,
1135 GNUNET_i2s (&peer_ids[i]));
1137 /* The actual size the message would occupy */
1138 size_needed = sizeof(struct GNUNET_RPS_CS_SeedMessage)
1139 + num_peers * sizeof(struct GNUNET_PeerIdentity);
1140 /* The number of peers that fit in one message together with
1141 * the respective header */
1142 num_peers_max = (GNUNET_MAX_MESSAGE_SIZE
1143 - sizeof(struct GNUNET_RPS_CS_SeedMessage))
1144 / sizeof(struct GNUNET_PeerIdentity);
1145 tmp_peer_pointer = peer_ids;
1147 while (GNUNET_MAX_MESSAGE_SIZE < size_needed)
1149 LOG (GNUNET_ERROR_TYPE_DEBUG,
1150 "Too many peers to send at once, sending %" PRIu32
1151 " (all we can so far)\n",
1153 ev = GNUNET_MQ_msg_extra (msg,
1154 num_peers_max * sizeof(struct
1155 GNUNET_PeerIdentity),
1156 GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
1157 msg->type = htonl (type);
1158 msg->num_peers = htonl (num_peers_max);
1161 msg->attacked_peer = peer_ids[num_peers];
1162 GNUNET_memcpy (&msg[1],
1164 num_peers_max * sizeof(struct GNUNET_PeerIdentity));
1166 GNUNET_MQ_send (h->mq, ev);
1168 num_peers -= num_peers_max;
1169 size_needed = sizeof(struct GNUNET_RPS_CS_SeedMessage)
1170 + num_peers * sizeof(struct GNUNET_PeerIdentity);
1171 /* Set pointer to beginning of next block of num_peers_max peers */
1172 tmp_peer_pointer = &peer_ids[num_peers_max];
1175 ev = GNUNET_MQ_msg_extra (msg,
1176 num_peers * sizeof(struct GNUNET_PeerIdentity),
1177 GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS);
1178 msg->type = htonl (type);
1179 msg->num_peers = htonl (num_peers);
1182 msg->attacked_peer = *target_peer;
1183 GNUNET_memcpy (&msg[1],
1185 num_peers * sizeof(struct GNUNET_PeerIdentity));
1187 GNUNET_MQ_send (h->mq, ev);
1191 #endif /* ENABLE_MALICIOUS */
1195 * Cancle an issued request.
1197 * @param rh request handle of request to cancle
1200 GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh)
1202 struct GNUNET_RPS_Handle *h;
1205 GNUNET_assert (NULL != rh);
1206 GNUNET_assert (NULL != rh->srh);
1207 GNUNET_assert (h == rh->srh->rps_handle);
1208 GNUNET_RPS_stream_cancel (rh->srh);
1210 if (NULL == h->stream_requests_head)
1212 if (NULL != rh->sampler_rh)
1214 RPS_sampler_request_cancel (rh->sampler_rh);
1216 RPS_sampler_destroy (rh->sampler);
1218 GNUNET_CONTAINER_DLL_remove (h->rh_head,
1226 * Cancle an issued single info request.
1228 * @param rhs request handle of request to cancle
1231 GNUNET_RPS_request_single_info_cancel (
1232 struct GNUNET_RPS_Request_Handle_Single_Info *rhs)
1234 struct GNUNET_RPS_Handle *h;
1236 h = rhs->rps_handle;
1237 GNUNET_assert (NULL != rhs);
1238 GNUNET_assert (NULL != rhs->srh);
1239 GNUNET_assert (h == rhs->srh->rps_handle);
1240 GNUNET_RPS_stream_cancel (rhs->srh);
1242 if (NULL == h->stream_requests_head)
1244 if (NULL != rhs->sampler_rh)
1246 RPS_sampler_request_single_info_cancel (rhs->sampler_rh);
1248 RPS_sampler_destroy (rhs->sampler);
1249 rhs->sampler = NULL;
1250 GNUNET_CONTAINER_DLL_remove (h->rhs_head,
1258 * Disconnect from the rps service
1260 * @param h the handle to the rps service
1263 GNUNET_RPS_disconnect (struct GNUNET_RPS_Handle *h)
1265 if (NULL != h->stream_requests_head)
1267 struct GNUNET_RPS_StreamRequestHandle *srh_next;
1269 LOG (GNUNET_ERROR_TYPE_WARNING,
1270 "Still waiting for replies\n");
1271 for (struct GNUNET_RPS_StreamRequestHandle *srh_iter =
1272 h->stream_requests_head;
1274 srh_iter = srh_next)
1276 srh_next = srh_iter->next;
1277 GNUNET_RPS_stream_cancel (srh_iter);
1280 if (NULL != h->rh_head)
1282 LOG (GNUNET_ERROR_TYPE_WARNING,
1283 "Not all requests were cancelled!\n");
1284 for (struct GNUNET_RPS_Request_Handle *rh_iter = h->rh_head;
1286 rh_iter = h->rh_head)
1288 GNUNET_RPS_request_cancel (rh_iter);
1291 if (NULL != h->rhs_head)
1293 LOG (GNUNET_ERROR_TYPE_WARNING,
1294 "Not all requests were cancelled!\n");
1295 for (struct GNUNET_RPS_Request_Handle_Single_Info *rhs_iter = h->rhs_head;
1296 h->rhs_head != NULL;
1297 rhs_iter = h->rhs_head)
1299 GNUNET_RPS_request_single_info_cancel (rhs_iter);
1302 if (NULL != srh_callback_peers)
1304 GNUNET_free (srh_callback_peers);
1305 srh_callback_peers = NULL;
1307 if (NULL != h->view_update_cb)
1309 LOG (GNUNET_ERROR_TYPE_WARNING,
1310 "Still waiting for view updates\n");
1311 GNUNET_RPS_view_request_cancel (h);
1314 GNUNET_NSE_disconnect (h->nse);
1315 GNUNET_MQ_destroy (h->mq);
1320 /* end of rps_api.c */