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 // hist_size_init, hist_size_max
60 static const struct GNUNET_CONFIGURATION_Handle *cfg;
65 static struct GNUNET_PeerIdentity own_identity;
68 struct GNUNET_PeerIdentity *
69 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list, unsigned int size,
70 const struct GNUNET_PeerIdentity *ignore_list, unsigned int ignore_size);
73 /***********************************************************************
74 * Housekeeping with peers
75 ***********************************************************************/
78 * Closure used to pass the client and the id to the callback
79 * that replies to a client's request
86 struct ReplyCls *next;
87 struct ReplyCls *prev;
90 * The identifier of the request
95 * The handle to the request
97 struct RPS_SamplerRequestHandle *req_handle;
100 * The client handle to send the reply to
102 struct GNUNET_SERVER_Client *client;
107 * Struct used to store the context of a connected client.
114 struct ClientContext *next;
115 struct ClientContext *prev;
118 * The message queue to communicate with the client.
120 struct GNUNET_MQ_Handle *mq;
123 * DLL with handles to single requests from the client
125 struct ReplyCls *rep_cls_head;
126 struct ReplyCls *rep_cls_tail;
130 * DLL with all clients currently connected to us
132 struct ClientContext *cli_ctx_head;
133 struct ClientContext *cli_ctx_tail;
136 * Used to keep track in what lists single peerIDs are.
141 * If we are waiting for a reply from that peer (sent a pull request).
143 PULL_REPLY_PENDING = 0x01,
145 IN_OTHER_GOSSIP_LIST = 0x02, // unneeded?
146 IN_OWN_SAMPLER_LIST = 0x04, // unneeded?
147 IN_OWN_GOSSIP_LIST = 0x08, // unneeded?
150 * We set this bit when we can be sure the other peer is/was live.
155 * We set this bit when we are going to destroy the channel to this peer.
156 * When cleanup_channel is called, we know that we wanted to destroy it.
157 * Otherwise the channel to the other peer was destroyed.
164 * Functions of this type can be used to be stored at a peer for later execution.
166 typedef void (* PeerOp) (void *cls, const struct GNUNET_PeerIdentity *peer);
169 * Outstanding operation on peer consisting of callback and closure
171 struct PeerOutstandingOp
186 * Struct used to keep track of other peer's status
188 * This is stored in a multipeermap.
193 * Message queue open to client
195 struct GNUNET_MQ_Handle *mq;
198 * Channel open to client.
200 struct GNUNET_CADET_Channel *send_channel;
203 * Channel open from client.
205 struct GNUNET_CADET_Channel *recv_channel; // unneeded?
208 * Array of outstanding operations on this peer.
210 struct PeerOutstandingOp *outstanding_ops;
213 * Handle to the callback given to cadet_ntfy_tmt_rdy()
215 * To be canceled on shutdown.
217 struct GNUNET_CADET_TransmitHandle *transmit_handle;
220 * Number of outstanding operations.
222 unsigned int num_outstanding_ops;
225 * Identity of the peer
227 struct GNUNET_PeerIdentity peer_id;
230 * Flags indicating status of peer
235 * This is pobably followed by 'statistical' data (when we first saw
236 * him, how did we get his ID, how many pushes (in a timeinterval),
241 /***********************************************************************
242 * /Housekeeping with peers
243 ***********************************************************************/
249 /***********************************************************************
251 ***********************************************************************/
254 * Sampler used for the Brahms protocol itself.
256 static struct RPS_Sampler *prot_sampler;
259 * Sampler used for the clients.
261 static struct RPS_Sampler *client_sampler;
264 * Set of all peers to keep track of them.
266 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
270 * Name to log view to
272 static char *file_name_view_log;
275 * The "local view" containing peers we learned from gossip and history
277 static struct GNUNET_CONTAINER_MultiPeerMap *view;
280 * An array containing the peers of the local view.
282 * This is created every time we send a pull reply if it has changed since the
283 * last pull reply we sent.
285 static struct GNUNET_PeerIdentity *view_array;
289 * The size of sampler we need to be able to satisfy the client's need of
292 static unsigned int sampler_size_client_need;
295 * The size of sampler we need to be able to satisfy the Brahms protocol's
296 * need of random peers.
298 * This is one minimum size the sampler grows to.
300 static unsigned int sampler_size_est_need;
304 * Percentage of total peer number in the view
305 * to send random PUSHes to
310 * Percentage of total peer number in the view
311 * to send random PULLs to
316 * The percentage gamma of history updates.
317 * Simply 1 - alpha - beta
322 * Identifier for the main task that runs periodically.
324 static struct GNUNET_SCHEDULER_Task *do_round_task;
327 * Time inverval the do_round task runs in.
329 static struct GNUNET_TIME_Relative round_interval;
334 * List to store peers received through pushes temporary.
336 * TODO -> multipeermap
338 static struct GNUNET_PeerIdentity *push_list;
341 * Size of the push_list;
343 static unsigned int push_list_size;
344 //size_t push_list_size;
347 * List to store peers received through pulls temporary.
349 * TODO -> multipeermap
351 static struct GNUNET_PeerIdentity *pull_list;
354 * Size of the pull_list;
356 static unsigned int pull_list_size;
357 //size_t pull_list_size;
363 static struct GNUNET_NSE_Handle *nse;
368 static struct GNUNET_CADET_Handle *cadet_handle;
371 * Handler to PEERINFO.
373 static struct GNUNET_PEERINFO_Handle *peerinfo_handle;
376 * Handle for cancellation of iteration over peers.
378 struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify_handle;
384 * Only needed in the beginning to check how many of the 64 deltas
387 static unsigned int req_counter;
390 * Time of the last request we received.
392 * Used to compute the expected request rate.
394 static struct GNUNET_TIME_Absolute last_request;
397 * Size of #request_deltas.
399 #define REQUEST_DELTAS_SIZE 64
400 static unsigned int request_deltas_size = REQUEST_DELTAS_SIZE;
403 * Last 64 deltas between requests
405 static struct GNUNET_TIME_Relative request_deltas[REQUEST_DELTAS_SIZE];
408 * The prediction of the rate of requests
410 static struct GNUNET_TIME_Relative request_rate;
414 * Number of history update tasks.
416 uint32_t num_hist_update_tasks;
419 #ifdef ENABLE_MALICIOUS
421 * Type of malicious peer
423 * 0 Don't act malicious at all - Default
424 * 1 Try to maximise representation
425 * 2 Try to partition the network
428 uint32_t mal_type = 0;
431 * Other malicious peers
433 static struct GNUNET_PeerIdentity *mal_peers = NULL;
436 * Hashmap of malicious peers used as set.
437 * Used to more efficiently check whether we know that peer.
439 static struct GNUNET_CONTAINER_MultiPeerMap *mal_peer_set = NULL;
442 * Number of other malicious peers
444 static uint32_t num_mal_peers;
448 * If type is 2 This struct is used to store the attacked peers in a DLL
455 struct AttackedPeer *next;
456 struct AttackedPeer *prev;
461 struct GNUNET_PeerIdentity peer_id;
465 * If type is 2 this is the DLL of attacked peers
467 static struct AttackedPeer *att_peers_head = NULL;
468 static struct AttackedPeer *att_peers_tail = NULL;
471 * This index is used to point to an attacked peer to
472 * implement the round-robin-ish way to select attacked peers.
474 static struct AttackedPeer *att_peer_index = NULL;
477 * Hashmap of attacked peers used as set.
478 * Used to more efficiently check whether we know that peer.
480 static struct GNUNET_CONTAINER_MultiPeerMap *att_peer_set = NULL;
483 * Number of attacked peers
485 static uint32_t num_attacked_peers = 0;
489 * If type is 1 this is the attacked peer
491 static struct GNUNET_PeerIdentity attacked_peer;
494 * The limit of PUSHes we can send in one round.
495 * This is an assumption of the Brahms protocol and either implemented
498 * assumend to be the bandwidth limitation.
500 static uint32_t push_limit = 10000;
501 #endif /* ENABLE_MALICIOUS */
504 /***********************************************************************
506 ***********************************************************************/
513 /***********************************************************************
515 ***********************************************************************/
518 * Set a peer flag of given peer context.
520 #define set_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags |= mask)
523 * Get peer flag of given peer context.
525 #define get_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags & mask ? GNUNET_YES : GNUNET_NO)
528 * Unset flag of given peer context.
530 #define unset_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags &= (~mask))
533 * Clean the send channel of a peer
536 peer_clean (const struct GNUNET_PeerIdentity *peer);
540 * Check if peer is already in peer array.
543 in_arr (const struct GNUNET_PeerIdentity *array,
544 unsigned int arr_size,
545 const struct GNUNET_PeerIdentity *peer)
547 GNUNET_assert (NULL != peer);
552 GNUNET_assert (NULL != array);
556 for (i = 0; i < arr_size ; i++)
557 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&array[i], peer))
564 * Print peerlist to log.
567 print_peer_list (struct GNUNET_PeerIdentity *list, unsigned int len)
571 LOG (GNUNET_ERROR_TYPE_DEBUG,
572 "Printing peer list of length %u at %p:\n",
575 for (i = 0 ; i < len ; i++)
577 LOG (GNUNET_ERROR_TYPE_DEBUG,
579 i, GNUNET_i2s (&list[i]));
585 * Remove peer from list.
588 rem_from_list (struct GNUNET_PeerIdentity **peer_list,
589 unsigned int *list_size,
590 const struct GNUNET_PeerIdentity *peer)
593 struct GNUNET_PeerIdentity *tmp;
597 LOG (GNUNET_ERROR_TYPE_DEBUG,
598 "Removing peer %s from list at %p\n",
602 for ( i = 0 ; i < *list_size ; i++ )
604 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&tmp[i], peer))
606 if (i < *list_size -1)
607 { /* Not at the last entry -- shift peers left */
608 memcpy (&tmp[i], &tmp[i +1],
609 ((*list_size) - i -1) * sizeof (struct GNUNET_PeerIdentity));
611 /* Remove last entry (should be now useless PeerID) */
612 GNUNET_array_grow (tmp, *list_size, (*list_size) -1);
619 * Get random peer from the given list but don't return one from the @a ignore_list.
621 struct GNUNET_PeerIdentity *
622 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list,
624 const struct GNUNET_PeerIdentity *ignore_list,
625 uint32_t ignore_size)
629 struct GNUNET_PeerIdentity *tmp_peer_list;
630 struct GNUNET_PeerIdentity *peer;
632 GNUNET_assert (NULL != peer_list);
637 tmp_peer_list = NULL;
638 GNUNET_array_grow (tmp_peer_list, tmp_size, list_size);
639 memcpy (tmp_peer_list,
641 list_size * sizeof (struct GNUNET_PeerIdentity));
642 peer = GNUNET_new (struct GNUNET_PeerIdentity);
645 * Choose the r_index of the peer we want to return
646 * at random from the interval of the view
648 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
650 *peer = tmp_peer_list[r_index];
652 while (in_arr (ignore_list, ignore_size, peer))
654 rem_from_list (&tmp_peer_list, &tmp_size, peer);
656 print_peer_list (tmp_peer_list, tmp_size);
665 * Choose the r_index of the peer we want to return
666 * at random from the interval of the view
668 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
670 *peer = tmp_peer_list[r_index];
674 GNUNET_array_grow (tmp_peer_list, tmp_size, 0);
681 * Get the context of a peer. If not existing, create.
684 get_peer_ctx (const struct GNUNET_PeerIdentity *peer)
686 struct PeerContext *ctx;
689 ret = GNUNET_CONTAINER_multipeermap_contains (peer_map, peer);
690 GNUNET_assert (GNUNET_YES == ret);
691 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
692 GNUNET_assert (NULL != ctx);
697 * Create a new peer context and insert it into the peer map
700 create_peer_ctx (const struct GNUNET_PeerIdentity *peer)
702 struct PeerContext *ctx;
705 ctx = GNUNET_new (struct PeerContext);
706 ctx->peer_id = *peer;
707 ret = GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
708 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
709 GNUNET_assert (GNUNET_OK == ret);
715 * Set the peer flag to living and call the outstanding operations on this peer.
718 peer_is_live (struct PeerContext *peer_ctx)
720 struct GNUNET_PeerIdentity *peer;
722 /* Cancle transmit_handle if still scheduled */
723 if (NULL != peer_ctx->transmit_handle)
725 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->transmit_handle);
726 peer_ctx->transmit_handle = NULL;
729 peer = &peer_ctx->peer_id;
730 set_peer_flag (peer_ctx, VALID);
732 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer %s is live\n", GNUNET_i2s (peer));
734 if (0 < peer_ctx->num_outstanding_ops)
735 { /* Call outstanding operations */
738 for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
739 peer_ctx->outstanding_ops[i].op (peer_ctx->outstanding_ops[i].op_cls, peer);
740 GNUNET_array_grow (peer_ctx->outstanding_ops, peer_ctx->num_outstanding_ops, 0);
748 * Callback that is called when a channel was effectively established.
749 * This is given to ntfy_tmt_rdy and called when the channel was
750 * successfully established.
753 cadet_ntfy_tmt_rdy_cb (void *cls, size_t size, void *buf)
755 struct PeerContext *peer_ctx = (struct PeerContext *) cls;
757 peer_ctx->transmit_handle = NULL;
758 LOG (GNUNET_ERROR_TYPE_DEBUG,
759 "Set ->transmit_handle = NULL for peer %s\n",
760 GNUNET_i2s (&peer_ctx->peer_id));
765 peer_is_live (peer_ctx);
769 LOG (GNUNET_ERROR_TYPE_WARNING,
770 "Problems establishing a connection to peer %s in order to check liveliness\n",
771 GNUNET_i2s (&peer_ctx->peer_id));
772 // TODO reschedule? cleanup?
775 //if (NULL != peer_ctx->transmit_handle)
777 // LOG (GNUNET_ERROR_TYPE_DEBUG,
778 // "Trying to cancle transmit_handle for peer %s\n",
779 // GNUNET_i2s (&peer_ctx->peer_id));
780 // GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->transmit_handle);
781 // peer_ctx->transmit_handle = NULL;
789 * Get the channel of a peer. If not existing, create.
791 struct GNUNET_CADET_Channel *
792 get_channel (const struct GNUNET_PeerIdentity *peer)
794 struct PeerContext *peer_ctx;
796 peer_ctx = get_peer_ctx (peer);
797 if (NULL == peer_ctx->send_channel)
799 LOG (GNUNET_ERROR_TYPE_DEBUG,
800 "Trying to establish channel to peer %s\n",
803 peer_ctx->send_channel =
804 GNUNET_CADET_channel_create (cadet_handle,
807 GNUNET_RPS_CADET_PORT,
808 GNUNET_CADET_OPTION_RELIABLE);
811 return peer_ctx->send_channel;
816 * Get the message queue of a specific peer.
818 * If we already have a message queue open to this client,
819 * simply return it, otherways create one.
821 struct GNUNET_MQ_Handle *
822 get_mq (const struct GNUNET_PeerIdentity *peer_id)
824 struct PeerContext *peer_ctx;
826 peer_ctx = get_peer_ctx (peer_id);
828 GNUNET_assert (NULL == peer_ctx->transmit_handle);
830 if (NULL == peer_ctx->mq)
832 (void) get_channel (peer_id);
833 peer_ctx->mq = GNUNET_CADET_mq_create (peer_ctx->send_channel);
834 //do I have to explicitly put it in the peer_map?
835 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer_id, peer_ctx,
836 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
843 * Issue check whether peer is live
845 * @param peer_ctx the context of the peer
848 check_peer_live (struct PeerContext *peer_ctx)
850 LOG (GNUNET_ERROR_TYPE_DEBUG,
851 "Get informed about peer %s getting live\n",
852 GNUNET_i2s (&peer_ctx->peer_id));
854 if (NULL == peer_ctx->transmit_handle &&
855 NULL == peer_ctx->send_channel)
857 (void) get_channel (&peer_ctx->peer_id);
858 peer_ctx->transmit_handle =
859 GNUNET_CADET_notify_transmit_ready (peer_ctx->send_channel,
861 GNUNET_TIME_UNIT_FOREVER_REL,
862 sizeof (struct GNUNET_MessageHeader),
863 cadet_ntfy_tmt_rdy_cb,
866 else if (NULL != peer_ctx->transmit_handle)
867 LOG (GNUNET_ERROR_TYPE_DEBUG,
868 "Already waiting for notification\n");
869 else if (NULL != peer_ctx->send_channel)
870 LOG (GNUNET_ERROR_TYPE_DEBUG,
871 "Already have established channel to peer\n");
876 * Sum all time relatives of an array.
878 struct GNUNET_TIME_Relative
879 T_relative_sum (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
881 struct GNUNET_TIME_Relative sum;
884 sum = GNUNET_TIME_UNIT_ZERO;
885 for ( i = 0 ; i < arr_size ; i++ )
887 sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
894 * Compute the average of given time relatives.
896 struct GNUNET_TIME_Relative
897 T_relative_avg (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
899 return GNUNET_TIME_relative_divide (T_relative_sum (rel_array, arr_size), arr_size);
904 * Insert PeerID in #pull_list
906 * Called once we know a peer is live.
909 insert_in_pull_list (void *cls, const struct GNUNET_PeerIdentity *peer)
911 if (GNUNET_NO == in_arr (pull_list, pull_list_size, peer))
912 GNUNET_array_append (pull_list, pull_list_size, *peer);
916 * Check whether #insert_in_pull_list was already scheduled
919 insert_in_pull_list_scheduled (const struct PeerContext *peer_ctx)
923 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
924 if (insert_in_pull_list == peer_ctx->outstanding_ops[i].op)
931 * Insert PeerID in #view
933 * Called once we know a peer is live.
936 insert_in_view (void *cls, const struct GNUNET_PeerIdentity *peer)
938 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (view, peer))
940 LOG (GNUNET_ERROR_TYPE_DEBUG,
941 "Going to put %s into view\n",
943 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_put (view,
946 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
948 LOG (GNUNET_ERROR_TYPE_WARNING,
949 "Failed to put peer into view. (insert_in_view)\n");
951 if (NULL != view_array)
953 GNUNET_free (view_array);
956 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
957 create_peer_ctx (peer);
958 (void) get_channel (peer);
962 * Check whether #insert_in_view was already scheduled
965 insert_in_view_scheduled (const struct PeerContext *peer_ctx)
969 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
970 if (insert_in_view == peer_ctx->outstanding_ops[i].op)
977 * Update sampler with given PeerID.
980 insert_in_sampler (void *cls, const struct GNUNET_PeerIdentity *peer)
982 LOG (GNUNET_ERROR_TYPE_DEBUG,
983 "Updating samplers with peer %s from insert_in_sampler()\n",
985 RPS_sampler_update (prot_sampler, peer);
986 RPS_sampler_update (client_sampler, peer);
987 if (0 < RPS_sampler_count_id (prot_sampler, peer))
989 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
990 (void) create_peer_ctx (peer);
991 (void) get_channel (peer);
997 * Check whether #insert_in_sampler was already scheduled
1000 insert_in_sampler_scheduled (const struct PeerContext *peer_ctx)
1004 for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
1005 if (insert_in_sampler== peer_ctx->outstanding_ops[i].op)
1011 * Put random peer from sampler into the view as history update.
1014 hist_update (void *cls, struct GNUNET_PeerIdentity *ids, uint32_t num_peers)
1018 for (i = 0; i < GNUNET_MIN (
1019 sampler_size_est_need - GNUNET_CONTAINER_multipeermap_size (view),
1022 insert_in_view (NULL, &ids[i]);
1023 to_file (file_name_view_log,
1025 GNUNET_i2s_full (ids));
1027 if (0 < num_hist_update_tasks)
1028 num_hist_update_tasks--;
1034 * Wrapper around #RPS_sampler_resize()
1036 * If we do not have enough sampler elements, double current sampler size
1037 * If we have more than enough sampler elements, halv current sampler size
1040 resize_wrapper (struct RPS_Sampler *sampler, uint32_t new_size)
1042 unsigned int sampler_size;
1045 // TODO respect the min, max
1046 sampler_size = RPS_sampler_get_size (sampler);
1047 if (sampler_size > new_size * 4)
1049 RPS_sampler_resize (sampler, sampler_size / 2);
1051 else if (sampler_size < new_size)
1053 RPS_sampler_resize (sampler, sampler_size * 2);
1055 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
1060 * Wrapper around #RPS_sampler_resize() resizing the client sampler
1063 client_resize_wrapper ()
1065 uint32_t bigger_size;
1069 bigger_size = GNUNET_MAX (sampler_size_est_need, sampler_size_client_need);
1071 // TODO respect the min, max
1072 resize_wrapper (client_sampler, bigger_size);
1073 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size_client is now %" PRIu32 "\n",
1079 * Estimate request rate
1081 * Called every time we receive a request from the client.
1086 struct GNUNET_TIME_Relative max_round_duration;
1088 if (request_deltas_size > req_counter)
1090 if ( 1 < req_counter)
1092 /* Shift last request deltas to the right */
1093 memcpy (&request_deltas[1],
1095 (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
1097 /* Add current delta to beginning */
1099 GNUNET_TIME_absolute_get_difference (last_request,
1100 GNUNET_TIME_absolute_get ());
1101 request_rate = T_relative_avg (request_deltas, req_counter);
1103 /* Compute the duration a round will maximally take */
1104 max_round_duration =
1105 GNUNET_TIME_relative_add (round_interval,
1106 GNUNET_TIME_relative_divide (round_interval, 2));
1108 /* Set the estimated size the sampler has to have to
1109 * satisfy the current client request rate */
1110 sampler_size_client_need =
1111 max_round_duration.rel_value_us / request_rate.rel_value_us;
1113 /* Resize the sampler */
1114 client_resize_wrapper ();
1116 last_request = GNUNET_TIME_absolute_get ();
1121 * Add all peers in @a peer_array to @a peer_map used as set.
1123 * @param peer_array array containing the peers
1124 * @param num_peers number of peers in @peer_array
1125 * @param peer_map the peermap to use as set
1128 add_peer_array_to_set (const struct GNUNET_PeerIdentity *peer_array,
1129 unsigned int num_peers,
1130 struct GNUNET_CONTAINER_MultiPeerMap *peer_map)
1133 if (NULL == peer_map)
1135 LOG (GNUNET_ERROR_TYPE_WARNING,
1136 "Trying to add peers to an empty peermap.\n");
1140 for (i = 0; i < num_peers; i++)
1142 GNUNET_CONTAINER_multipeermap_put (peer_map,
1145 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1151 * Send a PULL REPLY to @a peer_id
1153 * @param peer_id the peer to send the reply to.
1154 * @param peer_ids the peers to send to @a peer_id
1155 * @param num_peer_ids the number of peers to send to @a peer_id
1158 send_pull_reply (const struct GNUNET_PeerIdentity *peer_id,
1159 const struct GNUNET_PeerIdentity *peer_ids,
1160 unsigned int num_peer_ids)
1163 struct GNUNET_MQ_Handle *mq;
1164 struct GNUNET_MQ_Envelope *ev;
1165 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
1167 /* Compute actual size */
1168 send_size = sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) +
1169 num_peer_ids * sizeof (struct GNUNET_PeerIdentity);
1171 if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size)
1172 /* Compute number of peers to send
1173 * If too long, simply truncate */
1174 // TODO select random ones via permutation
1175 // or even better: do good protocol design
1177 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE -
1178 sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1179 sizeof (struct GNUNET_PeerIdentity);
1181 send_size = num_peer_ids;
1183 LOG (GNUNET_ERROR_TYPE_DEBUG,
1184 "PULL REQUEST from peer %s received, going to send %u peers\n",
1185 GNUNET_i2s (peer_id), send_size);
1187 mq = get_mq (peer_id);
1189 ev = GNUNET_MQ_msg_extra (out_msg,
1190 send_size * sizeof (struct GNUNET_PeerIdentity),
1191 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
1192 out_msg->num_peers = htonl (send_size);
1193 memcpy (&out_msg[1], peer_ids,
1194 send_size * sizeof (struct GNUNET_PeerIdentity));
1196 GNUNET_MQ_send (mq, ev);
1201 * This function is called on new peer_ids from 'external' sources
1202 * (client seed, cadet get_peers(), ...)
1204 * @param peer_id the new peer_id
1207 new_peer_id (const struct GNUNET_PeerIdentity *peer_id)
1209 struct PeerOutstandingOp out_op;
1210 struct PeerContext *peer_ctx;
1212 if ((NULL == peer_id) ||
1213 (0 == GNUNET_CRYPTO_cmp_peer_identity (&own_identity, peer_id)))
1215 LOG (GNUNET_ERROR_TYPE_DEBUG,
1216 "Got peer_id %s (at %p, view size: %u)\n",
1217 GNUNET_i2s (peer_id),
1219 GNUNET_CONTAINER_multipeermap_size (view));
1221 /* if the seed peer is already know, skip context creation */
1222 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer_id))
1223 peer_ctx = create_peer_ctx (peer_id);
1225 peer_ctx = get_peer_ctx (peer_id);
1227 if (GNUNET_NO == get_peer_flag (peer_ctx, VALID))
1229 if (GNUNET_NO == insert_in_sampler_scheduled (peer_ctx))
1231 out_op.op = insert_in_sampler;
1232 out_op.op_cls = NULL;
1233 GNUNET_array_append (peer_ctx->outstanding_ops,
1234 peer_ctx->num_outstanding_ops,
1238 if (GNUNET_NO == insert_in_view_scheduled (peer_ctx))
1240 out_op.op = insert_in_view;
1241 out_op.op_cls = NULL;
1242 GNUNET_array_append (peer_ctx->outstanding_ops,
1243 peer_ctx->num_outstanding_ops,
1247 /* Trigger livelyness test on peer */
1248 check_peer_live (peer_ctx);
1252 // send push/pull to each of those peers?
1256 /***********************************************************************
1258 ***********************************************************************/
1261 destroy_reply_cls (struct ReplyCls *rep_cls)
1263 struct ClientContext *cli_ctx;
1265 cli_ctx = GNUNET_SERVER_client_get_user_context (rep_cls->client,
1266 struct ClientContext);
1267 GNUNET_assert (NULL != cli_ctx);
1268 GNUNET_CONTAINER_DLL_remove (cli_ctx->rep_cls_head,
1269 cli_ctx->rep_cls_tail,
1271 GNUNET_free (rep_cls);
1275 destroy_cli_ctx (struct ClientContext *cli_ctx)
1277 GNUNET_assert (NULL != cli_ctx);
1278 if (NULL != cli_ctx->rep_cls_head)
1280 LOG (GNUNET_ERROR_TYPE_WARNING,
1281 "Trying to destroy the context of a client that still has pending requests. Going to clean those\n");
1282 while (NULL != cli_ctx->rep_cls_head)
1283 destroy_reply_cls (cli_ctx->rep_cls_head);
1285 GNUNET_CONTAINER_DLL_remove (cli_ctx_head,
1288 GNUNET_free (cli_ctx);
1293 * Function called by NSE.
1295 * Updates sizes of sampler list and view and adapt those lists
1299 nse_callback (void *cls, struct GNUNET_TIME_Absolute timestamp,
1300 double logestimate, double std_dev)
1303 //double scale; // TODO this might go gloabal/config
1305 LOG (GNUNET_ERROR_TYPE_DEBUG,
1306 "Received a ns estimate - logest: %f, std_dev: %f (old_size: %u)\n",
1307 logestimate, std_dev, RPS_sampler_get_size (prot_sampler));
1309 estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
1310 // GNUNET_NSE_log_estimate_to_n (logestimate);
1311 estimate = pow (estimate, 1.0 / 3);
1312 // TODO add if std_dev is a number
1313 // estimate += (std_dev * scale);
1314 if (2 < ceil (estimate))
1316 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
1317 sampler_size_est_need = estimate;
1319 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
1321 /* If the NSE has changed adapt the lists accordingly */
1322 resize_wrapper (prot_sampler, sampler_size_est_need);
1323 client_resize_wrapper ();
1328 * Callback called once the requested PeerIDs are ready.
1330 * Sends those to the requesting client.
1332 void client_respond (void *cls,
1333 struct GNUNET_PeerIdentity *peer_ids, uint32_t num_peers)
1336 struct GNUNET_MQ_Envelope *ev;
1337 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
1338 struct ReplyCls *reply_cls = (struct ReplyCls *) cls;
1339 uint32_t size_needed;
1340 struct ClientContext *cli_ctx;
1342 GNUNET_assert (NULL != reply_cls);
1343 LOG (GNUNET_ERROR_TYPE_DEBUG,
1344 "sampler returned %" PRIu32 " peers:\n",
1346 for (i = 0; i < num_peers; i++)
1348 LOG (GNUNET_ERROR_TYPE_DEBUG,
1351 GNUNET_i2s (&peer_ids[i]));
1354 size_needed = sizeof (struct GNUNET_RPS_CS_ReplyMessage) +
1355 num_peers * sizeof (struct GNUNET_PeerIdentity);
1357 GNUNET_assert (GNUNET_SERVER_MAX_MESSAGE_SIZE >= size_needed);
1359 ev = GNUNET_MQ_msg_extra (out_msg,
1360 num_peers * sizeof (struct GNUNET_PeerIdentity),
1361 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
1362 out_msg->num_peers = htonl (num_peers);
1363 out_msg->id = htonl (reply_cls->id);
1365 memcpy (&out_msg[1],
1367 num_peers * sizeof (struct GNUNET_PeerIdentity));
1368 GNUNET_free (peer_ids);
1370 cli_ctx = GNUNET_SERVER_client_get_user_context (reply_cls->client,
1371 struct ClientContext);
1372 GNUNET_assert (NULL != cli_ctx);
1373 destroy_reply_cls (reply_cls);
1374 GNUNET_MQ_send (cli_ctx->mq, ev);
1379 * Handle RPS request from the client.
1381 * @param cls closure
1382 * @param client identification of the client
1383 * @param message the actual message
1386 handle_client_request (void *cls,
1387 struct GNUNET_SERVER_Client *client,
1388 const struct GNUNET_MessageHeader *message)
1390 struct GNUNET_RPS_CS_RequestMessage *msg;
1392 uint32_t size_needed;
1393 struct ReplyCls *reply_cls;
1395 struct ClientContext *cli_ctx;
1397 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
1399 num_peers = ntohl (msg->num_peers);
1400 size_needed = sizeof (struct GNUNET_RPS_CS_RequestMessage) +
1401 num_peers * sizeof (struct GNUNET_PeerIdentity);
1403 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
1405 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1406 "Message received from client has size larger than expected\n");
1407 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1411 for (i = 0 ; i < num_peers ; i++)
1414 LOG (GNUNET_ERROR_TYPE_DEBUG,
1415 "Client requested %" PRIu32 " random peer(s).\n",
1418 reply_cls = GNUNET_new (struct ReplyCls);
1419 reply_cls->id = ntohl (msg->id);
1420 reply_cls->client = client;
1421 reply_cls->req_handle = RPS_sampler_get_n_rand_peers (client_sampler,
1426 cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientContext);
1427 GNUNET_assert (NULL != cli_ctx);
1428 GNUNET_CONTAINER_DLL_insert (cli_ctx->rep_cls_head,
1429 cli_ctx->rep_cls_tail,
1431 GNUNET_SERVER_receive_done (client,
1437 * @brief Handle a message that requests the cancellation of a request
1440 * @param client the client that requests the cancellation
1441 * @param message the message containing the id of the request
1444 handle_client_request_cancel (void *cls,
1445 struct GNUNET_SERVER_Client *client,
1446 const struct GNUNET_MessageHeader *message)
1448 struct GNUNET_RPS_CS_RequestCancelMessage *msg =
1449 (struct GNUNET_RPS_CS_RequestCancelMessage *) message;
1450 struct ClientContext *cli_ctx;
1451 struct ReplyCls *rep_cls;
1453 cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientContext);
1454 GNUNET_assert (NULL != cli_ctx->rep_cls_head);
1455 rep_cls = cli_ctx->rep_cls_head;
1456 LOG (GNUNET_ERROR_TYPE_DEBUG,
1457 "Client cancels request with id %lu\n",
1459 while ( (NULL != rep_cls->next) &&
1460 (rep_cls->id != ntohl (msg->id)) )
1461 rep_cls = rep_cls->next;
1462 GNUNET_assert (rep_cls->id == ntohl (msg->id));
1463 RPS_sampler_request_cancel (rep_cls->req_handle);
1464 destroy_reply_cls (rep_cls);
1465 GNUNET_SERVER_receive_done (client,
1471 * Handle seed from the client.
1473 * @param cls closure
1474 * @param client identification of the client
1475 * @param message the actual message
1478 handle_client_seed (void *cls,
1479 struct GNUNET_SERVER_Client *client,
1480 const struct GNUNET_MessageHeader *message)
1482 struct GNUNET_RPS_CS_SeedMessage *in_msg;
1483 struct GNUNET_PeerIdentity *peers;
1487 if (sizeof (struct GNUNET_RPS_CS_SeedMessage) > ntohs (message->size))
1489 GNUNET_break_op (0);
1490 GNUNET_SERVER_receive_done (client,
1494 in_msg = (struct GNUNET_RPS_CS_SeedMessage *) message;
1495 num_peers = ntohl (in_msg->num_peers);
1496 peers = (struct GNUNET_PeerIdentity *) &in_msg[1];
1497 //peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1498 //memcpy (peers, &in_msg[1], num_peers * sizeof (struct GNUNET_PeerIdentity));
1500 if ((ntohs (message->size) - sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
1501 sizeof (struct GNUNET_PeerIdentity) != num_peers)
1503 GNUNET_break_op (0);
1504 GNUNET_SERVER_receive_done (client,
1509 LOG (GNUNET_ERROR_TYPE_DEBUG,
1510 "Client seeded peers:\n");
1511 print_peer_list (peers, num_peers);
1513 for (i = 0 ; i < num_peers ; i++)
1515 LOG (GNUNET_ERROR_TYPE_DEBUG,
1516 "Updating samplers with seed %" PRIu32 ": %s\n",
1518 GNUNET_i2s (&peers[i]));
1520 new_peer_id (&peers[i]);
1522 //RPS_sampler_update (prot_sampler, &peers[i]);
1523 //RPS_sampler_update (client_sampler, &peers[i]);
1526 ////GNUNET_free (peers);
1528 GNUNET_SERVER_receive_done (client,
1534 * Handle a PUSH message from another peer.
1536 * Check the proof of work and store the PeerID
1537 * in the temporary list for pushed PeerIDs.
1539 * @param cls Closure
1540 * @param channel The channel the PUSH was received over
1541 * @param channel_ctx The context associated with this channel
1542 * @param msg The message header
1545 handle_peer_push (void *cls,
1546 struct GNUNET_CADET_Channel *channel,
1548 const struct GNUNET_MessageHeader *msg)
1550 const struct GNUNET_PeerIdentity *peer;
1552 // (check the proof of work)
1554 peer = (const struct GNUNET_PeerIdentity *)
1555 GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
1556 // FIXME wait for cadet to change this function
1558 LOG (GNUNET_ERROR_TYPE_DEBUG, "PUSH received (%s)\n", GNUNET_i2s (peer));
1560 #ifdef ENABLE_MALICIOUS
1561 struct AttackedPeer *tmp_att_peer;
1563 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1564 memcpy (&tmp_att_peer->peer_id, peer, sizeof (struct GNUNET_PeerIdentity));
1567 { /* Try to maximise representation */
1568 if (NULL == att_peer_set)
1569 att_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
1570 if (NULL == mal_peer_set)
1571 mal_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
1572 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1575 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1578 add_peer_array_to_set (peer, 1, att_peer_set);
1584 else if (2 == mal_type)
1585 { /* We attack one single well-known peer - simply ignore */
1590 GNUNET_free (tmp_att_peer);
1593 #endif /* ENABLE_MALICIOUS */
1595 /* Add the sending peer to the push_list */
1596 if (GNUNET_NO == in_arr (push_list, push_list_size, peer))
1598 GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer));
1599 GNUNET_array_append (push_list, push_list_size, *peer);
1602 GNUNET_CADET_receive_done (channel);
1608 * Iterator over hash map entries.
1609 * Called from #generate_view_array and writes every peer id into #view_array.
1611 * @param cls closure - the pointer to the counter
1612 * @param key current public key
1613 * @param value value in the hash map
1614 * @return #GNUNET_YES if we should continue to
1616 * #GNUNET_NO if not.
1619 dump_id_to_view_array (void *cls,
1620 const struct GNUNET_PeerIdentity *key,
1623 unsigned int *i = (unsigned int *) cls;
1625 view_array[(*i)++] = *key;
1631 * Makes sure the view_array is filled with the peer ids currently in #view.
1632 * Called from within #do_round before sending pushes and pulls and from
1633 * #handle_peer_pull_request when a reply is sent.
1636 generate_view_array (unsigned int view_size)
1641 if (NULL == view_array)
1643 view_array = GNUNET_new_array (view_size,
1644 struct GNUNET_PeerIdentity);
1645 i = GNUNET_new (unsigned int);
1648 ret = GNUNET_CONTAINER_multipeermap_iterate (view,
1649 dump_id_to_view_array,
1651 GNUNET_assert (view_size == ret);
1652 GNUNET_assert (view_size == *i);
1660 * Handle PULL REQUEST request message from another peer.
1662 * Reply with the view of PeerIDs.
1664 * @param cls Closure
1665 * @param channel The channel the PULL REQUEST was received over
1666 * @param channel_ctx The context associated with this channel
1667 * @param msg The message header
1670 handle_peer_pull_request (void *cls,
1671 struct GNUNET_CADET_Channel *channel,
1673 const struct GNUNET_MessageHeader *msg)
1675 struct GNUNET_PeerIdentity *peer;
1676 unsigned int view_size;
1678 peer = (struct GNUNET_PeerIdentity *)
1679 GNUNET_CADET_channel_get_info (channel,
1680 GNUNET_CADET_OPTION_PEER);
1681 // FIXME wait for cadet to change this function
1683 LOG (GNUNET_ERROR_TYPE_DEBUG, "PULL REQUEST received (%s)\n", GNUNET_i2s (peer));
1685 #ifdef ENABLE_MALICIOUS
1688 { /* Try to maximise representation */
1689 send_pull_reply (peer, mal_peers, num_mal_peers);
1693 else if (2 == mal_type)
1694 { /* Try to partition network */
1695 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
1697 send_pull_reply (peer, mal_peers, num_mal_peers);
1701 #endif /* ENABLE_MALICIOUS */
1703 view_size = GNUNET_CONTAINER_multipeermap_size (view);
1704 generate_view_array (view_size);
1706 send_pull_reply (peer, view_array, view_size);
1708 GNUNET_CADET_receive_done (channel);
1714 * Handle PULL REPLY message from another peer.
1716 * Check whether we sent a corresponding request and
1717 * whether this reply is the first one.
1719 * @param cls Closure
1720 * @param channel The channel the PUSH was received over
1721 * @param channel_ctx The context associated with this channel
1722 * @param msg The message header
1725 handle_peer_pull_reply (void *cls,
1726 struct GNUNET_CADET_Channel *channel,
1728 const struct GNUNET_MessageHeader *msg)
1730 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
1731 struct GNUNET_PeerIdentity *peers;
1732 struct PeerContext *peer_ctx;
1733 struct GNUNET_PeerIdentity *sender;
1734 struct PeerContext *sender_ctx;
1735 struct PeerOutstandingOp out_op;
1737 #ifdef ENABLE_MALICIOUS
1738 struct AttackedPeer *tmp_att_peer;
1739 #endif /* ENABLE_MALICIOUS */
1741 /* Check for protocol violation */
1742 if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->size))
1744 GNUNET_break_op (0);
1745 GNUNET_CADET_receive_done (channel);
1746 return GNUNET_SYSERR;
1749 in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
1750 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1751 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1753 LOG (GNUNET_ERROR_TYPE_ERROR,
1754 "message says it sends %" PRIu32 " peers, have space for %i peers\n",
1755 ntohl (in_msg->num_peers),
1756 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1757 sizeof (struct GNUNET_PeerIdentity));
1758 GNUNET_break_op (0);
1759 GNUNET_CADET_receive_done (channel);
1760 return GNUNET_SYSERR;
1763 // Guess simply casting isn't the nicest way...
1764 // FIXME wait for cadet to change this function
1765 sender = (struct GNUNET_PeerIdentity *)
1766 GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
1767 sender_ctx = get_peer_ctx (sender);
1769 LOG (GNUNET_ERROR_TYPE_DEBUG, "PULL REPLY received (%s)\n", GNUNET_i2s (sender));
1771 if (GNUNET_YES != get_peer_flag (sender_ctx, PULL_REPLY_PENDING))
1773 LOG (GNUNET_ERROR_TYPE_WARNING,
1774 "Received a pull reply from a peer we didn't request one from!\n");
1775 GNUNET_break_op (0);
1776 GNUNET_CADET_receive_done (channel);
1781 #ifdef ENABLE_MALICIOUS
1782 // We shouldn't even receive pull replies as we're not sending
1785 #endif /* ENABLE_MALICIOUS */
1787 /* Do actual logic */
1788 peers = (struct GNUNET_PeerIdentity *) &in_msg[1];
1790 LOG (GNUNET_ERROR_TYPE_DEBUG,
1791 "PULL REPLY received, got following %u peers:\n",
1792 ntohl (in_msg->num_peers));
1794 for (i = 0 ; i < ntohl (in_msg->num_peers) ; i++)
1796 LOG (GNUNET_ERROR_TYPE_DEBUG,
1799 GNUNET_i2s (&peers[i]));
1801 #ifdef ENABLE_MALICIOUS
1802 if ((NULL != att_peer_set) &&
1803 (1 == mal_type || 3 == mal_type))
1804 { /* Add attacked peer to local list */
1805 // TODO check if we sent a request and this was the first reply
1806 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1808 && GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mal_peer_set,
1810 && 0 != GNUNET_CRYPTO_cmp_peer_identity (&peers[i],
1813 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1814 tmp_att_peer->peer_id = peers[i];
1815 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1818 add_peer_array_to_set (&peers[i], 1, att_peer_set);
1822 #endif /* ENABLE_MALICIOUS */
1823 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity,
1826 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map,
1828 peer_ctx = create_peer_ctx (&peers[i]);
1830 peer_ctx = get_peer_ctx (&peers[i]);
1832 if (GNUNET_YES == get_peer_flag (peer_ctx, VALID))
1834 if (GNUNET_NO == in_arr (pull_list, pull_list_size, &peers[i]))
1835 GNUNET_array_append (pull_list, pull_list_size, peers[i]);
1837 else if (GNUNET_NO == insert_in_pull_list_scheduled (peer_ctx))
1839 out_op.op = insert_in_pull_list;
1840 out_op.op_cls = NULL;
1841 GNUNET_array_append (peer_ctx->outstanding_ops,
1842 peer_ctx->num_outstanding_ops,
1844 check_peer_live (peer_ctx);
1849 unset_peer_flag (sender_ctx, PULL_REPLY_PENDING);
1851 peer_clean (sender);
1853 GNUNET_CADET_receive_done (channel);
1859 * Compute a random delay.
1860 * A uniformly distributed value between mean + spread and mean - spread.
1862 * For example for mean 4 min and spread 2 the minimum is (4 min - (1/2 * 4 min))
1863 * It would return a random value between 2 and 6 min.
1865 * @param mean the mean
1866 * @param spread the inverse amount of deviation from the mean
1868 static struct GNUNET_TIME_Relative
1869 compute_rand_delay (struct GNUNET_TIME_Relative mean, unsigned int spread)
1871 struct GNUNET_TIME_Relative half_interval;
1872 struct GNUNET_TIME_Relative ret;
1873 unsigned int rand_delay;
1874 unsigned int max_rand_delay;
1878 LOG (GNUNET_ERROR_TYPE_WARNING,
1879 "Not accepting spread of 0\n");
1883 /* Compute random time value between spread * mean and spread * mean */
1884 half_interval = GNUNET_TIME_relative_divide (mean, spread);
1886 max_rand_delay = GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us / mean.rel_value_us * (2/spread);
1888 * Compute random value between (0 and 1) * round_interval
1889 * via multiplying round_interval with a 'fraction' (0 to value)/value
1891 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_rand_delay);
1892 ret = GNUNET_TIME_relative_multiply (mean, rand_delay);
1893 ret = GNUNET_TIME_relative_divide (ret, max_rand_delay);
1894 ret = GNUNET_TIME_relative_add (ret, half_interval);
1896 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ret.rel_value_us)
1897 LOG (GNUNET_ERROR_TYPE_WARNING,
1898 "Returning FOREVER_REL\n");
1905 * Send single pull request
1907 * @param peer_id the peer to send the pull request to.
1910 send_pull_request (struct GNUNET_PeerIdentity *peer_id)
1912 struct GNUNET_MQ_Envelope *ev;
1913 struct GNUNET_MQ_Handle *mq;
1914 struct PeerContext *peer_ctx;
1916 peer_ctx = get_peer_ctx (peer_id);
1917 GNUNET_assert (GNUNET_NO == get_peer_flag (peer_ctx, PULL_REPLY_PENDING));
1918 set_peer_flag (peer_ctx, PULL_REPLY_PENDING);
1920 LOG (GNUNET_ERROR_TYPE_DEBUG,
1921 "Sending PULL request to peer %s of view.\n",
1922 GNUNET_i2s (peer_id));
1924 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
1925 mq = get_mq (peer_id);
1926 GNUNET_MQ_send (mq, ev);
1933 * @param peer_id the peer to send the push to.
1936 send_push (struct GNUNET_PeerIdentity *peer_id)
1938 struct GNUNET_MQ_Envelope *ev;
1939 struct GNUNET_MQ_Handle *mq;
1941 LOG (GNUNET_ERROR_TYPE_DEBUG,
1942 "Sending PUSH to peer %s of view.\n",
1943 GNUNET_i2s (peer_id));
1945 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
1946 mq = get_mq (peer_id);
1947 GNUNET_MQ_send (mq, ev);
1952 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1955 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1958 #ifdef ENABLE_MALICIOUS
1960 * Turn RPS service to act malicious.
1962 * @param cls Closure
1963 * @param client The client that sent the message
1964 * @param msg The message header
1967 handle_client_act_malicious (void *cls,
1968 struct GNUNET_SERVER_Client *client,
1969 const struct GNUNET_MessageHeader *msg)
1971 struct GNUNET_RPS_CS_ActMaliciousMessage *in_msg;
1972 struct GNUNET_PeerIdentity *peers;
1973 uint32_t num_mal_peers_sent;
1974 uint32_t num_mal_peers_old;
1976 /* Check for protocol violation */
1977 if (sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage) > ntohs (msg->size))
1979 GNUNET_break_op (0);
1982 in_msg = (struct GNUNET_RPS_CS_ActMaliciousMessage *) msg;
1983 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
1984 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1986 LOG (GNUNET_ERROR_TYPE_ERROR,
1987 "message says it sends %" PRIu64 " peers, have space for %i peers\n",
1988 ntohl (in_msg->num_peers),
1989 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
1990 sizeof (struct GNUNET_PeerIdentity));
1991 GNUNET_break_op (0);
1995 /* Do actual logic */
1996 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1997 mal_type = ntohl (in_msg->type);
1999 LOG (GNUNET_ERROR_TYPE_DEBUG,
2000 "Now acting malicious type %" PRIu32 ", got %" PRIu32 " peers.\n",
2002 ntohl (in_msg->num_peers));
2005 { /* Try to maximise representation */
2006 /* Add other malicious peers to those we already know */
2008 num_mal_peers_sent = ntohl (in_msg->num_peers);
2009 num_mal_peers_old = num_mal_peers;
2010 GNUNET_array_grow (mal_peers,
2012 num_mal_peers + num_mal_peers_sent);
2013 memcpy (&mal_peers[num_mal_peers_old],
2015 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
2017 /* Add all mal peers to mal_peer_set */
2018 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
2022 /* Substitute do_round () with do_mal_round () */
2023 GNUNET_SCHEDULER_cancel (do_round_task);
2024 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
2027 else if (2 == mal_type
2029 { /* Try to partition the network */
2030 /* Add other malicious peers to those we already know */
2031 struct PeerContext *att_ctx;
2033 num_mal_peers_sent = ntohl (in_msg->num_peers) - 1;
2034 num_mal_peers_old = num_mal_peers;
2035 GNUNET_array_grow (mal_peers,
2037 num_mal_peers + num_mal_peers_sent);
2038 if (NULL != mal_peers &&
2041 memcpy (&mal_peers[num_mal_peers_old],
2043 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
2045 /* Add all mal peers to mal_peer_set */
2046 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
2051 /* Store the one attacked peer */
2052 memcpy (&attacked_peer,
2053 &in_msg->attacked_peer,
2054 sizeof (struct GNUNET_PeerIdentity));
2055 /* Set the flag of the attacked peer to valid to avoid problems */
2056 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map,
2059 att_ctx = create_peer_ctx (&attacked_peer);
2060 check_peer_live (att_ctx);
2063 LOG (GNUNET_ERROR_TYPE_DEBUG,
2064 "Attacked peer is %s\n",
2065 GNUNET_i2s (&attacked_peer));
2067 /* Substitute do_round () with do_mal_round () */
2068 GNUNET_SCHEDULER_cancel (do_round_task);
2069 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
2071 else if (0 == mal_type)
2072 { /* Stop acting malicious */
2073 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
2075 /* Substitute do_mal_round () with do_round () */
2076 GNUNET_SCHEDULER_cancel (do_round_task);
2077 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
2084 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2089 * Send out PUSHes and PULLs maliciously.
2091 * This is executed regylary.
2094 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2096 uint32_t num_pushes;
2098 struct GNUNET_TIME_Relative time_next_round;
2099 struct AttackedPeer *tmp_att_peer;
2100 struct PeerContext *peer_ctx;
2102 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round maliciously type %" PRIu32 ".\n",
2104 do_round_task = NULL;
2105 GNUNET_assert (mal_type <= 3);
2106 /* Do malicious actions */
2108 { /* Try to maximise representation */
2110 /* The maximum of pushes we're going to send this round */
2111 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit,
2112 num_attacked_peers),
2113 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
2115 LOG (GNUNET_ERROR_TYPE_DEBUG,
2116 "Going to send %" PRIu32 " pushes\n",
2119 /* Send PUSHes to attacked peers */
2120 for (i = 0 ; i < num_pushes ; i++)
2122 if (att_peers_tail == att_peer_index)
2123 att_peer_index = att_peers_head;
2125 att_peer_index = att_peer_index->next;
2127 send_push (&att_peer_index->peer_id);
2130 /* Send PULLs to some peers to learn about additional peers to attack */
2131 tmp_att_peer = att_peer_index;
2132 for (i = 0 ; i < num_pushes * alpha ; i++)
2134 if (att_peers_tail == tmp_att_peer)
2135 tmp_att_peer = att_peers_head;
2137 att_peer_index = tmp_att_peer->next;
2139 send_pull_request (&tmp_att_peer->peer_id);
2144 else if (2 == mal_type)
2146 * Try to partition the network
2147 * Send as many pushes to the attacked peer as possible
2148 * That is one push per round as it will ignore more.
2150 peer_ctx = get_peer_ctx (&attacked_peer);
2151 if (GNUNET_YES == get_peer_flag (peer_ctx, VALID))
2152 send_push (&attacked_peer);
2157 { /* Combined attack */
2159 /* Send PUSH to attacked peers */
2160 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map,
2163 peer_ctx = get_peer_ctx (&attacked_peer);
2164 if (GNUNET_YES == get_peer_flag (peer_ctx, VALID))
2166 LOG (GNUNET_ERROR_TYPE_DEBUG,
2167 "Goding to send push to attacked peer (%s)\n",
2168 GNUNET_i2s (&attacked_peer));
2169 send_push (&attacked_peer);
2172 check_peer_live (peer_ctx);
2175 peer_ctx = create_peer_ctx (&attacked_peer);
2176 check_peer_live (peer_ctx);
2178 /* The maximum of pushes we're going to send this round */
2179 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit - 1,
2180 num_attacked_peers),
2181 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
2183 LOG (GNUNET_ERROR_TYPE_DEBUG,
2184 "Going to send %" PRIu32 " pushes\n",
2187 for (i = 0 ; i < num_pushes ; i++)
2189 if (att_peers_tail == att_peer_index)
2190 att_peer_index = att_peers_head;
2192 att_peer_index = att_peer_index->next;
2194 send_push (&att_peer_index->peer_id);
2197 /* Send PULLs to some peers to learn about additional peers to attack */
2198 tmp_att_peer = att_peer_index;
2199 for (i = 0 ; i < num_pushes * alpha ; i++)
2201 if (att_peers_tail == tmp_att_peer)
2202 tmp_att_peer = att_peers_head;
2204 att_peer_index = tmp_att_peer->next;
2206 send_pull_request (&tmp_att_peer->peer_id);
2210 /* Schedule next round */
2211 time_next_round = compute_rand_delay (round_interval, 2);
2213 //do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_mal_round,
2215 GNUNET_assert (NULL == do_round_task);
2216 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_mal_round, NULL);
2217 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
2219 #endif /* ENABLE_MALICIOUS */
2223 * Send out PUSHes and PULLs, possibly update #view, samplers.
2225 * This is executed regylary.
2228 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2230 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round.\n");
2233 unsigned int view_size;
2234 unsigned int *permut;
2235 unsigned int a_peers; /* Number of peers we send pushes to */
2236 unsigned int b_peers; /* Number of peers we send pull requests to */
2237 uint32_t first_border;
2238 uint32_t second_border;
2239 struct GNUNET_PeerIdentity peer;
2240 struct PeerContext *peer_ctx;
2242 do_round_task = NULL;
2243 LOG (GNUNET_ERROR_TYPE_DEBUG,
2244 "Printing view:\n");
2245 to_file (file_name_view_log,
2246 "___ new round ___");
2247 view_size = GNUNET_CONTAINER_multipeermap_size (view);
2248 generate_view_array (view_size);
2249 for (i = 0 ; i < view_size ; i++)
2251 LOG (GNUNET_ERROR_TYPE_DEBUG,
2252 "\t%s\n", GNUNET_i2s (&view_array[i]));
2253 to_file (file_name_view_log,
2255 GNUNET_i2s_full (&view_array[i]));
2259 /* Send pushes and pull requests */
2262 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2263 (unsigned int) view_size);
2266 a_peers = ceil (alpha * view_size);
2268 LOG (GNUNET_ERROR_TYPE_DEBUG,
2269 "Going to send pushes to %u (ceil (%f * %u)) peers.\n",
2270 a_peers, alpha, view_size);
2271 for (i = 0; i < a_peers; i++)
2273 peer = view_array[permut[i]];
2274 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer)) // TODO
2275 { // FIXME if this fails schedule/loop this for later
2280 /* Send PULL requests */
2281 b_peers = ceil (beta * view_size);
2282 first_border = a_peers;
2283 second_border = a_peers + b_peers;
2284 if (second_border > view_size)
2286 first_border = view_size - b_peers;
2287 second_border = view_size;
2289 LOG (GNUNET_ERROR_TYPE_DEBUG,
2290 "Going to send pulls to %u (ceil (%f * %u)) peers.\n",
2291 b_peers, beta, view_size);
2292 for (i = first_border; i < second_border; i++)
2294 peer = view_array[permut[i]];
2295 peer_ctx = get_peer_ctx (&peer);
2296 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer) &&
2297 GNUNET_NO == get_peer_flag (peer_ctx, PULL_REPLY_PENDING)) // TODO
2298 { // FIXME if this fails schedule/loop this for later
2299 send_pull_request (&peer);
2303 GNUNET_free (permut);
2309 /* TODO see how many peers are in push-/pull- list! */
2311 if (push_list_size <= alpha * view_size &&
2312 0 < push_list_size &&
2315 LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the view.\n");
2317 uint32_t final_size;
2318 uint32_t peers_to_clean_size;
2319 struct GNUNET_PeerIdentity *peers_to_clean;
2321 peers_to_clean = NULL;
2322 peers_to_clean_size = 0;
2323 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, view_size);
2324 memcpy (peers_to_clean,
2326 view_size * sizeof (struct GNUNET_PeerIdentity));
2328 /* Seems like recreating is the easiest way of emptying the peermap */
2329 GNUNET_CONTAINER_multipeermap_destroy (view);
2330 view = GNUNET_CONTAINER_multipeermap_create (view_size, GNUNET_NO);
2331 to_file (file_name_view_log,
2334 first_border = GNUNET_MIN (ceil (alpha * sampler_size_est_need),
2336 second_border = first_border +
2337 GNUNET_MIN (floor (beta * sampler_size_est_need),
2339 final_size = second_border +
2340 ceil ((1 - (alpha + beta)) * sampler_size_est_need);
2342 GNUNET_array_grow (view_array, view_size, second_border);
2344 /* Update view with peers received through PUSHes */
2345 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2347 for (i = 0; i < first_border; i++)
2349 view_array[i] = push_list[permut[i]];
2350 GNUNET_CONTAINER_multipeermap_put (view, &push_list[permut[i]], NULL,
2351 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2353 to_file (file_name_view_log,
2355 GNUNET_i2s_full (&view_array[i]));
2356 // TODO change the peer_flags accordingly
2358 GNUNET_free (permut);
2361 /* Update view with peers received through PULLs */
2362 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2364 for (i = first_border; i < second_border; i++)
2366 view_array[i] = pull_list[permut[i - first_border]];
2367 GNUNET_CONTAINER_multipeermap_put (view,
2368 &pull_list[permut[i - first_border]],
2370 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2372 to_file (file_name_view_log,
2374 GNUNET_i2s_full (&view_array[i]));
2375 // TODO change the peer_flags accordingly
2377 GNUNET_free (permut);
2380 /* Update view with peers from history */
2381 RPS_sampler_get_n_rand_peers (prot_sampler,
2384 final_size - second_border);
2385 num_hist_update_tasks = final_size - second_border;
2386 // TODO change the peer_flags accordingly
2388 for (i = 0; i < view_size; i++)
2389 rem_from_list (&peers_to_clean, &peers_to_clean_size, &view_array[i]);
2391 /* Clean peers that were removed from the view */
2392 for (i = 0; i < peers_to_clean_size; i++)
2394 to_file (file_name_view_log,
2396 GNUNET_i2s_full (&peers_to_clean[i]));
2397 peer_clean (&peers_to_clean[i]);
2400 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, 0);
2401 peers_to_clean = NULL;
2405 LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the view.\n");
2407 // TODO independent of that also get some peers from CADET_get_peers()?
2409 LOG (GNUNET_ERROR_TYPE_DEBUG,
2410 "Received %u pushes and %u pulls last round (alpha (%.2f) * view_size (%u) = %.2f)\n",
2417 /* Update samplers */
2418 for (i = 0; i < push_list_size; i++)
2420 LOG (GNUNET_ERROR_TYPE_DEBUG,
2421 "Updating with peer %s from push list\n",
2422 GNUNET_i2s (&push_list[i]));
2423 insert_in_sampler (NULL, &push_list[i]);
2424 peer_clean (&push_list[i]); /* This cleans only if it is not in the view */
2427 for (i = 0; i < pull_list_size; i++)
2429 LOG (GNUNET_ERROR_TYPE_DEBUG,
2430 "Updating with peer %s from pull list\n",
2431 GNUNET_i2s (&pull_list[i]));
2432 insert_in_sampler (NULL, &pull_list[i]);
2433 peer_clean (&pull_list[i]); /* This cleans only if it is not in the view */
2437 /* Empty push/pull lists */
2438 GNUNET_array_grow (push_list, push_list_size, 0);
2439 GNUNET_array_grow (pull_list, pull_list_size, 0);
2441 struct GNUNET_TIME_Relative time_next_round;
2443 time_next_round = compute_rand_delay (round_interval, 2);
2445 /* Schedule next round */
2446 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_round, NULL);
2447 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
2452 rps_start (struct GNUNET_SERVER_Handle *server);
2456 * This is called from GNUNET_CADET_get_peers().
2458 * It is called on every peer(ID) that cadet somehow has contact with.
2459 * We use those to initialise the sampler.
2462 init_peer_cb (void *cls,
2463 const struct GNUNET_PeerIdentity *peer,
2464 int tunnel, // "Do we have a tunnel towards this peer?"
2465 unsigned int n_paths, // "Number of known paths towards this peer"
2466 unsigned int best_path) // "How long is the best path?
2467 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
2471 LOG (GNUNET_ERROR_TYPE_DEBUG,
2472 "Got peer_id %s from cadet\n",
2480 * Iterator over peers from peerinfo.
2482 * @param cls closure
2483 * @param peer id of the peer, NULL for last call
2484 * @param hello hello message for the peer (can be NULL)
2485 * @param error message
2488 process_peerinfo_peers (void *cls,
2489 const struct GNUNET_PeerIdentity *peer,
2490 const struct GNUNET_HELLO_Message *hello,
2491 const char *err_msg)
2495 LOG (GNUNET_ERROR_TYPE_DEBUG,
2496 "Got peer_id %s from peerinfo\n",
2504 * Callback used to remove peers from the multipeermap.
2507 peer_remove_cb (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
2509 struct PeerContext *peer_ctx;
2510 const struct GNUNET_CADET_Channel *channel =
2511 (const struct GNUNET_CADET_Channel *) cls;
2513 peer_ctx = (struct PeerContext *) value;
2514 set_peer_flag (peer_ctx, TO_DESTROY);
2516 LOG (GNUNET_ERROR_TYPE_DEBUG,
2517 "Going to remove peer %s\n",
2518 GNUNET_i2s (&peer_ctx->peer_id));
2520 /* Remove it from the sampler used for the Brahms protocol */
2521 RPS_sampler_reinitialise_by_value (prot_sampler, key);
2523 /* If operations are still scheduled for this peer cancel those */
2524 if (0 != peer_ctx->num_outstanding_ops)
2526 GNUNET_array_grow (peer_ctx->outstanding_ops,
2527 peer_ctx->num_outstanding_ops,
2531 /* If we are still waiting for notification whether this peer is live
2532 * cancel the according task */
2533 if (NULL != peer_ctx->transmit_handle)
2535 LOG (GNUNET_ERROR_TYPE_DEBUG,
2536 "Trying to cancle transmit_handle for peer %s\n",
2538 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->transmit_handle);
2539 peer_ctx->transmit_handle = NULL;
2542 unset_peer_flag (peer_ctx, PULL_REPLY_PENDING);
2544 /* Remove peer from view */
2545 if (GNUNET_CONTAINER_multipeermap_contains (view, key))
2547 to_file (file_name_view_log,
2548 "-%s\t(cleanup channel, other peer)",
2549 GNUNET_i2s_full (key));
2550 GNUNET_CONTAINER_multipeermap_remove_all (view, key);
2551 if (NULL != view_array)
2553 GNUNET_free (view_array);
2558 /* Remove from push and pull lists */
2559 if (GNUNET_YES == in_arr (push_list, push_list_size, key))
2560 rem_from_list (&push_list, &push_list_size, key);
2561 if (GNUNET_YES == in_arr (pull_list, pull_list_size, key))
2562 rem_from_list (&pull_list, &pull_list_size, key);
2564 /* If there is still a mq destroy it */
2565 if (NULL != peer_ctx->mq)
2567 GNUNET_MQ_destroy (peer_ctx->mq);
2568 peer_ctx->mq = NULL;
2572 /* Remove the send_channel
2573 * This function should be called again from #cleanup_channel (callback
2574 * called on the destruction of channels) and clean up the rest. */
2575 if (NULL != peer_ctx->send_channel &&
2576 channel != peer_ctx->send_channel)
2578 GNUNET_CADET_channel_destroy (peer_ctx->send_channel);
2579 peer_ctx->send_channel = NULL;
2582 /* Remove the recv_channel
2583 * This function should be called again from #cleanup_channel (callback
2584 * called on the destruction of channels) and clean up the rest. */
2585 if (NULL != peer_ctx->recv_channel &&
2586 channel != peer_ctx->recv_channel)
2588 GNUNET_CADET_channel_destroy (peer_ctx->recv_channel);
2589 peer_ctx->recv_channel = NULL;
2592 /* If there is no channel we have to remove the context now */
2593 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_remove_all (peer_map, key))
2594 LOG (GNUNET_ERROR_TYPE_WARNING, "removing peer from peer_map failed\n");
2596 GNUNET_free (peer_ctx);
2603 * Clean the send channel of a peer
2604 * If there is also no channel to receive messages from that peer, remove it
2608 peer_clean (const struct GNUNET_PeerIdentity *peer)
2610 struct PeerContext *peer_ctx;
2611 /* struct GNUNET_CADET_Channel *channel; */
2613 if ( (0 == RPS_sampler_count_id (prot_sampler, peer)) &&
2614 (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (view, peer)) &&
2615 (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer)) &&
2616 (GNUNET_NO == in_arr (push_list, push_list_size, peer)) &&
2617 (GNUNET_NO == in_arr (pull_list, pull_list_size, peer)) )
2619 peer_ctx = get_peer_ctx (peer);
2621 if ( (NULL == peer_ctx->recv_channel) &&
2622 (GNUNET_NO == get_peer_flag (peer_ctx, PULL_REPLY_PENDING)) )
2624 #ifdef ENABLE_MALICIOUS
2625 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
2626 peer_remove_cb (NULL, peer, peer_ctx);
2628 peer_remove_cb (NULL, peer, peer_ctx);
2629 #endif /* ENABLE_MALICIOUS */
2636 * Task run during shutdown.
2642 shutdown_task (void *cls,
2643 const struct GNUNET_SCHEDULER_TaskContext *tc)
2646 LOG (GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
2648 GNUNET_PEERINFO_notify_cancel (peerinfo_notify_handle);
2649 GNUNET_PEERINFO_disconnect (peerinfo_handle);
2651 if (NULL != do_round_task)
2653 GNUNET_SCHEDULER_cancel (do_round_task);
2654 do_round_task = NULL;
2658 if (GNUNET_SYSERR ==
2659 GNUNET_CONTAINER_multipeermap_iterate (peer_map, peer_remove_cb, NULL))
2660 LOG (GNUNET_ERROR_TYPE_WARNING,
2661 "Iterating over peers to disconnect from them was cancelled\n");
2664 GNUNET_NSE_disconnect (nse);
2665 RPS_sampler_destroy (prot_sampler);
2666 RPS_sampler_destroy (client_sampler);
2667 LOG (GNUNET_ERROR_TYPE_DEBUG,
2668 "Size of the peermap: %u\n",
2669 GNUNET_CONTAINER_multipeermap_size (peer_map));
2670 GNUNET_break (0 == GNUNET_CONTAINER_multipeermap_size (peer_map));
2671 GNUNET_CADET_disconnect (cadet_handle);
2672 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
2673 GNUNET_CONTAINER_multipeermap_destroy (view);
2675 GNUNET_array_grow (push_list, push_list_size, 0);
2676 GNUNET_array_grow (pull_list, pull_list_size, 0);
2677 #ifdef ENABLE_MALICIOUS
2678 struct AttackedPeer *tmp_att_peer;
2679 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
2680 if (NULL != mal_peer_set)
2681 GNUNET_CONTAINER_multipeermap_destroy (mal_peer_set);
2682 if (NULL != att_peer_set)
2683 GNUNET_CONTAINER_multipeermap_destroy (att_peer_set);
2684 while (NULL != att_peers_head)
2686 tmp_att_peer = att_peers_head;
2687 GNUNET_CONTAINER_DLL_remove (att_peers_head, att_peers_tail, tmp_att_peer);
2689 #endif /* ENABLE_MALICIOUS */
2694 * @brief Get informed about a connecting client.
2697 * @param client the client that connects
2700 handle_client_connect (void *cls,
2701 struct GNUNET_SERVER_Client *client)
2703 struct ClientContext *cli_ctx;
2705 LOG (GNUNET_ERROR_TYPE_DEBUG,
2706 "Client connected\n");
2708 return; /* Server was destroyed before a client connected. Shutting down */
2709 cli_ctx = GNUNET_new (struct ClientContext);
2710 cli_ctx->mq = GNUNET_MQ_queue_for_server_client (client);
2711 GNUNET_SERVER_client_set_user_context (client, cli_ctx);
2712 GNUNET_CONTAINER_DLL_insert (cli_ctx_head,
2718 * A client disconnected. Remove all of its data structure entries.
2720 * @param cls closure, NULL
2721 * @param client identification of the client
2724 handle_client_disconnect (void *cls,
2725 struct GNUNET_SERVER_Client *client)
2727 struct ClientContext *cli_ctx;
2730 {/* shutdown task */
2731 while (NULL != cli_ctx_head)
2732 destroy_cli_ctx (cli_ctx_head);
2736 cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientContext);
2737 destroy_cli_ctx (cli_ctx);
2743 * Handle the channel a peer opens to us.
2745 * @param cls The closure
2746 * @param channel The channel the peer wants to establish
2747 * @param initiator The peer's peer ID
2748 * @param port The port the channel is being established over
2749 * @param options Further options
2752 handle_inbound_channel (void *cls,
2753 struct GNUNET_CADET_Channel *channel,
2754 const struct GNUNET_PeerIdentity *initiator,
2756 enum GNUNET_CADET_ChannelOption options)
2758 struct PeerContext *peer_ctx;
2760 LOG (GNUNET_ERROR_TYPE_DEBUG,
2761 "New channel was established to us (Peer %s).\n",
2762 GNUNET_i2s (initiator));
2763 GNUNET_assert (NULL != channel); /* according to cadet API */
2764 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, initiator))
2765 peer_ctx = create_peer_ctx (initiator);
2767 peer_ctx = get_peer_ctx (initiator);
2768 /* We only accept one incoming channel from peers */
2769 if (NULL != peer_ctx->recv_channel)
2771 GNUNET_CADET_channel_destroy (channel);
2774 peer_ctx->recv_channel = channel;
2775 peer_is_live (peer_ctx);
2781 * This is called when a channel is destroyed.
2783 * @param cls The closure
2784 * @param channel The channel being closed
2785 * @param channel_ctx The context associated with this channel
2788 cleanup_channel (void *cls,
2789 const struct GNUNET_CADET_Channel *channel,
2792 struct GNUNET_PeerIdentity *peer;
2793 struct PeerContext *peer_ctx;
2795 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
2796 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
2797 // Guess simply casting isn't the nicest way...
2798 // FIXME wait for cadet to change this function
2800 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
2801 {/* We don't want to implicitly create a context that we're about to kill */
2802 peer_ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
2803 if (NULL == peer_ctx) /* It could have been removed by shutdown_task */
2806 if (get_peer_flag (peer_ctx, TO_DESTROY))
2807 {/* We initiatad the destruction of this particular peer */
2808 if (channel == peer_ctx->send_channel)
2809 peer_ctx->send_channel = NULL;
2810 else if (channel == peer_ctx->recv_channel)
2811 peer_ctx->recv_channel = NULL;
2813 to_file (file_name_view_log,
2814 "-%s\t(cleanup channel, ourself)",
2815 GNUNET_i2s_full (peer));
2819 { /* We did not initiate the destruction of this peer */
2820 if (channel == peer_ctx->send_channel)
2821 { /* Something (but us) killd the channel - clean up peer */
2822 LOG (GNUNET_ERROR_TYPE_DEBUG,
2823 "send channel (%s) was destroyed - cleaning up\n",
2825 peer_ctx->send_channel = NULL;
2826 /* Somwewhat {ab,re}use the iterator function */
2827 /* Cast to void is ok, because it's used as void in peer_remove_cb */
2828 (void) peer_remove_cb ((void *) channel, peer, peer_ctx);
2830 else if (channel == peer_ctx->recv_channel)
2831 { /* Other peer doesn't want to send us messages anymore */
2832 LOG (GNUNET_ERROR_TYPE_DEBUG,
2833 "Peer %s destroyed recv channel - cleaning up channel\n",
2835 peer_ctx->recv_channel = NULL;
2839 LOG (GNUNET_ERROR_TYPE_WARNING,
2840 "unknown channel (%s) was destroyed\n",
2847 { /* We don't know a context to that peer */
2848 LOG (GNUNET_ERROR_TYPE_DEBUG,
2849 "channel (%s) without associated context was destroyed\n",
2856 * Actually start the service.
2859 rps_start (struct GNUNET_SERVER_Handle *server)
2861 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
2862 {&handle_client_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
2863 sizeof (struct GNUNET_RPS_CS_RequestMessage)},
2864 {&handle_client_request_cancel, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST_CANCEL,
2865 sizeof (struct GNUNET_RPS_CS_RequestCancelMessage)},
2866 {&handle_client_seed, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_SEED, 0},
2867 #ifdef ENABLE_MALICIOUS
2868 {&handle_client_act_malicious, NULL, GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS , 0},
2869 #endif /* ENABLE_MALICIOUS */
2873 GNUNET_SERVER_add_handlers (server, handlers);
2874 GNUNET_SERVER_connect_notify (server,
2875 &handle_client_connect,
2877 GNUNET_SERVER_disconnect_notify (server,
2878 &handle_client_disconnect,
2880 LOG (GNUNET_ERROR_TYPE_INFO, "Ready to receive requests from clients\n");
2883 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
2884 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
2886 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2893 * Process statistics requests.
2895 * @param cls closure
2896 * @param server the initialized server
2897 * @param c configuration to use
2901 struct GNUNET_SERVER_Handle *server,
2902 const struct GNUNET_CONFIGURATION_Handle *c)
2907 // TODO check what this does -- copied from gnunet-boss
2908 // - seems to work as expected
2909 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
2914 GNUNET_CRYPTO_get_peer_identity (cfg, &own_identity); // TODO check return value
2915 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2916 "STARTING SERVICE (rps) for peer [%s]\n",
2917 GNUNET_i2s (&own_identity));
2918 #ifdef ENABLE_MALICIOUS
2919 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2920 "Malicious execution compiled in.\n");
2921 #endif /* ENABLE_MALICIOUS */
2925 /* Get time interval from the configuration */
2926 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
2930 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2931 "RPS", "ROUNDINTERVAL");
2932 GNUNET_SCHEDULER_shutdown ();
2936 /* Get initial size of sampler/view from the configuration */
2938 GNUNET_CONFIGURATION_get_value_number (cfg, "RPS", "INITSIZE",
2939 (long long unsigned int *) &sampler_size_est_need))
2941 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2943 GNUNET_SCHEDULER_shutdown ();
2946 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", sampler_size_est_need);
2949 view = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO);
2951 /* file_name_view_log */
2952 if (GNUNET_OK != GNUNET_DISK_directory_create ("/tmp/rps/"))
2954 LOG (GNUNET_ERROR_TYPE_WARNING,
2955 "Failed to create directory /tmp/rps/\n");
2958 size = (14 + strlen (GNUNET_i2s_full (&own_identity)) + 1) * sizeof (char);
2959 file_name_view_log = GNUNET_malloc (size);
2960 out_size = GNUNET_snprintf (file_name_view_log,
2963 GNUNET_i2s_full (&own_identity));
2964 if (size < out_size ||
2967 LOG (GNUNET_ERROR_TYPE_WARNING,
2968 "Failed to write string to buffer (size: %i, out_size: %i)\n",
2974 /* connect to NSE */
2975 nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
2981 peer_map = GNUNET_CONTAINER_multipeermap_create (sampler_size_est_need, GNUNET_NO);
2984 /* Initialise cadet */
2985 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
2986 {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH ,
2987 sizeof (struct GNUNET_MessageHeader)},
2988 {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
2989 sizeof (struct GNUNET_MessageHeader)},
2990 {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY , 0},
2994 const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
2995 cadet_handle = GNUNET_CADET_connect (cfg,
2997 &handle_inbound_channel,
3002 peerinfo_handle = GNUNET_PEERINFO_connect (cfg);
3004 /* Initialise sampler */
3005 struct GNUNET_TIME_Relative half_round_interval;
3006 struct GNUNET_TIME_Relative max_round_interval;
3008 half_round_interval = GNUNET_TIME_relative_multiply (round_interval, .5);
3009 max_round_interval = GNUNET_TIME_relative_add (round_interval, half_round_interval);
3011 prot_sampler = RPS_sampler_init (sampler_size_est_need, max_round_interval);
3012 client_sampler = RPS_sampler_mod_init (sampler_size_est_need, max_round_interval);
3014 /* Initialise push and pull maps */
3021 num_hist_update_tasks = 0;
3024 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
3025 GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, NULL);
3026 // TODO send push/pull to each of those peers?
3028 peerinfo_notify_handle = GNUNET_PEERINFO_notify (cfg,
3030 process_peerinfo_peers,
3038 * The main function for the rps service.
3040 * @param argc number of arguments from the command line
3041 * @param argv command line arguments
3042 * @return 0 ok, 1 on error
3045 main (int argc, char *const *argv)
3047 return (GNUNET_OK ==
3048 GNUNET_SERVICE_run (argc,
3051 GNUNET_SERVICE_OPTION_NONE,
3052 &run, NULL)) ? 0 : 1;
3055 /* end of gnunet-service-rps.c */