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 * Struct used to store the context of a connected client.
83 * The message queue to communicate with the client.
85 struct GNUNET_MQ_Handle *mq;
89 * Used to keep track in what lists single peerIDs are.
93 PULL_REPLY_PENDING = 0x01,
94 IN_OTHER_GOSSIP_LIST = 0x02, // unneeded?
95 IN_OWN_SAMPLER_LIST = 0x04, // unneeded?
96 IN_OWN_GOSSIP_LIST = 0x08, // unneeded?
99 * We set this bit when we can be sure the other peer is/was live.
104 * We set this bit when we are going to destroy the channel to this peer.
105 * When cleanup_channel is called, we know that we wanted to destroy it.
106 * Otherwise the channel to the other peer was destroyed.
113 * Functions of this type can be used to be stored at a peer for later execution.
115 typedef void (* PeerOp) (void *cls, const struct GNUNET_PeerIdentity *peer);
118 * Outstanding operation on peer consisting of callback and closure
120 struct PeerOutstandingOp
135 * Struct used to keep track of other peer's status
137 * This is stored in a multipeermap.
142 * Message queue open to client
144 struct GNUNET_MQ_Handle *mq;
147 * Channel open to client.
149 struct GNUNET_CADET_Channel *send_channel;
152 * Channel open from client.
154 struct GNUNET_CADET_Channel *recv_channel; // unneeded?
157 * Array of outstanding operations on this peer.
159 struct PeerOutstandingOp *outstanding_ops;
162 * Handle to the callback given to cadet_ntfy_tmt_rdy()
164 * To be canceled on shutdown.
166 struct GNUNET_CADET_TransmitHandle *transmit_handle;
169 * Number of outstanding operations.
171 unsigned int num_outstanding_ops;
174 * Identity of the peer
176 struct GNUNET_PeerIdentity peer_id;
179 * Flags indicating status of peer
184 * This is pobably followed by 'statistical' data (when we first saw
185 * him, how did we get his ID, how many pushes (in a timeinterval),
190 /***********************************************************************
191 * /Housekeeping with peers
192 ***********************************************************************/
198 /***********************************************************************
200 ***********************************************************************/
203 * Sampler used for the Brahms protocol itself.
205 static struct RPS_Sampler *prot_sampler;
208 * Sampler used for the clients.
210 static struct RPS_Sampler *client_sampler;
213 * Set of all peers to keep track of them.
215 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
219 * Name to log view to
221 static char *file_name_view_log;
224 * The "local view" containing peers we learned from gossip and history
226 static struct GNUNET_CONTAINER_MultiPeerMap *view;
229 * An array containing the peers of the local view.
231 * This is created every time we send a pull reply if it has changed since the
232 * last pull reply we sent.
234 static struct GNUNET_PeerIdentity *view_array;
238 * The size of sampler we need to be able to satisfy the client's need of
241 static unsigned int sampler_size_client_need;
244 * The size of sampler we need to be able to satisfy the Brahms protocol's
245 * need of random peers.
247 * This is one minimum size the sampler grows to.
249 static unsigned int sampler_size_est_need;
253 * Percentage of total peer number in the view
254 * to send random PUSHes to
259 * Percentage of total peer number in the view
260 * to send random PULLs to
265 * The percentage gamma of history updates.
266 * Simply 1 - alpha - beta
271 * Identifier for the main task that runs periodically.
273 static struct GNUNET_SCHEDULER_Task *do_round_task;
276 * Time inverval the do_round task runs in.
278 static struct GNUNET_TIME_Relative round_interval;
283 * List to store peers received through pushes temporary.
285 * TODO -> multipeermap
287 static struct GNUNET_PeerIdentity *push_list;
290 * Size of the push_list;
292 static unsigned int push_list_size;
293 //size_t push_list_size;
296 * List to store peers received through pulls temporary.
298 * TODO -> multipeermap
300 static struct GNUNET_PeerIdentity *pull_list;
303 * Size of the pull_list;
305 static unsigned int pull_list_size;
306 //size_t pull_list_size;
312 static struct GNUNET_NSE_Handle *nse;
317 static struct GNUNET_CADET_Handle *cadet_handle;
320 * Handler to PEERINFO.
322 static struct GNUNET_PEERINFO_Handle *peerinfo_handle;
325 * Handle for cancellation of iteration over peers.
327 struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify_handle;
333 * Only needed in the beginning to check how many of the 64 deltas
336 static unsigned int req_counter;
339 * Time of the last request we received.
341 * Used to compute the expected request rate.
343 static struct GNUNET_TIME_Absolute last_request;
346 * Size of #request_deltas.
348 #define REQUEST_DELTAS_SIZE 64
349 static unsigned int request_deltas_size = REQUEST_DELTAS_SIZE;
352 * Last 64 deltas between requests
354 static struct GNUNET_TIME_Relative request_deltas[REQUEST_DELTAS_SIZE];
357 * The prediction of the rate of requests
359 static struct GNUNET_TIME_Relative request_rate;
363 * Number of history update tasks.
365 uint32_t num_hist_update_tasks;
369 * Closure used to pass the client and the id to the callback
370 * that replies to a client's request
375 * The identifier of the request
380 * The client handle to send the reply to
382 struct GNUNET_SERVER_Client *client;
386 #ifdef ENABLE_MALICIOUS
388 * Type of malicious peer
390 * 0 Don't act malicious at all - Default
391 * 1 Try to maximise representation
392 * 2 Try to partition the network
395 uint32_t mal_type = 0;
398 * Other malicious peers
400 static struct GNUNET_PeerIdentity *mal_peers = NULL;
403 * Hashmap of malicious peers used as set.
404 * Used to more efficiently check whether we know that peer.
406 static struct GNUNET_CONTAINER_MultiPeerMap *mal_peer_set = NULL;
409 * Number of other malicious peers
411 static uint32_t num_mal_peers;
415 * If type is 2 This struct is used to store the attacked peers in a DLL
422 struct AttackedPeer *next;
423 struct AttackedPeer *prev;
428 struct GNUNET_PeerIdentity peer_id;
432 * If type is 2 this is the DLL of attacked peers
434 static struct AttackedPeer *att_peers_head = NULL;
435 static struct AttackedPeer *att_peers_tail = NULL;
438 * This index is used to point to an attacked peer to
439 * implement the round-robin-ish way to select attacked peers.
441 static struct AttackedPeer *att_peer_index = NULL;
444 * Hashmap of attacked peers used as set.
445 * Used to more efficiently check whether we know that peer.
447 static struct GNUNET_CONTAINER_MultiPeerMap *att_peer_set = NULL;
450 * Number of attacked peers
452 static uint32_t num_attacked_peers = 0;
456 * If type is 1 this is the attacked peer
458 static struct GNUNET_PeerIdentity attacked_peer;
461 * The limit of PUSHes we can send in one round.
462 * This is an assumption of the Brahms protocol and either implemented
465 * assumend to be the bandwidth limitation.
467 static uint32_t push_limit = 10000;
468 #endif /* ENABLE_MALICIOUS */
471 /***********************************************************************
473 ***********************************************************************/
480 /***********************************************************************
482 ***********************************************************************/
485 * Set a peer flag of given peer context.
487 #define set_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags |= mask)
490 * Get peer flag of given peer context.
492 #define get_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags & mask ? GNUNET_YES : GNUNET_NO)
495 * Unset flag of given peer context.
497 #define unset_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags &= (~mask))
500 * Clean the send channel of a peer
503 peer_clean (const struct GNUNET_PeerIdentity *peer);
507 * Check if peer is already in peer array.
510 in_arr (const struct GNUNET_PeerIdentity *array,
511 unsigned int arr_size,
512 const struct GNUNET_PeerIdentity *peer)
514 GNUNET_assert (NULL != peer);
519 GNUNET_assert (NULL != array);
523 for (i = 0; i < arr_size ; i++)
524 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&array[i], peer))
531 * Print peerlist to log.
534 print_peer_list (struct GNUNET_PeerIdentity *list, unsigned int len)
538 LOG (GNUNET_ERROR_TYPE_DEBUG,
539 "Printing peer list of length %u at %p:\n",
542 for (i = 0 ; i < len ; i++)
544 LOG (GNUNET_ERROR_TYPE_DEBUG,
546 i, GNUNET_i2s (&list[i]));
552 * Remove peer from list.
555 rem_from_list (struct GNUNET_PeerIdentity **peer_list,
556 unsigned int *list_size,
557 const struct GNUNET_PeerIdentity *peer)
560 struct GNUNET_PeerIdentity *tmp;
564 LOG (GNUNET_ERROR_TYPE_DEBUG,
565 "Removing peer %s from list at %p\n",
569 for ( i = 0 ; i < *list_size ; i++ )
571 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&tmp[i], peer))
573 if (i < *list_size -1)
574 { /* Not at the last entry -- shift peers left */
575 memcpy (&tmp[i], &tmp[i +1],
576 ((*list_size) - i -1) * sizeof (struct GNUNET_PeerIdentity));
578 /* Remove last entry (should be now useless PeerID) */
579 GNUNET_array_grow (tmp, *list_size, (*list_size) -1);
586 * Get random peer from the given list but don't return one from the @a ignore_list.
588 struct GNUNET_PeerIdentity *
589 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list,
591 const struct GNUNET_PeerIdentity *ignore_list,
592 uint32_t ignore_size)
596 struct GNUNET_PeerIdentity *tmp_peer_list;
597 struct GNUNET_PeerIdentity *peer;
599 GNUNET_assert (NULL != peer_list);
604 tmp_peer_list = NULL;
605 GNUNET_array_grow (tmp_peer_list, tmp_size, list_size);
606 memcpy (tmp_peer_list,
608 list_size * sizeof (struct GNUNET_PeerIdentity));
609 peer = GNUNET_new (struct GNUNET_PeerIdentity);
612 * Choose the r_index of the peer we want to return
613 * at random from the interval of the view
615 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
617 *peer = tmp_peer_list[r_index];
619 while (in_arr (ignore_list, ignore_size, peer))
621 rem_from_list (&tmp_peer_list, &tmp_size, peer);
623 print_peer_list (tmp_peer_list, tmp_size);
632 * Choose the r_index of the peer we want to return
633 * at random from the interval of the view
635 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
637 *peer = tmp_peer_list[r_index];
641 GNUNET_array_grow (tmp_peer_list, tmp_size, 0);
648 * Get the context of a peer. If not existing, create.
651 get_peer_ctx (const struct GNUNET_PeerIdentity *peer)
653 struct PeerContext *ctx;
656 ret = GNUNET_CONTAINER_multipeermap_contains (peer_map, peer);
657 GNUNET_assert (GNUNET_YES == ret);
658 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
659 GNUNET_assert (NULL != ctx);
664 * Create a new peer context and insert it into the peer map
667 create_peer_ctx (const struct GNUNET_PeerIdentity *peer)
669 struct PeerContext *ctx;
672 ctx = GNUNET_new (struct PeerContext);
673 ctx->peer_id = *peer;
674 ret = GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
675 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
676 GNUNET_assert (GNUNET_OK == ret);
682 * Put random peer from sampler into the view as history update.
685 hist_update (void *cls, struct GNUNET_PeerIdentity *ids, uint32_t num_peers)
689 for (i = 0; i < GNUNET_MIN (
690 sampler_size_est_need - GNUNET_CONTAINER_multipeermap_size (view),
693 /* Make sure there is a context associated with the id in the peermap */
694 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, &ids[i]))
695 (void) create_peer_ctx (&ids[i]);
697 if (GNUNET_OK != GNUNET_CONTAINER_multipeermap_put (view,
700 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
702 LOG (GNUNET_ERROR_TYPE_WARNING,
703 "Failed to put peer in peermap. (hist_update)\n");
706 /* Might want to check that we really updated the view */
707 if (NULL != view_array)
709 GNUNET_free (view_array);
713 to_file (file_name_view_log,
715 GNUNET_i2s_full (ids));
718 if (0 < num_hist_update_tasks)
719 num_hist_update_tasks--;
724 * Set the peer flag to living and call the outstanding operations on this peer.
727 peer_is_live (struct PeerContext *peer_ctx)
729 struct GNUNET_PeerIdentity *peer;
731 /* Cancle transmit_handle if still scheduled */
732 if (NULL != peer_ctx->transmit_handle)
734 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->transmit_handle);
735 peer_ctx->transmit_handle = NULL;
738 peer = &peer_ctx->peer_id;
739 set_peer_flag (peer_ctx, VALID);
741 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer %s is live\n", GNUNET_i2s (peer));
743 if (0 < peer_ctx->num_outstanding_ops)
744 { /* Call outstanding operations */
747 for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
748 peer_ctx->outstanding_ops[i].op (peer_ctx->outstanding_ops[i].op_cls, peer);
749 GNUNET_array_grow (peer_ctx->outstanding_ops, peer_ctx->num_outstanding_ops, 0);
757 * Callback that is called when a channel was effectively established.
758 * This is given to ntfy_tmt_rdy and called when the channel was
759 * successfully established.
762 cadet_ntfy_tmt_rdy_cb (void *cls, size_t size, void *buf)
764 struct PeerContext *peer_ctx = (struct PeerContext *) cls;
766 peer_ctx->transmit_handle = NULL;
767 LOG (GNUNET_ERROR_TYPE_DEBUG,
768 "Set ->transmit_handle = NULL for peer %s\n",
769 GNUNET_i2s (&peer_ctx->peer_id));
774 peer_is_live (peer_ctx);
778 LOG (GNUNET_ERROR_TYPE_WARNING,
779 "Problems establishing a connection to peer %s in order to check liveliness\n",
780 GNUNET_i2s (&peer_ctx->peer_id));
781 // TODO reschedule? cleanup?
784 //if (NULL != peer_ctx->transmit_handle)
786 // LOG (GNUNET_ERROR_TYPE_DEBUG,
787 // "Trying to cancle transmit_handle for peer %s\n",
788 // GNUNET_i2s (&peer_ctx->peer_id));
789 // GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->transmit_handle);
790 // peer_ctx->transmit_handle = NULL;
798 * Get the channel of a peer. If not existing, create.
800 struct GNUNET_CADET_Channel *
801 get_channel (const struct GNUNET_PeerIdentity *peer)
803 struct PeerContext *peer_ctx;
805 peer_ctx = get_peer_ctx (peer);
806 if (NULL == peer_ctx->send_channel)
808 LOG (GNUNET_ERROR_TYPE_DEBUG,
809 "Trying to establish channel to peer %s\n",
812 peer_ctx->send_channel =
813 GNUNET_CADET_channel_create (cadet_handle,
816 GNUNET_RPS_CADET_PORT,
817 GNUNET_CADET_OPTION_RELIABLE);
820 return peer_ctx->send_channel;
825 * Get the message queue of a specific peer.
827 * If we already have a message queue open to this client,
828 * simply return it, otherways create one.
830 struct GNUNET_MQ_Handle *
831 get_mq (const struct GNUNET_PeerIdentity *peer_id)
833 struct PeerContext *peer_ctx;
835 peer_ctx = get_peer_ctx (peer_id);
837 GNUNET_assert (NULL == peer_ctx->transmit_handle);
839 if (NULL == peer_ctx->mq)
841 (void) get_channel (peer_id);
842 peer_ctx->mq = GNUNET_CADET_mq_create (peer_ctx->send_channel);
843 //do I have to explicitly put it in the peer_map?
844 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer_id, peer_ctx,
845 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
852 * Issue check whether peer is live
854 * @param peer_ctx the context of the peer
857 check_peer_live (struct PeerContext *peer_ctx)
859 LOG (GNUNET_ERROR_TYPE_DEBUG,
860 "Get informed about peer %s getting live\n",
861 GNUNET_i2s (&peer_ctx->peer_id));
863 if (NULL == peer_ctx->transmit_handle &&
864 NULL == peer_ctx->send_channel)
866 (void) get_channel (&peer_ctx->peer_id);
867 peer_ctx->transmit_handle =
868 GNUNET_CADET_notify_transmit_ready (peer_ctx->send_channel,
870 GNUNET_TIME_UNIT_FOREVER_REL,
871 sizeof (struct GNUNET_MessageHeader),
872 cadet_ntfy_tmt_rdy_cb,
875 else if (NULL != peer_ctx->transmit_handle)
876 LOG (GNUNET_ERROR_TYPE_DEBUG,
877 "Already waiting for notification\n");
878 else if (NULL != peer_ctx->send_channel)
879 LOG (GNUNET_ERROR_TYPE_DEBUG,
880 "Already have established channel to peer\n");
885 * Sum all time relatives of an array.
887 struct GNUNET_TIME_Relative
888 T_relative_sum (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
890 struct GNUNET_TIME_Relative sum;
893 sum = GNUNET_TIME_UNIT_ZERO;
894 for ( i = 0 ; i < arr_size ; i++ )
896 sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
903 * Compute the average of given time relatives.
905 struct GNUNET_TIME_Relative
906 T_relative_avg (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
908 return GNUNET_TIME_relative_divide (T_relative_sum (rel_array, arr_size), arr_size);
913 * Insert PeerID in #pull_list
915 * Called once we know a peer is live.
918 insert_in_pull_list (void *cls, const struct GNUNET_PeerIdentity *peer)
920 if (GNUNET_NO == in_arr (pull_list, pull_list_size, peer))
921 GNUNET_array_append (pull_list, pull_list_size, *peer);
925 * Check whether #insert_in_pull_list was already scheduled
928 insert_in_pull_list_scheduled (const struct PeerContext *peer_ctx)
932 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
933 if (insert_in_pull_list == peer_ctx->outstanding_ops[i].op)
940 * Insert PeerID in #view
942 * Called once we know a peer is live.
945 insert_in_view (void *cls, const struct GNUNET_PeerIdentity *peer)
947 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_put (view,
950 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
952 LOG (GNUNET_ERROR_TYPE_WARNING,
953 "Failed to put peer into view. (insert_in_view)\n");
956 /* Might want to check whether we really modified the view */
957 if (NULL != view_array)
959 GNUNET_free (view_array);
963 (void) get_channel (peer);
967 * Check whether #insert_in_view was already scheduled
970 insert_in_view_scheduled (const struct PeerContext *peer_ctx)
974 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
975 if (insert_in_view == peer_ctx->outstanding_ops[i].op)
982 * Update sampler with given PeerID.
985 insert_in_sampler (void *cls, const struct GNUNET_PeerIdentity *peer)
987 LOG (GNUNET_ERROR_TYPE_DEBUG,
988 "Updating samplers with peer %s from insert_in_sampler()\n",
990 RPS_sampler_update (prot_sampler, peer);
991 RPS_sampler_update (client_sampler, peer);
996 * Check whether #insert_in_sampler was already scheduled
999 insert_in_sampler_scheduled (const struct PeerContext *peer_ctx)
1003 for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
1004 if (insert_in_sampler== peer_ctx->outstanding_ops[i].op)
1011 * Wrapper around #RPS_sampler_resize()
1013 * If we do not have enough sampler elements, double current sampler size
1014 * If we have more than enough sampler elements, halv current sampler size
1017 resize_wrapper (struct RPS_Sampler *sampler, uint32_t new_size)
1019 unsigned int sampler_size;
1022 // TODO respect the min, max
1023 sampler_size = RPS_sampler_get_size (sampler);
1024 if (sampler_size > new_size * 4)
1026 RPS_sampler_resize (sampler, sampler_size / 2);
1028 else if (sampler_size < new_size)
1030 RPS_sampler_resize (sampler, sampler_size * 2);
1032 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
1037 * Wrapper around #RPS_sampler_resize() resizing the client sampler
1040 client_resize_wrapper ()
1042 uint32_t bigger_size;
1046 bigger_size = GNUNET_MAX (sampler_size_est_need, sampler_size_client_need);
1048 // TODO respect the min, max
1049 resize_wrapper (client_sampler, bigger_size);
1050 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size_client is now %" PRIu32 "\n",
1056 * Estimate request rate
1058 * Called every time we receive a request from the client.
1063 struct GNUNET_TIME_Relative max_round_duration;
1065 if (request_deltas_size > req_counter)
1067 if ( 1 < req_counter)
1069 /* Shift last request deltas to the right */
1070 memcpy (&request_deltas[1],
1072 (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
1074 /* Add current delta to beginning */
1076 GNUNET_TIME_absolute_get_difference (last_request,
1077 GNUNET_TIME_absolute_get ());
1078 request_rate = T_relative_avg (request_deltas, req_counter);
1080 /* Compute the duration a round will maximally take */
1081 max_round_duration =
1082 GNUNET_TIME_relative_add (round_interval,
1083 GNUNET_TIME_relative_divide (round_interval, 2));
1085 /* Set the estimated size the sampler has to have to
1086 * satisfy the current client request rate */
1087 sampler_size_client_need =
1088 max_round_duration.rel_value_us / request_rate.rel_value_us;
1090 /* Resize the sampler */
1091 client_resize_wrapper ();
1093 last_request = GNUNET_TIME_absolute_get ();
1098 * Add all peers in @a peer_array to @a peer_map used as set.
1100 * @param peer_array array containing the peers
1101 * @param num_peers number of peers in @peer_array
1102 * @param peer_map the peermap to use as set
1105 add_peer_array_to_set (const struct GNUNET_PeerIdentity *peer_array,
1106 unsigned int num_peers,
1107 struct GNUNET_CONTAINER_MultiPeerMap *peer_map)
1110 if (NULL == peer_map)
1112 LOG (GNUNET_ERROR_TYPE_WARNING,
1113 "Trying to add peers to an empty peermap.\n");
1117 for (i = 0; i < num_peers; i++)
1119 GNUNET_CONTAINER_multipeermap_put (peer_map,
1122 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1128 * Send a PULL REPLY to @a peer_id
1130 * @param peer_id the peer to send the reply to.
1131 * @param peer_ids the peers to send to @a peer_id
1132 * @param num_peer_ids the number of peers to send to @a peer_id
1135 send_pull_reply (const struct GNUNET_PeerIdentity *peer_id,
1136 const struct GNUNET_PeerIdentity *peer_ids,
1137 unsigned int num_peer_ids)
1140 struct GNUNET_MQ_Handle *mq;
1141 struct GNUNET_MQ_Envelope *ev;
1142 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
1144 /* Compute actual size */
1145 send_size = sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) +
1146 num_peer_ids * sizeof (struct GNUNET_PeerIdentity);
1148 if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size)
1149 /* Compute number of peers to send
1150 * If too long, simply truncate */
1151 // TODO select random ones via permutation
1152 // or even better: do good protocol design
1154 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE -
1155 sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1156 sizeof (struct GNUNET_PeerIdentity);
1158 send_size = num_peer_ids;
1160 LOG (GNUNET_ERROR_TYPE_DEBUG,
1161 "PULL REQUEST from peer %s received, going to send %u peers\n",
1162 GNUNET_i2s (peer_id), send_size);
1164 mq = get_mq (peer_id);
1166 ev = GNUNET_MQ_msg_extra (out_msg,
1167 send_size * sizeof (struct GNUNET_PeerIdentity),
1168 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
1169 out_msg->num_peers = htonl (send_size);
1170 memcpy (&out_msg[1], peer_ids,
1171 send_size * sizeof (struct GNUNET_PeerIdentity));
1173 GNUNET_MQ_send (mq, ev);
1178 * This function is called on new peer_ids from 'external' sources
1179 * (client seed, cadet get_peers(), ...)
1181 * @param peer_id the new peer_id
1184 new_peer_id (const struct GNUNET_PeerIdentity *peer_id)
1186 struct PeerOutstandingOp out_op;
1187 struct PeerContext *peer_ctx;
1189 if ((NULL == peer_id) ||
1190 (0 == GNUNET_CRYPTO_cmp_peer_identity (&own_identity, peer_id)))
1192 LOG (GNUNET_ERROR_TYPE_DEBUG,
1193 "Got peer_id %s (at %p, view size: %u)\n",
1194 GNUNET_i2s (peer_id),
1196 GNUNET_CONTAINER_multipeermap_size (view));
1198 /* if the seed peer is already know, skip context creation */
1199 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer_id))
1200 peer_ctx = create_peer_ctx (peer_id);
1202 peer_ctx = get_peer_ctx (peer_id);
1204 if (GNUNET_NO == get_peer_flag (peer_ctx, VALID))
1206 if (GNUNET_NO == insert_in_sampler_scheduled (peer_ctx))
1208 out_op.op = insert_in_sampler;
1209 out_op.op_cls = NULL;
1210 GNUNET_array_append (peer_ctx->outstanding_ops,
1211 peer_ctx->num_outstanding_ops,
1215 if (GNUNET_NO == insert_in_view_scheduled (peer_ctx))
1217 out_op.op = insert_in_view;
1218 out_op.op_cls = NULL;
1219 GNUNET_array_append (peer_ctx->outstanding_ops,
1220 peer_ctx->num_outstanding_ops,
1224 /* Trigger livelyness test on peer */
1225 check_peer_live (peer_ctx);
1229 // send push/pull to each of those peers?
1233 /***********************************************************************
1235 ***********************************************************************/
1242 * Function called by NSE.
1244 * Updates sizes of sampler list and view and adapt those lists
1248 nse_callback (void *cls, struct GNUNET_TIME_Absolute timestamp,
1249 double logestimate, double std_dev)
1252 //double scale; // TODO this might go gloabal/config
1254 LOG (GNUNET_ERROR_TYPE_DEBUG,
1255 "Received a ns estimate - logest: %f, std_dev: %f (old_size: %u)\n",
1256 logestimate, std_dev, RPS_sampler_get_size (prot_sampler));
1258 estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
1259 // GNUNET_NSE_log_estimate_to_n (logestimate);
1260 estimate = pow (estimate, 1.0 / 3);
1261 // TODO add if std_dev is a number
1262 // estimate += (std_dev * scale);
1263 if (2 < ceil (estimate))
1265 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
1266 sampler_size_est_need = estimate;
1268 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
1270 /* If the NSE has changed adapt the lists accordingly */
1271 resize_wrapper (prot_sampler, sampler_size_est_need);
1272 client_resize_wrapper ();
1277 * Callback called once the requested PeerIDs are ready.
1279 * Sends those to the requesting client.
1281 void client_respond (void *cls,
1282 struct GNUNET_PeerIdentity *peer_ids, uint32_t num_peers)
1285 struct GNUNET_MQ_Envelope *ev;
1286 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
1287 struct ReplyCls *reply_cls = (struct ReplyCls *) cls;
1288 uint32_t size_needed;
1289 struct ClientContext *cli_ctx;
1291 GNUNET_assert (NULL != reply_cls);
1292 LOG (GNUNET_ERROR_TYPE_DEBUG,
1293 "sampler returned %" PRIu32 " peers:\n",
1295 for (i = 0; i < num_peers; i++)
1297 LOG (GNUNET_ERROR_TYPE_DEBUG,
1300 GNUNET_i2s (&peer_ids[i]));
1303 size_needed = sizeof (struct GNUNET_RPS_CS_ReplyMessage) +
1304 num_peers * sizeof (struct GNUNET_PeerIdentity);
1306 GNUNET_assert (GNUNET_SERVER_MAX_MESSAGE_SIZE >= size_needed);
1308 ev = GNUNET_MQ_msg_extra (out_msg,
1309 num_peers * sizeof (struct GNUNET_PeerIdentity),
1310 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
1311 out_msg->num_peers = htonl (num_peers);
1312 out_msg->id = htonl (reply_cls->id);
1314 memcpy (&out_msg[1],
1316 num_peers * sizeof (struct GNUNET_PeerIdentity));
1317 GNUNET_free (peer_ids);
1319 cli_ctx = GNUNET_SERVER_client_get_user_context (reply_cls->client, struct ClientContext);
1320 if (NULL == cli_ctx) {
1321 cli_ctx = GNUNET_new (struct ClientContext);
1322 cli_ctx->mq = GNUNET_MQ_queue_for_server_client (reply_cls->client);
1323 GNUNET_SERVER_client_set_user_context (reply_cls->client, cli_ctx);
1326 GNUNET_free (reply_cls);
1328 GNUNET_MQ_send (cli_ctx->mq, ev);
1333 * Handle RPS request from the client.
1335 * @param cls closure
1336 * @param client identification of the client
1337 * @param message the actual message
1340 handle_client_request (void *cls,
1341 struct GNUNET_SERVER_Client *client,
1342 const struct GNUNET_MessageHeader *message)
1344 struct GNUNET_RPS_CS_RequestMessage *msg;
1346 uint32_t size_needed;
1347 struct ReplyCls *reply_cls;
1350 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
1352 num_peers = ntohl (msg->num_peers);
1353 size_needed = sizeof (struct GNUNET_RPS_CS_RequestMessage) +
1354 num_peers * sizeof (struct GNUNET_PeerIdentity);
1356 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
1358 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1359 "Message received from client has size larger than expected\n");
1360 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1364 for (i = 0 ; i < num_peers ; i++)
1367 LOG (GNUNET_ERROR_TYPE_DEBUG,
1368 "Client requested %" PRIu32 " random peer(s).\n",
1371 reply_cls = GNUNET_new (struct ReplyCls);
1372 reply_cls->id = ntohl (msg->id);
1373 reply_cls->client = client;
1375 RPS_sampler_get_n_rand_peers (client_sampler,
1380 GNUNET_SERVER_receive_done (client,
1386 * Handle seed from the client.
1388 * @param cls closure
1389 * @param client identification of the client
1390 * @param message the actual message
1393 handle_client_seed (void *cls,
1394 struct GNUNET_SERVER_Client *client,
1395 const struct GNUNET_MessageHeader *message)
1397 struct GNUNET_RPS_CS_SeedMessage *in_msg;
1398 struct GNUNET_PeerIdentity *peers;
1402 if (sizeof (struct GNUNET_RPS_CS_SeedMessage) > ntohs (message->size))
1404 GNUNET_break_op (0);
1405 GNUNET_SERVER_receive_done (client,
1409 in_msg = (struct GNUNET_RPS_CS_SeedMessage *) message;
1410 num_peers = ntohl (in_msg->num_peers);
1411 peers = (struct GNUNET_PeerIdentity *) &in_msg[1];
1412 //peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1413 //memcpy (peers, &in_msg[1], num_peers * sizeof (struct GNUNET_PeerIdentity));
1415 if ((ntohs (message->size) - sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
1416 sizeof (struct GNUNET_PeerIdentity) != num_peers)
1418 GNUNET_break_op (0);
1419 GNUNET_SERVER_receive_done (client,
1424 LOG (GNUNET_ERROR_TYPE_DEBUG,
1425 "Client seeded peers:\n");
1426 print_peer_list (peers, num_peers);
1428 for (i = 0 ; i < num_peers ; i++)
1430 LOG (GNUNET_ERROR_TYPE_DEBUG,
1431 "Updating samplers with seed %" PRIu32 ": %s\n",
1433 GNUNET_i2s (&peers[i]));
1435 new_peer_id (&peers[i]);
1437 //RPS_sampler_update (prot_sampler, &peers[i]);
1438 //RPS_sampler_update (client_sampler, &peers[i]);
1441 ////GNUNET_free (peers);
1443 GNUNET_SERVER_receive_done (client,
1449 * Handle a PUSH message from another peer.
1451 * Check the proof of work and store the PeerID
1452 * in the temporary list for pushed PeerIDs.
1454 * @param cls Closure
1455 * @param channel The channel the PUSH was received over
1456 * @param channel_ctx The context associated with this channel
1457 * @param msg The message header
1460 handle_peer_push (void *cls,
1461 struct GNUNET_CADET_Channel *channel,
1463 const struct GNUNET_MessageHeader *msg)
1465 const struct GNUNET_PeerIdentity *peer;
1467 // (check the proof of work)
1469 peer = (const struct GNUNET_PeerIdentity *)
1470 GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
1471 // FIXME wait for cadet to change this function
1473 LOG (GNUNET_ERROR_TYPE_DEBUG, "PUSH received (%s)\n", GNUNET_i2s (peer));
1475 #ifdef ENABLE_MALICIOUS
1476 struct AttackedPeer *tmp_att_peer;
1478 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1479 memcpy (&tmp_att_peer->peer_id, peer, sizeof (struct GNUNET_PeerIdentity));
1482 { /* Try to maximise representation */
1483 if (NULL == att_peer_set)
1484 att_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
1485 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1488 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1491 add_peer_array_to_set (peer, 1, att_peer_set);
1497 else if (2 == mal_type)
1498 { /* We attack one single well-known peer - simply ignore */
1503 GNUNET_free (tmp_att_peer);
1506 #endif /* ENABLE_MALICIOUS */
1508 /* Add the sending peer to the push_list */
1509 if (GNUNET_NO == in_arr (push_list, push_list_size, peer))
1511 GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer));
1512 GNUNET_array_append (push_list, push_list_size, *peer);
1515 GNUNET_CADET_receive_done (channel);
1521 * Iterator over hash map entries.
1522 * Called from #generate_view_array and writes every peer id into #view_array.
1524 * @param cls closure - the pointer to the counter
1525 * @param key current public key
1526 * @param value value in the hash map
1527 * @return #GNUNET_YES if we should continue to
1529 * #GNUNET_NO if not.
1532 dump_id_to_view_array (void *cls,
1533 const struct GNUNET_PeerIdentity *key,
1536 unsigned int *i = (unsigned int *) cls;
1538 view_array[(*i)++] = *key;
1544 * Makes sure the view_array is filled with the peer ids currently in #view.
1545 * Called from within #do_round before sending pushes and pulls and from
1546 * #handle_peer_pull_request when a reply is sent.
1549 generate_view_array (unsigned int view_size)
1554 if (NULL == view_array)
1556 view_array = GNUNET_new_array (view_size,
1557 struct GNUNET_PeerIdentity);
1558 i = GNUNET_new (unsigned int);
1561 ret = GNUNET_CONTAINER_multipeermap_iterate (view,
1562 dump_id_to_view_array,
1564 GNUNET_assert (view_size == ret);
1565 GNUNET_assert (view_size == *i);
1573 * Handle PULL REQUEST request message from another peer.
1575 * Reply with the view of PeerIDs.
1577 * @param cls Closure
1578 * @param channel The channel the PULL REQUEST was received over
1579 * @param channel_ctx The context associated with this channel
1580 * @param msg The message header
1583 handle_peer_pull_request (void *cls,
1584 struct GNUNET_CADET_Channel *channel,
1586 const struct GNUNET_MessageHeader *msg)
1588 struct GNUNET_PeerIdentity *peer;
1589 unsigned int view_size;
1591 peer = (struct GNUNET_PeerIdentity *)
1592 GNUNET_CADET_channel_get_info (channel,
1593 GNUNET_CADET_OPTION_PEER);
1594 // FIXME wait for cadet to change this function
1596 LOG (GNUNET_ERROR_TYPE_DEBUG, "PULL REQUEST received (%s)\n", GNUNET_i2s (peer));
1598 #ifdef ENABLE_MALICIOUS
1601 { /* Try to maximise representation */
1602 send_pull_reply (peer, mal_peers, num_mal_peers);
1606 else if (2 == mal_type)
1607 { /* Try to partition network */
1608 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
1610 send_pull_reply (peer, mal_peers, num_mal_peers);
1614 #endif /* ENABLE_MALICIOUS */
1616 view_size = GNUNET_CONTAINER_multipeermap_size (view);
1617 generate_view_array (view_size);
1619 send_pull_reply (peer, view_array, view_size);
1621 GNUNET_CADET_receive_done (channel);
1627 * Handle PULL REPLY message from another peer.
1629 * Check whether we sent a corresponding request and
1630 * whether this reply is the first one.
1632 * @param cls Closure
1633 * @param channel The channel the PUSH was received over
1634 * @param channel_ctx The context associated with this channel
1635 * @param msg The message header
1638 handle_peer_pull_reply (void *cls,
1639 struct GNUNET_CADET_Channel *channel,
1641 const struct GNUNET_MessageHeader *msg)
1643 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
1644 struct GNUNET_PeerIdentity *peers;
1645 struct PeerContext *peer_ctx;
1646 struct GNUNET_PeerIdentity *sender;
1647 struct PeerContext *sender_ctx;
1648 struct PeerOutstandingOp out_op;
1650 #ifdef ENABLE_MALICIOUS
1651 struct AttackedPeer *tmp_att_peer;
1652 #endif /* ENABLE_MALICIOUS */
1654 /* Check for protocol violation */
1655 if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->size))
1657 GNUNET_break_op (0);
1658 GNUNET_CADET_receive_done (channel);
1659 return GNUNET_SYSERR;
1662 in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
1663 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1664 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1666 LOG (GNUNET_ERROR_TYPE_ERROR,
1667 "message says it sends %" PRIu32 " peers, have space for %i peers\n",
1668 ntohl (in_msg->num_peers),
1669 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1670 sizeof (struct GNUNET_PeerIdentity));
1671 GNUNET_break_op (0);
1672 GNUNET_CADET_receive_done (channel);
1673 return GNUNET_SYSERR;
1676 // Guess simply casting isn't the nicest way...
1677 // FIXME wait for cadet to change this function
1678 sender = (struct GNUNET_PeerIdentity *)
1679 GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
1680 sender_ctx = get_peer_ctx (sender);
1682 LOG (GNUNET_ERROR_TYPE_DEBUG, "PULL REPLY received (%s)\n", GNUNET_i2s (sender));
1684 if (GNUNET_YES != get_peer_flag (sender_ctx, PULL_REPLY_PENDING))
1686 LOG (GNUNET_ERROR_TYPE_WARNING,
1687 "Received a pull reply from a peer we didn't request one from!\n");
1688 GNUNET_break_op (0);
1689 GNUNET_CADET_receive_done (channel);
1694 #ifdef ENABLE_MALICIOUS
1695 // We shouldn't even receive pull replies as we're not sending
1698 #endif /* ENABLE_MALICIOUS */
1700 /* Do actual logic */
1701 peers = (struct GNUNET_PeerIdentity *) &in_msg[1];
1703 LOG (GNUNET_ERROR_TYPE_DEBUG,
1704 "PULL REPLY received, got following %u peers:\n",
1705 ntohl (in_msg->num_peers));
1707 for (i = 0 ; i < ntohl (in_msg->num_peers) ; i++)
1709 LOG (GNUNET_ERROR_TYPE_DEBUG,
1712 GNUNET_i2s (&peers[i]));
1714 #ifdef ENABLE_MALICIOUS
1715 if ((NULL != att_peer_set) &&
1716 (1 == mal_type || 3 == mal_type))
1717 { /* Add attacked peer to local list */
1718 // TODO check if we sent a request and this was the first reply
1719 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1721 && GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mal_peer_set,
1723 && 0 != GNUNET_CRYPTO_cmp_peer_identity (&peers[i],
1726 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1727 tmp_att_peer->peer_id = peers[i];
1728 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1731 add_peer_array_to_set (&peers[i], 1, att_peer_set);
1735 #endif /* ENABLE_MALICIOUS */
1736 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity,
1739 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map,
1741 peer_ctx = create_peer_ctx (&peers[i]);
1743 peer_ctx = get_peer_ctx (&peers[i]);
1745 if (GNUNET_YES == get_peer_flag (peer_ctx, VALID))
1747 if (GNUNET_NO == in_arr (pull_list, pull_list_size, &peers[i]))
1748 GNUNET_array_append (pull_list, pull_list_size, peers[i]);
1750 else if (GNUNET_NO == insert_in_pull_list_scheduled (peer_ctx))
1752 out_op.op = insert_in_pull_list;
1753 out_op.op_cls = NULL;
1754 GNUNET_array_append (peer_ctx->outstanding_ops,
1755 peer_ctx->num_outstanding_ops,
1757 check_peer_live (peer_ctx);
1762 unset_peer_flag (sender_ctx, PULL_REPLY_PENDING);
1764 peer_clean (sender);
1766 GNUNET_CADET_receive_done (channel);
1772 * Compute a random delay.
1773 * A uniformly distributed value between mean + spread and mean - spread.
1775 * For example for mean 4 min and spread 2 the minimum is (4 min - (1/2 * 4 min))
1776 * It would return a random value between 2 and 6 min.
1778 * @param mean the mean
1779 * @param spread the inverse amount of deviation from the mean
1781 static struct GNUNET_TIME_Relative
1782 compute_rand_delay (struct GNUNET_TIME_Relative mean, unsigned int spread)
1784 struct GNUNET_TIME_Relative half_interval;
1785 struct GNUNET_TIME_Relative ret;
1786 unsigned int rand_delay;
1787 unsigned int max_rand_delay;
1791 LOG (GNUNET_ERROR_TYPE_WARNING,
1792 "Not accepting spread of 0\n");
1796 /* Compute random time value between spread * mean and spread * mean */
1797 half_interval = GNUNET_TIME_relative_divide (mean, spread);
1799 max_rand_delay = GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us / mean.rel_value_us * (2/spread);
1801 * Compute random value between (0 and 1) * round_interval
1802 * via multiplying round_interval with a 'fraction' (0 to value)/value
1804 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_rand_delay);
1805 ret = GNUNET_TIME_relative_multiply (mean, rand_delay);
1806 ret = GNUNET_TIME_relative_divide (ret, max_rand_delay);
1807 ret = GNUNET_TIME_relative_add (ret, half_interval);
1809 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ret.rel_value_us)
1810 LOG (GNUNET_ERROR_TYPE_WARNING,
1811 "Returning FOREVER_REL\n");
1818 * Send single pull request
1820 * @param peer_id the peer to send the pull request to.
1823 send_pull_request (struct GNUNET_PeerIdentity *peer_id)
1825 struct GNUNET_MQ_Envelope *ev;
1826 struct GNUNET_MQ_Handle *mq;
1827 struct PeerContext *peer_ctx;
1829 peer_ctx = get_peer_ctx (peer_id);
1830 GNUNET_assert (GNUNET_NO == get_peer_flag (peer_ctx, PULL_REPLY_PENDING));
1831 set_peer_flag (peer_ctx, PULL_REPLY_PENDING);
1833 LOG (GNUNET_ERROR_TYPE_DEBUG,
1834 "Sending PULL request to peer %s of view.\n",
1835 GNUNET_i2s (peer_id));
1837 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
1838 mq = get_mq (peer_id);
1839 GNUNET_MQ_send (mq, ev);
1846 * @param peer_id the peer to send the push to.
1849 send_push (struct GNUNET_PeerIdentity *peer_id)
1851 struct GNUNET_MQ_Envelope *ev;
1852 struct GNUNET_MQ_Handle *mq;
1854 LOG (GNUNET_ERROR_TYPE_DEBUG,
1855 "Sending PUSH to peer %s of view.\n",
1856 GNUNET_i2s (peer_id));
1858 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
1859 mq = get_mq (peer_id);
1860 GNUNET_MQ_send (mq, ev);
1865 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1868 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1871 #ifdef ENABLE_MALICIOUS
1873 * Turn RPS service to act malicious.
1875 * @param cls Closure
1876 * @param client The client that sent the message
1877 * @param msg The message header
1880 handle_client_act_malicious (void *cls,
1881 struct GNUNET_SERVER_Client *client,
1882 const struct GNUNET_MessageHeader *msg)
1884 struct GNUNET_RPS_CS_ActMaliciousMessage *in_msg;
1885 struct GNUNET_PeerIdentity *peers;
1886 uint32_t num_mal_peers_sent;
1887 uint32_t num_mal_peers_old;
1889 /* Check for protocol violation */
1890 if (sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage) > ntohs (msg->size))
1892 GNUNET_break_op (0);
1895 in_msg = (struct GNUNET_RPS_CS_ActMaliciousMessage *) msg;
1896 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
1897 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1899 LOG (GNUNET_ERROR_TYPE_ERROR,
1900 "message says it sends %" PRIu64 " peers, have space for %i peers\n",
1901 ntohl (in_msg->num_peers),
1902 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
1903 sizeof (struct GNUNET_PeerIdentity));
1904 GNUNET_break_op (0);
1908 /* Do actual logic */
1909 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1910 mal_type = ntohl (in_msg->type);
1912 LOG (GNUNET_ERROR_TYPE_DEBUG,
1913 "Now acting malicious type %" PRIu32 ", got %" PRIu32 " peers.\n",
1915 ntohl (in_msg->num_peers));
1918 { /* Try to maximise representation */
1919 /* Add other malicious peers to those we already know */
1921 num_mal_peers_sent = ntohl (in_msg->num_peers);
1922 num_mal_peers_old = num_mal_peers;
1923 GNUNET_array_grow (mal_peers,
1925 num_mal_peers + num_mal_peers_sent);
1926 memcpy (&mal_peers[num_mal_peers_old],
1928 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1930 /* Add all mal peers to mal_peer_set */
1931 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1935 /* Substitute do_round () with do_mal_round () */
1936 GNUNET_SCHEDULER_cancel (do_round_task);
1937 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1940 else if (2 == mal_type
1942 { /* Try to partition the network */
1943 /* Add other malicious peers to those we already know */
1944 struct PeerContext *att_ctx;
1946 num_mal_peers_sent = ntohl (in_msg->num_peers) - 1;
1947 num_mal_peers_old = num_mal_peers;
1948 GNUNET_array_grow (mal_peers,
1950 num_mal_peers + num_mal_peers_sent);
1951 if (NULL != mal_peers &&
1954 memcpy (&mal_peers[num_mal_peers_old],
1956 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1958 /* Add all mal peers to mal_peer_set */
1959 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1964 /* Store the one attacked peer */
1965 memcpy (&attacked_peer,
1966 &in_msg->attacked_peer,
1967 sizeof (struct GNUNET_PeerIdentity));
1968 /* Set the flag of the attacked peer to valid to avoid problems */
1969 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map,
1972 att_ctx = create_peer_ctx (&attacked_peer);
1973 check_peer_live (att_ctx);
1976 LOG (GNUNET_ERROR_TYPE_DEBUG,
1977 "Attacked peer is %s\n",
1978 GNUNET_i2s (&attacked_peer));
1980 /* Substitute do_round () with do_mal_round () */
1981 GNUNET_SCHEDULER_cancel (do_round_task);
1982 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1984 else if (0 == mal_type)
1985 { /* Stop acting malicious */
1986 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
1988 /* Substitute do_mal_round () with do_round () */
1989 GNUNET_SCHEDULER_cancel (do_round_task);
1990 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
1997 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2002 * Send out PUSHes and PULLs maliciously.
2004 * This is executed regylary.
2007 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2009 uint32_t num_pushes;
2011 struct GNUNET_TIME_Relative time_next_round;
2012 struct AttackedPeer *tmp_att_peer;
2013 struct PeerContext *peer_ctx;
2015 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round maliciously type %" PRIu32 ".\n",
2017 do_round_task = NULL;
2018 GNUNET_assert (mal_type <= 3);
2019 /* Do malicious actions */
2021 { /* Try to maximise representation */
2023 /* The maximum of pushes we're going to send this round */
2024 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit,
2025 num_attacked_peers),
2026 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
2028 LOG (GNUNET_ERROR_TYPE_DEBUG,
2029 "Going to send %" PRIu32 " pushes\n",
2032 /* Send PUSHes to attacked peers */
2033 for (i = 0 ; i < num_pushes ; i++)
2035 if (att_peers_tail == att_peer_index)
2036 att_peer_index = att_peers_head;
2038 att_peer_index = att_peer_index->next;
2040 send_push (&att_peer_index->peer_id);
2043 /* Send PULLs to some peers to learn about additional peers to attack */
2044 tmp_att_peer = att_peer_index;
2045 for (i = 0 ; i < num_pushes * alpha ; i++)
2047 if (att_peers_tail == tmp_att_peer)
2048 tmp_att_peer = att_peers_head;
2050 att_peer_index = tmp_att_peer->next;
2052 send_pull_request (&tmp_att_peer->peer_id);
2057 else if (2 == mal_type)
2059 * Try to partition the network
2060 * Send as many pushes to the attacked peer as possible
2061 * That is one push per round as it will ignore more.
2063 peer_ctx = get_peer_ctx (&attacked_peer);
2064 if (GNUNET_YES == get_peer_flag (peer_ctx, VALID))
2065 send_push (&attacked_peer);
2070 { /* Combined attack */
2072 /* Send PUSH to attacked peers */
2073 peer_ctx = get_peer_ctx (&attacked_peer);
2074 if (GNUNET_YES == get_peer_flag (peer_ctx, VALID))
2076 LOG (GNUNET_ERROR_TYPE_DEBUG,
2077 "Goding to send push to attacked peer (%s)\n",
2078 GNUNET_i2s (&attacked_peer));
2079 send_push (&attacked_peer);
2082 check_peer_live (peer_ctx);
2084 /* The maximum of pushes we're going to send this round */
2085 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit - 1,
2086 num_attacked_peers),
2087 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
2089 LOG (GNUNET_ERROR_TYPE_DEBUG,
2090 "Going to send %" PRIu32 " pushes\n",
2093 for (i = 0 ; i < num_pushes ; i++)
2095 if (att_peers_tail == att_peer_index)
2096 att_peer_index = att_peers_head;
2098 att_peer_index = att_peer_index->next;
2100 send_push (&att_peer_index->peer_id);
2103 /* Send PULLs to some peers to learn about additional peers to attack */
2104 tmp_att_peer = att_peer_index;
2105 for (i = 0 ; i < num_pushes * alpha ; i++)
2107 if (att_peers_tail == tmp_att_peer)
2108 tmp_att_peer = att_peers_head;
2110 att_peer_index = tmp_att_peer->next;
2112 send_pull_request (&tmp_att_peer->peer_id);
2116 /* Schedule next round */
2117 time_next_round = compute_rand_delay (round_interval, 2);
2119 //do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_mal_round,
2121 GNUNET_assert (NULL == do_round_task);
2122 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_mal_round, NULL);
2123 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
2125 #endif /* ENABLE_MALICIOUS */
2129 * Send out PUSHes and PULLs, possibly update #view, samplers.
2131 * This is executed regylary.
2134 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2136 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round.\n");
2139 unsigned int view_size;
2140 unsigned int *permut;
2141 unsigned int a_peers; /* Number of peers we send pushes to */
2142 unsigned int b_peers; /* Number of peers we send pull requests to */
2143 uint32_t first_border;
2144 uint32_t second_border;
2145 struct GNUNET_PeerIdentity peer;
2146 struct PeerContext *peer_ctx;
2148 do_round_task = NULL;
2149 LOG (GNUNET_ERROR_TYPE_DEBUG,
2150 "Printing view:\n");
2151 to_file (file_name_view_log,
2152 "___ new round ___");
2153 view_size = GNUNET_CONTAINER_multipeermap_size (view);
2154 generate_view_array (view_size);
2155 for (i = 0 ; i < view_size ; i++)
2157 LOG (GNUNET_ERROR_TYPE_DEBUG,
2158 "\t%s\n", GNUNET_i2s (&view_array[i]));
2159 to_file (file_name_view_log,
2161 GNUNET_i2s_full (&view_array[i]));
2165 /* Send pushes and pull requests */
2168 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2169 (unsigned int) view_size);
2172 a_peers = ceil (alpha * view_size);
2174 LOG (GNUNET_ERROR_TYPE_DEBUG,
2175 "Going to send pushes to %u (ceil (%f * %u)) peers.\n",
2176 a_peers, alpha, view_size);
2177 for (i = 0; i < a_peers; i++)
2179 peer = view_array[permut[i]];
2180 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer)) // TODO
2181 { // FIXME if this fails schedule/loop this for later
2186 /* Send PULL requests */
2187 b_peers = ceil (beta * view_size);
2188 first_border = a_peers;
2189 second_border = a_peers + b_peers;
2190 if (second_border > view_size)
2192 first_border = view_size - b_peers;
2193 second_border = view_size;
2195 LOG (GNUNET_ERROR_TYPE_DEBUG,
2196 "Going to send pulls to %u (ceil (%f * %u)) peers.\n",
2197 b_peers, beta, view_size);
2198 for (i = first_border; i < second_border; i++)
2200 peer = view_array[permut[i]];
2201 peer_ctx = get_peer_ctx (&peer);
2202 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer) &&
2203 GNUNET_NO == get_peer_flag (peer_ctx, PULL_REPLY_PENDING)) // TODO
2204 { // FIXME if this fails schedule/loop this for later
2205 send_pull_request (&peer);
2209 GNUNET_free (permut);
2215 /* TODO see how many peers are in push-/pull- list! */
2217 if (push_list_size <= alpha * view_size &&
2218 0 < push_list_size &&
2221 LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the view.\n");
2223 uint32_t final_size;
2224 uint32_t peers_to_clean_size;
2225 struct GNUNET_PeerIdentity *peers_to_clean;
2227 peers_to_clean = NULL;
2228 peers_to_clean_size = 0;
2229 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, view_size);
2230 memcpy (peers_to_clean,
2232 view_size * sizeof (struct GNUNET_PeerIdentity));
2234 /* Seems like recreating is the easiest way of emptying the peermap */
2235 GNUNET_CONTAINER_multipeermap_destroy (view);
2236 view = GNUNET_CONTAINER_multipeermap_create (view_size, GNUNET_NO);
2237 to_file (file_name_view_log,
2240 first_border = GNUNET_MIN (ceil (alpha * sampler_size_est_need),
2242 second_border = first_border +
2243 GNUNET_MIN (floor (beta * sampler_size_est_need),
2245 final_size = second_border +
2246 ceil ((1 - (alpha + beta)) * sampler_size_est_need);
2248 GNUNET_array_grow (view_array, view_size, second_border);
2250 /* Update view with peers received through PUSHes */
2251 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2253 for (i = 0; i < first_border; i++)
2255 view_array[i] = push_list[permut[i]];
2256 GNUNET_CONTAINER_multipeermap_put (view, &push_list[permut[i]], NULL,
2257 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2259 to_file (file_name_view_log,
2261 GNUNET_i2s_full (&view_array[i]));
2262 // TODO change the peer_flags accordingly
2264 GNUNET_free (permut);
2267 /* Update view with peers received through PULLs */
2268 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2270 for (i = first_border; i < second_border; i++)
2272 view_array[i] = pull_list[permut[i - first_border]];
2273 GNUNET_CONTAINER_multipeermap_put (view,
2274 &pull_list[permut[i - first_border]],
2276 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2278 to_file (file_name_view_log,
2280 GNUNET_i2s_full (&view_array[i]));
2281 // TODO change the peer_flags accordingly
2283 GNUNET_free (permut);
2286 /* Update view with peers from history */
2287 RPS_sampler_get_n_rand_peers (prot_sampler,
2290 final_size - second_border);
2291 num_hist_update_tasks = final_size - second_border;
2292 // TODO change the peer_flags accordingly
2294 for (i = 0; i < view_size; i++)
2295 rem_from_list (&peers_to_clean, &peers_to_clean_size, &view_array[i]);
2297 /* Clean peers that were removed from the view */
2298 for (i = 0; i < peers_to_clean_size; i++)
2300 to_file (file_name_view_log,
2302 GNUNET_i2s_full (&peers_to_clean[i]));
2303 peer_clean (&peers_to_clean[i]);
2306 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, 0);
2307 peers_to_clean = NULL;
2311 LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the view.\n");
2313 // TODO independent of that also get some peers from CADET_get_peers()?
2315 LOG (GNUNET_ERROR_TYPE_DEBUG,
2316 "Received %u pushes and %u pulls last round (alpha (%.2f) * view_size (%u) = %.2f)\n",
2323 /* Update samplers */
2324 for (i = 0; i < push_list_size; i++)
2326 LOG (GNUNET_ERROR_TYPE_DEBUG,
2327 "Updating with peer %s from push list\n",
2328 GNUNET_i2s (&push_list[i]));
2329 RPS_sampler_update (prot_sampler, &push_list[i]);
2330 RPS_sampler_update (client_sampler, &push_list[i]);
2331 peer_clean (&push_list[i]); /* This cleans only if it is not in the view */
2334 for (i = 0; i < pull_list_size; i++)
2336 LOG (GNUNET_ERROR_TYPE_DEBUG,
2337 "Updating with peer %s from pull list\n",
2338 GNUNET_i2s (&pull_list[i]));
2339 RPS_sampler_update (prot_sampler, &pull_list[i]);
2340 RPS_sampler_update (client_sampler, &pull_list[i]);
2341 peer_clean (&pull_list[i]); /* This cleans only if it is not in the view */
2345 /* Empty push/pull lists */
2346 GNUNET_array_grow (push_list, push_list_size, 0);
2347 GNUNET_array_grow (pull_list, pull_list_size, 0);
2349 struct GNUNET_TIME_Relative time_next_round;
2351 time_next_round = compute_rand_delay (round_interval, 2);
2353 /* Schedule next round */
2354 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_round, NULL);
2355 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
2360 rps_start (struct GNUNET_SERVER_Handle *server);
2364 * This is called from GNUNET_CADET_get_peers().
2366 * It is called on every peer(ID) that cadet somehow has contact with.
2367 * We use those to initialise the sampler.
2370 init_peer_cb (void *cls,
2371 const struct GNUNET_PeerIdentity *peer,
2372 int tunnel, // "Do we have a tunnel towards this peer?"
2373 unsigned int n_paths, // "Number of known paths towards this peer"
2374 unsigned int best_path) // "How long is the best path?
2375 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
2379 LOG (GNUNET_ERROR_TYPE_DEBUG,
2380 "Got peer_id %s from cadet\n",
2388 * Iterator over peers from peerinfo.
2390 * @param cls closure
2391 * @param peer id of the peer, NULL for last call
2392 * @param hello hello message for the peer (can be NULL)
2393 * @param error message
2396 process_peerinfo_peers (void *cls,
2397 const struct GNUNET_PeerIdentity *peer,
2398 const struct GNUNET_HELLO_Message *hello,
2399 const char *err_msg)
2403 LOG (GNUNET_ERROR_TYPE_DEBUG,
2404 "Got peer_id %s from peerinfo\n",
2412 * Callback used to remove peers from the multipeermap.
2415 peer_remove_cb (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
2417 struct PeerContext *peer_ctx;
2418 const struct GNUNET_CADET_Channel *channel =
2419 (const struct GNUNET_CADET_Channel *) cls;
2421 peer_ctx = (struct PeerContext *) value;
2422 set_peer_flag (peer_ctx, TO_DESTROY);
2424 LOG (GNUNET_ERROR_TYPE_DEBUG,
2425 "Going to remove peer %s\n",
2426 GNUNET_i2s (&peer_ctx->peer_id));
2428 /* If operations are still scheduled for this peer cancel those */
2429 if (0 != peer_ctx->num_outstanding_ops)
2431 GNUNET_array_grow (peer_ctx->outstanding_ops,
2432 peer_ctx->num_outstanding_ops,
2436 /* If we are still waiting for notification whether this peer is live
2437 * cancel the according task */
2438 if (NULL != peer_ctx->transmit_handle)
2440 LOG (GNUNET_ERROR_TYPE_DEBUG,
2441 "Trying to cancle transmit_handle for peer %s\n",
2443 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->transmit_handle);
2444 peer_ctx->transmit_handle = NULL;
2447 unset_peer_flag (peer_ctx, PULL_REPLY_PENDING);
2449 /* Remove peer from view */
2450 if (GNUNET_CONTAINER_multipeermap_contains (view, key))
2452 to_file (file_name_view_log,
2453 "-%s\t(cleanup channel, other peer)",
2454 GNUNET_i2s_full (key));
2455 GNUNET_CONTAINER_multipeermap_remove_all (view, key);
2456 if (NULL != view_array)
2458 GNUNET_free (view_array);
2463 /* If there is still a mq destroy it */
2464 if (NULL != peer_ctx->mq)
2466 GNUNET_MQ_destroy (peer_ctx->mq);
2467 peer_ctx->mq = NULL;
2471 /* Remove the send_channel
2472 * This function should be called again from #cleanup_channel (callback
2473 * called on the destruction of channels) and clean up the rest. */
2474 if (NULL != peer_ctx->send_channel &&
2475 channel != peer_ctx->send_channel)
2477 GNUNET_CADET_channel_destroy (peer_ctx->send_channel);
2478 peer_ctx->send_channel = NULL;
2481 /* Remove the recv_channel
2482 * This function should be called again from #cleanup_channel (callback
2483 * called on the destruction of channels) and clean up the rest. */
2484 if (NULL != peer_ctx->recv_channel &&
2485 channel != peer_ctx->recv_channel)
2487 GNUNET_CADET_channel_destroy (peer_ctx->recv_channel);
2488 peer_ctx->recv_channel = NULL;
2491 /* If there is no channel we have to remove the context now */
2492 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_remove_all (peer_map, key))
2493 LOG (GNUNET_ERROR_TYPE_WARNING, "removing peer from peer_map failed\n");
2495 GNUNET_free (peer_ctx);
2502 * Clean the send channel of a peer
2503 * If there is also no channel to receive messages from that peer, remove it
2507 peer_clean (const struct GNUNET_PeerIdentity *peer)
2509 struct PeerContext *peer_ctx;
2510 /* struct GNUNET_CADET_Channel *channel; */
2512 if ( (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (view, peer)) &&
2513 (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer)) &&
2514 (GNUNET_NO == in_arr (push_list, push_list_size, peer)) &&
2515 (GNUNET_NO == in_arr (pull_list, pull_list_size, peer)) )
2517 peer_ctx = get_peer_ctx (peer);
2519 if ( (NULL == peer_ctx->recv_channel) &&
2520 (GNUNET_NO == get_peer_flag (peer_ctx, PULL_REPLY_PENDING)) )
2522 peer_remove_cb (NULL, peer, peer_ctx);
2529 * Task run during shutdown.
2535 shutdown_task (void *cls,
2536 const struct GNUNET_SCHEDULER_TaskContext *tc)
2539 LOG (GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
2541 GNUNET_PEERINFO_notify_cancel (peerinfo_notify_handle);
2542 GNUNET_PEERINFO_disconnect (peerinfo_handle);
2544 if (NULL != do_round_task)
2546 GNUNET_SCHEDULER_cancel (do_round_task);
2547 do_round_task = NULL;
2551 if (GNUNET_SYSERR ==
2552 GNUNET_CONTAINER_multipeermap_iterate (peer_map, peer_remove_cb, NULL))
2553 LOG (GNUNET_ERROR_TYPE_WARNING,
2554 "Iterating over peers to disconnect from them was cancelled\n");
2557 GNUNET_NSE_disconnect (nse);
2558 RPS_sampler_destroy (prot_sampler);
2559 RPS_sampler_destroy (client_sampler);
2560 LOG (GNUNET_ERROR_TYPE_DEBUG,
2561 "Size of the peermap: %u\n",
2562 GNUNET_CONTAINER_multipeermap_size (peer_map));
2563 GNUNET_break (0 == GNUNET_CONTAINER_multipeermap_size (peer_map));
2564 GNUNET_CADET_disconnect (cadet_handle);
2565 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
2566 GNUNET_CONTAINER_multipeermap_destroy (view);
2568 GNUNET_array_grow (push_list, push_list_size, 0);
2569 GNUNET_array_grow (pull_list, pull_list_size, 0);
2570 #ifdef ENABLE_MALICIOUS
2571 struct AttackedPeer *tmp_att_peer;
2572 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
2573 if (NULL != mal_peer_set)
2574 GNUNET_CONTAINER_multipeermap_destroy (mal_peer_set);
2575 if (NULL != att_peer_set)
2576 GNUNET_CONTAINER_multipeermap_destroy (att_peer_set);
2577 while (NULL != att_peers_head)
2579 tmp_att_peer = att_peers_head;
2580 GNUNET_CONTAINER_DLL_remove (att_peers_head, att_peers_tail, tmp_att_peer);
2582 #endif /* ENABLE_MALICIOUS */
2587 * A client disconnected. Remove all of its data structure entries.
2589 * @param cls closure, NULL
2590 * @param client identification of the client
2593 handle_client_disconnect (void *cls,
2594 struct GNUNET_SERVER_Client * client)
2600 * Handle the channel a peer opens to us.
2602 * @param cls The closure
2603 * @param channel The channel the peer wants to establish
2604 * @param initiator The peer's peer ID
2605 * @param port The port the channel is being established over
2606 * @param options Further options
2609 handle_inbound_channel (void *cls,
2610 struct GNUNET_CADET_Channel *channel,
2611 const struct GNUNET_PeerIdentity *initiator,
2613 enum GNUNET_CADET_ChannelOption options)
2615 struct PeerContext *peer_ctx;
2617 LOG (GNUNET_ERROR_TYPE_DEBUG,
2618 "New channel was established to us (Peer %s).\n",
2619 GNUNET_i2s (initiator));
2620 GNUNET_assert (NULL != channel); /* according to cadet API */
2621 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, initiator))
2622 peer_ctx = create_peer_ctx (initiator);
2624 peer_ctx = get_peer_ctx (initiator);
2625 /* We only accept one incoming channel from peers */
2626 if (NULL != peer_ctx->recv_channel)
2628 GNUNET_CADET_channel_destroy (channel);
2631 peer_ctx->recv_channel = channel;
2632 peer_is_live (peer_ctx);
2638 * This is called when a channel is destroyed.
2640 * @param cls The closure
2641 * @param channel The channel being closed
2642 * @param channel_ctx The context associated with this channel
2645 cleanup_channel (void *cls,
2646 const struct GNUNET_CADET_Channel *channel,
2649 struct GNUNET_PeerIdentity *peer;
2650 struct PeerContext *peer_ctx;
2652 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
2653 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
2654 // Guess simply casting isn't the nicest way...
2655 // FIXME wait for cadet to change this function
2657 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
2658 {/* We don't want to implicitly create a context that we're about to kill */
2659 peer_ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
2660 if (NULL == peer_ctx) /* It could have been removed by shutdown_task */
2663 if (get_peer_flag (peer_ctx, TO_DESTROY))
2664 {/* We initiatad the destruction of this particular peer */
2665 if (channel == peer_ctx->send_channel)
2666 peer_ctx->send_channel = NULL;
2667 else if (channel == peer_ctx->recv_channel)
2668 peer_ctx->recv_channel = NULL;
2670 to_file (file_name_view_log,
2671 "-%s\t(cleanup channel, ourself)",
2672 GNUNET_i2s_full (peer));
2676 { /* We did not initiate the destruction of this peer */
2677 if (channel == peer_ctx->send_channel)
2678 { /* Something (but us) killd the channel - clean up peer */
2679 LOG (GNUNET_ERROR_TYPE_DEBUG,
2680 "send channel (%s) was destroyed - cleaning up\n",
2682 peer_ctx->send_channel = NULL;
2683 /* Somwewhat {ab,re}use the iterator function */
2684 /* Cast to void is ok, because it's used as void in peer_remove_cb */
2685 (void) peer_remove_cb ((void *) channel, peer, peer_ctx);
2687 else if (channel == peer_ctx->recv_channel)
2688 { /* Other peer doesn't want to send us messages anymore */
2689 LOG (GNUNET_ERROR_TYPE_DEBUG,
2690 "Peer %s destroyed recv channel - cleaning up channel\n",
2692 peer_ctx->recv_channel = NULL;
2696 LOG (GNUNET_ERROR_TYPE_WARNING,
2697 "unknown channel (%s) was destroyed\n",
2704 { /* We don't know a context to that peer */
2705 LOG (GNUNET_ERROR_TYPE_DEBUG,
2706 "channel (%s) without associated context was destroyed\n",
2713 * Actually start the service.
2716 rps_start (struct GNUNET_SERVER_Handle *server)
2718 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
2719 {&handle_client_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
2720 sizeof (struct GNUNET_RPS_CS_RequestMessage)},
2721 {&handle_client_seed, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_SEED, 0},
2722 #ifdef ENABLE_MALICIOUS
2723 {&handle_client_act_malicious, NULL, GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS , 0},
2724 #endif /* ENABLE_MALICIOUS */
2728 GNUNET_SERVER_add_handlers (server, handlers);
2729 GNUNET_SERVER_disconnect_notify (server,
2730 &handle_client_disconnect,
2732 LOG (GNUNET_ERROR_TYPE_INFO, "Ready to receive requests from clients\n");
2735 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
2736 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
2738 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2745 * Process statistics requests.
2747 * @param cls closure
2748 * @param server the initialized server
2749 * @param c configuration to use
2753 struct GNUNET_SERVER_Handle *server,
2754 const struct GNUNET_CONFIGURATION_Handle *c)
2759 // TODO check what this does -- copied from gnunet-boss
2760 // - seems to work as expected
2761 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
2766 GNUNET_CRYPTO_get_peer_identity (cfg, &own_identity); // TODO check return value
2767 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2768 "STARTING SERVICE (rps) for peer [%s]\n",
2769 GNUNET_i2s (&own_identity));
2770 #ifdef ENABLE_MALICIOUS
2771 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2772 "Malicious execution compiled in.\n");
2773 #endif /* ENABLE_MALICIOUS */
2777 /* Get time interval from the configuration */
2778 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
2782 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2783 "RPS", "ROUNDINTERVAL");
2784 GNUNET_SCHEDULER_shutdown ();
2788 /* Get initial size of sampler/view from the configuration */
2790 GNUNET_CONFIGURATION_get_value_number (cfg, "RPS", "INITSIZE",
2791 (long long unsigned int *) &sampler_size_est_need))
2793 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2795 GNUNET_SCHEDULER_shutdown ();
2798 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", sampler_size_est_need);
2801 view = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO);
2803 /* file_name_view_log */
2804 if (GNUNET_OK != GNUNET_DISK_directory_create ("/tmp/rps/"))
2806 LOG (GNUNET_ERROR_TYPE_WARNING,
2807 "Failed to create directory /tmp/rps/\n");
2810 size = (14 + strlen (GNUNET_i2s_full (&own_identity)) + 1) * sizeof (char);
2811 file_name_view_log = GNUNET_malloc (size);
2812 out_size = GNUNET_snprintf (file_name_view_log,
2815 GNUNET_i2s_full (&own_identity));
2816 if (size < out_size ||
2819 LOG (GNUNET_ERROR_TYPE_WARNING,
2820 "Failed to write string to buffer (size: %i, out_size: %i)\n",
2826 /* connect to NSE */
2827 nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
2833 peer_map = GNUNET_CONTAINER_multipeermap_create (sampler_size_est_need, GNUNET_NO);
2836 /* Initialise cadet */
2837 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
2838 {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH ,
2839 sizeof (struct GNUNET_MessageHeader)},
2840 {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
2841 sizeof (struct GNUNET_MessageHeader)},
2842 {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY , 0},
2846 const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
2847 cadet_handle = GNUNET_CADET_connect (cfg,
2849 &handle_inbound_channel,
2854 peerinfo_handle = GNUNET_PEERINFO_connect (cfg);
2856 /* Initialise sampler */
2857 struct GNUNET_TIME_Relative half_round_interval;
2858 struct GNUNET_TIME_Relative max_round_interval;
2860 half_round_interval = GNUNET_TIME_relative_multiply (round_interval, .5);
2861 max_round_interval = GNUNET_TIME_relative_add (round_interval, half_round_interval);
2863 prot_sampler = RPS_sampler_init (sampler_size_est_need, max_round_interval);
2864 client_sampler = RPS_sampler_mod_init (sampler_size_est_need, max_round_interval);
2866 /* Initialise push and pull maps */
2873 num_hist_update_tasks = 0;
2876 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
2877 GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, NULL);
2878 // TODO send push/pull to each of those peers?
2880 peerinfo_notify_handle = GNUNET_PEERINFO_notify (cfg,
2882 process_peerinfo_peers,
2890 * The main function for the rps service.
2892 * @param argc number of arguments from the command line
2893 * @param argv command line arguments
2894 * @return 0 ok, 1 on error
2897 main (int argc, char *const *argv)
2899 return (GNUNET_OK ==
2900 GNUNET_SERVICE_run (argc,
2903 GNUNET_SERVICE_OPTION_NONE,
2904 &run, NULL)) ? 0 : 1;
2907 /* end of gnunet-service-rps.c */