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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 * Flags indicating status of peer
147 * Message queue open to client
149 struct GNUNET_MQ_Handle *mq;
152 * Channel open to client.
154 struct GNUNET_CADET_Channel *send_channel;
157 * Channel open from client.
159 struct GNUNET_CADET_Channel *recv_channel; // unneeded?
162 * Array of outstanding operations on this peer.
164 struct PeerOutstandingOp *outstanding_ops;
167 * Number of outstanding operations.
169 unsigned int num_outstanding_ops;
170 //size_t num_outstanding_ops;
173 * Handle to the callback given to cadet_ntfy_tmt_rdy()
175 * To be canceled on shutdown.
177 struct GNUNET_CADET_TransmitHandle *is_live_task;
180 * Identity of the peer
182 struct GNUNET_PeerIdentity peer_id;
185 * This is pobably followed by 'statistical' data (when we first saw
186 * him, how did we get his ID, how many pushes (in a timeinterval),
191 /***********************************************************************
192 * /Housekeeping with peers
193 ***********************************************************************/
199 /***********************************************************************
201 ***********************************************************************/
204 * Sampler used for the Brahms protocol itself.
206 static struct RPS_Sampler *prot_sampler;
209 * Sampler used for the clients.
211 static struct RPS_Sampler *client_sampler;
214 * Set of all peers to keep track of them.
216 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
220 * Name to log view to
222 static char *file_name_view_log;
225 * The "local view" containing peers we learned from gossip and history
227 static struct GNUNET_CONTAINER_MultiPeerMap *view;
230 * An array containing the peers of the local view.
232 * This is created every time we send a pull reply if it has changed since the
233 * last pull reply we sent.
235 static struct GNUNET_PeerIdentity *view_array;
239 * The size of sampler we need to be able to satisfy the client's need of
242 static unsigned int sampler_size_client_need;
245 * The size of sampler we need to be able to satisfy the Brahms protocol's
246 * need of random peers.
248 * This is one minimum size the sampler grows to.
250 static unsigned int sampler_size_est_need;
254 * Percentage of total peer number in the view
255 * to send random PUSHes to
260 * Percentage of total peer number in the view
261 * to send random PULLs to
266 * The percentage gamma of history updates.
267 * Simply 1 - alpha - beta
272 * Identifier for the main task that runs periodically.
274 static struct GNUNET_SCHEDULER_Task *do_round_task;
277 * Time inverval the do_round task runs in.
279 static struct GNUNET_TIME_Relative round_interval;
284 * List to store peers received through pushes temporary.
286 * TODO -> multipeermap
288 static struct GNUNET_PeerIdentity *push_list;
291 * Size of the push_list;
293 static unsigned int push_list_size;
294 //size_t push_list_size;
297 * List to store peers received through pulls temporary.
299 * TODO -> multipeermap
301 static struct GNUNET_PeerIdentity *pull_list;
304 * Size of the pull_list;
306 static unsigned int pull_list_size;
307 //size_t pull_list_size;
313 static struct GNUNET_NSE_Handle *nse;
318 static struct GNUNET_CADET_Handle *cadet_handle;
321 * Handler to PEERINFO.
323 static struct GNUNET_PEERINFO_Handle *peerinfo_handle;
326 * Handle for cancellation of iteration over peers.
328 struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify_handle;
334 * Only needed in the beginning to check how many of the 64 deltas
337 static unsigned int req_counter;
340 * Time of the last request we received.
342 * Used to compute the expected request rate.
344 static struct GNUNET_TIME_Absolute last_request;
347 * Size of #request_deltas.
349 #define REQUEST_DELTAS_SIZE 64
350 static unsigned int request_deltas_size = REQUEST_DELTAS_SIZE;
353 * Last 64 deltas between requests
355 static struct GNUNET_TIME_Relative request_deltas[REQUEST_DELTAS_SIZE];
358 * The prediction of the rate of requests
360 static struct GNUNET_TIME_Relative request_rate;
364 * Number of history update tasks.
366 uint32_t num_hist_update_tasks;
370 * Closure used to pass the client and the id to the callback
371 * that replies to a client's request
376 * The identifier of the request
381 * The client handle to send the reply to
383 struct GNUNET_SERVER_Client *client;
387 #ifdef ENABLE_MALICIOUS
389 * Type of malicious peer
391 * 0 Don't act malicious at all - Default
392 * 1 Try to maximise representation
393 * 2 Try to partition the network
396 uint32_t mal_type = 0;
399 * Other malicious peers
401 static struct GNUNET_PeerIdentity *mal_peers = NULL;
404 * Hashmap of malicious peers used as set.
405 * Used to more efficiently check whether we know that peer.
407 static struct GNUNET_CONTAINER_MultiPeerMap *mal_peer_set = NULL;
410 * Number of other malicious peers
412 static uint32_t num_mal_peers = 0;
416 * If type is 2 This struct is used to store the attacked peers in a DLL
423 struct AttackedPeer *next;
424 struct AttackedPeer *prev;
429 struct GNUNET_PeerIdentity peer_id;
433 * If type is 2 this is the DLL of attacked peers
435 static struct AttackedPeer *att_peers_head = NULL;
436 static struct AttackedPeer *att_peers_tail = NULL;
439 * This index is used to point to an attacked peer to
440 * implement the round-robin-ish way to select attacked peers.
442 static struct AttackedPeer *att_peer_index = NULL;
445 * Hashmap of attacked peers used as set.
446 * Used to more efficiently check whether we know that peer.
448 static struct GNUNET_CONTAINER_MultiPeerMap *att_peer_set = NULL;
451 * Number of attacked peers
453 static uint32_t num_attacked_peers = 0;
457 * If type is 1 this is the attacked peer
459 static struct GNUNET_PeerIdentity attacked_peer;
462 * The limit of PUSHes we can send in one round.
463 * This is an assumption of the Brahms protocol and either implemented
466 * assumend to be the bandwidth limitation.
468 static uint32_t push_limit = 10000;
469 #endif /* ENABLE_MALICIOUS */
472 /***********************************************************************
474 ***********************************************************************/
481 /***********************************************************************
483 ***********************************************************************/
486 * Set a peer flag of given peer context.
488 #define set_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags |= mask)
491 * Get peer flag of given peer context.
493 #define get_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags & mask ? GNUNET_YES : GNUNET_NO)
496 * Unset flag of given peer context.
498 #define unset_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags &= (~mask))
501 * Clean the send channel of a peer
504 peer_clean (const struct GNUNET_PeerIdentity *peer);
508 * Check if peer is already in peer array.
511 in_arr (const struct GNUNET_PeerIdentity *array,
512 unsigned int arr_size,
513 const struct GNUNET_PeerIdentity *peer)
515 GNUNET_assert (NULL != peer);
520 GNUNET_assert (NULL != array);
524 for (i = 0; i < arr_size ; i++)
525 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&array[i], peer))
532 * Print peerlist to log.
535 print_peer_list (struct GNUNET_PeerIdentity *list, unsigned int len)
539 LOG (GNUNET_ERROR_TYPE_DEBUG,
540 "Printing peer list of length %u at %p:\n",
543 for (i = 0 ; i < len ; i++)
545 LOG (GNUNET_ERROR_TYPE_DEBUG,
547 i, GNUNET_i2s (&list[i]));
553 * Remove peer from list.
556 rem_from_list (struct GNUNET_PeerIdentity **peer_list,
557 unsigned int *list_size,
558 const struct GNUNET_PeerIdentity *peer)
561 struct GNUNET_PeerIdentity *tmp;
565 LOG (GNUNET_ERROR_TYPE_DEBUG,
566 "Removing peer %s from list at %p\n",
570 for ( i = 0 ; i < *list_size ; i++ )
572 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&tmp[i], peer))
574 if (i < *list_size -1)
575 { /* Not at the last entry -- shift peers left */
576 memcpy (&tmp[i], &tmp[i +1],
577 ((*list_size) - i -1) * sizeof (struct GNUNET_PeerIdentity));
579 /* Remove last entry (should be now useless PeerID) */
580 GNUNET_array_grow (tmp, *list_size, (*list_size) -1);
587 * Get random peer from the given list but don't return one from the @a ignore_list.
589 struct GNUNET_PeerIdentity *
590 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list,
592 const struct GNUNET_PeerIdentity *ignore_list,
593 uint32_t ignore_size)
597 struct GNUNET_PeerIdentity *tmp_peer_list;
598 struct GNUNET_PeerIdentity *peer;
600 GNUNET_assert (NULL != peer_list);
605 tmp_peer_list = NULL;
606 GNUNET_array_grow (tmp_peer_list, tmp_size, list_size);
607 memcpy (tmp_peer_list,
609 list_size * sizeof (struct GNUNET_PeerIdentity));
610 peer = GNUNET_new (struct GNUNET_PeerIdentity);
613 * Choose the r_index of the peer we want to return
614 * at random from the interval of the view
616 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
618 *peer = tmp_peer_list[r_index];
620 while (in_arr (ignore_list, ignore_size, peer))
622 rem_from_list (&tmp_peer_list, &tmp_size, peer);
624 print_peer_list (tmp_peer_list, tmp_size);
633 * Choose the r_index of the peer we want to return
634 * at random from the interval of the view
636 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
638 *peer = tmp_peer_list[r_index];
642 GNUNET_array_grow (tmp_peer_list, tmp_size, 0);
649 * Get the context of a peer. If not existing, create.
652 get_peer_ctx (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
653 const struct GNUNET_PeerIdentity *peer)
655 struct PeerContext *ctx;
657 if ( GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
659 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
663 ctx = GNUNET_new (struct PeerContext);
666 ctx->send_channel = NULL;
667 ctx->recv_channel = NULL;
668 ctx->outstanding_ops = NULL;
669 ctx->num_outstanding_ops = 0;
670 ctx->is_live_task = NULL;
671 ctx->peer_id = *peer;
672 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
673 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
680 * Put random peer from sampler into the view as history update.
683 hist_update (void *cls, struct GNUNET_PeerIdentity *ids, uint32_t num_peers)
687 for (i = 0; i < GNUNET_MIN (
688 sampler_size_est_need - GNUNET_CONTAINER_multipeermap_size (view),
691 if (GNUNET_OK != GNUNET_CONTAINER_multipeermap_put (view,
694 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
696 LOG (GNUNET_ERROR_TYPE_WARNING,
697 "Failed to put peer in peermap. (hist_update)\n");
700 /* Might want to check that we really updated the view */
701 if (NULL != view_array)
703 GNUNET_free (view_array);
707 to_file (file_name_view_log,
709 GNUNET_i2s_full (ids));
712 if (0 < num_hist_update_tasks)
713 num_hist_update_tasks--;
718 * Set the peer flag to living and call the outstanding operations on this peer.
721 peer_is_live (struct PeerContext *peer_ctx)
723 struct GNUNET_PeerIdentity *peer;
725 /* Cancle is_live_task if still scheduled */
726 if (NULL != peer_ctx->is_live_task)
728 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
729 peer_ctx->is_live_task = NULL;
732 peer = &peer_ctx->peer_id;
733 set_peer_flag (peer_ctx, VALID);
735 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer %s is live\n", GNUNET_i2s (peer));
737 if (0 < peer_ctx->num_outstanding_ops)
738 { /* Call outstanding operations */
741 for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
742 peer_ctx->outstanding_ops[i].op (peer_ctx->outstanding_ops[i].op_cls, peer);
743 GNUNET_array_grow (peer_ctx->outstanding_ops, peer_ctx->num_outstanding_ops, 0);
751 * Callback that is called when a channel was effectively established.
752 * This is given to ntfy_tmt_rdy and called when the channel was
753 * successfully established.
756 cadet_ntfy_tmt_rdy_cb (void *cls, size_t size, void *buf)
758 struct PeerContext *peer_ctx = (struct PeerContext *) cls;
760 peer_ctx->is_live_task = NULL;
761 LOG (GNUNET_ERROR_TYPE_DEBUG,
762 "Set ->is_live_task = NULL for peer %s\n",
763 GNUNET_i2s (&peer_ctx->peer_id));
768 peer_is_live (peer_ctx);
772 LOG (GNUNET_ERROR_TYPE_WARNING,
773 "Problems establishing a connection to peer %s in order to check liveliness\n",
774 GNUNET_i2s (&peer_ctx->peer_id));
775 // TODO reschedule? cleanup?
778 //if (NULL != peer_ctx->is_live_task)
780 // LOG (GNUNET_ERROR_TYPE_DEBUG,
781 // "Trying to cancle is_live_task for peer %s\n",
782 // GNUNET_i2s (&peer_ctx->peer_id));
783 // GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
784 // peer_ctx->is_live_task = NULL;
792 * Get the channel of a peer. If not existing, create.
794 struct GNUNET_CADET_Channel *
795 get_channel (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
796 const struct GNUNET_PeerIdentity *peer)
798 struct PeerContext *peer_ctx;
800 peer_ctx = get_peer_ctx (peer_map, peer);
802 if (NULL == peer_ctx->send_channel)
804 LOG (GNUNET_ERROR_TYPE_DEBUG,
805 "Trying to establish channel to peer %s\n",
808 peer_ctx->send_channel =
809 GNUNET_CADET_channel_create (cadet_handle,
812 GNUNET_RPS_CADET_PORT,
813 GNUNET_CADET_OPTION_RELIABLE);
815 // do I have to explicitly put it in the peer_map?
816 (void) GNUNET_CONTAINER_multipeermap_put
820 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
822 return peer_ctx->send_channel;
827 * Get the message queue of a specific peer.
829 * If we already have a message queue open to this client,
830 * simply return it, otherways create one.
832 struct GNUNET_MQ_Handle *
833 get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
834 const struct GNUNET_PeerIdentity *peer_id)
836 struct PeerContext *peer_ctx;
838 peer_ctx = get_peer_ctx (peer_map, peer_id);
840 GNUNET_assert (NULL == peer_ctx->is_live_task);
842 if (NULL == peer_ctx->mq)
844 (void) get_channel (peer_map, peer_id);
845 peer_ctx->mq = GNUNET_CADET_mq_create (peer_ctx->send_channel);
846 //do I have to explicitly put it in the peer_map?
847 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer_id, peer_ctx,
848 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
855 * Issue check whether peer is live
857 * @param peer_ctx the context of the peer
860 check_peer_live (struct PeerContext *peer_ctx)
862 (void) get_channel (peer_map, &peer_ctx->peer_id);
863 LOG (GNUNET_ERROR_TYPE_DEBUG,
864 "Get informed about peer %s getting live\n",
865 GNUNET_i2s (&peer_ctx->peer_id));
866 if (NULL == peer_ctx->is_live_task)
868 peer_ctx->is_live_task =
869 GNUNET_CADET_notify_transmit_ready (peer_ctx->send_channel,
871 GNUNET_TIME_UNIT_FOREVER_REL,
872 sizeof (struct GNUNET_MessageHeader),
873 cadet_ntfy_tmt_rdy_cb,
875 (void) GNUNET_CONTAINER_multipeermap_put (peer_map,
878 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
882 LOG (GNUNET_ERROR_TYPE_DEBUG,
883 "Already waiting for notification\n");
889 * Sum all time relatives of an array.
891 struct GNUNET_TIME_Relative
892 T_relative_sum (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
894 struct GNUNET_TIME_Relative sum;
897 sum = GNUNET_TIME_UNIT_ZERO;
898 for ( i = 0 ; i < arr_size ; i++ )
900 sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
907 * Compute the average of given time relatives.
909 struct GNUNET_TIME_Relative
910 T_relative_avg (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
912 return GNUNET_TIME_relative_divide (T_relative_sum (rel_array, arr_size), arr_size);
917 * Insert PeerID in #pull_list
919 * Called once we know a peer is live.
922 insert_in_pull_list (void *cls, const struct GNUNET_PeerIdentity *peer)
924 if (GNUNET_NO == in_arr (pull_list, pull_list_size, peer))
925 GNUNET_array_append (pull_list, pull_list_size, *peer);
931 * Check whether #insert_in_pull_list was already scheduled
934 insert_in_pull_list_scheduled (const struct PeerContext *peer_ctx)
938 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
939 if (insert_in_pull_list == peer_ctx->outstanding_ops[i].op)
946 * Insert PeerID in #view
948 * Called once we know a peer is live.
951 insert_in_view (void *cls, const struct GNUNET_PeerIdentity *peer)
953 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_put (view,
956 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
958 LOG (GNUNET_ERROR_TYPE_WARNING,
959 "Failed to put peer into view. (insert_in_view)\n");
962 /* Might want to check whether we really modified the view */
963 if (NULL != view_array)
965 GNUNET_free (view_array);
969 (void) get_channel (peer_map, peer);
973 * Check whether #insert_in_view was already scheduled
976 insert_in_view_scheduled (const struct PeerContext *peer_ctx)
980 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
981 if (insert_in_view == peer_ctx->outstanding_ops[i].op)
988 * Update sampler with given PeerID.
991 insert_in_sampler (void *cls, const struct GNUNET_PeerIdentity *peer)
993 LOG (GNUNET_ERROR_TYPE_DEBUG,
994 "Updating samplers with peer %s from insert_in_sampler()\n",
996 RPS_sampler_update (prot_sampler, peer);
997 RPS_sampler_update (client_sampler, peer);
1002 * Check whether #insert_in_sampler was already scheduled
1005 insert_in_sampler_scheduled (const struct PeerContext *peer_ctx)
1009 for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
1010 if (insert_in_sampler== peer_ctx->outstanding_ops[i].op)
1017 * Wrapper around #RPS_sampler_resize()
1019 * If we do not have enough sampler elements, double current sampler size
1020 * If we have more than enough sampler elements, halv current sampler size
1023 resize_wrapper (struct RPS_Sampler *sampler, uint32_t new_size)
1025 unsigned int sampler_size;
1028 // TODO respect the min, max
1029 sampler_size = RPS_sampler_get_size (sampler);
1030 if (sampler_size > new_size * 4)
1032 RPS_sampler_resize (sampler, sampler_size / 2);
1034 else if (sampler_size < new_size)
1036 RPS_sampler_resize (sampler, sampler_size * 2);
1038 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
1043 * Wrapper around #RPS_sampler_resize() resizing the client sampler
1046 client_resize_wrapper ()
1048 uint32_t bigger_size;
1049 unsigned int sampler_size;
1053 sampler_size = RPS_sampler_get_size (client_sampler);
1055 if (sampler_size_est_need > sampler_size_client_need)
1056 bigger_size = sampler_size_est_need;
1058 bigger_size = sampler_size_client_need;
1060 // TODO respect the min, max
1061 resize_wrapper (client_sampler, bigger_size);
1062 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
1067 * Estimate request rate
1069 * Called every time we receive a request from the client.
1074 struct GNUNET_TIME_Relative max_round_duration;
1076 if (request_deltas_size > req_counter)
1078 if ( 1 < req_counter)
1080 /* Shift last request deltas to the right */
1081 memcpy (&request_deltas[1],
1083 (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
1085 /* Add current delta to beginning */
1087 GNUNET_TIME_absolute_get_difference (last_request,
1088 GNUNET_TIME_absolute_get ());
1089 request_rate = T_relative_avg (request_deltas, req_counter);
1091 /* Compute the duration a round will maximally take */
1092 max_round_duration =
1093 GNUNET_TIME_relative_add (round_interval,
1094 GNUNET_TIME_relative_divide (round_interval, 2));
1096 /* Set the estimated size the sampler has to have to
1097 * satisfy the current client request rate */
1098 sampler_size_client_need =
1099 max_round_duration.rel_value_us / request_rate.rel_value_us;
1101 /* Resize the sampler */
1102 client_resize_wrapper ();
1104 last_request = GNUNET_TIME_absolute_get ();
1109 * Add all peers in @a peer_array to @a peer_map used as set.
1111 * @param peer_array array containing the peers
1112 * @param num_peers number of peers in @peer_array
1113 * @param peer_map the peermap to use as set
1116 add_peer_array_to_set (const struct GNUNET_PeerIdentity *peer_array,
1117 unsigned int num_peers,
1118 struct GNUNET_CONTAINER_MultiPeerMap *peer_map)
1121 if (NULL == peer_map)
1123 LOG (GNUNET_ERROR_TYPE_WARNING,
1124 "Trying to add peers to an empty peermap.\n");
1128 for (i = 0; i < num_peers; i++)
1130 GNUNET_CONTAINER_multipeermap_put (peer_map,
1133 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1139 * Send a PULL REPLY to @a peer_id
1141 * @param peer_id the peer to send the reply to.
1142 * @param peer_ids the peers to send to @a peer_id
1143 * @param num_peer_ids the number of peers to send to @a peer_id
1146 send_pull_reply (const struct GNUNET_PeerIdentity *peer_id,
1147 const struct GNUNET_PeerIdentity *peer_ids,
1148 unsigned int num_peer_ids)
1151 struct GNUNET_MQ_Handle *mq;
1152 struct GNUNET_MQ_Envelope *ev;
1153 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
1155 /* Compute actual size */
1156 send_size = sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) +
1157 num_peer_ids * sizeof (struct GNUNET_PeerIdentity);
1159 if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size)
1160 /* Compute number of peers to send
1161 * If too long, simply truncate */
1162 // TODO select random ones via permutation
1163 // or even better: do good protocol design
1165 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE -
1166 sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1167 sizeof (struct GNUNET_PeerIdentity);
1169 send_size = num_peer_ids;
1171 LOG (GNUNET_ERROR_TYPE_DEBUG,
1172 "PULL REQUEST from peer %s received, going to send %u peers\n",
1173 GNUNET_i2s (peer_id), send_size);
1175 mq = get_mq (peer_map, peer_id);
1177 ev = GNUNET_MQ_msg_extra (out_msg,
1178 send_size * sizeof (struct GNUNET_PeerIdentity),
1179 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
1180 out_msg->num_peers = htonl (send_size);
1181 memcpy (&out_msg[1], peer_ids,
1182 send_size * sizeof (struct GNUNET_PeerIdentity));
1184 GNUNET_MQ_send (mq, ev);
1189 * This function is called on new peer_ids from 'external' sources
1190 * (client seed, cadet get_peers(), ...)
1192 * @param peer_id the new peer_id
1195 new_peer_id (const struct GNUNET_PeerIdentity *peer_id)
1197 struct PeerOutstandingOp out_op;
1198 struct PeerContext *peer_ctx;
1200 if (NULL != peer_id &&
1201 0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, peer_id))
1203 LOG (GNUNET_ERROR_TYPE_DEBUG,
1204 "Got peer_id %s (at %p, view size: %u)\n",
1205 GNUNET_i2s (peer_id),
1207 GNUNET_CONTAINER_multipeermap_size (view));
1209 peer_ctx = get_peer_ctx (peer_map, peer_id);
1210 if (GNUNET_YES != get_peer_flag (peer_ctx, VALID))
1212 if (GNUNET_NO == insert_in_sampler_scheduled (peer_ctx))
1214 out_op.op = insert_in_sampler;
1215 out_op.op_cls = NULL;
1216 GNUNET_array_append (peer_ctx->outstanding_ops,
1217 peer_ctx->num_outstanding_ops,
1221 if (GNUNET_NO == insert_in_view_scheduled (peer_ctx))
1223 out_op.op = insert_in_view;
1224 out_op.op_cls = NULL;
1225 GNUNET_array_append (peer_ctx->outstanding_ops,
1226 peer_ctx->num_outstanding_ops,
1230 /* Trigger livelyness test on peer */
1231 check_peer_live (peer_ctx);
1235 // send push/pull to each of those peers?
1240 /***********************************************************************
1242 ***********************************************************************/
1249 * Function called by NSE.
1251 * Updates sizes of sampler list and view and adapt those lists
1255 nse_callback (void *cls, struct GNUNET_TIME_Absolute timestamp,
1256 double logestimate, double std_dev)
1259 //double scale; // TODO this might go gloabal/config
1261 LOG (GNUNET_ERROR_TYPE_DEBUG,
1262 "Received a ns estimate - logest: %f, std_dev: %f (old_size: %u)\n",
1263 logestimate, std_dev, RPS_sampler_get_size (prot_sampler));
1265 estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
1266 // GNUNET_NSE_log_estimate_to_n (logestimate);
1267 estimate = pow (estimate, 1.0 / 3);
1268 // TODO add if std_dev is a number
1269 // estimate += (std_dev * scale);
1270 if (2 < ceil (estimate))
1272 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
1273 sampler_size_est_need = estimate;
1275 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
1277 /* If the NSE has changed adapt the lists accordingly */
1278 resize_wrapper (prot_sampler, sampler_size_est_need);
1279 client_resize_wrapper ();
1284 * Callback called once the requested PeerIDs are ready.
1286 * Sends those to the requesting client.
1288 void client_respond (void *cls,
1289 struct GNUNET_PeerIdentity *peer_ids, uint32_t num_peers)
1291 struct GNUNET_MQ_Envelope *ev;
1292 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
1293 struct ReplyCls *reply_cls = (struct ReplyCls *) cls;
1294 uint32_t size_needed;
1295 struct ClientContext *cli_ctx;
1297 LOG (GNUNET_ERROR_TYPE_DEBUG,
1298 "sampler returned %" PRIu32 " peers\n",
1301 size_needed = sizeof (struct GNUNET_RPS_CS_ReplyMessage) +
1302 num_peers * sizeof (struct GNUNET_PeerIdentity);
1304 GNUNET_assert (GNUNET_SERVER_MAX_MESSAGE_SIZE >= size_needed);
1306 ev = GNUNET_MQ_msg_extra (out_msg,
1307 num_peers * sizeof (struct GNUNET_PeerIdentity),
1308 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
1309 out_msg->num_peers = htonl (num_peers);
1310 out_msg->id = htonl (reply_cls->id);
1312 memcpy (&out_msg[1],
1314 num_peers * sizeof (struct GNUNET_PeerIdentity));
1315 GNUNET_free (peer_ids);
1317 cli_ctx = GNUNET_SERVER_client_get_user_context (reply_cls->client, struct ClientContext);
1318 if (NULL == cli_ctx) {
1319 cli_ctx = GNUNET_new (struct ClientContext);
1320 cli_ctx->mq = GNUNET_MQ_queue_for_server_client (reply_cls->client);
1321 GNUNET_SERVER_client_set_user_context (reply_cls->client, cli_ctx);
1324 GNUNET_free (reply_cls);
1326 GNUNET_MQ_send (cli_ctx->mq, ev);
1331 * Handle RPS request from the client.
1333 * @param cls closure
1334 * @param client identification of the client
1335 * @param message the actual message
1338 handle_client_request (void *cls,
1339 struct GNUNET_SERVER_Client *client,
1340 const struct GNUNET_MessageHeader *message)
1342 struct GNUNET_RPS_CS_RequestMessage *msg;
1344 uint32_t size_needed;
1345 struct ReplyCls *reply_cls;
1348 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
1350 num_peers = ntohl (msg->num_peers);
1351 size_needed = sizeof (struct GNUNET_RPS_CS_RequestMessage) +
1352 num_peers * sizeof (struct GNUNET_PeerIdentity);
1354 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
1356 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1360 for (i = 0 ; i < num_peers ; i++)
1363 LOG (GNUNET_ERROR_TYPE_DEBUG,
1364 "Client requested %" PRIu32 " random peer(s).\n",
1367 reply_cls = GNUNET_new (struct ReplyCls);
1368 reply_cls->id = ntohl (msg->id);
1369 reply_cls->client = client;
1371 RPS_sampler_get_n_rand_peers (client_sampler,
1376 GNUNET_SERVER_receive_done (client,
1382 * Handle seed from the client.
1384 * @param cls closure
1385 * @param client identification of the client
1386 * @param message the actual message
1389 handle_client_seed (void *cls,
1390 struct GNUNET_SERVER_Client *client,
1391 const struct GNUNET_MessageHeader *message)
1393 struct GNUNET_RPS_CS_SeedMessage *in_msg;
1394 struct GNUNET_PeerIdentity *peers;
1398 if (sizeof (struct GNUNET_RPS_CS_SeedMessage) > ntohs (message->size))
1400 GNUNET_break_op (0);
1401 GNUNET_SERVER_receive_done (client,
1405 in_msg = (struct GNUNET_RPS_CS_SeedMessage *) message;
1406 num_peers = ntohl (in_msg->num_peers);
1407 peers = (struct GNUNET_PeerIdentity *) &in_msg[1];
1408 //peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1409 //memcpy (peers, &in_msg[1], num_peers * sizeof (struct GNUNET_PeerIdentity));
1411 if ((ntohs (message->size) - sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
1412 sizeof (struct GNUNET_PeerIdentity) != num_peers)
1414 GNUNET_break_op (0);
1415 GNUNET_SERVER_receive_done (client,
1419 LOG (GNUNET_ERROR_TYPE_DEBUG,
1420 "Client seeded peers:\n");
1421 print_peer_list (peers, num_peers);
1423 for (i = 0 ; i < num_peers ; i++)
1425 LOG (GNUNET_ERROR_TYPE_DEBUG,
1426 "Updating samplers with seed %" PRIu32 ": %s\n",
1428 GNUNET_i2s (&peers[i]));
1430 new_peer_id (&peers[i]);
1432 //RPS_sampler_update (prot_sampler, &peers[i]);
1433 //RPS_sampler_update (client_sampler, &peers[i]);
1436 ////GNUNET_free (peers);
1438 GNUNET_SERVER_receive_done (client,
1444 * Handle a PUSH message from another peer.
1446 * Check the proof of work and store the PeerID
1447 * in the temporary list for pushed PeerIDs.
1449 * @param cls Closure
1450 * @param channel The channel the PUSH was received over
1451 * @param channel_ctx The context associated with this channel
1452 * @param msg The message header
1455 handle_peer_push (void *cls,
1456 struct GNUNET_CADET_Channel *channel,
1458 const struct GNUNET_MessageHeader *msg)
1460 const struct GNUNET_PeerIdentity *peer;
1462 // (check the proof of work)
1464 peer = (const struct GNUNET_PeerIdentity *)
1465 GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
1466 // FIXME wait for cadet to change this function
1468 LOG (GNUNET_ERROR_TYPE_DEBUG, "PUSH received (%s)\n", GNUNET_i2s (peer));
1470 #ifdef ENABLE_MALICIOUS
1471 struct AttackedPeer *tmp_att_peer;
1473 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1474 memcpy (&tmp_att_peer->peer_id, peer, sizeof (struct GNUNET_PeerIdentity));
1477 { /* Try to maximise representation */
1478 if (NULL == att_peer_set)
1479 att_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
1480 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1483 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1486 add_peer_array_to_set (peer, 1, att_peer_set);
1492 else if (2 == mal_type)
1493 { /* We attack one single well-known peer - simply ignore */
1498 GNUNET_free (tmp_att_peer);
1501 #endif /* ENABLE_MALICIOUS */
1503 /* Add the sending peer to the push_list */
1504 if (GNUNET_NO == in_arr (push_list, push_list_size, peer))
1505 GNUNET_array_append (push_list, push_list_size, *peer);
1512 * Iterator over hash map entries.
1513 * Called from #generate_view_array and writes every peer id into #view_array.
1515 * @param cls closure - the pointer to the counter
1516 * @param key current public key
1517 * @param value value in the hash map
1518 * @return #GNUNET_YES if we should continue to
1520 * #GNUNET_NO if not.
1523 dump_id_to_view_array (void *cls,
1524 const struct GNUNET_PeerIdentity *key,
1527 unsigned int *i = (unsigned int *) cls;
1529 view_array[(*i)++] = *key;
1535 * Makes sure the view_array is filled with the peer ids currently in #view.
1536 * Called from within #do_round before sending pushes and pulls and from
1537 * #handle_peer_pull_request when a reply is sent.
1540 generate_view_array (unsigned int view_size)
1545 if (NULL == view_array)
1547 view_array = GNUNET_new_array (view_size,
1548 struct GNUNET_PeerIdentity);
1549 i = GNUNET_new (unsigned int);
1552 ret = GNUNET_CONTAINER_multipeermap_iterate (view,
1553 dump_id_to_view_array,
1555 GNUNET_assert (view_size == ret);
1556 GNUNET_assert (view_size == *i);
1564 * Handle PULL REQUEST request message from another peer.
1566 * Reply with the view of PeerIDs.
1568 * @param cls Closure
1569 * @param channel The channel the PUSH was received over
1570 * @param channel_ctx The context associated with this channel
1571 * @param msg The message header
1574 handle_peer_pull_request (void *cls,
1575 struct GNUNET_CADET_Channel *channel,
1577 const struct GNUNET_MessageHeader *msg)
1579 struct GNUNET_PeerIdentity *peer;
1580 unsigned int view_size;
1582 peer = (struct GNUNET_PeerIdentity *)
1583 GNUNET_CADET_channel_get_info (channel,
1584 GNUNET_CADET_OPTION_PEER);
1585 // FIXME wait for cadet to change this function
1587 LOG (GNUNET_ERROR_TYPE_DEBUG, "PULL REQUEST received (%s)\n", GNUNET_i2s (peer));
1589 #ifdef ENABLE_MALICIOUS
1592 { /* Try to maximise representation */
1593 send_pull_reply (peer, mal_peers, num_mal_peers);
1597 else if (2 == mal_type)
1598 { /* Try to partition network */
1599 if (GNUNET_YES == GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
1601 send_pull_reply (peer, mal_peers, num_mal_peers);
1605 #endif /* ENABLE_MALICIOUS */
1607 view_size = GNUNET_CONTAINER_multipeermap_size (view);
1608 generate_view_array (view_size);
1610 send_pull_reply (peer, view_array, view_size);
1617 * Handle PULL REPLY message from another peer.
1619 * Check whether we sent a corresponding request and
1620 * whether this reply is the first one.
1622 * @param cls Closure
1623 * @param channel The channel the PUSH was received over
1624 * @param channel_ctx The context associated with this channel
1625 * @param msg The message header
1628 handle_peer_pull_reply (void *cls,
1629 struct GNUNET_CADET_Channel *channel,
1631 const struct GNUNET_MessageHeader *msg)
1633 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
1634 struct GNUNET_PeerIdentity *peers;
1635 struct PeerContext *peer_ctx;
1636 struct GNUNET_PeerIdentity *sender;
1637 struct PeerContext *sender_ctx;
1638 struct PeerOutstandingOp out_op;
1640 #ifdef ENABLE_MALICIOUS
1641 struct AttackedPeer *tmp_att_peer;
1642 #endif /* ENABLE_MALICIOUS */
1644 /* Check for protocol violation */
1645 if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->size))
1647 GNUNET_break_op (0);
1648 return GNUNET_SYSERR;
1651 in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
1652 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1653 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1655 LOG (GNUNET_ERROR_TYPE_ERROR,
1656 "message says it sends %" PRIu64 " peers, have space for %i peers\n",
1657 ntohl (in_msg->num_peers),
1658 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1659 sizeof (struct GNUNET_PeerIdentity));
1660 GNUNET_break_op (0);
1661 return GNUNET_SYSERR;
1664 sender = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
1665 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
1666 // Guess simply casting isn't the nicest way...
1667 // FIXME wait for cadet to change this function
1668 sender_ctx = get_peer_ctx (peer_map, sender);
1670 LOG (GNUNET_ERROR_TYPE_DEBUG, "PULL REPLY received (%s)\n", GNUNET_i2s (sender));
1672 if (GNUNET_YES != get_peer_flag (sender_ctx, PULL_REPLY_PENDING))
1674 LOG (GNUNET_ERROR_TYPE_WARNING,
1675 "Received a pull reply from a peer we didn't request one from!\n");
1676 GNUNET_break_op (0);
1681 #ifdef ENABLE_MALICIOUS
1682 // We shouldn't even receive pull replies as we're not sending
1685 #endif /* ENABLE_MALICIOUS */
1687 /* Do actual logic */
1688 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1690 LOG (GNUNET_ERROR_TYPE_DEBUG,
1691 "PULL REPLY received, got following %u peers:\n",
1692 ntohl (in_msg->num_peers));
1694 for (i = 0 ; i < ntohl (in_msg->num_peers) ; i++)
1696 LOG (GNUNET_ERROR_TYPE_DEBUG,
1699 GNUNET_i2s (&peers[i]));
1701 #ifdef ENABLE_MALICIOUS
1704 { /* Add attacked peer to local list */
1705 // TODO check if we sent a request and this was the first reply
1706 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1708 && GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mal_peer_set,
1710 && GNUNET_NO == GNUNET_CRYPTO_cmp_peer_identity (&peers[i],
1713 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1714 tmp_att_peer->peer_id = peers[i];
1715 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1718 add_peer_array_to_set (&peers[i], 1, att_peer_set);
1722 #endif /* ENABLE_MALICIOUS */
1723 if (GNUNET_YES != GNUNET_CRYPTO_cmp_peer_identity (&own_identity,
1726 peer_ctx = get_peer_ctx (peer_map, &peers[i]);
1727 if (GNUNET_YES == get_peer_flag (peer_ctx, VALID) ||
1728 NULL != peer_ctx->send_channel ||
1729 NULL != peer_ctx->recv_channel)
1731 if (GNUNET_NO == in_arr (pull_list, pull_list_size, &peers[i]))
1732 GNUNET_array_append (pull_list, pull_list_size, peers[i]);
1734 else if (GNUNET_NO == insert_in_pull_list_scheduled (peer_ctx))
1736 out_op.op = insert_in_pull_list;
1737 out_op.op_cls = NULL;
1738 GNUNET_array_append (peer_ctx->outstanding_ops,
1739 peer_ctx->num_outstanding_ops,
1741 check_peer_live (peer_ctx);
1746 unset_peer_flag (sender_ctx, PULL_REPLY_PENDING);
1753 * Compute a random delay.
1754 * A uniformly distributed value between mean + spread and mean - spread.
1756 * For example for mean 4 min and spread 2 the minimum is (4 min - (1/2 * 4 min))
1757 * It would return a random value between 2 and 6 min.
1759 * @param mean the mean
1760 * @param spread the inverse amount of deviation from the mean
1762 static struct GNUNET_TIME_Relative
1763 compute_rand_delay (struct GNUNET_TIME_Relative mean, unsigned int spread)
1765 struct GNUNET_TIME_Relative half_interval;
1766 struct GNUNET_TIME_Relative ret;
1767 unsigned int rand_delay;
1768 unsigned int max_rand_delay;
1772 LOG (GNUNET_ERROR_TYPE_WARNING,
1773 "Not accepting spread of 0\n");
1777 /* Compute random time value between spread * mean and spread * mean */
1778 half_interval = GNUNET_TIME_relative_divide (mean, spread);
1780 max_rand_delay = GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us / mean.rel_value_us * (2/spread);
1782 * Compute random value between (0 and 1) * round_interval
1783 * via multiplying round_interval with a 'fraction' (0 to value)/value
1785 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_rand_delay);
1786 ret = GNUNET_TIME_relative_multiply (mean, rand_delay);
1787 ret = GNUNET_TIME_relative_divide (ret, max_rand_delay);
1788 ret = GNUNET_TIME_relative_add (ret, half_interval);
1790 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ret.rel_value_us)
1791 LOG (GNUNET_ERROR_TYPE_WARNING,
1792 "Returning FOREVER_REL\n");
1799 * Send single pull request
1801 * @param peer_id the peer to send the pull request to.
1804 send_pull_request (struct GNUNET_PeerIdentity *peer_id)
1806 struct GNUNET_MQ_Envelope *ev;
1807 struct GNUNET_MQ_Handle *mq;
1808 struct PeerContext *peer_ctx;
1810 peer_ctx = get_peer_ctx (peer_map, peer_id);
1811 GNUNET_assert (GNUNET_NO == get_peer_flag (peer_ctx, PULL_REPLY_PENDING));
1812 set_peer_flag (peer_ctx, PULL_REPLY_PENDING);
1814 LOG (GNUNET_ERROR_TYPE_DEBUG,
1815 "Sending PULL request to peer %s of view.\n",
1816 GNUNET_i2s (peer_id));
1818 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
1819 mq = get_mq (peer_map, peer_id);
1820 GNUNET_MQ_send (mq, ev);
1827 * @param peer_id the peer to send the push to.
1830 send_push (struct GNUNET_PeerIdentity *peer_id)
1832 struct GNUNET_MQ_Envelope *ev;
1833 struct GNUNET_MQ_Handle *mq;
1835 LOG (GNUNET_ERROR_TYPE_DEBUG,
1836 "Sending PUSH to peer %s of view.\n",
1837 GNUNET_i2s (peer_id));
1839 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
1840 mq = get_mq (peer_map, peer_id);
1841 GNUNET_MQ_send (mq, ev);
1846 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1849 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1852 #ifdef ENABLE_MALICIOUS
1854 * Turn RPS service to act malicious.
1856 * @param cls Closure
1857 * @param channel The channel the PUSH was received over
1858 * @param channel_ctx The context associated with this channel
1859 * @param msg The message header
1862 handle_client_act_malicious (void *cls,
1863 struct GNUNET_SERVER_Client *client,
1864 const struct GNUNET_MessageHeader *msg)
1866 struct GNUNET_RPS_CS_ActMaliciousMessage *in_msg;
1867 struct GNUNET_PeerIdentity *peers;
1868 uint32_t num_mal_peers_sent;
1869 uint32_t num_mal_peers_old;
1871 /* Check for protocol violation */
1872 if (sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage) > ntohs (msg->size))
1874 GNUNET_break_op (0);
1877 in_msg = (struct GNUNET_RPS_CS_ActMaliciousMessage *) msg;
1878 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
1879 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1881 LOG (GNUNET_ERROR_TYPE_ERROR,
1882 "message says it sends %" PRIu64 " peers, have space for %i peers\n",
1883 ntohl (in_msg->num_peers),
1884 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
1885 sizeof (struct GNUNET_PeerIdentity));
1886 GNUNET_break_op (0);
1890 /* Do actual logic */
1891 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1892 mal_type = ntohl (in_msg->type);
1894 LOG (GNUNET_ERROR_TYPE_DEBUG,
1895 "Now acting malicious type %" PRIu32 "\n",
1899 { /* Try to maximise representation */
1900 /* Add other malicious peers to those we already know */
1902 num_mal_peers_sent = ntohl (in_msg->num_peers);
1903 num_mal_peers_old = num_mal_peers;
1904 GNUNET_array_grow (mal_peers,
1906 num_mal_peers + num_mal_peers_sent);
1907 memcpy (&mal_peers[num_mal_peers_old],
1909 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1911 /* Add all mal peers to mal_peer_set */
1912 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1916 /* Substitute do_round () with do_mal_round () */
1917 GNUNET_SCHEDULER_cancel (do_round_task);
1918 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1921 else if (2 == mal_type
1923 { /* Try to partition the network */
1924 /* Add other malicious peers to those we already know */
1925 num_mal_peers_sent = ntohl (in_msg->num_peers) - 1;
1926 num_mal_peers_old = num_mal_peers;
1927 GNUNET_array_grow (mal_peers,
1929 num_mal_peers + num_mal_peers_sent);
1930 memcpy (&mal_peers[num_mal_peers_old],
1932 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1934 /* Add all mal peers to mal_peer_set */
1935 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1939 /* Store the one attacked peer */
1940 memcpy (&attacked_peer,
1941 &in_msg->attacked_peer,
1942 sizeof (struct GNUNET_PeerIdentity));
1944 LOG (GNUNET_ERROR_TYPE_DEBUG,
1945 "Attacked peer is %s\n",
1946 GNUNET_i2s (&attacked_peer));
1948 /* Substitute do_round () with do_mal_round () */
1949 GNUNET_SCHEDULER_cancel (do_round_task);
1950 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1952 else if (0 == mal_type)
1953 { /* Stop acting malicious */
1954 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
1956 /* Substitute do_mal_round () with do_round () */
1957 GNUNET_SCHEDULER_cancel (do_round_task);
1958 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
1965 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1970 * Send out PUSHes and PULLs maliciously.
1972 * This is executed regylary.
1975 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1977 uint32_t num_pushes;
1979 struct GNUNET_TIME_Relative time_next_round;
1980 struct AttackedPeer *tmp_att_peer;
1982 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round maliciously.\n");
1984 /* Do malicious actions */
1986 { /* Try to maximise representation */
1988 /* The maximum of pushes we're going to send this round */
1989 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit,
1990 num_attacked_peers),
1991 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
1993 LOG (GNUNET_ERROR_TYPE_DEBUG,
1994 "Going to send %" PRIu32 " pushes\n",
1997 /* Send PUSHes to attacked peers */
1998 for (i = 0 ; i < num_pushes ; i++)
2000 if (att_peers_tail == att_peer_index)
2001 att_peer_index = att_peers_head;
2003 att_peer_index = att_peer_index->next;
2005 send_push (&att_peer_index->peer_id);
2008 /* Send PULLs to some peers to learn about additional peers to attack */
2009 tmp_att_peer = att_peer_index;
2010 for (i = 0 ; i < num_pushes * alpha ; i++)
2012 if (att_peers_tail == tmp_att_peer)
2013 tmp_att_peer = att_peers_head;
2015 att_peer_index = tmp_att_peer->next;
2017 send_pull_request (&tmp_att_peer->peer_id);
2022 else if (2 == mal_type)
2024 * Try to partition the network
2025 * Send as many pushes to the attacked peer as possible
2026 * That is one push per round as it will ignore more.
2028 send_push (&attacked_peer);
2033 { /* Combined attack */
2035 /* The maximum of pushes we're going to send this round */
2036 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit - 1,
2037 num_attacked_peers),
2038 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
2040 LOG (GNUNET_ERROR_TYPE_DEBUG,
2041 "Going to send %" PRIu32 " pushes\n",
2044 /* Send PUSHes to attacked peers */
2045 send_push (&attacked_peer);
2047 for (i = 0 ; i < num_pushes ; i++)
2049 if (att_peers_tail == att_peer_index)
2050 att_peer_index = att_peers_head;
2052 att_peer_index = att_peer_index->next;
2054 send_push (&att_peer_index->peer_id);
2057 /* Send PULLs to some peers to learn about additional peers to attack */
2058 tmp_att_peer = att_peer_index;
2059 for (i = 0 ; i < num_pushes * alpha ; i++)
2061 if (att_peers_tail == tmp_att_peer)
2062 tmp_att_peer = att_peers_head;
2064 att_peer_index = tmp_att_peer->next;
2066 send_pull_request (&tmp_att_peer->peer_id);
2070 /* Schedule next round */
2071 time_next_round = compute_rand_delay (round_interval, 2);
2073 //do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_mal_round, NULL);
2074 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_mal_round, NULL);
2075 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
2077 #endif /* ENABLE_MALICIOUS */
2081 * Send out PUSHes and PULLs, possibly update #view, samplers.
2083 * This is executed regylary.
2086 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2088 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round.\n");
2091 unsigned int view_size;
2092 unsigned int *permut;
2093 unsigned int a_peers; /* Number of peers we send pushes to */
2094 unsigned int b_peers; /* Number of peers we send pull requests to */
2095 struct GNUNET_PeerIdentity peer;
2097 LOG (GNUNET_ERROR_TYPE_DEBUG,
2098 "Printing view:\n");
2099 to_file (file_name_view_log,
2100 "___ new round ___");
2101 view_size = GNUNET_CONTAINER_multipeermap_size (view);
2102 generate_view_array (view_size);
2103 for (i = 0 ; i < view_size ; i++)
2105 LOG (GNUNET_ERROR_TYPE_DEBUG,
2106 "\t%s\n", GNUNET_i2s (&view_array[i]));
2107 to_file (file_name_view_log,
2109 GNUNET_i2s_full (&view_array[i]));
2113 /* Send pushes and pull requests */
2116 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2117 (unsigned int) view_size);
2120 a_peers = ceil (alpha * view_size);
2122 LOG (GNUNET_ERROR_TYPE_DEBUG,
2123 "Going to send pushes to %u (ceil (%f * %u)) peers.\n",
2124 a_peers, alpha, view_size);
2125 for (i = 0; i < a_peers; i++)
2127 peer = view_array[permut[i]];
2128 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer)) // TODO
2129 { // FIXME if this fails schedule/loop this for later
2134 /* Send PULL requests */
2135 b_peers = a_peers + floor (beta * view_size);
2136 LOG (GNUNET_ERROR_TYPE_DEBUG,
2137 "Going to send pulls to %u (ceil (%f * %u)) peers.\n",
2138 b_peers, beta, view_size);
2139 for (i = a_peers; i < b_peers; i++)
2141 peer = view_array[permut[i]];
2142 peer_ctx = get_peer_ctx (peer_map, &peer);
2143 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer) &&
2144 GNUNET_NO == get_peer_flag (peer_ctx, PULL_REPLY_PENDING)) // TODO
2145 { // FIXME if this fails schedule/loop this for later
2146 send_pull_request (&peer);
2150 GNUNET_free (permut);
2156 /* TODO see how many peers are in push-/pull- list! */
2158 if (push_list_size <= alpha * view_size &&
2159 0 < push_list_size &&
2162 LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the view.\n");
2164 uint32_t first_border;
2165 uint32_t second_border;
2166 uint32_t final_size;
2167 uint32_t peers_to_clean_size;
2168 struct GNUNET_PeerIdentity *peers_to_clean;
2170 peers_to_clean = NULL;
2171 peers_to_clean_size = 0;
2172 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, view_size);
2173 memcpy (peers_to_clean,
2175 view_size * sizeof (struct GNUNET_PeerIdentity));
2177 /* Seems like recreating is the easiest way of emptying the peermap */
2178 GNUNET_CONTAINER_multipeermap_destroy (view);
2179 view = GNUNET_CONTAINER_multipeermap_create (view_size, GNUNET_NO);
2180 to_file (file_name_view_log,
2183 first_border = GNUNET_MIN (ceil (alpha * sampler_size_est_need),
2185 second_border = first_border +
2186 GNUNET_MIN (floor (beta * sampler_size_est_need),
2188 final_size = second_border +
2189 ceil ((1 - (alpha + beta)) * sampler_size_est_need);
2191 GNUNET_array_grow (view_array, view_size, second_border);
2193 /* Update view with peers received through PUSHes */
2194 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2196 for (i = 0; i < first_border; i++)
2198 view_array[i] = push_list[permut[i]];
2199 GNUNET_CONTAINER_multipeermap_put (view, &push_list[permut[i]], NULL,
2200 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2202 to_file (file_name_view_log,
2204 GNUNET_i2s_full (&view_array[i]));
2205 // TODO change the peer_flags accordingly
2207 GNUNET_free (permut);
2210 /* Update view with peers received through PULLs */
2211 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2213 for (i = first_border; i < second_border; i++)
2215 view_array[i] = pull_list[permut[i]];
2216 GNUNET_CONTAINER_multipeermap_put (view, &pull_list[permut[i]], NULL,
2217 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2219 to_file (file_name_view_log,
2221 GNUNET_i2s_full (&view_array[i]));
2222 // TODO change the peer_flags accordingly
2224 GNUNET_free (permut);
2227 /* Update view with peers from history */
2228 RPS_sampler_get_n_rand_peers (prot_sampler,
2231 final_size - second_border);
2232 num_hist_update_tasks = final_size - second_border;
2233 // TODO change the peer_flags accordingly
2235 for (i = 0; i < view_size; i++)
2236 rem_from_list (&peers_to_clean, &peers_to_clean_size, &view_array[i]);
2238 /* Clean peers that were removed from the view */
2239 for (i = 0; i < peers_to_clean_size; i++)
2241 peer_clean (&peers_to_clean[i]);
2242 to_file (file_name_view_log,
2244 GNUNET_i2s_full (&peers_to_clean[i]));
2247 GNUNET_free (peers_to_clean);
2248 peers_to_clean = NULL;
2252 LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the view.\n");
2254 // TODO independent of that also get some peers from CADET_get_peers()?
2256 LOG (GNUNET_ERROR_TYPE_DEBUG,
2257 "Received %u pushes and %u pulls last round (alpha (%.2f) * view_size (%u) = %.2f)\n",
2264 /* Update samplers */
2265 for (i = 0; i < push_list_size; i++)
2267 LOG (GNUNET_ERROR_TYPE_DEBUG,
2268 "Updating with peer %s from push list\n",
2269 GNUNET_i2s (&push_list[i]));
2270 RPS_sampler_update (prot_sampler, &push_list[i]);
2271 RPS_sampler_update (client_sampler, &push_list[i]);
2272 // TODO set in_flag?
2275 for (i = 0; i < pull_list_size; i++)
2277 LOG (GNUNET_ERROR_TYPE_DEBUG,
2278 "Updating with peer %s from pull list\n",
2279 GNUNET_i2s (&pull_list[i]));
2280 RPS_sampler_update (prot_sampler, &pull_list[i]);
2281 RPS_sampler_update (client_sampler, &pull_list[i]);
2282 // TODO set in_flag?
2286 /* Empty push/pull lists */
2287 GNUNET_array_grow (push_list, push_list_size, 0);
2288 GNUNET_array_grow (pull_list, pull_list_size, 0);
2290 struct GNUNET_TIME_Relative time_next_round;
2292 time_next_round = compute_rand_delay (round_interval, 2);
2294 /* Schedule next round */
2295 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_round, NULL);
2296 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
2301 rps_start (struct GNUNET_SERVER_Handle *server);
2305 * This is called from GNUNET_CADET_get_peers().
2307 * It is called on every peer(ID) that cadet somehow has contact with.
2308 * We use those to initialise the sampler.
2311 init_peer_cb (void *cls,
2312 const struct GNUNET_PeerIdentity *peer,
2313 int tunnel, // "Do we have a tunnel towards this peer?"
2314 unsigned int n_paths, // "Number of known paths towards this peer"
2315 unsigned int best_path) // "How long is the best path?
2316 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
2320 LOG (GNUNET_ERROR_TYPE_DEBUG,
2321 "Got peer_id %s from cadet\n",
2329 * Iterator over peers from peerinfo.
2331 * @param cls closure
2332 * @param peer id of the peer, NULL for last call
2333 * @param hello hello message for the peer (can be NULL)
2334 * @param error message
2337 process_peerinfo_peers (void *cls,
2338 const struct GNUNET_PeerIdentity *peer,
2339 const struct GNUNET_HELLO_Message *hello,
2340 const char *err_msg)
2344 LOG (GNUNET_ERROR_TYPE_DEBUG,
2345 "Got peer_id %s from peerinfo\n",
2353 * Clean the send channel of a peer
2356 peer_clean (const struct GNUNET_PeerIdentity *peer)
2358 struct PeerContext *peer_ctx;
2359 struct GNUNET_CADET_Channel *channel;
2361 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_contains (view, peer) &&
2362 GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
2364 peer_ctx = get_peer_ctx (peer_map, peer);
2365 if (NULL != peer_ctx->send_channel)
2367 channel = peer_ctx->send_channel;
2368 peer_ctx->send_channel = NULL;
2369 GNUNET_CADET_channel_destroy (channel);
2376 * Callback used to remove peers from the multipeermap.
2379 peer_remove_cb (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
2381 struct PeerContext *peer_ctx;
2382 const struct GNUNET_CADET_Channel *channel =
2383 (const struct GNUNET_CADET_Channel *) cls;
2385 peer_ctx = (struct PeerContext *) value;
2386 set_peer_flag (peer_ctx, TO_DESTROY);
2388 LOG (GNUNET_ERROR_TYPE_DEBUG,
2389 "Going to clean peer %s\n",
2390 GNUNET_i2s (&peer_ctx->peer_id));
2392 /* If operations are still scheduled for this peer cancel those */
2393 if (0 != peer_ctx->num_outstanding_ops)
2395 GNUNET_array_grow (peer_ctx->outstanding_ops,
2396 peer_ctx->num_outstanding_ops,
2400 /* If we are still waiting for notification whether this peer is live
2401 * cancel the according task */
2402 if (NULL != peer_ctx->is_live_task)
2404 LOG (GNUNET_ERROR_TYPE_DEBUG,
2405 "Trying to cancle is_live_task for peer %s\n",
2407 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
2408 peer_ctx->is_live_task = NULL;
2411 unset_peer_flag (peer_ctx, PULL_REPLY_PENDING);
2413 to_file (file_name_view_log,
2414 "-%s\t(cleanup channel, other peer)",
2415 GNUNET_i2s_full (key));
2416 GNUNET_CONTAINER_multipeermap_remove_all (view, key);
2418 /* If there is still a mq destroy it */
2419 if (NULL != peer_ctx->mq)
2421 GNUNET_MQ_destroy (peer_ctx->mq);
2422 peer_ctx->mq = NULL;
2426 /* Remove the send_channel
2427 * This function should be called again from #cleanup_channel (callback
2428 * called on the destruction of channels) and clean up the rest. */
2429 if (NULL != peer_ctx->send_channel &&
2430 channel != peer_ctx->send_channel)
2432 GNUNET_CADET_channel_destroy (peer_ctx->send_channel);
2433 peer_ctx->send_channel = NULL;
2436 /* Remove the recv_channel
2437 * This function should be called again from #cleanup_channel (callback
2438 * called on the destruction of channels) and clean up the rest. */
2439 if (NULL != peer_ctx->recv_channel &&
2440 channel != peer_ctx->recv_channel)
2442 GNUNET_CADET_channel_destroy (peer_ctx->recv_channel);
2443 peer_ctx->recv_channel = NULL;
2446 /* If there is no channel we have to remove the context now */
2447 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_remove_all (peer_map, key))
2448 LOG (GNUNET_ERROR_TYPE_WARNING, "removing peer from peer_map failed\n");
2450 GNUNET_free (peer_ctx);
2457 * Task run during shutdown.
2463 shutdown_task (void *cls,
2464 const struct GNUNET_SCHEDULER_TaskContext *tc)
2467 LOG (GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
2469 GNUNET_PEERINFO_notify_cancel (peerinfo_notify_handle);
2470 GNUNET_PEERINFO_disconnect (peerinfo_handle);
2472 if (NULL != do_round_task)
2474 GNUNET_SCHEDULER_cancel (do_round_task);
2475 do_round_task = NULL;
2479 if (GNUNET_SYSERR ==
2480 GNUNET_CONTAINER_multipeermap_iterate (peer_map, peer_remove_cb, NULL))
2481 LOG (GNUNET_ERROR_TYPE_WARNING,
2482 "Iterating over peers to disconnect from them was cancelled\n");
2485 GNUNET_NSE_disconnect (nse);
2486 RPS_sampler_destroy (prot_sampler);
2487 RPS_sampler_destroy (client_sampler);
2488 LOG (GNUNET_ERROR_TYPE_DEBUG,
2489 "Size of the peermap: %u\n",
2490 GNUNET_CONTAINER_multipeermap_size (peer_map));
2491 GNUNET_break (0 != GNUNET_CONTAINER_multipeermap_size (peer_map));
2492 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
2493 GNUNET_CADET_disconnect (cadet_handle);
2494 GNUNET_CONTAINER_multipeermap_destroy (view);
2496 GNUNET_array_grow (push_list, push_list_size, 0);
2497 GNUNET_array_grow (pull_list, pull_list_size, 0);
2498 #ifdef ENABLE_MALICIOUS
2499 struct AttackedPeer *tmp_att_peer;
2500 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
2501 if (NULL != mal_peer_set)
2502 GNUNET_CONTAINER_multipeermap_destroy (mal_peer_set);
2503 if (NULL != att_peer_set)
2504 GNUNET_CONTAINER_multipeermap_destroy (att_peer_set);
2505 while (NULL != att_peers_head)
2507 tmp_att_peer = att_peers_head;
2508 GNUNET_CONTAINER_DLL_remove (att_peers_head, att_peers_tail, tmp_att_peer);
2510 #endif /* ENABLE_MALICIOUS */
2515 * A client disconnected. Remove all of its data structure entries.
2517 * @param cls closure, NULL
2518 * @param client identification of the client
2521 handle_client_disconnect (void *cls,
2522 struct GNUNET_SERVER_Client * client)
2528 * Handle the channel a peer opens to us.
2530 * @param cls The closure
2531 * @param channel The channel the peer wants to establish
2532 * @param initiator The peer's peer ID
2533 * @param port The port the channel is being established over
2534 * @param options Further options
2537 handle_inbound_channel (void *cls,
2538 struct GNUNET_CADET_Channel *channel,
2539 const struct GNUNET_PeerIdentity *initiator,
2541 enum GNUNET_CADET_ChannelOption options)
2543 struct PeerContext *peer_ctx;
2544 struct GNUNET_PeerIdentity peer;
2547 LOG (GNUNET_ERROR_TYPE_DEBUG,
2548 "New channel was established to us (Peer %s).\n",
2549 GNUNET_i2s (&peer));
2551 GNUNET_assert (NULL != channel);
2553 // we might not even store the recv_channel
2555 peer_ctx = get_peer_ctx (peer_map, &peer);
2556 // FIXME what do we do if a channel is established twice?
2557 // overwrite? Clean old channel? ...?
2558 //if (NULL != peer_ctx->recv_channel)
2560 // peer_ctx->recv_channel = channel;
2562 peer_ctx->recv_channel = channel;
2564 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, &peer, peer_ctx,
2565 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
2567 /* This would make the push-message unnecessary */
2568 LOG (GNUNET_ERROR_TYPE_DEBUG,
2569 "Got peer_id %s from peerinfo\n",
2570 GNUNET_i2s (&peer));
2571 new_peer_id (&peer);
2573 peer_is_live (peer_ctx);
2575 return NULL; // TODO
2580 * This is called when a channel is destroyed.
2582 * @param cls The closure
2583 * @param channel The channel being closed
2584 * @param channel_ctx The context associated with this channel
2587 cleanup_channel (void *cls,
2588 const struct GNUNET_CADET_Channel *channel,
2591 struct GNUNET_PeerIdentity *peer;
2592 struct PeerContext *peer_ctx;
2594 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
2595 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
2596 // Guess simply casting isn't the nicest way...
2597 // FIXME wait for cadet to change this function
2599 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
2600 {/* We don't want to implicitly create a context that we're about to kill */
2601 peer_ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
2602 if (NULL == peer_ctx) /* It could have been removed by shutdown_task */
2605 if (get_peer_flag (peer_ctx, TO_DESTROY))
2606 {/* We initiatad the destruction of this particular peer */
2607 if (channel == peer_ctx->send_channel)
2608 peer_ctx->send_channel = NULL;
2609 else if (channel == peer_ctx->recv_channel)
2610 peer_ctx->recv_channel = NULL;
2612 to_file (file_name_view_log,
2613 "-%s\t(cleanup channel, ourself)",
2614 GNUNET_i2s_full (peer));
2618 { /* We did not initiate the destruction of this peer */
2619 if (channel == peer_ctx->send_channel)
2620 { /* Something (but us) killd the channel - clean up peer */
2621 LOG (GNUNET_ERROR_TYPE_DEBUG,
2622 "send channel (%s) was destroyed - cleaning up\n",
2624 peer_ctx->send_channel = NULL;
2625 /* Somwewhat {ab,re}use the iterator function */
2626 /* Cast to void is ok, because it's used as void in peer_remove_cb */
2627 (void) peer_remove_cb ((void *) channel, peer, peer_ctx);
2629 else if (channel == peer_ctx->recv_channel)
2630 { /* Other peer doesn't want to send us messages anymore */
2631 LOG (GNUNET_ERROR_TYPE_DEBUG,
2632 "Peer %s destroyed recv channel - cleaning up channel\n",
2634 peer_ctx->recv_channel = NULL;
2638 LOG (GNUNET_ERROR_TYPE_WARNING,
2639 "unknown channel (%s) was destroyed\n",
2646 { /* We don't know a context to that peer */
2647 LOG (GNUNET_ERROR_TYPE_DEBUG,
2648 "channel (%s) without associated context was destroyed\n",
2655 * Actually start the service.
2658 rps_start (struct GNUNET_SERVER_Handle *server)
2660 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
2661 {&handle_client_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
2662 sizeof (struct GNUNET_RPS_CS_RequestMessage)},
2663 {&handle_client_seed, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_SEED, 0},
2664 #ifdef ENABLE_MALICIOUS
2665 {&handle_client_act_malicious, NULL, GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS , 0},
2666 #endif /* ENABLE_MALICIOUS */
2670 GNUNET_SERVER_add_handlers (server, handlers);
2671 GNUNET_SERVER_disconnect_notify (server,
2672 &handle_client_disconnect,
2674 LOG (GNUNET_ERROR_TYPE_INFO, "Ready to receive requests from clients\n");
2677 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
2678 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
2680 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2687 * Process statistics requests.
2689 * @param cls closure
2690 * @param server the initialized server
2691 * @param c configuration to use
2695 struct GNUNET_SERVER_Handle *server,
2696 const struct GNUNET_CONFIGURATION_Handle *c)
2701 // TODO check what this does -- copied from gnunet-boss
2702 // - seems to work as expected
2703 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
2708 GNUNET_CRYPTO_get_peer_identity (cfg, &own_identity); // TODO check return value
2709 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2710 "STARTING SERVICE (rps) for peer [%s]\n",
2711 GNUNET_i2s (&own_identity));
2712 #ifdef ENABLE_MALICIOUS
2713 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2714 "Malicious execution compiled in.\n");
2715 #endif /* ENABLE_MALICIOUS */
2719 /* Get time interval from the configuration */
2720 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
2724 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read ROUNDINTERVAL from config\n");
2725 GNUNET_SCHEDULER_shutdown ();
2729 /* Get initial size of sampler/view from the configuration */
2730 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "RPS",
2732 (long long unsigned int *) &sampler_size_est_need))
2734 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read INITSIZE from config\n");
2735 GNUNET_SCHEDULER_shutdown ();
2738 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", sampler_size_est_need);
2741 view = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO);
2743 /* file_name_view_log */
2744 if (GNUNET_OK != GNUNET_DISK_directory_create ("/tmp/rps/"))
2746 LOG (GNUNET_ERROR_TYPE_WARNING,
2747 "Failed to create directory /tmp/rps/\n");
2750 size = (14 + strlen (GNUNET_i2s_full (&own_identity)) + 1) * sizeof (char);
2751 file_name_view_log = GNUNET_malloc (size);
2752 out_size = GNUNET_snprintf (file_name_view_log,
2755 GNUNET_i2s_full (&own_identity));
2756 if (size < out_size ||
2759 LOG (GNUNET_ERROR_TYPE_WARNING,
2760 "Failed to write string to buffer (size: %i, out_size: %i)\n",
2766 /* connect to NSE */
2767 nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
2773 peer_map = GNUNET_CONTAINER_multipeermap_create (sampler_size_est_need, GNUNET_NO);
2776 /* Initialise cadet */
2777 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
2778 {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH ,
2779 sizeof (struct GNUNET_MessageHeader)},
2780 {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
2781 sizeof (struct GNUNET_MessageHeader)},
2782 {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY , 0},
2786 const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
2787 cadet_handle = GNUNET_CADET_connect (cfg,
2789 &handle_inbound_channel,
2794 peerinfo_handle = GNUNET_PEERINFO_connect (cfg);
2796 /* Initialise sampler */
2797 struct GNUNET_TIME_Relative half_round_interval;
2798 struct GNUNET_TIME_Relative max_round_interval;
2800 half_round_interval = GNUNET_TIME_relative_multiply (round_interval, .5);
2801 max_round_interval = GNUNET_TIME_relative_add (round_interval, half_round_interval);
2803 prot_sampler = RPS_sampler_init (sampler_size_est_need, max_round_interval);
2804 client_sampler = RPS_sampler_mod_init (sampler_size_est_need, max_round_interval);
2806 /* Initialise push and pull maps */
2813 num_hist_update_tasks = 0;
2816 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
2817 GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, NULL);
2818 // TODO send push/pull to each of those peers?
2820 peerinfo_notify_handle = GNUNET_PEERINFO_notify (cfg,
2822 process_peerinfo_peers,
2830 * The main function for the rps service.
2832 * @param argc number of arguments from the command line
2833 * @param argv command line arguments
2834 * @return 0 ok, 1 on error
2837 main (int argc, char *const *argv)
2839 return (GNUNET_OK ==
2840 GNUNET_SERVICE_run (argc,
2843 GNUNET_SERVICE_OPTION_NONE,
2844 &run, NULL)) ? 0 : 1;
2847 /* end of gnunet-service-rps.c */