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 * Hashmap of malicious peers used as set.
376 * Used to more efficiently check whether we know that peer.
378 static struct GNUNET_CONTAINER_MultiPeerMap *mal_peer_set = NULL;
381 * Number of other malicious peers
383 static uint32_t num_mal_peers = 0;
387 * If type is 2 This struct is used to store the attacked peers in a DLL
394 struct AttackedPeer *next;
395 struct AttackedPeer *prev;
400 struct GNUNET_PeerIdentity peer_id;
404 * If type is 2 this is the DLL of attacked peers
406 static struct AttackedPeer *att_peers_head = NULL;
407 static struct AttackedPeer *att_peers_tail = NULL;
410 * This index is used to point to an attacked peer to
411 * implement the round-robin-ish way to select attacked peers.
413 static struct AttackedPeer *att_peer_index = NULL;
416 * Hashmap of attacked peers used as set.
417 * Used to more efficiently check whether we know that peer.
419 static struct GNUNET_CONTAINER_MultiPeerMap *att_peer_set = NULL;
422 * Number of attacked peers
424 static uint32_t num_attacked_peers = 0;
428 * If type is 1 this is the attacked peer
430 static struct GNUNET_PeerIdentity attacked_peer;
433 * The limit of PUSHes we can send in one round.
434 * This is an assumption of the Brahms protocol and either implemented
437 * assumend to be the bandwidth limitation.
439 static uint32_t push_limit = 10000;
440 #endif /* ENABLE_MALICIOUS */
443 /***********************************************************************
445 ***********************************************************************/
452 /***********************************************************************
454 ***********************************************************************/
457 * Set a peer flag of given peer context.
459 #define set_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags |= mask)
462 * Get peer flag of given peer context.
464 #define get_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags & mask ? GNUNET_YES : GNUNET_NO)
467 * Unset flag of given peer context.
469 #define unset_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags &= (~mask))
472 * Compute the minimum of two ints
474 #define min(x, y) ((x < y) ? x : y)
477 * Clean the send channel of a peer
480 peer_clean (const struct GNUNET_PeerIdentity *peer);
484 * Check if peer is already in peer array.
487 in_arr (const struct GNUNET_PeerIdentity *array,
488 unsigned int arr_size,
489 const struct GNUNET_PeerIdentity *peer)
491 GNUNET_assert (NULL != peer);
496 GNUNET_assert (NULL != array);
500 for (i = 0; i < arr_size ; i++)
501 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&array[i], peer))
508 * Print peerlist to log.
511 print_peer_list (struct GNUNET_PeerIdentity *list, unsigned int len)
515 LOG (GNUNET_ERROR_TYPE_DEBUG,
516 "Printing peer list of length %u at %p:\n",
519 for (i = 0 ; i < len ; i++)
521 LOG (GNUNET_ERROR_TYPE_DEBUG,
523 i, GNUNET_i2s (&list[i]));
529 * Remove peer from list.
532 rem_from_list (struct GNUNET_PeerIdentity **peer_list,
533 unsigned int *list_size,
534 const struct GNUNET_PeerIdentity *peer)
537 struct GNUNET_PeerIdentity *tmp;
541 LOG (GNUNET_ERROR_TYPE_DEBUG,
542 "Removing peer %s from list at %p\n",
546 for ( i = 0 ; i < *list_size ; i++ )
548 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&tmp[i], peer))
550 if (i < *list_size -1)
551 { /* Not at the last entry -- shift peers left */
552 memcpy (&tmp[i], &tmp[i +1],
553 ((*list_size) - i -1) * sizeof (struct GNUNET_PeerIdentity));
555 /* Remove last entry (should be now useless PeerID) */
556 GNUNET_array_grow (tmp, *list_size, (*list_size) -1);
563 * Get random peer from the given list but don't return one from the @a ignore_list.
565 struct GNUNET_PeerIdentity *
566 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list,
568 const struct GNUNET_PeerIdentity *ignore_list,
569 uint32_t ignore_size)
573 struct GNUNET_PeerIdentity *tmp_peer_list;
574 struct GNUNET_PeerIdentity *peer;
576 GNUNET_assert (NULL != peer_list);
581 tmp_peer_list = NULL;
582 GNUNET_array_grow (tmp_peer_list, tmp_size, list_size);
583 memcpy (tmp_peer_list,
585 list_size * sizeof (struct GNUNET_PeerIdentity));
586 peer = GNUNET_new (struct GNUNET_PeerIdentity);
589 * Choose the r_index of the peer we want to return
590 * at random from the interval of the gossip list
592 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
594 *peer = tmp_peer_list[r_index];
596 while (in_arr (ignore_list, ignore_size, peer))
598 rem_from_list (&tmp_peer_list, &tmp_size, peer);
600 print_peer_list (tmp_peer_list, tmp_size);
609 * Choose the r_index of the peer we want to return
610 * at random from the interval of the gossip list
612 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
614 *peer = tmp_peer_list[r_index];
618 GNUNET_array_grow (tmp_peer_list, tmp_size, 0);
625 * Get the context of a peer. If not existing, create.
628 get_peer_ctx (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
629 const struct GNUNET_PeerIdentity *peer)
631 struct PeerContext *ctx;
633 if ( GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
635 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
639 ctx = GNUNET_new (struct PeerContext);
642 ctx->send_channel = NULL;
643 ctx->recv_channel = NULL;
644 ctx->outstanding_ops = NULL;
645 ctx->num_outstanding_ops = 0;
646 ctx->is_live_task = NULL;
647 ctx->peer_id = *peer;
648 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
649 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
656 * Put random peer from sampler into the gossip list as history update.
659 hist_update (void *cls, struct GNUNET_PeerIdentity *ids, uint32_t num_peers)
661 GNUNET_assert (1 == num_peers);
663 if (gossip_list_size < sampler_size_est_need)
664 GNUNET_array_append (gossip_list, gossip_list_size, *ids);
666 if (0 < num_hist_update_tasks)
667 num_hist_update_tasks--;
672 * Set the peer flag to living and call the outstanding operations on this peer.
675 peer_is_live (struct PeerContext *peer_ctx)
677 struct GNUNET_PeerIdentity *peer;
679 /* Cancle is_live_task if still scheduled */
680 if (NULL != peer_ctx->is_live_task)
682 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
683 peer_ctx->is_live_task = NULL;
686 peer = &peer_ctx->peer_id;
687 set_peer_flag (peer_ctx, VALID);
689 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer %s is live\n", GNUNET_i2s (peer));
691 if (0 < peer_ctx->num_outstanding_ops)
692 { /* Call outstanding operations */
695 for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
696 peer_ctx->outstanding_ops[i].op (peer_ctx->outstanding_ops[i].op_cls, peer);
697 GNUNET_array_grow (peer_ctx->outstanding_ops, peer_ctx->num_outstanding_ops, 0);
705 * Callback that is called when a channel was effectively established.
706 * This is given to ntfy_tmt_rdy and called when the channel was
707 * successfully established.
710 cadet_ntfy_tmt_rdy_cb (void *cls, size_t size, void *buf)
712 struct PeerContext *peer_ctx = (struct PeerContext *) cls;
714 peer_ctx->is_live_task = NULL;
715 LOG (GNUNET_ERROR_TYPE_DEBUG,
716 "Set ->is_live_task = NULL for peer %s\n",
717 GNUNET_i2s (&peer_ctx->peer_id));
722 peer_is_live (peer_ctx);
726 LOG (GNUNET_ERROR_TYPE_WARNING,
727 "Problems establishing a connection to peer %s in order to check liveliness\n",
728 GNUNET_i2s (&peer_ctx->peer_id));
729 // TODO reschedule? cleanup?
732 //if (NULL != peer_ctx->is_live_task)
734 // LOG (GNUNET_ERROR_TYPE_DEBUG,
735 // "Trying to cancle is_live_task for peer %s\n",
736 // GNUNET_i2s (&peer_ctx->peer_id));
737 // GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
738 // peer_ctx->is_live_task = NULL;
746 * Get the channel of a peer. If not existing, create.
748 struct GNUNET_CADET_Channel *
749 get_channel (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
750 const struct GNUNET_PeerIdentity *peer)
752 struct PeerContext *peer_ctx;
754 peer_ctx = get_peer_ctx (peer_map, peer);
756 GNUNET_assert (NULL == peer_ctx->is_live_task);
758 if (NULL == peer_ctx->send_channel)
760 LOG (GNUNET_ERROR_TYPE_DEBUG,
761 "Trying to establish channel to peer %s\n",
764 peer_ctx->send_channel =
765 GNUNET_CADET_channel_create (cadet_handle,
768 GNUNET_RPS_CADET_PORT,
769 GNUNET_CADET_OPTION_RELIABLE);
771 // do I have to explicitly put it in the peer_map?
772 (void) GNUNET_CONTAINER_multipeermap_put
776 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
778 return peer_ctx->send_channel;
783 * Get the message queue of a specific peer.
785 * If we already have a message queue open to this client,
786 * simply return it, otherways create one.
788 struct GNUNET_MQ_Handle *
789 get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
790 const struct GNUNET_PeerIdentity *peer_id)
792 struct PeerContext *peer_ctx;
794 peer_ctx = get_peer_ctx (peer_map, peer_id);
796 GNUNET_assert (NULL == peer_ctx->is_live_task);
798 if (NULL == peer_ctx->mq)
800 (void) get_channel (peer_map, peer_id);
801 peer_ctx->mq = GNUNET_CADET_mq_create (peer_ctx->send_channel);
802 //do I have to explicitly put it in the peer_map?
803 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer_id, peer_ctx,
804 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
811 * Issue check whether peer is live
813 * @param peer_ctx the context of the peer
816 check_peer_live (struct PeerContext *peer_ctx)
818 (void) get_channel (peer_map, &peer_ctx->peer_id);
819 LOG (GNUNET_ERROR_TYPE_DEBUG,
820 "Get informed about peer %s getting live\n",
821 GNUNET_i2s (&peer_ctx->peer_id));
822 if (NULL == peer_ctx->is_live_task)
824 peer_ctx->is_live_task =
825 GNUNET_CADET_notify_transmit_ready (peer_ctx->send_channel,
827 GNUNET_TIME_UNIT_FOREVER_REL,
828 sizeof (struct GNUNET_MessageHeader),
829 cadet_ntfy_tmt_rdy_cb,
834 LOG (GNUNET_ERROR_TYPE_DEBUG,
835 "Already waiting for notification\n");
841 * Sum all time relatives of an array.
843 struct GNUNET_TIME_Relative
844 T_relative_sum (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
846 struct GNUNET_TIME_Relative sum;
849 sum = GNUNET_TIME_UNIT_ZERO;
850 for ( i = 0 ; i < arr_size ; i++ )
852 sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
859 * Compute the average of given time relatives.
861 struct GNUNET_TIME_Relative
862 T_relative_avg (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
864 return GNUNET_TIME_relative_divide (T_relative_sum (rel_array, arr_size), arr_size);
869 * Insert PeerID in #pull_list
871 * Called once we know a peer is live.
874 insert_in_pull_list (void *cls, const struct GNUNET_PeerIdentity *peer)
876 if (GNUNET_NO == in_arr (pull_list, pull_list_size, peer))
877 GNUNET_array_append (pull_list, pull_list_size, *peer);
883 * Check whether #insert_in_pull_list was already scheduled
886 insert_in_pull_list_scheduled (const struct PeerContext *peer_ctx)
890 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
891 if (insert_in_pull_list == peer_ctx->outstanding_ops[i].op)
898 * Insert PeerID in #gossip_list
900 * Called once we know a peer is live.
903 insert_in_gossip_list (void *cls, const struct GNUNET_PeerIdentity *peer)
905 if (GNUNET_NO == in_arr (gossip_list, gossip_list_size, peer))
906 GNUNET_array_append (gossip_list, gossip_list_size, *peer);
908 (void) get_channel (peer_map, peer);
912 * Check whether #insert_in_gossip_list was already scheduled
915 insert_in_gossip_list_scheduled (const struct PeerContext *peer_ctx)
919 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
920 if (insert_in_gossip_list == peer_ctx->outstanding_ops[i].op)
927 * Update sampler with given PeerID.
930 insert_in_sampler (void *cls, const struct GNUNET_PeerIdentity *peer)
932 LOG (GNUNET_ERROR_TYPE_DEBUG,
933 "Updating samplers with peer %s from insert_in_sampler()\n",
935 RPS_sampler_update (prot_sampler, peer);
936 RPS_sampler_update (client_sampler, peer);
941 * Check whether #insert_in_sampler was already scheduled
944 insert_in_sampler_scheduled (const struct PeerContext *peer_ctx)
948 for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
949 if (insert_in_sampler== peer_ctx->outstanding_ops[i].op)
956 * Wrapper around #RPS_sampler_resize()
958 * If we do not have enough sampler elements, double current sampler size
959 * If we have more than enough sampler elements, halv current sampler size
962 resize_wrapper (struct RPS_Sampler *sampler, uint32_t new_size)
964 unsigned int sampler_size;
967 // TODO respect the min, max
968 sampler_size = RPS_sampler_get_size (sampler);
969 if (sampler_size > new_size * 4)
971 RPS_sampler_resize (sampler, sampler_size / 2);
973 else if (sampler_size < new_size)
975 RPS_sampler_resize (sampler, sampler_size * 2);
977 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
982 * Wrapper around #RPS_sampler_resize() resizing the client sampler
985 client_resize_wrapper ()
987 uint32_t bigger_size;
988 unsigned int sampler_size;
992 sampler_size = RPS_sampler_get_size (client_sampler);
994 if (sampler_size_est_need > sampler_size_client_need)
995 bigger_size = sampler_size_est_need;
997 bigger_size = sampler_size_client_need;
999 // TODO respect the min, max
1000 resize_wrapper (client_sampler, bigger_size);
1001 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
1006 * Estimate request rate
1008 * Called every time we receive a request from the client.
1013 struct GNUNET_TIME_Relative max_round_duration;
1015 if (request_deltas_size > req_counter)
1017 if ( 1 < req_counter)
1019 /* Shift last request deltas to the right */
1020 memcpy (&request_deltas[1],
1022 (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
1024 /* Add current delta to beginning */
1026 GNUNET_TIME_absolute_get_difference (last_request,
1027 GNUNET_TIME_absolute_get ());
1028 request_rate = T_relative_avg (request_deltas, req_counter);
1030 /* Compute the duration a round will maximally take */
1031 max_round_duration =
1032 GNUNET_TIME_relative_add (round_interval,
1033 GNUNET_TIME_relative_divide (round_interval, 2));
1035 /* Set the estimated size the sampler has to have to
1036 * satisfy the current client request rate */
1037 sampler_size_client_need =
1038 max_round_duration.rel_value_us / request_rate.rel_value_us;
1040 /* Resize the sampler */
1041 client_resize_wrapper ();
1043 last_request = GNUNET_TIME_absolute_get ();
1048 * Add all peers in @a peer_array to @peer_map used as set.
1050 * @param peer_array array containing the peers
1051 * @param num_peers number of peers in @peer_array
1052 * @param peer_map the peermap to use as set
1055 add_peer_array_to_set (const struct GNUNET_PeerIdentity *peer_array,
1056 unsigned int num_peers,
1057 struct GNUNET_CONTAINER_MultiPeerMap *peer_map)
1060 if (NULL == peer_map)
1061 peer_map = GNUNET_CONTAINER_multipeermap_create (num_peers,
1063 for (i = 0 ; i < num_peers ; i++)
1065 GNUNET_CONTAINER_multipeermap_put (peer_map,
1068 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1074 * Send a PULL REPLY to @a peer_id
1076 * @param peer_id the peer to send the reply to.
1077 * @param peer_ids the peers to send to @a peer_id
1078 * @param num_peer_ids the number of peers to send to @a peer_id
1081 send_pull_reply (const struct GNUNET_PeerIdentity *peer_id,
1082 const struct GNUNET_PeerIdentity *peer_ids,
1083 unsigned int num_peer_ids)
1086 struct GNUNET_MQ_Handle *mq;
1087 struct GNUNET_MQ_Envelope *ev;
1088 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
1090 /* Compute actual size */
1091 send_size = sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) +
1092 num_peer_ids * sizeof (struct GNUNET_PeerIdentity);
1094 if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size)
1095 /* Compute number of peers to send
1096 * If too long, simply truncate */
1097 // TODO select random ones via permutation
1098 // or even better: do good protocol design
1100 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE -
1101 sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1102 sizeof (struct GNUNET_PeerIdentity);
1104 send_size = num_peer_ids;
1106 LOG (GNUNET_ERROR_TYPE_DEBUG,
1107 "PULL REQUEST from peer %s received, going to send %u peers\n",
1108 GNUNET_i2s (peer_id), send_size);
1110 mq = get_mq (peer_map, peer_id);
1112 ev = GNUNET_MQ_msg_extra (out_msg,
1113 send_size * sizeof (struct GNUNET_PeerIdentity),
1114 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
1115 out_msg->num_peers = htonl (send_size);
1116 memcpy (&out_msg[1], peer_ids,
1117 send_size * sizeof (struct GNUNET_PeerIdentity));
1119 GNUNET_MQ_send (mq, ev);
1123 /***********************************************************************
1125 ***********************************************************************/
1132 * Function called by NSE.
1134 * Updates sizes of sampler list and gossip list and adapt those lists
1138 nse_callback (void *cls, struct GNUNET_TIME_Absolute timestamp,
1139 double logestimate, double std_dev)
1142 //double scale; // TODO this might go gloabal/config
1144 LOG (GNUNET_ERROR_TYPE_DEBUG,
1145 "Received a ns estimate - logest: %f, std_dev: %f (old_size: %u)\n",
1146 logestimate, std_dev, RPS_sampler_get_size (prot_sampler));
1148 estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
1149 // GNUNET_NSE_log_estimate_to_n (logestimate);
1150 estimate = pow (estimate, 1.0 / 3);
1151 // TODO add if std_dev is a number
1152 // estimate += (std_dev * scale);
1153 if (2 < ceil (estimate))
1155 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
1156 sampler_size_est_need = estimate;
1158 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
1160 /* If the NSE has changed adapt the lists accordingly */
1161 resize_wrapper (prot_sampler, sampler_size_est_need);
1162 client_resize_wrapper ();
1167 * Callback called once the requested PeerIDs are ready.
1169 * Sends those to the requesting client.
1171 void client_respond (void *cls,
1172 struct GNUNET_PeerIdentity *ids, uint32_t num_peers)
1174 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler returned %" PRIX32 " peers\n", num_peers);
1175 struct GNUNET_MQ_Envelope *ev;
1176 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
1177 struct GNUNET_SERVER_Client *client;
1178 uint32_t size_needed;
1179 struct client_ctx *cli_ctx;
1181 client = (struct GNUNET_SERVER_Client *) cls;
1183 size_needed = sizeof (struct GNUNET_RPS_CS_ReplyMessage) +
1184 num_peers * sizeof (struct GNUNET_PeerIdentity);
1186 GNUNET_assert (GNUNET_SERVER_MAX_MESSAGE_SIZE >= size_needed);
1188 ev = GNUNET_MQ_msg_extra (out_msg,
1189 num_peers * sizeof (struct GNUNET_PeerIdentity),
1190 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
1191 out_msg->num_peers = htonl (num_peers);
1193 memcpy (&out_msg[1],
1195 num_peers * sizeof (struct GNUNET_PeerIdentity));
1198 cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct client_ctx);
1199 if (NULL == cli_ctx) {
1200 cli_ctx = GNUNET_new (struct client_ctx);
1201 cli_ctx->mq = GNUNET_MQ_queue_for_server_client (client);
1202 GNUNET_SERVER_client_set_user_context (client, cli_ctx);
1205 GNUNET_MQ_send (cli_ctx->mq, ev);
1210 * Handle RPS request from the client.
1212 * @param cls closure
1213 * @param client identification of the client
1214 * @param message the actual message
1217 handle_client_request (void *cls,
1218 struct GNUNET_SERVER_Client *client,
1219 const struct GNUNET_MessageHeader *message)
1221 struct GNUNET_RPS_CS_RequestMessage *msg;
1223 uint32_t size_needed;
1226 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
1228 num_peers = ntohl (msg->num_peers);
1229 size_needed = sizeof (struct GNUNET_RPS_CS_RequestMessage) +
1230 num_peers * sizeof (struct GNUNET_PeerIdentity);
1232 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
1234 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1238 for (i = 0 ; i < num_peers ; i++)
1241 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client requested %" PRIX32 " random peer(s).\n", num_peers);
1243 RPS_sampler_get_n_rand_peers (client_sampler, client_respond,
1244 client, num_peers, GNUNET_YES);
1246 GNUNET_SERVER_receive_done (client,
1252 * Handle seed from the client.
1254 * @param cls closure
1255 * @param client identification of the client
1256 * @param message the actual message
1259 handle_client_seed (void *cls,
1260 struct GNUNET_SERVER_Client *client,
1261 const struct GNUNET_MessageHeader *message)
1263 struct GNUNET_RPS_CS_SeedMessage *in_msg;
1264 struct GNUNET_PeerIdentity *peers;
1268 if (sizeof (struct GNUNET_RPS_CS_SeedMessage) > ntohs (message->size))
1270 GNUNET_break_op (0);
1271 GNUNET_SERVER_receive_done (client,
1275 in_msg = (struct GNUNET_RPS_CS_SeedMessage *) message;
1276 num_peers = ntohl (in_msg->num_peers);
1277 peers = (struct GNUNET_PeerIdentity *) &in_msg[1];
1278 //peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1279 //memcpy (peers, &in_msg[1], num_peers * sizeof (struct GNUNET_PeerIdentity));
1281 if ((ntohs (message->size) - sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
1282 sizeof (struct GNUNET_PeerIdentity) != num_peers)
1284 GNUNET_break_op (0);
1285 GNUNET_SERVER_receive_done (client,
1289 LOG (GNUNET_ERROR_TYPE_DEBUG,
1290 "Client seeded peers:\n");
1291 print_peer_list (peers, num_peers);
1293 // TODO check for validity of ids
1295 for (i = 0 ; i < num_peers ; i++)
1297 LOG (GNUNET_ERROR_TYPE_DEBUG,
1298 "Updating samplers with seed %" PRIX32 ": %s\n",
1300 GNUNET_i2s (&peers[i]));
1302 RPS_sampler_update (prot_sampler, &peers[i]);
1303 RPS_sampler_update (client_sampler, &peers[i]);
1306 //GNUNET_free (peers);
1308 GNUNET_SERVER_receive_done (client,
1314 * Handle a PUSH message from another peer.
1316 * Check the proof of work and store the PeerID
1317 * in the temporary list for pushed PeerIDs.
1319 * @param cls Closure
1320 * @param channel The channel the PUSH was received over
1321 * @param channel_ctx The context associated with this channel
1322 * @param msg The message header
1325 handle_peer_push (void *cls,
1326 struct GNUNET_CADET_Channel *channel,
1328 const struct GNUNET_MessageHeader *msg)
1330 const struct GNUNET_PeerIdentity *peer;
1332 // (check the proof of work)
1334 peer = (const struct GNUNET_PeerIdentity *)
1335 GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
1336 // FIXME wait for cadet to change this function
1338 LOG (GNUNET_ERROR_TYPE_DEBUG, "PUSH received (%s)\n", GNUNET_i2s (peer));
1340 #ifdef ENABLE_MALICIOUS
1341 struct AttackedPeer *tmp_att_peer;
1343 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1344 memcpy (&tmp_att_peer->peer_id, peer, sizeof (struct GNUNET_PeerIdentity));
1346 { /* Try to maximise representation */
1347 if (NULL == att_peer_set)
1348 att_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
1349 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1352 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1355 add_peer_array_to_set (peer, 1, att_peer_set);
1361 else if (2 == mal_type)
1362 { /* We attack one single well-known peer - simply ignore */
1366 #endif /* ENABLE_MALICIOUS */
1368 /* Add the sending peer to the push_list */
1369 if (GNUNET_NO == in_arr (push_list, push_list_size, peer))
1370 GNUNET_array_append (push_list, push_list_size, *peer);
1377 * Handle PULL REQUEST request message from another peer.
1379 * Reply with the gossip list of PeerIDs.
1381 * @param cls Closure
1382 * @param channel The channel the PUSH was received over
1383 * @param channel_ctx The context associated with this channel
1384 * @param msg The message header
1387 handle_peer_pull_request (void *cls,
1388 struct GNUNET_CADET_Channel *channel,
1390 const struct GNUNET_MessageHeader *msg)
1392 struct GNUNET_PeerIdentity *peer;
1394 peer = (struct GNUNET_PeerIdentity *)
1395 GNUNET_CADET_channel_get_info (channel,
1396 GNUNET_CADET_OPTION_PEER);
1397 // FIXME wait for cadet to change this function
1399 #ifdef ENABLE_MALICIOUS
1401 { /* Try to maximise representation */
1402 send_pull_reply (peer, mal_peers, num_mal_peers);
1406 else if (2 == mal_type)
1407 { /* Try to partition network */
1408 if (GNUNET_YES == GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
1410 send_pull_reply (peer, mal_peers, num_mal_peers);
1414 #endif /* ENABLE_MALICIOUS */
1416 send_pull_reply (peer, gossip_list, gossip_list_size);
1423 * Handle PULL REPLY message from another peer.
1425 * Check whether we sent a corresponding request and
1426 * whether this reply is the first one.
1428 * @param cls Closure
1429 * @param channel The channel the PUSH was received over
1430 * @param channel_ctx The context associated with this channel
1431 * @param msg The message header
1434 handle_peer_pull_reply (void *cls,
1435 struct GNUNET_CADET_Channel *channel,
1437 const struct GNUNET_MessageHeader *msg)
1439 LOG (GNUNET_ERROR_TYPE_DEBUG, "PULL REPLY received\n");
1441 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
1442 struct GNUNET_PeerIdentity *peers;
1443 struct PeerContext *peer_ctx;
1444 struct GNUNET_PeerIdentity *sender;
1445 struct PeerContext *sender_ctx;
1446 struct PeerOutstandingOp out_op;
1449 /* Check for protocol violation */
1450 if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->size))
1452 GNUNET_break_op (0);
1453 return GNUNET_SYSERR;
1456 in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
1457 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1458 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1460 LOG (GNUNET_ERROR_TYPE_ERROR,
1461 "message says it sends %" PRIu64 " peers, have space for %i peers\n",
1462 ntohl (in_msg->num_peers),
1463 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1464 sizeof (struct GNUNET_PeerIdentity));
1465 GNUNET_break_op (0);
1466 return GNUNET_SYSERR;
1469 sender = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
1470 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
1471 // Guess simply casting isn't the nicest way...
1472 // FIXME wait for cadet to change this function
1473 sender_ctx = get_peer_ctx (peer_map, sender);
1475 if (GNUNET_YES == get_peer_flag (sender_ctx, PULL_REPLY_PENDING))
1477 GNUNET_break_op (0);
1482 /* Do actual logic */
1483 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1484 for (i = 0 ; i < ntohl (in_msg->num_peers) ; i++)
1486 peer_ctx = get_peer_ctx (peer_map, &peers[i]);
1487 if (GNUNET_YES == get_peer_flag (peer_ctx, VALID)
1488 || NULL != peer_ctx->send_channel
1489 || NULL != peer_ctx->recv_channel)
1491 if (GNUNET_NO == in_arr (pull_list, pull_list_size, &peers[i])
1492 && 0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peers[i]))
1493 GNUNET_array_append (pull_list, pull_list_size, peers[i]);
1495 else if (GNUNET_NO == insert_in_pull_list_scheduled (peer_ctx))
1497 out_op.op = insert_in_pull_list;
1498 out_op.op_cls = NULL;
1499 GNUNET_array_append (peer_ctx->outstanding_ops,
1500 peer_ctx->num_outstanding_ops,
1502 check_peer_live (peer_ctx);
1506 unset_peer_flag (sender_ctx, PULL_REPLY_PENDING);
1507 rem_from_list (&pending_pull_reply_list, &pending_pull_reply_list_size, sender);
1514 * Compute a random delay.
1515 * A uniformly distributed value between mean + spread and mean - spread.
1517 * For example for mean 4 min and spread 2 the minimum is (4 min - (1/2 * 4 min))
1518 * It would return a random value between 2 and 6 min.
1520 * @param mean the mean
1521 * @param spread the inverse amount of deviation from the mean
1523 static struct GNUNET_TIME_Relative
1524 compute_rand_delay (struct GNUNET_TIME_Relative mean, unsigned int spread)
1526 struct GNUNET_TIME_Relative half_interval;
1527 struct GNUNET_TIME_Relative ret;
1528 unsigned int rand_delay;
1529 unsigned int max_rand_delay;
1533 LOG (GNUNET_ERROR_TYPE_WARNING,
1534 "Not accepting spread of 0\n");
1538 /* Compute random time value between spread * mean and spread * mean */
1539 half_interval = GNUNET_TIME_relative_divide (mean, spread);
1541 max_rand_delay = GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us / mean.rel_value_us * (2/spread);
1543 * Compute random value between (0 and 1) * round_interval
1544 * via multiplying round_interval with a 'fraction' (0 to value)/value
1546 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_rand_delay);
1547 ret = GNUNET_TIME_relative_multiply (mean, rand_delay);
1548 ret = GNUNET_TIME_relative_divide (ret, max_rand_delay);
1549 ret = GNUNET_TIME_relative_add (ret, half_interval);
1551 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ret.rel_value_us)
1552 LOG (GNUNET_ERROR_TYPE_WARNING,
1553 "Returning FOREVER_REL\n");
1560 * Send single pull request
1562 * @param peer_id the peer to send the pull request to.
1565 send_pull_request (struct GNUNET_PeerIdentity *peer_id)
1567 struct GNUNET_MQ_Envelope *ev;
1568 struct GNUNET_MQ_Handle *mq;
1570 LOG (GNUNET_ERROR_TYPE_DEBUG,
1571 "Sending PULL request to peer %s of gossiped list.\n",
1572 GNUNET_i2s (peer_id));
1574 GNUNET_array_append (pending_pull_reply_list, pending_pull_reply_list_size, *peer_id);
1576 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
1577 mq = get_mq (peer_map, peer_id);
1578 GNUNET_MQ_send (mq, ev);
1585 * @param peer_id the peer to send the push to.
1588 send_push (struct GNUNET_PeerIdentity *peer_id)
1590 struct GNUNET_MQ_Envelope *ev;
1591 struct GNUNET_MQ_Handle *mq;
1593 LOG (GNUNET_ERROR_TYPE_DEBUG,
1594 "Sending PUSH to peer %s of gossiped list.\n",
1595 GNUNET_i2s (peer_id));
1597 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
1598 mq = get_mq (peer_map, peer_id);
1599 GNUNET_MQ_send (mq, ev);
1604 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1607 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1610 #ifdef ENABLE_MALICIOUS
1612 * Turn RPS service to act malicious.
1614 * @param cls Closure
1615 * @param channel The channel the PUSH was received over
1616 * @param channel_ctx The context associated with this channel
1617 * @param msg The message header
1620 handle_client_act_malicious (void *cls,
1621 struct GNUNET_SERVER_Client *client,
1622 const struct GNUNET_MessageHeader *msg)
1624 struct GNUNET_RPS_CS_ActMaliciousMessage *in_msg;
1625 struct GNUNET_PeerIdentity *peers;
1626 uint32_t num_mal_peers_sent;
1627 uint32_t num_mal_peers_old;
1629 /* Check for protocol violation */
1630 if (sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage) > ntohs (msg->size))
1632 GNUNET_break_op (0);
1635 in_msg = (struct GNUNET_RPS_CS_ActMaliciousMessage *) msg;
1636 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
1637 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1639 LOG (GNUNET_ERROR_TYPE_ERROR,
1640 "message says it sends %" PRIu64 " peers, have space for %i peers\n",
1641 ntohl (in_msg->num_peers),
1642 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
1643 sizeof (struct GNUNET_PeerIdentity));
1644 GNUNET_break_op (0);
1648 /* Do actual logic */
1649 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1650 mal_type = ntohl (in_msg->type);
1652 LOG (GNUNET_ERROR_TYPE_DEBUG,
1653 "Now acting malicious type %" PRIu32 "\n",
1657 { /* Try to maximise representation */
1658 /* Add other malicious peers to those we already know */
1660 num_mal_peers_sent = ntohl (in_msg->num_peers);
1661 num_mal_peers_old = num_mal_peers;
1662 GNUNET_array_grow (mal_peers,
1664 num_mal_peers + num_mal_peers_sent);
1665 memcpy (&mal_peers[num_mal_peers_old],
1667 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1669 /* Add all mal peers to mal_peer_set */
1670 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1674 /* Substitute do_round () with do_mal_round () */
1675 GNUNET_SCHEDULER_cancel (do_round_task);
1676 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1679 else if (2 == mal_type)
1680 { /* Try to partition the network */
1681 /* Add other malicious peers to those we already know */
1682 num_mal_peers_sent = ntohl (in_msg->num_peers) - 1;
1683 num_mal_peers_old = num_mal_peers;
1684 GNUNET_array_grow (mal_peers,
1686 num_mal_peers + num_mal_peers_sent);
1687 memcpy (&mal_peers[num_mal_peers_old],
1689 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1691 /* Add all mal peers to mal_peer_set */
1692 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1696 /* Store the one attacked peer */
1697 memcpy (&attacked_peer,
1698 &peers[num_mal_peers_sent],
1699 sizeof (struct GNUNET_PeerIdentity));
1701 LOG (GNUNET_ERROR_TYPE_DEBUG,
1702 "Attacked peer is %s\n",
1703 GNUNET_i2s (&attacked_peer));
1705 /* Substitute do_round () with do_mal_round () */
1706 GNUNET_SCHEDULER_cancel (do_round_task);
1707 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1709 else if (0 == mal_type)
1710 { /* Stop acting malicious */
1712 GNUNET_free (mal_peers);
1714 /* Substitute do_mal_round () with do_round () */
1715 GNUNET_SCHEDULER_cancel (do_round_task);
1716 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
1726 * Send out PUSHes and PULLs maliciously.
1728 * This is executed regylary.
1731 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1733 uint32_t num_pushes;
1735 struct GNUNET_TIME_Relative time_next_round;
1736 struct AttackedPeer *tmp_att_peer;
1738 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round maliciously.\n");
1740 /* Do malicious actions */
1742 { /* Try to maximise representation */
1744 /* The maximum of pushes we're going to send this round */
1745 num_pushes = min (min (push_limit,
1746 num_attacked_peers),
1747 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
1749 /* Send PUSHes to attacked peers */
1750 for (i = 0 ; i < num_pushes ; i++)
1752 if (att_peers_tail == att_peer_index)
1753 att_peer_index = att_peers_head;
1755 att_peer_index = att_peer_index->next;
1757 send_push (&att_peer_index->peer_id);
1760 /* Send PULLs to some peers to learn about additional peers to attack */
1761 for (i = 0 ; i < num_pushes * alpha ; i++)
1763 if (att_peers_tail == tmp_att_peer)
1764 tmp_att_peer = att_peers_head;
1766 att_peer_index = tmp_att_peer->next;
1768 send_pull_request (&tmp_att_peer->peer_id);
1773 else if (2 == mal_type)
1775 * Try to partition the network
1776 * Send as many pushes to the attacked peer as possible
1777 * That is one push per round as it will ignore more.
1779 send_push (&attacked_peer);
1783 /* Schedule next round */
1784 time_next_round = compute_rand_delay (round_interval, 2);
1786 //do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_mal_round, NULL);
1787 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_mal_round, NULL);
1788 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
1790 #endif /* ENABLE_MALICIOUS */
1794 * Send out PUSHes and PULLs, possibly update #gossip_list, samplers.
1796 * This is executed regylary.
1799 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1801 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round.\n");
1804 unsigned int *permut;
1805 unsigned int n_peers; /* Number of peers we send pushes/pulls to */
1806 struct GNUNET_PeerIdentity peer;
1807 struct GNUNET_PeerIdentity *tmp_peer;
1809 LOG (GNUNET_ERROR_TYPE_DEBUG,
1810 "Printing gossip list:\n");
1811 for (i = 0 ; i < gossip_list_size ; i++)
1812 LOG (GNUNET_ERROR_TYPE_DEBUG,
1813 "\t%s\n", GNUNET_i2s (&gossip_list[i]));
1814 // TODO log lists, ...
1816 /* Would it make sense to have one shuffeled gossip list and then
1817 * to send PUSHes to first alpha peers, PULL requests to next beta peers and
1818 * use the rest to update sampler?
1819 * in essence get random peers with consumption */
1822 if (0 < gossip_list_size)
1824 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
1825 (unsigned int) gossip_list_size);
1826 n_peers = ceil (alpha * gossip_list_size);
1827 LOG (GNUNET_ERROR_TYPE_DEBUG,
1828 "Going to send pushes to %u ceil (%f * %u) peers.\n",
1829 n_peers, alpha, gossip_list_size);
1830 for (i = 0 ; i < n_peers ; i++)
1832 peer = gossip_list[permut[i]];
1833 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer)) // TODO
1834 { // FIXME if this fails schedule/loop this for later
1838 GNUNET_free (permut);
1842 /* Send PULL requests */
1843 //permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list->size);
1844 n_peers = ceil (beta * gossip_list_size);
1845 LOG (GNUNET_ERROR_TYPE_DEBUG,
1846 "Going to send pulls to %u ceil (%f * %u) peers.\n",
1847 n_peers, beta, gossip_list_size);
1848 for (i = 0 ; i < n_peers ; i++)
1850 tmp_peer = get_rand_peer_ignore_list (gossip_list, gossip_list_size,
1851 pending_pull_reply_list, pending_pull_reply_list_size);
1852 if (NULL != tmp_peer)
1855 GNUNET_free (tmp_peer);
1857 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer))
1859 send_pull_request (&peer);
1865 /* Update gossip list */
1867 if ( push_list_size <= alpha * gossip_list_size &&
1868 push_list_size != 0 &&
1869 pull_list_size != 0 )
1871 LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the gossip list.\n");
1873 uint32_t first_border;
1874 uint32_t second_border;
1876 uint32_t peers_to_clean_size;
1877 struct GNUNET_PeerIdentity *peers_to_clean;
1879 peers_to_clean = NULL;
1880 peers_to_clean_size = 0;
1881 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, gossip_list_size);
1882 memcpy (peers_to_clean,
1884 gossip_list_size * sizeof (struct GNUNET_PeerIdentity));
1886 first_border = ceil (alpha * sampler_size_est_need);
1887 second_border = first_border + ceil (beta * sampler_size_est_need);
1889 GNUNET_array_grow (gossip_list, gossip_list_size, second_border);
1891 for (i = 0 ; i < first_border ; i++)
1892 { // TODO use RPS_sampler_get_n_rand_peers
1893 /* Update gossip list with peers received through PUSHes */
1894 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
1896 gossip_list[i] = push_list[r_index];
1897 // TODO change the peer_flags accordingly
1900 for (i = first_border ; i < second_border ; i++)
1902 /* Update gossip list with peers received through PULLs */
1903 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
1905 gossip_list[i] = pull_list[r_index];
1906 // TODO change the peer_flags accordingly
1909 for (i = second_border ; i < sampler_size_est_need ; i++)
1911 /* Update gossip list with peers from history */
1912 RPS_sampler_get_n_rand_peers (prot_sampler, hist_update, NULL, 1, GNUNET_NO);
1913 num_hist_update_tasks++;
1914 // TODO change the peer_flags accordingly
1917 for (i = 0 ; i < gossip_list_size ; i++)
1918 rem_from_list (&peers_to_clean, &peers_to_clean_size, &gossip_list[i]);
1920 for (i = 0 ; i < peers_to_clean_size ; i++)
1921 peer_clean (&peers_to_clean[i]);
1923 GNUNET_free (peers_to_clean);
1927 LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the gossip list.\n");
1929 // TODO independent of that also get some peers from CADET_get_peers()?
1932 /* Update samplers */
1933 for ( i = 0 ; i < push_list_size ; i++ )
1935 LOG (GNUNET_ERROR_TYPE_DEBUG,
1936 "Updating with peer %s from push list\n",
1937 GNUNET_i2s (&push_list[i]));
1938 RPS_sampler_update (prot_sampler, &push_list[i]);
1939 RPS_sampler_update (client_sampler, &push_list[i]);
1940 // TODO set in_flag?
1943 for ( i = 0 ; i < pull_list_size ; i++ )
1945 LOG (GNUNET_ERROR_TYPE_DEBUG,
1946 "Updating with peer %s from pull list\n",
1947 GNUNET_i2s (&pull_list[i]));
1948 RPS_sampler_update (prot_sampler, &pull_list[i]);
1949 RPS_sampler_update (client_sampler, &pull_list[i]);
1950 // TODO set in_flag?
1954 /* Empty push/pull lists */
1955 GNUNET_array_grow (push_list, push_list_size, 0);
1956 GNUNET_array_grow (pull_list, pull_list_size, 0);
1958 struct GNUNET_TIME_Relative time_next_round;
1960 time_next_round = compute_rand_delay (round_interval, 2);
1962 /* Schedule next round */
1963 //do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_round, NULL);
1964 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_round, NULL);
1965 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
1970 rps_start (struct GNUNET_SERVER_Handle *server);
1974 * This is called from GNUNET_CADET_get_peers().
1976 * It is called on every peer(ID) that cadet somehow has contact with.
1977 * We use those to initialise the sampler.
1980 init_peer_cb (void *cls,
1981 const struct GNUNET_PeerIdentity *peer,
1982 int tunnel, // "Do we have a tunnel towards this peer?"
1983 unsigned int n_paths, // "Number of known paths towards this peer"
1984 unsigned int best_path) // "How long is the best path?
1985 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
1987 struct PeerOutstandingOp out_op;
1988 struct PeerContext *peer_ctx;
1991 && 0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, peer))
1993 LOG (GNUNET_ERROR_TYPE_DEBUG,
1994 "Got peer %s (at %p) from CADET (gossip_list_size: %u)\n",
1995 GNUNET_i2s (peer), peer, gossip_list_size);
1997 // maybe create a function for that
1998 peer_ctx = get_peer_ctx (peer_map, peer);
1999 if (GNUNET_YES != get_peer_flag (peer_ctx, VALID))
2001 if (GNUNET_NO == insert_in_sampler_scheduled (peer_ctx))
2003 out_op.op = insert_in_sampler;
2004 out_op.op_cls = NULL;
2005 GNUNET_array_append (peer_ctx->outstanding_ops,
2006 peer_ctx->num_outstanding_ops,
2010 if (GNUNET_NO == insert_in_gossip_list_scheduled (peer_ctx))
2012 out_op.op = insert_in_gossip_list;
2013 out_op.op_cls = NULL;
2014 GNUNET_array_append (peer_ctx->outstanding_ops,
2015 peer_ctx->num_outstanding_ops,
2019 /* Trigger livelyness test on peer */
2020 check_peer_live (peer_ctx);
2023 // send push/pull to each of those peers?
2029 * Clean the send channel of a peer
2032 peer_clean (const struct GNUNET_PeerIdentity *peer)
2034 struct PeerContext *peer_ctx;
2035 struct GNUNET_CADET_Channel *channel;
2037 if (GNUNET_YES != in_arr (gossip_list, gossip_list_size, peer)
2038 && GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
2040 peer_ctx = get_peer_ctx (peer_map, peer);
2041 if (NULL != peer_ctx->send_channel)
2043 channel = peer_ctx->send_channel;
2044 peer_ctx->send_channel = NULL;
2045 GNUNET_CADET_channel_destroy (channel);
2052 * Callback used to remove peers from the multipeermap.
2055 peer_remove_cb (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
2057 struct PeerContext *peer_ctx;
2058 const struct GNUNET_CADET_Channel *channel =
2059 (const struct GNUNET_CADET_Channel *) cls;
2060 struct GNUNET_CADET_Channel *recv;
2061 struct GNUNET_CADET_Channel *send;
2063 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, value))
2065 peer_ctx = (struct PeerContext *) value;
2067 if (0 != peer_ctx->num_outstanding_ops)
2068 GNUNET_array_grow (peer_ctx->outstanding_ops,
2069 peer_ctx->num_outstanding_ops,
2072 if (NULL != peer_ctx->mq)
2074 GNUNET_MQ_destroy (peer_ctx->mq);
2075 peer_ctx->mq = NULL;
2079 if (NULL != peer_ctx->is_live_task)
2081 LOG (GNUNET_ERROR_TYPE_DEBUG,
2082 "Trying to cancle is_live_task for peer %s\n",
2084 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
2085 peer_ctx->is_live_task = NULL;
2088 send = peer_ctx->send_channel;
2089 peer_ctx->send_channel = NULL;
2093 GNUNET_CADET_channel_destroy (send);
2096 recv = peer_ctx->send_channel;
2097 peer_ctx->recv_channel = NULL;
2101 GNUNET_CADET_channel_destroy (recv);
2104 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_remove_all (peer_map, key))
2105 LOG (GNUNET_ERROR_TYPE_WARNING, "removing peer from peer_map failed\n");
2107 GNUNET_free (peer_ctx);
2115 * Task run during shutdown.
2121 shutdown_task (void *cls,
2122 const struct GNUNET_SCHEDULER_TaskContext *tc)
2124 LOG (GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
2126 if (NULL != do_round_task)
2128 GNUNET_SCHEDULER_cancel (do_round_task);
2129 do_round_task = NULL;
2134 if (GNUNET_SYSERR ==
2135 GNUNET_CONTAINER_multipeermap_iterate (peer_map, peer_remove_cb, NULL))
2136 LOG (GNUNET_ERROR_TYPE_WARNING,
2137 "Iterating over peers to disconnect from them was cancelled\n");
2140 GNUNET_NSE_disconnect (nse);
2141 GNUNET_CADET_disconnect (cadet_handle);
2142 RPS_sampler_destroy (prot_sampler);
2143 RPS_sampler_destroy (client_sampler);
2144 LOG (GNUNET_ERROR_TYPE_DEBUG,
2145 "Size of the peermap: %u\n",
2146 GNUNET_CONTAINER_multipeermap_size (peer_map));
2147 GNUNET_break (0 == GNUNET_CONTAINER_multipeermap_size (peer_map));
2148 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
2149 GNUNET_array_grow (gossip_list, gossip_list_size, 0);
2150 GNUNET_array_grow (push_list, push_list_size, 0);
2151 GNUNET_array_grow (pull_list, pull_list_size, 0);
2152 #ifdef ENABLE_MALICIOUS
2153 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
2154 if (NULL != mal_peer_set)
2155 GNUNET_CONTAINER_multipeermap_destroy (mal_peer_set);
2156 if (NULL != att_peer_set)
2157 GNUNET_CONTAINER_multipeermap_destroy (att_peer_set);
2158 // TODO empty attacked_peers DLL
2159 #endif /* ENABLE_MALICIOUS */
2164 * A client disconnected. Remove all of its data structure entries.
2166 * @param cls closure, NULL
2167 * @param client identification of the client
2170 handle_client_disconnect (void *cls,
2171 struct GNUNET_SERVER_Client * client)
2177 * Handle the channel a peer opens to us.
2179 * @param cls The closure
2180 * @param channel The channel the peer wants to establish
2181 * @param initiator The peer's peer ID
2182 * @param port The port the channel is being established over
2183 * @param options Further options
2186 handle_inbound_channel (void *cls,
2187 struct GNUNET_CADET_Channel *channel,
2188 const struct GNUNET_PeerIdentity *initiator,
2190 enum GNUNET_CADET_ChannelOption options)
2192 struct PeerContext *peer_ctx;
2193 struct GNUNET_PeerIdentity peer;
2196 LOG (GNUNET_ERROR_TYPE_DEBUG,
2197 "New channel was established to us (Peer %s).\n",
2198 GNUNET_i2s (&peer));
2200 GNUNET_assert (NULL != channel);
2202 // we might not even store the recv_channel
2204 peer_ctx = get_peer_ctx (peer_map, &peer);
2205 // FIXME what do we do if a channel is established twice?
2206 // overwrite? Clean old channel? ...?
2207 //if (NULL != peer_ctx->recv_channel)
2209 // peer_ctx->recv_channel = channel;
2211 peer_ctx->recv_channel = channel;
2213 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, &peer, peer_ctx,
2214 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
2216 peer_is_live (peer_ctx);
2218 return NULL; // TODO
2223 * This is called when a remote peer destroys a channel.
2225 * @param cls The closure
2226 * @param channel The channel being closed
2227 * @param channel_ctx The context associated with this channel
2230 cleanup_channel (void *cls,
2231 const struct GNUNET_CADET_Channel *channel,
2234 struct GNUNET_PeerIdentity *peer;
2235 struct PeerContext *peer_ctx;
2237 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
2238 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
2239 // Guess simply casting isn't the nicest way...
2240 // FIXME wait for cadet to change this function
2242 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
2244 peer_ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
2246 if (NULL == peer_ctx) /* It could have been removed by shutdown_task */
2249 if (channel == peer_ctx->send_channel)
2250 { /* Peer probably went down */
2251 LOG (GNUNET_ERROR_TYPE_DEBUG,
2252 "Peer %s destroyed send channel - probably went down, cleaning up\n",
2254 rem_from_list (&gossip_list, &gossip_list_size, peer);
2255 rem_from_list (&pending_pull_reply_list, &pending_pull_reply_list_size, peer);
2257 peer_ctx->send_channel = NULL;
2258 /* Somwewhat {ab,re}use the iterator function */
2259 /* Cast to void is ok, because it's used as void in peer_remove_cb */
2260 (void) peer_remove_cb ((void *) channel, peer, peer_ctx);
2262 else if (channel == peer_ctx->recv_channel)
2263 { /* Other peer doesn't want to send us messages anymore */
2264 LOG (GNUNET_ERROR_TYPE_DEBUG,
2265 "Peer %s destroyed recv channel - cleaning up channel\n",
2267 peer_ctx->recv_channel = NULL;
2274 * Actually start the service.
2277 rps_start (struct GNUNET_SERVER_Handle *server)
2279 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
2280 {&handle_client_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
2281 sizeof (struct GNUNET_RPS_CS_RequestMessage)},
2282 {&handle_client_seed, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_SEED, 0},
2283 #ifdef ENABLE_MALICIOUS
2284 {&handle_client_act_malicious, NULL, GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS , 0},
2285 #endif /* ENABLE_MALICIOUS */
2289 GNUNET_SERVER_add_handlers (server, handlers);
2290 GNUNET_SERVER_disconnect_notify (server,
2291 &handle_client_disconnect,
2293 LOG (GNUNET_ERROR_TYPE_DEBUG, "Ready to receive requests from clients\n");
2296 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
2297 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
2299 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2306 * Process statistics requests.
2308 * @param cls closure
2309 * @param server the initialized server
2310 * @param c configuration to use
2314 struct GNUNET_SERVER_Handle *server,
2315 const struct GNUNET_CONFIGURATION_Handle *c)
2317 // TODO check what this does -- copied from gnunet-boss
2318 // - seems to work as expected
2319 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
2324 GNUNET_CRYPTO_get_peer_identity (cfg, &own_identity); // TODO check return value
2325 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2326 "STARTING SERVICE (rps) for peer [%s]\n",
2327 GNUNET_i2s (&own_identity));
2328 #ifdef ENABLE_MALICIOUS
2329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2330 "Malicious execution compiled in.\n");
2331 #endif /* ENABLE_MALICIOUS */
2335 /* Get time interval from the configuration */
2336 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
2340 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read ROUNDINTERVAL from config\n");
2341 GNUNET_SCHEDULER_shutdown ();
2345 /* Get initial size of sampler/gossip list from the configuration */
2346 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "RPS",
2348 (long long unsigned int *) &sampler_size_est_need))
2350 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read INITSIZE from config\n");
2351 GNUNET_SCHEDULER_shutdown ();
2354 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", sampler_size_est_need);
2360 /* connect to NSE */
2361 nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
2362 // TODO check whether that was successful
2363 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to NSE\n");
2369 peer_map = GNUNET_CONTAINER_multipeermap_create (sampler_size_est_need, GNUNET_NO);
2372 /* Initialise cadet */
2373 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
2374 {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH ,
2375 sizeof (struct GNUNET_MessageHeader)},
2376 {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
2377 sizeof (struct GNUNET_MessageHeader)},
2378 {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY , 0},
2382 const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
2383 cadet_handle = GNUNET_CADET_connect (cfg,
2385 &handle_inbound_channel,
2389 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to CADET\n");
2392 /* Initialise sampler */
2393 struct GNUNET_TIME_Relative half_round_interval;
2394 struct GNUNET_TIME_Relative max_round_interval;
2396 half_round_interval = GNUNET_TIME_relative_multiply (round_interval, .5);
2397 max_round_interval = GNUNET_TIME_relative_add (round_interval, half_round_interval);
2399 prot_sampler = RPS_sampler_init (sampler_size_est_need, max_round_interval);
2400 client_sampler = RPS_sampler_init (sampler_size_est_need, max_round_interval);
2402 /* Initialise push and pull maps */
2407 pending_pull_reply_list = NULL;
2408 pending_pull_reply_list_size = 0;
2411 num_hist_update_tasks = 0;
2414 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
2415 GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, NULL);
2416 // TODO send push/pull to each of those peers?
2424 * The main function for the rps service.
2426 * @param argc number of arguments from the command line
2427 * @param argv command line arguments
2428 * @return 0 ok, 1 on error
2431 main (int argc, char *const *argv)
2433 return (GNUNET_OK ==
2434 GNUNET_SERVICE_run (argc,
2437 GNUNET_SERVICE_OPTION_NONE,
2438 &run, NULL)) ? 0 : 1;
2441 /* end of gnunet-service-rps.c */