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 * In own gossip/sampler list, in other's gossip/sampler list
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 gossip list
255 * to send random PUSHes to
260 * Percentage of total peer number in the gossip list
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 * List with the peers we sent requests to.
366 struct GNUNET_PeerIdentity *pending_pull_reply_list;
369 * Size of #pending_pull_reply_list.
371 uint32_t pending_pull_reply_list_size;
375 * Number of history update tasks.
377 uint32_t num_hist_update_tasks;
381 * Closure used to pass the client and the id to the callback
382 * that replies to a client's request
387 * The identifier of the request
392 * The client handle to send the reply to
394 struct GNUNET_SERVER_Client *client;
398 #ifdef ENABLE_MALICIOUS
400 * Type of malicious peer
402 * 0 Don't act malicious at all - Default
403 * 1 Try to maximise representation
404 * 2 Try to partition the network
407 uint32_t mal_type = 0;
410 * Other malicious peers
412 static struct GNUNET_PeerIdentity *mal_peers = NULL;
415 * Hashmap of malicious peers used as set.
416 * Used to more efficiently check whether we know that peer.
418 static struct GNUNET_CONTAINER_MultiPeerMap *mal_peer_set = NULL;
421 * Number of other malicious peers
423 static uint32_t num_mal_peers = 0;
427 * If type is 2 This struct is used to store the attacked peers in a DLL
434 struct AttackedPeer *next;
435 struct AttackedPeer *prev;
440 struct GNUNET_PeerIdentity peer_id;
444 * If type is 2 this is the DLL of attacked peers
446 static struct AttackedPeer *att_peers_head = NULL;
447 static struct AttackedPeer *att_peers_tail = NULL;
450 * This index is used to point to an attacked peer to
451 * implement the round-robin-ish way to select attacked peers.
453 static struct AttackedPeer *att_peer_index = NULL;
456 * Hashmap of attacked peers used as set.
457 * Used to more efficiently check whether we know that peer.
459 static struct GNUNET_CONTAINER_MultiPeerMap *att_peer_set = NULL;
462 * Number of attacked peers
464 static uint32_t num_attacked_peers = 0;
468 * If type is 1 this is the attacked peer
470 static struct GNUNET_PeerIdentity attacked_peer;
473 * The limit of PUSHes we can send in one round.
474 * This is an assumption of the Brahms protocol and either implemented
477 * assumend to be the bandwidth limitation.
479 static uint32_t push_limit = 10000;
480 #endif /* ENABLE_MALICIOUS */
483 /***********************************************************************
485 ***********************************************************************/
492 /***********************************************************************
494 ***********************************************************************/
497 * Set a peer flag of given peer context.
499 #define set_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags |= mask)
502 * Get peer flag of given peer context.
504 #define get_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags & mask ? GNUNET_YES : GNUNET_NO)
507 * Unset flag of given peer context.
509 #define unset_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags &= (~mask))
512 * Clean the send channel of a peer
515 peer_clean (const struct GNUNET_PeerIdentity *peer);
519 * Check if peer is already in peer array.
522 in_arr (const struct GNUNET_PeerIdentity *array,
523 unsigned int arr_size,
524 const struct GNUNET_PeerIdentity *peer)
526 GNUNET_assert (NULL != peer);
531 GNUNET_assert (NULL != array);
535 for (i = 0; i < arr_size ; i++)
536 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&array[i], peer))
543 * Print peerlist to log.
546 print_peer_list (struct GNUNET_PeerIdentity *list, unsigned int len)
550 LOG (GNUNET_ERROR_TYPE_DEBUG,
551 "Printing peer list of length %u at %p:\n",
554 for (i = 0 ; i < len ; i++)
556 LOG (GNUNET_ERROR_TYPE_DEBUG,
558 i, GNUNET_i2s (&list[i]));
564 * Remove peer from list.
567 rem_from_list (struct GNUNET_PeerIdentity **peer_list,
568 unsigned int *list_size,
569 const struct GNUNET_PeerIdentity *peer)
572 struct GNUNET_PeerIdentity *tmp;
576 LOG (GNUNET_ERROR_TYPE_DEBUG,
577 "Removing peer %s from list at %p\n",
581 for ( i = 0 ; i < *list_size ; i++ )
583 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&tmp[i], peer))
585 if (i < *list_size -1)
586 { /* Not at the last entry -- shift peers left */
587 memcpy (&tmp[i], &tmp[i +1],
588 ((*list_size) - i -1) * sizeof (struct GNUNET_PeerIdentity));
590 /* Remove last entry (should be now useless PeerID) */
591 GNUNET_array_grow (tmp, *list_size, (*list_size) -1);
598 * Get random peer from the given list but don't return one from the @a ignore_list.
600 struct GNUNET_PeerIdentity *
601 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list,
603 const struct GNUNET_PeerIdentity *ignore_list,
604 uint32_t ignore_size)
608 struct GNUNET_PeerIdentity *tmp_peer_list;
609 struct GNUNET_PeerIdentity *peer;
611 GNUNET_assert (NULL != peer_list);
616 tmp_peer_list = NULL;
617 GNUNET_array_grow (tmp_peer_list, tmp_size, list_size);
618 memcpy (tmp_peer_list,
620 list_size * sizeof (struct GNUNET_PeerIdentity));
621 peer = GNUNET_new (struct GNUNET_PeerIdentity);
624 * Choose the r_index of the peer we want to return
625 * at random from the interval of the gossip list
627 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
629 *peer = tmp_peer_list[r_index];
631 while (in_arr (ignore_list, ignore_size, peer))
633 rem_from_list (&tmp_peer_list, &tmp_size, peer);
635 print_peer_list (tmp_peer_list, tmp_size);
644 * Choose the r_index of the peer we want to return
645 * at random from the interval of the gossip list
647 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
649 *peer = tmp_peer_list[r_index];
653 GNUNET_array_grow (tmp_peer_list, tmp_size, 0);
660 * Get the context of a peer. If not existing, create.
663 get_peer_ctx (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
664 const struct GNUNET_PeerIdentity *peer)
666 struct PeerContext *ctx;
668 if ( GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
670 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
674 ctx = GNUNET_new (struct PeerContext);
677 ctx->send_channel = NULL;
678 ctx->recv_channel = NULL;
679 ctx->outstanding_ops = NULL;
680 ctx->num_outstanding_ops = 0;
681 ctx->is_live_task = NULL;
682 ctx->peer_id = *peer;
683 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
684 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
691 * Put random peer from sampler into the gossip list as history update.
694 hist_update (void *cls, struct GNUNET_PeerIdentity *ids, uint32_t num_peers)
698 for (i = 0; i < GNUNET_MIN (
699 sampler_size_est_need - GNUNET_CONTAINER_multipeermap_size (view),
702 if (GNUNET_OK != GNUNET_CONTAINER_multipeermap_put (view,
705 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
707 LOG (GNUNET_ERROR_TYPE_WARNING,
708 "Failed to put peer in peermap. (hist_update)\n");
711 /* Might want to check that we really updated the view */
712 if (NULL != view_array)
714 GNUNET_free (view_array);
718 to_file (file_name_view_log,
720 GNUNET_i2s_full (ids));
723 if (0 < num_hist_update_tasks)
724 num_hist_update_tasks--;
729 * Set the peer flag to living and call the outstanding operations on this peer.
732 peer_is_live (struct PeerContext *peer_ctx)
734 struct GNUNET_PeerIdentity *peer;
736 /* Cancle is_live_task if still scheduled */
737 if (NULL != peer_ctx->is_live_task)
739 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
740 peer_ctx->is_live_task = NULL;
743 peer = &peer_ctx->peer_id;
744 set_peer_flag (peer_ctx, VALID);
746 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer %s is live\n", GNUNET_i2s (peer));
748 if (0 < peer_ctx->num_outstanding_ops)
749 { /* Call outstanding operations */
752 for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
753 peer_ctx->outstanding_ops[i].op (peer_ctx->outstanding_ops[i].op_cls, peer);
754 GNUNET_array_grow (peer_ctx->outstanding_ops, peer_ctx->num_outstanding_ops, 0);
762 * Callback that is called when a channel was effectively established.
763 * This is given to ntfy_tmt_rdy and called when the channel was
764 * successfully established.
767 cadet_ntfy_tmt_rdy_cb (void *cls, size_t size, void *buf)
769 struct PeerContext *peer_ctx = (struct PeerContext *) cls;
771 peer_ctx->is_live_task = NULL;
772 LOG (GNUNET_ERROR_TYPE_DEBUG,
773 "Set ->is_live_task = NULL for peer %s\n",
774 GNUNET_i2s (&peer_ctx->peer_id));
779 peer_is_live (peer_ctx);
783 LOG (GNUNET_ERROR_TYPE_WARNING,
784 "Problems establishing a connection to peer %s in order to check liveliness\n",
785 GNUNET_i2s (&peer_ctx->peer_id));
786 // TODO reschedule? cleanup?
789 //if (NULL != peer_ctx->is_live_task)
791 // LOG (GNUNET_ERROR_TYPE_DEBUG,
792 // "Trying to cancle is_live_task for peer %s\n",
793 // GNUNET_i2s (&peer_ctx->peer_id));
794 // GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
795 // peer_ctx->is_live_task = NULL;
803 * Get the channel of a peer. If not existing, create.
805 struct GNUNET_CADET_Channel *
806 get_channel (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
807 const struct GNUNET_PeerIdentity *peer)
809 struct PeerContext *peer_ctx;
811 peer_ctx = get_peer_ctx (peer_map, peer);
813 if (NULL == peer_ctx->send_channel)
815 LOG (GNUNET_ERROR_TYPE_DEBUG,
816 "Trying to establish channel to peer %s\n",
819 peer_ctx->send_channel =
820 GNUNET_CADET_channel_create (cadet_handle,
823 GNUNET_RPS_CADET_PORT,
824 GNUNET_CADET_OPTION_RELIABLE);
826 // do I have to explicitly put it in the peer_map?
827 (void) GNUNET_CONTAINER_multipeermap_put
831 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
833 return peer_ctx->send_channel;
838 * Get the message queue of a specific peer.
840 * If we already have a message queue open to this client,
841 * simply return it, otherways create one.
843 struct GNUNET_MQ_Handle *
844 get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
845 const struct GNUNET_PeerIdentity *peer_id)
847 struct PeerContext *peer_ctx;
849 peer_ctx = get_peer_ctx (peer_map, peer_id);
851 GNUNET_assert (NULL == peer_ctx->is_live_task);
853 if (NULL == peer_ctx->mq)
855 (void) get_channel (peer_map, peer_id);
856 peer_ctx->mq = GNUNET_CADET_mq_create (peer_ctx->send_channel);
857 //do I have to explicitly put it in the peer_map?
858 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer_id, peer_ctx,
859 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
866 * Issue check whether peer is live
868 * @param peer_ctx the context of the peer
871 check_peer_live (struct PeerContext *peer_ctx)
873 (void) get_channel (peer_map, &peer_ctx->peer_id);
874 LOG (GNUNET_ERROR_TYPE_DEBUG,
875 "Get informed about peer %s getting live\n",
876 GNUNET_i2s (&peer_ctx->peer_id));
877 if (NULL == peer_ctx->is_live_task)
879 peer_ctx->is_live_task =
880 GNUNET_CADET_notify_transmit_ready (peer_ctx->send_channel,
882 GNUNET_TIME_UNIT_FOREVER_REL,
883 sizeof (struct GNUNET_MessageHeader),
884 cadet_ntfy_tmt_rdy_cb,
886 (void) GNUNET_CONTAINER_multipeermap_put (peer_map,
889 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
893 LOG (GNUNET_ERROR_TYPE_DEBUG,
894 "Already waiting for notification\n");
900 * Sum all time relatives of an array.
902 struct GNUNET_TIME_Relative
903 T_relative_sum (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
905 struct GNUNET_TIME_Relative sum;
908 sum = GNUNET_TIME_UNIT_ZERO;
909 for ( i = 0 ; i < arr_size ; i++ )
911 sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
918 * Compute the average of given time relatives.
920 struct GNUNET_TIME_Relative
921 T_relative_avg (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
923 return GNUNET_TIME_relative_divide (T_relative_sum (rel_array, arr_size), arr_size);
928 * Insert PeerID in #pull_list
930 * Called once we know a peer is live.
933 insert_in_pull_list (void *cls, const struct GNUNET_PeerIdentity *peer)
935 if (GNUNET_NO == in_arr (pull_list, pull_list_size, peer))
936 GNUNET_array_append (pull_list, pull_list_size, *peer);
942 * Check whether #insert_in_pull_list was already scheduled
945 insert_in_pull_list_scheduled (const struct PeerContext *peer_ctx)
949 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
950 if (insert_in_pull_list == peer_ctx->outstanding_ops[i].op)
957 * Insert PeerID in #view
959 * Called once we know a peer is live.
962 insert_in_view (void *cls, const struct GNUNET_PeerIdentity *peer)
964 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_put (view,
967 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
969 LOG (GNUNET_ERROR_TYPE_WARNING,
970 "Failed to put peer into view. (insert_in_view)\n");
973 /* Might want to check whether we really modified the view */
974 if (NULL != view_array)
976 GNUNET_free (view_array);
980 (void) get_channel (peer_map, peer);
984 * Check whether #insert_in_view was already scheduled
987 insert_in_view_scheduled (const struct PeerContext *peer_ctx)
991 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
992 if (insert_in_view == peer_ctx->outstanding_ops[i].op)
999 * Update sampler with given PeerID.
1002 insert_in_sampler (void *cls, const struct GNUNET_PeerIdentity *peer)
1004 LOG (GNUNET_ERROR_TYPE_DEBUG,
1005 "Updating samplers with peer %s from insert_in_sampler()\n",
1007 RPS_sampler_update (prot_sampler, peer);
1008 RPS_sampler_update (client_sampler, peer);
1013 * Check whether #insert_in_sampler was already scheduled
1016 insert_in_sampler_scheduled (const struct PeerContext *peer_ctx)
1020 for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
1021 if (insert_in_sampler== peer_ctx->outstanding_ops[i].op)
1028 * Wrapper around #RPS_sampler_resize()
1030 * If we do not have enough sampler elements, double current sampler size
1031 * If we have more than enough sampler elements, halv current sampler size
1034 resize_wrapper (struct RPS_Sampler *sampler, uint32_t new_size)
1036 unsigned int sampler_size;
1039 // TODO respect the min, max
1040 sampler_size = RPS_sampler_get_size (sampler);
1041 if (sampler_size > new_size * 4)
1043 RPS_sampler_resize (sampler, sampler_size / 2);
1045 else if (sampler_size < new_size)
1047 RPS_sampler_resize (sampler, sampler_size * 2);
1049 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
1054 * Wrapper around #RPS_sampler_resize() resizing the client sampler
1057 client_resize_wrapper ()
1059 uint32_t bigger_size;
1060 unsigned int sampler_size;
1064 sampler_size = RPS_sampler_get_size (client_sampler);
1066 if (sampler_size_est_need > sampler_size_client_need)
1067 bigger_size = sampler_size_est_need;
1069 bigger_size = sampler_size_client_need;
1071 // TODO respect the min, max
1072 resize_wrapper (client_sampler, bigger_size);
1073 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
1078 * Estimate request rate
1080 * Called every time we receive a request from the client.
1085 struct GNUNET_TIME_Relative max_round_duration;
1087 if (request_deltas_size > req_counter)
1089 if ( 1 < req_counter)
1091 /* Shift last request deltas to the right */
1092 memcpy (&request_deltas[1],
1094 (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
1096 /* Add current delta to beginning */
1098 GNUNET_TIME_absolute_get_difference (last_request,
1099 GNUNET_TIME_absolute_get ());
1100 request_rate = T_relative_avg (request_deltas, req_counter);
1102 /* Compute the duration a round will maximally take */
1103 max_round_duration =
1104 GNUNET_TIME_relative_add (round_interval,
1105 GNUNET_TIME_relative_divide (round_interval, 2));
1107 /* Set the estimated size the sampler has to have to
1108 * satisfy the current client request rate */
1109 sampler_size_client_need =
1110 max_round_duration.rel_value_us / request_rate.rel_value_us;
1112 /* Resize the sampler */
1113 client_resize_wrapper ();
1115 last_request = GNUNET_TIME_absolute_get ();
1120 * Add all peers in @a peer_array to @a peer_map used as set.
1122 * @param peer_array array containing the peers
1123 * @param num_peers number of peers in @peer_array
1124 * @param peer_map the peermap to use as set
1127 add_peer_array_to_set (const struct GNUNET_PeerIdentity *peer_array,
1128 unsigned int num_peers,
1129 struct GNUNET_CONTAINER_MultiPeerMap *peer_map)
1132 if (NULL == peer_map)
1134 LOG (GNUNET_ERROR_TYPE_WARNING,
1135 "Trying to add peers to an empty peermap.\n");
1139 for (i = 0; i < num_peers; i++)
1141 GNUNET_CONTAINER_multipeermap_put (peer_map,
1144 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1150 * Send a PULL REPLY to @a peer_id
1152 * @param peer_id the peer to send the reply to.
1153 * @param peer_ids the peers to send to @a peer_id
1154 * @param num_peer_ids the number of peers to send to @a peer_id
1157 send_pull_reply (const struct GNUNET_PeerIdentity *peer_id,
1158 const struct GNUNET_PeerIdentity *peer_ids,
1159 unsigned int num_peer_ids)
1162 struct GNUNET_MQ_Handle *mq;
1163 struct GNUNET_MQ_Envelope *ev;
1164 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
1166 /* Compute actual size */
1167 send_size = sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) +
1168 num_peer_ids * sizeof (struct GNUNET_PeerIdentity);
1170 if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size)
1171 /* Compute number of peers to send
1172 * If too long, simply truncate */
1173 // TODO select random ones via permutation
1174 // or even better: do good protocol design
1176 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE -
1177 sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1178 sizeof (struct GNUNET_PeerIdentity);
1180 send_size = num_peer_ids;
1182 LOG (GNUNET_ERROR_TYPE_DEBUG,
1183 "PULL REQUEST from peer %s received, going to send %u peers\n",
1184 GNUNET_i2s (peer_id), send_size);
1186 mq = get_mq (peer_map, peer_id);
1188 ev = GNUNET_MQ_msg_extra (out_msg,
1189 send_size * sizeof (struct GNUNET_PeerIdentity),
1190 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
1191 out_msg->num_peers = htonl (send_size);
1192 memcpy (&out_msg[1], peer_ids,
1193 send_size * sizeof (struct GNUNET_PeerIdentity));
1195 GNUNET_MQ_send (mq, ev);
1200 * This function is called on new peer_ids from 'external' sources
1201 * (client seed, cadet get_peers(), ...)
1203 * @param peer_id the new peer_id
1206 new_peer_id (const struct GNUNET_PeerIdentity *peer_id)
1208 struct PeerOutstandingOp out_op;
1209 struct PeerContext *peer_ctx;
1211 if (NULL != peer_id &&
1212 0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, peer_id))
1214 LOG (GNUNET_ERROR_TYPE_DEBUG,
1215 "Got peer_id %s (at %p, view size: %u)\n",
1216 GNUNET_i2s (peer_id),
1218 GNUNET_CONTAINER_multipeermap_size (view));
1220 peer_ctx = get_peer_ctx (peer_map, peer_id);
1221 if (GNUNET_YES != get_peer_flag (peer_ctx, VALID))
1223 if (GNUNET_NO == insert_in_sampler_scheduled (peer_ctx))
1225 out_op.op = insert_in_sampler;
1226 out_op.op_cls = NULL;
1227 GNUNET_array_append (peer_ctx->outstanding_ops,
1228 peer_ctx->num_outstanding_ops,
1232 if (GNUNET_NO == insert_in_view_scheduled (peer_ctx))
1234 out_op.op = insert_in_view;
1235 out_op.op_cls = NULL;
1236 GNUNET_array_append (peer_ctx->outstanding_ops,
1237 peer_ctx->num_outstanding_ops,
1241 /* Trigger livelyness test on peer */
1242 check_peer_live (peer_ctx);
1246 // send push/pull to each of those peers?
1251 /***********************************************************************
1253 ***********************************************************************/
1260 * Function called by NSE.
1262 * Updates sizes of sampler list and gossip list and adapt those lists
1266 nse_callback (void *cls, struct GNUNET_TIME_Absolute timestamp,
1267 double logestimate, double std_dev)
1270 //double scale; // TODO this might go gloabal/config
1272 LOG (GNUNET_ERROR_TYPE_DEBUG,
1273 "Received a ns estimate - logest: %f, std_dev: %f (old_size: %u)\n",
1274 logestimate, std_dev, RPS_sampler_get_size (prot_sampler));
1276 estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
1277 // GNUNET_NSE_log_estimate_to_n (logestimate);
1278 estimate = pow (estimate, 1.0 / 3);
1279 // TODO add if std_dev is a number
1280 // estimate += (std_dev * scale);
1281 if (2 < ceil (estimate))
1283 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
1284 sampler_size_est_need = estimate;
1286 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
1288 /* If the NSE has changed adapt the lists accordingly */
1289 resize_wrapper (prot_sampler, sampler_size_est_need);
1290 client_resize_wrapper ();
1295 * Callback called once the requested PeerIDs are ready.
1297 * Sends those to the requesting client.
1299 void client_respond (void *cls,
1300 struct GNUNET_PeerIdentity *peer_ids, uint32_t num_peers)
1302 struct GNUNET_MQ_Envelope *ev;
1303 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
1304 struct ReplyCls *reply_cls = (struct ReplyCls *) cls;
1305 uint32_t size_needed;
1306 struct ClientContext *cli_ctx;
1308 LOG (GNUNET_ERROR_TYPE_DEBUG,
1309 "sampler returned %" PRIu32 " peers\n",
1312 size_needed = sizeof (struct GNUNET_RPS_CS_ReplyMessage) +
1313 num_peers * sizeof (struct GNUNET_PeerIdentity);
1315 GNUNET_assert (GNUNET_SERVER_MAX_MESSAGE_SIZE >= size_needed);
1317 ev = GNUNET_MQ_msg_extra (out_msg,
1318 num_peers * sizeof (struct GNUNET_PeerIdentity),
1319 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
1320 out_msg->num_peers = htonl (num_peers);
1321 out_msg->id = htonl (reply_cls->id);
1323 memcpy (&out_msg[1],
1325 num_peers * sizeof (struct GNUNET_PeerIdentity));
1326 GNUNET_free (peer_ids);
1328 cli_ctx = GNUNET_SERVER_client_get_user_context (reply_cls->client, struct ClientContext);
1329 if (NULL == cli_ctx) {
1330 cli_ctx = GNUNET_new (struct ClientContext);
1331 cli_ctx->mq = GNUNET_MQ_queue_for_server_client (reply_cls->client);
1332 GNUNET_SERVER_client_set_user_context (reply_cls->client, cli_ctx);
1335 GNUNET_free (reply_cls);
1337 GNUNET_MQ_send (cli_ctx->mq, ev);
1342 * Handle RPS request from the client.
1344 * @param cls closure
1345 * @param client identification of the client
1346 * @param message the actual message
1349 handle_client_request (void *cls,
1350 struct GNUNET_SERVER_Client *client,
1351 const struct GNUNET_MessageHeader *message)
1353 struct GNUNET_RPS_CS_RequestMessage *msg;
1355 uint32_t size_needed;
1356 struct ReplyCls *reply_cls;
1359 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
1361 num_peers = ntohl (msg->num_peers);
1362 size_needed = sizeof (struct GNUNET_RPS_CS_RequestMessage) +
1363 num_peers * sizeof (struct GNUNET_PeerIdentity);
1365 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
1367 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1371 for (i = 0 ; i < num_peers ; i++)
1374 LOG (GNUNET_ERROR_TYPE_DEBUG,
1375 "Client requested %" PRIu32 " random peer(s).\n",
1378 reply_cls = GNUNET_new (struct ReplyCls);
1379 reply_cls->id = ntohl (msg->id);
1380 reply_cls->client = client;
1382 RPS_sampler_get_n_rand_peers (client_sampler,
1387 GNUNET_SERVER_receive_done (client,
1393 * Handle seed from the client.
1395 * @param cls closure
1396 * @param client identification of the client
1397 * @param message the actual message
1400 handle_client_seed (void *cls,
1401 struct GNUNET_SERVER_Client *client,
1402 const struct GNUNET_MessageHeader *message)
1404 struct GNUNET_RPS_CS_SeedMessage *in_msg;
1405 struct GNUNET_PeerIdentity *peers;
1409 if (sizeof (struct GNUNET_RPS_CS_SeedMessage) > ntohs (message->size))
1411 GNUNET_break_op (0);
1412 GNUNET_SERVER_receive_done (client,
1416 in_msg = (struct GNUNET_RPS_CS_SeedMessage *) message;
1417 num_peers = ntohl (in_msg->num_peers);
1418 peers = (struct GNUNET_PeerIdentity *) &in_msg[1];
1419 //peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1420 //memcpy (peers, &in_msg[1], num_peers * sizeof (struct GNUNET_PeerIdentity));
1422 if ((ntohs (message->size) - sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
1423 sizeof (struct GNUNET_PeerIdentity) != num_peers)
1425 GNUNET_break_op (0);
1426 GNUNET_SERVER_receive_done (client,
1430 LOG (GNUNET_ERROR_TYPE_DEBUG,
1431 "Client seeded peers:\n");
1432 print_peer_list (peers, num_peers);
1434 for (i = 0 ; i < num_peers ; i++)
1436 LOG (GNUNET_ERROR_TYPE_DEBUG,
1437 "Updating samplers with seed %" PRIu32 ": %s\n",
1439 GNUNET_i2s (&peers[i]));
1441 new_peer_id (&peers[i]);
1443 //RPS_sampler_update (prot_sampler, &peers[i]);
1444 //RPS_sampler_update (client_sampler, &peers[i]);
1447 ////GNUNET_free (peers);
1449 GNUNET_SERVER_receive_done (client,
1455 * Handle a PUSH message from another peer.
1457 * Check the proof of work and store the PeerID
1458 * in the temporary list for pushed PeerIDs.
1460 * @param cls Closure
1461 * @param channel The channel the PUSH was received over
1462 * @param channel_ctx The context associated with this channel
1463 * @param msg The message header
1466 handle_peer_push (void *cls,
1467 struct GNUNET_CADET_Channel *channel,
1469 const struct GNUNET_MessageHeader *msg)
1471 const struct GNUNET_PeerIdentity *peer;
1473 // (check the proof of work)
1475 peer = (const struct GNUNET_PeerIdentity *)
1476 GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
1477 // FIXME wait for cadet to change this function
1479 LOG (GNUNET_ERROR_TYPE_DEBUG, "PUSH received (%s)\n", GNUNET_i2s (peer));
1481 #ifdef ENABLE_MALICIOUS
1482 struct AttackedPeer *tmp_att_peer;
1484 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1485 memcpy (&tmp_att_peer->peer_id, peer, sizeof (struct GNUNET_PeerIdentity));
1488 { /* Try to maximise representation */
1489 if (NULL == att_peer_set)
1490 att_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
1491 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1494 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1497 add_peer_array_to_set (peer, 1, att_peer_set);
1503 else if (2 == mal_type)
1504 { /* We attack one single well-known peer - simply ignore */
1509 GNUNET_free (tmp_att_peer);
1512 #endif /* ENABLE_MALICIOUS */
1514 /* Add the sending peer to the push_list */
1515 if (GNUNET_NO == in_arr (push_list, push_list_size, peer))
1516 GNUNET_array_append (push_list, push_list_size, *peer);
1523 * Iterator over hash map entries.
1524 * Called from #generate_view_array and writes every peer id into #view_array.
1526 * @param cls closure - the pointer to the counter
1527 * @param key current public key
1528 * @param value value in the hash map
1529 * @return #GNUNET_YES if we should continue to
1531 * #GNUNET_NO if not.
1534 dump_id_to_view_array (void *cls,
1535 const struct GNUNET_PeerIdentity *key,
1538 unsigned int *i = (unsigned int *) cls;
1540 view_array[(*i)++] = *key;
1546 * Makes sure the view_array is filled with the peer ids currently in #view.
1547 * Called from within #do_round before sending pushes and pulls and from
1548 * #handle_peer_pull_request when a reply is sent.
1551 generate_view_array (unsigned int view_size)
1556 if (NULL == view_array)
1558 view_array = GNUNET_new_array (view_size,
1559 struct GNUNET_PeerIdentity);
1560 i = GNUNET_new (unsigned int);
1563 ret = GNUNET_CONTAINER_multipeermap_iterate (view,
1564 dump_id_to_view_array,
1566 GNUNET_assert (view_size == ret);
1567 GNUNET_assert (view_size == *i);
1575 * Handle PULL REQUEST request message from another peer.
1577 * Reply with the gossip list of PeerIDs.
1579 * @param cls Closure
1580 * @param channel The channel the PUSH was received over
1581 * @param channel_ctx The context associated with this channel
1582 * @param msg The message header
1585 handle_peer_pull_request (void *cls,
1586 struct GNUNET_CADET_Channel *channel,
1588 const struct GNUNET_MessageHeader *msg)
1590 struct GNUNET_PeerIdentity *peer;
1591 unsigned int view_size;
1593 peer = (struct GNUNET_PeerIdentity *)
1594 GNUNET_CADET_channel_get_info (channel,
1595 GNUNET_CADET_OPTION_PEER);
1596 // FIXME wait for cadet to change this function
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 (GNUNET_YES == 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);
1626 * Handle PULL REPLY message from another peer.
1628 * Check whether we sent a corresponding request and
1629 * whether this reply is the first one.
1631 * @param cls Closure
1632 * @param channel The channel the PUSH was received over
1633 * @param channel_ctx The context associated with this channel
1634 * @param msg The message header
1637 handle_peer_pull_reply (void *cls,
1638 struct GNUNET_CADET_Channel *channel,
1640 const struct GNUNET_MessageHeader *msg)
1642 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
1643 struct GNUNET_PeerIdentity *peers;
1644 struct PeerContext *peer_ctx;
1645 struct GNUNET_PeerIdentity *sender;
1646 struct PeerContext *sender_ctx;
1647 struct PeerOutstandingOp out_op;
1649 #ifdef ENABLE_MALICIOUS
1650 struct AttackedPeer *tmp_att_peer;
1651 #endif /* ENABLE_MALICIOUS */
1653 /* Check for protocol violation */
1654 if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->size))
1656 GNUNET_break_op (0);
1657 return GNUNET_SYSERR;
1660 in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
1661 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1662 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1664 LOG (GNUNET_ERROR_TYPE_ERROR,
1665 "message says it sends %" PRIu64 " peers, have space for %i peers\n",
1666 ntohl (in_msg->num_peers),
1667 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1668 sizeof (struct GNUNET_PeerIdentity));
1669 GNUNET_break_op (0);
1670 return GNUNET_SYSERR;
1673 sender = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
1674 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
1675 // Guess simply casting isn't the nicest way...
1676 // FIXME wait for cadet to change this function
1677 sender_ctx = get_peer_ctx (peer_map, sender);
1679 if (GNUNET_YES == get_peer_flag (sender_ctx, PULL_REPLY_PENDING))
1681 GNUNET_break_op (0);
1686 #ifdef ENABLE_MALICIOUS
1687 // We shouldn't even receive pull replies as we're not sending
1690 #endif /* ENABLE_MALICIOUS */
1692 /* Do actual logic */
1693 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1695 LOG (GNUNET_ERROR_TYPE_DEBUG,
1696 "PULL REPLY received, got following peers:\n");
1698 for (i = 0 ; i < ntohl (in_msg->num_peers) ; i++)
1700 LOG (GNUNET_ERROR_TYPE_DEBUG,
1703 GNUNET_i2s (&peers[i]));
1705 #ifdef ENABLE_MALICIOUS
1708 { /* Add attacked peer to local list */
1709 // TODO check if we sent a request and this was the first reply
1710 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1712 && GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mal_peer_set,
1714 && GNUNET_NO == GNUNET_CRYPTO_cmp_peer_identity (&peers[i],
1717 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1718 tmp_att_peer->peer_id = peers[i];
1719 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1722 add_peer_array_to_set (&peers[i], 1, att_peer_set);
1726 #endif /* ENABLE_MALICIOUS */
1727 peer_ctx = get_peer_ctx (peer_map, &peers[i]);
1728 if (GNUNET_YES == get_peer_flag (peer_ctx, VALID)
1729 || NULL != peer_ctx->send_channel
1730 || NULL != peer_ctx->recv_channel)
1732 if (GNUNET_NO == in_arr (pull_list, pull_list_size, &peers[i])
1733 && 0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peers[i]))
1734 GNUNET_array_append (pull_list, pull_list_size, peers[i]);
1736 else if (GNUNET_NO == insert_in_pull_list_scheduled (peer_ctx))
1738 out_op.op = insert_in_pull_list;
1739 out_op.op_cls = NULL;
1740 GNUNET_array_append (peer_ctx->outstanding_ops,
1741 peer_ctx->num_outstanding_ops,
1743 check_peer_live (peer_ctx);
1747 unset_peer_flag (sender_ctx, PULL_REPLY_PENDING);
1748 rem_from_list (&pending_pull_reply_list, &pending_pull_reply_list_size, sender);
1755 * Compute a random delay.
1756 * A uniformly distributed value between mean + spread and mean - spread.
1758 * For example for mean 4 min and spread 2 the minimum is (4 min - (1/2 * 4 min))
1759 * It would return a random value between 2 and 6 min.
1761 * @param mean the mean
1762 * @param spread the inverse amount of deviation from the mean
1764 static struct GNUNET_TIME_Relative
1765 compute_rand_delay (struct GNUNET_TIME_Relative mean, unsigned int spread)
1767 struct GNUNET_TIME_Relative half_interval;
1768 struct GNUNET_TIME_Relative ret;
1769 unsigned int rand_delay;
1770 unsigned int max_rand_delay;
1774 LOG (GNUNET_ERROR_TYPE_WARNING,
1775 "Not accepting spread of 0\n");
1779 /* Compute random time value between spread * mean and spread * mean */
1780 half_interval = GNUNET_TIME_relative_divide (mean, spread);
1782 max_rand_delay = GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us / mean.rel_value_us * (2/spread);
1784 * Compute random value between (0 and 1) * round_interval
1785 * via multiplying round_interval with a 'fraction' (0 to value)/value
1787 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_rand_delay);
1788 ret = GNUNET_TIME_relative_multiply (mean, rand_delay);
1789 ret = GNUNET_TIME_relative_divide (ret, max_rand_delay);
1790 ret = GNUNET_TIME_relative_add (ret, half_interval);
1792 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ret.rel_value_us)
1793 LOG (GNUNET_ERROR_TYPE_WARNING,
1794 "Returning FOREVER_REL\n");
1801 * Send single pull request
1803 * @param peer_id the peer to send the pull request to.
1806 send_pull_request (struct GNUNET_PeerIdentity *peer_id)
1808 struct GNUNET_MQ_Envelope *ev;
1809 struct GNUNET_MQ_Handle *mq;
1811 LOG (GNUNET_ERROR_TYPE_DEBUG,
1812 "Sending PULL request to peer %s of gossiped list.\n",
1813 GNUNET_i2s (peer_id));
1815 GNUNET_array_append (pending_pull_reply_list, pending_pull_reply_list_size, *peer_id);
1817 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
1818 mq = get_mq (peer_map, peer_id);
1819 GNUNET_MQ_send (mq, ev);
1826 * @param peer_id the peer to send the push to.
1829 send_push (struct GNUNET_PeerIdentity *peer_id)
1831 struct GNUNET_MQ_Envelope *ev;
1832 struct GNUNET_MQ_Handle *mq;
1834 LOG (GNUNET_ERROR_TYPE_DEBUG,
1835 "Sending PUSH to peer %s of gossiped list.\n",
1836 GNUNET_i2s (peer_id));
1838 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
1839 mq = get_mq (peer_map, peer_id);
1840 GNUNET_MQ_send (mq, ev);
1845 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1848 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1851 #ifdef ENABLE_MALICIOUS
1853 * Turn RPS service to act malicious.
1855 * @param cls Closure
1856 * @param channel The channel the PUSH was received over
1857 * @param channel_ctx The context associated with this channel
1858 * @param msg The message header
1861 handle_client_act_malicious (void *cls,
1862 struct GNUNET_SERVER_Client *client,
1863 const struct GNUNET_MessageHeader *msg)
1865 struct GNUNET_RPS_CS_ActMaliciousMessage *in_msg;
1866 struct GNUNET_PeerIdentity *peers;
1867 uint32_t num_mal_peers_sent;
1868 uint32_t num_mal_peers_old;
1870 /* Check for protocol violation */
1871 if (sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage) > ntohs (msg->size))
1873 GNUNET_break_op (0);
1876 in_msg = (struct GNUNET_RPS_CS_ActMaliciousMessage *) msg;
1877 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
1878 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1880 LOG (GNUNET_ERROR_TYPE_ERROR,
1881 "message says it sends %" PRIu64 " peers, have space for %i peers\n",
1882 ntohl (in_msg->num_peers),
1883 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
1884 sizeof (struct GNUNET_PeerIdentity));
1885 GNUNET_break_op (0);
1889 /* Do actual logic */
1890 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1891 mal_type = ntohl (in_msg->type);
1893 LOG (GNUNET_ERROR_TYPE_DEBUG,
1894 "Now acting malicious type %" PRIu32 "\n",
1898 { /* Try to maximise representation */
1899 /* Add other malicious peers to those we already know */
1901 num_mal_peers_sent = ntohl (in_msg->num_peers);
1902 num_mal_peers_old = num_mal_peers;
1903 GNUNET_array_grow (mal_peers,
1905 num_mal_peers + num_mal_peers_sent);
1906 memcpy (&mal_peers[num_mal_peers_old],
1908 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1910 /* Add all mal peers to mal_peer_set */
1911 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1915 /* Substitute do_round () with do_mal_round () */
1916 GNUNET_SCHEDULER_cancel (do_round_task);
1917 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1920 else if (2 == mal_type
1922 { /* Try to partition the network */
1923 /* Add other malicious peers to those we already know */
1924 num_mal_peers_sent = ntohl (in_msg->num_peers) - 1;
1925 num_mal_peers_old = num_mal_peers;
1926 GNUNET_array_grow (mal_peers,
1928 num_mal_peers + num_mal_peers_sent);
1929 memcpy (&mal_peers[num_mal_peers_old],
1931 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1933 /* Add all mal peers to mal_peer_set */
1934 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1938 /* Store the one attacked peer */
1939 memcpy (&attacked_peer,
1940 &in_msg->attacked_peer,
1941 sizeof (struct GNUNET_PeerIdentity));
1943 LOG (GNUNET_ERROR_TYPE_DEBUG,
1944 "Attacked peer is %s\n",
1945 GNUNET_i2s (&attacked_peer));
1947 /* Substitute do_round () with do_mal_round () */
1948 GNUNET_SCHEDULER_cancel (do_round_task);
1949 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1951 else if (0 == mal_type)
1952 { /* Stop acting malicious */
1953 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
1955 /* Substitute do_mal_round () with do_round () */
1956 GNUNET_SCHEDULER_cancel (do_round_task);
1957 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
1964 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1969 * Send out PUSHes and PULLs maliciously.
1971 * This is executed regylary.
1974 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1976 uint32_t num_pushes;
1978 struct GNUNET_TIME_Relative time_next_round;
1979 struct AttackedPeer *tmp_att_peer;
1981 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round maliciously.\n");
1983 /* Do malicious actions */
1985 { /* Try to maximise representation */
1987 /* The maximum of pushes we're going to send this round */
1988 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit,
1989 num_attacked_peers),
1990 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
1992 LOG (GNUNET_ERROR_TYPE_DEBUG,
1993 "Going to send %" PRIu32 " pushes\n",
1996 /* Send PUSHes to attacked peers */
1997 for (i = 0 ; i < num_pushes ; i++)
1999 if (att_peers_tail == att_peer_index)
2000 att_peer_index = att_peers_head;
2002 att_peer_index = att_peer_index->next;
2004 send_push (&att_peer_index->peer_id);
2007 /* Send PULLs to some peers to learn about additional peers to attack */
2008 tmp_att_peer = att_peer_index;
2009 for (i = 0 ; i < num_pushes * alpha ; i++)
2011 if (att_peers_tail == tmp_att_peer)
2012 tmp_att_peer = att_peers_head;
2014 att_peer_index = tmp_att_peer->next;
2016 send_pull_request (&tmp_att_peer->peer_id);
2021 else if (2 == mal_type)
2023 * Try to partition the network
2024 * Send as many pushes to the attacked peer as possible
2025 * That is one push per round as it will ignore more.
2027 send_push (&attacked_peer);
2032 { /* Combined attack */
2034 /* The maximum of pushes we're going to send this round */
2035 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit - 1,
2036 num_attacked_peers),
2037 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
2039 LOG (GNUNET_ERROR_TYPE_DEBUG,
2040 "Going to send %" PRIu32 " pushes\n",
2043 /* Send PUSHes to attacked peers */
2044 send_push (&attacked_peer);
2046 for (i = 0 ; i < num_pushes ; i++)
2048 if (att_peers_tail == att_peer_index)
2049 att_peer_index = att_peers_head;
2051 att_peer_index = att_peer_index->next;
2053 send_push (&att_peer_index->peer_id);
2056 /* Send PULLs to some peers to learn about additional peers to attack */
2057 tmp_att_peer = att_peer_index;
2058 for (i = 0 ; i < num_pushes * alpha ; i++)
2060 if (att_peers_tail == tmp_att_peer)
2061 tmp_att_peer = att_peers_head;
2063 att_peer_index = tmp_att_peer->next;
2065 send_pull_request (&tmp_att_peer->peer_id);
2069 /* Schedule next round */
2070 time_next_round = compute_rand_delay (round_interval, 2);
2072 //do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_mal_round, NULL);
2073 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_mal_round, NULL);
2074 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
2076 #endif /* ENABLE_MALICIOUS */
2080 * Send out PUSHes and PULLs, possibly update #view, samplers.
2082 * This is executed regylary.
2085 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2087 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round.\n");
2090 unsigned int view_size;
2091 unsigned int *permut;
2092 unsigned int a_peers; /* Number of peers we send pushes to */
2093 unsigned int b_peers; /* Number of peers we send pull requests to */
2094 struct GNUNET_PeerIdentity peer;
2096 LOG (GNUNET_ERROR_TYPE_DEBUG,
2097 "Printing gossip list:\n");
2098 to_file (file_name_view_log,
2099 "___ new round ___");
2100 view_size = GNUNET_CONTAINER_multipeermap_size (view);
2101 generate_view_array (view_size);
2102 for (i = 0 ; i < view_size ; i++)
2104 LOG (GNUNET_ERROR_TYPE_DEBUG,
2105 "\t%s\n", GNUNET_i2s (&view_array[i]));
2106 to_file (file_name_view_log,
2108 GNUNET_i2s_full (&view_array[i]));
2112 /* Send pushes and pull requests */
2115 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2116 (unsigned int) view_size);
2118 /* generate view_array */
2121 a_peers = ceil (alpha * view_size);
2123 LOG (GNUNET_ERROR_TYPE_DEBUG,
2124 "Going to send pushes to %u (ceil (%f * %u)) peers.\n",
2125 a_peers, alpha, view_size);
2126 for (i = 0; i < a_peers; i++)
2128 peer = view_array[permut[i]];
2129 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer)) // TODO
2130 { // FIXME if this fails schedule/loop this for later
2135 /* Send PULL requests */
2136 b_peers = a_peers + floor (beta * view_size);
2137 LOG (GNUNET_ERROR_TYPE_DEBUG,
2138 "Going to send pulls to %u (ceil (%f * %u)) peers.\n",
2139 b_peers, beta, view_size);
2140 for (i = a_peers; i < b_peers; i++)
2142 peer = view_array[permut[i]];
2143 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer) &&
2144 GNUNET_YES != in_arr (pending_pull_reply_list,
2145 pending_pull_reply_list_size,
2147 { // FIXME if this fails schedule/loop this for later
2148 send_pull_request (&peer);
2152 GNUNET_free (permut);
2158 /* TODO see how many peers are in push-/pull- list! */
2160 if (push_list_size <= alpha * view_size &&
2161 0 < push_list_size &&
2164 LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the gossip list.\n");
2166 uint32_t first_border;
2167 uint32_t second_border;
2168 uint32_t final_size;
2169 uint32_t peers_to_clean_size;
2170 struct GNUNET_PeerIdentity *peers_to_clean;
2172 peers_to_clean = NULL;
2173 peers_to_clean_size = 0;
2174 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, view_size);
2175 memcpy (peers_to_clean,
2177 view_size * sizeof (struct GNUNET_PeerIdentity));
2179 /* Seems like recreating is the easiest way of emptying the peermap */
2180 GNUNET_CONTAINER_multipeermap_destroy (view);
2181 view = GNUNET_CONTAINER_multipeermap_create (view_size, GNUNET_NO);
2183 /* first_border = ceil (alpha * sampler_size_est_need);
2184 second_border = first_border + ceil (beta * sampler_size_est_need); */
2185 first_border = GNUNET_MIN (ceil (alpha * sampler_size_est_need),
2187 second_border = first_border +
2188 GNUNET_MIN (floor (beta * sampler_size_est_need),
2190 final_size = second_border +
2191 ceil ((1 - (alpha + beta)) * sampler_size_est_need);
2193 GNUNET_array_grow (view_array, view_size, second_border);
2195 to_file (file_name_view_log,
2198 /* Update view with peers received through PUSHes */
2199 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2201 for (i = 0; i < first_border; i++)
2203 view_array[i] = push_list[permut[i]];
2204 GNUNET_CONTAINER_multipeermap_put (view, &push_list[permut[i]], NULL,
2205 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2207 to_file (file_name_view_log,
2209 GNUNET_i2s_full (&view_array[i]));
2210 // TODO change the peer_flags accordingly
2212 GNUNET_free (permut);
2215 /* Update view with peers received through PULLs */
2216 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2218 for (i = first_border; i < second_border; i++)
2220 view_array[i] = pull_list[permut[i]];
2221 GNUNET_CONTAINER_multipeermap_put (view, &pull_list[permut[i]], NULL,
2222 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2224 to_file (file_name_view_log,
2226 GNUNET_i2s_full (&view_array[i]));
2227 // TODO change the peer_flags accordingly
2229 GNUNET_free (permut);
2232 /* Update view with peers from history */
2233 RPS_sampler_get_n_rand_peers (prot_sampler,
2236 final_size - second_border);
2237 num_hist_update_tasks = final_size - second_border;
2238 // TODO change the peer_flags accordingly
2240 for (i = 0; i < view_size; i++)
2241 rem_from_list (&peers_to_clean, &peers_to_clean_size, &view_array[i]);
2243 /* Clean peers that were removed from the view */
2244 for (i = 0; i < peers_to_clean_size; i++)
2246 peer_clean (&peers_to_clean[i]);
2247 /* to_file (file_name_view_log,
2249 GNUNET_i2s_full (&peers_to_clean[i])); */
2252 GNUNET_free (peers_to_clean);
2253 peers_to_clean = NULL;
2257 LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the gossip list.\n");
2259 // TODO independent of that also get some peers from CADET_get_peers()?
2261 LOG (GNUNET_ERROR_TYPE_DEBUG,
2262 "Received %u pushes and %u pulls last round (alpha (%.2f) * view_size (%u) = %.2f)\n",
2269 /* Update samplers */
2270 for (i = 0; i < push_list_size; i++)
2272 LOG (GNUNET_ERROR_TYPE_DEBUG,
2273 "Updating with peer %s from push list\n",
2274 GNUNET_i2s (&push_list[i]));
2275 RPS_sampler_update (prot_sampler, &push_list[i]);
2276 RPS_sampler_update (client_sampler, &push_list[i]);
2277 // TODO set in_flag?
2280 for (i = 0; i < pull_list_size; i++)
2282 LOG (GNUNET_ERROR_TYPE_DEBUG,
2283 "Updating with peer %s from pull list\n",
2284 GNUNET_i2s (&pull_list[i]));
2285 RPS_sampler_update (prot_sampler, &pull_list[i]);
2286 RPS_sampler_update (client_sampler, &pull_list[i]);
2287 // TODO set in_flag?
2291 /* Empty push/pull lists */
2292 GNUNET_array_grow (push_list, push_list_size, 0);
2293 GNUNET_array_grow (pull_list, pull_list_size, 0);
2295 struct GNUNET_TIME_Relative time_next_round;
2297 time_next_round = compute_rand_delay (round_interval, 2);
2299 /* Schedule next round */
2300 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_round, NULL);
2301 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
2306 rps_start (struct GNUNET_SERVER_Handle *server);
2310 * This is called from GNUNET_CADET_get_peers().
2312 * It is called on every peer(ID) that cadet somehow has contact with.
2313 * We use those to initialise the sampler.
2316 init_peer_cb (void *cls,
2317 const struct GNUNET_PeerIdentity *peer,
2318 int tunnel, // "Do we have a tunnel towards this peer?"
2319 unsigned int n_paths, // "Number of known paths towards this peer"
2320 unsigned int best_path) // "How long is the best path?
2321 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
2325 LOG (GNUNET_ERROR_TYPE_DEBUG,
2326 "Got peer_id %s from cadet\n",
2334 * Iterator over peers from peerinfo.
2336 * @param cls closure
2337 * @param peer id of the peer, NULL for last call
2338 * @param hello hello message for the peer (can be NULL)
2339 * @param error message
2342 process_peerinfo_peers (void *cls,
2343 const struct GNUNET_PeerIdentity *peer,
2344 const struct GNUNET_HELLO_Message *hello,
2345 const char *err_msg)
2349 LOG (GNUNET_ERROR_TYPE_DEBUG,
2350 "Got peer_id %s from peerinfo\n",
2358 * Clean the send channel of a peer
2361 peer_clean (const struct GNUNET_PeerIdentity *peer)
2363 struct PeerContext *peer_ctx;
2364 struct GNUNET_CADET_Channel *channel;
2366 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_contains (view, peer) &&
2367 GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
2369 peer_ctx = get_peer_ctx (peer_map, peer);
2370 if (NULL != peer_ctx->send_channel)
2372 channel = peer_ctx->send_channel;
2373 peer_ctx->send_channel = NULL;
2374 GNUNET_CADET_channel_destroy (channel);
2381 * Callback used to remove peers from the multipeermap.
2384 peer_remove_cb (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
2386 struct PeerContext *peer_ctx;
2387 const struct GNUNET_CADET_Channel *channel =
2388 (const struct GNUNET_CADET_Channel *) cls;
2390 peer_ctx = (struct PeerContext *) value;
2391 set_peer_flag (peer_ctx, TO_DESTROY);
2393 LOG (GNUNET_ERROR_TYPE_DEBUG,
2394 "Going to clean peer %s\n",
2395 GNUNET_i2s (&peer_ctx->peer_id));
2397 /* If operations are still scheduled for this peer cancel those */
2398 if (0 != peer_ctx->num_outstanding_ops)
2400 GNUNET_array_grow (peer_ctx->outstanding_ops,
2401 peer_ctx->num_outstanding_ops,
2405 /* If we are still waiting for notification whether this peer is live
2406 * cancel the according task */
2407 if (NULL != peer_ctx->is_live_task)
2409 LOG (GNUNET_ERROR_TYPE_DEBUG,
2410 "Trying to cancle is_live_task for peer %s\n",
2412 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
2413 peer_ctx->is_live_task = NULL;
2416 rem_from_list (&pending_pull_reply_list,
2417 &pending_pull_reply_list_size,
2420 to_file (file_name_view_log,
2421 "-%s\t(cleanup channel, other peer)",
2422 GNUNET_i2s_full (key));
2423 GNUNET_CONTAINER_multipeermap_remove_all (view, key);
2425 /* If there is still a mq destroy it */
2426 if (NULL != peer_ctx->mq)
2428 GNUNET_MQ_destroy (peer_ctx->mq);
2429 peer_ctx->mq = NULL;
2433 /* Remove the send_channel
2434 * This function should be called again from #cleanup_channel (callback
2435 * called on the destruction of channels) and clean up the rest. */
2436 if (NULL != peer_ctx->send_channel &&
2437 channel != peer_ctx->send_channel)
2439 GNUNET_CADET_channel_destroy (peer_ctx->send_channel);
2440 peer_ctx->send_channel = NULL;
2443 /* Remove the recv_channel
2444 * This function should be called again from #cleanup_channel (callback
2445 * called on the destruction of channels) and clean up the rest. */
2446 if (NULL != peer_ctx->recv_channel &&
2447 channel != peer_ctx->recv_channel)
2449 GNUNET_CADET_channel_destroy (peer_ctx->recv_channel);
2450 peer_ctx->recv_channel = NULL;
2453 /* If there is no channel we have to remove the context now */
2454 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_remove_all (peer_map, key))
2455 LOG (GNUNET_ERROR_TYPE_WARNING, "removing peer from peer_map failed\n");
2457 GNUNET_free (peer_ctx);
2464 * Task run during shutdown.
2470 shutdown_task (void *cls,
2471 const struct GNUNET_SCHEDULER_TaskContext *tc)
2474 LOG (GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
2476 GNUNET_PEERINFO_notify_cancel (peerinfo_notify_handle);
2477 GNUNET_PEERINFO_disconnect (peerinfo_handle);
2479 if (NULL != do_round_task)
2481 GNUNET_SCHEDULER_cancel (do_round_task);
2482 do_round_task = NULL;
2486 if (GNUNET_SYSERR ==
2487 GNUNET_CONTAINER_multipeermap_iterate (peer_map, peer_remove_cb, NULL))
2488 LOG (GNUNET_ERROR_TYPE_WARNING,
2489 "Iterating over peers to disconnect from them was cancelled\n");
2492 GNUNET_NSE_disconnect (nse);
2493 RPS_sampler_destroy (prot_sampler);
2494 RPS_sampler_destroy (client_sampler);
2495 LOG (GNUNET_ERROR_TYPE_DEBUG,
2496 "Size of the peermap: %u\n",
2497 GNUNET_CONTAINER_multipeermap_size (peer_map));
2498 GNUNET_break (0 != GNUNET_CONTAINER_multipeermap_size (peer_map));
2499 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
2500 GNUNET_CADET_disconnect (cadet_handle);
2501 GNUNET_CONTAINER_multipeermap_destroy (view);
2503 GNUNET_array_grow (push_list, push_list_size, 0);
2504 GNUNET_array_grow (pull_list, pull_list_size, 0);
2505 #ifdef ENABLE_MALICIOUS
2506 struct AttackedPeer *tmp_att_peer;
2507 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
2508 if (NULL != mal_peer_set)
2509 GNUNET_CONTAINER_multipeermap_destroy (mal_peer_set);
2510 if (NULL != att_peer_set)
2511 GNUNET_CONTAINER_multipeermap_destroy (att_peer_set);
2512 while (NULL != att_peers_head)
2514 tmp_att_peer = att_peers_head;
2515 GNUNET_CONTAINER_DLL_remove (att_peers_head, att_peers_tail, tmp_att_peer);
2517 #endif /* ENABLE_MALICIOUS */
2522 * A client disconnected. Remove all of its data structure entries.
2524 * @param cls closure, NULL
2525 * @param client identification of the client
2528 handle_client_disconnect (void *cls,
2529 struct GNUNET_SERVER_Client * client)
2535 * Handle the channel a peer opens to us.
2537 * @param cls The closure
2538 * @param channel The channel the peer wants to establish
2539 * @param initiator The peer's peer ID
2540 * @param port The port the channel is being established over
2541 * @param options Further options
2544 handle_inbound_channel (void *cls,
2545 struct GNUNET_CADET_Channel *channel,
2546 const struct GNUNET_PeerIdentity *initiator,
2548 enum GNUNET_CADET_ChannelOption options)
2550 struct PeerContext *peer_ctx;
2551 struct GNUNET_PeerIdentity peer;
2554 LOG (GNUNET_ERROR_TYPE_DEBUG,
2555 "New channel was established to us (Peer %s).\n",
2556 GNUNET_i2s (&peer));
2558 GNUNET_assert (NULL != channel);
2560 // we might not even store the recv_channel
2562 peer_ctx = get_peer_ctx (peer_map, &peer);
2563 // FIXME what do we do if a channel is established twice?
2564 // overwrite? Clean old channel? ...?
2565 //if (NULL != peer_ctx->recv_channel)
2567 // peer_ctx->recv_channel = channel;
2569 peer_ctx->recv_channel = channel;
2571 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, &peer, peer_ctx,
2572 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
2574 /* This would make the push-message unnecessary */
2575 LOG (GNUNET_ERROR_TYPE_DEBUG,
2576 "Got peer_id %s from peerinfo\n",
2577 GNUNET_i2s (&peer));
2578 new_peer_id (&peer);
2580 peer_is_live (peer_ctx);
2582 return NULL; // TODO
2587 * This is called when a channel is destroyed.
2589 * @param cls The closure
2590 * @param channel The channel being closed
2591 * @param channel_ctx The context associated with this channel
2594 cleanup_channel (void *cls,
2595 const struct GNUNET_CADET_Channel *channel,
2598 struct GNUNET_PeerIdentity *peer;
2599 struct PeerContext *peer_ctx;
2601 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
2602 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
2603 // Guess simply casting isn't the nicest way...
2604 // FIXME wait for cadet to change this function
2606 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
2607 {/* We don't want to implicitly create a context that we're about to kill */
2608 peer_ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
2609 if (NULL == peer_ctx) /* It could have been removed by shutdown_task */
2612 if (get_peer_flag (peer_ctx, TO_DESTROY))
2613 {/* We initiatad the destruction of this particular peer */
2614 if (channel == peer_ctx->send_channel)
2615 peer_ctx->send_channel = NULL;
2616 else if (channel == peer_ctx->recv_channel)
2617 peer_ctx->recv_channel = NULL;
2619 to_file (file_name_view_log,
2620 "-%s\t(cleanup channel, ourself)",
2621 GNUNET_i2s_full (peer));
2625 { /* We did not initiate the destruction of this peer */
2626 if (channel == peer_ctx->send_channel)
2627 { /* Something (but us) killd the channel - clean up peer */
2628 LOG (GNUNET_ERROR_TYPE_DEBUG,
2629 "send channel (%s) was destroyed - cleaning up\n",
2631 peer_ctx->send_channel = NULL;
2632 /* Somwewhat {ab,re}use the iterator function */
2633 /* Cast to void is ok, because it's used as void in peer_remove_cb */
2634 (void) peer_remove_cb ((void *) channel, peer, peer_ctx);
2636 else if (channel == peer_ctx->recv_channel)
2637 { /* Other peer doesn't want to send us messages anymore */
2638 LOG (GNUNET_ERROR_TYPE_DEBUG,
2639 "Peer %s destroyed recv channel - cleaning up channel\n",
2641 peer_ctx->recv_channel = NULL;
2645 LOG (GNUNET_ERROR_TYPE_WARNING,
2646 "unknown channel (%s) was destroyed\n",
2653 { /* We don't know a context to that peer */
2654 LOG (GNUNET_ERROR_TYPE_DEBUG,
2655 "channel (%s) without associated context was destroyed\n",
2662 * Actually start the service.
2665 rps_start (struct GNUNET_SERVER_Handle *server)
2667 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
2668 {&handle_client_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
2669 sizeof (struct GNUNET_RPS_CS_RequestMessage)},
2670 {&handle_client_seed, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_SEED, 0},
2671 #ifdef ENABLE_MALICIOUS
2672 {&handle_client_act_malicious, NULL, GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS , 0},
2673 #endif /* ENABLE_MALICIOUS */
2677 GNUNET_SERVER_add_handlers (server, handlers);
2678 GNUNET_SERVER_disconnect_notify (server,
2679 &handle_client_disconnect,
2681 LOG (GNUNET_ERROR_TYPE_INFO, "Ready to receive requests from clients\n");
2684 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
2685 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
2687 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2694 * Process statistics requests.
2696 * @param cls closure
2697 * @param server the initialized server
2698 * @param c configuration to use
2702 struct GNUNET_SERVER_Handle *server,
2703 const struct GNUNET_CONFIGURATION_Handle *c)
2708 // TODO check what this does -- copied from gnunet-boss
2709 // - seems to work as expected
2710 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
2715 GNUNET_CRYPTO_get_peer_identity (cfg, &own_identity); // TODO check return value
2716 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2717 "STARTING SERVICE (rps) for peer [%s]\n",
2718 GNUNET_i2s (&own_identity));
2719 #ifdef ENABLE_MALICIOUS
2720 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2721 "Malicious execution compiled in.\n");
2722 #endif /* ENABLE_MALICIOUS */
2726 /* Get time interval from the configuration */
2727 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
2731 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read ROUNDINTERVAL from config\n");
2732 GNUNET_SCHEDULER_shutdown ();
2736 /* Get initial size of sampler/gossip list from the configuration */
2737 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "RPS",
2739 (long long unsigned int *) &sampler_size_est_need))
2741 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read INITSIZE from config\n");
2742 GNUNET_SCHEDULER_shutdown ();
2745 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", sampler_size_est_need);
2748 view = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO);
2750 /* file_name_view_log */
2751 if (GNUNET_OK != GNUNET_DISK_directory_create ("/tmp/rps/"))
2753 LOG (GNUNET_ERROR_TYPE_WARNING,
2754 "Failed to create directory /tmp/rps/\n");
2757 size = (14 + strlen (GNUNET_i2s_full (&own_identity)) + 1) * sizeof (char);
2758 file_name_view_log = GNUNET_malloc (size);
2759 out_size = GNUNET_snprintf (file_name_view_log,
2762 GNUNET_i2s_full (&own_identity));
2763 if (size < out_size ||
2766 LOG (GNUNET_ERROR_TYPE_WARNING,
2767 "Failed to write string to buffer (size: %i, out_size: %i)\n",
2773 /* connect to NSE */
2774 nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
2780 peer_map = GNUNET_CONTAINER_multipeermap_create (sampler_size_est_need, GNUNET_NO);
2783 /* Initialise cadet */
2784 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
2785 {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH ,
2786 sizeof (struct GNUNET_MessageHeader)},
2787 {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
2788 sizeof (struct GNUNET_MessageHeader)},
2789 {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY , 0},
2793 const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
2794 cadet_handle = GNUNET_CADET_connect (cfg,
2796 &handle_inbound_channel,
2801 peerinfo_handle = GNUNET_PEERINFO_connect (cfg);
2803 /* Initialise sampler */
2804 struct GNUNET_TIME_Relative half_round_interval;
2805 struct GNUNET_TIME_Relative max_round_interval;
2807 half_round_interval = GNUNET_TIME_relative_multiply (round_interval, .5);
2808 max_round_interval = GNUNET_TIME_relative_add (round_interval, half_round_interval);
2810 prot_sampler = RPS_sampler_init (sampler_size_est_need, max_round_interval);
2811 client_sampler = RPS_sampler_mod_init (sampler_size_est_need, max_round_interval);
2813 /* Initialise push and pull maps */
2818 pending_pull_reply_list = NULL;
2819 pending_pull_reply_list_size = 0;
2822 num_hist_update_tasks = 0;
2825 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
2826 GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, NULL);
2827 // TODO send push/pull to each of those peers?
2829 peerinfo_notify_handle = GNUNET_PEERINFO_notify (cfg,
2831 process_peerinfo_peers,
2839 * The main function for the rps service.
2841 * @param argc number of arguments from the command line
2842 * @param argv command line arguments
2843 * @return 0 ok, 1 on error
2846 main (int argc, char *const *argv)
2848 return (GNUNET_OK ==
2849 GNUNET_SERVICE_run (argc,
2852 GNUNET_SERVICE_OPTION_NONE,
2853 &run, NULL)) ? 0 : 1;
2856 /* end of gnunet-service-rps.c */