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 take care that messages are not longer than 64k
43 // TODO check for overflows
45 // TODO align message structs
47 // (TODO api -- possibility of getting weak random peer immideately)
49 // TODO malicious peer
51 // TODO Change API to accept initialisation peers
53 // TODO Change API to accept good peers 'friends'
55 // TODO store peers somewhere
57 // TODO check that every id we get is valid - is it reachable?
61 // hist_size_init, hist_size_max
66 static const struct GNUNET_CONFIGURATION_Handle *cfg;
71 static struct GNUNET_PeerIdentity *own_identity;
74 struct GNUNET_PeerIdentity *
75 get_rand_peer (const struct GNUNET_PeerIdentity *peer_list, unsigned int size);
78 /***********************************************************************
79 * Housekeeping with peers
80 ***********************************************************************/
83 * Struct used to store the context of a connected client.
88 * The message queue to communicate with the client.
90 struct GNUNET_MQ_Handle *mq;
94 * Used to keep track in what lists single peerIDs are.
98 IN_OTHER_SAMPLER_LIST = 0x01, // unneeded?
99 IN_OTHER_GOSSIP_LIST = 0x02, // unneeded?
100 IN_OWN_SAMPLER_LIST = 0x04, // unneeded?
101 IN_OWN_GOSSIP_LIST = 0x08, // unneeded?
104 * We set this bit when we can be sure the other peer is/was live.
111 * Functions of this type can be used to be stored at a peer for later execution.
113 typedef void (* PeerOp) (void *cls, const struct GNUNET_PeerIdentity *peer);
116 * Outstanding operation on peer consisting of callback and closure
118 struct PeerOutstandingOp
133 * Struct used to keep track of other peer's status
135 * This is stored in a multipeermap.
140 * In own gossip/sampler list, in other's gossip/sampler list
145 * Message queue open to client
147 struct GNUNET_MQ_Handle *mq;
150 * Channel open to client.
152 struct GNUNET_CADET_Channel *send_channel;
155 * Channel open from client.
157 struct GNUNET_CADET_Channel *recv_channel; // unneeded?
160 * Array of outstanding operations on this peer.
162 struct PeerOutstandingOp *outstanding_ops;
165 * Number of outstanding operations.
167 unsigned int num_outstanding_ops;
168 //size_t num_outstanding_ops;
171 * This is pobably followed by 'statistical' data (when we first saw
172 * him, how did we get his ID, how many pushes (in a timeinterval),
177 /***********************************************************************
178 * /Housekeeping with peers
179 ***********************************************************************/
181 /***********************************************************************
183 ***********************************************************************/
186 * Set of all peers to keep track of them.
188 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
192 * The gossiped list of peers.
194 static struct GNUNET_PeerIdentity *gossip_list;
197 * Size of the gossiped list
199 //static unsigned int gossip_list_size;
200 static uint32_t gossip_list_size;
204 * The actual size of the sampler
206 static unsigned int sampler_size;
207 //size_t sampler_size;
210 * The size of sampler we need to be able to satisfy the client's need of
213 static unsigned int sampler_size_client_need;
216 * The size of sampler we need to be able to satisfy the Brahms protocol's
217 * need of random peers.
219 * This is directly taken as the #gossip_list_size on update of the
222 * This is one minimum size the sampler grows to.
224 static unsigned int sampler_size_est_need;
228 * Percentage of total peer number in the gossip list
229 * to send random PUSHes to
231 * TODO do not read from configuration
236 * Percentage of total peer number in the gossip list
237 * to send random PULLs to
239 * TODO do not read from configuration
244 * The percentage gamma of history updates.
245 * Simply 1 - alpha - beta
250 * Identifier for the main task that runs periodically.
252 static struct GNUNET_SCHEDULER_Task *do_round_task;
255 * Time inverval the do_round task runs in.
257 static struct GNUNET_TIME_Relative round_interval;
262 * List to store peers received through pushes temporary.
264 * TODO -> multipeermap
266 static struct GNUNET_PeerIdentity *push_list;
269 * Size of the push_list;
271 static unsigned int push_list_size;
272 //size_t push_list_size;
275 * List to store peers received through pulls temporary.
277 * TODO -> multipeermap
279 static struct GNUNET_PeerIdentity *pull_list;
282 * Size of the pull_list;
284 static unsigned int pull_list_size;
285 //size_t pull_list_size;
291 static struct GNUNET_NSE_Handle *nse;
296 static struct GNUNET_CADET_Handle *cadet_handle;
302 * Only needed in the beginning to check how many of the 64 deltas
305 static unsigned int req_counter;
308 * Time of the last request we received.
310 * Used to compute the expected request rate.
312 static struct GNUNET_TIME_Absolute last_request;
315 * Size of #request_deltas.
317 #define REQUEST_DELTAS_SIZE 64
318 static unsigned int request_deltas_size = REQUEST_DELTAS_SIZE;
321 * Last 64 deltas between requests
323 static struct GNUNET_TIME_Relative request_deltas[REQUEST_DELTAS_SIZE];
326 * The prediction of the rate of requests
328 static struct GNUNET_TIME_Relative request_rate;
332 * Number of history update tasks.
334 uint32_t num_hist_update_tasks;
337 /***********************************************************************
339 ***********************************************************************/
342 /***********************************************************************
344 ***********************************************************************/
347 * Check if peer is already in peer array.
350 in_arr (const struct GNUNET_PeerIdentity *array,
351 unsigned int arr_size,
352 const struct GNUNET_PeerIdentity *peer)
354 GNUNET_assert (NULL != peer);
359 GNUNET_assert (NULL != array);
364 while (0 != GNUNET_CRYPTO_cmp_peer_identity (&array[i], peer) &&
376 * Get random peer from the gossip list.
378 struct GNUNET_PeerIdentity *
379 get_rand_peer (const struct GNUNET_PeerIdentity *peer_list, unsigned int list_size)
382 struct GNUNET_PeerIdentity *peer;
384 peer = GNUNET_new (struct GNUNET_PeerIdentity);
385 // FIXME if we have only NULL in gossip list this will block
386 // but then we might have a problem nevertheless
392 * Choose the r_index of the peer we want to return
393 * at random from the interval of the gossip list
395 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
398 *peer = peer_list[r_index];
399 } while (NULL == peer);
406 * Get the context of a peer. If not existing, create.
409 get_peer_ctx (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
410 const struct GNUNET_PeerIdentity *peer)
412 struct PeerContext *ctx;
414 if ( GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
416 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
420 ctx = GNUNET_new (struct PeerContext);
423 ctx->send_channel = NULL;
424 ctx->recv_channel = NULL;
425 ctx->outstanding_ops = NULL;
426 ctx->num_outstanding_ops = 0;
427 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
428 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
435 * Put random peer from sampler into the gossip list as history update.
438 hist_update (void *cls, struct GNUNET_PeerIdentity *ids, uint32_t num_peers)
440 GNUNET_assert (1 == num_peers);
442 if (gossip_list_size < sampler_size_est_need)
443 GNUNET_array_append (gossip_list, gossip_list_size, *ids);
445 if (0 < num_hist_update_tasks)
446 num_hist_update_tasks--;
451 * Callback that is called when a channel was effectively established.
452 * This is given to ntfy_tmt_rdy and called when the channel was
453 * successfully established.
456 peer_is_live (void *cls, size_t size, void *buf)
458 struct GNUNET_PeerIdentity *peer;
459 struct PeerContext *peer_ctx;
461 peer = (struct GNUNET_PeerIdentity *) cls;
462 peer_ctx = get_peer_ctx (peer_map, peer);
463 peer_ctx->peer_flags |= LIVING;
465 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer %s is live\n", GNUNET_i2s (peer));
467 if (0 != peer_ctx->num_outstanding_ops)
468 { /* Call outstanding operations */
471 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
472 peer_ctx->outstanding_ops[i].op (peer_ctx->outstanding_ops[i].op_cls, peer);
473 GNUNET_array_grow (peer_ctx->outstanding_ops, peer_ctx->num_outstanding_ops, 0);
484 * Get the channel of a peer. If not existing, create.
486 struct GNUNET_CADET_Channel *
487 get_channel (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
488 const struct GNUNET_PeerIdentity *peer)
490 struct PeerContext *ctx;
491 struct GNUNET_PeerIdentity *tmp_peer;
493 ctx = get_peer_ctx (peer_map, peer);
494 if (NULL == ctx->send_channel)
496 ctx->send_channel = GNUNET_CADET_channel_create (cadet_handle, NULL, peer,
497 GNUNET_RPS_CADET_PORT,
498 GNUNET_CADET_OPTION_RELIABLE);
500 if (NULL == ctx->recv_channel)
502 tmp_peer = GNUNET_new (struct GNUNET_PeerIdentity);
504 (void) GNUNET_CADET_notify_transmit_ready (ctx->send_channel, GNUNET_NO,
505 GNUNET_TIME_UNIT_FOREVER_REL,
506 0, peer_is_live, tmp_peer);
509 // do I have to explicitly put it in the peer_map?
510 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
511 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
513 return ctx->send_channel;
518 * Get the message queue of a specific peer.
520 * If we already have a message queue open to this client,
521 * simply return it, otherways create one.
523 struct GNUNET_MQ_Handle *
524 get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
525 const struct GNUNET_PeerIdentity *peer_id)
527 struct PeerContext *ctx;
529 ctx = get_peer_ctx (peer_map, peer_id);
532 (void) get_channel (peer_map, peer_id);
533 ctx->mq = GNUNET_CADET_mq_create (ctx->send_channel);
534 //do I have to explicitly put it in the peer_map?
535 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer_id, ctx,
536 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
543 * Sum all time relatives of an array.
545 struct GNUNET_TIME_Relative
546 T_relative_sum (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
548 struct GNUNET_TIME_Relative sum;
551 sum = GNUNET_TIME_UNIT_ZERO;
552 for ( i = 0 ; i < arr_size ; i++ )
554 sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
561 * Compute the average of given time relatives.
563 struct GNUNET_TIME_Relative
564 T_relative_avg (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
566 return GNUNET_TIME_relative_divide (T_relative_sum (rel_array, arr_size), arr_size);
571 * Insert PeerID in #pull_list
573 * Called once we know a peer is live.
576 insert_in_pull_list (void *cls, const struct GNUNET_PeerIdentity *peer)
578 if (GNUNET_NO == in_arr (pull_list, pull_list_size, peer))
579 GNUNET_array_append (pull_list, pull_list_size, *peer);
583 * Check whether #insert_in_pull_list was already scheduled
586 insert_in_pull_list_scheduled (const struct PeerContext *peer_ctx)
590 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
591 if (insert_in_pull_list == peer_ctx->outstanding_ops[i].op)
598 * Insert PeerID in #gossip_list
600 * Called once we know a peer is live.
603 insert_in_gossip_list (void *cls, const struct GNUNET_PeerIdentity *peer)
605 if (GNUNET_NO == in_arr (gossip_list, gossip_list_size, peer))
606 GNUNET_array_append (gossip_list, gossip_list_size, *peer);
610 * Check whether #insert_in_pull_list was already scheduled
613 insert_in_gossip_list_scheduled (const struct PeerContext *peer_ctx)
617 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
618 if (insert_in_gossip_list == peer_ctx->outstanding_ops[i].op)
625 * Update sampler with given PeerID.
628 insert_in_sampler (void *cls, const struct GNUNET_PeerIdentity *peer)
630 RPS_sampler_update_list (peer);
634 * Check whether #insert_in_sampler was already scheduled
637 insert_in_sampler_scheduled (const struct PeerContext *peer_ctx)
641 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
642 if (insert_in_sampler== peer_ctx->outstanding_ops[i].op)
650 * Wrapper around #RPS_sampler_resize()
655 uint32_t bigger_size;
659 if (sampler_size_est_need > sampler_size_client_need)
660 bigger_size = sampler_size_client_need;
662 bigger_size = sampler_size_est_need;
664 // TODO respect the request rate, min, max
665 if (sampler_size > bigger_size*4)
667 RPS_sampler_resize (sampler_size/2);
669 else if (sampler_size < bigger_size)
671 RPS_sampler_resize (sampler_size*2);
676 /***********************************************************************
678 ***********************************************************************/
681 * Function called by NSE.
683 * Updates sizes of sampler list and gossip list and adapt those lists
687 nse_callback (void *cls, struct GNUNET_TIME_Absolute timestamp, double logestimate, double std_dev)
690 //double scale; // TODO this might go gloabal/config
692 LOG (GNUNET_ERROR_TYPE_DEBUG,
693 "Received a ns estimate - logest: %f, std_dev: %f (old_size: %f)\n",
694 logestimate, std_dev, sampler_size);
696 estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
697 // GNUNET_NSE_log_estimate_to_n (logestimate);
698 estimate = pow (estimate, 1./3);
699 // TODO add if std_dev is a number
700 // estimate += (std_dev * scale);
701 if ( 0 < estimate ) {
702 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
703 sampler_size_est_need = estimate;
705 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
707 /* If the NSE has changed adapt the lists accordingly */
713 * Callback called once the requested PeerIDs are ready.
715 * Sends those to the requesting client.
717 void client_respond (void *cls,
718 struct GNUNET_PeerIdentity *ids, uint32_t num_peers)
720 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler returned %" PRIX32 " peers\n", num_peers);
721 struct GNUNET_MQ_Envelope *ev;
722 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
723 struct GNUNET_SERVER_Client *client;
724 struct client_ctx *cli_ctx;
726 client = (struct GNUNET_SERVER_Client *) cls;
728 ev = GNUNET_MQ_msg_extra (out_msg,
729 num_peers * sizeof (struct GNUNET_PeerIdentity),
730 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
731 out_msg->num_peers = htonl (num_peers);
735 num_peers * sizeof (struct GNUNET_PeerIdentity));
738 cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct client_ctx);
739 if ( NULL == cli_ctx ) {
740 cli_ctx = GNUNET_new (struct client_ctx);
741 cli_ctx->mq = GNUNET_MQ_queue_for_server_client (client);
742 GNUNET_SERVER_client_set_user_context (client, cli_ctx);
745 GNUNET_MQ_send (cli_ctx->mq, ev);
750 * Handle RPS request from the client.
753 * @param client identification of the client
754 * @param message the actual message
757 handle_client_request (void *cls,
758 struct GNUNET_SERVER_Client *client,
759 const struct GNUNET_MessageHeader *message)
761 struct GNUNET_RPS_CS_RequestMessage *msg;
763 struct GNUNET_TIME_Relative max_round_duration;
766 /* Estimate request rate */
767 if (request_deltas_size > req_counter)
769 if ( 1 < req_counter)
771 /* Shift last request deltas to the right */
772 memcpy (&request_deltas[1],
774 (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
775 /* Add current delta to beginning */
776 request_deltas[0] = GNUNET_TIME_absolute_get_difference (last_request,
777 GNUNET_TIME_absolute_get ());
778 request_rate = T_relative_avg (request_deltas, req_counter);
780 max_round_duration = GNUNET_TIME_relative_add (round_interval,
781 GNUNET_TIME_relative_divide (round_interval, 2));
782 sampler_size_client_need = max_round_duration.rel_value_us / request_rate.rel_value_us;
786 last_request = GNUNET_TIME_absolute_get ();
789 // TODO check message size
790 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
792 num_peers = ntohl (msg->num_peers);
794 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client requested %" PRIX32 " random peer(s).\n", num_peers);
796 RPS_sampler_get_n_rand_peers (client_respond, client, num_peers, GNUNET_YES);
798 GNUNET_SERVER_receive_done (client,
804 * Handle seed from the client.
807 * @param client identification of the client
808 * @param message the actual message
811 handle_client_seed (void *cls,
812 struct GNUNET_SERVER_Client *client,
813 const struct GNUNET_MessageHeader *message)
815 struct GNUNET_RPS_CS_SeedMessage *in_msg;
816 struct GNUNET_PeerIdentity *peers;
819 if (sizeof (struct GNUNET_RPS_CS_SeedMessage) < ntohs (message->size))
822 GNUNET_SERVER_receive_done (client,
825 in_msg = (struct GNUNET_RPS_CS_SeedMessage *) message;
826 if ((ntohs (message->size) - sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
827 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
830 GNUNET_SERVER_receive_done (client,
834 in_msg = (struct GNUNET_RPS_CS_SeedMessage *) message;
835 peers = (struct GNUNET_PeerIdentity *) &message[1];
837 for ( i = 0 ; i < ntohl (in_msg->num_peers) ; i++ )
838 RPS_sampler_update_list (&peers[i]);
840 GNUNET_SERVER_receive_done (client,
846 * Handle a PUSH message from another peer.
848 * Check the proof of work and store the PeerID
849 * in the temporary list for pushed PeerIDs.
852 * @param channel The channel the PUSH was received over
853 * @param channel_ctx The context associated with this channel
854 * @param msg The message header
857 handle_peer_push (void *cls,
858 struct GNUNET_CADET_Channel *channel,
860 const struct GNUNET_MessageHeader *msg)
862 const struct GNUNET_PeerIdentity *peer;
864 // (check the proof of work)
866 peer = (const struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
867 // FIXME wait for cadet to change this function
868 LOG (GNUNET_ERROR_TYPE_DEBUG, "PUSH received (%s)\n", GNUNET_i2s (peer));
870 /* Add the sending peer to the push_list */
871 if (GNUNET_NO == in_arr (push_list, pull_list_size, peer))
872 GNUNET_array_append (push_list, push_list_size, *peer);
878 * Handle PULL REQUEST request message from another peer.
880 * Reply with the gossip list of PeerIDs.
883 * @param channel The channel the PUSH was received over
884 * @param channel_ctx The context associated with this channel
885 * @param msg The message header
888 handle_peer_pull_request (void *cls,
889 struct GNUNET_CADET_Channel *channel,
891 const struct GNUNET_MessageHeader *msg)
893 struct GNUNET_PeerIdentity *peer;
894 struct GNUNET_MQ_Handle *mq;
895 struct GNUNET_MQ_Envelope *ev;
896 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
899 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (channel,
900 GNUNET_CADET_OPTION_PEER);
901 // FIXME wait for cadet to change this function
902 LOG (GNUNET_ERROR_TYPE_DEBUG,
903 "PULL REQUEST from peer %s received, going to send %u peers\n",
904 GNUNET_i2s (peer), gossip_list_size);
906 mq = get_mq (peer_map, peer);
908 ev = GNUNET_MQ_msg_extra (out_msg,
909 gossip_list_size * sizeof (struct GNUNET_PeerIdentity),
910 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
911 //out_msg->num_peers = htonl (gossip_list_size);
912 out_msg->num_peers = htonl (gossip_list_size);
913 memcpy (&out_msg[1], gossip_list,
914 gossip_list_size * sizeof (struct GNUNET_PeerIdentity));
916 GNUNET_MQ_send (mq, ev);
922 * Handle PULL REPLY message from another peer.
924 * Check whether we sent a corresponding request and
925 * whether this reply is the first one.
928 * @param channel The channel the PUSH was received over
929 * @param channel_ctx The context associated with this channel
930 * @param msg The message header
933 handle_peer_pull_reply (void *cls,
934 struct GNUNET_CADET_Channel *channel,
936 const struct GNUNET_MessageHeader *msg)
938 LOG (GNUNET_ERROR_TYPE_DEBUG, "PULL REPLY received\n");
940 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
941 struct GNUNET_PeerIdentity *peers;
942 struct PeerContext *peer_ctx;
943 struct PeerOutstandingOp out_op;
946 if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->size))
948 GNUNET_break_op (0); // At the moment our own implementation seems to break that.
949 return GNUNET_SYSERR;
951 in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
952 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) / sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
954 LOG (GNUNET_ERROR_TYPE_ERROR, "message says it sends %" PRIu64 " peers, have space for %i peers\n",
955 ntohl (in_msg->num_peers),
956 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) / sizeof (struct GNUNET_PeerIdentity));
958 return GNUNET_SYSERR;
961 // TODO check that we sent a request and that it is the first reply
963 peers = (struct GNUNET_PeerIdentity *) &msg[1];
964 for ( i = 0 ; i < ntohl (in_msg->num_peers) ; i++ )
966 peer_ctx = get_peer_ctx (peer_map, &peers[i]);
968 if ((0 != (peer_ctx->peer_flags && LIVING)) ||
969 NULL != peer_ctx->recv_channel)
971 if (GNUNET_NO == in_arr (pull_list, pull_list_size, &peers[i]))
972 GNUNET_array_append (pull_list, pull_list_size, peers[i]);
974 else if (GNUNET_NO == insert_in_pull_list_scheduled (peer_ctx))
976 out_op.op = insert_in_pull_list;
977 GNUNET_array_append (peer_ctx->outstanding_ops, peer_ctx->num_outstanding_ops, out_op);
981 // TODO check that id is valid - whether it is reachable
988 * Send out PUSHes and PULLs.
990 * This is executed regylary.
993 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
995 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round\n");
998 //unsigned int *n_arr;
999 unsigned int n_peers; /* Number of peers we send pushes/pulls to */
1000 struct GNUNET_MQ_Envelope *ev;
1001 const struct GNUNET_PeerIdentity *peer;
1002 struct GNUNET_MQ_Handle *mq;
1004 // TODO log lists, ...
1007 /* Would it make sense to have one shuffeled gossip list and then
1008 * to send PUSHes to first alpha peers, PULL requests to next beta peers and
1009 * use the rest to update sampler?
1010 * in essence get random peers with consumption */
1013 //n_arr = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) gossip_list_size);
1014 if (0 != gossip_list_size)
1016 n_peers = round (alpha * gossip_list_size);
1019 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to send pushes to %u (%f * %u) peers.\n",
1020 n_peers, alpha, gossip_list_size);
1021 for ( i = 0 ; i < n_peers ; i++ )
1023 peer = get_rand_peer (gossip_list, gossip_list_size);
1024 if (own_identity != peer)
1025 { // FIXME if this fails schedule/loop this for later
1026 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending PUSH to peer %s of gossiped list.\n", GNUNET_i2s (peer));
1028 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
1029 // FIXME sometimes it returns a pointer to a freed mq
1030 mq = get_mq (peer_map, peer);
1031 GNUNET_MQ_send (mq, ev);
1037 /* Send PULL requests */
1038 //n_arr = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list->size);
1039 if (0 != gossip_list_size)
1041 n_peers = round (beta * gossip_list_size);
1044 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to send pulls to %u (%f * %u) peers.\n",
1045 n_peers, beta, gossip_list_size);
1046 for ( i = 0 ; i < n_peers ; i++ )
1048 peer = get_rand_peer (gossip_list, gossip_list_size);
1049 if (own_identity != peer)
1050 { // FIXME if this fails schedule/loop this for later
1051 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending PULL request to peer %s of gossiped list.\n", GNUNET_i2s (peer));
1053 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
1055 mq = get_mq (peer_map, peer);
1056 GNUNET_MQ_send (mq, ev);
1062 /* Update gossip list */
1065 if ( push_list_size <= alpha * gossip_list_size &&
1066 push_list_size != 0 &&
1067 pull_list_size != 0 )
1069 LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the gossip list.\n");
1071 uint32_t first_border;
1072 uint32_t second_border;
1074 first_border = round (alpha * sampler_size_est_need);
1075 second_border = first_border + round (beta * sampler_size_est_need);
1077 GNUNET_array_grow (gossip_list, gossip_list_size, second_border);
1079 for ( i = 0 ; i < first_border ; i++ )
1080 { // TODO use RPS_sampler_get_n_rand_peers
1081 /* Update gossip list with peers received through PUSHes */
1082 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
1084 gossip_list[i] = push_list[r_index];
1085 // TODO change the peer_flags accordingly
1088 for ( i = first_border ; i < second_border ; i++ )
1090 /* Update gossip list with peers received through PULLs */
1091 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
1093 gossip_list[i] = pull_list[r_index];
1094 // TODO change the peer_flags accordingly
1097 for ( i = second_border ; i < gossip_list_size ; i++ )
1099 /* Update gossip list with peers from history */
1100 RPS_sampler_get_n_rand_peers (hist_update, NULL, 1, GNUNET_NO);
1101 num_hist_update_tasks++;
1102 // TODO change the peer_flags accordingly
1108 LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the gossip list. ()\n");
1110 // TODO independent of that also get some peers from CADET_get_peers()?
1113 /* Update samplers */
1115 for ( i = 0 ; i < push_list_size ; i++ )
1117 RPS_sampler_update_list (&push_list[i]);
1118 // TODO set in_flag?
1121 for ( i = 0 ; i < pull_list_size ; i++ )
1123 RPS_sampler_update_list (&pull_list[i]);
1124 // TODO set in_flag?
1128 /* Empty push/pull lists */
1129 GNUNET_array_grow (push_list, push_list_size, 0);
1130 GNUNET_array_grow (pull_list, pull_list_size, 0);
1132 struct GNUNET_TIME_Relative time_next_round;
1133 struct GNUNET_TIME_Relative half_round_interval;
1134 unsigned int rand_delay;
1136 /* Compute random time value between .5 * round_interval and 1.5 *round_interval */
1137 half_round_interval = GNUNET_TIME_relative_divide (round_interval, 2);
1141 * Compute random value between (0 and 1) * round_interval
1142 * via multiplying round_interval with a 'fraction' (0 to value)/value
1144 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT_MAX/10);
1145 time_next_round = GNUNET_TIME_relative_multiply (round_interval, rand_delay);
1146 time_next_round = GNUNET_TIME_relative_divide (time_next_round, UINT_MAX/10);
1147 time_next_round = GNUNET_TIME_relative_add (time_next_round, half_round_interval);
1148 } while (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == time_next_round.rel_value_us);
1150 /* Schedule next round */
1151 do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_round, NULL);
1152 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
1157 * Open a connection to given peer and store channel and mq.
1160 insertCB (void *cls, const struct GNUNET_PeerIdentity *id)
1162 // We open a channel to be notified when this peer goes down.
1163 (void) get_channel (peer_map, id);
1168 * Close the connection to given peer and delete channel and mq.
1171 removeCB (void *cls, const struct GNUNET_PeerIdentity *id)
1174 struct PeerContext *ctx;
1176 s = RPS_sampler_count_id (id);
1179 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, id))
1181 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, id);
1182 if (NULL != ctx->send_channel)
1184 if (NULL != ctx->mq)
1186 GNUNET_MQ_destroy (ctx->mq);
1188 // may already be freed at shutdown of cadet
1189 //GNUNET_CADET_channel_destroy (ctx->send_channel);
1191 // TODO cleanup peer
1192 (void) GNUNET_CONTAINER_multipeermap_remove_all (peer_map, id);
1198 rps_start (struct GNUNET_SERVER_Handle *server);
1201 * This is called from GNUNET_CADET_get_peers().
1203 * It is called on every peer(ID) that cadet somehow has contact with.
1204 * We use those to initialise the sampler.
1207 init_peer_cb (void *cls,
1208 const struct GNUNET_PeerIdentity *peer,
1209 int tunnel, // "Do we have a tunnel towards this peer?"
1210 unsigned int n_paths, // "Number of known paths towards this peer"
1211 unsigned int best_path) // "How long is the best path?
1212 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
1214 struct GNUNET_SERVER_Handle *server;
1215 struct PeerOutstandingOp out_op;
1216 struct PeerContext *peer_ctx;
1218 server = (struct GNUNET_SERVER_Handle *) cls;
1221 LOG (GNUNET_ERROR_TYPE_DEBUG,
1222 "Got peer %s (at %p) from CADET (gossip_list_size: %u)\n",
1223 GNUNET_i2s (peer), peer, gossip_list_size);
1225 // maybe create a function for that
1226 peer_ctx = get_peer_ctx (peer_map, peer);
1227 if (GNUNET_NO == insert_in_sampler_scheduled (peer_ctx))
1229 out_op.op = insert_in_sampler;
1230 GNUNET_array_append (peer_ctx->outstanding_ops, peer_ctx->num_outstanding_ops, out_op);
1233 if (GNUNET_NO == insert_in_gossip_list_scheduled (peer_ctx))
1235 out_op.op = insert_in_gossip_list;
1236 GNUNET_array_append (peer_ctx->outstanding_ops, peer_ctx->num_outstanding_ops, out_op);
1239 /* Issue livelyness test on peer */
1240 (void) get_channel (peer_map, peer);
1242 // send push/pull to each of those peers?
1250 * Callback used to clean the multipeermap.
1253 peer_remove_cb (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1255 struct PeerContext *peer_ctx;
1257 peer_ctx = (struct PeerContext *) value;
1259 if ( NULL != peer_ctx->mq)
1260 GNUNET_MQ_destroy (peer_ctx->mq);
1262 if ( NULL != peer_ctx->send_channel)
1263 GNUNET_CADET_channel_destroy (peer_ctx->send_channel);
1265 if ( NULL != peer_ctx->recv_channel)
1266 GNUNET_CADET_channel_destroy (peer_ctx->recv_channel);
1268 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_remove_all (peer_map, key))
1269 LOG (GNUNET_ERROR_TYPE_WARNING, "removing peer from peer_map failed\n");
1276 * Task run during shutdown.
1282 shutdown_task (void *cls,
1283 const struct GNUNET_SCHEDULER_TaskContext *tc)
1285 LOG (GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
1287 if ( NULL != do_round_task )
1289 GNUNET_SCHEDULER_cancel (do_round_task);
1290 do_round_task = NULL;
1294 if (GNUNET_SYSERR == GNUNET_CONTAINER_multipeermap_iterate (peer_map, peer_remove_cb, NULL))
1295 LOG (GNUNET_ERROR_TYPE_WARNING,
1296 "Iterating over peers to disconnect from them was cancelled\n");
1298 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
1300 GNUNET_NSE_disconnect (nse);
1301 GNUNET_CADET_disconnect (cadet_handle);
1302 GNUNET_free (own_identity);
1303 RPS_sampler_destroy ();
1304 GNUNET_array_grow (request_deltas, request_deltas_size, 0);
1305 GNUNET_array_grow (gossip_list, gossip_list_size, 0);
1306 GNUNET_array_grow (push_list, push_list_size, 0);
1307 GNUNET_array_grow (pull_list, pull_list_size, 0);
1312 * A client disconnected. Remove all of its data structure entries.
1314 * @param cls closure, NULL
1315 * @param client identification of the client
1318 handle_client_disconnect (void *cls,
1319 struct GNUNET_SERVER_Client * client)
1324 * Handle the channel a peer opens to us.
1326 * @param cls The closure
1327 * @param channel The channel the peer wants to establish
1328 * @param initiator The peer's peer ID
1329 * @param port The port the channel is being established over
1330 * @param options Further options
1333 handle_inbound_channel (void *cls,
1334 struct GNUNET_CADET_Channel *channel,
1335 const struct GNUNET_PeerIdentity *initiator,
1337 enum GNUNET_CADET_ChannelOption options)
1339 struct PeerContext *ctx;
1341 LOG (GNUNET_ERROR_TYPE_DEBUG,
1342 "New channel was established to us (Peer %s).\n",
1343 GNUNET_i2s (initiator));
1345 GNUNET_assert (NULL != channel);
1347 // we might not even store the recv_channel
1349 ctx = get_peer_ctx (peer_map, initiator);
1350 if (NULL != ctx->recv_channel)
1352 ctx->recv_channel = channel;
1355 // FIXME there might already be an established channel
1357 //ctx->peer_flags = IN_OTHER_GOSSIP_LIST;
1358 ctx->mq = NULL; // TODO create mq?
1360 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, initiator, ctx,
1361 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1362 return NULL; // TODO
1366 * This is called when a remote peer destroys a channel.
1368 * @param cls The closure
1369 * @param channel The channel being closed
1370 * @param channel_ctx The context associated with this channel
1373 cleanup_channel (void *cls,
1374 const struct GNUNET_CADET_Channel *channel,
1377 struct GNUNET_PeerIdentity *peer;
1378 struct PeerContext *peer_ctx;
1380 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel to remote peer was destroyed.\n");
1382 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
1383 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
1384 // Guess simply casting isn't the nicest way...
1385 // FIXME wait for cadet to change this function
1386 RPS_sampler_reinitialise_by_value (peer);
1388 peer_ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
1389 /* Somwewhat {ab,re}use the iterator function */
1390 (void) peer_remove_cb (peer, peer, peer_ctx);
1395 * Actually start the service.
1398 rps_start (struct GNUNET_SERVER_Handle *server)
1400 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1401 {&handle_client_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
1402 sizeof (struct GNUNET_RPS_CS_RequestMessage)},
1403 {&handle_client_seed, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_SEED, 0},
1407 GNUNET_SERVER_add_handlers (server, handlers);
1408 GNUNET_SERVER_disconnect_notify (server,
1409 &handle_client_disconnect,
1411 LOG (GNUNET_ERROR_TYPE_DEBUG, "Ready to receive requests from clients\n");
1414 num_hist_update_tasks = 0;
1416 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
1417 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
1419 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1426 * Process statistics requests.
1428 * @param cls closure
1429 * @param server the initialized server
1430 * @param c configuration to use
1434 struct GNUNET_SERVER_Handle *server,
1435 const struct GNUNET_CONFIGURATION_Handle *c)
1437 // TODO check what this does -- copied from gnunet-boss
1438 // - seems to work as expected
1439 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
1441 LOG (GNUNET_ERROR_TYPE_DEBUG, "RPS started\n");
1448 own_identity = GNUNET_new (struct GNUNET_PeerIdentity);
1449 GNUNET_CRYPTO_get_peer_identity (cfg, own_identity); // TODO check return value
1450 GNUNET_assert (NULL != own_identity);
1451 LOG (GNUNET_ERROR_TYPE_DEBUG, "Own identity is %s (at %p).\n", GNUNET_i2s (own_identity), own_identity);
1454 /* Get time interval from the configuration */
1455 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
1459 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read ROUNDINTERVAL from config\n");
1460 GNUNET_SCHEDULER_shutdown ();
1464 /* Get initial size of sampler/gossip list from the configuration */
1465 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "RPS",
1467 (long long unsigned int *) &sampler_size_est_need))
1469 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read INITSIZE from config\n");
1470 GNUNET_SCHEDULER_shutdown ();
1473 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", sampler_size_est_need);
1479 /* connect to NSE */
1480 nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
1481 // TODO check whether that was successful
1482 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to NSE\n");
1487 // TODO initialise thresholds - ?
1489 /* Get alpha from the configuration */
1490 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_float (cfg, "RPS",
1494 LOG (GNUNET_ERROR_TYPE_DEBUG, "No ALPHA specified in the config\n");
1496 LOG (GNUNET_ERROR_TYPE_DEBUG, "ALPHA is %f\n", alpha);
1498 /* Get beta from the configuration */
1499 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_float (cfg, "RPS",
1503 LOG (GNUNET_ERROR_TYPE_DEBUG, "No BETA specified in the config\n");
1505 LOG (GNUNET_ERROR_TYPE_DEBUG, "BETA is %f\n", beta);
1507 // TODO check that alpha + beta < 1
1509 peer_map = GNUNET_CONTAINER_multipeermap_create (sampler_size_est_need, GNUNET_NO);
1512 /* Initialise cadet */
1513 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1514 {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH ,
1515 sizeof (struct GNUNET_MessageHeader)},
1516 {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
1517 sizeof (struct GNUNET_MessageHeader)},
1518 {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY , 0},
1522 const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
1523 cadet_handle = GNUNET_CADET_connect (cfg,
1525 &handle_inbound_channel,
1529 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to CADET\n");
1532 /* Initialise sampler */
1533 struct GNUNET_TIME_Relative half_round_interval;
1534 struct GNUNET_TIME_Relative max_round_interval;
1536 half_round_interval = GNUNET_TIME_relative_multiply (round_interval, .5);
1537 max_round_interval = GNUNET_TIME_relative_add (round_interval, half_round_interval);
1539 RPS_sampler_init (sampler_size_est_need, max_round_interval,
1540 insertCB, NULL, removeCB, NULL);
1541 sampler_size = sampler_size_est_need;
1543 /* Initialise push and pull maps */
1550 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
1551 GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, server);
1553 // TODO send push/pull to each of those peers?
1558 * The main function for the rps service.
1560 * @param argc number of arguments from the command line
1561 * @param argv command line arguments
1562 * @return 0 ok, 1 on error
1565 main (int argc, char *const *argv)
1567 return (GNUNET_OK ==
1568 GNUNET_SERVICE_run (argc,
1571 GNUNET_SERVICE_OPTION_NONE,
1572 &run, NULL)) ? 0 : 1;
1575 /* end of gnunet-service-rps.c */