2 This file is part of GNUnet.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file rps/gnunet-service-rps.c
23 * @brief rps service implementation
24 * @author Julius Bünger
27 #include "gnunet_util_lib.h"
28 #include "gnunet_cadet_service.h"
29 #include "gnunet_nse_service.h"
32 #include "gnunet-service-rps_sampler.h"
37 #define LOG(kind, ...) GNUNET_log(kind, __VA_ARGS__)
39 // TODO modify @brief in every file
41 // TODO check for overflows
43 // TODO align message structs
45 // TODO connect to friends
47 // TODO store peers somewhere persistent
49 // TODO blacklist? (-> mal peer detection on top of brahms)
51 // TODO API request_cancel
53 // hist_size_init, hist_size_max
58 static const struct GNUNET_CONFIGURATION_Handle *cfg;
63 static struct GNUNET_PeerIdentity own_identity;
66 struct GNUNET_PeerIdentity *
67 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list, unsigned int size,
68 const struct GNUNET_PeerIdentity *ignore_list, unsigned int ignore_size);
71 /***********************************************************************
72 * Housekeeping with peers
73 ***********************************************************************/
76 * Struct used to store the context of a connected client.
81 * The message queue to communicate with the client.
83 struct GNUNET_MQ_Handle *mq;
87 * Used to keep track in what lists single peerIDs are.
91 PULL_REPLY_PENDING = 0x01,
92 IN_OTHER_GOSSIP_LIST = 0x02, // unneeded?
93 IN_OWN_SAMPLER_LIST = 0x04, // unneeded?
94 IN_OWN_GOSSIP_LIST = 0x08, // unneeded?
97 * We set this bit when we can be sure the other peer is/was live.
104 * Functions of this type can be used to be stored at a peer for later execution.
106 typedef void (* PeerOp) (void *cls, const struct GNUNET_PeerIdentity *peer);
109 * Outstanding operation on peer consisting of callback and closure
111 struct PeerOutstandingOp
126 * Struct used to keep track of other peer's status
128 * This is stored in a multipeermap.
133 * In own gossip/sampler list, in other's gossip/sampler list
138 * Message queue open to client
140 struct GNUNET_MQ_Handle *mq;
143 * Channel open to client.
145 struct GNUNET_CADET_Channel *send_channel;
148 * Channel open from client.
150 struct GNUNET_CADET_Channel *recv_channel; // unneeded?
153 * Array of outstanding operations on this peer.
155 struct PeerOutstandingOp *outstanding_ops;
158 * Number of outstanding operations.
160 unsigned int num_outstanding_ops;
161 //size_t num_outstanding_ops;
164 * Handle to the callback given to cadet_ntfy_tmt_rdy()
166 * To be canceled on shutdown.
168 struct GNUNET_CADET_TransmitHandle *is_live_task;
171 * Identity of the peer
173 struct GNUNET_PeerIdentity peer_id;
176 * This is pobably followed by 'statistical' data (when we first saw
177 * him, how did we get his ID, how many pushes (in a timeinterval),
182 /***********************************************************************
183 * /Housekeeping with peers
184 ***********************************************************************/
190 /***********************************************************************
192 ***********************************************************************/
195 * Sampler used for the Brahms protocol itself.
197 static struct RPS_Sampler *prot_sampler;
200 * Sampler used for the clients.
202 static struct RPS_Sampler *client_sampler;
205 * Set of all peers to keep track of them.
207 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
211 * The gossiped list of peers.
213 static struct GNUNET_PeerIdentity *gossip_list;
216 * Size of the gossiped list
218 //static unsigned int gossip_list_size;
219 static uint32_t gossip_list_size;
223 * The size of sampler we need to be able to satisfy the client's need of
226 static unsigned int sampler_size_client_need;
229 * The size of sampler we need to be able to satisfy the Brahms protocol's
230 * need of random peers.
232 * This is directly taken as the #gossip_list_size on update of the
235 * This is one minimum size the sampler grows to.
237 static unsigned int sampler_size_est_need;
241 * Percentage of total peer number in the gossip list
242 * to send random PUSHes to
247 * Percentage of total peer number in the gossip list
248 * to send random PULLs to
253 * The percentage gamma of history updates.
254 * Simply 1 - alpha - beta
259 * Identifier for the main task that runs periodically.
261 static struct GNUNET_SCHEDULER_Task *do_round_task;
264 * Time inverval the do_round task runs in.
266 static struct GNUNET_TIME_Relative round_interval;
271 * List to store peers received through pushes temporary.
273 * TODO -> multipeermap
275 static struct GNUNET_PeerIdentity *push_list;
278 * Size of the push_list;
280 static unsigned int push_list_size;
281 //size_t push_list_size;
284 * List to store peers received through pulls temporary.
286 * TODO -> multipeermap
288 static struct GNUNET_PeerIdentity *pull_list;
291 * Size of the pull_list;
293 static unsigned int pull_list_size;
294 //size_t pull_list_size;
300 static struct GNUNET_NSE_Handle *nse;
305 static struct GNUNET_CADET_Handle *cadet_handle;
311 * Only needed in the beginning to check how many of the 64 deltas
314 static unsigned int req_counter;
317 * Time of the last request we received.
319 * Used to compute the expected request rate.
321 static struct GNUNET_TIME_Absolute last_request;
324 * Size of #request_deltas.
326 #define REQUEST_DELTAS_SIZE 64
327 static unsigned int request_deltas_size = REQUEST_DELTAS_SIZE;
330 * Last 64 deltas between requests
332 static struct GNUNET_TIME_Relative request_deltas[REQUEST_DELTAS_SIZE];
335 * The prediction of the rate of requests
337 static struct GNUNET_TIME_Relative request_rate;
341 * List with the peers we sent requests to.
343 struct GNUNET_PeerIdentity *pending_pull_reply_list;
346 * Size of #pending_pull_reply_list.
348 uint32_t pending_pull_reply_list_size;
352 * Number of history update tasks.
354 uint32_t num_hist_update_tasks;
358 * Closure used to pass the client and the id to the callback
359 * that replies to a client's request
364 * The identifier of the request
369 * The client handle to send the reply to
371 struct GNUNET_SERVER_Client *client;
375 #ifdef ENABLE_MALICIOUS
377 * Type of malicious peer
379 * 0 Don't act malicious at all - Default
380 * 1 Try to maximise representation
381 * 2 Try to partition the network
384 uint32_t mal_type = 0;
387 * Other malicious peers
389 static struct GNUNET_PeerIdentity *mal_peers = NULL;
392 * Hashmap of malicious peers used as set.
393 * Used to more efficiently check whether we know that peer.
395 static struct GNUNET_CONTAINER_MultiPeerMap *mal_peer_set = NULL;
398 * Number of other malicious peers
400 static uint32_t num_mal_peers = 0;
404 * If type is 2 This struct is used to store the attacked peers in a DLL
411 struct AttackedPeer *next;
412 struct AttackedPeer *prev;
417 struct GNUNET_PeerIdentity peer_id;
421 * If type is 2 this is the DLL of attacked peers
423 static struct AttackedPeer *att_peers_head = NULL;
424 static struct AttackedPeer *att_peers_tail = NULL;
427 * This index is used to point to an attacked peer to
428 * implement the round-robin-ish way to select attacked peers.
430 static struct AttackedPeer *att_peer_index = NULL;
433 * Hashmap of attacked peers used as set.
434 * Used to more efficiently check whether we know that peer.
436 static struct GNUNET_CONTAINER_MultiPeerMap *att_peer_set = NULL;
439 * Number of attacked peers
441 static uint32_t num_attacked_peers = 0;
445 * If type is 1 this is the attacked peer
447 static struct GNUNET_PeerIdentity attacked_peer;
450 * The limit of PUSHes we can send in one round.
451 * This is an assumption of the Brahms protocol and either implemented
454 * assumend to be the bandwidth limitation.
456 static uint32_t push_limit = 10000;
457 #endif /* ENABLE_MALICIOUS */
460 /***********************************************************************
462 ***********************************************************************/
469 /***********************************************************************
471 ***********************************************************************/
474 * Set a peer flag of given peer context.
476 #define set_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags |= mask)
479 * Get peer flag of given peer context.
481 #define get_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags & mask ? GNUNET_YES : GNUNET_NO)
484 * Unset flag of given peer context.
486 #define unset_peer_flag(peer_ctx, mask) (peer_ctx->peer_flags &= (~mask))
489 * Clean the send channel of a peer
492 peer_clean (const struct GNUNET_PeerIdentity *peer);
496 * Check if peer is already in peer array.
499 in_arr (const struct GNUNET_PeerIdentity *array,
500 unsigned int arr_size,
501 const struct GNUNET_PeerIdentity *peer)
503 GNUNET_assert (NULL != peer);
508 GNUNET_assert (NULL != array);
512 for (i = 0; i < arr_size ; i++)
513 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&array[i], peer))
520 * Print peerlist to log.
523 print_peer_list (struct GNUNET_PeerIdentity *list, unsigned int len)
527 LOG (GNUNET_ERROR_TYPE_DEBUG,
528 "Printing peer list of length %u at %p:\n",
531 for (i = 0 ; i < len ; i++)
533 LOG (GNUNET_ERROR_TYPE_DEBUG,
535 i, GNUNET_i2s (&list[i]));
541 * Remove peer from list.
544 rem_from_list (struct GNUNET_PeerIdentity **peer_list,
545 unsigned int *list_size,
546 const struct GNUNET_PeerIdentity *peer)
549 struct GNUNET_PeerIdentity *tmp;
553 LOG (GNUNET_ERROR_TYPE_DEBUG,
554 "Removing peer %s from list at %p\n",
558 for ( i = 0 ; i < *list_size ; i++ )
560 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&tmp[i], peer))
562 if (i < *list_size -1)
563 { /* Not at the last entry -- shift peers left */
564 memcpy (&tmp[i], &tmp[i +1],
565 ((*list_size) - i -1) * sizeof (struct GNUNET_PeerIdentity));
567 /* Remove last entry (should be now useless PeerID) */
568 GNUNET_array_grow (tmp, *list_size, (*list_size) -1);
575 * Get random peer from the given list but don't return one from the @a ignore_list.
577 struct GNUNET_PeerIdentity *
578 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list,
580 const struct GNUNET_PeerIdentity *ignore_list,
581 uint32_t ignore_size)
585 struct GNUNET_PeerIdentity *tmp_peer_list;
586 struct GNUNET_PeerIdentity *peer;
588 GNUNET_assert (NULL != peer_list);
593 tmp_peer_list = NULL;
594 GNUNET_array_grow (tmp_peer_list, tmp_size, list_size);
595 memcpy (tmp_peer_list,
597 list_size * sizeof (struct GNUNET_PeerIdentity));
598 peer = GNUNET_new (struct GNUNET_PeerIdentity);
601 * Choose the r_index of the peer we want to return
602 * at random from the interval of the gossip list
604 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
606 *peer = tmp_peer_list[r_index];
608 while (in_arr (ignore_list, ignore_size, peer))
610 rem_from_list (&tmp_peer_list, &tmp_size, peer);
612 print_peer_list (tmp_peer_list, tmp_size);
621 * Choose the r_index of the peer we want to return
622 * at random from the interval of the gossip list
624 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
626 *peer = tmp_peer_list[r_index];
630 GNUNET_array_grow (tmp_peer_list, tmp_size, 0);
637 * Get the context of a peer. If not existing, create.
640 get_peer_ctx (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
641 const struct GNUNET_PeerIdentity *peer)
643 struct PeerContext *ctx;
645 if ( GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
647 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
651 ctx = GNUNET_new (struct PeerContext);
654 ctx->send_channel = NULL;
655 ctx->recv_channel = NULL;
656 ctx->outstanding_ops = NULL;
657 ctx->num_outstanding_ops = 0;
658 ctx->is_live_task = NULL;
659 ctx->peer_id = *peer;
660 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
661 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
668 * Put random peer from sampler into the gossip list as history update.
671 hist_update (void *cls, struct GNUNET_PeerIdentity *ids, uint32_t num_peers)
673 GNUNET_assert (1 == num_peers);
675 if (gossip_list_size < sampler_size_est_need)
676 GNUNET_array_append (gossip_list, gossip_list_size, *ids);
678 if (0 < num_hist_update_tasks)
679 num_hist_update_tasks--;
684 * Set the peer flag to living and call the outstanding operations on this peer.
687 peer_is_live (struct PeerContext *peer_ctx)
689 struct GNUNET_PeerIdentity *peer;
691 /* Cancle is_live_task if still scheduled */
692 if (NULL != peer_ctx->is_live_task)
694 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
695 peer_ctx->is_live_task = NULL;
698 peer = &peer_ctx->peer_id;
699 set_peer_flag (peer_ctx, VALID);
701 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer %s is live\n", GNUNET_i2s (peer));
703 if (0 < peer_ctx->num_outstanding_ops)
704 { /* Call outstanding operations */
707 for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
708 peer_ctx->outstanding_ops[i].op (peer_ctx->outstanding_ops[i].op_cls, peer);
709 GNUNET_array_grow (peer_ctx->outstanding_ops, peer_ctx->num_outstanding_ops, 0);
717 * Callback that is called when a channel was effectively established.
718 * This is given to ntfy_tmt_rdy and called when the channel was
719 * successfully established.
722 cadet_ntfy_tmt_rdy_cb (void *cls, size_t size, void *buf)
724 struct PeerContext *peer_ctx = (struct PeerContext *) cls;
726 peer_ctx->is_live_task = NULL;
727 LOG (GNUNET_ERROR_TYPE_DEBUG,
728 "Set ->is_live_task = NULL for peer %s\n",
729 GNUNET_i2s (&peer_ctx->peer_id));
734 peer_is_live (peer_ctx);
738 LOG (GNUNET_ERROR_TYPE_WARNING,
739 "Problems establishing a connection to peer %s in order to check liveliness\n",
740 GNUNET_i2s (&peer_ctx->peer_id));
741 // TODO reschedule? cleanup?
744 //if (NULL != peer_ctx->is_live_task)
746 // LOG (GNUNET_ERROR_TYPE_DEBUG,
747 // "Trying to cancle is_live_task for peer %s\n",
748 // GNUNET_i2s (&peer_ctx->peer_id));
749 // GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
750 // peer_ctx->is_live_task = NULL;
758 * Get the channel of a peer. If not existing, create.
760 struct GNUNET_CADET_Channel *
761 get_channel (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
762 const struct GNUNET_PeerIdentity *peer)
764 struct PeerContext *peer_ctx;
766 peer_ctx = get_peer_ctx (peer_map, peer);
768 if (NULL == peer_ctx->send_channel)
770 LOG (GNUNET_ERROR_TYPE_DEBUG,
771 "Trying to establish channel to peer %s\n",
774 peer_ctx->send_channel =
775 GNUNET_CADET_channel_create (cadet_handle,
778 GNUNET_RPS_CADET_PORT,
779 GNUNET_CADET_OPTION_RELIABLE);
781 // do I have to explicitly put it in the peer_map?
782 (void) GNUNET_CONTAINER_multipeermap_put
786 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
788 return peer_ctx->send_channel;
793 * Get the message queue of a specific peer.
795 * If we already have a message queue open to this client,
796 * simply return it, otherways create one.
798 struct GNUNET_MQ_Handle *
799 get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
800 const struct GNUNET_PeerIdentity *peer_id)
802 struct PeerContext *peer_ctx;
804 peer_ctx = get_peer_ctx (peer_map, peer_id);
806 GNUNET_assert (NULL == peer_ctx->is_live_task);
808 if (NULL == peer_ctx->mq)
810 (void) get_channel (peer_map, peer_id);
811 peer_ctx->mq = GNUNET_CADET_mq_create (peer_ctx->send_channel);
812 //do I have to explicitly put it in the peer_map?
813 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer_id, peer_ctx,
814 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
821 * Issue check whether peer is live
823 * @param peer_ctx the context of the peer
826 check_peer_live (struct PeerContext *peer_ctx)
828 (void) get_channel (peer_map, &peer_ctx->peer_id);
829 LOG (GNUNET_ERROR_TYPE_DEBUG,
830 "Get informed about peer %s getting live\n",
831 GNUNET_i2s (&peer_ctx->peer_id));
832 if (NULL == peer_ctx->is_live_task)
834 peer_ctx->is_live_task =
835 GNUNET_CADET_notify_transmit_ready (peer_ctx->send_channel,
837 GNUNET_TIME_UNIT_FOREVER_REL,
838 sizeof (struct GNUNET_MessageHeader),
839 cadet_ntfy_tmt_rdy_cb,
844 LOG (GNUNET_ERROR_TYPE_DEBUG,
845 "Already waiting for notification\n");
851 * Sum all time relatives of an array.
853 struct GNUNET_TIME_Relative
854 T_relative_sum (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
856 struct GNUNET_TIME_Relative sum;
859 sum = GNUNET_TIME_UNIT_ZERO;
860 for ( i = 0 ; i < arr_size ; i++ )
862 sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
869 * Compute the average of given time relatives.
871 struct GNUNET_TIME_Relative
872 T_relative_avg (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
874 return GNUNET_TIME_relative_divide (T_relative_sum (rel_array, arr_size), arr_size);
879 * Insert PeerID in #pull_list
881 * Called once we know a peer is live.
884 insert_in_pull_list (void *cls, const struct GNUNET_PeerIdentity *peer)
886 if (GNUNET_NO == in_arr (pull_list, pull_list_size, peer))
887 GNUNET_array_append (pull_list, pull_list_size, *peer);
893 * Check whether #insert_in_pull_list was already scheduled
896 insert_in_pull_list_scheduled (const struct PeerContext *peer_ctx)
900 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
901 if (insert_in_pull_list == peer_ctx->outstanding_ops[i].op)
908 * Insert PeerID in #gossip_list
910 * Called once we know a peer is live.
913 insert_in_gossip_list (void *cls, const struct GNUNET_PeerIdentity *peer)
915 if (GNUNET_NO == in_arr (gossip_list, gossip_list_size, peer))
916 GNUNET_array_append (gossip_list, gossip_list_size, *peer);
918 (void) get_channel (peer_map, peer);
922 * Check whether #insert_in_gossip_list was already scheduled
925 insert_in_gossip_list_scheduled (const struct PeerContext *peer_ctx)
929 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
930 if (insert_in_gossip_list == peer_ctx->outstanding_ops[i].op)
937 * Update sampler with given PeerID.
940 insert_in_sampler (void *cls, const struct GNUNET_PeerIdentity *peer)
942 LOG (GNUNET_ERROR_TYPE_DEBUG,
943 "Updating samplers with peer %s from insert_in_sampler()\n",
945 RPS_sampler_update (prot_sampler, peer);
946 RPS_sampler_update (client_sampler, peer);
951 * Check whether #insert_in_sampler was already scheduled
954 insert_in_sampler_scheduled (const struct PeerContext *peer_ctx)
958 for (i = 0 ; i < peer_ctx->num_outstanding_ops ; i++)
959 if (insert_in_sampler== peer_ctx->outstanding_ops[i].op)
966 * Wrapper around #RPS_sampler_resize()
968 * If we do not have enough sampler elements, double current sampler size
969 * If we have more than enough sampler elements, halv current sampler size
972 resize_wrapper (struct RPS_Sampler *sampler, uint32_t new_size)
974 unsigned int sampler_size;
977 // TODO respect the min, max
978 sampler_size = RPS_sampler_get_size (sampler);
979 if (sampler_size > new_size * 4)
981 RPS_sampler_resize (sampler, sampler_size / 2);
983 else if (sampler_size < new_size)
985 RPS_sampler_resize (sampler, sampler_size * 2);
987 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
992 * Wrapper around #RPS_sampler_resize() resizing the client sampler
995 client_resize_wrapper ()
997 uint32_t bigger_size;
998 unsigned int sampler_size;
1002 sampler_size = RPS_sampler_get_size (client_sampler);
1004 if (sampler_size_est_need > sampler_size_client_need)
1005 bigger_size = sampler_size_est_need;
1007 bigger_size = sampler_size_client_need;
1009 // TODO respect the min, max
1010 resize_wrapper (client_sampler, bigger_size);
1011 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
1016 * Estimate request rate
1018 * Called every time we receive a request from the client.
1023 struct GNUNET_TIME_Relative max_round_duration;
1025 if (request_deltas_size > req_counter)
1027 if ( 1 < req_counter)
1029 /* Shift last request deltas to the right */
1030 memcpy (&request_deltas[1],
1032 (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
1034 /* Add current delta to beginning */
1036 GNUNET_TIME_absolute_get_difference (last_request,
1037 GNUNET_TIME_absolute_get ());
1038 request_rate = T_relative_avg (request_deltas, req_counter);
1040 /* Compute the duration a round will maximally take */
1041 max_round_duration =
1042 GNUNET_TIME_relative_add (round_interval,
1043 GNUNET_TIME_relative_divide (round_interval, 2));
1045 /* Set the estimated size the sampler has to have to
1046 * satisfy the current client request rate */
1047 sampler_size_client_need =
1048 max_round_duration.rel_value_us / request_rate.rel_value_us;
1050 /* Resize the sampler */
1051 client_resize_wrapper ();
1053 last_request = GNUNET_TIME_absolute_get ();
1058 * Add all peers in @a peer_array to @a peer_map used as set.
1060 * @param peer_array array containing the peers
1061 * @param num_peers number of peers in @peer_array
1062 * @param peer_map the peermap to use as set
1065 add_peer_array_to_set (const struct GNUNET_PeerIdentity *peer_array,
1066 unsigned int num_peers,
1067 struct GNUNET_CONTAINER_MultiPeerMap *peer_map)
1070 if (NULL == peer_map)
1071 peer_map = GNUNET_CONTAINER_multipeermap_create (num_peers + 1,
1073 for (i = 0 ; i < num_peers ; i++)
1075 GNUNET_CONTAINER_multipeermap_put (peer_map,
1078 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1084 * Send a PULL REPLY to @a peer_id
1086 * @param peer_id the peer to send the reply to.
1087 * @param peer_ids the peers to send to @a peer_id
1088 * @param num_peer_ids the number of peers to send to @a peer_id
1091 send_pull_reply (const struct GNUNET_PeerIdentity *peer_id,
1092 const struct GNUNET_PeerIdentity *peer_ids,
1093 unsigned int num_peer_ids)
1096 struct GNUNET_MQ_Handle *mq;
1097 struct GNUNET_MQ_Envelope *ev;
1098 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
1100 /* Compute actual size */
1101 send_size = sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) +
1102 num_peer_ids * sizeof (struct GNUNET_PeerIdentity);
1104 if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size)
1105 /* Compute number of peers to send
1106 * If too long, simply truncate */
1107 // TODO select random ones via permutation
1108 // or even better: do good protocol design
1110 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE -
1111 sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1112 sizeof (struct GNUNET_PeerIdentity);
1114 send_size = num_peer_ids;
1116 LOG (GNUNET_ERROR_TYPE_DEBUG,
1117 "PULL REQUEST from peer %s received, going to send %u peers\n",
1118 GNUNET_i2s (peer_id), send_size);
1120 mq = get_mq (peer_map, peer_id);
1122 ev = GNUNET_MQ_msg_extra (out_msg,
1123 send_size * sizeof (struct GNUNET_PeerIdentity),
1124 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
1125 out_msg->num_peers = htonl (send_size);
1126 memcpy (&out_msg[1], peer_ids,
1127 send_size * sizeof (struct GNUNET_PeerIdentity));
1129 GNUNET_MQ_send (mq, ev);
1134 * This function is called on new peer_ids from 'external' sources
1135 * (client seed, cadet get_peers(), ...)
1137 * @param peer_id the new peer_id
1140 new_peer_id (const struct GNUNET_PeerIdentity *peer_id)
1142 struct PeerOutstandingOp out_op;
1143 struct PeerContext *peer_ctx;
1146 && 0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, peer_id))
1148 LOG (GNUNET_ERROR_TYPE_DEBUG,
1149 "Got peer_id %s (at %p, gossip_list_size: %u)\n",
1150 GNUNET_i2s (peer_id),
1154 peer_ctx = get_peer_ctx (peer_map, peer_id);
1155 if (GNUNET_YES != get_peer_flag (peer_ctx, VALID))
1157 if (GNUNET_NO == insert_in_sampler_scheduled (peer_ctx))
1159 out_op.op = insert_in_sampler;
1160 out_op.op_cls = NULL;
1161 GNUNET_array_append (peer_ctx->outstanding_ops,
1162 peer_ctx->num_outstanding_ops,
1166 if (GNUNET_NO == insert_in_gossip_list_scheduled (peer_ctx))
1168 out_op.op = insert_in_gossip_list;
1169 out_op.op_cls = NULL;
1170 GNUNET_array_append (peer_ctx->outstanding_ops,
1171 peer_ctx->num_outstanding_ops,
1175 /* Trigger livelyness test on peer */
1176 check_peer_live (peer_ctx);
1180 // send push/pull to each of those peers?
1185 /***********************************************************************
1187 ***********************************************************************/
1194 * Function called by NSE.
1196 * Updates sizes of sampler list and gossip list and adapt those lists
1200 nse_callback (void *cls, struct GNUNET_TIME_Absolute timestamp,
1201 double logestimate, double std_dev)
1204 //double scale; // TODO this might go gloabal/config
1206 LOG (GNUNET_ERROR_TYPE_DEBUG,
1207 "Received a ns estimate - logest: %f, std_dev: %f (old_size: %u)\n",
1208 logestimate, std_dev, RPS_sampler_get_size (prot_sampler));
1210 estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
1211 // GNUNET_NSE_log_estimate_to_n (logestimate);
1212 estimate = pow (estimate, 1.0 / 3);
1213 // TODO add if std_dev is a number
1214 // estimate += (std_dev * scale);
1215 if (2 < ceil (estimate))
1217 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
1218 sampler_size_est_need = estimate;
1220 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
1222 /* If the NSE has changed adapt the lists accordingly */
1223 resize_wrapper (prot_sampler, sampler_size_est_need);
1224 client_resize_wrapper ();
1229 * Callback called once the requested PeerIDs are ready.
1231 * Sends those to the requesting client.
1233 void client_respond (void *cls,
1234 struct GNUNET_PeerIdentity *peer_ids, uint32_t num_peers)
1236 struct GNUNET_MQ_Envelope *ev;
1237 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
1238 struct ReplyCls *reply_cls = (struct ReplyCls *) cls;
1239 uint32_t size_needed;
1240 struct ClientContext *cli_ctx;
1242 LOG (GNUNET_ERROR_TYPE_DEBUG,
1243 "sampler returned %" PRIu32 " peers\n",
1246 size_needed = sizeof (struct GNUNET_RPS_CS_ReplyMessage) +
1247 num_peers * sizeof (struct GNUNET_PeerIdentity);
1249 GNUNET_assert (GNUNET_SERVER_MAX_MESSAGE_SIZE >= size_needed);
1251 ev = GNUNET_MQ_msg_extra (out_msg,
1252 num_peers * sizeof (struct GNUNET_PeerIdentity),
1253 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
1254 out_msg->num_peers = htonl (num_peers);
1255 out_msg->id = htonl (reply_cls->id);
1257 memcpy (&out_msg[1],
1259 num_peers * sizeof (struct GNUNET_PeerIdentity));
1260 GNUNET_free (peer_ids);
1262 cli_ctx = GNUNET_SERVER_client_get_user_context (reply_cls->client, struct ClientContext);
1263 if (NULL == cli_ctx) {
1264 cli_ctx = GNUNET_new (struct ClientContext);
1265 cli_ctx->mq = GNUNET_MQ_queue_for_server_client (reply_cls->client);
1266 GNUNET_SERVER_client_set_user_context (reply_cls->client, cli_ctx);
1269 GNUNET_free (reply_cls);
1271 GNUNET_MQ_send (cli_ctx->mq, ev);
1276 * Handle RPS request from the client.
1278 * @param cls closure
1279 * @param client identification of the client
1280 * @param message the actual message
1283 handle_client_request (void *cls,
1284 struct GNUNET_SERVER_Client *client,
1285 const struct GNUNET_MessageHeader *message)
1287 struct GNUNET_RPS_CS_RequestMessage *msg;
1289 uint32_t size_needed;
1290 struct ReplyCls *reply_cls;
1293 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
1295 num_peers = ntohl (msg->num_peers);
1296 size_needed = sizeof (struct GNUNET_RPS_CS_RequestMessage) +
1297 num_peers * sizeof (struct GNUNET_PeerIdentity);
1299 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
1301 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1305 for (i = 0 ; i < num_peers ; i++)
1308 LOG (GNUNET_ERROR_TYPE_DEBUG,
1309 "Client requested %" PRIu32 " random peer(s).\n",
1312 reply_cls = GNUNET_new (struct ReplyCls);
1313 reply_cls->id = ntohl (msg->id);
1314 reply_cls->client = client;
1316 RPS_sampler_get_n_rand_peers (client_sampler,
1322 GNUNET_SERVER_receive_done (client,
1328 * Handle seed from the client.
1330 * @param cls closure
1331 * @param client identification of the client
1332 * @param message the actual message
1335 handle_client_seed (void *cls,
1336 struct GNUNET_SERVER_Client *client,
1337 const struct GNUNET_MessageHeader *message)
1339 struct GNUNET_RPS_CS_SeedMessage *in_msg;
1340 struct GNUNET_PeerIdentity *peers;
1344 if (sizeof (struct GNUNET_RPS_CS_SeedMessage) > ntohs (message->size))
1346 GNUNET_break_op (0);
1347 GNUNET_SERVER_receive_done (client,
1351 in_msg = (struct GNUNET_RPS_CS_SeedMessage *) message;
1352 num_peers = ntohl (in_msg->num_peers);
1353 peers = (struct GNUNET_PeerIdentity *) &in_msg[1];
1354 //peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
1355 //memcpy (peers, &in_msg[1], num_peers * sizeof (struct GNUNET_PeerIdentity));
1357 if ((ntohs (message->size) - sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
1358 sizeof (struct GNUNET_PeerIdentity) != num_peers)
1360 GNUNET_break_op (0);
1361 GNUNET_SERVER_receive_done (client,
1365 LOG (GNUNET_ERROR_TYPE_DEBUG,
1366 "Client seeded peers:\n");
1367 print_peer_list (peers, num_peers);
1369 for (i = 0 ; i < num_peers ; i++)
1371 LOG (GNUNET_ERROR_TYPE_DEBUG,
1372 "Updating samplers with seed %" PRIu32 ": %s\n",
1374 GNUNET_i2s (&peers[i]));
1376 new_peer_id (&peers[i]);
1378 //RPS_sampler_update (prot_sampler, &peers[i]);
1379 //RPS_sampler_update (client_sampler, &peers[i]);
1382 ////GNUNET_free (peers);
1384 GNUNET_SERVER_receive_done (client,
1390 * Handle a PUSH message from another peer.
1392 * Check the proof of work and store the PeerID
1393 * in the temporary list for pushed PeerIDs.
1395 * @param cls Closure
1396 * @param channel The channel the PUSH was received over
1397 * @param channel_ctx The context associated with this channel
1398 * @param msg The message header
1401 handle_peer_push (void *cls,
1402 struct GNUNET_CADET_Channel *channel,
1404 const struct GNUNET_MessageHeader *msg)
1406 const struct GNUNET_PeerIdentity *peer;
1408 // (check the proof of work)
1410 peer = (const struct GNUNET_PeerIdentity *)
1411 GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
1412 // FIXME wait for cadet to change this function
1414 LOG (GNUNET_ERROR_TYPE_DEBUG, "PUSH received (%s)\n", GNUNET_i2s (peer));
1416 #ifdef ENABLE_MALICIOUS
1417 struct AttackedPeer *tmp_att_peer;
1419 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1420 memcpy (&tmp_att_peer->peer_id, peer, sizeof (struct GNUNET_PeerIdentity));
1423 { /* Try to maximise representation */
1424 if (NULL == att_peer_set)
1425 att_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO);
1426 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1429 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1432 add_peer_array_to_set (peer, 1, att_peer_set);
1438 else if (2 == mal_type)
1439 { /* We attack one single well-known peer - simply ignore */
1443 #endif /* ENABLE_MALICIOUS */
1445 /* Add the sending peer to the push_list */
1446 if (GNUNET_NO == in_arr (push_list, push_list_size, peer))
1447 GNUNET_array_append (push_list, push_list_size, *peer);
1454 * Handle PULL REQUEST request message from another peer.
1456 * Reply with the gossip list of PeerIDs.
1458 * @param cls Closure
1459 * @param channel The channel the PUSH was received over
1460 * @param channel_ctx The context associated with this channel
1461 * @param msg The message header
1464 handle_peer_pull_request (void *cls,
1465 struct GNUNET_CADET_Channel *channel,
1467 const struct GNUNET_MessageHeader *msg)
1469 struct GNUNET_PeerIdentity *peer;
1471 peer = (struct GNUNET_PeerIdentity *)
1472 GNUNET_CADET_channel_get_info (channel,
1473 GNUNET_CADET_OPTION_PEER);
1474 // FIXME wait for cadet to change this function
1476 #ifdef ENABLE_MALICIOUS
1479 { /* Try to maximise representation */
1480 send_pull_reply (peer, mal_peers, num_mal_peers);
1484 else if (2 == mal_type)
1485 { /* Try to partition network */
1486 if (GNUNET_YES == GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
1488 send_pull_reply (peer, mal_peers, num_mal_peers);
1492 #endif /* ENABLE_MALICIOUS */
1494 send_pull_reply (peer, gossip_list, gossip_list_size);
1501 * Handle PULL REPLY message from another peer.
1503 * Check whether we sent a corresponding request and
1504 * whether this reply is the first one.
1506 * @param cls Closure
1507 * @param channel The channel the PUSH was received over
1508 * @param channel_ctx The context associated with this channel
1509 * @param msg The message header
1512 handle_peer_pull_reply (void *cls,
1513 struct GNUNET_CADET_Channel *channel,
1515 const struct GNUNET_MessageHeader *msg)
1517 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
1518 struct GNUNET_PeerIdentity *peers;
1519 struct PeerContext *peer_ctx;
1520 struct GNUNET_PeerIdentity *sender;
1521 struct PeerContext *sender_ctx;
1522 struct PeerOutstandingOp out_op;
1524 #ifdef ENABLE_MALICIOUS
1525 struct AttackedPeer *tmp_att_peer;
1526 #endif /* ENABLE_MALICIOUS */
1528 /* Check for protocol violation */
1529 if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->size))
1531 GNUNET_break_op (0);
1532 return GNUNET_SYSERR;
1535 in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
1536 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1537 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1539 LOG (GNUNET_ERROR_TYPE_ERROR,
1540 "message says it sends %" PRIu64 " peers, have space for %i peers\n",
1541 ntohl (in_msg->num_peers),
1542 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1543 sizeof (struct GNUNET_PeerIdentity));
1544 GNUNET_break_op (0);
1545 return GNUNET_SYSERR;
1548 sender = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
1549 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
1550 // Guess simply casting isn't the nicest way...
1551 // FIXME wait for cadet to change this function
1552 sender_ctx = get_peer_ctx (peer_map, sender);
1554 if (GNUNET_YES == get_peer_flag (sender_ctx, PULL_REPLY_PENDING))
1556 GNUNET_break_op (0);
1561 #ifdef ENABLE_MALICIOUS
1562 // We shouldn't even receive pull replies as we're not sending
1565 #endif /* ENABLE_MALICIOUS */
1567 /* Do actual logic */
1568 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1570 LOG (GNUNET_ERROR_TYPE_DEBUG,
1571 "PULL REPLY received, got following peers:\n");
1573 for (i = 0 ; i < ntohl (in_msg->num_peers) ; i++)
1575 LOG (GNUNET_ERROR_TYPE_DEBUG,
1578 GNUNET_i2s (&peers[i]));
1580 #ifdef ENABLE_MALICIOUS
1583 { /* Add attacked peer to local list */
1584 // TODO check if we sent a request and this was the first reply
1585 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set,
1587 && GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mal_peer_set,
1589 && GNUNET_NO == GNUNET_CRYPTO_cmp_peer_identity (&peers[i],
1592 tmp_att_peer = GNUNET_new (struct AttackedPeer);
1593 tmp_att_peer->peer_id = peers[i];
1594 GNUNET_CONTAINER_DLL_insert (att_peers_head,
1597 add_peer_array_to_set (&peers[i], 1, att_peer_set);
1601 #endif /* ENABLE_MALICIOUS */
1602 peer_ctx = get_peer_ctx (peer_map, &peers[i]);
1603 if (GNUNET_YES == get_peer_flag (peer_ctx, VALID)
1604 || NULL != peer_ctx->send_channel
1605 || NULL != peer_ctx->recv_channel)
1607 if (GNUNET_NO == in_arr (pull_list, pull_list_size, &peers[i])
1608 && 0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peers[i]))
1609 GNUNET_array_append (pull_list, pull_list_size, peers[i]);
1611 else if (GNUNET_NO == insert_in_pull_list_scheduled (peer_ctx))
1613 out_op.op = insert_in_pull_list;
1614 out_op.op_cls = NULL;
1615 GNUNET_array_append (peer_ctx->outstanding_ops,
1616 peer_ctx->num_outstanding_ops,
1618 check_peer_live (peer_ctx);
1622 unset_peer_flag (sender_ctx, PULL_REPLY_PENDING);
1623 rem_from_list (&pending_pull_reply_list, &pending_pull_reply_list_size, sender);
1630 * Compute a random delay.
1631 * A uniformly distributed value between mean + spread and mean - spread.
1633 * For example for mean 4 min and spread 2 the minimum is (4 min - (1/2 * 4 min))
1634 * It would return a random value between 2 and 6 min.
1636 * @param mean the mean
1637 * @param spread the inverse amount of deviation from the mean
1639 static struct GNUNET_TIME_Relative
1640 compute_rand_delay (struct GNUNET_TIME_Relative mean, unsigned int spread)
1642 struct GNUNET_TIME_Relative half_interval;
1643 struct GNUNET_TIME_Relative ret;
1644 unsigned int rand_delay;
1645 unsigned int max_rand_delay;
1649 LOG (GNUNET_ERROR_TYPE_WARNING,
1650 "Not accepting spread of 0\n");
1654 /* Compute random time value between spread * mean and spread * mean */
1655 half_interval = GNUNET_TIME_relative_divide (mean, spread);
1657 max_rand_delay = GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us / mean.rel_value_us * (2/spread);
1659 * Compute random value between (0 and 1) * round_interval
1660 * via multiplying round_interval with a 'fraction' (0 to value)/value
1662 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_rand_delay);
1663 ret = GNUNET_TIME_relative_multiply (mean, rand_delay);
1664 ret = GNUNET_TIME_relative_divide (ret, max_rand_delay);
1665 ret = GNUNET_TIME_relative_add (ret, half_interval);
1667 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ret.rel_value_us)
1668 LOG (GNUNET_ERROR_TYPE_WARNING,
1669 "Returning FOREVER_REL\n");
1676 * Send single pull request
1678 * @param peer_id the peer to send the pull request to.
1681 send_pull_request (struct GNUNET_PeerIdentity *peer_id)
1683 struct GNUNET_MQ_Envelope *ev;
1684 struct GNUNET_MQ_Handle *mq;
1686 LOG (GNUNET_ERROR_TYPE_DEBUG,
1687 "Sending PULL request to peer %s of gossiped list.\n",
1688 GNUNET_i2s (peer_id));
1690 GNUNET_array_append (pending_pull_reply_list, pending_pull_reply_list_size, *peer_id);
1692 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
1693 mq = get_mq (peer_map, peer_id);
1694 GNUNET_MQ_send (mq, ev);
1701 * @param peer_id the peer to send the push to.
1704 send_push (struct GNUNET_PeerIdentity *peer_id)
1706 struct GNUNET_MQ_Envelope *ev;
1707 struct GNUNET_MQ_Handle *mq;
1709 LOG (GNUNET_ERROR_TYPE_DEBUG,
1710 "Sending PUSH to peer %s of gossiped list.\n",
1711 GNUNET_i2s (peer_id));
1713 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
1714 mq = get_mq (peer_map, peer_id);
1715 GNUNET_MQ_send (mq, ev);
1720 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1723 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1726 #ifdef ENABLE_MALICIOUS
1728 * Turn RPS service to act malicious.
1730 * @param cls Closure
1731 * @param channel The channel the PUSH was received over
1732 * @param channel_ctx The context associated with this channel
1733 * @param msg The message header
1736 handle_client_act_malicious (void *cls,
1737 struct GNUNET_SERVER_Client *client,
1738 const struct GNUNET_MessageHeader *msg)
1740 struct GNUNET_RPS_CS_ActMaliciousMessage *in_msg;
1741 struct GNUNET_PeerIdentity *peers;
1742 uint32_t num_mal_peers_sent;
1743 uint32_t num_mal_peers_old;
1745 /* Check for protocol violation */
1746 if (sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage) > ntohs (msg->size))
1748 GNUNET_break_op (0);
1751 in_msg = (struct GNUNET_RPS_CS_ActMaliciousMessage *) msg;
1752 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
1753 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1755 LOG (GNUNET_ERROR_TYPE_ERROR,
1756 "message says it sends %" PRIu64 " peers, have space for %i peers\n",
1757 ntohl (in_msg->num_peers),
1758 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
1759 sizeof (struct GNUNET_PeerIdentity));
1760 GNUNET_break_op (0);
1764 /* Do actual logic */
1765 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1766 mal_type = ntohl (in_msg->type);
1768 LOG (GNUNET_ERROR_TYPE_DEBUG,
1769 "Now acting malicious type %" PRIu32 "\n",
1773 { /* Try to maximise representation */
1774 /* Add other malicious peers to those we already know */
1776 num_mal_peers_sent = ntohl (in_msg->num_peers);
1777 num_mal_peers_old = num_mal_peers;
1778 GNUNET_array_grow (mal_peers,
1780 num_mal_peers + num_mal_peers_sent);
1781 memcpy (&mal_peers[num_mal_peers_old],
1783 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1785 /* Add all mal peers to mal_peer_set */
1786 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1790 /* Substitute do_round () with do_mal_round () */
1791 GNUNET_SCHEDULER_cancel (do_round_task);
1792 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1795 else if (2 == mal_type
1797 { /* Try to partition the network */
1798 /* Add other malicious peers to those we already know */
1799 num_mal_peers_sent = ntohl (in_msg->num_peers) - 1;
1800 num_mal_peers_old = num_mal_peers;
1801 GNUNET_array_grow (mal_peers,
1803 num_mal_peers + num_mal_peers_sent);
1804 memcpy (&mal_peers[num_mal_peers_old],
1806 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
1808 /* Add all mal peers to mal_peer_set */
1809 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
1813 /* Store the one attacked peer */
1814 memcpy (&attacked_peer,
1815 &in_msg->attacked_peer,
1816 sizeof (struct GNUNET_PeerIdentity));
1818 LOG (GNUNET_ERROR_TYPE_DEBUG,
1819 "Attacked peer is %s\n",
1820 GNUNET_i2s (&attacked_peer));
1822 /* Substitute do_round () with do_mal_round () */
1823 GNUNET_SCHEDULER_cancel (do_round_task);
1824 do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, NULL);
1826 else if (0 == mal_type)
1827 { /* Stop acting malicious */
1828 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
1830 /* Substitute do_mal_round () with do_round () */
1831 GNUNET_SCHEDULER_cancel (do_round_task);
1832 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
1839 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1844 * Send out PUSHes and PULLs maliciously.
1846 * This is executed regylary.
1849 do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1851 uint32_t num_pushes;
1853 struct GNUNET_TIME_Relative time_next_round;
1854 struct AttackedPeer *tmp_att_peer;
1856 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round maliciously.\n");
1858 /* Do malicious actions */
1860 { /* Try to maximise representation */
1862 /* The maximum of pushes we're going to send this round */
1863 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit,
1864 num_attacked_peers),
1865 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
1867 LOG (GNUNET_ERROR_TYPE_DEBUG,
1868 "Going to send %" PRIu32 " pushes\n",
1871 /* Send PUSHes to attacked peers */
1872 for (i = 0 ; i < num_pushes ; i++)
1874 if (att_peers_tail == att_peer_index)
1875 att_peer_index = att_peers_head;
1877 att_peer_index = att_peer_index->next;
1879 send_push (&att_peer_index->peer_id);
1882 /* Send PULLs to some peers to learn about additional peers to attack */
1883 for (i = 0 ; i < num_pushes * alpha ; i++)
1885 if (att_peers_tail == tmp_att_peer)
1886 tmp_att_peer = att_peers_head;
1888 att_peer_index = tmp_att_peer->next;
1890 send_pull_request (&tmp_att_peer->peer_id);
1895 else if (2 == mal_type)
1897 * Try to partition the network
1898 * Send as many pushes to the attacked peer as possible
1899 * That is one push per round as it will ignore more.
1901 send_push (&attacked_peer);
1906 { /* Combined attack */
1908 /* The maximum of pushes we're going to send this round */
1909 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit - 1,
1910 num_attacked_peers),
1911 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
1913 LOG (GNUNET_ERROR_TYPE_DEBUG,
1914 "Going to send %" PRIu32 " pushes\n",
1917 /* Send PUSHes to attacked peers */
1918 send_push (&attacked_peer);
1920 for (i = 0 ; i < num_pushes ; i++)
1922 if (att_peers_tail == att_peer_index)
1923 att_peer_index = att_peers_head;
1925 att_peer_index = att_peer_index->next;
1927 send_push (&att_peer_index->peer_id);
1930 /* Send PULLs to some peers to learn about additional peers to attack */
1931 for (i = 0 ; i < num_pushes * alpha ; i++)
1933 if (att_peers_tail == tmp_att_peer)
1934 tmp_att_peer = att_peers_head;
1936 att_peer_index = tmp_att_peer->next;
1938 send_pull_request (&tmp_att_peer->peer_id);
1942 /* Schedule next round */
1943 time_next_round = compute_rand_delay (round_interval, 2);
1945 //do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_mal_round, NULL);
1946 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_mal_round, NULL);
1947 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
1949 #endif /* ENABLE_MALICIOUS */
1953 * Send out PUSHes and PULLs, possibly update #gossip_list, samplers.
1955 * This is executed regylary.
1958 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1960 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round.\n");
1963 unsigned int *permut;
1964 unsigned int n_peers; /* Number of peers we send pushes/pulls to */
1965 struct GNUNET_PeerIdentity peer;
1966 struct GNUNET_PeerIdentity *tmp_peer;
1968 LOG (GNUNET_ERROR_TYPE_DEBUG,
1969 "Printing gossip list:\n");
1970 for (i = 0 ; i < gossip_list_size ; i++)
1971 LOG (GNUNET_ERROR_TYPE_DEBUG,
1972 "\t%s\n", GNUNET_i2s (&gossip_list[i]));
1973 // TODO log lists, ...
1975 /* Would it make sense to have one shuffeled gossip list and then
1976 * to send PUSHes to first alpha peers, PULL requests to next beta peers and
1977 * use the rest to update sampler?
1978 * in essence get random peers with consumption */
1981 if (0 < gossip_list_size)
1983 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
1984 (unsigned int) gossip_list_size);
1985 n_peers = ceil (alpha * gossip_list_size);
1986 LOG (GNUNET_ERROR_TYPE_DEBUG,
1987 "Going to send pushes to %u (ceil (%f * %u)) peers.\n",
1988 n_peers, alpha, gossip_list_size);
1989 for (i = 0 ; i < n_peers ; i++)
1991 peer = gossip_list[permut[i]];
1992 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer)) // TODO
1993 { // FIXME if this fails schedule/loop this for later
1997 GNUNET_free (permut);
2001 /* Send PULL requests */
2002 //permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list->size);
2003 n_peers = ceil (beta * gossip_list_size);
2004 LOG (GNUNET_ERROR_TYPE_DEBUG,
2005 "Going to send pulls to %u (ceil (%f * %u)) peers.\n",
2006 n_peers, beta, gossip_list_size);
2007 for (i = 0 ; i < n_peers ; i++)
2009 tmp_peer = get_rand_peer_ignore_list (gossip_list, gossip_list_size,
2010 pending_pull_reply_list, pending_pull_reply_list_size);
2011 if (NULL != tmp_peer)
2014 GNUNET_free (tmp_peer);
2016 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer))
2018 send_pull_request (&peer);
2024 /* Update gossip list */
2026 if (push_list_size <= alpha * gossip_list_size
2027 && push_list_size > 0
2028 && pull_list_size > 0)
2030 LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the gossip list.\n");
2032 uint32_t first_border;
2033 uint32_t second_border;
2035 uint32_t peers_to_clean_size;
2036 struct GNUNET_PeerIdentity *peers_to_clean;
2038 peers_to_clean = NULL;
2039 peers_to_clean_size = 0;
2040 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, gossip_list_size);
2041 memcpy (peers_to_clean,
2043 gossip_list_size * sizeof (struct GNUNET_PeerIdentity));
2045 first_border = ceil (alpha * sampler_size_est_need);
2046 second_border = first_border + ceil (beta * sampler_size_est_need);
2048 GNUNET_array_grow (gossip_list, gossip_list_size, second_border);
2050 for (i = 0 ; i < first_border ; i++)
2051 { // TODO use RPS_sampler_get_n_rand_peers
2052 /* Update gossip list with peers received through PUSHes */
2053 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
2055 gossip_list[i] = push_list[r_index];
2056 // TODO change the peer_flags accordingly
2059 for (i = first_border ; i < second_border ; i++)
2061 /* Update gossip list with peers received through PULLs */
2062 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
2064 gossip_list[i] = pull_list[r_index];
2065 // TODO change the peer_flags accordingly
2068 for (i = second_border ; i < sampler_size_est_need ; i++)
2070 /* Update gossip list with peers from history */
2071 RPS_sampler_get_n_rand_peers (prot_sampler, hist_update, NULL, 1, GNUNET_NO);
2072 num_hist_update_tasks++;
2073 // TODO change the peer_flags accordingly
2076 for (i = 0 ; i < gossip_list_size ; i++)
2077 rem_from_list (&peers_to_clean, &peers_to_clean_size, &gossip_list[i]);
2079 for (i = 0 ; i < peers_to_clean_size ; i++)
2080 peer_clean (&peers_to_clean[i]);
2082 GNUNET_free (peers_to_clean);
2086 LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the gossip list.\n");
2088 // TODO independent of that also get some peers from CADET_get_peers()?
2090 LOG (GNUNET_ERROR_TYPE_DEBUG,
2091 "Received %u pushes and %u pulls last round (alpha (%.2f) * gossip_list_size (%u) = %.2f)\n",
2096 alpha * gossip_list_size);
2098 /* Update samplers */
2099 for ( i = 0 ; i < push_list_size ; i++ )
2101 LOG (GNUNET_ERROR_TYPE_DEBUG,
2102 "Updating with peer %s from push list\n",
2103 GNUNET_i2s (&push_list[i]));
2104 RPS_sampler_update (prot_sampler, &push_list[i]);
2105 RPS_sampler_update (client_sampler, &push_list[i]);
2106 // TODO set in_flag?
2109 for ( i = 0 ; i < pull_list_size ; i++ )
2111 LOG (GNUNET_ERROR_TYPE_DEBUG,
2112 "Updating with peer %s from pull list\n",
2113 GNUNET_i2s (&pull_list[i]));
2114 RPS_sampler_update (prot_sampler, &pull_list[i]);
2115 RPS_sampler_update (client_sampler, &pull_list[i]);
2116 // TODO set in_flag?
2120 /* Empty push/pull lists */
2121 GNUNET_array_grow (push_list, push_list_size, 0);
2122 GNUNET_array_grow (pull_list, pull_list_size, 0);
2124 struct GNUNET_TIME_Relative time_next_round;
2126 time_next_round = compute_rand_delay (round_interval, 2);
2128 /* Schedule next round */
2129 //do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_round, NULL);
2130 do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, &do_round, NULL);
2131 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
2136 rps_start (struct GNUNET_SERVER_Handle *server);
2140 * This is called from GNUNET_CADET_get_peers().
2142 * It is called on every peer(ID) that cadet somehow has contact with.
2143 * We use those to initialise the sampler.
2146 init_peer_cb (void *cls,
2147 const struct GNUNET_PeerIdentity *peer,
2148 int tunnel, // "Do we have a tunnel towards this peer?"
2149 unsigned int n_paths, // "Number of known paths towards this peer"
2150 unsigned int best_path) // "How long is the best path?
2151 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
2155 LOG (GNUNET_ERROR_TYPE_DEBUG,
2156 "Got peer_id %s from cadet\n",
2164 * Clean the send channel of a peer
2167 peer_clean (const struct GNUNET_PeerIdentity *peer)
2169 struct PeerContext *peer_ctx;
2170 struct GNUNET_CADET_Channel *channel;
2172 if (GNUNET_YES != in_arr (gossip_list, gossip_list_size, peer)
2173 && GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
2175 peer_ctx = get_peer_ctx (peer_map, peer);
2176 if (NULL != peer_ctx->send_channel)
2178 channel = peer_ctx->send_channel;
2179 peer_ctx->send_channel = NULL;
2180 GNUNET_CADET_channel_destroy (channel);
2187 * Callback used to remove peers from the multipeermap.
2190 peer_remove_cb (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
2192 struct PeerContext *peer_ctx;
2193 const struct GNUNET_CADET_Channel *channel =
2194 (const struct GNUNET_CADET_Channel *) cls;
2195 struct GNUNET_CADET_Channel *recv;
2196 struct GNUNET_CADET_Channel *send;
2198 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, value))
2200 peer_ctx = (struct PeerContext *) value;
2202 if (0 != peer_ctx->num_outstanding_ops)
2203 GNUNET_array_grow (peer_ctx->outstanding_ops,
2204 peer_ctx->num_outstanding_ops,
2207 if (NULL != peer_ctx->mq)
2209 GNUNET_MQ_destroy (peer_ctx->mq);
2210 peer_ctx->mq = NULL;
2214 if (NULL != peer_ctx->is_live_task)
2216 LOG (GNUNET_ERROR_TYPE_DEBUG,
2217 "Trying to cancle is_live_task for peer %s\n",
2219 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
2220 peer_ctx->is_live_task = NULL;
2223 send = peer_ctx->send_channel;
2224 peer_ctx->send_channel = NULL;
2228 GNUNET_CADET_channel_destroy (send);
2231 recv = peer_ctx->send_channel;
2232 peer_ctx->recv_channel = NULL;
2236 GNUNET_CADET_channel_destroy (recv);
2239 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_remove_all (peer_map, key))
2240 LOG (GNUNET_ERROR_TYPE_WARNING, "removing peer from peer_map failed\n");
2242 GNUNET_free (peer_ctx);
2250 * Task run during shutdown.
2256 shutdown_task (void *cls,
2257 const struct GNUNET_SCHEDULER_TaskContext *tc)
2260 LOG (GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
2262 if (NULL != do_round_task)
2264 GNUNET_SCHEDULER_cancel (do_round_task);
2265 do_round_task = NULL;
2270 if (GNUNET_SYSERR ==
2271 GNUNET_CONTAINER_multipeermap_iterate (peer_map, peer_remove_cb, NULL))
2272 LOG (GNUNET_ERROR_TYPE_WARNING,
2273 "Iterating over peers to disconnect from them was cancelled\n");
2276 GNUNET_NSE_disconnect (nse);
2277 GNUNET_CADET_disconnect (cadet_handle);
2278 RPS_sampler_destroy (prot_sampler);
2279 RPS_sampler_destroy (client_sampler);
2280 LOG (GNUNET_ERROR_TYPE_DEBUG,
2281 "Size of the peermap: %u\n",
2282 GNUNET_CONTAINER_multipeermap_size (peer_map));
2283 GNUNET_break (0 == GNUNET_CONTAINER_multipeermap_size (peer_map));
2284 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
2285 GNUNET_array_grow (gossip_list, gossip_list_size, 0);
2286 GNUNET_array_grow (push_list, push_list_size, 0);
2287 GNUNET_array_grow (pull_list, pull_list_size, 0);
2288 #ifdef ENABLE_MALICIOUS
2289 struct AttackedPeer *tmp_att_peer;
2290 GNUNET_array_grow (mal_peers, num_mal_peers, 0);
2291 if (NULL != mal_peer_set)
2292 GNUNET_CONTAINER_multipeermap_destroy (mal_peer_set);
2293 if (NULL != att_peer_set)
2294 GNUNET_CONTAINER_multipeermap_destroy (att_peer_set);
2295 while (NULL != att_peers_head)
2297 tmp_att_peer = att_peers_head;
2298 GNUNET_CONTAINER_DLL_remove (att_peers_head, att_peers_tail, tmp_att_peer);
2300 #endif /* ENABLE_MALICIOUS */
2305 * A client disconnected. Remove all of its data structure entries.
2307 * @param cls closure, NULL
2308 * @param client identification of the client
2311 handle_client_disconnect (void *cls,
2312 struct GNUNET_SERVER_Client * client)
2318 * Handle the channel a peer opens to us.
2320 * @param cls The closure
2321 * @param channel The channel the peer wants to establish
2322 * @param initiator The peer's peer ID
2323 * @param port The port the channel is being established over
2324 * @param options Further options
2327 handle_inbound_channel (void *cls,
2328 struct GNUNET_CADET_Channel *channel,
2329 const struct GNUNET_PeerIdentity *initiator,
2331 enum GNUNET_CADET_ChannelOption options)
2333 struct PeerContext *peer_ctx;
2334 struct GNUNET_PeerIdentity peer;
2337 LOG (GNUNET_ERROR_TYPE_DEBUG,
2338 "New channel was established to us (Peer %s).\n",
2339 GNUNET_i2s (&peer));
2341 GNUNET_assert (NULL != channel);
2343 // we might not even store the recv_channel
2345 peer_ctx = get_peer_ctx (peer_map, &peer);
2346 // FIXME what do we do if a channel is established twice?
2347 // overwrite? Clean old channel? ...?
2348 //if (NULL != peer_ctx->recv_channel)
2350 // peer_ctx->recv_channel = channel;
2352 peer_ctx->recv_channel = channel;
2354 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, &peer, peer_ctx,
2355 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
2357 peer_is_live (peer_ctx);
2359 return NULL; // TODO
2364 * This is called when a remote peer destroys a channel.
2366 * @param cls The closure
2367 * @param channel The channel being closed
2368 * @param channel_ctx The context associated with this channel
2371 cleanup_channel (void *cls,
2372 const struct GNUNET_CADET_Channel *channel,
2375 struct GNUNET_PeerIdentity *peer;
2376 struct PeerContext *peer_ctx;
2378 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
2379 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
2380 // Guess simply casting isn't the nicest way...
2381 // FIXME wait for cadet to change this function
2383 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
2385 peer_ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
2387 if (NULL == peer_ctx) /* It could have been removed by shutdown_task */
2390 if (channel == peer_ctx->send_channel)
2391 { /* Peer probably went down */
2392 LOG (GNUNET_ERROR_TYPE_DEBUG,
2393 "Peer %s destroyed send channel - probably went down, cleaning up\n",
2395 rem_from_list (&gossip_list, &gossip_list_size, peer);
2396 rem_from_list (&pending_pull_reply_list, &pending_pull_reply_list_size, peer);
2398 peer_ctx->send_channel = NULL;
2399 /* Somwewhat {ab,re}use the iterator function */
2400 /* Cast to void is ok, because it's used as void in peer_remove_cb */
2401 (void) peer_remove_cb ((void *) channel, peer, peer_ctx);
2403 else if (channel == peer_ctx->recv_channel)
2404 { /* Other peer doesn't want to send us messages anymore */
2405 LOG (GNUNET_ERROR_TYPE_DEBUG,
2406 "Peer %s destroyed recv channel - cleaning up channel\n",
2408 peer_ctx->recv_channel = NULL;
2415 * Actually start the service.
2418 rps_start (struct GNUNET_SERVER_Handle *server)
2420 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
2421 {&handle_client_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
2422 sizeof (struct GNUNET_RPS_CS_RequestMessage)},
2423 {&handle_client_seed, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_SEED, 0},
2424 #ifdef ENABLE_MALICIOUS
2425 {&handle_client_act_malicious, NULL, GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS , 0},
2426 #endif /* ENABLE_MALICIOUS */
2430 GNUNET_SERVER_add_handlers (server, handlers);
2431 GNUNET_SERVER_disconnect_notify (server,
2432 &handle_client_disconnect,
2434 LOG (GNUNET_ERROR_TYPE_INFO, "Ready to receive requests from clients\n");
2437 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
2438 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
2440 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2447 * Process statistics requests.
2449 * @param cls closure
2450 * @param server the initialized server
2451 * @param c configuration to use
2455 struct GNUNET_SERVER_Handle *server,
2456 const struct GNUNET_CONFIGURATION_Handle *c)
2458 // TODO check what this does -- copied from gnunet-boss
2459 // - seems to work as expected
2460 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
2465 GNUNET_CRYPTO_get_peer_identity (cfg, &own_identity); // TODO check return value
2466 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2467 "STARTING SERVICE (rps) for peer [%s]\n",
2468 GNUNET_i2s (&own_identity));
2469 #ifdef ENABLE_MALICIOUS
2470 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2471 "Malicious execution compiled in.\n");
2472 #endif /* ENABLE_MALICIOUS */
2476 /* Get time interval from the configuration */
2477 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
2481 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read ROUNDINTERVAL from config\n");
2482 GNUNET_SCHEDULER_shutdown ();
2486 /* Get initial size of sampler/gossip list from the configuration */
2487 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "RPS",
2489 (long long unsigned int *) &sampler_size_est_need))
2491 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read INITSIZE from config\n");
2492 GNUNET_SCHEDULER_shutdown ();
2495 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", sampler_size_est_need);
2501 /* connect to NSE */
2502 nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
2508 peer_map = GNUNET_CONTAINER_multipeermap_create (sampler_size_est_need, GNUNET_NO);
2511 /* Initialise cadet */
2512 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
2513 {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH ,
2514 sizeof (struct GNUNET_MessageHeader)},
2515 {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
2516 sizeof (struct GNUNET_MessageHeader)},
2517 {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY , 0},
2521 const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
2522 cadet_handle = GNUNET_CADET_connect (cfg,
2524 &handle_inbound_channel,
2530 /* Initialise sampler */
2531 struct GNUNET_TIME_Relative half_round_interval;
2532 struct GNUNET_TIME_Relative max_round_interval;
2534 half_round_interval = GNUNET_TIME_relative_multiply (round_interval, .5);
2535 max_round_interval = GNUNET_TIME_relative_add (round_interval, half_round_interval);
2537 prot_sampler = RPS_sampler_init (sampler_size_est_need, max_round_interval);
2538 client_sampler = RPS_sampler_init (sampler_size_est_need, max_round_interval);
2540 /* Initialise push and pull maps */
2545 pending_pull_reply_list = NULL;
2546 pending_pull_reply_list_size = 0;
2549 num_hist_update_tasks = 0;
2552 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
2553 GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, NULL);
2554 // TODO send push/pull to each of those peers?
2562 * The main function for the rps service.
2564 * @param argc number of arguments from the command line
2565 * @param argv command line arguments
2566 * @return 0 ok, 1 on error
2569 main (int argc, char *const *argv)
2571 return (GNUNET_OK ==
2572 GNUNET_SERVICE_run (argc,
2575 GNUNET_SERVICE_OPTION_NONE,
2576 &run, NULL)) ? 0 : 1;
2579 /* end of gnunet-service-rps.c */