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"
35 #define LOG(kind, ...) GNUNET_log(kind, __VA_ARGS__)
37 // TODO modify @brief in every file
39 // TODO take care that messages are not longer than 64k
41 // TODO check for overflows
43 // TODO align message structs
45 // TODO multipeerlist indep of gossiped list
47 // (TODO api -- possibility of getting weak random peer immideately)
49 // TODO malicious peer
51 // TODO switch Slist -> DLL
56 static const struct GNUNET_CONFIGURATION_Handle *cfg;
61 struct GNUNET_PeerIdentity *own_identity;
64 * Compare two peer identities. Taken from secretsharing.
66 * @param p1 Some peer identity.
67 * @param p2 Some peer identity.
68 * @return 1 if p1 > p2, -1 if p1 < p2 and 0 if p1 == p2.
71 peer_id_cmp (const void *p1, const void *p2)
73 return memcmp (p1, p2, sizeof (struct GNUNET_PeerIdentity));
76 /***********************************************************************
79 * WARNING: This section needs to be reviewed regarding the use of
80 * functions providing (pseudo)randomness!
81 ***********************************************************************/
83 // TODO care about invalid input of the caller (size 0 or less...)
86 * A sampler sampling PeerIDs.
91 * Min-wise linear permutation used by this sampler.
93 * This is an key later used by a hmac.
95 struct GNUNET_CRYPTO_AuthKey auth_key;
98 * The PeerID this sampler currently samples.
100 struct GNUNET_PeerIdentity *peer_id;
103 * The according hash value of this PeerID.
105 struct GNUNET_HashCode peer_id_hash;
108 * Samplers are kept in a linked list.
110 struct Sampler *next;
113 * Samplers are kept in a linked list.
115 struct Sampler *prev;
120 * A n-tuple of samplers.
125 * Number of samplers we hold.
131 * All PeerIDs in one array.
133 struct GNUNET_PeerIdentity *peer_ids;
136 * The head of the DLL.
138 struct Sampler *head;
141 * The tail of the DLL.
143 struct Sampler *tail;
148 typedef void (* SAMPLER_deleteCB) (void *cls, const struct GNUNET_PeerIdentity *id, struct GNUNET_HashCode hash);
151 * (Re)Initialise given Sampler with random min-wise independent function.
153 * In this implementation this means choosing an auth_key for later use in
156 * @param id pointer to the place where this sampler will store the PeerID.
157 * This will be overwritten.
160 SAMPLER_init(struct GNUNET_PeerIdentity *id)
164 s = GNUNET_new(struct Sampler);
166 // I guess I don't need to call GNUNET_CRYPTO_hmac_derive_key()...
167 GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_STRONG,
169 GNUNET_CRYPTO_HASH_LENGTH);
171 //s->peer_id = GNUNET_new( struct GNUNET_PeerIdentity );
172 GNUNET_assert(NULL != id);
174 memcpy(s->peer_id, own_identity, sizeof(struct GNUNET_PeerIdentity));
175 //s->peer_id = own_identity; // Maybe set to own PeerID. So we always have
176 // a valid PeerID in the sampler.
177 // Maybe take a PeerID as second argument.
178 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: initialised with PeerID %s (at %p) \n", GNUNET_i2s(s->peer_id), s->peer_id);
180 GNUNET_CRYPTO_hmac(&s->auth_key, s->peer_id,
181 sizeof(struct GNUNET_PeerIdentity),
191 * Compare two hashes.
193 * Returns if the first one is smaller then the second.
194 * Used by SAMPLER_next() to compare hashes.
197 hash_cmp(struct GNUNET_HashCode hash1, struct GNUNET_HashCode hash2)
199 return memcmp( (const void *) &hash1, (const void *) & hash2, sizeof(struct GNUNET_HashCode));
203 * Input an PeerID into the given sampler.
206 SAMPLER_next(struct Sampler *s, const struct GNUNET_PeerIdentity *id, SAMPLER_deleteCB del_cb, void *cb_cls)
207 // TODO set id in peer_ids
209 struct GNUNET_HashCode other_hash;
211 if ( id == s->peer_id )
213 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s\n",
215 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Have already PeerID %s\n",
216 GNUNET_i2s(s->peer_id));
220 GNUNET_CRYPTO_hmac(&s->auth_key,
222 sizeof(struct GNUNET_PeerIdentity),
225 if ( NULL == s->peer_id )
226 { // Or whatever is a valid way to say
227 // "we have no PeerID at the moment"
228 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s; Simply accepting (got NULL previously).\n",
230 memcpy(s->peer_id, id, sizeof(struct GNUNET_PeerIdentity));
232 s->peer_id_hash = other_hash;
234 else if ( 0 > hash_cmp(other_hash, s->peer_id_hash) )
236 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s\n",
238 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Discarding old PeerID %s\n",
239 GNUNET_i2s(s->peer_id));
241 if ( NULL != del_cb )
243 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Removing old PeerID %s with the delete callback.\n",
244 GNUNET_i2s(s->peer_id));
245 del_cb(cb_cls, s->peer_id, s->peer_id_hash);
248 memcpy(s->peer_id, id, sizeof(struct GNUNET_PeerIdentity));
250 s->peer_id_hash = other_hash;
254 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s\n",
256 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Keeping old PeerID %s\n",
257 GNUNET_i2s(s->peer_id), s->peer_id);
264 * Initialise a tuple of samplers.
267 SAMPLER_samplers_init(size_t init_size)
269 struct Samplers *samplers;
273 samplers = GNUNET_new(struct Samplers);
275 samplers->head = samplers->tail = NULL;
276 samplers->peer_ids = NULL;
277 //samplers->peer_ids = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity);
279 for ( i = 0 ; i < init_size ; i++ )
281 GNUNET_array_append(samplers->peer_ids,
285 s = SAMPLER_init(&samplers->peer_ids[i]);
286 GNUNET_CONTAINER_DLL_insert_tail(samplers->head,
290 //samplers->size = init_size;
291 GNUNET_assert(init_size == samplers->size);
297 * A fuction to update every sampler in the given list
300 SAMPLER_update_list(struct Samplers *samplers, const struct GNUNET_PeerIdentity *id,
301 SAMPLER_deleteCB del_cb, void *cb_cls)
303 struct Sampler *sampler;
305 sampler = samplers->head;
306 while ( NULL != sampler->next )
308 SAMPLER_next(sampler, id, del_cb, cb_cls);
314 * Get one random peer out of the sampled peers.
316 * We might want to reinitialise this sampler after giving the
317 * corrsponding peer to the client.
319 const struct GNUNET_PeerIdentity*
320 SAMPLER_get_rand_peer (struct Samplers *samplers)
322 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER_get_rand_peer:\n");
324 if ( 0 == samplers->size )
326 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: List empty - Returning own PeerID %s\n", GNUNET_i2s(own_identity));
332 struct Sampler *iter;
334 const struct GNUNET_PeerIdentity *peer;
337 * Choose the index of the peer we want to give back
338 * at random from the interval of the sampler list
340 index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
342 // TODO check that it does not overflow
343 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: Length of Slist: %" PRIu64 ", index: %" PRIu64 "\n", samplers->size, index);
345 iter = samplers->head;
346 for ( i = 0 ; i < index ; i++ )
348 if ( NULL == iter->next )
350 iter = samplers->head;
354 // TODO something missing?
356 peer = iter->peer_id;
357 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: Returning PeerID %s\n", GNUNET_i2s(peer));
358 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: (own ID: %s)\n", GNUNET_i2s(own_identity));
365 * Get n random peers out of the sampled peers.
367 * We might want to reinitialise this sampler after giving the
368 * corrsponding peer to the client.
369 * Random with or without consumption?
371 const struct GNUNET_PeerIdentity* // TODO give back simple array
372 SAMPLER_get_n_rand_peers (struct Samplers *samplers, uint64_t n)
374 // TODO check if we have too much (distinct) sampled peers
375 // If we are not ready yet maybe schedule for later
376 struct GNUNET_PeerIdentity *peers;
379 peers = GNUNET_malloc(n * sizeof(struct GNUNET_PeerIdentity));
381 for ( i = 0 ; i < n ; i++ ) {
382 //peers[i] = SAMPLER_get_rand_peer(samplers);
383 memcpy(&peers[i], SAMPLER_get_rand_peer(samplers), sizeof(struct GNUNET_PeerIdentity));
386 // TODO something else missing?
391 * Counts how many Samplers currently hold a given PeerID.
394 SAMPLER_count_id ( struct Samplers *samplers, struct GNUNET_PeerIdentity *id )
396 struct Sampler *iter;
399 iter = samplers->head;
401 while ( NULL != iter )
403 if ( peer_id_cmp( iter->peer_id, id) )
411 * Gow the size of the tuple of samplers.
414 SAMPLER_samplers_grow (struct Samplers * samplers, size_t new_size)
417 struct Sampler *sampler;
419 if ( new_size > samplers->size )
421 GNUNET_array_grow(samplers->peer_ids, samplers->size, new_size);
422 for ( i = 0 ; i < new_size - samplers->size ; i++ )
424 sampler = SAMPLER_init(&samplers->peer_ids[samplers->size + i]);
425 GNUNET_CONTAINER_DLL_insert_tail(samplers->head, samplers->tail, sampler);
428 else if ( new_size < samplers->size )
430 for ( i = 0 ; i < samplers->size - new_size ; i++)
432 // TODO call delCB on elem?
433 GNUNET_CONTAINER_DLL_remove(samplers->head, samplers->tail, samplers->tail);
435 GNUNET_array_grow(samplers->peer_ids, samplers->size, new_size);
438 samplers->size = new_size;
441 /***********************************************************************
443 ***********************************************************************/
447 /***********************************************************************
448 * Housekeeping with peers
449 ***********************************************************************/
452 * Struct used to store the context of a connected client.
457 * The message queue to communicate with the client.
459 struct GNUNET_MQ_Handle *mq;
463 * Used to keep track in what lists single peerIDs are.
465 enum in_list_flag // probably unneeded
467 in_other_sampler_list = 0x1,
468 in_other_gossip_list = 0x2, // unneeded?
469 in_own_sampler_list = 0x4,
470 in_own_gossip_list = 0x8 // unneeded?
474 * Struct used to keep track of other peer's status
476 * This is stored in a multipeermap.
481 * In own gossip/sampler list, in other's gossip/sampler list
483 uint32_t in_flags; // unneeded?
486 * Message queue open to client
488 struct GNUNET_MQ_Handle *mq;
491 * Channel open to client.
493 struct GNUNET_CADET_Channel *to_channel;
496 * Channel open from client.
498 struct GNUNET_CADET_Channel *from_channel; // unneeded
501 * This is pobably followed by 'statistical' data (when we first saw
502 * him, how did we get his ID, how many pushes (in a timeinterval),
507 /***********************************************************************
508 * /Housekeeping with peers
509 ***********************************************************************/
512 * Set of all peers to keep track of them.
514 static struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
517 // -- gossip list length --
518 // Depends on the (estimated) size of the
519 // network. - Initial size might be the
520 // number of peers cadet provides.
521 // TODO other events to grow/shrink size?
526 struct Samplers *sampler_list; // TODO rename to sampler_list
529 * Sampler list size // TODO get rid of that
531 * Adapts to the nse. Size should be in BigTheta(network_size)^(1/3).
533 size_t sampler_list_size;
537 * The gossiped list of peers.
539 struct GNUNET_PeerIdentity *gossip_list;
542 * Size of the gossiped list
544 unsigned int gossip_list_size;
547 * Min size of the gossip list
549 uint64_t gossip_list_min_size;
552 // * Max size of the gossip list
554 // * This will probably be left to be set by the client.
556 //uint64_t gossip_list_max_size;
560 * The estimated size of the network.
562 * Influenced by the stdev.
569 * Percentage of total peer number in the gossip list
570 * to send random PUSHes to
575 * Percentage of total peer number in the gossip list
576 * to send random PULLs to
581 * The percentage gamma of history updates.
582 * Simply 1 - alpha - beta
589 * Identifier for the main task that runs periodically.
591 GNUNET_SCHEDULER_TaskIdentifier do_round_task;
594 * Time inverval the do_round task runs in.
596 struct GNUNET_TIME_Relative round_interval;
601 * List to store peers received through pushes temporary.
603 struct GNUNET_PeerIdentity *push_list;
606 * Size of the push_list;
608 unsigned int push_list_size;
609 //size_t push_list_size;
612 * List to store peers received through pulls temporary.
614 struct GNUNET_PeerIdentity *pull_list;
617 * Size of the pull_list;
619 unsigned int pull_list_size;
620 //size_t pull_list_size;
626 struct GNUNET_NSE_Handle *nse;
631 struct GNUNET_CADET_Handle *cadet_handle;
634 /***********************************************************************
636 ***********************************************************************/
639 * Get random peer from the gossip list.
641 struct GNUNET_PeerIdentity *
642 get_rand_gossip_peer()
645 struct GNUNET_PeerIdentity *peer;
647 // TODO find a better solution.
648 // FIXME if we have only own ID in gossip list this will block
649 // but then we might have a problem nevertheless ?
654 * Choose the index of the peer we want to return
655 * at random from the interval of the gossip list
657 index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
660 peer = &(gossip_list[index]);
661 } while ( own_identity == peer || NULL == peer );
667 * Get the message queue of a specific peer.
669 * If we already have a message queue open to this client,
670 * simply return it, otherways create one.
672 struct GNUNET_MQ_Handle *
673 get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, struct GNUNET_PeerIdentity *peer_id)
675 struct peer_context *ctx;
676 struct GNUNET_MQ_Handle * mq;
677 struct GNUNET_CADET_Channel *channel;
679 if ( GNUNET_OK != GNUNET_CONTAINER_multipeermap_contains( peer_map, peer_id ) ) {
681 channel = GNUNET_CADET_channel_create(cadet_handle, NULL, peer_id,
682 GNUNET_RPS_CADET_PORT,
683 GNUNET_CADET_OPTION_RELIABLE);
684 mq = GNUNET_CADET_mq_create(channel);
686 ctx = GNUNET_malloc(sizeof(struct peer_context));
688 ctx->to_channel = channel;
691 GNUNET_CONTAINER_multipeermap_put(peer_map, peer_id, ctx,
692 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
694 ctx = GNUNET_CONTAINER_multipeermap_get(peer_map, peer_id);
695 if ( NULL == ctx->mq ) {
696 if ( NULL == ctx->to_channel ) {
697 channel = GNUNET_CADET_channel_create(cadet_handle, NULL, peer_id,
698 GNUNET_RPS_CADET_PORT,
699 GNUNET_CADET_OPTION_RELIABLE);
700 ctx->to_channel = channel;
703 mq = GNUNET_CADET_mq_create(ctx->to_channel);
712 /***********************************************************************
714 ***********************************************************************/
717 * Function called by NSE.
719 * Updates sizes of sampler list and gossip list and adapt those lists
723 nse_callback(void *cls, struct GNUNET_TIME_Absolute timestamp, double logestimate, double std_dev)
726 //double scale; // TODO this might go gloabal/config
728 LOG(GNUNET_ERROR_TYPE_DEBUG, "Received a ns estimate - logest: %f, std_dev: %f\n", logestimate, std_dev);
730 estimate = 1 << (uint64_t) round(logestimate);
731 // GNUNET_NSE_log_estimate_to_n (logestimate);
732 estimate = pow(estimate, 1./3);// * (std_dev * scale); // TODO add
733 if ( 0 < estimate ) {
734 LOG(GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
737 LOG(GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
742 * Handle RPS request from the client.
745 * @param client identification of the client
746 * @param message the actual message
750 handle_cs_request (void *cls,
751 struct GNUNET_SERVER_Client *client,
752 const struct GNUNET_MessageHeader *message)
754 LOG(GNUNET_ERROR_TYPE_DEBUG, "Client requested (a) random peer(s).\n");
756 struct GNUNET_RPS_CS_RequestMessage *msg;
757 //unsigned int n_arr[sampler_list_size];// =
758 //GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list_size);
759 //struct GNUNET_MQ_Handle *mq;
760 struct client_ctx *cli_ctx;
761 struct GNUNET_MQ_Envelope *ev;
762 struct GNUNET_RPS_CS_ReplyMessage *out_msg;
767 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
768 // Does not work because the compiler seems not to find it.
769 cli_ctx = GNUNET_SERVER_client_get_user_context(client, struct client_ctx);
770 if ( NULL == cli_ctx ) {
771 cli_ctx = GNUNET_new(struct client_ctx);
772 cli_ctx->mq = GNUNET_MQ_queue_for_server_client(client);
773 GNUNET_SERVER_client_set_user_context(client, cli_ctx);
776 //mq = GNUNET_MQ_queue_for_server_client(client);
778 // TODO How many peers do we give back?
779 // Wait until we have enough random peers?
781 ev = GNUNET_MQ_msg_extra(out_msg,
782 GNUNET_ntohll(msg->num_peers) * sizeof(struct GNUNET_PeerIdentity),
783 GNUNET_MESSAGE_TYPE_RPS_CS_REPLY);
784 out_msg->num_peers = GNUNET_ntohll(msg->num_peers);
786 num_peers = GNUNET_ntohll(msg->num_peers);
787 //&out_msg[1] = SAMPLER_get_n_rand_peers(sampler_list, num_peers);
788 for ( i = 0 ; i < num_peers ; i++ ) {
789 memcpy(&out_msg[1] + i * sizeof(struct GNUNET_PeerIdentity),
790 SAMPLER_get_rand_peer(sampler_list),
791 sizeof(struct GNUNET_PeerIdentity));
794 GNUNET_MQ_send(cli_ctx->mq, ev);
795 //GNUNET_MQ_send(mq, ev);
796 //GNUNET_MQ_destroy(mq);
798 GNUNET_SERVER_receive_done (client,
803 * Handle a PUSH message from another peer.
805 * Check the proof of work and store the PeerID
806 * in the temporary list for pushed PeerIDs.
809 * @param channel The channel the PUSH was received over
810 * @param channel_ctx The context associated with this channel
811 * @param msg The message header
814 handle_peer_push (void *cls,
815 struct GNUNET_CADET_Channel *channel,
817 const struct GNUNET_MessageHeader *msg)
819 LOG(GNUNET_ERROR_TYPE_DEBUG, "PUSH received\n");
821 struct GNUNET_PeerIdentity *peer;
823 // TODO check the proof of work
824 // and check limit for PUSHes
825 // IF we count per peer PUSHes
826 // maybe remove from gossip/sampler list
828 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info( channel, GNUNET_CADET_OPTION_PEER );
830 /* Add the sending peer to the push_list */
831 GNUNET_array_append(push_list, push_list_size, *peer);
838 * Handle PULL REQUEST request message from another peer.
840 * Reply with the gossip list of PeerIDs.
843 * @param channel The channel the PUSH was received over
844 * @param channel_ctx The context associated with this channel
845 * @param msg The message header
848 handle_peer_pull_request (void *cls,
849 struct GNUNET_CADET_Channel *channel,
851 const struct GNUNET_MessageHeader *msg)
854 struct GNUNET_PeerIdentity *peer;
855 struct GNUNET_MQ_Handle *mq;
856 //struct GNUNET_RPS_P2P_PullRequestMessage *in_msg;
857 struct GNUNET_MQ_Envelope *ev;
858 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
860 // TODO find some way to keep one peer from spamming with pull requests
861 // allow only one request per time interval ?
862 // otherwise remove from peerlist?
864 peer = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info(channel, GNUNET_CADET_OPTION_PEER);
865 LOG(GNUNET_ERROR_TYPE_DEBUG, "PULL REQUEST from peer %s received\n", GNUNET_i2s(peer));
867 mq = GNUNET_CADET_mq_create(channel); // TODO without mq?
868 //mq = get_mq(peer_map, peer);
870 //in_msg = (struct GNUNET_RPS_P2P_PullRequestMessage *) msg;
871 // TODO how many peers do we actually send?
872 // GNUNET_ntohll(in_msg->num_peers)
873 ev = GNUNET_MQ_msg_extra(out_msg,
874 gossip_list_size * sizeof(struct GNUNET_PeerIdentity),
875 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
876 out_msg->num_peers = GNUNET_htonll(gossip_list_size);
877 memcpy(&out_msg[1], gossip_list,
878 gossip_list_size * sizeof(struct GNUNET_PeerIdentity));
880 GNUNET_MQ_send(mq, ev);
882 GNUNET_MQ_destroy(mq);
889 * Handle PULL REPLY message from another peer.
891 * Check whether we sent a corresponding request and
892 * whether this reply is the first one.
895 * @param channel The channel the PUSH was received over
896 * @param channel_ctx The context associated with this channel
897 * @param msg The message header
900 handle_peer_pull_reply (void *cls,
901 struct GNUNET_CADET_Channel *channel,
903 const struct GNUNET_MessageHeader *msg)
905 LOG(GNUNET_ERROR_TYPE_DEBUG, "PULL REPLY received\n");
907 struct GNUNET_RPS_P2P_PullReplyMessage *in_msg;
908 struct GNUNET_PeerIdentity *peers;
911 // TODO check that we sent a request and that it is the first reply
913 in_msg = (struct GNUNET_RPS_P2P_PullReplyMessage *) msg;
914 peers = (struct GNUNET_PeerIdentity *) &msg[1];
915 for ( i = 0 ; i < GNUNET_ntohll(in_msg->num_peers) ; i++ ) {
916 GNUNET_array_append(pull_list, pull_list_size, peers[i]);
920 // TODO maybe a disconnect happens here
927 * Callback called when a Sampler is updated.
930 delete_cb (void *cls, struct GNUNET_PeerIdentity *id, struct GNUNET_HashCode hash)
934 s = SAMPLER_count_id(sampler_list, id);
937 GNUNET_CONTAINER_multipeermap_remove_all( peer_map, id);
943 * Send out PUSHes and PULLs.
945 * This is executed regylary.
948 do_round(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
950 LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to execute next round\n");
953 //unsigned int *n_arr;
954 struct GNUNET_RPS_P2P_PushMessage *push_msg;
955 struct GNUNET_RPS_P2P_PullRequestMessage *pull_msg; // FIXME Send empty message
956 struct GNUNET_MQ_Envelope *ev;
957 struct GNUNET_PeerIdentity *peer;
959 // TODO print lists, ...
960 // TODO cleanup peer_map
963 /* If the NSE has changed adapt the lists accordingly */
964 // TODO check nse == 0!
965 LOG(GNUNET_ERROR_TYPE_DEBUG, "Checking size estimate.\n");
966 SAMPLER_samplers_grow(sampler_list, est_size);
968 GNUNET_array_grow(gossip_list, gossip_list_size, est_size); // FIXME Do conversion correct or change type
970 gossip_list_size = sampler_list_size = est_size;
975 /* Would it make sense to have one shuffeled gossip list and then
976 * to send PUSHes to first alpha peers, PULL requests to next beta peers and
977 * use the rest to update sampler? */
980 //n_arr = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) gossip_list_size);
981 LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to send pushes to %f (%f * %" PRIu64 ") peers.\n",
982 alpha * gossip_list_size, alpha, gossip_list_size);
983 for ( i = 0 ; i < alpha * gossip_list_size ; i++ ) { // TODO compute length
984 peer = get_rand_gossip_peer();
985 // TODO check NULL == peer
986 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sending PUSH to peer %s of gossiped list.\n", GNUNET_i2s(peer));
988 ev = GNUNET_MQ_msg(push_msg, GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
989 //ev = GNUNET_MQ_msg_extra();
990 /* TODO Compute proof of work here
992 push_msg->placeholder = 0;
993 GNUNET_MQ_send( get_mq(peer_map, peer), ev );
995 // TODO modify in_flags of respective peer?
999 /* Send PULL requests */
1001 //n_arr = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int) sampler_list_size);
1002 LOG(GNUNET_ERROR_TYPE_DEBUG, "Going to send pulls to %f (%f * %" PRIu64 ") peers.\n",
1003 beta * gossip_list_size, beta, gossip_list_size);
1004 for ( i = 0 ; i < beta * gossip_list_size ; i++ ){ // TODO compute length
1005 peer = get_rand_gossip_peer();
1006 // TODO check NULL == peer
1007 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sending PULL request to peer %s of gossiped list.\n", GNUNET_i2s(peer));
1009 ev = GNUNET_MQ_msg(pull_msg, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
1010 //ev = GNUNET_MQ_msg_extra();
1011 pull_msg->placeholder = 0;
1012 GNUNET_MQ_send( get_mq(peer_map, peer), ev );
1013 // TODO modify in_flags of respective peer?
1019 /* Update gossip list */
1023 if ( push_list_size <= alpha * gossip_list_size &&
1024 push_list_size != 0 &&
1025 pull_list_size != 0 ) {
1026 LOG(GNUNET_ERROR_TYPE_DEBUG, "Update of the gossip list. ()\n");
1028 for ( i = 0 ; i < alpha * gossip_list_size ; i++ ) { // TODO use SAMPLER_get_n_rand_peers
1029 /* Update gossip list with peers received through PUSHes */
1030 index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
1032 gossip_list[i] = push_list[index];
1033 // TODO change the in_flags accordingly
1036 for ( i = 0 ; i < beta * gossip_list_size ; i++ ) {
1037 /* Update gossip list with peers received through PULLs */
1038 tmp_index = i + round(alpha * gossip_list_size);
1039 index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
1041 gossip_list[tmp_index] = pull_list[index];
1042 // TODO change the in_flags accordingly
1045 for ( i = 0 ; i < (1 - (alpha + beta)) * gossip_list_size ; i++ ) {
1046 /* Update gossip list with peers from history */
1047 tmp_index = i + round((alpha + beta) * gossip_list_size);
1048 index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
1049 sampler_list->size);
1050 gossip_list[tmp_index] = sampler_list->peer_ids[index];
1051 // TODO change the in_flags accordingly
1055 LOG(GNUNET_ERROR_TYPE_DEBUG, "No update of the gossip list. ()\n");
1057 // TODO independent of that also get some peers from CADET_get_peers()?
1061 /* Update samplers */
1063 for ( i = 0 ; i < push_list_size ; i++ )
1065 SAMPLER_update_list(sampler_list, &push_list[i], NULL, NULL);
1066 // TODO set in_flag?
1069 for ( i = 0 ; i < pull_list_size ; i++ )
1071 SAMPLER_update_list(sampler_list, &pull_list[i], NULL, NULL);
1072 // TODO set in_flag?
1076 // TODO go over whole peer_map and do cleanups
1077 // delete unneeded peers, set in_flags, check channel/mq
1080 /* Empty push/pull lists */
1081 GNUNET_array_grow(push_list, push_list_size, 0);
1083 GNUNET_array_grow(pull_list, pull_list_size, 0);
1087 /* Schedule next round */
1089 do_round_task = GNUNET_SCHEDULER_add_delayed( round_interval, &do_round, NULL );
1090 LOG(GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
1094 rps_start (struct GNUNET_SERVER_Handle *server);
1097 * This is called from GNUNET_CADET_get_peers().
1099 * It is called on every peer(ID) that cadet somehow has contact with.
1100 * We use those to initialise the sampler.
1103 init_peer_cb (void *cls,
1104 const struct GNUNET_PeerIdentity *peer,
1105 int tunnel, // "Do we have a tunnel towards this peer?"
1106 unsigned int n_paths, // "Number of known paths towards this peer"
1107 unsigned int best_path) // "How long is the best path?
1108 // (0 = unknown, 1 = ourselves, 2 = neighbor)"
1110 // FIXME use the magic 0000 PeerID
1111 if ( NULL != peer ) {
1112 LOG(GNUNET_ERROR_TYPE_DEBUG, "Got peer %s (at %p) from CADET\n", GNUNET_i2s(peer), peer);
1113 SAMPLER_update_list(sampler_list, peer, NULL, NULL);
1114 if ( GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains( peer_map, peer ) ) {
1116 struct peer_context *ctx;
1118 ctx = GNUNET_malloc(sizeof(struct peer_context));
1121 ctx->to_channel = NULL;
1122 ctx->from_channel = NULL;
1123 GNUNET_CONTAINER_multipeermap_put( peer_map, peer, ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1127 i = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG, gossip_list_size);
1128 gossip_list[i] = *peer;
1129 // TODO send push/pull to each of those peers?
1131 rps_start( (struct GNUNET_SERVER_Handle *) cls);
1139 * Task run during shutdown.
1145 shutdown_task (void *cls,
1146 const struct GNUNET_SCHEDULER_TaskContext *tc)
1148 LOG(GNUNET_ERROR_TYPE_DEBUG, "RPS is going down\n");
1150 if ( GNUNET_SCHEDULER_NO_TASK != do_round_task )
1152 GNUNET_SCHEDULER_cancel (do_round_task);
1153 do_round_task = GNUNET_SCHEDULER_NO_TASK;
1156 GNUNET_NSE_disconnect(nse);
1157 GNUNET_CADET_disconnect(cadet_handle);
1158 GNUNET_free(own_identity);
1159 //GNUNET_free(round_interval);
1160 //GNUNET_free(est_size);
1161 //GNUNET_free(gossip_list_size);
1162 //GNUNET_free(sampler_list_size);
1163 GNUNET_free(gossip_list);
1164 // TODO for i in sampler_list free sampler
1165 // TODO destroy sampler_list
1166 // TODO destroy push/pull_list
1167 // TODO delete global data
1172 * A client disconnected. Remove all of its data structure entries.
1174 * @param cls closure, NULL
1175 * @param client identification of the client
1178 handle_client_disconnect (void *cls,
1179 struct GNUNET_SERVER_Client * client)
1181 // TODO reinitialise that sampler
1185 * Handle the channel a peer opens to us.
1187 * @param cls The closure
1188 * @param channel The channel the peer wants to establish
1189 * @param initiator The peer's peer ID
1190 * @param port The port the channel is being established over
1191 * @param options Further options
1194 handle_inbound_channel (void *cls,
1195 struct GNUNET_CADET_Channel *channel,
1196 const struct GNUNET_PeerIdentity *initiator,
1198 enum GNUNET_CADET_ChannelOption options)
1200 LOG(GNUNET_ERROR_TYPE_DEBUG, "New channel was established to us.\n");
1202 GNUNET_assert( NULL != channel );
1204 // TODO we might even not store the from_channel
1206 if ( GNUNET_CONTAINER_multipeermap_contains( peer_map, initiator ) ) {
1207 ((struct peer_context *) GNUNET_CONTAINER_multipeermap_get( peer_map, initiator ))->from_channel = channel;
1208 // FIXME there might already be an established channel
1210 struct peer_context *ctx;
1212 ctx = GNUNET_malloc( sizeof(struct peer_context));
1213 ctx->in_flags = in_other_gossip_list;
1214 ctx->mq = NULL; // TODO create mq?
1215 ctx->from_channel = channel;
1217 GNUNET_CONTAINER_multipeermap_put( peer_map, initiator, ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1219 return NULL; // TODO
1223 * This is called when a remote peer destroys a channel.
1225 * @param cls The closure
1226 * @param channel The channel being closed
1227 * @param channel_ctx The context associated with this channel
1230 cleanup_channel(void *cls,
1231 const struct GNUNET_CADET_Channel *channel,
1234 LOG(GNUNET_ERROR_TYPE_DEBUG, "Channel was destroyed by remote peer.\n");
1238 * Actually start the service.
1241 rps_start (struct GNUNET_SERVER_Handle *server)
1243 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1244 {&handle_cs_request, NULL, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST, 0},
1248 GNUNET_SERVER_add_handlers (server, handlers);
1249 GNUNET_SERVER_disconnect_notify (server,
1250 &handle_client_disconnect,
1252 LOG(GNUNET_ERROR_TYPE_DEBUG, "Ready to receive requests from clients\n");
1256 do_round_task = GNUNET_SCHEDULER_add_delayed( round_interval, &do_round, NULL);
1257 LOG(GNUNET_ERROR_TYPE_DEBUG, "Scheduled first round\n");
1259 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1267 * Process statistics requests.
1269 * @param cls closure
1270 * @param server the initialized server
1271 * @param c configuration to use
1275 struct GNUNET_SERVER_Handle *server,
1276 const struct GNUNET_CONFIGURATION_Handle *c)
1278 // TODO check what this does -- copied from gnunet-boss
1279 // - seems to work as expected
1280 GNUNET_log_setup("rps", GNUNET_error_type_to_string(GNUNET_ERROR_TYPE_DEBUG), NULL);
1282 LOG(GNUNET_ERROR_TYPE_DEBUG, "RPS started\n");
1287 own_identity = GNUNET_new(struct GNUNET_PeerIdentity);
1289 GNUNET_CRYPTO_get_peer_identity(cfg, own_identity); // TODO check return value
1291 LOG(GNUNET_ERROR_TYPE_DEBUG, "Own identity is %s (at %p).\n", GNUNET_i2s(own_identity), own_identity);
1295 /* Get time interval from the configuration */
1296 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "RPS",
1300 LOG(GNUNET_ERROR_TYPE_DEBUG, "Failed to read ROUNDINTERVAL from config\n");
1301 GNUNET_SCHEDULER_shutdown();
1305 /* Get initial size of sampler/gossip list from the configuration */
1306 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "RPS",
1308 (long long unsigned int *) &est_size))
1310 LOG(GNUNET_ERROR_TYPE_DEBUG, "Failed to read INITSIZE from config\n");
1311 GNUNET_SCHEDULER_shutdown();
1314 LOG(GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", est_size);
1316 gossip_list_size = sampler_list_size = est_size; // TODO rename est_size
1321 static unsigned int tmp = 0;
1323 GNUNET_array_grow(gossip_list, tmp, gossip_list_size);
1327 /* connect to NSE */
1328 nse = GNUNET_NSE_connect(cfg, nse_callback, NULL);
1329 // TODO check whether that was successful
1330 // TODO disconnect on shutdown
1331 LOG(GNUNET_ERROR_TYPE_DEBUG, "Connected to NSE\n");
1336 // TODO initialise thresholds - ?
1338 ///* Get alpha from the configuration */
1339 //if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_float (cfg, "RPS",
1343 // LOG(GNUNET_ERROR_TYPE_DEBUG, "No ALPHA specified in the config\n");
1345 //LOG(GNUNET_ERROR_TYPE_DEBUG, "ALPHA is %f\n", alpha);
1347 ///* Get beta from the configuration */
1348 //if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_float (cfg, "RPS",
1352 // LOG(GNUNET_ERROR_TYPE_DEBUG, "No BETA specified in the config\n");
1354 //LOG(GNUNET_ERROR_TYPE_DEBUG, "BETA is %f\n", beta);
1359 peer_map = GNUNET_CONTAINER_multipeermap_create(est_size, GNUNET_NO);
1362 /* Initialise sampler and gossip list */
1364 sampler_list = SAMPLER_samplers_init(est_size);
1371 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1372 {&handle_peer_push , GNUNET_MESSAGE_TYPE_RPS_PP_PUSH , 0},
1373 {&handle_peer_pull_request, GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST, 0},
1374 {&handle_peer_pull_reply , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY , 0},
1378 const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
1379 cadet_handle = GNUNET_CADET_connect(cfg,
1381 &handle_inbound_channel,
1385 LOG(GNUNET_ERROR_TYPE_DEBUG, "Connected to CADET\n");
1388 LOG(GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
1389 GNUNET_CADET_get_peers(cadet_handle, &init_peer_cb, server);
1390 // FIXME use magic 0000 PeerID to _start_ the service
1392 // TODO send push/pull to each of those peers?
1397 * The main function for the rps service.
1399 * @param argc number of arguments from the command line
1400 * @param argv command line arguments
1401 * @return 0 ok, 1 on error
1404 main (int argc, char *const *argv)
1406 return (GNUNET_OK ==
1407 GNUNET_SERVICE_run (argc,
1410 GNUNET_SERVICE_OPTION_NONE,
1411 &run, NULL)) ? 0 : 1;
1414 /* end of gnunet-service-rps.c */