2 This file is part of GNUnet.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file rps/gnunet-service-rps.c
23 * @brief rps service implementation
24 * @author Julius Bünger
27 #include "gnunet_util_lib.h"
28 #include "gnunet_cadet_service.h"
29 #include "gnunet_peerinfo_service.h"
30 #include "gnunet_nse_service.h"
32 #include "gnunet-service-rps_peers.h"
33 #include "rps-test_util.h"
35 #include "gnunet-service-rps_sampler.h"
40 #define LOG(kind, ...) GNUNET_log(kind, __VA_ARGS__)
42 // TODO modify @brief in every file
44 // TODO check for overflows
46 // TODO align message structs
48 // TODO connect to friends
50 // TODO store peers somewhere persistent
52 // TODO blacklist? (-> mal peer detection on top of brahms)
54 // TODO API request_cancel
56 // hist_size_init, hist_size_max
61 static const struct GNUNET_CONFIGURATION_Handle *cfg;
66 static struct GNUNET_PeerIdentity own_identity;
69 struct GNUNET_PeerIdentity *
70 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list, unsigned int size,
71 const struct GNUNET_PeerIdentity *ignore_list, unsigned int ignore_size);
74 /***********************************************************************
75 * Housekeeping with peers
76 ***********************************************************************/
79 * Closure used to pass the client and the id to the callback
80 * that replies to a client's request
87 struct ReplyCls *next;
88 struct ReplyCls *prev;
91 * The identifier of the request
96 * The handle to the request
98 struct RPS_SamplerRequestHandle *req_handle;
101 * The client handle to send the reply to
103 struct GNUNET_SERVER_Client *client;
108 * Struct used to store the context of a connected client.
115 struct ClientContext *next;
116 struct ClientContext *prev;
119 * The message queue to communicate with the client.
121 struct GNUNET_MQ_Handle *mq;
124 * DLL with handles to single requests from the client
126 struct ReplyCls *rep_cls_head;
127 struct ReplyCls *rep_cls_tail;
131 * DLL with all clients currently connected to us
133 struct ClientContext *cli_ctx_head;
134 struct ClientContext *cli_ctx_tail;
137 * Used to keep track in what lists single peerIDs are.
142 * If we are waiting for a reply from that peer (sent a pull request).
144 PULL_REPLY_PENDING = 0x01,
146 IN_OTHER_GOSSIP_LIST = 0x02, // unneeded?
147 IN_OWN_SAMPLER_LIST = 0x04, // unneeded?
148 IN_OWN_GOSSIP_LIST = 0x08, // unneeded?
151 * We set this bit when we can be sure the other peer is/was live.
156 * We set this bit when we are going to destroy the channel to this peer.
157 * When cleanup_channel is called, we know that we wanted to destroy it.
158 * Otherwise the channel to the other peer was destroyed.
165 * Functions of this type can be used to be stored at a peer for later execution.
167 typedef void (* PeerOp) (void *cls, const struct GNUNET_PeerIdentity *peer);
170 * Outstanding operation on peer consisting of callback and closure
172 struct PeerOutstandingOp
187 * List containing all messages that are yet to be send
189 struct PendingMessage
194 struct PendingMessage *next;
195 struct PendingMessage *prev;
198 * The envelope to the corresponding message
200 struct GNUNET_MQ_Envelope *ev;
203 * The corresponding context
205 struct PeerContext *peer_ctx;
215 * Struct used to keep track of other peer's status
217 * This is stored in a multipeermap.
222 * Message queue open to client
224 struct GNUNET_MQ_Handle *mq;
227 * Channel open to client.
229 struct GNUNET_CADET_Channel *send_channel;
232 * Channel open from client.
234 struct GNUNET_CADET_Channel *recv_channel; // unneeded?
237 * Array of outstanding operations on this peer.
239 struct PeerOutstandingOp *outstanding_ops;
242 * Handle to the callback given to cadet_ntfy_tmt_rdy()
244 * To be canceled on shutdown.
246 struct GNUNET_CADET_TransmitHandle *transmit_handle;
249 * Number of outstanding operations.
251 unsigned int num_outstanding_ops;
254 * Identity of the peer
256 struct GNUNET_PeerIdentity peer_id;
259 * Flags indicating status of peer
264 * DLL with all messages that are yet to be sent
266 struct PendingMessage *pending_messages_head;
267 struct PendingMessage *pending_messages_tail;
270 * This is pobably followed by 'statistical' data (when we first saw
271 * him, how did we get his ID, how many pushes (in a timeinterval),
276 /***********************************************************************
277 * /Housekeeping with peers
278 ***********************************************************************/
284 /***********************************************************************
286 ***********************************************************************/
289 * Sampler used for the Brahms protocol itself.
291 static struct RPS_Sampler *prot_sampler;
294 * Sampler used for the clients.
296 static struct RPS_Sampler *client_sampler;
299 * Set of all peers to keep track of them.
301 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
305 * Name to log view to
307 static char *file_name_view_log;
310 * The "local view" containing peers we learned from gossip and history
312 static struct GNUNET_CONTAINER_MultiPeerMap *view;
315 * An array containing the peers of the local view.
317 * This is created every time we send a pull reply if it has changed since the
318 * last pull reply we sent.
320 static struct GNUNET_PeerIdentity *view_array;
324 * The size of sampler we need to be able to satisfy the client's need of
327 static unsigned int sampler_size_client_need;
330 * The size of sampler we need to be able to satisfy the Brahms protocol's
331 * need of random peers.
333 * This is one minimum size the sampler grows to.
335 static unsigned int sampler_size_est_need;
339 * Percentage of total peer number in the view
340 * to send random PUSHes to
345 * Percentage of total peer number in the view
346 * to send random PULLs to
351 * The percentage gamma of history updates.
352 * Simply 1 - alpha - beta
357 * Identifier for the main task that runs periodically.
359 static struct GNUNET_SCHEDULER_Task *do_round_task;
362 * Time inverval the do_round task runs in.
364 static struct GNUNET_TIME_Relative round_interval;
369 * List to store peers received through pushes temporary.
371 static struct CustomPeerMap *push_map;
374 * List to store peers received through pulls temporary.
376 static struct CustomPeerMap *pull_map;
382 static struct GNUNET_NSE_Handle *nse;
387 static struct GNUNET_CADET_Handle *cadet_handle;
390 * Handler to PEERINFO.
392 static struct GNUNET_PEERINFO_Handle *peerinfo_handle;
395 * Handle for cancellation of iteration over peers.
397 struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify_handle;
403 * Only needed in the beginning to check how many of the 64 deltas
406 static unsigned int req_counter;
409 * Time of the last request we received.
411 * Used to compute the expected request rate.
413 static struct GNUNET_TIME_Absolute last_request;
416 * Size of #request_deltas.
418 #define REQUEST_DELTAS_SIZE 64
419 static unsigned int request_deltas_size = REQUEST_DELTAS_SIZE;
422 * Last 64 deltas between requests
424 static struct GNUNET_TIME_Relative request_deltas[REQUEST_DELTAS_SIZE];
427 * The prediction of the rate of requests
429 static struct GNUNET_TIME_Relative request_rate;
433 * Number of history update tasks.
435 uint32_t num_hist_update_tasks;
438 #ifdef ENABLE_MALICIOUS
440 * Type of malicious peer
442 * 0 Don't act malicious at all - Default
443 * 1 Try to maximise representation
444 * 2 Try to partition the network
447 uint32_t mal_type = 0;
450 * Other malicious peers
452 static struct GNUNET_PeerIdentity *mal_peers = NULL;
455 * Hashmap of malicious peers used as set.
456 * Used to more efficiently check whether we know that peer.
458 static struct GNUNET_CONTAINER_MultiPeerMap *mal_peer_set = NULL;
461 * Number of other malicious peers
463 static uint32_t num_mal_peers;
467 * If type is 2 This struct is used to store the attacked peers in a DLL
474 struct AttackedPeer *next;
475 struct AttackedPeer *prev;
480 struct GNUNET_PeerIdentity peer_id;
484 * If type is 2 this is the DLL of attacked peers
486 static struct AttackedPeer *att_peers_head = NULL;
487 static struct AttackedPeer *att_peers_tail = NULL;
490 * This index is used to point to an attacked peer to
491 * implement the round-robin-ish way to select attacked peers.
493 static struct AttackedPeer *att_peer_index = NULL;
496 * Hashmap of attacked peers used as set.
497 * Used to more efficiently check whether we know that peer.
499 static struct GNUNET_CONTAINER_MultiPeerMap *att_peer_set = NULL;
502 * Number of attacked peers
504 static uint32_t num_attacked_peers = 0;
508 * If type is 1 this is the attacked peer
510 static struct GNUNET_PeerIdentity attacked_peer;
513 * The limit of PUSHes we can send in one round.
514 * This is an assumption of the Brahms protocol and either implemented
517 * assumend to be the bandwidth limitation.
519 static uint32_t push_limit = 10000;
520 #endif /* ENABLE_MALICIOUS */
523 /***********************************************************************
525 ***********************************************************************/
532 /***********************************************************************
534 ***********************************************************************/
537 * Set a peer flag of given peer context.
539 #define set_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags |= mask)
542 * Get peer flag of given peer context.
544 #define get_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags & mask ? GNUNET_YES : GNUNET_NO)
547 * Unset flag of given peer context.
549 #define unset_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags &= (~mask))
552 * Clean the send channel of a peer
555 peer_clean (const struct GNUNET_PeerIdentity *peer);
559 * Check if peer is already in peer array.
562 in_arr (const struct GNUNET_PeerIdentity *array,
563 unsigned int arr_size,
564 const struct GNUNET_PeerIdentity *peer)
566 GNUNET_assert (NULL != peer);
571 GNUNET_assert (NULL != array);
575 for (i = 0; i < arr_size ; i++)
576 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&array[i], peer))
583 * Print peerlist to log.
586 print_peer_list (struct GNUNET_PeerIdentity *list, unsigned int len)
590 LOG (GNUNET_ERROR_TYPE_DEBUG,
591 "Printing peer list of length %u at %p:\n",
594 for (i = 0 ; i < len ; i++)
596 LOG (GNUNET_ERROR_TYPE_DEBUG,
598 i, GNUNET_i2s (&list[i]));
604 * Remove peer from list.
607 rem_from_list (struct GNUNET_PeerIdentity **peer_list,
608 unsigned int *list_size,
609 const struct GNUNET_PeerIdentity *peer)
612 struct GNUNET_PeerIdentity *tmp;
616 LOG (GNUNET_ERROR_TYPE_DEBUG,
617 "Removing peer %s from list at %p\n",
621 for ( i = 0 ; i < *list_size ; i++ )
623 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&tmp[i], peer))
625 if (i < *list_size -1)
626 { /* Not at the last entry -- shift peers left */
627 memcpy (&tmp[i], &tmp[i +1],
628 ((*list_size) - i -1) * sizeof (struct GNUNET_PeerIdentity));
630 /* Remove last entry (should be now useless PeerID) */
631 GNUNET_array_grow (tmp, *list_size, (*list_size) -1);
638 * Get random peer from the given list but don't return one from the @a ignore_list.
640 struct GNUNET_PeerIdentity *
641 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list,
643 const struct GNUNET_PeerIdentity *ignore_list,
644 uint32_t ignore_size)
648 struct GNUNET_PeerIdentity *tmp_peer_list;
649 struct GNUNET_PeerIdentity *peer;
651 GNUNET_assert (NULL != peer_list);
656 tmp_peer_list = NULL;
657 GNUNET_array_grow (tmp_peer_list, tmp_size, list_size);
658 memcpy (tmp_peer_list,
660 list_size * sizeof (struct GNUNET_PeerIdentity));
661 peer = GNUNET_new (struct GNUNET_PeerIdentity);
664 * Choose the r_index of the peer we want to return
665 * at random from the interval of the view
667 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
669 *peer = tmp_peer_list[r_index];
671 while (in_arr (ignore_list, ignore_size, peer))
673 rem_from_list (&tmp_peer_list, &tmp_size, peer);
675 print_peer_list (tmp_peer_list, tmp_size);
684 * Choose the r_index of the peer we want to return
685 * at random from the interval of the view
687 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
689 *peer = tmp_peer_list[r_index];
693 GNUNET_array_grow (tmp_peer_list, tmp_size, 0);
700 * Get the context of a peer. If not existing, create.
703 get_peer_ctx (const struct GNUNET_PeerIdentity *peer)
705 struct PeerContext *ctx;
708 ret = GNUNET_CONTAINER_multipeermap_contains (peer_map, peer);
709 GNUNET_assert (GNUNET_YES == ret);
710 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
711 GNUNET_assert (NULL != ctx);
716 * Create a new peer context and insert it into the peer map
719 create_peer_ctx (const struct GNUNET_PeerIdentity *peer)
721 struct PeerContext *ctx;
724 ctx = GNUNET_new (struct PeerContext);
725 ctx->peer_id = *peer;
726 ret = GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
727 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
728 GNUNET_assert (GNUNET_OK == ret);
734 * Set the peer flag to living and call the outstanding operations on this peer.
737 peer_is_live (struct PeerContext *peer_ctx)
739 struct GNUNET_PeerIdentity *peer;
741 /* Cancle transmit_handle if still scheduled */
742 if (NULL != peer_ctx->transmit_handle)
744 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->transmit_handle);
745 peer_ctx->transmit_handle = NULL;
748 peer = &peer_ctx->peer_id;
749 set_peer_flag (peer_ctx, VALID);
751 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer %s is live\n", GNUNET_i2s (peer));
753 if (0 < peer_ctx->num_outstanding_ops)
754 { /* Call outstanding operations */
757 for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
758 peer_ctx->outstanding_ops[i].op (peer_ctx->outstanding_ops[i].op_cls, peer);
759 GNUNET_array_grow (peer_ctx->outstanding_ops, peer_ctx->num_outstanding_ops, 0);
767 * Callback that is called when a channel was effectively established.
768 * This is given to ntfy_tmt_rdy and called when the channel was
769 * successfully established.
772 cadet_ntfy_tmt_rdy_cb (void *cls, size_t size, void *buf)
774 struct PeerContext *peer_ctx = (struct PeerContext *) cls;
776 peer_ctx->transmit_handle = NULL;
777 LOG (GNUNET_ERROR_TYPE_DEBUG,
778 "Set ->transmit_handle = NULL for peer %s\n",
779 GNUNET_i2s (&peer_ctx->peer_id));
784 peer_is_live (peer_ctx);
788 LOG (GNUNET_ERROR_TYPE_WARNING,
789 "Problems establishing a connection to peer %s in order to check liveliness\n",
790 GNUNET_i2s (&peer_ctx->peer_id));
791 // TODO reschedule? cleanup?
794 //if (NULL != peer_ctx->transmit_handle)
796 // LOG (GNUNET_ERROR_TYPE_DEBUG,
797 // "Trying to cancle transmit_handle for peer %s\n",
798 // GNUNET_i2s (&peer_ctx->peer_id));
799 // GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->transmit_handle);
800 // peer_ctx->transmit_handle = NULL;
808 * Get the channel of a peer. If not existing, create.
810 struct GNUNET_CADET_Channel *
811 get_channel (const struct GNUNET_PeerIdentity *peer)
813 struct PeerContext *peer_ctx;
815 peer_ctx = get_peer_ctx (peer);
816 if (NULL == peer_ctx->send_channel)
818 LOG (GNUNET_ERROR_TYPE_DEBUG,
819 "Trying to establish channel to peer %s\n",
822 peer_ctx->send_channel =
823 GNUNET_CADET_channel_create (cadet_handle,
826 GNUNET_RPS_CADET_PORT,
827 GNUNET_CADET_OPTION_RELIABLE);
830 return peer_ctx->send_channel;
835 * Get the message queue of a specific peer.
837 * If we already have a message queue open to this client,
838 * simply return it, otherways create one.
840 struct GNUNET_MQ_Handle *
841 get_mq (const struct GNUNET_PeerIdentity *peer_id)
843 struct PeerContext *peer_ctx;
845 peer_ctx = get_peer_ctx (peer_id);
847 GNUNET_assert (NULL == peer_ctx->transmit_handle);
849 if (NULL == peer_ctx->mq)
851 (void) get_channel (peer_id);
852 peer_ctx->mq = GNUNET_CADET_mq_create (peer_ctx->send_channel);
853 //do I have to explicitly put it in the peer_map?
854 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer_id, peer_ctx,
855 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
862 * Issue check whether peer is live
864 * @param peer_ctx the context of the peer
867 check_peer_live (struct PeerContext *peer_ctx)
869 LOG (GNUNET_ERROR_TYPE_DEBUG,
870 "Get informed about peer %s getting live\n",
871 GNUNET_i2s (&peer_ctx->peer_id));
873 if (NULL == peer_ctx->transmit_handle &&
874 NULL == peer_ctx->send_channel)
876 (void) get_channel (&peer_ctx->peer_id);
877 peer_ctx->transmit_handle =
878 GNUNET_CADET_notify_transmit_ready (peer_ctx->send_channel,
880 GNUNET_TIME_UNIT_FOREVER_REL,
881 sizeof (struct GNUNET_MessageHeader),
882 cadet_ntfy_tmt_rdy_cb,
885 else if (NULL != peer_ctx->transmit_handle)
886 LOG (GNUNET_ERROR_TYPE_DEBUG,
887 "Already waiting for notification\n");
888 else if (NULL != peer_ctx->send_channel)
889 LOG (GNUNET_ERROR_TYPE_DEBUG,
890 "Already have established channel to peer\n");
895 * Sum all time relatives of an array.
897 struct GNUNET_TIME_Relative
898 T_relative_sum (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
900 struct GNUNET_TIME_Relative sum;
903 sum = GNUNET_TIME_UNIT_ZERO;
904 for ( i = 0 ; i < arr_size ; i++ )
906 sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
913 * Compute the average of given time relatives.
915 struct GNUNET_TIME_Relative
916 T_relative_avg (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
918 return GNUNET_TIME_relative_divide (T_relative_sum (rel_array, arr_size), arr_size);
923 * Insert PeerID in #pull_map
925 * Called once we know a peer is live.
928 insert_in_pull_map (void *cls, const struct GNUNET_PeerIdentity *peer)
930 CustomPeerMap_put (pull_map, peer);
934 * Check whether #insert_in_pull_map was already scheduled
937 insert_in_pull_map_scheduled (const struct PeerContext *peer_ctx)
941 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
942 if (insert_in_pull_map == peer_ctx->outstanding_ops[i].op)
949 * Insert PeerID in #view
951 * Called once we know a peer is live.
954 insert_in_view (void *cls, const struct GNUNET_PeerIdentity *peer)
956 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (view, peer))
958 LOG (GNUNET_ERROR_TYPE_DEBUG,
959 "Going to put %s into view\n",
961 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_put (view,
964 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
966 LOG (GNUNET_ERROR_TYPE_WARNING,
967 "Failed to put peer into view. (insert_in_view)\n");
969 if (NULL != view_array)
971 GNUNET_free (view_array);
974 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
975 create_peer_ctx (peer);
976 (void) get_channel (peer);
980 * Check whether #insert_in_view was already scheduled
983 insert_in_view_scheduled (const struct PeerContext *peer_ctx)
987 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
988 if (insert_in_view == peer_ctx->outstanding_ops[i].op)
995 * Update sampler with given PeerID.
998 insert_in_sampler (void *cls, const struct GNUNET_PeerIdentity *peer)
1000 LOG (GNUNET_ERROR_TYPE_DEBUG,
1001 "Updating samplers with peer %s from insert_in_sampler()\n",
1003 RPS_sampler_update (prot_sampler, peer);
1004 RPS_sampler_update (client_sampler, peer);
1005 if (0 < RPS_sampler_count_id (prot_sampler, peer))
1007 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
1008 (void) create_peer_ctx (peer);
1009 (void) get_channel (peer);
1015 * Check whether #insert_in_sampler was already scheduled
1018 insert_in_sampler_scheduled (const struct PeerContext *peer_ctx)
1022 for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
1023 if (insert_in_sampler== peer_ctx->outstanding_ops[i].op)
1029 * Put random peer from sampler into the view as history update.
1032 hist_update (void *cls, struct GNUNET_PeerIdentity *ids, uint32_t num_peers)
1036 for (i = 0; i < GNUNET_MIN (
1037 sampler_size_est_need - GNUNET_CONTAINER_multipeermap_size (view),
1040 insert_in_view (NULL, &ids[i]);
1041 to_file (file_name_view_log,
1043 GNUNET_i2s_full (ids));
1045 if (0 < num_hist_update_tasks)
1046 num_hist_update_tasks--;
1052 * Wrapper around #RPS_sampler_resize()
1054 * If we do not have enough sampler elements, double current sampler size
1055 * If we have more than enough sampler elements, halv current sampler size
1058 resize_wrapper (struct RPS_Sampler *sampler, uint32_t new_size)
1060 unsigned int sampler_size;
1063 // TODO respect the min, max
1064 sampler_size = RPS_sampler_get_size (sampler);
1065 if (sampler_size > new_size * 4)
1067 RPS_sampler_resize (sampler, sampler_size / 2);
1069 else if (sampler_size < new_size)
1071 RPS_sampler_resize (sampler, sampler_size * 2);
1073 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
1078 * Wrapper around #RPS_sampler_resize() resizing the client sampler
1081 client_resize_wrapper ()
1083 uint32_t bigger_size;
1087 bigger_size = GNUNET_MAX (sampler_size_est_need, sampler_size_client_need);
1089 // TODO respect the min, max
1090 resize_wrapper (client_sampler, bigger_size);
1091 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size_client is now %" PRIu32 "\n",
1097 * Estimate request rate
1099 * Called every time we receive a request from the client.
1104 struct GNUNET_TIME_Relative max_round_duration;
1106 if (request_deltas_size > req_counter)
1108 if ( 1 < req_counter)
1110 /* Shift last request deltas to the right */
1111 memcpy (&request_deltas[1],
1113 (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
1115 /* Add current delta to beginning */
1117 GNUNET_TIME_absolute_get_difference (last_request,
1118 GNUNET_TIME_absolute_get ());
1119 request_rate = T_relative_avg (request_deltas, req_counter);
1120 request_rate = (request_rate.rel_value_us < 1) ?
1121 GNUNET_TIME_relative_get_unit_ () : request_rate;
1123 /* Compute the duration a round will maximally take */
1124 max_round_duration =
1125 GNUNET_TIME_relative_add (round_interval,
1126 GNUNET_TIME_relative_divide (round_interval, 2));
1128 /* Set the estimated size the sampler has to have to
1129 * satisfy the current client request rate */
1130 sampler_size_client_need =
1131 max_round_duration.rel_value_us / request_rate.rel_value_us;
1133 /* Resize the sampler */
1134 client_resize_wrapper ();
1136 last_request = GNUNET_TIME_absolute_get ();
1141 * Add all peers in @a peer_array to @a peer_map used as set.
1143 * @param peer_array array containing the peers
1144 * @param num_peers number of peers in @peer_array
1145 * @param peer_map the peermap to use as set
1148 add_peer_array_to_set (const struct GNUNET_PeerIdentity *peer_array,
1149 unsigned int num_peers,
1150 struct GNUNET_CONTAINER_MultiPeerMap *peer_map)
1153 if (NULL == peer_map)
1155 LOG (GNUNET_ERROR_TYPE_WARNING,
1156 "Trying to add peers to non-existing peermap.\n");
1160 for (i = 0; i < num_peers; i++)
1162 GNUNET_CONTAINER_multipeermap_put (peer_map,
1165 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1171 * @brief Add an envelope to a message passed to mq to list of pending messages
1173 * @param peer peer the message was sent to
1174 * @param ev envelope to the message
1175 * @param type type of the message to be sent
1177 static struct PendingMessage *
1178 insert_pending_message (const struct GNUNET_PeerIdentity *peer,
1179 struct GNUNET_MQ_Envelope *ev,
1182 struct PendingMessage *pending_msg;
1183 struct PeerContext *peer_ctx;
1185 peer_ctx = get_peer_ctx (peer);
1186 pending_msg = GNUNET_new (struct PendingMessage);
1187 pending_msg->ev = ev;
1188 pending_msg->peer_ctx = peer_ctx;
1189 pending_msg->type = type;
1190 GNUNET_CONTAINER_DLL_insert (peer_ctx->pending_messages_head,
1191 peer_ctx->pending_messages_tail,
1197 remove_pending_message (struct PendingMessage *pending_msg)
1199 struct PeerContext *peer_ctx;
1201 peer_ctx = pending_msg->peer_ctx;
1202 GNUNET_CONTAINER_DLL_remove (peer_ctx->pending_messages_head,
1203 peer_ctx->pending_messages_tail,
1205 GNUNET_free (pending_msg);
1210 * @brief This is called once a message is sent.
1212 * @param cls type of the message that was sent
1215 mq_notify_sent_cb (void *cls)
1217 struct PendingMessage *pending_msg = (struct PendingMessage *) cls;
1218 LOG (GNUNET_ERROR_TYPE_DEBUG,
1221 remove_pending_message (pending_msg);
1226 * Send a PULL REPLY to @a peer_id
1228 * @param peer_id the peer to send the reply to.
1229 * @param peer_ids the peers to send to @a peer_id
1230 * @param num_peer_ids the number of peers to send to @a peer_id
1233 send_pull_reply (const struct GNUNET_PeerIdentity *peer_id,
1234 const struct GNUNET_PeerIdentity *peer_ids,
1235 unsigned int num_peer_ids)
1238 struct GNUNET_MQ_Handle *mq;
1239 struct GNUNET_MQ_Envelope *ev;
1240 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
1241 struct PendingMessage *pending_msg;
1243 /* Compute actual size */
1244 send_size = sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) +
1245 num_peer_ids * sizeof (struct GNUNET_PeerIdentity);
1247 if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size)
1248 /* Compute number of peers to send
1249 * If too long, simply truncate */
1250 // TODO select random ones via permutation
1251 // or even better: do good protocol design
1253 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE -
1254 sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1255 sizeof (struct GNUNET_PeerIdentity);
1257 send_size = num_peer_ids;
1259 LOG (GNUNET_ERROR_TYPE_DEBUG,
1260 "Going to send PULL REPLY with %u peers to %s\n",
1261 send_size, GNUNET_i2s (peer_id));
1263 mq = get_mq (peer_id);
1265 ev = GNUNET_MQ_msg_extra (out_msg,
1266 send_size * sizeof (struct GNUNET_PeerIdentity),
1267 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
1268 out_msg->num_peers = htonl (send_size);
1269 memcpy (&out_msg[1], peer_ids,
1270 send_size * sizeof (struct GNUNET_PeerIdentity));
1272 pending_msg = insert_pending_message (peer_id, ev, "PULL REPLY");
1273 GNUNET_MQ_notify_sent (ev,
1276 GNUNET_MQ_send (mq, ev);
1281 * This function is called on new peer_ids from 'external' sources
1282 * (client seed, cadet get_peers(), ...)
1284 * @param peer_id the new peer_id
1287 new_peer_id (const struct GNUNET_PeerIdentity *peer_id)
1289 struct PeerOutstandingOp out_op;
1290 struct PeerContext *peer_ctx;
1292 if ((NULL == peer_id) ||
1293 (0 == GNUNET_CRYPTO_cmp_peer_identity (&own_identity, peer_id)))
1295 LOG (GNUNET_ERROR_TYPE_DEBUG,
1296 "Got peer_id %s (at %p, view size: %u)\n",
1297 GNUNET_i2s (peer_id),
1299 GNUNET_CONTAINER_multipeermap_size (view));
1301 /* if the seed peer is already know, skip context creation */
1302 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer_id))
1303 peer_ctx = create_peer_ctx (peer_id);
1305 peer_ctx = get_peer_ctx (peer_id);
1307 if (GNUNET_NO == get_peer_flag (peer_ctx, VALID))
1309 if (GNUNET_NO == insert_in_sampler_scheduled (peer_ctx))
1311 out_op.op = insert_in_sampler;
1312 out_op.op_cls = NULL;
1313 GNUNET_array_append (peer_ctx->outstanding_ops,
1314 peer_ctx->num_outstanding_ops,
1318 if (GNUNET_NO == insert_in_view_scheduled (peer_ctx))
1320 out_op.op = insert_in_view;
1321 out_op.op_cls = NULL;
1322 GNUNET_array_append (peer_ctx->outstanding_ops,
1323 peer_ctx->num_outstanding_ops,
1327 /* Trigger livelyness test on peer */
1328 check_peer_live (peer_ctx);
1332 // send push/pull to each of those peers?
1336 /***********************************************************************
1338 ***********************************************************************/
1341 destroy_reply_cls (struct ReplyCls *rep_cls)
1343 struct ClientContext *cli_ctx;
1345 cli_ctx = GNUNET_SERVER_client_get_user_context (rep_cls->client,
1346 struct ClientContext);
1347 GNUNET_assert (NULL != cli_ctx);
1348 GNUNET_CONTAINER_DLL_remove (cli_ctx->rep_cls_head,
1349 cli_ctx->rep_cls_tail,
1351 GNUNET_free (rep_cls);
1355 destroy_cli_ctx (struct ClientContext *cli_ctx)
1357 GNUNET_assert (NULL != cli_ctx);
1358 if (NULL != cli_ctx->rep_cls_head)
1360 LOG (GNUNET_ERROR_TYPE_WARNING,
1361 "Trying to destroy the context of a client that still has pending requests. Going to clean those\n");
1362 while (NULL != cli_ctx->rep_cls_head)
1363 destroy_reply_cls (cli_ctx->rep_cls_head);
1365 GNUNET_CONTAINER_DLL_remove (cli_ctx_head,
1368 GNUNET_free (cli_ctx);
1373 * Function called by NSE.
1375 * Updates sizes of sampler list and view and adapt those lists
1379 nse_callback (void *cls, struct GNUNET_TIME_Absolute timestamp,
1380 double logestimate, double std_dev)
1383 //double scale; // TODO this might go gloabal/config
1385 LOG (GNUNET_ERROR_TYPE_DEBUG,
1386 "Received a ns estimate - logest: %f, std_dev: %f (old_size: %u)\n",
1387 logestimate, std_dev, RPS_sampler_get_size (prot_sampler));
1389 estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
1390 // GNUNET_NSE_log_estimate_to_n (logestimate);
1391 estimate = pow (estimate, 1.0 / 3);
1392 // TODO add if std_dev is a number
1393 // estimate += (std_dev * scale);
1394 if (2 < ceil (estimate))
1396 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
1397 sampler_size_est_need = estimate;
1399 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
1401 /* If the NSE has changed adapt the lists accordingly */
1402 resize_wrapper (prot_sampler, sampler_size_est_need);
1403 client_resize_wrapper ();
1408 * Callback called once the requested PeerIDs are ready.
1410 * Sends those to the requesting client.
1412 void client_respond (void *cls,
1413 struct GNUNET_PeerIdentity *peer_ids, uint32_t num_peers)
1416 struct GNUNET_MQ_Envelope *ev;
1417 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
1418 struct ReplyCls *reply_cls = (struct ReplyCls *) cls;
1419 uint32_t size_needed;
1420 struct ClientContext *cli_ctx;
1422 GNUNET_assert (NULL != reply_cls);
1423 LOG (GNUNET_ERROR_TYPE_DEBUG,
1424 "sampler returned %" PRIu32 " peers:\n",
1426 for (i = 0; i < num_peers; i++)
1428 LOG (GNUNET_ERROR_TYPE_DEBUG,
1431 GNUNET_i2s (&peer_ids[i]));
1434 size_needed = sizeof (struct GNUNET_RPS_CS_ReplyMessage) +
1435 num_peers * sizeof (struct GNUNET_PeerIdentity);
1437 GNUNET_assert (GNUNET_SERVER_MAX_MESSAGE_SIZE >= size_needed);
1439 ev = GNUNET_MQ_msg_extra (out_msg,
1440 num_peers * sizeof (struct GNUNET_PeerIdentity),
1441 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
1442 out_msg->num_peers = htonl (num_peers);
1443 out_msg->id = htonl (reply_cls->id);
1445 memcpy (&out_msg[1],
1447 num_peers * sizeof (struct GNUNET_PeerIdentity));
1448 GNUNET_free (peer_ids);
1450 cli_ctx = GNUNET_SERVER_client_get_user_context (reply_cls->client,
1451 struct ClientContext);
1452 GNUNET_assert (NULL != cli_ctx);
1453 destroy_reply_cls (reply_cls);
1454 GNUNET_MQ_send (cli_ctx->mq, ev);
1459 * Handle RPS request from the client.
1461 * @param cls closure
1462 * @param client identification of the client
1463 * @param message the actual message
1466 handle_client_request (void *cls,
1467 struct GNUNET_SERVER_Client *client,
1468 const struct GNUNET_MessageHeader *message)
1470 struct GNUNET_RPS_CS_RequestMessage *msg;
1472 uint32_t size_needed;
1473 struct ReplyCls *reply_cls;
1475 struct ClientContext *cli_ctx;
1477 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
1479 num_peers = ntohl (msg->num_peers);
1480 size_needed = sizeof (struct GNUNET_RPS_CS_RequestMessage) +
1481 num_peers * sizeof (struct GNUNET_PeerIdentity);
1483 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
1485 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1486 "Message received from client has size larger than expected\n");
1487 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1491 for (i = 0 ; i < num_peers ; i++)
1494 LOG (GNUNET_ERROR_TYPE_DEBUG,
1495 "Client requested %" PRIu32 " random peer(s).\n",
1498 reply_cls = GNUNET_new (struct ReplyCls);
1499 reply_cls->id = ntohl (msg->id);
1500 reply_cls->client = client;
1501 reply_cls->req_handle = RPS_sampler_get_n_rand_peers (client_sampler,
1506 cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientContext);
1507 GNUNET_assert (NULL != cli_ctx);
1508 GNUNET_CONTAINER_DLL_insert (cli_ctx->rep_cls_head,
1509 cli_ctx->rep_cls_tail,
1511 GNUNET_SERVER_receive_done (client,
1517 * @brief Handle a message that requests the cancellation of a request
1520 * @param client the client that requests the cancellation
1521 * @param message the message containing the id of the request
1524 handle_client_request_cancel (void *cls,
1525 struct GNUNET_SERVER_Client *client,
1526 const struct GNUNET_MessageHeader *message)
1528 struct GNUNET_RPS_CS_RequestCancelMessage *msg =
1529 (struct GNUNET_RPS_CS_RequestCancelMessage *) message;
1530 struct ClientContext *cli_ctx;
1531 struct ReplyCls *rep_cls;
1533 cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientContext);
1534 GNUNET_assert (NULL != cli_ctx->rep_cls_head);
1535 rep_cls = cli_ctx->rep_cls_head;
1536 LOG (GNUNET_ERROR_TYPE_DEBUG,
1537 "Client cancels request with id %lu\n",
1539 while ( (NULL != rep_cls->next) &&
1540 (rep_cls->id != ntohl (msg->id)) )
1541 rep_cls = rep_cls->next;
1542 GNUNET_assert (rep_cls->id == ntohl (msg->id));
1543 RPS_sampler_request_cancel (rep_cls->req_handle);
1544 destroy_reply_cls (rep_cls);
1545 GNUNET_SERVER_receive_done (client,
1551 * Handle seed from the client.
1553 * @param cls closure
1554 * @param client identification of the client
1555 * @param message the actual message
1558 handle_client_seed (void *cls,
1559 struct GNUNET_SERVER_Client *client,
1560 const struct GNUNET_MessageHeader *message)
1562 struct GNUNET_RPS_CS_SeedMessage *in_msg;
1563 struct GNUNET_PeerIdentity *peers;
1567 if (sizeof (struct GNUNET_RPS_CS_SeedMessage) > ntohs (message->size))
1569 GNUNET_break_op (0);
1570 GNUNET_SERVER_receive_done (client,
1574 in_msg = (struct GNUNET_RPS_CS_SeedMessage *) message;
1575 num_peers = ntohl (in_msg->num_peers);
1576 peers = (struct GNUNET_PeerIdentity *) &in_msg[1];
1577 //peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1578 //memcpy (peers, &in_msg[1], num_peers * sizeof (struct GNUNET_PeerIdentity));
1580 if ((ntohs (message->size) - sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
1581 sizeof (struct GNUNET_PeerIdentity) != num_peers)
1583 GNUNET_break_op (0);
1584 GNUNET_SERVER_receive_done (client,
1589 LOG (GNUNET_ERROR_TYPE_DEBUG,
1590 "Client seeded peers:\n");
1591 print_peer_list (peers, num_peers);
1593 for (i = 0 ; i < num_peers ; i++)
1595 LOG (GNUNET_ERROR_TYPE_DEBUG,
1596 "Updating samplers with seed %" PRIu32 ": %s\n",
1598 GNUNET_i2s (&peers[i]));
1600 new_peer_id (&peers[i]);
1602 //RPS_sampler_update (prot_sampler, &peers[i]);
1603 //RPS_sampler_update (client_sampler, &peers[i]);
1606 ////GNUNET_free (peers);
1608 GNUNET_SERVER_receive_done (client,
1614 * Handle a PUSH message from another peer.
1616 * Check the proof of work and store the PeerID
1617 * in the temporary list for pushed PeerIDs.
1619 * @param cls Closure
1620 * @param channel The channel the PUSH was received over
1621 * @param channel_ctx The context associated with this channel
1622 * @param msg The message header
1625 handle_peer_push (void *cls,
1626 struct GNUNET_CADET_Channel *channel,
1628 const struct GNUNET_MessageHeader *msg)
1630 const struct GNUNET_PeerIdentity *peer;
1632 // (check the proof of work)
1634 peer = (const struct GNUNET_PeerIdentity *)
1635 GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
1636 // FIXME wait for cadet to change this function
1638 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PUSH (%s)\n", GNUNET_i2s (peer));
1640 #ifdef ENABLE_MALICIOUS
1641 struct AttackedPeer *tmp_att_peer;
1643 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1644 memcpy (&tmp_att_peer->peer_id, peer, sizeof (struct GNUNET_PeerIdentity));
1647 { /* Try to maximise representation */
1648 if (NULL == att_peer_set)
1649 att_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
1650 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1653 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1656 add_peer_array_to_set (peer, 1, att_peer_set);
1662 else if (2 == mal_type)
1663 { /* We attack one single well-known peer - simply ignore */
1668 GNUNET_free (tmp_att_peer);
1671 #endif /* ENABLE_MALICIOUS */
1673 /* Add the sending peer to the push_map */
1674 CustomPeerMap_put (push_map, peer);
1676 GNUNET_CADET_receive_done (channel);
1682 * Iterator over hash map entries.
1683 * Called from #generate_view_array and writes every peer id into #view_array.
1685 * @param cls closure - the pointer to the counter
1686 * @param key current public key
1687 * @param value value in the hash map
1688 * @return #GNUNET_YES if we should continue to
1690 * #GNUNET_NO if not.
1693 dump_id_to_view_array (void *cls,
1694 const struct GNUNET_PeerIdentity *key,
1697 unsigned int *i = (unsigned int *) cls;
1699 view_array[(*i)++] = *key;
1705 * Makes sure the view_array is filled with the peer ids currently in #view.
1706 * Called from within #do_round before sending pushes and pulls and from
1707 * #handle_peer_pull_request when a reply is sent.
1710 generate_view_array (unsigned int view_size)
1715 if (NULL == view_array)
1717 view_array = GNUNET_new_array (view_size,
1718 struct GNUNET_PeerIdentity);
1719 i = GNUNET_new (unsigned int);
1722 ret = GNUNET_CONTAINER_multipeermap_iterate (view,
1723 dump_id_to_view_array,
1725 GNUNET_assert (view_size == ret);
1726 GNUNET_assert (view_size == *i);
1734 * Handle PULL REQUEST request message from another peer.
1736 * Reply with the view of PeerIDs.
1738 * @param cls Closure
1739 * @param channel The channel the PULL REQUEST was received over
1740 * @param channel_ctx The context associated with this channel
1741 * @param msg The message header
1744 handle_peer_pull_request (void *cls,
1745 struct GNUNET_CADET_Channel *channel,
1747 const struct GNUNET_MessageHeader *msg)
1749 struct GNUNET_PeerIdentity *peer;
1750 unsigned int view_size;
1752 peer = (struct GNUNET_PeerIdentity *)
1753 GNUNET_CADET_channel_get_info (channel,
1754 GNUNET_CADET_OPTION_PEER);
1755 // FIXME wait for cadet to change this function
1757 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REQUEST (%s)\n", GNUNET_i2s (peer));
1759 #ifdef ENABLE_MALICIOUS
1762 { /* Try to maximise representation */
1763 send_pull_reply (peer, mal_peers, num_mal_peers);
1767 else if (2 == mal_type)
1768 { /* Try to partition network */
1769 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
1771 send_pull_reply (peer, mal_peers, num_mal_peers);
1775 #endif /* ENABLE_MALICIOUS */
1777 view_size = GNUNET_CONTAINER_multipeermap_size (view);
1778 generate_view_array (view_size);
1780 send_pull_reply (peer, view_array, view_size);
1782 GNUNET_CADET_receive_done (channel);
1788 * Handle PULL REPLY message from another peer.
1790 * Check whether we sent a corresponding request and
1791 * whether this reply is the first one.
1793 * @param cls Closure
1794 * @param channel The channel the PUSH was received over
1795 * @param channel_ctx The context associated with this channel
1796 * @param msg The message header
1799 handle_peer_pull_reply (void *cls,
1800 struct GNUNET_CADET_Channel *channel,
1802 const struct GNUNET_MessageHeader *msg)
1804 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
1805 struct GNUNET_PeerIdentity *peers;
1806 struct PeerContext *peer_ctx;
1807 struct GNUNET_PeerIdentity *sender;
1808 struct PeerContext *sender_ctx;
1809 struct PeerOutstandingOp out_op;
1811 #ifdef ENABLE_MALICIOUS
1812 struct AttackedPeer *tmp_att_peer;
1813 #endif /* ENABLE_MALICIOUS */
1815 /* Check for protocol violation */
1816 if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->size))
1818 GNUNET_break_op (0);
1819 GNUNET_CADET_receive_done (channel);
1820 return GNUNET_SYSERR;
1823 in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
1824 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1825 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1827 LOG (GNUNET_ERROR_TYPE_ERROR,
1828 "message says it sends %" PRIu32 " peers, have space for %i peers\n",
1829 ntohl (in_msg->num_peers),
1830 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1831 sizeof (struct GNUNET_PeerIdentity));
1832 GNUNET_break_op (0);
1833 GNUNET_CADET_receive_done (channel);
1834 return GNUNET_SYSERR;
1837 // Guess simply casting isn't the nicest way...
1838 // FIXME wait for cadet to change this function
1839 sender = (struct GNUNET_PeerIdentity *)
1840 GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
1841 sender_ctx = get_peer_ctx (sender);
1843 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REPLY (%s)\n", GNUNET_i2s (sender));
1845 if (GNUNET_YES != get_peer_flag (sender_ctx, PULL_REPLY_PENDING))
1847 LOG (GNUNET_ERROR_TYPE_WARNING,
1848 "Received a pull reply from a peer we didn't request one from!\n");
1849 GNUNET_break_op (0);
1850 GNUNET_CADET_receive_done (channel);
1855 #ifdef ENABLE_MALICIOUS
1856 // We shouldn't even receive pull replies as we're not sending
1859 #endif /* ENABLE_MALICIOUS */
1861 /* Do actual logic */
1862 peers = (struct GNUNET_PeerIdentity *) &in_msg[1];
1864 LOG (GNUNET_ERROR_TYPE_DEBUG,
1865 "PULL REPLY received, got following %u peers:\n",
1866 ntohl (in_msg->num_peers));
1868 for (i = 0 ; i < ntohl (in_msg->num_peers) ; i++)
1870 LOG (GNUNET_ERROR_TYPE_DEBUG,
1873 GNUNET_i2s (&peers[i]));
1875 #ifdef ENABLE_MALICIOUS
1876 if ((NULL != att_peer_set) &&
1877 (1 == mal_type || 3 == mal_type))
1878 { /* Add attacked peer to local list */
1879 // TODO check if we sent a request and this was the first reply
1880 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1882 && GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mal_peer_set,
1884 && 0 != GNUNET_CRYPTO_cmp_peer_identity (&peers[i],
1887 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1888 tmp_att_peer->peer_id = peers[i];
1889 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1892 add_peer_array_to_set (&peers[i], 1, att_peer_set);
1896 #endif /* ENABLE_MALICIOUS */
1897 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity,
1900 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map,
1902 peer_ctx = create_peer_ctx (&peers[i]);
1904 peer_ctx = get_peer_ctx (&peers[i]);
1906 if (GNUNET_YES == get_peer_flag (peer_ctx, VALID))
1908 CustomPeerMap_put (pull_map, &peers[i]);
1910 else if (GNUNET_NO == insert_in_pull_map_scheduled (peer_ctx))
1912 out_op.op = insert_in_pull_map;
1913 out_op.op_cls = NULL;
1914 GNUNET_array_append (peer_ctx->outstanding_ops,
1915 peer_ctx->num_outstanding_ops,
1917 check_peer_live (peer_ctx);
1922 unset_peer_flag (sender_ctx, PULL_REPLY_PENDING);
1924 peer_clean (sender);
1926 GNUNET_CADET_receive_done (channel);
1932 * Compute a random delay.
1933 * A uniformly distributed value between mean + spread and mean - spread.
1935 * For example for mean 4 min and spread 2 the minimum is (4 min - (1/2 * 4 min))
1936 * It would return a random value between 2 and 6 min.
1938 * @param mean the mean
1939 * @param spread the inverse amount of deviation from the mean
1941 static struct GNUNET_TIME_Relative
1942 compute_rand_delay (struct GNUNET_TIME_Relative mean, unsigned int spread)
1944 struct GNUNET_TIME_Relative half_interval;
1945 struct GNUNET_TIME_Relative ret;
1946 unsigned int rand_delay;
1947 unsigned int max_rand_delay;
1951 LOG (GNUNET_ERROR_TYPE_WARNING,
1952 "Not accepting spread of 0\n");
1956 /* Compute random time value between spread * mean and spread * mean */
1957 half_interval = GNUNET_TIME_relative_divide (mean, spread);
1959 max_rand_delay = GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us / mean.rel_value_us * (2/spread);
1961 * Compute random value between (0 and 1) * round_interval
1962 * via multiplying round_interval with a 'fraction' (0 to value)/value
1964 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_rand_delay);
1965 ret = GNUNET_TIME_relative_multiply (mean, rand_delay);
1966 ret = GNUNET_TIME_relative_divide (ret, max_rand_delay);
1967 ret = GNUNET_TIME_relative_add (ret, half_interval);
1969 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ret.rel_value_us)
1970 LOG (GNUNET_ERROR_TYPE_WARNING,
1971 "Returning FOREVER_REL\n");
1978 * Send single pull request
1980 * @param peer_id the peer to send the pull request to.
1983 send_pull_request (const struct GNUNET_PeerIdentity *peer_id)
1985 struct GNUNET_MQ_Envelope *ev;
1986 struct GNUNET_MQ_Handle *mq;
1987 struct PeerContext *peer_ctx;
1988 struct PendingMessage *pending_msg;
1990 peer_ctx = get_peer_ctx (peer_id);
1991 GNUNET_assert (GNUNET_NO == get_peer_flag (peer_ctx, PULL_REPLY_PENDING));
1992 set_peer_flag (peer_ctx, PULL_REPLY_PENDING);
1994 LOG (GNUNET_ERROR_TYPE_DEBUG,
1995 "Going to send PULL REQUEST to peer %s.\n",
1996 GNUNET_i2s (peer_id));
1998 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
1999 mq = get_mq (peer_id);
2000 pending_msg = insert_pending_message (peer_id, ev, "PULL REQUEST");
2001 GNUNET_MQ_notify_sent (ev,
2004 GNUNET_MQ_send (mq, ev);
2011 * @param peer_id the peer to send the push to.
2014 send_push (const struct GNUNET_PeerIdentity *peer_id)
2016 struct GNUNET_MQ_Envelope *ev;
2017 struct GNUNET_MQ_Handle *mq;
2018 struct PendingMessage *pending_msg;
2020 LOG (GNUNET_ERROR_TYPE_DEBUG,
2021 "Going to send PUSH to peer %s.\n",
2022 GNUNET_i2s (peer_id));
2024 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
2025 mq = get_mq (peer_id);
2026 pending_msg = insert_pending_message (peer_id, ev, "PUSH");
2027 GNUNET_MQ_notify_sent (ev,
2030 GNUNET_MQ_send (mq, ev);
2035 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2038 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2041 #ifdef ENABLE_MALICIOUS
2043 * Turn RPS service to act malicious.
2045 * @param cls Closure
2046 * @param client The client that sent the message
2047 * @param msg The message header
2050 handle_client_act_malicious (void *cls,
2051 struct GNUNET_SERVER_Client *client,
2052 const struct GNUNET_MessageHeader *msg)
2054 struct GNUNET_RPS_CS_ActMaliciousMessage *in_msg;
2055 struct GNUNET_PeerIdentity *peers;
2056 uint32_t num_mal_peers_sent;
2057 uint32_t num_mal_peers_old;
2059 /* Check for protocol violation */
2060 if (sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage) > ntohs (msg->size))
2062 GNUNET_break_op (0);
2065 in_msg = (struct GNUNET_RPS_CS_ActMaliciousMessage *) msg;
2066 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
2067 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
2069 LOG (GNUNET_ERROR_TYPE_ERROR,
2070 "message says it sends %" PRIu64 " peers, have space for %i peers\n",
2071 ntohl (in_msg->num_peers),
2072 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
2073 sizeof (struct GNUNET_PeerIdentity));
2074 GNUNET_break_op (0);
2078 /* Do actual logic */
2079 peers = (struct GNUNET_PeerIdentity *) &msg[1];
2080 mal_type = ntohl (in_msg->type);
2081 if (NULL == mal_peer_set)
2082 mal_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
2084 LOG (GNUNET_ERROR_TYPE_DEBUG,
2085 "Now acting malicious type %" PRIu32 ", got %" PRIu32 " peers.\n",
2087 ntohl (in_msg->num_peers));
2090 { /* Try to maximise representation */
2091 /* Add other malicious peers to those we already know */
2093 num_mal_peers_sent = ntohl (in_msg->num_peers);
2094 num_mal_peers_old = num_mal_peers;
2095 GNUNET_array_grow (mal_peers,
2097 num_mal_peers + num_mal_peers_sent);
2098 memcpy (&mal_peers[num_mal_peers_old],
2100 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
2102 /* Add all mal peers to mal_peer_set */
2103 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
2107 /* Substitute do_round () with do_mal_round () */
2108 GNUNET_SCHEDULER_cancel (do_round_task);
2109 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
2112 else if (2 == mal_type
2114 { /* Try to partition the network */
2115 /* Add other malicious peers to those we already know */
2116 struct PeerContext *att_ctx;
2118 num_mal_peers_sent = ntohl (in_msg->num_peers) - 1;
2119 num_mal_peers_old = num_mal_peers;
2120 GNUNET_array_grow (mal_peers,
2122 num_mal_peers + num_mal_peers_sent);
2123 if (NULL != mal_peers &&
2126 memcpy (&mal_peers[num_mal_peers_old],
2128 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
2130 /* Add all mal peers to mal_peer_set */
2131 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
2136 /* Store the one attacked peer */
2137 memcpy (&attacked_peer,
2138 &in_msg->attacked_peer,
2139 sizeof (struct GNUNET_PeerIdentity));
2140 /* Set the flag of the attacked peer to valid to avoid problems */
2141 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map,
2144 att_ctx = create_peer_ctx (&attacked_peer);
2145 check_peer_live (att_ctx);
2148 LOG (GNUNET_ERROR_TYPE_DEBUG,
2149 "Attacked peer is %s\n",
2150 GNUNET_i2s (&attacked_peer));
2152 /* Substitute do_round () with do_mal_round () */
2153 GNUNET_SCHEDULER_cancel (do_round_task);
2154 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
2156 else if (0 == mal_type)
2157 { /* Stop acting malicious */
2158 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
2160 /* Substitute do_mal_round () with do_round () */
2161 GNUNET_SCHEDULER_cancel (do_round_task);
2162 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
2169 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2174 * Send out PUSHes and PULLs maliciously.
2176 * This is executed regylary.
2179 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2181 uint32_t num_pushes;
2183 struct GNUNET_TIME_Relative time_next_round;
2184 struct AttackedPeer *tmp_att_peer;
2185 struct PeerContext *peer_ctx;
2187 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round maliciously type %" PRIu32 ".\n",
2189 do_round_task = NULL;
2190 GNUNET_assert (mal_type <= 3);
2191 /* Do malicious actions */
2193 { /* Try to maximise representation */
2195 /* The maximum of pushes we're going to send this round */
2196 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit,
2197 num_attacked_peers),
2198 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
2200 LOG (GNUNET_ERROR_TYPE_DEBUG,
2201 "Going to send %" PRIu32 " pushes\n",
2204 /* Send PUSHes to attacked peers */
2205 for (i = 0 ; i < num_pushes ; i++)
2207 if (att_peers_tail == att_peer_index)
2208 att_peer_index = att_peers_head;
2210 att_peer_index = att_peer_index->next;
2212 send_push (&att_peer_index->peer_id);
2215 /* Send PULLs to some peers to learn about additional peers to attack */
2216 tmp_att_peer = att_peer_index;
2217 for (i = 0 ; i < num_pushes * alpha ; i++)
2219 if (att_peers_tail == tmp_att_peer)
2220 tmp_att_peer = att_peers_head;
2222 att_peer_index = tmp_att_peer->next;
2224 send_pull_request (&tmp_att_peer->peer_id);
2229 else if (2 == mal_type)
2231 * Try to partition the network
2232 * Send as many pushes to the attacked peer as possible
2233 * That is one push per round as it will ignore more.
2235 peer_ctx = get_peer_ctx (&attacked_peer);
2236 if (GNUNET_YES == get_peer_flag (peer_ctx, VALID))
2237 send_push (&attacked_peer);
2242 { /* Combined attack */
2244 /* Send PUSH to attacked peers */
2245 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map,
2248 peer_ctx = get_peer_ctx (&attacked_peer);
2249 if (GNUNET_YES == get_peer_flag (peer_ctx, VALID))
2251 LOG (GNUNET_ERROR_TYPE_DEBUG,
2252 "Goding to send push to attacked peer (%s)\n",
2253 GNUNET_i2s (&attacked_peer));
2254 send_push (&attacked_peer);
2257 check_peer_live (peer_ctx);
2260 peer_ctx = create_peer_ctx (&attacked_peer);
2261 check_peer_live (peer_ctx);
2263 /* The maximum of pushes we're going to send this round */
2264 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit - 1,
2265 num_attacked_peers),
2266 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
2268 LOG (GNUNET_ERROR_TYPE_DEBUG,
2269 "Going to send %" PRIu32 " pushes\n",
2272 for (i = 0 ; i < num_pushes ; i++)
2274 if (att_peers_tail == att_peer_index)
2275 att_peer_index = att_peers_head;
2277 att_peer_index = att_peer_index->next;
2279 send_push (&att_peer_index->peer_id);
2282 /* Send PULLs to some peers to learn about additional peers to attack */
2283 tmp_att_peer = att_peer_index;
2284 for (i = 0 ; i < num_pushes * alpha ; i++)
2286 if (att_peers_tail == tmp_att_peer)
2287 tmp_att_peer = att_peers_head;
2289 att_peer_index = tmp_att_peer->next;
2291 send_pull_request (&tmp_att_peer->peer_id);
2295 /* Schedule next round */
2296 time_next_round = compute_rand_delay (round_interval, 2);
2298 //do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_mal_round,
2300 GNUNET_assert (NULL == do_round_task);
2301 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_mal_round, NULL);
2302 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
2304 #endif /* ENABLE_MALICIOUS */
2308 * Send out PUSHes and PULLs, possibly update #view, samplers.
2310 * This is executed regylary.
2313 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2315 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round.\n");
2318 unsigned int view_size;
2319 unsigned int *permut;
2320 unsigned int a_peers; /* Number of peers we send pushes to */
2321 unsigned int b_peers; /* Number of peers we send pull requests to */
2322 uint32_t first_border;
2323 uint32_t second_border;
2324 struct GNUNET_PeerIdentity peer;
2325 struct PeerContext *peer_ctx;
2326 struct GNUNET_PeerIdentity *update_peer;
2328 do_round_task = NULL;
2329 LOG (GNUNET_ERROR_TYPE_DEBUG,
2330 "Printing view:\n");
2331 to_file (file_name_view_log,
2332 "___ new round ___");
2333 view_size = GNUNET_CONTAINER_multipeermap_size (view);
2334 generate_view_array (view_size);
2335 for (i = 0 ; i < view_size ; i++)
2337 LOG (GNUNET_ERROR_TYPE_DEBUG,
2338 "\t%s\n", GNUNET_i2s (&view_array[i]));
2339 to_file (file_name_view_log,
2341 GNUNET_i2s_full (&view_array[i]));
2345 /* Send pushes and pull requests */
2348 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2349 (unsigned int) view_size);
2352 a_peers = ceil (alpha * view_size);
2354 LOG (GNUNET_ERROR_TYPE_DEBUG,
2355 "Going to send pushes to %u (ceil (%f * %u)) peers.\n",
2356 a_peers, alpha, view_size);
2357 for (i = 0; i < a_peers; i++)
2359 peer = view_array[permut[i]];
2360 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer)) // TODO
2361 { // FIXME if this fails schedule/loop this for later
2366 /* Send PULL requests */
2367 b_peers = ceil (beta * view_size);
2368 first_border = a_peers;
2369 second_border = a_peers + b_peers;
2370 if (second_border > view_size)
2372 first_border = view_size - b_peers;
2373 second_border = view_size;
2375 LOG (GNUNET_ERROR_TYPE_DEBUG,
2376 "Going to send pulls to %u (ceil (%f * %u)) peers.\n",
2377 b_peers, beta, view_size);
2378 for (i = first_border; i < second_border; i++)
2380 peer = view_array[permut[i]];
2381 peer_ctx = get_peer_ctx (&peer);
2382 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer) &&
2383 GNUNET_NO == get_peer_flag (peer_ctx, PULL_REPLY_PENDING)) // TODO
2384 { // FIXME if this fails schedule/loop this for later
2385 send_pull_request (&peer);
2389 GNUNET_free (permut);
2395 /* TODO see how many peers are in push-/pull- list! */
2397 if ((CustomPeerMap_size (push_map) <= alpha * view_size) &&
2398 (0 < CustomPeerMap_size (push_map)) &&
2399 (0 < CustomPeerMap_size (pull_map)))
2400 { /* If conditions for update are fulfilled, update */
2401 LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the view.\n");
2403 uint32_t final_size;
2404 uint32_t peers_to_clean_size;
2405 struct GNUNET_PeerIdentity *peers_to_clean;
2407 peers_to_clean = NULL;
2408 peers_to_clean_size = 0;
2409 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, view_size);
2410 memcpy (peers_to_clean,
2412 view_size * sizeof (struct GNUNET_PeerIdentity));
2414 /* Seems like recreating is the easiest way of emptying the peermap */
2415 GNUNET_CONTAINER_multipeermap_destroy (view);
2416 view = GNUNET_CONTAINER_multipeermap_create (view_size, GNUNET_NO);
2417 to_file (file_name_view_log,
2420 first_border = GNUNET_MIN (ceil (alpha * sampler_size_est_need),
2421 CustomPeerMap_size (push_map));
2422 second_border = first_border +
2423 GNUNET_MIN (floor (beta * sampler_size_est_need),
2424 CustomPeerMap_size (pull_map));
2425 final_size = second_border +
2426 ceil ((1 - (alpha + beta)) * sampler_size_est_need);
2428 GNUNET_array_grow (view_array, view_size, second_border);
2430 /* Update view with peers received through PUSHes */
2431 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2432 CustomPeerMap_size (push_map));
2433 for (i = 0; i < first_border; i++)
2435 view_array[i] = *CustomPeerMap_get_peer_by_index (push_map, permut[i]);
2436 GNUNET_CONTAINER_multipeermap_put (view, &view_array[i], NULL,
2437 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2439 to_file (file_name_view_log,
2441 GNUNET_i2s_full (&view_array[i]));
2442 // TODO change the peer_flags accordingly
2444 GNUNET_free (permut);
2447 /* Update view with peers received through PULLs */
2448 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2449 CustomPeerMap_size (pull_map));
2450 for (i = first_border; i < second_border; i++)
2453 *CustomPeerMap_get_peer_by_index (pull_map, permut[i - first_border]);
2454 GNUNET_CONTAINER_multipeermap_put (view, &view_array[i], NULL,
2455 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2457 to_file (file_name_view_log,
2459 GNUNET_i2s_full (&view_array[i]));
2460 // TODO change the peer_flags accordingly
2462 GNUNET_free (permut);
2465 /* Update view with peers from history */
2466 RPS_sampler_get_n_rand_peers (prot_sampler,
2469 final_size - second_border);
2470 num_hist_update_tasks = final_size - second_border;
2471 // TODO change the peer_flags accordingly
2473 for (i = 0; i < view_size; i++)
2474 rem_from_list (&peers_to_clean, &peers_to_clean_size, &view_array[i]);
2476 /* Clean peers that were removed from the view */
2477 for (i = 0; i < peers_to_clean_size; i++)
2479 to_file (file_name_view_log,
2481 GNUNET_i2s_full (&peers_to_clean[i]));
2482 peer_clean (&peers_to_clean[i]);
2485 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, 0);
2486 peers_to_clean = NULL;
2490 LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the view.\n");
2492 // TODO independent of that also get some peers from CADET_get_peers()?
2494 LOG (GNUNET_ERROR_TYPE_DEBUG,
2495 "Received %u pushes and %u pulls last round (alpha (%.2f) * view_size (%u) = %.2f)\n",
2496 CustomPeerMap_size (push_map),
2497 CustomPeerMap_size (pull_map),
2502 /* Update samplers */
2503 for (i = 0; i < CustomPeerMap_size (push_map); i++)
2505 update_peer = CustomPeerMap_get_peer_by_index (push_map, i);
2506 LOG (GNUNET_ERROR_TYPE_DEBUG,
2507 "Updating with peer %s from push list\n",
2508 GNUNET_i2s (update_peer));
2509 insert_in_sampler (NULL, update_peer);
2510 peer_clean (update_peer); /* This cleans only if it is not in the view */
2513 for (i = 0; i < CustomPeerMap_size (pull_map); i++)
2515 LOG (GNUNET_ERROR_TYPE_DEBUG,
2516 "Updating with peer %s from pull list\n",
2517 GNUNET_i2s (CustomPeerMap_get_peer_by_index (pull_map, i)));
2518 insert_in_sampler (NULL, CustomPeerMap_get_peer_by_index (pull_map, i));
2519 /* This cleans only if it is not in the view */
2520 peer_clean (CustomPeerMap_get_peer_by_index (pull_map, i));
2524 /* Empty push/pull lists */
2525 CustomPeerMap_clear (push_map);
2526 CustomPeerMap_clear (pull_map);
2528 struct GNUNET_TIME_Relative time_next_round;
2530 time_next_round = compute_rand_delay (round_interval, 2);
2532 /* Schedule next round */
2533 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_round, NULL);
2534 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
2539 rps_start (struct GNUNET_SERVER_Handle *server);
2543 * This is called from GNUNET_CADET_get_peers().
2545 * It is called on every peer(ID) that cadet somehow has contact with.
2546 * We use those to initialise the sampler.
2549 init_peer_cb (void *cls,
2550 const struct GNUNET_PeerIdentity *peer,
2551 int tunnel, // "Do we have a tunnel towards this peer?"
2552 unsigned int n_paths, // "Number of known paths towards this peer"
2553 unsigned int best_path) // "How long is the best path?
2554 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
2558 LOG (GNUNET_ERROR_TYPE_DEBUG,
2559 "Got peer_id %s from cadet\n",
2567 * Iterator over peers from peerinfo.
2569 * @param cls closure
2570 * @param peer id of the peer, NULL for last call
2571 * @param hello hello message for the peer (can be NULL)
2572 * @param error message
2575 process_peerinfo_peers (void *cls,
2576 const struct GNUNET_PeerIdentity *peer,
2577 const struct GNUNET_HELLO_Message *hello,
2578 const char *err_msg)
2582 LOG (GNUNET_ERROR_TYPE_DEBUG,
2583 "Got peer_id %s from peerinfo\n",
2591 * Callback used to remove peers from the multipeermap.
2594 peer_remove_cb (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
2596 struct PeerContext *peer_ctx;
2597 const struct GNUNET_CADET_Channel *channel =
2598 (const struct GNUNET_CADET_Channel *) cls;
2600 peer_ctx = (struct PeerContext *) value;
2601 set_peer_flag (peer_ctx, TO_DESTROY);
2603 LOG (GNUNET_ERROR_TYPE_DEBUG,
2604 "Going to remove peer %s\n",
2605 GNUNET_i2s (&peer_ctx->peer_id));
2607 /* Remove it from the sampler used for the Brahms protocol */
2608 RPS_sampler_reinitialise_by_value (prot_sampler, key);
2610 /* If operations are still scheduled for this peer cancel those */
2611 if (0 != peer_ctx->num_outstanding_ops)
2613 GNUNET_array_grow (peer_ctx->outstanding_ops,
2614 peer_ctx->num_outstanding_ops,
2618 /* If we are still waiting for notification whether this peer is live
2619 * cancel the according task */
2620 if (NULL != peer_ctx->transmit_handle)
2622 LOG (GNUNET_ERROR_TYPE_DEBUG,
2623 "Trying to cancle transmit_handle for peer %s\n",
2625 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->transmit_handle);
2626 peer_ctx->transmit_handle = NULL;
2629 unset_peer_flag (peer_ctx, PULL_REPLY_PENDING);
2631 /* Remove peer from view */
2632 if (GNUNET_CONTAINER_multipeermap_contains (view, key))
2634 to_file (file_name_view_log,
2635 "-%s\t(cleanup channel, other peer)",
2636 GNUNET_i2s_full (key));
2637 GNUNET_CONTAINER_multipeermap_remove_all (view, key);
2638 if (NULL != view_array)
2640 GNUNET_free (view_array);
2645 /* Remove from push and pull lists */
2646 CustomPeerMap_remove_peer (push_map, key);
2647 CustomPeerMap_remove_peer (pull_map, key);
2649 /* Cancle messages that have not been sent yet */
2650 while (NULL != peer_ctx->pending_messages_head)
2652 LOG (GNUNET_ERROR_TYPE_DEBUG,
2653 "Removing unsent %s\n",
2654 peer_ctx->pending_messages_head->type);
2655 /* We are not able to cancel messages as #GNUNET_CADET_mq_create () does not
2656 * set a #GNUNET_MQ_CancelImpl */
2657 /* GNUNET_MQ_send_cancel (peer_ctx->pending_messages_head->ev); */
2658 remove_pending_message (peer_ctx->pending_messages_head);
2661 /* If there is still a mq destroy it */
2662 if (NULL != peer_ctx->mq)
2664 GNUNET_MQ_destroy (peer_ctx->mq);
2665 peer_ctx->mq = NULL;
2669 /* Remove the send_channel
2670 * This function should be called again from #cleanup_channel (callback
2671 * called on the destruction of channels) and clean up the rest. */
2672 if (NULL != peer_ctx->send_channel &&
2673 channel != peer_ctx->send_channel)
2675 GNUNET_CADET_channel_destroy (peer_ctx->send_channel);
2676 peer_ctx->send_channel = NULL;
2679 /* Remove the recv_channel
2680 * This function should be called again from #cleanup_channel (callback
2681 * called on the destruction of channels) and clean up the rest. */
2682 if (NULL != peer_ctx->recv_channel &&
2683 channel != peer_ctx->recv_channel)
2685 GNUNET_CADET_channel_destroy (peer_ctx->recv_channel);
2686 peer_ctx->recv_channel = NULL;
2689 /* If there is no channel we have to remove the context now */
2690 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_remove_all (peer_map, key))
2691 LOG (GNUNET_ERROR_TYPE_WARNING, "removing peer from peer_map failed\n");
2693 GNUNET_free (peer_ctx);
2700 * Clean the send channel of a peer
2701 * If there is also no channel to receive messages from that peer, remove it
2705 peer_clean (const struct GNUNET_PeerIdentity *peer)
2707 struct PeerContext *peer_ctx;
2708 /* struct GNUNET_CADET_Channel *channel; */
2710 if ( (0 == RPS_sampler_count_id (prot_sampler, peer)) &&
2711 (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (view, peer)) &&
2712 (GNUNET_NO == CustomPeerMap_contains_peer (push_map, peer)) &&
2713 (GNUNET_NO == CustomPeerMap_contains_peer (pull_map, peer)) &&
2714 (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer)) )
2716 peer_ctx = get_peer_ctx (peer);
2718 if ( (NULL == peer_ctx->recv_channel) &&
2719 (NULL == peer_ctx->pending_messages_head) &&
2720 (GNUNET_NO == get_peer_flag (peer_ctx, PULL_REPLY_PENDING)) )
2722 #ifdef ENABLE_MALICIOUS
2723 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
2724 peer_remove_cb (NULL, peer, peer_ctx);
2725 #else /* ENABLE_MALICIOUS */
2726 peer_remove_cb (NULL, peer, peer_ctx);
2727 #endif /* ENABLE_MALICIOUS */
2734 * Task run during shutdown.
2740 shutdown_task (void *cls,
2741 const struct GNUNET_SCHEDULER_TaskContext *tc)
2744 LOG (GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
2746 GNUNET_PEERINFO_notify_cancel (peerinfo_notify_handle);
2747 GNUNET_PEERINFO_disconnect (peerinfo_handle);
2749 if (NULL != do_round_task)
2751 GNUNET_SCHEDULER_cancel (do_round_task);
2752 do_round_task = NULL;
2756 if (GNUNET_SYSERR ==
2757 GNUNET_CONTAINER_multipeermap_iterate (peer_map, peer_remove_cb, NULL))
2758 LOG (GNUNET_ERROR_TYPE_WARNING,
2759 "Iterating over peers to disconnect from them was cancelled\n");
2762 GNUNET_NSE_disconnect (nse);
2763 RPS_sampler_destroy (prot_sampler);
2764 RPS_sampler_destroy (client_sampler);
2765 LOG (GNUNET_ERROR_TYPE_DEBUG,
2766 "Size of the peermap: %u\n",
2767 GNUNET_CONTAINER_multipeermap_size (peer_map));
2768 GNUNET_break (0 == GNUNET_CONTAINER_multipeermap_size (peer_map));
2769 GNUNET_CADET_disconnect (cadet_handle);
2770 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
2771 GNUNET_CONTAINER_multipeermap_destroy (view);
2773 CustomPeerMap_destroy (push_map);
2774 CustomPeerMap_destroy (pull_map);
2775 #ifdef ENABLE_MALICIOUS
2776 struct AttackedPeer *tmp_att_peer;
2777 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
2778 if (NULL != mal_peer_set)
2779 GNUNET_CONTAINER_multipeermap_destroy (mal_peer_set);
2780 if (NULL != att_peer_set)
2781 GNUNET_CONTAINER_multipeermap_destroy (att_peer_set);
2782 while (NULL != att_peers_head)
2784 tmp_att_peer = att_peers_head;
2785 GNUNET_CONTAINER_DLL_remove (att_peers_head, att_peers_tail, tmp_att_peer);
2787 #endif /* ENABLE_MALICIOUS */
2792 * @brief Get informed about a connecting client.
2795 * @param client the client that connects
2798 handle_client_connect (void *cls,
2799 struct GNUNET_SERVER_Client *client)
2801 struct ClientContext *cli_ctx;
2803 LOG (GNUNET_ERROR_TYPE_DEBUG,
2804 "Client connected\n");
2806 return; /* Server was destroyed before a client connected. Shutting down */
2807 cli_ctx = GNUNET_new (struct ClientContext);
2808 cli_ctx->mq = GNUNET_MQ_queue_for_server_client (client);
2809 GNUNET_SERVER_client_set_user_context (client, cli_ctx);
2810 GNUNET_CONTAINER_DLL_insert (cli_ctx_head,
2816 * A client disconnected. Remove all of its data structure entries.
2818 * @param cls closure, NULL
2819 * @param client identification of the client
2822 handle_client_disconnect (void *cls,
2823 struct GNUNET_SERVER_Client *client)
2825 struct ClientContext *cli_ctx;
2828 {/* shutdown task */
2829 while (NULL != cli_ctx_head)
2830 destroy_cli_ctx (cli_ctx_head);
2834 cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientContext);
2835 destroy_cli_ctx (cli_ctx);
2841 * Handle the channel a peer opens to us.
2843 * @param cls The closure
2844 * @param channel The channel the peer wants to establish
2845 * @param initiator The peer's peer ID
2846 * @param port The port the channel is being established over
2847 * @param options Further options
2850 handle_inbound_channel (void *cls,
2851 struct GNUNET_CADET_Channel *channel,
2852 const struct GNUNET_PeerIdentity *initiator,
2854 enum GNUNET_CADET_ChannelOption options)
2856 struct PeerContext *peer_ctx;
2858 LOG (GNUNET_ERROR_TYPE_DEBUG,
2859 "New channel was established to us (Peer %s).\n",
2860 GNUNET_i2s (initiator));
2861 GNUNET_assert (NULL != channel); /* according to cadet API */
2862 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, initiator))
2863 peer_ctx = create_peer_ctx (initiator);
2865 peer_ctx = get_peer_ctx (initiator);
2866 /* We only accept one incoming channel from peers */
2867 if (NULL != peer_ctx->recv_channel)
2869 GNUNET_CADET_channel_destroy (channel);
2872 peer_ctx->recv_channel = channel;
2873 peer_is_live (peer_ctx);
2879 * This is called when a channel is destroyed.
2881 * @param cls The closure
2882 * @param channel The channel being closed
2883 * @param channel_ctx The context associated with this channel
2886 cleanup_channel (void *cls,
2887 const struct GNUNET_CADET_Channel *channel,
2890 struct GNUNET_PeerIdentity *peer;
2891 struct PeerContext *peer_ctx;
2893 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
2894 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
2895 // Guess simply casting isn't the nicest way...
2896 // FIXME wait for cadet to change this function
2898 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
2899 {/* We don't want to implicitly create a context that we're about to kill */
2900 peer_ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
2901 if (NULL == peer_ctx) /* It could have been removed by shutdown_task */
2904 if (get_peer_flag (peer_ctx, TO_DESTROY))
2905 {/* We initiatad the destruction of this particular peer */
2906 if (channel == peer_ctx->send_channel)
2907 peer_ctx->send_channel = NULL;
2908 else if (channel == peer_ctx->recv_channel)
2909 peer_ctx->recv_channel = NULL;
2911 to_file (file_name_view_log,
2912 "-%s\t(cleanup channel, ourself)",
2913 GNUNET_i2s_full (peer));
2917 { /* We did not initiate the destruction of this peer */
2918 if (channel == peer_ctx->send_channel)
2919 { /* Something (but us) killd the channel - clean up peer */
2920 LOG (GNUNET_ERROR_TYPE_DEBUG,
2921 "send channel (%s) was destroyed - cleaning up\n",
2923 peer_ctx->send_channel = NULL;
2924 /* Somwewhat {ab,re}use the iterator function */
2925 /* Cast to void is ok, because it's used as void in peer_remove_cb */
2926 (void) peer_remove_cb ((void *) channel, peer, peer_ctx);
2928 else if (channel == peer_ctx->recv_channel)
2929 { /* Other peer doesn't want to send us messages anymore */
2930 LOG (GNUNET_ERROR_TYPE_DEBUG,
2931 "Peer %s destroyed recv channel - cleaning up channel\n",
2933 peer_ctx->recv_channel = NULL;
2937 LOG (GNUNET_ERROR_TYPE_WARNING,
2938 "unknown channel (%s) was destroyed\n",
2945 { /* We don't know a context to that peer */
2946 LOG (GNUNET_ERROR_TYPE_DEBUG,
2947 "channel (%s) without associated context was destroyed\n",
2954 * Actually start the service.
2957 rps_start (struct GNUNET_SERVER_Handle *server)
2959 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
2960 {&handle_client_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
2961 sizeof (struct GNUNET_RPS_CS_RequestMessage)},
2962 {&handle_client_request_cancel, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST_CANCEL,
2963 sizeof (struct GNUNET_RPS_CS_RequestCancelMessage)},
2964 {&handle_client_seed, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_SEED, 0},
2965 #ifdef ENABLE_MALICIOUS
2966 {&handle_client_act_malicious, NULL, GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS , 0},
2967 #endif /* ENABLE_MALICIOUS */
2971 GNUNET_SERVER_add_handlers (server, handlers);
2972 GNUNET_SERVER_connect_notify (server,
2973 &handle_client_connect,
2975 GNUNET_SERVER_disconnect_notify (server,
2976 &handle_client_disconnect,
2978 LOG (GNUNET_ERROR_TYPE_INFO, "Ready to receive requests from clients\n");
2981 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
2982 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
2984 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2991 * Process statistics requests.
2993 * @param cls closure
2994 * @param server the initialized server
2995 * @param c configuration to use
2999 struct GNUNET_SERVER_Handle *server,
3000 const struct GNUNET_CONFIGURATION_Handle *c)
3005 // TODO check what this does -- copied from gnunet-boss
3006 // - seems to work as expected
3007 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
3012 GNUNET_CRYPTO_get_peer_identity (cfg, &own_identity); // TODO check return value
3013 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3014 "STARTING SERVICE (rps) for peer [%s]\n",
3015 GNUNET_i2s (&own_identity));
3016 #ifdef ENABLE_MALICIOUS
3017 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3018 "Malicious execution compiled in.\n");
3019 #endif /* ENABLE_MALICIOUS */
3023 /* Get time interval from the configuration */
3024 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
3028 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
3029 "RPS", "ROUNDINTERVAL");
3030 GNUNET_SCHEDULER_shutdown ();
3034 /* Get initial size of sampler/view from the configuration */
3036 GNUNET_CONFIGURATION_get_value_number (cfg, "RPS", "INITSIZE",
3037 (long long unsigned int *) &sampler_size_est_need))
3039 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
3041 GNUNET_SCHEDULER_shutdown ();
3044 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", sampler_size_est_need);
3047 view = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO);
3049 /* file_name_view_log */
3050 if (GNUNET_OK != GNUNET_DISK_directory_create ("/tmp/rps/"))
3052 LOG (GNUNET_ERROR_TYPE_WARNING,
3053 "Failed to create directory /tmp/rps/\n");
3056 size = (14 + strlen (GNUNET_i2s_full (&own_identity)) + 1) * sizeof (char);
3057 file_name_view_log = GNUNET_malloc (size);
3058 out_size = GNUNET_snprintf (file_name_view_log,
3061 GNUNET_i2s_full (&own_identity));
3062 if (size < out_size ||
3065 LOG (GNUNET_ERROR_TYPE_WARNING,
3066 "Failed to write string to buffer (size: %i, out_size: %i)\n",
3072 /* connect to NSE */
3073 nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
3079 peer_map = GNUNET_CONTAINER_multipeermap_create (sampler_size_est_need, GNUNET_NO);
3082 /* Initialise cadet */
3083 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
3084 {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH ,
3085 sizeof (struct GNUNET_MessageHeader)},
3086 {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
3087 sizeof (struct GNUNET_MessageHeader)},
3088 {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY , 0},
3092 const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
3093 cadet_handle = GNUNET_CADET_connect (cfg,
3095 &handle_inbound_channel,
3100 peerinfo_handle = GNUNET_PEERINFO_connect (cfg);
3102 /* Initialise sampler */
3103 struct GNUNET_TIME_Relative half_round_interval;
3104 struct GNUNET_TIME_Relative max_round_interval;
3106 half_round_interval = GNUNET_TIME_relative_multiply (round_interval, .5);
3107 max_round_interval = GNUNET_TIME_relative_add (round_interval, half_round_interval);
3109 prot_sampler = RPS_sampler_init (sampler_size_est_need, max_round_interval);
3110 client_sampler = RPS_sampler_mod_init (sampler_size_est_need, max_round_interval);
3112 /* Initialise push and pull maps */
3113 push_map = CustomPeerMap_create (4);
3114 pull_map = CustomPeerMap_create (4);
3117 num_hist_update_tasks = 0;
3120 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
3121 GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, NULL);
3122 // TODO send push/pull to each of those peers?
3124 peerinfo_notify_handle = GNUNET_PEERINFO_notify (cfg,
3126 process_peerinfo_peers,
3134 * The main function for the rps service.
3136 * @param argc number of arguments from the command line
3137 * @param argv command line arguments
3138 * @return 0 ok, 1 on error
3141 main (int argc, char *const *argv)
3143 return (GNUNET_OK ==
3144 GNUNET_SERVICE_run (argc,
3147 GNUNET_SERVICE_OPTION_NONE,
3148 &run, NULL)) ? 0 : 1;
3151 /* end of gnunet-service-rps.c */