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_peerinfo_service.h"
30 #include "gnunet_nse_service.h"
32 #include "rps-test_util.h"
34 #include "gnunet-service-rps_sampler.h"
39 #define LOG(kind, ...) GNUNET_log(kind, __VA_ARGS__)
41 // TODO modify @brief in every file
43 // TODO check for overflows
45 // TODO align message structs
47 // TODO connect to friends
49 // TODO store peers somewhere persistent
51 // TODO blacklist? (-> mal peer detection on top of brahms)
53 // TODO API request_cancel
55 // hist_size_init, hist_size_max
60 static const struct GNUNET_CONFIGURATION_Handle *cfg;
65 static struct GNUNET_PeerIdentity own_identity;
68 struct GNUNET_PeerIdentity *
69 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list, unsigned int size,
70 const struct GNUNET_PeerIdentity *ignore_list, unsigned int ignore_size);
73 /***********************************************************************
74 * Housekeeping with peers
75 ***********************************************************************/
78 * Struct used to store the context of a connected client.
83 * The message queue to communicate with the client.
85 struct GNUNET_MQ_Handle *mq;
89 * Used to keep track in what lists single peerIDs are.
93 PULL_REPLY_PENDING = 0x01,
94 IN_OTHER_GOSSIP_LIST = 0x02, // unneeded?
95 IN_OWN_SAMPLER_LIST = 0x04, // unneeded?
96 IN_OWN_GOSSIP_LIST = 0x08, // unneeded?
99 * We set this bit when we can be sure the other peer is/was live.
104 * We set this bit when we are going to destroy the channel to this peer.
105 * When cleanup_channel is called, we know that we wanted to destroy it.
106 * Otherwise the channel to the other peer was destroyed.
113 * Functions of this type can be used to be stored at a peer for later execution.
115 typedef void (* PeerOp) (void *cls, const struct GNUNET_PeerIdentity *peer);
118 * Outstanding operation on peer consisting of callback and closure
120 struct PeerOutstandingOp
135 * Struct used to keep track of other peer's status
137 * This is stored in a multipeermap.
142 * In own gossip/sampler list, in other's gossip/sampler list
147 * Message queue open to client
149 struct GNUNET_MQ_Handle *mq;
152 * Channel open to client.
154 struct GNUNET_CADET_Channel *send_channel;
157 * Channel open from client.
159 struct GNUNET_CADET_Channel *recv_channel; // unneeded?
162 * Array of outstanding operations on this peer.
164 struct PeerOutstandingOp *outstanding_ops;
167 * Number of outstanding operations.
169 unsigned int num_outstanding_ops;
170 //size_t num_outstanding_ops;
173 * Handle to the callback given to cadet_ntfy_tmt_rdy()
175 * To be canceled on shutdown.
177 struct GNUNET_CADET_TransmitHandle *is_live_task;
180 * Identity of the peer
182 struct GNUNET_PeerIdentity peer_id;
185 * This is pobably followed by 'statistical' data (when we first saw
186 * him, how did we get his ID, how many pushes (in a timeinterval),
191 /***********************************************************************
192 * /Housekeeping with peers
193 ***********************************************************************/
199 /***********************************************************************
201 ***********************************************************************/
204 * Sampler used for the Brahms protocol itself.
206 static struct RPS_Sampler *prot_sampler;
209 * Sampler used for the clients.
211 static struct RPS_Sampler *client_sampler;
214 * Set of all peers to keep track of them.
216 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
220 * The gossiped list of peers.
222 static struct GNUNET_PeerIdentity *gossip_list;
225 * Size of the gossiped list
227 //static unsigned int gossip_list_size;
228 static uint32_t gossip_list_size;
231 * Name to log view (gossip_list) to
233 static char *file_name_view_log;
237 * The size of sampler we need to be able to satisfy the client's need of
240 static unsigned int sampler_size_client_need;
243 * The size of sampler we need to be able to satisfy the Brahms protocol's
244 * need of random peers.
246 * This is directly taken as the #gossip_list_size on update of the
249 * This is one minimum size the sampler grows to.
251 static unsigned int sampler_size_est_need;
255 * Percentage of total peer number in the gossip list
256 * to send random PUSHes to
261 * Percentage of total peer number in the gossip list
262 * to send random PULLs to
267 * The percentage gamma of history updates.
268 * Simply 1 - alpha - beta
273 * Identifier for the main task that runs periodically.
275 static struct GNUNET_SCHEDULER_Task *do_round_task;
278 * Time inverval the do_round task runs in.
280 static struct GNUNET_TIME_Relative round_interval;
285 * List to store peers received through pushes temporary.
287 * TODO -> multipeermap
289 static struct GNUNET_PeerIdentity *push_list;
292 * Size of the push_list;
294 static unsigned int push_list_size;
295 //size_t push_list_size;
298 * List to store peers received through pulls temporary.
300 * TODO -> multipeermap
302 static struct GNUNET_PeerIdentity *pull_list;
305 * Size of the pull_list;
307 static unsigned int pull_list_size;
308 //size_t pull_list_size;
314 static struct GNUNET_NSE_Handle *nse;
319 static struct GNUNET_CADET_Handle *cadet_handle;
322 * Handler to PEERINFO.
324 static struct GNUNET_PEERINFO_Handle *peerinfo_handle;
327 * Handle for cancellation of iteration over peers.
329 struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify_handle;
335 * Only needed in the beginning to check how many of the 64 deltas
338 static unsigned int req_counter;
341 * Time of the last request we received.
343 * Used to compute the expected request rate.
345 static struct GNUNET_TIME_Absolute last_request;
348 * Size of #request_deltas.
350 #define REQUEST_DELTAS_SIZE 64
351 static unsigned int request_deltas_size = REQUEST_DELTAS_SIZE;
354 * Last 64 deltas between requests
356 static struct GNUNET_TIME_Relative request_deltas[REQUEST_DELTAS_SIZE];
359 * The prediction of the rate of requests
361 static struct GNUNET_TIME_Relative request_rate;
365 * List with the peers we sent requests to.
367 struct GNUNET_PeerIdentity *pending_pull_reply_list;
370 * Size of #pending_pull_reply_list.
372 uint32_t pending_pull_reply_list_size;
376 * Number of history update tasks.
378 uint32_t num_hist_update_tasks;
382 * Closure used to pass the client and the id to the callback
383 * that replies to a client's request
388 * The identifier of the request
393 * The client handle to send the reply to
395 struct GNUNET_SERVER_Client *client;
399 #ifdef ENABLE_MALICIOUS
401 * Type of malicious peer
403 * 0 Don't act malicious at all - Default
404 * 1 Try to maximise representation
405 * 2 Try to partition the network
408 uint32_t mal_type = 0;
411 * Other malicious peers
413 static struct GNUNET_PeerIdentity *mal_peers = NULL;
416 * Hashmap of malicious peers used as set.
417 * Used to more efficiently check whether we know that peer.
419 static struct GNUNET_CONTAINER_MultiPeerMap *mal_peer_set = NULL;
422 * Number of other malicious peers
424 static uint32_t num_mal_peers = 0;
428 * If type is 2 This struct is used to store the attacked peers in a DLL
435 struct AttackedPeer *next;
436 struct AttackedPeer *prev;
441 struct GNUNET_PeerIdentity peer_id;
445 * If type is 2 this is the DLL of attacked peers
447 static struct AttackedPeer *att_peers_head = NULL;
448 static struct AttackedPeer *att_peers_tail = NULL;
451 * This index is used to point to an attacked peer to
452 * implement the round-robin-ish way to select attacked peers.
454 static struct AttackedPeer *att_peer_index = NULL;
457 * Hashmap of attacked peers used as set.
458 * Used to more efficiently check whether we know that peer.
460 static struct GNUNET_CONTAINER_MultiPeerMap *att_peer_set = NULL;
463 * Number of attacked peers
465 static uint32_t num_attacked_peers = 0;
469 * If type is 1 this is the attacked peer
471 static struct GNUNET_PeerIdentity attacked_peer;
474 * The limit of PUSHes we can send in one round.
475 * This is an assumption of the Brahms protocol and either implemented
478 * assumend to be the bandwidth limitation.
480 static uint32_t push_limit = 10000;
481 #endif /* ENABLE_MALICIOUS */
484 /***********************************************************************
486 ***********************************************************************/
493 /***********************************************************************
495 ***********************************************************************/
498 * Set a peer flag of given peer context.
500 #define set_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags |= mask)
503 * Get peer flag of given peer context.
505 #define get_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags & mask ? GNUNET_YES : GNUNET_NO)
508 * Unset flag of given peer context.
510 #define unset_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags &= (~mask))
513 * Clean the send channel of a peer
516 peer_clean (const struct GNUNET_PeerIdentity *peer);
520 * Check if peer is already in peer array.
523 in_arr (const struct GNUNET_PeerIdentity *array,
524 unsigned int arr_size,
525 const struct GNUNET_PeerIdentity *peer)
527 GNUNET_assert (NULL != peer);
532 GNUNET_assert (NULL != array);
536 for (i = 0; i < arr_size ; i++)
537 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&array[i], peer))
544 * Print peerlist to log.
547 print_peer_list (struct GNUNET_PeerIdentity *list, unsigned int len)
551 LOG (GNUNET_ERROR_TYPE_DEBUG,
552 "Printing peer list of length %u at %p:\n",
555 for (i = 0 ; i < len ; i++)
557 LOG (GNUNET_ERROR_TYPE_DEBUG,
559 i, GNUNET_i2s (&list[i]));
565 * Remove peer from list.
568 rem_from_list (struct GNUNET_PeerIdentity **peer_list,
569 unsigned int *list_size,
570 const struct GNUNET_PeerIdentity *peer)
573 struct GNUNET_PeerIdentity *tmp;
577 LOG (GNUNET_ERROR_TYPE_DEBUG,
578 "Removing peer %s from list at %p\n",
582 for ( i = 0 ; i < *list_size ; i++ )
584 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&tmp[i], peer))
586 if (i < *list_size -1)
587 { /* Not at the last entry -- shift peers left */
588 memcpy (&tmp[i], &tmp[i +1],
589 ((*list_size) - i -1) * sizeof (struct GNUNET_PeerIdentity));
591 /* Remove last entry (should be now useless PeerID) */
592 GNUNET_array_grow (tmp, *list_size, (*list_size) -1);
599 * Get random peer from the given list but don't return one from the @a ignore_list.
601 struct GNUNET_PeerIdentity *
602 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list,
604 const struct GNUNET_PeerIdentity *ignore_list,
605 uint32_t ignore_size)
609 struct GNUNET_PeerIdentity *tmp_peer_list;
610 struct GNUNET_PeerIdentity *peer;
612 GNUNET_assert (NULL != peer_list);
617 tmp_peer_list = NULL;
618 GNUNET_array_grow (tmp_peer_list, tmp_size, list_size);
619 memcpy (tmp_peer_list,
621 list_size * sizeof (struct GNUNET_PeerIdentity));
622 peer = GNUNET_new (struct GNUNET_PeerIdentity);
625 * Choose the r_index of the peer we want to return
626 * at random from the interval of the gossip list
628 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
630 *peer = tmp_peer_list[r_index];
632 while (in_arr (ignore_list, ignore_size, peer))
634 rem_from_list (&tmp_peer_list, &tmp_size, peer);
636 print_peer_list (tmp_peer_list, tmp_size);
645 * Choose the r_index of the peer we want to return
646 * at random from the interval of the gossip list
648 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
650 *peer = tmp_peer_list[r_index];
654 GNUNET_array_grow (tmp_peer_list, tmp_size, 0);
661 * Get the context of a peer. If not existing, create.
664 get_peer_ctx (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
665 const struct GNUNET_PeerIdentity *peer)
667 struct PeerContext *ctx;
669 if ( GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
671 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
675 ctx = GNUNET_new (struct PeerContext);
678 ctx->send_channel = NULL;
679 ctx->recv_channel = NULL;
680 ctx->outstanding_ops = NULL;
681 ctx->num_outstanding_ops = 0;
682 ctx->is_live_task = NULL;
683 ctx->peer_id = *peer;
684 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
685 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
692 * Put random peer from sampler into the gossip list as history update.
695 hist_update (void *cls, struct GNUNET_PeerIdentity *ids, uint32_t num_peers)
697 GNUNET_assert (1 == num_peers);
699 if (gossip_list_size < sampler_size_est_need)
701 GNUNET_array_append (gossip_list, gossip_list_size, *ids);
702 to_file (file_name_view_log,
704 GNUNET_i2s_full (ids));
707 if (0 < num_hist_update_tasks)
708 num_hist_update_tasks--;
713 * Set the peer flag to living and call the outstanding operations on this peer.
716 peer_is_live (struct PeerContext *peer_ctx)
718 struct GNUNET_PeerIdentity *peer;
720 /* Cancle is_live_task if still scheduled */
721 if (NULL != peer_ctx->is_live_task)
723 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
724 peer_ctx->is_live_task = NULL;
727 peer = &peer_ctx->peer_id;
728 set_peer_flag (peer_ctx, VALID);
730 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer %s is live\n", GNUNET_i2s (peer));
732 if (0 < peer_ctx->num_outstanding_ops)
733 { /* Call outstanding operations */
736 for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
737 peer_ctx->outstanding_ops[i].op (peer_ctx->outstanding_ops[i].op_cls, peer);
738 GNUNET_array_grow (peer_ctx->outstanding_ops, peer_ctx->num_outstanding_ops, 0);
746 * Callback that is called when a channel was effectively established.
747 * This is given to ntfy_tmt_rdy and called when the channel was
748 * successfully established.
751 cadet_ntfy_tmt_rdy_cb (void *cls, size_t size, void *buf)
753 struct PeerContext *peer_ctx = (struct PeerContext *) cls;
755 peer_ctx->is_live_task = NULL;
756 LOG (GNUNET_ERROR_TYPE_DEBUG,
757 "Set ->is_live_task = NULL for peer %s\n",
758 GNUNET_i2s (&peer_ctx->peer_id));
763 peer_is_live (peer_ctx);
767 LOG (GNUNET_ERROR_TYPE_WARNING,
768 "Problems establishing a connection to peer %s in order to check liveliness\n",
769 GNUNET_i2s (&peer_ctx->peer_id));
770 // TODO reschedule? cleanup?
773 //if (NULL != peer_ctx->is_live_task)
775 // LOG (GNUNET_ERROR_TYPE_DEBUG,
776 // "Trying to cancle is_live_task for peer %s\n",
777 // GNUNET_i2s (&peer_ctx->peer_id));
778 // GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
779 // peer_ctx->is_live_task = NULL;
787 * Get the channel of a peer. If not existing, create.
789 struct GNUNET_CADET_Channel *
790 get_channel (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
791 const struct GNUNET_PeerIdentity *peer)
793 struct PeerContext *peer_ctx;
795 peer_ctx = get_peer_ctx (peer_map, peer);
797 if (NULL == peer_ctx->send_channel)
799 LOG (GNUNET_ERROR_TYPE_DEBUG,
800 "Trying to establish channel to peer %s\n",
803 peer_ctx->send_channel =
804 GNUNET_CADET_channel_create (cadet_handle,
807 GNUNET_RPS_CADET_PORT,
808 GNUNET_CADET_OPTION_RELIABLE);
810 // do I have to explicitly put it in the peer_map?
811 (void) GNUNET_CONTAINER_multipeermap_put
815 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
817 return peer_ctx->send_channel;
822 * Get the message queue of a specific peer.
824 * If we already have a message queue open to this client,
825 * simply return it, otherways create one.
827 struct GNUNET_MQ_Handle *
828 get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
829 const struct GNUNET_PeerIdentity *peer_id)
831 struct PeerContext *peer_ctx;
833 peer_ctx = get_peer_ctx (peer_map, peer_id);
835 GNUNET_assert (NULL == peer_ctx->is_live_task);
837 if (NULL == peer_ctx->mq)
839 (void) get_channel (peer_map, peer_id);
840 peer_ctx->mq = GNUNET_CADET_mq_create (peer_ctx->send_channel);
841 //do I have to explicitly put it in the peer_map?
842 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer_id, peer_ctx,
843 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
850 * Issue check whether peer is live
852 * @param peer_ctx the context of the peer
855 check_peer_live (struct PeerContext *peer_ctx)
857 (void) get_channel (peer_map, &peer_ctx->peer_id);
858 LOG (GNUNET_ERROR_TYPE_DEBUG,
859 "Get informed about peer %s getting live\n",
860 GNUNET_i2s (&peer_ctx->peer_id));
861 if (NULL == peer_ctx->is_live_task)
863 peer_ctx->is_live_task =
864 GNUNET_CADET_notify_transmit_ready (peer_ctx->send_channel,
866 GNUNET_TIME_UNIT_FOREVER_REL,
867 sizeof (struct GNUNET_MessageHeader),
868 cadet_ntfy_tmt_rdy_cb,
873 LOG (GNUNET_ERROR_TYPE_DEBUG,
874 "Already waiting for notification\n");
880 * Sum all time relatives of an array.
882 struct GNUNET_TIME_Relative
883 T_relative_sum (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
885 struct GNUNET_TIME_Relative sum;
888 sum = GNUNET_TIME_UNIT_ZERO;
889 for ( i = 0 ; i < arr_size ; i++ )
891 sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
898 * Compute the average of given time relatives.
900 struct GNUNET_TIME_Relative
901 T_relative_avg (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
903 return GNUNET_TIME_relative_divide (T_relative_sum (rel_array, arr_size), arr_size);
908 * Insert PeerID in #pull_list
910 * Called once we know a peer is live.
913 insert_in_pull_list (void *cls, const struct GNUNET_PeerIdentity *peer)
915 if (GNUNET_NO == in_arr (pull_list, pull_list_size, peer))
916 GNUNET_array_append (pull_list, pull_list_size, *peer);
922 * Check whether #insert_in_pull_list was already scheduled
925 insert_in_pull_list_scheduled (const struct PeerContext *peer_ctx)
929 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
930 if (insert_in_pull_list == peer_ctx->outstanding_ops[i].op)
937 * Insert PeerID in #gossip_list
939 * Called once we know a peer is live.
942 insert_in_gossip_list (void *cls, const struct GNUNET_PeerIdentity *peer)
944 if (GNUNET_NO == in_arr (gossip_list, gossip_list_size, peer))
946 GNUNET_array_append (gossip_list, gossip_list_size, *peer);
947 to_file (file_name_view_log,
948 "+%s\t(ins in gossip list)",
949 GNUNET_i2s_full (peer));
952 (void) get_channel (peer_map, peer);
956 * Check whether #insert_in_gossip_list was already scheduled
959 insert_in_gossip_list_scheduled (const struct PeerContext *peer_ctx)
963 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
964 if (insert_in_gossip_list == peer_ctx->outstanding_ops[i].op)
971 * Update sampler with given PeerID.
974 insert_in_sampler (void *cls, const struct GNUNET_PeerIdentity *peer)
976 LOG (GNUNET_ERROR_TYPE_DEBUG,
977 "Updating samplers with peer %s from insert_in_sampler()\n",
979 RPS_sampler_update (prot_sampler, peer);
980 RPS_sampler_update (client_sampler, peer);
985 * Check whether #insert_in_sampler was already scheduled
988 insert_in_sampler_scheduled (const struct PeerContext *peer_ctx)
992 for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
993 if (insert_in_sampler== peer_ctx->outstanding_ops[i].op)
1000 * Wrapper around #RPS_sampler_resize()
1002 * If we do not have enough sampler elements, double current sampler size
1003 * If we have more than enough sampler elements, halv current sampler size
1006 resize_wrapper (struct RPS_Sampler *sampler, uint32_t new_size)
1008 unsigned int sampler_size;
1011 // TODO respect the min, max
1012 sampler_size = RPS_sampler_get_size (sampler);
1013 if (sampler_size > new_size * 4)
1015 RPS_sampler_resize (sampler, sampler_size / 2);
1017 else if (sampler_size < new_size)
1019 RPS_sampler_resize (sampler, sampler_size * 2);
1021 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
1026 * Wrapper around #RPS_sampler_resize() resizing the client sampler
1029 client_resize_wrapper ()
1031 uint32_t bigger_size;
1032 unsigned int sampler_size;
1036 sampler_size = RPS_sampler_get_size (client_sampler);
1038 if (sampler_size_est_need > sampler_size_client_need)
1039 bigger_size = sampler_size_est_need;
1041 bigger_size = sampler_size_client_need;
1043 // TODO respect the min, max
1044 resize_wrapper (client_sampler, bigger_size);
1045 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
1050 * Estimate request rate
1052 * Called every time we receive a request from the client.
1057 struct GNUNET_TIME_Relative max_round_duration;
1059 if (request_deltas_size > req_counter)
1061 if ( 1 < req_counter)
1063 /* Shift last request deltas to the right */
1064 memcpy (&request_deltas[1],
1066 (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
1068 /* Add current delta to beginning */
1070 GNUNET_TIME_absolute_get_difference (last_request,
1071 GNUNET_TIME_absolute_get ());
1072 request_rate = T_relative_avg (request_deltas, req_counter);
1074 /* Compute the duration a round will maximally take */
1075 max_round_duration =
1076 GNUNET_TIME_relative_add (round_interval,
1077 GNUNET_TIME_relative_divide (round_interval, 2));
1079 /* Set the estimated size the sampler has to have to
1080 * satisfy the current client request rate */
1081 sampler_size_client_need =
1082 max_round_duration.rel_value_us / request_rate.rel_value_us;
1084 /* Resize the sampler */
1085 client_resize_wrapper ();
1087 last_request = GNUNET_TIME_absolute_get ();
1092 * Add all peers in @a peer_array to @a peer_map used as set.
1094 * @param peer_array array containing the peers
1095 * @param num_peers number of peers in @peer_array
1096 * @param peer_map the peermap to use as set
1099 add_peer_array_to_set (const struct GNUNET_PeerIdentity *peer_array,
1100 unsigned int num_peers,
1101 struct GNUNET_CONTAINER_MultiPeerMap *peer_map)
1104 if (NULL == peer_map)
1106 LOG (GNUNET_ERROR_TYPE_WARNING,
1107 "Trying to add peers to an empty peermap.\n");
1111 for (i = 0 ; i < num_peers ; i++)
1113 GNUNET_CONTAINER_multipeermap_put (peer_map,
1116 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1122 * Send a PULL REPLY to @a peer_id
1124 * @param peer_id the peer to send the reply to.
1125 * @param peer_ids the peers to send to @a peer_id
1126 * @param num_peer_ids the number of peers to send to @a peer_id
1129 send_pull_reply (const struct GNUNET_PeerIdentity *peer_id,
1130 const struct GNUNET_PeerIdentity *peer_ids,
1131 unsigned int num_peer_ids)
1134 struct GNUNET_MQ_Handle *mq;
1135 struct GNUNET_MQ_Envelope *ev;
1136 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
1138 /* Compute actual size */
1139 send_size = sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) +
1140 num_peer_ids * sizeof (struct GNUNET_PeerIdentity);
1142 if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size)
1143 /* Compute number of peers to send
1144 * If too long, simply truncate */
1145 // TODO select random ones via permutation
1146 // or even better: do good protocol design
1148 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE -
1149 sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1150 sizeof (struct GNUNET_PeerIdentity);
1152 send_size = num_peer_ids;
1154 LOG (GNUNET_ERROR_TYPE_DEBUG,
1155 "PULL REQUEST from peer %s received, going to send %u peers\n",
1156 GNUNET_i2s (peer_id), send_size);
1158 mq = get_mq (peer_map, peer_id);
1160 ev = GNUNET_MQ_msg_extra (out_msg,
1161 send_size * sizeof (struct GNUNET_PeerIdentity),
1162 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
1163 out_msg->num_peers = htonl (send_size);
1164 memcpy (&out_msg[1], peer_ids,
1165 send_size * sizeof (struct GNUNET_PeerIdentity));
1167 GNUNET_MQ_send (mq, ev);
1172 * This function is called on new peer_ids from 'external' sources
1173 * (client seed, cadet get_peers(), ...)
1175 * @param peer_id the new peer_id
1178 new_peer_id (const struct GNUNET_PeerIdentity *peer_id)
1180 struct PeerOutstandingOp out_op;
1181 struct PeerContext *peer_ctx;
1184 && 0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, peer_id))
1186 LOG (GNUNET_ERROR_TYPE_DEBUG,
1187 "Got peer_id %s (at %p, gossip_list_size: %u)\n",
1188 GNUNET_i2s (peer_id),
1192 peer_ctx = get_peer_ctx (peer_map, peer_id);
1193 if (GNUNET_YES != get_peer_flag (peer_ctx, VALID))
1195 if (GNUNET_NO == insert_in_sampler_scheduled (peer_ctx))
1197 out_op.op = insert_in_sampler;
1198 out_op.op_cls = NULL;
1199 GNUNET_array_append (peer_ctx->outstanding_ops,
1200 peer_ctx->num_outstanding_ops,
1204 if (GNUNET_NO == insert_in_gossip_list_scheduled (peer_ctx))
1206 out_op.op = insert_in_gossip_list;
1207 out_op.op_cls = NULL;
1208 GNUNET_array_append (peer_ctx->outstanding_ops,
1209 peer_ctx->num_outstanding_ops,
1213 /* Trigger livelyness test on peer */
1214 check_peer_live (peer_ctx);
1218 // send push/pull to each of those peers?
1223 /***********************************************************************
1225 ***********************************************************************/
1232 * Function called by NSE.
1234 * Updates sizes of sampler list and gossip list and adapt those lists
1238 nse_callback (void *cls, struct GNUNET_TIME_Absolute timestamp,
1239 double logestimate, double std_dev)
1242 //double scale; // TODO this might go gloabal/config
1244 LOG (GNUNET_ERROR_TYPE_DEBUG,
1245 "Received a ns estimate - logest: %f, std_dev: %f (old_size: %u)\n",
1246 logestimate, std_dev, RPS_sampler_get_size (prot_sampler));
1248 estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
1249 // GNUNET_NSE_log_estimate_to_n (logestimate);
1250 estimate = pow (estimate, 1.0 / 3);
1251 // TODO add if std_dev is a number
1252 // estimate += (std_dev * scale);
1253 if (2 < ceil (estimate))
1255 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
1256 sampler_size_est_need = estimate;
1258 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
1260 /* If the NSE has changed adapt the lists accordingly */
1261 resize_wrapper (prot_sampler, sampler_size_est_need);
1262 client_resize_wrapper ();
1267 * Callback called once the requested PeerIDs are ready.
1269 * Sends those to the requesting client.
1271 void client_respond (void *cls,
1272 struct GNUNET_PeerIdentity *peer_ids, uint32_t num_peers)
1274 struct GNUNET_MQ_Envelope *ev;
1275 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
1276 struct ReplyCls *reply_cls = (struct ReplyCls *) cls;
1277 uint32_t size_needed;
1278 struct ClientContext *cli_ctx;
1280 LOG (GNUNET_ERROR_TYPE_DEBUG,
1281 "sampler returned %" PRIu32 " peers\n",
1284 size_needed = sizeof (struct GNUNET_RPS_CS_ReplyMessage) +
1285 num_peers * sizeof (struct GNUNET_PeerIdentity);
1287 GNUNET_assert (GNUNET_SERVER_MAX_MESSAGE_SIZE >= size_needed);
1289 ev = GNUNET_MQ_msg_extra (out_msg,
1290 num_peers * sizeof (struct GNUNET_PeerIdentity),
1291 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
1292 out_msg->num_peers = htonl (num_peers);
1293 out_msg->id = htonl (reply_cls->id);
1295 memcpy (&out_msg[1],
1297 num_peers * sizeof (struct GNUNET_PeerIdentity));
1298 GNUNET_free (peer_ids);
1300 cli_ctx = GNUNET_SERVER_client_get_user_context (reply_cls->client, struct ClientContext);
1301 if (NULL == cli_ctx) {
1302 cli_ctx = GNUNET_new (struct ClientContext);
1303 cli_ctx->mq = GNUNET_MQ_queue_for_server_client (reply_cls->client);
1304 GNUNET_SERVER_client_set_user_context (reply_cls->client, cli_ctx);
1307 GNUNET_free (reply_cls);
1309 GNUNET_MQ_send (cli_ctx->mq, ev);
1314 * Handle RPS request from the client.
1316 * @param cls closure
1317 * @param client identification of the client
1318 * @param message the actual message
1321 handle_client_request (void *cls,
1322 struct GNUNET_SERVER_Client *client,
1323 const struct GNUNET_MessageHeader *message)
1325 struct GNUNET_RPS_CS_RequestMessage *msg;
1327 uint32_t size_needed;
1328 struct ReplyCls *reply_cls;
1331 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
1333 num_peers = ntohl (msg->num_peers);
1334 size_needed = sizeof (struct GNUNET_RPS_CS_RequestMessage) +
1335 num_peers * sizeof (struct GNUNET_PeerIdentity);
1337 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
1339 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1343 for (i = 0 ; i < num_peers ; i++)
1346 LOG (GNUNET_ERROR_TYPE_DEBUG,
1347 "Client requested %" PRIu32 " random peer(s).\n",
1350 reply_cls = GNUNET_new (struct ReplyCls);
1351 reply_cls->id = ntohl (msg->id);
1352 reply_cls->client = client;
1354 RPS_sampler_get_n_rand_peers (client_sampler,
1359 GNUNET_SERVER_receive_done (client,
1365 * Handle seed from the client.
1367 * @param cls closure
1368 * @param client identification of the client
1369 * @param message the actual message
1372 handle_client_seed (void *cls,
1373 struct GNUNET_SERVER_Client *client,
1374 const struct GNUNET_MessageHeader *message)
1376 struct GNUNET_RPS_CS_SeedMessage *in_msg;
1377 struct GNUNET_PeerIdentity *peers;
1381 if (sizeof (struct GNUNET_RPS_CS_SeedMessage) > ntohs (message->size))
1383 GNUNET_break_op (0);
1384 GNUNET_SERVER_receive_done (client,
1388 in_msg = (struct GNUNET_RPS_CS_SeedMessage *) message;
1389 num_peers = ntohl (in_msg->num_peers);
1390 peers = (struct GNUNET_PeerIdentity *) &in_msg[1];
1391 //peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1392 //memcpy (peers, &in_msg[1], num_peers * sizeof (struct GNUNET_PeerIdentity));
1394 if ((ntohs (message->size) - sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
1395 sizeof (struct GNUNET_PeerIdentity) != num_peers)
1397 GNUNET_break_op (0);
1398 GNUNET_SERVER_receive_done (client,
1402 LOG (GNUNET_ERROR_TYPE_DEBUG,
1403 "Client seeded peers:\n");
1404 print_peer_list (peers, num_peers);
1406 for (i = 0 ; i < num_peers ; i++)
1408 LOG (GNUNET_ERROR_TYPE_DEBUG,
1409 "Updating samplers with seed %" PRIu32 ": %s\n",
1411 GNUNET_i2s (&peers[i]));
1413 new_peer_id (&peers[i]);
1415 //RPS_sampler_update (prot_sampler, &peers[i]);
1416 //RPS_sampler_update (client_sampler, &peers[i]);
1419 ////GNUNET_free (peers);
1421 GNUNET_SERVER_receive_done (client,
1427 * Handle a PUSH message from another peer.
1429 * Check the proof of work and store the PeerID
1430 * in the temporary list for pushed PeerIDs.
1432 * @param cls Closure
1433 * @param channel The channel the PUSH was received over
1434 * @param channel_ctx The context associated with this channel
1435 * @param msg The message header
1438 handle_peer_push (void *cls,
1439 struct GNUNET_CADET_Channel *channel,
1441 const struct GNUNET_MessageHeader *msg)
1443 const struct GNUNET_PeerIdentity *peer;
1445 // (check the proof of work)
1447 peer = (const struct GNUNET_PeerIdentity *)
1448 GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
1449 // FIXME wait for cadet to change this function
1451 LOG (GNUNET_ERROR_TYPE_DEBUG, "PUSH received (%s)\n", GNUNET_i2s (peer));
1453 #ifdef ENABLE_MALICIOUS
1454 struct AttackedPeer *tmp_att_peer;
1456 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1457 memcpy (&tmp_att_peer->peer_id, peer, sizeof (struct GNUNET_PeerIdentity));
1460 { /* Try to maximise representation */
1461 if (NULL == att_peer_set)
1462 att_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
1463 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1466 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1469 add_peer_array_to_set (peer, 1, att_peer_set);
1475 else if (2 == mal_type)
1476 { /* We attack one single well-known peer - simply ignore */
1481 GNUNET_free (tmp_att_peer);
1484 #endif /* ENABLE_MALICIOUS */
1486 /* Add the sending peer to the push_list */
1487 if (GNUNET_NO == in_arr (push_list, push_list_size, peer))
1488 GNUNET_array_append (push_list, push_list_size, *peer);
1495 * Handle PULL REQUEST request message from another peer.
1497 * Reply with the gossip list of PeerIDs.
1499 * @param cls Closure
1500 * @param channel The channel the PUSH was received over
1501 * @param channel_ctx The context associated with this channel
1502 * @param msg The message header
1505 handle_peer_pull_request (void *cls,
1506 struct GNUNET_CADET_Channel *channel,
1508 const struct GNUNET_MessageHeader *msg)
1510 struct GNUNET_PeerIdentity *peer;
1512 peer = (struct GNUNET_PeerIdentity *)
1513 GNUNET_CADET_channel_get_info (channel,
1514 GNUNET_CADET_OPTION_PEER);
1515 // FIXME wait for cadet to change this function
1517 #ifdef ENABLE_MALICIOUS
1520 { /* Try to maximise representation */
1521 send_pull_reply (peer, mal_peers, num_mal_peers);
1525 else if (2 == mal_type)
1526 { /* Try to partition network */
1527 if (GNUNET_YES == GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
1529 send_pull_reply (peer, mal_peers, num_mal_peers);
1533 #endif /* ENABLE_MALICIOUS */
1535 send_pull_reply (peer, gossip_list, gossip_list_size);
1542 * Handle PULL REPLY message from another peer.
1544 * Check whether we sent a corresponding request and
1545 * whether this reply is the first one.
1547 * @param cls Closure
1548 * @param channel The channel the PUSH was received over
1549 * @param channel_ctx The context associated with this channel
1550 * @param msg The message header
1553 handle_peer_pull_reply (void *cls,
1554 struct GNUNET_CADET_Channel *channel,
1556 const struct GNUNET_MessageHeader *msg)
1558 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
1559 struct GNUNET_PeerIdentity *peers;
1560 struct PeerContext *peer_ctx;
1561 struct GNUNET_PeerIdentity *sender;
1562 struct PeerContext *sender_ctx;
1563 struct PeerOutstandingOp out_op;
1565 #ifdef ENABLE_MALICIOUS
1566 struct AttackedPeer *tmp_att_peer;
1567 #endif /* ENABLE_MALICIOUS */
1569 /* Check for protocol violation */
1570 if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->size))
1572 GNUNET_break_op (0);
1573 return GNUNET_SYSERR;
1576 in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
1577 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1578 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1580 LOG (GNUNET_ERROR_TYPE_ERROR,
1581 "message says it sends %" PRIu64 " peers, have space for %i peers\n",
1582 ntohl (in_msg->num_peers),
1583 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1584 sizeof (struct GNUNET_PeerIdentity));
1585 GNUNET_break_op (0);
1586 return GNUNET_SYSERR;
1589 sender = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
1590 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
1591 // Guess simply casting isn't the nicest way...
1592 // FIXME wait for cadet to change this function
1593 sender_ctx = get_peer_ctx (peer_map, sender);
1595 if (GNUNET_YES == get_peer_flag (sender_ctx, PULL_REPLY_PENDING))
1597 GNUNET_break_op (0);
1602 #ifdef ENABLE_MALICIOUS
1603 // We shouldn't even receive pull replies as we're not sending
1606 #endif /* ENABLE_MALICIOUS */
1608 /* Do actual logic */
1609 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1611 LOG (GNUNET_ERROR_TYPE_DEBUG,
1612 "PULL REPLY received, got following peers:\n");
1614 for (i = 0 ; i < ntohl (in_msg->num_peers) ; i++)
1616 LOG (GNUNET_ERROR_TYPE_DEBUG,
1619 GNUNET_i2s (&peers[i]));
1621 #ifdef ENABLE_MALICIOUS
1624 { /* Add attacked peer to local list */
1625 // TODO check if we sent a request and this was the first reply
1626 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1628 && GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mal_peer_set,
1630 && GNUNET_NO == GNUNET_CRYPTO_cmp_peer_identity (&peers[i],
1633 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1634 tmp_att_peer->peer_id = peers[i];
1635 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1638 add_peer_array_to_set (&peers[i], 1, att_peer_set);
1642 #endif /* ENABLE_MALICIOUS */
1643 peer_ctx = get_peer_ctx (peer_map, &peers[i]);
1644 if (GNUNET_YES == get_peer_flag (peer_ctx, VALID)
1645 || NULL != peer_ctx->send_channel
1646 || NULL != peer_ctx->recv_channel)
1648 if (GNUNET_NO == in_arr (pull_list, pull_list_size, &peers[i])
1649 && 0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peers[i]))
1650 GNUNET_array_append (pull_list, pull_list_size, peers[i]);
1652 else if (GNUNET_NO == insert_in_pull_list_scheduled (peer_ctx))
1654 out_op.op = insert_in_pull_list;
1655 out_op.op_cls = NULL;
1656 GNUNET_array_append (peer_ctx->outstanding_ops,
1657 peer_ctx->num_outstanding_ops,
1659 check_peer_live (peer_ctx);
1663 unset_peer_flag (sender_ctx, PULL_REPLY_PENDING);
1664 rem_from_list (&pending_pull_reply_list, &pending_pull_reply_list_size, sender);
1671 * Compute a random delay.
1672 * A uniformly distributed value between mean + spread and mean - spread.
1674 * For example for mean 4 min and spread 2 the minimum is (4 min - (1/2 * 4 min))
1675 * It would return a random value between 2 and 6 min.
1677 * @param mean the mean
1678 * @param spread the inverse amount of deviation from the mean
1680 static struct GNUNET_TIME_Relative
1681 compute_rand_delay (struct GNUNET_TIME_Relative mean, unsigned int spread)
1683 struct GNUNET_TIME_Relative half_interval;
1684 struct GNUNET_TIME_Relative ret;
1685 unsigned int rand_delay;
1686 unsigned int max_rand_delay;
1690 LOG (GNUNET_ERROR_TYPE_WARNING,
1691 "Not accepting spread of 0\n");
1695 /* Compute random time value between spread * mean and spread * mean */
1696 half_interval = GNUNET_TIME_relative_divide (mean, spread);
1698 max_rand_delay = GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us / mean.rel_value_us * (2/spread);
1700 * Compute random value between (0 and 1) * round_interval
1701 * via multiplying round_interval with a 'fraction' (0 to value)/value
1703 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_rand_delay);
1704 ret = GNUNET_TIME_relative_multiply (mean, rand_delay);
1705 ret = GNUNET_TIME_relative_divide (ret, max_rand_delay);
1706 ret = GNUNET_TIME_relative_add (ret, half_interval);
1708 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ret.rel_value_us)
1709 LOG (GNUNET_ERROR_TYPE_WARNING,
1710 "Returning FOREVER_REL\n");
1717 * Send single pull request
1719 * @param peer_id the peer to send the pull request to.
1722 send_pull_request (struct GNUNET_PeerIdentity *peer_id)
1724 struct GNUNET_MQ_Envelope *ev;
1725 struct GNUNET_MQ_Handle *mq;
1727 LOG (GNUNET_ERROR_TYPE_DEBUG,
1728 "Sending PULL request to peer %s of gossiped list.\n",
1729 GNUNET_i2s (peer_id));
1731 GNUNET_array_append (pending_pull_reply_list, pending_pull_reply_list_size, *peer_id);
1733 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
1734 mq = get_mq (peer_map, peer_id);
1735 GNUNET_MQ_send (mq, ev);
1742 * @param peer_id the peer to send the push to.
1745 send_push (struct GNUNET_PeerIdentity *peer_id)
1747 struct GNUNET_MQ_Envelope *ev;
1748 struct GNUNET_MQ_Handle *mq;
1750 LOG (GNUNET_ERROR_TYPE_DEBUG,
1751 "Sending PUSH to peer %s of gossiped list.\n",
1752 GNUNET_i2s (peer_id));
1754 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
1755 mq = get_mq (peer_map, peer_id);
1756 GNUNET_MQ_send (mq, ev);
1761 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1764 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1767 #ifdef ENABLE_MALICIOUS
1769 * Turn RPS service to act malicious.
1771 * @param cls Closure
1772 * @param channel The channel the PUSH was received over
1773 * @param channel_ctx The context associated with this channel
1774 * @param msg The message header
1777 handle_client_act_malicious (void *cls,
1778 struct GNUNET_SERVER_Client *client,
1779 const struct GNUNET_MessageHeader *msg)
1781 struct GNUNET_RPS_CS_ActMaliciousMessage *in_msg;
1782 struct GNUNET_PeerIdentity *peers;
1783 uint32_t num_mal_peers_sent;
1784 uint32_t num_mal_peers_old;
1786 /* Check for protocol violation */
1787 if (sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage) > ntohs (msg->size))
1789 GNUNET_break_op (0);
1792 in_msg = (struct GNUNET_RPS_CS_ActMaliciousMessage *) msg;
1793 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
1794 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1796 LOG (GNUNET_ERROR_TYPE_ERROR,
1797 "message says it sends %" PRIu64 " peers, have space for %i peers\n",
1798 ntohl (in_msg->num_peers),
1799 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
1800 sizeof (struct GNUNET_PeerIdentity));
1801 GNUNET_break_op (0);
1805 /* Do actual logic */
1806 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1807 mal_type = ntohl (in_msg->type);
1809 LOG (GNUNET_ERROR_TYPE_DEBUG,
1810 "Now acting malicious type %" PRIu32 "\n",
1814 { /* Try to maximise representation */
1815 /* Add other malicious peers to those we already know */
1817 num_mal_peers_sent = ntohl (in_msg->num_peers);
1818 num_mal_peers_old = num_mal_peers;
1819 GNUNET_array_grow (mal_peers,
1821 num_mal_peers + num_mal_peers_sent);
1822 memcpy (&mal_peers[num_mal_peers_old],
1824 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1826 /* Add all mal peers to mal_peer_set */
1827 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1831 /* Substitute do_round () with do_mal_round () */
1832 GNUNET_SCHEDULER_cancel (do_round_task);
1833 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1836 else if (2 == mal_type
1838 { /* Try to partition the network */
1839 /* Add other malicious peers to those we already know */
1840 num_mal_peers_sent = ntohl (in_msg->num_peers) - 1;
1841 num_mal_peers_old = num_mal_peers;
1842 GNUNET_array_grow (mal_peers,
1844 num_mal_peers + num_mal_peers_sent);
1845 memcpy (&mal_peers[num_mal_peers_old],
1847 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1849 /* Add all mal peers to mal_peer_set */
1850 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1854 /* Store the one attacked peer */
1855 memcpy (&attacked_peer,
1856 &in_msg->attacked_peer,
1857 sizeof (struct GNUNET_PeerIdentity));
1859 LOG (GNUNET_ERROR_TYPE_DEBUG,
1860 "Attacked peer is %s\n",
1861 GNUNET_i2s (&attacked_peer));
1863 /* Substitute do_round () with do_mal_round () */
1864 GNUNET_SCHEDULER_cancel (do_round_task);
1865 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1867 else if (0 == mal_type)
1868 { /* Stop acting malicious */
1869 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
1871 /* Substitute do_mal_round () with do_round () */
1872 GNUNET_SCHEDULER_cancel (do_round_task);
1873 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
1880 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1885 * Send out PUSHes and PULLs maliciously.
1887 * This is executed regylary.
1890 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1892 uint32_t num_pushes;
1894 struct GNUNET_TIME_Relative time_next_round;
1895 struct AttackedPeer *tmp_att_peer;
1897 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round maliciously.\n");
1899 /* Do malicious actions */
1901 { /* Try to maximise representation */
1903 /* The maximum of pushes we're going to send this round */
1904 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit,
1905 num_attacked_peers),
1906 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
1908 LOG (GNUNET_ERROR_TYPE_DEBUG,
1909 "Going to send %" PRIu32 " pushes\n",
1912 /* Send PUSHes to attacked peers */
1913 for (i = 0 ; i < num_pushes ; i++)
1915 if (att_peers_tail == att_peer_index)
1916 att_peer_index = att_peers_head;
1918 att_peer_index = att_peer_index->next;
1920 send_push (&att_peer_index->peer_id);
1923 /* Send PULLs to some peers to learn about additional peers to attack */
1924 tmp_att_peer = att_peer_index;
1925 for (i = 0 ; i < num_pushes * alpha ; i++)
1927 if (att_peers_tail == tmp_att_peer)
1928 tmp_att_peer = att_peers_head;
1930 att_peer_index = tmp_att_peer->next;
1932 send_pull_request (&tmp_att_peer->peer_id);
1937 else if (2 == mal_type)
1939 * Try to partition the network
1940 * Send as many pushes to the attacked peer as possible
1941 * That is one push per round as it will ignore more.
1943 send_push (&attacked_peer);
1948 { /* Combined attack */
1950 /* The maximum of pushes we're going to send this round */
1951 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit - 1,
1952 num_attacked_peers),
1953 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
1955 LOG (GNUNET_ERROR_TYPE_DEBUG,
1956 "Going to send %" PRIu32 " pushes\n",
1959 /* Send PUSHes to attacked peers */
1960 send_push (&attacked_peer);
1962 for (i = 0 ; i < num_pushes ; i++)
1964 if (att_peers_tail == att_peer_index)
1965 att_peer_index = att_peers_head;
1967 att_peer_index = att_peer_index->next;
1969 send_push (&att_peer_index->peer_id);
1972 /* Send PULLs to some peers to learn about additional peers to attack */
1973 tmp_att_peer = att_peer_index;
1974 for (i = 0 ; i < num_pushes * alpha ; i++)
1976 if (att_peers_tail == tmp_att_peer)
1977 tmp_att_peer = att_peers_head;
1979 att_peer_index = tmp_att_peer->next;
1981 send_pull_request (&tmp_att_peer->peer_id);
1985 /* Schedule next round */
1986 time_next_round = compute_rand_delay (round_interval, 2);
1988 //do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_mal_round, NULL);
1989 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_mal_round, NULL);
1990 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
1992 #endif /* ENABLE_MALICIOUS */
1996 * Send out PUSHes and PULLs, possibly update #gossip_list, samplers.
1998 * This is executed regylary.
2001 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2003 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round.\n");
2006 unsigned int *permut;
2007 unsigned int n_peers; /* Number of peers we send pushes/pulls to */
2008 struct GNUNET_PeerIdentity peer;
2009 struct GNUNET_PeerIdentity *tmp_peer;
2011 LOG (GNUNET_ERROR_TYPE_DEBUG,
2012 "Printing gossip list:\n");
2013 to_file (file_name_view_log,
2014 "___ new round ___");
2015 for (i = 0 ; i < gossip_list_size ; i++)
2017 LOG (GNUNET_ERROR_TYPE_DEBUG,
2018 "\t%s\n", GNUNET_i2s (&gossip_list[i]));
2019 to_file (file_name_view_log,
2021 GNUNET_i2s_full (&gossip_list[i]));
2023 // TODO log lists, ...
2025 /* Would it make sense to have one shuffeled gossip list and then
2026 * to send PUSHes to first alpha peers, PULL requests to next beta peers and
2027 * use the rest to update sampler?
2028 * in essence get random peers with consumption */
2031 if (0 < gossip_list_size)
2033 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2034 (unsigned int) gossip_list_size);
2035 n_peers = ceil (alpha * gossip_list_size);
2036 LOG (GNUNET_ERROR_TYPE_DEBUG,
2037 "Going to send pushes to %u (ceil (%f * %u)) peers.\n",
2038 n_peers, alpha, gossip_list_size);
2039 for (i = 0 ; i < n_peers ; i++)
2041 peer = gossip_list[permut[i]];
2042 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer)) // TODO
2043 { // FIXME if this fails schedule/loop this for later
2047 GNUNET_free (permut);
2051 /* Send PULL requests */
2052 //permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list->size);
2053 n_peers = ceil (beta * gossip_list_size);
2054 LOG (GNUNET_ERROR_TYPE_DEBUG,
2055 "Going to send pulls to %u (ceil (%f * %u)) peers.\n",
2056 n_peers, beta, gossip_list_size);
2057 for (i = 0 ; i < n_peers ; i++)
2059 tmp_peer = get_rand_peer_ignore_list (gossip_list, gossip_list_size,
2060 pending_pull_reply_list, pending_pull_reply_list_size);
2061 if (NULL != tmp_peer)
2064 GNUNET_free (tmp_peer);
2066 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer))
2068 send_pull_request (&peer);
2074 /* Update gossip list */
2075 /* TODO see how many peers are in push-/pull- list! */
2077 if (push_list_size <= alpha * gossip_list_size
2078 && push_list_size > 0
2079 && pull_list_size > 0)
2081 LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the gossip list.\n");
2083 uint32_t first_border;
2084 uint32_t second_border;
2086 uint32_t peers_to_clean_size;
2087 struct GNUNET_PeerIdentity *peers_to_clean;
2089 peers_to_clean = NULL;
2090 peers_to_clean_size = 0;
2091 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, gossip_list_size);
2092 memcpy (peers_to_clean,
2094 gossip_list_size * sizeof (struct GNUNET_PeerIdentity));
2096 first_border = ceil (alpha * sampler_size_est_need);
2097 second_border = first_border + ceil (beta * sampler_size_est_need);
2099 GNUNET_array_grow (gossip_list, gossip_list_size, second_border);
2101 to_file (file_name_view_log,
2104 for (i = 0 ; i < first_border ; i++)
2105 {/* Update gossip list with peers received through PUSHes */
2106 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
2108 gossip_list[i] = push_list[r_index];
2109 to_file (file_name_view_log,
2111 GNUNET_i2s_full (&gossip_list[i]));
2112 // TODO change the peer_flags accordingly
2115 for (i = first_border ; i < second_border ; i++)
2116 {/* Update gossip list with peers received through PULLs */
2117 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
2119 gossip_list[i] = pull_list[r_index];
2120 to_file (file_name_view_log,
2122 GNUNET_i2s_full (&gossip_list[i]));
2123 // TODO change the peer_flags accordingly
2126 for (i = second_border ; i < sampler_size_est_need ; i++)
2127 {/* Update gossip list with peers from history */
2128 RPS_sampler_get_n_rand_peers (prot_sampler,
2132 num_hist_update_tasks++;
2133 // TODO change the peer_flags accordingly
2136 for (i = 0 ; i < gossip_list_size ; i++)
2137 rem_from_list (&peers_to_clean, &peers_to_clean_size, &gossip_list[i]);
2139 for (i = 0 ; i < peers_to_clean_size ; i++)
2141 peer_clean (&peers_to_clean[i]);
2142 /* to_file (file_name_view_log,
2144 GNUNET_i2s_full (&peers_to_clean[i])); */
2147 GNUNET_free (peers_to_clean);
2151 LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the gossip list.\n");
2153 // TODO independent of that also get some peers from CADET_get_peers()?
2155 LOG (GNUNET_ERROR_TYPE_DEBUG,
2156 "Received %u pushes and %u pulls last round (alpha (%.2f) * gossip_list_size (%u) = %.2f)\n",
2161 alpha * gossip_list_size);
2163 /* Update samplers */
2164 for ( i = 0 ; i < push_list_size ; i++ )
2166 LOG (GNUNET_ERROR_TYPE_DEBUG,
2167 "Updating with peer %s from push list\n",
2168 GNUNET_i2s (&push_list[i]));
2169 RPS_sampler_update (prot_sampler, &push_list[i]);
2170 RPS_sampler_update (client_sampler, &push_list[i]);
2171 // TODO set in_flag?
2174 for ( i = 0 ; i < pull_list_size ; i++ )
2176 LOG (GNUNET_ERROR_TYPE_DEBUG,
2177 "Updating with peer %s from pull list\n",
2178 GNUNET_i2s (&pull_list[i]));
2179 RPS_sampler_update (prot_sampler, &pull_list[i]);
2180 RPS_sampler_update (client_sampler, &pull_list[i]);
2181 // TODO set in_flag?
2185 /* Empty push/pull lists */
2186 GNUNET_array_grow (push_list, push_list_size, 0);
2187 GNUNET_array_grow (pull_list, pull_list_size, 0);
2189 struct GNUNET_TIME_Relative time_next_round;
2191 time_next_round = compute_rand_delay (round_interval, 2);
2193 /* Schedule next round */
2194 //do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_round, NULL);
2195 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_round, NULL);
2196 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
2201 rps_start (struct GNUNET_SERVER_Handle *server);
2205 * This is called from GNUNET_CADET_get_peers().
2207 * It is called on every peer(ID) that cadet somehow has contact with.
2208 * We use those to initialise the sampler.
2211 init_peer_cb (void *cls,
2212 const struct GNUNET_PeerIdentity *peer,
2213 int tunnel, // "Do we have a tunnel towards this peer?"
2214 unsigned int n_paths, // "Number of known paths towards this peer"
2215 unsigned int best_path) // "How long is the best path?
2216 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
2220 LOG (GNUNET_ERROR_TYPE_DEBUG,
2221 "Got peer_id %s from cadet\n",
2229 * Iterator over peers from peerinfo.
2231 * @param cls closure
2232 * @param peer id of the peer, NULL for last call
2233 * @param hello hello message for the peer (can be NULL)
2234 * @param error message
2237 process_peerinfo_peers (void *cls,
2238 const struct GNUNET_PeerIdentity *peer,
2239 const struct GNUNET_HELLO_Message *hello,
2240 const char *err_msg)
2244 LOG (GNUNET_ERROR_TYPE_DEBUG,
2245 "Got peer_id %s from peerinfo\n",
2253 * Clean the send channel of a peer
2256 peer_clean (const struct GNUNET_PeerIdentity *peer)
2258 struct PeerContext *peer_ctx;
2259 struct GNUNET_CADET_Channel *channel;
2261 if (GNUNET_YES != in_arr (gossip_list, gossip_list_size, peer)
2262 && GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
2264 peer_ctx = get_peer_ctx (peer_map, peer);
2265 if (NULL != peer_ctx->send_channel)
2267 channel = peer_ctx->send_channel;
2268 peer_ctx->send_channel = NULL;
2269 GNUNET_CADET_channel_destroy (channel);
2276 * Callback used to remove peers from the multipeermap.
2279 peer_remove_cb (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
2281 struct PeerContext *peer_ctx;
2282 const struct GNUNET_CADET_Channel *channel =
2283 (const struct GNUNET_CADET_Channel *) cls;
2284 struct GNUNET_CADET_Channel *recv;
2285 struct GNUNET_CADET_Channel *send;
2287 peer_ctx = (struct PeerContext *) value;
2289 LOG (GNUNET_ERROR_TYPE_DEBUG,
2290 "Going to clean peer %s\n",
2291 GNUNET_i2s (&peer_ctx->peer_id));
2293 /* If operations are still scheduled for this peer cancel those */
2294 if (0 != peer_ctx->num_outstanding_ops)
2295 GNUNET_array_grow (peer_ctx->outstanding_ops,
2296 peer_ctx->num_outstanding_ops,
2299 /* If we are still waiting for notification whether this peer is live
2300 * cancel the according task */
2301 if (NULL != peer_ctx->is_live_task)
2303 LOG (GNUNET_ERROR_TYPE_DEBUG,
2304 "Trying to cancle is_live_task for peer %s\n",
2306 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
2307 peer_ctx->is_live_task = NULL;
2311 recv = peer_ctx->recv_channel;
2312 peer_ctx->recv_channel = NULL;
2316 GNUNET_CADET_channel_destroy (recv);
2319 /* If there is still a mq destroy it */
2320 if (NULL != peer_ctx->mq)
2322 GNUNET_MQ_destroy (peer_ctx->mq);
2323 peer_ctx->mq = NULL;
2326 /* Remove the send_channel
2327 * The peer map entry should be removed
2328 * from the callback #cleanup_channel */
2329 send = peer_ctx->send_channel;
2330 peer_ctx->send_channel = NULL;
2334 set_peer_flag (peer_ctx, TO_DESTROY);
2335 GNUNET_CADET_channel_destroy (send);
2338 { /* If there is no channel we have to remove it now */
2339 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_remove_all (peer_map, key))
2340 LOG (GNUNET_ERROR_TYPE_WARNING, "removing peer from peer_map failed\n");
2342 GNUNET_free (peer_ctx);
2350 * Task run during shutdown.
2356 shutdown_task (void *cls,
2357 const struct GNUNET_SCHEDULER_TaskContext *tc)
2360 LOG (GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
2362 GNUNET_PEERINFO_notify_cancel (peerinfo_notify_handle);
2363 GNUNET_PEERINFO_disconnect (peerinfo_handle);
2365 if (NULL != do_round_task)
2367 GNUNET_SCHEDULER_cancel (do_round_task);
2368 do_round_task = NULL;
2372 if (GNUNET_SYSERR ==
2373 GNUNET_CONTAINER_multipeermap_iterate (peer_map, peer_remove_cb, NULL))
2374 LOG (GNUNET_ERROR_TYPE_WARNING,
2375 "Iterating over peers to disconnect from them was cancelled\n");
2378 GNUNET_NSE_disconnect (nse);
2379 RPS_sampler_destroy (prot_sampler);
2380 RPS_sampler_destroy (client_sampler);
2381 LOG (GNUNET_ERROR_TYPE_DEBUG,
2382 "Size of the peermap: %u\n",
2383 GNUNET_CONTAINER_multipeermap_size (peer_map));
2384 GNUNET_break (0 == GNUNET_CONTAINER_multipeermap_size (peer_map));
2385 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
2386 GNUNET_CADET_disconnect (cadet_handle);
2387 GNUNET_array_grow (gossip_list, gossip_list_size, 0);
2388 GNUNET_array_grow (push_list, push_list_size, 0);
2389 GNUNET_array_grow (pull_list, pull_list_size, 0);
2390 #ifdef ENABLE_MALICIOUS
2391 struct AttackedPeer *tmp_att_peer;
2392 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
2393 if (NULL != mal_peer_set)
2394 GNUNET_CONTAINER_multipeermap_destroy (mal_peer_set);
2395 if (NULL != att_peer_set)
2396 GNUNET_CONTAINER_multipeermap_destroy (att_peer_set);
2397 while (NULL != att_peers_head)
2399 tmp_att_peer = att_peers_head;
2400 GNUNET_CONTAINER_DLL_remove (att_peers_head, att_peers_tail, tmp_att_peer);
2402 #endif /* ENABLE_MALICIOUS */
2407 * A client disconnected. Remove all of its data structure entries.
2409 * @param cls closure, NULL
2410 * @param client identification of the client
2413 handle_client_disconnect (void *cls,
2414 struct GNUNET_SERVER_Client * client)
2420 * Handle the channel a peer opens to us.
2422 * @param cls The closure
2423 * @param channel The channel the peer wants to establish
2424 * @param initiator The peer's peer ID
2425 * @param port The port the channel is being established over
2426 * @param options Further options
2429 handle_inbound_channel (void *cls,
2430 struct GNUNET_CADET_Channel *channel,
2431 const struct GNUNET_PeerIdentity *initiator,
2433 enum GNUNET_CADET_ChannelOption options)
2435 struct PeerContext *peer_ctx;
2436 struct GNUNET_PeerIdentity peer;
2439 LOG (GNUNET_ERROR_TYPE_DEBUG,
2440 "New channel was established to us (Peer %s).\n",
2441 GNUNET_i2s (&peer));
2443 GNUNET_assert (NULL != channel);
2445 // we might not even store the recv_channel
2447 peer_ctx = get_peer_ctx (peer_map, &peer);
2448 // FIXME what do we do if a channel is established twice?
2449 // overwrite? Clean old channel? ...?
2450 //if (NULL != peer_ctx->recv_channel)
2452 // peer_ctx->recv_channel = channel;
2454 peer_ctx->recv_channel = channel;
2456 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, &peer, peer_ctx,
2457 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
2459 peer_is_live (peer_ctx);
2461 return NULL; // TODO
2466 * This is called when a remote peer destroys a channel.
2468 * @param cls The closure
2469 * @param channel The channel being closed
2470 * @param channel_ctx The context associated with this channel
2473 cleanup_channel (void *cls,
2474 const struct GNUNET_CADET_Channel *channel,
2477 struct GNUNET_PeerIdentity *peer;
2478 struct PeerContext *peer_ctx;
2480 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
2481 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
2482 // Guess simply casting isn't the nicest way...
2483 // FIXME wait for cadet to change this function
2485 //if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
2487 peer_ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
2489 if (NULL == peer_ctx) /* It could have been removed by shutdown_task */
2493 if (channel == peer_ctx->send_channel)
2494 { /* Something (but us) killd the channel */
2495 LOG (GNUNET_ERROR_TYPE_DEBUG,
2496 "send channel (%s) was destroyed - cleaning up\n",
2499 rem_from_list (&gossip_list,
2502 rem_from_list (&pending_pull_reply_list,
2503 &pending_pull_reply_list_size,
2505 to_file (file_name_view_log,
2506 "-%s\t(cleanup channel, other peer)",
2507 GNUNET_i2s_full (peer));
2509 peer_ctx->send_channel = NULL;
2510 /* Somwewhat {ab,re}use the iterator function */
2511 /* Cast to void is ok, because it's used as void in peer_remove_cb */
2512 (void) peer_remove_cb ((void *) channel, peer, peer_ctx);
2514 //if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_remove_all (peer_map, key))
2515 // LOG (GNUNET_ERROR_TYPE_WARNING, "Removing peer from peer_map failed\n");
2517 // GNUNET_free (peer_ctx);
2519 else if (channel == peer_ctx->recv_channel)
2520 { /* Other peer doesn't want to send us messages anymore */
2521 LOG (GNUNET_ERROR_TYPE_DEBUG,
2522 "Peer %s destroyed recv channel - cleaning up channel\n",
2524 peer_ctx->recv_channel = NULL;
2526 else if (NULL == peer_ctx->send_channel &&
2527 get_peer_flag (peer_ctx, TO_DESTROY))
2528 { /* We closed the channel to that peer */
2529 LOG (GNUNET_ERROR_TYPE_DEBUG,
2530 "send channel (%s) was destroyed by us - cleaning up\n",
2533 rem_from_list (&gossip_list,
2536 rem_from_list (&pending_pull_reply_list,
2537 &pending_pull_reply_list_size,
2539 to_file (file_name_view_log,
2540 "-%s\t(cleanup channel, ourself)",
2541 GNUNET_i2s_full (peer));
2543 /* Somwewhat {ab,re}use the iterator function */
2544 /* Cast to void is ok, because it's used as void in peer_remove_cb */
2545 (void) peer_remove_cb ((void *) channel, peer, peer_ctx);
2549 LOG (GNUNET_ERROR_TYPE_WARNING,
2550 "unknown channel (%s) was destroyed\n",
2558 * Actually start the service.
2561 rps_start (struct GNUNET_SERVER_Handle *server)
2563 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
2564 {&handle_client_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
2565 sizeof (struct GNUNET_RPS_CS_RequestMessage)},
2566 {&handle_client_seed, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_SEED, 0},
2567 #ifdef ENABLE_MALICIOUS
2568 {&handle_client_act_malicious, NULL, GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS , 0},
2569 #endif /* ENABLE_MALICIOUS */
2573 GNUNET_SERVER_add_handlers (server, handlers);
2574 GNUNET_SERVER_disconnect_notify (server,
2575 &handle_client_disconnect,
2577 LOG (GNUNET_ERROR_TYPE_INFO, "Ready to receive requests from clients\n");
2580 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
2581 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
2583 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2590 * Process statistics requests.
2592 * @param cls closure
2593 * @param server the initialized server
2594 * @param c configuration to use
2598 struct GNUNET_SERVER_Handle *server,
2599 const struct GNUNET_CONFIGURATION_Handle *c)
2604 // TODO check what this does -- copied from gnunet-boss
2605 // - seems to work as expected
2606 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
2611 GNUNET_CRYPTO_get_peer_identity (cfg, &own_identity); // TODO check return value
2612 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2613 "STARTING SERVICE (rps) for peer [%s]\n",
2614 GNUNET_i2s (&own_identity));
2615 #ifdef ENABLE_MALICIOUS
2616 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2617 "Malicious execution compiled in.\n");
2618 #endif /* ENABLE_MALICIOUS */
2622 /* Get time interval from the configuration */
2623 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
2627 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read ROUNDINTERVAL from config\n");
2628 GNUNET_SCHEDULER_shutdown ();
2632 /* Get initial size of sampler/gossip list from the configuration */
2633 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "RPS",
2635 (long long unsigned int *) &sampler_size_est_need))
2637 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read INITSIZE from config\n");
2638 GNUNET_SCHEDULER_shutdown ();
2641 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", sampler_size_est_need);
2645 gossip_list_size = 0;
2647 /* file_name_view_log */
2648 if (GNUNET_OK != GNUNET_DISK_directory_create ("/tmp/rps/"))
2650 LOG (GNUNET_ERROR_TYPE_WARNING,
2651 "Failed to create directory /tmp/rps/\n");
2654 size = (14 + strlen (GNUNET_i2s_full (&own_identity)) + 1) * sizeof (char);
2655 file_name_view_log = GNUNET_malloc (size);
2656 out_size = GNUNET_snprintf (file_name_view_log,
2659 GNUNET_i2s_full (&own_identity));
2660 if (size < out_size ||
2663 LOG (GNUNET_ERROR_TYPE_WARNING,
2664 "Failed to write string to buffer (size: %i, out_size: %i)\n",
2670 /* connect to NSE */
2671 nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
2677 peer_map = GNUNET_CONTAINER_multipeermap_create (sampler_size_est_need, GNUNET_NO);
2680 /* Initialise cadet */
2681 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
2682 {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH ,
2683 sizeof (struct GNUNET_MessageHeader)},
2684 {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
2685 sizeof (struct GNUNET_MessageHeader)},
2686 {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY , 0},
2690 const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
2691 cadet_handle = GNUNET_CADET_connect (cfg,
2693 &handle_inbound_channel,
2698 peerinfo_handle = GNUNET_PEERINFO_connect (cfg);
2700 /* Initialise sampler */
2701 struct GNUNET_TIME_Relative half_round_interval;
2702 struct GNUNET_TIME_Relative max_round_interval;
2704 half_round_interval = GNUNET_TIME_relative_multiply (round_interval, .5);
2705 max_round_interval = GNUNET_TIME_relative_add (round_interval, half_round_interval);
2707 prot_sampler = RPS_sampler_init (sampler_size_est_need, max_round_interval);
2708 client_sampler = RPS_sampler_mod_init (sampler_size_est_need, max_round_interval);
2710 /* Initialise push and pull maps */
2715 pending_pull_reply_list = NULL;
2716 pending_pull_reply_list_size = 0;
2719 num_hist_update_tasks = 0;
2722 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
2723 GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, NULL);
2724 // TODO send push/pull to each of those peers?
2726 peerinfo_notify_handle = GNUNET_PEERINFO_notify (cfg,
2728 process_peerinfo_peers,
2736 * The main function for the rps service.
2738 * @param argc number of arguments from the command line
2739 * @param argv command line arguments
2740 * @return 0 ok, 1 on error
2743 main (int argc, char *const *argv)
2745 return (GNUNET_OK ==
2746 GNUNET_SERVICE_run (argc,
2749 GNUNET_SERVICE_OPTION_NONE,
2750 &run, NULL)) ? 0 : 1;
2753 /* end of gnunet-service-rps.c */