2 This file is part of GNUnet.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 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_util_lib.h"
28 #include "gnunet_cadet_service.h"
29 #include "gnunet_peerinfo_service.h"
30 #include "gnunet_nse_service.h"
32 #include "rps-test_util.h"
34 #include "gnunet-service-rps_sampler.h"
39 #define LOG(kind, ...) GNUNET_log(kind, __VA_ARGS__)
41 // TODO modify @brief in every file
43 // TODO check for overflows
45 // TODO align message structs
47 // TODO connect to friends
49 // TODO store peers somewhere persistent
51 // TODO blacklist? (-> mal peer detection on top of brahms)
53 // TODO API request_cancel
55 // TODO don't pass peermap all the time
57 // hist_size_init, hist_size_max
62 static const struct GNUNET_CONFIGURATION_Handle *cfg;
67 static struct GNUNET_PeerIdentity own_identity;
70 struct GNUNET_PeerIdentity *
71 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list, unsigned int size,
72 const struct GNUNET_PeerIdentity *ignore_list, unsigned int ignore_size);
75 /***********************************************************************
76 * Housekeeping with peers
77 ***********************************************************************/
80 * Struct used to store the context of a connected client.
85 * The message queue to communicate with the client.
87 struct GNUNET_MQ_Handle *mq;
91 * Used to keep track in what lists single peerIDs are.
95 PULL_REPLY_PENDING = 0x01,
96 IN_OTHER_GOSSIP_LIST = 0x02, // unneeded?
97 IN_OWN_SAMPLER_LIST = 0x04, // unneeded?
98 IN_OWN_GOSSIP_LIST = 0x08, // unneeded?
101 * We set this bit when we can be sure the other peer is/was live.
106 * We set this bit when we are going to destroy the channel to this peer.
107 * When cleanup_channel is called, we know that we wanted to destroy it.
108 * Otherwise the channel to the other peer was destroyed.
115 * Functions of this type can be used to be stored at a peer for later execution.
117 typedef void (* PeerOp) (void *cls, const struct GNUNET_PeerIdentity *peer);
120 * Outstanding operation on peer consisting of callback and closure
122 struct PeerOutstandingOp
137 * Struct used to keep track of other peer's status
139 * This is stored in a multipeermap.
144 * Flags indicating status of peer
149 * Message queue open to client
151 struct GNUNET_MQ_Handle *mq;
154 * Channel open to client.
156 struct GNUNET_CADET_Channel *send_channel;
159 * Channel open from client.
161 struct GNUNET_CADET_Channel *recv_channel; // unneeded?
164 * Array of outstanding operations on this peer.
166 struct PeerOutstandingOp *outstanding_ops;
169 * Number of outstanding operations.
171 unsigned int num_outstanding_ops;
172 //size_t num_outstanding_ops;
175 * Handle to the callback given to cadet_ntfy_tmt_rdy()
177 * To be canceled on shutdown.
179 struct GNUNET_CADET_TransmitHandle *is_live_task;
182 * Identity of the peer
184 struct GNUNET_PeerIdentity peer_id;
187 * This is pobably followed by 'statistical' data (when we first saw
188 * him, how did we get his ID, how many pushes (in a timeinterval),
193 /***********************************************************************
194 * /Housekeeping with peers
195 ***********************************************************************/
201 /***********************************************************************
203 ***********************************************************************/
206 * Sampler used for the Brahms protocol itself.
208 static struct RPS_Sampler *prot_sampler;
211 * Sampler used for the clients.
213 static struct RPS_Sampler *client_sampler;
216 * Set of all peers to keep track of them.
218 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
222 * Name to log view to
224 static char *file_name_view_log;
227 * The "local view" containing peers we learned from gossip and history
229 static struct GNUNET_CONTAINER_MultiPeerMap *view;
232 * An array containing the peers of the local view.
234 * This is created every time we send a pull reply if it has changed since the
235 * last pull reply we sent.
237 static struct GNUNET_PeerIdentity *view_array;
241 * The size of sampler we need to be able to satisfy the client's need of
244 static unsigned int sampler_size_client_need;
247 * The size of sampler we need to be able to satisfy the Brahms protocol's
248 * need of random peers.
250 * This is one minimum size the sampler grows to.
252 static unsigned int sampler_size_est_need;
256 * Percentage of total peer number in the view
257 * to send random PUSHes to
262 * Percentage of total peer number in the view
263 * to send random PULLs to
268 * The percentage gamma of history updates.
269 * Simply 1 - alpha - beta
274 * Identifier for the main task that runs periodically.
276 static struct GNUNET_SCHEDULER_Task *do_round_task;
279 * Time inverval the do_round task runs in.
281 static struct GNUNET_TIME_Relative round_interval;
286 * List to store peers received through pushes temporary.
288 * TODO -> multipeermap
290 static struct GNUNET_PeerIdentity *push_list;
293 * Size of the push_list;
295 static unsigned int push_list_size;
296 //size_t push_list_size;
299 * List to store peers received through pulls temporary.
301 * TODO -> multipeermap
303 static struct GNUNET_PeerIdentity *pull_list;
306 * Size of the pull_list;
308 static unsigned int pull_list_size;
309 //size_t pull_list_size;
315 static struct GNUNET_NSE_Handle *nse;
320 static struct GNUNET_CADET_Handle *cadet_handle;
323 * Handler to PEERINFO.
325 static struct GNUNET_PEERINFO_Handle *peerinfo_handle;
328 * Handle for cancellation of iteration over peers.
330 struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify_handle;
336 * Only needed in the beginning to check how many of the 64 deltas
339 static unsigned int req_counter;
342 * Time of the last request we received.
344 * Used to compute the expected request rate.
346 static struct GNUNET_TIME_Absolute last_request;
349 * Size of #request_deltas.
351 #define REQUEST_DELTAS_SIZE 64
352 static unsigned int request_deltas_size = REQUEST_DELTAS_SIZE;
355 * Last 64 deltas between requests
357 static struct GNUNET_TIME_Relative request_deltas[REQUEST_DELTAS_SIZE];
360 * The prediction of the rate of requests
362 static struct GNUNET_TIME_Relative request_rate;
366 * Number of history update tasks.
368 uint32_t num_hist_update_tasks;
372 * Closure used to pass the client and the id to the callback
373 * that replies to a client's request
378 * The identifier of the request
383 * The client handle to send the reply to
385 struct GNUNET_SERVER_Client *client;
389 #ifdef ENABLE_MALICIOUS
391 * Type of malicious peer
393 * 0 Don't act malicious at all - Default
394 * 1 Try to maximise representation
395 * 2 Try to partition the network
398 uint32_t mal_type = 0;
401 * Other malicious peers
403 static struct GNUNET_PeerIdentity *mal_peers = NULL;
406 * Hashmap of malicious peers used as set.
407 * Used to more efficiently check whether we know that peer.
409 static struct GNUNET_CONTAINER_MultiPeerMap *mal_peer_set = NULL;
412 * Number of other malicious peers
414 static uint32_t num_mal_peers = 0;
418 * If type is 2 This struct is used to store the attacked peers in a DLL
425 struct AttackedPeer *next;
426 struct AttackedPeer *prev;
431 struct GNUNET_PeerIdentity peer_id;
435 * If type is 2 this is the DLL of attacked peers
437 static struct AttackedPeer *att_peers_head = NULL;
438 static struct AttackedPeer *att_peers_tail = NULL;
441 * This index is used to point to an attacked peer to
442 * implement the round-robin-ish way to select attacked peers.
444 static struct AttackedPeer *att_peer_index = NULL;
447 * Hashmap of attacked peers used as set.
448 * Used to more efficiently check whether we know that peer.
450 static struct GNUNET_CONTAINER_MultiPeerMap *att_peer_set = NULL;
453 * Number of attacked peers
455 static uint32_t num_attacked_peers = 0;
459 * If type is 1 this is the attacked peer
461 static struct GNUNET_PeerIdentity attacked_peer;
464 * The limit of PUSHes we can send in one round.
465 * This is an assumption of the Brahms protocol and either implemented
468 * assumend to be the bandwidth limitation.
470 static uint32_t push_limit = 10000;
471 #endif /* ENABLE_MALICIOUS */
474 /***********************************************************************
476 ***********************************************************************/
483 /***********************************************************************
485 ***********************************************************************/
488 * Set a peer flag of given peer context.
490 #define set_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags |= mask)
493 * Get peer flag of given peer context.
495 #define get_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags & mask ? GNUNET_YES : GNUNET_NO)
498 * Unset flag of given peer context.
500 #define unset_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags &= (~mask))
503 * Clean the send channel of a peer
506 peer_clean (const struct GNUNET_PeerIdentity *peer);
510 * Check if peer is already in peer array.
513 in_arr (const struct GNUNET_PeerIdentity *array,
514 unsigned int arr_size,
515 const struct GNUNET_PeerIdentity *peer)
517 GNUNET_assert (NULL != peer);
522 GNUNET_assert (NULL != array);
526 for (i = 0; i < arr_size ; i++)
527 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&array[i], peer))
534 * Print peerlist to log.
537 print_peer_list (struct GNUNET_PeerIdentity *list, unsigned int len)
541 LOG (GNUNET_ERROR_TYPE_DEBUG,
542 "Printing peer list of length %u at %p:\n",
545 for (i = 0 ; i < len ; i++)
547 LOG (GNUNET_ERROR_TYPE_DEBUG,
549 i, GNUNET_i2s (&list[i]));
555 * Remove peer from list.
558 rem_from_list (struct GNUNET_PeerIdentity **peer_list,
559 unsigned int *list_size,
560 const struct GNUNET_PeerIdentity *peer)
563 struct GNUNET_PeerIdentity *tmp;
567 LOG (GNUNET_ERROR_TYPE_DEBUG,
568 "Removing peer %s from list at %p\n",
572 for ( i = 0 ; i < *list_size ; i++ )
574 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&tmp[i], peer))
576 if (i < *list_size -1)
577 { /* Not at the last entry -- shift peers left */
578 memcpy (&tmp[i], &tmp[i +1],
579 ((*list_size) - i -1) * sizeof (struct GNUNET_PeerIdentity));
581 /* Remove last entry (should be now useless PeerID) */
582 GNUNET_array_grow (tmp, *list_size, (*list_size) -1);
589 * Get random peer from the given list but don't return one from the @a ignore_list.
591 struct GNUNET_PeerIdentity *
592 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list,
594 const struct GNUNET_PeerIdentity *ignore_list,
595 uint32_t ignore_size)
599 struct GNUNET_PeerIdentity *tmp_peer_list;
600 struct GNUNET_PeerIdentity *peer;
602 GNUNET_assert (NULL != peer_list);
607 tmp_peer_list = NULL;
608 GNUNET_array_grow (tmp_peer_list, tmp_size, list_size);
609 memcpy (tmp_peer_list,
611 list_size * sizeof (struct GNUNET_PeerIdentity));
612 peer = GNUNET_new (struct GNUNET_PeerIdentity);
615 * Choose the r_index of the peer we want to return
616 * at random from the interval of the view
618 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
620 *peer = tmp_peer_list[r_index];
622 while (in_arr (ignore_list, ignore_size, peer))
624 rem_from_list (&tmp_peer_list, &tmp_size, peer);
626 print_peer_list (tmp_peer_list, tmp_size);
635 * Choose the r_index of the peer we want to return
636 * at random from the interval of the view
638 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
640 *peer = tmp_peer_list[r_index];
644 GNUNET_array_grow (tmp_peer_list, tmp_size, 0);
651 * Get the context of a peer. If not existing, create.
654 get_peer_ctx (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
655 const struct GNUNET_PeerIdentity *peer)
657 struct PeerContext *ctx;
659 if ( GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
661 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
665 ctx = GNUNET_new (struct PeerContext);
668 ctx->send_channel = NULL;
669 ctx->recv_channel = NULL;
670 ctx->outstanding_ops = NULL;
671 ctx->num_outstanding_ops = 0;
672 ctx->is_live_task = NULL;
673 ctx->peer_id = *peer;
674 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
675 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
682 * Put random peer from sampler into the view as history update.
685 hist_update (void *cls, struct GNUNET_PeerIdentity *ids, uint32_t num_peers)
689 for (i = 0; i < GNUNET_MIN (
690 sampler_size_est_need - GNUNET_CONTAINER_multipeermap_size (view),
693 if (GNUNET_OK != GNUNET_CONTAINER_multipeermap_put (view,
696 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
698 LOG (GNUNET_ERROR_TYPE_WARNING,
699 "Failed to put peer in peermap. (hist_update)\n");
702 /* Might want to check that we really updated the view */
703 if (NULL != view_array)
705 GNUNET_free (view_array);
709 to_file (file_name_view_log,
711 GNUNET_i2s_full (ids));
714 if (0 < num_hist_update_tasks)
715 num_hist_update_tasks--;
720 * Set the peer flag to living and call the outstanding operations on this peer.
723 peer_is_live (struct PeerContext *peer_ctx)
725 struct GNUNET_PeerIdentity *peer;
727 /* Cancle is_live_task if still scheduled */
728 if (NULL != peer_ctx->is_live_task)
730 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
731 peer_ctx->is_live_task = NULL;
734 peer = &peer_ctx->peer_id;
735 set_peer_flag (peer_ctx, VALID);
737 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer %s is live\n", GNUNET_i2s (peer));
739 if (0 < peer_ctx->num_outstanding_ops)
740 { /* Call outstanding operations */
743 for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
744 peer_ctx->outstanding_ops[i].op (peer_ctx->outstanding_ops[i].op_cls, peer);
745 GNUNET_array_grow (peer_ctx->outstanding_ops, peer_ctx->num_outstanding_ops, 0);
753 * Callback that is called when a channel was effectively established.
754 * This is given to ntfy_tmt_rdy and called when the channel was
755 * successfully established.
758 cadet_ntfy_tmt_rdy_cb (void *cls, size_t size, void *buf)
760 struct PeerContext *peer_ctx = (struct PeerContext *) cls;
762 peer_ctx->is_live_task = NULL;
763 LOG (GNUNET_ERROR_TYPE_DEBUG,
764 "Set ->is_live_task = NULL for peer %s\n",
765 GNUNET_i2s (&peer_ctx->peer_id));
770 peer_is_live (peer_ctx);
774 LOG (GNUNET_ERROR_TYPE_WARNING,
775 "Problems establishing a connection to peer %s in order to check liveliness\n",
776 GNUNET_i2s (&peer_ctx->peer_id));
777 // TODO reschedule? cleanup?
780 //if (NULL != peer_ctx->is_live_task)
782 // LOG (GNUNET_ERROR_TYPE_DEBUG,
783 // "Trying to cancle is_live_task for peer %s\n",
784 // GNUNET_i2s (&peer_ctx->peer_id));
785 // GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
786 // peer_ctx->is_live_task = NULL;
794 * Get the channel of a peer. If not existing, create.
796 struct GNUNET_CADET_Channel *
797 get_channel (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
798 const struct GNUNET_PeerIdentity *peer)
800 struct PeerContext *peer_ctx;
802 peer_ctx = get_peer_ctx (peer_map, peer);
804 if (NULL == peer_ctx->send_channel)
806 LOG (GNUNET_ERROR_TYPE_DEBUG,
807 "Trying to establish channel to peer %s\n",
810 peer_ctx->send_channel =
811 GNUNET_CADET_channel_create (cadet_handle,
814 GNUNET_RPS_CADET_PORT,
815 GNUNET_CADET_OPTION_RELIABLE);
818 return peer_ctx->send_channel;
823 * Get the message queue of a specific peer.
825 * If we already have a message queue open to this client,
826 * simply return it, otherways create one.
828 struct GNUNET_MQ_Handle *
829 get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
830 const struct GNUNET_PeerIdentity *peer_id)
832 struct PeerContext *peer_ctx;
834 peer_ctx = get_peer_ctx (peer_map, peer_id);
836 GNUNET_assert (NULL == peer_ctx->is_live_task);
838 if (NULL == peer_ctx->mq)
840 (void) get_channel (peer_map, peer_id);
841 peer_ctx->mq = GNUNET_CADET_mq_create (peer_ctx->send_channel);
842 //do I have to explicitly put it in the peer_map?
843 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer_id, peer_ctx,
844 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
851 * Issue check whether peer is live
853 * @param peer_ctx the context of the peer
856 check_peer_live (struct PeerContext *peer_ctx)
858 LOG (GNUNET_ERROR_TYPE_DEBUG,
859 "Get informed about peer %s getting live\n",
860 GNUNET_i2s (&peer_ctx->peer_id));
862 if (NULL == peer_ctx->is_live_task &&
863 NULL == peer_ctx->send_channel)
865 (void) get_channel (peer_map, &peer_ctx->peer_id);
866 peer_ctx->is_live_task =
867 GNUNET_CADET_notify_transmit_ready (peer_ctx->send_channel,
869 GNUNET_TIME_UNIT_FOREVER_REL,
870 sizeof (struct GNUNET_MessageHeader),
871 cadet_ntfy_tmt_rdy_cb,
874 else if (NULL != peer_ctx->is_live_task)
875 LOG (GNUNET_ERROR_TYPE_DEBUG,
876 "Already waiting for notification\n");
877 else if (NULL != peer_ctx->send_channel)
878 LOG (GNUNET_ERROR_TYPE_DEBUG,
879 "Already have established channel to peer\n");
884 * Sum all time relatives of an array.
886 struct GNUNET_TIME_Relative
887 T_relative_sum (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
889 struct GNUNET_TIME_Relative sum;
892 sum = GNUNET_TIME_UNIT_ZERO;
893 for ( i = 0 ; i < arr_size ; i++ )
895 sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
902 * Compute the average of given time relatives.
904 struct GNUNET_TIME_Relative
905 T_relative_avg (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
907 return GNUNET_TIME_relative_divide (T_relative_sum (rel_array, arr_size), arr_size);
912 * Insert PeerID in #pull_list
914 * Called once we know a peer is live.
917 insert_in_pull_list (void *cls, const struct GNUNET_PeerIdentity *peer)
919 if (GNUNET_NO == in_arr (pull_list, pull_list_size, peer))
920 GNUNET_array_append (pull_list, pull_list_size, *peer);
924 * Check whether #insert_in_pull_list was already scheduled
927 insert_in_pull_list_scheduled (const struct PeerContext *peer_ctx)
931 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
932 if (insert_in_pull_list == peer_ctx->outstanding_ops[i].op)
939 * Insert PeerID in #view
941 * Called once we know a peer is live.
944 insert_in_view (void *cls, const struct GNUNET_PeerIdentity *peer)
946 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_put (view,
949 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
951 LOG (GNUNET_ERROR_TYPE_WARNING,
952 "Failed to put peer into view. (insert_in_view)\n");
955 /* Might want to check whether we really modified the view */
956 if (NULL != view_array)
958 GNUNET_free (view_array);
962 (void) get_channel (peer_map, peer);
966 * Check whether #insert_in_view was already scheduled
969 insert_in_view_scheduled (const struct PeerContext *peer_ctx)
973 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
974 if (insert_in_view == peer_ctx->outstanding_ops[i].op)
981 * Update sampler with given PeerID.
984 insert_in_sampler (void *cls, const struct GNUNET_PeerIdentity *peer)
986 LOG (GNUNET_ERROR_TYPE_DEBUG,
987 "Updating samplers with peer %s from insert_in_sampler()\n",
989 RPS_sampler_update (prot_sampler, peer);
990 RPS_sampler_update (client_sampler, peer);
995 * Check whether #insert_in_sampler was already scheduled
998 insert_in_sampler_scheduled (const struct PeerContext *peer_ctx)
1002 for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
1003 if (insert_in_sampler== peer_ctx->outstanding_ops[i].op)
1010 * Wrapper around #RPS_sampler_resize()
1012 * If we do not have enough sampler elements, double current sampler size
1013 * If we have more than enough sampler elements, halv current sampler size
1016 resize_wrapper (struct RPS_Sampler *sampler, uint32_t new_size)
1018 unsigned int sampler_size;
1021 // TODO respect the min, max
1022 sampler_size = RPS_sampler_get_size (sampler);
1023 if (sampler_size > new_size * 4)
1025 RPS_sampler_resize (sampler, sampler_size / 2);
1027 else if (sampler_size < new_size)
1029 RPS_sampler_resize (sampler, sampler_size * 2);
1031 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
1036 * Wrapper around #RPS_sampler_resize() resizing the client sampler
1039 client_resize_wrapper ()
1041 uint32_t bigger_size;
1042 unsigned int sampler_size;
1046 sampler_size = RPS_sampler_get_size (client_sampler);
1048 if (sampler_size_est_need > sampler_size_client_need)
1049 bigger_size = sampler_size_est_need;
1051 bigger_size = sampler_size_client_need;
1053 // TODO respect the min, max
1054 resize_wrapper (client_sampler, bigger_size);
1055 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
1060 * Estimate request rate
1062 * Called every time we receive a request from the client.
1067 struct GNUNET_TIME_Relative max_round_duration;
1069 if (request_deltas_size > req_counter)
1071 if ( 1 < req_counter)
1073 /* Shift last request deltas to the right */
1074 memcpy (&request_deltas[1],
1076 (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
1078 /* Add current delta to beginning */
1080 GNUNET_TIME_absolute_get_difference (last_request,
1081 GNUNET_TIME_absolute_get ());
1082 request_rate = T_relative_avg (request_deltas, req_counter);
1084 /* Compute the duration a round will maximally take */
1085 max_round_duration =
1086 GNUNET_TIME_relative_add (round_interval,
1087 GNUNET_TIME_relative_divide (round_interval, 2));
1089 /* Set the estimated size the sampler has to have to
1090 * satisfy the current client request rate */
1091 sampler_size_client_need =
1092 max_round_duration.rel_value_us / request_rate.rel_value_us;
1094 /* Resize the sampler */
1095 client_resize_wrapper ();
1097 last_request = GNUNET_TIME_absolute_get ();
1102 * Add all peers in @a peer_array to @a peer_map used as set.
1104 * @param peer_array array containing the peers
1105 * @param num_peers number of peers in @peer_array
1106 * @param peer_map the peermap to use as set
1109 add_peer_array_to_set (const struct GNUNET_PeerIdentity *peer_array,
1110 unsigned int num_peers,
1111 struct GNUNET_CONTAINER_MultiPeerMap *peer_map)
1114 if (NULL == peer_map)
1116 LOG (GNUNET_ERROR_TYPE_WARNING,
1117 "Trying to add peers to an empty peermap.\n");
1121 for (i = 0; i < num_peers; i++)
1123 GNUNET_CONTAINER_multipeermap_put (peer_map,
1126 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1132 * Send a PULL REPLY to @a peer_id
1134 * @param peer_id the peer to send the reply to.
1135 * @param peer_ids the peers to send to @a peer_id
1136 * @param num_peer_ids the number of peers to send to @a peer_id
1139 send_pull_reply (const struct GNUNET_PeerIdentity *peer_id,
1140 const struct GNUNET_PeerIdentity *peer_ids,
1141 unsigned int num_peer_ids)
1144 struct GNUNET_MQ_Handle *mq;
1145 struct GNUNET_MQ_Envelope *ev;
1146 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
1148 /* Compute actual size */
1149 send_size = sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) +
1150 num_peer_ids * sizeof (struct GNUNET_PeerIdentity);
1152 if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size)
1153 /* Compute number of peers to send
1154 * If too long, simply truncate */
1155 // TODO select random ones via permutation
1156 // or even better: do good protocol design
1158 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE -
1159 sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1160 sizeof (struct GNUNET_PeerIdentity);
1162 send_size = num_peer_ids;
1164 LOG (GNUNET_ERROR_TYPE_DEBUG,
1165 "PULL REQUEST from peer %s received, going to send %u peers\n",
1166 GNUNET_i2s (peer_id), send_size);
1168 mq = get_mq (peer_map, peer_id);
1170 ev = GNUNET_MQ_msg_extra (out_msg,
1171 send_size * sizeof (struct GNUNET_PeerIdentity),
1172 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
1173 out_msg->num_peers = htonl (send_size);
1174 memcpy (&out_msg[1], peer_ids,
1175 send_size * sizeof (struct GNUNET_PeerIdentity));
1177 GNUNET_MQ_send (mq, ev);
1182 * This function is called on new peer_ids from 'external' sources
1183 * (client seed, cadet get_peers(), ...)
1185 * @param peer_id the new peer_id
1188 new_peer_id (const struct GNUNET_PeerIdentity *peer_id)
1190 struct PeerOutstandingOp out_op;
1191 struct PeerContext *peer_ctx;
1193 if (NULL != peer_id &&
1194 0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, peer_id))
1196 LOG (GNUNET_ERROR_TYPE_DEBUG,
1197 "Got peer_id %s (at %p, view size: %u)\n",
1198 GNUNET_i2s (peer_id),
1200 GNUNET_CONTAINER_multipeermap_size (view));
1202 peer_ctx = get_peer_ctx (peer_map, peer_id);
1203 if (GNUNET_YES != get_peer_flag (peer_ctx, VALID))
1205 if (GNUNET_NO == insert_in_sampler_scheduled (peer_ctx))
1207 out_op.op = insert_in_sampler;
1208 out_op.op_cls = NULL;
1209 GNUNET_array_append (peer_ctx->outstanding_ops,
1210 peer_ctx->num_outstanding_ops,
1214 if (GNUNET_NO == insert_in_view_scheduled (peer_ctx))
1216 out_op.op = insert_in_view;
1217 out_op.op_cls = NULL;
1218 GNUNET_array_append (peer_ctx->outstanding_ops,
1219 peer_ctx->num_outstanding_ops,
1223 /* Trigger livelyness test on peer */
1224 check_peer_live (peer_ctx);
1228 // send push/pull to each of those peers?
1233 /***********************************************************************
1235 ***********************************************************************/
1242 * Function called by NSE.
1244 * Updates sizes of sampler list and view and adapt those lists
1248 nse_callback (void *cls, struct GNUNET_TIME_Absolute timestamp,
1249 double logestimate, double std_dev)
1252 //double scale; // TODO this might go gloabal/config
1254 LOG (GNUNET_ERROR_TYPE_DEBUG,
1255 "Received a ns estimate - logest: %f, std_dev: %f (old_size: %u)\n",
1256 logestimate, std_dev, RPS_sampler_get_size (prot_sampler));
1258 estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
1259 // GNUNET_NSE_log_estimate_to_n (logestimate);
1260 estimate = pow (estimate, 1.0 / 3);
1261 // TODO add if std_dev is a number
1262 // estimate += (std_dev * scale);
1263 if (2 < ceil (estimate))
1265 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
1266 sampler_size_est_need = estimate;
1268 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
1270 /* If the NSE has changed adapt the lists accordingly */
1271 resize_wrapper (prot_sampler, sampler_size_est_need);
1272 client_resize_wrapper ();
1277 * Callback called once the requested PeerIDs are ready.
1279 * Sends those to the requesting client.
1281 void client_respond (void *cls,
1282 struct GNUNET_PeerIdentity *peer_ids, uint32_t num_peers)
1284 struct GNUNET_MQ_Envelope *ev;
1285 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
1286 struct ReplyCls *reply_cls = (struct ReplyCls *) cls;
1287 uint32_t size_needed;
1288 struct ClientContext *cli_ctx;
1290 LOG (GNUNET_ERROR_TYPE_DEBUG,
1291 "sampler returned %" PRIu32 " peers\n",
1294 size_needed = sizeof (struct GNUNET_RPS_CS_ReplyMessage) +
1295 num_peers * sizeof (struct GNUNET_PeerIdentity);
1297 GNUNET_assert (GNUNET_SERVER_MAX_MESSAGE_SIZE >= size_needed);
1299 ev = GNUNET_MQ_msg_extra (out_msg,
1300 num_peers * sizeof (struct GNUNET_PeerIdentity),
1301 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
1302 out_msg->num_peers = htonl (num_peers);
1303 out_msg->id = htonl (reply_cls->id);
1305 memcpy (&out_msg[1],
1307 num_peers * sizeof (struct GNUNET_PeerIdentity));
1308 GNUNET_free (peer_ids);
1310 cli_ctx = GNUNET_SERVER_client_get_user_context (reply_cls->client, struct ClientContext);
1311 if (NULL == cli_ctx) {
1312 cli_ctx = GNUNET_new (struct ClientContext);
1313 cli_ctx->mq = GNUNET_MQ_queue_for_server_client (reply_cls->client);
1314 GNUNET_SERVER_client_set_user_context (reply_cls->client, cli_ctx);
1317 GNUNET_free (reply_cls);
1319 GNUNET_MQ_send (cli_ctx->mq, ev);
1324 * Handle RPS request from the client.
1326 * @param cls closure
1327 * @param client identification of the client
1328 * @param message the actual message
1331 handle_client_request (void *cls,
1332 struct GNUNET_SERVER_Client *client,
1333 const struct GNUNET_MessageHeader *message)
1335 struct GNUNET_RPS_CS_RequestMessage *msg;
1337 uint32_t size_needed;
1338 struct ReplyCls *reply_cls;
1341 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
1343 num_peers = ntohl (msg->num_peers);
1344 size_needed = sizeof (struct GNUNET_RPS_CS_RequestMessage) +
1345 num_peers * sizeof (struct GNUNET_PeerIdentity);
1347 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
1349 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1353 for (i = 0 ; i < num_peers ; i++)
1356 LOG (GNUNET_ERROR_TYPE_DEBUG,
1357 "Client requested %" PRIu32 " random peer(s).\n",
1360 reply_cls = GNUNET_new (struct ReplyCls);
1361 reply_cls->id = ntohl (msg->id);
1362 reply_cls->client = client;
1364 RPS_sampler_get_n_rand_peers (client_sampler,
1369 GNUNET_SERVER_receive_done (client,
1375 * Handle seed from the client.
1377 * @param cls closure
1378 * @param client identification of the client
1379 * @param message the actual message
1382 handle_client_seed (void *cls,
1383 struct GNUNET_SERVER_Client *client,
1384 const struct GNUNET_MessageHeader *message)
1386 struct GNUNET_RPS_CS_SeedMessage *in_msg;
1387 struct GNUNET_PeerIdentity *peers;
1391 if (sizeof (struct GNUNET_RPS_CS_SeedMessage) > ntohs (message->size))
1393 GNUNET_break_op (0);
1394 GNUNET_SERVER_receive_done (client,
1398 in_msg = (struct GNUNET_RPS_CS_SeedMessage *) message;
1399 num_peers = ntohl (in_msg->num_peers);
1400 peers = (struct GNUNET_PeerIdentity *) &in_msg[1];
1401 //peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1402 //memcpy (peers, &in_msg[1], num_peers * sizeof (struct GNUNET_PeerIdentity));
1404 if ((ntohs (message->size) - sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
1405 sizeof (struct GNUNET_PeerIdentity) != num_peers)
1407 GNUNET_break_op (0);
1408 GNUNET_SERVER_receive_done (client,
1412 LOG (GNUNET_ERROR_TYPE_DEBUG,
1413 "Client seeded peers:\n");
1414 print_peer_list (peers, num_peers);
1416 for (i = 0 ; i < num_peers ; i++)
1418 LOG (GNUNET_ERROR_TYPE_DEBUG,
1419 "Updating samplers with seed %" PRIu32 ": %s\n",
1421 GNUNET_i2s (&peers[i]));
1423 new_peer_id (&peers[i]);
1425 //RPS_sampler_update (prot_sampler, &peers[i]);
1426 //RPS_sampler_update (client_sampler, &peers[i]);
1429 ////GNUNET_free (peers);
1431 GNUNET_SERVER_receive_done (client,
1437 * Handle a PUSH message from another peer.
1439 * Check the proof of work and store the PeerID
1440 * in the temporary list for pushed PeerIDs.
1442 * @param cls Closure
1443 * @param channel The channel the PUSH was received over
1444 * @param channel_ctx The context associated with this channel
1445 * @param msg The message header
1448 handle_peer_push (void *cls,
1449 struct GNUNET_CADET_Channel *channel,
1451 const struct GNUNET_MessageHeader *msg)
1453 const struct GNUNET_PeerIdentity *peer;
1455 // (check the proof of work)
1457 peer = (const struct GNUNET_PeerIdentity *)
1458 GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
1459 // FIXME wait for cadet to change this function
1461 LOG (GNUNET_ERROR_TYPE_DEBUG, "PUSH received (%s)\n", GNUNET_i2s (peer));
1463 #ifdef ENABLE_MALICIOUS
1464 struct AttackedPeer *tmp_att_peer;
1466 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1467 memcpy (&tmp_att_peer->peer_id, peer, sizeof (struct GNUNET_PeerIdentity));
1470 { /* Try to maximise representation */
1471 if (NULL == att_peer_set)
1472 att_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
1473 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1476 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1479 add_peer_array_to_set (peer, 1, att_peer_set);
1485 else if (2 == mal_type)
1486 { /* We attack one single well-known peer - simply ignore */
1491 GNUNET_free (tmp_att_peer);
1494 #endif /* ENABLE_MALICIOUS */
1496 /* Add the sending peer to the push_list */
1497 if (GNUNET_NO == in_arr (push_list, push_list_size, peer))
1498 GNUNET_array_append (push_list, push_list_size, *peer);
1505 * Iterator over hash map entries.
1506 * Called from #generate_view_array and writes every peer id into #view_array.
1508 * @param cls closure - the pointer to the counter
1509 * @param key current public key
1510 * @param value value in the hash map
1511 * @return #GNUNET_YES if we should continue to
1513 * #GNUNET_NO if not.
1516 dump_id_to_view_array (void *cls,
1517 const struct GNUNET_PeerIdentity *key,
1520 unsigned int *i = (unsigned int *) cls;
1522 view_array[(*i)++] = *key;
1528 * Makes sure the view_array is filled with the peer ids currently in #view.
1529 * Called from within #do_round before sending pushes and pulls and from
1530 * #handle_peer_pull_request when a reply is sent.
1533 generate_view_array (unsigned int view_size)
1538 if (NULL == view_array)
1540 view_array = GNUNET_new_array (view_size,
1541 struct GNUNET_PeerIdentity);
1542 i = GNUNET_new (unsigned int);
1545 ret = GNUNET_CONTAINER_multipeermap_iterate (view,
1546 dump_id_to_view_array,
1548 GNUNET_assert (view_size == ret);
1549 GNUNET_assert (view_size == *i);
1557 * Handle PULL REQUEST request message from another peer.
1559 * Reply with the view of PeerIDs.
1561 * @param cls Closure
1562 * @param channel The channel the PULL REQUEST was received over
1563 * @param channel_ctx The context associated with this channel
1564 * @param msg The message header
1567 handle_peer_pull_request (void *cls,
1568 struct GNUNET_CADET_Channel *channel,
1570 const struct GNUNET_MessageHeader *msg)
1572 struct GNUNET_PeerIdentity *peer;
1573 unsigned int view_size;
1575 peer = (struct GNUNET_PeerIdentity *)
1576 GNUNET_CADET_channel_get_info (channel,
1577 GNUNET_CADET_OPTION_PEER);
1578 // FIXME wait for cadet to change this function
1580 LOG (GNUNET_ERROR_TYPE_DEBUG, "PULL REQUEST received (%s)\n", GNUNET_i2s (peer));
1582 #ifdef ENABLE_MALICIOUS
1585 { /* Try to maximise representation */
1586 send_pull_reply (peer, mal_peers, num_mal_peers);
1590 else if (2 == mal_type)
1591 { /* Try to partition network */
1592 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
1594 send_pull_reply (peer, mal_peers, num_mal_peers);
1598 #endif /* ENABLE_MALICIOUS */
1600 view_size = GNUNET_CONTAINER_multipeermap_size (view);
1601 generate_view_array (view_size);
1603 send_pull_reply (peer, view_array, view_size);
1610 * Handle PULL REPLY message from another peer.
1612 * Check whether we sent a corresponding request and
1613 * whether this reply is the first one.
1615 * @param cls Closure
1616 * @param channel The channel the PUSH was received over
1617 * @param channel_ctx The context associated with this channel
1618 * @param msg The message header
1621 handle_peer_pull_reply (void *cls,
1622 struct GNUNET_CADET_Channel *channel,
1624 const struct GNUNET_MessageHeader *msg)
1626 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
1627 struct GNUNET_PeerIdentity *peers;
1628 struct PeerContext *peer_ctx;
1629 struct GNUNET_PeerIdentity *sender;
1630 struct PeerContext *sender_ctx;
1631 struct PeerOutstandingOp out_op;
1633 #ifdef ENABLE_MALICIOUS
1634 struct AttackedPeer *tmp_att_peer;
1635 #endif /* ENABLE_MALICIOUS */
1637 /* Check for protocol violation */
1638 if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->size))
1640 GNUNET_break_op (0);
1641 return GNUNET_SYSERR;
1644 in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
1645 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1646 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1648 LOG (GNUNET_ERROR_TYPE_ERROR,
1649 "message says it sends %" PRIu64 " peers, have space for %i peers\n",
1650 ntohl (in_msg->num_peers),
1651 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1652 sizeof (struct GNUNET_PeerIdentity));
1653 GNUNET_break_op (0);
1654 return GNUNET_SYSERR;
1657 sender = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
1658 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
1659 // Guess simply casting isn't the nicest way...
1660 // FIXME wait for cadet to change this function
1661 sender_ctx = get_peer_ctx (peer_map, sender);
1663 LOG (GNUNET_ERROR_TYPE_DEBUG, "PULL REPLY received (%s)\n", GNUNET_i2s (sender));
1665 if (GNUNET_YES != get_peer_flag (sender_ctx, PULL_REPLY_PENDING))
1667 LOG (GNUNET_ERROR_TYPE_WARNING,
1668 "Received a pull reply from a peer we didn't request one from!\n");
1669 GNUNET_break_op (0);
1674 #ifdef ENABLE_MALICIOUS
1675 // We shouldn't even receive pull replies as we're not sending
1678 #endif /* ENABLE_MALICIOUS */
1680 /* Do actual logic */
1681 peers = (struct GNUNET_PeerIdentity *) &in_msg[1];
1683 LOG (GNUNET_ERROR_TYPE_DEBUG,
1684 "PULL REPLY received, got following %u peers:\n",
1685 ntohl (in_msg->num_peers));
1687 for (i = 0 ; i < ntohl (in_msg->num_peers) ; i++)
1689 LOG (GNUNET_ERROR_TYPE_DEBUG,
1692 GNUNET_i2s (&peers[i]));
1694 #ifdef ENABLE_MALICIOUS
1697 { /* Add attacked peer to local list */
1698 // TODO check if we sent a request and this was the first reply
1699 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1701 && GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mal_peer_set,
1703 && 0 != GNUNET_CRYPTO_cmp_peer_identity (&peers[i],
1706 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1707 tmp_att_peer->peer_id = peers[i];
1708 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1711 add_peer_array_to_set (&peers[i], 1, att_peer_set);
1715 #endif /* ENABLE_MALICIOUS */
1716 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity,
1719 peer_ctx = get_peer_ctx (peer_map, &peers[i]);
1720 if (GNUNET_YES == get_peer_flag (peer_ctx, VALID))
1722 if (GNUNET_NO == in_arr (pull_list, pull_list_size, &peers[i]))
1723 GNUNET_array_append (pull_list, pull_list_size, peers[i]);
1725 else if (GNUNET_NO == insert_in_pull_list_scheduled (peer_ctx))
1727 out_op.op = insert_in_pull_list;
1728 out_op.op_cls = NULL;
1729 GNUNET_array_append (peer_ctx->outstanding_ops,
1730 peer_ctx->num_outstanding_ops,
1732 check_peer_live (peer_ctx);
1737 unset_peer_flag (sender_ctx, PULL_REPLY_PENDING);
1744 * Compute a random delay.
1745 * A uniformly distributed value between mean + spread and mean - spread.
1747 * For example for mean 4 min and spread 2 the minimum is (4 min - (1/2 * 4 min))
1748 * It would return a random value between 2 and 6 min.
1750 * @param mean the mean
1751 * @param spread the inverse amount of deviation from the mean
1753 static struct GNUNET_TIME_Relative
1754 compute_rand_delay (struct GNUNET_TIME_Relative mean, unsigned int spread)
1756 struct GNUNET_TIME_Relative half_interval;
1757 struct GNUNET_TIME_Relative ret;
1758 unsigned int rand_delay;
1759 unsigned int max_rand_delay;
1763 LOG (GNUNET_ERROR_TYPE_WARNING,
1764 "Not accepting spread of 0\n");
1768 /* Compute random time value between spread * mean and spread * mean */
1769 half_interval = GNUNET_TIME_relative_divide (mean, spread);
1771 max_rand_delay = GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us / mean.rel_value_us * (2/spread);
1773 * Compute random value between (0 and 1) * round_interval
1774 * via multiplying round_interval with a 'fraction' (0 to value)/value
1776 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_rand_delay);
1777 ret = GNUNET_TIME_relative_multiply (mean, rand_delay);
1778 ret = GNUNET_TIME_relative_divide (ret, max_rand_delay);
1779 ret = GNUNET_TIME_relative_add (ret, half_interval);
1781 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ret.rel_value_us)
1782 LOG (GNUNET_ERROR_TYPE_WARNING,
1783 "Returning FOREVER_REL\n");
1790 * Send single pull request
1792 * @param peer_id the peer to send the pull request to.
1795 send_pull_request (struct GNUNET_PeerIdentity *peer_id)
1797 struct GNUNET_MQ_Envelope *ev;
1798 struct GNUNET_MQ_Handle *mq;
1799 struct PeerContext *peer_ctx;
1801 peer_ctx = get_peer_ctx (peer_map, peer_id);
1802 GNUNET_assert (GNUNET_NO == get_peer_flag (peer_ctx, PULL_REPLY_PENDING));
1803 set_peer_flag (peer_ctx, PULL_REPLY_PENDING);
1805 LOG (GNUNET_ERROR_TYPE_DEBUG,
1806 "Sending PULL request to peer %s of view.\n",
1807 GNUNET_i2s (peer_id));
1809 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
1810 mq = get_mq (peer_map, peer_id);
1811 GNUNET_MQ_send (mq, ev);
1818 * @param peer_id the peer to send the push to.
1821 send_push (struct GNUNET_PeerIdentity *peer_id)
1823 struct GNUNET_MQ_Envelope *ev;
1824 struct GNUNET_MQ_Handle *mq;
1826 LOG (GNUNET_ERROR_TYPE_DEBUG,
1827 "Sending PUSH to peer %s of view.\n",
1828 GNUNET_i2s (peer_id));
1830 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
1831 mq = get_mq (peer_map, peer_id);
1832 GNUNET_MQ_send (mq, ev);
1837 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1840 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1843 #ifdef ENABLE_MALICIOUS
1845 * Turn RPS service to act malicious.
1847 * @param cls Closure
1848 * @param client The client that sent the message
1849 * @param msg The message header
1852 handle_client_act_malicious (void *cls,
1853 struct GNUNET_SERVER_Client *client,
1854 const struct GNUNET_MessageHeader *msg)
1856 struct GNUNET_RPS_CS_ActMaliciousMessage *in_msg;
1857 struct GNUNET_PeerIdentity *peers;
1858 uint32_t num_mal_peers_sent;
1859 uint32_t num_mal_peers_old;
1861 /* Check for protocol violation */
1862 if (sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage) > ntohs (msg->size))
1864 GNUNET_break_op (0);
1867 in_msg = (struct GNUNET_RPS_CS_ActMaliciousMessage *) msg;
1868 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
1869 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1871 LOG (GNUNET_ERROR_TYPE_ERROR,
1872 "message says it sends %" PRIu64 " peers, have space for %i peers\n",
1873 ntohl (in_msg->num_peers),
1874 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
1875 sizeof (struct GNUNET_PeerIdentity));
1876 GNUNET_break_op (0);
1880 /* Do actual logic */
1881 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1882 mal_type = ntohl (in_msg->type);
1884 LOG (GNUNET_ERROR_TYPE_DEBUG,
1885 "Now acting malicious type %" PRIu32 "\n",
1889 { /* Try to maximise representation */
1890 /* Add other malicious peers to those we already know */
1892 num_mal_peers_sent = ntohl (in_msg->num_peers);
1893 num_mal_peers_old = num_mal_peers;
1894 GNUNET_array_grow (mal_peers,
1896 num_mal_peers + num_mal_peers_sent);
1897 memcpy (&mal_peers[num_mal_peers_old],
1899 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1901 /* Add all mal peers to mal_peer_set */
1902 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1906 /* Substitute do_round () with do_mal_round () */
1907 GNUNET_SCHEDULER_cancel (do_round_task);
1908 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1911 else if (2 == mal_type
1913 { /* Try to partition the network */
1914 /* Add other malicious peers to those we already know */
1915 num_mal_peers_sent = ntohl (in_msg->num_peers) - 1;
1916 num_mal_peers_old = num_mal_peers;
1917 GNUNET_array_grow (mal_peers,
1919 num_mal_peers + num_mal_peers_sent);
1920 memcpy (&mal_peers[num_mal_peers_old],
1922 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1924 /* Add all mal peers to mal_peer_set */
1925 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1929 /* Store the one attacked peer */
1930 memcpy (&attacked_peer,
1931 &in_msg->attacked_peer,
1932 sizeof (struct GNUNET_PeerIdentity));
1934 LOG (GNUNET_ERROR_TYPE_DEBUG,
1935 "Attacked peer is %s\n",
1936 GNUNET_i2s (&attacked_peer));
1938 /* Substitute do_round () with do_mal_round () */
1939 GNUNET_SCHEDULER_cancel (do_round_task);
1940 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1942 else if (0 == mal_type)
1943 { /* Stop acting malicious */
1944 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
1946 /* Substitute do_mal_round () with do_round () */
1947 GNUNET_SCHEDULER_cancel (do_round_task);
1948 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
1955 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1960 * Send out PUSHes and PULLs maliciously.
1962 * This is executed regylary.
1965 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1967 uint32_t num_pushes;
1969 struct GNUNET_TIME_Relative time_next_round;
1970 struct AttackedPeer *tmp_att_peer;
1971 struct PeerContext *peer_ctx;
1973 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round maliciously type %" PRIu32 ".\n",
1976 /* Do malicious actions */
1978 { /* Try to maximise representation */
1980 /* The maximum of pushes we're going to send this round */
1981 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit,
1982 num_attacked_peers),
1983 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
1985 LOG (GNUNET_ERROR_TYPE_DEBUG,
1986 "Going to send %" PRIu32 " pushes\n",
1989 /* Send PUSHes to attacked peers */
1990 for (i = 0 ; i < num_pushes ; i++)
1992 if (att_peers_tail == att_peer_index)
1993 att_peer_index = att_peers_head;
1995 att_peer_index = att_peer_index->next;
1997 send_push (&att_peer_index->peer_id);
2000 /* Send PULLs to some peers to learn about additional peers to attack */
2001 tmp_att_peer = att_peer_index;
2002 for (i = 0 ; i < num_pushes * alpha ; i++)
2004 if (att_peers_tail == tmp_att_peer)
2005 tmp_att_peer = att_peers_head;
2007 att_peer_index = tmp_att_peer->next;
2009 send_pull_request (&tmp_att_peer->peer_id);
2014 else if (2 == mal_type)
2016 * Try to partition the network
2017 * Send as many pushes to the attacked peer as possible
2018 * That is one push per round as it will ignore more.
2020 send_push (&attacked_peer);
2025 { /* Combined attack */
2027 /* Send PUSH to attacked peers */
2028 peer_ctx = get_peer_ctx (peer_map, &attacked_peer);
2029 if (GNUNET_YES == get_peer_flag (peer_ctx, VALID))
2031 LOG (GNUNET_ERROR_TYPE_DEBUG,
2032 "Goding to send push to attacked peer (%s)\n",
2033 GNUNET_i2s (&attacked_peer));
2034 send_push (&attacked_peer);
2037 check_peer_live (peer_ctx);
2039 /* The maximum of pushes we're going to send this round */
2040 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit - 1,
2041 num_attacked_peers),
2042 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
2044 LOG (GNUNET_ERROR_TYPE_DEBUG,
2045 "Going to send %" PRIu32 " pushes\n",
2048 for (i = 0 ; i < num_pushes ; i++)
2050 if (att_peers_tail == att_peer_index)
2051 att_peer_index = att_peers_head;
2053 att_peer_index = att_peer_index->next;
2055 send_push (&att_peer_index->peer_id);
2058 /* Send PULLs to some peers to learn about additional peers to attack */
2059 tmp_att_peer = att_peer_index;
2060 for (i = 0 ; i < num_pushes * alpha ; i++)
2062 if (att_peers_tail == tmp_att_peer)
2063 tmp_att_peer = att_peers_head;
2065 att_peer_index = tmp_att_peer->next;
2067 send_pull_request (&tmp_att_peer->peer_id);
2071 /* Schedule next round */
2072 time_next_round = compute_rand_delay (round_interval, 2);
2074 //do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_mal_round, NULL);
2075 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_mal_round, NULL);
2076 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
2078 #endif /* ENABLE_MALICIOUS */
2082 * Send out PUSHes and PULLs, possibly update #view, samplers.
2084 * This is executed regylary.
2087 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2089 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round.\n");
2092 unsigned int view_size;
2093 unsigned int *permut;
2094 unsigned int a_peers; /* Number of peers we send pushes to */
2095 unsigned int b_peers; /* Number of peers we send pull requests to */
2096 uint32_t first_border;
2097 uint32_t second_border;
2098 struct GNUNET_PeerIdentity peer;
2099 struct PeerContext *peer_ctx;
2101 LOG (GNUNET_ERROR_TYPE_DEBUG,
2102 "Printing view:\n");
2103 to_file (file_name_view_log,
2104 "___ new round ___");
2105 view_size = GNUNET_CONTAINER_multipeermap_size (view);
2106 generate_view_array (view_size);
2107 for (i = 0 ; i < view_size ; i++)
2109 LOG (GNUNET_ERROR_TYPE_DEBUG,
2110 "\t%s\n", GNUNET_i2s (&view_array[i]));
2111 to_file (file_name_view_log,
2113 GNUNET_i2s_full (&view_array[i]));
2117 /* Send pushes and pull requests */
2120 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2121 (unsigned int) view_size);
2124 a_peers = ceil (alpha * view_size);
2126 LOG (GNUNET_ERROR_TYPE_DEBUG,
2127 "Going to send pushes to %u (ceil (%f * %u)) peers.\n",
2128 a_peers, alpha, view_size);
2129 for (i = 0; i < a_peers; i++)
2131 peer = view_array[permut[i]];
2132 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer)) // TODO
2133 { // FIXME if this fails schedule/loop this for later
2138 /* Send PULL requests */
2139 b_peers = ceil (beta * view_size);
2140 first_border = a_peers;
2141 second_border = a_peers + b_peers;
2142 if (second_border > view_size)
2144 first_border = view_size - b_peers;
2145 second_border = view_size;
2147 LOG (GNUNET_ERROR_TYPE_DEBUG,
2148 "Going to send pulls to %u (ceil (%f * %u)) peers.\n",
2149 b_peers, beta, view_size);
2150 for (i = first_border; i < second_border; i++)
2152 peer = view_array[permut[i]];
2153 peer_ctx = get_peer_ctx (peer_map, &peer);
2154 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer) &&
2155 GNUNET_NO == get_peer_flag (peer_ctx, PULL_REPLY_PENDING)) // TODO
2156 { // FIXME if this fails schedule/loop this for later
2157 send_pull_request (&peer);
2161 GNUNET_free (permut);
2167 /* TODO see how many peers are in push-/pull- list! */
2169 if (push_list_size <= alpha * view_size &&
2170 0 < push_list_size &&
2173 LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the view.\n");
2175 uint32_t final_size;
2176 uint32_t peers_to_clean_size;
2177 struct GNUNET_PeerIdentity *peers_to_clean;
2179 peers_to_clean = NULL;
2180 peers_to_clean_size = 0;
2181 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, view_size);
2182 memcpy (peers_to_clean,
2184 view_size * sizeof (struct GNUNET_PeerIdentity));
2186 /* Seems like recreating is the easiest way of emptying the peermap */
2187 GNUNET_CONTAINER_multipeermap_destroy (view);
2188 view = GNUNET_CONTAINER_multipeermap_create (view_size, GNUNET_NO);
2189 to_file (file_name_view_log,
2192 first_border = GNUNET_MIN (ceil (alpha * sampler_size_est_need),
2194 second_border = first_border +
2195 GNUNET_MIN (floor (beta * sampler_size_est_need),
2197 final_size = second_border +
2198 ceil ((1 - (alpha + beta)) * sampler_size_est_need);
2200 GNUNET_array_grow (view_array, view_size, second_border);
2202 /* Update view with peers received through PUSHes */
2203 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2205 for (i = 0; i < first_border; i++)
2207 view_array[i] = push_list[permut[i]];
2208 GNUNET_CONTAINER_multipeermap_put (view, &push_list[permut[i]], NULL,
2209 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2211 to_file (file_name_view_log,
2213 GNUNET_i2s_full (&view_array[i]));
2214 // TODO change the peer_flags accordingly
2216 GNUNET_free (permut);
2219 /* Update view with peers received through PULLs */
2220 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2222 for (i = first_border; i < second_border; i++)
2224 view_array[i] = pull_list[permut[i]];
2225 GNUNET_CONTAINER_multipeermap_put (view, &pull_list[permut[i]], NULL,
2226 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2228 to_file (file_name_view_log,
2230 GNUNET_i2s_full (&view_array[i]));
2231 // TODO change the peer_flags accordingly
2233 GNUNET_free (permut);
2236 /* Update view with peers from history */
2237 RPS_sampler_get_n_rand_peers (prot_sampler,
2240 final_size - second_border);
2241 num_hist_update_tasks = final_size - second_border;
2242 // TODO change the peer_flags accordingly
2244 for (i = 0; i < view_size; i++)
2245 rem_from_list (&peers_to_clean, &peers_to_clean_size, &view_array[i]);
2247 /* Clean peers that were removed from the view */
2248 for (i = 0; i < peers_to_clean_size; i++)
2250 peer_clean (&peers_to_clean[i]);
2251 to_file (file_name_view_log,
2253 GNUNET_i2s_full (&peers_to_clean[i]));
2256 GNUNET_free (peers_to_clean);
2257 peers_to_clean = NULL;
2261 LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the view.\n");
2263 // TODO independent of that also get some peers from CADET_get_peers()?
2265 LOG (GNUNET_ERROR_TYPE_DEBUG,
2266 "Received %u pushes and %u pulls last round (alpha (%.2f) * view_size (%u) = %.2f)\n",
2273 /* Update samplers */
2274 for (i = 0; i < push_list_size; i++)
2276 LOG (GNUNET_ERROR_TYPE_DEBUG,
2277 "Updating with peer %s from push list\n",
2278 GNUNET_i2s (&push_list[i]));
2279 RPS_sampler_update (prot_sampler, &push_list[i]);
2280 RPS_sampler_update (client_sampler, &push_list[i]);
2281 // TODO set in_flag?
2284 for (i = 0; i < pull_list_size; i++)
2286 LOG (GNUNET_ERROR_TYPE_DEBUG,
2287 "Updating with peer %s from pull list\n",
2288 GNUNET_i2s (&pull_list[i]));
2289 RPS_sampler_update (prot_sampler, &pull_list[i]);
2290 RPS_sampler_update (client_sampler, &pull_list[i]);
2291 // TODO set in_flag?
2295 /* Empty push/pull lists */
2296 GNUNET_array_grow (push_list, push_list_size, 0);
2297 GNUNET_array_grow (pull_list, pull_list_size, 0);
2299 struct GNUNET_TIME_Relative time_next_round;
2301 time_next_round = compute_rand_delay (round_interval, 2);
2303 /* Schedule next round */
2304 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_round, NULL);
2305 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
2310 rps_start (struct GNUNET_SERVER_Handle *server);
2314 * This is called from GNUNET_CADET_get_peers().
2316 * It is called on every peer(ID) that cadet somehow has contact with.
2317 * We use those to initialise the sampler.
2320 init_peer_cb (void *cls,
2321 const struct GNUNET_PeerIdentity *peer,
2322 int tunnel, // "Do we have a tunnel towards this peer?"
2323 unsigned int n_paths, // "Number of known paths towards this peer"
2324 unsigned int best_path) // "How long is the best path?
2325 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
2329 LOG (GNUNET_ERROR_TYPE_DEBUG,
2330 "Got peer_id %s from cadet\n",
2338 * Iterator over peers from peerinfo.
2340 * @param cls closure
2341 * @param peer id of the peer, NULL for last call
2342 * @param hello hello message for the peer (can be NULL)
2343 * @param error message
2346 process_peerinfo_peers (void *cls,
2347 const struct GNUNET_PeerIdentity *peer,
2348 const struct GNUNET_HELLO_Message *hello,
2349 const char *err_msg)
2353 LOG (GNUNET_ERROR_TYPE_DEBUG,
2354 "Got peer_id %s from peerinfo\n",
2362 * Callback used to remove peers from the multipeermap.
2365 peer_remove_cb (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
2367 struct PeerContext *peer_ctx;
2368 const struct GNUNET_CADET_Channel *channel =
2369 (const struct GNUNET_CADET_Channel *) cls;
2371 peer_ctx = (struct PeerContext *) value;
2372 set_peer_flag (peer_ctx, TO_DESTROY);
2374 LOG (GNUNET_ERROR_TYPE_DEBUG,
2375 "Going to clean peer %s\n",
2376 GNUNET_i2s (&peer_ctx->peer_id));
2378 /* If operations are still scheduled for this peer cancel those */
2379 if (0 != peer_ctx->num_outstanding_ops)
2381 GNUNET_array_grow (peer_ctx->outstanding_ops,
2382 peer_ctx->num_outstanding_ops,
2386 /* If we are still waiting for notification whether this peer is live
2387 * cancel the according task */
2388 if (NULL != peer_ctx->is_live_task)
2390 LOG (GNUNET_ERROR_TYPE_DEBUG,
2391 "Trying to cancle is_live_task for peer %s\n",
2393 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
2394 peer_ctx->is_live_task = NULL;
2397 unset_peer_flag (peer_ctx, PULL_REPLY_PENDING);
2399 to_file (file_name_view_log,
2400 "-%s\t(cleanup channel, other peer)",
2401 GNUNET_i2s_full (key));
2402 GNUNET_CONTAINER_multipeermap_remove_all (view, key);
2404 /* If there is still a mq destroy it */
2405 if (NULL != peer_ctx->mq)
2407 GNUNET_MQ_destroy (peer_ctx->mq);
2408 peer_ctx->mq = NULL;
2412 /* Remove the send_channel
2413 * This function should be called again from #cleanup_channel (callback
2414 * called on the destruction of channels) and clean up the rest. */
2415 if (NULL != peer_ctx->send_channel &&
2416 channel != peer_ctx->send_channel)
2418 GNUNET_CADET_channel_destroy (peer_ctx->send_channel);
2419 peer_ctx->send_channel = NULL;
2422 /* Remove the recv_channel
2423 * This function should be called again from #cleanup_channel (callback
2424 * called on the destruction of channels) and clean up the rest. */
2425 if (NULL != peer_ctx->recv_channel &&
2426 channel != peer_ctx->recv_channel)
2428 GNUNET_CADET_channel_destroy (peer_ctx->recv_channel);
2429 peer_ctx->recv_channel = NULL;
2432 /* If there is no channel we have to remove the context now */
2433 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_remove_all (peer_map, key))
2434 LOG (GNUNET_ERROR_TYPE_WARNING, "removing peer from peer_map failed\n");
2436 GNUNET_free (peer_ctx);
2443 * Clean the send channel of a peer
2444 * If there is also no channel to receive messages from that peer, remove it
2448 peer_clean (const struct GNUNET_PeerIdentity *peer)
2450 struct PeerContext *peer_ctx;
2451 /* struct GNUNET_CADET_Channel *channel; */
2453 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_contains (view, peer) &&
2454 GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
2456 peer_ctx = get_peer_ctx (peer_map, peer);
2457 GNUNET_CADET_channel_destroy (peer_ctx->send_channel);
2458 peer_ctx->send_channel = NULL;
2460 if (NULL == peer_ctx->recv_channel)
2462 peer_remove_cb (NULL, peer, peer_ctx);
2469 * Task run during shutdown.
2475 shutdown_task (void *cls,
2476 const struct GNUNET_SCHEDULER_TaskContext *tc)
2479 LOG (GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
2481 GNUNET_PEERINFO_notify_cancel (peerinfo_notify_handle);
2482 GNUNET_PEERINFO_disconnect (peerinfo_handle);
2484 if (NULL != do_round_task)
2486 GNUNET_SCHEDULER_cancel (do_round_task);
2487 do_round_task = NULL;
2491 if (GNUNET_SYSERR ==
2492 GNUNET_CONTAINER_multipeermap_iterate (peer_map, peer_remove_cb, NULL))
2493 LOG (GNUNET_ERROR_TYPE_WARNING,
2494 "Iterating over peers to disconnect from them was cancelled\n");
2497 GNUNET_NSE_disconnect (nse);
2498 RPS_sampler_destroy (prot_sampler);
2499 RPS_sampler_destroy (client_sampler);
2500 LOG (GNUNET_ERROR_TYPE_DEBUG,
2501 "Size of the peermap: %u\n",
2502 GNUNET_CONTAINER_multipeermap_size (peer_map));
2503 GNUNET_break (0 == GNUNET_CONTAINER_multipeermap_size (peer_map));
2504 GNUNET_CADET_disconnect (cadet_handle);
2505 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
2506 GNUNET_CONTAINER_multipeermap_destroy (view);
2508 GNUNET_array_grow (push_list, push_list_size, 0);
2509 GNUNET_array_grow (pull_list, pull_list_size, 0);
2510 #ifdef ENABLE_MALICIOUS
2511 struct AttackedPeer *tmp_att_peer;
2512 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
2513 if (NULL != mal_peer_set)
2514 GNUNET_CONTAINER_multipeermap_destroy (mal_peer_set);
2515 if (NULL != att_peer_set)
2516 GNUNET_CONTAINER_multipeermap_destroy (att_peer_set);
2517 while (NULL != att_peers_head)
2519 tmp_att_peer = att_peers_head;
2520 GNUNET_CONTAINER_DLL_remove (att_peers_head, att_peers_tail, tmp_att_peer);
2522 #endif /* ENABLE_MALICIOUS */
2527 * A client disconnected. Remove all of its data structure entries.
2529 * @param cls closure, NULL
2530 * @param client identification of the client
2533 handle_client_disconnect (void *cls,
2534 struct GNUNET_SERVER_Client * client)
2540 * Handle the channel a peer opens to us.
2542 * @param cls The closure
2543 * @param channel The channel the peer wants to establish
2544 * @param initiator The peer's peer ID
2545 * @param port The port the channel is being established over
2546 * @param options Further options
2549 handle_inbound_channel (void *cls,
2550 struct GNUNET_CADET_Channel *channel,
2551 const struct GNUNET_PeerIdentity *initiator,
2553 enum GNUNET_CADET_ChannelOption options)
2555 struct PeerContext *peer_ctx;
2556 struct GNUNET_PeerIdentity peer;
2559 LOG (GNUNET_ERROR_TYPE_DEBUG,
2560 "New channel was established to us (Peer %s).\n",
2561 GNUNET_i2s (&peer));
2563 GNUNET_assert (NULL != channel);
2565 // we might not even store the recv_channel
2567 peer_ctx = get_peer_ctx (peer_map, &peer);
2568 // FIXME what do we do if a channel is established twice?
2569 // overwrite? Clean old channel? ...?
2570 //if (NULL != peer_ctx->recv_channel)
2572 // peer_ctx->recv_channel = channel;
2574 peer_ctx->recv_channel = channel;
2576 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, &peer, peer_ctx,
2577 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
2579 /* This would make the push-message unnecessary */
2580 LOG (GNUNET_ERROR_TYPE_DEBUG,
2581 "Got peer_id %s from peerinfo\n",
2582 GNUNET_i2s (&peer));
2583 new_peer_id (&peer);
2585 peer_is_live (peer_ctx);
2587 return NULL; // TODO
2592 * This is called when a channel is destroyed.
2594 * @param cls The closure
2595 * @param channel The channel being closed
2596 * @param channel_ctx The context associated with this channel
2599 cleanup_channel (void *cls,
2600 const struct GNUNET_CADET_Channel *channel,
2603 struct GNUNET_PeerIdentity *peer;
2604 struct PeerContext *peer_ctx;
2606 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
2607 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
2608 // Guess simply casting isn't the nicest way...
2609 // FIXME wait for cadet to change this function
2611 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
2612 {/* We don't want to implicitly create a context that we're about to kill */
2613 peer_ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
2614 if (NULL == peer_ctx) /* It could have been removed by shutdown_task */
2617 if (get_peer_flag (peer_ctx, TO_DESTROY))
2618 {/* We initiatad the destruction of this particular peer */
2619 if (channel == peer_ctx->send_channel)
2620 peer_ctx->send_channel = NULL;
2621 else if (channel == peer_ctx->recv_channel)
2622 peer_ctx->recv_channel = NULL;
2624 to_file (file_name_view_log,
2625 "-%s\t(cleanup channel, ourself)",
2626 GNUNET_i2s_full (peer));
2630 { /* We did not initiate the destruction of this peer */
2631 if (channel == peer_ctx->send_channel)
2632 { /* Something (but us) killd the channel - clean up peer */
2633 LOG (GNUNET_ERROR_TYPE_DEBUG,
2634 "send channel (%s) was destroyed - cleaning up\n",
2636 peer_ctx->send_channel = NULL;
2637 /* Somwewhat {ab,re}use the iterator function */
2638 /* Cast to void is ok, because it's used as void in peer_remove_cb */
2639 (void) peer_remove_cb ((void *) channel, peer, peer_ctx);
2641 else if (channel == peer_ctx->recv_channel)
2642 { /* Other peer doesn't want to send us messages anymore */
2643 LOG (GNUNET_ERROR_TYPE_DEBUG,
2644 "Peer %s destroyed recv channel - cleaning up channel\n",
2646 peer_ctx->recv_channel = NULL;
2650 LOG (GNUNET_ERROR_TYPE_WARNING,
2651 "unknown channel (%s) was destroyed\n",
2658 { /* We don't know a context to that peer */
2659 LOG (GNUNET_ERROR_TYPE_DEBUG,
2660 "channel (%s) without associated context was destroyed\n",
2667 * Actually start the service.
2670 rps_start (struct GNUNET_SERVER_Handle *server)
2672 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
2673 {&handle_client_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
2674 sizeof (struct GNUNET_RPS_CS_RequestMessage)},
2675 {&handle_client_seed, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_SEED, 0},
2676 #ifdef ENABLE_MALICIOUS
2677 {&handle_client_act_malicious, NULL, GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS , 0},
2678 #endif /* ENABLE_MALICIOUS */
2682 GNUNET_SERVER_add_handlers (server, handlers);
2683 GNUNET_SERVER_disconnect_notify (server,
2684 &handle_client_disconnect,
2686 LOG (GNUNET_ERROR_TYPE_INFO, "Ready to receive requests from clients\n");
2689 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
2690 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
2692 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2699 * Process statistics requests.
2701 * @param cls closure
2702 * @param server the initialized server
2703 * @param c configuration to use
2707 struct GNUNET_SERVER_Handle *server,
2708 const struct GNUNET_CONFIGURATION_Handle *c)
2713 // TODO check what this does -- copied from gnunet-boss
2714 // - seems to work as expected
2715 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
2720 GNUNET_CRYPTO_get_peer_identity (cfg, &own_identity); // TODO check return value
2721 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2722 "STARTING SERVICE (rps) for peer [%s]\n",
2723 GNUNET_i2s (&own_identity));
2724 #ifdef ENABLE_MALICIOUS
2725 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2726 "Malicious execution compiled in.\n");
2727 #endif /* ENABLE_MALICIOUS */
2731 /* Get time interval from the configuration */
2732 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
2736 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2737 "RPS", "ROUNDINTERVAL");
2738 GNUNET_SCHEDULER_shutdown ();
2742 /* Get initial size of sampler/view from the configuration */
2744 GNUNET_CONFIGURATION_get_value_number (cfg, "RPS", "INITSIZE",
2745 (long long unsigned int *) &sampler_size_est_need))
2747 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2749 GNUNET_SCHEDULER_shutdown ();
2752 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", sampler_size_est_need);
2755 view = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO);
2757 /* file_name_view_log */
2758 if (GNUNET_OK != GNUNET_DISK_directory_create ("/tmp/rps/"))
2760 LOG (GNUNET_ERROR_TYPE_WARNING,
2761 "Failed to create directory /tmp/rps/\n");
2764 size = (14 + strlen (GNUNET_i2s_full (&own_identity)) + 1) * sizeof (char);
2765 file_name_view_log = GNUNET_malloc (size);
2766 out_size = GNUNET_snprintf (file_name_view_log,
2769 GNUNET_i2s_full (&own_identity));
2770 if (size < out_size ||
2773 LOG (GNUNET_ERROR_TYPE_WARNING,
2774 "Failed to write string to buffer (size: %i, out_size: %i)\n",
2780 /* connect to NSE */
2781 nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
2787 peer_map = GNUNET_CONTAINER_multipeermap_create (sampler_size_est_need, GNUNET_NO);
2790 /* Initialise cadet */
2791 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
2792 {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH ,
2793 sizeof (struct GNUNET_MessageHeader)},
2794 {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
2795 sizeof (struct GNUNET_MessageHeader)},
2796 {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY , 0},
2800 const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
2801 cadet_handle = GNUNET_CADET_connect (cfg,
2803 &handle_inbound_channel,
2808 peerinfo_handle = GNUNET_PEERINFO_connect (cfg);
2810 /* Initialise sampler */
2811 struct GNUNET_TIME_Relative half_round_interval;
2812 struct GNUNET_TIME_Relative max_round_interval;
2814 half_round_interval = GNUNET_TIME_relative_multiply (round_interval, .5);
2815 max_round_interval = GNUNET_TIME_relative_add (round_interval, half_round_interval);
2817 prot_sampler = RPS_sampler_init (sampler_size_est_need, max_round_interval);
2818 client_sampler = RPS_sampler_mod_init (sampler_size_est_need, max_round_interval);
2820 /* Initialise push and pull maps */
2827 num_hist_update_tasks = 0;
2830 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
2831 GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, NULL);
2832 // TODO send push/pull to each of those peers?
2834 peerinfo_notify_handle = GNUNET_PEERINFO_notify (cfg,
2836 process_peerinfo_peers,
2844 * The main function for the rps service.
2846 * @param argc number of arguments from the command line
2847 * @param argv command line arguments
2848 * @return 0 ok, 1 on error
2851 main (int argc, char *const *argv)
2853 return (GNUNET_OK ==
2854 GNUNET_SERVICE_run (argc,
2857 GNUNET_SERVICE_OPTION_NONE,
2858 &run, NULL)) ? 0 : 1;
2861 /* end of gnunet-service-rps.c */