2 This file is part of GNUnet.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file rps/gnunet-service-rps.c
23 * @brief rps service implementation
24 * @author Julius Bünger
27 #include "gnunet_util_lib.h"
28 #include "gnunet_cadet_service.h"
29 #include "gnunet_nse_service.h"
31 #include "rps-test_util.h"
33 #include "gnunet-service-rps_sampler.h"
38 #define LOG(kind, ...) GNUNET_log(kind, __VA_ARGS__)
40 // TODO modify @brief in every file
42 // TODO check for overflows
44 // TODO align message structs
46 // TODO connect to friends
48 // TODO store peers somewhere persistent
50 // TODO blacklist? (-> mal peer detection on top of brahms)
52 // TODO API request_cancel
54 // hist_size_init, hist_size_max
59 static const struct GNUNET_CONFIGURATION_Handle *cfg;
64 static struct GNUNET_PeerIdentity own_identity;
67 struct GNUNET_PeerIdentity *
68 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list, unsigned int size,
69 const struct GNUNET_PeerIdentity *ignore_list, unsigned int ignore_size);
72 /***********************************************************************
73 * Housekeeping with peers
74 ***********************************************************************/
77 * Struct used to store the context of a connected client.
82 * The message queue to communicate with the client.
84 struct GNUNET_MQ_Handle *mq;
88 * Used to keep track in what lists single peerIDs are.
92 PULL_REPLY_PENDING = 0x01,
93 IN_OTHER_GOSSIP_LIST = 0x02, // unneeded?
94 IN_OWN_SAMPLER_LIST = 0x04, // unneeded?
95 IN_OWN_GOSSIP_LIST = 0x08, // unneeded?
98 * We set this bit when we can be sure the other peer is/was live.
105 * Functions of this type can be used to be stored at a peer for later execution.
107 typedef void (* PeerOp) (void *cls, const struct GNUNET_PeerIdentity *peer);
110 * Outstanding operation on peer consisting of callback and closure
112 struct PeerOutstandingOp
127 * Struct used to keep track of other peer's status
129 * This is stored in a multipeermap.
134 * In own gossip/sampler list, in other's gossip/sampler list
139 * Message queue open to client
141 struct GNUNET_MQ_Handle *mq;
144 * Channel open to client.
146 struct GNUNET_CADET_Channel *send_channel;
149 * Channel open from client.
151 struct GNUNET_CADET_Channel *recv_channel; // unneeded?
154 * Array of outstanding operations on this peer.
156 struct PeerOutstandingOp *outstanding_ops;
159 * Number of outstanding operations.
161 unsigned int num_outstanding_ops;
162 //size_t num_outstanding_ops;
165 * Handle to the callback given to cadet_ntfy_tmt_rdy()
167 * To be canceled on shutdown.
169 struct GNUNET_CADET_TransmitHandle *is_live_task;
172 * Identity of the peer
174 struct GNUNET_PeerIdentity peer_id;
177 * This is pobably followed by 'statistical' data (when we first saw
178 * him, how did we get his ID, how many pushes (in a timeinterval),
183 /***********************************************************************
184 * /Housekeeping with peers
185 ***********************************************************************/
191 /***********************************************************************
193 ***********************************************************************/
196 * Sampler used for the Brahms protocol itself.
198 static struct RPS_Sampler *prot_sampler;
201 * Sampler used for the clients.
203 static struct RPS_Sampler *client_sampler;
206 * Set of all peers to keep track of them.
208 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
212 * The gossiped list of peers.
214 static struct GNUNET_PeerIdentity *gossip_list;
217 * Size of the gossiped list
219 //static unsigned int gossip_list_size;
220 static uint32_t gossip_list_size;
223 * Name to log view (gossip_list) to
225 static char *file_name_view_log;
229 * The size of sampler we need to be able to satisfy the client's need of
232 static unsigned int sampler_size_client_need;
235 * The size of sampler we need to be able to satisfy the Brahms protocol's
236 * need of random peers.
238 * This is directly taken as the #gossip_list_size on update of the
241 * This is one minimum size the sampler grows to.
243 static unsigned int sampler_size_est_need;
247 * Percentage of total peer number in the gossip list
248 * to send random PUSHes to
253 * Percentage of total peer number in the gossip list
254 * to send random PULLs to
259 * The percentage gamma of history updates.
260 * Simply 1 - alpha - beta
265 * Identifier for the main task that runs periodically.
267 static struct GNUNET_SCHEDULER_Task *do_round_task;
270 * Time inverval the do_round task runs in.
272 static struct GNUNET_TIME_Relative round_interval;
277 * List to store peers received through pushes temporary.
279 * TODO -> multipeermap
281 static struct GNUNET_PeerIdentity *push_list;
284 * Size of the push_list;
286 static unsigned int push_list_size;
287 //size_t push_list_size;
290 * List to store peers received through pulls temporary.
292 * TODO -> multipeermap
294 static struct GNUNET_PeerIdentity *pull_list;
297 * Size of the pull_list;
299 static unsigned int pull_list_size;
300 //size_t pull_list_size;
306 static struct GNUNET_NSE_Handle *nse;
311 static struct GNUNET_CADET_Handle *cadet_handle;
317 * Only needed in the beginning to check how many of the 64 deltas
320 static unsigned int req_counter;
323 * Time of the last request we received.
325 * Used to compute the expected request rate.
327 static struct GNUNET_TIME_Absolute last_request;
330 * Size of #request_deltas.
332 #define REQUEST_DELTAS_SIZE 64
333 static unsigned int request_deltas_size = REQUEST_DELTAS_SIZE;
336 * Last 64 deltas between requests
338 static struct GNUNET_TIME_Relative request_deltas[REQUEST_DELTAS_SIZE];
341 * The prediction of the rate of requests
343 static struct GNUNET_TIME_Relative request_rate;
347 * List with the peers we sent requests to.
349 struct GNUNET_PeerIdentity *pending_pull_reply_list;
352 * Size of #pending_pull_reply_list.
354 uint32_t pending_pull_reply_list_size;
358 * Number of history update tasks.
360 uint32_t num_hist_update_tasks;
364 * Closure used to pass the client and the id to the callback
365 * that replies to a client's request
370 * The identifier of the request
375 * The client handle to send the reply to
377 struct GNUNET_SERVER_Client *client;
381 #ifdef ENABLE_MALICIOUS
383 * Type of malicious peer
385 * 0 Don't act malicious at all - Default
386 * 1 Try to maximise representation
387 * 2 Try to partition the network
390 uint32_t mal_type = 0;
393 * Other malicious peers
395 static struct GNUNET_PeerIdentity *mal_peers = NULL;
398 * Hashmap of malicious peers used as set.
399 * Used to more efficiently check whether we know that peer.
401 static struct GNUNET_CONTAINER_MultiPeerMap *mal_peer_set = NULL;
404 * Number of other malicious peers
406 static uint32_t num_mal_peers = 0;
410 * If type is 2 This struct is used to store the attacked peers in a DLL
417 struct AttackedPeer *next;
418 struct AttackedPeer *prev;
423 struct GNUNET_PeerIdentity peer_id;
427 * If type is 2 this is the DLL of attacked peers
429 static struct AttackedPeer *att_peers_head = NULL;
430 static struct AttackedPeer *att_peers_tail = NULL;
433 * This index is used to point to an attacked peer to
434 * implement the round-robin-ish way to select attacked peers.
436 static struct AttackedPeer *att_peer_index = NULL;
439 * Hashmap of attacked peers used as set.
440 * Used to more efficiently check whether we know that peer.
442 static struct GNUNET_CONTAINER_MultiPeerMap *att_peer_set = NULL;
445 * Number of attacked peers
447 static uint32_t num_attacked_peers = 0;
451 * If type is 1 this is the attacked peer
453 static struct GNUNET_PeerIdentity attacked_peer;
456 * The limit of PUSHes we can send in one round.
457 * This is an assumption of the Brahms protocol and either implemented
460 * assumend to be the bandwidth limitation.
462 static uint32_t push_limit = 10000;
463 #endif /* ENABLE_MALICIOUS */
466 /***********************************************************************
468 ***********************************************************************/
475 /***********************************************************************
477 ***********************************************************************/
480 * Set a peer flag of given peer context.
482 #define set_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags |= mask)
485 * Get peer flag of given peer context.
487 #define get_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags & mask ? GNUNET_YES : GNUNET_NO)
490 * Unset flag of given peer context.
492 #define unset_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags &= (~mask))
495 * Clean the send channel of a peer
498 peer_clean (const struct GNUNET_PeerIdentity *peer);
502 * Check if peer is already in peer array.
505 in_arr (const struct GNUNET_PeerIdentity *array,
506 unsigned int arr_size,
507 const struct GNUNET_PeerIdentity *peer)
509 GNUNET_assert (NULL != peer);
514 GNUNET_assert (NULL != array);
518 for (i = 0; i < arr_size ; i++)
519 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&array[i], peer))
526 * Print peerlist to log.
529 print_peer_list (struct GNUNET_PeerIdentity *list, unsigned int len)
533 LOG (GNUNET_ERROR_TYPE_DEBUG,
534 "Printing peer list of length %u at %p:\n",
537 for (i = 0 ; i < len ; i++)
539 LOG (GNUNET_ERROR_TYPE_DEBUG,
541 i, GNUNET_i2s (&list[i]));
547 * Remove peer from list.
550 rem_from_list (struct GNUNET_PeerIdentity **peer_list,
551 unsigned int *list_size,
552 const struct GNUNET_PeerIdentity *peer)
555 struct GNUNET_PeerIdentity *tmp;
559 LOG (GNUNET_ERROR_TYPE_DEBUG,
560 "Removing peer %s from list at %p\n",
564 for ( i = 0 ; i < *list_size ; i++ )
566 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&tmp[i], peer))
568 if (i < *list_size -1)
569 { /* Not at the last entry -- shift peers left */
570 memcpy (&tmp[i], &tmp[i +1],
571 ((*list_size) - i -1) * sizeof (struct GNUNET_PeerIdentity));
573 /* Remove last entry (should be now useless PeerID) */
574 GNUNET_array_grow (tmp, *list_size, (*list_size) -1);
581 * Get random peer from the given list but don't return one from the @a ignore_list.
583 struct GNUNET_PeerIdentity *
584 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list,
586 const struct GNUNET_PeerIdentity *ignore_list,
587 uint32_t ignore_size)
591 struct GNUNET_PeerIdentity *tmp_peer_list;
592 struct GNUNET_PeerIdentity *peer;
594 GNUNET_assert (NULL != peer_list);
599 tmp_peer_list = NULL;
600 GNUNET_array_grow (tmp_peer_list, tmp_size, list_size);
601 memcpy (tmp_peer_list,
603 list_size * sizeof (struct GNUNET_PeerIdentity));
604 peer = GNUNET_new (struct GNUNET_PeerIdentity);
607 * Choose the r_index of the peer we want to return
608 * at random from the interval of the gossip list
610 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
612 *peer = tmp_peer_list[r_index];
614 while (in_arr (ignore_list, ignore_size, peer))
616 rem_from_list (&tmp_peer_list, &tmp_size, peer);
618 print_peer_list (tmp_peer_list, tmp_size);
627 * Choose the r_index of the peer we want to return
628 * at random from the interval of the gossip list
630 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
632 *peer = tmp_peer_list[r_index];
636 GNUNET_array_grow (tmp_peer_list, tmp_size, 0);
643 * Get the context of a peer. If not existing, create.
646 get_peer_ctx (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
647 const struct GNUNET_PeerIdentity *peer)
649 struct PeerContext *ctx;
651 if ( GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
653 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
657 ctx = GNUNET_new (struct PeerContext);
660 ctx->send_channel = NULL;
661 ctx->recv_channel = NULL;
662 ctx->outstanding_ops = NULL;
663 ctx->num_outstanding_ops = 0;
664 ctx->is_live_task = NULL;
665 ctx->peer_id = *peer;
666 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
667 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
674 * Put random peer from sampler into the gossip list as history update.
677 hist_update (void *cls, struct GNUNET_PeerIdentity *ids, uint32_t num_peers)
679 GNUNET_assert (1 == num_peers);
681 if (gossip_list_size < sampler_size_est_need)
683 GNUNET_array_append (gossip_list, gossip_list_size, *ids);
684 to_file (file_name_view_log,
686 GNUNET_i2s_full (ids));
689 if (0 < num_hist_update_tasks)
690 num_hist_update_tasks--;
695 * Set the peer flag to living and call the outstanding operations on this peer.
698 peer_is_live (struct PeerContext *peer_ctx)
700 struct GNUNET_PeerIdentity *peer;
702 /* Cancle is_live_task if still scheduled */
703 if (NULL != peer_ctx->is_live_task)
705 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
706 peer_ctx->is_live_task = NULL;
709 peer = &peer_ctx->peer_id;
710 set_peer_flag (peer_ctx, VALID);
712 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer %s is live\n", GNUNET_i2s (peer));
714 if (0 < peer_ctx->num_outstanding_ops)
715 { /* Call outstanding operations */
718 for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
719 peer_ctx->outstanding_ops[i].op (peer_ctx->outstanding_ops[i].op_cls, peer);
720 GNUNET_array_grow (peer_ctx->outstanding_ops, peer_ctx->num_outstanding_ops, 0);
728 * Callback that is called when a channel was effectively established.
729 * This is given to ntfy_tmt_rdy and called when the channel was
730 * successfully established.
733 cadet_ntfy_tmt_rdy_cb (void *cls, size_t size, void *buf)
735 struct PeerContext *peer_ctx = (struct PeerContext *) cls;
737 peer_ctx->is_live_task = NULL;
738 LOG (GNUNET_ERROR_TYPE_DEBUG,
739 "Set ->is_live_task = NULL for peer %s\n",
740 GNUNET_i2s (&peer_ctx->peer_id));
745 peer_is_live (peer_ctx);
749 LOG (GNUNET_ERROR_TYPE_WARNING,
750 "Problems establishing a connection to peer %s in order to check liveliness\n",
751 GNUNET_i2s (&peer_ctx->peer_id));
752 // TODO reschedule? cleanup?
755 //if (NULL != peer_ctx->is_live_task)
757 // LOG (GNUNET_ERROR_TYPE_DEBUG,
758 // "Trying to cancle is_live_task for peer %s\n",
759 // GNUNET_i2s (&peer_ctx->peer_id));
760 // GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
761 // peer_ctx->is_live_task = NULL;
769 * Get the channel of a peer. If not existing, create.
771 struct GNUNET_CADET_Channel *
772 get_channel (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
773 const struct GNUNET_PeerIdentity *peer)
775 struct PeerContext *peer_ctx;
777 peer_ctx = get_peer_ctx (peer_map, peer);
779 if (NULL == peer_ctx->send_channel)
781 LOG (GNUNET_ERROR_TYPE_DEBUG,
782 "Trying to establish channel to peer %s\n",
785 peer_ctx->send_channel =
786 GNUNET_CADET_channel_create (cadet_handle,
789 GNUNET_RPS_CADET_PORT,
790 GNUNET_CADET_OPTION_RELIABLE);
792 // do I have to explicitly put it in the peer_map?
793 (void) GNUNET_CONTAINER_multipeermap_put
797 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
799 return peer_ctx->send_channel;
804 * Get the message queue of a specific peer.
806 * If we already have a message queue open to this client,
807 * simply return it, otherways create one.
809 struct GNUNET_MQ_Handle *
810 get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
811 const struct GNUNET_PeerIdentity *peer_id)
813 struct PeerContext *peer_ctx;
815 peer_ctx = get_peer_ctx (peer_map, peer_id);
817 GNUNET_assert (NULL == peer_ctx->is_live_task);
819 if (NULL == peer_ctx->mq)
821 (void) get_channel (peer_map, peer_id);
822 peer_ctx->mq = GNUNET_CADET_mq_create (peer_ctx->send_channel);
823 //do I have to explicitly put it in the peer_map?
824 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer_id, peer_ctx,
825 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
832 * Issue check whether peer is live
834 * @param peer_ctx the context of the peer
837 check_peer_live (struct PeerContext *peer_ctx)
839 (void) get_channel (peer_map, &peer_ctx->peer_id);
840 LOG (GNUNET_ERROR_TYPE_DEBUG,
841 "Get informed about peer %s getting live\n",
842 GNUNET_i2s (&peer_ctx->peer_id));
843 if (NULL == peer_ctx->is_live_task)
845 peer_ctx->is_live_task =
846 GNUNET_CADET_notify_transmit_ready (peer_ctx->send_channel,
848 GNUNET_TIME_UNIT_FOREVER_REL,
849 sizeof (struct GNUNET_MessageHeader),
850 cadet_ntfy_tmt_rdy_cb,
855 LOG (GNUNET_ERROR_TYPE_DEBUG,
856 "Already waiting for notification\n");
862 * Sum all time relatives of an array.
864 struct GNUNET_TIME_Relative
865 T_relative_sum (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
867 struct GNUNET_TIME_Relative sum;
870 sum = GNUNET_TIME_UNIT_ZERO;
871 for ( i = 0 ; i < arr_size ; i++ )
873 sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
880 * Compute the average of given time relatives.
882 struct GNUNET_TIME_Relative
883 T_relative_avg (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
885 return GNUNET_TIME_relative_divide (T_relative_sum (rel_array, arr_size), arr_size);
890 * Insert PeerID in #pull_list
892 * Called once we know a peer is live.
895 insert_in_pull_list (void *cls, const struct GNUNET_PeerIdentity *peer)
897 if (GNUNET_NO == in_arr (pull_list, pull_list_size, peer))
898 GNUNET_array_append (pull_list, pull_list_size, *peer);
904 * Check whether #insert_in_pull_list was already scheduled
907 insert_in_pull_list_scheduled (const struct PeerContext *peer_ctx)
911 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
912 if (insert_in_pull_list == peer_ctx->outstanding_ops[i].op)
919 * Insert PeerID in #gossip_list
921 * Called once we know a peer is live.
924 insert_in_gossip_list (void *cls, const struct GNUNET_PeerIdentity *peer)
926 if (GNUNET_NO == in_arr (gossip_list, gossip_list_size, peer))
928 GNUNET_array_append (gossip_list, gossip_list_size, *peer);
929 to_file (file_name_view_log,
930 "+%s\t(ins in gossip list)",
931 GNUNET_i2s_full (peer));
934 (void) get_channel (peer_map, peer);
938 * Check whether #insert_in_gossip_list was already scheduled
941 insert_in_gossip_list_scheduled (const struct PeerContext *peer_ctx)
945 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
946 if (insert_in_gossip_list == peer_ctx->outstanding_ops[i].op)
953 * Update sampler with given PeerID.
956 insert_in_sampler (void *cls, const struct GNUNET_PeerIdentity *peer)
958 LOG (GNUNET_ERROR_TYPE_DEBUG,
959 "Updating samplers with peer %s from insert_in_sampler()\n",
961 RPS_sampler_update (prot_sampler, peer);
962 RPS_sampler_update (client_sampler, peer);
967 * Check whether #insert_in_sampler was already scheduled
970 insert_in_sampler_scheduled (const struct PeerContext *peer_ctx)
974 for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
975 if (insert_in_sampler== peer_ctx->outstanding_ops[i].op)
982 * Wrapper around #RPS_sampler_resize()
984 * If we do not have enough sampler elements, double current sampler size
985 * If we have more than enough sampler elements, halv current sampler size
988 resize_wrapper (struct RPS_Sampler *sampler, uint32_t new_size)
990 unsigned int sampler_size;
993 // TODO respect the min, max
994 sampler_size = RPS_sampler_get_size (sampler);
995 if (sampler_size > new_size * 4)
997 RPS_sampler_resize (sampler, sampler_size / 2);
999 else if (sampler_size < new_size)
1001 RPS_sampler_resize (sampler, sampler_size * 2);
1003 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
1008 * Wrapper around #RPS_sampler_resize() resizing the client sampler
1011 client_resize_wrapper ()
1013 uint32_t bigger_size;
1014 unsigned int sampler_size;
1018 sampler_size = RPS_sampler_get_size (client_sampler);
1020 if (sampler_size_est_need > sampler_size_client_need)
1021 bigger_size = sampler_size_est_need;
1023 bigger_size = sampler_size_client_need;
1025 // TODO respect the min, max
1026 resize_wrapper (client_sampler, bigger_size);
1027 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
1032 * Estimate request rate
1034 * Called every time we receive a request from the client.
1039 struct GNUNET_TIME_Relative max_round_duration;
1041 if (request_deltas_size > req_counter)
1043 if ( 1 < req_counter)
1045 /* Shift last request deltas to the right */
1046 memcpy (&request_deltas[1],
1048 (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
1050 /* Add current delta to beginning */
1052 GNUNET_TIME_absolute_get_difference (last_request,
1053 GNUNET_TIME_absolute_get ());
1054 request_rate = T_relative_avg (request_deltas, req_counter);
1056 /* Compute the duration a round will maximally take */
1057 max_round_duration =
1058 GNUNET_TIME_relative_add (round_interval,
1059 GNUNET_TIME_relative_divide (round_interval, 2));
1061 /* Set the estimated size the sampler has to have to
1062 * satisfy the current client request rate */
1063 sampler_size_client_need =
1064 max_round_duration.rel_value_us / request_rate.rel_value_us;
1066 /* Resize the sampler */
1067 client_resize_wrapper ();
1069 last_request = GNUNET_TIME_absolute_get ();
1074 * Add all peers in @a peer_array to @a peer_map used as set.
1076 * @param peer_array array containing the peers
1077 * @param num_peers number of peers in @peer_array
1078 * @param peer_map the peermap to use as set
1081 add_peer_array_to_set (const struct GNUNET_PeerIdentity *peer_array,
1082 unsigned int num_peers,
1083 struct GNUNET_CONTAINER_MultiPeerMap *peer_map)
1086 if (NULL == peer_map)
1087 peer_map = GNUNET_CONTAINER_multipeermap_create (num_peers + 1,
1089 for (i = 0 ; i < num_peers ; i++)
1091 GNUNET_CONTAINER_multipeermap_put (peer_map,
1094 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1100 * Send a PULL REPLY to @a peer_id
1102 * @param peer_id the peer to send the reply to.
1103 * @param peer_ids the peers to send to @a peer_id
1104 * @param num_peer_ids the number of peers to send to @a peer_id
1107 send_pull_reply (const struct GNUNET_PeerIdentity *peer_id,
1108 const struct GNUNET_PeerIdentity *peer_ids,
1109 unsigned int num_peer_ids)
1112 struct GNUNET_MQ_Handle *mq;
1113 struct GNUNET_MQ_Envelope *ev;
1114 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
1116 /* Compute actual size */
1117 send_size = sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) +
1118 num_peer_ids * sizeof (struct GNUNET_PeerIdentity);
1120 if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size)
1121 /* Compute number of peers to send
1122 * If too long, simply truncate */
1123 // TODO select random ones via permutation
1124 // or even better: do good protocol design
1126 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE -
1127 sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1128 sizeof (struct GNUNET_PeerIdentity);
1130 send_size = num_peer_ids;
1132 LOG (GNUNET_ERROR_TYPE_DEBUG,
1133 "PULL REQUEST from peer %s received, going to send %u peers\n",
1134 GNUNET_i2s (peer_id), send_size);
1136 mq = get_mq (peer_map, peer_id);
1138 ev = GNUNET_MQ_msg_extra (out_msg,
1139 send_size * sizeof (struct GNUNET_PeerIdentity),
1140 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
1141 out_msg->num_peers = htonl (send_size);
1142 memcpy (&out_msg[1], peer_ids,
1143 send_size * sizeof (struct GNUNET_PeerIdentity));
1145 GNUNET_MQ_send (mq, ev);
1150 * This function is called on new peer_ids from 'external' sources
1151 * (client seed, cadet get_peers(), ...)
1153 * @param peer_id the new peer_id
1156 new_peer_id (const struct GNUNET_PeerIdentity *peer_id)
1158 struct PeerOutstandingOp out_op;
1159 struct PeerContext *peer_ctx;
1162 && 0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, peer_id))
1164 LOG (GNUNET_ERROR_TYPE_DEBUG,
1165 "Got peer_id %s (at %p, gossip_list_size: %u)\n",
1166 GNUNET_i2s (peer_id),
1170 peer_ctx = get_peer_ctx (peer_map, peer_id);
1171 if (GNUNET_YES != get_peer_flag (peer_ctx, VALID))
1173 if (GNUNET_NO == insert_in_sampler_scheduled (peer_ctx))
1175 out_op.op = insert_in_sampler;
1176 out_op.op_cls = NULL;
1177 GNUNET_array_append (peer_ctx->outstanding_ops,
1178 peer_ctx->num_outstanding_ops,
1182 if (GNUNET_NO == insert_in_gossip_list_scheduled (peer_ctx))
1184 out_op.op = insert_in_gossip_list;
1185 out_op.op_cls = NULL;
1186 GNUNET_array_append (peer_ctx->outstanding_ops,
1187 peer_ctx->num_outstanding_ops,
1191 /* Trigger livelyness test on peer */
1192 check_peer_live (peer_ctx);
1196 // send push/pull to each of those peers?
1201 /***********************************************************************
1203 ***********************************************************************/
1210 * Function called by NSE.
1212 * Updates sizes of sampler list and gossip list and adapt those lists
1216 nse_callback (void *cls, struct GNUNET_TIME_Absolute timestamp,
1217 double logestimate, double std_dev)
1220 //double scale; // TODO this might go gloabal/config
1222 LOG (GNUNET_ERROR_TYPE_DEBUG,
1223 "Received a ns estimate - logest: %f, std_dev: %f (old_size: %u)\n",
1224 logestimate, std_dev, RPS_sampler_get_size (prot_sampler));
1226 estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
1227 // GNUNET_NSE_log_estimate_to_n (logestimate);
1228 estimate = pow (estimate, 1.0 / 3);
1229 // TODO add if std_dev is a number
1230 // estimate += (std_dev * scale);
1231 if (2 < ceil (estimate))
1233 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
1234 sampler_size_est_need = estimate;
1236 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
1238 /* If the NSE has changed adapt the lists accordingly */
1239 resize_wrapper (prot_sampler, sampler_size_est_need);
1240 client_resize_wrapper ();
1245 * Callback called once the requested PeerIDs are ready.
1247 * Sends those to the requesting client.
1249 void client_respond (void *cls,
1250 struct GNUNET_PeerIdentity *peer_ids, uint32_t num_peers)
1252 struct GNUNET_MQ_Envelope *ev;
1253 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
1254 struct ReplyCls *reply_cls = (struct ReplyCls *) cls;
1255 uint32_t size_needed;
1256 struct ClientContext *cli_ctx;
1258 LOG (GNUNET_ERROR_TYPE_DEBUG,
1259 "sampler returned %" PRIu32 " peers\n",
1262 size_needed = sizeof (struct GNUNET_RPS_CS_ReplyMessage) +
1263 num_peers * sizeof (struct GNUNET_PeerIdentity);
1265 GNUNET_assert (GNUNET_SERVER_MAX_MESSAGE_SIZE >= size_needed);
1267 ev = GNUNET_MQ_msg_extra (out_msg,
1268 num_peers * sizeof (struct GNUNET_PeerIdentity),
1269 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
1270 out_msg->num_peers = htonl (num_peers);
1271 out_msg->id = htonl (reply_cls->id);
1273 memcpy (&out_msg[1],
1275 num_peers * sizeof (struct GNUNET_PeerIdentity));
1276 GNUNET_free (peer_ids);
1278 cli_ctx = GNUNET_SERVER_client_get_user_context (reply_cls->client, struct ClientContext);
1279 if (NULL == cli_ctx) {
1280 cli_ctx = GNUNET_new (struct ClientContext);
1281 cli_ctx->mq = GNUNET_MQ_queue_for_server_client (reply_cls->client);
1282 GNUNET_SERVER_client_set_user_context (reply_cls->client, cli_ctx);
1285 GNUNET_free (reply_cls);
1287 GNUNET_MQ_send (cli_ctx->mq, ev);
1292 * Handle RPS request from the client.
1294 * @param cls closure
1295 * @param client identification of the client
1296 * @param message the actual message
1299 handle_client_request (void *cls,
1300 struct GNUNET_SERVER_Client *client,
1301 const struct GNUNET_MessageHeader *message)
1303 struct GNUNET_RPS_CS_RequestMessage *msg;
1305 uint32_t size_needed;
1306 struct ReplyCls *reply_cls;
1309 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
1311 num_peers = ntohl (msg->num_peers);
1312 size_needed = sizeof (struct GNUNET_RPS_CS_RequestMessage) +
1313 num_peers * sizeof (struct GNUNET_PeerIdentity);
1315 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
1317 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1321 for (i = 0 ; i < num_peers ; i++)
1324 LOG (GNUNET_ERROR_TYPE_DEBUG,
1325 "Client requested %" PRIu32 " random peer(s).\n",
1328 reply_cls = GNUNET_new (struct ReplyCls);
1329 reply_cls->id = ntohl (msg->id);
1330 reply_cls->client = client;
1332 RPS_sampler_get_n_rand_peers (client_sampler,
1337 GNUNET_SERVER_receive_done (client,
1343 * Handle seed from the client.
1345 * @param cls closure
1346 * @param client identification of the client
1347 * @param message the actual message
1350 handle_client_seed (void *cls,
1351 struct GNUNET_SERVER_Client *client,
1352 const struct GNUNET_MessageHeader *message)
1354 struct GNUNET_RPS_CS_SeedMessage *in_msg;
1355 struct GNUNET_PeerIdentity *peers;
1359 if (sizeof (struct GNUNET_RPS_CS_SeedMessage) > ntohs (message->size))
1361 GNUNET_break_op (0);
1362 GNUNET_SERVER_receive_done (client,
1366 in_msg = (struct GNUNET_RPS_CS_SeedMessage *) message;
1367 num_peers = ntohl (in_msg->num_peers);
1368 peers = (struct GNUNET_PeerIdentity *) &in_msg[1];
1369 //peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1370 //memcpy (peers, &in_msg[1], num_peers * sizeof (struct GNUNET_PeerIdentity));
1372 if ((ntohs (message->size) - sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
1373 sizeof (struct GNUNET_PeerIdentity) != num_peers)
1375 GNUNET_break_op (0);
1376 GNUNET_SERVER_receive_done (client,
1380 LOG (GNUNET_ERROR_TYPE_DEBUG,
1381 "Client seeded peers:\n");
1382 print_peer_list (peers, num_peers);
1384 for (i = 0 ; i < num_peers ; i++)
1386 LOG (GNUNET_ERROR_TYPE_DEBUG,
1387 "Updating samplers with seed %" PRIu32 ": %s\n",
1389 GNUNET_i2s (&peers[i]));
1391 new_peer_id (&peers[i]);
1393 //RPS_sampler_update (prot_sampler, &peers[i]);
1394 //RPS_sampler_update (client_sampler, &peers[i]);
1397 ////GNUNET_free (peers);
1399 GNUNET_SERVER_receive_done (client,
1405 * Handle a PUSH message from another peer.
1407 * Check the proof of work and store the PeerID
1408 * in the temporary list for pushed PeerIDs.
1410 * @param cls Closure
1411 * @param channel The channel the PUSH was received over
1412 * @param channel_ctx The context associated with this channel
1413 * @param msg The message header
1416 handle_peer_push (void *cls,
1417 struct GNUNET_CADET_Channel *channel,
1419 const struct GNUNET_MessageHeader *msg)
1421 const struct GNUNET_PeerIdentity *peer;
1423 // (check the proof of work)
1425 peer = (const struct GNUNET_PeerIdentity *)
1426 GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
1427 // FIXME wait for cadet to change this function
1429 LOG (GNUNET_ERROR_TYPE_DEBUG, "PUSH received (%s)\n", GNUNET_i2s (peer));
1431 #ifdef ENABLE_MALICIOUS
1432 struct AttackedPeer *tmp_att_peer;
1434 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1435 memcpy (&tmp_att_peer->peer_id, peer, sizeof (struct GNUNET_PeerIdentity));
1438 { /* Try to maximise representation */
1439 if (NULL == att_peer_set)
1440 att_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
1441 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1444 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1447 add_peer_array_to_set (peer, 1, att_peer_set);
1453 else if (2 == mal_type)
1454 { /* We attack one single well-known peer - simply ignore */
1458 #endif /* ENABLE_MALICIOUS */
1460 /* Add the sending peer to the push_list */
1461 if (GNUNET_NO == in_arr (push_list, push_list_size, peer))
1462 GNUNET_array_append (push_list, push_list_size, *peer);
1469 * Handle PULL REQUEST request message from another peer.
1471 * Reply with the gossip list of PeerIDs.
1473 * @param cls Closure
1474 * @param channel The channel the PUSH was received over
1475 * @param channel_ctx The context associated with this channel
1476 * @param msg The message header
1479 handle_peer_pull_request (void *cls,
1480 struct GNUNET_CADET_Channel *channel,
1482 const struct GNUNET_MessageHeader *msg)
1484 struct GNUNET_PeerIdentity *peer;
1486 peer = (struct GNUNET_PeerIdentity *)
1487 GNUNET_CADET_channel_get_info (channel,
1488 GNUNET_CADET_OPTION_PEER);
1489 // FIXME wait for cadet to change this function
1491 #ifdef ENABLE_MALICIOUS
1494 { /* Try to maximise representation */
1495 send_pull_reply (peer, mal_peers, num_mal_peers);
1499 else if (2 == mal_type)
1500 { /* Try to partition network */
1501 if (GNUNET_YES == GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
1503 send_pull_reply (peer, mal_peers, num_mal_peers);
1507 #endif /* ENABLE_MALICIOUS */
1509 send_pull_reply (peer, gossip_list, gossip_list_size);
1516 * Handle PULL REPLY message from another peer.
1518 * Check whether we sent a corresponding request and
1519 * whether this reply is the first one.
1521 * @param cls Closure
1522 * @param channel The channel the PUSH was received over
1523 * @param channel_ctx The context associated with this channel
1524 * @param msg The message header
1527 handle_peer_pull_reply (void *cls,
1528 struct GNUNET_CADET_Channel *channel,
1530 const struct GNUNET_MessageHeader *msg)
1532 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
1533 struct GNUNET_PeerIdentity *peers;
1534 struct PeerContext *peer_ctx;
1535 struct GNUNET_PeerIdentity *sender;
1536 struct PeerContext *sender_ctx;
1537 struct PeerOutstandingOp out_op;
1539 #ifdef ENABLE_MALICIOUS
1540 struct AttackedPeer *tmp_att_peer;
1541 #endif /* ENABLE_MALICIOUS */
1543 /* Check for protocol violation */
1544 if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->size))
1546 GNUNET_break_op (0);
1547 return GNUNET_SYSERR;
1550 in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
1551 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1552 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1554 LOG (GNUNET_ERROR_TYPE_ERROR,
1555 "message says it sends %" PRIu64 " peers, have space for %i peers\n",
1556 ntohl (in_msg->num_peers),
1557 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1558 sizeof (struct GNUNET_PeerIdentity));
1559 GNUNET_break_op (0);
1560 return GNUNET_SYSERR;
1563 sender = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
1564 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
1565 // Guess simply casting isn't the nicest way...
1566 // FIXME wait for cadet to change this function
1567 sender_ctx = get_peer_ctx (peer_map, sender);
1569 if (GNUNET_YES == get_peer_flag (sender_ctx, PULL_REPLY_PENDING))
1571 GNUNET_break_op (0);
1576 #ifdef ENABLE_MALICIOUS
1577 // We shouldn't even receive pull replies as we're not sending
1580 #endif /* ENABLE_MALICIOUS */
1582 /* Do actual logic */
1583 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1585 LOG (GNUNET_ERROR_TYPE_DEBUG,
1586 "PULL REPLY received, got following peers:\n");
1588 for (i = 0 ; i < ntohl (in_msg->num_peers) ; i++)
1590 LOG (GNUNET_ERROR_TYPE_DEBUG,
1593 GNUNET_i2s (&peers[i]));
1595 #ifdef ENABLE_MALICIOUS
1598 { /* Add attacked peer to local list */
1599 // TODO check if we sent a request and this was the first reply
1600 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1602 && GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mal_peer_set,
1604 && GNUNET_NO == GNUNET_CRYPTO_cmp_peer_identity (&peers[i],
1607 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1608 tmp_att_peer->peer_id = peers[i];
1609 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1612 add_peer_array_to_set (&peers[i], 1, att_peer_set);
1616 #endif /* ENABLE_MALICIOUS */
1617 peer_ctx = get_peer_ctx (peer_map, &peers[i]);
1618 if (GNUNET_YES == get_peer_flag (peer_ctx, VALID)
1619 || NULL != peer_ctx->send_channel
1620 || NULL != peer_ctx->recv_channel)
1622 if (GNUNET_NO == in_arr (pull_list, pull_list_size, &peers[i])
1623 && 0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peers[i]))
1624 GNUNET_array_append (pull_list, pull_list_size, peers[i]);
1626 else if (GNUNET_NO == insert_in_pull_list_scheduled (peer_ctx))
1628 out_op.op = insert_in_pull_list;
1629 out_op.op_cls = NULL;
1630 GNUNET_array_append (peer_ctx->outstanding_ops,
1631 peer_ctx->num_outstanding_ops,
1633 check_peer_live (peer_ctx);
1637 unset_peer_flag (sender_ctx, PULL_REPLY_PENDING);
1638 rem_from_list (&pending_pull_reply_list, &pending_pull_reply_list_size, sender);
1645 * Compute a random delay.
1646 * A uniformly distributed value between mean + spread and mean - spread.
1648 * For example for mean 4 min and spread 2 the minimum is (4 min - (1/2 * 4 min))
1649 * It would return a random value between 2 and 6 min.
1651 * @param mean the mean
1652 * @param spread the inverse amount of deviation from the mean
1654 static struct GNUNET_TIME_Relative
1655 compute_rand_delay (struct GNUNET_TIME_Relative mean, unsigned int spread)
1657 struct GNUNET_TIME_Relative half_interval;
1658 struct GNUNET_TIME_Relative ret;
1659 unsigned int rand_delay;
1660 unsigned int max_rand_delay;
1664 LOG (GNUNET_ERROR_TYPE_WARNING,
1665 "Not accepting spread of 0\n");
1669 /* Compute random time value between spread * mean and spread * mean */
1670 half_interval = GNUNET_TIME_relative_divide (mean, spread);
1672 max_rand_delay = GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us / mean.rel_value_us * (2/spread);
1674 * Compute random value between (0 and 1) * round_interval
1675 * via multiplying round_interval with a 'fraction' (0 to value)/value
1677 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_rand_delay);
1678 ret = GNUNET_TIME_relative_multiply (mean, rand_delay);
1679 ret = GNUNET_TIME_relative_divide (ret, max_rand_delay);
1680 ret = GNUNET_TIME_relative_add (ret, half_interval);
1682 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ret.rel_value_us)
1683 LOG (GNUNET_ERROR_TYPE_WARNING,
1684 "Returning FOREVER_REL\n");
1691 * Send single pull request
1693 * @param peer_id the peer to send the pull request to.
1696 send_pull_request (struct GNUNET_PeerIdentity *peer_id)
1698 struct GNUNET_MQ_Envelope *ev;
1699 struct GNUNET_MQ_Handle *mq;
1701 LOG (GNUNET_ERROR_TYPE_DEBUG,
1702 "Sending PULL request to peer %s of gossiped list.\n",
1703 GNUNET_i2s (peer_id));
1705 GNUNET_array_append (pending_pull_reply_list, pending_pull_reply_list_size, *peer_id);
1707 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
1708 mq = get_mq (peer_map, peer_id);
1709 GNUNET_MQ_send (mq, ev);
1716 * @param peer_id the peer to send the push to.
1719 send_push (struct GNUNET_PeerIdentity *peer_id)
1721 struct GNUNET_MQ_Envelope *ev;
1722 struct GNUNET_MQ_Handle *mq;
1724 LOG (GNUNET_ERROR_TYPE_DEBUG,
1725 "Sending PUSH to peer %s of gossiped list.\n",
1726 GNUNET_i2s (peer_id));
1728 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
1729 mq = get_mq (peer_map, peer_id);
1730 GNUNET_MQ_send (mq, ev);
1735 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1738 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1741 #ifdef ENABLE_MALICIOUS
1743 * Turn RPS service to act malicious.
1745 * @param cls Closure
1746 * @param channel The channel the PUSH was received over
1747 * @param channel_ctx The context associated with this channel
1748 * @param msg The message header
1751 handle_client_act_malicious (void *cls,
1752 struct GNUNET_SERVER_Client *client,
1753 const struct GNUNET_MessageHeader *msg)
1755 struct GNUNET_RPS_CS_ActMaliciousMessage *in_msg;
1756 struct GNUNET_PeerIdentity *peers;
1757 uint32_t num_mal_peers_sent;
1758 uint32_t num_mal_peers_old;
1760 /* Check for protocol violation */
1761 if (sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage) > ntohs (msg->size))
1763 GNUNET_break_op (0);
1766 in_msg = (struct GNUNET_RPS_CS_ActMaliciousMessage *) msg;
1767 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
1768 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1770 LOG (GNUNET_ERROR_TYPE_ERROR,
1771 "message says it sends %" PRIu64 " peers, have space for %i peers\n",
1772 ntohl (in_msg->num_peers),
1773 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
1774 sizeof (struct GNUNET_PeerIdentity));
1775 GNUNET_break_op (0);
1779 /* Do actual logic */
1780 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1781 mal_type = ntohl (in_msg->type);
1783 LOG (GNUNET_ERROR_TYPE_DEBUG,
1784 "Now acting malicious type %" PRIu32 "\n",
1788 { /* Try to maximise representation */
1789 /* Add other malicious peers to those we already know */
1791 num_mal_peers_sent = ntohl (in_msg->num_peers);
1792 num_mal_peers_old = num_mal_peers;
1793 GNUNET_array_grow (mal_peers,
1795 num_mal_peers + num_mal_peers_sent);
1796 memcpy (&mal_peers[num_mal_peers_old],
1798 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1800 /* Add all mal peers to mal_peer_set */
1801 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1805 /* Substitute do_round () with do_mal_round () */
1806 GNUNET_SCHEDULER_cancel (do_round_task);
1807 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1810 else if (2 == mal_type
1812 { /* Try to partition the network */
1813 /* Add other malicious peers to those we already know */
1814 num_mal_peers_sent = ntohl (in_msg->num_peers) - 1;
1815 num_mal_peers_old = num_mal_peers;
1816 GNUNET_array_grow (mal_peers,
1818 num_mal_peers + num_mal_peers_sent);
1819 memcpy (&mal_peers[num_mal_peers_old],
1821 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1823 /* Add all mal peers to mal_peer_set */
1824 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1828 /* Store the one attacked peer */
1829 memcpy (&attacked_peer,
1830 &in_msg->attacked_peer,
1831 sizeof (struct GNUNET_PeerIdentity));
1833 LOG (GNUNET_ERROR_TYPE_DEBUG,
1834 "Attacked peer is %s\n",
1835 GNUNET_i2s (&attacked_peer));
1837 /* Substitute do_round () with do_mal_round () */
1838 GNUNET_SCHEDULER_cancel (do_round_task);
1839 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1841 else if (0 == mal_type)
1842 { /* Stop acting malicious */
1843 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
1845 /* Substitute do_mal_round () with do_round () */
1846 GNUNET_SCHEDULER_cancel (do_round_task);
1847 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
1854 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1859 * Send out PUSHes and PULLs maliciously.
1861 * This is executed regylary.
1864 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1866 uint32_t num_pushes;
1868 struct GNUNET_TIME_Relative time_next_round;
1869 struct AttackedPeer *tmp_att_peer;
1871 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round maliciously.\n");
1873 /* Do malicious actions */
1875 { /* Try to maximise representation */
1877 /* The maximum of pushes we're going to send this round */
1878 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit,
1879 num_attacked_peers),
1880 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
1882 LOG (GNUNET_ERROR_TYPE_DEBUG,
1883 "Going to send %" PRIu32 " pushes\n",
1886 /* Send PUSHes to attacked peers */
1887 for (i = 0 ; i < num_pushes ; i++)
1889 if (att_peers_tail == att_peer_index)
1890 att_peer_index = att_peers_head;
1892 att_peer_index = att_peer_index->next;
1894 send_push (&att_peer_index->peer_id);
1897 /* Send PULLs to some peers to learn about additional peers to attack */
1898 tmp_att_peer = att_peer_index;
1899 for (i = 0 ; i < num_pushes * alpha ; i++)
1901 if (att_peers_tail == tmp_att_peer)
1902 tmp_att_peer = att_peers_head;
1904 att_peer_index = tmp_att_peer->next;
1906 send_pull_request (&tmp_att_peer->peer_id);
1911 else if (2 == mal_type)
1913 * Try to partition the network
1914 * Send as many pushes to the attacked peer as possible
1915 * That is one push per round as it will ignore more.
1917 send_push (&attacked_peer);
1922 { /* Combined attack */
1924 /* The maximum of pushes we're going to send this round */
1925 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit - 1,
1926 num_attacked_peers),
1927 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
1929 LOG (GNUNET_ERROR_TYPE_DEBUG,
1930 "Going to send %" PRIu32 " pushes\n",
1933 /* Send PUSHes to attacked peers */
1934 send_push (&attacked_peer);
1936 for (i = 0 ; i < num_pushes ; i++)
1938 if (att_peers_tail == att_peer_index)
1939 att_peer_index = att_peers_head;
1941 att_peer_index = att_peer_index->next;
1943 send_push (&att_peer_index->peer_id);
1946 /* Send PULLs to some peers to learn about additional peers to attack */
1947 for (i = 0 ; i < num_pushes * alpha ; i++)
1949 if (att_peers_tail == tmp_att_peer)
1950 tmp_att_peer = att_peers_head;
1952 att_peer_index = tmp_att_peer->next;
1954 send_pull_request (&tmp_att_peer->peer_id);
1958 /* Schedule next round */
1959 time_next_round = compute_rand_delay (round_interval, 2);
1961 //do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_mal_round, NULL);
1962 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_mal_round, NULL);
1963 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
1965 #endif /* ENABLE_MALICIOUS */
1969 * Send out PUSHes and PULLs, possibly update #gossip_list, samplers.
1971 * This is executed regylary.
1974 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1976 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round.\n");
1979 unsigned int *permut;
1980 unsigned int n_peers; /* Number of peers we send pushes/pulls to */
1981 struct GNUNET_PeerIdentity peer;
1982 struct GNUNET_PeerIdentity *tmp_peer;
1984 LOG (GNUNET_ERROR_TYPE_DEBUG,
1985 "Printing gossip list:\n");
1986 for (i = 0 ; i < gossip_list_size ; i++)
1988 LOG (GNUNET_ERROR_TYPE_DEBUG,
1989 "\t%s\n", GNUNET_i2s (&gossip_list[i]));
1990 to_file (file_name_view_log,
1992 GNUNET_i2s_full (&gossip_list[i]));
1994 // TODO log lists, ...
1996 /* Would it make sense to have one shuffeled gossip list and then
1997 * to send PUSHes to first alpha peers, PULL requests to next beta peers and
1998 * use the rest to update sampler?
1999 * in essence get random peers with consumption */
2002 if (0 < gossip_list_size)
2004 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2005 (unsigned int) gossip_list_size);
2006 n_peers = ceil (alpha * gossip_list_size);
2007 LOG (GNUNET_ERROR_TYPE_DEBUG,
2008 "Going to send pushes to %u (ceil (%f * %u)) peers.\n",
2009 n_peers, alpha, gossip_list_size);
2010 for (i = 0 ; i < n_peers ; i++)
2012 peer = gossip_list[permut[i]];
2013 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer)) // TODO
2014 { // FIXME if this fails schedule/loop this for later
2018 GNUNET_free (permut);
2022 /* Send PULL requests */
2023 //permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list->size);
2024 n_peers = ceil (beta * gossip_list_size);
2025 LOG (GNUNET_ERROR_TYPE_DEBUG,
2026 "Going to send pulls to %u (ceil (%f * %u)) peers.\n",
2027 n_peers, beta, gossip_list_size);
2028 for (i = 0 ; i < n_peers ; i++)
2030 tmp_peer = get_rand_peer_ignore_list (gossip_list, gossip_list_size,
2031 pending_pull_reply_list, pending_pull_reply_list_size);
2032 if (NULL != tmp_peer)
2035 GNUNET_free (tmp_peer);
2037 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer))
2039 send_pull_request (&peer);
2045 /* Update gossip list */
2046 /* TODO see how many peers are in push-/pull- list! */
2048 if (push_list_size <= alpha * gossip_list_size
2049 && push_list_size > 0
2050 && pull_list_size > 0)
2052 LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the gossip list.\n");
2054 uint32_t first_border;
2055 uint32_t second_border;
2057 uint32_t peers_to_clean_size;
2058 struct GNUNET_PeerIdentity *peers_to_clean;
2060 peers_to_clean = NULL;
2061 peers_to_clean_size = 0;
2062 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, gossip_list_size);
2063 memcpy (peers_to_clean,
2065 gossip_list_size * sizeof (struct GNUNET_PeerIdentity));
2067 first_border = ceil (alpha * sampler_size_est_need);
2068 second_border = first_border + ceil (beta * sampler_size_est_need);
2070 GNUNET_array_grow (gossip_list, gossip_list_size, second_border);
2072 to_file (file_name_view_log,
2075 for (i = 0 ; i < first_border ; i++)
2076 {/* Update gossip list with peers received through PUSHes */
2077 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
2079 gossip_list[i] = push_list[r_index];
2080 to_file (file_name_view_log,
2082 GNUNET_i2s_full (&gossip_list[i]));
2083 // TODO change the peer_flags accordingly
2086 for (i = first_border ; i < second_border ; i++)
2087 {/* Update gossip list with peers received through PULLs */
2088 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
2090 gossip_list[i] = pull_list[r_index];
2091 to_file (file_name_view_log,
2093 GNUNET_i2s_full (&gossip_list[i]));
2094 // TODO change the peer_flags accordingly
2097 for (i = second_border ; i < sampler_size_est_need ; i++)
2098 {/* Update gossip list with peers from history */
2099 RPS_sampler_get_n_rand_peers (prot_sampler,
2103 num_hist_update_tasks++;
2104 // TODO change the peer_flags accordingly
2107 for (i = 0 ; i < gossip_list_size ; i++)
2108 rem_from_list (&peers_to_clean, &peers_to_clean_size, &gossip_list[i]);
2110 for (i = 0 ; i < peers_to_clean_size ; i++)
2112 peer_clean (&peers_to_clean[i]);
2113 /* to_file (file_name_view_log,
2115 GNUNET_i2s_full (&peers_to_clean[i])); */
2118 GNUNET_free (peers_to_clean);
2122 LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the gossip list.\n");
2124 // TODO independent of that also get some peers from CADET_get_peers()?
2126 LOG (GNUNET_ERROR_TYPE_DEBUG,
2127 "Received %u pushes and %u pulls last round (alpha (%.2f) * gossip_list_size (%u) = %.2f)\n",
2132 alpha * gossip_list_size);
2134 /* Update samplers */
2135 for ( i = 0 ; i < push_list_size ; i++ )
2137 LOG (GNUNET_ERROR_TYPE_DEBUG,
2138 "Updating with peer %s from push list\n",
2139 GNUNET_i2s (&push_list[i]));
2140 RPS_sampler_update (prot_sampler, &push_list[i]);
2141 RPS_sampler_update (client_sampler, &push_list[i]);
2142 // TODO set in_flag?
2145 for ( i = 0 ; i < pull_list_size ; i++ )
2147 LOG (GNUNET_ERROR_TYPE_DEBUG,
2148 "Updating with peer %s from pull list\n",
2149 GNUNET_i2s (&pull_list[i]));
2150 RPS_sampler_update (prot_sampler, &pull_list[i]);
2151 RPS_sampler_update (client_sampler, &pull_list[i]);
2152 // TODO set in_flag?
2156 /* Empty push/pull lists */
2157 GNUNET_array_grow (push_list, push_list_size, 0);
2158 GNUNET_array_grow (pull_list, pull_list_size, 0);
2160 struct GNUNET_TIME_Relative time_next_round;
2162 time_next_round = compute_rand_delay (round_interval, 2);
2164 /* Schedule next round */
2165 //do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_round, NULL);
2166 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_round, NULL);
2167 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
2172 rps_start (struct GNUNET_SERVER_Handle *server);
2176 * This is called from GNUNET_CADET_get_peers().
2178 * It is called on every peer(ID) that cadet somehow has contact with.
2179 * We use those to initialise the sampler.
2182 init_peer_cb (void *cls,
2183 const struct GNUNET_PeerIdentity *peer,
2184 int tunnel, // "Do we have a tunnel towards this peer?"
2185 unsigned int n_paths, // "Number of known paths towards this peer"
2186 unsigned int best_path) // "How long is the best path?
2187 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
2191 LOG (GNUNET_ERROR_TYPE_DEBUG,
2192 "Got peer_id %s from cadet\n",
2200 * Clean the send channel of a peer
2203 peer_clean (const struct GNUNET_PeerIdentity *peer)
2205 struct PeerContext *peer_ctx;
2206 struct GNUNET_CADET_Channel *channel;
2208 if (GNUNET_YES != in_arr (gossip_list, gossip_list_size, peer)
2209 && GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
2211 peer_ctx = get_peer_ctx (peer_map, peer);
2212 if (NULL != peer_ctx->send_channel)
2214 channel = peer_ctx->send_channel;
2215 peer_ctx->send_channel = NULL;
2216 GNUNET_CADET_channel_destroy (channel);
2223 * Callback used to remove peers from the multipeermap.
2226 peer_remove_cb (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
2228 struct PeerContext *peer_ctx;
2229 const struct GNUNET_CADET_Channel *channel =
2230 (const struct GNUNET_CADET_Channel *) cls;
2231 struct GNUNET_CADET_Channel *recv;
2232 struct GNUNET_CADET_Channel *send;
2234 peer_ctx = (struct PeerContext *) value;
2236 LOG (GNUNET_ERROR_TYPE_DEBUG,
2237 "Going to clean peer %s\n",
2238 GNUNET_i2s (&peer_ctx->peer_id));
2240 /* If operations are still scheduled for this peer cancel those */
2241 if (0 != peer_ctx->num_outstanding_ops)
2242 GNUNET_array_grow (peer_ctx->outstanding_ops,
2243 peer_ctx->num_outstanding_ops,
2246 /* If we are still waiting for notification whether this peer is live
2247 * cancel the according task */
2248 if (NULL != peer_ctx->is_live_task)
2250 LOG (GNUNET_ERROR_TYPE_DEBUG,
2251 "Trying to cancle is_live_task for peer %s\n",
2253 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
2254 peer_ctx->is_live_task = NULL;
2258 recv = peer_ctx->recv_channel;
2259 peer_ctx->recv_channel = NULL;
2263 GNUNET_CADET_channel_destroy (recv);
2266 /* If there is still a mq destroy it */
2267 if (NULL != peer_ctx->mq)
2269 GNUNET_MQ_destroy (peer_ctx->mq);
2270 peer_ctx->mq = NULL;
2273 /* Remove the send_channel
2274 * The peer map entry should be removed
2275 * from the callback #cleanup_channel */
2276 send = peer_ctx->send_channel;
2277 peer_ctx->send_channel = NULL;
2281 GNUNET_CADET_channel_destroy (send);
2284 { /* If there is no channel we have to remove it now */
2285 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_remove_all (peer_map, key))
2286 LOG (GNUNET_ERROR_TYPE_WARNING, "removing peer from peer_map failed\n");
2288 GNUNET_free (peer_ctx);
2296 * Task run during shutdown.
2302 shutdown_task (void *cls,
2303 const struct GNUNET_SCHEDULER_TaskContext *tc)
2306 LOG (GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
2308 if (NULL != do_round_task)
2310 GNUNET_SCHEDULER_cancel (do_round_task);
2311 do_round_task = NULL;
2316 if (GNUNET_SYSERR ==
2317 GNUNET_CONTAINER_multipeermap_iterate (peer_map, peer_remove_cb, NULL))
2318 LOG (GNUNET_ERROR_TYPE_WARNING,
2319 "Iterating over peers to disconnect from them was cancelled\n");
2322 GNUNET_NSE_disconnect (nse);
2323 RPS_sampler_destroy (prot_sampler);
2324 RPS_sampler_destroy (client_sampler);
2325 LOG (GNUNET_ERROR_TYPE_DEBUG,
2326 "Size of the peermap: %u\n",
2327 GNUNET_CONTAINER_multipeermap_size (peer_map));
2328 GNUNET_break (0 == GNUNET_CONTAINER_multipeermap_size (peer_map));
2329 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
2330 GNUNET_CADET_disconnect (cadet_handle);
2331 GNUNET_array_grow (gossip_list, gossip_list_size, 0);
2332 GNUNET_array_grow (push_list, push_list_size, 0);
2333 GNUNET_array_grow (pull_list, pull_list_size, 0);
2334 #ifdef ENABLE_MALICIOUS
2335 struct AttackedPeer *tmp_att_peer;
2336 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
2337 if (NULL != mal_peer_set)
2338 GNUNET_CONTAINER_multipeermap_destroy (mal_peer_set);
2339 if (NULL != att_peer_set)
2340 GNUNET_CONTAINER_multipeermap_destroy (att_peer_set);
2341 while (NULL != att_peers_head)
2343 tmp_att_peer = att_peers_head;
2344 GNUNET_CONTAINER_DLL_remove (att_peers_head, att_peers_tail, tmp_att_peer);
2346 #endif /* ENABLE_MALICIOUS */
2351 * A client disconnected. Remove all of its data structure entries.
2353 * @param cls closure, NULL
2354 * @param client identification of the client
2357 handle_client_disconnect (void *cls,
2358 struct GNUNET_SERVER_Client * client)
2364 * Handle the channel a peer opens to us.
2366 * @param cls The closure
2367 * @param channel The channel the peer wants to establish
2368 * @param initiator The peer's peer ID
2369 * @param port The port the channel is being established over
2370 * @param options Further options
2373 handle_inbound_channel (void *cls,
2374 struct GNUNET_CADET_Channel *channel,
2375 const struct GNUNET_PeerIdentity *initiator,
2377 enum GNUNET_CADET_ChannelOption options)
2379 struct PeerContext *peer_ctx;
2380 struct GNUNET_PeerIdentity peer;
2383 LOG (GNUNET_ERROR_TYPE_DEBUG,
2384 "New channel was established to us (Peer %s).\n",
2385 GNUNET_i2s (&peer));
2387 GNUNET_assert (NULL != channel);
2389 // we might not even store the recv_channel
2391 peer_ctx = get_peer_ctx (peer_map, &peer);
2392 // FIXME what do we do if a channel is established twice?
2393 // overwrite? Clean old channel? ...?
2394 //if (NULL != peer_ctx->recv_channel)
2396 // peer_ctx->recv_channel = channel;
2398 peer_ctx->recv_channel = channel;
2400 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, &peer, peer_ctx,
2401 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
2403 peer_is_live (peer_ctx);
2405 return NULL; // TODO
2410 * This is called when a remote peer destroys a channel.
2412 * @param cls The closure
2413 * @param channel The channel being closed
2414 * @param channel_ctx The context associated with this channel
2417 cleanup_channel (void *cls,
2418 const struct GNUNET_CADET_Channel *channel,
2421 struct GNUNET_PeerIdentity *peer;
2422 struct PeerContext *peer_ctx;
2424 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
2425 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
2426 // Guess simply casting isn't the nicest way...
2427 // FIXME wait for cadet to change this function
2429 //if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
2431 peer_ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
2433 if (NULL == peer_ctx) /* It could have been removed by shutdown_task */
2436 if (channel == peer_ctx->send_channel)
2437 { /* Peer went down or we killd the channel */
2438 LOG (GNUNET_ERROR_TYPE_DEBUG,
2439 "send channel (%s) was destroyed - cleaning up\n",
2441 rem_from_list (&gossip_list, &gossip_list_size, peer);
2442 rem_from_list (&pending_pull_reply_list, &pending_pull_reply_list_size, peer);
2444 peer_ctx->send_channel = NULL;
2445 /* Somwewhat {ab,re}use the iterator function */
2446 /* Cast to void is ok, because it's used as void in peer_remove_cb */
2447 (void) peer_remove_cb ((void *) channel, peer, peer_ctx);
2449 //if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_remove_all (peer_map, key))
2450 // LOG (GNUNET_ERROR_TYPE_WARNING, "Removing peer from peer_map failed\n");
2452 // GNUNET_free (peer_ctx);
2454 else if (channel == peer_ctx->recv_channel)
2455 { /* Other peer doesn't want to send us messages anymore */
2456 LOG (GNUNET_ERROR_TYPE_DEBUG,
2457 "Peer %s destroyed recv channel - cleaning up channel\n",
2459 peer_ctx->recv_channel = NULL;
2466 * Actually start the service.
2469 rps_start (struct GNUNET_SERVER_Handle *server)
2471 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
2472 {&handle_client_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
2473 sizeof (struct GNUNET_RPS_CS_RequestMessage)},
2474 {&handle_client_seed, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_SEED, 0},
2475 #ifdef ENABLE_MALICIOUS
2476 {&handle_client_act_malicious, NULL, GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS , 0},
2477 #endif /* ENABLE_MALICIOUS */
2481 GNUNET_SERVER_add_handlers (server, handlers);
2482 GNUNET_SERVER_disconnect_notify (server,
2483 &handle_client_disconnect,
2485 LOG (GNUNET_ERROR_TYPE_INFO, "Ready to receive requests from clients\n");
2488 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
2489 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
2491 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2498 * Process statistics requests.
2500 * @param cls closure
2501 * @param server the initialized server
2502 * @param c configuration to use
2506 struct GNUNET_SERVER_Handle *server,
2507 const struct GNUNET_CONFIGURATION_Handle *c)
2512 // TODO check what this does -- copied from gnunet-boss
2513 // - seems to work as expected
2514 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
2519 GNUNET_CRYPTO_get_peer_identity (cfg, &own_identity); // TODO check return value
2520 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2521 "STARTING SERVICE (rps) for peer [%s]\n",
2522 GNUNET_i2s (&own_identity));
2523 #ifdef ENABLE_MALICIOUS
2524 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2525 "Malicious execution compiled in.\n");
2526 #endif /* ENABLE_MALICIOUS */
2530 /* Get time interval from the configuration */
2531 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
2535 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read ROUNDINTERVAL from config\n");
2536 GNUNET_SCHEDULER_shutdown ();
2540 /* Get initial size of sampler/gossip list from the configuration */
2541 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "RPS",
2543 (long long unsigned int *) &sampler_size_est_need))
2545 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read INITSIZE from config\n");
2546 GNUNET_SCHEDULER_shutdown ();
2549 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", sampler_size_est_need);
2553 gossip_list_size = 0;
2555 /* file_name_view_log */
2556 GNUNET_DISK_directory_create ("/tmp/rps/");
2558 size = (14 + strlen (GNUNET_i2s_full (&own_identity)) + 1) * sizeof (char);
2559 file_name_view_log = GNUNET_malloc (size);
2560 out_size = GNUNET_snprintf (file_name_view_log,
2563 GNUNET_i2s_full (&own_identity));
2564 if (size < out_size ||
2567 LOG (GNUNET_ERROR_TYPE_WARNING,
2568 "Failed to write string to buffer (size: %i, out_size: %i)\n",
2574 /* connect to NSE */
2575 nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
2581 peer_map = GNUNET_CONTAINER_multipeermap_create (sampler_size_est_need, GNUNET_NO);
2584 /* Initialise cadet */
2585 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
2586 {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH ,
2587 sizeof (struct GNUNET_MessageHeader)},
2588 {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
2589 sizeof (struct GNUNET_MessageHeader)},
2590 {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY , 0},
2594 const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
2595 cadet_handle = GNUNET_CADET_connect (cfg,
2597 &handle_inbound_channel,
2603 /* Initialise sampler */
2604 struct GNUNET_TIME_Relative half_round_interval;
2605 struct GNUNET_TIME_Relative max_round_interval;
2607 half_round_interval = GNUNET_TIME_relative_multiply (round_interval, .5);
2608 max_round_interval = GNUNET_TIME_relative_add (round_interval, half_round_interval);
2610 prot_sampler = RPS_sampler_init (sampler_size_est_need, max_round_interval);
2611 client_sampler = RPS_sampler_mod_init (sampler_size_est_need, max_round_interval);
2613 /* Initialise push and pull maps */
2618 pending_pull_reply_list = NULL;
2619 pending_pull_reply_list_size = 0;
2622 num_hist_update_tasks = 0;
2625 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
2626 GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, NULL);
2627 // TODO send push/pull to each of those peers?
2635 * The main function for the rps service.
2637 * @param argc number of arguments from the command line
2638 * @param argv command line arguments
2639 * @return 0 ok, 1 on error
2642 main (int argc, char *const *argv)
2644 return (GNUNET_OK ==
2645 GNUNET_SERVICE_run (argc,
2648 GNUNET_SERVICE_OPTION_NONE,
2649 &run, NULL)) ? 0 : 1;
2652 /* end of gnunet-service-rps.c */