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"
32 #include "gnunet-service-rps_sampler.h"
37 #define LOG(kind, ...) GNUNET_log(kind, __VA_ARGS__)
39 // TODO modify @brief in every file
41 // TODO check for overflows
43 // TODO align message structs
45 // (TODO api -- possibility of getting weak random peer immideately)
47 // TODO malicious peer
49 // TODO connect to friends
51 // TODO store peers somewhere
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.
106 * Functions of this type can be used to be stored at a peer for later execution.
108 typedef void (* PeerOp) (void *cls, const struct GNUNET_PeerIdentity *peer);
111 * Outstanding operation on peer consisting of callback and closure
113 struct PeerOutstandingOp
128 * Struct used to keep track of other peer's status
130 * This is stored in a multipeermap.
135 * In own gossip/sampler list, in other's gossip/sampler list
140 * Message queue open to client
142 struct GNUNET_MQ_Handle *mq;
145 * Channel open to client.
147 struct GNUNET_CADET_Channel *send_channel;
150 * Channel open from client.
152 struct GNUNET_CADET_Channel *recv_channel; // unneeded?
155 * Array of outstanding operations on this peer.
157 struct PeerOutstandingOp *outstanding_ops;
160 * Number of outstanding operations.
162 unsigned int num_outstanding_ops;
163 //size_t num_outstanding_ops;
166 * Handle to the callback given to cadet_ntfy_tmt_rdy()
168 * To be canceled on shutdown.
170 struct GNUNET_CADET_TransmitHandle *is_live_task;
173 * Identity of the peer
175 struct GNUNET_PeerIdentity peer_id;
178 * This is pobably followed by 'statistical' data (when we first saw
179 * him, how did we get his ID, how many pushes (in a timeinterval),
184 /***********************************************************************
185 * /Housekeeping with peers
186 ***********************************************************************/
192 /***********************************************************************
194 ***********************************************************************/
197 * Sampler used for the Brahms protocol itself.
199 static struct RPS_Sampler *prot_sampler;
202 * Sampler used for the clients.
204 static struct RPS_Sampler *client_sampler;
207 * Set of all peers to keep track of them.
209 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
213 * The gossiped list of peers.
215 static struct GNUNET_PeerIdentity *gossip_list;
218 * Size of the gossiped list
220 //static unsigned int gossip_list_size;
221 static uint32_t gossip_list_size;
225 * The size of sampler we need to be able to satisfy the client's need of
228 static unsigned int sampler_size_client_need;
231 * The size of sampler we need to be able to satisfy the Brahms protocol's
232 * need of random peers.
234 * This is directly taken as the #gossip_list_size on update of the
237 * This is one minimum size the sampler grows to.
239 static unsigned int sampler_size_est_need;
243 * Percentage of total peer number in the gossip list
244 * to send random PUSHes to
249 * Percentage of total peer number in the gossip list
250 * to send random PULLs to
255 * The percentage gamma of history updates.
256 * Simply 1 - alpha - beta
261 * Identifier for the main task that runs periodically.
263 static struct GNUNET_SCHEDULER_Task *do_round_task;
266 * Time inverval the do_round task runs in.
268 static struct GNUNET_TIME_Relative round_interval;
273 * List to store peers received through pushes temporary.
275 * TODO -> multipeermap
277 static struct GNUNET_PeerIdentity *push_list;
280 * Size of the push_list;
282 static unsigned int push_list_size;
283 //size_t push_list_size;
286 * List to store peers received through pulls temporary.
288 * TODO -> multipeermap
290 static struct GNUNET_PeerIdentity *pull_list;
293 * Size of the pull_list;
295 static unsigned int pull_list_size;
296 //size_t pull_list_size;
302 static struct GNUNET_NSE_Handle *nse;
307 static struct GNUNET_CADET_Handle *cadet_handle;
313 * Only needed in the beginning to check how many of the 64 deltas
316 static unsigned int req_counter;
319 * Time of the last request we received.
321 * Used to compute the expected request rate.
323 static struct GNUNET_TIME_Absolute last_request;
326 * Size of #request_deltas.
328 #define REQUEST_DELTAS_SIZE 64
329 static unsigned int request_deltas_size = REQUEST_DELTAS_SIZE;
332 * Last 64 deltas between requests
334 static struct GNUNET_TIME_Relative request_deltas[REQUEST_DELTAS_SIZE];
337 * The prediction of the rate of requests
339 static struct GNUNET_TIME_Relative request_rate;
343 * List with the peers we sent requests to.
345 struct GNUNET_PeerIdentity *pending_pull_reply_list;
348 * Size of #pending_pull_reply_list.
350 uint32_t pending_pull_reply_list_size;
354 * Number of history update tasks.
356 uint32_t num_hist_update_tasks;
359 #ifdef ENABLE_MALICIOUS
361 * Type of malicious peer
363 * 0 Don't act malicious at all - Default
364 * 1 Try to maximise representation
365 * 2 Try to partition the network
367 uint32_t mal_type = 0;
370 * Other malicious peers
372 static struct GNUNET_PeerIdentity *mal_peers = NULL;
375 * Number of other malicious peers
377 static uint32_t num_mal_peers = 0;
381 * If type is 2 This struct is used to store the attacked peers in a DLL
388 struct AttackedPeer *next;
389 struct AttackedPeer *prev;
394 struct GNUNET_PeerIdentity *peer_id;
398 * If type is 2 this is the DLL of attacked peers
400 static struct AttackedPeer *att_peers_head = NULL;
401 static struct AttackedPeer *att_peers_tail = NULL;
404 * This index is used to point to an attacked peer to
405 * implement the round-robin-ish way to select attacked peers.
407 static struct AttackedPeer *att_peer_index = NULL;
410 * Number of attacked peers
412 //static uint32_t num_attacked_peers = 0;
416 * If type is 1 this is the attacked peer
418 static struct GNUNET_PeerIdentity attacked_peer;
421 * The limit of PUSHes we can send in one round.
422 * This is an assumption of the Brahms protocol and either implemented
425 * assumend to be the bandwidth limitation.
427 static uint32_t push_limit = 10000;
428 #endif /* ENABLE_MALICIOUS */
431 /***********************************************************************
433 ***********************************************************************/
440 /***********************************************************************
442 ***********************************************************************/
445 * Set a peer flag of given peer context.
447 #define set_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags |= mask)
450 * Get peer flag of given peer context.
452 #define get_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags & mask ? GNUNET_YES : GNUNET_NO)
455 * Unset flag of given peer context.
457 #define unset_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags &= (~mask))
460 * Compute the minimum of two ints
462 #define min(x, y) ((x < y) ? x : y)
465 * Clean the send channel of a peer
468 peer_clean (const struct GNUNET_PeerIdentity *peer);
472 * Check if peer is already in peer array.
475 in_arr (const struct GNUNET_PeerIdentity *array,
476 unsigned int arr_size,
477 const struct GNUNET_PeerIdentity *peer)
479 GNUNET_assert (NULL != peer);
484 GNUNET_assert (NULL != array);
488 for (i = 0; i < arr_size ; i++)
489 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&array[i], peer))
496 * Print peerlist to log.
499 print_peer_list (struct GNUNET_PeerIdentity *list, unsigned int len)
503 LOG (GNUNET_ERROR_TYPE_DEBUG,
504 "Printing peer list of length %u at %p:\n",
507 for (i = 0 ; i < len ; i++)
509 LOG (GNUNET_ERROR_TYPE_DEBUG,
511 i, GNUNET_i2s (&list[i]));
517 * Remove peer from list.
520 rem_from_list (struct GNUNET_PeerIdentity **peer_list,
521 unsigned int *list_size,
522 const struct GNUNET_PeerIdentity *peer)
525 struct GNUNET_PeerIdentity *tmp;
529 LOG (GNUNET_ERROR_TYPE_DEBUG,
530 "Removing peer %s from list at %p\n",
534 for ( i = 0 ; i < *list_size ; i++ )
536 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&tmp[i], peer))
538 if (i < *list_size -1)
539 { /* Not at the last entry -- shift peers left */
540 memcpy (&tmp[i], &tmp[i +1],
541 ((*list_size) - i -1) * sizeof (struct GNUNET_PeerIdentity));
543 /* Remove last entry (should be now useless PeerID) */
544 GNUNET_array_grow (tmp, *list_size, (*list_size) -1);
551 * Get random peer from the given list but don't return one from the @a ignore_list.
553 struct GNUNET_PeerIdentity *
554 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list,
556 const struct GNUNET_PeerIdentity *ignore_list,
557 uint32_t ignore_size)
561 struct GNUNET_PeerIdentity *tmp_peer_list;
562 struct GNUNET_PeerIdentity *peer;
564 GNUNET_assert (NULL != peer_list);
569 tmp_peer_list = NULL;
570 GNUNET_array_grow (tmp_peer_list, tmp_size, list_size);
571 memcpy (tmp_peer_list,
573 list_size * sizeof (struct GNUNET_PeerIdentity));
574 peer = GNUNET_new (struct GNUNET_PeerIdentity);
577 * Choose the r_index of the peer we want to return
578 * at random from the interval of the gossip list
580 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
582 *peer = tmp_peer_list[r_index];
584 while (in_arr (ignore_list, ignore_size, peer))
586 rem_from_list (&tmp_peer_list, &tmp_size, peer);
588 print_peer_list (tmp_peer_list, tmp_size);
597 * Choose the r_index of the peer we want to return
598 * at random from the interval of the gossip list
600 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
602 *peer = tmp_peer_list[r_index];
606 GNUNET_array_grow (tmp_peer_list, tmp_size, 0);
613 * Get the context of a peer. If not existing, create.
616 get_peer_ctx (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
617 const struct GNUNET_PeerIdentity *peer)
619 struct PeerContext *ctx;
621 if ( GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
623 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
627 ctx = GNUNET_new (struct PeerContext);
630 ctx->send_channel = NULL;
631 ctx->recv_channel = NULL;
632 ctx->outstanding_ops = NULL;
633 ctx->num_outstanding_ops = 0;
634 ctx->is_live_task = NULL;
635 ctx->peer_id = *peer;
636 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
637 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
644 * Put random peer from sampler into the gossip list as history update.
647 hist_update (void *cls, struct GNUNET_PeerIdentity *ids, uint32_t num_peers)
649 GNUNET_assert (1 == num_peers);
651 if (gossip_list_size < sampler_size_est_need)
652 GNUNET_array_append (gossip_list, gossip_list_size, *ids);
654 if (0 < num_hist_update_tasks)
655 num_hist_update_tasks--;
660 * Set the peer flag to living and call the outstanding operations on this peer.
663 peer_is_live (struct PeerContext *peer_ctx)
665 struct GNUNET_PeerIdentity *peer;
667 /* Cancle is_live_task if still scheduled */
668 if (NULL != peer_ctx->is_live_task)
670 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
671 peer_ctx->is_live_task = NULL;
674 peer = &peer_ctx->peer_id;
675 set_peer_flag (peer_ctx, VALID);
677 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer %s is live\n", GNUNET_i2s (peer));
679 if (0 < peer_ctx->num_outstanding_ops)
680 { /* Call outstanding operations */
683 for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
684 peer_ctx->outstanding_ops[i].op (peer_ctx->outstanding_ops[i].op_cls, peer);
685 GNUNET_array_grow (peer_ctx->outstanding_ops, peer_ctx->num_outstanding_ops, 0);
693 * Callback that is called when a channel was effectively established.
694 * This is given to ntfy_tmt_rdy and called when the channel was
695 * successfully established.
698 cadet_ntfy_tmt_rdy_cb (void *cls, size_t size, void *buf)
700 struct PeerContext *peer_ctx = (struct PeerContext *) cls;
702 peer_ctx->is_live_task = NULL;
703 LOG (GNUNET_ERROR_TYPE_DEBUG,
704 "Set ->is_live_task = NULL for peer %s\n",
705 GNUNET_i2s (&peer_ctx->peer_id));
710 peer_is_live (peer_ctx);
714 LOG (GNUNET_ERROR_TYPE_WARNING,
715 "Problems establishing a connection to peer %s in order to check liveliness\n",
716 GNUNET_i2s (&peer_ctx->peer_id));
717 // TODO reschedule? cleanup?
720 //if (NULL != peer_ctx->is_live_task)
722 // LOG (GNUNET_ERROR_TYPE_DEBUG,
723 // "Trying to cancle is_live_task for peer %s\n",
724 // GNUNET_i2s (&peer_ctx->peer_id));
725 // GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
726 // peer_ctx->is_live_task = NULL;
734 * Get the channel of a peer. If not existing, create.
736 struct GNUNET_CADET_Channel *
737 get_channel (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
738 const struct GNUNET_PeerIdentity *peer)
740 struct PeerContext *peer_ctx;
742 peer_ctx = get_peer_ctx (peer_map, peer);
744 GNUNET_assert (NULL == peer_ctx->is_live_task);
746 if (NULL == peer_ctx->send_channel)
748 LOG (GNUNET_ERROR_TYPE_DEBUG,
749 "Trying to establish channel to peer %s\n",
752 peer_ctx->send_channel =
753 GNUNET_CADET_channel_create (cadet_handle,
756 GNUNET_RPS_CADET_PORT,
757 GNUNET_CADET_OPTION_RELIABLE);
759 // do I have to explicitly put it in the peer_map?
760 (void) GNUNET_CONTAINER_multipeermap_put
764 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
766 return peer_ctx->send_channel;
771 * Get the message queue of a specific peer.
773 * If we already have a message queue open to this client,
774 * simply return it, otherways create one.
776 struct GNUNET_MQ_Handle *
777 get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
778 const struct GNUNET_PeerIdentity *peer_id)
780 struct PeerContext *peer_ctx;
782 peer_ctx = get_peer_ctx (peer_map, peer_id);
784 GNUNET_assert (NULL == peer_ctx->is_live_task);
786 if (NULL == peer_ctx->mq)
788 (void) get_channel (peer_map, peer_id);
789 peer_ctx->mq = GNUNET_CADET_mq_create (peer_ctx->send_channel);
790 //do I have to explicitly put it in the peer_map?
791 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer_id, peer_ctx,
792 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
799 * Issue check whether peer is live
801 * @param peer_ctx the context of the peer
804 check_peer_live (struct PeerContext *peer_ctx)
806 (void) get_channel (peer_map, &peer_ctx->peer_id);
807 LOG (GNUNET_ERROR_TYPE_DEBUG,
808 "Get informed about peer %s getting live\n",
809 GNUNET_i2s (&peer_ctx->peer_id));
810 if (NULL == peer_ctx->is_live_task)
812 peer_ctx->is_live_task =
813 GNUNET_CADET_notify_transmit_ready (peer_ctx->send_channel,
815 GNUNET_TIME_UNIT_FOREVER_REL,
816 sizeof (struct GNUNET_MessageHeader),
817 cadet_ntfy_tmt_rdy_cb,
822 LOG (GNUNET_ERROR_TYPE_DEBUG,
823 "Already waiting for notification\n");
829 * Sum all time relatives of an array.
831 struct GNUNET_TIME_Relative
832 T_relative_sum (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
834 struct GNUNET_TIME_Relative sum;
837 sum = GNUNET_TIME_UNIT_ZERO;
838 for ( i = 0 ; i < arr_size ; i++ )
840 sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
847 * Compute the average of given time relatives.
849 struct GNUNET_TIME_Relative
850 T_relative_avg (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
852 return GNUNET_TIME_relative_divide (T_relative_sum (rel_array, arr_size), arr_size);
857 * Insert PeerID in #pull_list
859 * Called once we know a peer is live.
862 insert_in_pull_list (void *cls, const struct GNUNET_PeerIdentity *peer)
864 if (GNUNET_NO == in_arr (pull_list, pull_list_size, peer))
865 GNUNET_array_append (pull_list, pull_list_size, *peer);
871 * Check whether #insert_in_pull_list was already scheduled
874 insert_in_pull_list_scheduled (const struct PeerContext *peer_ctx)
878 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
879 if (insert_in_pull_list == peer_ctx->outstanding_ops[i].op)
886 * Insert PeerID in #gossip_list
888 * Called once we know a peer is live.
891 insert_in_gossip_list (void *cls, const struct GNUNET_PeerIdentity *peer)
893 if (GNUNET_NO == in_arr (gossip_list, gossip_list_size, peer))
894 GNUNET_array_append (gossip_list, gossip_list_size, *peer);
896 (void) get_channel (peer_map, peer);
900 * Check whether #insert_in_gossip_list was already scheduled
903 insert_in_gossip_list_scheduled (const struct PeerContext *peer_ctx)
907 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
908 if (insert_in_gossip_list == peer_ctx->outstanding_ops[i].op)
915 * Update sampler with given PeerID.
918 insert_in_sampler (void *cls, const struct GNUNET_PeerIdentity *peer)
920 LOG (GNUNET_ERROR_TYPE_DEBUG,
921 "Updating samplers with peer %s from insert_in_sampler()\n",
923 RPS_sampler_update (prot_sampler, peer);
924 RPS_sampler_update (client_sampler, peer);
929 * Check whether #insert_in_sampler was already scheduled
932 insert_in_sampler_scheduled (const struct PeerContext *peer_ctx)
936 for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
937 if (insert_in_sampler== peer_ctx->outstanding_ops[i].op)
944 * Wrapper around #RPS_sampler_resize()
946 * If we do not have enough sampler elements, double current sampler size
947 * If we have more than enough sampler elements, halv current sampler size
950 resize_wrapper (struct RPS_Sampler *sampler, uint32_t new_size)
952 unsigned int sampler_size;
955 // TODO respect the min, max
956 sampler_size = RPS_sampler_get_size (sampler);
957 if (sampler_size > new_size * 4)
959 RPS_sampler_resize (sampler, sampler_size / 2);
961 else if (sampler_size < new_size)
963 RPS_sampler_resize (sampler, sampler_size * 2);
965 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
970 * Wrapper around #RPS_sampler_resize() resizing the client sampler
973 client_resize_wrapper ()
975 uint32_t bigger_size;
976 unsigned int sampler_size;
980 sampler_size = RPS_sampler_get_size (client_sampler);
982 if (sampler_size_est_need > sampler_size_client_need)
983 bigger_size = sampler_size_est_need;
985 bigger_size = sampler_size_client_need;
987 // TODO respect the min, max
988 resize_wrapper (client_sampler, bigger_size);
989 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
994 * Estimate request rate
996 * Called every time we receive a request from the client.
1001 struct GNUNET_TIME_Relative max_round_duration;
1003 if (request_deltas_size > req_counter)
1005 if ( 1 < req_counter)
1007 /* Shift last request deltas to the right */
1008 memcpy (&request_deltas[1],
1010 (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
1012 /* Add current delta to beginning */
1014 GNUNET_TIME_absolute_get_difference (last_request,
1015 GNUNET_TIME_absolute_get ());
1016 request_rate = T_relative_avg (request_deltas, req_counter);
1018 /* Compute the duration a round will maximally take */
1019 max_round_duration =
1020 GNUNET_TIME_relative_add (round_interval,
1021 GNUNET_TIME_relative_divide (round_interval, 2));
1023 /* Set the estimated size the sampler has to have to
1024 * satisfy the current client request rate */
1025 sampler_size_client_need =
1026 max_round_duration.rel_value_us / request_rate.rel_value_us;
1028 /* Resize the sampler */
1029 client_resize_wrapper ();
1031 last_request = GNUNET_TIME_absolute_get ();
1035 /***********************************************************************
1037 ***********************************************************************/
1044 * Function called by NSE.
1046 * Updates sizes of sampler list and gossip list and adapt those lists
1050 nse_callback (void *cls, struct GNUNET_TIME_Absolute timestamp,
1051 double logestimate, double std_dev)
1054 //double scale; // TODO this might go gloabal/config
1056 LOG (GNUNET_ERROR_TYPE_DEBUG,
1057 "Received a ns estimate - logest: %f, std_dev: %f (old_size: %u)\n",
1058 logestimate, std_dev, RPS_sampler_get_size (prot_sampler));
1060 estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
1061 // GNUNET_NSE_log_estimate_to_n (logestimate);
1062 estimate = pow (estimate, 1.0 / 3);
1063 // TODO add if std_dev is a number
1064 // estimate += (std_dev * scale);
1065 if (2 < ceil (estimate))
1067 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
1068 sampler_size_est_need = estimate;
1070 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
1072 /* If the NSE has changed adapt the lists accordingly */
1073 resize_wrapper (prot_sampler, sampler_size_est_need);
1074 client_resize_wrapper ();
1079 * Callback called once the requested PeerIDs are ready.
1081 * Sends those to the requesting client.
1083 void client_respond (void *cls,
1084 struct GNUNET_PeerIdentity *ids, uint32_t num_peers)
1086 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler returned %" PRIX32 " peers\n", num_peers);
1087 struct GNUNET_MQ_Envelope *ev;
1088 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
1089 struct GNUNET_SERVER_Client *client;
1090 uint32_t size_needed;
1091 struct client_ctx *cli_ctx;
1093 client = (struct GNUNET_SERVER_Client *) cls;
1095 size_needed = sizeof (struct GNUNET_RPS_CS_ReplyMessage) +
1096 num_peers * sizeof (struct GNUNET_PeerIdentity);
1098 GNUNET_assert (GNUNET_SERVER_MAX_MESSAGE_SIZE >= size_needed);
1100 ev = GNUNET_MQ_msg_extra (out_msg,
1101 num_peers * sizeof (struct GNUNET_PeerIdentity),
1102 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
1103 out_msg->num_peers = htonl (num_peers);
1105 memcpy (&out_msg[1],
1107 num_peers * sizeof (struct GNUNET_PeerIdentity));
1110 cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct client_ctx);
1111 if (NULL == cli_ctx) {
1112 cli_ctx = GNUNET_new (struct client_ctx);
1113 cli_ctx->mq = GNUNET_MQ_queue_for_server_client (client);
1114 GNUNET_SERVER_client_set_user_context (client, cli_ctx);
1117 GNUNET_MQ_send (cli_ctx->mq, ev);
1122 * Handle RPS request from the client.
1124 * @param cls closure
1125 * @param client identification of the client
1126 * @param message the actual message
1129 handle_client_request (void *cls,
1130 struct GNUNET_SERVER_Client *client,
1131 const struct GNUNET_MessageHeader *message)
1133 struct GNUNET_RPS_CS_RequestMessage *msg;
1135 uint32_t size_needed;
1138 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
1140 num_peers = ntohl (msg->num_peers);
1141 size_needed = sizeof (struct GNUNET_RPS_CS_RequestMessage) +
1142 num_peers * sizeof (struct GNUNET_PeerIdentity);
1144 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
1146 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1150 for (i = 0 ; i < num_peers ; i++)
1153 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client requested %" PRIX32 " random peer(s).\n", num_peers);
1155 RPS_sampler_get_n_rand_peers (client_sampler, client_respond,
1156 client, num_peers, GNUNET_YES);
1158 GNUNET_SERVER_receive_done (client,
1164 * Handle seed from the client.
1166 * @param cls closure
1167 * @param client identification of the client
1168 * @param message the actual message
1171 handle_client_seed (void *cls,
1172 struct GNUNET_SERVER_Client *client,
1173 const struct GNUNET_MessageHeader *message)
1175 struct GNUNET_RPS_CS_SeedMessage *in_msg;
1176 struct GNUNET_PeerIdentity *peers;
1180 if (sizeof (struct GNUNET_RPS_CS_SeedMessage) > ntohs (message->size))
1182 GNUNET_break_op (0);
1183 GNUNET_SERVER_receive_done (client,
1187 in_msg = (struct GNUNET_RPS_CS_SeedMessage *) message;
1188 num_peers = ntohl (in_msg->num_peers);
1189 peers = (struct GNUNET_PeerIdentity *) &in_msg[1];
1190 //peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1191 //memcpy (peers, &in_msg[1], num_peers * sizeof (struct GNUNET_PeerIdentity));
1193 if ((ntohs (message->size) - sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
1194 sizeof (struct GNUNET_PeerIdentity) != num_peers)
1196 GNUNET_break_op (0);
1197 GNUNET_SERVER_receive_done (client,
1201 LOG (GNUNET_ERROR_TYPE_DEBUG,
1202 "Client seeded peers:\n");
1203 print_peer_list (peers, num_peers);
1205 // TODO check for validity of ids
1207 for (i = 0 ; i < num_peers ; i++)
1209 LOG (GNUNET_ERROR_TYPE_DEBUG,
1210 "Updating samplers with seed %" PRIX32 ": %s\n",
1212 GNUNET_i2s (&peers[i]));
1214 RPS_sampler_update (prot_sampler, &peers[i]);
1215 RPS_sampler_update (client_sampler, &peers[i]);
1218 //GNUNET_free (peers);
1220 GNUNET_SERVER_receive_done (client,
1226 * Handle a PUSH message from another peer.
1228 * Check the proof of work and store the PeerID
1229 * in the temporary list for pushed PeerIDs.
1231 * @param cls Closure
1232 * @param channel The channel the PUSH was received over
1233 * @param channel_ctx The context associated with this channel
1234 * @param msg The message header
1237 handle_peer_push (void *cls,
1238 struct GNUNET_CADET_Channel *channel,
1240 const struct GNUNET_MessageHeader *msg)
1242 const struct GNUNET_PeerIdentity *peer;
1244 // (check the proof of work)
1246 peer = (const struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
1247 // FIXME wait for cadet to change this function
1248 LOG (GNUNET_ERROR_TYPE_DEBUG, "PUSH received (%s)\n", GNUNET_i2s (peer));
1250 /* Add the sending peer to the push_list */
1251 if (GNUNET_NO == in_arr (push_list, push_list_size, peer))
1252 GNUNET_array_append (push_list, push_list_size, *peer);
1258 * Handle PULL REQUEST request message from another peer.
1260 * Reply with the gossip list of PeerIDs.
1262 * @param cls Closure
1263 * @param channel The channel the PUSH was received over
1264 * @param channel_ctx The context associated with this channel
1265 * @param msg The message header
1268 handle_peer_pull_request (void *cls,
1269 struct GNUNET_CADET_Channel *channel,
1271 const struct GNUNET_MessageHeader *msg)
1273 struct GNUNET_PeerIdentity *peer;
1275 struct GNUNET_MQ_Handle *mq;
1276 struct GNUNET_MQ_Envelope *ev;
1277 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
1280 peer = (struct GNUNET_PeerIdentity *)
1281 GNUNET_CADET_channel_get_info (channel,
1282 GNUNET_CADET_OPTION_PEER);
1283 // FIXME wait for cadet to change this function
1285 /* Compute actual size */
1286 send_size = sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) +
1287 gossip_list_size * sizeof (struct GNUNET_PeerIdentity);
1289 if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size)
1290 /* Compute number of peers to send
1291 * If too long, simply truncate */
1292 // TODO select random ones via permutation
1294 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE -
1295 sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1296 sizeof (struct GNUNET_PeerIdentity);
1298 send_size = gossip_list_size;
1300 LOG (GNUNET_ERROR_TYPE_DEBUG,
1301 "PULL REQUEST from peer %s received, going to send %u peers\n",
1302 GNUNET_i2s (peer), send_size);
1304 mq = get_mq (peer_map, peer);
1306 ev = GNUNET_MQ_msg_extra (out_msg,
1307 send_size * sizeof (struct GNUNET_PeerIdentity),
1308 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
1309 //out_msg->num_peers = htonl (gossip_list_size);
1310 out_msg->num_peers = htonl (send_size);
1311 memcpy (&out_msg[1], gossip_list,
1312 send_size * sizeof (struct GNUNET_PeerIdentity));
1314 GNUNET_MQ_send (mq, ev);
1321 * Handle PULL REPLY message from another peer.
1323 * Check whether we sent a corresponding request and
1324 * whether this reply is the first one.
1326 * @param cls Closure
1327 * @param channel The channel the PUSH was received over
1328 * @param channel_ctx The context associated with this channel
1329 * @param msg The message header
1332 handle_peer_pull_reply (void *cls,
1333 struct GNUNET_CADET_Channel *channel,
1335 const struct GNUNET_MessageHeader *msg)
1337 LOG (GNUNET_ERROR_TYPE_DEBUG, "PULL REPLY received\n");
1339 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
1340 struct GNUNET_PeerIdentity *peers;
1341 struct PeerContext *peer_ctx;
1342 struct GNUNET_PeerIdentity *sender;
1343 struct PeerContext *sender_ctx;
1344 struct PeerOutstandingOp out_op;
1347 /* Check for protocol violation */
1348 if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->size))
1350 GNUNET_break_op (0);
1351 return GNUNET_SYSERR;
1354 in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
1355 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1356 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1358 LOG (GNUNET_ERROR_TYPE_ERROR,
1359 "message says it sends %" PRIu64 " peers, have space for %i peers\n",
1360 ntohl (in_msg->num_peers),
1361 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1362 sizeof (struct GNUNET_PeerIdentity));
1363 GNUNET_break_op (0);
1364 return GNUNET_SYSERR;
1367 sender = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
1368 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
1369 // Guess simply casting isn't the nicest way...
1370 // FIXME wait for cadet to change this function
1371 sender_ctx = get_peer_ctx (peer_map, sender);
1373 if (GNUNET_YES == get_peer_flag (sender_ctx, PULL_REPLY_PENDING))
1375 GNUNET_break_op (0);
1380 /* Do actual logic */
1381 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1382 for (i = 0 ; i < ntohl (in_msg->num_peers) ; i++)
1384 peer_ctx = get_peer_ctx (peer_map, &peers[i]);
1385 if (GNUNET_YES == get_peer_flag (peer_ctx, VALID)
1386 || NULL != peer_ctx->send_channel
1387 || NULL != peer_ctx->recv_channel)
1389 if (GNUNET_NO == in_arr (pull_list, pull_list_size, &peers[i])
1390 && 0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peers[i]))
1391 GNUNET_array_append (pull_list, pull_list_size, peers[i]);
1393 else if (GNUNET_NO == insert_in_pull_list_scheduled (peer_ctx))
1395 out_op.op = insert_in_pull_list;
1396 out_op.op_cls = NULL;
1397 GNUNET_array_append (peer_ctx->outstanding_ops,
1398 peer_ctx->num_outstanding_ops,
1400 check_peer_live (peer_ctx);
1404 unset_peer_flag (sender_ctx, PULL_REPLY_PENDING);
1405 rem_from_list (&pending_pull_reply_list, &pending_pull_reply_list_size, sender);
1412 * Compute a random delay.
1413 * A uniformly distributed value between mean + spread and mean - spread.
1415 * For example for mean 4 min and spread 2 the minimum is (4 min - (1/2 * 4 min))
1416 * It would return a random value between 2 and 6 min.
1418 * @param mean the mean
1419 * @param spread the inverse amount of deviation from the mean
1421 static struct GNUNET_TIME_Relative
1422 compute_rand_delay (struct GNUNET_TIME_Relative mean, unsigned int spread)
1424 struct GNUNET_TIME_Relative half_interval;
1425 struct GNUNET_TIME_Relative ret;
1426 unsigned int rand_delay;
1427 unsigned int max_rand_delay;
1431 LOG (GNUNET_ERROR_TYPE_WARNING,
1432 "Not accepting spread of 0\n");
1436 /* Compute random time value between spread * mean and spread * mean */
1437 half_interval = GNUNET_TIME_relative_divide (mean, spread);
1439 max_rand_delay = GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us / mean.rel_value_us * (2/spread);
1441 * Compute random value between (0 and 1) * round_interval
1442 * via multiplying round_interval with a 'fraction' (0 to value)/value
1444 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_rand_delay);
1445 ret = GNUNET_TIME_relative_multiply (mean, rand_delay);
1446 ret = GNUNET_TIME_relative_divide (ret, max_rand_delay);
1447 ret = GNUNET_TIME_relative_add (ret, half_interval);
1449 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ret.rel_value_us)
1450 LOG (GNUNET_ERROR_TYPE_WARNING,
1451 "Returning FOREVER_REL\n");
1458 * Send single pull request
1460 * @param peer_id the peer to send the pull request to.
1463 send_pull_request (struct GNUNET_PeerIdentity *peer_id)
1465 struct GNUNET_MQ_Envelope *ev;
1466 struct GNUNET_MQ_Handle *mq;
1468 LOG (GNUNET_ERROR_TYPE_DEBUG,
1469 "Sending PULL request to peer %s of gossiped list.\n",
1470 GNUNET_i2s (peer_id));
1472 GNUNET_array_append (pending_pull_reply_list, pending_pull_reply_list_size, *peer_id);
1474 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
1475 mq = get_mq (peer_map, peer_id);
1476 GNUNET_MQ_send (mq, ev);
1483 * @param peer_id the peer to send the push to.
1486 send_push (struct GNUNET_PeerIdentity *peer_id)
1488 struct GNUNET_MQ_Envelope *ev;
1489 struct GNUNET_MQ_Handle *mq;
1491 LOG (GNUNET_ERROR_TYPE_DEBUG,
1492 "Sending PUSH to peer %s of gossiped list.\n",
1493 GNUNET_i2s (peer_id));
1495 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
1496 mq = get_mq (peer_map, peer_id);
1497 GNUNET_MQ_send (mq, ev);
1502 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1505 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1508 #ifdef ENABLE_MALICIOUS
1510 * Turn RPS service to act malicious.
1512 * @param cls Closure
1513 * @param channel The channel the PUSH was received over
1514 * @param channel_ctx The context associated with this channel
1515 * @param msg The message header
1518 handle_client_act_malicious (void *cls,
1519 struct GNUNET_SERVER_Client *client,
1520 const struct GNUNET_MessageHeader *msg)
1522 struct GNUNET_RPS_CS_ActMaliciousMessage *in_msg;
1523 struct GNUNET_PeerIdentity *peers;
1524 uint32_t num_mal_peers_sent;
1525 uint32_t num_mal_peers_old;
1527 /* Check for protocol violation */
1528 if (sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage) > ntohs (msg->size))
1530 GNUNET_break_op (0);
1533 in_msg = (struct GNUNET_RPS_CS_ActMaliciousMessage *) msg;
1534 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
1535 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1537 LOG (GNUNET_ERROR_TYPE_ERROR,
1538 "message says it sends %" PRIu64 " peers, have space for %i peers\n",
1539 ntohl (in_msg->num_peers),
1540 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
1541 sizeof (struct GNUNET_PeerIdentity));
1542 GNUNET_break_op (0);
1546 /* Do actual logic */
1547 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1548 mal_type = ntohl (in_msg->type);
1550 LOG (GNUNET_ERROR_TYPE_DEBUG,
1551 "Now acting malicious type %" PRIu32 "\n",
1555 { /* Try to maximise representation */
1556 /* Add other malicious peers to those we already know */
1557 num_mal_peers_sent = ntohl (in_msg->num_peers);
1558 num_mal_peers_old = num_mal_peers;
1559 GNUNET_array_grow (mal_peers,
1561 num_mal_peers + num_mal_peers_sent);
1562 memcpy (&mal_peers[num_mal_peers_old],
1564 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1566 /* Substitute do_round () with do_mal_round () */
1567 GNUNET_SCHEDULER_cancel (do_round_task);
1568 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1570 else if (2 == mal_type)
1571 { /* Try to partition the network */
1572 /* Add other malicious peers to those we already know */
1573 num_mal_peers_sent = ntohl (in_msg->num_peers) - 1;
1574 num_mal_peers_old = num_mal_peers;
1575 GNUNET_array_grow (mal_peers,
1577 num_mal_peers + num_mal_peers_sent);
1578 memcpy (&mal_peers[num_mal_peers_old],
1580 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1582 /* Store the one attacked peer */
1583 memcpy (&attacked_peer,
1584 &peers[num_mal_peers_sent],
1585 sizeof (struct GNUNET_PeerIdentity));
1587 LOG (GNUNET_ERROR_TYPE_DEBUG,
1588 "Attacked peer is %s\n",
1589 GNUNET_i2s (&attacked_peer));
1591 /* Substitute do_round () with do_mal_round () */
1592 GNUNET_SCHEDULER_cancel (do_round_task);
1593 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1595 else if (0 == mal_type)
1596 { /* Stop acting malicious */
1598 GNUNET_free (mal_peers);
1600 /* Substitute do_mal_round () with do_round () */
1601 GNUNET_SCHEDULER_cancel (do_round_task);
1602 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
1612 * Send out PUSHes and PULLs maliciously.
1614 * This is executed regylary.
1617 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1619 uint32_t num_pushes;
1621 struct GNUNET_TIME_Relative time_next_round;
1623 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round maliciously.\n");
1625 /* Do malicious actions */
1627 { /* Try to maximise representation */
1628 num_pushes = min (min (push_limit, /* FIXME: attacked peer */ num_mal_peers), GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
1629 for (i = 0 ; i < num_pushes ; i++)
1630 { /* Send PUSHes to attacked peers */
1631 if (att_peers_tail == att_peer_index)
1632 att_peer_index = att_peers_head;
1634 att_peer_index = att_peer_index->next;
1636 send_push (att_peer_index->peer_id);
1639 // send_pull_request
1642 else if (2 == mal_type)
1644 * Try to partition the network
1645 * Send as many pushes to attacked peer as possible
1647 send_push (&attacked_peer);
1650 /* Schedule next round */
1651 time_next_round = compute_rand_delay (round_interval, 2);
1653 //do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_mal_round, NULL);
1654 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_mal_round, NULL);
1655 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
1657 #endif /* ENABLE_MALICIOUS */
1661 * Send out PUSHes and PULLs, possibly update #gossip_list, samplers.
1663 * This is executed regylary.
1666 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1668 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round.\n");
1671 unsigned int *permut;
1672 unsigned int n_peers; /* Number of peers we send pushes/pulls to */
1673 struct GNUNET_PeerIdentity peer;
1674 struct GNUNET_PeerIdentity *tmp_peer;
1676 LOG (GNUNET_ERROR_TYPE_DEBUG,
1677 "Printing gossip list:\n");
1678 for (i = 0 ; i < gossip_list_size ; i++)
1679 LOG (GNUNET_ERROR_TYPE_DEBUG,
1680 "\t%s\n", GNUNET_i2s (&gossip_list[i]));
1681 // TODO log lists, ...
1683 /* Would it make sense to have one shuffeled gossip list and then
1684 * to send PUSHes to first alpha peers, PULL requests to next beta peers and
1685 * use the rest to update sampler?
1686 * in essence get random peers with consumption */
1689 if (0 < gossip_list_size)
1691 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
1692 (unsigned int) gossip_list_size);
1693 n_peers = ceil (alpha * gossip_list_size);
1694 LOG (GNUNET_ERROR_TYPE_DEBUG,
1695 "Going to send pushes to %u ceil (%f * %u) peers.\n",
1696 n_peers, alpha, gossip_list_size);
1697 for (i = 0 ; i < n_peers ; i++)
1699 peer = gossip_list[permut[i]];
1700 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer)) // TODO
1701 { // FIXME if this fails schedule/loop this for later
1705 GNUNET_free (permut);
1709 /* Send PULL requests */
1710 //permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list->size);
1711 n_peers = ceil (beta * gossip_list_size);
1712 LOG (GNUNET_ERROR_TYPE_DEBUG,
1713 "Going to send pulls to %u ceil (%f * %u) peers.\n",
1714 n_peers, beta, gossip_list_size);
1715 for (i = 0 ; i < n_peers ; i++)
1717 tmp_peer = get_rand_peer_ignore_list (gossip_list, gossip_list_size,
1718 pending_pull_reply_list, pending_pull_reply_list_size);
1719 if (NULL != tmp_peer)
1722 GNUNET_free (tmp_peer);
1724 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer))
1726 send_pull_request (&peer);
1732 /* Update gossip list */
1734 if ( push_list_size <= alpha * gossip_list_size &&
1735 push_list_size != 0 &&
1736 pull_list_size != 0 )
1738 LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the gossip list.\n");
1740 uint32_t first_border;
1741 uint32_t second_border;
1743 uint32_t peers_to_clean_size;
1744 struct GNUNET_PeerIdentity *peers_to_clean;
1746 peers_to_clean = NULL;
1747 peers_to_clean_size = 0;
1748 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, gossip_list_size);
1749 memcpy (peers_to_clean,
1751 gossip_list_size * sizeof (struct GNUNET_PeerIdentity));
1753 first_border = ceil (alpha * sampler_size_est_need);
1754 second_border = first_border + ceil (beta * sampler_size_est_need);
1756 GNUNET_array_grow (gossip_list, gossip_list_size, second_border);
1758 for (i = 0 ; i < first_border ; i++)
1759 { // TODO use RPS_sampler_get_n_rand_peers
1760 /* Update gossip list with peers received through PUSHes */
1761 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
1763 gossip_list[i] = push_list[r_index];
1764 // TODO change the peer_flags accordingly
1767 for (i = first_border ; i < second_border ; i++)
1769 /* Update gossip list with peers received through PULLs */
1770 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
1772 gossip_list[i] = pull_list[r_index];
1773 // TODO change the peer_flags accordingly
1776 for (i = second_border ; i < sampler_size_est_need ; i++)
1778 /* Update gossip list with peers from history */
1779 RPS_sampler_get_n_rand_peers (prot_sampler, hist_update, NULL, 1, GNUNET_NO);
1780 num_hist_update_tasks++;
1781 // TODO change the peer_flags accordingly
1784 for (i = 0 ; i < gossip_list_size ; i++)
1785 rem_from_list (&peers_to_clean, &peers_to_clean_size, &gossip_list[i]);
1787 for (i = 0 ; i < peers_to_clean_size ; i++)
1788 peer_clean (&peers_to_clean[i]);
1790 GNUNET_free (peers_to_clean);
1794 LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the gossip list.\n");
1796 // TODO independent of that also get some peers from CADET_get_peers()?
1799 /* Update samplers */
1800 for ( i = 0 ; i < push_list_size ; i++ )
1802 LOG (GNUNET_ERROR_TYPE_DEBUG,
1803 "Updating with peer %s from push list\n",
1804 GNUNET_i2s (&push_list[i]));
1805 RPS_sampler_update (prot_sampler, &push_list[i]);
1806 RPS_sampler_update (client_sampler, &push_list[i]);
1807 // TODO set in_flag?
1810 for ( i = 0 ; i < pull_list_size ; i++ )
1812 LOG (GNUNET_ERROR_TYPE_DEBUG,
1813 "Updating with peer %s from pull list\n",
1814 GNUNET_i2s (&pull_list[i]));
1815 RPS_sampler_update (prot_sampler, &pull_list[i]);
1816 RPS_sampler_update (client_sampler, &pull_list[i]);
1817 // TODO set in_flag?
1821 /* Empty push/pull lists */
1822 GNUNET_array_grow (push_list, push_list_size, 0);
1823 GNUNET_array_grow (pull_list, pull_list_size, 0);
1825 struct GNUNET_TIME_Relative time_next_round;
1827 time_next_round = compute_rand_delay (round_interval, 2);
1829 /* Schedule next round */
1830 //do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_round, NULL);
1831 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_round, NULL);
1832 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
1837 rps_start (struct GNUNET_SERVER_Handle *server);
1841 * This is called from GNUNET_CADET_get_peers().
1843 * It is called on every peer(ID) that cadet somehow has contact with.
1844 * We use those to initialise the sampler.
1847 init_peer_cb (void *cls,
1848 const struct GNUNET_PeerIdentity *peer,
1849 int tunnel, // "Do we have a tunnel towards this peer?"
1850 unsigned int n_paths, // "Number of known paths towards this peer"
1851 unsigned int best_path) // "How long is the best path?
1852 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
1854 struct PeerOutstandingOp out_op;
1855 struct PeerContext *peer_ctx;
1858 && 0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, peer))
1860 LOG (GNUNET_ERROR_TYPE_DEBUG,
1861 "Got peer %s (at %p) from CADET (gossip_list_size: %u)\n",
1862 GNUNET_i2s (peer), peer, gossip_list_size);
1864 // maybe create a function for that
1865 peer_ctx = get_peer_ctx (peer_map, peer);
1866 if (GNUNET_YES != get_peer_flag (peer_ctx, VALID))
1868 if (GNUNET_NO == insert_in_sampler_scheduled (peer_ctx))
1870 out_op.op = insert_in_sampler;
1871 out_op.op_cls = NULL;
1872 GNUNET_array_append (peer_ctx->outstanding_ops,
1873 peer_ctx->num_outstanding_ops,
1877 if (GNUNET_NO == insert_in_gossip_list_scheduled (peer_ctx))
1879 out_op.op = insert_in_gossip_list;
1880 out_op.op_cls = NULL;
1881 GNUNET_array_append (peer_ctx->outstanding_ops,
1882 peer_ctx->num_outstanding_ops,
1886 /* Trigger livelyness test on peer */
1887 check_peer_live (peer_ctx);
1890 // send push/pull to each of those peers?
1896 * Clean the send channel of a peer
1899 peer_clean (const struct GNUNET_PeerIdentity *peer)
1901 struct PeerContext *peer_ctx;
1902 struct GNUNET_CADET_Channel *channel;
1904 if (GNUNET_YES != in_arr (gossip_list, gossip_list_size, peer)
1905 && GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
1907 peer_ctx = get_peer_ctx (peer_map, peer);
1908 if (NULL != peer_ctx->send_channel)
1910 channel = peer_ctx->send_channel;
1911 peer_ctx->send_channel = NULL;
1912 GNUNET_CADET_channel_destroy (channel);
1919 * Callback used to remove peers from the multipeermap.
1922 peer_remove_cb (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1924 struct PeerContext *peer_ctx;
1925 const struct GNUNET_CADET_Channel *channel =
1926 (const struct GNUNET_CADET_Channel *) cls;
1927 struct GNUNET_CADET_Channel *recv;
1928 struct GNUNET_CADET_Channel *send;
1930 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, value))
1932 peer_ctx = (struct PeerContext *) value;
1934 if (0 != peer_ctx->num_outstanding_ops)
1935 GNUNET_array_grow (peer_ctx->outstanding_ops,
1936 peer_ctx->num_outstanding_ops,
1939 if (NULL != peer_ctx->mq)
1941 GNUNET_MQ_destroy (peer_ctx->mq);
1942 peer_ctx->mq = NULL;
1946 if (NULL != peer_ctx->is_live_task)
1948 LOG (GNUNET_ERROR_TYPE_DEBUG,
1949 "Trying to cancle is_live_task for peer %s\n",
1951 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
1952 peer_ctx->is_live_task = NULL;
1955 send = peer_ctx->send_channel;
1956 peer_ctx->send_channel = NULL;
1960 GNUNET_CADET_channel_destroy (send);
1963 recv = peer_ctx->send_channel;
1964 peer_ctx->recv_channel = NULL;
1968 GNUNET_CADET_channel_destroy (recv);
1971 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_remove_all (peer_map, key))
1972 LOG (GNUNET_ERROR_TYPE_WARNING, "removing peer from peer_map failed\n");
1974 GNUNET_free (peer_ctx);
1982 * Task run during shutdown.
1988 shutdown_task (void *cls,
1989 const struct GNUNET_SCHEDULER_TaskContext *tc)
1991 LOG (GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
1993 if (NULL != do_round_task)
1995 GNUNET_SCHEDULER_cancel (do_round_task);
1996 do_round_task = NULL;
2001 if (GNUNET_SYSERR ==
2002 GNUNET_CONTAINER_multipeermap_iterate (peer_map, peer_remove_cb, NULL))
2003 LOG (GNUNET_ERROR_TYPE_WARNING,
2004 "Iterating over peers to disconnect from them was cancelled\n");
2007 GNUNET_NSE_disconnect (nse);
2008 GNUNET_CADET_disconnect (cadet_handle);
2009 RPS_sampler_destroy (prot_sampler);
2010 RPS_sampler_destroy (client_sampler);
2011 LOG (GNUNET_ERROR_TYPE_DEBUG,
2012 "Size of the peermap: %u\n",
2013 GNUNET_CONTAINER_multipeermap_size (peer_map));
2014 GNUNET_break (0 == GNUNET_CONTAINER_multipeermap_size (peer_map));
2015 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
2016 GNUNET_array_grow (gossip_list, gossip_list_size, 0);
2017 GNUNET_array_grow (push_list, push_list_size, 0);
2018 GNUNET_array_grow (pull_list, pull_list_size, 0);
2019 #ifdef ENABLE_MALICIOUS
2020 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
2021 // TODO empty attacked_peers DLL
2022 #endif /* ENABLE_MALICIOUS */
2027 * A client disconnected. Remove all of its data structure entries.
2029 * @param cls closure, NULL
2030 * @param client identification of the client
2033 handle_client_disconnect (void *cls,
2034 struct GNUNET_SERVER_Client * client)
2040 * Handle the channel a peer opens to us.
2042 * @param cls The closure
2043 * @param channel The channel the peer wants to establish
2044 * @param initiator The peer's peer ID
2045 * @param port The port the channel is being established over
2046 * @param options Further options
2049 handle_inbound_channel (void *cls,
2050 struct GNUNET_CADET_Channel *channel,
2051 const struct GNUNET_PeerIdentity *initiator,
2053 enum GNUNET_CADET_ChannelOption options)
2055 struct PeerContext *peer_ctx;
2056 struct GNUNET_PeerIdentity peer;
2059 LOG (GNUNET_ERROR_TYPE_DEBUG,
2060 "New channel was established to us (Peer %s).\n",
2061 GNUNET_i2s (&peer));
2063 GNUNET_assert (NULL != channel);
2065 // we might not even store the recv_channel
2067 peer_ctx = get_peer_ctx (peer_map, &peer);
2068 // FIXME what do we do if a channel is established twice?
2069 // overwrite? Clean old channel? ...?
2070 //if (NULL != peer_ctx->recv_channel)
2072 // peer_ctx->recv_channel = channel;
2074 peer_ctx->recv_channel = channel;
2076 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, &peer, peer_ctx,
2077 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
2079 peer_is_live (peer_ctx);
2081 return NULL; // TODO
2086 * This is called when a remote peer destroys a channel.
2088 * @param cls The closure
2089 * @param channel The channel being closed
2090 * @param channel_ctx The context associated with this channel
2093 cleanup_channel (void *cls,
2094 const struct GNUNET_CADET_Channel *channel,
2097 struct GNUNET_PeerIdentity *peer;
2098 struct PeerContext *peer_ctx;
2100 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
2101 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
2102 // Guess simply casting isn't the nicest way...
2103 // FIXME wait for cadet to change this function
2105 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
2107 peer_ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
2109 if (NULL == peer_ctx) /* It could have been removed by shutdown_task */
2112 if (channel == peer_ctx->send_channel)
2113 { /* Peer probably went down */
2114 LOG (GNUNET_ERROR_TYPE_DEBUG,
2115 "Peer %s destroyed send channel - probably went down, cleaning up\n",
2117 rem_from_list (&gossip_list, &gossip_list_size, peer);
2118 rem_from_list (&pending_pull_reply_list, &pending_pull_reply_list_size, peer);
2120 peer_ctx->send_channel = NULL;
2121 /* Somwewhat {ab,re}use the iterator function */
2122 /* Cast to void is ok, because it's used as void in peer_remove_cb */
2123 (void) peer_remove_cb ((void *) channel, peer, peer_ctx);
2125 else if (channel == peer_ctx->recv_channel)
2126 { /* Other peer doesn't want to send us messages anymore */
2127 LOG (GNUNET_ERROR_TYPE_DEBUG,
2128 "Peer %s destroyed recv channel - cleaning up channel\n",
2130 peer_ctx->recv_channel = NULL;
2137 * Actually start the service.
2140 rps_start (struct GNUNET_SERVER_Handle *server)
2142 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
2143 {&handle_client_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
2144 sizeof (struct GNUNET_RPS_CS_RequestMessage)},
2145 {&handle_client_seed, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_SEED, 0},
2146 #ifdef ENABLE_MALICIOUS
2147 {&handle_client_act_malicious, NULL, GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS , 0},
2148 #endif /* ENABLE_MALICIOUS */
2152 GNUNET_SERVER_add_handlers (server, handlers);
2153 GNUNET_SERVER_disconnect_notify (server,
2154 &handle_client_disconnect,
2156 LOG (GNUNET_ERROR_TYPE_DEBUG, "Ready to receive requests from clients\n");
2159 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
2160 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
2162 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2169 * Process statistics requests.
2171 * @param cls closure
2172 * @param server the initialized server
2173 * @param c configuration to use
2177 struct GNUNET_SERVER_Handle *server,
2178 const struct GNUNET_CONFIGURATION_Handle *c)
2180 // TODO check what this does -- copied from gnunet-boss
2181 // - seems to work as expected
2182 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
2187 GNUNET_CRYPTO_get_peer_identity (cfg, &own_identity); // TODO check return value
2188 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2189 "STARTING SERVICE (rps) for peer [%s]\n",
2190 GNUNET_i2s (&own_identity));
2191 #ifdef ENABLE_MALICIOUS
2192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2193 "Malicious execution compiled in.\n");
2194 #endif /* ENABLE_MALICIOUS */
2198 /* Get time interval from the configuration */
2199 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
2203 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read ROUNDINTERVAL from config\n");
2204 GNUNET_SCHEDULER_shutdown ();
2208 /* Get initial size of sampler/gossip list from the configuration */
2209 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "RPS",
2211 (long long unsigned int *) &sampler_size_est_need))
2213 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read INITSIZE from config\n");
2214 GNUNET_SCHEDULER_shutdown ();
2217 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", sampler_size_est_need);
2223 /* connect to NSE */
2224 nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
2225 // TODO check whether that was successful
2226 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to NSE\n");
2232 peer_map = GNUNET_CONTAINER_multipeermap_create (sampler_size_est_need, GNUNET_NO);
2235 /* Initialise cadet */
2236 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
2237 {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH ,
2238 sizeof (struct GNUNET_MessageHeader)},
2239 {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
2240 sizeof (struct GNUNET_MessageHeader)},
2241 {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY , 0},
2245 const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
2246 cadet_handle = GNUNET_CADET_connect (cfg,
2248 &handle_inbound_channel,
2252 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to CADET\n");
2255 /* Initialise sampler */
2256 struct GNUNET_TIME_Relative half_round_interval;
2257 struct GNUNET_TIME_Relative max_round_interval;
2259 half_round_interval = GNUNET_TIME_relative_multiply (round_interval, .5);
2260 max_round_interval = GNUNET_TIME_relative_add (round_interval, half_round_interval);
2262 prot_sampler = RPS_sampler_init (sampler_size_est_need, max_round_interval);
2263 client_sampler = RPS_sampler_init (sampler_size_est_need, max_round_interval);
2265 /* Initialise push and pull maps */
2270 pending_pull_reply_list = NULL;
2271 pending_pull_reply_list_size = 0;
2274 num_hist_update_tasks = 0;
2277 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
2278 GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, NULL);
2279 // TODO send push/pull to each of those peers?
2287 * The main function for the rps service.
2289 * @param argc number of arguments from the command line
2290 * @param argv command line arguments
2291 * @return 0 ok, 1 on error
2294 main (int argc, char *const *argv)
2296 return (GNUNET_OK ==
2297 GNUNET_SERVICE_run (argc,
2300 GNUNET_SERVICE_OPTION_NONE,
2301 &run, NULL)) ? 0 : 1;
2304 /* end of gnunet-service-rps.c */