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 "gnunet-service-rps_view.h"
34 #include "rps-test_util.h"
36 #include "gnunet-service-rps_sampler.h"
41 #define LOG(kind, ...) GNUNET_log(kind, __VA_ARGS__)
43 // TODO modify @brief in every file
45 // TODO check for overflows
47 // TODO align message structs
49 // TODO connect to friends
51 // TODO store peers somewhere persistent
53 // TODO blacklist? (-> mal peer detection on top of brahms)
55 // TODO API request_cancel
57 // hist_size_init, hist_size_max
62 static const struct GNUNET_CONFIGURATION_Handle *cfg;
67 static struct GNUNET_PeerIdentity own_identity;
70 struct GNUNET_PeerIdentity *
71 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list, unsigned int size,
72 const struct GNUNET_PeerIdentity *ignore_list, unsigned int ignore_size);
75 /***********************************************************************
76 * Housekeeping with peers
77 ***********************************************************************/
80 * Closure used to pass the client and the id to the callback
81 * that replies to a client's request
88 struct ReplyCls *next;
89 struct ReplyCls *prev;
92 * The identifier of the request
97 * The handle to the request
99 struct RPS_SamplerRequestHandle *req_handle;
102 * The client handle to send the reply to
104 struct GNUNET_SERVER_Client *client;
109 * Struct used to store the context of a connected client.
116 struct ClientContext *next;
117 struct ClientContext *prev;
120 * The message queue to communicate with the client.
122 struct GNUNET_MQ_Handle *mq;
125 * DLL with handles to single requests from the client
127 struct ReplyCls *rep_cls_head;
128 struct ReplyCls *rep_cls_tail;
132 * DLL with all clients currently connected to us
134 struct ClientContext *cli_ctx_head;
135 struct ClientContext *cli_ctx_tail;
138 * Used to keep track in what lists single peerIDs are.
143 * If we are waiting for a reply from that peer (sent a pull request).
145 PULL_REPLY_PENDING = 0x01,
147 IN_OTHER_GOSSIP_LIST = 0x02, // unneeded?
148 IN_OWN_SAMPLER_LIST = 0x04, // unneeded?
149 IN_OWN_GOSSIP_LIST = 0x08, // unneeded?
152 * We set this bit when we can be sure the other peer is/was live.
157 * We set this bit when we are going to destroy the channel to this peer.
158 * When cleanup_channel is called, we know that we wanted to destroy it.
159 * Otherwise the channel to the other peer was destroyed.
166 * Functions of this type can be used to be stored at a peer for later execution.
168 typedef void (* PeerOp) (void *cls, const struct GNUNET_PeerIdentity *peer);
171 * Outstanding operation on peer consisting of callback and closure
173 struct PeerOutstandingOp
188 * List containing all messages that are yet to be send
190 struct PendingMessage
195 struct PendingMessage *next;
196 struct PendingMessage *prev;
199 * The envelope to the corresponding message
201 struct GNUNET_MQ_Envelope *ev;
204 * The corresponding context
206 struct PeerContext *peer_ctx;
216 * Struct used to keep track of other peer's status
218 * This is stored in a multipeermap.
223 * Message queue open to client
225 struct GNUNET_MQ_Handle *mq;
228 * Channel open to client.
230 struct GNUNET_CADET_Channel *send_channel;
233 * Channel open from client.
235 struct GNUNET_CADET_Channel *recv_channel; // unneeded?
238 * Array of outstanding operations on this peer.
240 struct PeerOutstandingOp *outstanding_ops;
243 * Handle to the callback given to cadet_ntfy_tmt_rdy()
245 * To be canceled on shutdown.
247 struct GNUNET_CADET_TransmitHandle *transmit_handle;
250 * Number of outstanding operations.
252 unsigned int num_outstanding_ops;
255 * Identity of the peer
257 struct GNUNET_PeerIdentity peer_id;
260 * Flags indicating status of peer
265 * DLL with all messages that are yet to be sent
267 struct PendingMessage *pending_messages_head;
268 struct PendingMessage *pending_messages_tail;
271 * This is pobably followed by 'statistical' data (when we first saw
272 * him, how did we get his ID, how many pushes (in a timeinterval),
277 /***********************************************************************
278 * /Housekeeping with peers
279 ***********************************************************************/
285 /***********************************************************************
287 ***********************************************************************/
290 * Sampler used for the Brahms protocol itself.
292 static struct RPS_Sampler *prot_sampler;
295 * Sampler used for the clients.
297 static struct RPS_Sampler *client_sampler;
300 * Set of all peers to keep track of them.
302 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
306 * Name to log view to
308 static char *file_name_view_log;
312 * The size of sampler we need to be able to satisfy the client's need of
315 static unsigned int sampler_size_client_need;
318 * The size of sampler we need to be able to satisfy the Brahms protocol's
319 * need of random peers.
321 * This is one minimum size the sampler grows to.
323 static unsigned int sampler_size_est_need;
327 * Percentage of total peer number in the view
328 * to send random PUSHes to
333 * Percentage of total peer number in the view
334 * to send random PULLs to
339 * The percentage gamma of history updates.
340 * Simply 1 - alpha - beta
345 * Identifier for the main task that runs periodically.
347 static struct GNUNET_SCHEDULER_Task *do_round_task;
350 * Time inverval the do_round task runs in.
352 static struct GNUNET_TIME_Relative round_interval;
357 * List to store peers received through pushes temporary.
359 static struct CustomPeerMap *push_map;
362 * List to store peers received through pulls temporary.
364 static struct CustomPeerMap *pull_map;
370 static struct GNUNET_NSE_Handle *nse;
375 static struct GNUNET_CADET_Handle *cadet_handle;
378 * Handler to PEERINFO.
380 static struct GNUNET_PEERINFO_Handle *peerinfo_handle;
383 * Handle for cancellation of iteration over peers.
385 struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify_handle;
391 * Only needed in the beginning to check how many of the 64 deltas
394 static unsigned int req_counter;
397 * Time of the last request we received.
399 * Used to compute the expected request rate.
401 static struct GNUNET_TIME_Absolute last_request;
404 * Size of #request_deltas.
406 #define REQUEST_DELTAS_SIZE 64
407 static unsigned int request_deltas_size = REQUEST_DELTAS_SIZE;
410 * Last 64 deltas between requests
412 static struct GNUNET_TIME_Relative request_deltas[REQUEST_DELTAS_SIZE];
415 * The prediction of the rate of requests
417 static struct GNUNET_TIME_Relative request_rate;
421 * Number of history update tasks.
423 uint32_t num_hist_update_tasks;
426 #ifdef ENABLE_MALICIOUS
428 * Type of malicious peer
430 * 0 Don't act malicious at all - Default
431 * 1 Try to maximise representation
432 * 2 Try to partition the network
435 uint32_t mal_type = 0;
438 * Other malicious peers
440 static struct GNUNET_PeerIdentity *mal_peers = NULL;
443 * Hashmap of malicious peers used as set.
444 * Used to more efficiently check whether we know that peer.
446 static struct GNUNET_CONTAINER_MultiPeerMap *mal_peer_set = NULL;
449 * Number of other malicious peers
451 static uint32_t num_mal_peers;
455 * If type is 2 This struct is used to store the attacked peers in a DLL
462 struct AttackedPeer *next;
463 struct AttackedPeer *prev;
468 struct GNUNET_PeerIdentity peer_id;
472 * If type is 2 this is the DLL of attacked peers
474 static struct AttackedPeer *att_peers_head = NULL;
475 static struct AttackedPeer *att_peers_tail = NULL;
478 * This index is used to point to an attacked peer to
479 * implement the round-robin-ish way to select attacked peers.
481 static struct AttackedPeer *att_peer_index = NULL;
484 * Hashmap of attacked peers used as set.
485 * Used to more efficiently check whether we know that peer.
487 static struct GNUNET_CONTAINER_MultiPeerMap *att_peer_set = NULL;
490 * Number of attacked peers
492 static uint32_t num_attacked_peers = 0;
496 * If type is 1 this is the attacked peer
498 static struct GNUNET_PeerIdentity attacked_peer;
501 * The limit of PUSHes we can send in one round.
502 * This is an assumption of the Brahms protocol and either implemented
505 * assumend to be the bandwidth limitation.
507 static uint32_t push_limit = 10000;
508 #endif /* ENABLE_MALICIOUS */
511 /***********************************************************************
513 ***********************************************************************/
520 /***********************************************************************
522 ***********************************************************************/
525 * Set a peer flag of given peer context.
527 #define set_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags |= mask)
530 * Get peer flag of given peer context.
532 #define get_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags & mask ? GNUNET_YES : GNUNET_NO)
535 * Unset flag of given peer context.
537 #define unset_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags &= (~mask))
540 * Clean the send channel of a peer
543 peer_clean (const struct GNUNET_PeerIdentity *peer);
547 * Check if peer is already in peer array.
550 in_arr (const struct GNUNET_PeerIdentity *array,
551 unsigned int arr_size,
552 const struct GNUNET_PeerIdentity *peer)
554 GNUNET_assert (NULL != peer);
559 GNUNET_assert (NULL != array);
563 for (i = 0; i < arr_size ; i++)
564 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&array[i], peer))
571 * Print peerlist to log.
574 print_peer_list (struct GNUNET_PeerIdentity *list, unsigned int len)
578 LOG (GNUNET_ERROR_TYPE_DEBUG,
579 "Printing peer list of length %u at %p:\n",
582 for (i = 0 ; i < len ; i++)
584 LOG (GNUNET_ERROR_TYPE_DEBUG,
586 i, GNUNET_i2s (&list[i]));
592 * Remove peer from list.
595 rem_from_list (struct GNUNET_PeerIdentity **peer_list,
596 unsigned int *list_size,
597 const struct GNUNET_PeerIdentity *peer)
600 struct GNUNET_PeerIdentity *tmp;
604 LOG (GNUNET_ERROR_TYPE_DEBUG,
605 "Removing peer %s from list at %p\n",
609 for ( i = 0 ; i < *list_size ; i++ )
611 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&tmp[i], peer))
613 if (i < *list_size -1)
614 { /* Not at the last entry -- shift peers left */
615 memcpy (&tmp[i], &tmp[i +1],
616 ((*list_size) - i -1) * sizeof (struct GNUNET_PeerIdentity));
618 /* Remove last entry (should be now useless PeerID) */
619 GNUNET_array_grow (tmp, *list_size, (*list_size) -1);
626 * Get random peer from the given list but don't return one from the @a ignore_list.
628 struct GNUNET_PeerIdentity *
629 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list,
631 const struct GNUNET_PeerIdentity *ignore_list,
632 uint32_t ignore_size)
636 struct GNUNET_PeerIdentity *tmp_peer_list;
637 struct GNUNET_PeerIdentity *peer;
639 GNUNET_assert (NULL != peer_list);
644 tmp_peer_list = NULL;
645 GNUNET_array_grow (tmp_peer_list, tmp_size, list_size);
646 memcpy (tmp_peer_list,
648 list_size * sizeof (struct GNUNET_PeerIdentity));
649 peer = GNUNET_new (struct GNUNET_PeerIdentity);
652 * Choose the r_index of the peer we want to return
653 * at random from the interval of the view
655 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
657 *peer = tmp_peer_list[r_index];
659 while (in_arr (ignore_list, ignore_size, peer))
661 rem_from_list (&tmp_peer_list, &tmp_size, peer);
663 print_peer_list (tmp_peer_list, tmp_size);
672 * Choose the r_index of the peer we want to return
673 * at random from the interval of the view
675 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
677 *peer = tmp_peer_list[r_index];
681 GNUNET_array_grow (tmp_peer_list, tmp_size, 0);
688 * Get the context of a peer. If not existing, create.
691 get_peer_ctx (const struct GNUNET_PeerIdentity *peer)
693 struct PeerContext *ctx;
696 ret = GNUNET_CONTAINER_multipeermap_contains (peer_map, peer);
697 GNUNET_assert (GNUNET_YES == ret);
698 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
699 GNUNET_assert (NULL != ctx);
704 * Create a new peer context and insert it into the peer map
707 create_peer_ctx (const struct GNUNET_PeerIdentity *peer)
709 struct PeerContext *ctx;
712 ctx = GNUNET_new (struct PeerContext);
713 ctx->peer_id = *peer;
714 ret = GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
715 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
716 GNUNET_assert (GNUNET_OK == ret);
722 * Set the peer flag to living and call the outstanding operations on this peer.
725 peer_is_live (struct PeerContext *peer_ctx)
727 struct GNUNET_PeerIdentity *peer;
729 /* Cancle transmit_handle if still scheduled */
730 if (NULL != peer_ctx->transmit_handle)
732 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->transmit_handle);
733 peer_ctx->transmit_handle = NULL;
736 peer = &peer_ctx->peer_id;
737 set_peer_flag (peer_ctx, VALID);
739 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer %s is live\n", GNUNET_i2s (peer));
741 if (0 < peer_ctx->num_outstanding_ops)
742 { /* Call outstanding operations */
745 for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
746 peer_ctx->outstanding_ops[i].op (peer_ctx->outstanding_ops[i].op_cls, peer);
747 GNUNET_array_grow (peer_ctx->outstanding_ops, peer_ctx->num_outstanding_ops, 0);
755 * Callback that is called when a channel was effectively established.
756 * This is given to ntfy_tmt_rdy and called when the channel was
757 * successfully established.
760 cadet_ntfy_tmt_rdy_cb (void *cls, size_t size, void *buf)
762 struct PeerContext *peer_ctx = (struct PeerContext *) cls;
764 peer_ctx->transmit_handle = NULL;
765 LOG (GNUNET_ERROR_TYPE_DEBUG,
766 "Set ->transmit_handle = NULL for peer %s\n",
767 GNUNET_i2s (&peer_ctx->peer_id));
772 peer_is_live (peer_ctx);
776 LOG (GNUNET_ERROR_TYPE_WARNING,
777 "Problems establishing a connection to peer %s in order to check liveliness\n",
778 GNUNET_i2s (&peer_ctx->peer_id));
779 // TODO reschedule? cleanup?
782 //if (NULL != peer_ctx->transmit_handle)
784 // LOG (GNUNET_ERROR_TYPE_DEBUG,
785 // "Trying to cancle transmit_handle for peer %s\n",
786 // GNUNET_i2s (&peer_ctx->peer_id));
787 // GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->transmit_handle);
788 // peer_ctx->transmit_handle = NULL;
796 * Get the channel of a peer. If not existing, create.
798 struct GNUNET_CADET_Channel *
799 get_channel (const struct GNUNET_PeerIdentity *peer)
801 struct PeerContext *peer_ctx;
803 peer_ctx = get_peer_ctx (peer);
804 if (NULL == peer_ctx->send_channel)
806 LOG (GNUNET_ERROR_TYPE_DEBUG,
807 "Trying to establish channel to peer %s\n",
810 peer_ctx->send_channel =
811 GNUNET_CADET_channel_create (cadet_handle,
814 GNUNET_RPS_CADET_PORT,
815 GNUNET_CADET_OPTION_RELIABLE);
818 return peer_ctx->send_channel;
823 * Get the message queue of a specific peer.
825 * If we already have a message queue open to this client,
826 * simply return it, otherways create one.
828 struct GNUNET_MQ_Handle *
829 get_mq (const struct GNUNET_PeerIdentity *peer_id)
831 struct PeerContext *peer_ctx;
833 peer_ctx = get_peer_ctx (peer_id);
835 GNUNET_assert (NULL == peer_ctx->transmit_handle);
837 if (NULL == peer_ctx->mq)
839 (void) get_channel (peer_id);
840 peer_ctx->mq = GNUNET_CADET_mq_create (peer_ctx->send_channel);
841 //do I have to explicitly put it in the peer_map?
842 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer_id, peer_ctx,
843 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
850 * Issue check whether peer is live
852 * @param peer_ctx the context of the peer
855 check_peer_live (struct PeerContext *peer_ctx)
857 LOG (GNUNET_ERROR_TYPE_DEBUG,
858 "Get informed about peer %s getting live\n",
859 GNUNET_i2s (&peer_ctx->peer_id));
861 if (NULL == peer_ctx->transmit_handle &&
862 NULL == peer_ctx->send_channel)
864 (void) get_channel (&peer_ctx->peer_id);
865 peer_ctx->transmit_handle =
866 GNUNET_CADET_notify_transmit_ready (peer_ctx->send_channel,
868 GNUNET_TIME_UNIT_FOREVER_REL,
869 sizeof (struct GNUNET_MessageHeader),
870 cadet_ntfy_tmt_rdy_cb,
873 else if (NULL != peer_ctx->transmit_handle)
874 LOG (GNUNET_ERROR_TYPE_DEBUG,
875 "Already waiting for notification\n");
876 else if (NULL != peer_ctx->send_channel)
877 LOG (GNUNET_ERROR_TYPE_DEBUG,
878 "Already have established channel to peer\n");
883 * Sum all time relatives of an array.
885 struct GNUNET_TIME_Relative
886 T_relative_sum (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
888 struct GNUNET_TIME_Relative sum;
891 sum = GNUNET_TIME_UNIT_ZERO;
892 for ( i = 0 ; i < arr_size ; i++ )
894 sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
901 * Compute the average of given time relatives.
903 struct GNUNET_TIME_Relative
904 T_relative_avg (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
906 return GNUNET_TIME_relative_divide (T_relative_sum (rel_array, arr_size), arr_size);
911 * Insert PeerID in #pull_map
913 * Called once we know a peer is live.
916 insert_in_pull_map (void *cls, const struct GNUNET_PeerIdentity *peer)
918 CustomPeerMap_put (pull_map, peer);
922 * Check whether #insert_in_pull_map was already scheduled
925 insert_in_pull_map_scheduled (const struct PeerContext *peer_ctx)
929 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
930 if (insert_in_pull_map == peer_ctx->outstanding_ops[i].op)
937 * Insert PeerID in #view
939 * Called once we know a peer is live.
942 insert_in_view (void *cls, const struct GNUNET_PeerIdentity *peer)
948 * Check whether #insert_in_view was already scheduled
951 insert_in_view_scheduled (const struct PeerContext *peer_ctx)
955 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
956 if (insert_in_view == peer_ctx->outstanding_ops[i].op)
963 * Update sampler with given PeerID.
966 insert_in_sampler (void *cls, const struct GNUNET_PeerIdentity *peer)
968 LOG (GNUNET_ERROR_TYPE_DEBUG,
969 "Updating samplers with peer %s from insert_in_sampler()\n",
971 RPS_sampler_update (prot_sampler, peer);
972 RPS_sampler_update (client_sampler, peer);
973 if (0 < RPS_sampler_count_id (prot_sampler, peer))
975 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
976 (void) create_peer_ctx (peer);
977 (void) get_channel (peer);
983 * Check whether #insert_in_sampler was already scheduled
986 insert_in_sampler_scheduled (const struct PeerContext *peer_ctx)
990 for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
991 if (insert_in_sampler== peer_ctx->outstanding_ops[i].op)
997 * Put random peer from sampler into the view as history update.
1000 hist_update (void *cls, struct GNUNET_PeerIdentity *ids, uint32_t num_peers)
1004 for (i = 0; i < num_peers; i++)
1007 to_file (file_name_view_log,
1009 GNUNET_i2s_full (ids));
1011 if (0 < num_hist_update_tasks)
1012 num_hist_update_tasks--;
1018 * Wrapper around #RPS_sampler_resize()
1020 * If we do not have enough sampler elements, double current sampler size
1021 * If we have more than enough sampler elements, halv current sampler size
1024 resize_wrapper (struct RPS_Sampler *sampler, uint32_t new_size)
1026 unsigned int sampler_size;
1029 // TODO respect the min, max
1030 sampler_size = RPS_sampler_get_size (sampler);
1031 if (sampler_size > new_size * 4)
1033 RPS_sampler_resize (sampler, sampler_size / 2);
1035 else if (sampler_size < new_size)
1037 RPS_sampler_resize (sampler, sampler_size * 2);
1039 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
1044 * Wrapper around #RPS_sampler_resize() resizing the client sampler
1047 client_resize_wrapper ()
1049 uint32_t bigger_size;
1053 bigger_size = GNUNET_MAX (sampler_size_est_need, sampler_size_client_need);
1055 // TODO respect the min, max
1056 resize_wrapper (client_sampler, bigger_size);
1057 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size_client is now %" PRIu32 "\n",
1063 * Estimate request rate
1065 * Called every time we receive a request from the client.
1070 struct GNUNET_TIME_Relative max_round_duration;
1072 if (request_deltas_size > req_counter)
1074 if ( 1 < req_counter)
1076 /* Shift last request deltas to the right */
1077 memcpy (&request_deltas[1],
1079 (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
1081 /* Add current delta to beginning */
1083 GNUNET_TIME_absolute_get_difference (last_request,
1084 GNUNET_TIME_absolute_get ());
1085 request_rate = T_relative_avg (request_deltas, req_counter);
1086 request_rate = (request_rate.rel_value_us < 1) ?
1087 GNUNET_TIME_relative_get_unit_ () : request_rate;
1089 /* Compute the duration a round will maximally take */
1090 max_round_duration =
1091 GNUNET_TIME_relative_add (round_interval,
1092 GNUNET_TIME_relative_divide (round_interval, 2));
1094 /* Set the estimated size the sampler has to have to
1095 * satisfy the current client request rate */
1096 sampler_size_client_need =
1097 max_round_duration.rel_value_us / request_rate.rel_value_us;
1099 /* Resize the sampler */
1100 client_resize_wrapper ();
1102 last_request = GNUNET_TIME_absolute_get ();
1107 * Add all peers in @a peer_array to @a peer_map used as set.
1109 * @param peer_array array containing the peers
1110 * @param num_peers number of peers in @peer_array
1111 * @param peer_map the peermap to use as set
1114 add_peer_array_to_set (const struct GNUNET_PeerIdentity *peer_array,
1115 unsigned int num_peers,
1116 struct GNUNET_CONTAINER_MultiPeerMap *peer_map)
1119 if (NULL == peer_map)
1121 LOG (GNUNET_ERROR_TYPE_WARNING,
1122 "Trying to add peers to non-existing peermap.\n");
1126 for (i = 0; i < num_peers; i++)
1128 GNUNET_CONTAINER_multipeermap_put (peer_map,
1131 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1137 * @brief Add an envelope to a message passed to mq to list of pending messages
1139 * @param peer peer the message was sent to
1140 * @param ev envelope to the message
1141 * @param type type of the message to be sent
1143 static struct PendingMessage *
1144 insert_pending_message (const struct GNUNET_PeerIdentity *peer,
1145 struct GNUNET_MQ_Envelope *ev,
1148 struct PendingMessage *pending_msg;
1149 struct PeerContext *peer_ctx;
1151 peer_ctx = get_peer_ctx (peer);
1152 pending_msg = GNUNET_new (struct PendingMessage);
1153 pending_msg->ev = ev;
1154 pending_msg->peer_ctx = peer_ctx;
1155 pending_msg->type = type;
1156 GNUNET_CONTAINER_DLL_insert (peer_ctx->pending_messages_head,
1157 peer_ctx->pending_messages_tail,
1163 remove_pending_message (struct PendingMessage *pending_msg)
1165 struct PeerContext *peer_ctx;
1167 peer_ctx = pending_msg->peer_ctx;
1168 GNUNET_CONTAINER_DLL_remove (peer_ctx->pending_messages_head,
1169 peer_ctx->pending_messages_tail,
1171 GNUNET_free (pending_msg);
1176 * @brief This is called once a message is sent.
1178 * @param cls type of the message that was sent
1181 mq_notify_sent_cb (void *cls)
1183 struct PendingMessage *pending_msg = (struct PendingMessage *) cls;
1184 LOG (GNUNET_ERROR_TYPE_DEBUG,
1187 remove_pending_message (pending_msg);
1192 * Send a PULL REPLY to @a peer_id
1194 * @param peer_id the peer to send the reply to.
1195 * @param peer_ids the peers to send to @a peer_id
1196 * @param num_peer_ids the number of peers to send to @a peer_id
1199 send_pull_reply (const struct GNUNET_PeerIdentity *peer_id,
1200 const struct GNUNET_PeerIdentity *peer_ids,
1201 unsigned int num_peer_ids)
1204 struct GNUNET_MQ_Handle *mq;
1205 struct GNUNET_MQ_Envelope *ev;
1206 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
1207 struct PendingMessage *pending_msg;
1209 /* Compute actual size */
1210 send_size = sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) +
1211 num_peer_ids * sizeof (struct GNUNET_PeerIdentity);
1213 if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size)
1214 /* Compute number of peers to send
1215 * If too long, simply truncate */
1216 // TODO select random ones via permutation
1217 // or even better: do good protocol design
1219 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE -
1220 sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1221 sizeof (struct GNUNET_PeerIdentity);
1223 send_size = num_peer_ids;
1225 LOG (GNUNET_ERROR_TYPE_DEBUG,
1226 "Going to send PULL REPLY with %u peers to %s\n",
1227 send_size, GNUNET_i2s (peer_id));
1229 mq = get_mq (peer_id);
1231 ev = GNUNET_MQ_msg_extra (out_msg,
1232 send_size * sizeof (struct GNUNET_PeerIdentity),
1233 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
1234 out_msg->num_peers = htonl (send_size);
1235 memcpy (&out_msg[1], peer_ids,
1236 send_size * sizeof (struct GNUNET_PeerIdentity));
1238 pending_msg = insert_pending_message (peer_id, ev, "PULL REPLY");
1239 GNUNET_MQ_notify_sent (ev,
1242 GNUNET_MQ_send (mq, ev);
1247 * This function is called on new peer_ids from 'external' sources
1248 * (client seed, cadet get_peers(), ...)
1250 * @param peer_id the new peer_id
1253 new_peer_id (const struct GNUNET_PeerIdentity *peer_id)
1255 struct PeerOutstandingOp out_op;
1256 struct PeerContext *peer_ctx;
1258 if ((NULL == peer_id) ||
1259 (0 == GNUNET_CRYPTO_cmp_peer_identity (&own_identity, peer_id)))
1261 LOG (GNUNET_ERROR_TYPE_DEBUG,
1262 "Got peer_id %s (at %p, view size: %u)\n",
1263 GNUNET_i2s (peer_id),
1267 /* if the seed peer is already know, skip context creation */
1268 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer_id))
1269 peer_ctx = create_peer_ctx (peer_id);
1271 peer_ctx = get_peer_ctx (peer_id);
1273 if (GNUNET_NO == get_peer_flag (peer_ctx, VALID))
1275 if (GNUNET_NO == insert_in_sampler_scheduled (peer_ctx))
1277 out_op.op = insert_in_sampler;
1278 out_op.op_cls = NULL;
1279 GNUNET_array_append (peer_ctx->outstanding_ops,
1280 peer_ctx->num_outstanding_ops,
1284 if (GNUNET_NO == insert_in_view_scheduled (peer_ctx))
1286 out_op.op = insert_in_view;
1287 out_op.op_cls = NULL;
1288 GNUNET_array_append (peer_ctx->outstanding_ops,
1289 peer_ctx->num_outstanding_ops,
1293 /* Trigger livelyness test on peer */
1294 check_peer_live (peer_ctx);
1298 // send push/pull to each of those peers?
1302 /***********************************************************************
1304 ***********************************************************************/
1307 destroy_reply_cls (struct ReplyCls *rep_cls)
1309 struct ClientContext *cli_ctx;
1311 cli_ctx = GNUNET_SERVER_client_get_user_context (rep_cls->client,
1312 struct ClientContext);
1313 GNUNET_assert (NULL != cli_ctx);
1314 GNUNET_CONTAINER_DLL_remove (cli_ctx->rep_cls_head,
1315 cli_ctx->rep_cls_tail,
1317 GNUNET_free (rep_cls);
1321 destroy_cli_ctx (struct ClientContext *cli_ctx)
1323 GNUNET_assert (NULL != cli_ctx);
1324 if (NULL != cli_ctx->rep_cls_head)
1326 LOG (GNUNET_ERROR_TYPE_WARNING,
1327 "Trying to destroy the context of a client that still has pending requests. Going to clean those\n");
1328 while (NULL != cli_ctx->rep_cls_head)
1329 destroy_reply_cls (cli_ctx->rep_cls_head);
1331 GNUNET_CONTAINER_DLL_remove (cli_ctx_head,
1334 GNUNET_free (cli_ctx);
1339 * Function called by NSE.
1341 * Updates sizes of sampler list and view and adapt those lists
1345 nse_callback (void *cls, struct GNUNET_TIME_Absolute timestamp,
1346 double logestimate, double std_dev)
1349 //double scale; // TODO this might go gloabal/config
1351 LOG (GNUNET_ERROR_TYPE_DEBUG,
1352 "Received a ns estimate - logest: %f, std_dev: %f (old_size: %u)\n",
1353 logestimate, std_dev, RPS_sampler_get_size (prot_sampler));
1355 estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
1356 // GNUNET_NSE_log_estimate_to_n (logestimate);
1357 estimate = pow (estimate, 1.0 / 3);
1358 // TODO add if std_dev is a number
1359 // estimate += (std_dev * scale);
1360 if (2 < ceil (estimate))
1362 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
1363 sampler_size_est_need = estimate;
1365 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
1367 /* If the NSE has changed adapt the lists accordingly */
1368 resize_wrapper (prot_sampler, sampler_size_est_need);
1369 client_resize_wrapper ();
1374 * Callback called once the requested PeerIDs are ready.
1376 * Sends those to the requesting client.
1378 void client_respond (void *cls,
1379 struct GNUNET_PeerIdentity *peer_ids, uint32_t num_peers)
1382 struct GNUNET_MQ_Envelope *ev;
1383 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
1384 struct ReplyCls *reply_cls = (struct ReplyCls *) cls;
1385 uint32_t size_needed;
1386 struct ClientContext *cli_ctx;
1388 GNUNET_assert (NULL != reply_cls);
1389 LOG (GNUNET_ERROR_TYPE_DEBUG,
1390 "sampler returned %" PRIu32 " peers:\n",
1392 for (i = 0; i < num_peers; i++)
1394 LOG (GNUNET_ERROR_TYPE_DEBUG,
1397 GNUNET_i2s (&peer_ids[i]));
1400 size_needed = sizeof (struct GNUNET_RPS_CS_ReplyMessage) +
1401 num_peers * sizeof (struct GNUNET_PeerIdentity);
1403 GNUNET_assert (GNUNET_SERVER_MAX_MESSAGE_SIZE >= size_needed);
1405 ev = GNUNET_MQ_msg_extra (out_msg,
1406 num_peers * sizeof (struct GNUNET_PeerIdentity),
1407 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
1408 out_msg->num_peers = htonl (num_peers);
1409 out_msg->id = htonl (reply_cls->id);
1411 memcpy (&out_msg[1],
1413 num_peers * sizeof (struct GNUNET_PeerIdentity));
1414 GNUNET_free (peer_ids);
1416 cli_ctx = GNUNET_SERVER_client_get_user_context (reply_cls->client,
1417 struct ClientContext);
1418 GNUNET_assert (NULL != cli_ctx);
1419 destroy_reply_cls (reply_cls);
1420 GNUNET_MQ_send (cli_ctx->mq, ev);
1425 * Handle RPS request from the client.
1427 * @param cls closure
1428 * @param client identification of the client
1429 * @param message the actual message
1432 handle_client_request (void *cls,
1433 struct GNUNET_SERVER_Client *client,
1434 const struct GNUNET_MessageHeader *message)
1436 struct GNUNET_RPS_CS_RequestMessage *msg;
1438 uint32_t size_needed;
1439 struct ReplyCls *reply_cls;
1441 struct ClientContext *cli_ctx;
1443 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
1445 num_peers = ntohl (msg->num_peers);
1446 size_needed = sizeof (struct GNUNET_RPS_CS_RequestMessage) +
1447 num_peers * sizeof (struct GNUNET_PeerIdentity);
1449 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
1451 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1452 "Message received from client has size larger than expected\n");
1453 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1457 for (i = 0 ; i < num_peers ; i++)
1460 LOG (GNUNET_ERROR_TYPE_DEBUG,
1461 "Client requested %" PRIu32 " random peer(s).\n",
1464 reply_cls = GNUNET_new (struct ReplyCls);
1465 reply_cls->id = ntohl (msg->id);
1466 reply_cls->client = client;
1467 reply_cls->req_handle = RPS_sampler_get_n_rand_peers (client_sampler,
1472 cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientContext);
1473 GNUNET_assert (NULL != cli_ctx);
1474 GNUNET_CONTAINER_DLL_insert (cli_ctx->rep_cls_head,
1475 cli_ctx->rep_cls_tail,
1477 GNUNET_SERVER_receive_done (client,
1483 * @brief Handle a message that requests the cancellation of a request
1486 * @param client the client that requests the cancellation
1487 * @param message the message containing the id of the request
1490 handle_client_request_cancel (void *cls,
1491 struct GNUNET_SERVER_Client *client,
1492 const struct GNUNET_MessageHeader *message)
1494 struct GNUNET_RPS_CS_RequestCancelMessage *msg =
1495 (struct GNUNET_RPS_CS_RequestCancelMessage *) message;
1496 struct ClientContext *cli_ctx;
1497 struct ReplyCls *rep_cls;
1499 cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientContext);
1500 GNUNET_assert (NULL != cli_ctx->rep_cls_head);
1501 rep_cls = cli_ctx->rep_cls_head;
1502 LOG (GNUNET_ERROR_TYPE_DEBUG,
1503 "Client cancels request with id %lu\n",
1505 while ( (NULL != rep_cls->next) &&
1506 (rep_cls->id != ntohl (msg->id)) )
1507 rep_cls = rep_cls->next;
1508 GNUNET_assert (rep_cls->id == ntohl (msg->id));
1509 RPS_sampler_request_cancel (rep_cls->req_handle);
1510 destroy_reply_cls (rep_cls);
1511 GNUNET_SERVER_receive_done (client,
1517 * Handle seed from the client.
1519 * @param cls closure
1520 * @param client identification of the client
1521 * @param message the actual message
1524 handle_client_seed (void *cls,
1525 struct GNUNET_SERVER_Client *client,
1526 const struct GNUNET_MessageHeader *message)
1528 struct GNUNET_RPS_CS_SeedMessage *in_msg;
1529 struct GNUNET_PeerIdentity *peers;
1533 if (sizeof (struct GNUNET_RPS_CS_SeedMessage) > ntohs (message->size))
1535 GNUNET_break_op (0);
1536 GNUNET_SERVER_receive_done (client,
1540 in_msg = (struct GNUNET_RPS_CS_SeedMessage *) message;
1541 num_peers = ntohl (in_msg->num_peers);
1542 peers = (struct GNUNET_PeerIdentity *) &in_msg[1];
1543 //peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1544 //memcpy (peers, &in_msg[1], num_peers * sizeof (struct GNUNET_PeerIdentity));
1546 if ((ntohs (message->size) - sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
1547 sizeof (struct GNUNET_PeerIdentity) != num_peers)
1549 GNUNET_break_op (0);
1550 GNUNET_SERVER_receive_done (client,
1555 LOG (GNUNET_ERROR_TYPE_DEBUG,
1556 "Client seeded peers:\n");
1557 print_peer_list (peers, num_peers);
1559 for (i = 0 ; i < num_peers ; i++)
1561 LOG (GNUNET_ERROR_TYPE_DEBUG,
1562 "Updating samplers with seed %" PRIu32 ": %s\n",
1564 GNUNET_i2s (&peers[i]));
1566 new_peer_id (&peers[i]);
1568 //RPS_sampler_update (prot_sampler, &peers[i]);
1569 //RPS_sampler_update (client_sampler, &peers[i]);
1572 ////GNUNET_free (peers);
1574 GNUNET_SERVER_receive_done (client,
1580 * Handle a PUSH message from another peer.
1582 * Check the proof of work and store the PeerID
1583 * in the temporary list for pushed PeerIDs.
1585 * @param cls Closure
1586 * @param channel The channel the PUSH was received over
1587 * @param channel_ctx The context associated with this channel
1588 * @param msg The message header
1591 handle_peer_push (void *cls,
1592 struct GNUNET_CADET_Channel *channel,
1594 const struct GNUNET_MessageHeader *msg)
1596 const struct GNUNET_PeerIdentity *peer;
1598 // (check the proof of work)
1600 peer = (const struct GNUNET_PeerIdentity *)
1601 GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
1602 // FIXME wait for cadet to change this function
1604 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PUSH (%s)\n", GNUNET_i2s (peer));
1606 #ifdef ENABLE_MALICIOUS
1607 struct AttackedPeer *tmp_att_peer;
1609 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1610 memcpy (&tmp_att_peer->peer_id, peer, sizeof (struct GNUNET_PeerIdentity));
1613 { /* Try to maximise representation */
1614 if (NULL == att_peer_set)
1615 att_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
1616 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1619 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1622 add_peer_array_to_set (peer, 1, att_peer_set);
1628 else if (2 == mal_type)
1629 { /* We attack one single well-known peer - simply ignore */
1634 GNUNET_free (tmp_att_peer);
1637 #endif /* ENABLE_MALICIOUS */
1639 /* Add the sending peer to the push_map */
1640 CustomPeerMap_put (push_map, peer);
1642 GNUNET_CADET_receive_done (channel);
1648 * Handle PULL REQUEST request message from another peer.
1650 * Reply with the view of PeerIDs.
1652 * @param cls Closure
1653 * @param channel The channel the PULL REQUEST was received over
1654 * @param channel_ctx The context associated with this channel
1655 * @param msg The message header
1658 handle_peer_pull_request (void *cls,
1659 struct GNUNET_CADET_Channel *channel,
1661 const struct GNUNET_MessageHeader *msg)
1663 struct GNUNET_PeerIdentity *peer;
1664 const struct GNUNET_PeerIdentity *view_array;
1666 peer = (struct GNUNET_PeerIdentity *)
1667 GNUNET_CADET_channel_get_info (channel,
1668 GNUNET_CADET_OPTION_PEER);
1669 // FIXME wait for cadet to change this function
1671 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REQUEST (%s)\n", GNUNET_i2s (peer));
1673 #ifdef ENABLE_MALICIOUS
1676 { /* Try to maximise representation */
1677 send_pull_reply (peer, mal_peers, num_mal_peers);
1681 else if (2 == mal_type)
1682 { /* Try to partition network */
1683 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
1685 send_pull_reply (peer, mal_peers, num_mal_peers);
1689 #endif /* ENABLE_MALICIOUS */
1691 view_array = View_get_as_array ();
1693 send_pull_reply (peer, view_array, View_size ());
1695 GNUNET_CADET_receive_done (channel);
1701 * Handle PULL REPLY message from another peer.
1703 * Check whether we sent a corresponding request and
1704 * whether this reply is the first one.
1706 * @param cls Closure
1707 * @param channel The channel the PUSH was received over
1708 * @param channel_ctx The context associated with this channel
1709 * @param msg The message header
1712 handle_peer_pull_reply (void *cls,
1713 struct GNUNET_CADET_Channel *channel,
1715 const struct GNUNET_MessageHeader *msg)
1717 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
1718 struct GNUNET_PeerIdentity *peers;
1719 struct PeerContext *peer_ctx;
1720 struct GNUNET_PeerIdentity *sender;
1721 struct PeerContext *sender_ctx;
1722 struct PeerOutstandingOp out_op;
1724 #ifdef ENABLE_MALICIOUS
1725 struct AttackedPeer *tmp_att_peer;
1726 #endif /* ENABLE_MALICIOUS */
1728 /* Check for protocol violation */
1729 if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->size))
1731 GNUNET_break_op (0);
1732 GNUNET_CADET_receive_done (channel);
1733 return GNUNET_SYSERR;
1736 in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
1737 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1738 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1740 LOG (GNUNET_ERROR_TYPE_ERROR,
1741 "message says it sends %" PRIu32 " peers, have space for %i peers\n",
1742 ntohl (in_msg->num_peers),
1743 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1744 sizeof (struct GNUNET_PeerIdentity));
1745 GNUNET_break_op (0);
1746 GNUNET_CADET_receive_done (channel);
1747 return GNUNET_SYSERR;
1750 // Guess simply casting isn't the nicest way...
1751 // FIXME wait for cadet to change this function
1752 sender = (struct GNUNET_PeerIdentity *)
1753 GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
1754 sender_ctx = get_peer_ctx (sender);
1756 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REPLY (%s)\n", GNUNET_i2s (sender));
1758 if (GNUNET_YES != get_peer_flag (sender_ctx, PULL_REPLY_PENDING))
1760 LOG (GNUNET_ERROR_TYPE_WARNING,
1761 "Received a pull reply from a peer we didn't request one from!\n");
1762 GNUNET_break_op (0);
1763 GNUNET_CADET_receive_done (channel);
1768 #ifdef ENABLE_MALICIOUS
1769 // We shouldn't even receive pull replies as we're not sending
1772 #endif /* ENABLE_MALICIOUS */
1774 /* Do actual logic */
1775 peers = (struct GNUNET_PeerIdentity *) &in_msg[1];
1777 LOG (GNUNET_ERROR_TYPE_DEBUG,
1778 "PULL REPLY received, got following %u peers:\n",
1779 ntohl (in_msg->num_peers));
1781 for (i = 0 ; i < ntohl (in_msg->num_peers) ; i++)
1783 LOG (GNUNET_ERROR_TYPE_DEBUG,
1786 GNUNET_i2s (&peers[i]));
1788 #ifdef ENABLE_MALICIOUS
1789 if ((NULL != att_peer_set) &&
1790 (1 == mal_type || 3 == mal_type))
1791 { /* Add attacked peer to local list */
1792 // TODO check if we sent a request and this was the first reply
1793 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1795 && GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mal_peer_set,
1797 && 0 != GNUNET_CRYPTO_cmp_peer_identity (&peers[i],
1800 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1801 tmp_att_peer->peer_id = peers[i];
1802 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1805 add_peer_array_to_set (&peers[i], 1, att_peer_set);
1809 #endif /* ENABLE_MALICIOUS */
1810 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity,
1813 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map,
1815 peer_ctx = create_peer_ctx (&peers[i]);
1817 peer_ctx = get_peer_ctx (&peers[i]);
1819 if (GNUNET_YES == get_peer_flag (peer_ctx, VALID))
1821 CustomPeerMap_put (pull_map, &peers[i]);
1823 else if (GNUNET_NO == insert_in_pull_map_scheduled (peer_ctx))
1825 out_op.op = insert_in_pull_map;
1826 out_op.op_cls = NULL;
1827 GNUNET_array_append (peer_ctx->outstanding_ops,
1828 peer_ctx->num_outstanding_ops,
1830 check_peer_live (peer_ctx);
1835 unset_peer_flag (sender_ctx, PULL_REPLY_PENDING);
1837 peer_clean (sender);
1839 GNUNET_CADET_receive_done (channel);
1845 * Compute a random delay.
1846 * A uniformly distributed value between mean + spread and mean - spread.
1848 * For example for mean 4 min and spread 2 the minimum is (4 min - (1/2 * 4 min))
1849 * It would return a random value between 2 and 6 min.
1851 * @param mean the mean
1852 * @param spread the inverse amount of deviation from the mean
1854 static struct GNUNET_TIME_Relative
1855 compute_rand_delay (struct GNUNET_TIME_Relative mean, unsigned int spread)
1857 struct GNUNET_TIME_Relative half_interval;
1858 struct GNUNET_TIME_Relative ret;
1859 unsigned int rand_delay;
1860 unsigned int max_rand_delay;
1864 LOG (GNUNET_ERROR_TYPE_WARNING,
1865 "Not accepting spread of 0\n");
1869 /* Compute random time value between spread * mean and spread * mean */
1870 half_interval = GNUNET_TIME_relative_divide (mean, spread);
1872 max_rand_delay = GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us / mean.rel_value_us * (2/spread);
1874 * Compute random value between (0 and 1) * round_interval
1875 * via multiplying round_interval with a 'fraction' (0 to value)/value
1877 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_rand_delay);
1878 ret = GNUNET_TIME_relative_multiply (mean, rand_delay);
1879 ret = GNUNET_TIME_relative_divide (ret, max_rand_delay);
1880 ret = GNUNET_TIME_relative_add (ret, half_interval);
1882 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ret.rel_value_us)
1883 LOG (GNUNET_ERROR_TYPE_WARNING,
1884 "Returning FOREVER_REL\n");
1891 * Send single pull request
1893 * @param peer_id the peer to send the pull request to.
1896 send_pull_request (const struct GNUNET_PeerIdentity *peer_id)
1898 struct GNUNET_MQ_Envelope *ev;
1899 struct GNUNET_MQ_Handle *mq;
1900 struct PeerContext *peer_ctx;
1901 struct PendingMessage *pending_msg;
1903 peer_ctx = get_peer_ctx (peer_id);
1904 GNUNET_assert (GNUNET_NO == get_peer_flag (peer_ctx, PULL_REPLY_PENDING));
1905 set_peer_flag (peer_ctx, PULL_REPLY_PENDING);
1907 LOG (GNUNET_ERROR_TYPE_DEBUG,
1908 "Going to send PULL REQUEST to peer %s.\n",
1909 GNUNET_i2s (peer_id));
1911 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
1912 mq = get_mq (peer_id);
1913 pending_msg = insert_pending_message (peer_id, ev, "PULL REQUEST");
1914 GNUNET_MQ_notify_sent (ev,
1917 GNUNET_MQ_send (mq, ev);
1924 * @param peer_id the peer to send the push to.
1927 send_push (const struct GNUNET_PeerIdentity *peer_id)
1929 struct GNUNET_MQ_Envelope *ev;
1930 struct GNUNET_MQ_Handle *mq;
1931 struct PendingMessage *pending_msg;
1933 LOG (GNUNET_ERROR_TYPE_DEBUG,
1934 "Going to send PUSH to peer %s.\n",
1935 GNUNET_i2s (peer_id));
1937 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
1938 mq = get_mq (peer_id);
1939 pending_msg = insert_pending_message (peer_id, ev, "PUSH");
1940 GNUNET_MQ_notify_sent (ev,
1943 GNUNET_MQ_send (mq, ev);
1948 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1951 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1954 #ifdef ENABLE_MALICIOUS
1956 * Turn RPS service to act malicious.
1958 * @param cls Closure
1959 * @param client The client that sent the message
1960 * @param msg The message header
1963 handle_client_act_malicious (void *cls,
1964 struct GNUNET_SERVER_Client *client,
1965 const struct GNUNET_MessageHeader *msg)
1967 struct GNUNET_RPS_CS_ActMaliciousMessage *in_msg;
1968 struct GNUNET_PeerIdentity *peers;
1969 uint32_t num_mal_peers_sent;
1970 uint32_t num_mal_peers_old;
1972 /* Check for protocol violation */
1973 if (sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage) > ntohs (msg->size))
1975 GNUNET_break_op (0);
1978 in_msg = (struct GNUNET_RPS_CS_ActMaliciousMessage *) msg;
1979 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
1980 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1982 LOG (GNUNET_ERROR_TYPE_ERROR,
1983 "message says it sends %" PRIu64 " peers, have space for %i peers\n",
1984 ntohl (in_msg->num_peers),
1985 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
1986 sizeof (struct GNUNET_PeerIdentity));
1987 GNUNET_break_op (0);
1991 /* Do actual logic */
1992 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1993 mal_type = ntohl (in_msg->type);
1994 if (NULL == mal_peer_set)
1995 mal_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
1997 LOG (GNUNET_ERROR_TYPE_DEBUG,
1998 "Now acting malicious type %" PRIu32 ", got %" PRIu32 " peers.\n",
2000 ntohl (in_msg->num_peers));
2003 { /* Try to maximise representation */
2004 /* Add other malicious peers to those we already know */
2006 num_mal_peers_sent = ntohl (in_msg->num_peers);
2007 num_mal_peers_old = num_mal_peers;
2008 GNUNET_array_grow (mal_peers,
2010 num_mal_peers + num_mal_peers_sent);
2011 memcpy (&mal_peers[num_mal_peers_old],
2013 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
2015 /* Add all mal peers to mal_peer_set */
2016 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
2020 /* Substitute do_round () with do_mal_round () */
2021 GNUNET_SCHEDULER_cancel (do_round_task);
2022 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
2025 else if (2 == mal_type
2027 { /* Try to partition the network */
2028 /* Add other malicious peers to those we already know */
2029 struct PeerContext *att_ctx;
2031 num_mal_peers_sent = ntohl (in_msg->num_peers) - 1;
2032 num_mal_peers_old = num_mal_peers;
2033 GNUNET_array_grow (mal_peers,
2035 num_mal_peers + num_mal_peers_sent);
2036 if (NULL != mal_peers &&
2039 memcpy (&mal_peers[num_mal_peers_old],
2041 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
2043 /* Add all mal peers to mal_peer_set */
2044 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
2049 /* Store the one attacked peer */
2050 memcpy (&attacked_peer,
2051 &in_msg->attacked_peer,
2052 sizeof (struct GNUNET_PeerIdentity));
2053 /* Set the flag of the attacked peer to valid to avoid problems */
2054 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map,
2057 att_ctx = create_peer_ctx (&attacked_peer);
2058 check_peer_live (att_ctx);
2061 LOG (GNUNET_ERROR_TYPE_DEBUG,
2062 "Attacked peer is %s\n",
2063 GNUNET_i2s (&attacked_peer));
2065 /* Substitute do_round () with do_mal_round () */
2066 GNUNET_SCHEDULER_cancel (do_round_task);
2067 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
2069 else if (0 == mal_type)
2070 { /* Stop acting malicious */
2071 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
2073 /* Substitute do_mal_round () with do_round () */
2074 GNUNET_SCHEDULER_cancel (do_round_task);
2075 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
2082 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2087 * Send out PUSHes and PULLs maliciously.
2089 * This is executed regylary.
2092 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2094 uint32_t num_pushes;
2096 struct GNUNET_TIME_Relative time_next_round;
2097 struct AttackedPeer *tmp_att_peer;
2098 struct PeerContext *peer_ctx;
2100 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round maliciously type %" PRIu32 ".\n",
2102 do_round_task = NULL;
2103 GNUNET_assert (mal_type <= 3);
2104 /* Do malicious actions */
2106 { /* Try to maximise representation */
2108 /* The maximum of pushes we're going to send this round */
2109 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit,
2110 num_attacked_peers),
2111 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
2113 LOG (GNUNET_ERROR_TYPE_DEBUG,
2114 "Going to send %" PRIu32 " pushes\n",
2117 /* Send PUSHes to attacked peers */
2118 for (i = 0 ; i < num_pushes ; i++)
2120 if (att_peers_tail == att_peer_index)
2121 att_peer_index = att_peers_head;
2123 att_peer_index = att_peer_index->next;
2125 send_push (&att_peer_index->peer_id);
2128 /* Send PULLs to some peers to learn about additional peers to attack */
2129 tmp_att_peer = att_peer_index;
2130 for (i = 0 ; i < num_pushes * alpha ; i++)
2132 if (att_peers_tail == tmp_att_peer)
2133 tmp_att_peer = att_peers_head;
2135 att_peer_index = tmp_att_peer->next;
2137 send_pull_request (&tmp_att_peer->peer_id);
2142 else if (2 == mal_type)
2144 * Try to partition the network
2145 * Send as many pushes to the attacked peer as possible
2146 * That is one push per round as it will ignore more.
2148 peer_ctx = get_peer_ctx (&attacked_peer);
2149 if (GNUNET_YES == get_peer_flag (peer_ctx, VALID))
2150 send_push (&attacked_peer);
2155 { /* Combined attack */
2157 /* Send PUSH to attacked peers */
2158 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map,
2161 peer_ctx = get_peer_ctx (&attacked_peer);
2162 if (GNUNET_YES == get_peer_flag (peer_ctx, VALID))
2164 LOG (GNUNET_ERROR_TYPE_DEBUG,
2165 "Goding to send push to attacked peer (%s)\n",
2166 GNUNET_i2s (&attacked_peer));
2167 send_push (&attacked_peer);
2170 check_peer_live (peer_ctx);
2173 peer_ctx = create_peer_ctx (&attacked_peer);
2174 check_peer_live (peer_ctx);
2176 /* The maximum of pushes we're going to send this round */
2177 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit - 1,
2178 num_attacked_peers),
2179 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
2181 LOG (GNUNET_ERROR_TYPE_DEBUG,
2182 "Going to send %" PRIu32 " pushes\n",
2185 for (i = 0 ; i < num_pushes ; i++)
2187 if (att_peers_tail == att_peer_index)
2188 att_peer_index = att_peers_head;
2190 att_peer_index = att_peer_index->next;
2192 send_push (&att_peer_index->peer_id);
2195 /* Send PULLs to some peers to learn about additional peers to attack */
2196 tmp_att_peer = att_peer_index;
2197 for (i = 0 ; i < num_pushes * alpha ; i++)
2199 if (att_peers_tail == tmp_att_peer)
2200 tmp_att_peer = att_peers_head;
2202 att_peer_index = tmp_att_peer->next;
2204 send_pull_request (&tmp_att_peer->peer_id);
2208 /* Schedule next round */
2209 time_next_round = compute_rand_delay (round_interval, 2);
2211 //do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_mal_round,
2213 GNUNET_assert (NULL == do_round_task);
2214 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_mal_round, NULL);
2215 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
2217 #endif /* ENABLE_MALICIOUS */
2221 * Send out PUSHes and PULLs, possibly update #view, samplers.
2223 * This is executed regylary.
2226 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2228 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round.\n");
2231 const struct GNUNET_PeerIdentity *view_array;
2232 unsigned int *permut;
2233 unsigned int a_peers; /* Number of peers we send pushes to */
2234 unsigned int b_peers; /* Number of peers we send pull requests to */
2235 uint32_t first_border;
2236 uint32_t second_border;
2237 struct GNUNET_PeerIdentity peer;
2238 struct PeerContext *peer_ctx;
2239 struct GNUNET_PeerIdentity *update_peer;
2241 do_round_task = NULL;
2242 LOG (GNUNET_ERROR_TYPE_DEBUG,
2243 "Printing view:\n");
2244 to_file (file_name_view_log,
2245 "___ new round ___");
2246 view_array = View_get_as_array ();
2247 for (i = 0; i < View_size (); i++)
2249 LOG (GNUNET_ERROR_TYPE_DEBUG,
2250 "\t%s\n", GNUNET_i2s (&view_array[i]));
2251 to_file (file_name_view_log,
2253 GNUNET_i2s_full (&view_array[i]));
2257 /* Send pushes and pull requests */
2258 if (0 < View_size ())
2260 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2264 a_peers = ceil (alpha * View_size ());
2266 LOG (GNUNET_ERROR_TYPE_DEBUG,
2267 "Going to send pushes to %u (ceil (%f * %u)) peers.\n",
2268 a_peers, alpha, View_size ());
2269 for (i = 0; i < a_peers; i++)
2271 peer = view_array[permut[i]];
2272 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer)) // TODO
2273 { // FIXME if this fails schedule/loop this for later
2278 /* Send PULL requests */
2279 b_peers = ceil (beta * View_size ());
2280 first_border = a_peers;
2281 second_border = a_peers + b_peers;
2282 if (second_border > View_size ())
2284 first_border = View_size () - b_peers;
2285 second_border = View_size ();
2287 LOG (GNUNET_ERROR_TYPE_DEBUG,
2288 "Going to send pulls to %u (ceil (%f * %u)) peers.\n",
2289 b_peers, beta, View_size ());
2290 for (i = first_border; i < second_border; i++)
2292 peer = view_array[permut[i]];
2293 peer_ctx = get_peer_ctx (&peer);
2294 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer) &&
2295 GNUNET_NO == get_peer_flag (peer_ctx, PULL_REPLY_PENDING)) // TODO
2296 { // FIXME if this fails schedule/loop this for later
2297 send_pull_request (&peer);
2301 GNUNET_free (permut);
2307 /* TODO see how many peers are in push-/pull- list! */
2309 if ((CustomPeerMap_size (push_map) <= alpha * View_size ()) &&
2310 (0 < CustomPeerMap_size (push_map)) &&
2311 (0 < CustomPeerMap_size (pull_map)))
2312 { /* If conditions for update are fulfilled, update */
2313 LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the view.\n");
2315 uint32_t final_size;
2316 uint32_t peers_to_clean_size;
2317 struct GNUNET_PeerIdentity *peers_to_clean;
2319 peers_to_clean = NULL;
2320 peers_to_clean_size = 0;
2321 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, View_size ());
2322 memcpy (peers_to_clean,
2324 View_size () * sizeof (struct GNUNET_PeerIdentity));
2326 /* Seems like recreating is the easiest way of emptying the peermap */
2328 to_file (file_name_view_log,
2331 first_border = GNUNET_MIN (ceil (alpha * sampler_size_est_need),
2332 CustomPeerMap_size (push_map));
2333 second_border = first_border +
2334 GNUNET_MIN (floor (beta * sampler_size_est_need),
2335 CustomPeerMap_size (pull_map));
2336 final_size = second_border +
2337 ceil ((1 - (alpha + beta)) * sampler_size_est_need);
2339 /* Update view with peers received through PUSHes */
2340 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2341 CustomPeerMap_size (push_map));
2342 for (i = 0; i < first_border; i++)
2344 View_put (CustomPeerMap_get_peer_by_index (push_map, permut[i]));
2345 to_file (file_name_view_log,
2347 GNUNET_i2s_full (&view_array[i]));
2348 // TODO change the peer_flags accordingly
2350 GNUNET_free (permut);
2353 /* Update view with peers received through PULLs */
2354 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2355 CustomPeerMap_size (pull_map));
2356 for (i = first_border; i < second_border; i++)
2358 View_put (CustomPeerMap_get_peer_by_index (pull_map,
2359 permut[i - first_border]));
2360 to_file (file_name_view_log,
2362 GNUNET_i2s_full (&view_array[i]));
2363 // TODO change the peer_flags accordingly
2365 GNUNET_free (permut);
2368 /* Update view with peers from history */
2369 RPS_sampler_get_n_rand_peers (prot_sampler,
2372 final_size - second_border);
2373 num_hist_update_tasks = final_size - second_border;
2374 // TODO change the peer_flags accordingly
2376 for (i = 0; i < View_size (); i++)
2377 rem_from_list (&peers_to_clean, &peers_to_clean_size, &view_array[i]);
2379 /* Clean peers that were removed from the view */
2380 for (i = 0; i < peers_to_clean_size; i++)
2382 to_file (file_name_view_log,
2384 GNUNET_i2s_full (&peers_to_clean[i]));
2385 peer_clean (&peers_to_clean[i]);
2388 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, 0);
2389 peers_to_clean = NULL;
2393 LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the view.\n");
2395 // TODO independent of that also get some peers from CADET_get_peers()?
2397 LOG (GNUNET_ERROR_TYPE_DEBUG,
2398 "Received %u pushes and %u pulls last round (alpha (%.2f) * view_size (%u) = %.2f)\n",
2399 CustomPeerMap_size (push_map),
2400 CustomPeerMap_size (pull_map),
2403 alpha * View_size ());
2405 /* Update samplers */
2406 for (i = 0; i < CustomPeerMap_size (push_map); i++)
2408 update_peer = CustomPeerMap_get_peer_by_index (push_map, i);
2409 LOG (GNUNET_ERROR_TYPE_DEBUG,
2410 "Updating with peer %s from push list\n",
2411 GNUNET_i2s (update_peer));
2412 insert_in_sampler (NULL, update_peer);
2413 peer_clean (update_peer); /* This cleans only if it is not in the view */
2416 for (i = 0; i < CustomPeerMap_size (pull_map); i++)
2418 LOG (GNUNET_ERROR_TYPE_DEBUG,
2419 "Updating with peer %s from pull list\n",
2420 GNUNET_i2s (CustomPeerMap_get_peer_by_index (pull_map, i)));
2421 insert_in_sampler (NULL, CustomPeerMap_get_peer_by_index (pull_map, i));
2422 /* This cleans only if it is not in the view */
2423 peer_clean (CustomPeerMap_get_peer_by_index (pull_map, i));
2427 /* Empty push/pull lists */
2428 CustomPeerMap_clear (push_map);
2429 CustomPeerMap_clear (pull_map);
2431 struct GNUNET_TIME_Relative time_next_round;
2433 time_next_round = compute_rand_delay (round_interval, 2);
2435 /* Schedule next round */
2436 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_round, NULL);
2437 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
2442 rps_start (struct GNUNET_SERVER_Handle *server);
2446 * This is called from GNUNET_CADET_get_peers().
2448 * It is called on every peer(ID) that cadet somehow has contact with.
2449 * We use those to initialise the sampler.
2452 init_peer_cb (void *cls,
2453 const struct GNUNET_PeerIdentity *peer,
2454 int tunnel, // "Do we have a tunnel towards this peer?"
2455 unsigned int n_paths, // "Number of known paths towards this peer"
2456 unsigned int best_path) // "How long is the best path?
2457 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
2461 LOG (GNUNET_ERROR_TYPE_DEBUG,
2462 "Got peer_id %s from cadet\n",
2470 * Iterator over peers from peerinfo.
2472 * @param cls closure
2473 * @param peer id of the peer, NULL for last call
2474 * @param hello hello message for the peer (can be NULL)
2475 * @param error message
2478 process_peerinfo_peers (void *cls,
2479 const struct GNUNET_PeerIdentity *peer,
2480 const struct GNUNET_HELLO_Message *hello,
2481 const char *err_msg)
2485 LOG (GNUNET_ERROR_TYPE_DEBUG,
2486 "Got peer_id %s from peerinfo\n",
2494 * Callback used to remove peers from the multipeermap.
2497 peer_remove_cb (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
2499 struct PeerContext *peer_ctx;
2500 const struct GNUNET_CADET_Channel *channel =
2501 (const struct GNUNET_CADET_Channel *) cls;
2503 peer_ctx = (struct PeerContext *) value;
2504 set_peer_flag (peer_ctx, TO_DESTROY);
2506 LOG (GNUNET_ERROR_TYPE_DEBUG,
2507 "Going to remove peer %s\n",
2508 GNUNET_i2s (&peer_ctx->peer_id));
2510 /* Remove it from the sampler used for the Brahms protocol */
2511 RPS_sampler_reinitialise_by_value (prot_sampler, key);
2513 /* If operations are still scheduled for this peer cancel those */
2514 if (0 != peer_ctx->num_outstanding_ops)
2516 GNUNET_array_grow (peer_ctx->outstanding_ops,
2517 peer_ctx->num_outstanding_ops,
2521 /* If we are still waiting for notification whether this peer is live
2522 * cancel the according task */
2523 if (NULL != peer_ctx->transmit_handle)
2525 LOG (GNUNET_ERROR_TYPE_DEBUG,
2526 "Trying to cancle transmit_handle for peer %s\n",
2528 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->transmit_handle);
2529 peer_ctx->transmit_handle = NULL;
2532 unset_peer_flag (peer_ctx, PULL_REPLY_PENDING);
2534 /* Remove peer from view */
2535 if (View_contains_peer (key))
2537 to_file (file_name_view_log,
2538 "-%s\t(cleanup channel, other peer)",
2539 GNUNET_i2s_full (key));
2540 View_remove_peer (key);
2543 /* Remove from push and pull lists */
2544 CustomPeerMap_remove_peer (push_map, key);
2545 CustomPeerMap_remove_peer (pull_map, key);
2547 /* Cancle messages that have not been sent yet */
2548 while (NULL != peer_ctx->pending_messages_head)
2550 LOG (GNUNET_ERROR_TYPE_DEBUG,
2551 "Removing unsent %s\n",
2552 peer_ctx->pending_messages_head->type);
2553 /* We are not able to cancel messages as #GNUNET_CADET_mq_create () does not
2554 * set a #GNUNET_MQ_CancelImpl */
2555 /* GNUNET_MQ_send_cancel (peer_ctx->pending_messages_head->ev); */
2556 remove_pending_message (peer_ctx->pending_messages_head);
2559 /* If there is still a mq destroy it */
2560 if (NULL != peer_ctx->mq)
2562 GNUNET_MQ_destroy (peer_ctx->mq);
2563 peer_ctx->mq = NULL;
2567 /* Remove the send_channel
2568 * This function should be called again from #cleanup_channel (callback
2569 * called on the destruction of channels) and clean up the rest. */
2570 if (NULL != peer_ctx->send_channel &&
2571 channel != peer_ctx->send_channel)
2573 GNUNET_CADET_channel_destroy (peer_ctx->send_channel);
2574 peer_ctx->send_channel = NULL;
2577 /* Remove the recv_channel
2578 * This function should be called again from #cleanup_channel (callback
2579 * called on the destruction of channels) and clean up the rest. */
2580 if (NULL != peer_ctx->recv_channel &&
2581 channel != peer_ctx->recv_channel)
2583 GNUNET_CADET_channel_destroy (peer_ctx->recv_channel);
2584 peer_ctx->recv_channel = NULL;
2587 /* If there is no channel we have to remove the context now */
2588 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_remove_all (peer_map, key))
2589 LOG (GNUNET_ERROR_TYPE_WARNING, "removing peer from peer_map failed\n");
2591 GNUNET_free (peer_ctx);
2598 * Clean the send channel of a peer
2599 * If there is also no channel to receive messages from that peer, remove it
2603 peer_clean (const struct GNUNET_PeerIdentity *peer)
2605 struct PeerContext *peer_ctx;
2606 /* struct GNUNET_CADET_Channel *channel; */
2608 if ( (0 == RPS_sampler_count_id (prot_sampler, peer)) &&
2609 (GNUNET_NO == View_contains_peer (peer)) &&
2610 (GNUNET_NO == CustomPeerMap_contains_peer (push_map, peer)) &&
2611 (GNUNET_NO == CustomPeerMap_contains_peer (pull_map, peer)) &&
2612 (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer)) )
2614 peer_ctx = get_peer_ctx (peer);
2616 if ( (NULL == peer_ctx->recv_channel) &&
2617 (NULL == peer_ctx->pending_messages_head) &&
2618 (GNUNET_NO == get_peer_flag (peer_ctx, PULL_REPLY_PENDING)) )
2620 #ifdef ENABLE_MALICIOUS
2621 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
2622 peer_remove_cb (NULL, peer, peer_ctx);
2623 #else /* ENABLE_MALICIOUS */
2624 peer_remove_cb (NULL, peer, peer_ctx);
2625 #endif /* ENABLE_MALICIOUS */
2632 * Task run during shutdown.
2638 shutdown_task (void *cls,
2639 const struct GNUNET_SCHEDULER_TaskContext *tc)
2642 LOG (GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
2644 GNUNET_PEERINFO_notify_cancel (peerinfo_notify_handle);
2645 GNUNET_PEERINFO_disconnect (peerinfo_handle);
2647 if (NULL != do_round_task)
2649 GNUNET_SCHEDULER_cancel (do_round_task);
2650 do_round_task = NULL;
2654 if (GNUNET_SYSERR ==
2655 GNUNET_CONTAINER_multipeermap_iterate (peer_map, peer_remove_cb, NULL))
2656 LOG (GNUNET_ERROR_TYPE_WARNING,
2657 "Iterating over peers to disconnect from them was cancelled\n");
2660 GNUNET_NSE_disconnect (nse);
2661 RPS_sampler_destroy (prot_sampler);
2662 RPS_sampler_destroy (client_sampler);
2663 LOG (GNUNET_ERROR_TYPE_DEBUG,
2664 "Size of the peermap: %u\n",
2665 GNUNET_CONTAINER_multipeermap_size (peer_map));
2666 GNUNET_break (0 == GNUNET_CONTAINER_multipeermap_size (peer_map));
2667 GNUNET_CADET_disconnect (cadet_handle);
2668 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
2670 CustomPeerMap_destroy (push_map);
2671 CustomPeerMap_destroy (pull_map);
2672 #ifdef ENABLE_MALICIOUS
2673 struct AttackedPeer *tmp_att_peer;
2674 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
2675 if (NULL != mal_peer_set)
2676 GNUNET_CONTAINER_multipeermap_destroy (mal_peer_set);
2677 if (NULL != att_peer_set)
2678 GNUNET_CONTAINER_multipeermap_destroy (att_peer_set);
2679 while (NULL != att_peers_head)
2681 tmp_att_peer = att_peers_head;
2682 GNUNET_CONTAINER_DLL_remove (att_peers_head, att_peers_tail, tmp_att_peer);
2684 #endif /* ENABLE_MALICIOUS */
2689 * @brief Get informed about a connecting client.
2692 * @param client the client that connects
2695 handle_client_connect (void *cls,
2696 struct GNUNET_SERVER_Client *client)
2698 struct ClientContext *cli_ctx;
2700 LOG (GNUNET_ERROR_TYPE_DEBUG,
2701 "Client connected\n");
2703 return; /* Server was destroyed before a client connected. Shutting down */
2704 cli_ctx = GNUNET_new (struct ClientContext);
2705 cli_ctx->mq = GNUNET_MQ_queue_for_server_client (client);
2706 GNUNET_SERVER_client_set_user_context (client, cli_ctx);
2707 GNUNET_CONTAINER_DLL_insert (cli_ctx_head,
2713 * A client disconnected. Remove all of its data structure entries.
2715 * @param cls closure, NULL
2716 * @param client identification of the client
2719 handle_client_disconnect (void *cls,
2720 struct GNUNET_SERVER_Client *client)
2722 struct ClientContext *cli_ctx;
2725 {/* shutdown task */
2726 while (NULL != cli_ctx_head)
2727 destroy_cli_ctx (cli_ctx_head);
2731 cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientContext);
2732 destroy_cli_ctx (cli_ctx);
2738 * Handle the channel a peer opens to us.
2740 * @param cls The closure
2741 * @param channel The channel the peer wants to establish
2742 * @param initiator The peer's peer ID
2743 * @param port The port the channel is being established over
2744 * @param options Further options
2747 handle_inbound_channel (void *cls,
2748 struct GNUNET_CADET_Channel *channel,
2749 const struct GNUNET_PeerIdentity *initiator,
2751 enum GNUNET_CADET_ChannelOption options)
2753 struct PeerContext *peer_ctx;
2755 LOG (GNUNET_ERROR_TYPE_DEBUG,
2756 "New channel was established to us (Peer %s).\n",
2757 GNUNET_i2s (initiator));
2758 GNUNET_assert (NULL != channel); /* according to cadet API */
2759 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, initiator))
2760 peer_ctx = create_peer_ctx (initiator);
2762 peer_ctx = get_peer_ctx (initiator);
2763 /* We only accept one incoming channel from peers */
2764 if (NULL != peer_ctx->recv_channel)
2766 GNUNET_CADET_channel_destroy (channel);
2769 peer_ctx->recv_channel = channel;
2770 peer_is_live (peer_ctx);
2776 * This is called when a channel is destroyed.
2778 * @param cls The closure
2779 * @param channel The channel being closed
2780 * @param channel_ctx The context associated with this channel
2783 cleanup_channel (void *cls,
2784 const struct GNUNET_CADET_Channel *channel,
2787 struct GNUNET_PeerIdentity *peer;
2788 struct PeerContext *peer_ctx;
2790 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
2791 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
2792 // Guess simply casting isn't the nicest way...
2793 // FIXME wait for cadet to change this function
2795 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
2796 {/* We don't want to implicitly create a context that we're about to kill */
2797 peer_ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
2798 if (NULL == peer_ctx) /* It could have been removed by shutdown_task */
2801 if (get_peer_flag (peer_ctx, TO_DESTROY))
2802 {/* We initiatad the destruction of this particular peer */
2803 if (channel == peer_ctx->send_channel)
2804 peer_ctx->send_channel = NULL;
2805 else if (channel == peer_ctx->recv_channel)
2806 peer_ctx->recv_channel = NULL;
2808 to_file (file_name_view_log,
2809 "-%s\t(cleanup channel, ourself)",
2810 GNUNET_i2s_full (peer));
2814 { /* We did not initiate the destruction of this peer */
2815 if (channel == peer_ctx->send_channel)
2816 { /* Something (but us) killd the channel - clean up peer */
2817 LOG (GNUNET_ERROR_TYPE_DEBUG,
2818 "send channel (%s) was destroyed - cleaning up\n",
2820 peer_ctx->send_channel = NULL;
2821 /* Somwewhat {ab,re}use the iterator function */
2822 /* Cast to void is ok, because it's used as void in peer_remove_cb */
2823 (void) peer_remove_cb ((void *) channel, peer, peer_ctx);
2825 else if (channel == peer_ctx->recv_channel)
2826 { /* Other peer doesn't want to send us messages anymore */
2827 LOG (GNUNET_ERROR_TYPE_DEBUG,
2828 "Peer %s destroyed recv channel - cleaning up channel\n",
2830 peer_ctx->recv_channel = NULL;
2834 LOG (GNUNET_ERROR_TYPE_WARNING,
2835 "unknown channel (%s) was destroyed\n",
2842 { /* We don't know a context to that peer */
2843 LOG (GNUNET_ERROR_TYPE_DEBUG,
2844 "channel (%s) without associated context was destroyed\n",
2851 * Actually start the service.
2854 rps_start (struct GNUNET_SERVER_Handle *server)
2856 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
2857 {&handle_client_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
2858 sizeof (struct GNUNET_RPS_CS_RequestMessage)},
2859 {&handle_client_request_cancel, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST_CANCEL,
2860 sizeof (struct GNUNET_RPS_CS_RequestCancelMessage)},
2861 {&handle_client_seed, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_SEED, 0},
2862 #ifdef ENABLE_MALICIOUS
2863 {&handle_client_act_malicious, NULL, GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS , 0},
2864 #endif /* ENABLE_MALICIOUS */
2868 GNUNET_SERVER_add_handlers (server, handlers);
2869 GNUNET_SERVER_connect_notify (server,
2870 &handle_client_connect,
2872 GNUNET_SERVER_disconnect_notify (server,
2873 &handle_client_disconnect,
2875 LOG (GNUNET_ERROR_TYPE_INFO, "Ready to receive requests from clients\n");
2878 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
2879 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
2881 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2888 * Process statistics requests.
2890 * @param cls closure
2891 * @param server the initialized server
2892 * @param c configuration to use
2896 struct GNUNET_SERVER_Handle *server,
2897 const struct GNUNET_CONFIGURATION_Handle *c)
2902 // TODO check what this does -- copied from gnunet-boss
2903 // - seems to work as expected
2904 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
2909 GNUNET_CRYPTO_get_peer_identity (cfg, &own_identity); // TODO check return value
2910 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2911 "STARTING SERVICE (rps) for peer [%s]\n",
2912 GNUNET_i2s (&own_identity));
2913 #ifdef ENABLE_MALICIOUS
2914 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2915 "Malicious execution compiled in.\n");
2916 #endif /* ENABLE_MALICIOUS */
2920 /* Get time interval from the configuration */
2921 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
2925 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2926 "RPS", "ROUNDINTERVAL");
2927 GNUNET_SCHEDULER_shutdown ();
2931 /* Get initial size of sampler/view from the configuration */
2933 GNUNET_CONFIGURATION_get_value_number (cfg, "RPS", "INITSIZE",
2934 (long long unsigned int *) &sampler_size_est_need))
2936 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2938 GNUNET_SCHEDULER_shutdown ();
2941 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", sampler_size_est_need);
2946 /* file_name_view_log */
2947 if (GNUNET_OK != GNUNET_DISK_directory_create ("/tmp/rps/"))
2949 LOG (GNUNET_ERROR_TYPE_WARNING,
2950 "Failed to create directory /tmp/rps/\n");
2953 size = (14 + strlen (GNUNET_i2s_full (&own_identity)) + 1) * sizeof (char);
2954 file_name_view_log = GNUNET_malloc (size);
2955 out_size = GNUNET_snprintf (file_name_view_log,
2958 GNUNET_i2s_full (&own_identity));
2959 if (size < out_size ||
2962 LOG (GNUNET_ERROR_TYPE_WARNING,
2963 "Failed to write string to buffer (size: %i, out_size: %i)\n",
2969 /* connect to NSE */
2970 nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
2976 peer_map = GNUNET_CONTAINER_multipeermap_create (sampler_size_est_need, GNUNET_NO);
2979 /* Initialise cadet */
2980 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
2981 {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH ,
2982 sizeof (struct GNUNET_MessageHeader)},
2983 {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
2984 sizeof (struct GNUNET_MessageHeader)},
2985 {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY , 0},
2989 const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
2990 cadet_handle = GNUNET_CADET_connect (cfg,
2992 &handle_inbound_channel,
2997 peerinfo_handle = GNUNET_PEERINFO_connect (cfg);
2999 /* Initialise sampler */
3000 struct GNUNET_TIME_Relative half_round_interval;
3001 struct GNUNET_TIME_Relative max_round_interval;
3003 half_round_interval = GNUNET_TIME_relative_multiply (round_interval, .5);
3004 max_round_interval = GNUNET_TIME_relative_add (round_interval, half_round_interval);
3006 prot_sampler = RPS_sampler_init (sampler_size_est_need, max_round_interval);
3007 client_sampler = RPS_sampler_mod_init (sampler_size_est_need, max_round_interval);
3009 /* Initialise push and pull maps */
3010 push_map = CustomPeerMap_create (4);
3011 pull_map = CustomPeerMap_create (4);
3014 num_hist_update_tasks = 0;
3017 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
3018 GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, NULL);
3019 // TODO send push/pull to each of those peers?
3021 peerinfo_notify_handle = GNUNET_PEERINFO_notify (cfg,
3023 process_peerinfo_peers,
3031 * The main function for the rps service.
3033 * @param argc number of arguments from the command line
3034 * @param argv command line arguments
3035 * @return 0 ok, 1 on error
3038 main (int argc, char *const *argv)
3040 return (GNUNET_OK ==
3041 GNUNET_SERVICE_run (argc,
3044 GNUNET_SERVICE_OPTION_NONE,
3045 &run, NULL)) ? 0 : 1;
3048 /* end of gnunet-service-rps.c */