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 tmp_att_peer->peer_id = *peer;
1243 if (NULL == att_peer_set)
1244 att_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
1246 GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1249 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1252 add_peer_array_to_set (peer, 1, att_peer_set);
1257 else if (2 == mal_type)
1259 /* We attack one single well-known peer - simply ignore */
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);
1294 else if (2 == mal_type)
1295 { /* Try to partition network */
1296 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
1298 send_pull_reply (peer, mal_peers, num_mal_peers);
1301 #endif /* ENABLE_MALICIOUS */
1303 view_array = View_get_as_array ();
1304 send_pull_reply (peer, view_array, View_size ());
1306 GNUNET_CADET_receive_done (Peers_get_recv_channel (peer));
1311 * Check whether we sent a corresponding request and
1312 * whether this reply is the first one.
1314 * @param cls Closure
1315 * @param msg The message header
1318 check_peer_pull_reply (void *cls,
1319 const struct GNUNET_RPS_P2P_PullReplyMessage *msg)
1321 struct GNUNET_PeerIdentity *sender = cls;
1323 if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->header.size))
1325 GNUNET_break_op (0);
1326 return GNUNET_SYSERR;
1329 if ((ntohs (msg->header.size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1330 sizeof (struct GNUNET_PeerIdentity) != ntohl (msg->num_peers))
1332 LOG (GNUNET_ERROR_TYPE_ERROR,
1333 "message says it sends %" PRIu32 " peers, have space for %lu peers\n",
1334 ntohl (msg->num_peers),
1335 (ntohs (msg->header.size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1336 sizeof (struct GNUNET_PeerIdentity));
1337 GNUNET_break_op (0);
1338 return GNUNET_SYSERR;
1341 if (GNUNET_YES != Peers_check_peer_flag (sender, Peers_PULL_REPLY_PENDING))
1343 LOG (GNUNET_ERROR_TYPE_WARNING,
1344 "Received a pull reply from a peer we didn't request one from!\n");
1345 GNUNET_break_op (0);
1346 return GNUNET_SYSERR;
1352 * Handle PULL REPLY message from another peer.
1354 * @param cls Closure
1355 * @param msg The message header
1358 handle_peer_pull_reply (void *cls,
1359 const struct GNUNET_RPS_P2P_PullReplyMessage *msg)
1361 const struct GNUNET_PeerIdentity *peers;
1362 struct GNUNET_PeerIdentity *sender = cls;
1364 #ifdef ENABLE_MALICIOUS
1365 struct AttackedPeer *tmp_att_peer;
1366 #endif /* ENABLE_MALICIOUS */
1368 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REPLY (%s)\n", GNUNET_i2s (sender));
1370 #ifdef ENABLE_MALICIOUS
1371 // We shouldn't even receive pull replies as we're not sending
1375 #endif /* ENABLE_MALICIOUS */
1377 /* Do actual logic */
1378 peers = (const struct GNUNET_PeerIdentity *) &msg[1];
1380 LOG (GNUNET_ERROR_TYPE_DEBUG,
1381 "PULL REPLY received, got following %u peers:\n",
1382 ntohl (msg->num_peers));
1384 for (i = 0; i < ntohl (msg->num_peers); i++)
1386 LOG (GNUNET_ERROR_TYPE_DEBUG,
1389 GNUNET_i2s (&peers[i]));
1391 #ifdef ENABLE_MALICIOUS
1392 if ((NULL != att_peer_set) &&
1393 (1 == mal_type || 3 == mal_type))
1394 { /* Add attacked peer to local list */
1395 // TODO check if we sent a request and this was the first reply
1396 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1398 && GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mal_peer_set,
1400 && 0 != GNUNET_CRYPTO_cmp_peer_identity (&peers[i],
1403 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1404 tmp_att_peer->peer_id = peers[i];
1405 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1408 add_peer_array_to_set (&peers[i], 1, att_peer_set);
1412 #endif /* ENABLE_MALICIOUS */
1413 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity,
1416 /* Make sure we 'know' about this peer */
1417 (void) Peers_insert_peer (&peers[i]);
1419 if (GNUNET_YES == Peers_check_peer_valid (&peers[i]))
1421 CustomPeerMap_put (pull_map, &peers[i]);
1425 Peers_schedule_operation (&peers[i], insert_in_pull_map);
1426 (void) Peers_issue_peer_liveliness_check (&peers[i]);
1431 Peers_unset_peer_flag (sender, Peers_PULL_REPLY_PENDING);
1432 clean_peer (sender);
1434 GNUNET_CADET_receive_done (Peers_get_recv_channel (sender));
1439 * Compute a random delay.
1440 * A uniformly distributed value between mean + spread and mean - spread.
1442 * For example for mean 4 min and spread 2 the minimum is (4 min - (1/2 * 4 min))
1443 * It would return a random value between 2 and 6 min.
1445 * @param mean the mean
1446 * @param spread the inverse amount of deviation from the mean
1448 static struct GNUNET_TIME_Relative
1449 compute_rand_delay (struct GNUNET_TIME_Relative mean,
1450 unsigned int spread)
1452 struct GNUNET_TIME_Relative half_interval;
1453 struct GNUNET_TIME_Relative ret;
1454 unsigned int rand_delay;
1455 unsigned int max_rand_delay;
1459 LOG (GNUNET_ERROR_TYPE_WARNING,
1460 "Not accepting spread of 0\n");
1464 GNUNET_assert (0 != mean.rel_value_us);
1466 /* Compute random time value between spread * mean and spread * mean */
1467 half_interval = GNUNET_TIME_relative_divide (mean, spread);
1469 max_rand_delay = GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us / mean.rel_value_us * (2/spread);
1471 * Compute random value between (0 and 1) * round_interval
1472 * via multiplying round_interval with a 'fraction' (0 to value)/value
1474 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_rand_delay);
1475 ret = GNUNET_TIME_relative_saturating_multiply (mean, rand_delay);
1476 ret = GNUNET_TIME_relative_divide (ret, max_rand_delay);
1477 ret = GNUNET_TIME_relative_add (ret, half_interval);
1479 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ret.rel_value_us)
1480 LOG (GNUNET_ERROR_TYPE_WARNING,
1481 "Returning FOREVER_REL\n");
1488 * Send single pull request
1490 * @param peer_id the peer to send the pull request to.
1493 send_pull_request (const struct GNUNET_PeerIdentity *peer)
1495 struct GNUNET_MQ_Envelope *ev;
1497 GNUNET_assert (GNUNET_NO == Peers_check_peer_flag (peer,
1498 Peers_PULL_REPLY_PENDING));
1499 Peers_set_peer_flag (peer, Peers_PULL_REPLY_PENDING);
1501 LOG (GNUNET_ERROR_TYPE_DEBUG,
1502 "Going to send PULL REQUEST to peer %s.\n",
1505 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
1506 Peers_send_message (peer, ev, "PULL REQUEST");
1513 * @param peer_id the peer to send the push to.
1516 send_push (const struct GNUNET_PeerIdentity *peer_id)
1518 struct GNUNET_MQ_Envelope *ev;
1520 LOG (GNUNET_ERROR_TYPE_DEBUG,
1521 "Going to send PUSH to peer %s.\n",
1522 GNUNET_i2s (peer_id));
1524 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
1525 Peers_send_message (peer_id, ev, "PUSH");
1530 do_round (void *cls);
1533 do_mal_round (void *cls);
1535 #ifdef ENABLE_MALICIOUS
1539 * @brief This function is called, when the client tells us to act malicious.
1540 * It verifies that @a msg is well-formed.
1542 * @param cls the closure (#ClientContext)
1543 * @param msg the message
1544 * @return #GNUNET_OK if @a msg is well-formed
1547 check_client_act_malicious (void *cls,
1548 const struct GNUNET_RPS_CS_ActMaliciousMessage *msg)
1550 struct ClientContext *cli_ctx = cls;
1551 uint16_t msize = ntohs (msg->header.size);
1552 uint32_t num_peers = ntohl (msg->num_peers);
1554 msize -= sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage);
1555 if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) ||
1556 (msize % sizeof (struct GNUNET_PeerIdentity) != 0) )
1558 LOG (GNUNET_ERROR_TYPE_ERROR,
1559 "message says it sends %" PRIu32 " peers, have space for %lu peers\n",
1560 ntohl (msg->num_peers),
1561 (msize / sizeof (struct GNUNET_PeerIdentity)));
1563 GNUNET_SERVICE_client_drop (cli_ctx->client);
1564 return GNUNET_SYSERR;
1570 * Turn RPS service to act malicious.
1572 * @param cls Closure
1573 * @param client The client that sent the message
1574 * @param msg The message header
1577 handle_client_act_malicious (void *cls,
1578 const struct GNUNET_RPS_CS_ActMaliciousMessage *msg)
1580 struct ClientContext *cli_ctx = cls;
1581 struct GNUNET_PeerIdentity *peers;
1582 uint32_t num_mal_peers_sent;
1583 uint32_t num_mal_peers_old;
1585 /* Do actual logic */
1586 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1587 mal_type = ntohl (msg->type);
1588 if (NULL == mal_peer_set)
1589 mal_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
1591 LOG (GNUNET_ERROR_TYPE_DEBUG,
1592 "Now acting malicious type %" PRIu32 ", got %" PRIu32 " peers.\n",
1594 ntohl (msg->num_peers));
1597 { /* Try to maximise representation */
1598 /* Add other malicious peers to those we already know */
1600 num_mal_peers_sent = ntohl (msg->num_peers);
1601 num_mal_peers_old = num_mal_peers;
1602 GNUNET_array_grow (mal_peers,
1604 num_mal_peers + num_mal_peers_sent);
1605 GNUNET_memcpy (&mal_peers[num_mal_peers_old],
1607 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1609 /* Add all mal peers to mal_peer_set */
1610 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1614 /* Substitute do_round () with do_mal_round () */
1615 GNUNET_SCHEDULER_cancel (do_round_task);
1616 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1619 else if ( (2 == mal_type) ||
1621 { /* Try to partition the network */
1622 /* Add other malicious peers to those we already know */
1624 num_mal_peers_sent = ntohl (msg->num_peers) - 1;
1625 num_mal_peers_old = num_mal_peers;
1626 GNUNET_array_grow (mal_peers,
1628 num_mal_peers + num_mal_peers_sent);
1629 if (NULL != mal_peers &&
1632 GNUNET_memcpy (&mal_peers[num_mal_peers_old],
1634 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1636 /* Add all mal peers to mal_peer_set */
1637 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1642 /* Store the one attacked peer */
1643 GNUNET_memcpy (&attacked_peer,
1644 &msg->attacked_peer,
1645 sizeof (struct GNUNET_PeerIdentity));
1646 /* Set the flag of the attacked peer to valid to avoid problems */
1647 if (GNUNET_NO == Peers_check_peer_known (&attacked_peer))
1649 (void) Peers_issue_peer_liveliness_check (&attacked_peer);
1652 LOG (GNUNET_ERROR_TYPE_DEBUG,
1653 "Attacked peer is %s\n",
1654 GNUNET_i2s (&attacked_peer));
1656 /* Substitute do_round () with do_mal_round () */
1657 GNUNET_SCHEDULER_cancel (do_round_task);
1658 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1660 else if (0 == mal_type)
1661 { /* Stop acting malicious */
1662 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
1664 /* Substitute do_mal_round () with do_round () */
1665 GNUNET_SCHEDULER_cancel (do_round_task);
1666 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
1671 GNUNET_SERVICE_client_continue (cli_ctx->client);
1673 GNUNET_SERVICE_client_continue (cli_ctx->client);
1678 * Send out PUSHes and PULLs maliciously.
1680 * This is executed regylary.
1683 do_mal_round (void *cls)
1685 uint32_t num_pushes;
1687 struct GNUNET_TIME_Relative time_next_round;
1688 struct AttackedPeer *tmp_att_peer;
1690 LOG (GNUNET_ERROR_TYPE_DEBUG,
1691 "Going to execute next round maliciously type %" PRIu32 ".\n",
1693 do_round_task = NULL;
1694 GNUNET_assert (mal_type <= 3);
1695 /* Do malicious actions */
1697 { /* Try to maximise representation */
1699 /* The maximum of pushes we're going to send this round */
1700 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit,
1701 num_attacked_peers),
1702 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
1704 LOG (GNUNET_ERROR_TYPE_DEBUG,
1705 "Going to send %" PRIu32 " pushes\n",
1708 /* Send PUSHes to attacked peers */
1709 for (i = 0 ; i < num_pushes ; i++)
1711 if (att_peers_tail == att_peer_index)
1712 att_peer_index = att_peers_head;
1714 att_peer_index = att_peer_index->next;
1716 send_push (&att_peer_index->peer_id);
1719 /* Send PULLs to some peers to learn about additional peers to attack */
1720 tmp_att_peer = att_peer_index;
1721 for (i = 0 ; i < num_pushes * alpha ; i++)
1723 if (att_peers_tail == tmp_att_peer)
1724 tmp_att_peer = att_peers_head;
1726 att_peer_index = tmp_att_peer->next;
1728 send_pull_request (&tmp_att_peer->peer_id);
1733 else if (2 == mal_type)
1735 * Try to partition the network
1736 * Send as many pushes to the attacked peer as possible
1737 * That is one push per round as it will ignore more.
1739 (void) Peers_issue_peer_liveliness_check (&attacked_peer);
1740 if (GNUNET_YES == Peers_check_peer_flag (&attacked_peer, Peers_ONLINE))
1741 send_push (&attacked_peer);
1746 { /* Combined attack */
1748 /* Send PUSH to attacked peers */
1749 if (GNUNET_YES == Peers_check_peer_known (&attacked_peer))
1751 (void) Peers_issue_peer_liveliness_check (&attacked_peer);
1752 if (GNUNET_YES == Peers_check_peer_flag (&attacked_peer, Peers_ONLINE))
1754 LOG (GNUNET_ERROR_TYPE_DEBUG,
1755 "Goding to send push to attacked peer (%s)\n",
1756 GNUNET_i2s (&attacked_peer));
1757 send_push (&attacked_peer);
1760 (void) Peers_issue_peer_liveliness_check (&attacked_peer);
1762 /* The maximum of pushes we're going to send this round */
1763 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit - 1,
1764 num_attacked_peers),
1765 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
1767 LOG (GNUNET_ERROR_TYPE_DEBUG,
1768 "Going to send %" PRIu32 " pushes\n",
1771 for (i = 0; i < num_pushes; i++)
1773 if (att_peers_tail == att_peer_index)
1774 att_peer_index = att_peers_head;
1776 att_peer_index = att_peer_index->next;
1778 send_push (&att_peer_index->peer_id);
1781 /* Send PULLs to some peers to learn about additional peers to attack */
1782 tmp_att_peer = att_peer_index;
1783 for (i = 0; i < num_pushes * alpha; i++)
1785 if (att_peers_tail == tmp_att_peer)
1786 tmp_att_peer = att_peers_head;
1788 att_peer_index = tmp_att_peer->next;
1790 send_pull_request (&tmp_att_peer->peer_id);
1794 /* Schedule next round */
1795 time_next_round = compute_rand_delay (round_interval, 2);
1797 //do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_mal_round,
1799 GNUNET_assert (NULL == do_round_task);
1800 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round,
1801 &do_mal_round, NULL);
1802 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
1804 #endif /* ENABLE_MALICIOUS */
1808 * Send out PUSHes and PULLs, possibly update #view, samplers.
1810 * This is executed regylary.
1813 do_round (void *cls)
1816 const struct GNUNET_PeerIdentity *view_array;
1817 unsigned int *permut;
1818 unsigned int a_peers; /* Number of peers we send pushes to */
1819 unsigned int b_peers; /* Number of peers we send pull requests to */
1820 uint32_t first_border;
1821 uint32_t second_border;
1822 struct GNUNET_PeerIdentity peer;
1823 struct GNUNET_PeerIdentity *update_peer;
1825 LOG (GNUNET_ERROR_TYPE_DEBUG,
1826 "Going to execute next round.\n");
1827 do_round_task = NULL;
1828 LOG (GNUNET_ERROR_TYPE_DEBUG,
1829 "Printing view:\n");
1830 to_file (file_name_view_log,
1831 "___ new round ___");
1832 view_array = View_get_as_array ();
1833 for (i = 0; i < View_size (); i++)
1835 LOG (GNUNET_ERROR_TYPE_DEBUG,
1836 "\t%s\n", GNUNET_i2s (&view_array[i]));
1837 to_file (file_name_view_log,
1839 GNUNET_i2s_full (&view_array[i]));
1843 /* Send pushes and pull requests */
1844 if (0 < View_size ())
1846 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
1850 a_peers = ceil (alpha * View_size ());
1852 LOG (GNUNET_ERROR_TYPE_DEBUG,
1853 "Going to send pushes to %u (ceil (%f * %u)) peers.\n",
1854 a_peers, alpha, View_size ());
1855 for (i = 0; i < a_peers; i++)
1857 peer = view_array[permut[i]];
1858 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer)) // TODO
1859 { // FIXME if this fails schedule/loop this for later
1864 /* Send PULL requests */
1865 b_peers = ceil (beta * View_size ());
1866 first_border = a_peers;
1867 second_border = a_peers + b_peers;
1868 if (second_border > View_size ())
1870 first_border = View_size () - b_peers;
1871 second_border = View_size ();
1873 LOG (GNUNET_ERROR_TYPE_DEBUG,
1874 "Going to send pulls to %u (ceil (%f * %u)) peers.\n",
1875 b_peers, beta, View_size ());
1876 for (i = first_border; i < second_border; i++)
1878 peer = view_array[permut[i]];
1879 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer) &&
1880 GNUNET_NO == Peers_check_peer_flag (&peer, Peers_PULL_REPLY_PENDING)) // TODO
1881 { // FIXME if this fails schedule/loop this for later
1882 send_pull_request (&peer);
1886 GNUNET_free (permut);
1892 /* TODO see how many peers are in push-/pull- list! */
1894 if ((CustomPeerMap_size (push_map) <= alpha * View_size ()) &&
1895 (0 < CustomPeerMap_size (push_map)) &&
1896 (0 < CustomPeerMap_size (pull_map)))
1897 { /* If conditions for update are fulfilled, update */
1898 LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the view.\n");
1900 uint32_t final_size;
1901 uint32_t peers_to_clean_size;
1902 struct GNUNET_PeerIdentity *peers_to_clean;
1904 peers_to_clean = NULL;
1905 peers_to_clean_size = 0;
1906 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, View_size ());
1907 GNUNET_memcpy (peers_to_clean,
1909 View_size () * sizeof (struct GNUNET_PeerIdentity));
1911 /* Seems like recreating is the easiest way of emptying the peermap */
1913 to_file (file_name_view_log,
1916 first_border = GNUNET_MIN (ceil (alpha * sampler_size_est_need),
1917 CustomPeerMap_size (push_map));
1918 second_border = first_border +
1919 GNUNET_MIN (floor (beta * sampler_size_est_need),
1920 CustomPeerMap_size (pull_map));
1921 final_size = second_border +
1922 ceil ((1 - (alpha + beta)) * sampler_size_est_need);
1924 /* Update view with peers received through PUSHes */
1925 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
1926 CustomPeerMap_size (push_map));
1927 for (i = 0; i < first_border; i++)
1929 (void) insert_in_view (CustomPeerMap_get_peer_by_index (push_map,
1931 to_file (file_name_view_log,
1933 GNUNET_i2s_full (&view_array[i]));
1934 // TODO change the peer_flags accordingly
1936 GNUNET_free (permut);
1939 /* Update view with peers received through PULLs */
1940 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
1941 CustomPeerMap_size (pull_map));
1942 for (i = first_border; i < second_border; i++)
1944 (void) insert_in_view (CustomPeerMap_get_peer_by_index (pull_map,
1945 permut[i - first_border]));
1946 to_file (file_name_view_log,
1948 GNUNET_i2s_full (&view_array[i]));
1949 // TODO change the peer_flags accordingly
1951 GNUNET_free (permut);
1954 /* Update view with peers from history */
1955 RPS_sampler_get_n_rand_peers (prot_sampler,
1958 final_size - second_border);
1959 // TODO change the peer_flags accordingly
1961 for (i = 0; i < View_size (); i++)
1962 rem_from_list (&peers_to_clean, &peers_to_clean_size, &view_array[i]);
1964 /* Clean peers that were removed from the view */
1965 for (i = 0; i < peers_to_clean_size; i++)
1967 to_file (file_name_view_log,
1969 GNUNET_i2s_full (&peers_to_clean[i]));
1970 clean_peer (&peers_to_clean[i]);
1971 //peer_destroy_channel_send (sender);
1974 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, 0);
1978 LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the view.\n");
1980 // TODO independent of that also get some peers from CADET_get_peers()?
1982 LOG (GNUNET_ERROR_TYPE_DEBUG,
1983 "Received %u pushes and %u pulls last round (alpha (%.2f) * view_size (%u) = %.2f)\n",
1984 CustomPeerMap_size (push_map),
1985 CustomPeerMap_size (pull_map),
1988 alpha * View_size ());
1990 /* Update samplers */
1991 for (i = 0; i < CustomPeerMap_size (push_map); i++)
1993 update_peer = CustomPeerMap_get_peer_by_index (push_map, i);
1994 LOG (GNUNET_ERROR_TYPE_DEBUG,
1995 "Updating with peer %s from push list\n",
1996 GNUNET_i2s (update_peer));
1997 insert_in_sampler (NULL, update_peer);
1998 clean_peer (update_peer); /* This cleans only if it is not in the view */
1999 //peer_destroy_channel_send (sender);
2002 for (i = 0; i < CustomPeerMap_size (pull_map); i++)
2004 LOG (GNUNET_ERROR_TYPE_DEBUG,
2005 "Updating with peer %s from pull list\n",
2006 GNUNET_i2s (CustomPeerMap_get_peer_by_index (pull_map, i)));
2007 insert_in_sampler (NULL, CustomPeerMap_get_peer_by_index (pull_map, i));
2008 /* This cleans only if it is not in the view */
2009 clean_peer (CustomPeerMap_get_peer_by_index (pull_map, i));
2010 //peer_destroy_channel_send (sender);
2014 /* Empty push/pull lists */
2015 CustomPeerMap_clear (push_map);
2016 CustomPeerMap_clear (pull_map);
2018 struct GNUNET_TIME_Relative time_next_round;
2020 time_next_round = compute_rand_delay (round_interval, 2);
2022 /* Schedule next round */
2023 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round,
2025 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
2030 * This is called from GNUNET_CADET_get_peers().
2032 * It is called on every peer(ID) that cadet somehow has contact with.
2033 * We use those to initialise the sampler.
2036 init_peer_cb (void *cls,
2037 const struct GNUNET_PeerIdentity *peer,
2038 int tunnel, // "Do we have a tunnel towards this peer?"
2039 unsigned int n_paths, // "Number of known paths towards this peer"
2040 unsigned int best_path) // "How long is the best path?
2041 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
2045 LOG (GNUNET_ERROR_TYPE_DEBUG,
2046 "Got peer_id %s from cadet\n",
2053 * @brief Iterator function over stored, valid peers.
2055 * We initialise the sampler with those.
2057 * @param cls the closure
2058 * @param peer the peer id
2059 * @return #GNUNET_YES if we should continue to
2061 * #GNUNET_NO if not.
2064 valid_peers_iterator (void *cls,
2065 const struct GNUNET_PeerIdentity *peer)
2069 LOG (GNUNET_ERROR_TYPE_DEBUG,
2070 "Got stored, valid peer %s\n",
2079 * Iterator over peers from peerinfo.
2081 * @param cls closure
2082 * @param peer id of the peer, NULL for last call
2083 * @param hello hello message for the peer (can be NULL)
2084 * @param error message
2087 process_peerinfo_peers (void *cls,
2088 const struct GNUNET_PeerIdentity *peer,
2089 const struct GNUNET_HELLO_Message *hello,
2090 const char *err_msg)
2094 LOG (GNUNET_ERROR_TYPE_DEBUG,
2095 "Got peer_id %s from peerinfo\n",
2103 * Task run during shutdown.
2108 shutdown_task (void *cls)
2110 struct ClientContext *client_ctx;
2111 struct ReplyCls *reply_cls;
2113 LOG (GNUNET_ERROR_TYPE_DEBUG,
2114 "RPS is going down\n");
2116 /* Clean all clients */
2117 for (client_ctx = cli_ctx_head;
2118 NULL != cli_ctx_head;
2119 client_ctx = cli_ctx_head)
2121 /* Clean pending requests to the sampler */
2122 for (reply_cls = client_ctx->rep_cls_head;
2123 NULL != client_ctx->rep_cls_head;
2124 reply_cls = client_ctx->rep_cls_head)
2126 RPS_sampler_request_cancel (reply_cls->req_handle);
2127 GNUNET_CONTAINER_DLL_remove (client_ctx->rep_cls_head,
2128 client_ctx->rep_cls_tail,
2130 GNUNET_free (reply_cls);
2132 GNUNET_CONTAINER_DLL_remove (cli_ctx_head, cli_ctx_tail, client_ctx);
2133 GNUNET_free (client_ctx);
2135 GNUNET_PEERINFO_notify_cancel (peerinfo_notify_handle);
2136 GNUNET_PEERINFO_disconnect (peerinfo_handle);
2138 if (NULL != do_round_task)
2140 GNUNET_SCHEDULER_cancel (do_round_task);
2141 do_round_task = NULL;
2146 GNUNET_NSE_disconnect (nse);
2147 RPS_sampler_destroy (prot_sampler);
2148 RPS_sampler_destroy (client_sampler);
2149 GNUNET_CADET_close_port (cadet_port);
2150 GNUNET_CADET_disconnect (cadet_handle);
2152 CustomPeerMap_destroy (push_map);
2153 CustomPeerMap_destroy (pull_map);
2154 #ifdef ENABLE_MALICIOUS
2155 struct AttackedPeer *tmp_att_peer;
2156 GNUNET_free (file_name_view_log);
2157 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
2158 if (NULL != mal_peer_set)
2159 GNUNET_CONTAINER_multipeermap_destroy (mal_peer_set);
2160 if (NULL != att_peer_set)
2161 GNUNET_CONTAINER_multipeermap_destroy (att_peer_set);
2162 while (NULL != att_peers_head)
2164 tmp_att_peer = att_peers_head;
2165 GNUNET_CONTAINER_DLL_remove (att_peers_head, att_peers_tail, tmp_att_peer);
2167 #endif /* ENABLE_MALICIOUS */
2172 * Handle client connecting to the service.
2175 * @param client the new client
2176 * @param mq the message queue of @a client
2180 client_connect_cb (void *cls,
2181 struct GNUNET_SERVICE_Client *client,
2182 struct GNUNET_MQ_Handle *mq)
2184 struct ClientContext *cli_ctx;
2186 LOG (GNUNET_ERROR_TYPE_DEBUG,
2187 "Client connected\n");
2189 return client; /* Server was destroyed before a client connected. Shutting down */
2190 cli_ctx = GNUNET_new (struct ClientContext);
2191 cli_ctx->mq = GNUNET_SERVICE_client_get_mq (client);
2192 cli_ctx->client = client;
2193 GNUNET_CONTAINER_DLL_insert (cli_ctx_head,
2200 * Callback called when a client disconnected from the service
2202 * @param cls closure for the service
2203 * @param c the client that disconnected
2204 * @param internal_cls should be equal to @a c
2207 client_disconnect_cb (void *cls,
2208 struct GNUNET_SERVICE_Client *client,
2211 struct ClientContext *cli_ctx = internal_cls;
2213 GNUNET_assert (client == cli_ctx->client);
2215 {/* shutdown task - destroy all clients */
2216 while (NULL != cli_ctx_head)
2217 destroy_cli_ctx (cli_ctx_head);
2220 { /* destroy this client */
2221 LOG (GNUNET_ERROR_TYPE_DEBUG,
2222 "Client disconnected. Destroy its context.\n");
2223 destroy_cli_ctx (cli_ctx);
2229 * Handle random peer sampling clients.
2231 * @param cls closure
2232 * @param c configuration to use
2233 * @param service the initialized service
2237 const struct GNUNET_CONFIGURATION_Handle *c,
2238 struct GNUNET_SERVICE_Handle *service)
2240 struct GNUNET_MQ_MessageHandler cadet_handlers[] = {
2241 GNUNET_MQ_hd_fixed_size (peer_check,
2242 GNUNET_MESSAGE_TYPE_RPS_PP_CHECK_LIVE,
2243 struct GNUNET_MessageHeader,
2245 GNUNET_MQ_hd_fixed_size (peer_push,
2246 GNUNET_MESSAGE_TYPE_RPS_PP_PUSH,
2247 struct GNUNET_MessageHeader,
2249 GNUNET_MQ_hd_fixed_size (peer_pull_request,
2250 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
2251 struct GNUNET_MessageHeader,
2253 GNUNET_MQ_hd_var_size (peer_pull_reply,
2254 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY,
2255 struct GNUNET_RPS_P2P_PullReplyMessage,
2257 GNUNET_MQ_handler_end ()
2262 char* fn_valid_peers;
2263 struct GNUNET_HashCode port;
2265 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
2270 GNUNET_CRYPTO_get_peer_identity (cfg, &own_identity); // TODO check return value
2271 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2272 "STARTING SERVICE (rps) for peer [%s]\n",
2273 GNUNET_i2s (&own_identity));
2274 #ifdef ENABLE_MALICIOUS
2275 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2276 "Malicious execution compiled in.\n");
2277 #endif /* ENABLE_MALICIOUS */
2281 /* Get time interval from the configuration */
2282 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
2286 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2287 "RPS", "ROUNDINTERVAL");
2288 GNUNET_SCHEDULER_shutdown ();
2292 /* Get initial size of sampler/view from the configuration */
2294 GNUNET_CONFIGURATION_get_value_number (cfg, "RPS", "INITSIZE",
2295 (long long unsigned int *) &sampler_size_est_need))
2297 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2299 GNUNET_SCHEDULER_shutdown ();
2302 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %u\n", sampler_size_est_need);
2305 GNUNET_CONFIGURATION_get_value_filename (cfg,
2307 "FILENAME_VALID_PEERS",
2310 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2311 "rps", "FILENAME_VALID_PEERS");
2317 /* file_name_view_log */
2318 if (GNUNET_OK != GNUNET_DISK_directory_create ("/tmp/rps/"))
2320 LOG (GNUNET_ERROR_TYPE_WARNING,
2321 "Failed to create directory /tmp/rps/\n");
2324 size = (14 + strlen (GNUNET_i2s_full (&own_identity)) + 1) * sizeof (char);
2325 file_name_view_log = GNUNET_malloc (size);
2326 out_size = GNUNET_snprintf (file_name_view_log,
2329 GNUNET_i2s_full (&own_identity));
2330 if (size < out_size ||
2333 LOG (GNUNET_ERROR_TYPE_WARNING,
2334 "Failed to write string to buffer (size: %i, out_size: %i)\n",
2340 /* connect to NSE */
2341 nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
2348 /* Initialise cadet */
2349 cadet_handle = GNUNET_CADET_connect (cfg);
2350 GNUNET_assert (NULL != cadet_handle);
2351 GNUNET_CRYPTO_hash (GNUNET_APPLICATION_PORT_RPS,
2352 strlen (GNUNET_APPLICATION_PORT_RPS),
2354 cadet_port = GNUNET_CADET_open_port (cadet_handle,
2356 &Peers_handle_inbound_channel, /* Connect handler */
2358 NULL, /* WindowSize handler */
2359 cleanup_destroyed_channel, /* Disconnect handler */
2363 peerinfo_handle = GNUNET_PEERINFO_connect (cfg);
2364 Peers_initialise (fn_valid_peers, cadet_handle, cleanup_destroyed_channel,
2365 cadet_handlers, &own_identity);
2366 GNUNET_free (fn_valid_peers);
2368 /* Initialise sampler */
2369 struct GNUNET_TIME_Relative half_round_interval;
2370 struct GNUNET_TIME_Relative max_round_interval;
2372 half_round_interval = GNUNET_TIME_relative_divide (round_interval, 2);
2373 max_round_interval = GNUNET_TIME_relative_add (round_interval, half_round_interval);
2375 prot_sampler = RPS_sampler_init (sampler_size_est_need, max_round_interval);
2376 client_sampler = RPS_sampler_mod_init (sampler_size_est_need, max_round_interval);
2378 /* Initialise push and pull maps */
2379 push_map = CustomPeerMap_create (4);
2380 pull_map = CustomPeerMap_create (4);
2383 //LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
2384 //GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, NULL);
2385 // TODO send push/pull to each of those peers?
2386 // TODO read stored valid peers from last run
2387 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting stored valid peers\n");
2388 Peers_get_valid_peers (valid_peers_iterator, NULL);
2390 peerinfo_notify_handle = GNUNET_PEERINFO_notify (cfg,
2392 process_peerinfo_peers,
2395 LOG (GNUNET_ERROR_TYPE_INFO, "Ready to receive requests from clients\n");
2397 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
2398 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
2400 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
2405 * Define "main" method using service macro.
2409 GNUNET_SERVICE_OPTION_NONE,
2412 &client_disconnect_cb,
2414 GNUNET_MQ_hd_fixed_size (client_request,
2415 GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
2416 struct GNUNET_RPS_CS_RequestMessage,
2418 GNUNET_MQ_hd_fixed_size (client_request_cancel,
2419 GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST_CANCEL,
2420 struct GNUNET_RPS_CS_RequestCancelMessage,
2422 GNUNET_MQ_hd_var_size (client_seed,
2423 GNUNET_MESSAGE_TYPE_RPS_CS_SEED,
2424 struct GNUNET_RPS_CS_SeedMessage,
2426 #ifdef ENABLE_MALICIOUS
2427 GNUNET_MQ_hd_var_size (client_act_malicious,
2428 GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS,
2429 struct GNUNET_RPS_CS_ActMaliciousMessage,
2431 #endif /* ENABLE_MALICIOUS */
2432 GNUNET_MQ_handler_end());
2434 /* end of gnunet-service-rps.c */