2 This file is part of GNUnet.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file rps/gnunet-service-rps.c
23 * @brief rps service implementation
24 * @author Julius Bünger
27 #include "gnunet_util_lib.h"
28 #include "gnunet_cadet_service.h"
29 #include "gnunet_peerinfo_service.h"
30 #include "gnunet_nse_service.h"
32 #include "rps-test_util.h"
34 #include "gnunet-service-rps_sampler.h"
39 #define LOG(kind, ...) GNUNET_log(kind, __VA_ARGS__)
41 // TODO modify @brief in every file
43 // TODO check for overflows
45 // TODO align message structs
47 // TODO connect to friends
49 // TODO store peers somewhere persistent
51 // TODO blacklist? (-> mal peer detection on top of brahms)
53 // TODO API request_cancel
55 // TODO don't pass peermap all the time
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 * Struct used to store the context of a connected client.
85 * The message queue to communicate with the client.
87 struct GNUNET_MQ_Handle *mq;
91 * Used to keep track in what lists single peerIDs are.
95 PULL_REPLY_PENDING = 0x01,
96 IN_OTHER_GOSSIP_LIST = 0x02, // unneeded?
97 IN_OWN_SAMPLER_LIST = 0x04, // unneeded?
98 IN_OWN_GOSSIP_LIST = 0x08, // unneeded?
101 * We set this bit when we can be sure the other peer is/was live.
106 * We set this bit when we are going to destroy the channel to this peer.
107 * When cleanup_channel is called, we know that we wanted to destroy it.
108 * Otherwise the channel to the other peer was destroyed.
115 * Functions of this type can be used to be stored at a peer for later execution.
117 typedef void (* PeerOp) (void *cls, const struct GNUNET_PeerIdentity *peer);
120 * Outstanding operation on peer consisting of callback and closure
122 struct PeerOutstandingOp
137 * Struct used to keep track of other peer's status
139 * This is stored in a multipeermap.
144 * Message queue open to client
146 struct GNUNET_MQ_Handle *mq;
149 * Channel open to client.
151 struct GNUNET_CADET_Channel *send_channel;
154 * Channel open from client.
156 struct GNUNET_CADET_Channel *recv_channel; // unneeded?
159 * Array of outstanding operations on this peer.
161 struct PeerOutstandingOp *outstanding_ops;
164 * Handle to the callback given to cadet_ntfy_tmt_rdy()
166 * To be canceled on shutdown.
168 struct GNUNET_CADET_TransmitHandle *transmit_handle;
171 * Number of outstanding operations.
173 unsigned int num_outstanding_ops;
176 * Identity of the peer
178 struct GNUNET_PeerIdentity peer_id;
181 * Flags indicating status of peer
186 * This is pobably followed by 'statistical' data (when we first saw
187 * him, how did we get his ID, how many pushes (in a timeinterval),
192 /***********************************************************************
193 * /Housekeeping with peers
194 ***********************************************************************/
200 /***********************************************************************
202 ***********************************************************************/
205 * Sampler used for the Brahms protocol itself.
207 static struct RPS_Sampler *prot_sampler;
210 * Sampler used for the clients.
212 static struct RPS_Sampler *client_sampler;
215 * Set of all peers to keep track of them.
217 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
221 * Name to log view to
223 static char *file_name_view_log;
226 * The "local view" containing peers we learned from gossip and history
228 static struct GNUNET_CONTAINER_MultiPeerMap *view;
231 * An array containing the peers of the local view.
233 * This is created every time we send a pull reply if it has changed since the
234 * last pull reply we sent.
236 static struct GNUNET_PeerIdentity *view_array;
240 * The size of sampler we need to be able to satisfy the client's need of
243 static unsigned int sampler_size_client_need;
246 * The size of sampler we need to be able to satisfy the Brahms protocol's
247 * need of random peers.
249 * This is one minimum size the sampler grows to.
251 static unsigned int sampler_size_est_need;
255 * Percentage of total peer number in the view
256 * to send random PUSHes to
261 * Percentage of total peer number in the view
262 * to send random PULLs to
267 * The percentage gamma of history updates.
268 * Simply 1 - alpha - beta
273 * Identifier for the main task that runs periodically.
275 static struct GNUNET_SCHEDULER_Task *do_round_task;
278 * Time inverval the do_round task runs in.
280 static struct GNUNET_TIME_Relative round_interval;
285 * List to store peers received through pushes temporary.
287 * TODO -> multipeermap
289 static struct GNUNET_PeerIdentity *push_list;
292 * Size of the push_list;
294 static unsigned int push_list_size;
295 //size_t push_list_size;
298 * List to store peers received through pulls temporary.
300 * TODO -> multipeermap
302 static struct GNUNET_PeerIdentity *pull_list;
305 * Size of the pull_list;
307 static unsigned int pull_list_size;
308 //size_t pull_list_size;
314 static struct GNUNET_NSE_Handle *nse;
319 static struct GNUNET_CADET_Handle *cadet_handle;
322 * Handler to PEERINFO.
324 static struct GNUNET_PEERINFO_Handle *peerinfo_handle;
327 * Handle for cancellation of iteration over peers.
329 struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify_handle;
335 * Only needed in the beginning to check how many of the 64 deltas
338 static unsigned int req_counter;
341 * Time of the last request we received.
343 * Used to compute the expected request rate.
345 static struct GNUNET_TIME_Absolute last_request;
348 * Size of #request_deltas.
350 #define REQUEST_DELTAS_SIZE 64
351 static unsigned int request_deltas_size = REQUEST_DELTAS_SIZE;
354 * Last 64 deltas between requests
356 static struct GNUNET_TIME_Relative request_deltas[REQUEST_DELTAS_SIZE];
359 * The prediction of the rate of requests
361 static struct GNUNET_TIME_Relative request_rate;
365 * Number of history update tasks.
367 uint32_t num_hist_update_tasks;
371 * Closure used to pass the client and the id to the callback
372 * that replies to a client's request
377 * The identifier of the request
382 * The client handle to send the reply to
384 struct GNUNET_SERVER_Client *client;
388 #ifdef ENABLE_MALICIOUS
390 * Type of malicious peer
392 * 0 Don't act malicious at all - Default
393 * 1 Try to maximise representation
394 * 2 Try to partition the network
397 uint32_t mal_type = 0;
400 * Other malicious peers
402 static struct GNUNET_PeerIdentity *mal_peers = NULL;
405 * Hashmap of malicious peers used as set.
406 * Used to more efficiently check whether we know that peer.
408 static struct GNUNET_CONTAINER_MultiPeerMap *mal_peer_set = NULL;
411 * Number of other malicious peers
413 static uint32_t num_mal_peers;
417 * If type is 2 This struct is used to store the attacked peers in a DLL
424 struct AttackedPeer *next;
425 struct AttackedPeer *prev;
430 struct GNUNET_PeerIdentity peer_id;
434 * If type is 2 this is the DLL of attacked peers
436 static struct AttackedPeer *att_peers_head = NULL;
437 static struct AttackedPeer *att_peers_tail = NULL;
440 * This index is used to point to an attacked peer to
441 * implement the round-robin-ish way to select attacked peers.
443 static struct AttackedPeer *att_peer_index = NULL;
446 * Hashmap of attacked peers used as set.
447 * Used to more efficiently check whether we know that peer.
449 static struct GNUNET_CONTAINER_MultiPeerMap *att_peer_set = NULL;
452 * Number of attacked peers
454 static uint32_t num_attacked_peers = 0;
458 * If type is 1 this is the attacked peer
460 static struct GNUNET_PeerIdentity attacked_peer;
463 * The limit of PUSHes we can send in one round.
464 * This is an assumption of the Brahms protocol and either implemented
467 * assumend to be the bandwidth limitation.
469 static uint32_t push_limit = 10000;
470 #endif /* ENABLE_MALICIOUS */
473 /***********************************************************************
475 ***********************************************************************/
482 /***********************************************************************
484 ***********************************************************************/
487 * Set a peer flag of given peer context.
489 #define set_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags |= mask)
492 * Get peer flag of given peer context.
494 #define get_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags & mask ? GNUNET_YES : GNUNET_NO)
497 * Unset flag of given peer context.
499 #define unset_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags &= (~mask))
502 * Clean the send channel of a peer
505 peer_clean (const struct GNUNET_PeerIdentity *peer);
509 * Check if peer is already in peer array.
512 in_arr (const struct GNUNET_PeerIdentity *array,
513 unsigned int arr_size,
514 const struct GNUNET_PeerIdentity *peer)
516 GNUNET_assert (NULL != peer);
521 GNUNET_assert (NULL != array);
525 for (i = 0; i < arr_size ; i++)
526 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&array[i], peer))
533 * Print peerlist to log.
536 print_peer_list (struct GNUNET_PeerIdentity *list, unsigned int len)
540 LOG (GNUNET_ERROR_TYPE_DEBUG,
541 "Printing peer list of length %u at %p:\n",
544 for (i = 0 ; i < len ; i++)
546 LOG (GNUNET_ERROR_TYPE_DEBUG,
548 i, GNUNET_i2s (&list[i]));
554 * Remove peer from list.
557 rem_from_list (struct GNUNET_PeerIdentity **peer_list,
558 unsigned int *list_size,
559 const struct GNUNET_PeerIdentity *peer)
562 struct GNUNET_PeerIdentity *tmp;
566 LOG (GNUNET_ERROR_TYPE_DEBUG,
567 "Removing peer %s from list at %p\n",
571 for ( i = 0 ; i < *list_size ; i++ )
573 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&tmp[i], peer))
575 if (i < *list_size -1)
576 { /* Not at the last entry -- shift peers left */
577 memcpy (&tmp[i], &tmp[i +1],
578 ((*list_size) - i -1) * sizeof (struct GNUNET_PeerIdentity));
580 /* Remove last entry (should be now useless PeerID) */
581 GNUNET_array_grow (tmp, *list_size, (*list_size) -1);
588 * Get random peer from the given list but don't return one from the @a ignore_list.
590 struct GNUNET_PeerIdentity *
591 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list,
593 const struct GNUNET_PeerIdentity *ignore_list,
594 uint32_t ignore_size)
598 struct GNUNET_PeerIdentity *tmp_peer_list;
599 struct GNUNET_PeerIdentity *peer;
601 GNUNET_assert (NULL != peer_list);
606 tmp_peer_list = NULL;
607 GNUNET_array_grow (tmp_peer_list, tmp_size, list_size);
608 memcpy (tmp_peer_list,
610 list_size * sizeof (struct GNUNET_PeerIdentity));
611 peer = GNUNET_new (struct GNUNET_PeerIdentity);
614 * Choose the r_index of the peer we want to return
615 * at random from the interval of the view
617 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
619 *peer = tmp_peer_list[r_index];
621 while (in_arr (ignore_list, ignore_size, peer))
623 rem_from_list (&tmp_peer_list, &tmp_size, peer);
625 print_peer_list (tmp_peer_list, tmp_size);
634 * Choose the r_index of the peer we want to return
635 * at random from the interval of the view
637 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
639 *peer = tmp_peer_list[r_index];
643 GNUNET_array_grow (tmp_peer_list, tmp_size, 0);
650 * Get the context of a peer. If not existing, create.
653 get_peer_ctx (const struct GNUNET_PeerIdentity *peer)
655 struct PeerContext *ctx;
658 ret = GNUNET_CONTAINER_multipeermap_contains (peer_map, peer);
659 GNUNET_assert (GNUNET_YES == ret);
660 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
661 GNUNET_assert (NULL != ctx);
666 * Create a new peer context and insert it into the peer map
669 create_peer_ctx (const struct GNUNET_PeerIdentity *peer)
671 struct PeerContext *ctx;
674 ctx = GNUNET_new (struct PeerContext);
675 ctx->peer_id = *peer;
676 ret = GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
677 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
678 GNUNET_assert (GNUNET_OK == ret);
684 * Put random peer from sampler into the view as history update.
687 hist_update (void *cls, struct GNUNET_PeerIdentity *ids, uint32_t num_peers)
691 for (i = 0; i < GNUNET_MIN (
692 sampler_size_est_need - GNUNET_CONTAINER_multipeermap_size (view),
695 if (GNUNET_OK != GNUNET_CONTAINER_multipeermap_put (view,
698 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
700 LOG (GNUNET_ERROR_TYPE_WARNING,
701 "Failed to put peer in peermap. (hist_update)\n");
704 /* Might want to check that we really updated the view */
705 if (NULL != view_array)
707 GNUNET_free (view_array);
711 to_file (file_name_view_log,
713 GNUNET_i2s_full (ids));
716 if (0 < num_hist_update_tasks)
717 num_hist_update_tasks--;
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 (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
800 const struct GNUNET_PeerIdentity *peer)
802 struct PeerContext *peer_ctx;
804 peer_ctx = get_peer_ctx (peer);
805 if (NULL == peer_ctx->send_channel)
807 LOG (GNUNET_ERROR_TYPE_DEBUG,
808 "Trying to establish channel to peer %s\n",
811 peer_ctx->send_channel =
812 GNUNET_CADET_channel_create (cadet_handle,
815 GNUNET_RPS_CADET_PORT,
816 GNUNET_CADET_OPTION_RELIABLE);
819 return peer_ctx->send_channel;
824 * Get the message queue of a specific peer.
826 * If we already have a message queue open to this client,
827 * simply return it, otherways create one.
829 struct GNUNET_MQ_Handle *
830 get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
831 const struct GNUNET_PeerIdentity *peer_id)
833 struct PeerContext *peer_ctx;
835 peer_ctx = get_peer_ctx (peer_id);
837 GNUNET_assert (NULL == peer_ctx->transmit_handle);
839 if (NULL == peer_ctx->mq)
841 (void) get_channel (peer_map, peer_id);
842 peer_ctx->mq = GNUNET_CADET_mq_create (peer_ctx->send_channel);
843 //do I have to explicitly put it in the peer_map?
844 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer_id, peer_ctx,
845 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
852 * Issue check whether peer is live
854 * @param peer_ctx the context of the peer
857 check_peer_live (struct PeerContext *peer_ctx)
859 LOG (GNUNET_ERROR_TYPE_DEBUG,
860 "Get informed about peer %s getting live\n",
861 GNUNET_i2s (&peer_ctx->peer_id));
863 if (NULL == peer_ctx->transmit_handle &&
864 NULL == peer_ctx->send_channel)
866 (void) get_channel (peer_map, &peer_ctx->peer_id);
867 peer_ctx->transmit_handle =
868 GNUNET_CADET_notify_transmit_ready (peer_ctx->send_channel,
870 GNUNET_TIME_UNIT_FOREVER_REL,
871 sizeof (struct GNUNET_MessageHeader),
872 cadet_ntfy_tmt_rdy_cb,
875 else if (NULL != peer_ctx->transmit_handle)
876 LOG (GNUNET_ERROR_TYPE_DEBUG,
877 "Already waiting for notification\n");
878 else if (NULL != peer_ctx->send_channel)
879 LOG (GNUNET_ERROR_TYPE_DEBUG,
880 "Already have established channel to peer\n");
885 * Sum all time relatives of an array.
887 struct GNUNET_TIME_Relative
888 T_relative_sum (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
890 struct GNUNET_TIME_Relative sum;
893 sum = GNUNET_TIME_UNIT_ZERO;
894 for ( i = 0 ; i < arr_size ; i++ )
896 sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
903 * Compute the average of given time relatives.
905 struct GNUNET_TIME_Relative
906 T_relative_avg (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
908 return GNUNET_TIME_relative_divide (T_relative_sum (rel_array, arr_size), arr_size);
913 * Insert PeerID in #pull_list
915 * Called once we know a peer is live.
918 insert_in_pull_list (void *cls, const struct GNUNET_PeerIdentity *peer)
920 if (GNUNET_NO == in_arr (pull_list, pull_list_size, peer))
921 GNUNET_array_append (pull_list, pull_list_size, *peer);
925 * Check whether #insert_in_pull_list was already scheduled
928 insert_in_pull_list_scheduled (const struct PeerContext *peer_ctx)
932 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
933 if (insert_in_pull_list == peer_ctx->outstanding_ops[i].op)
940 * Insert PeerID in #view
942 * Called once we know a peer is live.
945 insert_in_view (void *cls, const struct GNUNET_PeerIdentity *peer)
947 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_put (view,
950 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
952 LOG (GNUNET_ERROR_TYPE_WARNING,
953 "Failed to put peer into view. (insert_in_view)\n");
956 /* Might want to check whether we really modified the view */
957 if (NULL != view_array)
959 GNUNET_free (view_array);
963 (void) get_channel (peer_map, peer);
967 * Check whether #insert_in_view was already scheduled
970 insert_in_view_scheduled (const struct PeerContext *peer_ctx)
974 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
975 if (insert_in_view == peer_ctx->outstanding_ops[i].op)
982 * Update sampler with given PeerID.
985 insert_in_sampler (void *cls, const struct GNUNET_PeerIdentity *peer)
987 LOG (GNUNET_ERROR_TYPE_DEBUG,
988 "Updating samplers with peer %s from insert_in_sampler()\n",
990 RPS_sampler_update (prot_sampler, peer);
991 RPS_sampler_update (client_sampler, peer);
996 * Check whether #insert_in_sampler was already scheduled
999 insert_in_sampler_scheduled (const struct PeerContext *peer_ctx)
1003 for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
1004 if (insert_in_sampler== peer_ctx->outstanding_ops[i].op)
1011 * Wrapper around #RPS_sampler_resize()
1013 * If we do not have enough sampler elements, double current sampler size
1014 * If we have more than enough sampler elements, halv current sampler size
1017 resize_wrapper (struct RPS_Sampler *sampler, uint32_t new_size)
1019 unsigned int sampler_size;
1022 // TODO respect the min, max
1023 sampler_size = RPS_sampler_get_size (sampler);
1024 if (sampler_size > new_size * 4)
1026 RPS_sampler_resize (sampler, sampler_size / 2);
1028 else if (sampler_size < new_size)
1030 RPS_sampler_resize (sampler, sampler_size * 2);
1032 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
1037 * Wrapper around #RPS_sampler_resize() resizing the client sampler
1040 client_resize_wrapper ()
1042 uint32_t bigger_size;
1046 bigger_size = GNUNET_MAX (sampler_size_est_need, sampler_size_client_need);
1048 // TODO respect the min, max
1049 resize_wrapper (client_sampler, bigger_size);
1050 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size_client is now %" PRIu32 "\n",
1056 * Estimate request rate
1058 * Called every time we receive a request from the client.
1063 struct GNUNET_TIME_Relative max_round_duration;
1065 if (request_deltas_size > req_counter)
1067 if ( 1 < req_counter)
1069 /* Shift last request deltas to the right */
1070 memcpy (&request_deltas[1],
1072 (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
1074 /* Add current delta to beginning */
1076 GNUNET_TIME_absolute_get_difference (last_request,
1077 GNUNET_TIME_absolute_get ());
1078 request_rate = T_relative_avg (request_deltas, req_counter);
1080 /* Compute the duration a round will maximally take */
1081 max_round_duration =
1082 GNUNET_TIME_relative_add (round_interval,
1083 GNUNET_TIME_relative_divide (round_interval, 2));
1085 /* Set the estimated size the sampler has to have to
1086 * satisfy the current client request rate */
1087 sampler_size_client_need =
1088 max_round_duration.rel_value_us / request_rate.rel_value_us;
1090 /* Resize the sampler */
1091 client_resize_wrapper ();
1093 last_request = GNUNET_TIME_absolute_get ();
1098 * Add all peers in @a peer_array to @a peer_map used as set.
1100 * @param peer_array array containing the peers
1101 * @param num_peers number of peers in @peer_array
1102 * @param peer_map the peermap to use as set
1105 add_peer_array_to_set (const struct GNUNET_PeerIdentity *peer_array,
1106 unsigned int num_peers,
1107 struct GNUNET_CONTAINER_MultiPeerMap *peer_map)
1110 if (NULL == peer_map)
1112 LOG (GNUNET_ERROR_TYPE_WARNING,
1113 "Trying to add peers to an empty peermap.\n");
1117 for (i = 0; i < num_peers; i++)
1119 GNUNET_CONTAINER_multipeermap_put (peer_map,
1122 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1128 * Send a PULL REPLY to @a peer_id
1130 * @param peer_id the peer to send the reply to.
1131 * @param peer_ids the peers to send to @a peer_id
1132 * @param num_peer_ids the number of peers to send to @a peer_id
1135 send_pull_reply (const struct GNUNET_PeerIdentity *peer_id,
1136 const struct GNUNET_PeerIdentity *peer_ids,
1137 unsigned int num_peer_ids)
1140 struct GNUNET_MQ_Handle *mq;
1141 struct GNUNET_MQ_Envelope *ev;
1142 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
1144 /* Compute actual size */
1145 send_size = sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) +
1146 num_peer_ids * sizeof (struct GNUNET_PeerIdentity);
1148 if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size)
1149 /* Compute number of peers to send
1150 * If too long, simply truncate */
1151 // TODO select random ones via permutation
1152 // or even better: do good protocol design
1154 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE -
1155 sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1156 sizeof (struct GNUNET_PeerIdentity);
1158 send_size = num_peer_ids;
1160 LOG (GNUNET_ERROR_TYPE_DEBUG,
1161 "PULL REQUEST from peer %s received, going to send %u peers\n",
1162 GNUNET_i2s (peer_id), send_size);
1164 mq = get_mq (peer_map, peer_id);
1166 ev = GNUNET_MQ_msg_extra (out_msg,
1167 send_size * sizeof (struct GNUNET_PeerIdentity),
1168 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
1169 out_msg->num_peers = htonl (send_size);
1170 memcpy (&out_msg[1], peer_ids,
1171 send_size * sizeof (struct GNUNET_PeerIdentity));
1173 GNUNET_MQ_send (mq, ev);
1178 * This function is called on new peer_ids from 'external' sources
1179 * (client seed, cadet get_peers(), ...)
1181 * @param peer_id the new peer_id
1184 new_peer_id (const struct GNUNET_PeerIdentity *peer_id)
1186 struct PeerOutstandingOp out_op;
1187 struct PeerContext *peer_ctx;
1189 if ((NULL == peer_id) ||
1190 (0 == GNUNET_CRYPTO_cmp_peer_identity (&own_identity, peer_id)))
1192 LOG (GNUNET_ERROR_TYPE_DEBUG,
1193 "Got peer_id %s (at %p, view size: %u)\n",
1194 GNUNET_i2s (peer_id),
1196 GNUNET_CONTAINER_multipeermap_size (view));
1198 /* if the seed peer is already know, skip context creation */
1199 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer_id))
1200 peer_ctx = create_peer_ctx (peer_id);
1202 peer_ctx = get_peer_ctx (peer_id);
1204 if (GNUNET_NO == get_peer_flag (peer_ctx, VALID))
1206 if (GNUNET_NO == insert_in_sampler_scheduled (peer_ctx))
1208 out_op.op = insert_in_sampler;
1209 out_op.op_cls = NULL;
1210 GNUNET_array_append (peer_ctx->outstanding_ops,
1211 peer_ctx->num_outstanding_ops,
1215 if (GNUNET_NO == insert_in_view_scheduled (peer_ctx))
1217 out_op.op = insert_in_view;
1218 out_op.op_cls = NULL;
1219 GNUNET_array_append (peer_ctx->outstanding_ops,
1220 peer_ctx->num_outstanding_ops,
1224 /* Trigger livelyness test on peer */
1225 check_peer_live (peer_ctx);
1229 // send push/pull to each of those peers?
1233 /***********************************************************************
1235 ***********************************************************************/
1242 * Function called by NSE.
1244 * Updates sizes of sampler list and view and adapt those lists
1248 nse_callback (void *cls, struct GNUNET_TIME_Absolute timestamp,
1249 double logestimate, double std_dev)
1252 //double scale; // TODO this might go gloabal/config
1254 LOG (GNUNET_ERROR_TYPE_DEBUG,
1255 "Received a ns estimate - logest: %f, std_dev: %f (old_size: %u)\n",
1256 logestimate, std_dev, RPS_sampler_get_size (prot_sampler));
1258 estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
1259 // GNUNET_NSE_log_estimate_to_n (logestimate);
1260 estimate = pow (estimate, 1.0 / 3);
1261 // TODO add if std_dev is a number
1262 // estimate += (std_dev * scale);
1263 if (2 < ceil (estimate))
1265 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
1266 sampler_size_est_need = estimate;
1268 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
1270 /* If the NSE has changed adapt the lists accordingly */
1271 resize_wrapper (prot_sampler, sampler_size_est_need);
1272 client_resize_wrapper ();
1277 * Callback called once the requested PeerIDs are ready.
1279 * Sends those to the requesting client.
1281 void client_respond (void *cls,
1282 struct GNUNET_PeerIdentity *peer_ids, uint32_t num_peers)
1285 struct GNUNET_MQ_Envelope *ev;
1286 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
1287 struct ReplyCls *reply_cls = (struct ReplyCls *) cls;
1288 uint32_t size_needed;
1289 struct ClientContext *cli_ctx;
1291 GNUNET_assert (NULL != reply_cls);
1292 LOG (GNUNET_ERROR_TYPE_DEBUG,
1293 "sampler returned %" PRIu32 " peers:\n",
1295 for (i = 0; i < num_peers; i++)
1297 LOG (GNUNET_ERROR_TYPE_DEBUG,
1300 GNUNET_i2s (&peer_ids[i]));
1303 size_needed = sizeof (struct GNUNET_RPS_CS_ReplyMessage) +
1304 num_peers * sizeof (struct GNUNET_PeerIdentity);
1306 GNUNET_assert (GNUNET_SERVER_MAX_MESSAGE_SIZE >= size_needed);
1308 ev = GNUNET_MQ_msg_extra (out_msg,
1309 num_peers * sizeof (struct GNUNET_PeerIdentity),
1310 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
1311 out_msg->num_peers = htonl (num_peers);
1312 out_msg->id = htonl (reply_cls->id);
1314 memcpy (&out_msg[1],
1316 num_peers * sizeof (struct GNUNET_PeerIdentity));
1317 GNUNET_free (peer_ids);
1319 cli_ctx = GNUNET_SERVER_client_get_user_context (reply_cls->client, struct ClientContext);
1320 if (NULL == cli_ctx) {
1321 cli_ctx = GNUNET_new (struct ClientContext);
1322 cli_ctx->mq = GNUNET_MQ_queue_for_server_client (reply_cls->client);
1323 GNUNET_SERVER_client_set_user_context (reply_cls->client, cli_ctx);
1326 GNUNET_free (reply_cls);
1328 GNUNET_MQ_send (cli_ctx->mq, ev);
1333 * Handle RPS request from the client.
1335 * @param cls closure
1336 * @param client identification of the client
1337 * @param message the actual message
1340 handle_client_request (void *cls,
1341 struct GNUNET_SERVER_Client *client,
1342 const struct GNUNET_MessageHeader *message)
1344 struct GNUNET_RPS_CS_RequestMessage *msg;
1346 uint32_t size_needed;
1347 struct ReplyCls *reply_cls;
1350 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
1352 num_peers = ntohl (msg->num_peers);
1353 size_needed = sizeof (struct GNUNET_RPS_CS_RequestMessage) +
1354 num_peers * sizeof (struct GNUNET_PeerIdentity);
1356 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
1358 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1362 for (i = 0 ; i < num_peers ; i++)
1365 LOG (GNUNET_ERROR_TYPE_DEBUG,
1366 "Client requested %" PRIu32 " random peer(s).\n",
1369 reply_cls = GNUNET_new (struct ReplyCls);
1370 reply_cls->id = ntohl (msg->id);
1371 reply_cls->client = client;
1373 RPS_sampler_get_n_rand_peers (client_sampler,
1378 GNUNET_SERVER_receive_done (client,
1384 * Handle seed from the client.
1386 * @param cls closure
1387 * @param client identification of the client
1388 * @param message the actual message
1391 handle_client_seed (void *cls,
1392 struct GNUNET_SERVER_Client *client,
1393 const struct GNUNET_MessageHeader *message)
1395 struct GNUNET_RPS_CS_SeedMessage *in_msg;
1396 struct GNUNET_PeerIdentity *peers;
1400 if (sizeof (struct GNUNET_RPS_CS_SeedMessage) > ntohs (message->size))
1402 GNUNET_break_op (0);
1403 GNUNET_SERVER_receive_done (client,
1407 in_msg = (struct GNUNET_RPS_CS_SeedMessage *) message;
1408 num_peers = ntohl (in_msg->num_peers);
1409 peers = (struct GNUNET_PeerIdentity *) &in_msg[1];
1410 //peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1411 //memcpy (peers, &in_msg[1], num_peers * sizeof (struct GNUNET_PeerIdentity));
1413 if ((ntohs (message->size) - sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
1414 sizeof (struct GNUNET_PeerIdentity) != num_peers)
1416 GNUNET_break_op (0);
1417 GNUNET_SERVER_receive_done (client,
1422 LOG (GNUNET_ERROR_TYPE_DEBUG,
1423 "Client seeded peers:\n");
1424 print_peer_list (peers, num_peers);
1426 for (i = 0 ; i < num_peers ; i++)
1428 LOG (GNUNET_ERROR_TYPE_DEBUG,
1429 "Updating samplers with seed %" PRIu32 ": %s\n",
1431 GNUNET_i2s (&peers[i]));
1433 new_peer_id (&peers[i]);
1435 //RPS_sampler_update (prot_sampler, &peers[i]);
1436 //RPS_sampler_update (client_sampler, &peers[i]);
1439 ////GNUNET_free (peers);
1441 GNUNET_SERVER_receive_done (client,
1447 * Handle a PUSH message from another peer.
1449 * Check the proof of work and store the PeerID
1450 * in the temporary list for pushed PeerIDs.
1452 * @param cls Closure
1453 * @param channel The channel the PUSH was received over
1454 * @param channel_ctx The context associated with this channel
1455 * @param msg The message header
1458 handle_peer_push (void *cls,
1459 struct GNUNET_CADET_Channel *channel,
1461 const struct GNUNET_MessageHeader *msg)
1463 const struct GNUNET_PeerIdentity *peer;
1465 // (check the proof of work)
1467 peer = (const struct GNUNET_PeerIdentity *)
1468 GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
1469 // FIXME wait for cadet to change this function
1471 LOG (GNUNET_ERROR_TYPE_DEBUG, "PUSH received (%s)\n", GNUNET_i2s (peer));
1473 #ifdef ENABLE_MALICIOUS
1474 struct AttackedPeer *tmp_att_peer;
1476 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1477 memcpy (&tmp_att_peer->peer_id, peer, sizeof (struct GNUNET_PeerIdentity));
1480 { /* Try to maximise representation */
1481 if (NULL == att_peer_set)
1482 att_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
1483 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1486 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1489 add_peer_array_to_set (peer, 1, att_peer_set);
1495 else if (2 == mal_type)
1496 { /* We attack one single well-known peer - simply ignore */
1501 GNUNET_free (tmp_att_peer);
1504 #endif /* ENABLE_MALICIOUS */
1506 /* Add the sending peer to the push_list */
1507 if (GNUNET_NO == in_arr (push_list, push_list_size, peer))
1508 GNUNET_array_append (push_list, push_list_size, *peer);
1510 GNUNET_CADET_receive_done (channel);
1516 * Iterator over hash map entries.
1517 * Called from #generate_view_array and writes every peer id into #view_array.
1519 * @param cls closure - the pointer to the counter
1520 * @param key current public key
1521 * @param value value in the hash map
1522 * @return #GNUNET_YES if we should continue to
1524 * #GNUNET_NO if not.
1527 dump_id_to_view_array (void *cls,
1528 const struct GNUNET_PeerIdentity *key,
1531 unsigned int *i = (unsigned int *) cls;
1533 view_array[(*i)++] = *key;
1539 * Makes sure the view_array is filled with the peer ids currently in #view.
1540 * Called from within #do_round before sending pushes and pulls and from
1541 * #handle_peer_pull_request when a reply is sent.
1544 generate_view_array (unsigned int view_size)
1549 if (NULL == view_array)
1551 view_array = GNUNET_new_array (view_size,
1552 struct GNUNET_PeerIdentity);
1553 i = GNUNET_new (unsigned int);
1556 ret = GNUNET_CONTAINER_multipeermap_iterate (view,
1557 dump_id_to_view_array,
1559 GNUNET_assert (view_size == ret);
1560 GNUNET_assert (view_size == *i);
1568 * Handle PULL REQUEST request message from another peer.
1570 * Reply with the view of PeerIDs.
1572 * @param cls Closure
1573 * @param channel The channel the PULL REQUEST was received over
1574 * @param channel_ctx The context associated with this channel
1575 * @param msg The message header
1578 handle_peer_pull_request (void *cls,
1579 struct GNUNET_CADET_Channel *channel,
1581 const struct GNUNET_MessageHeader *msg)
1583 struct GNUNET_PeerIdentity *peer;
1584 unsigned int view_size;
1586 peer = (struct GNUNET_PeerIdentity *)
1587 GNUNET_CADET_channel_get_info (channel,
1588 GNUNET_CADET_OPTION_PEER);
1589 // FIXME wait for cadet to change this function
1591 LOG (GNUNET_ERROR_TYPE_DEBUG, "PULL REQUEST received (%s)\n", GNUNET_i2s (peer));
1593 #ifdef ENABLE_MALICIOUS
1596 { /* Try to maximise representation */
1597 send_pull_reply (peer, mal_peers, num_mal_peers);
1601 else if (2 == mal_type)
1602 { /* Try to partition network */
1603 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
1605 send_pull_reply (peer, mal_peers, num_mal_peers);
1609 #endif /* ENABLE_MALICIOUS */
1611 view_size = GNUNET_CONTAINER_multipeermap_size (view);
1612 generate_view_array (view_size);
1614 send_pull_reply (peer, view_array, view_size);
1616 GNUNET_CADET_receive_done (channel);
1622 * Handle PULL REPLY message from another peer.
1624 * Check whether we sent a corresponding request and
1625 * whether this reply is the first one.
1627 * @param cls Closure
1628 * @param channel The channel the PUSH was received over
1629 * @param channel_ctx The context associated with this channel
1630 * @param msg The message header
1633 handle_peer_pull_reply (void *cls,
1634 struct GNUNET_CADET_Channel *channel,
1636 const struct GNUNET_MessageHeader *msg)
1638 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
1639 struct GNUNET_PeerIdentity *peers;
1640 struct PeerContext *peer_ctx;
1641 struct GNUNET_PeerIdentity *sender;
1642 struct PeerContext *sender_ctx;
1643 struct PeerOutstandingOp out_op;
1645 #ifdef ENABLE_MALICIOUS
1646 struct AttackedPeer *tmp_att_peer;
1647 #endif /* ENABLE_MALICIOUS */
1649 /* Check for protocol violation */
1650 if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->size))
1652 GNUNET_break_op (0);
1653 return GNUNET_SYSERR;
1656 in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
1657 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1658 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1660 LOG (GNUNET_ERROR_TYPE_ERROR,
1661 "message says it sends %" PRIu32 " peers, have space for %i peers\n",
1662 ntohl (in_msg->num_peers),
1663 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1664 sizeof (struct GNUNET_PeerIdentity));
1665 GNUNET_break_op (0);
1666 return GNUNET_SYSERR;
1669 // Guess simply casting isn't the nicest way...
1670 // FIXME wait for cadet to change this function
1671 sender = (struct GNUNET_PeerIdentity *)
1672 GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
1673 sender_ctx = get_peer_ctx (sender);
1675 LOG (GNUNET_ERROR_TYPE_DEBUG, "PULL REPLY received (%s)\n", GNUNET_i2s (sender));
1677 if (GNUNET_YES != get_peer_flag (sender_ctx, PULL_REPLY_PENDING))
1679 LOG (GNUNET_ERROR_TYPE_WARNING,
1680 "Received a pull reply from a peer we didn't request one from!\n");
1681 GNUNET_break_op (0);
1686 #ifdef ENABLE_MALICIOUS
1687 // We shouldn't even receive pull replies as we're not sending
1690 #endif /* ENABLE_MALICIOUS */
1692 /* Do actual logic */
1693 peers = (struct GNUNET_PeerIdentity *) &in_msg[1];
1695 LOG (GNUNET_ERROR_TYPE_DEBUG,
1696 "PULL REPLY received, got following %u peers:\n",
1697 ntohl (in_msg->num_peers));
1699 for (i = 0 ; i < ntohl (in_msg->num_peers) ; i++)
1701 LOG (GNUNET_ERROR_TYPE_DEBUG,
1704 GNUNET_i2s (&peers[i]));
1706 #ifdef ENABLE_MALICIOUS
1709 { /* Add attacked peer to local list */
1710 // TODO check if we sent a request and this was the first reply
1711 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1713 && GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mal_peer_set,
1715 && 0 != GNUNET_CRYPTO_cmp_peer_identity (&peers[i],
1718 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1719 tmp_att_peer->peer_id = peers[i];
1720 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1723 add_peer_array_to_set (&peers[i], 1, att_peer_set);
1727 #endif /* ENABLE_MALICIOUS */
1728 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity,
1731 peer_ctx = get_peer_ctx (&peers[i]);
1732 if (GNUNET_YES == get_peer_flag (peer_ctx, VALID))
1734 if (GNUNET_NO == in_arr (pull_list, pull_list_size, &peers[i]))
1735 GNUNET_array_append (pull_list, pull_list_size, peers[i]);
1737 else if (GNUNET_NO == insert_in_pull_list_scheduled (peer_ctx))
1739 out_op.op = insert_in_pull_list;
1740 out_op.op_cls = NULL;
1741 GNUNET_array_append (peer_ctx->outstanding_ops,
1742 peer_ctx->num_outstanding_ops,
1744 check_peer_live (peer_ctx);
1749 unset_peer_flag (sender_ctx, PULL_REPLY_PENDING);
1751 GNUNET_CADET_receive_done (channel);
1757 * Compute a random delay.
1758 * A uniformly distributed value between mean + spread and mean - spread.
1760 * For example for mean 4 min and spread 2 the minimum is (4 min - (1/2 * 4 min))
1761 * It would return a random value between 2 and 6 min.
1763 * @param mean the mean
1764 * @param spread the inverse amount of deviation from the mean
1766 static struct GNUNET_TIME_Relative
1767 compute_rand_delay (struct GNUNET_TIME_Relative mean, unsigned int spread)
1769 struct GNUNET_TIME_Relative half_interval;
1770 struct GNUNET_TIME_Relative ret;
1771 unsigned int rand_delay;
1772 unsigned int max_rand_delay;
1776 LOG (GNUNET_ERROR_TYPE_WARNING,
1777 "Not accepting spread of 0\n");
1781 /* Compute random time value between spread * mean and spread * mean */
1782 half_interval = GNUNET_TIME_relative_divide (mean, spread);
1784 max_rand_delay = GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us / mean.rel_value_us * (2/spread);
1786 * Compute random value between (0 and 1) * round_interval
1787 * via multiplying round_interval with a 'fraction' (0 to value)/value
1789 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_rand_delay);
1790 ret = GNUNET_TIME_relative_multiply (mean, rand_delay);
1791 ret = GNUNET_TIME_relative_divide (ret, max_rand_delay);
1792 ret = GNUNET_TIME_relative_add (ret, half_interval);
1794 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ret.rel_value_us)
1795 LOG (GNUNET_ERROR_TYPE_WARNING,
1796 "Returning FOREVER_REL\n");
1803 * Send single pull request
1805 * @param peer_id the peer to send the pull request to.
1808 send_pull_request (struct GNUNET_PeerIdentity *peer_id)
1810 struct GNUNET_MQ_Envelope *ev;
1811 struct GNUNET_MQ_Handle *mq;
1812 struct PeerContext *peer_ctx;
1814 peer_ctx = get_peer_ctx (peer_id);
1815 GNUNET_assert (GNUNET_NO == get_peer_flag (peer_ctx, PULL_REPLY_PENDING));
1816 set_peer_flag (peer_ctx, PULL_REPLY_PENDING);
1818 LOG (GNUNET_ERROR_TYPE_DEBUG,
1819 "Sending PULL request to peer %s of view.\n",
1820 GNUNET_i2s (peer_id));
1822 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
1823 mq = get_mq (peer_map, peer_id);
1824 GNUNET_MQ_send (mq, ev);
1831 * @param peer_id the peer to send the push to.
1834 send_push (struct GNUNET_PeerIdentity *peer_id)
1836 struct GNUNET_MQ_Envelope *ev;
1837 struct GNUNET_MQ_Handle *mq;
1839 LOG (GNUNET_ERROR_TYPE_DEBUG,
1840 "Sending PUSH to peer %s of view.\n",
1841 GNUNET_i2s (peer_id));
1843 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
1844 mq = get_mq (peer_map, peer_id);
1845 GNUNET_MQ_send (mq, ev);
1850 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1853 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1856 #ifdef ENABLE_MALICIOUS
1858 * Turn RPS service to act malicious.
1860 * @param cls Closure
1861 * @param client The client that sent the message
1862 * @param msg The message header
1865 handle_client_act_malicious (void *cls,
1866 struct GNUNET_SERVER_Client *client,
1867 const struct GNUNET_MessageHeader *msg)
1869 struct GNUNET_RPS_CS_ActMaliciousMessage *in_msg;
1870 struct GNUNET_PeerIdentity *peers;
1871 uint32_t num_mal_peers_sent;
1872 uint32_t num_mal_peers_old;
1874 /* Check for protocol violation */
1875 if (sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage) > ntohs (msg->size))
1877 GNUNET_break_op (0);
1880 in_msg = (struct GNUNET_RPS_CS_ActMaliciousMessage *) msg;
1881 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
1882 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1884 LOG (GNUNET_ERROR_TYPE_ERROR,
1885 "message says it sends %" PRIu64 " peers, have space for %i peers\n",
1886 ntohl (in_msg->num_peers),
1887 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
1888 sizeof (struct GNUNET_PeerIdentity));
1889 GNUNET_break_op (0);
1893 /* Do actual logic */
1894 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1895 mal_type = ntohl (in_msg->type);
1897 LOG (GNUNET_ERROR_TYPE_DEBUG,
1898 "Now acting malicious type %" PRIu32 ", got %" PRIu32 " peers.\n",
1900 ntohl (in_msg->num_peers));
1903 { /* Try to maximise representation */
1904 /* Add other malicious peers to those we already know */
1906 num_mal_peers_sent = ntohl (in_msg->num_peers);
1907 num_mal_peers_old = num_mal_peers;
1908 GNUNET_array_grow (mal_peers,
1910 num_mal_peers + num_mal_peers_sent);
1911 memcpy (&mal_peers[num_mal_peers_old],
1913 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1915 /* Add all mal peers to mal_peer_set */
1916 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1920 /* Substitute do_round () with do_mal_round () */
1921 GNUNET_SCHEDULER_cancel (do_round_task);
1922 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1925 else if (2 == mal_type
1927 { /* Try to partition the network */
1928 /* Add other malicious peers to those we already know */
1929 num_mal_peers_sent = ntohl (in_msg->num_peers) - 1;
1930 num_mal_peers_old = num_mal_peers;
1931 GNUNET_array_grow (mal_peers,
1933 num_mal_peers + num_mal_peers_sent);
1934 if (NULL != mal_peers &&
1937 memcpy (&mal_peers[num_mal_peers_old],
1939 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1941 /* Add all mal peers to mal_peer_set */
1942 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1947 /* Store the one attacked peer */
1948 memcpy (&attacked_peer,
1949 &in_msg->attacked_peer,
1950 sizeof (struct GNUNET_PeerIdentity));
1952 LOG (GNUNET_ERROR_TYPE_DEBUG,
1953 "Attacked peer is %s\n",
1954 GNUNET_i2s (&attacked_peer));
1956 /* Substitute do_round () with do_mal_round () */
1957 GNUNET_SCHEDULER_cancel (do_round_task);
1958 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1960 else if (0 == mal_type)
1961 { /* Stop acting malicious */
1962 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
1964 /* Substitute do_mal_round () with do_round () */
1965 GNUNET_SCHEDULER_cancel (do_round_task);
1966 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
1973 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1978 * Send out PUSHes and PULLs maliciously.
1980 * This is executed regylary.
1983 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1985 uint32_t num_pushes;
1987 struct GNUNET_TIME_Relative time_next_round;
1988 struct AttackedPeer *tmp_att_peer;
1989 struct PeerContext *peer_ctx;
1991 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round maliciously type %" PRIu32 ".\n",
1994 GNUNET_assert (mal_type <= 3);
1995 /* Do malicious actions */
1997 { /* Try to maximise representation */
1999 /* The maximum of pushes we're going to send this round */
2000 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit,
2001 num_attacked_peers),
2002 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
2004 LOG (GNUNET_ERROR_TYPE_DEBUG,
2005 "Going to send %" PRIu32 " pushes\n",
2008 /* Send PUSHes to attacked peers */
2009 for (i = 0 ; i < num_pushes ; i++)
2011 if (att_peers_tail == att_peer_index)
2012 att_peer_index = att_peers_head;
2014 att_peer_index = att_peer_index->next;
2016 send_push (&att_peer_index->peer_id);
2019 /* Send PULLs to some peers to learn about additional peers to attack */
2020 tmp_att_peer = att_peer_index;
2021 for (i = 0 ; i < num_pushes * alpha ; i++)
2023 if (att_peers_tail == tmp_att_peer)
2024 tmp_att_peer = att_peers_head;
2026 att_peer_index = tmp_att_peer->next;
2028 send_pull_request (&tmp_att_peer->peer_id);
2033 else if (2 == mal_type)
2035 * Try to partition the network
2036 * Send as many pushes to the attacked peer as possible
2037 * That is one push per round as it will ignore more.
2039 send_push (&attacked_peer);
2044 { /* Combined attack */
2046 /* Send PUSH to attacked peers */
2047 peer_ctx = get_peer_ctx (&attacked_peer);
2048 if (GNUNET_YES == get_peer_flag (peer_ctx, VALID))
2050 LOG (GNUNET_ERROR_TYPE_DEBUG,
2051 "Goding to send push to attacked peer (%s)\n",
2052 GNUNET_i2s (&attacked_peer));
2053 send_push (&attacked_peer);
2056 check_peer_live (peer_ctx);
2058 /* The maximum of pushes we're going to send this round */
2059 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit - 1,
2060 num_attacked_peers),
2061 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
2063 LOG (GNUNET_ERROR_TYPE_DEBUG,
2064 "Going to send %" PRIu32 " pushes\n",
2067 for (i = 0 ; i < num_pushes ; i++)
2069 if (att_peers_tail == att_peer_index)
2070 att_peer_index = att_peers_head;
2072 att_peer_index = att_peer_index->next;
2074 send_push (&att_peer_index->peer_id);
2077 /* Send PULLs to some peers to learn about additional peers to attack */
2078 tmp_att_peer = att_peer_index;
2079 for (i = 0 ; i < num_pushes * alpha ; i++)
2081 if (att_peers_tail == tmp_att_peer)
2082 tmp_att_peer = att_peers_head;
2084 att_peer_index = tmp_att_peer->next;
2086 send_pull_request (&tmp_att_peer->peer_id);
2090 /* Schedule next round */
2091 time_next_round = compute_rand_delay (round_interval, 2);
2093 //do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_mal_round, NULL);
2094 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_mal_round, NULL);
2095 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
2097 #endif /* ENABLE_MALICIOUS */
2101 * Send out PUSHes and PULLs, possibly update #view, samplers.
2103 * This is executed regylary.
2106 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2108 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round.\n");
2111 unsigned int view_size;
2112 unsigned int *permut;
2113 unsigned int a_peers; /* Number of peers we send pushes to */
2114 unsigned int b_peers; /* Number of peers we send pull requests to */
2115 uint32_t first_border;
2116 uint32_t second_border;
2117 struct GNUNET_PeerIdentity peer;
2118 struct PeerContext *peer_ctx;
2120 LOG (GNUNET_ERROR_TYPE_DEBUG,
2121 "Printing view:\n");
2122 to_file (file_name_view_log,
2123 "___ new round ___");
2124 view_size = GNUNET_CONTAINER_multipeermap_size (view);
2125 generate_view_array (view_size);
2126 for (i = 0 ; i < view_size ; i++)
2128 LOG (GNUNET_ERROR_TYPE_DEBUG,
2129 "\t%s\n", GNUNET_i2s (&view_array[i]));
2130 to_file (file_name_view_log,
2132 GNUNET_i2s_full (&view_array[i]));
2136 /* Send pushes and pull requests */
2139 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2140 (unsigned int) view_size);
2143 a_peers = ceil (alpha * view_size);
2145 LOG (GNUNET_ERROR_TYPE_DEBUG,
2146 "Going to send pushes to %u (ceil (%f * %u)) peers.\n",
2147 a_peers, alpha, view_size);
2148 for (i = 0; i < a_peers; i++)
2150 peer = view_array[permut[i]];
2151 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer)) // TODO
2152 { // FIXME if this fails schedule/loop this for later
2157 /* Send PULL requests */
2158 b_peers = ceil (beta * view_size);
2159 first_border = a_peers;
2160 second_border = a_peers + b_peers;
2161 if (second_border > view_size)
2163 first_border = view_size - b_peers;
2164 second_border = view_size;
2166 LOG (GNUNET_ERROR_TYPE_DEBUG,
2167 "Going to send pulls to %u (ceil (%f * %u)) peers.\n",
2168 b_peers, beta, view_size);
2169 for (i = first_border; i < second_border; i++)
2171 peer = view_array[permut[i]];
2172 peer_ctx = get_peer_ctx (&peer);
2173 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer) &&
2174 GNUNET_NO == get_peer_flag (peer_ctx, PULL_REPLY_PENDING)) // TODO
2175 { // FIXME if this fails schedule/loop this for later
2176 send_pull_request (&peer);
2180 GNUNET_free (permut);
2186 /* TODO see how many peers are in push-/pull- list! */
2188 if (push_list_size <= alpha * view_size &&
2189 0 < push_list_size &&
2192 LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the view.\n");
2194 uint32_t final_size;
2195 uint32_t peers_to_clean_size;
2196 struct GNUNET_PeerIdentity *peers_to_clean;
2198 peers_to_clean = NULL;
2199 peers_to_clean_size = 0;
2200 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, view_size);
2201 memcpy (peers_to_clean,
2203 view_size * sizeof (struct GNUNET_PeerIdentity));
2205 /* Seems like recreating is the easiest way of emptying the peermap */
2206 GNUNET_CONTAINER_multipeermap_destroy (view);
2207 view = GNUNET_CONTAINER_multipeermap_create (view_size, GNUNET_NO);
2208 to_file (file_name_view_log,
2211 first_border = GNUNET_MIN (ceil (alpha * sampler_size_est_need),
2213 second_border = first_border +
2214 GNUNET_MIN (floor (beta * sampler_size_est_need),
2216 final_size = second_border +
2217 ceil ((1 - (alpha + beta)) * sampler_size_est_need);
2219 GNUNET_array_grow (view_array, view_size, second_border);
2221 /* Update view with peers received through PUSHes */
2222 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2224 for (i = 0; i < first_border; i++)
2226 view_array[i] = push_list[permut[i]];
2227 GNUNET_CONTAINER_multipeermap_put (view, &push_list[permut[i]], NULL,
2228 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2230 to_file (file_name_view_log,
2232 GNUNET_i2s_full (&view_array[i]));
2233 // TODO change the peer_flags accordingly
2235 GNUNET_free (permut);
2238 /* Update view with peers received through PULLs */
2239 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
2241 for (i = first_border; i < second_border; i++)
2243 view_array[i] = pull_list[permut[i - first_border]];
2244 GNUNET_CONTAINER_multipeermap_put (view,
2245 &pull_list[permut[i - first_border]],
2247 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2249 to_file (file_name_view_log,
2251 GNUNET_i2s_full (&view_array[i]));
2252 // TODO change the peer_flags accordingly
2254 GNUNET_free (permut);
2257 /* Update view with peers from history */
2258 RPS_sampler_get_n_rand_peers (prot_sampler,
2261 final_size - second_border);
2262 num_hist_update_tasks = final_size - second_border;
2263 // TODO change the peer_flags accordingly
2265 for (i = 0; i < view_size; i++)
2266 rem_from_list (&peers_to_clean, &peers_to_clean_size, &view_array[i]);
2268 /* Clean peers that were removed from the view */
2269 for (i = 0; i < peers_to_clean_size; i++)
2271 peer_clean (&peers_to_clean[i]);
2272 to_file (file_name_view_log,
2274 GNUNET_i2s_full (&peers_to_clean[i]));
2277 GNUNET_free (peers_to_clean);
2278 peers_to_clean = NULL;
2282 LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the view.\n");
2284 // TODO independent of that also get some peers from CADET_get_peers()?
2286 LOG (GNUNET_ERROR_TYPE_DEBUG,
2287 "Received %u pushes and %u pulls last round (alpha (%.2f) * view_size (%u) = %.2f)\n",
2294 /* Update samplers */
2295 for (i = 0; i < push_list_size; i++)
2297 LOG (GNUNET_ERROR_TYPE_DEBUG,
2298 "Updating with peer %s from push list\n",
2299 GNUNET_i2s (&push_list[i]));
2300 RPS_sampler_update (prot_sampler, &push_list[i]);
2301 RPS_sampler_update (client_sampler, &push_list[i]);
2302 // TODO set in_flag?
2305 for (i = 0; i < pull_list_size; i++)
2307 LOG (GNUNET_ERROR_TYPE_DEBUG,
2308 "Updating with peer %s from pull list\n",
2309 GNUNET_i2s (&pull_list[i]));
2310 RPS_sampler_update (prot_sampler, &pull_list[i]);
2311 RPS_sampler_update (client_sampler, &pull_list[i]);
2312 // TODO set in_flag?
2316 /* Empty push/pull lists */
2317 GNUNET_array_grow (push_list, push_list_size, 0);
2318 GNUNET_array_grow (pull_list, pull_list_size, 0);
2320 struct GNUNET_TIME_Relative time_next_round;
2322 time_next_round = compute_rand_delay (round_interval, 2);
2324 /* Schedule next round */
2325 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_round, NULL);
2326 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
2331 rps_start (struct GNUNET_SERVER_Handle *server);
2335 * This is called from GNUNET_CADET_get_peers().
2337 * It is called on every peer(ID) that cadet somehow has contact with.
2338 * We use those to initialise the sampler.
2341 init_peer_cb (void *cls,
2342 const struct GNUNET_PeerIdentity *peer,
2343 int tunnel, // "Do we have a tunnel towards this peer?"
2344 unsigned int n_paths, // "Number of known paths towards this peer"
2345 unsigned int best_path) // "How long is the best path?
2346 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
2350 LOG (GNUNET_ERROR_TYPE_DEBUG,
2351 "Got peer_id %s from cadet\n",
2359 * Iterator over peers from peerinfo.
2361 * @param cls closure
2362 * @param peer id of the peer, NULL for last call
2363 * @param hello hello message for the peer (can be NULL)
2364 * @param error message
2367 process_peerinfo_peers (void *cls,
2368 const struct GNUNET_PeerIdentity *peer,
2369 const struct GNUNET_HELLO_Message *hello,
2370 const char *err_msg)
2374 LOG (GNUNET_ERROR_TYPE_DEBUG,
2375 "Got peer_id %s from peerinfo\n",
2383 * Callback used to remove peers from the multipeermap.
2386 peer_remove_cb (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
2388 struct PeerContext *peer_ctx;
2389 const struct GNUNET_CADET_Channel *channel =
2390 (const struct GNUNET_CADET_Channel *) cls;
2392 peer_ctx = (struct PeerContext *) value;
2393 set_peer_flag (peer_ctx, TO_DESTROY);
2395 LOG (GNUNET_ERROR_TYPE_DEBUG,
2396 "Going to clean peer %s\n",
2397 GNUNET_i2s (&peer_ctx->peer_id));
2399 /* If operations are still scheduled for this peer cancel those */
2400 if (0 != peer_ctx->num_outstanding_ops)
2402 GNUNET_array_grow (peer_ctx->outstanding_ops,
2403 peer_ctx->num_outstanding_ops,
2407 /* If we are still waiting for notification whether this peer is live
2408 * cancel the according task */
2409 if (NULL != peer_ctx->transmit_handle)
2411 LOG (GNUNET_ERROR_TYPE_DEBUG,
2412 "Trying to cancle transmit_handle for peer %s\n",
2414 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->transmit_handle);
2415 peer_ctx->transmit_handle = NULL;
2418 unset_peer_flag (peer_ctx, PULL_REPLY_PENDING);
2420 to_file (file_name_view_log,
2421 "-%s\t(cleanup channel, other peer)",
2422 GNUNET_i2s_full (key));
2423 GNUNET_CONTAINER_multipeermap_remove_all (view, key);
2425 /* If there is still a mq destroy it */
2426 if (NULL != peer_ctx->mq)
2428 GNUNET_MQ_destroy (peer_ctx->mq);
2429 peer_ctx->mq = NULL;
2433 /* Remove the send_channel
2434 * This function should be called again from #cleanup_channel (callback
2435 * called on the destruction of channels) and clean up the rest. */
2436 if (NULL != peer_ctx->send_channel &&
2437 channel != peer_ctx->send_channel)
2439 GNUNET_CADET_channel_destroy (peer_ctx->send_channel);
2440 peer_ctx->send_channel = NULL;
2443 /* Remove the recv_channel
2444 * This function should be called again from #cleanup_channel (callback
2445 * called on the destruction of channels) and clean up the rest. */
2446 if (NULL != peer_ctx->recv_channel &&
2447 channel != peer_ctx->recv_channel)
2449 GNUNET_CADET_channel_destroy (peer_ctx->recv_channel);
2450 peer_ctx->recv_channel = NULL;
2453 /* If there is no channel we have to remove the context now */
2454 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_remove_all (peer_map, key))
2455 LOG (GNUNET_ERROR_TYPE_WARNING, "removing peer from peer_map failed\n");
2457 GNUNET_free (peer_ctx);
2464 * Clean the send channel of a peer
2465 * If there is also no channel to receive messages from that peer, remove it
2469 peer_clean (const struct GNUNET_PeerIdentity *peer)
2471 struct PeerContext *peer_ctx;
2472 /* struct GNUNET_CADET_Channel *channel; */
2474 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_contains (view, peer) &&
2475 GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
2477 peer_ctx = get_peer_ctx (peer);
2479 if (NULL == peer_ctx->recv_channel)
2481 peer_remove_cb (NULL, peer, peer_ctx);
2488 * Task run during shutdown.
2494 shutdown_task (void *cls,
2495 const struct GNUNET_SCHEDULER_TaskContext *tc)
2498 LOG (GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
2500 GNUNET_PEERINFO_notify_cancel (peerinfo_notify_handle);
2501 GNUNET_PEERINFO_disconnect (peerinfo_handle);
2503 if (NULL != do_round_task)
2505 GNUNET_SCHEDULER_cancel (do_round_task);
2506 do_round_task = NULL;
2510 if (GNUNET_SYSERR ==
2511 GNUNET_CONTAINER_multipeermap_iterate (peer_map, peer_remove_cb, NULL))
2512 LOG (GNUNET_ERROR_TYPE_WARNING,
2513 "Iterating over peers to disconnect from them was cancelled\n");
2516 GNUNET_NSE_disconnect (nse);
2517 RPS_sampler_destroy (prot_sampler);
2518 RPS_sampler_destroy (client_sampler);
2519 LOG (GNUNET_ERROR_TYPE_DEBUG,
2520 "Size of the peermap: %u\n",
2521 GNUNET_CONTAINER_multipeermap_size (peer_map));
2522 GNUNET_break (0 == GNUNET_CONTAINER_multipeermap_size (peer_map));
2523 GNUNET_CADET_disconnect (cadet_handle);
2524 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
2525 GNUNET_CONTAINER_multipeermap_destroy (view);
2527 GNUNET_array_grow (push_list, push_list_size, 0);
2528 GNUNET_array_grow (pull_list, pull_list_size, 0);
2529 #ifdef ENABLE_MALICIOUS
2530 struct AttackedPeer *tmp_att_peer;
2531 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
2532 if (NULL != mal_peer_set)
2533 GNUNET_CONTAINER_multipeermap_destroy (mal_peer_set);
2534 if (NULL != att_peer_set)
2535 GNUNET_CONTAINER_multipeermap_destroy (att_peer_set);
2536 while (NULL != att_peers_head)
2538 tmp_att_peer = att_peers_head;
2539 GNUNET_CONTAINER_DLL_remove (att_peers_head, att_peers_tail, tmp_att_peer);
2541 #endif /* ENABLE_MALICIOUS */
2546 * A client disconnected. Remove all of its data structure entries.
2548 * @param cls closure, NULL
2549 * @param client identification of the client
2552 handle_client_disconnect (void *cls,
2553 struct GNUNET_SERVER_Client * client)
2559 * Handle the channel a peer opens to us.
2561 * @param cls The closure
2562 * @param channel The channel the peer wants to establish
2563 * @param initiator The peer's peer ID
2564 * @param port The port the channel is being established over
2565 * @param options Further options
2568 handle_inbound_channel (void *cls,
2569 struct GNUNET_CADET_Channel *channel,
2570 const struct GNUNET_PeerIdentity *initiator,
2572 enum GNUNET_CADET_ChannelOption options)
2574 struct PeerContext *peer_ctx;
2576 LOG (GNUNET_ERROR_TYPE_DEBUG,
2577 "New channel was established to us (Peer %s).\n",
2578 GNUNET_i2s (initiator));
2579 GNUNET_assert (NULL != channel); /* according to cadet API */
2580 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, initiator))
2581 peer_ctx = create_peer_ctx (initiator);
2583 peer_ctx = get_peer_ctx (initiator);
2584 /* We only accept one incoming channel from peers */
2585 if (NULL != peer_ctx->recv_channel)
2587 GNUNET_CADET_channel_destroy (channel);
2590 peer_ctx->recv_channel = channel;
2591 peer_is_live (peer_ctx);
2597 * This is called when a channel is destroyed.
2599 * @param cls The closure
2600 * @param channel The channel being closed
2601 * @param channel_ctx The context associated with this channel
2604 cleanup_channel (void *cls,
2605 const struct GNUNET_CADET_Channel *channel,
2608 struct GNUNET_PeerIdentity *peer;
2609 struct PeerContext *peer_ctx;
2611 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
2612 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
2613 // Guess simply casting isn't the nicest way...
2614 // FIXME wait for cadet to change this function
2616 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
2617 {/* We don't want to implicitly create a context that we're about to kill */
2618 peer_ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
2619 if (NULL == peer_ctx) /* It could have been removed by shutdown_task */
2622 if (get_peer_flag (peer_ctx, TO_DESTROY))
2623 {/* We initiatad the destruction of this particular peer */
2624 if (channel == peer_ctx->send_channel)
2625 peer_ctx->send_channel = NULL;
2626 else if (channel == peer_ctx->recv_channel)
2627 peer_ctx->recv_channel = NULL;
2629 to_file (file_name_view_log,
2630 "-%s\t(cleanup channel, ourself)",
2631 GNUNET_i2s_full (peer));
2635 { /* We did not initiate the destruction of this peer */
2636 if (channel == peer_ctx->send_channel)
2637 { /* Something (but us) killd the channel - clean up peer */
2638 LOG (GNUNET_ERROR_TYPE_DEBUG,
2639 "send channel (%s) was destroyed - cleaning up\n",
2641 peer_ctx->send_channel = NULL;
2642 /* Somwewhat {ab,re}use the iterator function */
2643 /* Cast to void is ok, because it's used as void in peer_remove_cb */
2644 (void) peer_remove_cb ((void *) channel, peer, peer_ctx);
2646 else if (channel == peer_ctx->recv_channel)
2647 { /* Other peer doesn't want to send us messages anymore */
2648 LOG (GNUNET_ERROR_TYPE_DEBUG,
2649 "Peer %s destroyed recv channel - cleaning up channel\n",
2651 peer_ctx->recv_channel = NULL;
2655 LOG (GNUNET_ERROR_TYPE_WARNING,
2656 "unknown channel (%s) was destroyed\n",
2663 { /* We don't know a context to that peer */
2664 LOG (GNUNET_ERROR_TYPE_DEBUG,
2665 "channel (%s) without associated context was destroyed\n",
2672 * Actually start the service.
2675 rps_start (struct GNUNET_SERVER_Handle *server)
2677 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
2678 {&handle_client_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
2679 sizeof (struct GNUNET_RPS_CS_RequestMessage)},
2680 {&handle_client_seed, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_SEED, 0},
2681 #ifdef ENABLE_MALICIOUS
2682 {&handle_client_act_malicious, NULL, GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS , 0},
2683 #endif /* ENABLE_MALICIOUS */
2687 GNUNET_SERVER_add_handlers (server, handlers);
2688 GNUNET_SERVER_disconnect_notify (server,
2689 &handle_client_disconnect,
2691 LOG (GNUNET_ERROR_TYPE_INFO, "Ready to receive requests from clients\n");
2694 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
2695 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
2697 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2704 * Process statistics requests.
2706 * @param cls closure
2707 * @param server the initialized server
2708 * @param c configuration to use
2712 struct GNUNET_SERVER_Handle *server,
2713 const struct GNUNET_CONFIGURATION_Handle *c)
2718 // TODO check what this does -- copied from gnunet-boss
2719 // - seems to work as expected
2720 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
2725 GNUNET_CRYPTO_get_peer_identity (cfg, &own_identity); // TODO check return value
2726 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2727 "STARTING SERVICE (rps) for peer [%s]\n",
2728 GNUNET_i2s (&own_identity));
2729 #ifdef ENABLE_MALICIOUS
2730 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2731 "Malicious execution compiled in.\n");
2732 #endif /* ENABLE_MALICIOUS */
2736 /* Get time interval from the configuration */
2737 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
2741 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2742 "RPS", "ROUNDINTERVAL");
2743 GNUNET_SCHEDULER_shutdown ();
2747 /* Get initial size of sampler/view from the configuration */
2749 GNUNET_CONFIGURATION_get_value_number (cfg, "RPS", "INITSIZE",
2750 (long long unsigned int *) &sampler_size_est_need))
2752 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2754 GNUNET_SCHEDULER_shutdown ();
2757 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", sampler_size_est_need);
2760 view = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO);
2762 /* file_name_view_log */
2763 if (GNUNET_OK != GNUNET_DISK_directory_create ("/tmp/rps/"))
2765 LOG (GNUNET_ERROR_TYPE_WARNING,
2766 "Failed to create directory /tmp/rps/\n");
2769 size = (14 + strlen (GNUNET_i2s_full (&own_identity)) + 1) * sizeof (char);
2770 file_name_view_log = GNUNET_malloc (size);
2771 out_size = GNUNET_snprintf (file_name_view_log,
2774 GNUNET_i2s_full (&own_identity));
2775 if (size < out_size ||
2778 LOG (GNUNET_ERROR_TYPE_WARNING,
2779 "Failed to write string to buffer (size: %i, out_size: %i)\n",
2785 /* connect to NSE */
2786 nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
2792 peer_map = GNUNET_CONTAINER_multipeermap_create (sampler_size_est_need, GNUNET_NO);
2795 /* Initialise cadet */
2796 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
2797 {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH ,
2798 sizeof (struct GNUNET_MessageHeader)},
2799 {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
2800 sizeof (struct GNUNET_MessageHeader)},
2801 {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY , 0},
2805 const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
2806 cadet_handle = GNUNET_CADET_connect (cfg,
2808 &handle_inbound_channel,
2813 peerinfo_handle = GNUNET_PEERINFO_connect (cfg);
2815 /* Initialise sampler */
2816 struct GNUNET_TIME_Relative half_round_interval;
2817 struct GNUNET_TIME_Relative max_round_interval;
2819 half_round_interval = GNUNET_TIME_relative_multiply (round_interval, .5);
2820 max_round_interval = GNUNET_TIME_relative_add (round_interval, half_round_interval);
2822 prot_sampler = RPS_sampler_init (sampler_size_est_need, max_round_interval);
2823 client_sampler = RPS_sampler_mod_init (sampler_size_est_need, max_round_interval);
2825 /* Initialise push and pull maps */
2832 num_hist_update_tasks = 0;
2835 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
2836 GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, NULL);
2837 // TODO send push/pull to each of those peers?
2839 peerinfo_notify_handle = GNUNET_PEERINFO_notify (cfg,
2841 process_peerinfo_peers,
2849 * The main function for the rps service.
2851 * @param argc number of arguments from the command line
2852 * @param argv command line arguments
2853 * @return 0 ok, 1 on error
2856 main (int argc, char *const *argv)
2858 return (GNUNET_OK ==
2859 GNUNET_SERVICE_run (argc,
2862 GNUNET_SERVICE_OPTION_NONE,
2863 &run, NULL)) ? 0 : 1;
2866 /* end of gnunet-service-rps.c */