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 GNUNET_SERVER_Client *client;
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;
126 * DLL with all clients currently connected to us
128 struct ClientContext *cli_ctx_head;
129 struct ClientContext *cli_ctx_tail;
131 /***********************************************************************
132 * /Housekeeping with clients
133 ***********************************************************************/
139 /***********************************************************************
141 ***********************************************************************/
144 * Sampler used for the Brahms protocol itself.
146 static struct RPS_Sampler *prot_sampler;
149 * Sampler used for the clients.
151 static struct RPS_Sampler *client_sampler;
154 * Name to log view to
156 static char *file_name_view_log;
159 * The size of sampler we need to be able to satisfy the client's need
162 static unsigned int sampler_size_client_need;
165 * The size of sampler we need to be able to satisfy the Brahms protocol's
166 * need of random peers.
168 * This is one minimum size the sampler grows to.
170 static unsigned int sampler_size_est_need;
173 * Percentage of total peer number in the view
174 * to send random PUSHes to
179 * Percentage of total peer number in the view
180 * to send random PULLs to
185 * Identifier for the main task that runs periodically.
187 static struct GNUNET_SCHEDULER_Task *do_round_task;
190 * Time inverval the do_round task runs in.
192 static struct GNUNET_TIME_Relative round_interval;
195 * List to store peers received through pushes temporary.
197 static struct CustomPeerMap *push_map;
200 * List to store peers received through pulls temporary.
202 static struct CustomPeerMap *pull_map;
207 static struct GNUNET_NSE_Handle *nse;
212 static struct GNUNET_CADET_Handle *cadet_handle;
215 * Handler to PEERINFO.
217 static struct GNUNET_PEERINFO_Handle *peerinfo_handle;
220 * Handle for cancellation of iteration over peers.
222 static struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify_handle;
227 * Counts how many requets clients already issued.
228 * Only needed in the beginning to check how many of the 64 deltas
231 static unsigned int req_counter;
234 * Time of the last request we received.
236 * Used to compute the expected request rate.
238 static struct GNUNET_TIME_Absolute last_request;
241 * Size of #request_deltas.
243 #define REQUEST_DELTAS_SIZE 64
244 static unsigned int request_deltas_size = REQUEST_DELTAS_SIZE;
247 * Last 64 deltas between requests
249 static struct GNUNET_TIME_Relative request_deltas[REQUEST_DELTAS_SIZE];
252 * The prediction of the rate of requests
254 static struct GNUNET_TIME_Relative request_rate;
257 #ifdef ENABLE_MALICIOUS
259 * Type of malicious peer
261 * 0 Don't act malicious at all - Default
262 * 1 Try to maximise representation
263 * 2 Try to partition the network
266 static uint32_t mal_type;
269 * Other malicious peers
271 static struct GNUNET_PeerIdentity *mal_peers;
274 * Hashmap of malicious peers used as set.
275 * Used to more efficiently check whether we know that peer.
277 static struct GNUNET_CONTAINER_MultiPeerMap *mal_peer_set;
280 * Number of other malicious peers
282 static uint32_t num_mal_peers;
286 * If type is 2 This struct is used to store the attacked peers in a DLL
293 struct AttackedPeer *next;
294 struct AttackedPeer *prev;
299 struct GNUNET_PeerIdentity peer_id;
303 * If type is 2 this is the DLL of attacked peers
305 static struct AttackedPeer *att_peers_head;
306 static struct AttackedPeer *att_peers_tail;
309 * This index is used to point to an attacked peer to
310 * implement the round-robin-ish way to select attacked peers.
312 static struct AttackedPeer *att_peer_index;
315 * Hashmap of attacked peers used as set.
316 * Used to more efficiently check whether we know that peer.
318 static struct GNUNET_CONTAINER_MultiPeerMap *att_peer_set;
321 * Number of attacked peers
323 static uint32_t num_attacked_peers;
326 * If type is 1 this is the attacked peer
328 static struct GNUNET_PeerIdentity attacked_peer;
331 * The limit of PUSHes we can send in one round.
332 * This is an assumption of the Brahms protocol and either implemented
335 * assumend to be the bandwidth limitation.
337 static uint32_t push_limit = 10000;
338 #endif /* ENABLE_MALICIOUS */
341 /***********************************************************************
343 ***********************************************************************/
346 /***********************************************************************
348 ***********************************************************************/
352 * Print peerlist to log.
355 print_peer_list (struct GNUNET_PeerIdentity *list,
360 LOG (GNUNET_ERROR_TYPE_DEBUG,
361 "Printing peer list of length %u at %p:\n",
364 for (i = 0 ; i < len ; i++)
366 LOG (GNUNET_ERROR_TYPE_DEBUG,
368 i, GNUNET_i2s (&list[i]));
374 * Remove peer from list.
377 rem_from_list (struct GNUNET_PeerIdentity **peer_list,
378 unsigned int *list_size,
379 const struct GNUNET_PeerIdentity *peer)
382 struct GNUNET_PeerIdentity *tmp;
386 LOG (GNUNET_ERROR_TYPE_DEBUG,
387 "Removing peer %s from list at %p\n",
391 for ( i = 0 ; i < *list_size ; i++ )
393 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&tmp[i], peer))
395 if (i < *list_size -1)
396 { /* Not at the last entry -- shift peers left */
397 GNUNET_memcpy (&tmp[i], &tmp[i +1],
398 ((*list_size) - i -1) * sizeof (struct GNUNET_PeerIdentity));
400 /* Remove last entry (should be now useless PeerID) */
401 GNUNET_array_grow (tmp, *list_size, (*list_size) -1);
409 * Sum all time relatives of an array.
411 static struct GNUNET_TIME_Relative
412 T_relative_sum (const struct GNUNET_TIME_Relative *rel_array,
415 struct GNUNET_TIME_Relative sum;
418 sum = GNUNET_TIME_UNIT_ZERO;
419 for ( i = 0 ; i < arr_size ; i++ )
421 sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
428 * Compute the average of given time relatives.
430 static struct GNUNET_TIME_Relative
431 T_relative_avg (const struct GNUNET_TIME_Relative *rel_array,
434 return GNUNET_TIME_relative_divide (T_relative_sum (rel_array,
441 * Insert PeerID in #view
443 * Called once we know a peer is live.
446 * @return GNUNET_OK if peer was actually inserted
447 * GNUNET_NO if peer was not inserted
450 insert_in_view_op (void *cls,
451 const struct GNUNET_PeerIdentity *peer);
454 * Insert PeerID in #view
456 * Called once we know a peer is live.
458 * @return GNUNET_OK if peer was actually inserted
459 * GNUNET_NO if peer was not inserted
462 insert_in_view (const struct GNUNET_PeerIdentity *peer)
466 online = Peers_check_peer_flag (peer, Peers_ONLINE);
467 if ( (GNUNET_NO == online) ||
468 (GNUNET_SYSERR == online) ) /* peer is not even known */
470 (void) Peers_issue_peer_liveliness_check (peer);
471 (void) Peers_schedule_operation (peer, insert_in_view_op);
474 /* Open channel towards peer to keep connection open */
475 Peers_indicate_sending_intention (peer);
476 return View_put (peer);
480 * Put random peer from sampler into the view as history update.
483 hist_update (void *cls,
484 struct GNUNET_PeerIdentity *ids,
489 for (i = 0; i < num_peers; i++)
491 (void) insert_in_view (&ids[i]);
492 to_file (file_name_view_log,
494 GNUNET_i2s_full (ids));
500 * Wrapper around #RPS_sampler_resize()
502 * If we do not have enough sampler elements, double current sampler size
503 * If we have more than enough sampler elements, halv current sampler size
506 resize_wrapper (struct RPS_Sampler *sampler, uint32_t new_size)
508 unsigned int sampler_size;
511 // TODO respect the min, max
512 sampler_size = RPS_sampler_get_size (sampler);
513 if (sampler_size > new_size * 4)
515 RPS_sampler_resize (sampler, sampler_size / 2);
517 else if (sampler_size < new_size)
519 RPS_sampler_resize (sampler, sampler_size * 2);
521 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
526 * Wrapper around #RPS_sampler_resize() resizing the client sampler
529 client_resize_wrapper ()
531 uint32_t bigger_size;
535 bigger_size = GNUNET_MAX (sampler_size_est_need, sampler_size_client_need);
537 // TODO respect the min, max
538 resize_wrapper (client_sampler, bigger_size);
539 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size_client is now %" PRIu32 "\n",
545 * Estimate request rate
547 * Called every time we receive a request from the client.
552 struct GNUNET_TIME_Relative max_round_duration;
554 if (request_deltas_size > req_counter)
556 if ( 1 < req_counter)
558 /* Shift last request deltas to the right */
559 GNUNET_memcpy (&request_deltas[1],
561 (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
563 /* Add current delta to beginning */
565 GNUNET_TIME_absolute_get_difference (last_request,
566 GNUNET_TIME_absolute_get ());
567 request_rate = T_relative_avg (request_deltas, req_counter);
568 request_rate = (request_rate.rel_value_us < 1) ?
569 GNUNET_TIME_relative_get_unit_ () : request_rate;
571 /* Compute the duration a round will maximally take */
573 GNUNET_TIME_relative_add (round_interval,
574 GNUNET_TIME_relative_divide (round_interval, 2));
576 /* Set the estimated size the sampler has to have to
577 * satisfy the current client request rate */
578 sampler_size_client_need =
579 max_round_duration.rel_value_us / request_rate.rel_value_us;
581 /* Resize the sampler */
582 client_resize_wrapper ();
584 last_request = GNUNET_TIME_absolute_get ();
589 * Add all peers in @a peer_array to @a peer_map used as set.
591 * @param peer_array array containing the peers
592 * @param num_peers number of peers in @peer_array
593 * @param peer_map the peermap to use as set
596 add_peer_array_to_set (const struct GNUNET_PeerIdentity *peer_array,
597 unsigned int num_peers,
598 struct GNUNET_CONTAINER_MultiPeerMap *peer_map)
601 if (NULL == peer_map)
603 LOG (GNUNET_ERROR_TYPE_WARNING,
604 "Trying to add peers to non-existing peermap.\n");
608 for (i = 0; i < num_peers; i++)
610 GNUNET_CONTAINER_multipeermap_put (peer_map,
613 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
619 * Send a PULL REPLY to @a peer_id
621 * @param peer_id the peer to send the reply to.
622 * @param peer_ids the peers to send to @a peer_id
623 * @param num_peer_ids the number of peers to send to @a peer_id
626 send_pull_reply (const struct GNUNET_PeerIdentity *peer_id,
627 const struct GNUNET_PeerIdentity *peer_ids,
628 unsigned int num_peer_ids)
631 struct GNUNET_MQ_Envelope *ev;
632 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
634 /* Compute actual size */
635 send_size = sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) +
636 num_peer_ids * sizeof (struct GNUNET_PeerIdentity);
638 if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size)
639 /* Compute number of peers to send
640 * If too long, simply truncate */
641 // TODO select random ones via permutation
642 // or even better: do good protocol design
644 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE -
645 sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
646 sizeof (struct GNUNET_PeerIdentity);
648 send_size = num_peer_ids;
650 LOG (GNUNET_ERROR_TYPE_DEBUG,
651 "Going to send PULL REPLY with %u peers to %s\n",
652 send_size, GNUNET_i2s (peer_id));
654 ev = GNUNET_MQ_msg_extra (out_msg,
655 send_size * sizeof (struct GNUNET_PeerIdentity),
656 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
657 out_msg->num_peers = htonl (send_size);
658 GNUNET_memcpy (&out_msg[1], peer_ids,
659 send_size * sizeof (struct GNUNET_PeerIdentity));
661 Peers_send_message (peer_id, ev, "PULL REPLY");
666 * Insert PeerID in #pull_map
668 * Called once we know a peer is live.
671 insert_in_pull_map (void *cls,
672 const struct GNUNET_PeerIdentity *peer)
674 CustomPeerMap_put (pull_map, peer);
679 * Insert PeerID in #view
681 * Called once we know a peer is live.
685 insert_in_view_op (void *cls,
686 const struct GNUNET_PeerIdentity *peer)
688 (void) insert_in_view (peer);
693 * Update sampler with given PeerID.
697 insert_in_sampler (void *cls,
698 const struct GNUNET_PeerIdentity *peer)
700 LOG (GNUNET_ERROR_TYPE_DEBUG,
701 "Updating samplers with peer %s from insert_in_sampler()\n",
703 RPS_sampler_update (prot_sampler, peer);
704 RPS_sampler_update (client_sampler, peer);
705 if (0 < RPS_sampler_count_id (prot_sampler, peer))
707 /* Make sure we 'know' about this peer */
708 (void) Peers_issue_peer_liveliness_check (peer);
709 /* Establish a channel towards that peer to indicate we are going to send
711 //Peers_indicate_sending_intention (peer);
716 * @brief This is called on peers from external sources (cadet, peerinfo, ...)
717 * If the peer is not known, liveliness check is issued and it is
718 * scheduled to be inserted in sampler and view.
720 * "External sources" refer to every source except the gossip.
722 * @param peer peer to insert
725 got_peer (const struct GNUNET_PeerIdentity *peer)
727 /* If we did not know this peer already, insert it into sampler and view */
728 if (GNUNET_YES == Peers_issue_peer_liveliness_check (peer))
730 Peers_schedule_operation (peer, insert_in_sampler);
731 Peers_schedule_operation (peer, insert_in_view_op);
736 * @brief Checks if there is a sending channel and if it is needed
738 * @param peer the peer whose sending channel is checked
739 * @return GNUNET_YES if sending channel exists and is still needed
740 * GNUNET_NO otherwise
743 check_sending_channel_needed (const struct GNUNET_PeerIdentity *peer)
745 /* struct GNUNET_CADET_Channel *channel; */
746 if (GNUNET_NO == Peers_check_peer_known (peer))
750 if (GNUNET_YES == Peers_check_sending_channel_exists (peer))
752 if ( (0 < RPS_sampler_count_id (prot_sampler, peer)) ||
753 (GNUNET_YES == View_contains_peer (peer)) ||
754 (GNUNET_YES == CustomPeerMap_contains_peer (push_map, peer)) ||
755 (GNUNET_YES == CustomPeerMap_contains_peer (pull_map, peer)) ||
756 (GNUNET_YES == Peers_check_peer_flag (peer, Peers_PULL_REPLY_PENDING)))
757 { /* If we want to keep the connection to peer open */
766 * @brief remove peer from our knowledge, the view, push and pull maps and
769 * @param peer the peer to remove
772 remove_peer (const struct GNUNET_PeerIdentity *peer)
774 View_remove_peer (peer);
775 CustomPeerMap_remove_peer (pull_map, peer);
776 CustomPeerMap_remove_peer (push_map, peer);
777 RPS_sampler_reinitialise_by_value (prot_sampler, peer);
778 RPS_sampler_reinitialise_by_value (client_sampler, peer);
779 Peers_remove_peer (peer);
784 * @brief Remove data that is not needed anymore.
786 * If the sending channel is no longer needed it is destroyed.
788 * @param peer the peer whose data is about to be cleaned
791 clean_peer (const struct GNUNET_PeerIdentity *peer)
793 if (GNUNET_NO == check_sending_channel_needed (peer))
795 #ifdef ENABLE_MALICIOUS
796 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
797 Peers_destroy_sending_channel (peer);
798 #else /* ENABLE_MALICIOUS */
799 Peers_destroy_sending_channel (peer);
800 #endif /* ENABLE_MALICIOUS */
803 if ( (GNUNET_NO == Peers_check_peer_send_intention (peer)) &&
804 (GNUNET_NO == View_contains_peer (peer)) &&
805 (GNUNET_NO == CustomPeerMap_contains_peer (push_map, peer)) &&
806 (GNUNET_NO == CustomPeerMap_contains_peer (push_map, peer)) &&
807 (0 == RPS_sampler_count_id (prot_sampler, peer)) &&
808 (0 == RPS_sampler_count_id (client_sampler, peer)) )
809 { /* We can safely remov this peer */
813 Peers_clean_peer (peer);
817 * @brief This is called when a channel is destroyed.
819 * Removes peer completely from our knowledge if the send_channel was destroyed
820 * Otherwise simply delete the recv_channel
822 * @param cls The closure
823 * @param channel The channel being closed
824 * @param channel_ctx The context associated with this channel
827 cleanup_destroyed_channel (void *cls,
828 const struct GNUNET_CADET_Channel *channel,
831 struct GNUNET_PeerIdentity *peer;
833 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
834 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
835 // FIXME wait for cadet to change this function
837 if (GNUNET_NO == Peers_check_peer_known (peer))
838 { /* We don't know a context to that peer */
839 LOG (GNUNET_ERROR_TYPE_WARNING,
840 "channel (%s) without associated context was destroyed\n",
845 if (GNUNET_YES == Peers_check_peer_flag (peer, Peers_TO_DESTROY))
846 { /* We are in the middle of removing that peer from our knowledge. In this
847 case simply make sure that the channels are cleaned. */
848 Peers_cleanup_destroyed_channel (cls, channel, channel_ctx);
849 to_file (file_name_view_log,
850 "-%s\t(cleanup channel, ourself)",
851 GNUNET_i2s_full (peer));
856 Peers_check_channel_role (peer, channel, Peers_CHANNEL_ROLE_SENDING))
857 { /* Channel used for sending was destroyed */
858 /* Possible causes of channel destruction:
859 * - ourselves -> cleaning send channel -> clean context
860 * - other peer -> peer probably went down -> remove
862 if (GNUNET_YES == Peers_check_channel_flag (channel_ctx, Peers_CHANNEL_CLEAN))
863 { /* We are about to clean the sending channel. Clean the respective
865 Peers_cleanup_destroyed_channel (cls, channel, channel_ctx);
869 { /* Other peer destroyed our sending channel that he is supposed to keep
870 * open. It probably went down. Remove it from our knowledge. */
871 Peers_cleanup_destroyed_channel (cls, channel, channel_ctx);
876 else if (GNUNET_YES ==
877 Peers_check_channel_role (peer, channel, Peers_CHANNEL_ROLE_RECEIVING))
878 { /* Channel used for receiving was destroyed */
879 /* Possible causes of channel destruction:
880 * - ourselves -> peer tried to establish channel twice -> clean context
881 * - other peer -> peer doesn't want to send us data -> clean
884 Peers_check_channel_flag (channel_ctx, Peers_CHANNEL_ESTABLISHED_TWICE))
885 { /* Other peer tried to establish a channel to us twice. We do not accept
886 * that. Clean the context. */
887 Peers_cleanup_destroyed_channel (cls, channel, channel_ctx);
891 { /* Other peer doesn't want to send us data anymore. We are free to clean
893 Peers_cleanup_destroyed_channel (cls, channel, channel_ctx);
900 LOG (GNUNET_ERROR_TYPE_WARNING,
901 "Destroyed channel is neither sending nor receiving channel\n");
905 /***********************************************************************
907 ***********************************************************************/
910 destroy_reply_cls (struct ReplyCls *rep_cls)
912 struct ClientContext *cli_ctx;
914 cli_ctx = GNUNET_SERVER_client_get_user_context (rep_cls->client,
915 struct ClientContext);
916 GNUNET_assert (NULL != cli_ctx);
917 GNUNET_CONTAINER_DLL_remove (cli_ctx->rep_cls_head,
918 cli_ctx->rep_cls_tail,
920 GNUNET_free (rep_cls);
925 destroy_cli_ctx (struct ClientContext *cli_ctx)
927 GNUNET_assert (NULL != cli_ctx);
928 if (NULL != cli_ctx->mq)
930 GNUNET_MQ_destroy (cli_ctx->mq);
932 if (NULL != cli_ctx->rep_cls_head)
934 LOG (GNUNET_ERROR_TYPE_WARNING,
935 "Trying to destroy the context of a client that still has pending requests. Going to clean those\n");
936 while (NULL != cli_ctx->rep_cls_head)
937 destroy_reply_cls (cli_ctx->rep_cls_head);
939 GNUNET_CONTAINER_DLL_remove (cli_ctx_head,
942 GNUNET_free (cli_ctx);
947 * Function called by NSE.
949 * Updates sizes of sampler list and view and adapt those lists
953 nse_callback (void *cls,
954 struct GNUNET_TIME_Absolute timestamp,
955 double logestimate, double std_dev)
958 //double scale; // TODO this might go gloabal/config
960 LOG (GNUNET_ERROR_TYPE_DEBUG,
961 "Received a ns estimate - logest: %f, std_dev: %f (old_size: %u)\n",
962 logestimate, std_dev, RPS_sampler_get_size (prot_sampler));
964 estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
965 // GNUNET_NSE_log_estimate_to_n (logestimate);
966 estimate = pow (estimate, 1.0 / 3);
967 // TODO add if std_dev is a number
968 // estimate += (std_dev * scale);
969 if (2 < ceil (estimate))
971 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
972 sampler_size_est_need = estimate;
974 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
976 /* If the NSE has changed adapt the lists accordingly */
977 resize_wrapper (prot_sampler, sampler_size_est_need);
978 client_resize_wrapper ();
983 * Callback called once the requested PeerIDs are ready.
985 * Sends those to the requesting client.
988 client_respond (void *cls,
989 struct GNUNET_PeerIdentity *peer_ids,
993 struct GNUNET_MQ_Envelope *ev;
994 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
995 struct ReplyCls *reply_cls = (struct ReplyCls *) cls;
996 uint32_t size_needed;
997 struct ClientContext *cli_ctx;
999 GNUNET_assert (NULL != reply_cls);
1000 LOG (GNUNET_ERROR_TYPE_DEBUG,
1001 "sampler returned %" PRIu32 " peers:\n",
1003 for (i = 0; i < num_peers; i++)
1005 LOG (GNUNET_ERROR_TYPE_DEBUG,
1006 " %" PRIu32 ": %s\n",
1008 GNUNET_i2s (&peer_ids[i]));
1011 size_needed = sizeof (struct GNUNET_RPS_CS_ReplyMessage) +
1012 num_peers * sizeof (struct GNUNET_PeerIdentity);
1014 GNUNET_assert (GNUNET_SERVER_MAX_MESSAGE_SIZE >= size_needed);
1016 ev = GNUNET_MQ_msg_extra (out_msg,
1017 num_peers * sizeof (struct GNUNET_PeerIdentity),
1018 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
1019 out_msg->num_peers = htonl (num_peers);
1020 out_msg->id = htonl (reply_cls->id);
1022 GNUNET_memcpy (&out_msg[1],
1024 num_peers * sizeof (struct GNUNET_PeerIdentity));
1025 GNUNET_free (peer_ids);
1027 cli_ctx = GNUNET_SERVER_client_get_user_context (reply_cls->client,
1028 struct ClientContext);
1029 GNUNET_assert (NULL != cli_ctx);
1030 destroy_reply_cls (reply_cls);
1031 GNUNET_MQ_send (cli_ctx->mq, ev);
1036 * Handle RPS request from the client.
1038 * @param cls closure
1039 * @param client identification of the client
1040 * @param message the actual message
1043 handle_client_request (void *cls,
1044 struct GNUNET_SERVER_Client *client,
1045 const struct GNUNET_MessageHeader *message)
1047 struct GNUNET_RPS_CS_RequestMessage *msg;
1049 uint32_t size_needed;
1050 struct ReplyCls *reply_cls;
1052 struct ClientContext *cli_ctx;
1054 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
1056 num_peers = ntohl (msg->num_peers);
1057 size_needed = sizeof (struct GNUNET_RPS_CS_RequestMessage) +
1058 num_peers * sizeof (struct GNUNET_PeerIdentity);
1060 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
1062 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1063 "Message received from client has size larger than expected\n");
1064 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1068 for (i = 0 ; i < num_peers ; i++)
1071 LOG (GNUNET_ERROR_TYPE_DEBUG,
1072 "Client requested %" PRIu32 " random peer(s).\n",
1075 reply_cls = GNUNET_new (struct ReplyCls);
1076 reply_cls->id = ntohl (msg->id);
1077 reply_cls->client = client;
1078 reply_cls->req_handle = RPS_sampler_get_n_rand_peers (client_sampler,
1083 cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientContext);
1084 GNUNET_assert (NULL != cli_ctx);
1085 GNUNET_CONTAINER_DLL_insert (cli_ctx->rep_cls_head,
1086 cli_ctx->rep_cls_tail,
1088 GNUNET_SERVER_receive_done (client,
1094 * @brief Handle a message that requests the cancellation of a request
1097 * @param client the client that requests the cancellation
1098 * @param message the message containing the id of the request
1101 handle_client_request_cancel (void *cls,
1102 struct GNUNET_SERVER_Client *client,
1103 const struct GNUNET_MessageHeader *message)
1105 struct GNUNET_RPS_CS_RequestCancelMessage *msg =
1106 (struct GNUNET_RPS_CS_RequestCancelMessage *) message;
1107 struct ClientContext *cli_ctx;
1108 struct ReplyCls *rep_cls;
1110 cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientContext);
1111 GNUNET_assert (NULL != cli_ctx);
1112 GNUNET_assert (NULL != cli_ctx->rep_cls_head);
1113 rep_cls = cli_ctx->rep_cls_head;
1114 LOG (GNUNET_ERROR_TYPE_DEBUG,
1115 "Client cancels request with id %" PRIu32 "\n",
1117 while ( (NULL != rep_cls->next) &&
1118 (rep_cls->id != ntohl (msg->id)) )
1119 rep_cls = rep_cls->next;
1120 GNUNET_assert (rep_cls->id == ntohl (msg->id));
1121 RPS_sampler_request_cancel (rep_cls->req_handle);
1122 destroy_reply_cls (rep_cls);
1123 GNUNET_SERVER_receive_done (client,
1129 * Handle seed from the client.
1131 * @param cls closure
1132 * @param client identification of the client
1133 * @param message the actual message
1136 handle_client_seed (void *cls,
1137 struct GNUNET_SERVER_Client *client,
1138 const struct GNUNET_MessageHeader *message)
1140 struct GNUNET_RPS_CS_SeedMessage *in_msg;
1141 struct GNUNET_PeerIdentity *peers;
1145 if (sizeof (struct GNUNET_RPS_CS_SeedMessage) > ntohs (message->size))
1147 GNUNET_SERVER_receive_done (client,
1149 GNUNET_break_op (0);
1153 in_msg = (struct GNUNET_RPS_CS_SeedMessage *) message;
1154 num_peers = ntohl (in_msg->num_peers);
1155 peers = (struct GNUNET_PeerIdentity *) &in_msg[1];
1156 //peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1157 //GNUNET_memcpy (peers, &in_msg[1], num_peers * sizeof (struct GNUNET_PeerIdentity));
1159 if ((ntohs (message->size) - sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
1160 sizeof (struct GNUNET_PeerIdentity) != num_peers)
1162 GNUNET_break_op (0);
1163 GNUNET_SERVER_receive_done (client,
1168 LOG (GNUNET_ERROR_TYPE_DEBUG,
1169 "Client seeded peers:\n");
1170 print_peer_list (peers, num_peers);
1172 for (i = 0; i < num_peers; i++)
1174 LOG (GNUNET_ERROR_TYPE_DEBUG,
1175 "Updating samplers with seed %" PRIu32 ": %s\n",
1177 GNUNET_i2s (&peers[i]));
1179 got_peer (&peers[i]);
1181 //RPS_sampler_update (prot_sampler, &peers[i]);
1182 //RPS_sampler_update (client_sampler, &peers[i]);
1185 ////GNUNET_free (peers);
1187 GNUNET_SERVER_receive_done (client,
1192 * Handle a CHECK_LIVE message from another peer.
1194 * This does nothing. But without calling #GNUNET_CADET_receive_done()
1195 * the channel is blocked for all other communication.
1197 * @param cls Closure
1198 * @param channel The channel the CHECK was received over
1199 * @param channel_ctx The context associated with this channel
1200 * @param msg The message header
1203 handle_peer_check (void *cls,
1204 struct GNUNET_CADET_Channel *channel,
1206 const struct GNUNET_MessageHeader *msg)
1208 GNUNET_CADET_receive_done (channel);
1213 * Handle a PUSH message from another peer.
1215 * Check the proof of work and store the PeerID
1216 * in the temporary list for pushed PeerIDs.
1218 * @param cls Closure
1219 * @param channel The channel the PUSH was received over
1220 * @param channel_ctx The context associated with this channel
1221 * @param msg The message header
1224 handle_peer_push (void *cls,
1225 struct GNUNET_CADET_Channel *channel,
1227 const struct GNUNET_MessageHeader *msg)
1229 const struct GNUNET_PeerIdentity *peer;
1231 // (check the proof of work (?))
1233 peer = (const struct GNUNET_PeerIdentity *)
1234 GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
1235 // FIXME wait for cadet to change this function
1237 LOG (GNUNET_ERROR_TYPE_DEBUG,
1238 "Received PUSH (%s)\n",
1241 #ifdef ENABLE_MALICIOUS
1242 struct AttackedPeer *tmp_att_peer;
1244 if ( (1 == mal_type) ||
1246 { /* Try to maximise representation */
1247 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1248 GNUNET_memcpy (&tmp_att_peer->peer_id, peer, sizeof (struct GNUNET_PeerIdentity));
1249 if (NULL == att_peer_set)
1250 att_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
1251 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1254 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1257 add_peer_array_to_set (peer, 1, att_peer_set);
1259 GNUNET_CADET_receive_done (channel);
1264 else if (2 == mal_type)
1265 { /* We attack one single well-known peer - simply ignore */
1266 GNUNET_CADET_receive_done (channel);
1269 #endif /* ENABLE_MALICIOUS */
1271 /* Add the sending peer to the push_map */
1272 CustomPeerMap_put (push_map, peer);
1274 GNUNET_CADET_receive_done (channel);
1280 * Handle PULL REQUEST request message from another peer.
1282 * Reply with the view of PeerIDs.
1284 * @param cls Closure
1285 * @param channel The channel the PULL REQUEST was received over
1286 * @param channel_ctx The context associated with this channel
1287 * @param msg The message header
1290 handle_peer_pull_request (void *cls,
1291 struct GNUNET_CADET_Channel *channel,
1293 const struct GNUNET_MessageHeader *msg)
1295 struct GNUNET_PeerIdentity *peer;
1296 const struct GNUNET_PeerIdentity *view_array;
1298 peer = (struct GNUNET_PeerIdentity *)
1299 GNUNET_CADET_channel_get_info (channel,
1300 GNUNET_CADET_OPTION_PEER);
1301 // FIXME wait for cadet to change this function
1303 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REQUEST (%s)\n", GNUNET_i2s (peer));
1305 #ifdef ENABLE_MALICIOUS
1308 { /* Try to maximise representation */
1309 send_pull_reply (peer, mal_peers, num_mal_peers);
1310 GNUNET_CADET_receive_done (channel);
1314 else if (2 == mal_type)
1315 { /* Try to partition network */
1316 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
1318 send_pull_reply (peer, mal_peers, num_mal_peers);
1320 GNUNET_CADET_receive_done (channel);
1323 #endif /* ENABLE_MALICIOUS */
1325 view_array = View_get_as_array ();
1327 send_pull_reply (peer, view_array, View_size ());
1329 GNUNET_CADET_receive_done (channel);
1335 * Handle PULL REPLY message from another peer.
1337 * Check whether we sent a corresponding request and
1338 * whether this reply is the first one.
1340 * @param cls Closure
1341 * @param channel The channel the PUSH was received over
1342 * @param channel_ctx The context associated with this channel
1343 * @param msg The message header
1346 handle_peer_pull_reply (void *cls,
1347 struct GNUNET_CADET_Channel *channel,
1349 const struct GNUNET_MessageHeader *msg)
1351 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
1352 struct GNUNET_PeerIdentity *peers;
1353 struct GNUNET_PeerIdentity *sender;
1355 #ifdef ENABLE_MALICIOUS
1356 struct AttackedPeer *tmp_att_peer;
1357 #endif /* ENABLE_MALICIOUS */
1359 /* Check for protocol violation */
1360 if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->size))
1362 GNUNET_break_op (0);
1363 GNUNET_CADET_receive_done (channel);
1364 return GNUNET_SYSERR;
1367 in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
1368 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1369 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1371 LOG (GNUNET_ERROR_TYPE_ERROR,
1372 "message says it sends %" PRIu32 " peers, have space for %lu peers\n",
1373 ntohl (in_msg->num_peers),
1374 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1375 sizeof (struct GNUNET_PeerIdentity));
1376 GNUNET_break_op (0);
1377 GNUNET_CADET_receive_done (channel);
1378 return GNUNET_SYSERR;
1381 // Guess simply casting isn't the nicest way...
1382 // FIXME wait for cadet to change this function
1383 sender = (struct GNUNET_PeerIdentity *)
1384 GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
1386 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REPLY (%s)\n", GNUNET_i2s (sender));
1388 if (GNUNET_YES != Peers_check_peer_flag (sender, Peers_PULL_REPLY_PENDING))
1390 LOG (GNUNET_ERROR_TYPE_WARNING,
1391 "Received a pull reply from a peer we didn't request one from!\n");
1392 GNUNET_CADET_receive_done (channel);
1393 GNUNET_break_op (0);
1398 #ifdef ENABLE_MALICIOUS
1399 // We shouldn't even receive pull replies as we're not sending
1402 GNUNET_CADET_receive_done (channel);
1405 #endif /* ENABLE_MALICIOUS */
1407 /* Do actual logic */
1408 peers = (struct GNUNET_PeerIdentity *) &in_msg[1];
1410 LOG (GNUNET_ERROR_TYPE_DEBUG,
1411 "PULL REPLY received, got following %u peers:\n",
1412 ntohl (in_msg->num_peers));
1414 for (i = 0 ; i < ntohl (in_msg->num_peers) ; i++)
1416 LOG (GNUNET_ERROR_TYPE_DEBUG,
1419 GNUNET_i2s (&peers[i]));
1421 #ifdef ENABLE_MALICIOUS
1422 if ((NULL != att_peer_set) &&
1423 (1 == mal_type || 3 == mal_type))
1424 { /* Add attacked peer to local list */
1425 // TODO check if we sent a request and this was the first reply
1426 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1428 && GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mal_peer_set,
1430 && 0 != GNUNET_CRYPTO_cmp_peer_identity (&peers[i],
1433 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1434 tmp_att_peer->peer_id = peers[i];
1435 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1438 add_peer_array_to_set (&peers[i], 1, att_peer_set);
1442 #endif /* ENABLE_MALICIOUS */
1443 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity,
1446 /* Make sure we 'know' about this peer */
1447 (void) Peers_insert_peer (&peers[i]);
1449 if (GNUNET_YES == Peers_check_peer_valid (&peers[i]))
1451 CustomPeerMap_put (pull_map, &peers[i]);
1455 Peers_schedule_operation (&peers[i], insert_in_pull_map);
1456 (void) Peers_issue_peer_liveliness_check (&peers[i]);
1461 Peers_unset_peer_flag (sender, Peers_PULL_REPLY_PENDING);
1462 clean_peer (sender);
1464 GNUNET_CADET_receive_done (channel);
1470 * Compute a random delay.
1471 * A uniformly distributed value between mean + spread and mean - spread.
1473 * For example for mean 4 min and spread 2 the minimum is (4 min - (1/2 * 4 min))
1474 * It would return a random value between 2 and 6 min.
1476 * @param mean the mean
1477 * @param spread the inverse amount of deviation from the mean
1479 static struct GNUNET_TIME_Relative
1480 compute_rand_delay (struct GNUNET_TIME_Relative mean,
1481 unsigned int spread)
1483 struct GNUNET_TIME_Relative half_interval;
1484 struct GNUNET_TIME_Relative ret;
1485 unsigned int rand_delay;
1486 unsigned int max_rand_delay;
1490 LOG (GNUNET_ERROR_TYPE_WARNING,
1491 "Not accepting spread of 0\n");
1495 GNUNET_assert (0 != mean.rel_value_us);
1497 /* Compute random time value between spread * mean and spread * mean */
1498 half_interval = GNUNET_TIME_relative_divide (mean, spread);
1500 max_rand_delay = GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us / mean.rel_value_us * (2/spread);
1502 * Compute random value between (0 and 1) * round_interval
1503 * via multiplying round_interval with a 'fraction' (0 to value)/value
1505 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_rand_delay);
1506 ret = GNUNET_TIME_relative_multiply (mean, rand_delay);
1507 ret = GNUNET_TIME_relative_divide (ret, max_rand_delay);
1508 ret = GNUNET_TIME_relative_add (ret, half_interval);
1510 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ret.rel_value_us)
1511 LOG (GNUNET_ERROR_TYPE_WARNING,
1512 "Returning FOREVER_REL\n");
1519 * Send single pull request
1521 * @param peer_id the peer to send the pull request to.
1524 send_pull_request (const struct GNUNET_PeerIdentity *peer)
1526 struct GNUNET_MQ_Envelope *ev;
1528 GNUNET_assert (GNUNET_NO == Peers_check_peer_flag (peer,
1529 Peers_PULL_REPLY_PENDING));
1530 Peers_set_peer_flag (peer, Peers_PULL_REPLY_PENDING);
1532 LOG (GNUNET_ERROR_TYPE_DEBUG,
1533 "Going to send PULL REQUEST to peer %s.\n",
1536 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
1537 Peers_send_message (peer, ev, "PULL REQUEST");
1544 * @param peer_id the peer to send the push to.
1547 send_push (const struct GNUNET_PeerIdentity *peer_id)
1549 struct GNUNET_MQ_Envelope *ev;
1551 LOG (GNUNET_ERROR_TYPE_DEBUG,
1552 "Going to send PUSH to peer %s.\n",
1553 GNUNET_i2s (peer_id));
1555 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
1556 Peers_send_message (peer_id, ev, "PUSH");
1561 do_round (void *cls);
1564 do_mal_round (void *cls);
1567 #ifdef ENABLE_MALICIOUS
1569 * Turn RPS service to act malicious.
1571 * @param cls Closure
1572 * @param client The client that sent the message
1573 * @param msg The message header
1576 handle_client_act_malicious (void *cls,
1577 struct GNUNET_SERVER_Client *client,
1578 const struct GNUNET_MessageHeader *msg)
1580 struct GNUNET_RPS_CS_ActMaliciousMessage *in_msg;
1581 struct GNUNET_PeerIdentity *peers;
1582 uint32_t num_mal_peers_sent;
1583 uint32_t num_mal_peers_old;
1585 /* Check for protocol violation */
1586 if (sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage) > ntohs (msg->size))
1588 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1589 GNUNET_break_op (0);
1592 in_msg = (struct GNUNET_RPS_CS_ActMaliciousMessage *) msg;
1593 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
1594 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1596 LOG (GNUNET_ERROR_TYPE_ERROR,
1597 "message says it sends %" PRIu32 " peers, have space for %lu peers\n",
1598 ntohl (in_msg->num_peers),
1599 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
1600 sizeof (struct GNUNET_PeerIdentity));
1601 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1602 GNUNET_break_op (0);
1606 /* Do actual logic */
1607 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1608 mal_type = ntohl (in_msg->type);
1609 if (NULL == mal_peer_set)
1610 mal_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
1612 LOG (GNUNET_ERROR_TYPE_DEBUG,
1613 "Now acting malicious type %" PRIu32 ", got %" PRIu32 " peers.\n",
1615 ntohl (in_msg->num_peers));
1618 { /* Try to maximise representation */
1619 /* Add other malicious peers to those we already know */
1621 num_mal_peers_sent = ntohl (in_msg->num_peers);
1622 num_mal_peers_old = num_mal_peers;
1623 GNUNET_array_grow (mal_peers,
1625 num_mal_peers + num_mal_peers_sent);
1626 GNUNET_memcpy (&mal_peers[num_mal_peers_old],
1628 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1630 /* Add all mal peers to mal_peer_set */
1631 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1635 /* Substitute do_round () with do_mal_round () */
1636 GNUNET_SCHEDULER_cancel (do_round_task);
1637 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1640 else if ( (2 == mal_type) ||
1642 { /* Try to partition the network */
1643 /* Add other malicious peers to those we already know */
1645 num_mal_peers_sent = ntohl (in_msg->num_peers) - 1;
1646 num_mal_peers_old = num_mal_peers;
1647 GNUNET_array_grow (mal_peers,
1649 num_mal_peers + num_mal_peers_sent);
1650 if (NULL != mal_peers &&
1653 GNUNET_memcpy (&mal_peers[num_mal_peers_old],
1655 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1657 /* Add all mal peers to mal_peer_set */
1658 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1663 /* Store the one attacked peer */
1664 GNUNET_memcpy (&attacked_peer,
1665 &in_msg->attacked_peer,
1666 sizeof (struct GNUNET_PeerIdentity));
1667 /* Set the flag of the attacked peer to valid to avoid problems */
1668 if (GNUNET_NO == Peers_check_peer_known (&attacked_peer))
1670 (void) Peers_issue_peer_liveliness_check (&attacked_peer);
1673 LOG (GNUNET_ERROR_TYPE_DEBUG,
1674 "Attacked peer is %s\n",
1675 GNUNET_i2s (&attacked_peer));
1677 /* Substitute do_round () with do_mal_round () */
1678 GNUNET_SCHEDULER_cancel (do_round_task);
1679 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1681 else if (0 == mal_type)
1682 { /* Stop acting malicious */
1683 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
1685 /* Substitute do_mal_round () with do_round () */
1686 GNUNET_SCHEDULER_cancel (do_round_task);
1687 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
1692 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1694 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1699 * Send out PUSHes and PULLs maliciously.
1701 * This is executed regylary.
1704 do_mal_round (void *cls)
1706 uint32_t num_pushes;
1708 struct GNUNET_TIME_Relative time_next_round;
1709 struct AttackedPeer *tmp_att_peer;
1711 LOG (GNUNET_ERROR_TYPE_DEBUG,
1712 "Going to execute next round maliciously type %" PRIu32 ".\n",
1714 do_round_task = NULL;
1715 GNUNET_assert (mal_type <= 3);
1716 /* Do malicious actions */
1718 { /* Try to maximise representation */
1720 /* The maximum of pushes we're going to send this round */
1721 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit,
1722 num_attacked_peers),
1723 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
1725 LOG (GNUNET_ERROR_TYPE_DEBUG,
1726 "Going to send %" PRIu32 " pushes\n",
1729 /* Send PUSHes to attacked peers */
1730 for (i = 0 ; i < num_pushes ; i++)
1732 if (att_peers_tail == att_peer_index)
1733 att_peer_index = att_peers_head;
1735 att_peer_index = att_peer_index->next;
1737 send_push (&att_peer_index->peer_id);
1740 /* Send PULLs to some peers to learn about additional peers to attack */
1741 tmp_att_peer = att_peer_index;
1742 for (i = 0 ; i < num_pushes * alpha ; i++)
1744 if (att_peers_tail == tmp_att_peer)
1745 tmp_att_peer = att_peers_head;
1747 att_peer_index = tmp_att_peer->next;
1749 send_pull_request (&tmp_att_peer->peer_id);
1754 else if (2 == mal_type)
1756 * Try to partition the network
1757 * Send as many pushes to the attacked peer as possible
1758 * That is one push per round as it will ignore more.
1760 (void) Peers_issue_peer_liveliness_check (&attacked_peer);
1761 if (GNUNET_YES == Peers_check_peer_flag (&attacked_peer, Peers_ONLINE))
1762 send_push (&attacked_peer);
1767 { /* Combined attack */
1769 /* Send PUSH to attacked peers */
1770 if (GNUNET_YES == Peers_check_peer_known (&attacked_peer))
1772 (void) Peers_issue_peer_liveliness_check (&attacked_peer);
1773 if (GNUNET_YES == Peers_check_peer_flag (&attacked_peer, Peers_ONLINE))
1775 LOG (GNUNET_ERROR_TYPE_DEBUG,
1776 "Goding to send push to attacked peer (%s)\n",
1777 GNUNET_i2s (&attacked_peer));
1778 send_push (&attacked_peer);
1781 (void) Peers_issue_peer_liveliness_check (&attacked_peer);
1783 /* The maximum of pushes we're going to send this round */
1784 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit - 1,
1785 num_attacked_peers),
1786 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
1788 LOG (GNUNET_ERROR_TYPE_DEBUG,
1789 "Going to send %" PRIu32 " pushes\n",
1792 for (i = 0; i < num_pushes; i++)
1794 if (att_peers_tail == att_peer_index)
1795 att_peer_index = att_peers_head;
1797 att_peer_index = att_peer_index->next;
1799 send_push (&att_peer_index->peer_id);
1802 /* Send PULLs to some peers to learn about additional peers to attack */
1803 tmp_att_peer = att_peer_index;
1804 for (i = 0; i < num_pushes * alpha; i++)
1806 if (att_peers_tail == tmp_att_peer)
1807 tmp_att_peer = att_peers_head;
1809 att_peer_index = tmp_att_peer->next;
1811 send_pull_request (&tmp_att_peer->peer_id);
1815 /* Schedule next round */
1816 time_next_round = compute_rand_delay (round_interval, 2);
1818 //do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_mal_round,
1820 GNUNET_assert (NULL == do_round_task);
1821 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round,
1822 &do_mal_round, NULL);
1823 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
1825 #endif /* ENABLE_MALICIOUS */
1829 * Send out PUSHes and PULLs, possibly update #view, samplers.
1831 * This is executed regylary.
1834 do_round (void *cls)
1837 const struct GNUNET_PeerIdentity *view_array;
1838 unsigned int *permut;
1839 unsigned int a_peers; /* Number of peers we send pushes to */
1840 unsigned int b_peers; /* Number of peers we send pull requests to */
1841 uint32_t first_border;
1842 uint32_t second_border;
1843 struct GNUNET_PeerIdentity peer;
1844 struct GNUNET_PeerIdentity *update_peer;
1846 LOG (GNUNET_ERROR_TYPE_DEBUG,
1847 "Going to execute next round.\n");
1848 do_round_task = NULL;
1849 LOG (GNUNET_ERROR_TYPE_DEBUG,
1850 "Printing view:\n");
1851 to_file (file_name_view_log,
1852 "___ new round ___");
1853 view_array = View_get_as_array ();
1854 for (i = 0; i < View_size (); i++)
1856 LOG (GNUNET_ERROR_TYPE_DEBUG,
1857 "\t%s\n", GNUNET_i2s (&view_array[i]));
1858 to_file (file_name_view_log,
1860 GNUNET_i2s_full (&view_array[i]));
1864 /* Send pushes and pull requests */
1865 if (0 < View_size ())
1867 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
1871 a_peers = ceil (alpha * View_size ());
1873 LOG (GNUNET_ERROR_TYPE_DEBUG,
1874 "Going to send pushes to %u (ceil (%f * %u)) peers.\n",
1875 a_peers, alpha, View_size ());
1876 for (i = 0; i < a_peers; i++)
1878 peer = view_array[permut[i]];
1879 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer)) // TODO
1880 { // FIXME if this fails schedule/loop this for later
1885 /* Send PULL requests */
1886 b_peers = ceil (beta * View_size ());
1887 first_border = a_peers;
1888 second_border = a_peers + b_peers;
1889 if (second_border > View_size ())
1891 first_border = View_size () - b_peers;
1892 second_border = View_size ();
1894 LOG (GNUNET_ERROR_TYPE_DEBUG,
1895 "Going to send pulls to %u (ceil (%f * %u)) peers.\n",
1896 b_peers, beta, View_size ());
1897 for (i = first_border; i < second_border; i++)
1899 peer = view_array[permut[i]];
1900 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer) &&
1901 GNUNET_NO == Peers_check_peer_flag (&peer, Peers_PULL_REPLY_PENDING)) // TODO
1902 { // FIXME if this fails schedule/loop this for later
1903 send_pull_request (&peer);
1907 GNUNET_free (permut);
1913 /* TODO see how many peers are in push-/pull- list! */
1915 if ((CustomPeerMap_size (push_map) <= alpha * View_size ()) &&
1916 (0 < CustomPeerMap_size (push_map)) &&
1917 (0 < CustomPeerMap_size (pull_map)))
1918 { /* If conditions for update are fulfilled, update */
1919 LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the view.\n");
1921 uint32_t final_size;
1922 uint32_t peers_to_clean_size;
1923 struct GNUNET_PeerIdentity *peers_to_clean;
1925 peers_to_clean = NULL;
1926 peers_to_clean_size = 0;
1927 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, View_size ());
1928 GNUNET_memcpy (peers_to_clean,
1930 View_size () * sizeof (struct GNUNET_PeerIdentity));
1932 /* Seems like recreating is the easiest way of emptying the peermap */
1934 to_file (file_name_view_log,
1937 first_border = GNUNET_MIN (ceil (alpha * sampler_size_est_need),
1938 CustomPeerMap_size (push_map));
1939 second_border = first_border +
1940 GNUNET_MIN (floor (beta * sampler_size_est_need),
1941 CustomPeerMap_size (pull_map));
1942 final_size = second_border +
1943 ceil ((1 - (alpha + beta)) * sampler_size_est_need);
1945 /* Update view with peers received through PUSHes */
1946 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
1947 CustomPeerMap_size (push_map));
1948 for (i = 0; i < first_border; i++)
1950 (void) insert_in_view (CustomPeerMap_get_peer_by_index (push_map,
1952 to_file (file_name_view_log,
1954 GNUNET_i2s_full (&view_array[i]));
1955 // TODO change the peer_flags accordingly
1957 GNUNET_free (permut);
1960 /* Update view with peers received through PULLs */
1961 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
1962 CustomPeerMap_size (pull_map));
1963 for (i = first_border; i < second_border; i++)
1965 (void) insert_in_view (CustomPeerMap_get_peer_by_index (pull_map,
1966 permut[i - first_border]));
1967 to_file (file_name_view_log,
1969 GNUNET_i2s_full (&view_array[i]));
1970 // TODO change the peer_flags accordingly
1972 GNUNET_free (permut);
1975 /* Update view with peers from history */
1976 RPS_sampler_get_n_rand_peers (prot_sampler,
1979 final_size - second_border);
1980 // TODO change the peer_flags accordingly
1982 for (i = 0; i < View_size (); i++)
1983 rem_from_list (&peers_to_clean, &peers_to_clean_size, &view_array[i]);
1985 /* Clean peers that were removed from the view */
1986 for (i = 0; i < peers_to_clean_size; i++)
1988 to_file (file_name_view_log,
1990 GNUNET_i2s_full (&peers_to_clean[i]));
1991 Peers_clean_peer (&peers_to_clean[i]);
1992 //peer_destroy_channel_send (sender);
1995 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, 0);
1999 LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the view.\n");
2001 // TODO independent of that also get some peers from CADET_get_peers()?
2003 LOG (GNUNET_ERROR_TYPE_DEBUG,
2004 "Received %u pushes and %u pulls last round (alpha (%.2f) * view_size (%u) = %.2f)\n",
2005 CustomPeerMap_size (push_map),
2006 CustomPeerMap_size (pull_map),
2009 alpha * View_size ());
2011 /* Update samplers */
2012 for (i = 0; i < CustomPeerMap_size (push_map); i++)
2014 update_peer = CustomPeerMap_get_peer_by_index (push_map, i);
2015 LOG (GNUNET_ERROR_TYPE_DEBUG,
2016 "Updating with peer %s from push list\n",
2017 GNUNET_i2s (update_peer));
2018 insert_in_sampler (NULL, update_peer);
2019 Peers_clean_peer (update_peer); /* This cleans only if it is not in the view */
2020 //peer_destroy_channel_send (sender);
2023 for (i = 0; i < CustomPeerMap_size (pull_map); i++)
2025 LOG (GNUNET_ERROR_TYPE_DEBUG,
2026 "Updating with peer %s from pull list\n",
2027 GNUNET_i2s (CustomPeerMap_get_peer_by_index (pull_map, i)));
2028 insert_in_sampler (NULL, CustomPeerMap_get_peer_by_index (pull_map, i));
2029 /* This cleans only if it is not in the view */
2030 Peers_clean_peer (CustomPeerMap_get_peer_by_index (pull_map, i));
2031 //peer_destroy_channel_send (sender);
2035 /* Empty push/pull lists */
2036 CustomPeerMap_clear (push_map);
2037 CustomPeerMap_clear (pull_map);
2039 struct GNUNET_TIME_Relative time_next_round;
2041 time_next_round = compute_rand_delay (round_interval, 2);
2043 /* Schedule next round */
2044 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round,
2046 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
2051 rps_start (struct GNUNET_SERVER_Handle *server);
2055 * This is called from GNUNET_CADET_get_peers().
2057 * It is called on every peer(ID) that cadet somehow has contact with.
2058 * We use those to initialise the sampler.
2061 init_peer_cb (void *cls,
2062 const struct GNUNET_PeerIdentity *peer,
2063 int tunnel, // "Do we have a tunnel towards this peer?"
2064 unsigned int n_paths, // "Number of known paths towards this peer"
2065 unsigned int best_path) // "How long is the best path?
2066 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
2070 LOG (GNUNET_ERROR_TYPE_DEBUG,
2071 "Got peer_id %s from cadet\n",
2078 * @brief Iterator function over stored, valid peers.
2080 * We initialise the sampler with those.
2082 * @param cls the closure
2083 * @param peer the peer id
2084 * @return #GNUNET_YES if we should continue to
2086 * #GNUNET_NO if not.
2089 valid_peers_iterator (void *cls,
2090 const struct GNUNET_PeerIdentity *peer)
2094 LOG (GNUNET_ERROR_TYPE_DEBUG,
2095 "Got stored, valid peer %s\n",
2104 * Iterator over peers from peerinfo.
2106 * @param cls closure
2107 * @param peer id of the peer, NULL for last call
2108 * @param hello hello message for the peer (can be NULL)
2109 * @param error message
2112 process_peerinfo_peers (void *cls,
2113 const struct GNUNET_PeerIdentity *peer,
2114 const struct GNUNET_HELLO_Message *hello,
2115 const char *err_msg)
2119 LOG (GNUNET_ERROR_TYPE_DEBUG,
2120 "Got peer_id %s from peerinfo\n",
2128 * Task run during shutdown.
2133 shutdown_task (void *cls)
2135 LOG (GNUNET_ERROR_TYPE_DEBUG,
2136 "RPS is going down\n");
2137 GNUNET_PEERINFO_notify_cancel (peerinfo_notify_handle);
2138 GNUNET_PEERINFO_disconnect (peerinfo_handle);
2140 if (NULL != do_round_task)
2142 GNUNET_SCHEDULER_cancel (do_round_task);
2143 do_round_task = NULL;
2148 GNUNET_NSE_disconnect (nse);
2149 RPS_sampler_destroy (prot_sampler);
2150 RPS_sampler_destroy (client_sampler);
2151 GNUNET_CADET_disconnect (cadet_handle);
2153 CustomPeerMap_destroy (push_map);
2154 CustomPeerMap_destroy (pull_map);
2155 #ifdef ENABLE_MALICIOUS
2156 struct AttackedPeer *tmp_att_peer;
2157 GNUNET_free (file_name_view_log);
2158 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
2159 if (NULL != mal_peer_set)
2160 GNUNET_CONTAINER_multipeermap_destroy (mal_peer_set);
2161 if (NULL != att_peer_set)
2162 GNUNET_CONTAINER_multipeermap_destroy (att_peer_set);
2163 while (NULL != att_peers_head)
2165 tmp_att_peer = att_peers_head;
2166 GNUNET_CONTAINER_DLL_remove (att_peers_head, att_peers_tail, tmp_att_peer);
2168 #endif /* ENABLE_MALICIOUS */
2173 * @brief Get informed about a connecting client.
2176 * @param client the client that connects
2179 handle_client_connect (void *cls,
2180 struct GNUNET_SERVER_Client *client)
2182 struct ClientContext *cli_ctx;
2184 LOG (GNUNET_ERROR_TYPE_DEBUG,
2185 "Client connected\n");
2187 return; /* Server was destroyed before a client connected. Shutting down */
2188 cli_ctx = GNUNET_new (struct ClientContext);
2189 cli_ctx->mq = GNUNET_MQ_queue_for_server_client (client);
2190 GNUNET_SERVER_client_set_user_context (client, cli_ctx);
2191 GNUNET_CONTAINER_DLL_insert (cli_ctx_head,
2197 * A client disconnected. Remove all of its data structure entries.
2199 * @param cls closure, NULL
2200 * @param client identification of the client
2203 handle_client_disconnect (void *cls,
2204 struct GNUNET_SERVER_Client *client)
2206 struct ClientContext *cli_ctx;
2209 {/* shutdown task */
2210 while (NULL != cli_ctx_head)
2211 destroy_cli_ctx (cli_ctx_head);
2215 cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientContext);
2216 destroy_cli_ctx (cli_ctx);
2222 * Actually start the service.
2225 rps_start (struct GNUNET_SERVER_Handle *server)
2227 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
2228 {&handle_client_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
2229 sizeof (struct GNUNET_RPS_CS_RequestMessage)},
2230 {&handle_client_request_cancel, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST_CANCEL,
2231 sizeof (struct GNUNET_RPS_CS_RequestCancelMessage)},
2232 {&handle_client_seed, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_SEED, 0},
2233 #ifdef ENABLE_MALICIOUS
2234 {&handle_client_act_malicious, NULL, GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS , 0},
2235 #endif /* ENABLE_MALICIOUS */
2239 GNUNET_SERVER_add_handlers (server, handlers);
2240 GNUNET_SERVER_connect_notify (server,
2241 &handle_client_connect,
2243 GNUNET_SERVER_disconnect_notify (server,
2244 &handle_client_disconnect,
2246 LOG (GNUNET_ERROR_TYPE_INFO, "Ready to receive requests from clients\n");
2249 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
2250 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
2252 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
2258 * Process statistics requests.
2260 * @param cls closure
2261 * @param server the initialized server
2262 * @param c configuration to use
2266 struct GNUNET_SERVER_Handle *server,
2267 const struct GNUNET_CONFIGURATION_Handle *c)
2269 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
2270 {&handle_peer_check , GNUNET_MESSAGE_TYPE_RPS_PP_CHECK_LIVE,
2271 sizeof (struct GNUNET_MessageHeader)},
2272 {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH,
2273 sizeof (struct GNUNET_MessageHeader)},
2274 {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
2275 sizeof (struct GNUNET_MessageHeader)},
2276 {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY, 0},
2282 char* fn_valid_peers;
2283 struct GNUNET_HashCode port;
2285 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
2290 GNUNET_CRYPTO_get_peer_identity (cfg, &own_identity); // TODO check return value
2291 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2292 "STARTING SERVICE (rps) for peer [%s]\n",
2293 GNUNET_i2s (&own_identity));
2294 #ifdef ENABLE_MALICIOUS
2295 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2296 "Malicious execution compiled in.\n");
2297 #endif /* ENABLE_MALICIOUS */
2301 /* Get time interval from the configuration */
2302 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
2306 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2307 "RPS", "ROUNDINTERVAL");
2308 GNUNET_SCHEDULER_shutdown ();
2312 /* Get initial size of sampler/view from the configuration */
2314 GNUNET_CONFIGURATION_get_value_number (cfg, "RPS", "INITSIZE",
2315 (long long unsigned int *) &sampler_size_est_need))
2317 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2319 GNUNET_SCHEDULER_shutdown ();
2322 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %u\n", sampler_size_est_need);
2325 GNUNET_CONFIGURATION_get_value_filename (cfg,
2327 "FILENAME_VALID_PEERS",
2330 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2331 "rps", "FILENAME_VALID_PEERS");
2337 /* file_name_view_log */
2338 if (GNUNET_OK != GNUNET_DISK_directory_create ("/tmp/rps/"))
2340 LOG (GNUNET_ERROR_TYPE_WARNING,
2341 "Failed to create directory /tmp/rps/\n");
2344 size = (14 + strlen (GNUNET_i2s_full (&own_identity)) + 1) * sizeof (char);
2345 file_name_view_log = GNUNET_malloc (size);
2346 out_size = GNUNET_snprintf (file_name_view_log,
2349 GNUNET_i2s_full (&own_identity));
2350 if (size < out_size ||
2353 LOG (GNUNET_ERROR_TYPE_WARNING,
2354 "Failed to write string to buffer (size: %i, out_size: %i)\n",
2360 /* connect to NSE */
2361 nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
2368 /* Initialise cadet */
2369 cadet_handle = GNUNET_CADET_connect (cfg,
2371 &cleanup_destroyed_channel,
2373 GNUNET_assert (NULL != cadet_handle);
2374 GNUNET_CRYPTO_hash (GNUNET_APPLICATION_PORT_RPS,
2375 strlen (GNUNET_APPLICATION_PORT_RPS),
2377 GNUNET_CADET_open_port (cadet_handle,
2379 &Peers_handle_inbound_channel, cls);
2382 peerinfo_handle = GNUNET_PEERINFO_connect (cfg);
2383 Peers_initialise (fn_valid_peers, cadet_handle, &own_identity);
2384 GNUNET_free (fn_valid_peers);
2386 /* Initialise sampler */
2387 struct GNUNET_TIME_Relative half_round_interval;
2388 struct GNUNET_TIME_Relative max_round_interval;
2390 half_round_interval = GNUNET_TIME_relative_multiply (round_interval, .5);
2391 max_round_interval = GNUNET_TIME_relative_add (round_interval, half_round_interval);
2393 prot_sampler = RPS_sampler_init (sampler_size_est_need, max_round_interval);
2394 client_sampler = RPS_sampler_mod_init (sampler_size_est_need, max_round_interval);
2396 /* Initialise push and pull maps */
2397 push_map = CustomPeerMap_create (4);
2398 pull_map = CustomPeerMap_create (4);
2401 //LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
2402 //GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, NULL);
2403 // TODO send push/pull to each of those peers?
2404 // TODO read stored valid peers from last run
2405 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting stored valid peers\n");
2406 Peers_get_valid_peers (valid_peers_iterator, NULL);
2408 peerinfo_notify_handle = GNUNET_PEERINFO_notify (cfg,
2410 process_peerinfo_peers,
2418 * The main function for the rps service.
2420 * @param argc number of arguments from the command line
2421 * @param argv command line arguments
2422 * @return 0 ok, 1 on error
2425 main (int argc, char *const *argv)
2427 return (GNUNET_OK ==
2428 GNUNET_SERVICE_run (argc,
2431 GNUNET_SERVICE_OPTION_NONE,
2432 &run, NULL)) ? 0 : 1;
2435 /* end of gnunet-service-rps.c */