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 * List containing all messages that are yet to be send
188 struct PendingMessage
193 struct PendingMessage *next;
194 struct PendingMessage *prev;
197 * The envelope to the corresponding message
199 struct GNUNET_MQ_Envelope *ev;
202 * The corresponding context
204 struct PeerContext *peer_ctx;
214 * Struct used to keep track of other peer's status
216 * This is stored in a multipeermap.
221 * Message queue open to client
223 struct GNUNET_MQ_Handle *mq;
226 * Channel open to client.
228 struct GNUNET_CADET_Channel *send_channel;
231 * Channel open from client.
233 struct GNUNET_CADET_Channel *recv_channel; // unneeded?
236 * Array of outstanding operations on this peer.
238 struct PeerOutstandingOp *outstanding_ops;
241 * Handle to the callback given to cadet_ntfy_tmt_rdy()
243 * To be canceled on shutdown.
245 struct GNUNET_CADET_TransmitHandle *transmit_handle;
248 * Number of outstanding operations.
250 unsigned int num_outstanding_ops;
253 * Identity of the peer
255 struct GNUNET_PeerIdentity peer_id;
258 * Flags indicating status of peer
263 * DLL with all messages that are yet to be sent
265 struct PendingMessage *pending_messages_head;
266 struct PendingMessage *pending_messages_tail;
269 * This is pobably followed by 'statistical' data (when we first saw
270 * him, how did we get his ID, how many pushes (in a timeinterval),
275 /***********************************************************************
276 * /Housekeeping with peers
277 ***********************************************************************/
283 /***********************************************************************
285 ***********************************************************************/
288 * Sampler used for the Brahms protocol itself.
290 static struct RPS_Sampler *prot_sampler;
293 * Sampler used for the clients.
295 static struct RPS_Sampler *client_sampler;
298 * Set of all peers to keep track of them.
300 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
304 * Name to log view to
306 static char *file_name_view_log;
309 * The "local view" containing peers we learned from gossip and history
311 static struct GNUNET_CONTAINER_MultiPeerMap *view;
314 * An array containing the peers of the local view.
316 * This is created every time we send a pull reply if it has changed since the
317 * last pull reply we sent.
319 static struct GNUNET_PeerIdentity *view_array;
323 * The size of sampler we need to be able to satisfy the client's need of
326 static unsigned int sampler_size_client_need;
329 * The size of sampler we need to be able to satisfy the Brahms protocol's
330 * need of random peers.
332 * This is one minimum size the sampler grows to.
334 static unsigned int sampler_size_est_need;
338 * Percentage of total peer number in the view
339 * to send random PUSHes to
344 * Percentage of total peer number in the view
345 * to send random PULLs to
350 * The percentage gamma of history updates.
351 * Simply 1 - alpha - beta
356 * Identifier for the main task that runs periodically.
358 static struct GNUNET_SCHEDULER_Task *do_round_task;
361 * Time inverval the do_round task runs in.
363 static struct GNUNET_TIME_Relative round_interval;
368 * List to store peers received through pushes temporary.
370 * TODO -> multipeermap
372 static struct GNUNET_PeerIdentity *push_list;
375 * Size of the push_list;
377 static unsigned int push_list_size;
378 //size_t push_list_size;
381 * List to store peers received through pulls temporary.
383 * TODO -> multipeermap
385 static struct GNUNET_PeerIdentity *pull_list;
388 * Size of the pull_list;
390 static unsigned int pull_list_size;
391 //size_t pull_list_size;
397 static struct GNUNET_NSE_Handle *nse;
402 static struct GNUNET_CADET_Handle *cadet_handle;
405 * Handler to PEERINFO.
407 static struct GNUNET_PEERINFO_Handle *peerinfo_handle;
410 * Handle for cancellation of iteration over peers.
412 struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify_handle;
418 * Only needed in the beginning to check how many of the 64 deltas
421 static unsigned int req_counter;
424 * Time of the last request we received.
426 * Used to compute the expected request rate.
428 static struct GNUNET_TIME_Absolute last_request;
431 * Size of #request_deltas.
433 #define REQUEST_DELTAS_SIZE 64
434 static unsigned int request_deltas_size = REQUEST_DELTAS_SIZE;
437 * Last 64 deltas between requests
439 static struct GNUNET_TIME_Relative request_deltas[REQUEST_DELTAS_SIZE];
442 * The prediction of the rate of requests
444 static struct GNUNET_TIME_Relative request_rate;
448 * Number of history update tasks.
450 uint32_t num_hist_update_tasks;
453 #ifdef ENABLE_MALICIOUS
455 * Type of malicious peer
457 * 0 Don't act malicious at all - Default
458 * 1 Try to maximise representation
459 * 2 Try to partition the network
462 uint32_t mal_type = 0;
465 * Other malicious peers
467 static struct GNUNET_PeerIdentity *mal_peers = NULL;
470 * Hashmap of malicious peers used as set.
471 * Used to more efficiently check whether we know that peer.
473 static struct GNUNET_CONTAINER_MultiPeerMap *mal_peer_set = NULL;
476 * Number of other malicious peers
478 static uint32_t num_mal_peers;
482 * If type is 2 This struct is used to store the attacked peers in a DLL
489 struct AttackedPeer *next;
490 struct AttackedPeer *prev;
495 struct GNUNET_PeerIdentity peer_id;
499 * If type is 2 this is the DLL of attacked peers
501 static struct AttackedPeer *att_peers_head = NULL;
502 static struct AttackedPeer *att_peers_tail = NULL;
505 * This index is used to point to an attacked peer to
506 * implement the round-robin-ish way to select attacked peers.
508 static struct AttackedPeer *att_peer_index = NULL;
511 * Hashmap of attacked peers used as set.
512 * Used to more efficiently check whether we know that peer.
514 static struct GNUNET_CONTAINER_MultiPeerMap *att_peer_set = NULL;
517 * Number of attacked peers
519 static uint32_t num_attacked_peers = 0;
523 * If type is 1 this is the attacked peer
525 static struct GNUNET_PeerIdentity attacked_peer;
528 * The limit of PUSHes we can send in one round.
529 * This is an assumption of the Brahms protocol and either implemented
532 * assumend to be the bandwidth limitation.
534 static uint32_t push_limit = 10000;
535 #endif /* ENABLE_MALICIOUS */
538 /***********************************************************************
540 ***********************************************************************/
547 /***********************************************************************
549 ***********************************************************************/
552 * Set a peer flag of given peer context.
554 #define set_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags |= mask)
557 * Get peer flag of given peer context.
559 #define get_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags & mask ? GNUNET_YES : GNUNET_NO)
562 * Unset flag of given peer context.
564 #define unset_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags &= (~mask))
567 * Clean the send channel of a peer
570 peer_clean (const struct GNUNET_PeerIdentity *peer);
574 * Check if peer is already in peer array.
577 in_arr (const struct GNUNET_PeerIdentity *array,
578 unsigned int arr_size,
579 const struct GNUNET_PeerIdentity *peer)
581 GNUNET_assert (NULL != peer);
586 GNUNET_assert (NULL != array);
590 for (i = 0; i < arr_size ; i++)
591 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&array[i], peer))
598 * Print peerlist to log.
601 print_peer_list (struct GNUNET_PeerIdentity *list, unsigned int len)
605 LOG (GNUNET_ERROR_TYPE_DEBUG,
606 "Printing peer list of length %u at %p:\n",
609 for (i = 0 ; i < len ; i++)
611 LOG (GNUNET_ERROR_TYPE_DEBUG,
613 i, GNUNET_i2s (&list[i]));
619 * Remove peer from list.
622 rem_from_list (struct GNUNET_PeerIdentity **peer_list,
623 unsigned int *list_size,
624 const struct GNUNET_PeerIdentity *peer)
627 struct GNUNET_PeerIdentity *tmp;
631 LOG (GNUNET_ERROR_TYPE_DEBUG,
632 "Removing peer %s from list at %p\n",
636 for ( i = 0 ; i < *list_size ; i++ )
638 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&tmp[i], peer))
640 if (i < *list_size -1)
641 { /* Not at the last entry -- shift peers left */
642 memcpy (&tmp[i], &tmp[i +1],
643 ((*list_size) - i -1) * sizeof (struct GNUNET_PeerIdentity));
645 /* Remove last entry (should be now useless PeerID) */
646 GNUNET_array_grow (tmp, *list_size, (*list_size) -1);
653 * Get random peer from the given list but don't return one from the @a ignore_list.
655 struct GNUNET_PeerIdentity *
656 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list,
658 const struct GNUNET_PeerIdentity *ignore_list,
659 uint32_t ignore_size)
663 struct GNUNET_PeerIdentity *tmp_peer_list;
664 struct GNUNET_PeerIdentity *peer;
666 GNUNET_assert (NULL != peer_list);
671 tmp_peer_list = NULL;
672 GNUNET_array_grow (tmp_peer_list, tmp_size, list_size);
673 memcpy (tmp_peer_list,
675 list_size * sizeof (struct GNUNET_PeerIdentity));
676 peer = GNUNET_new (struct GNUNET_PeerIdentity);
679 * Choose the r_index of the peer we want to return
680 * at random from the interval of the view
682 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
684 *peer = tmp_peer_list[r_index];
686 while (in_arr (ignore_list, ignore_size, peer))
688 rem_from_list (&tmp_peer_list, &tmp_size, peer);
690 print_peer_list (tmp_peer_list, tmp_size);
699 * Choose the r_index of the peer we want to return
700 * at random from the interval of the view
702 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
704 *peer = tmp_peer_list[r_index];
708 GNUNET_array_grow (tmp_peer_list, tmp_size, 0);
715 * Get the context of a peer. If not existing, create.
718 get_peer_ctx (const struct GNUNET_PeerIdentity *peer)
720 struct PeerContext *ctx;
723 ret = GNUNET_CONTAINER_multipeermap_contains (peer_map, peer);
724 GNUNET_assert (GNUNET_YES == ret);
725 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
726 GNUNET_assert (NULL != ctx);
731 * Create a new peer context and insert it into the peer map
734 create_peer_ctx (const struct GNUNET_PeerIdentity *peer)
736 struct PeerContext *ctx;
739 ctx = GNUNET_new (struct PeerContext);
740 ctx->peer_id = *peer;
741 ret = GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
742 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
743 GNUNET_assert (GNUNET_OK == ret);
749 * Set the peer flag to living and call the outstanding operations on this peer.
752 peer_is_live (struct PeerContext *peer_ctx)
754 struct GNUNET_PeerIdentity *peer;
756 /* Cancle transmit_handle if still scheduled */
757 if (NULL != peer_ctx->transmit_handle)
759 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->transmit_handle);
760 peer_ctx->transmit_handle = NULL;
763 peer = &peer_ctx->peer_id;
764 set_peer_flag (peer_ctx, VALID);
766 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer %s is live\n", GNUNET_i2s (peer));
768 if (0 < peer_ctx->num_outstanding_ops)
769 { /* Call outstanding operations */
772 for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
773 peer_ctx->outstanding_ops[i].op (peer_ctx->outstanding_ops[i].op_cls, peer);
774 GNUNET_array_grow (peer_ctx->outstanding_ops, peer_ctx->num_outstanding_ops, 0);
782 * Callback that is called when a channel was effectively established.
783 * This is given to ntfy_tmt_rdy and called when the channel was
784 * successfully established.
787 cadet_ntfy_tmt_rdy_cb (void *cls, size_t size, void *buf)
789 struct PeerContext *peer_ctx = (struct PeerContext *) cls;
791 peer_ctx->transmit_handle = NULL;
792 LOG (GNUNET_ERROR_TYPE_DEBUG,
793 "Set ->transmit_handle = NULL for peer %s\n",
794 GNUNET_i2s (&peer_ctx->peer_id));
799 peer_is_live (peer_ctx);
803 LOG (GNUNET_ERROR_TYPE_WARNING,
804 "Problems establishing a connection to peer %s in order to check liveliness\n",
805 GNUNET_i2s (&peer_ctx->peer_id));
806 // TODO reschedule? cleanup?
809 //if (NULL != peer_ctx->transmit_handle)
811 // LOG (GNUNET_ERROR_TYPE_DEBUG,
812 // "Trying to cancle transmit_handle for peer %s\n",
813 // GNUNET_i2s (&peer_ctx->peer_id));
814 // GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->transmit_handle);
815 // peer_ctx->transmit_handle = NULL;
823 * Get the channel of a peer. If not existing, create.
825 struct GNUNET_CADET_Channel *
826 get_channel (const struct GNUNET_PeerIdentity *peer)
828 struct PeerContext *peer_ctx;
830 peer_ctx = get_peer_ctx (peer);
831 if (NULL == peer_ctx->send_channel)
833 LOG (GNUNET_ERROR_TYPE_DEBUG,
834 "Trying to establish channel to peer %s\n",
837 peer_ctx->send_channel =
838 GNUNET_CADET_channel_create (cadet_handle,
841 GNUNET_RPS_CADET_PORT,
842 GNUNET_CADET_OPTION_RELIABLE);
845 return peer_ctx->send_channel;
850 * Get the message queue of a specific peer.
852 * If we already have a message queue open to this client,
853 * simply return it, otherways create one.
855 struct GNUNET_MQ_Handle *
856 get_mq (const struct GNUNET_PeerIdentity *peer_id)
858 struct PeerContext *peer_ctx;
860 peer_ctx = get_peer_ctx (peer_id);
862 GNUNET_assert (NULL == peer_ctx->transmit_handle);
864 if (NULL == peer_ctx->mq)
866 (void) get_channel (peer_id);
867 peer_ctx->mq = GNUNET_CADET_mq_create (peer_ctx->send_channel);
868 //do I have to explicitly put it in the peer_map?
869 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer_id, peer_ctx,
870 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
877 * Issue check whether peer is live
879 * @param peer_ctx the context of the peer
882 check_peer_live (struct PeerContext *peer_ctx)
884 LOG (GNUNET_ERROR_TYPE_DEBUG,
885 "Get informed about peer %s getting live\n",
886 GNUNET_i2s (&peer_ctx->peer_id));
888 if (NULL == peer_ctx->transmit_handle &&
889 NULL == peer_ctx->send_channel)
891 (void) get_channel (&peer_ctx->peer_id);
892 peer_ctx->transmit_handle =
893 GNUNET_CADET_notify_transmit_ready (peer_ctx->send_channel,
895 GNUNET_TIME_UNIT_FOREVER_REL,
896 sizeof (struct GNUNET_MessageHeader),
897 cadet_ntfy_tmt_rdy_cb,
900 else if (NULL != peer_ctx->transmit_handle)
901 LOG (GNUNET_ERROR_TYPE_DEBUG,
902 "Already waiting for notification\n");
903 else if (NULL != peer_ctx->send_channel)
904 LOG (GNUNET_ERROR_TYPE_DEBUG,
905 "Already have established channel to peer\n");
910 * Sum all time relatives of an array.
912 struct GNUNET_TIME_Relative
913 T_relative_sum (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
915 struct GNUNET_TIME_Relative sum;
918 sum = GNUNET_TIME_UNIT_ZERO;
919 for ( i = 0 ; i < arr_size ; i++ )
921 sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
928 * Compute the average of given time relatives.
930 struct GNUNET_TIME_Relative
931 T_relative_avg (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
933 return GNUNET_TIME_relative_divide (T_relative_sum (rel_array, arr_size), arr_size);
938 * Insert PeerID in #pull_list
940 * Called once we know a peer is live.
943 insert_in_pull_list (void *cls, const struct GNUNET_PeerIdentity *peer)
945 if (GNUNET_NO == in_arr (pull_list, pull_list_size, peer))
946 GNUNET_array_append (pull_list, pull_list_size, *peer);
950 * Check whether #insert_in_pull_list was already scheduled
953 insert_in_pull_list_scheduled (const struct PeerContext *peer_ctx)
957 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
958 if (insert_in_pull_list == peer_ctx->outstanding_ops[i].op)
965 * Insert PeerID in #view
967 * Called once we know a peer is live.
970 insert_in_view (void *cls, const struct GNUNET_PeerIdentity *peer)
972 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (view, peer))
974 LOG (GNUNET_ERROR_TYPE_DEBUG,
975 "Going to put %s into view\n",
977 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_put (view,
980 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
982 LOG (GNUNET_ERROR_TYPE_WARNING,
983 "Failed to put peer into view. (insert_in_view)\n");
985 if (NULL != view_array)
987 GNUNET_free (view_array);
990 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
991 create_peer_ctx (peer);
992 (void) get_channel (peer);
996 * Check whether #insert_in_view was already scheduled
999 insert_in_view_scheduled (const struct PeerContext *peer_ctx)
1003 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
1004 if (insert_in_view == peer_ctx->outstanding_ops[i].op)
1011 * Update sampler with given PeerID.
1014 insert_in_sampler (void *cls, const struct GNUNET_PeerIdentity *peer)
1016 LOG (GNUNET_ERROR_TYPE_DEBUG,
1017 "Updating samplers with peer %s from insert_in_sampler()\n",
1019 RPS_sampler_update (prot_sampler, peer);
1020 RPS_sampler_update (client_sampler, peer);
1021 if (0 < RPS_sampler_count_id (prot_sampler, peer))
1023 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
1024 (void) create_peer_ctx (peer);
1025 (void) get_channel (peer);
1031 * Check whether #insert_in_sampler was already scheduled
1034 insert_in_sampler_scheduled (const struct PeerContext *peer_ctx)
1038 for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
1039 if (insert_in_sampler== peer_ctx->outstanding_ops[i].op)
1045 * Put random peer from sampler into the view as history update.
1048 hist_update (void *cls, struct GNUNET_PeerIdentity *ids, uint32_t num_peers)
1052 for (i = 0; i < GNUNET_MIN (
1053 sampler_size_est_need - GNUNET_CONTAINER_multipeermap_size (view),
1056 insert_in_view (NULL, &ids[i]);
1057 to_file (file_name_view_log,
1059 GNUNET_i2s_full (ids));
1061 if (0 < num_hist_update_tasks)
1062 num_hist_update_tasks--;
1068 * Wrapper around #RPS_sampler_resize()
1070 * If we do not have enough sampler elements, double current sampler size
1071 * If we have more than enough sampler elements, halv current sampler size
1074 resize_wrapper (struct RPS_Sampler *sampler, uint32_t new_size)
1076 unsigned int sampler_size;
1079 // TODO respect the min, max
1080 sampler_size = RPS_sampler_get_size (sampler);
1081 if (sampler_size > new_size * 4)
1083 RPS_sampler_resize (sampler, sampler_size / 2);
1085 else if (sampler_size < new_size)
1087 RPS_sampler_resize (sampler, sampler_size * 2);
1089 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
1094 * Wrapper around #RPS_sampler_resize() resizing the client sampler
1097 client_resize_wrapper ()
1099 uint32_t bigger_size;
1103 bigger_size = GNUNET_MAX (sampler_size_est_need, sampler_size_client_need);
1105 // TODO respect the min, max
1106 resize_wrapper (client_sampler, bigger_size);
1107 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size_client is now %" PRIu32 "\n",
1113 * Estimate request rate
1115 * Called every time we receive a request from the client.
1120 struct GNUNET_TIME_Relative max_round_duration;
1122 if (request_deltas_size > req_counter)
1124 if ( 1 < req_counter)
1126 /* Shift last request deltas to the right */
1127 memcpy (&request_deltas[1],
1129 (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
1131 /* Add current delta to beginning */
1133 GNUNET_TIME_absolute_get_difference (last_request,
1134 GNUNET_TIME_absolute_get ());
1135 request_rate = T_relative_avg (request_deltas, req_counter);
1137 /* Compute the duration a round will maximally take */
1138 max_round_duration =
1139 GNUNET_TIME_relative_add (round_interval,
1140 GNUNET_TIME_relative_divide (round_interval, 2));
1142 /* Set the estimated size the sampler has to have to
1143 * satisfy the current client request rate */
1144 sampler_size_client_need =
1145 max_round_duration.rel_value_us / request_rate.rel_value_us;
1147 /* Resize the sampler */
1148 client_resize_wrapper ();
1150 last_request = GNUNET_TIME_absolute_get ();
1155 * Add all peers in @a peer_array to @a peer_map used as set.
1157 * @param peer_array array containing the peers
1158 * @param num_peers number of peers in @peer_array
1159 * @param peer_map the peermap to use as set
1162 add_peer_array_to_set (const struct GNUNET_PeerIdentity *peer_array,
1163 unsigned int num_peers,
1164 struct GNUNET_CONTAINER_MultiPeerMap *peer_map)
1167 if (NULL == peer_map)
1169 LOG (GNUNET_ERROR_TYPE_WARNING,
1170 "Trying to add peers to non-existing peermap.\n");
1174 for (i = 0; i < num_peers; i++)
1176 GNUNET_CONTAINER_multipeermap_put (peer_map,
1179 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1185 * @brief Add an envelope to a message passed to mq to list of pending messages
1187 * @param peer peer the message was sent to
1188 * @param ev envelope to the message
1189 * @param type type of the message to be sent
1191 static struct PendingMessage *
1192 insert_pending_message (const struct GNUNET_PeerIdentity *peer,
1193 struct GNUNET_MQ_Envelope *ev,
1196 struct PendingMessage *pending_msg;
1197 struct PeerContext *peer_ctx;
1199 peer_ctx = get_peer_ctx (peer);
1200 pending_msg = GNUNET_new (struct PendingMessage);
1201 pending_msg->ev = ev;
1202 pending_msg->peer_ctx = peer_ctx;
1203 pending_msg->type = type;
1204 GNUNET_CONTAINER_DLL_insert (peer_ctx->pending_messages_head,
1205 peer_ctx->pending_messages_tail,
1211 remove_pending_message (struct PendingMessage *pending_msg)
1213 struct PeerContext *peer_ctx;
1215 peer_ctx = pending_msg->peer_ctx;
1216 GNUNET_CONTAINER_DLL_remove (peer_ctx->pending_messages_head,
1217 peer_ctx->pending_messages_tail,
1219 GNUNET_free (pending_msg);
1224 * @brief This is called once a message is sent.
1226 * @param cls type of the message that was sent
1229 mq_notify_sent_cb (void *cls)
1231 struct PendingMessage *pending_msg = (struct PendingMessage *) cls;
1232 LOG (GNUNET_ERROR_TYPE_DEBUG,
1235 remove_pending_message (pending_msg);
1240 * Send a PULL REPLY to @a peer_id
1242 * @param peer_id the peer to send the reply to.
1243 * @param peer_ids the peers to send to @a peer_id
1244 * @param num_peer_ids the number of peers to send to @a peer_id
1247 send_pull_reply (const struct GNUNET_PeerIdentity *peer_id,
1248 const struct GNUNET_PeerIdentity *peer_ids,
1249 unsigned int num_peer_ids)
1252 struct GNUNET_MQ_Handle *mq;
1253 struct GNUNET_MQ_Envelope *ev;
1254 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
1255 struct PendingMessage *pending_msg;
1257 /* Compute actual size */
1258 send_size = sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) +
1259 num_peer_ids * sizeof (struct GNUNET_PeerIdentity);
1261 if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size)
1262 /* Compute number of peers to send
1263 * If too long, simply truncate */
1264 // TODO select random ones via permutation
1265 // or even better: do good protocol design
1267 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE -
1268 sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1269 sizeof (struct GNUNET_PeerIdentity);
1271 send_size = num_peer_ids;
1273 LOG (GNUNET_ERROR_TYPE_DEBUG,
1274 "Going to send PULL REPLY with %u peers to %s\n",
1275 send_size, GNUNET_i2s (peer_id));
1277 mq = get_mq (peer_id);
1279 ev = GNUNET_MQ_msg_extra (out_msg,
1280 send_size * sizeof (struct GNUNET_PeerIdentity),
1281 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
1282 out_msg->num_peers = htonl (send_size);
1283 memcpy (&out_msg[1], peer_ids,
1284 send_size * sizeof (struct GNUNET_PeerIdentity));
1286 pending_msg = insert_pending_message (peer_id, ev, "PULL REPLY");
1287 GNUNET_MQ_notify_sent (ev,
1290 GNUNET_MQ_send (mq, ev);
1295 * This function is called on new peer_ids from 'external' sources
1296 * (client seed, cadet get_peers(), ...)
1298 * @param peer_id the new peer_id
1301 new_peer_id (const struct GNUNET_PeerIdentity *peer_id)
1303 struct PeerOutstandingOp out_op;
1304 struct PeerContext *peer_ctx;
1306 if ((NULL == peer_id) ||
1307 (0 == GNUNET_CRYPTO_cmp_peer_identity (&own_identity, peer_id)))
1309 LOG (GNUNET_ERROR_TYPE_DEBUG,
1310 "Got peer_id %s (at %p, view size: %u)\n",
1311 GNUNET_i2s (peer_id),
1313 GNUNET_CONTAINER_multipeermap_size (view));
1315 /* if the seed peer is already know, skip context creation */
1316 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer_id))
1317 peer_ctx = create_peer_ctx (peer_id);
1319 peer_ctx = get_peer_ctx (peer_id);
1321 if (GNUNET_NO == get_peer_flag (peer_ctx, VALID))
1323 if (GNUNET_NO == insert_in_sampler_scheduled (peer_ctx))
1325 out_op.op = insert_in_sampler;
1326 out_op.op_cls = NULL;
1327 GNUNET_array_append (peer_ctx->outstanding_ops,
1328 peer_ctx->num_outstanding_ops,
1332 if (GNUNET_NO == insert_in_view_scheduled (peer_ctx))
1334 out_op.op = insert_in_view;
1335 out_op.op_cls = NULL;
1336 GNUNET_array_append (peer_ctx->outstanding_ops,
1337 peer_ctx->num_outstanding_ops,
1341 /* Trigger livelyness test on peer */
1342 check_peer_live (peer_ctx);
1346 // send push/pull to each of those peers?
1350 /***********************************************************************
1352 ***********************************************************************/
1355 destroy_reply_cls (struct ReplyCls *rep_cls)
1357 struct ClientContext *cli_ctx;
1359 cli_ctx = GNUNET_SERVER_client_get_user_context (rep_cls->client,
1360 struct ClientContext);
1361 GNUNET_assert (NULL != cli_ctx);
1362 GNUNET_CONTAINER_DLL_remove (cli_ctx->rep_cls_head,
1363 cli_ctx->rep_cls_tail,
1365 GNUNET_free (rep_cls);
1369 destroy_cli_ctx (struct ClientContext *cli_ctx)
1371 GNUNET_assert (NULL != cli_ctx);
1372 if (NULL != cli_ctx->rep_cls_head)
1374 LOG (GNUNET_ERROR_TYPE_WARNING,
1375 "Trying to destroy the context of a client that still has pending requests. Going to clean those\n");
1376 while (NULL != cli_ctx->rep_cls_head)
1377 destroy_reply_cls (cli_ctx->rep_cls_head);
1379 GNUNET_CONTAINER_DLL_remove (cli_ctx_head,
1382 GNUNET_free (cli_ctx);
1387 * Function called by NSE.
1389 * Updates sizes of sampler list and view and adapt those lists
1393 nse_callback (void *cls, struct GNUNET_TIME_Absolute timestamp,
1394 double logestimate, double std_dev)
1397 //double scale; // TODO this might go gloabal/config
1399 LOG (GNUNET_ERROR_TYPE_DEBUG,
1400 "Received a ns estimate - logest: %f, std_dev: %f (old_size: %u)\n",
1401 logestimate, std_dev, RPS_sampler_get_size (prot_sampler));
1403 estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
1404 // GNUNET_NSE_log_estimate_to_n (logestimate);
1405 estimate = pow (estimate, 1.0 / 3);
1406 // TODO add if std_dev is a number
1407 // estimate += (std_dev * scale);
1408 if (2 < ceil (estimate))
1410 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
1411 sampler_size_est_need = estimate;
1413 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
1415 /* If the NSE has changed adapt the lists accordingly */
1416 resize_wrapper (prot_sampler, sampler_size_est_need);
1417 client_resize_wrapper ();
1422 * Callback called once the requested PeerIDs are ready.
1424 * Sends those to the requesting client.
1426 void client_respond (void *cls,
1427 struct GNUNET_PeerIdentity *peer_ids, uint32_t num_peers)
1430 struct GNUNET_MQ_Envelope *ev;
1431 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
1432 struct ReplyCls *reply_cls = (struct ReplyCls *) cls;
1433 uint32_t size_needed;
1434 struct ClientContext *cli_ctx;
1436 GNUNET_assert (NULL != reply_cls);
1437 LOG (GNUNET_ERROR_TYPE_DEBUG,
1438 "sampler returned %" PRIu32 " peers:\n",
1440 for (i = 0; i < num_peers; i++)
1442 LOG (GNUNET_ERROR_TYPE_DEBUG,
1445 GNUNET_i2s (&peer_ids[i]));
1448 size_needed = sizeof (struct GNUNET_RPS_CS_ReplyMessage) +
1449 num_peers * sizeof (struct GNUNET_PeerIdentity);
1451 GNUNET_assert (GNUNET_SERVER_MAX_MESSAGE_SIZE >= size_needed);
1453 ev = GNUNET_MQ_msg_extra (out_msg,
1454 num_peers * sizeof (struct GNUNET_PeerIdentity),
1455 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
1456 out_msg->num_peers = htonl (num_peers);
1457 out_msg->id = htonl (reply_cls->id);
1459 memcpy (&out_msg[1],
1461 num_peers * sizeof (struct GNUNET_PeerIdentity));
1462 GNUNET_free (peer_ids);
1464 cli_ctx = GNUNET_SERVER_client_get_user_context (reply_cls->client,
1465 struct ClientContext);
1466 GNUNET_assert (NULL != cli_ctx);
1467 destroy_reply_cls (reply_cls);
1468 GNUNET_MQ_send (cli_ctx->mq, ev);
1473 * Handle RPS request from the client.
1475 * @param cls closure
1476 * @param client identification of the client
1477 * @param message the actual message
1480 handle_client_request (void *cls,
1481 struct GNUNET_SERVER_Client *client,
1482 const struct GNUNET_MessageHeader *message)
1484 struct GNUNET_RPS_CS_RequestMessage *msg;
1486 uint32_t size_needed;
1487 struct ReplyCls *reply_cls;
1489 struct ClientContext *cli_ctx;
1491 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
1493 num_peers = ntohl (msg->num_peers);
1494 size_needed = sizeof (struct GNUNET_RPS_CS_RequestMessage) +
1495 num_peers * sizeof (struct GNUNET_PeerIdentity);
1497 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
1499 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1500 "Message received from client has size larger than expected\n");
1501 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1505 for (i = 0 ; i < num_peers ; i++)
1508 LOG (GNUNET_ERROR_TYPE_DEBUG,
1509 "Client requested %" PRIu32 " random peer(s).\n",
1512 reply_cls = GNUNET_new (struct ReplyCls);
1513 reply_cls->id = ntohl (msg->id);
1514 reply_cls->client = client;
1515 reply_cls->req_handle = RPS_sampler_get_n_rand_peers (client_sampler,
1520 cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientContext);
1521 GNUNET_assert (NULL != cli_ctx);
1522 GNUNET_CONTAINER_DLL_insert (cli_ctx->rep_cls_head,
1523 cli_ctx->rep_cls_tail,
1525 GNUNET_SERVER_receive_done (client,
1531 * @brief Handle a message that requests the cancellation of a request
1534 * @param client the client that requests the cancellation
1535 * @param message the message containing the id of the request
1538 handle_client_request_cancel (void *cls,
1539 struct GNUNET_SERVER_Client *client,
1540 const struct GNUNET_MessageHeader *message)
1542 struct GNUNET_RPS_CS_RequestCancelMessage *msg =
1543 (struct GNUNET_RPS_CS_RequestCancelMessage *) message;
1544 struct ClientContext *cli_ctx;
1545 struct ReplyCls *rep_cls;
1547 cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientContext);
1548 GNUNET_assert (NULL != cli_ctx->rep_cls_head);
1549 rep_cls = cli_ctx->rep_cls_head;
1550 LOG (GNUNET_ERROR_TYPE_DEBUG,
1551 "Client cancels request with id %lu\n",
1553 while ( (NULL != rep_cls->next) &&
1554 (rep_cls->id != ntohl (msg->id)) )
1555 rep_cls = rep_cls->next;
1556 GNUNET_assert (rep_cls->id == ntohl (msg->id));
1557 RPS_sampler_request_cancel (rep_cls->req_handle);
1558 destroy_reply_cls (rep_cls);
1559 GNUNET_SERVER_receive_done (client,
1565 * Handle seed from the client.
1567 * @param cls closure
1568 * @param client identification of the client
1569 * @param message the actual message
1572 handle_client_seed (void *cls,
1573 struct GNUNET_SERVER_Client *client,
1574 const struct GNUNET_MessageHeader *message)
1576 struct GNUNET_RPS_CS_SeedMessage *in_msg;
1577 struct GNUNET_PeerIdentity *peers;
1581 if (sizeof (struct GNUNET_RPS_CS_SeedMessage) > ntohs (message->size))
1583 GNUNET_break_op (0);
1584 GNUNET_SERVER_receive_done (client,
1588 in_msg = (struct GNUNET_RPS_CS_SeedMessage *) message;
1589 num_peers = ntohl (in_msg->num_peers);
1590 peers = (struct GNUNET_PeerIdentity *) &in_msg[1];
1591 //peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1592 //memcpy (peers, &in_msg[1], num_peers * sizeof (struct GNUNET_PeerIdentity));
1594 if ((ntohs (message->size) - sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
1595 sizeof (struct GNUNET_PeerIdentity) != num_peers)
1597 GNUNET_break_op (0);
1598 GNUNET_SERVER_receive_done (client,
1603 LOG (GNUNET_ERROR_TYPE_DEBUG,
1604 "Client seeded peers:\n");
1605 print_peer_list (peers, num_peers);
1607 for (i = 0 ; i < num_peers ; i++)
1609 LOG (GNUNET_ERROR_TYPE_DEBUG,
1610 "Updating samplers with seed %" PRIu32 ": %s\n",
1612 GNUNET_i2s (&peers[i]));
1614 new_peer_id (&peers[i]);
1616 //RPS_sampler_update (prot_sampler, &peers[i]);
1617 //RPS_sampler_update (client_sampler, &peers[i]);
1620 ////GNUNET_free (peers);
1622 GNUNET_SERVER_receive_done (client,
1628 * Handle a PUSH message from another peer.
1630 * Check the proof of work and store the PeerID
1631 * in the temporary list for pushed PeerIDs.
1633 * @param cls Closure
1634 * @param channel The channel the PUSH was received over
1635 * @param channel_ctx The context associated with this channel
1636 * @param msg The message header
1639 handle_peer_push (void *cls,
1640 struct GNUNET_CADET_Channel *channel,
1642 const struct GNUNET_MessageHeader *msg)
1644 const struct GNUNET_PeerIdentity *peer;
1646 // (check the proof of work)
1648 peer = (const struct GNUNET_PeerIdentity *)
1649 GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
1650 // FIXME wait for cadet to change this function
1652 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PUSH (%s)\n", GNUNET_i2s (peer));
1654 #ifdef ENABLE_MALICIOUS
1655 struct AttackedPeer *tmp_att_peer;
1657 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1658 memcpy (&tmp_att_peer->peer_id, peer, sizeof (struct GNUNET_PeerIdentity));
1661 { /* Try to maximise representation */
1662 if (NULL == att_peer_set)
1663 att_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
1664 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1667 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1670 add_peer_array_to_set (peer, 1, att_peer_set);
1676 else if (2 == mal_type)
1677 { /* We attack one single well-known peer - simply ignore */
1682 GNUNET_free (tmp_att_peer);
1685 #endif /* ENABLE_MALICIOUS */
1687 /* Add the sending peer to the push_list */
1688 if (GNUNET_NO == in_arr (push_list, push_list_size, peer))
1690 GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer));
1691 GNUNET_array_append (push_list, push_list_size, *peer);
1694 GNUNET_CADET_receive_done (channel);
1700 * Iterator over hash map entries.
1701 * Called from #generate_view_array and writes every peer id into #view_array.
1703 * @param cls closure - the pointer to the counter
1704 * @param key current public key
1705 * @param value value in the hash map
1706 * @return #GNUNET_YES if we should continue to
1708 * #GNUNET_NO if not.
1711 dump_id_to_view_array (void *cls,
1712 const struct GNUNET_PeerIdentity *key,
1715 unsigned int *i = (unsigned int *) cls;
1717 view_array[(*i)++] = *key;
1723 * Makes sure the view_array is filled with the peer ids currently in #view.
1724 * Called from within #do_round before sending pushes and pulls and from
1725 * #handle_peer_pull_request when a reply is sent.
1728 generate_view_array (unsigned int view_size)
1733 if (NULL == view_array)
1735 view_array = GNUNET_new_array (view_size,
1736 struct GNUNET_PeerIdentity);
1737 i = GNUNET_new (unsigned int);
1740 ret = GNUNET_CONTAINER_multipeermap_iterate (view,
1741 dump_id_to_view_array,
1743 GNUNET_assert (view_size == ret);
1744 GNUNET_assert (view_size == *i);
1752 * Handle PULL REQUEST request message from another peer.
1754 * Reply with the view of PeerIDs.
1756 * @param cls Closure
1757 * @param channel The channel the PULL REQUEST was received over
1758 * @param channel_ctx The context associated with this channel
1759 * @param msg The message header
1762 handle_peer_pull_request (void *cls,
1763 struct GNUNET_CADET_Channel *channel,
1765 const struct GNUNET_MessageHeader *msg)
1767 struct GNUNET_PeerIdentity *peer;
1768 unsigned int view_size;
1770 peer = (struct GNUNET_PeerIdentity *)
1771 GNUNET_CADET_channel_get_info (channel,
1772 GNUNET_CADET_OPTION_PEER);
1773 // FIXME wait for cadet to change this function
1775 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REQUEST (%s)\n", GNUNET_i2s (peer));
1777 #ifdef ENABLE_MALICIOUS
1780 { /* Try to maximise representation */
1781 send_pull_reply (peer, mal_peers, num_mal_peers);
1785 else if (2 == mal_type)
1786 { /* Try to partition network */
1787 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
1789 send_pull_reply (peer, mal_peers, num_mal_peers);
1793 #endif /* ENABLE_MALICIOUS */
1795 view_size = GNUNET_CONTAINER_multipeermap_size (view);
1796 generate_view_array (view_size);
1798 send_pull_reply (peer, view_array, view_size);
1800 GNUNET_CADET_receive_done (channel);
1806 * Handle PULL REPLY message from another peer.
1808 * Check whether we sent a corresponding request and
1809 * whether this reply is the first one.
1811 * @param cls Closure
1812 * @param channel The channel the PUSH was received over
1813 * @param channel_ctx The context associated with this channel
1814 * @param msg The message header
1817 handle_peer_pull_reply (void *cls,
1818 struct GNUNET_CADET_Channel *channel,
1820 const struct GNUNET_MessageHeader *msg)
1822 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
1823 struct GNUNET_PeerIdentity *peers;
1824 struct PeerContext *peer_ctx;
1825 struct GNUNET_PeerIdentity *sender;
1826 struct PeerContext *sender_ctx;
1827 struct PeerOutstandingOp out_op;
1829 #ifdef ENABLE_MALICIOUS
1830 struct AttackedPeer *tmp_att_peer;
1831 #endif /* ENABLE_MALICIOUS */
1833 /* Check for protocol violation */
1834 if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->size))
1836 GNUNET_break_op (0);
1837 GNUNET_CADET_receive_done (channel);
1838 return GNUNET_SYSERR;
1841 in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
1842 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1843 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1845 LOG (GNUNET_ERROR_TYPE_ERROR,
1846 "message says it sends %" PRIu32 " peers, have space for %i peers\n",
1847 ntohl (in_msg->num_peers),
1848 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1849 sizeof (struct GNUNET_PeerIdentity));
1850 GNUNET_break_op (0);
1851 GNUNET_CADET_receive_done (channel);
1852 return GNUNET_SYSERR;
1855 // Guess simply casting isn't the nicest way...
1856 // FIXME wait for cadet to change this function
1857 sender = (struct GNUNET_PeerIdentity *)
1858 GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
1859 sender_ctx = get_peer_ctx (sender);
1861 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REPLY (%s)\n", GNUNET_i2s (sender));
1863 if (GNUNET_YES != get_peer_flag (sender_ctx, PULL_REPLY_PENDING))
1865 LOG (GNUNET_ERROR_TYPE_WARNING,
1866 "Received a pull reply from a peer we didn't request one from!\n");
1867 GNUNET_break_op (0);
1868 GNUNET_CADET_receive_done (channel);
1873 #ifdef ENABLE_MALICIOUS
1874 // We shouldn't even receive pull replies as we're not sending
1877 #endif /* ENABLE_MALICIOUS */
1879 /* Do actual logic */
1880 peers = (struct GNUNET_PeerIdentity *) &in_msg[1];
1882 LOG (GNUNET_ERROR_TYPE_DEBUG,
1883 "PULL REPLY received, got following %u peers:\n",
1884 ntohl (in_msg->num_peers));
1886 for (i = 0 ; i < ntohl (in_msg->num_peers) ; i++)
1888 LOG (GNUNET_ERROR_TYPE_DEBUG,
1891 GNUNET_i2s (&peers[i]));
1893 #ifdef ENABLE_MALICIOUS
1894 if ((NULL != att_peer_set) &&
1895 (1 == mal_type || 3 == mal_type))
1896 { /* Add attacked peer to local list */
1897 // TODO check if we sent a request and this was the first reply
1898 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1900 && GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mal_peer_set,
1902 && 0 != GNUNET_CRYPTO_cmp_peer_identity (&peers[i],
1905 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1906 tmp_att_peer->peer_id = peers[i];
1907 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1910 add_peer_array_to_set (&peers[i], 1, att_peer_set);
1914 #endif /* ENABLE_MALICIOUS */
1915 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity,
1918 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map,
1920 peer_ctx = create_peer_ctx (&peers[i]);
1922 peer_ctx = get_peer_ctx (&peers[i]);
1924 if (GNUNET_YES == get_peer_flag (peer_ctx, VALID))
1926 if (GNUNET_NO == in_arr (pull_list, pull_list_size, &peers[i]))
1927 GNUNET_array_append (pull_list, pull_list_size, peers[i]);
1929 else if (GNUNET_NO == insert_in_pull_list_scheduled (peer_ctx))
1931 out_op.op = insert_in_pull_list;
1932 out_op.op_cls = NULL;
1933 GNUNET_array_append (peer_ctx->outstanding_ops,
1934 peer_ctx->num_outstanding_ops,
1936 check_peer_live (peer_ctx);
1941 unset_peer_flag (sender_ctx, PULL_REPLY_PENDING);
1943 peer_clean (sender);
1945 GNUNET_CADET_receive_done (channel);
1951 * Compute a random delay.
1952 * A uniformly distributed value between mean + spread and mean - spread.
1954 * For example for mean 4 min and spread 2 the minimum is (4 min - (1/2 * 4 min))
1955 * It would return a random value between 2 and 6 min.
1957 * @param mean the mean
1958 * @param spread the inverse amount of deviation from the mean
1960 static struct GNUNET_TIME_Relative
1961 compute_rand_delay (struct GNUNET_TIME_Relative mean, unsigned int spread)
1963 struct GNUNET_TIME_Relative half_interval;
1964 struct GNUNET_TIME_Relative ret;
1965 unsigned int rand_delay;
1966 unsigned int max_rand_delay;
1970 LOG (GNUNET_ERROR_TYPE_WARNING,
1971 "Not accepting spread of 0\n");
1975 /* Compute random time value between spread * mean and spread * mean */
1976 half_interval = GNUNET_TIME_relative_divide (mean, spread);
1978 max_rand_delay = GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us / mean.rel_value_us * (2/spread);
1980 * Compute random value between (0 and 1) * round_interval
1981 * via multiplying round_interval with a 'fraction' (0 to value)/value
1983 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_rand_delay);
1984 ret = GNUNET_TIME_relative_multiply (mean, rand_delay);
1985 ret = GNUNET_TIME_relative_divide (ret, max_rand_delay);
1986 ret = GNUNET_TIME_relative_add (ret, half_interval);
1988 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ret.rel_value_us)
1989 LOG (GNUNET_ERROR_TYPE_WARNING,
1990 "Returning FOREVER_REL\n");
1997 * Send single pull request
1999 * @param peer_id the peer to send the pull request to.
2002 send_pull_request (const struct GNUNET_PeerIdentity *peer_id)
2004 struct GNUNET_MQ_Envelope *ev;
2005 struct GNUNET_MQ_Handle *mq;
2006 struct PeerContext *peer_ctx;
2007 struct PendingMessage *pending_msg;
2009 peer_ctx = get_peer_ctx (peer_id);
2010 GNUNET_assert (GNUNET_NO == get_peer_flag (peer_ctx, PULL_REPLY_PENDING));
2011 set_peer_flag (peer_ctx, PULL_REPLY_PENDING);
2013 LOG (GNUNET_ERROR_TYPE_DEBUG,
2014 "Going to send PULL REQUEST to peer %s.\n",
2015 GNUNET_i2s (peer_id));
2017 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
2018 mq = get_mq (peer_id);
2019 pending_msg = insert_pending_message (peer_id, ev, "PULL REQUEST");
2020 GNUNET_MQ_notify_sent (ev,
2023 GNUNET_MQ_send (mq, ev);
2030 * @param peer_id the peer to send the push to.
2033 send_push (const struct GNUNET_PeerIdentity *peer_id)
2035 struct GNUNET_MQ_Envelope *ev;
2036 struct GNUNET_MQ_Handle *mq;
2037 struct PendingMessage *pending_msg;
2039 LOG (GNUNET_ERROR_TYPE_DEBUG,
2040 "Going to send PUSH to peer %s.\n",
2041 GNUNET_i2s (peer_id));
2043 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
2044 mq = get_mq (peer_id);
2045 pending_msg = insert_pending_message (peer_id, ev, "PUSH");
2046 GNUNET_MQ_notify_sent (ev,
2049 GNUNET_MQ_send (mq, ev);
2054 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2057 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2060 #ifdef ENABLE_MALICIOUS
2062 * Turn RPS service to act malicious.
2064 * @param cls Closure
2065 * @param client The client that sent the message
2066 * @param msg The message header
2069 handle_client_act_malicious (void *cls,
2070 struct GNUNET_SERVER_Client *client,
2071 const struct GNUNET_MessageHeader *msg)
2073 struct GNUNET_RPS_CS_ActMaliciousMessage *in_msg;
2074 struct GNUNET_PeerIdentity *peers;
2075 uint32_t num_mal_peers_sent;
2076 uint32_t num_mal_peers_old;
2078 /* Check for protocol violation */
2079 if (sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage) > ntohs (msg->size))
2081 GNUNET_break_op (0);
2084 in_msg = (struct GNUNET_RPS_CS_ActMaliciousMessage *) msg;
2085 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
2086 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
2088 LOG (GNUNET_ERROR_TYPE_ERROR,
2089 "message says it sends %" PRIu64 " peers, have space for %i peers\n",
2090 ntohl (in_msg->num_peers),
2091 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
2092 sizeof (struct GNUNET_PeerIdentity));
2093 GNUNET_break_op (0);
2097 /* Do actual logic */
2098 peers = (struct GNUNET_PeerIdentity *) &msg[1];
2099 mal_type = ntohl (in_msg->type);
2100 if (NULL == mal_peer_set)
2101 mal_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
2103 LOG (GNUNET_ERROR_TYPE_DEBUG,
2104 "Now acting malicious type %" PRIu32 ", got %" PRIu32 " peers.\n",
2106 ntohl (in_msg->num_peers));
2109 { /* Try to maximise representation */
2110 /* Add other malicious peers to those we already know */
2112 num_mal_peers_sent = ntohl (in_msg->num_peers);
2113 num_mal_peers_old = num_mal_peers;
2114 GNUNET_array_grow (mal_peers,
2116 num_mal_peers + num_mal_peers_sent);
2117 memcpy (&mal_peers[num_mal_peers_old],
2119 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
2121 /* Add all mal peers to mal_peer_set */
2122 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
2126 /* Substitute do_round () with do_mal_round () */
2127 GNUNET_SCHEDULER_cancel (do_round_task);
2128 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
2131 else if (2 == mal_type
2133 { /* Try to partition the network */
2134 /* Add other malicious peers to those we already know */
2135 struct PeerContext *att_ctx;
2137 num_mal_peers_sent = ntohl (in_msg->num_peers) - 1;
2138 num_mal_peers_old = num_mal_peers;
2139 GNUNET_array_grow (mal_peers,
2141 num_mal_peers + num_mal_peers_sent);
2142 if (NULL != mal_peers &&
2145 memcpy (&mal_peers[num_mal_peers_old],
2147 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
2149 /* Add all mal peers to mal_peer_set */
2150 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
2155 /* Store the one attacked peer */
2156 memcpy (&attacked_peer,
2157 &in_msg->attacked_peer,
2158 sizeof (struct GNUNET_PeerIdentity));
2159 /* Set the flag of the attacked peer to valid to avoid problems */
2160 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map,
2163 att_ctx = create_peer_ctx (&attacked_peer);
2164 check_peer_live (att_ctx);
2167 LOG (GNUNET_ERROR_TYPE_DEBUG,
2168 "Attacked peer is %s\n",
2169 GNUNET_i2s (&attacked_peer));
2171 /* Substitute do_round () with do_mal_round () */
2172 GNUNET_SCHEDULER_cancel (do_round_task);
2173 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
2175 else if (0 == mal_type)
2176 { /* Stop acting malicious */
2177 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
2179 /* Substitute do_mal_round () with do_round () */
2180 GNUNET_SCHEDULER_cancel (do_round_task);
2181 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
2188 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2193 * Send out PUSHes and PULLs maliciously.
2195 * This is executed regylary.
2198 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2200 uint32_t num_pushes;
2202 struct GNUNET_TIME_Relative time_next_round;
2203 struct AttackedPeer *tmp_att_peer;
2204 struct PeerContext *peer_ctx;
2206 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round maliciously type %" PRIu32 ".\n",
2208 do_round_task = NULL;
2209 GNUNET_assert (mal_type <= 3);
2210 /* Do malicious actions */
2212 { /* Try to maximise representation */
2214 /* The maximum of pushes we're going to send this round */
2215 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit,
2216 num_attacked_peers),
2217 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
2219 LOG (GNUNET_ERROR_TYPE_DEBUG,
2220 "Going to send %" PRIu32 " pushes\n",
2223 /* Send PUSHes to attacked peers */
2224 for (i = 0 ; i < num_pushes ; i++)
2226 if (att_peers_tail == att_peer_index)
2227 att_peer_index = att_peers_head;
2229 att_peer_index = att_peer_index->next;
2231 send_push (&att_peer_index->peer_id);
2234 /* Send PULLs to some peers to learn about additional peers to attack */
2235 tmp_att_peer = att_peer_index;
2236 for (i = 0 ; i < num_pushes * alpha ; i++)
2238 if (att_peers_tail == tmp_att_peer)
2239 tmp_att_peer = att_peers_head;
2241 att_peer_index = tmp_att_peer->next;
2243 send_pull_request (&tmp_att_peer->peer_id);
2248 else if (2 == mal_type)
2250 * Try to partition the network
2251 * Send as many pushes to the attacked peer as possible
2252 * That is one push per round as it will ignore more.
2254 peer_ctx = get_peer_ctx (&attacked_peer);
2255 if (GNUNET_YES == get_peer_flag (peer_ctx, VALID))
2256 send_push (&attacked_peer);
2261 { /* Combined attack */
2263 /* Send PUSH to attacked peers */
2264 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map,
2267 peer_ctx = get_peer_ctx (&attacked_peer);
2268 if (GNUNET_YES == get_peer_flag (peer_ctx, VALID))
2270 LOG (GNUNET_ERROR_TYPE_DEBUG,
2271 "Goding to send push to attacked peer (%s)\n",
2272 GNUNET_i2s (&attacked_peer));
2273 send_push (&attacked_peer);
2276 check_peer_live (peer_ctx);
2279 peer_ctx = create_peer_ctx (&attacked_peer);
2280 check_peer_live (peer_ctx);
2282 /* The maximum of pushes we're going to send this round */
2283 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit - 1,
2284 num_attacked_peers),
2285 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
2287 LOG (GNUNET_ERROR_TYPE_DEBUG,
2288 "Going to send %" PRIu32 " pushes\n",
2291 for (i = 0 ; i < num_pushes ; i++)
2293 if (att_peers_tail == att_peer_index)
2294 att_peer_index = att_peers_head;
2296 att_peer_index = att_peer_index->next;
2298 send_push (&att_peer_index->peer_id);
2301 /* Send PULLs to some peers to learn about additional peers to attack */
2302 tmp_att_peer = att_peer_index;
2303 for (i = 0 ; i < num_pushes * alpha ; i++)
2305 if (att_peers_tail == tmp_att_peer)
2306 tmp_att_peer = att_peers_head;
2308 att_peer_index = tmp_att_peer->next;
2310 send_pull_request (&tmp_att_peer->peer_id);
2314 /* Schedule next round */
2315 time_next_round = compute_rand_delay (round_interval, 2);
2317 //do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_mal_round,
2319 GNUNET_assert (NULL == do_round_task);
2320 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_mal_round, NULL);
2321 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
2323 #endif /* ENABLE_MALICIOUS */
2327 * Send out PUSHes and PULLs, possibly update #view, samplers.
2329 * This is executed regylary.
2332 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2334 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round.\n");
2337 unsigned int view_size;
2338 unsigned int *permut;
2339 unsigned int a_peers; /* Number of peers we send pushes to */
2340 unsigned int b_peers; /* Number of peers we send pull requests to */
2341 uint32_t first_border;
2342 uint32_t second_border;
2343 struct GNUNET_PeerIdentity peer;
2344 struct PeerContext *peer_ctx;
2346 do_round_task = NULL;
2347 LOG (GNUNET_ERROR_TYPE_DEBUG,
2348 "Printing view:\n");
2349 to_file (file_name_view_log,
2350 "___ new round ___");
2351 view_size = GNUNET_CONTAINER_multipeermap_size (view);
2352 generate_view_array (view_size);
2353 for (i = 0 ; i < view_size ; i++)
2355 LOG (GNUNET_ERROR_TYPE_DEBUG,
2356 "\t%s\n", GNUNET_i2s (&view_array[i]));
2357 to_file (file_name_view_log,
2359 GNUNET_i2s_full (&view_array[i]));
2363 /* Send pushes and pull requests */
2366 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2367 (unsigned int) view_size);
2370 a_peers = ceil (alpha * view_size);
2372 LOG (GNUNET_ERROR_TYPE_DEBUG,
2373 "Going to send pushes to %u (ceil (%f * %u)) peers.\n",
2374 a_peers, alpha, view_size);
2375 for (i = 0; i < a_peers; i++)
2377 peer = view_array[permut[i]];
2378 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer)) // TODO
2379 { // FIXME if this fails schedule/loop this for later
2384 /* Send PULL requests */
2385 b_peers = ceil (beta * view_size);
2386 first_border = a_peers;
2387 second_border = a_peers + b_peers;
2388 if (second_border > view_size)
2390 first_border = view_size - b_peers;
2391 second_border = view_size;
2393 LOG (GNUNET_ERROR_TYPE_DEBUG,
2394 "Going to send pulls to %u (ceil (%f * %u)) peers.\n",
2395 b_peers, beta, view_size);
2396 for (i = first_border; i < second_border; i++)
2398 peer = view_array[permut[i]];
2399 peer_ctx = get_peer_ctx (&peer);
2400 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer) &&
2401 GNUNET_NO == get_peer_flag (peer_ctx, PULL_REPLY_PENDING)) // TODO
2402 { // FIXME if this fails schedule/loop this for later
2403 send_pull_request (&peer);
2407 GNUNET_free (permut);
2413 /* TODO see how many peers are in push-/pull- list! */
2415 if (push_list_size <= alpha * view_size &&
2416 0 < push_list_size &&
2419 LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the view.\n");
2421 uint32_t final_size;
2422 uint32_t peers_to_clean_size;
2423 struct GNUNET_PeerIdentity *peers_to_clean;
2425 peers_to_clean = NULL;
2426 peers_to_clean_size = 0;
2427 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, view_size);
2428 memcpy (peers_to_clean,
2430 view_size * sizeof (struct GNUNET_PeerIdentity));
2432 /* Seems like recreating is the easiest way of emptying the peermap */
2433 GNUNET_CONTAINER_multipeermap_destroy (view);
2434 view = GNUNET_CONTAINER_multipeermap_create (view_size, GNUNET_NO);
2435 to_file (file_name_view_log,
2438 first_border = GNUNET_MIN (ceil (alpha * sampler_size_est_need),
2440 second_border = first_border +
2441 GNUNET_MIN (floor (beta * sampler_size_est_need),
2443 final_size = second_border +
2444 ceil ((1 - (alpha + beta)) * sampler_size_est_need);
2446 GNUNET_array_grow (view_array, view_size, second_border);
2448 /* Update view with peers received through PUSHes */
2449 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2451 for (i = 0; i < first_border; i++)
2453 view_array[i] = push_list[permut[i]];
2454 GNUNET_CONTAINER_multipeermap_put (view, &push_list[permut[i]], NULL,
2455 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2457 to_file (file_name_view_log,
2459 GNUNET_i2s_full (&view_array[i]));
2460 // TODO change the peer_flags accordingly
2462 GNUNET_free (permut);
2465 /* Update view with peers received through PULLs */
2466 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2468 for (i = first_border; i < second_border; i++)
2470 view_array[i] = pull_list[permut[i - first_border]];
2471 GNUNET_CONTAINER_multipeermap_put (view,
2472 &pull_list[permut[i - first_border]],
2474 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2476 to_file (file_name_view_log,
2478 GNUNET_i2s_full (&view_array[i]));
2479 // TODO change the peer_flags accordingly
2481 GNUNET_free (permut);
2484 /* Update view with peers from history */
2485 RPS_sampler_get_n_rand_peers (prot_sampler,
2488 final_size - second_border);
2489 num_hist_update_tasks = final_size - second_border;
2490 // TODO change the peer_flags accordingly
2492 for (i = 0; i < view_size; i++)
2493 rem_from_list (&peers_to_clean, &peers_to_clean_size, &view_array[i]);
2495 /* Clean peers that were removed from the view */
2496 for (i = 0; i < peers_to_clean_size; i++)
2498 to_file (file_name_view_log,
2500 GNUNET_i2s_full (&peers_to_clean[i]));
2501 peer_clean (&peers_to_clean[i]);
2504 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, 0);
2505 peers_to_clean = NULL;
2509 LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the view.\n");
2511 // TODO independent of that also get some peers from CADET_get_peers()?
2513 LOG (GNUNET_ERROR_TYPE_DEBUG,
2514 "Received %u pushes and %u pulls last round (alpha (%.2f) * view_size (%u) = %.2f)\n",
2521 /* Update samplers */
2522 for (i = 0; i < push_list_size; i++)
2524 LOG (GNUNET_ERROR_TYPE_DEBUG,
2525 "Updating with peer %s from push list\n",
2526 GNUNET_i2s (&push_list[i]));
2527 insert_in_sampler (NULL, &push_list[i]);
2528 peer_clean (&push_list[i]); /* This cleans only if it is not in the view */
2531 for (i = 0; i < pull_list_size; i++)
2533 LOG (GNUNET_ERROR_TYPE_DEBUG,
2534 "Updating with peer %s from pull list\n",
2535 GNUNET_i2s (&pull_list[i]));
2536 insert_in_sampler (NULL, &pull_list[i]);
2537 peer_clean (&pull_list[i]); /* This cleans only if it is not in the view */
2541 /* Empty push/pull lists */
2542 GNUNET_array_grow (push_list, push_list_size, 0);
2543 GNUNET_array_grow (pull_list, pull_list_size, 0);
2545 struct GNUNET_TIME_Relative time_next_round;
2547 time_next_round = compute_rand_delay (round_interval, 2);
2549 /* Schedule next round */
2550 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_round, NULL);
2551 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
2556 rps_start (struct GNUNET_SERVER_Handle *server);
2560 * This is called from GNUNET_CADET_get_peers().
2562 * It is called on every peer(ID) that cadet somehow has contact with.
2563 * We use those to initialise the sampler.
2566 init_peer_cb (void *cls,
2567 const struct GNUNET_PeerIdentity *peer,
2568 int tunnel, // "Do we have a tunnel towards this peer?"
2569 unsigned int n_paths, // "Number of known paths towards this peer"
2570 unsigned int best_path) // "How long is the best path?
2571 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
2575 LOG (GNUNET_ERROR_TYPE_DEBUG,
2576 "Got peer_id %s from cadet\n",
2584 * Iterator over peers from peerinfo.
2586 * @param cls closure
2587 * @param peer id of the peer, NULL for last call
2588 * @param hello hello message for the peer (can be NULL)
2589 * @param error message
2592 process_peerinfo_peers (void *cls,
2593 const struct GNUNET_PeerIdentity *peer,
2594 const struct GNUNET_HELLO_Message *hello,
2595 const char *err_msg)
2599 LOG (GNUNET_ERROR_TYPE_DEBUG,
2600 "Got peer_id %s from peerinfo\n",
2608 * Callback used to remove peers from the multipeermap.
2611 peer_remove_cb (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
2613 struct PeerContext *peer_ctx;
2614 const struct GNUNET_CADET_Channel *channel =
2615 (const struct GNUNET_CADET_Channel *) cls;
2617 peer_ctx = (struct PeerContext *) value;
2618 set_peer_flag (peer_ctx, TO_DESTROY);
2620 LOG (GNUNET_ERROR_TYPE_DEBUG,
2621 "Going to remove peer %s\n",
2622 GNUNET_i2s (&peer_ctx->peer_id));
2624 /* Remove it from the sampler used for the Brahms protocol */
2625 RPS_sampler_reinitialise_by_value (prot_sampler, key);
2627 /* If operations are still scheduled for this peer cancel those */
2628 if (0 != peer_ctx->num_outstanding_ops)
2630 GNUNET_array_grow (peer_ctx->outstanding_ops,
2631 peer_ctx->num_outstanding_ops,
2635 /* If we are still waiting for notification whether this peer is live
2636 * cancel the according task */
2637 if (NULL != peer_ctx->transmit_handle)
2639 LOG (GNUNET_ERROR_TYPE_DEBUG,
2640 "Trying to cancle transmit_handle for peer %s\n",
2642 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->transmit_handle);
2643 peer_ctx->transmit_handle = NULL;
2646 unset_peer_flag (peer_ctx, PULL_REPLY_PENDING);
2648 /* Remove peer from view */
2649 if (GNUNET_CONTAINER_multipeermap_contains (view, key))
2651 to_file (file_name_view_log,
2652 "-%s\t(cleanup channel, other peer)",
2653 GNUNET_i2s_full (key));
2654 GNUNET_CONTAINER_multipeermap_remove_all (view, key);
2655 if (NULL != view_array)
2657 GNUNET_free (view_array);
2662 /* Remove from push and pull lists */
2663 if (GNUNET_YES == in_arr (push_list, push_list_size, key))
2664 rem_from_list (&push_list, &push_list_size, key);
2665 if (GNUNET_YES == in_arr (pull_list, pull_list_size, key))
2666 rem_from_list (&pull_list, &pull_list_size, key);
2668 /* Cancle messages that have not been sent yet */
2669 while (NULL != peer_ctx->pending_messages_head)
2671 LOG (GNUNET_ERROR_TYPE_DEBUG,
2672 "Removing unsent %s\n",
2673 peer_ctx->pending_messages_head->type);
2674 /* We are not able to cancel messages as #GNUNET_CADET_mq_create () does not
2675 * set a #GNUNET_MQ_CancelImpl */
2676 /* GNUNET_MQ_send_cancel (peer_ctx->pending_messages_head->ev); */
2677 remove_pending_message (peer_ctx->pending_messages_head);
2680 /* If there is still a mq destroy it */
2681 if (NULL != peer_ctx->mq)
2683 GNUNET_MQ_destroy (peer_ctx->mq);
2684 peer_ctx->mq = NULL;
2688 /* Remove the send_channel
2689 * This function should be called again from #cleanup_channel (callback
2690 * called on the destruction of channels) and clean up the rest. */
2691 if (NULL != peer_ctx->send_channel &&
2692 channel != peer_ctx->send_channel)
2694 GNUNET_CADET_channel_destroy (peer_ctx->send_channel);
2695 peer_ctx->send_channel = NULL;
2698 /* Remove the recv_channel
2699 * This function should be called again from #cleanup_channel (callback
2700 * called on the destruction of channels) and clean up the rest. */
2701 if (NULL != peer_ctx->recv_channel &&
2702 channel != peer_ctx->recv_channel)
2704 GNUNET_CADET_channel_destroy (peer_ctx->recv_channel);
2705 peer_ctx->recv_channel = NULL;
2708 /* If there is no channel we have to remove the context now */
2709 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_remove_all (peer_map, key))
2710 LOG (GNUNET_ERROR_TYPE_WARNING, "removing peer from peer_map failed\n");
2712 GNUNET_free (peer_ctx);
2719 * Clean the send channel of a peer
2720 * If there is also no channel to receive messages from that peer, remove it
2724 peer_clean (const struct GNUNET_PeerIdentity *peer)
2726 struct PeerContext *peer_ctx;
2727 /* struct GNUNET_CADET_Channel *channel; */
2729 if ( (0 == RPS_sampler_count_id (prot_sampler, peer)) &&
2730 (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (view, peer)) &&
2731 (GNUNET_NO == in_arr (push_list, push_list_size, peer)) &&
2732 (GNUNET_NO == in_arr (pull_list, pull_list_size, peer)) &&
2733 (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer)) )
2735 peer_ctx = get_peer_ctx (peer);
2737 if ( (NULL == peer_ctx->recv_channel) &&
2738 (NULL == peer_ctx->pending_messages_head) &&
2739 (GNUNET_NO == get_peer_flag (peer_ctx, PULL_REPLY_PENDING)) )
2741 #ifdef ENABLE_MALICIOUS
2742 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
2743 peer_remove_cb (NULL, peer, peer_ctx);
2744 #else /* ENABLE_MALICIOUS */
2745 peer_remove_cb (NULL, peer, peer_ctx);
2746 #endif /* ENABLE_MALICIOUS */
2753 * Task run during shutdown.
2759 shutdown_task (void *cls,
2760 const struct GNUNET_SCHEDULER_TaskContext *tc)
2763 LOG (GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
2765 GNUNET_PEERINFO_notify_cancel (peerinfo_notify_handle);
2766 GNUNET_PEERINFO_disconnect (peerinfo_handle);
2768 if (NULL != do_round_task)
2770 GNUNET_SCHEDULER_cancel (do_round_task);
2771 do_round_task = NULL;
2775 if (GNUNET_SYSERR ==
2776 GNUNET_CONTAINER_multipeermap_iterate (peer_map, peer_remove_cb, NULL))
2777 LOG (GNUNET_ERROR_TYPE_WARNING,
2778 "Iterating over peers to disconnect from them was cancelled\n");
2781 GNUNET_NSE_disconnect (nse);
2782 RPS_sampler_destroy (prot_sampler);
2783 RPS_sampler_destroy (client_sampler);
2784 LOG (GNUNET_ERROR_TYPE_DEBUG,
2785 "Size of the peermap: %u\n",
2786 GNUNET_CONTAINER_multipeermap_size (peer_map));
2787 GNUNET_break (0 == GNUNET_CONTAINER_multipeermap_size (peer_map));
2788 GNUNET_CADET_disconnect (cadet_handle);
2789 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
2790 GNUNET_CONTAINER_multipeermap_destroy (view);
2792 GNUNET_array_grow (push_list, push_list_size, 0);
2793 GNUNET_array_grow (pull_list, pull_list_size, 0);
2794 #ifdef ENABLE_MALICIOUS
2795 struct AttackedPeer *tmp_att_peer;
2796 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
2797 if (NULL != mal_peer_set)
2798 GNUNET_CONTAINER_multipeermap_destroy (mal_peer_set);
2799 if (NULL != att_peer_set)
2800 GNUNET_CONTAINER_multipeermap_destroy (att_peer_set);
2801 while (NULL != att_peers_head)
2803 tmp_att_peer = att_peers_head;
2804 GNUNET_CONTAINER_DLL_remove (att_peers_head, att_peers_tail, tmp_att_peer);
2806 #endif /* ENABLE_MALICIOUS */
2811 * @brief Get informed about a connecting client.
2814 * @param client the client that connects
2817 handle_client_connect (void *cls,
2818 struct GNUNET_SERVER_Client *client)
2820 struct ClientContext *cli_ctx;
2822 LOG (GNUNET_ERROR_TYPE_DEBUG,
2823 "Client connected\n");
2825 return; /* Server was destroyed before a client connected. Shutting down */
2826 cli_ctx = GNUNET_new (struct ClientContext);
2827 cli_ctx->mq = GNUNET_MQ_queue_for_server_client (client);
2828 GNUNET_SERVER_client_set_user_context (client, cli_ctx);
2829 GNUNET_CONTAINER_DLL_insert (cli_ctx_head,
2835 * A client disconnected. Remove all of its data structure entries.
2837 * @param cls closure, NULL
2838 * @param client identification of the client
2841 handle_client_disconnect (void *cls,
2842 struct GNUNET_SERVER_Client *client)
2844 struct ClientContext *cli_ctx;
2847 {/* shutdown task */
2848 while (NULL != cli_ctx_head)
2849 destroy_cli_ctx (cli_ctx_head);
2853 cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientContext);
2854 destroy_cli_ctx (cli_ctx);
2860 * Handle the channel a peer opens to us.
2862 * @param cls The closure
2863 * @param channel The channel the peer wants to establish
2864 * @param initiator The peer's peer ID
2865 * @param port The port the channel is being established over
2866 * @param options Further options
2869 handle_inbound_channel (void *cls,
2870 struct GNUNET_CADET_Channel *channel,
2871 const struct GNUNET_PeerIdentity *initiator,
2873 enum GNUNET_CADET_ChannelOption options)
2875 struct PeerContext *peer_ctx;
2877 LOG (GNUNET_ERROR_TYPE_DEBUG,
2878 "New channel was established to us (Peer %s).\n",
2879 GNUNET_i2s (initiator));
2880 GNUNET_assert (NULL != channel); /* according to cadet API */
2881 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, initiator))
2882 peer_ctx = create_peer_ctx (initiator);
2884 peer_ctx = get_peer_ctx (initiator);
2885 /* We only accept one incoming channel from peers */
2886 if (NULL != peer_ctx->recv_channel)
2888 GNUNET_CADET_channel_destroy (channel);
2891 peer_ctx->recv_channel = channel;
2892 peer_is_live (peer_ctx);
2898 * This is called when a channel is destroyed.
2900 * @param cls The closure
2901 * @param channel The channel being closed
2902 * @param channel_ctx The context associated with this channel
2905 cleanup_channel (void *cls,
2906 const struct GNUNET_CADET_Channel *channel,
2909 struct GNUNET_PeerIdentity *peer;
2910 struct PeerContext *peer_ctx;
2912 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
2913 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
2914 // Guess simply casting isn't the nicest way...
2915 // FIXME wait for cadet to change this function
2917 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
2918 {/* We don't want to implicitly create a context that we're about to kill */
2919 peer_ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
2920 if (NULL == peer_ctx) /* It could have been removed by shutdown_task */
2923 if (get_peer_flag (peer_ctx, TO_DESTROY))
2924 {/* We initiatad the destruction of this particular peer */
2925 if (channel == peer_ctx->send_channel)
2926 peer_ctx->send_channel = NULL;
2927 else if (channel == peer_ctx->recv_channel)
2928 peer_ctx->recv_channel = NULL;
2930 to_file (file_name_view_log,
2931 "-%s\t(cleanup channel, ourself)",
2932 GNUNET_i2s_full (peer));
2936 { /* We did not initiate the destruction of this peer */
2937 if (channel == peer_ctx->send_channel)
2938 { /* Something (but us) killd the channel - clean up peer */
2939 LOG (GNUNET_ERROR_TYPE_DEBUG,
2940 "send channel (%s) was destroyed - cleaning up\n",
2942 peer_ctx->send_channel = NULL;
2943 /* Somwewhat {ab,re}use the iterator function */
2944 /* Cast to void is ok, because it's used as void in peer_remove_cb */
2945 (void) peer_remove_cb ((void *) channel, peer, peer_ctx);
2947 else if (channel == peer_ctx->recv_channel)
2948 { /* Other peer doesn't want to send us messages anymore */
2949 LOG (GNUNET_ERROR_TYPE_DEBUG,
2950 "Peer %s destroyed recv channel - cleaning up channel\n",
2952 peer_ctx->recv_channel = NULL;
2956 LOG (GNUNET_ERROR_TYPE_WARNING,
2957 "unknown channel (%s) was destroyed\n",
2964 { /* We don't know a context to that peer */
2965 LOG (GNUNET_ERROR_TYPE_DEBUG,
2966 "channel (%s) without associated context was destroyed\n",
2973 * Actually start the service.
2976 rps_start (struct GNUNET_SERVER_Handle *server)
2978 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
2979 {&handle_client_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
2980 sizeof (struct GNUNET_RPS_CS_RequestMessage)},
2981 {&handle_client_request_cancel, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST_CANCEL,
2982 sizeof (struct GNUNET_RPS_CS_RequestCancelMessage)},
2983 {&handle_client_seed, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_SEED, 0},
2984 #ifdef ENABLE_MALICIOUS
2985 {&handle_client_act_malicious, NULL, GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS , 0},
2986 #endif /* ENABLE_MALICIOUS */
2990 GNUNET_SERVER_add_handlers (server, handlers);
2991 GNUNET_SERVER_connect_notify (server,
2992 &handle_client_connect,
2994 GNUNET_SERVER_disconnect_notify (server,
2995 &handle_client_disconnect,
2997 LOG (GNUNET_ERROR_TYPE_INFO, "Ready to receive requests from clients\n");
3000 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
3001 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
3003 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
3010 * Process statistics requests.
3012 * @param cls closure
3013 * @param server the initialized server
3014 * @param c configuration to use
3018 struct GNUNET_SERVER_Handle *server,
3019 const struct GNUNET_CONFIGURATION_Handle *c)
3024 // TODO check what this does -- copied from gnunet-boss
3025 // - seems to work as expected
3026 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
3031 GNUNET_CRYPTO_get_peer_identity (cfg, &own_identity); // TODO check return value
3032 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3033 "STARTING SERVICE (rps) for peer [%s]\n",
3034 GNUNET_i2s (&own_identity));
3035 #ifdef ENABLE_MALICIOUS
3036 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3037 "Malicious execution compiled in.\n");
3038 #endif /* ENABLE_MALICIOUS */
3042 /* Get time interval from the configuration */
3043 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
3047 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
3048 "RPS", "ROUNDINTERVAL");
3049 GNUNET_SCHEDULER_shutdown ();
3053 /* Get initial size of sampler/view from the configuration */
3055 GNUNET_CONFIGURATION_get_value_number (cfg, "RPS", "INITSIZE",
3056 (long long unsigned int *) &sampler_size_est_need))
3058 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
3060 GNUNET_SCHEDULER_shutdown ();
3063 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", sampler_size_est_need);
3066 view = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO);
3068 /* file_name_view_log */
3069 if (GNUNET_OK != GNUNET_DISK_directory_create ("/tmp/rps/"))
3071 LOG (GNUNET_ERROR_TYPE_WARNING,
3072 "Failed to create directory /tmp/rps/\n");
3075 size = (14 + strlen (GNUNET_i2s_full (&own_identity)) + 1) * sizeof (char);
3076 file_name_view_log = GNUNET_malloc (size);
3077 out_size = GNUNET_snprintf (file_name_view_log,
3080 GNUNET_i2s_full (&own_identity));
3081 if (size < out_size ||
3084 LOG (GNUNET_ERROR_TYPE_WARNING,
3085 "Failed to write string to buffer (size: %i, out_size: %i)\n",
3091 /* connect to NSE */
3092 nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
3098 peer_map = GNUNET_CONTAINER_multipeermap_create (sampler_size_est_need, GNUNET_NO);
3101 /* Initialise cadet */
3102 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
3103 {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH ,
3104 sizeof (struct GNUNET_MessageHeader)},
3105 {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
3106 sizeof (struct GNUNET_MessageHeader)},
3107 {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY , 0},
3111 const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
3112 cadet_handle = GNUNET_CADET_connect (cfg,
3114 &handle_inbound_channel,
3119 peerinfo_handle = GNUNET_PEERINFO_connect (cfg);
3121 /* Initialise sampler */
3122 struct GNUNET_TIME_Relative half_round_interval;
3123 struct GNUNET_TIME_Relative max_round_interval;
3125 half_round_interval = GNUNET_TIME_relative_multiply (round_interval, .5);
3126 max_round_interval = GNUNET_TIME_relative_add (round_interval, half_round_interval);
3128 prot_sampler = RPS_sampler_init (sampler_size_est_need, max_round_interval);
3129 client_sampler = RPS_sampler_mod_init (sampler_size_est_need, max_round_interval);
3131 /* Initialise push and pull maps */
3138 num_hist_update_tasks = 0;
3141 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
3142 GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, NULL);
3143 // TODO send push/pull to each of those peers?
3145 peerinfo_notify_handle = GNUNET_PEERINFO_notify (cfg,
3147 process_peerinfo_peers,
3155 * The main function for the rps service.
3157 * @param argc number of arguments from the command line
3158 * @param argv command line arguments
3159 * @return 0 ok, 1 on error
3162 main (int argc, char *const *argv)
3164 return (GNUNET_OK ==
3165 GNUNET_SERVICE_run (argc,
3168 GNUNET_SERVICE_OPTION_NONE,
3169 &run, NULL)) ? 0 : 1;
3172 /* end of gnunet-service-rps.c */