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 api -- possibility of getting weak random peer immideately)
47 // TODO malicious peer
49 // TODO connect to friends
51 // TODO store peers somewhere
55 // hist_size_init, hist_size_max
60 static const struct GNUNET_CONFIGURATION_Handle *cfg;
65 static struct GNUNET_PeerIdentity *own_identity;
68 struct GNUNET_PeerIdentity *
69 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list, unsigned int size,
70 const struct GNUNET_PeerIdentity *ignore_list, unsigned int ignore_size);
73 /***********************************************************************
74 * Housekeeping with peers
75 ***********************************************************************/
78 * Struct used to store the context of a connected client.
83 * The message queue to communicate with the client.
85 struct GNUNET_MQ_Handle *mq;
89 * Used to keep track in what lists single peerIDs are.
93 PULL_REPLY_PENDING = 0x01,
94 IN_OTHER_GOSSIP_LIST = 0x02, // unneeded?
95 IN_OWN_SAMPLER_LIST = 0x04, // unneeded?
96 IN_OWN_GOSSIP_LIST = 0x08, // unneeded?
99 * We set this bit when we can be sure the other peer is/was live.
106 * Functions of this type can be used to be stored at a peer for later execution.
108 typedef void (* PeerOp) (void *cls, const struct GNUNET_PeerIdentity *peer);
111 * Outstanding operation on peer consisting of callback and closure
113 struct PeerOutstandingOp
128 * Struct used to keep track of other peer's status
130 * This is stored in a multipeermap.
135 * In own gossip/sampler list, in other's gossip/sampler list
140 * Message queue open to client
142 struct GNUNET_MQ_Handle *mq;
145 * Channel open to client.
147 struct GNUNET_CADET_Channel *send_channel;
150 * Channel open from client.
152 struct GNUNET_CADET_Channel *recv_channel; // unneeded?
155 * Array of outstanding operations on this peer.
157 struct PeerOutstandingOp *outstanding_ops;
160 * Number of outstanding operations.
162 unsigned int num_outstanding_ops;
163 //size_t num_outstanding_ops;
166 * Handle to the callback given to cadet_ntfy_tmt_rdy()
168 * To be canceled on shutdown.
170 struct GNUNET_CADET_TransmitHandle *is_live_task;
173 * This is pobably followed by 'statistical' data (when we first saw
174 * him, how did we get his ID, how many pushes (in a timeinterval),
179 /***********************************************************************
180 * /Housekeeping with peers
181 ***********************************************************************/
183 /***********************************************************************
185 ***********************************************************************/
188 * Set of all peers to keep track of them.
190 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
194 * The gossiped list of peers.
196 static struct GNUNET_PeerIdentity *gossip_list;
199 * Size of the gossiped list
201 //static unsigned int gossip_list_size;
202 static uint32_t gossip_list_size;
206 * The actual size of the sampler
208 static unsigned int sampler_size;
209 //size_t sampler_size;
212 * The size of sampler we need to be able to satisfy the client's need of
215 static unsigned int sampler_size_client_need;
218 * The size of sampler we need to be able to satisfy the Brahms protocol's
219 * need of random peers.
221 * This is directly taken as the #gossip_list_size on update of the
224 * This is one minimum size the sampler grows to.
226 static unsigned int sampler_size_est_need;
230 * Percentage of total peer number in the gossip list
231 * to send random PUSHes to
236 * Percentage of total peer number in the gossip list
237 * to send random PULLs to
242 * The percentage gamma of history updates.
243 * Simply 1 - alpha - beta
248 * Identifier for the main task that runs periodically.
250 static struct GNUNET_SCHEDULER_Task *do_round_task;
253 * Time inverval the do_round task runs in.
255 static struct GNUNET_TIME_Relative round_interval;
260 * List to store peers received through pushes temporary.
262 * TODO -> multipeermap
264 static struct GNUNET_PeerIdentity *push_list;
267 * Size of the push_list;
269 static unsigned int push_list_size;
270 //size_t push_list_size;
273 * List to store peers received through pulls temporary.
275 * TODO -> multipeermap
277 static struct GNUNET_PeerIdentity *pull_list;
280 * Size of the pull_list;
282 static unsigned int pull_list_size;
283 //size_t pull_list_size;
289 static struct GNUNET_NSE_Handle *nse;
294 static struct GNUNET_CADET_Handle *cadet_handle;
300 * Only needed in the beginning to check how many of the 64 deltas
303 static unsigned int req_counter;
306 * Time of the last request we received.
308 * Used to compute the expected request rate.
310 static struct GNUNET_TIME_Absolute last_request;
313 * Size of #request_deltas.
315 #define REQUEST_DELTAS_SIZE 64
316 static unsigned int request_deltas_size = REQUEST_DELTAS_SIZE;
319 * Last 64 deltas between requests
321 static struct GNUNET_TIME_Relative request_deltas[REQUEST_DELTAS_SIZE];
324 * The prediction of the rate of requests
326 static struct GNUNET_TIME_Relative request_rate;
330 * List with the peers we sent requests to.
332 struct GNUNET_PeerIdentity *pending_pull_reply_list;
335 * Size of #pending_pull_reply_list.
337 uint32_t pending_pull_reply_list_size;
341 * Number of history update tasks.
343 uint32_t num_hist_update_tasks;
346 /***********************************************************************
348 ***********************************************************************/
351 /***********************************************************************
353 ***********************************************************************/
356 * Check if peer is already in peer array.
359 in_arr (const struct GNUNET_PeerIdentity *array,
360 unsigned int arr_size,
361 const struct GNUNET_PeerIdentity *peer)
363 GNUNET_assert (NULL != peer);
368 GNUNET_assert (NULL != array);
373 while (0 != GNUNET_CRYPTO_cmp_peer_identity (&array[i], peer) &&
384 * Remove peer from list.
387 rem_from_list (struct GNUNET_PeerIdentity *peer_list,
388 unsigned int *list_size,
389 const struct GNUNET_PeerIdentity *peer)
393 for ( i = 0 ; i < *list_size ; i++ )
395 if (0 == GNUNET_CRYPTO_cmp_peer_identity (&peer_list[i], peer))
397 if (i < *list_size -1)
398 { /* Not at the last entry -- shift peers left */
399 memcpy (&peer_list[i], &peer_list[i +1],
400 (*list_size - i -1) * sizeof (struct GNUNET_PeerIdentity));
402 /* Remove last entry (should be now useless PeerID) */
403 GNUNET_array_grow (peer_list, *list_size, *list_size -1);
409 * Get random peer from the given list but don't return one from the @a ignore_list.
411 struct GNUNET_PeerIdentity *
412 get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list,
414 const struct GNUNET_PeerIdentity *ignore_list,
415 uint32_t ignore_size)
419 struct GNUNET_PeerIdentity *tmp_peer_list;
420 struct GNUNET_PeerIdentity *peer;
422 GNUNET_assert (NULL != peer_list);
425 tmp_peer_list = NULL;
426 GNUNET_array_grow (tmp_peer_list, tmp_size, list_size);
427 memcpy (tmp_peer_list, peer_list, list_size * sizeof (struct GNUNET_PeerIdentity));
428 peer = GNUNET_new (struct GNUNET_PeerIdentity);
433 * Choose the r_index of the peer we want to return
434 * at random from the interval of the gossip list
436 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
439 *peer = tmp_peer_list[r_index];
440 if (in_arr (tmp_peer_list, list_size, peer))
442 rem_from_list (tmp_peer_list, &tmp_size, peer);
448 } while (NULL == peer);
450 GNUNET_free (tmp_peer_list);
457 * Get the context of a peer. If not existing, create.
460 get_peer_ctx (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
461 const struct GNUNET_PeerIdentity *peer)
463 struct PeerContext *ctx;
465 if ( GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
467 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
471 ctx = GNUNET_new (struct PeerContext);
474 ctx->send_channel = NULL;
475 ctx->recv_channel = NULL;
476 ctx->outstanding_ops = NULL;
477 ctx->num_outstanding_ops = 0;
478 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
479 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
486 * Put random peer from sampler into the gossip list as history update.
489 hist_update (void *cls, struct GNUNET_PeerIdentity *ids, uint32_t num_peers)
491 GNUNET_assert (1 == num_peers);
493 if (gossip_list_size < sampler_size_est_need)
494 GNUNET_array_append (gossip_list, gossip_list_size, *ids);
496 if (0 < num_hist_update_tasks)
497 num_hist_update_tasks--;
502 * Callback that is called when a channel was effectively established.
503 * This is given to ntfy_tmt_rdy and called when the channel was
504 * successfully established.
507 peer_is_live (void *cls, size_t size, void *buf)
509 struct GNUNET_PeerIdentity *peer;
510 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);
531 //if (NULL != peer_ctx->is_live_task)
533 // GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
534 // peer_ctx->is_live_task = NULL; // needed?
541 * Get the channel of a peer. If not existing, create.
543 struct GNUNET_CADET_Channel *
544 get_channel (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
545 const struct GNUNET_PeerIdentity *peer)
547 struct PeerContext *ctx;
548 struct GNUNET_PeerIdentity *tmp_peer;
550 ctx = get_peer_ctx (peer_map, peer);
551 if (NULL == ctx->send_channel)
553 ctx->send_channel = GNUNET_CADET_channel_create (cadet_handle, NULL, peer,
554 GNUNET_RPS_CADET_PORT,
555 GNUNET_CADET_OPTION_RELIABLE);
557 if (NULL == ctx->recv_channel)
559 tmp_peer = GNUNET_new (struct GNUNET_PeerIdentity);
561 ctx->is_live_task = GNUNET_CADET_notify_transmit_ready (ctx->send_channel, GNUNET_NO,
562 GNUNET_TIME_UNIT_FOREVER_REL,
563 0, peer_is_live, tmp_peer);
566 // do I have to explicitly put it in the peer_map?
567 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
568 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
570 return ctx->send_channel;
575 * Get the message queue of a specific peer.
577 * If we already have a message queue open to this client,
578 * simply return it, otherways create one.
580 struct GNUNET_MQ_Handle *
581 get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
582 const struct GNUNET_PeerIdentity *peer_id)
584 struct PeerContext *ctx;
586 ctx = get_peer_ctx (peer_map, peer_id);
589 (void) get_channel (peer_map, peer_id);
590 ctx->mq = GNUNET_CADET_mq_create (ctx->send_channel);
591 //do I have to explicitly put it in the peer_map?
592 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer_id, ctx,
593 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
600 * Sum all time relatives of an array.
602 struct GNUNET_TIME_Relative
603 T_relative_sum (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
605 struct GNUNET_TIME_Relative sum;
608 sum = GNUNET_TIME_UNIT_ZERO;
609 for ( i = 0 ; i < arr_size ; i++ )
611 sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
618 * Compute the average of given time relatives.
620 struct GNUNET_TIME_Relative
621 T_relative_avg (const struct GNUNET_TIME_Relative *rel_array, uint32_t arr_size)
623 return GNUNET_TIME_relative_divide (T_relative_sum (rel_array, arr_size), arr_size);
628 * Insert PeerID in #pull_list
630 * Called once we know a peer is live.
633 insert_in_pull_list (void *cls, const struct GNUNET_PeerIdentity *peer)
635 if (GNUNET_NO == in_arr (pull_list, pull_list_size, peer))
636 GNUNET_array_append (pull_list, pull_list_size, *peer);
640 * Check whether #insert_in_pull_list was already scheduled
643 insert_in_pull_list_scheduled (const struct PeerContext *peer_ctx)
647 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
648 if (insert_in_pull_list == peer_ctx->outstanding_ops[i].op)
655 * Insert PeerID in #gossip_list
657 * Called once we know a peer is live.
660 insert_in_gossip_list (void *cls, const struct GNUNET_PeerIdentity *peer)
662 if (GNUNET_NO == in_arr (gossip_list, gossip_list_size, peer))
663 GNUNET_array_append (gossip_list, gossip_list_size, *peer);
667 * Check whether #insert_in_pull_list was already scheduled
670 insert_in_gossip_list_scheduled (const struct PeerContext *peer_ctx)
674 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
675 if (insert_in_gossip_list == peer_ctx->outstanding_ops[i].op)
682 * Update sampler with given PeerID.
685 insert_in_sampler (void *cls, const struct GNUNET_PeerIdentity *peer)
687 RPS_sampler_update_list (peer);
692 * Check whether #insert_in_sampler was already scheduled
695 insert_in_sampler_scheduled (const struct PeerContext *peer_ctx)
699 for ( i = 0 ; i < peer_ctx->num_outstanding_ops ; i++ )
700 if (insert_in_sampler== peer_ctx->outstanding_ops[i].op)
707 * Wrapper around #RPS_sampler_resize()
712 uint32_t bigger_size;
716 if (sampler_size_est_need > sampler_size_client_need)
717 bigger_size = sampler_size_client_need;
719 bigger_size = sampler_size_est_need;
721 // TODO respect the min, max
722 if (sampler_size > bigger_size*4)
724 RPS_sampler_resize (sampler_size/2);
726 else if (sampler_size < bigger_size)
728 RPS_sampler_resize (sampler_size*2);
734 * Estimate request rate
736 * Called every time we receive a request from the client.
741 struct GNUNET_TIME_Relative max_round_duration;
743 if (request_deltas_size > req_counter)
745 if ( 1 < req_counter)
747 /* Shift last request deltas to the right */
748 memcpy (&request_deltas[1],
750 (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
752 /* Add current delta to beginning */
753 request_deltas[0] = GNUNET_TIME_absolute_get_difference (last_request,
754 GNUNET_TIME_absolute_get ());
755 request_rate = T_relative_avg (request_deltas, req_counter);
757 /* Compute the duration a round will maximally take */
758 max_round_duration = GNUNET_TIME_relative_add (round_interval,
759 GNUNET_TIME_relative_divide (round_interval, 2));
761 /* Set the estimated size the sampler has to have to
762 * satisfy the current client request rate */
763 sampler_size_client_need = max_round_duration.rel_value_us / request_rate.rel_value_us;
765 /* Resize the sampler */
768 last_request = GNUNET_TIME_absolute_get ();
772 /***********************************************************************
774 ***********************************************************************/
777 * Function called by NSE.
779 * Updates sizes of sampler list and gossip list and adapt those lists
783 nse_callback (void *cls, struct GNUNET_TIME_Absolute timestamp, double logestimate, double std_dev)
786 //double scale; // TODO this might go gloabal/config
788 LOG (GNUNET_ERROR_TYPE_DEBUG,
789 "Received a ns estimate - logest: %f, std_dev: %f (old_size: %f)\n",
790 logestimate, std_dev, sampler_size);
792 estimate = GNUNET_NSE_log_estimate_to_n (logestimate);
793 // GNUNET_NSE_log_estimate_to_n (logestimate);
794 estimate = pow (estimate, 1./3);
795 // TODO add if std_dev is a number
796 // estimate += (std_dev * scale);
797 if ( 0 < estimate ) {
798 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
799 sampler_size_est_need = estimate;
801 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
803 /* If the NSE has changed adapt the lists accordingly */
809 * Callback called once the requested PeerIDs are ready.
811 * Sends those to the requesting client.
813 void client_respond (void *cls,
814 struct GNUNET_PeerIdentity *ids, uint32_t num_peers)
816 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler returned %" PRIX32 " peers\n", num_peers);
817 struct GNUNET_MQ_Envelope *ev;
818 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
819 struct GNUNET_SERVER_Client *client;
820 uint32_t size_needed;
821 struct client_ctx *cli_ctx;
823 client = (struct GNUNET_SERVER_Client *) cls;
825 size_needed = sizeof (struct GNUNET_RPS_CS_ReplyMessage) +
826 num_peers * sizeof (struct GNUNET_PeerIdentity);
828 GNUNET_assert (GNUNET_SERVER_MAX_MESSAGE_SIZE >= size_needed);
830 ev = GNUNET_MQ_msg_extra (out_msg,
831 num_peers * sizeof (struct GNUNET_PeerIdentity),
832 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
833 out_msg->num_peers = htonl (num_peers);
837 num_peers * sizeof (struct GNUNET_PeerIdentity));
840 cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct client_ctx);
841 if ( NULL == cli_ctx ) {
842 cli_ctx = GNUNET_new (struct client_ctx);
843 cli_ctx->mq = GNUNET_MQ_queue_for_server_client (client);
844 GNUNET_SERVER_client_set_user_context (client, cli_ctx);
847 GNUNET_MQ_send (cli_ctx->mq, ev);
852 * Handle RPS request from the client.
855 * @param client identification of the client
856 * @param message the actual message
859 handle_client_request (void *cls,
860 struct GNUNET_SERVER_Client *client,
861 const struct GNUNET_MessageHeader *message)
863 struct GNUNET_RPS_CS_RequestMessage *msg;
865 uint32_t size_needed;
868 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
870 num_peers = ntohl (msg->num_peers);
871 size_needed = sizeof (struct GNUNET_RPS_CS_ReplyMessage) +
872 num_peers * sizeof (struct GNUNET_PeerIdentity);
874 if (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed)
876 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
880 for (i = 0 ; i < num_peers ; i++)
883 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client requested %" PRIX32 " random peer(s).\n", num_peers);
885 RPS_sampler_get_n_rand_peers (client_respond, client, num_peers, GNUNET_YES);
887 GNUNET_SERVER_receive_done (client,
893 * Handle seed from the client.
896 * @param client identification of the client
897 * @param message the actual message
900 handle_client_seed (void *cls,
901 struct GNUNET_SERVER_Client *client,
902 const struct GNUNET_MessageHeader *message)
904 struct GNUNET_RPS_CS_SeedMessage *in_msg;
905 struct GNUNET_PeerIdentity *peers;
908 if (sizeof (struct GNUNET_RPS_CS_SeedMessage) < ntohs (message->size))
911 GNUNET_SERVER_receive_done (client,
914 in_msg = (struct GNUNET_RPS_CS_SeedMessage *) message;
915 if ((ntohs (message->size) - sizeof (struct GNUNET_RPS_CS_SeedMessage)) /
916 sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
919 GNUNET_SERVER_receive_done (client,
923 in_msg = (struct GNUNET_RPS_CS_SeedMessage *) message;
924 peers = (struct GNUNET_PeerIdentity *) &message[1];
926 for ( i = 0 ; i < ntohl (in_msg->num_peers) ; i++ )
927 RPS_sampler_update_list (&peers[i]);
929 GNUNET_SERVER_receive_done (client,
935 * Handle a PUSH message from another peer.
937 * Check the proof of work and store the PeerID
938 * in the temporary list for pushed PeerIDs.
941 * @param channel The channel the PUSH was received over
942 * @param channel_ctx The context associated with this channel
943 * @param msg The message header
946 handle_peer_push (void *cls,
947 struct GNUNET_CADET_Channel *channel,
949 const struct GNUNET_MessageHeader *msg)
951 const struct GNUNET_PeerIdentity *peer;
953 // (check the proof of work)
955 peer = (const struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
956 // FIXME wait for cadet to change this function
957 LOG (GNUNET_ERROR_TYPE_DEBUG, "PUSH received (%s)\n", GNUNET_i2s (peer));
959 /* Add the sending peer to the push_list */
960 if (GNUNET_NO == in_arr (push_list, pull_list_size, peer))
961 GNUNET_array_append (push_list, push_list_size, *peer);
967 * Handle PULL REQUEST request message from another peer.
969 * Reply with the gossip list of PeerIDs.
972 * @param channel The channel the PUSH was received over
973 * @param channel_ctx The context associated with this channel
974 * @param msg The message header
977 handle_peer_pull_request (void *cls,
978 struct GNUNET_CADET_Channel *channel,
980 const struct GNUNET_MessageHeader *msg)
982 struct GNUNET_PeerIdentity *peer;
984 struct GNUNET_MQ_Handle *mq;
985 struct GNUNET_MQ_Envelope *ev;
986 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
989 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (channel,
990 GNUNET_CADET_OPTION_PEER);
991 // FIXME wait for cadet to change this function
993 /* Compute actual size */
994 send_size = sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) +
995 gossip_list_size * sizeof (struct GNUNET_PeerIdentity);
997 if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size)
998 /* Compute number of peers to send
999 * If too long, simply truncate */
1000 send_size = (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE -
1001 sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
1002 sizeof (struct GNUNET_PeerIdentity);
1004 send_size = gossip_list_size;
1006 LOG (GNUNET_ERROR_TYPE_DEBUG,
1007 "PULL REQUEST from peer %s received, going to send %u peers\n",
1008 GNUNET_i2s (peer), send_size);
1010 mq = get_mq (peer_map, peer);
1012 ev = GNUNET_MQ_msg_extra (out_msg,
1013 send_size * sizeof (struct GNUNET_PeerIdentity),
1014 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
1015 //out_msg->num_peers = htonl (gossip_list_size);
1016 out_msg->num_peers = htonl (send_size);
1017 memcpy (&out_msg[1], gossip_list,
1018 send_size * sizeof (struct GNUNET_PeerIdentity));
1020 GNUNET_MQ_send (mq, ev);
1026 * Handle PULL REPLY message from another peer.
1028 * Check whether we sent a corresponding request and
1029 * whether this reply is the first one.
1031 * @param cls Closure
1032 * @param channel The channel the PUSH was received over
1033 * @param channel_ctx The context associated with this channel
1034 * @param msg The message header
1037 handle_peer_pull_reply (void *cls,
1038 struct GNUNET_CADET_Channel *channel,
1040 const struct GNUNET_MessageHeader *msg)
1042 LOG (GNUNET_ERROR_TYPE_DEBUG, "PULL REPLY received\n");
1044 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
1045 struct GNUNET_PeerIdentity *peers;
1046 struct PeerContext *peer_ctx;
1047 struct GNUNET_PeerIdentity *sender;
1048 struct PeerContext *sender_ctx;
1049 struct PeerOutstandingOp out_op;
1052 if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->size))
1054 GNUNET_break_op (0); // At the moment our own implementation seems to break that.
1055 return GNUNET_SYSERR;
1057 in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
1058 if ((ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) / sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
1060 LOG (GNUNET_ERROR_TYPE_ERROR, "message says it sends %" PRIu64 " peers, have space for %i peers\n",
1061 ntohl (in_msg->num_peers),
1062 (ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) / sizeof (struct GNUNET_PeerIdentity));
1063 GNUNET_break_op (0);
1064 return GNUNET_SYSERR;
1067 sender = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
1068 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
1069 // Guess simply casting isn't the nicest way...
1070 // FIXME wait for cadet to change this function
1071 sender_ctx = get_peer_ctx (peer_map, sender);
1073 if (0 == (peer_ctx->peer_flags || PULL_REPLY_PENDING))
1075 GNUNET_break_op (0);
1079 peers = (struct GNUNET_PeerIdentity *) &msg[1];
1080 for ( i = 0 ; i < ntohl (in_msg->num_peers) ; i++ )
1082 peer_ctx = get_peer_ctx (peer_map, &peers[i]);
1083 if ((0 != (peer_ctx->peer_flags && LIVING)) ||
1084 NULL != peer_ctx->recv_channel)
1086 if (GNUNET_NO == in_arr (pull_list, pull_list_size, &peers[i]))
1087 GNUNET_array_append (pull_list, pull_list_size, peers[i]);
1089 else if (GNUNET_NO == insert_in_pull_list_scheduled (peer_ctx))
1091 out_op.op = insert_in_pull_list;
1092 GNUNET_array_append (peer_ctx->outstanding_ops, peer_ctx->num_outstanding_ops, out_op);
1096 sender_ctx->peer_flags &= (~PULL_REPLY_PENDING);
1097 rem_from_list (pending_pull_reply_list, &pending_pull_reply_list_size, sender);
1104 * Send out PUSHes and PULLs.
1106 * This is executed regylary.
1109 do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1111 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round\n");
1114 unsigned int *permut;
1115 unsigned int n_peers; /* Number of peers we send pushes/pulls to */
1116 struct GNUNET_MQ_Envelope *ev;
1117 const struct GNUNET_PeerIdentity *peer;
1118 struct GNUNET_MQ_Handle *mq;
1120 // TODO log lists, ...
1123 /* Would it make sense to have one shuffeled gossip list and then
1124 * to send PUSHes to first alpha peers, PULL requests to next beta peers and
1125 * use the rest to update sampler?
1126 * in essence get random peers with consumption */
1129 if (0 < gossip_list_size)
1131 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
1132 (unsigned int) gossip_list_size);
1133 if (0 != gossip_list_size)
1135 n_peers = round (alpha * gossip_list_size);
1138 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to send pushes to %u (%f * %u) peers.\n",
1139 n_peers, alpha, gossip_list_size);
1140 for ( i = 0 ; i < n_peers ; i++ )
1142 peer = &gossip_list[permut[i]];
1143 if (own_identity != peer) // TODO
1144 { // FIXME if this fails schedule/loop this for later
1145 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending PUSH to peer %s of gossiped list.\n", GNUNET_i2s (peer));
1147 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
1148 mq = get_mq (peer_map, peer);
1149 GNUNET_MQ_send (mq, ev);
1156 /* Send PULL requests */
1157 //permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list->size);
1158 if (0 != gossip_list_size)
1160 n_peers = round (beta * gossip_list_size);
1163 LOG (GNUNET_ERROR_TYPE_DEBUG, "Going to send pulls to %u (%f * %u) peers.\n",
1164 n_peers, beta, gossip_list_size);
1165 for ( i = 0 ; i < n_peers ; i++ )
1167 peer = get_rand_peer_ignore_list (gossip_list, gossip_list_size,
1168 pending_pull_reply_list, pending_pull_reply_list_size);
1171 GNUNET_array_append (pending_pull_reply_list, pending_pull_reply_list_size, *peer);
1173 if (own_identity != peer)
1174 { // FIXME if this fails schedule/loop this for later
1175 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending PULL request to peer %s of gossiped list.\n", GNUNET_i2s (peer));
1177 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
1179 mq = get_mq (peer_map, peer);
1180 GNUNET_MQ_send (mq, ev);
1187 /* Update gossip list */
1190 if ( push_list_size <= alpha * gossip_list_size &&
1191 push_list_size != 0 &&
1192 pull_list_size != 0 )
1194 LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the gossip list.\n");
1196 uint32_t first_border;
1197 uint32_t second_border;
1199 first_border = round (alpha * sampler_size_est_need);
1200 second_border = first_border + round (beta * sampler_size_est_need);
1202 GNUNET_array_grow (gossip_list, gossip_list_size, second_border);
1204 for ( i = 0 ; i < first_border ; i++ )
1205 { // TODO use RPS_sampler_get_n_rand_peers
1206 /* Update gossip list with peers received through PUSHes */
1207 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
1209 gossip_list[i] = push_list[r_index];
1210 // TODO change the peer_flags accordingly
1213 for ( i = first_border ; i < second_border ; i++ )
1215 /* Update gossip list with peers received through PULLs */
1216 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
1218 gossip_list[i] = pull_list[r_index];
1219 // TODO change the peer_flags accordingly
1222 for ( i = second_border ; i < gossip_list_size ; i++ )
1224 /* Update gossip list with peers from history */
1225 RPS_sampler_get_n_rand_peers (hist_update, NULL, 1, GNUNET_NO);
1226 num_hist_update_tasks++;
1227 // TODO change the peer_flags accordingly
1233 LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the gossip list. ()\n");
1235 // TODO independent of that also get some peers from CADET_get_peers()?
1238 /* Update samplers */
1240 for ( i = 0 ; i < push_list_size ; i++ )
1242 RPS_sampler_update_list (&push_list[i]);
1243 // TODO set in_flag?
1246 for ( i = 0 ; i < pull_list_size ; i++ )
1248 RPS_sampler_update_list (&pull_list[i]);
1249 // TODO set in_flag?
1253 /* Empty push/pull lists */
1254 GNUNET_array_grow (push_list, push_list_size, 0);
1255 GNUNET_array_grow (pull_list, pull_list_size, 0);
1257 struct GNUNET_TIME_Relative time_next_round;
1258 struct GNUNET_TIME_Relative half_round_interval;
1259 unsigned int rand_delay;
1261 /* Compute random time value between .5 * round_interval and 1.5 *round_interval */
1262 half_round_interval = GNUNET_TIME_relative_divide (round_interval, 2);
1266 * Compute random value between (0 and 1) * round_interval
1267 * via multiplying round_interval with a 'fraction' (0 to value)/value
1269 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT_MAX/10);
1270 time_next_round = GNUNET_TIME_relative_multiply (round_interval, rand_delay);
1271 time_next_round = GNUNET_TIME_relative_divide (time_next_round, UINT_MAX/10);
1272 time_next_round = GNUNET_TIME_relative_add (time_next_round, half_round_interval);
1273 } while (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == time_next_round.rel_value_us);
1275 /* Schedule next round */
1276 do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_round, NULL);
1277 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
1282 * Open a connection to given peer and store channel and mq.
1285 insertCB (void *cls, const struct GNUNET_PeerIdentity *id)
1287 // We open a channel to be notified when this peer goes down.
1288 (void) get_channel (peer_map, id);
1293 * Close the connection to given peer and delete channel and mq.
1296 removeCB (void *cls, const struct GNUNET_PeerIdentity *id)
1299 struct PeerContext *ctx;
1301 s = RPS_sampler_count_id (id);
1304 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, id))
1306 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, id);
1307 if (NULL != ctx->send_channel)
1309 if (NULL != ctx->mq)
1311 GNUNET_MQ_destroy (ctx->mq);
1313 // may already be freed at shutdown of cadet
1314 //GNUNET_CADET_channel_destroy (ctx->send_channel);
1316 // TODO cleanup peer
1317 (void) GNUNET_CONTAINER_multipeermap_remove_all (peer_map, id);
1323 rps_start (struct GNUNET_SERVER_Handle *server);
1326 * This is called from GNUNET_CADET_get_peers().
1328 * It is called on every peer(ID) that cadet somehow has contact with.
1329 * We use those to initialise the sampler.
1332 init_peer_cb (void *cls,
1333 const struct GNUNET_PeerIdentity *peer,
1334 int tunnel, // "Do we have a tunnel towards this peer?"
1335 unsigned int n_paths, // "Number of known paths towards this peer"
1336 unsigned int best_path) // "How long is the best path?
1337 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
1339 struct GNUNET_SERVER_Handle *server;
1340 struct PeerOutstandingOp out_op;
1341 struct PeerContext *peer_ctx;
1343 server = (struct GNUNET_SERVER_Handle *) cls;
1346 LOG (GNUNET_ERROR_TYPE_DEBUG,
1347 "Got peer %s (at %p) from CADET (gossip_list_size: %u)\n",
1348 GNUNET_i2s (peer), peer, gossip_list_size);
1350 // maybe create a function for that
1351 peer_ctx = get_peer_ctx (peer_map, peer);
1352 if (GNUNET_NO == insert_in_sampler_scheduled (peer_ctx))
1354 out_op.op = insert_in_sampler;
1355 GNUNET_array_append (peer_ctx->outstanding_ops, peer_ctx->num_outstanding_ops, out_op);
1358 if (GNUNET_NO == insert_in_gossip_list_scheduled (peer_ctx))
1360 out_op.op = insert_in_gossip_list;
1361 GNUNET_array_append (peer_ctx->outstanding_ops, peer_ctx->num_outstanding_ops, out_op);
1364 /* Issue livelyness test on peer */
1365 (void) get_channel (peer_map, peer);
1367 // send push/pull to each of those peers?
1375 * Callback used to clean the multipeermap.
1378 peer_remove_cb (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1380 struct PeerContext *peer_ctx;
1382 peer_ctx = (struct PeerContext *) value;
1384 if ( NULL != peer_ctx->mq)
1385 GNUNET_MQ_destroy (peer_ctx->mq);
1387 if ( NULL != peer_ctx->is_live_task)
1389 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task);
1390 peer_ctx->is_live_task = NULL;
1393 if ( NULL != peer_ctx->send_channel)
1394 GNUNET_CADET_channel_destroy (peer_ctx->send_channel);
1396 if ( NULL != peer_ctx->recv_channel)
1397 GNUNET_CADET_channel_destroy (peer_ctx->recv_channel);
1399 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_remove_all (peer_map, key))
1400 LOG (GNUNET_ERROR_TYPE_WARNING, "removing peer from peer_map failed\n");
1407 * Task run during shutdown.
1413 shutdown_task (void *cls,
1414 const struct GNUNET_SCHEDULER_TaskContext *tc)
1416 LOG (GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
1418 if ( NULL != do_round_task )
1420 GNUNET_SCHEDULER_cancel (do_round_task);
1421 do_round_task = NULL;
1425 if (GNUNET_SYSERR == GNUNET_CONTAINER_multipeermap_iterate (peer_map, peer_remove_cb, NULL))
1426 LOG (GNUNET_ERROR_TYPE_WARNING,
1427 "Iterating over peers to disconnect from them was cancelled\n");
1429 GNUNET_CONTAINER_multipeermap_destroy (peer_map);
1431 GNUNET_NSE_disconnect (nse);
1432 GNUNET_CADET_disconnect (cadet_handle);
1433 GNUNET_free (own_identity);
1434 RPS_sampler_destroy ();
1435 GNUNET_array_grow (request_deltas, request_deltas_size, 0);
1436 GNUNET_array_grow (gossip_list, gossip_list_size, 0);
1437 GNUNET_array_grow (push_list, push_list_size, 0);
1438 GNUNET_array_grow (pull_list, pull_list_size, 0);
1443 * A client disconnected. Remove all of its data structure entries.
1445 * @param cls closure, NULL
1446 * @param client identification of the client
1449 handle_client_disconnect (void *cls,
1450 struct GNUNET_SERVER_Client * client)
1456 * Handle the channel a peer opens to us.
1458 * @param cls The closure
1459 * @param channel The channel the peer wants to establish
1460 * @param initiator The peer's peer ID
1461 * @param port The port the channel is being established over
1462 * @param options Further options
1465 handle_inbound_channel (void *cls,
1466 struct GNUNET_CADET_Channel *channel,
1467 const struct GNUNET_PeerIdentity *initiator,
1469 enum GNUNET_CADET_ChannelOption options)
1471 struct PeerContext *ctx;
1473 LOG (GNUNET_ERROR_TYPE_DEBUG,
1474 "New channel was established to us (Peer %s).\n",
1475 GNUNET_i2s (initiator));
1477 GNUNET_assert (NULL != channel);
1479 // we might not even store the recv_channel
1481 ctx = get_peer_ctx (peer_map, initiator);
1482 if (NULL != ctx->recv_channel)
1484 ctx->recv_channel = channel;
1487 ctx->peer_flags |= LIVING;
1489 //ctx->peer_flags = IN_OTHER_GOSSIP_LIST;
1492 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, initiator, ctx,
1493 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1495 return NULL; // TODO
1500 * This is called when a remote peer destroys a channel.
1502 * @param cls The closure
1503 * @param channel The channel being closed
1504 * @param channel_ctx The context associated with this channel
1507 cleanup_channel (void *cls,
1508 const struct GNUNET_CADET_Channel *channel,
1511 struct GNUNET_PeerIdentity *peer;
1512 struct PeerContext *peer_ctx;
1514 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel to remote peer was destroyed.\n");
1516 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info (
1517 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER);
1518 // Guess simply casting isn't the nicest way...
1519 // FIXME wait for cadet to change this function
1520 RPS_sampler_reinitialise_by_value (peer);
1522 peer_ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
1523 /* Somwewhat {ab,re}use the iterator function */
1524 (void) peer_remove_cb (peer, peer, peer_ctx);
1529 * Actually start the service.
1532 rps_start (struct GNUNET_SERVER_Handle *server)
1534 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1535 {&handle_client_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST,
1536 sizeof (struct GNUNET_RPS_CS_RequestMessage)},
1537 {&handle_client_seed, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_SEED, 0},
1541 GNUNET_SERVER_add_handlers (server, handlers);
1542 GNUNET_SERVER_disconnect_notify (server,
1543 &handle_client_disconnect,
1545 LOG (GNUNET_ERROR_TYPE_DEBUG, "Ready to receive requests from clients\n");
1548 num_hist_update_tasks = 0;
1550 do_round_task = GNUNET_SCHEDULER_add_now (&do_round, NULL);
1551 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
1553 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1560 * Process statistics requests.
1562 * @param cls closure
1563 * @param server the initialized server
1564 * @param c configuration to use
1568 struct GNUNET_SERVER_Handle *server,
1569 const struct GNUNET_CONFIGURATION_Handle *c)
1571 // TODO check what this does -- copied from gnunet-boss
1572 // - seems to work as expected
1573 GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
1575 LOG (GNUNET_ERROR_TYPE_DEBUG, "RPS started\n");
1582 own_identity = GNUNET_new (struct GNUNET_PeerIdentity);
1583 GNUNET_CRYPTO_get_peer_identity (cfg, own_identity); // TODO check return value
1584 GNUNET_assert (NULL != own_identity);
1585 LOG (GNUNET_ERROR_TYPE_DEBUG, "Own identity is %s (at %p).\n", GNUNET_i2s (own_identity), own_identity);
1588 /* Get time interval from the configuration */
1589 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
1593 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read ROUNDINTERVAL from config\n");
1594 GNUNET_SCHEDULER_shutdown ();
1598 /* Get initial size of sampler/gossip list from the configuration */
1599 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "RPS",
1601 (long long unsigned int *) &sampler_size_est_need))
1603 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to read INITSIZE from config\n");
1604 GNUNET_SCHEDULER_shutdown ();
1607 LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", sampler_size_est_need);
1613 /* connect to NSE */
1614 nse = GNUNET_NSE_connect (cfg, nse_callback, NULL);
1615 // TODO check whether that was successful
1616 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to NSE\n");
1622 peer_map = GNUNET_CONTAINER_multipeermap_create (sampler_size_est_need, GNUNET_NO);
1625 /* Initialise cadet */
1626 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1627 {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH ,
1628 sizeof (struct GNUNET_MessageHeader)},
1629 {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
1630 sizeof (struct GNUNET_MessageHeader)},
1631 {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY , 0},
1635 const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
1636 cadet_handle = GNUNET_CADET_connect (cfg,
1638 &handle_inbound_channel,
1642 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to CADET\n");
1645 /* Initialise sampler */
1646 struct GNUNET_TIME_Relative half_round_interval;
1647 struct GNUNET_TIME_Relative max_round_interval;
1649 half_round_interval = GNUNET_TIME_relative_multiply (round_interval, .5);
1650 max_round_interval = GNUNET_TIME_relative_add (round_interval, half_round_interval);
1652 RPS_sampler_init (sampler_size_est_need, max_round_interval,
1653 insertCB, NULL, removeCB, NULL);
1654 sampler_size = sampler_size_est_need;
1656 /* Initialise push and pull maps */
1661 pending_pull_reply_list = NULL;
1662 pending_pull_reply_list_size = 0;
1665 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
1666 GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, server);
1668 // TODO send push/pull to each of those peers?
1673 * The main function for the rps service.
1675 * @param argc number of arguments from the command line
1676 * @param argv command line arguments
1677 * @return 0 ok, 1 on error
1680 main (int argc, char *const *argv)
1682 return (GNUNET_OK ==
1683 GNUNET_SERVICE_run (argc,
1686 GNUNET_SERVICE_OPTION_NONE,
1687 &run, NULL)) ? 0 : 1;
1690 /* end of gnunet-service-rps.c */