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.
103 * We set this bit when we are going to destroy the channel to this peer.
104 * When cleanup_channel is called, we know that we wanted to destroy it.
105 * Otherwise the channel to the other peer was destroyed.
112 * Functions of this type can be used to be stored at a peer for later execution.
114 typedef void (* PeerOp) (void *cls, const struct GNUNET_PeerIdentity *peer);
117 * Outstanding operation on peer consisting of callback and closure
119 struct PeerOutstandingOp
134 * Struct used to keep track of other peer's status
136 * This is stored in a multipeermap.
141 * In own gossip/sampler list, in other's gossip/sampler list
146 * Message queue open to client
148 struct GNUNET_MQ_Handle *mq;
151 * Channel open to client.
153 struct GNUNET_CADET_Channel *send_channel;
156 * Channel open from client.
158 struct GNUNET_CADET_Channel *recv_channel; // unneeded?
161 * Array of outstanding operations on this peer.
163 struct PeerOutstandingOp *outstanding_ops;
166 * Number of outstanding operations.
168 unsigned int num_outstanding_ops;
169 //size_t num_outstanding_ops;
172 * Handle to the callback given to cadet_ntfy_tmt_rdy()
174 * To be canceled on shutdown.
176 struct GNUNET_CADET_TransmitHandle *is_live_task;
179 * Identity of the peer
181 struct GNUNET_PeerIdentity peer_id;
184 * This is pobably followed by 'statistical' data (when we first saw
185 * him, how did we get his ID, how many pushes (in a timeinterval),
190 /***********************************************************************
191 * /Housekeeping with peers
192 ***********************************************************************/
198 /***********************************************************************
200 ***********************************************************************/
203 * Sampler used for the Brahms protocol itself.
205 static struct RPS_Sampler *prot_sampler;
208 * Sampler used for the clients.
210 static struct RPS_Sampler *client_sampler;
213 * Set of all peers to keep track of them.
215 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
219 * The gossiped list of peers.
221 static struct GNUNET_PeerIdentity *gossip_list;
224 * Size of the gossiped list
226 //static unsigned int gossip_list_size;
227 static uint32_t gossip_list_size;
230 * Name to log view (gossip_list) to
232 static char *file_name_view_log;
236 * The size of sampler we need to be able to satisfy the client's need of
239 static unsigned int sampler_size_client_need;
242 * The size of sampler we need to be able to satisfy the Brahms protocol's
243 * need of random peers.
245 * This is directly taken as the #gossip_list_size on update of the
248 * This is one minimum size the sampler grows to.
250 static unsigned int sampler_size_est_need;
254 * Percentage of total peer number in the gossip list
255 * to send random PUSHes to
260 * Percentage of total peer number in the gossip list
261 * to send random PULLs to
266 * The percentage gamma of history updates.
267 * Simply 1 - alpha - beta
272 * Identifier for the main task that runs periodically.
274 static struct GNUNET_SCHEDULER_Task *do_round_task;
277 * Time inverval the do_round task runs in.
279 static struct GNUNET_TIME_Relative round_interval;
284 * List to store peers received through pushes temporary.
286 * TODO -> multipeermap
288 static struct GNUNET_PeerIdentity *push_list;
291 * Size of the push_list;
293 static unsigned int push_list_size;
294 //size_t push_list_size;
297 * List to store peers received through pulls temporary.
299 * TODO -> multipeermap
301 static struct GNUNET_PeerIdentity *pull_list;
304 * Size of the pull_list;
306 static unsigned int pull_list_size;
307 //size_t pull_list_size;
313 static struct GNUNET_NSE_Handle *nse;
318 static struct GNUNET_CADET_Handle *cadet_handle;
324 * Only needed in the beginning to check how many of the 64 deltas
327 static unsigned int req_counter;
330 * Time of the last request we received.
332 * Used to compute the expected request rate.
334 static struct GNUNET_TIME_Absolute last_request;
337 * Size of #request_deltas.
339 #define REQUEST_DELTAS_SIZE 64
340 static unsigned int request_deltas_size = REQUEST_DELTAS_SIZE;
343 * Last 64 deltas between requests
345 static struct GNUNET_TIME_Relative request_deltas[REQUEST_DELTAS_SIZE];
348 * The prediction of the rate of requests
350 static struct GNUNET_TIME_Relative request_rate;
354 * List with the peers we sent requests to.
356 struct GNUNET_PeerIdentity *pending_pull_reply_list;
359 * Size of #pending_pull_reply_list.
361 uint32_t pending_pull_reply_list_size;
365 * Number of history update tasks.
367 uint32_t num_hist_update_tasks;
371 * Closure used to pass the client and the id to the callback
372 * that replies to a client's request
377 * The identifier of the request
382 * The client handle to send the reply to
384 struct GNUNET_SERVER_Client *client;
388 #ifdef ENABLE_MALICIOUS
390 * Type of malicious peer
392 * 0 Don't act malicious at all - Default
393 * 1 Try to maximise representation
394 * 2 Try to partition the network
397 uint32_t mal_type = 0;
400 * Other malicious peers
402 static struct GNUNET_PeerIdentity *mal_peers = NULL;
405 * Hashmap of malicious peers used as set.
406 * Used to more efficiently check whether we know that peer.
408 static struct GNUNET_CONTAINER_MultiPeerMap *mal_peer_set = NULL;
411 * Number of other malicious peers
413 static uint32_t num_mal_peers = 0;
417 * If type is 2 This struct is used to store the attacked peers in a DLL
424 struct AttackedPeer *next;
425 struct AttackedPeer *prev;
430 struct GNUNET_PeerIdentity peer_id;
434 * If type is 2 this is the DLL of attacked peers
436 static struct AttackedPeer *att_peers_head = NULL;
437 static struct AttackedPeer *att_peers_tail = NULL;
440 * This index is used to point to an attacked peer to
441 * implement the round-robin-ish way to select attacked peers.
443 static struct AttackedPeer *att_peer_index = NULL;
446 * Hashmap of attacked peers used as set.
447 * Used to more efficiently check whether we know that peer.
449 static struct GNUNET_CONTAINER_MultiPeerMap *att_peer_set = NULL;
452 * Number of attacked peers
454 static uint32_t num_attacked_peers = 0;
458 * If type is 1 this is the attacked peer
460 static struct GNUNET_PeerIdentity attacked_peer;
463 * The limit of PUSHes we can send in one round.
464 * This is an assumption of the Brahms protocol and either implemented
467 * assumend to be the bandwidth limitation.
469 static uint32_t push_limit = 10000;
470 #endif /* ENABLE_MALICIOUS */
473 /***********************************************************************
475 ***********************************************************************/
482 /***********************************************************************
484 ***********************************************************************/
487 * Set a peer flag of given peer context.
489 #define set_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags |= mask)
492 * Get peer flag of given peer context.
494 #define get_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags & mask ? GNUNET_YES : GNUNET_NO)
497 * Unset flag of given peer context.
499 #define unset_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags &= (~mask))
502 * Clean the send channel of a peer
505 peer_clean (const struct GNUNET_PeerIdentity *peer);
509 * Check if peer is already in peer array.
512 in_arr (const struct GNUNET_PeerIdentity *array,
513 unsigned int arr_size,
514 const struct GNUNET_PeerIdentity *peer)
516 GNUNET_assert (NULL != peer);
521 GNUNET_assert (NULL != array);
525 for (i = 0; i < arr_size ; i++)
526 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&array[i], peer))
533 * Print peerlist to log.
536 print_peer_list (struct GNUNET_PeerIdentity *list, unsigned int len)
540 LOG (GNUNET_ERROR_TYPE_DEBUG,
541 "Printing peer list of length %u at %p:\n",
544 for (i = 0 ; i < len ; i++)
546 LOG (GNUNET_ERROR_TYPE_DEBUG,
548 i, GNUNET_i2s (&list[i]));
554 * Remove peer from list.
557 rem_from_list (struct GNUNET_PeerIdentity **peer_list,
558 unsigned int *list_size,
559 const struct GNUNET_PeerIdentity *peer)
562 struct GNUNET_PeerIdentity *tmp;
566 LOG (GNUNET_ERROR_TYPE_DEBUG,
567 "Removing peer %s from list at %p\n",
571 for ( i = 0 ; i < *list_size ; i++ )
573 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&tmp[i], peer))
575 if (i < *list_size -1)
576 { /* Not at the last entry -- shift peers left */
577 memcpy (&tmp[i], &tmp[i +1],
578 ((*list_size) - i -1) * sizeof (struct GNUNET_PeerIdentity));
580 /* Remove last entry (should be now useless PeerID) */
581 GNUNET_array_grow (tmp, *list_size, (*list_size) -1);
588 * Get random peer from the given list but don't return one from the @a ignore_list.
590 struct GNUNET_PeerIdentity *
591 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list,
593 const struct GNUNET_PeerIdentity *ignore_list,
594 uint32_t ignore_size)
598 struct GNUNET_PeerIdentity *tmp_peer_list;
599 struct GNUNET_PeerIdentity *peer;
601 GNUNET_assert (NULL != peer_list);
606 tmp_peer_list = NULL;
607 GNUNET_array_grow (tmp_peer_list, tmp_size, list_size);
608 memcpy (tmp_peer_list,
610 list_size * sizeof (struct GNUNET_PeerIdentity));
611 peer = GNUNET_new (struct GNUNET_PeerIdentity);
614 * Choose the r_index of the peer we want to return
615 * at random from the interval of the gossip list
617 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
619 *peer = tmp_peer_list[r_index];
621 while (in_arr (ignore_list, ignore_size, peer))
623 rem_from_list (&tmp_peer_list, &tmp_size, peer);
625 print_peer_list (tmp_peer_list, tmp_size);
634 * Choose the r_index of the peer we want to return
635 * at random from the interval of the gossip list
637 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
639 *peer = tmp_peer_list[r_index];
643 GNUNET_array_grow (tmp_peer_list, tmp_size, 0);
650 * Get the context of a peer. If not existing, create.
653 get_peer_ctx (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
654 const struct GNUNET_PeerIdentity *peer)
656 struct PeerContext *ctx;
658 if ( GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
660 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
664 ctx = GNUNET_new (struct PeerContext);
667 ctx->send_channel = NULL;
668 ctx->recv_channel = NULL;
669 ctx->outstanding_ops = NULL;
670 ctx->num_outstanding_ops = 0;
671 ctx->is_live_task = NULL;
672 ctx->peer_id = *peer;
673 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
674 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
681 * Put random peer from sampler into the gossip list as history update.
684 hist_update (void *cls, struct GNUNET_PeerIdentity *ids, uint32_t num_peers)
686 GNUNET_assert (1 == num_peers);
688 if (gossip_list_size < sampler_size_est_need)
690 GNUNET_array_append (gossip_list, gossip_list_size, *ids);
691 to_file (file_name_view_log,
693 GNUNET_i2s_full (ids));
696 if (0 < num_hist_update_tasks)
697 num_hist_update_tasks--;
702 * Set the peer flag to living and call the outstanding operations on this peer.
705 peer_is_live (struct PeerContext *peer_ctx)
707 struct GNUNET_PeerIdentity *peer;
709 /* Cancle is_live_task if still scheduled */
710 if (NULL != peer_ctx->is_live_task)
712 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
713 peer_ctx->is_live_task = NULL;
716 peer = &peer_ctx->peer_id;
717 set_peer_flag (peer_ctx, VALID);
719 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer %s is live\n", GNUNET_i2s (peer));
721 if (0 < peer_ctx->num_outstanding_ops)
722 { /* Call outstanding operations */
725 for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
726 peer_ctx->outstanding_ops[i].op (peer_ctx->outstanding_ops[i].op_cls, peer);
727 GNUNET_array_grow (peer_ctx->outstanding_ops, peer_ctx->num_outstanding_ops, 0);
735 * Callback that is called when a channel was effectively established.
736 * This is given to ntfy_tmt_rdy and called when the channel was
737 * successfully established.
740 cadet_ntfy_tmt_rdy_cb (void *cls, size_t size, void *buf)
742 struct PeerContext *peer_ctx = (struct PeerContext *) cls;
744 peer_ctx->is_live_task = NULL;
745 LOG (GNUNET_ERROR_TYPE_DEBUG,
746 "Set ->is_live_task = NULL for peer %s\n",
747 GNUNET_i2s (&peer_ctx->peer_id));
752 peer_is_live (peer_ctx);
756 LOG (GNUNET_ERROR_TYPE_WARNING,
757 "Problems establishing a connection to peer %s in order to check liveliness\n",
758 GNUNET_i2s (&peer_ctx->peer_id));
759 // TODO reschedule? cleanup?
762 //if (NULL != peer_ctx->is_live_task)
764 // LOG (GNUNET_ERROR_TYPE_DEBUG,
765 // "Trying to cancle is_live_task for peer %s\n",
766 // GNUNET_i2s (&peer_ctx->peer_id));
767 // GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
768 // peer_ctx->is_live_task = NULL;
776 * Get the channel of a peer. If not existing, create.
778 struct GNUNET_CADET_Channel *
779 get_channel (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
780 const struct GNUNET_PeerIdentity *peer)
782 struct PeerContext *peer_ctx;
784 peer_ctx = get_peer_ctx (peer_map, peer);
786 if (NULL == peer_ctx->send_channel)
788 LOG (GNUNET_ERROR_TYPE_DEBUG,
789 "Trying to establish channel to peer %s\n",
792 peer_ctx->send_channel =
793 GNUNET_CADET_channel_create (cadet_handle,
796 GNUNET_RPS_CADET_PORT,
797 GNUNET_CADET_OPTION_RELIABLE);
799 // do I have to explicitly put it in the peer_map?
800 (void) GNUNET_CONTAINER_multipeermap_put
804 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
806 return peer_ctx->send_channel;
811 * Get the message queue of a specific peer.
813 * If we already have a message queue open to this client,
814 * simply return it, otherways create one.
816 struct GNUNET_MQ_Handle *
817 get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
818 const struct GNUNET_PeerIdentity *peer_id)
820 struct PeerContext *peer_ctx;
822 peer_ctx = get_peer_ctx (peer_map, peer_id);
824 GNUNET_assert (NULL == peer_ctx->is_live_task);
826 if (NULL == peer_ctx->mq)
828 (void) get_channel (peer_map, peer_id);
829 peer_ctx->mq = GNUNET_CADET_mq_create (peer_ctx->send_channel);
830 //do I have to explicitly put it in the peer_map?
831 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer_id, peer_ctx,
832 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
839 * Issue check whether peer is live
841 * @param peer_ctx the context of the peer
844 check_peer_live (struct PeerContext *peer_ctx)
846 (void) get_channel (peer_map, &peer_ctx->peer_id);
847 LOG (GNUNET_ERROR_TYPE_DEBUG,
848 "Get informed about peer %s getting live\n",
849 GNUNET_i2s (&peer_ctx->peer_id));
850 if (NULL == peer_ctx->is_live_task)
852 peer_ctx->is_live_task =
853 GNUNET_CADET_notify_transmit_ready (peer_ctx->send_channel,
855 GNUNET_TIME_UNIT_FOREVER_REL,
856 sizeof (struct GNUNET_MessageHeader),
857 cadet_ntfy_tmt_rdy_cb,
862 LOG (GNUNET_ERROR_TYPE_DEBUG,
863 "Already waiting for notification\n");
869 * Sum all time relatives of an array.
871 struct GNUNET_TIME_Relative
872 T_relative_sum (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
874 struct GNUNET_TIME_Relative sum;
877 sum = GNUNET_TIME_UNIT_ZERO;
878 for ( i = 0 ; i < arr_size ; i++ )
880 sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
887 * Compute the average of given time relatives.
889 struct GNUNET_TIME_Relative
890 T_relative_avg (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
892 return GNUNET_TIME_relative_divide (T_relative_sum (rel_array, arr_size), arr_size);
897 * Insert PeerID in #pull_list
899 * Called once we know a peer is live.
902 insert_in_pull_list (void *cls, const struct GNUNET_PeerIdentity *peer)
904 if (GNUNET_NO == in_arr (pull_list, pull_list_size, peer))
905 GNUNET_array_append (pull_list, pull_list_size, *peer);
911 * Check whether #insert_in_pull_list was already scheduled
914 insert_in_pull_list_scheduled (const struct PeerContext *peer_ctx)
918 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
919 if (insert_in_pull_list == peer_ctx->outstanding_ops[i].op)
926 * Insert PeerID in #gossip_list
928 * Called once we know a peer is live.
931 insert_in_gossip_list (void *cls, const struct GNUNET_PeerIdentity *peer)
933 if (GNUNET_NO == in_arr (gossip_list, gossip_list_size, peer))
935 GNUNET_array_append (gossip_list, gossip_list_size, *peer);
936 to_file (file_name_view_log,
937 "+%s\t(ins in gossip list)",
938 GNUNET_i2s_full (peer));
941 (void) get_channel (peer_map, peer);
945 * Check whether #insert_in_gossip_list was already scheduled
948 insert_in_gossip_list_scheduled (const struct PeerContext *peer_ctx)
952 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
953 if (insert_in_gossip_list == peer_ctx->outstanding_ops[i].op)
960 * Update sampler with given PeerID.
963 insert_in_sampler (void *cls, const struct GNUNET_PeerIdentity *peer)
965 LOG (GNUNET_ERROR_TYPE_DEBUG,
966 "Updating samplers with peer %s from insert_in_sampler()\n",
968 RPS_sampler_update (prot_sampler, peer);
969 RPS_sampler_update (client_sampler, peer);
974 * Check whether #insert_in_sampler was already scheduled
977 insert_in_sampler_scheduled (const struct PeerContext *peer_ctx)
981 for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
982 if (insert_in_sampler== peer_ctx->outstanding_ops[i].op)
989 * Wrapper around #RPS_sampler_resize()
991 * If we do not have enough sampler elements, double current sampler size
992 * If we have more than enough sampler elements, halv current sampler size
995 resize_wrapper (struct RPS_Sampler *sampler, uint32_t new_size)
997 unsigned int sampler_size;
1000 // TODO respect the min, max
1001 sampler_size = RPS_sampler_get_size (sampler);
1002 if (sampler_size > new_size * 4)
1004 RPS_sampler_resize (sampler, sampler_size / 2);
1006 else if (sampler_size < new_size)
1008 RPS_sampler_resize (sampler, sampler_size * 2);
1010 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
1015 * Wrapper around #RPS_sampler_resize() resizing the client sampler
1018 client_resize_wrapper ()
1020 uint32_t bigger_size;
1021 unsigned int sampler_size;
1025 sampler_size = RPS_sampler_get_size (client_sampler);
1027 if (sampler_size_est_need > sampler_size_client_need)
1028 bigger_size = sampler_size_est_need;
1030 bigger_size = sampler_size_client_need;
1032 // TODO respect the min, max
1033 resize_wrapper (client_sampler, bigger_size);
1034 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
1039 * Estimate request rate
1041 * Called every time we receive a request from the client.
1046 struct GNUNET_TIME_Relative max_round_duration;
1048 if (request_deltas_size > req_counter)
1050 if ( 1 < req_counter)
1052 /* Shift last request deltas to the right */
1053 memcpy (&request_deltas[1],
1055 (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
1057 /* Add current delta to beginning */
1059 GNUNET_TIME_absolute_get_difference (last_request,
1060 GNUNET_TIME_absolute_get ());
1061 request_rate = T_relative_avg (request_deltas, req_counter);
1063 /* Compute the duration a round will maximally take */
1064 max_round_duration =
1065 GNUNET_TIME_relative_add (round_interval,
1066 GNUNET_TIME_relative_divide (round_interval, 2));
1068 /* Set the estimated size the sampler has to have to
1069 * satisfy the current client request rate */
1070 sampler_size_client_need =
1071 max_round_duration.rel_value_us / request_rate.rel_value_us;
1073 /* Resize the sampler */
1074 client_resize_wrapper ();
1076 last_request = GNUNET_TIME_absolute_get ();
1081 * Add all peers in @a peer_array to @a peer_map used as set.
1083 * @param peer_array array containing the peers
1084 * @param num_peers number of peers in @peer_array
1085 * @param peer_map the peermap to use as set
1088 add_peer_array_to_set (const struct GNUNET_PeerIdentity *peer_array,
1089 unsigned int num_peers,
1090 struct GNUNET_CONTAINER_MultiPeerMap *peer_map)
1093 if (NULL == peer_map)
1094 peer_map = GNUNET_CONTAINER_multipeermap_create (num_peers + 1,
1096 for (i = 0 ; i < num_peers ; i++)
1098 GNUNET_CONTAINER_multipeermap_put (peer_map,
1101 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1107 * Send a PULL REPLY to @a peer_id
1109 * @param peer_id the peer to send the reply to.
1110 * @param peer_ids the peers to send to @a peer_id
1111 * @param num_peer_ids the number of peers to send to @a peer_id
1114 send_pull_reply (const struct GNUNET_PeerIdentity *peer_id,
1115 const struct GNUNET_PeerIdentity *peer_ids,
1116 unsigned int num_peer_ids)
1119 struct GNUNET_MQ_Handle *mq;
1120 struct GNUNET_MQ_Envelope *ev;
1121 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
1123 /* Compute actual size */
1124 send_size = sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) +
1125 num_peer_ids * sizeof (struct GNUNET_PeerIdentity);
1127 if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size)
1128 /* Compute number of peers to send
1129 * If too long, simply truncate */
1130 // TODO select random ones via permutation
1131 // or even better: do good protocol design
1133 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE -
1134 sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1135 sizeof (struct GNUNET_PeerIdentity);
1137 send_size = num_peer_ids;
1139 LOG (GNUNET_ERROR_TYPE_DEBUG,
1140 "PULL REQUEST from peer %s received, going to send %u peers\n",
1141 GNUNET_i2s (peer_id), send_size);
1143 mq = get_mq (peer_map, peer_id);
1145 ev = GNUNET_MQ_msg_extra (out_msg,
1146 send_size * sizeof (struct GNUNET_PeerIdentity),
1147 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
1148 out_msg->num_peers = htonl (send_size);
1149 memcpy (&out_msg[1], peer_ids,
1150 send_size * sizeof (struct GNUNET_PeerIdentity));
1152 GNUNET_MQ_send (mq, ev);
1157 * This function is called on new peer_ids from 'external' sources
1158 * (client seed, cadet get_peers(), ...)
1160 * @param peer_id the new peer_id
1163 new_peer_id (const struct GNUNET_PeerIdentity *peer_id)
1165 struct PeerOutstandingOp out_op;
1166 struct PeerContext *peer_ctx;
1169 && 0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, peer_id))
1171 LOG (GNUNET_ERROR_TYPE_DEBUG,
1172 "Got peer_id %s (at %p, gossip_list_size: %u)\n",
1173 GNUNET_i2s (peer_id),
1177 peer_ctx = get_peer_ctx (peer_map, peer_id);
1178 if (GNUNET_YES != get_peer_flag (peer_ctx, VALID))
1180 if (GNUNET_NO == insert_in_sampler_scheduled (peer_ctx))
1182 out_op.op = insert_in_sampler;
1183 out_op.op_cls = NULL;
1184 GNUNET_array_append (peer_ctx->outstanding_ops,
1185 peer_ctx->num_outstanding_ops,
1189 if (GNUNET_NO == insert_in_gossip_list_scheduled (peer_ctx))
1191 out_op.op = insert_in_gossip_list;
1192 out_op.op_cls = NULL;
1193 GNUNET_array_append (peer_ctx->outstanding_ops,
1194 peer_ctx->num_outstanding_ops,
1198 /* Trigger livelyness test on peer */
1199 check_peer_live (peer_ctx);
1203 // send push/pull to each of those peers?
1208 /***********************************************************************
1210 ***********************************************************************/
1217 * Function called by NSE.
1219 * Updates sizes of sampler list and gossip list and adapt those lists
1223 nse_callback (void *cls, struct GNUNET_TIME_Absolute timestamp,
1224 double logestimate, double std_dev)
1227 //double scale; // TODO this might go gloabal/config
1229 LOG (GNUNET_ERROR_TYPE_DEBUG,
1230 "Received a ns estimate - logest: %f, std_dev: %f (old_size: %u)\n",
1231 logestimate, std_dev, RPS_sampler_get_size (prot_sampler));
1233 estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
1234 // GNUNET_NSE_log_estimate_to_n (logestimate);
1235 estimate = pow (estimate, 1.0 / 3);
1236 // TODO add if std_dev is a number
1237 // estimate += (std_dev * scale);
1238 if (2 < ceil (estimate))
1240 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
1241 sampler_size_est_need = estimate;
1243 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
1245 /* If the NSE has changed adapt the lists accordingly */
1246 resize_wrapper (prot_sampler, sampler_size_est_need);
1247 client_resize_wrapper ();
1252 * Callback called once the requested PeerIDs are ready.
1254 * Sends those to the requesting client.
1256 void client_respond (void *cls,
1257 struct GNUNET_PeerIdentity *peer_ids, uint32_t num_peers)
1259 struct GNUNET_MQ_Envelope *ev;
1260 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
1261 struct ReplyCls *reply_cls = (struct ReplyCls *) cls;
1262 uint32_t size_needed;
1263 struct ClientContext *cli_ctx;
1265 LOG (GNUNET_ERROR_TYPE_DEBUG,
1266 "sampler returned %" PRIu32 " peers\n",
1269 size_needed = sizeof (struct GNUNET_RPS_CS_ReplyMessage) +
1270 num_peers * sizeof (struct GNUNET_PeerIdentity);
1272 GNUNET_assert (GNUNET_SERVER_MAX_MESSAGE_SIZE >= size_needed);
1274 ev = GNUNET_MQ_msg_extra (out_msg,
1275 num_peers * sizeof (struct GNUNET_PeerIdentity),
1276 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
1277 out_msg->num_peers = htonl (num_peers);
1278 out_msg->id = htonl (reply_cls->id);
1280 memcpy (&out_msg[1],
1282 num_peers * sizeof (struct GNUNET_PeerIdentity));
1283 GNUNET_free (peer_ids);
1285 cli_ctx = GNUNET_SERVER_client_get_user_context (reply_cls->client, struct ClientContext);
1286 if (NULL == cli_ctx) {
1287 cli_ctx = GNUNET_new (struct ClientContext);
1288 cli_ctx->mq = GNUNET_MQ_queue_for_server_client (reply_cls->client);
1289 GNUNET_SERVER_client_set_user_context (reply_cls->client, cli_ctx);
1292 GNUNET_free (reply_cls);
1294 GNUNET_MQ_send (cli_ctx->mq, ev);
1299 * Handle RPS request from the client.
1301 * @param cls closure
1302 * @param client identification of the client
1303 * @param message the actual message
1306 handle_client_request (void *cls,
1307 struct GNUNET_SERVER_Client *client,
1308 const struct GNUNET_MessageHeader *message)
1310 struct GNUNET_RPS_CS_RequestMessage *msg;
1312 uint32_t size_needed;
1313 struct ReplyCls *reply_cls;
1316 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
1318 num_peers = ntohl (msg->num_peers);
1319 size_needed = sizeof (struct GNUNET_RPS_CS_RequestMessage) +
1320 num_peers * sizeof (struct GNUNET_PeerIdentity);
1322 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
1324 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1328 for (i = 0 ; i < num_peers ; i++)
1331 LOG (GNUNET_ERROR_TYPE_DEBUG,
1332 "Client requested %" PRIu32 " random peer(s).\n",
1335 reply_cls = GNUNET_new (struct ReplyCls);
1336 reply_cls->id = ntohl (msg->id);
1337 reply_cls->client = client;
1339 RPS_sampler_get_n_rand_peers (client_sampler,
1344 GNUNET_SERVER_receive_done (client,
1350 * Handle seed from the client.
1352 * @param cls closure
1353 * @param client identification of the client
1354 * @param message the actual message
1357 handle_client_seed (void *cls,
1358 struct GNUNET_SERVER_Client *client,
1359 const struct GNUNET_MessageHeader *message)
1361 struct GNUNET_RPS_CS_SeedMessage *in_msg;
1362 struct GNUNET_PeerIdentity *peers;
1366 if (sizeof (struct GNUNET_RPS_CS_SeedMessage) > ntohs (message->size))
1368 GNUNET_break_op (0);
1369 GNUNET_SERVER_receive_done (client,
1373 in_msg = (struct GNUNET_RPS_CS_SeedMessage *) message;
1374 num_peers = ntohl (in_msg->num_peers);
1375 peers = (struct GNUNET_PeerIdentity *) &in_msg[1];
1376 //peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1377 //memcpy (peers, &in_msg[1], num_peers * sizeof (struct GNUNET_PeerIdentity));
1379 if ((ntohs (message->size) - sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
1380 sizeof (struct GNUNET_PeerIdentity) != num_peers)
1382 GNUNET_break_op (0);
1383 GNUNET_SERVER_receive_done (client,
1387 LOG (GNUNET_ERROR_TYPE_DEBUG,
1388 "Client seeded peers:\n");
1389 print_peer_list (peers, num_peers);
1391 for (i = 0 ; i < num_peers ; i++)
1393 LOG (GNUNET_ERROR_TYPE_DEBUG,
1394 "Updating samplers with seed %" PRIu32 ": %s\n",
1396 GNUNET_i2s (&peers[i]));
1398 new_peer_id (&peers[i]);
1400 //RPS_sampler_update (prot_sampler, &peers[i]);
1401 //RPS_sampler_update (client_sampler, &peers[i]);
1404 ////GNUNET_free (peers);
1406 GNUNET_SERVER_receive_done (client,
1412 * Handle a PUSH message from another peer.
1414 * Check the proof of work and store the PeerID
1415 * in the temporary list for pushed PeerIDs.
1417 * @param cls Closure
1418 * @param channel The channel the PUSH was received over
1419 * @param channel_ctx The context associated with this channel
1420 * @param msg The message header
1423 handle_peer_push (void *cls,
1424 struct GNUNET_CADET_Channel *channel,
1426 const struct GNUNET_MessageHeader *msg)
1428 const struct GNUNET_PeerIdentity *peer;
1430 // (check the proof of work)
1432 peer = (const struct GNUNET_PeerIdentity *)
1433 GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
1434 // FIXME wait for cadet to change this function
1436 LOG (GNUNET_ERROR_TYPE_DEBUG, "PUSH received (%s)\n", GNUNET_i2s (peer));
1438 #ifdef ENABLE_MALICIOUS
1439 struct AttackedPeer *tmp_att_peer;
1441 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1442 memcpy (&tmp_att_peer->peer_id, peer, sizeof (struct GNUNET_PeerIdentity));
1445 { /* Try to maximise representation */
1446 if (NULL == att_peer_set)
1447 att_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
1448 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1451 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1454 add_peer_array_to_set (peer, 1, att_peer_set);
1460 else if (2 == mal_type)
1461 { /* We attack one single well-known peer - simply ignore */
1465 #endif /* ENABLE_MALICIOUS */
1467 /* Add the sending peer to the push_list */
1468 if (GNUNET_NO == in_arr (push_list, push_list_size, peer))
1469 GNUNET_array_append (push_list, push_list_size, *peer);
1476 * Handle PULL REQUEST request message from another peer.
1478 * Reply with the gossip list of PeerIDs.
1480 * @param cls Closure
1481 * @param channel The channel the PUSH was received over
1482 * @param channel_ctx The context associated with this channel
1483 * @param msg The message header
1486 handle_peer_pull_request (void *cls,
1487 struct GNUNET_CADET_Channel *channel,
1489 const struct GNUNET_MessageHeader *msg)
1491 struct GNUNET_PeerIdentity *peer;
1493 peer = (struct GNUNET_PeerIdentity *)
1494 GNUNET_CADET_channel_get_info (channel,
1495 GNUNET_CADET_OPTION_PEER);
1496 // FIXME wait for cadet to change this function
1498 #ifdef ENABLE_MALICIOUS
1501 { /* Try to maximise representation */
1502 send_pull_reply (peer, mal_peers, num_mal_peers);
1506 else if (2 == mal_type)
1507 { /* Try to partition network */
1508 if (GNUNET_YES == GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
1510 send_pull_reply (peer, mal_peers, num_mal_peers);
1514 #endif /* ENABLE_MALICIOUS */
1516 send_pull_reply (peer, gossip_list, gossip_list_size);
1523 * Handle PULL REPLY message from another peer.
1525 * Check whether we sent a corresponding request and
1526 * whether this reply is the first one.
1528 * @param cls Closure
1529 * @param channel The channel the PUSH was received over
1530 * @param channel_ctx The context associated with this channel
1531 * @param msg The message header
1534 handle_peer_pull_reply (void *cls,
1535 struct GNUNET_CADET_Channel *channel,
1537 const struct GNUNET_MessageHeader *msg)
1539 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
1540 struct GNUNET_PeerIdentity *peers;
1541 struct PeerContext *peer_ctx;
1542 struct GNUNET_PeerIdentity *sender;
1543 struct PeerContext *sender_ctx;
1544 struct PeerOutstandingOp out_op;
1546 #ifdef ENABLE_MALICIOUS
1547 struct AttackedPeer *tmp_att_peer;
1548 #endif /* ENABLE_MALICIOUS */
1550 /* Check for protocol violation */
1551 if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->size))
1553 GNUNET_break_op (0);
1554 return GNUNET_SYSERR;
1557 in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
1558 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1559 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1561 LOG (GNUNET_ERROR_TYPE_ERROR,
1562 "message says it sends %" PRIu64 " peers, have space for %i peers\n",
1563 ntohl (in_msg->num_peers),
1564 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1565 sizeof (struct GNUNET_PeerIdentity));
1566 GNUNET_break_op (0);
1567 return GNUNET_SYSERR;
1570 sender = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
1571 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
1572 // Guess simply casting isn't the nicest way...
1573 // FIXME wait for cadet to change this function
1574 sender_ctx = get_peer_ctx (peer_map, sender);
1576 if (GNUNET_YES == get_peer_flag (sender_ctx, PULL_REPLY_PENDING))
1578 GNUNET_break_op (0);
1583 #ifdef ENABLE_MALICIOUS
1584 // We shouldn't even receive pull replies as we're not sending
1587 #endif /* ENABLE_MALICIOUS */
1589 /* Do actual logic */
1590 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1592 LOG (GNUNET_ERROR_TYPE_DEBUG,
1593 "PULL REPLY received, got following peers:\n");
1595 for (i = 0 ; i < ntohl (in_msg->num_peers) ; i++)
1597 LOG (GNUNET_ERROR_TYPE_DEBUG,
1600 GNUNET_i2s (&peers[i]));
1602 #ifdef ENABLE_MALICIOUS
1605 { /* Add attacked peer to local list */
1606 // TODO check if we sent a request and this was the first reply
1607 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1609 && GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mal_peer_set,
1611 && GNUNET_NO == GNUNET_CRYPTO_cmp_peer_identity (&peers[i],
1614 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1615 tmp_att_peer->peer_id = peers[i];
1616 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1619 add_peer_array_to_set (&peers[i], 1, att_peer_set);
1623 #endif /* ENABLE_MALICIOUS */
1624 peer_ctx = get_peer_ctx (peer_map, &peers[i]);
1625 if (GNUNET_YES == get_peer_flag (peer_ctx, VALID)
1626 || NULL != peer_ctx->send_channel
1627 || NULL != peer_ctx->recv_channel)
1629 if (GNUNET_NO == in_arr (pull_list, pull_list_size, &peers[i])
1630 && 0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peers[i]))
1631 GNUNET_array_append (pull_list, pull_list_size, peers[i]);
1633 else if (GNUNET_NO == insert_in_pull_list_scheduled (peer_ctx))
1635 out_op.op = insert_in_pull_list;
1636 out_op.op_cls = NULL;
1637 GNUNET_array_append (peer_ctx->outstanding_ops,
1638 peer_ctx->num_outstanding_ops,
1640 check_peer_live (peer_ctx);
1644 unset_peer_flag (sender_ctx, PULL_REPLY_PENDING);
1645 rem_from_list (&pending_pull_reply_list, &pending_pull_reply_list_size, sender);
1652 * Compute a random delay.
1653 * A uniformly distributed value between mean + spread and mean - spread.
1655 * For example for mean 4 min and spread 2 the minimum is (4 min - (1/2 * 4 min))
1656 * It would return a random value between 2 and 6 min.
1658 * @param mean the mean
1659 * @param spread the inverse amount of deviation from the mean
1661 static struct GNUNET_TIME_Relative
1662 compute_rand_delay (struct GNUNET_TIME_Relative mean, unsigned int spread)
1664 struct GNUNET_TIME_Relative half_interval;
1665 struct GNUNET_TIME_Relative ret;
1666 unsigned int rand_delay;
1667 unsigned int max_rand_delay;
1671 LOG (GNUNET_ERROR_TYPE_WARNING,
1672 "Not accepting spread of 0\n");
1676 /* Compute random time value between spread * mean and spread * mean */
1677 half_interval = GNUNET_TIME_relative_divide (mean, spread);
1679 max_rand_delay = GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us / mean.rel_value_us * (2/spread);
1681 * Compute random value between (0 and 1) * round_interval
1682 * via multiplying round_interval with a 'fraction' (0 to value)/value
1684 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_rand_delay);
1685 ret = GNUNET_TIME_relative_multiply (mean, rand_delay);
1686 ret = GNUNET_TIME_relative_divide (ret, max_rand_delay);
1687 ret = GNUNET_TIME_relative_add (ret, half_interval);
1689 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ret.rel_value_us)
1690 LOG (GNUNET_ERROR_TYPE_WARNING,
1691 "Returning FOREVER_REL\n");
1698 * Send single pull request
1700 * @param peer_id the peer to send the pull request to.
1703 send_pull_request (struct GNUNET_PeerIdentity *peer_id)
1705 struct GNUNET_MQ_Envelope *ev;
1706 struct GNUNET_MQ_Handle *mq;
1708 LOG (GNUNET_ERROR_TYPE_DEBUG,
1709 "Sending PULL request to peer %s of gossiped list.\n",
1710 GNUNET_i2s (peer_id));
1712 GNUNET_array_append (pending_pull_reply_list, pending_pull_reply_list_size, *peer_id);
1714 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
1715 mq = get_mq (peer_map, peer_id);
1716 GNUNET_MQ_send (mq, ev);
1723 * @param peer_id the peer to send the push to.
1726 send_push (struct GNUNET_PeerIdentity *peer_id)
1728 struct GNUNET_MQ_Envelope *ev;
1729 struct GNUNET_MQ_Handle *mq;
1731 LOG (GNUNET_ERROR_TYPE_DEBUG,
1732 "Sending PUSH to peer %s of gossiped list.\n",
1733 GNUNET_i2s (peer_id));
1735 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
1736 mq = get_mq (peer_map, peer_id);
1737 GNUNET_MQ_send (mq, ev);
1742 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1745 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1748 #ifdef ENABLE_MALICIOUS
1750 * Turn RPS service to act malicious.
1752 * @param cls Closure
1753 * @param channel The channel the PUSH was received over
1754 * @param channel_ctx The context associated with this channel
1755 * @param msg The message header
1758 handle_client_act_malicious (void *cls,
1759 struct GNUNET_SERVER_Client *client,
1760 const struct GNUNET_MessageHeader *msg)
1762 struct GNUNET_RPS_CS_ActMaliciousMessage *in_msg;
1763 struct GNUNET_PeerIdentity *peers;
1764 uint32_t num_mal_peers_sent;
1765 uint32_t num_mal_peers_old;
1767 /* Check for protocol violation */
1768 if (sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage) > ntohs (msg->size))
1770 GNUNET_break_op (0);
1773 in_msg = (struct GNUNET_RPS_CS_ActMaliciousMessage *) msg;
1774 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
1775 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1777 LOG (GNUNET_ERROR_TYPE_ERROR,
1778 "message says it sends %" PRIu64 " peers, have space for %i peers\n",
1779 ntohl (in_msg->num_peers),
1780 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
1781 sizeof (struct GNUNET_PeerIdentity));
1782 GNUNET_break_op (0);
1786 /* Do actual logic */
1787 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1788 mal_type = ntohl (in_msg->type);
1790 LOG (GNUNET_ERROR_TYPE_DEBUG,
1791 "Now acting malicious type %" PRIu32 "\n",
1795 { /* Try to maximise representation */
1796 /* Add other malicious peers to those we already know */
1798 num_mal_peers_sent = ntohl (in_msg->num_peers);
1799 num_mal_peers_old = num_mal_peers;
1800 GNUNET_array_grow (mal_peers,
1802 num_mal_peers + num_mal_peers_sent);
1803 memcpy (&mal_peers[num_mal_peers_old],
1805 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1807 /* Add all mal peers to mal_peer_set */
1808 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1812 /* Substitute do_round () with do_mal_round () */
1813 GNUNET_SCHEDULER_cancel (do_round_task);
1814 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1817 else if (2 == mal_type
1819 { /* Try to partition the network */
1820 /* Add other malicious peers to those we already know */
1821 num_mal_peers_sent = ntohl (in_msg->num_peers) - 1;
1822 num_mal_peers_old = num_mal_peers;
1823 GNUNET_array_grow (mal_peers,
1825 num_mal_peers + num_mal_peers_sent);
1826 memcpy (&mal_peers[num_mal_peers_old],
1828 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1830 /* Add all mal peers to mal_peer_set */
1831 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1835 /* Store the one attacked peer */
1836 memcpy (&attacked_peer,
1837 &in_msg->attacked_peer,
1838 sizeof (struct GNUNET_PeerIdentity));
1840 LOG (GNUNET_ERROR_TYPE_DEBUG,
1841 "Attacked peer is %s\n",
1842 GNUNET_i2s (&attacked_peer));
1844 /* Substitute do_round () with do_mal_round () */
1845 GNUNET_SCHEDULER_cancel (do_round_task);
1846 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1848 else if (0 == mal_type)
1849 { /* Stop acting malicious */
1850 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
1852 /* Substitute do_mal_round () with do_round () */
1853 GNUNET_SCHEDULER_cancel (do_round_task);
1854 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
1861 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1866 * Send out PUSHes and PULLs maliciously.
1868 * This is executed regylary.
1871 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1873 uint32_t num_pushes;
1875 struct GNUNET_TIME_Relative time_next_round;
1876 struct AttackedPeer *tmp_att_peer;
1878 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round maliciously.\n");
1880 /* Do malicious actions */
1882 { /* Try to maximise representation */
1884 /* The maximum of pushes we're going to send this round */
1885 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit,
1886 num_attacked_peers),
1887 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
1889 LOG (GNUNET_ERROR_TYPE_DEBUG,
1890 "Going to send %" PRIu32 " pushes\n",
1893 /* Send PUSHes to attacked peers */
1894 for (i = 0 ; i < num_pushes ; i++)
1896 if (att_peers_tail == att_peer_index)
1897 att_peer_index = att_peers_head;
1899 att_peer_index = att_peer_index->next;
1901 send_push (&att_peer_index->peer_id);
1904 /* Send PULLs to some peers to learn about additional peers to attack */
1905 tmp_att_peer = att_peer_index;
1906 for (i = 0 ; i < num_pushes * alpha ; i++)
1908 if (att_peers_tail == tmp_att_peer)
1909 tmp_att_peer = att_peers_head;
1911 att_peer_index = tmp_att_peer->next;
1913 send_pull_request (&tmp_att_peer->peer_id);
1918 else if (2 == mal_type)
1920 * Try to partition the network
1921 * Send as many pushes to the attacked peer as possible
1922 * That is one push per round as it will ignore more.
1924 send_push (&attacked_peer);
1929 { /* Combined attack */
1931 /* The maximum of pushes we're going to send this round */
1932 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit - 1,
1933 num_attacked_peers),
1934 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
1936 LOG (GNUNET_ERROR_TYPE_DEBUG,
1937 "Going to send %" PRIu32 " pushes\n",
1940 /* Send PUSHes to attacked peers */
1941 send_push (&attacked_peer);
1943 for (i = 0 ; i < num_pushes ; i++)
1945 if (att_peers_tail == att_peer_index)
1946 att_peer_index = att_peers_head;
1948 att_peer_index = att_peer_index->next;
1950 send_push (&att_peer_index->peer_id);
1953 /* Send PULLs to some peers to learn about additional peers to attack */
1954 for (i = 0 ; i < num_pushes * alpha ; i++)
1956 if (att_peers_tail == tmp_att_peer)
1957 tmp_att_peer = att_peers_head;
1959 att_peer_index = tmp_att_peer->next;
1961 send_pull_request (&tmp_att_peer->peer_id);
1965 /* Schedule next round */
1966 time_next_round = compute_rand_delay (round_interval, 2);
1968 //do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_mal_round, NULL);
1969 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_mal_round, NULL);
1970 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
1972 #endif /* ENABLE_MALICIOUS */
1976 * Send out PUSHes and PULLs, possibly update #gossip_list, samplers.
1978 * This is executed regylary.
1981 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1983 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round.\n");
1986 unsigned int *permut;
1987 unsigned int n_peers; /* Number of peers we send pushes/pulls to */
1988 struct GNUNET_PeerIdentity peer;
1989 struct GNUNET_PeerIdentity *tmp_peer;
1991 LOG (GNUNET_ERROR_TYPE_DEBUG,
1992 "Printing gossip list:\n");
1993 for (i = 0 ; i < gossip_list_size ; i++)
1995 LOG (GNUNET_ERROR_TYPE_DEBUG,
1996 "\t%s\n", GNUNET_i2s (&gossip_list[i]));
1997 to_file (file_name_view_log,
1999 GNUNET_i2s_full (&gossip_list[i]));
2001 // TODO log lists, ...
2003 /* Would it make sense to have one shuffeled gossip list and then
2004 * to send PUSHes to first alpha peers, PULL requests to next beta peers and
2005 * use the rest to update sampler?
2006 * in essence get random peers with consumption */
2009 if (0 < gossip_list_size)
2011 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2012 (unsigned int) gossip_list_size);
2013 n_peers = ceil (alpha * gossip_list_size);
2014 LOG (GNUNET_ERROR_TYPE_DEBUG,
2015 "Going to send pushes to %u (ceil (%f * %u)) peers.\n",
2016 n_peers, alpha, gossip_list_size);
2017 for (i = 0 ; i < n_peers ; i++)
2019 peer = gossip_list[permut[i]];
2020 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer)) // TODO
2021 { // FIXME if this fails schedule/loop this for later
2025 GNUNET_free (permut);
2029 /* Send PULL requests */
2030 //permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list->size);
2031 n_peers = ceil (beta * gossip_list_size);
2032 LOG (GNUNET_ERROR_TYPE_DEBUG,
2033 "Going to send pulls to %u (ceil (%f * %u)) peers.\n",
2034 n_peers, beta, gossip_list_size);
2035 for (i = 0 ; i < n_peers ; i++)
2037 tmp_peer = get_rand_peer_ignore_list (gossip_list, gossip_list_size,
2038 pending_pull_reply_list, pending_pull_reply_list_size);
2039 if (NULL != tmp_peer)
2042 GNUNET_free (tmp_peer);
2044 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer))
2046 send_pull_request (&peer);
2052 /* Update gossip list */
2053 /* TODO see how many peers are in push-/pull- list! */
2055 if (push_list_size <= alpha * gossip_list_size
2056 && push_list_size > 0
2057 && pull_list_size > 0)
2059 LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the gossip list.\n");
2061 uint32_t first_border;
2062 uint32_t second_border;
2064 uint32_t peers_to_clean_size;
2065 struct GNUNET_PeerIdentity *peers_to_clean;
2067 peers_to_clean = NULL;
2068 peers_to_clean_size = 0;
2069 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, gossip_list_size);
2070 memcpy (peers_to_clean,
2072 gossip_list_size * sizeof (struct GNUNET_PeerIdentity));
2074 first_border = ceil (alpha * sampler_size_est_need);
2075 second_border = first_border + ceil (beta * sampler_size_est_need);
2077 GNUNET_array_grow (gossip_list, gossip_list_size, second_border);
2079 to_file (file_name_view_log,
2082 for (i = 0 ; i < first_border ; i++)
2083 {/* Update gossip list with peers received through PUSHes */
2084 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
2086 gossip_list[i] = push_list[r_index];
2087 to_file (file_name_view_log,
2089 GNUNET_i2s_full (&gossip_list[i]));
2090 // TODO change the peer_flags accordingly
2093 for (i = first_border ; i < second_border ; i++)
2094 {/* Update gossip list with peers received through PULLs */
2095 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
2097 gossip_list[i] = pull_list[r_index];
2098 to_file (file_name_view_log,
2100 GNUNET_i2s_full (&gossip_list[i]));
2101 // TODO change the peer_flags accordingly
2104 for (i = second_border ; i < sampler_size_est_need ; i++)
2105 {/* Update gossip list with peers from history */
2106 RPS_sampler_get_n_rand_peers (prot_sampler,
2110 num_hist_update_tasks++;
2111 // TODO change the peer_flags accordingly
2114 for (i = 0 ; i < gossip_list_size ; i++)
2115 rem_from_list (&peers_to_clean, &peers_to_clean_size, &gossip_list[i]);
2117 for (i = 0 ; i < peers_to_clean_size ; i++)
2119 peer_clean (&peers_to_clean[i]);
2120 /* to_file (file_name_view_log,
2122 GNUNET_i2s_full (&peers_to_clean[i])); */
2125 GNUNET_free (peers_to_clean);
2129 LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the gossip list.\n");
2131 // TODO independent of that also get some peers from CADET_get_peers()?
2133 LOG (GNUNET_ERROR_TYPE_DEBUG,
2134 "Received %u pushes and %u pulls last round (alpha (%.2f) * gossip_list_size (%u) = %.2f)\n",
2139 alpha * gossip_list_size);
2141 /* Update samplers */
2142 for ( i = 0 ; i < push_list_size ; i++ )
2144 LOG (GNUNET_ERROR_TYPE_DEBUG,
2145 "Updating with peer %s from push list\n",
2146 GNUNET_i2s (&push_list[i]));
2147 RPS_sampler_update (prot_sampler, &push_list[i]);
2148 RPS_sampler_update (client_sampler, &push_list[i]);
2149 // TODO set in_flag?
2152 for ( i = 0 ; i < pull_list_size ; i++ )
2154 LOG (GNUNET_ERROR_TYPE_DEBUG,
2155 "Updating with peer %s from pull list\n",
2156 GNUNET_i2s (&pull_list[i]));
2157 RPS_sampler_update (prot_sampler, &pull_list[i]);
2158 RPS_sampler_update (client_sampler, &pull_list[i]);
2159 // TODO set in_flag?
2163 /* Empty push/pull lists */
2164 GNUNET_array_grow (push_list, push_list_size, 0);
2165 GNUNET_array_grow (pull_list, pull_list_size, 0);
2167 struct GNUNET_TIME_Relative time_next_round;
2169 time_next_round = compute_rand_delay (round_interval, 2);
2171 /* Schedule next round */
2172 //do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_round, NULL);
2173 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_round, NULL);
2174 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
2179 rps_start (struct GNUNET_SERVER_Handle *server);
2183 * This is called from GNUNET_CADET_get_peers().
2185 * It is called on every peer(ID) that cadet somehow has contact with.
2186 * We use those to initialise the sampler.
2189 init_peer_cb (void *cls,
2190 const struct GNUNET_PeerIdentity *peer,
2191 int tunnel, // "Do we have a tunnel towards this peer?"
2192 unsigned int n_paths, // "Number of known paths towards this peer"
2193 unsigned int best_path) // "How long is the best path?
2194 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
2198 LOG (GNUNET_ERROR_TYPE_DEBUG,
2199 "Got peer_id %s from cadet\n",
2207 * Clean the send channel of a peer
2210 peer_clean (const struct GNUNET_PeerIdentity *peer)
2212 struct PeerContext *peer_ctx;
2213 struct GNUNET_CADET_Channel *channel;
2215 if (GNUNET_YES != in_arr (gossip_list, gossip_list_size, peer)
2216 && GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
2218 peer_ctx = get_peer_ctx (peer_map, peer);
2219 if (NULL != peer_ctx->send_channel)
2221 channel = peer_ctx->send_channel;
2222 peer_ctx->send_channel = NULL;
2223 GNUNET_CADET_channel_destroy (channel);
2230 * Callback used to remove peers from the multipeermap.
2233 peer_remove_cb (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
2235 struct PeerContext *peer_ctx;
2236 const struct GNUNET_CADET_Channel *channel =
2237 (const struct GNUNET_CADET_Channel *) cls;
2238 struct GNUNET_CADET_Channel *recv;
2239 struct GNUNET_CADET_Channel *send;
2241 peer_ctx = (struct PeerContext *) value;
2243 LOG (GNUNET_ERROR_TYPE_DEBUG,
2244 "Going to clean peer %s\n",
2245 GNUNET_i2s (&peer_ctx->peer_id));
2247 /* If operations are still scheduled for this peer cancel those */
2248 if (0 != peer_ctx->num_outstanding_ops)
2249 GNUNET_array_grow (peer_ctx->outstanding_ops,
2250 peer_ctx->num_outstanding_ops,
2253 /* If we are still waiting for notification whether this peer is live
2254 * cancel the according task */
2255 if (NULL != peer_ctx->is_live_task)
2257 LOG (GNUNET_ERROR_TYPE_DEBUG,
2258 "Trying to cancle is_live_task for peer %s\n",
2260 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
2261 peer_ctx->is_live_task = NULL;
2265 recv = peer_ctx->recv_channel;
2266 peer_ctx->recv_channel = NULL;
2270 GNUNET_CADET_channel_destroy (recv);
2273 /* If there is still a mq destroy it */
2274 if (NULL != peer_ctx->mq)
2276 GNUNET_MQ_destroy (peer_ctx->mq);
2277 peer_ctx->mq = NULL;
2280 /* Remove the send_channel
2281 * The peer map entry should be removed
2282 * from the callback #cleanup_channel */
2283 send = peer_ctx->send_channel;
2284 peer_ctx->send_channel = NULL;
2288 set_peer_flag (peer_ctx, TO_DESTROY);
2289 GNUNET_CADET_channel_destroy (send);
2292 { /* If there is no channel we have to remove it now */
2293 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_remove_all (peer_map, key))
2294 LOG (GNUNET_ERROR_TYPE_WARNING, "removing peer from peer_map failed\n");
2296 GNUNET_free (peer_ctx);
2304 * Task run during shutdown.
2310 shutdown_task (void *cls,
2311 const struct GNUNET_SCHEDULER_TaskContext *tc)
2314 LOG (GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
2316 if (NULL != do_round_task)
2318 GNUNET_SCHEDULER_cancel (do_round_task);
2319 do_round_task = NULL;
2324 if (GNUNET_SYSERR ==
2325 GNUNET_CONTAINER_multipeermap_iterate (peer_map, peer_remove_cb, NULL))
2326 LOG (GNUNET_ERROR_TYPE_WARNING,
2327 "Iterating over peers to disconnect from them was cancelled\n");
2330 GNUNET_NSE_disconnect (nse);
2331 RPS_sampler_destroy (prot_sampler);
2332 RPS_sampler_destroy (client_sampler);
2333 LOG (GNUNET_ERROR_TYPE_DEBUG,
2334 "Size of the peermap: %u\n",
2335 GNUNET_CONTAINER_multipeermap_size (peer_map));
2336 GNUNET_break (0 == GNUNET_CONTAINER_multipeermap_size (peer_map));
2337 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
2338 GNUNET_CADET_disconnect (cadet_handle);
2339 GNUNET_array_grow (gossip_list, gossip_list_size, 0);
2340 GNUNET_array_grow (push_list, push_list_size, 0);
2341 GNUNET_array_grow (pull_list, pull_list_size, 0);
2342 #ifdef ENABLE_MALICIOUS
2343 struct AttackedPeer *tmp_att_peer;
2344 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
2345 if (NULL != mal_peer_set)
2346 GNUNET_CONTAINER_multipeermap_destroy (mal_peer_set);
2347 if (NULL != att_peer_set)
2348 GNUNET_CONTAINER_multipeermap_destroy (att_peer_set);
2349 while (NULL != att_peers_head)
2351 tmp_att_peer = att_peers_head;
2352 GNUNET_CONTAINER_DLL_remove (att_peers_head, att_peers_tail, tmp_att_peer);
2354 #endif /* ENABLE_MALICIOUS */
2359 * A client disconnected. Remove all of its data structure entries.
2361 * @param cls closure, NULL
2362 * @param client identification of the client
2365 handle_client_disconnect (void *cls,
2366 struct GNUNET_SERVER_Client * client)
2372 * Handle the channel a peer opens to us.
2374 * @param cls The closure
2375 * @param channel The channel the peer wants to establish
2376 * @param initiator The peer's peer ID
2377 * @param port The port the channel is being established over
2378 * @param options Further options
2381 handle_inbound_channel (void *cls,
2382 struct GNUNET_CADET_Channel *channel,
2383 const struct GNUNET_PeerIdentity *initiator,
2385 enum GNUNET_CADET_ChannelOption options)
2387 struct PeerContext *peer_ctx;
2388 struct GNUNET_PeerIdentity peer;
2391 LOG (GNUNET_ERROR_TYPE_DEBUG,
2392 "New channel was established to us (Peer %s).\n",
2393 GNUNET_i2s (&peer));
2395 GNUNET_assert (NULL != channel);
2397 // we might not even store the recv_channel
2399 peer_ctx = get_peer_ctx (peer_map, &peer);
2400 // FIXME what do we do if a channel is established twice?
2401 // overwrite? Clean old channel? ...?
2402 //if (NULL != peer_ctx->recv_channel)
2404 // peer_ctx->recv_channel = channel;
2406 peer_ctx->recv_channel = channel;
2408 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, &peer, peer_ctx,
2409 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
2411 peer_is_live (peer_ctx);
2413 return NULL; // TODO
2418 * This is called when a remote peer destroys a channel.
2420 * @param cls The closure
2421 * @param channel The channel being closed
2422 * @param channel_ctx The context associated with this channel
2425 cleanup_channel (void *cls,
2426 const struct GNUNET_CADET_Channel *channel,
2429 struct GNUNET_PeerIdentity *peer;
2430 struct PeerContext *peer_ctx;
2432 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
2433 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
2434 // Guess simply casting isn't the nicest way...
2435 // FIXME wait for cadet to change this function
2437 //if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
2439 peer_ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
2441 if (NULL == peer_ctx) /* It could have been removed by shutdown_task */
2445 if (channel == peer_ctx->send_channel)
2446 { /* Something (but us) killd the channel */
2447 LOG (GNUNET_ERROR_TYPE_DEBUG,
2448 "send channel (%s) was destroyed - cleaning up\n",
2451 rem_from_list (&gossip_list,
2454 rem_from_list (&pending_pull_reply_list,
2455 &pending_pull_reply_list_size,
2457 to_file (file_name_view_log,
2458 "-%s\t(cleanup channel, other peer)",
2459 GNUNET_i2s_full (peer));
2461 peer_ctx->send_channel = NULL;
2462 /* Somwewhat {ab,re}use the iterator function */
2463 /* Cast to void is ok, because it's used as void in peer_remove_cb */
2464 (void) peer_remove_cb ((void *) channel, peer, peer_ctx);
2466 //if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_remove_all (peer_map, key))
2467 // LOG (GNUNET_ERROR_TYPE_WARNING, "Removing peer from peer_map failed\n");
2469 // GNUNET_free (peer_ctx);
2471 else if (channel == peer_ctx->recv_channel)
2472 { /* Other peer doesn't want to send us messages anymore */
2473 LOG (GNUNET_ERROR_TYPE_DEBUG,
2474 "Peer %s destroyed recv channel - cleaning up channel\n",
2476 peer_ctx->recv_channel = NULL;
2478 else if (NULL == peer_ctx->send_channel &&
2479 get_peer_flag (peer_ctx, TO_DESTROY))
2480 { /* We closed the channel to that peer */
2481 LOG (GNUNET_ERROR_TYPE_DEBUG,
2482 "send channel (%s) was destroyed by us - cleaning up\n",
2485 rem_from_list (&gossip_list,
2488 rem_from_list (&pending_pull_reply_list,
2489 &pending_pull_reply_list_size,
2491 to_file (file_name_view_log,
2492 "-%s\t(cleanup channel, ourself)",
2493 GNUNET_i2s_full (peer));
2495 /* Somwewhat {ab,re}use the iterator function */
2496 /* Cast to void is ok, because it's used as void in peer_remove_cb */
2497 (void) peer_remove_cb ((void *) channel, peer, peer_ctx);
2501 LOG (GNUNET_ERROR_TYPE_WARNING,
2502 "unknown channel (%s) was destroyed\n",
2510 * Actually start the service.
2513 rps_start (struct GNUNET_SERVER_Handle *server)
2515 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
2516 {&handle_client_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
2517 sizeof (struct GNUNET_RPS_CS_RequestMessage)},
2518 {&handle_client_seed, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_SEED, 0},
2519 #ifdef ENABLE_MALICIOUS
2520 {&handle_client_act_malicious, NULL, GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS , 0},
2521 #endif /* ENABLE_MALICIOUS */
2525 GNUNET_SERVER_add_handlers (server, handlers);
2526 GNUNET_SERVER_disconnect_notify (server,
2527 &handle_client_disconnect,
2529 LOG (GNUNET_ERROR_TYPE_INFO, "Ready to receive requests from clients\n");
2532 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
2533 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
2535 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2542 * Process statistics requests.
2544 * @param cls closure
2545 * @param server the initialized server
2546 * @param c configuration to use
2550 struct GNUNET_SERVER_Handle *server,
2551 const struct GNUNET_CONFIGURATION_Handle *c)
2556 // TODO check what this does -- copied from gnunet-boss
2557 // - seems to work as expected
2558 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
2563 GNUNET_CRYPTO_get_peer_identity (cfg, &own_identity); // TODO check return value
2564 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2565 "STARTING SERVICE (rps) for peer [%s]\n",
2566 GNUNET_i2s (&own_identity));
2567 #ifdef ENABLE_MALICIOUS
2568 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2569 "Malicious execution compiled in.\n");
2570 #endif /* ENABLE_MALICIOUS */
2574 /* Get time interval from the configuration */
2575 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
2579 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read ROUNDINTERVAL from config\n");
2580 GNUNET_SCHEDULER_shutdown ();
2584 /* Get initial size of sampler/gossip list from the configuration */
2585 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "RPS",
2587 (long long unsigned int *) &sampler_size_est_need))
2589 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read INITSIZE from config\n");
2590 GNUNET_SCHEDULER_shutdown ();
2593 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", sampler_size_est_need);
2597 gossip_list_size = 0;
2599 /* file_name_view_log */
2600 GNUNET_DISK_directory_create ("/tmp/rps/");
2602 size = (14 + strlen (GNUNET_i2s_full (&own_identity)) + 1) * sizeof (char);
2603 file_name_view_log = GNUNET_malloc (size);
2604 out_size = GNUNET_snprintf (file_name_view_log,
2607 GNUNET_i2s_full (&own_identity));
2608 if (size < out_size ||
2611 LOG (GNUNET_ERROR_TYPE_WARNING,
2612 "Failed to write string to buffer (size: %i, out_size: %i)\n",
2618 /* connect to NSE */
2619 nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
2625 peer_map = GNUNET_CONTAINER_multipeermap_create (sampler_size_est_need, GNUNET_NO);
2628 /* Initialise cadet */
2629 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
2630 {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH ,
2631 sizeof (struct GNUNET_MessageHeader)},
2632 {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
2633 sizeof (struct GNUNET_MessageHeader)},
2634 {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY , 0},
2638 const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
2639 cadet_handle = GNUNET_CADET_connect (cfg,
2641 &handle_inbound_channel,
2647 /* Initialise sampler */
2648 struct GNUNET_TIME_Relative half_round_interval;
2649 struct GNUNET_TIME_Relative max_round_interval;
2651 half_round_interval = GNUNET_TIME_relative_multiply (round_interval, .5);
2652 max_round_interval = GNUNET_TIME_relative_add (round_interval, half_round_interval);
2654 prot_sampler = RPS_sampler_init (sampler_size_est_need, max_round_interval);
2655 client_sampler = RPS_sampler_mod_init (sampler_size_est_need, max_round_interval);
2657 /* Initialise push and pull maps */
2662 pending_pull_reply_list = NULL;
2663 pending_pull_reply_list_size = 0;
2666 num_hist_update_tasks = 0;
2669 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
2670 GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, NULL);
2671 // TODO send push/pull to each of those peers?
2679 * The main function for the rps service.
2681 * @param argc number of arguments from the command line
2682 * @param argv command line arguments
2683 * @return 0 ok, 1 on error
2686 main (int argc, char *const *argv)
2688 return (GNUNET_OK ==
2689 GNUNET_SERVICE_run (argc,
2692 GNUNET_SERVICE_OPTION_NONE,
2693 &run, NULL)) ? 0 : 1;
2696 /* end of gnunet-service-rps.c */