2 This file is part of GNUnet.
3 Copyright (C) 2013-2015 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file rps/gnunet-service-rps.c
23 * @brief rps service implementation
24 * @author Julius Bünger
27 #include "gnunet_applications.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_cadet_service.h"
30 #include "gnunet_peerinfo_service.h"
31 #include "gnunet_nse_service.h"
33 #include "rps-test_util.h"
34 #include "gnunet-service-rps_sampler.h"
35 #include "gnunet-service-rps_custommap.h"
36 #include "gnunet-service-rps_peers.h"
37 #include "gnunet-service-rps_view.h"
42 #define LOG(kind, ...) GNUNET_log(kind, __VA_ARGS__)
44 // TODO modify @brief in every file
46 // TODO check for overflows
48 // TODO align message structs
50 // TODO connect to friends
52 // TODO store peers somewhere persistent
54 // TODO blacklist? (-> mal peer detection on top of brahms)
56 // hist_size_init, hist_size_max
61 static const struct GNUNET_CONFIGURATION_Handle *cfg;
66 static struct GNUNET_PeerIdentity own_identity;
69 /***********************************************************************
70 * Housekeeping with clients
71 ***********************************************************************/
74 * Closure used to pass the client and the id to the callback
75 * that replies to a client's request
82 struct ReplyCls *next;
83 struct ReplyCls *prev;
86 * The identifier of the request
91 * The handle to the request
93 struct RPS_SamplerRequestHandle *req_handle;
96 * The client handle to send the reply to
98 struct ClientContext *cli_ctx;
103 * Struct used to store the context of a connected client.
110 struct ClientContext *next;
111 struct ClientContext *prev;
114 * The message queue to communicate with the client.
116 struct GNUNET_MQ_Handle *mq;
119 * DLL with handles to single requests from the client
121 struct ReplyCls *rep_cls_head;
122 struct ReplyCls *rep_cls_tail;
125 * The client handle to send the reply to
127 struct GNUNET_SERVICE_Client *client;
131 * DLL with all clients currently connected to us
133 struct ClientContext *cli_ctx_head;
134 struct ClientContext *cli_ctx_tail;
136 /***********************************************************************
137 * /Housekeeping with clients
138 ***********************************************************************/
144 /***********************************************************************
146 ***********************************************************************/
149 * Sampler used for the Brahms protocol itself.
151 static struct RPS_Sampler *prot_sampler;
154 * Sampler used for the clients.
156 static struct RPS_Sampler *client_sampler;
159 * Name to log view to
161 static char *file_name_view_log;
164 * The size of sampler we need to be able to satisfy the client's need
167 static unsigned int sampler_size_client_need;
170 * The size of sampler we need to be able to satisfy the Brahms protocol's
171 * need of random peers.
173 * This is one minimum size the sampler grows to.
175 static unsigned int sampler_size_est_need;
178 * Percentage of total peer number in the view
179 * to send random PUSHes to
184 * Percentage of total peer number in the view
185 * to send random PULLs to
190 * Identifier for the main task that runs periodically.
192 static struct GNUNET_SCHEDULER_Task *do_round_task;
195 * Time inverval the do_round task runs in.
197 static struct GNUNET_TIME_Relative round_interval;
200 * List to store peers received through pushes temporary.
202 static struct CustomPeerMap *push_map;
205 * List to store peers received through pulls temporary.
207 static struct CustomPeerMap *pull_map;
212 static struct GNUNET_NSE_Handle *nse;
217 static struct GNUNET_CADET_Handle *cadet_handle;
220 * @brief Port to communicate to other peers.
222 static struct GNUNET_CADET_Port *cadet_port;
225 * Handler to PEERINFO.
227 static struct GNUNET_PEERINFO_Handle *peerinfo_handle;
230 * Handle for cancellation of iteration over peers.
232 static struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify_handle;
237 * Counts how many requets clients already issued.
238 * Only needed in the beginning to check how many of the 64 deltas
241 static unsigned int req_counter;
244 * Time of the last request we received.
246 * Used to compute the expected request rate.
248 static struct GNUNET_TIME_Absolute last_request;
251 * Size of #request_deltas.
253 #define REQUEST_DELTAS_SIZE 64
254 static unsigned int request_deltas_size = REQUEST_DELTAS_SIZE;
257 * Last 64 deltas between requests
259 static struct GNUNET_TIME_Relative request_deltas[REQUEST_DELTAS_SIZE];
262 * The prediction of the rate of requests
264 static struct GNUNET_TIME_Relative request_rate;
267 #ifdef ENABLE_MALICIOUS
269 * Type of malicious peer
271 * 0 Don't act malicious at all - Default
272 * 1 Try to maximise representation
273 * 2 Try to partition the network
276 static uint32_t mal_type;
279 * Other malicious peers
281 static struct GNUNET_PeerIdentity *mal_peers;
284 * Hashmap of malicious peers used as set.
285 * Used to more efficiently check whether we know that peer.
287 static struct GNUNET_CONTAINER_MultiPeerMap *mal_peer_set;
290 * Number of other malicious peers
292 static uint32_t num_mal_peers;
296 * If type is 2 This struct is used to store the attacked peers in a DLL
303 struct AttackedPeer *next;
304 struct AttackedPeer *prev;
309 struct GNUNET_PeerIdentity peer_id;
313 * If type is 2 this is the DLL of attacked peers
315 static struct AttackedPeer *att_peers_head;
316 static struct AttackedPeer *att_peers_tail;
319 * This index is used to point to an attacked peer to
320 * implement the round-robin-ish way to select attacked peers.
322 static struct AttackedPeer *att_peer_index;
325 * Hashmap of attacked peers used as set.
326 * Used to more efficiently check whether we know that peer.
328 static struct GNUNET_CONTAINER_MultiPeerMap *att_peer_set;
331 * Number of attacked peers
333 static uint32_t num_attacked_peers;
336 * If type is 1 this is the attacked peer
338 static struct GNUNET_PeerIdentity attacked_peer;
341 * The limit of PUSHes we can send in one round.
342 * This is an assumption of the Brahms protocol and either implemented
345 * assumend to be the bandwidth limitation.
347 static uint32_t push_limit = 10000;
348 #endif /* ENABLE_MALICIOUS */
351 /***********************************************************************
353 ***********************************************************************/
356 /***********************************************************************
358 ***********************************************************************/
362 * Print peerlist to log.
365 print_peer_list (struct GNUNET_PeerIdentity *list,
370 LOG (GNUNET_ERROR_TYPE_DEBUG,
371 "Printing peer list of length %u at %p:\n",
374 for (i = 0 ; i < len ; i++)
376 LOG (GNUNET_ERROR_TYPE_DEBUG,
378 i, GNUNET_i2s (&list[i]));
384 * Remove peer from list.
387 rem_from_list (struct GNUNET_PeerIdentity **peer_list,
388 unsigned int *list_size,
389 const struct GNUNET_PeerIdentity *peer)
392 struct GNUNET_PeerIdentity *tmp;
396 LOG (GNUNET_ERROR_TYPE_DEBUG,
397 "Removing peer %s from list at %p\n",
401 for ( i = 0 ; i < *list_size ; i++ )
403 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&tmp[i], peer))
405 if (i < *list_size -1)
406 { /* Not at the last entry -- shift peers left */
407 memmove (&tmp[i], &tmp[i +1],
408 ((*list_size) - i -1) * sizeof (struct GNUNET_PeerIdentity));
410 /* Remove last entry (should be now useless PeerID) */
411 GNUNET_array_grow (tmp, *list_size, (*list_size) -1);
419 * Sum all time relatives of an array.
421 static struct GNUNET_TIME_Relative
422 T_relative_sum (const struct GNUNET_TIME_Relative *rel_array,
425 struct GNUNET_TIME_Relative sum;
428 sum = GNUNET_TIME_UNIT_ZERO;
429 for ( i = 0 ; i < arr_size ; i++ )
431 sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
438 * Compute the average of given time relatives.
440 static struct GNUNET_TIME_Relative
441 T_relative_avg (const struct GNUNET_TIME_Relative *rel_array,
444 return GNUNET_TIME_relative_divide (T_relative_sum (rel_array,
451 * Insert PeerID in #view
453 * Called once we know a peer is live.
456 * @return GNUNET_OK if peer was actually inserted
457 * GNUNET_NO if peer was not inserted
460 insert_in_view_op (void *cls,
461 const struct GNUNET_PeerIdentity *peer);
464 * Insert PeerID in #view
466 * Called once we know a peer is live.
468 * @return GNUNET_OK if peer was actually inserted
469 * GNUNET_NO if peer was not inserted
472 insert_in_view (const struct GNUNET_PeerIdentity *peer)
476 online = Peers_check_peer_flag (peer, Peers_ONLINE);
477 if ( (GNUNET_NO == online) ||
478 (GNUNET_SYSERR == online) ) /* peer is not even known */
480 (void) Peers_issue_peer_liveliness_check (peer);
481 (void) Peers_schedule_operation (peer, insert_in_view_op);
484 /* Open channel towards peer to keep connection open */
485 Peers_indicate_sending_intention (peer);
486 return View_put (peer);
490 * Put random peer from sampler into the view as history update.
493 hist_update (void *cls,
494 struct GNUNET_PeerIdentity *ids,
499 for (i = 0; i < num_peers; i++)
501 (void) insert_in_view (&ids[i]);
502 to_file (file_name_view_log,
504 GNUNET_i2s_full (ids));
510 * Wrapper around #RPS_sampler_resize()
512 * If we do not have enough sampler elements, double current sampler size
513 * If we have more than enough sampler elements, halv current sampler size
516 resize_wrapper (struct RPS_Sampler *sampler, uint32_t new_size)
518 unsigned int sampler_size;
521 // TODO respect the min, max
522 sampler_size = RPS_sampler_get_size (sampler);
523 if (sampler_size > new_size * 4)
525 RPS_sampler_resize (sampler, sampler_size / 2);
527 else if (sampler_size < new_size)
529 RPS_sampler_resize (sampler, sampler_size * 2);
531 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
536 * Wrapper around #RPS_sampler_resize() resizing the client sampler
539 client_resize_wrapper ()
541 uint32_t bigger_size;
545 bigger_size = GNUNET_MAX (sampler_size_est_need, sampler_size_client_need);
547 // TODO respect the min, max
548 resize_wrapper (client_sampler, bigger_size);
549 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size_client is now %" PRIu32 "\n",
555 * Estimate request rate
557 * Called every time we receive a request from the client.
562 struct GNUNET_TIME_Relative max_round_duration;
564 if (request_deltas_size > req_counter)
566 if ( 1 < req_counter)
568 /* Shift last request deltas to the right */
569 memmove (&request_deltas[1],
571 (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
573 /* Add current delta to beginning */
575 GNUNET_TIME_absolute_get_difference (last_request,
576 GNUNET_TIME_absolute_get ());
577 request_rate = T_relative_avg (request_deltas, req_counter);
578 request_rate = (request_rate.rel_value_us < 1) ?
579 GNUNET_TIME_relative_get_unit_ () : request_rate;
581 /* Compute the duration a round will maximally take */
583 GNUNET_TIME_relative_add (round_interval,
584 GNUNET_TIME_relative_divide (round_interval, 2));
586 /* Set the estimated size the sampler has to have to
587 * satisfy the current client request rate */
588 sampler_size_client_need =
589 max_round_duration.rel_value_us / request_rate.rel_value_us;
591 /* Resize the sampler */
592 client_resize_wrapper ();
594 last_request = GNUNET_TIME_absolute_get ();
599 * Add all peers in @a peer_array to @a peer_map used as set.
601 * @param peer_array array containing the peers
602 * @param num_peers number of peers in @peer_array
603 * @param peer_map the peermap to use as set
606 add_peer_array_to_set (const struct GNUNET_PeerIdentity *peer_array,
607 unsigned int num_peers,
608 struct GNUNET_CONTAINER_MultiPeerMap *peer_map)
611 if (NULL == peer_map)
613 LOG (GNUNET_ERROR_TYPE_WARNING,
614 "Trying to add peers to non-existing peermap.\n");
618 for (i = 0; i < num_peers; i++)
620 GNUNET_CONTAINER_multipeermap_put (peer_map,
623 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
629 * Send a PULL REPLY to @a peer_id
631 * @param peer_id the peer to send the reply to.
632 * @param peer_ids the peers to send to @a peer_id
633 * @param num_peer_ids the number of peers to send to @a peer_id
636 send_pull_reply (const struct GNUNET_PeerIdentity *peer_id,
637 const struct GNUNET_PeerIdentity *peer_ids,
638 unsigned int num_peer_ids)
641 struct GNUNET_MQ_Envelope *ev;
642 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
644 /* Compute actual size */
645 send_size = sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) +
646 num_peer_ids * sizeof (struct GNUNET_PeerIdentity);
648 if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size)
649 /* Compute number of peers to send
650 * If too long, simply truncate */
651 // TODO select random ones via permutation
652 // or even better: do good protocol design
654 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE -
655 sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
656 sizeof (struct GNUNET_PeerIdentity);
658 send_size = num_peer_ids;
660 LOG (GNUNET_ERROR_TYPE_DEBUG,
661 "Going to send PULL REPLY with %u peers to %s\n",
662 send_size, GNUNET_i2s (peer_id));
664 ev = GNUNET_MQ_msg_extra (out_msg,
665 send_size * sizeof (struct GNUNET_PeerIdentity),
666 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
667 out_msg->num_peers = htonl (send_size);
668 GNUNET_memcpy (&out_msg[1], peer_ids,
669 send_size * sizeof (struct GNUNET_PeerIdentity));
671 Peers_send_message (peer_id, ev, "PULL REPLY");
676 * Insert PeerID in #pull_map
678 * Called once we know a peer is live.
681 insert_in_pull_map (void *cls,
682 const struct GNUNET_PeerIdentity *peer)
684 CustomPeerMap_put (pull_map, peer);
689 * Insert PeerID in #view
691 * Called once we know a peer is live.
695 insert_in_view_op (void *cls,
696 const struct GNUNET_PeerIdentity *peer)
698 (void) insert_in_view (peer);
703 * Update sampler with given PeerID.
707 insert_in_sampler (void *cls,
708 const struct GNUNET_PeerIdentity *peer)
710 LOG (GNUNET_ERROR_TYPE_DEBUG,
711 "Updating samplers with peer %s from insert_in_sampler()\n",
713 RPS_sampler_update (prot_sampler, peer);
714 RPS_sampler_update (client_sampler, peer);
715 if (0 < RPS_sampler_count_id (prot_sampler, peer))
717 /* Make sure we 'know' about this peer */
718 (void) Peers_issue_peer_liveliness_check (peer);
719 /* Establish a channel towards that peer to indicate we are going to send
721 //Peers_indicate_sending_intention (peer);
726 * @brief This is called on peers from external sources (cadet, peerinfo, ...)
727 * If the peer is not known, liveliness check is issued and it is
728 * scheduled to be inserted in sampler and view.
730 * "External sources" refer to every source except the gossip.
732 * @param peer peer to insert
735 got_peer (const struct GNUNET_PeerIdentity *peer)
737 /* If we did not know this peer already, insert it into sampler and view */
738 if (GNUNET_YES == Peers_issue_peer_liveliness_check (peer))
740 Peers_schedule_operation (peer, insert_in_sampler);
741 Peers_schedule_operation (peer, insert_in_view_op);
746 * @brief Checks if there is a sending channel and if it is needed
748 * @param peer the peer whose sending channel is checked
749 * @return GNUNET_YES if sending channel exists and is still needed
750 * GNUNET_NO otherwise
753 check_sending_channel_needed (const struct GNUNET_PeerIdentity *peer)
755 /* struct GNUNET_CADET_Channel *channel; */
756 if (GNUNET_NO == Peers_check_peer_known (peer))
760 if (GNUNET_YES == Peers_check_sending_channel_exists (peer))
762 if ( (0 < RPS_sampler_count_id (prot_sampler, peer)) ||
763 (GNUNET_YES == View_contains_peer (peer)) ||
764 (GNUNET_YES == CustomPeerMap_contains_peer (push_map, peer)) ||
765 (GNUNET_YES == CustomPeerMap_contains_peer (pull_map, peer)) ||
766 (GNUNET_YES == Peers_check_peer_flag (peer, Peers_PULL_REPLY_PENDING)))
767 { /* If we want to keep the connection to peer open */
776 * @brief remove peer from our knowledge, the view, push and pull maps and
779 * @param peer the peer to remove
782 remove_peer (const struct GNUNET_PeerIdentity *peer)
784 (void) View_remove_peer (peer);
785 CustomPeerMap_remove_peer (pull_map, peer);
786 CustomPeerMap_remove_peer (push_map, peer);
787 RPS_sampler_reinitialise_by_value (prot_sampler, peer);
788 RPS_sampler_reinitialise_by_value (client_sampler, peer);
789 Peers_remove_peer (peer);
794 * @brief Remove data that is not needed anymore.
796 * If the sending channel is no longer needed it is destroyed.
798 * @param peer the peer whose data is about to be cleaned
801 clean_peer (const struct GNUNET_PeerIdentity *peer)
803 if (GNUNET_NO == check_sending_channel_needed (peer))
805 LOG (GNUNET_ERROR_TYPE_DEBUG,
806 "Going to remove send channel to peer %s\n",
808 #ifdef ENABLE_MALICIOUS
809 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
810 (void) Peers_destroy_sending_channel (peer);
811 #else /* ENABLE_MALICIOUS */
812 (void) Peers_destroy_sending_channel (peer);
813 #endif /* ENABLE_MALICIOUS */
816 if ( (GNUNET_NO == Peers_check_peer_send_intention (peer)) &&
817 (GNUNET_NO == View_contains_peer (peer)) &&
818 (GNUNET_NO == CustomPeerMap_contains_peer (push_map, peer)) &&
819 (GNUNET_NO == CustomPeerMap_contains_peer (push_map, peer)) &&
820 (0 == RPS_sampler_count_id (prot_sampler, peer)) &&
821 (0 == RPS_sampler_count_id (client_sampler, peer)) &&
822 (GNUNET_NO != Peers_check_removable (peer)) )
823 { /* We can safely remove this peer */
824 LOG (GNUNET_ERROR_TYPE_DEBUG,
825 "Going to remove peer %s\n",
833 * @brief This is called when a channel is destroyed.
835 * Removes peer completely from our knowledge if the send_channel was destroyed
836 * Otherwise simply delete the recv_channel
837 * Also check if the knowledge about this peer is still needed.
838 * If not, remove this peer from our knowledge.
840 * @param cls The closure
841 * @param channel The channel being closed
842 * @param channel_ctx The context associated with this channel
845 cleanup_destroyed_channel (void *cls,
846 const struct GNUNET_CADET_Channel *channel)
848 struct GNUNET_PeerIdentity *peer = cls;
849 uint32_t *channel_flag;
851 if (GNUNET_NO == Peers_check_peer_known (peer))
852 { /* We don't know a context to that peer */
853 LOG (GNUNET_ERROR_TYPE_WARNING,
854 "channel (%s) without associated context was destroyed\n",
859 if (GNUNET_YES == Peers_check_peer_flag (peer, Peers_TO_DESTROY))
860 { /* We are in the middle of removing that peer from our knowledge. In this
861 case simply make sure that the channels are cleaned. */
862 Peers_cleanup_destroyed_channel (cls, channel);
863 to_file (file_name_view_log,
864 "-%s\t(cleanup channel, ourself)",
865 GNUNET_i2s_full (peer));
870 Peers_check_channel_role (peer, channel, Peers_CHANNEL_ROLE_SENDING))
871 { /* Channel used for sending was destroyed */
872 /* Possible causes of channel destruction:
873 * - ourselves -> cleaning send channel -> clean context
874 * - other peer -> peer probably went down -> remove
876 channel_flag = Peers_get_channel_flag (peer, Peers_CHANNEL_ROLE_SENDING);
877 if (GNUNET_YES == Peers_check_channel_flag (channel_flag, Peers_CHANNEL_CLEAN))
878 { /* We are about to clean the sending channel. Clean the respective
880 Peers_cleanup_destroyed_channel (cls, channel);
884 { /* Other peer destroyed our sending channel that he is supposed to keep
885 * open. It probably went down. Remove it from our knowledge. */
886 Peers_cleanup_destroyed_channel (cls, channel);
891 else if (GNUNET_YES ==
892 Peers_check_channel_role (peer, channel, Peers_CHANNEL_ROLE_RECEIVING))
893 { /* Channel used for receiving was destroyed */
894 /* Possible causes of channel destruction:
895 * - ourselves -> peer tried to establish channel twice -> clean context
896 * - other peer -> peer doesn't want to send us data -> clean
898 channel_flag = Peers_get_channel_flag (peer, Peers_CHANNEL_ROLE_RECEIVING);
900 Peers_check_channel_flag (channel_flag, Peers_CHANNEL_ESTABLISHED_TWICE))
901 { /* Other peer tried to establish a channel to us twice. We do not accept
902 * that. Clean the context. */
903 Peers_cleanup_destroyed_channel (cls, channel);
907 { /* Other peer doesn't want to send us data anymore. We are free to clean
909 Peers_cleanup_destroyed_channel (cls, channel);
916 LOG (GNUNET_ERROR_TYPE_WARNING,
917 "Destroyed channel is neither sending nor receiving channel\n");
921 /***********************************************************************
923 ***********************************************************************/
926 destroy_reply_cls (struct ReplyCls *rep_cls)
928 struct ClientContext *cli_ctx;
930 cli_ctx = rep_cls->cli_ctx;
931 GNUNET_assert (NULL != cli_ctx);
932 if (NULL != rep_cls->req_handle)
934 RPS_sampler_request_cancel (rep_cls->req_handle);
936 GNUNET_CONTAINER_DLL_remove (cli_ctx->rep_cls_head,
937 cli_ctx->rep_cls_tail,
939 GNUNET_free (rep_cls);
944 destroy_cli_ctx (struct ClientContext *cli_ctx)
946 GNUNET_assert (NULL != cli_ctx);
947 if (NULL != cli_ctx->rep_cls_head)
949 LOG (GNUNET_ERROR_TYPE_WARNING,
950 "Trying to destroy the context of a client that still has pending requests. Going to clean those\n");
951 while (NULL != cli_ctx->rep_cls_head)
952 destroy_reply_cls (cli_ctx->rep_cls_head);
954 GNUNET_CONTAINER_DLL_remove (cli_ctx_head,
957 GNUNET_free (cli_ctx);
962 * Function called by NSE.
964 * Updates sizes of sampler list and view and adapt those lists
968 nse_callback (void *cls,
969 struct GNUNET_TIME_Absolute timestamp,
970 double logestimate, double std_dev)
973 //double scale; // TODO this might go gloabal/config
975 LOG (GNUNET_ERROR_TYPE_DEBUG,
976 "Received a ns estimate - logest: %f, std_dev: %f (old_size: %u)\n",
977 logestimate, std_dev, RPS_sampler_get_size (prot_sampler));
979 estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
980 // GNUNET_NSE_log_estimate_to_n (logestimate);
981 estimate = pow (estimate, 1.0 / 3);
982 // TODO add if std_dev is a number
983 // estimate += (std_dev * scale);
984 if (2 < ceil (estimate))
986 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
987 sampler_size_est_need = estimate;
989 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
991 /* If the NSE has changed adapt the lists accordingly */
992 resize_wrapper (prot_sampler, sampler_size_est_need);
993 client_resize_wrapper ();
998 * Callback called once the requested PeerIDs are ready.
1000 * Sends those to the requesting client.
1003 client_respond (void *cls,
1004 struct GNUNET_PeerIdentity *peer_ids,
1007 struct ReplyCls *reply_cls = cls;
1009 struct GNUNET_MQ_Envelope *ev;
1010 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
1011 uint32_t size_needed;
1012 struct ClientContext *cli_ctx;
1014 GNUNET_assert (NULL != reply_cls);
1015 LOG (GNUNET_ERROR_TYPE_DEBUG,
1016 "sampler returned %" PRIu32 " peers:\n",
1018 for (i = 0; i < num_peers; i++)
1020 LOG (GNUNET_ERROR_TYPE_DEBUG,
1021 " %" PRIu32 ": %s\n",
1023 GNUNET_i2s (&peer_ids[i]));
1026 size_needed = sizeof (struct GNUNET_RPS_CS_ReplyMessage) +
1027 num_peers * sizeof (struct GNUNET_PeerIdentity);
1029 GNUNET_assert (GNUNET_MAX_MESSAGE_SIZE >= size_needed);
1031 ev = GNUNET_MQ_msg_extra (out_msg,
1032 num_peers * sizeof (struct GNUNET_PeerIdentity),
1033 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
1034 out_msg->num_peers = htonl (num_peers);
1035 out_msg->id = htonl (reply_cls->id);
1037 GNUNET_memcpy (&out_msg[1],
1039 num_peers * sizeof (struct GNUNET_PeerIdentity));
1040 GNUNET_free (peer_ids);
1042 cli_ctx = reply_cls->cli_ctx;
1043 GNUNET_assert (NULL != cli_ctx);
1044 reply_cls->req_handle = NULL;
1045 destroy_reply_cls (reply_cls);
1046 GNUNET_MQ_send (cli_ctx->mq, ev);
1051 * Handle RPS request from the client.
1053 * @param cls closure
1054 * @param message the actual message
1057 handle_client_request (void *cls,
1058 const struct GNUNET_RPS_CS_RequestMessage *msg)
1060 struct ClientContext *cli_ctx = cls;
1062 uint32_t size_needed;
1063 struct ReplyCls *reply_cls;
1066 num_peers = ntohl (msg->num_peers);
1067 size_needed = sizeof (struct GNUNET_RPS_CS_RequestMessage) +
1068 num_peers * sizeof (struct GNUNET_PeerIdentity);
1070 if (GNUNET_MAX_MESSAGE_SIZE < size_needed)
1072 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1073 "Message received from client has size larger than expected\n");
1074 GNUNET_SERVICE_client_drop (cli_ctx->client);
1078 for (i = 0 ; i < num_peers ; i++)
1081 LOG (GNUNET_ERROR_TYPE_DEBUG,
1082 "Client requested %" PRIu32 " random peer(s).\n",
1085 reply_cls = GNUNET_new (struct ReplyCls);
1086 reply_cls->id = ntohl (msg->id);
1087 reply_cls->cli_ctx = cli_ctx;
1088 reply_cls->req_handle = RPS_sampler_get_n_rand_peers (client_sampler,
1093 GNUNET_assert (NULL != cli_ctx);
1094 GNUNET_CONTAINER_DLL_insert (cli_ctx->rep_cls_head,
1095 cli_ctx->rep_cls_tail,
1097 GNUNET_SERVICE_client_continue (cli_ctx->client);
1102 * @brief Handle a message that requests the cancellation of a request
1105 * @param message the message containing the id of the request
1108 handle_client_request_cancel (void *cls,
1109 const struct GNUNET_RPS_CS_RequestCancelMessage *msg)
1111 struct ClientContext *cli_ctx = cls;
1112 struct ReplyCls *rep_cls;
1114 GNUNET_assert (NULL != cli_ctx);
1115 GNUNET_assert (NULL != cli_ctx->rep_cls_head);
1116 rep_cls = cli_ctx->rep_cls_head;
1117 LOG (GNUNET_ERROR_TYPE_DEBUG,
1118 "Client cancels request with id %" PRIu32 "\n",
1120 while ( (NULL != rep_cls->next) &&
1121 (rep_cls->id != ntohl (msg->id)) )
1122 rep_cls = rep_cls->next;
1123 GNUNET_assert (rep_cls->id == ntohl (msg->id));
1124 RPS_sampler_request_cancel (rep_cls->req_handle);
1125 destroy_reply_cls (rep_cls);
1126 GNUNET_SERVICE_client_continue (cli_ctx->client);
1131 * @brief This function is called, when the client seeds peers.
1132 * It verifies that @a msg is well-formed.
1134 * @param cls the closure (#ClientContext)
1135 * @param msg the message
1136 * @return #GNUNET_OK if @a msg is well-formed
1139 check_client_seed (void *cls, const struct GNUNET_RPS_CS_SeedMessage *msg)
1141 struct ClientContext *cli_ctx = cls;
1142 uint16_t msize = ntohs (msg->header.size);
1143 uint32_t num_peers = ntohl (msg->num_peers);
1145 msize -= sizeof (struct GNUNET_RPS_CS_SeedMessage);
1146 if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) ||
1147 (msize % sizeof (struct GNUNET_PeerIdentity) != 0) )
1150 GNUNET_SERVICE_client_drop (cli_ctx->client);
1151 return GNUNET_SYSERR;
1158 * Handle seed from the client.
1160 * @param cls closure
1161 * @param message the actual message
1164 handle_client_seed (void *cls,
1165 const struct GNUNET_RPS_CS_SeedMessage *msg)
1167 struct ClientContext *cli_ctx = cls;
1168 struct GNUNET_PeerIdentity *peers;
1172 num_peers = ntohl (msg->num_peers);
1173 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1174 //peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1175 //GNUNET_memcpy (peers, &msg[1], num_peers * sizeof (struct GNUNET_PeerIdentity));
1177 LOG (GNUNET_ERROR_TYPE_DEBUG,
1178 "Client seeded peers:\n");
1179 print_peer_list (peers, num_peers);
1181 for (i = 0; i < num_peers; i++)
1183 LOG (GNUNET_ERROR_TYPE_DEBUG,
1184 "Updating samplers with seed %" PRIu32 ": %s\n",
1186 GNUNET_i2s (&peers[i]));
1188 got_peer (&peers[i]);
1191 ////GNUNET_free (peers);
1193 GNUNET_SERVICE_client_continue (cli_ctx->client);
1197 * Handle a CHECK_LIVE message from another peer.
1199 * This does nothing. But without calling #GNUNET_CADET_receive_done()
1200 * the channel is blocked for all other communication.
1202 * @param cls Closure
1203 * @param msg The message header
1206 handle_peer_check (void *cls,
1207 const struct GNUNET_MessageHeader *msg)
1209 const struct GNUNET_PeerIdentity *peer = cls;
1211 GNUNET_CADET_receive_done (Peers_get_recv_channel (peer));
1215 * Handle a PUSH message from another peer.
1217 * Check the proof of work and store the PeerID
1218 * in the temporary list for pushed PeerIDs.
1220 * @param cls Closure
1221 * @param msg The message header
1224 handle_peer_push (void *cls,
1225 const struct GNUNET_MessageHeader *msg)
1227 const struct GNUNET_PeerIdentity *peer = cls;
1229 // (check the proof of work (?))
1231 LOG (GNUNET_ERROR_TYPE_DEBUG,
1232 "Received PUSH (%s)\n",
1235 #ifdef ENABLE_MALICIOUS
1236 struct AttackedPeer *tmp_att_peer;
1238 if ( (1 == mal_type) ||
1240 { /* Try to maximise representation */
1241 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1242 GNUNET_memcpy (&tmp_att_peer->peer_id, peer, sizeof (struct GNUNET_PeerIdentity));
1243 if (NULL == att_peer_set)
1244 att_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
1245 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1248 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1251 add_peer_array_to_set (peer, 1, att_peer_set);
1253 GNUNET_CADET_receive_done (Peers_get_recv_channel (peer));
1257 else if (2 == mal_type)
1258 { /* We attack one single well-known peer - simply ignore */
1259 GNUNET_CADET_receive_done (Peers_get_recv_channel (peer));
1261 #endif /* ENABLE_MALICIOUS */
1263 /* Add the sending peer to the push_map */
1264 CustomPeerMap_put (push_map, peer);
1266 GNUNET_CADET_receive_done (Peers_get_recv_channel (peer));
1271 * Handle PULL REQUEST request message from another peer.
1273 * Reply with the view of PeerIDs.
1275 * @param cls Closure
1276 * @param msg The message header
1279 handle_peer_pull_request (void *cls,
1280 const struct GNUNET_MessageHeader *msg)
1282 struct GNUNET_PeerIdentity *peer = cls;
1283 const struct GNUNET_PeerIdentity *view_array;
1285 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REQUEST (%s)\n", GNUNET_i2s (peer));
1287 #ifdef ENABLE_MALICIOUS
1290 { /* Try to maximise representation */
1291 send_pull_reply (peer, mal_peers, num_mal_peers);
1292 GNUNET_CADET_receive_done (Peers_get_recv_channel (peer));
1295 else if (2 == mal_type)
1296 { /* Try to partition network */
1297 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
1299 send_pull_reply (peer, mal_peers, num_mal_peers);
1301 GNUNET_CADET_receive_done (Peers_get_recv_channel (peer));
1303 #endif /* ENABLE_MALICIOUS */
1305 view_array = View_get_as_array ();
1306 send_pull_reply (peer, view_array, View_size ());
1308 GNUNET_CADET_receive_done (Peers_get_recv_channel (peer));
1313 * Check whether we sent a corresponding request and
1314 * whether this reply is the first one.
1316 * @param cls Closure
1317 * @param msg The message header
1320 check_peer_pull_reply (void *cls,
1321 const struct GNUNET_RPS_P2P_PullReplyMessage *msg)
1323 struct GNUNET_PeerIdentity *sender = cls;
1325 if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->header.size))
1327 GNUNET_break_op (0);
1328 return GNUNET_SYSERR;
1331 if ((ntohs (msg->header.size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1332 sizeof (struct GNUNET_PeerIdentity) != ntohl (msg->num_peers))
1334 LOG (GNUNET_ERROR_TYPE_ERROR,
1335 "message says it sends %" PRIu32 " peers, have space for %lu peers\n",
1336 ntohl (msg->num_peers),
1337 (ntohs (msg->header.size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1338 sizeof (struct GNUNET_PeerIdentity));
1339 GNUNET_break_op (0);
1340 return GNUNET_SYSERR;
1343 if (GNUNET_YES != Peers_check_peer_flag (sender, Peers_PULL_REPLY_PENDING))
1345 LOG (GNUNET_ERROR_TYPE_WARNING,
1346 "Received a pull reply from a peer we didn't request one from!\n");
1347 GNUNET_break_op (0);
1348 return GNUNET_SYSERR;
1354 * Handle PULL REPLY message from another peer.
1356 * @param cls Closure
1357 * @param msg The message header
1360 handle_peer_pull_reply (void *cls,
1361 const struct GNUNET_RPS_P2P_PullReplyMessage *msg)
1363 struct GNUNET_PeerIdentity *peers;
1364 struct GNUNET_PeerIdentity *sender = cls;
1366 #ifdef ENABLE_MALICIOUS
1367 struct AttackedPeer *tmp_att_peer;
1368 #endif /* ENABLE_MALICIOUS */
1370 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REPLY (%s)\n", GNUNET_i2s (sender));
1372 #ifdef ENABLE_MALICIOUS
1373 // We shouldn't even receive pull replies as we're not sending
1376 GNUNET_CADET_receive_done (Peers_get_recv_channel (sender));
1378 #endif /* ENABLE_MALICIOUS */
1380 /* Do actual logic */
1381 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1383 LOG (GNUNET_ERROR_TYPE_DEBUG,
1384 "PULL REPLY received, got following %u peers:\n",
1385 ntohl (msg->num_peers));
1387 for (i = 0; i < ntohl (msg->num_peers); i++)
1389 LOG (GNUNET_ERROR_TYPE_DEBUG,
1392 GNUNET_i2s (&peers[i]));
1394 #ifdef ENABLE_MALICIOUS
1395 if ((NULL != att_peer_set) &&
1396 (1 == mal_type || 3 == mal_type))
1397 { /* Add attacked peer to local list */
1398 // TODO check if we sent a request and this was the first reply
1399 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1401 && GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mal_peer_set,
1403 && 0 != GNUNET_CRYPTO_cmp_peer_identity (&peers[i],
1406 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1407 tmp_att_peer->peer_id = peers[i];
1408 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1411 add_peer_array_to_set (&peers[i], 1, att_peer_set);
1415 #endif /* ENABLE_MALICIOUS */
1416 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity,
1419 /* Make sure we 'know' about this peer */
1420 (void) Peers_insert_peer (&peers[i]);
1422 if (GNUNET_YES == Peers_check_peer_valid (&peers[i]))
1424 CustomPeerMap_put (pull_map, &peers[i]);
1428 Peers_schedule_operation (&peers[i], insert_in_pull_map);
1429 (void) Peers_issue_peer_liveliness_check (&peers[i]);
1434 Peers_unset_peer_flag (sender, Peers_PULL_REPLY_PENDING);
1435 clean_peer (sender);
1437 GNUNET_CADET_receive_done (Peers_get_recv_channel (sender));
1442 * Compute a random delay.
1443 * A uniformly distributed value between mean + spread and mean - spread.
1445 * For example for mean 4 min and spread 2 the minimum is (4 min - (1/2 * 4 min))
1446 * It would return a random value between 2 and 6 min.
1448 * @param mean the mean
1449 * @param spread the inverse amount of deviation from the mean
1451 static struct GNUNET_TIME_Relative
1452 compute_rand_delay (struct GNUNET_TIME_Relative mean,
1453 unsigned int spread)
1455 struct GNUNET_TIME_Relative half_interval;
1456 struct GNUNET_TIME_Relative ret;
1457 unsigned int rand_delay;
1458 unsigned int max_rand_delay;
1462 LOG (GNUNET_ERROR_TYPE_WARNING,
1463 "Not accepting spread of 0\n");
1467 GNUNET_assert (0 != mean.rel_value_us);
1469 /* Compute random time value between spread * mean and spread * mean */
1470 half_interval = GNUNET_TIME_relative_divide (mean, spread);
1472 max_rand_delay = GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us / mean.rel_value_us * (2/spread);
1474 * Compute random value between (0 and 1) * round_interval
1475 * via multiplying round_interval with a 'fraction' (0 to value)/value
1477 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_rand_delay);
1478 ret = GNUNET_TIME_relative_saturating_multiply (mean, rand_delay);
1479 ret = GNUNET_TIME_relative_divide (ret, max_rand_delay);
1480 ret = GNUNET_TIME_relative_add (ret, half_interval);
1482 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ret.rel_value_us)
1483 LOG (GNUNET_ERROR_TYPE_WARNING,
1484 "Returning FOREVER_REL\n");
1491 * Send single pull request
1493 * @param peer_id the peer to send the pull request to.
1496 send_pull_request (const struct GNUNET_PeerIdentity *peer)
1498 struct GNUNET_MQ_Envelope *ev;
1500 GNUNET_assert (GNUNET_NO == Peers_check_peer_flag (peer,
1501 Peers_PULL_REPLY_PENDING));
1502 Peers_set_peer_flag (peer, Peers_PULL_REPLY_PENDING);
1504 LOG (GNUNET_ERROR_TYPE_DEBUG,
1505 "Going to send PULL REQUEST to peer %s.\n",
1508 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
1509 Peers_send_message (peer, ev, "PULL REQUEST");
1516 * @param peer_id the peer to send the push to.
1519 send_push (const struct GNUNET_PeerIdentity *peer_id)
1521 struct GNUNET_MQ_Envelope *ev;
1523 LOG (GNUNET_ERROR_TYPE_DEBUG,
1524 "Going to send PUSH to peer %s.\n",
1525 GNUNET_i2s (peer_id));
1527 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
1528 Peers_send_message (peer_id, ev, "PUSH");
1533 do_round (void *cls);
1536 do_mal_round (void *cls);
1538 #ifdef ENABLE_MALICIOUS
1542 * @brief This function is called, when the client tells us to act malicious.
1543 * It verifies that @a msg is well-formed.
1545 * @param cls the closure (#ClientContext)
1546 * @param msg the message
1547 * @return #GNUNET_OK if @a msg is well-formed
1550 check_client_act_malicious (void *cls,
1551 const struct GNUNET_RPS_CS_ActMaliciousMessage *msg)
1553 struct ClientContext *cli_ctx = cls;
1554 uint16_t msize = ntohs (msg->header.size);
1555 uint32_t num_peers = ntohl (msg->num_peers);
1557 msize -= sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage);
1558 if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) ||
1559 (msize % sizeof (struct GNUNET_PeerIdentity) != 0) )
1561 LOG (GNUNET_ERROR_TYPE_ERROR,
1562 "message says it sends %" PRIu32 " peers, have space for %lu peers\n",
1563 ntohl (msg->num_peers),
1564 (msize / sizeof (struct GNUNET_PeerIdentity)));
1566 GNUNET_SERVICE_client_drop (cli_ctx->client);
1567 return GNUNET_SYSERR;
1573 * Turn RPS service to act malicious.
1575 * @param cls Closure
1576 * @param client The client that sent the message
1577 * @param msg The message header
1580 handle_client_act_malicious (void *cls,
1581 const struct GNUNET_RPS_CS_ActMaliciousMessage *msg)
1583 struct ClientContext *cli_ctx = cls;
1584 struct GNUNET_PeerIdentity *peers;
1585 uint32_t num_mal_peers_sent;
1586 uint32_t num_mal_peers_old;
1588 /* Do actual logic */
1589 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1590 mal_type = ntohl (msg->type);
1591 if (NULL == mal_peer_set)
1592 mal_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
1594 LOG (GNUNET_ERROR_TYPE_DEBUG,
1595 "Now acting malicious type %" PRIu32 ", got %" PRIu32 " peers.\n",
1597 ntohl (msg->num_peers));
1600 { /* Try to maximise representation */
1601 /* Add other malicious peers to those we already know */
1603 num_mal_peers_sent = ntohl (msg->num_peers);
1604 num_mal_peers_old = num_mal_peers;
1605 GNUNET_array_grow (mal_peers,
1607 num_mal_peers + num_mal_peers_sent);
1608 GNUNET_memcpy (&mal_peers[num_mal_peers_old],
1610 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1612 /* Add all mal peers to mal_peer_set */
1613 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1617 /* Substitute do_round () with do_mal_round () */
1618 GNUNET_SCHEDULER_cancel (do_round_task);
1619 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1622 else if ( (2 == mal_type) ||
1624 { /* Try to partition the network */
1625 /* Add other malicious peers to those we already know */
1627 num_mal_peers_sent = ntohl (msg->num_peers) - 1;
1628 num_mal_peers_old = num_mal_peers;
1629 GNUNET_array_grow (mal_peers,
1631 num_mal_peers + num_mal_peers_sent);
1632 if (NULL != mal_peers &&
1635 GNUNET_memcpy (&mal_peers[num_mal_peers_old],
1637 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1639 /* Add all mal peers to mal_peer_set */
1640 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1645 /* Store the one attacked peer */
1646 GNUNET_memcpy (&attacked_peer,
1647 &msg->attacked_peer,
1648 sizeof (struct GNUNET_PeerIdentity));
1649 /* Set the flag of the attacked peer to valid to avoid problems */
1650 if (GNUNET_NO == Peers_check_peer_known (&attacked_peer))
1652 (void) Peers_issue_peer_liveliness_check (&attacked_peer);
1655 LOG (GNUNET_ERROR_TYPE_DEBUG,
1656 "Attacked peer is %s\n",
1657 GNUNET_i2s (&attacked_peer));
1659 /* Substitute do_round () with do_mal_round () */
1660 GNUNET_SCHEDULER_cancel (do_round_task);
1661 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1663 else if (0 == mal_type)
1664 { /* Stop acting malicious */
1665 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
1667 /* Substitute do_mal_round () with do_round () */
1668 GNUNET_SCHEDULER_cancel (do_round_task);
1669 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
1674 GNUNET_SERVICE_client_continue (cli_ctx->client);
1676 GNUNET_SERVICE_client_continue (cli_ctx->client);
1681 * Send out PUSHes and PULLs maliciously.
1683 * This is executed regylary.
1686 do_mal_round (void *cls)
1688 uint32_t num_pushes;
1690 struct GNUNET_TIME_Relative time_next_round;
1691 struct AttackedPeer *tmp_att_peer;
1693 LOG (GNUNET_ERROR_TYPE_DEBUG,
1694 "Going to execute next round maliciously type %" PRIu32 ".\n",
1696 do_round_task = NULL;
1697 GNUNET_assert (mal_type <= 3);
1698 /* Do malicious actions */
1700 { /* Try to maximise representation */
1702 /* The maximum of pushes we're going to send this round */
1703 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit,
1704 num_attacked_peers),
1705 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
1707 LOG (GNUNET_ERROR_TYPE_DEBUG,
1708 "Going to send %" PRIu32 " pushes\n",
1711 /* Send PUSHes to attacked peers */
1712 for (i = 0 ; i < num_pushes ; i++)
1714 if (att_peers_tail == att_peer_index)
1715 att_peer_index = att_peers_head;
1717 att_peer_index = att_peer_index->next;
1719 send_push (&att_peer_index->peer_id);
1722 /* Send PULLs to some peers to learn about additional peers to attack */
1723 tmp_att_peer = att_peer_index;
1724 for (i = 0 ; i < num_pushes * alpha ; i++)
1726 if (att_peers_tail == tmp_att_peer)
1727 tmp_att_peer = att_peers_head;
1729 att_peer_index = tmp_att_peer->next;
1731 send_pull_request (&tmp_att_peer->peer_id);
1736 else if (2 == mal_type)
1738 * Try to partition the network
1739 * Send as many pushes to the attacked peer as possible
1740 * That is one push per round as it will ignore more.
1742 (void) Peers_issue_peer_liveliness_check (&attacked_peer);
1743 if (GNUNET_YES == Peers_check_peer_flag (&attacked_peer, Peers_ONLINE))
1744 send_push (&attacked_peer);
1749 { /* Combined attack */
1751 /* Send PUSH to attacked peers */
1752 if (GNUNET_YES == Peers_check_peer_known (&attacked_peer))
1754 (void) Peers_issue_peer_liveliness_check (&attacked_peer);
1755 if (GNUNET_YES == Peers_check_peer_flag (&attacked_peer, Peers_ONLINE))
1757 LOG (GNUNET_ERROR_TYPE_DEBUG,
1758 "Goding to send push to attacked peer (%s)\n",
1759 GNUNET_i2s (&attacked_peer));
1760 send_push (&attacked_peer);
1763 (void) Peers_issue_peer_liveliness_check (&attacked_peer);
1765 /* The maximum of pushes we're going to send this round */
1766 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit - 1,
1767 num_attacked_peers),
1768 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
1770 LOG (GNUNET_ERROR_TYPE_DEBUG,
1771 "Going to send %" PRIu32 " pushes\n",
1774 for (i = 0; i < num_pushes; i++)
1776 if (att_peers_tail == att_peer_index)
1777 att_peer_index = att_peers_head;
1779 att_peer_index = att_peer_index->next;
1781 send_push (&att_peer_index->peer_id);
1784 /* Send PULLs to some peers to learn about additional peers to attack */
1785 tmp_att_peer = att_peer_index;
1786 for (i = 0; i < num_pushes * alpha; i++)
1788 if (att_peers_tail == tmp_att_peer)
1789 tmp_att_peer = att_peers_head;
1791 att_peer_index = tmp_att_peer->next;
1793 send_pull_request (&tmp_att_peer->peer_id);
1797 /* Schedule next round */
1798 time_next_round = compute_rand_delay (round_interval, 2);
1800 //do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_mal_round,
1802 GNUNET_assert (NULL == do_round_task);
1803 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round,
1804 &do_mal_round, NULL);
1805 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
1807 #endif /* ENABLE_MALICIOUS */
1811 * Send out PUSHes and PULLs, possibly update #view, samplers.
1813 * This is executed regylary.
1816 do_round (void *cls)
1819 const struct GNUNET_PeerIdentity *view_array;
1820 unsigned int *permut;
1821 unsigned int a_peers; /* Number of peers we send pushes to */
1822 unsigned int b_peers; /* Number of peers we send pull requests to */
1823 uint32_t first_border;
1824 uint32_t second_border;
1825 struct GNUNET_PeerIdentity peer;
1826 struct GNUNET_PeerIdentity *update_peer;
1828 LOG (GNUNET_ERROR_TYPE_DEBUG,
1829 "Going to execute next round.\n");
1830 do_round_task = NULL;
1831 LOG (GNUNET_ERROR_TYPE_DEBUG,
1832 "Printing view:\n");
1833 to_file (file_name_view_log,
1834 "___ new round ___");
1835 view_array = View_get_as_array ();
1836 for (i = 0; i < View_size (); i++)
1838 LOG (GNUNET_ERROR_TYPE_DEBUG,
1839 "\t%s\n", GNUNET_i2s (&view_array[i]));
1840 to_file (file_name_view_log,
1842 GNUNET_i2s_full (&view_array[i]));
1846 /* Send pushes and pull requests */
1847 if (0 < View_size ())
1849 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
1853 a_peers = ceil (alpha * View_size ());
1855 LOG (GNUNET_ERROR_TYPE_DEBUG,
1856 "Going to send pushes to %u (ceil (%f * %u)) peers.\n",
1857 a_peers, alpha, View_size ());
1858 for (i = 0; i < a_peers; i++)
1860 peer = view_array[permut[i]];
1861 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer)) // TODO
1862 { // FIXME if this fails schedule/loop this for later
1867 /* Send PULL requests */
1868 b_peers = ceil (beta * View_size ());
1869 first_border = a_peers;
1870 second_border = a_peers + b_peers;
1871 if (second_border > View_size ())
1873 first_border = View_size () - b_peers;
1874 second_border = View_size ();
1876 LOG (GNUNET_ERROR_TYPE_DEBUG,
1877 "Going to send pulls to %u (ceil (%f * %u)) peers.\n",
1878 b_peers, beta, View_size ());
1879 for (i = first_border; i < second_border; i++)
1881 peer = view_array[permut[i]];
1882 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer) &&
1883 GNUNET_NO == Peers_check_peer_flag (&peer, Peers_PULL_REPLY_PENDING)) // TODO
1884 { // FIXME if this fails schedule/loop this for later
1885 send_pull_request (&peer);
1889 GNUNET_free (permut);
1895 /* TODO see how many peers are in push-/pull- list! */
1897 if ((CustomPeerMap_size (push_map) <= alpha * View_size ()) &&
1898 (0 < CustomPeerMap_size (push_map)) &&
1899 (0 < CustomPeerMap_size (pull_map)))
1900 { /* If conditions for update are fulfilled, update */
1901 LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the view.\n");
1903 uint32_t final_size;
1904 uint32_t peers_to_clean_size;
1905 struct GNUNET_PeerIdentity *peers_to_clean;
1907 peers_to_clean = NULL;
1908 peers_to_clean_size = 0;
1909 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, View_size ());
1910 GNUNET_memcpy (peers_to_clean,
1912 View_size () * sizeof (struct GNUNET_PeerIdentity));
1914 /* Seems like recreating is the easiest way of emptying the peermap */
1916 to_file (file_name_view_log,
1919 first_border = GNUNET_MIN (ceil (alpha * sampler_size_est_need),
1920 CustomPeerMap_size (push_map));
1921 second_border = first_border +
1922 GNUNET_MIN (floor (beta * sampler_size_est_need),
1923 CustomPeerMap_size (pull_map));
1924 final_size = second_border +
1925 ceil ((1 - (alpha + beta)) * sampler_size_est_need);
1927 /* Update view with peers received through PUSHes */
1928 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
1929 CustomPeerMap_size (push_map));
1930 for (i = 0; i < first_border; i++)
1932 (void) insert_in_view (CustomPeerMap_get_peer_by_index (push_map,
1934 to_file (file_name_view_log,
1936 GNUNET_i2s_full (&view_array[i]));
1937 // TODO change the peer_flags accordingly
1939 GNUNET_free (permut);
1942 /* Update view with peers received through PULLs */
1943 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
1944 CustomPeerMap_size (pull_map));
1945 for (i = first_border; i < second_border; i++)
1947 (void) insert_in_view (CustomPeerMap_get_peer_by_index (pull_map,
1948 permut[i - first_border]));
1949 to_file (file_name_view_log,
1951 GNUNET_i2s_full (&view_array[i]));
1952 // TODO change the peer_flags accordingly
1954 GNUNET_free (permut);
1957 /* Update view with peers from history */
1958 RPS_sampler_get_n_rand_peers (prot_sampler,
1961 final_size - second_border);
1962 // TODO change the peer_flags accordingly
1964 for (i = 0; i < View_size (); i++)
1965 rem_from_list (&peers_to_clean, &peers_to_clean_size, &view_array[i]);
1967 /* Clean peers that were removed from the view */
1968 for (i = 0; i < peers_to_clean_size; i++)
1970 to_file (file_name_view_log,
1972 GNUNET_i2s_full (&peers_to_clean[i]));
1973 clean_peer (&peers_to_clean[i]);
1974 //peer_destroy_channel_send (sender);
1977 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, 0);
1981 LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the view.\n");
1983 // TODO independent of that also get some peers from CADET_get_peers()?
1985 LOG (GNUNET_ERROR_TYPE_DEBUG,
1986 "Received %u pushes and %u pulls last round (alpha (%.2f) * view_size (%u) = %.2f)\n",
1987 CustomPeerMap_size (push_map),
1988 CustomPeerMap_size (pull_map),
1991 alpha * View_size ());
1993 /* Update samplers */
1994 for (i = 0; i < CustomPeerMap_size (push_map); i++)
1996 update_peer = CustomPeerMap_get_peer_by_index (push_map, i);
1997 LOG (GNUNET_ERROR_TYPE_DEBUG,
1998 "Updating with peer %s from push list\n",
1999 GNUNET_i2s (update_peer));
2000 insert_in_sampler (NULL, update_peer);
2001 clean_peer (update_peer); /* This cleans only if it is not in the view */
2002 //peer_destroy_channel_send (sender);
2005 for (i = 0; i < CustomPeerMap_size (pull_map); i++)
2007 LOG (GNUNET_ERROR_TYPE_DEBUG,
2008 "Updating with peer %s from pull list\n",
2009 GNUNET_i2s (CustomPeerMap_get_peer_by_index (pull_map, i)));
2010 insert_in_sampler (NULL, CustomPeerMap_get_peer_by_index (pull_map, i));
2011 /* This cleans only if it is not in the view */
2012 clean_peer (CustomPeerMap_get_peer_by_index (pull_map, i));
2013 //peer_destroy_channel_send (sender);
2017 /* Empty push/pull lists */
2018 CustomPeerMap_clear (push_map);
2019 CustomPeerMap_clear (pull_map);
2021 struct GNUNET_TIME_Relative time_next_round;
2023 time_next_round = compute_rand_delay (round_interval, 2);
2025 /* Schedule next round */
2026 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round,
2028 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
2033 * This is called from GNUNET_CADET_get_peers().
2035 * It is called on every peer(ID) that cadet somehow has contact with.
2036 * We use those to initialise the sampler.
2039 init_peer_cb (void *cls,
2040 const struct GNUNET_PeerIdentity *peer,
2041 int tunnel, // "Do we have a tunnel towards this peer?"
2042 unsigned int n_paths, // "Number of known paths towards this peer"
2043 unsigned int best_path) // "How long is the best path?
2044 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
2048 LOG (GNUNET_ERROR_TYPE_DEBUG,
2049 "Got peer_id %s from cadet\n",
2056 * @brief Iterator function over stored, valid peers.
2058 * We initialise the sampler with those.
2060 * @param cls the closure
2061 * @param peer the peer id
2062 * @return #GNUNET_YES if we should continue to
2064 * #GNUNET_NO if not.
2067 valid_peers_iterator (void *cls,
2068 const struct GNUNET_PeerIdentity *peer)
2072 LOG (GNUNET_ERROR_TYPE_DEBUG,
2073 "Got stored, valid peer %s\n",
2082 * Iterator over peers from peerinfo.
2084 * @param cls closure
2085 * @param peer id of the peer, NULL for last call
2086 * @param hello hello message for the peer (can be NULL)
2087 * @param error message
2090 process_peerinfo_peers (void *cls,
2091 const struct GNUNET_PeerIdentity *peer,
2092 const struct GNUNET_HELLO_Message *hello,
2093 const char *err_msg)
2097 LOG (GNUNET_ERROR_TYPE_DEBUG,
2098 "Got peer_id %s from peerinfo\n",
2106 * Task run during shutdown.
2111 shutdown_task (void *cls)
2113 struct ClientContext *client_ctx;
2114 struct ReplyCls *reply_cls;
2116 LOG (GNUNET_ERROR_TYPE_DEBUG,
2117 "RPS is going down\n");
2119 /* Clean all clients */
2120 for (client_ctx = cli_ctx_head;
2121 NULL != cli_ctx_head;
2122 client_ctx = cli_ctx_head)
2124 /* Clean pending requests to the sampler */
2125 for (reply_cls = client_ctx->rep_cls_head;
2126 NULL != client_ctx->rep_cls_head;
2127 reply_cls = client_ctx->rep_cls_head)
2129 RPS_sampler_request_cancel (reply_cls->req_handle);
2130 GNUNET_CONTAINER_DLL_remove (client_ctx->rep_cls_head,
2131 client_ctx->rep_cls_tail,
2133 GNUNET_free (reply_cls);
2135 GNUNET_MQ_destroy (client_ctx->mq);
2136 GNUNET_CONTAINER_DLL_remove (cli_ctx_head, cli_ctx_tail, client_ctx);
2137 GNUNET_free (client_ctx);
2139 GNUNET_PEERINFO_notify_cancel (peerinfo_notify_handle);
2140 GNUNET_PEERINFO_disconnect (peerinfo_handle);
2142 if (NULL != do_round_task)
2144 GNUNET_SCHEDULER_cancel (do_round_task);
2145 do_round_task = NULL;
2150 GNUNET_NSE_disconnect (nse);
2151 RPS_sampler_destroy (prot_sampler);
2152 RPS_sampler_destroy (client_sampler);
2153 GNUNET_CADET_disconnect (cadet_handle);
2155 CustomPeerMap_destroy (push_map);
2156 CustomPeerMap_destroy (pull_map);
2157 #ifdef ENABLE_MALICIOUS
2158 struct AttackedPeer *tmp_att_peer;
2159 GNUNET_free (file_name_view_log);
2160 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
2161 if (NULL != mal_peer_set)
2162 GNUNET_CONTAINER_multipeermap_destroy (mal_peer_set);
2163 if (NULL != att_peer_set)
2164 GNUNET_CONTAINER_multipeermap_destroy (att_peer_set);
2165 while (NULL != att_peers_head)
2167 tmp_att_peer = att_peers_head;
2168 GNUNET_CONTAINER_DLL_remove (att_peers_head, att_peers_tail, tmp_att_peer);
2170 #endif /* ENABLE_MALICIOUS */
2175 * Handle client connecting to the service.
2178 * @param client the new client
2179 * @param mq the message queue of @a client
2183 client_connect_cb (void *cls,
2184 struct GNUNET_SERVICE_Client *client,
2185 struct GNUNET_MQ_Handle *mq)
2187 struct ClientContext *cli_ctx;
2189 LOG (GNUNET_ERROR_TYPE_DEBUG,
2190 "Client connected\n");
2192 return client; /* Server was destroyed before a client connected. Shutting down */
2193 cli_ctx = GNUNET_new (struct ClientContext);
2194 cli_ctx->mq = GNUNET_SERVICE_client_get_mq (client);
2195 cli_ctx->client = client;
2196 GNUNET_CONTAINER_DLL_insert (cli_ctx_head,
2203 * Callback called when a client disconnected from the service
2205 * @param cls closure for the service
2206 * @param c the client that disconnected
2207 * @param internal_cls should be equal to @a c
2210 client_disconnect_cb (void *cls,
2211 struct GNUNET_SERVICE_Client *client,
2214 struct ClientContext *cli_ctx = internal_cls;
2216 GNUNET_assert (client == cli_ctx->client);
2218 {/* shutdown task - destroy all clients */
2219 while (NULL != cli_ctx_head)
2220 destroy_cli_ctx (cli_ctx_head);
2223 { /* destroy this client */
2224 LOG (GNUNET_ERROR_TYPE_DEBUG,
2225 "Client disconnected. Destroy its context.\n");
2226 destroy_cli_ctx (cli_ctx);
2232 * Handle random peer sampling clients.
2234 * @param cls closure
2235 * @param c configuration to use
2236 * @param service the initialized service
2240 const struct GNUNET_CONFIGURATION_Handle *c,
2241 struct GNUNET_SERVICE_Handle *service)
2243 struct GNUNET_MQ_MessageHandler cadet_handlers[] = {
2244 GNUNET_MQ_hd_fixed_size (peer_check,
2245 GNUNET_MESSAGE_TYPE_RPS_PP_CHECK_LIVE,
2246 struct GNUNET_MessageHeader,
2248 GNUNET_MQ_hd_fixed_size (peer_push,
2249 GNUNET_MESSAGE_TYPE_RPS_PP_PUSH,
2250 struct GNUNET_MessageHeader,
2252 GNUNET_MQ_hd_fixed_size (peer_pull_request,
2253 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
2254 struct GNUNET_MessageHeader,
2256 GNUNET_MQ_hd_var_size (peer_pull_reply,
2257 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY,
2258 struct GNUNET_RPS_P2P_PullReplyMessage,
2260 GNUNET_MQ_handler_end ()
2265 char* fn_valid_peers;
2266 struct GNUNET_HashCode port;
2268 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
2273 GNUNET_CRYPTO_get_peer_identity (cfg, &own_identity); // TODO check return value
2274 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2275 "STARTING SERVICE (rps) for peer [%s]\n",
2276 GNUNET_i2s (&own_identity));
2277 #ifdef ENABLE_MALICIOUS
2278 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2279 "Malicious execution compiled in.\n");
2280 #endif /* ENABLE_MALICIOUS */
2284 /* Get time interval from the configuration */
2285 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
2289 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2290 "RPS", "ROUNDINTERVAL");
2291 GNUNET_SCHEDULER_shutdown ();
2295 /* Get initial size of sampler/view from the configuration */
2297 GNUNET_CONFIGURATION_get_value_number (cfg, "RPS", "INITSIZE",
2298 (long long unsigned int *) &sampler_size_est_need))
2300 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2302 GNUNET_SCHEDULER_shutdown ();
2305 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %u\n", sampler_size_est_need);
2308 GNUNET_CONFIGURATION_get_value_filename (cfg,
2310 "FILENAME_VALID_PEERS",
2313 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2314 "rps", "FILENAME_VALID_PEERS");
2320 /* file_name_view_log */
2321 if (GNUNET_OK != GNUNET_DISK_directory_create ("/tmp/rps/"))
2323 LOG (GNUNET_ERROR_TYPE_WARNING,
2324 "Failed to create directory /tmp/rps/\n");
2327 size = (14 + strlen (GNUNET_i2s_full (&own_identity)) + 1) * sizeof (char);
2328 file_name_view_log = GNUNET_malloc (size);
2329 out_size = GNUNET_snprintf (file_name_view_log,
2332 GNUNET_i2s_full (&own_identity));
2333 if (size < out_size ||
2336 LOG (GNUNET_ERROR_TYPE_WARNING,
2337 "Failed to write string to buffer (size: %i, out_size: %i)\n",
2343 /* connect to NSE */
2344 nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
2351 /* Initialise cadet */
2352 cadet_handle = GNUNET_CADET_connect (cfg);
2353 GNUNET_assert (NULL != cadet_handle);
2354 GNUNET_CRYPTO_hash (GNUNET_APPLICATION_PORT_RPS,
2355 strlen (GNUNET_APPLICATION_PORT_RPS),
2357 cadet_port = GNUNET_CADET_open_port (cadet_handle,
2359 &Peers_handle_inbound_channel, /* Connect handler */
2361 NULL, /* WindowSize handler */
2362 cleanup_destroyed_channel, /* Disconnect handler */
2366 peerinfo_handle = GNUNET_PEERINFO_connect (cfg);
2367 Peers_initialise (fn_valid_peers, cadet_handle, cleanup_destroyed_channel,
2368 cadet_handlers, &own_identity);
2369 GNUNET_free (fn_valid_peers);
2371 /* Initialise sampler */
2372 struct GNUNET_TIME_Relative half_round_interval;
2373 struct GNUNET_TIME_Relative max_round_interval;
2375 half_round_interval = GNUNET_TIME_relative_divide (round_interval, 2);
2376 max_round_interval = GNUNET_TIME_relative_add (round_interval, half_round_interval);
2378 prot_sampler = RPS_sampler_init (sampler_size_est_need, max_round_interval);
2379 client_sampler = RPS_sampler_mod_init (sampler_size_est_need, max_round_interval);
2381 /* Initialise push and pull maps */
2382 push_map = CustomPeerMap_create (4);
2383 pull_map = CustomPeerMap_create (4);
2386 //LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
2387 //GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, NULL);
2388 // TODO send push/pull to each of those peers?
2389 // TODO read stored valid peers from last run
2390 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting stored valid peers\n");
2391 Peers_get_valid_peers (valid_peers_iterator, NULL);
2393 peerinfo_notify_handle = GNUNET_PEERINFO_notify (cfg,
2395 process_peerinfo_peers,
2398 LOG (GNUNET_ERROR_TYPE_INFO, "Ready to receive requests from clients\n");
2400 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
2401 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
2403 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
2408 * Define "main" method using service macro.
2412 GNUNET_SERVICE_OPTION_NONE,
2415 &client_disconnect_cb,
2417 GNUNET_MQ_hd_fixed_size (client_request,
2418 GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
2419 struct GNUNET_RPS_CS_RequestMessage,
2421 GNUNET_MQ_hd_fixed_size (client_request_cancel,
2422 GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST_CANCEL,
2423 struct GNUNET_RPS_CS_RequestCancelMessage,
2425 GNUNET_MQ_hd_var_size (client_seed,
2426 GNUNET_MESSAGE_TYPE_RPS_CS_SEED,
2427 struct GNUNET_RPS_CS_SeedMessage,
2429 #ifdef ENABLE_MALICIOUS
2430 GNUNET_MQ_hd_var_size (client_act_malicious,
2431 GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS,
2432 struct GNUNET_RPS_CS_ActMaliciousMessage,
2434 #endif /* ENABLE_MALICIOUS */
2435 GNUNET_MQ_handler_end());
2437 /* end of gnunet-service-rps.c */