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 connect to friends
53 // TODO store peers somewhere
57 // hist_size_init, hist_size_max
62 static const struct GNUNET_CONFIGURATION_Handle *cfg;
67 static struct GNUNET_PeerIdentity *own_identity;
70 struct GNUNET_PeerIdentity *
71 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list, unsigned int size,
72 const struct GNUNET_PeerIdentity *ignore_list, unsigned int ignore_size);
75 /***********************************************************************
76 * Housekeeping with peers
77 ***********************************************************************/
80 * Struct used to store the context of a connected client.
85 * The message queue to communicate with the client.
87 struct GNUNET_MQ_Handle *mq;
91 * Used to keep track in what lists single peerIDs are.
95 PULL_REPLY_PENDING = 0x01,
96 IN_OTHER_GOSSIP_LIST = 0x02, // unneeded?
97 IN_OWN_SAMPLER_LIST = 0x04, // unneeded?
98 IN_OWN_GOSSIP_LIST = 0x08, // unneeded?
101 * We set this bit when we can be sure the other peer is/was live.
108 * Functions of this type can be used to be stored at a peer for later execution.
110 typedef void (* PeerOp) (void *cls, const struct GNUNET_PeerIdentity *peer);
113 * Outstanding operation on peer consisting of callback and closure
115 struct PeerOutstandingOp
130 * Struct used to keep track of other peer's status
132 * This is stored in a multipeermap.
137 * In own gossip/sampler list, in other's gossip/sampler list
142 * Message queue open to client
144 struct GNUNET_MQ_Handle *mq;
147 * Channel open to client.
149 struct GNUNET_CADET_Channel *send_channel;
152 * Channel open from client.
154 struct GNUNET_CADET_Channel *recv_channel; // unneeded?
157 * Array of outstanding operations on this peer.
159 struct PeerOutstandingOp *outstanding_ops;
162 * Number of outstanding operations.
164 unsigned int num_outstanding_ops;
165 //size_t num_outstanding_ops;
168 * Handle to the callback given to cadet_ntfy_tmt_rdy()
170 * To be canceled on shutdown.
172 struct GNUNET_CADET_TransmitHandle *is_live_task;
175 * This is pobably followed by 'statistical' data (when we first saw
176 * him, how did we get his ID, how many pushes (in a timeinterval),
181 /***********************************************************************
182 * /Housekeeping with peers
183 ***********************************************************************/
185 /***********************************************************************
187 ***********************************************************************/
190 * Set of all peers to keep track of them.
192 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
196 * The gossiped list of peers.
198 static struct GNUNET_PeerIdentity *gossip_list;
201 * Size of the gossiped list
203 //static unsigned int gossip_list_size;
204 static uint32_t gossip_list_size;
208 * The actual size of the sampler
210 static unsigned int sampler_size;
211 //size_t sampler_size;
214 * The size of sampler we need to be able to satisfy the client's need of
217 static unsigned int sampler_size_client_need;
220 * The size of sampler we need to be able to satisfy the Brahms protocol's
221 * need of random peers.
223 * This is directly taken as the #gossip_list_size on update of the
226 * This is one minimum size the sampler grows to.
228 static unsigned int sampler_size_est_need;
232 * Percentage of total peer number in the gossip list
233 * to send random PUSHes to
238 * Percentage of total peer number in the gossip list
239 * to send random PULLs to
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 * List with the peers we sent requests to.
334 struct GNUNET_PeerIdentity *pending_pull_reply_list;
337 * Size of #pending_pull_reply_list.
339 uint32_t pending_pull_reply_list_size;
343 * Number of history update tasks.
345 uint32_t num_hist_update_tasks;
348 /***********************************************************************
350 ***********************************************************************/
353 /***********************************************************************
355 ***********************************************************************/
358 * Check if peer is already in peer array.
361 in_arr (const struct GNUNET_PeerIdentity *array,
362 unsigned int arr_size,
363 const struct GNUNET_PeerIdentity *peer)
365 GNUNET_assert (NULL != peer);
370 GNUNET_assert (NULL != array);
375 while (0 != GNUNET_CRYPTO_cmp_peer_identity (&array[i], peer) &&
386 * Remove peer from list.
389 rem_from_list (struct GNUNET_PeerIdentity *peer_list,
390 unsigned int *list_size,
391 const struct GNUNET_PeerIdentity *peer)
395 for ( i = 0 ; i < *list_size ; i++ )
397 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&peer_list[i], peer))
399 if (i < *list_size -1)
400 { /* Not at the last entry -- shift peers left */
401 memcpy (&peer_list[i], &peer_list[i +1],
402 (*list_size - i -1) * sizeof (struct GNUNET_PeerIdentity));
404 /* Remove last entry (should be now useless PeerID) */
405 GNUNET_array_grow (peer_list, *list_size, *list_size -1);
411 * Get random peer from the given list but don't return one from the @a ignore_list.
413 struct GNUNET_PeerIdentity *
414 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list,
416 const struct GNUNET_PeerIdentity *ignore_list,
417 uint32_t ignore_size)
421 struct GNUNET_PeerIdentity *tmp_peer_list;
422 struct GNUNET_PeerIdentity *peer;
424 GNUNET_assert (NULL != peer_list);
427 tmp_peer_list = NULL;
428 GNUNET_array_grow (tmp_peer_list, tmp_size, list_size);
429 memcpy (tmp_peer_list, peer_list, list_size * sizeof (struct GNUNET_PeerIdentity));
430 peer = GNUNET_new (struct GNUNET_PeerIdentity);
435 * Choose the r_index of the peer we want to return
436 * at random from the interval of the gossip list
438 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
441 *peer = tmp_peer_list[r_index];
442 if (in_arr (tmp_peer_list, list_size, peer))
444 rem_from_list (tmp_peer_list, &tmp_size, peer);
450 } while (NULL == peer);
452 GNUNET_free (tmp_peer_list);
459 * Get the context of a peer. If not existing, create.
462 get_peer_ctx (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
463 const struct GNUNET_PeerIdentity *peer)
465 struct PeerContext *ctx;
467 if ( GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
469 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
473 ctx = GNUNET_new (struct PeerContext);
476 ctx->send_channel = NULL;
477 ctx->recv_channel = NULL;
478 ctx->outstanding_ops = NULL;
479 ctx->num_outstanding_ops = 0;
480 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
481 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
488 * Put random peer from sampler into the gossip list as history update.
491 hist_update (void *cls, struct GNUNET_PeerIdentity *ids, uint32_t num_peers)
493 GNUNET_assert (1 == num_peers);
495 if (gossip_list_size < sampler_size_est_need)
496 GNUNET_array_append (gossip_list, gossip_list_size, *ids);
498 if (0 < num_hist_update_tasks)
499 num_hist_update_tasks--;
504 * Callback that is called when a channel was effectively established.
505 * This is given to ntfy_tmt_rdy and called when the channel was
506 * successfully established.
509 peer_is_live (void *cls, size_t size, void *buf)
511 struct GNUNET_PeerIdentity *peer;
512 struct PeerContext *peer_ctx;
514 peer = (struct GNUNET_PeerIdentity *) cls;
515 peer_ctx = get_peer_ctx (peer_map, peer);
516 peer_ctx->peer_flags |= LIVING;
518 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer %s is live\n", GNUNET_i2s (peer));
520 if (0 != peer_ctx->num_outstanding_ops)
521 { /* Call outstanding operations */
524 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
525 peer_ctx->outstanding_ops[i].op (peer_ctx->outstanding_ops[i].op_cls, peer);
526 GNUNET_array_grow (peer_ctx->outstanding_ops, peer_ctx->num_outstanding_ops, 0);
537 * Get the channel of a peer. If not existing, create.
539 struct GNUNET_CADET_Channel *
540 get_channel (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
541 const struct GNUNET_PeerIdentity *peer)
543 struct PeerContext *ctx;
544 struct GNUNET_PeerIdentity *tmp_peer;
546 ctx = get_peer_ctx (peer_map, peer);
547 if (NULL == ctx->send_channel)
549 ctx->send_channel = GNUNET_CADET_channel_create (cadet_handle, NULL, peer,
550 GNUNET_RPS_CADET_PORT,
551 GNUNET_CADET_OPTION_RELIABLE);
553 if (NULL == ctx->recv_channel)
555 tmp_peer = GNUNET_new (struct GNUNET_PeerIdentity);
557 ctx->is_live_task = GNUNET_CADET_notify_transmit_ready (ctx->send_channel, GNUNET_NO,
558 GNUNET_TIME_UNIT_FOREVER_REL,
559 0, peer_is_live, tmp_peer);
562 // do I have to explicitly put it in the peer_map?
563 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
564 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
566 return ctx->send_channel;
571 * Get the message queue of a specific peer.
573 * If we already have a message queue open to this client,
574 * simply return it, otherways create one.
576 struct GNUNET_MQ_Handle *
577 get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
578 const struct GNUNET_PeerIdentity *peer_id)
580 struct PeerContext *ctx;
582 ctx = get_peer_ctx (peer_map, peer_id);
585 (void) get_channel (peer_map, peer_id);
586 ctx->mq = GNUNET_CADET_mq_create (ctx->send_channel);
587 //do I have to explicitly put it in the peer_map?
588 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer_id, ctx,
589 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
596 * Sum all time relatives of an array.
598 struct GNUNET_TIME_Relative
599 T_relative_sum (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
601 struct GNUNET_TIME_Relative sum;
604 sum = GNUNET_TIME_UNIT_ZERO;
605 for ( i = 0 ; i < arr_size ; i++ )
607 sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
614 * Compute the average of given time relatives.
616 struct GNUNET_TIME_Relative
617 T_relative_avg (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
619 return GNUNET_TIME_relative_divide (T_relative_sum (rel_array, arr_size), arr_size);
624 * Insert PeerID in #pull_list
626 * Called once we know a peer is live.
629 insert_in_pull_list (void *cls, const struct GNUNET_PeerIdentity *peer)
631 if (GNUNET_NO == in_arr (pull_list, pull_list_size, peer))
632 GNUNET_array_append (pull_list, pull_list_size, *peer);
636 * Check whether #insert_in_pull_list was already scheduled
639 insert_in_pull_list_scheduled (const struct PeerContext *peer_ctx)
643 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
644 if (insert_in_pull_list == peer_ctx->outstanding_ops[i].op)
651 * Insert PeerID in #gossip_list
653 * Called once we know a peer is live.
656 insert_in_gossip_list (void *cls, const struct GNUNET_PeerIdentity *peer)
658 if (GNUNET_NO == in_arr (gossip_list, gossip_list_size, peer))
659 GNUNET_array_append (gossip_list, gossip_list_size, *peer);
663 * Check whether #insert_in_pull_list was already scheduled
666 insert_in_gossip_list_scheduled (const struct PeerContext *peer_ctx)
670 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
671 if (insert_in_gossip_list == peer_ctx->outstanding_ops[i].op)
678 * Update sampler with given PeerID.
681 insert_in_sampler (void *cls, const struct GNUNET_PeerIdentity *peer)
683 RPS_sampler_update_list (peer);
688 * Check whether #insert_in_sampler was already scheduled
691 insert_in_sampler_scheduled (const struct PeerContext *peer_ctx)
695 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
696 if (insert_in_sampler== peer_ctx->outstanding_ops[i].op)
703 * Wrapper around #RPS_sampler_resize()
708 uint32_t bigger_size;
712 if (sampler_size_est_need > sampler_size_client_need)
713 bigger_size = sampler_size_client_need;
715 bigger_size = sampler_size_est_need;
717 // TODO respect the request rate, min, max
718 if (sampler_size > bigger_size*4)
720 RPS_sampler_resize (sampler_size/2);
722 else if (sampler_size < bigger_size)
724 RPS_sampler_resize (sampler_size*2);
730 * Estimate request rate
732 * Called every time we receive a request from the client.
737 struct GNUNET_TIME_Relative max_round_duration;
739 if (request_deltas_size > req_counter)
741 if ( 1 < req_counter)
743 /* Shift last request deltas to the right */
744 memcpy (&request_deltas[1],
746 (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
748 /* Add current delta to beginning */
749 request_deltas[0] = GNUNET_TIME_absolute_get_difference (last_request,
750 GNUNET_TIME_absolute_get ());
751 request_rate = T_relative_avg (request_deltas, req_counter);
753 /* Compute the duration a round will maximally take */
754 max_round_duration = GNUNET_TIME_relative_add (round_interval,
755 GNUNET_TIME_relative_divide (round_interval, 2));
757 /* Set the estimated size the sampler has to have to
758 * satisfy the current client request rate */
759 sampler_size_client_need = max_round_duration.rel_value_us / request_rate.rel_value_us;
761 /* Resize the sampler */
764 last_request = GNUNET_TIME_absolute_get ();
768 /***********************************************************************
770 ***********************************************************************/
773 * Function called by NSE.
775 * Updates sizes of sampler list and gossip list and adapt those lists
779 nse_callback (void *cls, struct GNUNET_TIME_Absolute timestamp, double logestimate, double std_dev)
782 //double scale; // TODO this might go gloabal/config
784 LOG (GNUNET_ERROR_TYPE_DEBUG,
785 "Received a ns estimate - logest: %f, std_dev: %f (old_size: %f)\n",
786 logestimate, std_dev, sampler_size);
788 estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
789 // GNUNET_NSE_log_estimate_to_n (logestimate);
790 estimate = pow (estimate, 1./3);
791 // TODO add if std_dev is a number
792 // estimate += (std_dev * scale);
793 if ( 0 < estimate ) {
794 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
795 sampler_size_est_need = estimate;
797 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
799 /* If the NSE has changed adapt the lists accordingly */
805 * Callback called once the requested PeerIDs are ready.
807 * Sends those to the requesting client.
809 void client_respond (void *cls,
810 struct GNUNET_PeerIdentity *ids, uint32_t num_peers)
812 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler returned %" PRIX32 " peers\n", num_peers);
813 struct GNUNET_MQ_Envelope *ev;
814 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
815 struct GNUNET_SERVER_Client *client;
816 struct client_ctx *cli_ctx;
818 client = (struct GNUNET_SERVER_Client *) cls;
820 ev = GNUNET_MQ_msg_extra (out_msg,
821 num_peers * sizeof (struct GNUNET_PeerIdentity),
822 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
823 out_msg->num_peers = htonl (num_peers);
827 num_peers * sizeof (struct GNUNET_PeerIdentity));
830 cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct client_ctx);
831 if ( NULL == cli_ctx ) {
832 cli_ctx = GNUNET_new (struct client_ctx);
833 cli_ctx->mq = GNUNET_MQ_queue_for_server_client (client);
834 GNUNET_SERVER_client_set_user_context (client, cli_ctx);
837 GNUNET_MQ_send (cli_ctx->mq, ev);
842 * Handle RPS request from the client.
845 * @param client identification of the client
846 * @param message the actual message
849 handle_client_request (void *cls,
850 struct GNUNET_SERVER_Client *client,
851 const struct GNUNET_MessageHeader *message)
853 struct GNUNET_RPS_CS_RequestMessage *msg;
857 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
859 num_peers = ntohl (msg->num_peers);
861 for (i = 0 ; i < num_peers ; i++)
864 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client requested %" PRIX32 " random peer(s).\n", num_peers);
866 RPS_sampler_get_n_rand_peers (client_respond, client, num_peers, GNUNET_YES);
868 GNUNET_SERVER_receive_done (client,
874 * Handle seed from the client.
877 * @param client identification of the client
878 * @param message the actual message
881 handle_client_seed (void *cls,
882 struct GNUNET_SERVER_Client *client,
883 const struct GNUNET_MessageHeader *message)
885 struct GNUNET_RPS_CS_SeedMessage *in_msg;
886 struct GNUNET_PeerIdentity *peers;
889 if (sizeof (struct GNUNET_RPS_CS_SeedMessage) < ntohs (message->size))
892 GNUNET_SERVER_receive_done (client,
895 in_msg = (struct GNUNET_RPS_CS_SeedMessage *) message;
896 if ((ntohs (message->size) - sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
897 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
900 GNUNET_SERVER_receive_done (client,
904 in_msg = (struct GNUNET_RPS_CS_SeedMessage *) message;
905 peers = (struct GNUNET_PeerIdentity *) &message[1];
907 for ( i = 0 ; i < ntohl (in_msg->num_peers) ; i++ )
908 RPS_sampler_update_list (&peers[i]);
910 GNUNET_SERVER_receive_done (client,
916 * Handle a PUSH message from another peer.
918 * Check the proof of work and store the PeerID
919 * in the temporary list for pushed PeerIDs.
922 * @param channel The channel the PUSH was received over
923 * @param channel_ctx The context associated with this channel
924 * @param msg The message header
927 handle_peer_push (void *cls,
928 struct GNUNET_CADET_Channel *channel,
930 const struct GNUNET_MessageHeader *msg)
932 const struct GNUNET_PeerIdentity *peer;
934 // (check the proof of work)
936 peer = (const struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
937 // FIXME wait for cadet to change this function
938 LOG (GNUNET_ERROR_TYPE_DEBUG, "PUSH received (%s)\n", GNUNET_i2s (peer));
940 /* Add the sending peer to the push_list */
941 if (GNUNET_NO == in_arr (push_list, pull_list_size, peer))
942 GNUNET_array_append (push_list, push_list_size, *peer);
948 * Handle PULL REQUEST request message from another peer.
950 * Reply with the gossip list of PeerIDs.
953 * @param channel The channel the PUSH was received over
954 * @param channel_ctx The context associated with this channel
955 * @param msg The message header
958 handle_peer_pull_request (void *cls,
959 struct GNUNET_CADET_Channel *channel,
961 const struct GNUNET_MessageHeader *msg)
963 struct GNUNET_PeerIdentity *peer;
964 struct GNUNET_MQ_Handle *mq;
965 struct GNUNET_MQ_Envelope *ev;
966 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
969 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (channel,
970 GNUNET_CADET_OPTION_PEER);
971 // FIXME wait for cadet to change this function
972 LOG (GNUNET_ERROR_TYPE_DEBUG,
973 "PULL REQUEST from peer %s received, going to send %u peers\n",
974 GNUNET_i2s (peer), gossip_list_size);
976 mq = get_mq (peer_map, peer);
978 ev = GNUNET_MQ_msg_extra (out_msg,
979 gossip_list_size * sizeof (struct GNUNET_PeerIdentity),
980 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
981 //out_msg->num_peers = htonl (gossip_list_size);
982 out_msg->num_peers = htonl (gossip_list_size);
983 memcpy (&out_msg[1], gossip_list,
984 gossip_list_size * sizeof (struct GNUNET_PeerIdentity));
986 GNUNET_MQ_send (mq, ev);
992 * Handle PULL REPLY message from another peer.
994 * Check whether we sent a corresponding request and
995 * whether this reply is the first one.
998 * @param channel The channel the PUSH was received over
999 * @param channel_ctx The context associated with this channel
1000 * @param msg The message header
1003 handle_peer_pull_reply (void *cls,
1004 struct GNUNET_CADET_Channel *channel,
1006 const struct GNUNET_MessageHeader *msg)
1008 LOG (GNUNET_ERROR_TYPE_DEBUG, "PULL REPLY received\n");
1010 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
1011 struct GNUNET_PeerIdentity *peers;
1012 struct PeerContext *peer_ctx;
1013 struct GNUNET_PeerIdentity *sender;
1014 struct PeerContext *sender_ctx;
1015 struct PeerOutstandingOp out_op;
1018 if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->size))
1020 GNUNET_break_op (0); // At the moment our own implementation seems to break that.
1021 return GNUNET_SYSERR;
1023 in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
1024 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) / sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1026 LOG (GNUNET_ERROR_TYPE_ERROR, "message says it sends %" PRIu64 " peers, have space for %i peers\n",
1027 ntohl (in_msg->num_peers),
1028 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) / sizeof (struct GNUNET_PeerIdentity));
1029 GNUNET_break_op (0);
1030 return GNUNET_SYSERR;
1033 sender = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
1034 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
1035 // Guess simply casting isn't the nicest way...
1036 // FIXME wait for cadet to change this function
1037 sender_ctx = get_peer_ctx (peer_map, sender);
1039 if (0 == (peer_ctx->peer_flags || PULL_REPLY_PENDING))
1041 GNUNET_break_op (0);
1045 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1046 for ( i = 0 ; i < ntohl (in_msg->num_peers) ; i++ )
1048 peer_ctx = get_peer_ctx (peer_map, &peers[i]);
1049 if ((0 != (peer_ctx->peer_flags && LIVING)) ||
1050 NULL != peer_ctx->recv_channel)
1052 if (GNUNET_NO == in_arr (pull_list, pull_list_size, &peers[i]))
1053 GNUNET_array_append (pull_list, pull_list_size, peers[i]);
1055 else if (GNUNET_NO == insert_in_pull_list_scheduled (peer_ctx))
1057 out_op.op = insert_in_pull_list;
1058 GNUNET_array_append (peer_ctx->outstanding_ops, peer_ctx->num_outstanding_ops, out_op);
1062 sender_ctx->peer_flags &= (~PULL_REPLY_PENDING);
1063 rem_from_list (pending_pull_reply_list, &pending_pull_reply_list_size, sender);
1070 * Send out PUSHes and PULLs.
1072 * This is executed regylary.
1075 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1077 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round\n");
1080 unsigned int *permut;
1081 unsigned int n_peers; /* Number of peers we send pushes/pulls to */
1082 struct GNUNET_MQ_Envelope *ev;
1083 const struct GNUNET_PeerIdentity *peer;
1084 struct GNUNET_MQ_Handle *mq;
1086 // TODO log lists, ...
1089 /* Would it make sense to have one shuffeled gossip list and then
1090 * to send PUSHes to first alpha peers, PULL requests to next beta peers and
1091 * use the rest to update sampler?
1092 * in essence get random peers with consumption */
1095 if (0 < gossip_list_size)
1097 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
1098 (unsigned int) gossip_list_size);
1099 if (0 != gossip_list_size)
1101 n_peers = round (alpha * gossip_list_size);
1104 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to send pushes to %u (%f * %u) peers.\n",
1105 n_peers, alpha, gossip_list_size);
1106 for ( i = 0 ; i < n_peers ; i++ )
1108 peer = &gossip_list[permut[i]];
1109 if (own_identity != peer) // TODO
1110 { // FIXME if this fails schedule/loop this for later
1111 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending PUSH to peer %s of gossiped list.\n", GNUNET_i2s (peer));
1113 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
1114 mq = get_mq (peer_map, peer);
1115 GNUNET_MQ_send (mq, ev);
1122 /* Send PULL requests */
1123 //permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list->size);
1124 if (0 != gossip_list_size)
1126 n_peers = round (beta * gossip_list_size);
1129 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to send pulls to %u (%f * %u) peers.\n",
1130 n_peers, beta, gossip_list_size);
1131 for ( i = 0 ; i < n_peers ; i++ )
1133 peer = get_rand_peer_ignore_list (gossip_list, gossip_list_size,
1134 pending_pull_reply_list, pending_pull_reply_list_size);
1137 GNUNET_array_append (pending_pull_reply_list, pending_pull_reply_list_size, *peer);
1139 if (own_identity != peer)
1140 { // FIXME if this fails schedule/loop this for later
1141 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending PULL request to peer %s of gossiped list.\n", GNUNET_i2s (peer));
1143 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
1145 mq = get_mq (peer_map, peer);
1146 GNUNET_MQ_send (mq, ev);
1153 /* Update gossip list */
1156 if ( push_list_size <= alpha * gossip_list_size &&
1157 push_list_size != 0 &&
1158 pull_list_size != 0 )
1160 LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the gossip list.\n");
1162 uint32_t first_border;
1163 uint32_t second_border;
1165 first_border = round (alpha * sampler_size_est_need);
1166 second_border = first_border + round (beta * sampler_size_est_need);
1168 GNUNET_array_grow (gossip_list, gossip_list_size, second_border);
1170 for ( i = 0 ; i < first_border ; i++ )
1171 { // TODO use RPS_sampler_get_n_rand_peers
1172 /* Update gossip list with peers received through PUSHes */
1173 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
1175 gossip_list[i] = push_list[r_index];
1176 // TODO change the peer_flags accordingly
1179 for ( i = first_border ; i < second_border ; i++ )
1181 /* Update gossip list with peers received through PULLs */
1182 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
1184 gossip_list[i] = pull_list[r_index];
1185 // TODO change the peer_flags accordingly
1188 for ( i = second_border ; i < gossip_list_size ; i++ )
1190 /* Update gossip list with peers from history */
1191 RPS_sampler_get_n_rand_peers (hist_update, NULL, 1, GNUNET_NO);
1192 num_hist_update_tasks++;
1193 // TODO change the peer_flags accordingly
1199 LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the gossip list. ()\n");
1201 // TODO independent of that also get some peers from CADET_get_peers()?
1204 /* Update samplers */
1206 for ( i = 0 ; i < push_list_size ; i++ )
1208 RPS_sampler_update_list (&push_list[i]);
1209 // TODO set in_flag?
1212 for ( i = 0 ; i < pull_list_size ; i++ )
1214 RPS_sampler_update_list (&pull_list[i]);
1215 // TODO set in_flag?
1219 /* Empty push/pull lists */
1220 GNUNET_array_grow (push_list, push_list_size, 0);
1221 GNUNET_array_grow (pull_list, pull_list_size, 0);
1223 struct GNUNET_TIME_Relative time_next_round;
1224 struct GNUNET_TIME_Relative half_round_interval;
1225 unsigned int rand_delay;
1227 /* Compute random time value between .5 * round_interval and 1.5 *round_interval */
1228 half_round_interval = GNUNET_TIME_relative_divide (round_interval, 2);
1232 * Compute random value between (0 and 1) * round_interval
1233 * via multiplying round_interval with a 'fraction' (0 to value)/value
1235 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT_MAX/10);
1236 time_next_round = GNUNET_TIME_relative_multiply (round_interval, rand_delay);
1237 time_next_round = GNUNET_TIME_relative_divide (time_next_round, UINT_MAX/10);
1238 time_next_round = GNUNET_TIME_relative_add (time_next_round, half_round_interval);
1239 } while (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == time_next_round.rel_value_us);
1241 /* Schedule next round */
1242 do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_round, NULL);
1243 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
1248 * Open a connection to given peer and store channel and mq.
1251 insertCB (void *cls, const struct GNUNET_PeerIdentity *id)
1253 // We open a channel to be notified when this peer goes down.
1254 (void) get_channel (peer_map, id);
1259 * Close the connection to given peer and delete channel and mq.
1262 removeCB (void *cls, const struct GNUNET_PeerIdentity *id)
1265 struct PeerContext *ctx;
1267 s = RPS_sampler_count_id (id);
1270 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, id))
1272 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, id);
1273 if (NULL != ctx->send_channel)
1275 if (NULL != ctx->mq)
1277 GNUNET_MQ_destroy (ctx->mq);
1279 // may already be freed at shutdown of cadet
1280 //GNUNET_CADET_channel_destroy (ctx->send_channel);
1282 // TODO cleanup peer
1283 (void) GNUNET_CONTAINER_multipeermap_remove_all (peer_map, id);
1289 rps_start (struct GNUNET_SERVER_Handle *server);
1292 * This is called from GNUNET_CADET_get_peers().
1294 * It is called on every peer(ID) that cadet somehow has contact with.
1295 * We use those to initialise the sampler.
1298 init_peer_cb (void *cls,
1299 const struct GNUNET_PeerIdentity *peer,
1300 int tunnel, // "Do we have a tunnel towards this peer?"
1301 unsigned int n_paths, // "Number of known paths towards this peer"
1302 unsigned int best_path) // "How long is the best path?
1303 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
1305 struct GNUNET_SERVER_Handle *server;
1306 struct PeerOutstandingOp out_op;
1307 struct PeerContext *peer_ctx;
1309 server = (struct GNUNET_SERVER_Handle *) cls;
1312 LOG (GNUNET_ERROR_TYPE_DEBUG,
1313 "Got peer %s (at %p) from CADET (gossip_list_size: %u)\n",
1314 GNUNET_i2s (peer), peer, gossip_list_size);
1316 // maybe create a function for that
1317 peer_ctx = get_peer_ctx (peer_map, peer);
1318 if (GNUNET_NO == insert_in_sampler_scheduled (peer_ctx))
1320 out_op.op = insert_in_sampler;
1321 GNUNET_array_append (peer_ctx->outstanding_ops, peer_ctx->num_outstanding_ops, out_op);
1324 if (GNUNET_NO == insert_in_gossip_list_scheduled (peer_ctx))
1326 out_op.op = insert_in_gossip_list;
1327 GNUNET_array_append (peer_ctx->outstanding_ops, peer_ctx->num_outstanding_ops, out_op);
1330 /* Issue livelyness test on peer */
1331 (void) get_channel (peer_map, peer);
1333 // send push/pull to each of those peers?
1341 * Callback used to clean the multipeermap.
1344 peer_remove_cb (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1346 struct PeerContext *peer_ctx;
1348 peer_ctx = (struct PeerContext *) value;
1350 if ( NULL != peer_ctx->mq)
1351 GNUNET_MQ_destroy (peer_ctx->mq);
1353 if ( NULL != peer_ctx->is_live_task)
1354 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
1356 if ( NULL != peer_ctx->send_channel)
1357 GNUNET_CADET_channel_destroy (peer_ctx->send_channel);
1359 if ( NULL != peer_ctx->recv_channel)
1360 GNUNET_CADET_channel_destroy (peer_ctx->recv_channel);
1362 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_remove_all (peer_map, key))
1363 LOG (GNUNET_ERROR_TYPE_WARNING, "removing peer from peer_map failed\n");
1370 * Task run during shutdown.
1376 shutdown_task (void *cls,
1377 const struct GNUNET_SCHEDULER_TaskContext *tc)
1379 LOG (GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
1381 if ( NULL != do_round_task )
1383 GNUNET_SCHEDULER_cancel (do_round_task);
1384 do_round_task = NULL;
1388 if (GNUNET_SYSERR == GNUNET_CONTAINER_multipeermap_iterate (peer_map, peer_remove_cb, NULL))
1389 LOG (GNUNET_ERROR_TYPE_WARNING,
1390 "Iterating over peers to disconnect from them was cancelled\n");
1392 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
1394 GNUNET_NSE_disconnect (nse);
1395 GNUNET_CADET_disconnect (cadet_handle);
1396 GNUNET_free (own_identity);
1397 RPS_sampler_destroy ();
1398 GNUNET_array_grow (request_deltas, request_deltas_size, 0);
1399 GNUNET_array_grow (gossip_list, gossip_list_size, 0);
1400 GNUNET_array_grow (push_list, push_list_size, 0);
1401 GNUNET_array_grow (pull_list, pull_list_size, 0);
1406 * A client disconnected. Remove all of its data structure entries.
1408 * @param cls closure, NULL
1409 * @param client identification of the client
1412 handle_client_disconnect (void *cls,
1413 struct GNUNET_SERVER_Client * client)
1419 * Handle the channel a peer opens to us.
1421 * @param cls The closure
1422 * @param channel The channel the peer wants to establish
1423 * @param initiator The peer's peer ID
1424 * @param port The port the channel is being established over
1425 * @param options Further options
1428 handle_inbound_channel (void *cls,
1429 struct GNUNET_CADET_Channel *channel,
1430 const struct GNUNET_PeerIdentity *initiator,
1432 enum GNUNET_CADET_ChannelOption options)
1434 struct PeerContext *ctx;
1436 LOG (GNUNET_ERROR_TYPE_DEBUG,
1437 "New channel was established to us (Peer %s).\n",
1438 GNUNET_i2s (initiator));
1440 GNUNET_assert (NULL != channel);
1442 // we might not even store the recv_channel
1444 ctx = get_peer_ctx (peer_map, initiator);
1445 if (NULL != ctx->recv_channel)
1447 ctx->recv_channel = channel;
1450 ctx->peer_flags |= LIVING;
1452 //ctx->peer_flags = IN_OTHER_GOSSIP_LIST;
1455 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, initiator, ctx,
1456 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1458 return NULL; // TODO
1463 * This is called when a remote peer destroys a channel.
1465 * @param cls The closure
1466 * @param channel The channel being closed
1467 * @param channel_ctx The context associated with this channel
1470 cleanup_channel (void *cls,
1471 const struct GNUNET_CADET_Channel *channel,
1474 struct GNUNET_PeerIdentity *peer;
1475 struct PeerContext *peer_ctx;
1477 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel to remote peer was destroyed.\n");
1479 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
1480 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
1481 // Guess simply casting isn't the nicest way...
1482 // FIXME wait for cadet to change this function
1483 RPS_sampler_reinitialise_by_value (peer);
1485 peer_ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
1486 /* Somwewhat {ab,re}use the iterator function */
1487 (void) peer_remove_cb (peer, peer, peer_ctx);
1492 * Actually start the service.
1495 rps_start (struct GNUNET_SERVER_Handle *server)
1497 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1498 {&handle_client_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
1499 sizeof (struct GNUNET_RPS_CS_RequestMessage)},
1500 {&handle_client_seed, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_SEED, 0},
1504 GNUNET_SERVER_add_handlers (server, handlers);
1505 GNUNET_SERVER_disconnect_notify (server,
1506 &handle_client_disconnect,
1508 LOG (GNUNET_ERROR_TYPE_DEBUG, "Ready to receive requests from clients\n");
1511 num_hist_update_tasks = 0;
1513 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
1514 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
1516 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1523 * Process statistics requests.
1525 * @param cls closure
1526 * @param server the initialized server
1527 * @param c configuration to use
1531 struct GNUNET_SERVER_Handle *server,
1532 const struct GNUNET_CONFIGURATION_Handle *c)
1534 // TODO check what this does -- copied from gnunet-boss
1535 // - seems to work as expected
1536 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
1538 LOG (GNUNET_ERROR_TYPE_DEBUG, "RPS started\n");
1545 own_identity = GNUNET_new (struct GNUNET_PeerIdentity);
1546 GNUNET_CRYPTO_get_peer_identity (cfg, own_identity); // TODO check return value
1547 GNUNET_assert (NULL != own_identity);
1548 LOG (GNUNET_ERROR_TYPE_DEBUG, "Own identity is %s (at %p).\n", GNUNET_i2s (own_identity), own_identity);
1551 /* Get time interval from the configuration */
1552 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
1556 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read ROUNDINTERVAL from config\n");
1557 GNUNET_SCHEDULER_shutdown ();
1561 /* Get initial size of sampler/gossip list from the configuration */
1562 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "RPS",
1564 (long long unsigned int *) &sampler_size_est_need))
1566 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read INITSIZE from config\n");
1567 GNUNET_SCHEDULER_shutdown ();
1570 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", sampler_size_est_need);
1576 /* connect to NSE */
1577 nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
1578 // TODO check whether that was successful
1579 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to NSE\n");
1585 peer_map = GNUNET_CONTAINER_multipeermap_create (sampler_size_est_need, GNUNET_NO);
1588 /* Initialise cadet */
1589 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1590 {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH ,
1591 sizeof (struct GNUNET_MessageHeader)},
1592 {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
1593 sizeof (struct GNUNET_MessageHeader)},
1594 {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY , 0},
1598 const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
1599 cadet_handle = GNUNET_CADET_connect (cfg,
1601 &handle_inbound_channel,
1605 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to CADET\n");
1608 /* Initialise sampler */
1609 struct GNUNET_TIME_Relative half_round_interval;
1610 struct GNUNET_TIME_Relative max_round_interval;
1612 half_round_interval = GNUNET_TIME_relative_multiply (round_interval, .5);
1613 max_round_interval = GNUNET_TIME_relative_add (round_interval, half_round_interval);
1615 RPS_sampler_init (sampler_size_est_need, max_round_interval,
1616 insertCB, NULL, removeCB, NULL);
1617 sampler_size = sampler_size_est_need;
1619 /* Initialise push and pull maps */
1624 pending_pull_reply_list = NULL;
1625 pending_pull_reply_list_size = 0;
1628 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
1629 GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, server);
1631 // TODO send push/pull to each of those peers?
1636 * The main function for the rps service.
1638 * @param argc number of arguments from the command line
1639 * @param argv command line arguments
1640 * @return 0 ok, 1 on error
1643 main (int argc, char *const *argv)
1645 return (GNUNET_OK ==
1646 GNUNET_SERVICE_run (argc,
1649 GNUNET_SERVICE_OPTION_NONE,
1650 &run, NULL)) ? 0 : 1;
1653 /* end of gnunet-service-rps.c */